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