]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
gtk/gtkmessagedialog.c: use accessor functions to access GtkWidget
[~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 _GtkMessageDialogPriv
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    * GtkMessageDialog:use-separator:
192    *
193    * Whether to draw a separator line between the message label and the buttons
194    * in the dialog.
195    *
196    * Since: 2.4
197    */
198   gtk_widget_class_install_style_property (widget_class,
199                                            g_param_spec_boolean ("use-separator",
200                                                                  P_("Use separator"),
201                                                                  P_("Whether to put a separator between the message dialog's text and the buttons"),
202                                                                  FALSE,
203                                                                  GTK_PARAM_READABLE));
204   /**
205    * GtkMessageDialog:message-type:
206    *
207    * The type of the message. The type is used to determine
208    * the image that is shown in the dialog, unless the image is 
209    * explicitly set by the ::image property.
210    */
211   g_object_class_install_property (gobject_class,
212                                    PROP_MESSAGE_TYPE,
213                                    g_param_spec_enum ("message-type",
214                                                       P_("Message Type"),
215                                                       P_("The type of message"),
216                                                       GTK_TYPE_MESSAGE_TYPE,
217                                                       GTK_MESSAGE_INFO,
218                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
219   g_object_class_install_property (gobject_class,
220                                    PROP_BUTTONS,
221                                    g_param_spec_enum ("buttons",
222                                                       P_("Message Buttons"),
223                                                       P_("The buttons shown in the message dialog"),
224                                                       GTK_TYPE_BUTTONS_TYPE,
225                                                       GTK_BUTTONS_NONE,
226                                                       GTK_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
227
228   /**
229    * GtkMessageDialog:text:
230    * 
231    * The primary text of the message dialog. If the dialog has 
232    * a secondary text, this will appear as the title.
233    *
234    * Since: 2.10
235    */
236   g_object_class_install_property (gobject_class,
237                                    PROP_TEXT,
238                                    g_param_spec_string ("text",
239                                                         P_("Text"),
240                                                         P_("The primary text of the message dialog"),
241                                                         "",
242                                                         GTK_PARAM_READWRITE));
243
244   /**
245    * GtkMessageDialog:use-markup:
246    * 
247    * %TRUE if the primary text of the dialog includes Pango markup. 
248    * See pango_parse_markup(). 
249    *
250    * Since: 2.10
251    */
252   g_object_class_install_property (gobject_class,
253                                    PROP_USE_MARKUP,
254                                    g_param_spec_boolean ("use-markup",
255                                                          P_("Use Markup"),
256                                                          P_("The primary text of the title includes Pango markup."),
257                                                          FALSE,
258                                                          GTK_PARAM_READWRITE));
259   
260   /**
261    * GtkMessageDialog:secondary-text:
262    * 
263    * The secondary text of the message dialog. 
264    *
265    * Since: 2.10
266    */
267   g_object_class_install_property (gobject_class,
268                                    PROP_SECONDARY_TEXT,
269                                    g_param_spec_string ("secondary-text",
270                                                         P_("Secondary Text"),
271                                                         P_("The secondary text of the message dialog"),
272                                                         NULL,
273                                                         GTK_PARAM_READWRITE));
274
275   /**
276    * GtkMessageDialog:secondary-use-markup:
277    * 
278    * %TRUE if the secondary text of the dialog includes Pango markup. 
279    * See pango_parse_markup(). 
280    *
281    * Since: 2.10
282    */
283   g_object_class_install_property (gobject_class,
284                                    PROP_SECONDARY_USE_MARKUP,
285                                    g_param_spec_boolean ("secondary-use-markup",
286                                                          P_("Use Markup in secondary"),
287                                                          P_("The secondary text includes Pango markup."),
288                                                          FALSE,
289                                                          GTK_PARAM_READWRITE));
290
291   /**
292    * GtkMessageDialog:image:
293    * 
294    * The image for this dialog.
295    *
296    * Since: 2.10
297    */
298   g_object_class_install_property (gobject_class,
299                                    PROP_IMAGE,
300                                    g_param_spec_object ("image",
301                                                         P_("Image"),
302                                                         P_("The image"),
303                                                         GTK_TYPE_WIDGET,
304                                                         GTK_PARAM_READWRITE));
305
306   /**
307    * GtkMessageDialog:message-area
308    *
309    * The #GtkVBox that corresponds to the message area of this dialog.  See
310    * gtk_message_dialog_get_message_area() for a detailed description of this
311    * area.
312    *
313    * Since: 2.22
314    */
315   g_object_class_install_property (gobject_class,
316                                    PROP_MESSAGE_AREA,
317                                    g_param_spec_object ("message-area",
318                                                         P_("Message area"),
319                                                         P_("GtkVBox that holds the dialog's primary and secondary labels"),
320                                                         GTK_TYPE_WIDGET,
321                                                         GTK_PARAM_READABLE));
322
323   g_type_class_add_private (gobject_class, sizeof (GtkMessageDialogPriv));
324 }
325
326 static void
327 gtk_message_dialog_init (GtkMessageDialog *dialog)
328 {
329   GtkWidget *hbox;
330   GtkDialog *message_dialog = GTK_DIALOG (dialog);
331   GtkWidget *action_area, *content_area;
332   GtkMessageDialogPriv *priv;
333
334   dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
335                                               GTK_TYPE_MESSAGE_DIALOG,
336                                               GtkMessageDialogPriv);
337   priv = dialog->priv;
338
339   content_area = gtk_dialog_get_content_area (message_dialog);
340   action_area = gtk_dialog_get_action_area (message_dialog);
341
342   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
343   gtk_window_set_title (GTK_WINDOW (dialog), "");
344   gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), TRUE);
345
346   priv->has_primary_markup = FALSE;
347   priv->has_secondary_text = FALSE;
348   priv->secondary_label = gtk_label_new (NULL);
349   gtk_widget_set_no_show_all (priv->secondary_label, TRUE);
350
351   priv->label = gtk_label_new (NULL);
352   priv->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
353   gtk_misc_set_alignment (GTK_MISC (priv->image), 0.5, 0.0);
354   
355   gtk_label_set_line_wrap  (GTK_LABEL (priv->label), TRUE);
356   gtk_label_set_selectable (GTK_LABEL (priv->label), TRUE);
357   gtk_misc_set_alignment   (GTK_MISC  (priv->label), 0.0, 0.0);
358   
359   gtk_label_set_line_wrap  (GTK_LABEL (priv->secondary_label), TRUE);
360   gtk_label_set_selectable (GTK_LABEL (priv->secondary_label), TRUE);
361   gtk_misc_set_alignment   (GTK_MISC  (priv->secondary_label), 0.0, 0.0);
362
363   hbox = gtk_hbox_new (FALSE, 12);
364   priv->message_area = gtk_vbox_new (FALSE, 12);
365
366   gtk_box_pack_start (GTK_BOX (priv->message_area), priv->label,
367                       FALSE, FALSE, 0);
368
369   gtk_box_pack_start (GTK_BOX (priv->message_area), priv->secondary_label,
370                       TRUE, TRUE, 0);
371
372   gtk_box_pack_start (GTK_BOX (hbox), priv->image,
373                       FALSE, FALSE, 0);
374
375   gtk_box_pack_start (GTK_BOX (hbox), priv->message_area,
376                       TRUE, TRUE, 0);
377
378   gtk_box_pack_start (GTK_BOX (content_area),
379                       hbox,
380                       FALSE, FALSE, 0);
381
382   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
383   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
384   gtk_box_set_spacing (GTK_BOX (content_area), 14); /* 14 + 2 * 5 = 24 */
385   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
386   gtk_box_set_spacing (GTK_BOX (action_area), 6);
387
388   gtk_widget_show_all (hbox);
389
390   _gtk_dialog_set_ignore_separator (GTK_DIALOG (dialog), TRUE);
391 }
392
393 static void
394 setup_primary_label_font (GtkMessageDialog *dialog)
395 {
396   GtkMessageDialogPriv *priv = dialog->priv;
397   gint size;
398   PangoFontDescription *font_desc;
399
400   /* unset the font settings */
401   gtk_widget_modify_font (priv->label, NULL);
402
403   if (priv->has_secondary_text && !priv->has_primary_markup)
404     {
405       size = pango_font_description_get_size (gtk_widget_get_style (priv->label)->font_desc);
406       font_desc = pango_font_description_new ();
407       pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
408       pango_font_description_set_size (font_desc, size * PANGO_SCALE_LARGE);
409       gtk_widget_modify_font (priv->label, font_desc);
410       pango_font_description_free (font_desc);
411     }
412 }
413
414 static void
415 setup_type (GtkMessageDialog *dialog,
416             GtkMessageType    type)
417 {
418   GtkMessageDialogPriv *priv = dialog->priv;
419   const gchar *stock_id = NULL;
420   AtkObject *atk_obj;
421  
422   priv->message_type = type;
423
424   switch (type)
425     {
426     case GTK_MESSAGE_INFO:
427       stock_id = GTK_STOCK_DIALOG_INFO;
428       break;
429
430     case GTK_MESSAGE_QUESTION:
431       stock_id = GTK_STOCK_DIALOG_QUESTION;
432       break;
433
434     case GTK_MESSAGE_WARNING:
435       stock_id = GTK_STOCK_DIALOG_WARNING;
436       break;
437       
438     case GTK_MESSAGE_ERROR:
439       stock_id = GTK_STOCK_DIALOG_ERROR;
440       break;
441
442     case GTK_MESSAGE_OTHER:
443       break;
444
445     default:
446       g_warning ("Unknown GtkMessageType %u", type);
447       break;
448     }
449
450   if (stock_id)
451     gtk_image_set_from_stock (GTK_IMAGE (priv->image), stock_id,
452                               GTK_ICON_SIZE_DIALOG);
453       
454   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (dialog));
455   if (GTK_IS_ACCESSIBLE (atk_obj))
456     {
457       atk_object_set_role (atk_obj, ATK_ROLE_ALERT);
458       if (stock_id)
459         {
460           GtkStockItem item;
461
462           gtk_stock_lookup (stock_id, &item);
463           atk_object_set_name (atk_obj, item.label);
464         }
465     }
466 }
467
468 static void 
469 gtk_message_dialog_set_property (GObject      *object,
470                                  guint         prop_id,
471                                  const GValue *value,
472                                  GParamSpec   *pspec)
473 {
474   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object);
475   GtkMessageDialogPriv *priv = dialog->priv;
476
477   switch (prop_id)
478     {
479     case PROP_MESSAGE_TYPE:
480       setup_type (dialog, g_value_get_enum (value));
481       break;
482     case PROP_BUTTONS:
483       gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
484       break;
485     case PROP_TEXT:
486       if (priv->has_primary_markup)
487         gtk_label_set_markup (GTK_LABEL (priv->label),
488                               g_value_get_string (value));
489       else
490         gtk_label_set_text (GTK_LABEL (priv->label),
491                             g_value_get_string (value));
492       break;
493     case PROP_USE_MARKUP:
494       priv->has_primary_markup = g_value_get_boolean (value) != FALSE;
495       gtk_label_set_use_markup (GTK_LABEL (priv->label),
496                                 priv->has_primary_markup);
497       setup_primary_label_font (dialog);
498       break;
499     case PROP_SECONDARY_TEXT:
500       {
501         const gchar *txt = g_value_get_string (value);
502
503         if (gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)))
504           gtk_label_set_markup (GTK_LABEL (priv->secondary_label), txt);
505         else
506           gtk_label_set_text (GTK_LABEL (priv->secondary_label), txt);
507
508         if (txt)
509           {
510             priv->has_secondary_text = TRUE;
511             gtk_widget_show (priv->secondary_label);
512           }
513         else
514           {
515             priv->has_secondary_text = FALSE;
516             gtk_widget_hide (priv->secondary_label);
517           }
518         setup_primary_label_font (dialog);
519       }
520       break;
521     case PROP_SECONDARY_USE_MARKUP:
522       gtk_label_set_use_markup (GTK_LABEL (priv->secondary_label), 
523                                 g_value_get_boolean (value));
524       break;
525     case PROP_IMAGE:
526       gtk_message_dialog_set_image (dialog, g_value_get_object (value));
527       break;
528
529     default:
530       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531       break;
532     }
533 }
534
535 static void 
536 gtk_message_dialog_get_property (GObject     *object,
537                                  guint        prop_id,
538                                  GValue      *value,
539                                  GParamSpec  *pspec)
540 {
541   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object);
542   GtkMessageDialogPriv *priv = dialog->priv;
543
544   switch (prop_id)
545     {
546     case PROP_MESSAGE_TYPE:
547       g_value_set_enum (value, (GtkMessageType) priv->message_type);
548       break;
549     case PROP_TEXT:
550       g_value_set_string (value, gtk_label_get_label (GTK_LABEL (priv->label)));
551       break;
552     case PROP_USE_MARKUP:
553       g_value_set_boolean (value, priv->has_primary_markup);
554       break;
555     case PROP_SECONDARY_TEXT:
556       if (priv->has_secondary_text)
557       g_value_set_string (value, 
558                           gtk_label_get_label (GTK_LABEL (priv->secondary_label)));
559       else
560         g_value_set_string (value, NULL);
561       break;
562     case PROP_SECONDARY_USE_MARKUP:
563       if (priv->has_secondary_text)
564         g_value_set_boolean (value, 
565                              gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)));
566       else
567         g_value_set_boolean (value, FALSE);
568       break;
569     case PROP_IMAGE:
570       g_value_set_object (value, priv->image);
571       break;
572     case PROP_MESSAGE_AREA:
573       g_value_set_object (value, priv->message_area);
574       break;
575     default:
576       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
577       break;
578     }
579 }
580
581 /**
582  * gtk_message_dialog_new:
583  * @parent: (allow-none): transient parent, or %NULL for none
584  * @flags: flags
585  * @type: type of message
586  * @buttons: set of buttons to use
587  * @message_format: (allow-none): printf()-style format string, or %NULL
588  * @Varargs: arguments for @message_format
589  *
590  * Creates a new message dialog, which is a simple dialog with an icon
591  * indicating the dialog type (error, warning, etc.) and some text the
592  * user may want to see. When the user clicks a button a "response"
593  * signal is emitted with response IDs from #GtkResponseType. See
594  * #GtkDialog for more details.
595  *
596  * Return value: (transfer none): a new #GtkMessageDialog
597  **/
598 GtkWidget*
599 gtk_message_dialog_new (GtkWindow     *parent,
600                         GtkDialogFlags flags,
601                         GtkMessageType type,
602                         GtkButtonsType buttons,
603                         const gchar   *message_format,
604                         ...)
605 {
606   GtkWidget *widget;
607   GtkDialog *dialog;
608   gchar* msg = NULL;
609   va_list args;
610
611   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
612
613   widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
614                          "message-type", type,
615                          "buttons", buttons,
616                          NULL);
617   dialog = GTK_DIALOG (widget);
618
619   if (flags & GTK_DIALOG_NO_SEPARATOR)
620     {
621       g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
622       flags &= ~GTK_DIALOG_NO_SEPARATOR;
623     }
624
625   if (message_format)
626     {
627       va_start (args, message_format);
628       msg = g_strdup_vprintf (message_format, args);
629       va_end (args);
630
631       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->priv->label),
632                           msg);
633
634       g_free (msg);
635     }
636
637   if (parent != NULL)
638     gtk_window_set_transient_for (GTK_WINDOW (widget),
639                                   GTK_WINDOW (parent));
640   
641   if (flags & GTK_DIALOG_MODAL)
642     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
643
644   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
645     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
646
647   return widget;
648 }
649
650 /**
651  * gtk_message_dialog_new_with_markup:
652  * @parent: (allow-none): transient parent, or %NULL for none 
653  * @flags: flags
654  * @type: type of message
655  * @buttons: set of buttons to use
656  * @message_format: (allow-none): printf()-style format string, or %NULL
657  * @Varargs: arguments for @message_format
658  * 
659  * Creates a new message dialog, which is a simple dialog with an icon
660  * indicating the dialog type (error, warning, etc.) and some text which
661  * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
662  * When the user clicks a button a "response" signal is emitted with
663  * response IDs from #GtkResponseType. See #GtkDialog for more details.
664  *
665  * Special XML characters in the printf() arguments passed to this
666  * function will automatically be escaped as necessary.
667  * (See g_markup_printf_escaped() for how this is implemented.)
668  * Usually this is what you want, but if you have an existing
669  * Pango markup string that you want to use literally as the
670  * label, then you need to use gtk_message_dialog_set_markup()
671  * instead, since you can't pass the markup string either
672  * as the format (it might contain '%' characters) or as a string
673  * argument.
674  * |[
675  *  GtkWidget *dialog;
676  *  dialog = gtk_message_dialog_new (main_application_window,
677  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
678  *                                   GTK_MESSAGE_ERROR,
679  *                                   GTK_BUTTONS_CLOSE,
680  *                                   NULL);
681  *  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
682  *                                 markup);
683  * ]|
684  * 
685  * Return value: a new #GtkMessageDialog
686  *
687  * Since: 2.4
688  **/
689 GtkWidget*
690 gtk_message_dialog_new_with_markup (GtkWindow     *parent,
691                                     GtkDialogFlags flags,
692                                     GtkMessageType type,
693                                     GtkButtonsType buttons,
694                                     const gchar   *message_format,
695                                     ...)
696 {
697   GtkWidget *widget;
698   va_list args;
699   gchar *msg = NULL;
700
701   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
702
703   widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
704
705   if (message_format)
706     {
707       va_start (args, message_format);
708       msg = g_markup_vprintf_escaped (message_format, args);
709       va_end (args);
710
711       gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
712
713       g_free (msg);
714     }
715
716   return widget;
717 }
718
719 /**
720  * gtk_message_dialog_set_image:
721  * @dialog: a #GtkMessageDialog
722  * @image: the image
723  * 
724  * Sets the dialog's image to @image.
725  *
726  * Since: 2.10
727  **/
728 void
729 gtk_message_dialog_set_image (GtkMessageDialog *dialog,
730                               GtkWidget        *image)
731 {
732   GtkMessageDialogPriv *priv;
733   GtkWidget *parent;
734
735   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (dialog));
736   g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
737
738   priv = dialog->priv;
739
740   if (image == NULL)
741     {
742       image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
743       gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
744     }
745
746   priv->message_type = GTK_MESSAGE_OTHER;
747
748   parent = gtk_widget_get_parent (priv->image);
749   gtk_container_add (GTK_CONTAINER (parent), image);
750   gtk_container_remove (GTK_CONTAINER (parent), priv->image);
751   gtk_box_reorder_child (GTK_BOX (parent), image, 0);
752
753   priv->image = image;
754
755   g_object_notify (G_OBJECT (dialog), "image");
756 }
757
758 /**
759  * gtk_message_dialog_get_image:
760  * @dialog: a #GtkMessageDialog
761  *
762  * Gets the dialog's image.
763  *
764  * Return value: the dialog's image
765  *
766  * Since: 2.14
767  **/
768 GtkWidget *
769 gtk_message_dialog_get_image (GtkMessageDialog *dialog)
770 {
771   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), NULL);
772
773   return dialog->priv->image;
774 }
775
776 /**
777  * gtk_message_dialog_set_markup:
778  * @message_dialog: a #GtkMessageDialog
779  * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
780  * 
781  * Sets the text of the message dialog to be @str, which is marked
782  * up with the <link linkend="PangoMarkupFormat">Pango text markup
783  * language</link>.
784  *
785  * Since: 2.4
786  **/
787 void
788 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
789                                const gchar      *str)
790 {
791   GtkMessageDialogPriv *priv;
792
793   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
794
795   priv = message_dialog->priv;
796
797   priv->has_primary_markup = TRUE;
798   gtk_label_set_markup (GTK_LABEL (priv->label), str);
799 }
800
801 /**
802  * gtk_message_dialog_format_secondary_text:
803  * @message_dialog: a #GtkMessageDialog
804  * @message_format: (allow-none): printf()-style format string, or %NULL
805  * @Varargs: arguments for @message_format
806  * 
807  * Sets the secondary text of the message dialog to be @message_format 
808  * (with printf()-style).
809  *
810  * Note that setting a secondary text makes the primary text become
811  * bold, unless you have provided explicit markup.
812  *
813  * Since: 2.6
814  **/
815 void
816 gtk_message_dialog_format_secondary_text (GtkMessageDialog *message_dialog,
817                                           const gchar      *message_format,
818                                           ...)
819 {
820   va_list args;
821   gchar *msg = NULL;
822   GtkMessageDialogPriv *priv;
823
824   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
825
826   priv = message_dialog->priv;
827
828   if (message_format)
829     {
830       priv->has_secondary_text = TRUE;
831
832       va_start (args, message_format);
833       msg = g_strdup_vprintf (message_format, args);
834       va_end (args);
835
836       gtk_widget_show (priv->secondary_label);
837       gtk_label_set_text (GTK_LABEL (priv->secondary_label), msg);
838
839       g_free (msg);
840     }
841   else
842     {
843       priv->has_secondary_text = FALSE;
844       gtk_widget_hide (priv->secondary_label);
845     }
846
847   setup_primary_label_font (message_dialog);
848 }
849
850 /**
851  * gtk_message_dialog_format_secondary_markup:
852  * @message_dialog: a #GtkMessageDialog
853  * @message_format: printf()-style markup string (see 
854      <link linkend="PangoMarkupFormat">Pango markup format</link>), or %NULL
855  * @Varargs: arguments for @message_format
856  * 
857  * Sets the secondary text of the message dialog to be @message_format (with 
858  * printf()-style), which is marked up with the 
859  * <link linkend="PangoMarkupFormat">Pango text markup language</link>.
860  *
861  * Note that setting a secondary text makes the primary text become
862  * bold, unless you have provided explicit markup.
863  *
864  * Due to an oversight, this function does not escape special XML characters
865  * like gtk_message_dialog_new_with_markup() does. Thus, if the arguments 
866  * may contain special XML characters, you should use g_markup_printf_escaped()
867  * to escape it.
868
869  * <informalexample><programlisting>
870  * gchar *msg;
871  *  
872  * msg = g_markup_printf_escaped (message_format, ...);
873  * gtk_message_dialog_format_secondary_markup (message_dialog, "&percnt;s", msg);
874  * g_free (msg);
875  * </programlisting></informalexample>
876  *
877  * Since: 2.6
878  **/
879 void
880 gtk_message_dialog_format_secondary_markup (GtkMessageDialog *message_dialog,
881                                             const gchar      *message_format,
882                                             ...)
883 {
884   va_list args;
885   gchar *msg = NULL;
886   GtkMessageDialogPriv *priv;
887
888   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
889
890   priv = message_dialog->priv;
891
892   if (message_format)
893     {
894       priv->has_secondary_text = TRUE;
895
896       va_start (args, message_format);
897       msg = g_strdup_vprintf (message_format, args);
898       va_end (args);
899
900       gtk_widget_show (priv->secondary_label);
901       gtk_label_set_markup (GTK_LABEL (priv->secondary_label), msg);
902
903       g_free (msg);
904     }
905   else
906     {
907       priv->has_secondary_text = FALSE;
908       gtk_widget_hide (priv->secondary_label);
909     }
910
911   setup_primary_label_font (message_dialog);
912 }
913
914 /**
915  * gtk_message_dialog_get_message_area:
916  * @message_dialog: a #GtkMessageDialog
917  *
918  * Return value: A #GtkVBox corresponding to the "message area" in the
919  * @message_dialog.  This is the box where the dialog's primary and secondary
920  * labels are packed.  You can add your own extra content to that box and it
921  * will appear below those labels, on the right side of the dialog's image (or
922  * on the left for right-to-left languages).  See gtk_dialog_get_content_area()
923  * for the corresponding function in the parent #GtkDialog.
924  *
925  * Since: 2.22
926  **/
927 GtkWidget *
928 gtk_message_dialog_get_message_area (GtkMessageDialog *message_dialog)
929 {
930   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog), NULL);
931
932   return message_dialog->priv->message_area;
933 }
934
935 static void
936 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
937                                 GtkButtonsType buttons)
938 {
939   GtkDialog* dialog = GTK_DIALOG (message_dialog);
940
941   switch (buttons)
942     {
943     case GTK_BUTTONS_NONE:
944       /* nothing */
945       break;
946
947     case GTK_BUTTONS_OK:
948       gtk_dialog_add_button (dialog,
949                              GTK_STOCK_OK,
950                              GTK_RESPONSE_OK);
951       break;
952
953     case GTK_BUTTONS_CLOSE:
954       gtk_dialog_add_button (dialog,
955                              GTK_STOCK_CLOSE,
956                              GTK_RESPONSE_CLOSE);
957       break;
958
959     case GTK_BUTTONS_CANCEL:
960       gtk_dialog_add_button (dialog,
961                              GTK_STOCK_CANCEL,
962                              GTK_RESPONSE_CANCEL);
963       break;
964
965     case GTK_BUTTONS_YES_NO:
966       gtk_dialog_add_button (dialog,
967                              GTK_STOCK_NO,
968                              GTK_RESPONSE_NO);
969       gtk_dialog_add_button (dialog,
970                              GTK_STOCK_YES,
971                              GTK_RESPONSE_YES);
972       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
973                                                GTK_RESPONSE_YES,
974                                                GTK_RESPONSE_NO,
975                                                -1);
976       break;
977
978     case GTK_BUTTONS_OK_CANCEL:
979       gtk_dialog_add_button (dialog,
980                              GTK_STOCK_CANCEL,
981                              GTK_RESPONSE_CANCEL);
982       gtk_dialog_add_button (dialog,
983                              GTK_STOCK_OK,
984                              GTK_RESPONSE_OK);
985       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
986                                                GTK_RESPONSE_OK,
987                                                GTK_RESPONSE_CANCEL,
988                                                -1);
989       break;
990       
991     default:
992       g_warning ("Unknown GtkButtonsType");
993       break;
994     } 
995
996   g_object_notify (G_OBJECT (message_dialog), "buttons");
997 }
998
999 static void
1000 gtk_message_dialog_style_set (GtkWidget *widget,
1001                               GtkStyle  *prev_style)
1002 {
1003   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (widget);
1004   gboolean use_separator;
1005   GtkWidget *parent;
1006   gint border_width;
1007
1008   parent = gtk_widget_get_parent (gtk_message_dialog_get_image (dialog));
1009
1010   if (parent)
1011     {
1012       gtk_widget_style_get (widget, "message-border",
1013                             &border_width, NULL);
1014       
1015       gtk_container_set_border_width (GTK_CONTAINER (parent),
1016                                       MAX (0, border_width - 7));
1017     }
1018
1019   gtk_widget_style_get (widget,
1020                         "use-separator", &use_separator,
1021                         NULL);
1022
1023   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), FALSE);
1024   gtk_dialog_set_has_separator (GTK_DIALOG (widget), use_separator);
1025   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), TRUE);
1026
1027   setup_primary_label_font (dialog);
1028
1029   GTK_WIDGET_CLASS (gtk_message_dialog_parent_class)->style_set (widget, prev_style);
1030 }