Debian Codegraph @

demo/treelist/ListText.hs

raw
1 import Graphics.UI.Gtk
2 import Data.Char
3 import Data.List
4 import Data.Maybe
5
6 data RowInfo = RowInfo { rowString :: String, rowCase :: Maybe Bool }
7
8 mkCase Nothing str = str
9 mkCase (Just False) str = map toLower str
10 mkCase (Just True) str = map toUpper str
11
12 advCase Nothing = Just False
13 advCase (Just False) = Just True
14 advCase (Just True) = Nothing
15
16 main :: IO ()
17 main = do
18 unsafeInitGUIForThreadedRTS
19 win <- windowNew
20 win `on` objectDestroy $ mainQuit
21
22 content <- readFile "ListText.hs"
23
24 model <- listStoreNew (map (\r -> RowInfo r Nothing) (lines content))
25 view <- treeViewNewWithModel model
26
27 -- add a column showing the index
28 col <- treeViewColumnNew
29 treeViewAppendColumn view col
30
31 cell <- cellRendererTextNew
32 cellLayoutPackStart col cell True
33 cellLayoutSetAttributeFunc col cell model $ \(TreeIter _ n _ _) ->
34 set cell [cellText := show n]
35 set col [treeViewColumnTitle := "line",
36 treeViewColumnReorderable := True ]
37
38 -- add a column showing the line in the file
39 col <- treeViewColumnNew
40 treeViewAppendColumn view col
41 set col [treeViewColumnTitle := "line in file",
42 treeViewColumnReorderable := True ]
43
44 cell <- cellRendererTextNew
45 cellLayoutPackStart col cell True
46 cellLayoutSetAttributes col cell model $
47 \row -> [cellText := mkCase (rowCase row) (rowString row)]
48
49 -- add a column showing if it is forced to a specific case
50 col <- treeViewColumnNew
51 treeViewAppendColumn view col
52 set col [treeViewColumnTitle := "case",
53 treeViewColumnReorderable := True ]
54
55 cell <- cellRendererToggleNew
56 cellLayoutPackStart col cell True
57 cellLayoutSetAttributes col cell model $
58 \row -> [cellToggleActive := fromMaybe False (rowCase row),
59 cellToggleInconsistent := rowCase row==Nothing]
60 cell `on` cellToggled $ \tpStr -> do
61 let [i] = stringToTreePath tpStr
62 row@RowInfo { rowCase = c } <- listStoreGetValue model i
63 listStoreSetValue model i row { rowCase = advCase c }
64
65 -- to annoy the user: don't allow any columns to be dropped at the far right
66 treeViewSetColumnDragFunction view $ Just $ \_ rCol _ -> do
67 return (rCol /= Nothing)
68
69 treeViewSetSearchEqualFunc view $ Just $ \str (TreeIter _ n _ _) -> do
70 row <- listStoreGetValue model (fromIntegral n)
71 return (map toLower str `isPrefixOf` map toLower (filter isAlphaNum (rowString row)))
72
73 swin <- scrolledWindowNew Nothing Nothing
74 set swin [ containerChild := view ]
75 set win [ containerChild := swin ]
76 widgetShowAll win
77 mainGUI