I have some trouble understanding how to use toggle buttons for GTK+ binding with Haskell.
What I want to do is to display a treeView
with two column: one containing strings and the other containing toggle buttons. The user must tick the toggle buttons to select the inputs he wants to use in another the part of the program.
My model is a list of tuples (String,Bool)
, the boolean supposed to reflect the state of the toggle button. Initialy, all are False.
Here is my code:
treeview <- builderGetObject builder castToTreeView "diffDisplayTreeView"
treeviewselect <- treeViewGetSelection difftreeview
dcolumn <- builderGetObject builder castToTreeViewColumn "dcolumn"
selcolumn <- builderGetObject builder castToTreeViewColumn "selcolumn"
dcell <- builderGetObject builder castToCellRendererText "dcell"
selcell <- builderGetObject builder castToCellRendererToggle "selcell"
[...]
store <- listStoreNew modelFromSomewhereElse
cellLayoutSetAttributes dcolumn dcell store $ \x -> [cellText := fst(x)]
cellLayoutSetAttributes selcolumn selcell store $ \x -> [cellToggleActivate := snd(x)]
treeViewSetModel treeview store
It works well at initating the treeView, but when I click on a toggle button, it remains on the state it was initalized. I'd like to catch the cellToggled
for a specific cell and change the model properly, but I don't quite understand how to navigate within a treeView selection.
Any help would be greatly appreciated :)
Solved thanks to the comment of theGtknerd and some serious digging in the documentation (altough I still do not understand why the lambda expression: http://hackage.haskell.org/package/glib-0.13.4.1/docs/System-Glib-Signals.html#t:Signal):
toggleStuff t m = do
tvwS <- treeViewGetSelection t
tvwP <- treeSelectionGetSelectedRows tvwS
if tvwP == []
then return()
else
do let s = Prelude.head (Prelude.head tvwP)
v <- listStoreGetValue m s
listStoreSetValue m s (fst v, not (snd v))
[...]
a <- treeViewGetSelection treeview
b <- treeSelectionGetSelectedRows a
on celltg cellToggled $ \(b::[Char]) -> do toggleStuff treeview store