packages feed

AutoForms-0.4.0: src/Graphics/UI/AF/WxForm/WxEnumeration.hs

module Graphics.UI.AF.WxForm.WxEnumeration
    ( enumeration
    )
where

import Graphics.UI.AF.WxForm.EditorComponent
import Graphics.UI.AF.General

import Graphics.UI.WX hiding (value, entry, Parent, items, label)
import qualified Graphics.UI.WX as WX (items)
import List (findIndex)

enumeration :: (Show a)
            => String                    -- ^Label for the resulting 'EC'.
            -> [(String, a, a -> Bool)]  -- ^ValueName, Value, isValue
            -> a                         -- ^The initial value in 'EC' a () (see return value)
            -> EC a
enumeration typeName items y = mapValue intToA (const aToInt) $ builderToCom' $
    let enumSize      = sum $ map (+ bottomAndSpaceSize) $ map length itemNames
        bottomAndSpaceSize = 3
    in do comH <- if enumSize < 40
                     then box Horizontal
                     else dropdownChoice
          return comH
    where
    
    intToA sel = itemValues !! sel
    aToInt x   = maybe 0 id (List.findIndex (== True) $ (map (\f -> f x) isItems))

    enumLabel = PriLabel GoodConstr typeName
    box direction =
        do Parent w <- getPanel
           r1 <- io $ radioBox w direction itemNames [ ]
           let gui = (SelfContained (\label' -> do set r1 [text := label']
                                                   return $ hfill $ widget r1
                                    )
                      enumLabel
                     )
           addGuiAndOnSelect r1 gui
    
    dropdownChoice =
        do Parent w <- getPanel
           r1 <- io $ choice w [WX.items := itemNames]
           let gui     = singleGui enumLabel r1 hfill
           addGuiAndOnSelect r1 gui
    
    addGuiAndOnSelect enum gui =
        do (comH, parms) <- addCustomGui (aToInt y) (setSelection enum) enum gui
           io $ set enum [ on select := get enum selection >>= (testInputParm' parms) SetOnReject ]
           return comH
    
    setSelection enum x = set enum [ selection := x]
    (itemNames, itemValues, isItems) = unzip3 items


{- Old enumeration (20071216)

-- |Constructs an enumerated EC. The GUI will be a radioBox or a drop-down choice.
enumeration :: (Show a)
            => String          -- ^Label for the resulting 'EC'.
            -> [(String, a, a -> Bool)]   -- ^ValueName, Value, isValue
            -> a               -- ^The initial value in 'EC' a () (see return value)
            -> EC a
enumeration typeName items y = builderToCom' $
    let enumSize      = sum $ map (+ bottomAndSpaceSize) $ map length itemNames
        bottomAndSpaceSize = 3
    in do comH <- if enumSize < 40
                     then box Horizontal
                     else dropdownChoice
          return comH
    where
    
    enumLabel = PriLabel GoodConstr typeName
    box direction =
        do Parent w <- getPanel
           r1 <- io $ radioBox w direction itemNames [ ]
           let gui = (SelfContained (\label -> do set r1 [text := label]
                                                  return $ hfill $ widget r1
                                    )
                      enumLabel
                     )
           foo r1 gui
    
    dropdownChoice =
        do Parent w <- getPanel
           r1 <- io $ choice w [WX.items := itemNames]
           let gui     = singleGui enumLabel r1 hfill
           foo r1 gui
    
    foo enum gui =
        do lastSelection <- io $ varCreate 0
           (comH, parms) <- addCustomGuiNoEquals y (setSelection enum lastSelection) enum gui
           io $ set enum [ on select := do sel <- get enum selection
                                           last <- varGet lastSelection
                                           when (sel /= last)
                                                (getSelection enum >>= (testInputParm' parms) SetOnReject)
                         ]
           return comH
    
    getSelection enum = do sel <- get enum selection
                           return (itemValues !! sel)
    setSelection enum lastSelection x =
        maybe (return())
                                (\i -> set enum [ selection := i] >> varSet lastSelection i )
                                (List.findIndex (== True) $ (map (\f -> f x) isItems))
    (itemNames, itemValues, isItems) = unzip3 items


-}