Debian Codegraph @

demo/filechooser/FileChooserDemo.hs

raw
1 module Main where
2
3 import Graphics.UI.Gtk hiding (response)
4
5 main :: IO ()
6 main = do
7 initGUI
8
9 -- load up our main window
10 gui <- builderNew
11 builderAddFromFile gui "FileChooserDemo.glade"
12
13 mainWindow <- builderGetObject gui castToWindow "mainWindow"
14
15 -- get a handle on a various objects from the glade file
16 on mainWindow objectDestroy mainQuit
17
18 let onClicked obj = on obj buttonActivated
19
20 -- -- and associate actions with the buttons
21 selectFolderButton <- builderGetObject gui castToButton "selectFolderButton"
22 selectFolderButton `onClicked` openSelectFolderDialog mainWindow
23
24 createFolderButton <- builderGetObject gui castToButton "createFolderButton"
25 createFolderButton `onClicked` openCreateFolderDialog mainWindow
26
27 openFileButton <- builderGetObject gui castToButton "openFileButton"
28 openFileButton `onClicked` openOpenFileDialog mainWindow
29
30 saveFileButton <- builderGetObject gui castToButton "saveFileButton"
31 saveFileButton `onClicked` openSaveFileDialog mainWindow
32
33 openFilePreviewButton <- builderGetObject gui castToButton "openFilePreviewButton"
34 openFilePreviewButton `onClicked` openFilePreviewDialog mainWindow
35
36 quitButton <- builderGetObject gui castToButton "quitButton"
37 quitButton `onClicked` (do
38 widgetDestroy mainWindow
39 mainQuit)
40
41 -- The final step is to display the main window and run the main loop
42 widgetShowAll mainWindow
43 mainGUI
44
45
46 openSelectFolderDialog :: Window -> IO ()
47 openSelectFolderDialog parentWindow = do
48 dialog <- fileChooserDialogNew
49 (Just $ "Demo of the standard dialog "
50 ++ "to select an existing folder") --dialog title
51 (Just parentWindow) --the parent window
52 FileChooserActionSelectFolder --the kind of dialog we want
53 [("Yes, this new dialog looks nice" --The buttons to display
54 , ResponseAccept)
55 ,("Eugh! Take me away!"
56 ,ResponseCancel)]
57 widgetShow dialog
58 response <- dialogRun dialog
59 case response of
60 ResponseAccept -> do Just fileName <- fileChooserGetFilename dialog
61 putStrLn $ "you selected the folder " ++ show fileName
62 ResponseCancel -> putStrLn "dialog canceled"
63 ResponseDeleteEvent -> putStrLn "dialog closed"
64 widgetHide dialog
65
66 openCreateFolderDialog :: Window -> IO ()
67 openCreateFolderDialog parentWindow = do
68 dialog <- fileChooserDialogNew
69 (Just $ "Demo of the standard dialog to select "
70 ++ "a new folder (or existing) folder") --dialog title
71 (Just parentWindow) --the parent window
72 FileChooserActionCreateFolder --the kind of dialog we want
73 [("I want this new folder" --The buttons to display
74 , ResponseAccept)
75 ,("Bored now."
76 ,ResponseCancel)]
77 widgetShow dialog
78 response <- dialogRun dialog
79 case response of
80 ResponseAccept -> do Just fileName <- fileChooserGetFilename dialog
81 putStrLn $ "you selected the folder " ++ show fileName
82 ResponseCancel -> putStrLn "Getting bored?"
83 ResponseDeleteEvent -> putStrLn "dialog closed"
84 widgetHide dialog
85
86 openOpenFileDialog :: Window -> IO ()
87 openOpenFileDialog parentWindow = do
88 dialog <- fileChooserDialogNew
89 (Just $ "Demo of the standard dialog to select "
90 ++ "an existing file") --dialog title
91 (Just parentWindow) --the parent window
92 FileChooserActionOpen --the kind of dialog we want
93 [("gtk-cancel" --The buttons to display
94 ,ResponseCancel)
95 ,("gtk-open"
96 , ResponseAccept)]
97
98 widgetShow dialog
99 response <- dialogRun dialog
100 case response of
101 ResponseAccept -> do Just fileName <- fileChooserGetFilename dialog
102 putStrLn $ "you selected the file " ++ show fileName
103 ResponseCancel -> putStrLn "dialog canceled"
104 ResponseDeleteEvent -> putStrLn "dialog closed"
105 widgetHide dialog
106
107 openSaveFileDialog :: Window -> IO ()
108 openSaveFileDialog parentWindow = do
109 dialog <- fileChooserDialogNew
110 (Just $ "Demo of the standard dialog to select "
111 ++ "a new file") --dialog title
112 (Just parentWindow) --the parent window
113 FileChooserActionSave --the kind of dialog we want
114 [("gtk-cancel" --The buttons to display
115 ,ResponseCancel) --you can use stock buttons
116 ,("gtk-save"
117 , ResponseAccept)]
118 widgetShow dialog
119 response <- dialogRun dialog
120 case response of
121 ResponseAccept -> do Just fileName <- fileChooserGetFilename dialog
122 putStrLn $ "you called the new file " ++ show fileName
123 ResponseCancel -> putStrLn "dialog canceled"
124 ResponseDeleteEvent -> putStrLn "dialog closed"
125 widgetHide dialog
126
127 openFilePreviewDialog :: Window -> IO ()
128 openFilePreviewDialog parentWindow = do
129 dialog <- fileChooserDialogNew
130 (Just $ "Demo of the standard dialog to select "
131 ++ "a new file - with a preview widget") --dialog title
132 (Just parentWindow) --the parent window
133 FileChooserActionOpen --the kind of dialog we want
134 [("_Yes, yes that's very clever" --The buttons to display
135 , ResponseAccept)
136 ,("_No, I'm not impressed"
137 ,ResponseCancel)]
138
139 --create and set an extra widget
140 checkButton <- checkButtonNewWithLabel "frobnicate this file"
141 dialog `fileChooserSetExtraWidget` checkButton
142
143 --create and set a preview widget
144 previewLabel <- labelNew $ Just "Preview appears here"
145 previewLabel `labelSetLineWrap` True
146 dialog `fileChooserSetPreviewWidget` previewLabel
147 on dialog updatePreview $ do
148 previewFile <- fileChooserGetPreviewFilename dialog
149 previewLabel `labelSetText` case previewFile of
150 Nothing -> "Preview appears here"
151 (Just filename) -> "Just pretend this is a preview of the file:\n" ++
152 show filename
153
154 widgetShow dialog
155 response <- dialogRun dialog
156 case response of
157 ResponseAccept -> do fileName <- fileChooserGetFilename dialog
158 putStrLn $ "you selected the new file " ++ show fileName
159
160 --check the state of the extra widget
161 frobnicate <- toggleButtonGetActive checkButton
162 putStrLn $ if frobnicate
163 then "you foolishly decided to frobnicate the file"
164 else "you wisely decided not to frobnicate the file"
165 ResponseCancel -> putStrLn "you were not impressed"
166 ResponseDeleteEvent -> putStrLn "dialog closed"
167 widgetHide dialog