Debian Codegraph @

demo/notebook/Notebook.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 -- | Notebook demo (include Spinner animation).
3 -- Author : Andy Stewart
4 -- Copyright : (c) 2010 Andy Stewart <[email protected]>
5
6 module Main where
7
8 import Control.Monad
9 import Control.Monad.IO.Class
10 import Data.Maybe
11 import Data.Text (Text)
12 import Data.Monoid ((<>))
13 import Graphics.UI.Gtk
14 import qualified Data.Text as T (unpack)
15
16 data NotebookTab =
17 NotebookTab {ntBox :: HBox
18 ,ntSpinner :: Spinner
19 ,ntLabel :: Label
20 ,ntCloseButton :: ToolButton
21 ,ntSize :: Int}
22
23 -- | Main
24 main :: IO ()
25 main = do
26 -- Init.
27 initGUI
28
29 -- Create window and notebook.
30 window <- windowNew
31 notebook <- notebookNew
32
33 -- Set window.
34 windowSetDefaultSize window 800 600
35 windowSetPosition window WinPosCenter
36 set window [windowTitle := ("Press Ctrl + n to create new tab."::Text)]
37
38 -- Handle key press action.
39 window `on` keyPressEvent $ tryEvent $ do
40 -- Create new tab when user press Ctrl+n
41 [Control] <- eventModifier
42 "n" <- eventKeyName
43 liftIO $ do
44 -- Create text view.
45 textView <- textViewNew
46 widgetShowAll textView -- must show before add notebook,
47 -- otherwise notebook won't display child widget
48 -- even have add in notebook.
49
50 -- Create notebook tab.
51 tab <- notebookTabNew (Just "Cool tab") Nothing
52 menuLabel <- labelNew (Nothing :: Maybe Text)
53
54 -- Add widgets in notebook.
55 notebookAppendPageMenu notebook textView (ntBox tab) menuLabel
56
57 -- Start spinner animation when create tab.
58 notebookTabStart tab
59
60 -- Stop spinner animation after finish load.
61 timeoutAdd (notebookTabStop tab >> return False) 5000
62
63 -- Close tab when click button.
64 ntCloseButton tab `onToolButtonClicked` do
65 index <- notebookPageNum notebook textView
66 index ?>= \i -> notebookRemovePage notebook i
67
68 return ()
69
70 -- Show window.
71 window `containerAdd` notebook
72 widgetShowAll window
73 on window objectDestroy mainQuit
74 mainGUI
75
76 -- | Create notebook tab.
77 notebookTabNew :: Maybe Text -> Maybe Int -> IO NotebookTab
78 notebookTabNew name size = do
79 -- Init.
80 let iconSize = fromMaybe 12 size
81 box <- hBoxNew False 0
82 spinner <- spinnerNew
83 label <- labelNew name
84 image <- imageNewFromIcon "window-close" iconSize
85 closeButton <- toolButtonNew (Just image) (Nothing::Maybe Text)
86
87 -- Show.
88 boxPackStart box label PackNatural 0
89 boxPackStart box closeButton PackNatural 0
90 widgetShowAll box
91
92 return $ NotebookTab box spinner label closeButton iconSize
93
94 -- | Set tab name.
95 notebookTabSetName :: NotebookTab -> Text -> IO ()
96 notebookTabSetName tab =
97 labelSetText (ntLabel tab)
98
99 -- | Start spinner animation.
100 notebookTabStart :: NotebookTab -> IO ()
101 notebookTabStart NotebookTab {ntBox = box
102 ,ntSpinner = spinner
103 ,ntSize = size} = do
104 boxTryPack box spinner PackNatural (Just 0) (size `div` 2)
105 spinnerStart spinner
106 widgetShow spinner
107
108 -- | Stop spinner animation.
109 notebookTabStop :: NotebookTab -> IO ()
110 notebookTabStop NotebookTab {ntBox = box
111 ,ntSpinner = spinner} = do
112 containerTryRemove box spinner
113 spinnerStop spinner
114
115 -- | Create image widget with given icon name and size.
116 imageNewFromIcon :: Text -> Int -> IO Image
117 imageNewFromIcon iconName size = do
118 iconTheme <- iconThemeGetDefault
119 pixbuf <- do
120 -- Function 'iconThemeLoadIcon' can scale icon with specified size.
121 pixbuf <- iconThemeLoadIcon iconTheme iconName size IconLookupUseBuiltin
122 case pixbuf of
123 Just p -> return p
124 Nothing -> error $ "imageNewFromIcon : search icon " <> T.unpack iconName <> " failed."
125 imageNewFromPixbuf pixbuf
126
127 -- | Try to packing widget in box.
128 -- If @child@ have exist parent, do nothing,
129 -- otherwise, add @child@ to @parent@.
130 boxTryPack :: (BoxClass parent, WidgetClass child) => parent -> child -> Packing -> Maybe Int -> Int -> IO ()
131 boxTryPack box widget packing order space = do
132 parent <- widgetGetParent widget
133 when (isNothing parent) $ do
134 boxPackStart box widget packing space
135 order ?>= boxReorderChild box widget
136
137 -- | Try to remove child from parent.
138 containerTryRemove :: (ContainerClass parent, WidgetClass child) => parent -> child -> IO ()
139 containerTryRemove parent widget = do
140 hasParent <- widgetGetParent widget
141 unless (isNothing hasParent) $ containerRemove parent widget
142
143 -- | Maybe.
144 (?>=) :: Monad m => Maybe a -> (a -> m ()) -> m ()
145 m ?>= f = maybe (return ()) f m