Created
December 25, 2011 10:33
-
-
Save wavewave/1519081 to your computer and use it in GitHub Desktop.
gtk2hs: ListStore and TreeView example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Main where | |
| {- an example how to select from a list | |
| not satisfactory yet: | |
| - there should be a simpler way to render a simple list | |
| - i could not convert the model i got back to a list | |
| from which to get the value | |
| - the interface offers a great number of functions | |
| and it is very difficult to find which ones are | |
| really needed for simple tasks | |
| -} | |
| import Graphics.UI.Gtk | |
| import Graphics.UI.Gtk.ModelView as Model | |
| import Control.Monad.Trans | |
| main :: IO () | |
| main = do | |
| initGUI -- is start | |
| window <- windowNew | |
| list <- listStoreNew ["Vince", "Jhen", "Chris", "Sharon"] | |
| treeview <- Model.treeViewNewWithModel list | |
| Model.treeViewSetHeadersVisible treeview True | |
| -- there should be a simpler way to render a list as the following! | |
| col <- Model.treeViewColumnNew | |
| Model.treeViewColumnSetTitle col "colTitle" | |
| renderer <- Model.cellRendererTextNew | |
| listStoreAppend list "dkdk" | |
| Model.cellLayoutPackStart col renderer False | |
| Model.cellLayoutSetAttributes col renderer list | |
| $ \ind -> [Model.cellText := ind] | |
| Model.treeViewAppendColumn treeview col | |
| tree <- Model.treeViewGetSelection treeview | |
| Model.treeSelectionSetMode tree SelectionBrowse --Multiple | |
| Model.onSelectionChanged tree (oneSelection list tree) | |
| vbox <- vBoxNew False 0 | |
| buttonAppend <- buttonNewWithLabel "Append" | |
| boxPackStart vbox treeview PackGrow 0 | |
| boxPackStart vbox buttonAppend PackNatural 0 | |
| containerAdd window vbox | |
| buttonAppend `on` buttonActivated $ do | |
| putStrLn "button clicked" | |
| addToList list "123" | |
| set window [ windowDefaultWidth := 100 | |
| , windowDefaultHeight := 200 | |
| -- , containerChild := treeview | |
| ] | |
| onDestroy window mainQuit | |
| widgetShowAll window | |
| mainGUI | |
| return () | |
| addToList :: ListStore String -> String -> IO () | |
| addToList list str = do | |
| listStoreAppend list str | |
| return () | |
| oneSelection :: ListStore String -> Model.TreeSelection -> IO () | |
| oneSelection list tree = do | |
| sel <- Model.treeSelectionGetSelectedRows tree | |
| let s = head (head sel) | |
| v <- Model.listStoreGetValue list s | |
| putStrLn $ "selected " ++ v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment