]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
652b5a6815fe62d1f7e38ab7370ac51947d0a6fe
[~andy/gtk] / gtk / gtkmessagedialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) 2000 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2003.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29 #include <string.h>
30
31 #include "gtkmessagedialog.h"
32 #include "gtkaccessible.h"
33 #include "gtkbuildable.h"
34 #include "gtklabel.h"
35 #include "gtkhbox.h"
36 #include "gtkvbox.h"
37 #include "gtkimage.h"
38 #include "gtkstock.h"
39 #include "gtkiconfactory.h"
40 #include "gtkintl.h"
41 #include "gtkprivate.h"
42
43 /**
44  * SECTION:gtkmessagedialog
45  * @Short_description: A convenient message window
46  * @Title: GtkMessageDialog
47  * @See_also:#GtkDialog
48  *
49  * #GtkMessageDialog presents a dialog with an image representing the type of
50  * message (Error, Question, etc.) alongside some message text. It's simply a
51  * convenience widget; you could construct the equivalent of #GtkMessageDialog
52  * from #GtkDialog without too much effort, but #GtkMessageDialog saves typing.
53  *
54  * The easiest way to do a modal message dialog is to use gtk_dialog_run(), though
55  * you can also pass in the %GTK_DIALOG_MODAL flag, gtk_dialog_run() automatically
56  * makes the dialog modal and waits for the user to respond to it. gtk_dialog_run()
57  * returns when any dialog button is clicked.
58  * <example>
59  * <title>A modal dialog.</title>
60  * <programlisting>
61  *  dialog = gtk_message_dialog_new (main_application_window,
62  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
63  *                                   GTK_MESSAGE_ERROR,
64  *                                   GTK_BUTTONS_CLOSE,
65  *                                   "Error loading file '&percnt;s': &percnt;s",
66  *                                   filename, g_strerror (errno));
67  *  gtk_dialog_run (GTK_DIALOG (dialog));
68  *  gtk_widget_destroy (dialog);
69  * </programlisting>
70  * </example>
71  * You might do a non-modal #GtkMessageDialog as follows:
72  * <example>
73  * <title>A non-modal dialog.</title>
74  * <programlisting>
75  *  dialog = gtk_message_dialog_new (main_application_window,
76  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
77  *                                   GTK_MESSAGE_ERROR,
78  *                                   GTK_BUTTONS_CLOSE,
79  *                                   "Error loading file '&percnt;s': &percnt;s",
80  *                                   filename, g_strerror (errno));
81  *
82  *  /&ast; Destroy the dialog when the user responds to it (e.g. clicks a button) &ast;/
83  *  g_signal_connect_swapped (dialog, "response",
84  *                            G_CALLBACK (gtk_widget_destroy),
85  *                            dialog);
86  * </programlisting>
87  * </example>
88  *
89  * <refsect2 id="GtkMessageDialog-BUILDER-UI">
90  * <title>GtkMessageDialog as GtkBuildable</title>
91  * <para>
92  * The GtkMessageDialog implementation of the GtkBuildable interface exposes
93  * the message area as an internal child with the name "message_area".
94  * </para>
95  * </refsect2>
96  */
97
98 struct _GtkMessageDialogPrivate
99 {
100   GtkWidget     *image;
101   GtkWidget     *label;
102   GtkWidget     *message_area; /* vbox for the primary and secondary labels, and any extra content from the caller */
103   GtkWidget     *secondary_label;
104
105   guint          has_primary_markup : 1;
106   guint          has_secondary_text : 1;
107   guint          message_type       : 3;
108 };
109
110 static void gtk_message_dialog_style_set  (GtkWidget             *widget,
111                                            GtkStyle              *prev_style);
112
113 static void gtk_message_dialog_set_property (GObject          *object,
114                                              guint             prop_id,
115                                              const GValue     *value,
116                                              GParamSpec       *pspec);
117 static void gtk_message_dialog_get_property (GObject          *object,
118                                              guint             prop_id,
119                                              GValue           *value,
120                                              GParamSpec       *pspec);
121 static void gtk_message_dialog_add_buttons  (GtkMessageDialog *message_dialog,
122                                              GtkButtonsType    buttons);
123 static void      gtk_message_dialog_buildable_interface_init     (GtkBuildableIface *iface);
124 static GObject * gtk_message_dialog_buildable_get_internal_child (GtkBuildable  *buildable,
125                                                                   GtkBuilder    *builder,
126                                                                   const gchar   *childname);
127
128
129 enum {
130   PROP_0,
131   PROP_MESSAGE_TYPE,
132   PROP_BUTTONS,
133   PROP_TEXT,
134   PROP_USE_MARKUP,
135   PROP_SECONDARY_TEXT,
136   PROP_SECONDARY_USE_MARKUP,
137   PROP_IMAGE,
138   PROP_MESSAGE_AREA
139 };
140
141 G_DEFINE_TYPE_WITH_CODE (GtkMessageDialog, gtk_message_dialog, GTK_TYPE_DIALOG,
142                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
143                                                 gtk_message_dialog_buildable_interface_init))
144
145 static GtkBuildableIface *parent_buildable_iface;
146
147 static void
148 gtk_message_dialog_buildable_interface_init (GtkBuildableIface *iface)
149 {
150   parent_buildable_iface = g_type_interface_peek_parent (iface);
151   iface->get_internal_child = gtk_message_dialog_buildable_get_internal_child;
152   iface->custom_tag_start = parent_buildable_iface->custom_tag_start;
153   iface->custom_finished = parent_buildable_iface->custom_finished;
154 }
155
156 static GObject *
157 gtk_message_dialog_buildable_get_internal_child (GtkBuildable *buildable,
158                                                  GtkBuilder   *builder,
159                                                  const gchar  *childname)
160 {
161   if (strcmp (childname, "message_area") == 0)
162     return G_OBJECT (gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (buildable)));
163
164   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
165 }
166
167
168 static void
169 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
170 {
171   GtkWidgetClass *widget_class;
172   GObjectClass *gobject_class;
173
174   widget_class = GTK_WIDGET_CLASS (class);
175   gobject_class = G_OBJECT_CLASS (class);
176   
177   widget_class->style_set = gtk_message_dialog_style_set;
178
179   gobject_class->set_property = gtk_message_dialog_set_property;
180   gobject_class->get_property = gtk_message_dialog_get_property;
181   
182   gtk_widget_class_install_style_property (widget_class,
183                                            g_param_spec_int ("message-border",
184                                                              P_("Image/label border"),
185                                                              P_("Width of border around the label and image in the message dialog"),
186                                                              0,
187                                                              G_MAXINT,
188                                                              12,
189                                                              GTK_PARAM_READABLE));
190
191   /**
192    * GtkMessageDialog:message-type:
193    *
194    * The type of the message. The type is used to determine
195    * the image that is shown in the dialog, unless the image is 
196    * explicitly set by the ::image property.
197    */
198   g_object_class_install_property (gobject_class,
199                                    PROP_MESSAGE_TYPE,
200                                    g_param_spec_enum ("message-type",
201                                                       P_("Message Type"),
202                                                       P_("The type of message"),
203                                                       GTK_TYPE_MESSAGE_TYPE,
204                                                       GTK_MESSAGE_INFO,
205                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
206   g_object_class_install_property (gobject_class,
207                                    PROP_BUTTONS,
208                                    g_param_spec_enum ("buttons",
209                                                       P_("Message Buttons"),
210                                                       P_("The buttons shown in the message dialog"),
211                                                       GTK_TYPE_BUTTONS_TYPE,
212                                                       GTK_BUTTONS_NONE,
213                                                       GTK_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
214
215   /**
216    * GtkMessageDialog:text:
217    * 
218    * The primary text of the message dialog. If the dialog has 
219    * a secondary text, this will appear as the title.
220    *
221    * Since: 2.10
222    */
223   g_object_class_install_property (gobject_class,
224                                    PROP_TEXT,
225                                    g_param_spec_string ("text",
226                                                         P_("Text"),
227                                                         P_("The primary text of the message dialog"),
228                                                         "",
229                                                         GTK_PARAM_READWRITE));
230
231   /**
232    * GtkMessageDialog:use-markup:
233    * 
234    * %TRUE if the primary text of the dialog includes Pango markup. 
235    * See pango_parse_markup(). 
236    *
237    * Since: 2.10
238    */
239   g_object_class_install_property (gobject_class,
240                                    PROP_USE_MARKUP,
241                                    g_param_spec_boolean ("use-markup",
242                                                          P_("Use Markup"),
243                                                          P_("The primary text of the title includes Pango markup."),
244                                                          FALSE,
245                                                          GTK_PARAM_READWRITE));
246   
247   /**
248    * GtkMessageDialog:secondary-text:
249    * 
250    * The secondary text of the message dialog. 
251    *
252    * Since: 2.10
253    */
254   g_object_class_install_property (gobject_class,
255                                    PROP_SECONDARY_TEXT,
256                                    g_param_spec_string ("secondary-text",
257                                                         P_("Secondary Text"),
258                                                         P_("The secondary text of the message dialog"),
259                                                         NULL,
260                                                         GTK_PARAM_READWRITE));
261
262   /**
263    * GtkMessageDialog:secondary-use-markup:
264    * 
265    * %TRUE if the secondary text of the dialog includes Pango markup. 
266    * See pango_parse_markup(). 
267    *
268    * Since: 2.10
269    */
270   g_object_class_install_property (gobject_class,
271                                    PROP_SECONDARY_USE_MARKUP,
272                                    g_param_spec_boolean ("secondary-use-markup",
273                                                          P_("Use Markup in secondary"),
274                                                          P_("The secondary text includes Pango markup."),
275                                                          FALSE,
276                                                          GTK_PARAM_READWRITE));
277
278   /**
279    * GtkMessageDialog:image:
280    * 
281    * The image for this dialog.
282    *
283    * Since: 2.10
284    */
285   g_object_class_install_property (gobject_class,
286                                    PROP_IMAGE,
287                                    g_param_spec_object ("image",
288                                                         P_("Image"),
289                                                         P_("The image"),
290                                                         GTK_TYPE_WIDGET,
291                                                         GTK_PARAM_READWRITE));
292
293   /**
294    * GtkMessageDialog:message-area
295    *
296    * The #GtkVBox that corresponds to the message area of this dialog.  See
297    * gtk_message_dialog_get_message_area() for a detailed description of this
298    * area.
299    *
300    * Since: 2.22
301    */
302   g_object_class_install_property (gobject_class,
303                                    PROP_MESSAGE_AREA,
304                                    g_param_spec_object ("message-area",
305                                                         P_("Message area"),
306                                                         P_("GtkVBox that holds the dialog's primary and secondary labels"),
307                                                         GTK_TYPE_WIDGET,
308                                                         GTK_PARAM_READABLE));
309
310   g_type_class_add_private (gobject_class, sizeof (GtkMessageDialogPrivate));
311 }
312
313 static void
314 gtk_message_dialog_init (GtkMessageDialog *dialog)
315 {
316   GtkWidget *hbox;
317   GtkDialog *message_dialog = GTK_DIALOG (dialog);
318   GtkWidget *action_area, *content_area;
319   GtkMessageDialogPrivate *priv;
320
321   dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
322                                               GTK_TYPE_MESSAGE_DIALOG,
323                                               GtkMessageDialogPrivate);
324   priv = dialog->priv;
325
326   content_area = gtk_dialog_get_content_area (message_dialog);
327   action_area = gtk_dialog_get_action_area (message_dialog);
328
329   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
330   gtk_window_set_title (GTK_WINDOW (dialog), "");
331   gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), TRUE);
332
333   priv->has_primary_markup = FALSE;
334   priv->has_secondary_text = FALSE;
335   priv->secondary_label = gtk_label_new (NULL);
336   gtk_widget_set_no_show_all (priv->secondary_label, TRUE);
337
338   priv->label = gtk_label_new (NULL);
339   priv->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
340   gtk_misc_set_alignment (GTK_MISC (priv->image), 0.5, 0.0);
341   
342   gtk_label_set_line_wrap  (GTK_LABEL (priv->label), TRUE);
343   gtk_label_set_selectable (GTK_LABEL (priv->label), TRUE);
344   gtk_misc_set_alignment   (GTK_MISC  (priv->label), 0.0, 0.0);
345   
346   gtk_label_set_line_wrap  (GTK_LABEL (priv->secondary_label), TRUE);
347   gtk_label_set_selectable (GTK_LABEL (priv->secondary_label), TRUE);
348   gtk_misc_set_alignment   (GTK_MISC  (priv->secondary_label), 0.0, 0.0);
349
350   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 12);
351   priv->message_area = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 12);
352
353   gtk_box_pack_start (GTK_BOX (priv->message_area), priv->label,
354                       FALSE, FALSE, 0);
355
356   gtk_box_pack_start (GTK_BOX (priv->message_area), priv->secondary_label,
357                       TRUE, TRUE, 0);
358
359   gtk_box_pack_start (GTK_BOX (hbox), priv->image,
360                       FALSE, FALSE, 0);
361
362   gtk_box_pack_start (GTK_BOX (hbox), priv->message_area,
363                       TRUE, TRUE, 0);
364
365   gtk_box_pack_start (GTK_BOX (content_area),
366                       hbox,
367                       FALSE, FALSE, 0);
368
369   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
370   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
371   gtk_box_set_spacing (GTK_BOX (content_area), 14); /* 14 + 2 * 5 = 24 */
372   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
373   gtk_box_set_spacing (GTK_BOX (action_area), 6);
374
375   gtk_widget_show_all (hbox);
376 }
377
378 static void
379 setup_primary_label_font (GtkMessageDialog *dialog)
380 {
381   GtkMessageDialogPrivate *priv = dialog->priv;
382   gint size;
383   PangoFontDescription *font_desc;
384
385   /* unset the font settings */
386   gtk_widget_modify_font (priv->label, NULL);
387
388   if (priv->has_secondary_text && !priv->has_primary_markup)
389     {
390       size = pango_font_description_get_size (gtk_widget_get_style (priv->label)->font_desc);
391       font_desc = pango_font_description_new ();
392       pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
393       pango_font_description_set_size (font_desc, size * PANGO_SCALE_LARGE);
394       gtk_widget_modify_font (priv->label, font_desc);
395       pango_font_description_free (font_desc);
396     }
397 }
398
399 static void
400 setup_type (GtkMessageDialog *dialog,
401             GtkMessageType    type)
402 {
403   GtkMessageDialogPrivate *priv = dialog->priv;
404   const gchar *stock_id = NULL;
405   AtkObject *atk_obj;
406  
407   priv->message_type = type;
408
409   switch (type)
410     {
411     case GTK_MESSAGE_INFO:
412       stock_id = GTK_STOCK_DIALOG_INFO;
413       break;
414
415     case GTK_MESSAGE_QUESTION:
416       stock_id = GTK_STOCK_DIALOG_QUESTION;
417       break;
418
419     case GTK_MESSAGE_WARNING:
420       stock_id = GTK_STOCK_DIALOG_WARNING;
421       break;
422       
423     case GTK_MESSAGE_ERROR:
424       stock_id = GTK_STOCK_DIALOG_ERROR;
425       break;
426
427     case GTK_MESSAGE_OTHER:
428       break;
429
430     default:
431       g_warning ("Unknown GtkMessageType %u", type);
432       break;
433     }
434
435   if (stock_id)
436     gtk_image_set_from_stock (GTK_IMAGE (priv->image), stock_id,
437                               GTK_ICON_SIZE_DIALOG);
438       
439   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (dialog));
440   if (GTK_IS_ACCESSIBLE (atk_obj))
441     {
442       atk_object_set_role (atk_obj, ATK_ROLE_ALERT);
443       if (stock_id)
444         {
445           GtkStockItem item;
446
447           gtk_stock_lookup (stock_id, &item);
448           atk_object_set_name (atk_obj, item.label);
449         }
450     }
451 }
452
453 static void 
454 gtk_message_dialog_set_property (GObject      *object,
455                                  guint         prop_id,
456                                  const GValue *value,
457                                  GParamSpec   *pspec)
458 {
459   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object);
460   GtkMessageDialogPrivate *priv = dialog->priv;
461
462   switch (prop_id)
463     {
464     case PROP_MESSAGE_TYPE:
465       setup_type (dialog, g_value_get_enum (value));
466       break;
467     case PROP_BUTTONS:
468       gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
469       break;
470     case PROP_TEXT:
471       if (priv->has_primary_markup)
472         gtk_label_set_markup (GTK_LABEL (priv->label),
473                               g_value_get_string (value));
474       else
475         gtk_label_set_text (GTK_LABEL (priv->label),
476                             g_value_get_string (value));
477       break;
478     case PROP_USE_MARKUP:
479       priv->has_primary_markup = g_value_get_boolean (value) != FALSE;
480       gtk_label_set_use_markup (GTK_LABEL (priv->label),
481                                 priv->has_primary_markup);
482       setup_primary_label_font (dialog);
483       break;
484     case PROP_SECONDARY_TEXT:
485       {
486         const gchar *txt = g_value_get_string (value);
487
488         if (gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)))
489           gtk_label_set_markup (GTK_LABEL (priv->secondary_label), txt);
490         else
491           gtk_label_set_text (GTK_LABEL (priv->secondary_label), txt);
492
493         if (txt)
494           {
495             priv->has_secondary_text = TRUE;
496             gtk_widget_show (priv->secondary_label);
497           }
498         else
499           {
500             priv->has_secondary_text = FALSE;
501             gtk_widget_hide (priv->secondary_label);
502           }
503         setup_primary_label_font (dialog);
504       }
505       break;
506     case PROP_SECONDARY_USE_MARKUP:
507       gtk_label_set_use_markup (GTK_LABEL (priv->secondary_label), 
508                                 g_value_get_boolean (value));
509       break;
510     case PROP_IMAGE:
511       gtk_message_dialog_set_image (dialog, g_value_get_object (value));
512       break;
513
514     default:
515       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
516       break;
517     }
518 }
519
520 static void 
521 gtk_message_dialog_get_property (GObject     *object,
522                                  guint        prop_id,
523                                  GValue      *value,
524                                  GParamSpec  *pspec)
525 {
526   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object);
527   GtkMessageDialogPrivate *priv = dialog->priv;
528
529   switch (prop_id)
530     {
531     case PROP_MESSAGE_TYPE:
532       g_value_set_enum (value, (GtkMessageType) priv->message_type);
533       break;
534     case PROP_TEXT:
535       g_value_set_string (value, gtk_label_get_label (GTK_LABEL (priv->label)));
536       break;
537     case PROP_USE_MARKUP:
538       g_value_set_boolean (value, priv->has_primary_markup);
539       break;
540     case PROP_SECONDARY_TEXT:
541       if (priv->has_secondary_text)
542       g_value_set_string (value, 
543                           gtk_label_get_label (GTK_LABEL (priv->secondary_label)));
544       else
545         g_value_set_string (value, NULL);
546       break;
547     case PROP_SECONDARY_USE_MARKUP:
548       if (priv->has_secondary_text)
549         g_value_set_boolean (value, 
550                              gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)));
551       else
552         g_value_set_boolean (value, FALSE);
553       break;
554     case PROP_IMAGE:
555       g_value_set_object (value, priv->image);
556       break;
557     case PROP_MESSAGE_AREA:
558       g_value_set_object (value, priv->message_area);
559       break;
560     default:
561       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
562       break;
563     }
564 }
565
566 /**
567  * gtk_message_dialog_new:
568  * @parent: (allow-none): transient parent, or %NULL for none
569  * @flags: flags
570  * @type: type of message
571  * @buttons: set of buttons to use
572  * @message_format: (allow-none): printf()-style format string, or %NULL
573  * @Varargs: arguments for @message_format
574  *
575  * Creates a new message dialog, which is a simple dialog with an icon
576  * indicating the dialog type (error, warning, etc.) and some text the
577  * user may want to see. When the user clicks a button a "response"
578  * signal is emitted with response IDs from #GtkResponseType. See
579  * #GtkDialog for more details.
580  *
581  * Return value: (transfer none): a new #GtkMessageDialog
582  **/
583 GtkWidget*
584 gtk_message_dialog_new (GtkWindow     *parent,
585                         GtkDialogFlags flags,
586                         GtkMessageType type,
587                         GtkButtonsType buttons,
588                         const gchar   *message_format,
589                         ...)
590 {
591   GtkWidget *widget;
592   GtkDialog *dialog;
593   gchar* msg = NULL;
594   va_list args;
595
596   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
597
598   widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
599                          "message-type", type,
600                          "buttons", buttons,
601                          NULL);
602   dialog = GTK_DIALOG (widget);
603
604   if (message_format)
605     {
606       va_start (args, message_format);
607       msg = g_strdup_vprintf (message_format, args);
608       va_end (args);
609
610       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->priv->label),
611                           msg);
612
613       g_free (msg);
614     }
615
616   if (parent != NULL)
617     gtk_window_set_transient_for (GTK_WINDOW (widget),
618                                   GTK_WINDOW (parent));
619   
620   if (flags & GTK_DIALOG_MODAL)
621     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
622
623   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
624     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
625
626   return widget;
627 }
628
629 /**
630  * gtk_message_dialog_new_with_markup:
631  * @parent: (allow-none): transient parent, or %NULL for none 
632  * @flags: flags
633  * @type: type of message
634  * @buttons: set of buttons to use
635  * @message_format: (allow-none): printf()-style format string, or %NULL
636  * @Varargs: arguments for @message_format
637  * 
638  * Creates a new message dialog, which is a simple dialog with an icon
639  * indicating the dialog type (error, warning, etc.) and some text which
640  * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
641  * When the user clicks a button a "response" signal is emitted with
642  * response IDs from #GtkResponseType. See #GtkDialog for more details.
643  *
644  * Special XML characters in the printf() arguments passed to this
645  * function will automatically be escaped as necessary.
646  * (See g_markup_printf_escaped() for how this is implemented.)
647  * Usually this is what you want, but if you have an existing
648  * Pango markup string that you want to use literally as the
649  * label, then you need to use gtk_message_dialog_set_markup()
650  * instead, since you can't pass the markup string either
651  * as the format (it might contain '%' characters) or as a string
652  * argument.
653  * |[
654  *  GtkWidget *dialog;
655  *  dialog = gtk_message_dialog_new (main_application_window,
656  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
657  *                                   GTK_MESSAGE_ERROR,
658  *                                   GTK_BUTTONS_CLOSE,
659  *                                   NULL);
660  *  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
661  *                                 markup);
662  * ]|
663  * 
664  * Return value: a new #GtkMessageDialog
665  *
666  * Since: 2.4
667  **/
668 GtkWidget*
669 gtk_message_dialog_new_with_markup (GtkWindow     *parent,
670                                     GtkDialogFlags flags,
671                                     GtkMessageType type,
672                                     GtkButtonsType buttons,
673                                     const gchar   *message_format,
674                                     ...)
675 {
676   GtkWidget *widget;
677   va_list args;
678   gchar *msg = NULL;
679
680   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
681
682   widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
683
684   if (message_format)
685     {
686       va_start (args, message_format);
687       msg = g_markup_vprintf_escaped (message_format, args);
688       va_end (args);
689
690       gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
691
692       g_free (msg);
693     }
694
695   return widget;
696 }
697
698 /**
699  * gtk_message_dialog_set_image:
700  * @dialog: a #GtkMessageDialog
701  * @image: the image
702  * 
703  * Sets the dialog's image to @image.
704  *
705  * Since: 2.10
706  **/
707 void
708 gtk_message_dialog_set_image (GtkMessageDialog *dialog,
709                               GtkWidget        *image)
710 {
711   GtkMessageDialogPrivate *priv;
712   GtkWidget *parent;
713
714   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (dialog));
715   g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
716
717   priv = dialog->priv;
718
719   if (image == NULL)
720     {
721       image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
722       gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
723     }
724
725   priv->message_type = GTK_MESSAGE_OTHER;
726
727   parent = gtk_widget_get_parent (priv->image);
728   gtk_container_add (GTK_CONTAINER (parent), image);
729   gtk_container_remove (GTK_CONTAINER (parent), priv->image);
730   gtk_box_reorder_child (GTK_BOX (parent), image, 0);
731
732   priv->image = image;
733
734   g_object_notify (G_OBJECT (dialog), "image");
735 }
736
737 /**
738  * gtk_message_dialog_get_image:
739  * @dialog: a #GtkMessageDialog
740  *
741  * Gets the dialog's image.
742  *
743  * Return value: (transfer none): the dialog's image
744  *
745  * Since: 2.14
746  **/
747 GtkWidget *
748 gtk_message_dialog_get_image (GtkMessageDialog *dialog)
749 {
750   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), NULL);
751
752   return dialog->priv->image;
753 }
754
755 /**
756  * gtk_message_dialog_set_markup:
757  * @message_dialog: a #GtkMessageDialog
758  * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
759  * 
760  * Sets the text of the message dialog to be @str, which is marked
761  * up with the <link linkend="PangoMarkupFormat">Pango text markup
762  * language</link>.
763  *
764  * Since: 2.4
765  **/
766 void
767 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
768                                const gchar      *str)
769 {
770   GtkMessageDialogPrivate *priv;
771
772   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
773
774   priv = message_dialog->priv;
775
776   priv->has_primary_markup = TRUE;
777   gtk_label_set_markup (GTK_LABEL (priv->label), str);
778 }
779
780 /**
781  * gtk_message_dialog_format_secondary_text:
782  * @message_dialog: a #GtkMessageDialog
783  * @message_format: (allow-none): printf()-style format string, or %NULL
784  * @Varargs: arguments for @message_format
785  * 
786  * Sets the secondary text of the message dialog to be @message_format 
787  * (with printf()-style).
788  *
789  * Note that setting a secondary text makes the primary text become
790  * bold, unless you have provided explicit markup.
791  *
792  * Since: 2.6
793  **/
794 void
795 gtk_message_dialog_format_secondary_text (GtkMessageDialog *message_dialog,
796                                           const gchar      *message_format,
797                                           ...)
798 {
799   va_list args;
800   gchar *msg = NULL;
801   GtkMessageDialogPrivate *priv;
802
803   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
804
805   priv = message_dialog->priv;
806
807   if (message_format)
808     {
809       priv->has_secondary_text = TRUE;
810
811       va_start (args, message_format);
812       msg = g_strdup_vprintf (message_format, args);
813       va_end (args);
814
815       gtk_widget_show (priv->secondary_label);
816       gtk_label_set_text (GTK_LABEL (priv->secondary_label), msg);
817
818       g_free (msg);
819     }
820   else
821     {
822       priv->has_secondary_text = FALSE;
823       gtk_widget_hide (priv->secondary_label);
824     }
825
826   setup_primary_label_font (message_dialog);
827 }
828
829 /**
830  * gtk_message_dialog_format_secondary_markup:
831  * @message_dialog: a #GtkMessageDialog
832  * @message_format: printf()-style markup string (see 
833      <link linkend="PangoMarkupFormat">Pango markup format</link>), or %NULL
834  * @Varargs: arguments for @message_format
835  * 
836  * Sets the secondary text of the message dialog to be @message_format (with 
837  * printf()-style), which is marked up with the 
838  * <link linkend="PangoMarkupFormat">Pango text markup language</link>.
839  *
840  * Note that setting a secondary text makes the primary text become
841  * bold, unless you have provided explicit markup.
842  *
843  * Due to an oversight, this function does not escape special XML characters
844  * like gtk_message_dialog_new_with_markup() does. Thus, if the arguments 
845  * may contain special XML characters, you should use g_markup_printf_escaped()
846  * to escape it.
847
848  * <informalexample><programlisting>
849  * gchar *msg;
850  *  
851  * msg = g_markup_printf_escaped (message_format, ...);
852  * gtk_message_dialog_format_secondary_markup (message_dialog, "&percnt;s", msg);
853  * g_free (msg);
854  * </programlisting></informalexample>
855  *
856  * Since: 2.6
857  **/
858 void
859 gtk_message_dialog_format_secondary_markup (GtkMessageDialog *message_dialog,
860                                             const gchar      *message_format,
861                                             ...)
862 {
863   va_list args;
864   gchar *msg = NULL;
865   GtkMessageDialogPrivate *priv;
866
867   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
868
869   priv = message_dialog->priv;
870
871   if (message_format)
872     {
873       priv->has_secondary_text = TRUE;
874
875       va_start (args, message_format);
876       msg = g_strdup_vprintf (message_format, args);
877       va_end (args);
878
879       gtk_widget_show (priv->secondary_label);
880       gtk_label_set_markup (GTK_LABEL (priv->secondary_label), msg);
881
882       g_free (msg);
883     }
884   else
885     {
886       priv->has_secondary_text = FALSE;
887       gtk_widget_hide (priv->secondary_label);
888     }
889
890   setup_primary_label_font (message_dialog);
891 }
892
893 /**
894  * gtk_message_dialog_get_message_area:
895  * @message_dialog: a #GtkMessageDialog
896  *
897  * Returns the message area of the dialog. This is the box where the
898  * dialog's primary and secondary labels are packed. You can add your
899  * own extra content to that box and it will appear below those labels,
900  * on the right side of the dialog's image (or on the left for right-to-left
901  * languages).  See gtk_dialog_get_content_area() for the corresponding
902  * function in the parent #GtkDialog.
903  *
904  * Return value: (transfer none): A #GtkVBox corresponding to the
905  *     "message area" in the @message_dialog.
906  *
907  * Since: 2.22
908  **/
909 GtkWidget *
910 gtk_message_dialog_get_message_area (GtkMessageDialog *message_dialog)
911 {
912   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog), NULL);
913
914   return message_dialog->priv->message_area;
915 }
916
917 static void
918 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
919                                 GtkButtonsType buttons)
920 {
921   GtkDialog* dialog = GTK_DIALOG (message_dialog);
922
923   switch (buttons)
924     {
925     case GTK_BUTTONS_NONE:
926       /* nothing */
927       break;
928
929     case GTK_BUTTONS_OK:
930       gtk_dialog_add_button (dialog,
931                              GTK_STOCK_OK,
932                              GTK_RESPONSE_OK);
933       break;
934
935     case GTK_BUTTONS_CLOSE:
936       gtk_dialog_add_button (dialog,
937                              GTK_STOCK_CLOSE,
938                              GTK_RESPONSE_CLOSE);
939       break;
940
941     case GTK_BUTTONS_CANCEL:
942       gtk_dialog_add_button (dialog,
943                              GTK_STOCK_CANCEL,
944                              GTK_RESPONSE_CANCEL);
945       break;
946
947     case GTK_BUTTONS_YES_NO:
948       gtk_dialog_add_button (dialog,
949                              GTK_STOCK_NO,
950                              GTK_RESPONSE_NO);
951       gtk_dialog_add_button (dialog,
952                              GTK_STOCK_YES,
953                              GTK_RESPONSE_YES);
954       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
955                                                GTK_RESPONSE_YES,
956                                                GTK_RESPONSE_NO,
957                                                -1);
958       break;
959
960     case GTK_BUTTONS_OK_CANCEL:
961       gtk_dialog_add_button (dialog,
962                              GTK_STOCK_CANCEL,
963                              GTK_RESPONSE_CANCEL);
964       gtk_dialog_add_button (dialog,
965                              GTK_STOCK_OK,
966                              GTK_RESPONSE_OK);
967       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
968                                                GTK_RESPONSE_OK,
969                                                GTK_RESPONSE_CANCEL,
970                                                -1);
971       break;
972       
973     default:
974       g_warning ("Unknown GtkButtonsType");
975       break;
976     } 
977
978   g_object_notify (G_OBJECT (message_dialog), "buttons");
979 }
980
981 static void
982 gtk_message_dialog_style_set (GtkWidget *widget,
983                               GtkStyle  *prev_style)
984 {
985   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (widget);
986   GtkWidget *parent;
987   gint border_width;
988
989   parent = gtk_widget_get_parent (gtk_message_dialog_get_image (dialog));
990
991   if (parent)
992     {
993       gtk_widget_style_get (widget, "message-border",
994                             &border_width, NULL);
995       
996       gtk_container_set_border_width (GTK_CONTAINER (parent),
997                                       MAX (0, border_width - 7));
998     }
999
1000   setup_primary_label_font (dialog);
1001
1002   GTK_WIDGET_CLASS (gtk_message_dialog_parent_class)->style_set (widget, prev_style);
1003 }