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