| 1 |
import Graphics.UI.Gtk |
| 2 |
import Graphics.UI.Gtk.ModelView as New |
| 3 |
import Data.Tree |
| 4 |
|
| 5 |
main = do |
| 6 |
initGUI |
| 7 |
win <- windowNew |
| 8 |
-- Create a tree model with some unsorted data. |
| 9 |
rawmodel <- New.treeStoreNew |
| 10 |
[Node ("zoo",8) [], Node ("foo",5) [], |
| 11 |
Node ("bar",20) [], Node ("baz",2) []] |
| 12 |
|
| 13 |
-- Create a sorting proxy model, that is, a model that permutates the |
| 14 |
-- rows of a different model such that they appear to be sorted. |
| 15 |
model <- New.treeModelSortNewWithModel rawmodel |
| 16 |
|
| 17 |
-- Define two sorting functions, one being the default sorting function and |
| 18 |
-- the other one being the sorting function for the 'SortColumnId' 2. |
| 19 |
-- 'SortColumnId's are arbitrary positive numbers, i.e., we could have chosen |
| 20 |
-- any other unique number. |
| 21 |
New.treeSortableSetDefaultSortFunc model $ Just $ \iter1 iter2 -> do |
| 22 |
(t1,_) <- New.treeModelGetRow rawmodel iter1 |
| 23 |
(t2,_) <- New.treeModelGetRow rawmodel iter2 |
| 24 |
return (compare t1 t2) |
| 25 |
New.treeSortableSetSortFunc model 2 $ \iter1 iter2 -> do |
| 26 |
(_,n1) <- New.treeModelGetRow rawmodel iter1 |
| 27 |
(_,n2) <- New.treeModelGetRow rawmodel iter2 |
| 28 |
return (compare n1 n2) |
| 29 |
|
| 30 |
-- Create the view. |
| 31 |
view <- New.treeViewNewWithModel model |
| 32 |
|
| 33 |
-- Create and insert two columns, one with the heading Name, one with the |
| 34 |
-- heading Number. Associate the 'SortColumnId' 2 with the latter column such |
| 35 |
-- that clicking on the Number header will sort the rows by the numbers. |
| 36 |
col <- New.treeViewColumnNew |
| 37 |
New.treeViewColumnSetTitle col "Name" |
| 38 |
rend <- New.cellRendererTextNew |
| 39 |
New.cellLayoutPackStart col rend True |
| 40 |
New.cellLayoutSetAttributeFunc col rend model $ \iter -> do |
| 41 |
cIter <- New.treeModelSortConvertIterToChildIter model iter |
| 42 |
(n,_) <- New.treeModelGetRow rawmodel cIter |
| 43 |
set rend [New.cellText := n] |
| 44 |
New.treeViewAppendColumn view col |
| 45 |
|
| 46 |
col' <- New.treeViewColumnNew |
| 47 |
New.treeViewColumnSetTitle col' "Number" |
| 48 |
rend <- New.cellRendererTextNew |
| 49 |
New.cellLayoutPackStart col' rend True |
| 50 |
New.cellLayoutSetAttributeFunc col' rend model $ \iter -> do |
| 51 |
cIter <- New.treeModelSortConvertIterToChildIter model iter |
| 52 |
(_,c) <- New.treeModelGetRow rawmodel cIter |
| 53 |
set rend [New.cellText := show c] |
| 54 |
New.treeViewAppendColumn view col' |
| 55 |
New.treeViewColumnSetSortColumnId col' 2 |
| 56 |
|
| 57 |
-- Create a button that shows information on the current state of the sorting |
| 58 |
-- settings. |
| 59 |
button <- buttonNewWithLabel "Dump Info" |
| 60 |
on button buttonActivated $ do |
| 61 |
sId <- New.treeViewColumnGetSortColumnId col |
| 62 |
putStrLn ("tvc1 sort id is "++show sId) |
| 63 |
sId <- New.treeViewColumnGetSortColumnId col' |
| 64 |
putStrLn ("tvc2 sort id is "++show sId) |
| 65 |
sId <- New.treeSortableGetSortColumnId model |
| 66 |
putStrLn ("sort id is "++show sId) |
| 67 |
-- Show all entries of the proxy model |
| 68 |
let recurse Nothing = return () |
| 69 |
recurse (Just iter) = do |
| 70 |
cIter <- New.treeModelSortConvertIterToChildIter model iter |
| 71 |
row <- New.treeModelGetRow rawmodel cIter |
| 72 |
putStrLn ("iter "++show cIter++": "++show row) |
| 73 |
mIter <- New.treeModelIterNext model iter |
| 74 |
recurse mIter |
| 75 |
mIter <- New.treeModelGetIterFirst model |
| 76 |
recurse mIter |
| 77 |
|
| 78 |
-- Put it all together. |
| 79 |
vBox <- vBoxNew False 3 |
| 80 |
-- boxPackStartDefaults vBox view |
| 81 |
boxPackStart vBox view PackRepel 0 |
| 82 |
boxPackEnd vBox button PackNatural 0 |
| 83 |
containerAdd win vBox |
| 84 |
widgetShowAll win |
| 85 |
on win objectDestroy mainQuit |
| 86 |
mainGUI |