]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
Allow NULL for message_format, as described in the docs and as worked
[~andy/gtk] / gtk / gtkmessagedialog.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2003.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtkmessagedialog.h"
28 #include "gtklabel.h"
29 #include "gtkhbox.h"
30 #include "gtkimage.h"
31 #include "gtkstock.h"
32 #include "gtkiconfactory.h"
33 #include "gtkintl.h"
34 #include <string.h>
35
36 static void gtk_message_dialog_class_init (GtkMessageDialogClass *klass);
37 static void gtk_message_dialog_init       (GtkMessageDialog      *dialog);
38 static void gtk_message_dialog_style_set  (GtkWidget             *widget,
39                                            GtkStyle              *prev_style);
40
41 static void gtk_message_dialog_set_property (GObject          *object,
42                                              guint             prop_id,
43                                              const GValue     *value,
44                                              GParamSpec       *pspec);
45 static void gtk_message_dialog_get_property (GObject          *object,
46                                              guint             prop_id,
47                                              GValue           *value,
48                                              GParamSpec       *pspec);
49 static void gtk_message_dialog_add_buttons  (GtkMessageDialog *message_dialog,
50                                              GtkButtonsType    buttons);
51
52
53 enum {
54   PROP_0,
55   PROP_MESSAGE_TYPE,
56   PROP_BUTTONS
57 };
58
59 static gpointer parent_class;
60
61 GType
62 gtk_message_dialog_get_type (void)
63 {
64   static GType dialog_type = 0;
65
66   if (!dialog_type)
67     {
68       static const GTypeInfo dialog_info =
69       {
70         sizeof (GtkMessageDialogClass),
71         NULL,           /* base_init */
72         NULL,           /* base_finalize */
73         (GClassInitFunc) gtk_message_dialog_class_init,
74         NULL,           /* class_finalize */
75         NULL,           /* class_data */
76         sizeof (GtkMessageDialog),
77         0,              /* n_preallocs */
78         (GInstanceInitFunc) gtk_message_dialog_init,
79       };
80
81       dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkMessageDialog",
82                                             &dialog_info, 0);
83     }
84
85   return dialog_type;
86 }
87
88 static void
89 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
90 {
91   GtkWidgetClass *widget_class;
92   GObjectClass *gobject_class;
93
94   widget_class = GTK_WIDGET_CLASS (class);
95   gobject_class = G_OBJECT_CLASS (class);
96
97   parent_class = g_type_class_peek_parent (class);
98   
99   widget_class->style_set = gtk_message_dialog_style_set;
100
101   gobject_class->set_property = gtk_message_dialog_set_property;
102   gobject_class->get_property = gtk_message_dialog_get_property;
103   
104   gtk_widget_class_install_style_property (widget_class,
105                                            g_param_spec_int ("message_border",
106                                                              _("Image/label border"),
107                                                              _("Width of border around the label and image in the message dialog"),
108                                                              0,
109                                                              G_MAXINT,
110                                                              8,
111                                                              G_PARAM_READABLE));
112   g_object_class_install_property (gobject_class,
113                                    PROP_MESSAGE_TYPE,
114                                    g_param_spec_enum ("message_type",
115                                                       _("Message Type"),
116                                                       _("The type of message"),
117                                                       GTK_TYPE_MESSAGE_TYPE,
118                                                       GTK_MESSAGE_INFO,
119                                                       G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
120   g_object_class_install_property (gobject_class,
121                                    PROP_BUTTONS,
122                                    g_param_spec_enum ("buttons",
123                                                       _("Message Buttons"),
124                                                       _("The buttons shown in the message dialog"),
125                                                       GTK_TYPE_BUTTONS_TYPE,
126                                                       GTK_BUTTONS_NONE,
127                                                       G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
128
129 }
130
131 static void
132 gtk_message_dialog_init (GtkMessageDialog *dialog)
133 {
134   GtkWidget *hbox;
135   
136   dialog->label = gtk_label_new (NULL);
137   dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
138   gtk_misc_set_alignment (GTK_MISC (dialog->image), 0.5, 0.0);
139   
140   gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
141   gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
142   
143   hbox = gtk_hbox_new (FALSE, 6);
144
145   gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
146                       FALSE, FALSE, 0);
147
148   gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
149                       TRUE, TRUE, 0);
150
151   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
152                       hbox,
153                       FALSE, FALSE, 0);
154
155   gtk_widget_show_all (hbox);
156 }
157
158 static GtkMessageType
159 gtk_message_dialog_get_message_type (GtkMessageDialog *dialog)
160 {
161   const gchar* stock_id = NULL;
162
163   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), GTK_MESSAGE_INFO);
164   g_return_val_if_fail (GTK_IS_IMAGE(dialog->image), GTK_MESSAGE_INFO);
165
166   stock_id = GTK_IMAGE(dialog->image)->data.stock.stock_id;
167
168   /* Look at the stock id of the image to guess the
169    * GtkMessageType value that was used to choose it
170    * in setup_type()
171    */
172   if (strcmp (stock_id, GTK_STOCK_DIALOG_INFO) == 0)
173     return GTK_MESSAGE_INFO;
174   else if (strcmp (stock_id, GTK_STOCK_DIALOG_QUESTION) == 0)
175     return GTK_MESSAGE_QUESTION;
176   else if (strcmp (stock_id, GTK_STOCK_DIALOG_WARNING) == 0)
177     return GTK_MESSAGE_WARNING;
178   else if (strcmp (stock_id, GTK_STOCK_DIALOG_ERROR) == 0)
179     return GTK_MESSAGE_ERROR;
180   else
181     {
182       g_assert_not_reached (); 
183       return GTK_MESSAGE_INFO;
184     }
185 }
186
187 static void
188 setup_type (GtkMessageDialog *dialog,
189             GtkMessageType    type)
190 {
191   const gchar *stock_id = NULL;
192   GtkStockItem item;
193   
194   switch (type)
195     {
196     case GTK_MESSAGE_INFO:
197       stock_id = GTK_STOCK_DIALOG_INFO;
198       break;
199
200     case GTK_MESSAGE_QUESTION:
201       stock_id = GTK_STOCK_DIALOG_QUESTION;
202       break;
203
204     case GTK_MESSAGE_WARNING:
205       stock_id = GTK_STOCK_DIALOG_WARNING;
206       break;
207       
208     case GTK_MESSAGE_ERROR:
209       stock_id = GTK_STOCK_DIALOG_ERROR;
210       break;
211       
212     default:
213       g_warning ("Unknown GtkMessageType %d", type);
214       break;
215     }
216
217   if (stock_id == NULL)
218     stock_id = GTK_STOCK_DIALOG_INFO;
219
220   if (gtk_stock_lookup (stock_id, &item))
221     {
222       gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
223                                 GTK_ICON_SIZE_DIALOG);
224       
225       gtk_window_set_title (GTK_WINDOW (dialog), item.label);
226     }
227   else
228     g_warning ("Stock dialog ID doesn't exist?");  
229 }
230
231 static void 
232 gtk_message_dialog_set_property (GObject      *object,
233                                  guint         prop_id,
234                                  const GValue *value,
235                                  GParamSpec   *pspec)
236 {
237   GtkMessageDialog *dialog;
238   
239   dialog = GTK_MESSAGE_DIALOG (object);
240   
241   switch (prop_id)
242     {
243     case PROP_MESSAGE_TYPE:
244       setup_type (dialog, g_value_get_enum (value));
245       break;
246     case PROP_BUTTONS:
247       gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
248       break;
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252     }
253 }
254
255 static void 
256 gtk_message_dialog_get_property (GObject     *object,
257                                  guint        prop_id,
258                                  GValue      *value,
259                                  GParamSpec  *pspec)
260 {
261   GtkMessageDialog *dialog;
262   
263   dialog = GTK_MESSAGE_DIALOG (object);
264   
265   switch (prop_id)
266     {
267     case PROP_MESSAGE_TYPE:
268       g_value_set_enum (value, gtk_message_dialog_get_message_type (dialog));
269       break;
270     default:
271       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272       break;
273     }
274 }
275
276
277 /**
278  * gtk_message_dialog_new:
279  * @parent: transient parent, or NULL for none 
280  * @flags: flags
281  * @type: type of message
282  * @buttons: set of buttons to use
283  * @message_format: printf()-style format string, or NULL
284  * @Varargs: arguments for @message_format
285  * 
286  * Creates a new message dialog, which is a simple dialog with an icon
287  * indicating the dialog type (error, warning, etc.) and some text the
288  * user may want to see. When the user clicks a button a "response"
289  * signal is emitted with response IDs from #GtkResponseType. See
290  * #GtkDialog for more details.
291  * 
292  * Return value: a new #GtkMessageDialog
293  **/
294 GtkWidget*
295 gtk_message_dialog_new (GtkWindow     *parent,
296                         GtkDialogFlags flags,
297                         GtkMessageType type,
298                         GtkButtonsType buttons,
299                         const gchar   *message_format,
300                         ...)
301 {
302   GtkWidget *widget;
303   GtkDialog *dialog;
304   gchar* msg = 0;
305   va_list args;
306
307   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
308
309   widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
310                          "message_type", type,
311                          "buttons", buttons,
312                          NULL);
313   dialog = GTK_DIALOG (widget);
314
315   if (flags & GTK_DIALOG_NO_SEPARATOR)
316     {
317       g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
318       flags &= ~GTK_DIALOG_NO_SEPARATOR;
319     }
320
321   if (message_format)
322     {
323       va_start (args, message_format);
324       msg = g_strdup_vprintf (message_format, args);
325       va_end (args);
326       
327       
328       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
329                           msg);
330       
331       g_free (msg);
332     }
333
334   if (parent != NULL)
335     gtk_window_set_transient_for (GTK_WINDOW (widget),
336                                   GTK_WINDOW (parent));
337   
338   if (flags & GTK_DIALOG_MODAL)
339     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
340
341   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
342     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
343
344   if (flags & GTK_DIALOG_NO_SEPARATOR)
345     gtk_dialog_set_has_separator (dialog, FALSE);
346
347   return widget;
348 }
349
350 /**
351  * gtk_message_dialog_new_with_markup:
352  * @parent: transient parent, or %NULL for none 
353  * @flags: flags
354  * @type: type of message
355  * @buttons: set of buttons to use
356  * @message_format: printf()-style format string, or %NULL
357  * @Varargs: arguments for @message_format
358  * 
359  * Creates a new message dialog, which is a simple dialog with an icon
360  * indicating the dialog type (error, warning, etc.) and some text which
361  * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
362  * When the user clicks a button a "response" signal is emitted with
363  * response IDs from #GtkResponseType. See #GtkDialog for more details.
364  *
365  * Special XML characters in the printf() arguments passed to this
366  * function will automatically be escaped as necessary.
367  * (See g_markup_printf_escaped() for how this is implemented.)
368  * Usually this is what you want, but if you have an existing
369  * Pango markup string that you want to use literally as the
370  * label, then you need to use gtk_message_dialog_set_markup()
371  * instead, since you can't pass the markup string either
372  * as the format (it might contain '%' characters) or as a string
373  * argument.
374  *
375  * <informalexample><programlisting>
376  *  GtkWidget *dialog;
377  *  dialog = gtk_message_dialog_new (main_application_window,
378  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
379  *                                   GTK_MESSAGE_ERROR,
380  *                                   GTK_BUTTON_CLOSE,
381  *                                   NULL);
382  *  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
383  *                                 markup);
384  * </programlisting></informalexample>
385  * 
386  * Return value: a new #GtkMessageDialog
387  *
388  * Since: 2.4
389  **/
390 GtkWidget*
391 gtk_message_dialog_new_with_markup (GtkWindow     *parent,
392                                     GtkDialogFlags flags,
393                                     GtkMessageType type,
394                                     GtkButtonsType buttons,
395                                     const gchar   *message_format,
396                                     ...)
397 {
398   GtkWidget *widget;
399   gchar* msg = 0;
400   va_list args;
401
402   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
403
404   widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
405
406   if (message_format)
407     {
408       va_start (args, message_format);
409       msg = g_markup_vprintf_escaped (message_format, args);
410       va_end (args);
411
412       gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
413
414       g_free (msg);
415     }
416
417   return widget;
418 }
419
420 /**
421  * gtk_message_dialog_set_markup:
422  * @message_dialog: a #GtkMessageDialog
423  * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
424  * 
425  * Sets the text of the message dialog to be @str, which is marked
426  * up with the <link linkend="PangoMarkupFormat">Pango text markup
427  * language</link>.
428  *
429  * Since: 2.4
430  **/
431 void
432 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
433                                const gchar      *str)
434 {
435   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
436   
437   gtk_label_set_markup (GTK_LABEL (message_dialog->label), str);
438 }
439
440 static void
441 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
442                                 GtkButtonsType buttons)
443 {
444   GtkDialog* dialog = GTK_DIALOG (message_dialog);
445
446   switch (buttons)
447     {
448     case GTK_BUTTONS_NONE:
449       /* nothing */
450       break;
451
452     case GTK_BUTTONS_OK:
453       gtk_dialog_add_button (dialog,
454                              GTK_STOCK_OK,
455                              GTK_RESPONSE_OK);
456       break;
457
458     case GTK_BUTTONS_CLOSE:
459       gtk_dialog_add_button (dialog,
460                              GTK_STOCK_CLOSE,
461                              GTK_RESPONSE_CLOSE);
462       break;
463
464     case GTK_BUTTONS_CANCEL:
465       gtk_dialog_add_button (dialog,
466                              GTK_STOCK_CANCEL,
467                              GTK_RESPONSE_CANCEL);
468       break;
469
470     case GTK_BUTTONS_YES_NO:
471       gtk_dialog_add_button (dialog,
472                              GTK_STOCK_NO,
473                              GTK_RESPONSE_NO);
474       gtk_dialog_add_button (dialog,
475                              GTK_STOCK_YES,
476                              GTK_RESPONSE_YES);
477       break;
478
479     case GTK_BUTTONS_OK_CANCEL:
480       gtk_dialog_add_button (dialog,
481                              GTK_STOCK_CANCEL,
482                              GTK_RESPONSE_CANCEL);
483       gtk_dialog_add_button (dialog,
484                              GTK_STOCK_OK,
485                              GTK_RESPONSE_OK);
486       break;
487       
488     default:
489       g_warning ("Unknown GtkButtonsType");
490       break;
491     } 
492
493   g_object_notify (G_OBJECT (message_dialog), "buttons");
494 }
495
496 static void
497 gtk_message_dialog_style_set (GtkWidget *widget,
498                               GtkStyle  *prev_style)
499 {
500   GtkWidget *parent;
501   gint border_width = 0;
502
503   parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
504
505   if (parent)
506     {
507       gtk_widget_style_get (widget, "message_border",
508                             &border_width, NULL);
509       
510       gtk_container_set_border_width (GTK_CONTAINER (parent),
511                                       border_width);
512     }
513
514   if (GTK_WIDGET_CLASS (parent_class)->style_set)
515     (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);
516 }