Talk:Gtk2Hs/Tutorials/TreeView
I don't know if any of these comments have anything to do with what these commands really do. Is it okay if I ask for review? What else can we add?
{- 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
main :: IO ()
main = do
initGUI -- is start
window <- windowNew -- make a new window to throw things into
list <- listStoreNew ["Vince", "Jhen", "Chris", "Sharon"] --store this
-- the statements inside the {- -} pair is equivalent to the statement after it.
-- it'll create a treeview widget and put the list into it.
{- treeview <- Model.treeViewNew
treeViewSetModel treeview list -}
treeview <- Model.treeViewNewWithModel list
-- will make the columns' headers visible
Model.treeViewSetHeadersVisible treeview True
-- there should be a simpler way to render a list as the following!
col <- Model.treeViewColumnNew -- new column that'll show our list
Model.treeViewColumnSetTitle col "colTitle" -- pretty much describes it, eh?
renderer <- Model.cellRendererTextNew -- i guess that'll render the text inside the column's cells?
Model.cellLayoutPackStart col renderer False -- i have no idea
-- Model.cellLayoutPackStart col renderer True -- NO IDEA
--
-- i guess this function says that "col" will be displayed using "renderer"
-- and for each item in the list it runs that nameless function which changes the cellText attribute
-- of that particular cell in the column
Model.cellLayoutSetAttributes col renderer list
$ \ind -> [Model.cellText := ind]
-- kinda clear, appends the "col" into the "treeview"
Model.treeViewAppendColumn treeview col
-- get currently selected node in the tree
tree <- Model.treeViewGetSelection treeview
-- oh, i'm not sure what this one does because when it runs we can select several items.
Model.treeSelectionSetMode tree SelectionSingle
-- well, see explanation below
Model.onSelectionChanged tree (oneSelection list tree)
-- setting a few attributes in "window" - size and the widget inside of it, which is our "treeview"
set window [ windowDefaultWidth := 100
, windowDefaultHeight := 200
, containerChild := treeview
]
-- self explanatory, isn't it? when window is destroyed for whatever reason, the program will quit
onDestroy window mainQuit
-- blah
widgetShowAll window
-- blah
mainGUI
return ()
--
oneSelection :: ListStore String -> Model.TreeSelection -> IO ()
oneSelection list tree = do
-- this creates a list of paths of all selected rows
sel <- Model.treeSelectionGetSelectedRows tree
-- sel is [[a]] so now s is a
let s = head (head sel)
-- get the s item's value out of the list?
v <- Model.listStoreGetValue list s
putStrLn $ "selected " ++ v