File: MessageDialog.chs

package info (click to toggle)
haskell-gtk 0.11.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,920 kB
  • ctags: 82
  • sloc: haskell: 1,929; ansic: 714; sh: 5; makefile: 3
file content (344 lines) | stat: -rw-r--r-- 10,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{-# LANGUAGE CPP #-}
-- -*-haskell-*-
--  GIMP Toolkit (GTK) Widget MessageDialog
--
--  Author : Axel Simon
--
--  Created: 20 October 2006
--
--  Copyright (C) 2006 Axel Simon
--
--  This library is free software; you can redistribute it and/or
--  modify it under the terms of the GNU Lesser General Public
--  License as published by the Free Software Foundation; either
--  version 2.1 of the License, or (at your option) any later version.
--
--  This library is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--  Lesser General Public License for more details.
--
--
-- |
-- Maintainer  : gtk2hs-users@lists.sourceforge.net
-- Stability   : provisional
-- Portability : portable (depends on GHC)
--
-- A convenient message window
--
module Graphics.UI.Gtk.Windows.MessageDialog (
-- * Detail
-- 
-- | 'MessageDialog' presents a dialog with an image representing the type of
-- message (Error, Question, etc.) alongside some message text. It's simply a
-- convenience widget; you could construct the equivalent of 'MessageDialog'
-- from 'Dialog' without too much effort, but 'MessageDialog' saves typing.
--
-- The easiest way to do a modal message dialog is to use 'dialogRun',
-- though you can also pass in the 'DialogModal' flag, 'dialogRun'
-- automatically makes the dialog modal and waits for the user to respond to
-- it. 'dialogRun' returns when any dialog button is clicked.

-- * Class Hierarchy
-- |
-- @
-- |  'GObject'
-- |   +----'Object'
-- |         +----'Widget'
-- |               +----'Container'
-- |                     +----'Bin'
-- |                           +----'Window'
-- |                                 +----'Dialog'
-- |                                       +----MessageDialog
-- @

-- * Types
  MessageDialog,
  MessageDialogClass,
  castToMessageDialog, gTypeMessageDialog,
  toMessageDialog,
  MessageType(..),
  ButtonsType(..),
  DialogFlags(..),
  
-- * Constructors
  messageDialogNew,
#if GTK_CHECK_VERSION(2,4,0)
  messageDialogNewWithMarkup,
#endif
  
-- * Methods
#if GTK_CHECK_VERSION(2,4,0)
  messageDialogSetMarkup,
#endif
#if GTK_CHECK_VERSION(2,10,0)
  messageDialogSetImage,
#endif
#if GTK_CHECK_VERSION(2,6,0)
  messageDialogSetSecondaryMarkup,
  messageDialogSetSecondaryText,
#endif

-- * Attributes
  messageDialogMessageType,
#if GTK_CHECK_VERSION(2,10,0)
  messageDialogText,
  messageDialogUseMarkup,
  messageDialogSecondaryText,
  messageDialogSecondaryUseMarkup,
  messageDialogImage,
#endif
  messageDialogButtons,
  ) where

import Control.Monad	(liftM)

import System.Glib.FFI
{#import Graphics.UI.Gtk.Types#}
import System.Glib.UTFString
import System.Glib.Attributes
import System.Glib.Properties
import System.Glib.Flags	(Flags, fromFlags)
import Graphics.UI.Gtk.Abstract.Object	(makeNewObject)
import Graphics.Rendering.Pango.Markup (Markup)

{# context lib="gtk" prefix="gtk" #}

--------------------
-- Types

-- | Specify what message icon this dialog should show.
--
#if GTK_CHECK_VERSION(2,10,0)
--
-- * From Gtk 2.10 onwards, you can pass 'MessageOther' and supply your
--   own image using 'messageDialogSetImage'.
--
#endif
{#enum MessageType {underscoreToCase} deriving(Show,Eq)#}

-- | Specify what buttons this dialog should show.
--
-- * Prebuilt sets of buttons for the dialog. If none of these choices
--   are appropriate, simply use 'ButtonsNone' then call 'dialogAddButtons'.
--
{#enum ButtonsType {underscoreToCase} deriving(Show,Eq)#}

-- | Flags used to influence dialog construction.
--
-- * Marking a dialog as model will call 'widgetSetModal' on the dialog
--   window, the 'DialogDestroyWithParent' will call
--   'windowSetDestroyWithParent' on the dialog window. Note that in
--   case the dialog is simply destroyed, no response signal is ever
--   emitted. Finally, 'DialogNoSeparator' omits the separator between
--   the action area and the dialog content which is preferable for
--   very simple messages, i.e. those that only contain one button.
--
{#enum DialogFlags {underscoreToCase} deriving (Show,Eq,Bounded)#}

instance Flags DialogFlags

--------------------
-- Constructors

-- | Create a new message dialog, which is a simple dialog with an icon 
--   indicating the dialog type (error, warning, etc.) and some text the 
--   user may want to see. When the user clicks a button a \"response\" signal
--   is emitted with response IDs from 'ResponseType'. See 'Dialog' for more
--   details.
-- 
messageDialogNew
  :: Maybe Window  -- ^ Transient parent of the dialog (or none)
  -> [DialogFlags]
  -> MessageType
  -> ButtonsType
  -> String        -- ^ The text of the message
  -> IO MessageDialog
messageDialogNew mWindow flags mType bType msg =
  withUTFString (unPrintf msg) $ \msgPtr ->
  makeNewObject mkMessageDialog $
  liftM (castPtr :: Ptr Widget -> Ptr MessageDialog) $
  call_message_dialog_new mWindow flags mType bType msgPtr
	  			 
	  			 
call_message_dialog_new :: Maybe Window -> [DialogFlags] ->
			   MessageType -> ButtonsType -> Ptr CChar ->
			   IO (Ptr Widget)
call_message_dialog_new (Just (Window fPtr)) flags mType bType msgPtr =
  withForeignPtr fPtr $ \ptr ->
    message_dialog_new ptr (fromIntegral (fromFlags flags))
      (fromIntegral (fromEnum mType))
      (fromIntegral (fromEnum bType)) msgPtr
call_message_dialog_new Nothing flags mType bType msgPtr =
    message_dialog_new nullPtr (fromIntegral (fromFlags flags))
      (fromIntegral (fromEnum mType))
      (fromIntegral (fromEnum bType)) msgPtr

foreign import ccall unsafe "gtk_message_dialog_new"
  message_dialog_new :: Ptr Window -> CInt -> CInt -> CInt ->
		        Ptr CChar -> IO (Ptr Widget)

#if GTK_CHECK_VERSION(2,4,0)
-- | Creates a new message dialog, which is a simple dialog with an icon
--   indicating the dialog type (error, warning, etc.) and some text which
--   is marked up with the Pango text markup language. When the user clicks
--   a button a \"response\" signal is emitted with response IDs from
--   'ResponseType'. See 'Dialog' and 'PangoMarkup' for more details.
--
-- * Available since Gtk+ version 2.4
--
messageDialogNewWithMarkup
  :: Maybe Window  -- ^ Transient parent of the dialog (or none)
  -> [DialogFlags]
  -> MessageType
  -> ButtonsType
  -> Markup        -- ^ The text of the message
  -> IO MessageDialog
messageDialogNewWithMarkup mWindow flags mType bType msg = do
  md <- makeNewObject mkMessageDialog $
    liftM (castPtr :: Ptr Widget -> Ptr MessageDialog) $
    call_message_dialog_new mWindow flags mType bType nullPtr
  messageDialogSetMarkup md msg
  return md
#endif
  
--------------------
-- Methods

#if GTK_CHECK_VERSION(2,4,0)
-- | Sets the text of the message dialog to be @str@, which is marked up with
-- the Pango text markup language.
--
-- * Available since Gtk+ version 2.4
--
messageDialogSetMarkup :: MessageDialogClass self => self
 -> Markup -- ^ @str@ - markup string (see Pango markup format)
 -> IO ()
messageDialogSetMarkup self str =
  withUTFString (unPrintf str) $ \strPtr ->
  {# call gtk_message_dialog_set_markup #}
    (toMessageDialog self)
    strPtr
#endif

#if GTK_CHECK_VERSION(2,6,0)
messageDialogSetSecondaryMarkup :: MessageDialogClass self => self
 -> String -- ^ @str@ - markup string (see Pango markup format)
 -> IO ()
messageDialogSetSecondaryMarkup self str =
  withUTFString (unPrintf str) $ \strPtr ->
  let (MessageDialog fPtr) = toMessageDialog self in
  withForeignPtr fPtr $ \ptr ->
  message_dialog_format_secondary_markup ptr strPtr

foreign import ccall unsafe "gtk_message_dialog_format_secondary_markup"
  message_dialog_format_secondary_markup :: Ptr MessageDialog -> 
  					   Ptr CChar -> IO ()
  					
messageDialogSetSecondaryText :: MessageDialogClass self => self
 -> String -- ^ @str@ - text to be shown as second line
 -> IO ()
messageDialogSetSecondaryText self str =
  withUTFString str $ \strPtr ->
  let (MessageDialog fPtr) = toMessageDialog self in
  withForeignPtr fPtr $ \ptr ->
  message_dialog_format_secondary_text ptr strPtr

foreign import ccall unsafe "gtk_message_dialog_format_secondary_text"
  message_dialog_format_secondary_text :: Ptr MessageDialog -> 
 					 Ptr CChar -> IO ()

#if GTK_CHECK_VERSION(2,10,0)
-- %hash c:6cb7 d:ebdd
-- | Sets the dialog's image to @image@.
--
-- * Available since Gtk+ version 2.10
--
messageDialogSetImage :: (MessageDialogClass self, WidgetClass image) => self
 -> image -- ^ @image@ - the image
 -> IO ()
messageDialogSetImage self image =
  {# call gtk_message_dialog_set_image #}
    (toMessageDialog self)
    (toWidget image)
#endif
#endif


--------------------
-- Attributes

-- | The type of message.
--
-- Default value: 'MessageInfo'
--
messageDialogMessageType :: MessageDialogClass self => Attr self MessageType
messageDialogMessageType = newAttrFromEnumProperty "message-type"
  {#call pure unsafe gtk_message_type_get_type #}

#if GTK_CHECK_VERSION(2,10,0)
-- %hash c:a2fe d:e4a2
-- | The primary text of the message dialog. If the dialog has a secondary
-- text, this will appear as the title.
--
-- Default value: @Nothing@
--
-- * Available since Gtk+ version 2.10
--
messageDialogText :: MessageDialogClass self => Attr self (Maybe String)
messageDialogText = newAttrFromMaybeStringProperty "text"

-- %hash c:e1dd d:ca3
-- | Interpret the string 'messageDialogText' as markup.
--
-- Default value: @False@
--
-- * Available since Gtk+ version 2.10
--
messageDialogUseMarkup :: MessageDialogClass self => Attr self Bool
messageDialogUseMarkup = newAttrFromBoolProperty "use-markup"

-- %hash c:9623 d:1fbe
-- | The secondary text of the message dialog.
--
-- Default value: @Nothing@
--
-- * Available since Gtk+ version 2.10
--
messageDialogSecondaryText :: MessageDialogClass self => Attr self (Maybe String)
messageDialogSecondaryText = newAttrFromMaybeStringProperty "secondary-text"

-- %hash c:1ce2 d:ca3
-- | Default value: @False@
--
-- * Available since Gtk+ version 2.10
--
messageDialogSecondaryUseMarkup :: MessageDialogClass self => Attr self Bool
messageDialogSecondaryUseMarkup = newAttrFromBoolProperty "secondary-use-markup"

-- %hash c:da36 d:b7dd
-- | The image for this dialog.
--
-- * Available since Gtk+ version 2.10
--
messageDialogImage :: (MessageDialogClass self, WidgetClass widget) => ReadWriteAttr self Widget widget
messageDialogImage = newAttrFromObjectProperty "image"
                       {# call pure unsafe gtk_widget_get_type #}
#endif

-- | The buttons shown in the message dialog.
--
-- Default value: 'ButtonsNone'
--
messageDialogButtons :: MessageDialogClass self => WriteAttr self ButtonsType
messageDialogButtons = writeAttrFromEnumProperty "buttons"
  {#call pure unsafe gtk_buttons_type_get_type #}

--------------------
-- helpers

-- Escape percent signs
unPrintf :: String -> String
unPrintf [] = []
unPrintf ('%':xs) = '%':'%':unPrintf xs
unPrintf (x:xs) = x:unPrintf xs