Debian Codegraph @

demo/menu/ComboDemo.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 module Main where
3
4 import Graphics.UI.Gtk
5 import Data.List ( findIndex )
6 import Control.Monad.IO.Class (MonadIO(..))
7 import qualified Data.Text as T
8
9 main = do
10 initGUI
11
12 win <- windowNew
13 on win deleteEvent $ liftIO mainQuit >> return False
14
15 combo <- comboBoxNewWithEntry
16 comboBoxSetModelText combo
17
18 mapM_ (comboBoxAppendText combo)
19 (T.words "ice-cream turkey pasta sandwich steak")
20
21 -- select the first item
22 comboBoxSetActive combo 0
23
24 -- Get the entry widget that the ComboBoxEntry uses.
25 (Just w) <- binGetChild combo
26 let entry = castToEntry w
27
28 -- Whenever the user has completed editing the text, append the new
29 -- text to the store unless it's already in there.
30 on entry entryActivated $ do
31 str <- entryGetText entry
32 store <- comboBoxGetModelText combo
33 elems <- listStoreToList store
34 comboBoxSetActive combo (-1)
35 idx <- case (findIndex ((==) str) elems) of
36 Just idx -> return idx
37 Nothing -> listStoreAppend store str
38 comboBoxSetActive combo idx
39 return ()
40
41 containerAdd win combo
42
43 widgetShowAll win
44 mainGUI