]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
Make the message area available in gtkbuilder as well
[~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 #include "gtkalias.h"
43
44 /**
45  * SECTION:gtkmessagedialog
46  * @Short_description: A convenient message window
47  * @Title: GtkMessageDialog
48  * @See_also:#GtkDialog
49  *
50  * #GtkMessageDialog presents a dialog with an image representing the type of
51  * message (Error, Question, etc.) alongside some message text. It's simply a
52  * convenience widget; you could construct the equivalent of #GtkMessageDialog
53  * from #GtkDialog without too much effort, but #GtkMessageDialog saves typing.
54  *
55  * The easiest way to do a modal message dialog is to use gtk_dialog_run(), though
56  * you can also pass in the %GTK_DIALOG_MODAL flag, gtk_dialog_run() automatically
57  * makes the dialog modal and waits for the user to respond to it. gtk_dialog_run()
58  * returns when any dialog button is clicked.
59  * <example>
60  * <title>A modal dialog.</title>
61  * <programlisting>
62  *  dialog = gtk_message_dialog_new (main_application_window,
63  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
64  *                                   GTK_MESSAGE_ERROR,
65  *                                   GTK_BUTTONS_CLOSE,
66  *                                   "Error loading file '&percnt;s': &percnt;s",
67  *                                   filename, g_strerror (errno));
68  *  gtk_dialog_run (GTK_DIALOG (dialog));
69  *  gtk_widget_destroy (dialog);
70  * </programlisting>
71  * </example>
72  * You might do a non-modal #GtkMessageDialog as follows:
73  * <example>
74  * <title>A non-modal dialog.</title>
75  * <programlisting>
76  *  dialog = gtk_message_dialog_new (main_application_window,
77  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
78  *                                   GTK_MESSAGE_ERROR,
79  *                                   GTK_BUTTONS_CLOSE,
80  *                                   "Error loading file '&percnt;s': &percnt;s",
81  *                                   filename, g_strerror (errno));
82  *
83  *  /&ast; Destroy the dialog when the user responds to it (e.g. clicks a button) &ast;/
84  *  g_signal_connect_swapped (dialog, "response",
85  *                            G_CALLBACK (gtk_widget_destroy),
86  *                            dialog);
87  * </programlisting>
88  * </example>
89  *
90  * <refsect2 id="GtkMessageDialog-BUILDER-UI">
91  * <title>GtkMessageDialog as GtkBuildable</title>
92  * <para>
93  * The GtkMessageDialog implementation of the GtkBuildable interface exposes
94  * the message area as an internal child with the name "message_area".
95  * </para>
96  * </refsect2>
97  */
98
99 #define GTK_MESSAGE_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MESSAGE_DIALOG, GtkMessageDialogPrivate))
100
101 typedef struct _GtkMessageDialogPrivate GtkMessageDialogPrivate;
102
103 struct _GtkMessageDialogPrivate
104 {
105   GtkWidget *message_area; /* vbox for the primary and secondary labels, and any extra content from the caller */
106   GtkWidget *secondary_label;
107   guint message_type : 3;
108   guint has_primary_markup : 1;
109   guint has_secondary_text : 1;
110 };
111
112 static void gtk_message_dialog_style_set  (GtkWidget             *widget,
113                                            GtkStyle              *prev_style);
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_set = gtk_message_dialog_style_set;
180
181   gobject_class->set_property = gtk_message_dialog_set_property;
182   gobject_class->get_property = gtk_message_dialog_get_property;
183   
184   gtk_widget_class_install_style_property (widget_class,
185                                            g_param_spec_int ("message-border",
186                                                              P_("Image/label border"),
187                                                              P_("Width of border around the label and image in the message dialog"),
188                                                              0,
189                                                              G_MAXINT,
190                                                              12,
191                                                              GTK_PARAM_READABLE));
192   /**
193    * GtkMessageDialog:use-separator:
194    *
195    * Whether to draw a separator line between the message label and the buttons
196    * in the dialog.
197    *
198    * Since: 2.4
199    */
200   gtk_widget_class_install_style_property (widget_class,
201                                            g_param_spec_boolean ("use-separator",
202                                                                  P_("Use separator"),
203                                                                  P_("Whether to put a separator between the message dialog's text and the buttons"),
204                                                                  FALSE,
205                                                                  GTK_PARAM_READABLE));
206   /**
207    * GtkMessageDialog:message-type:
208    *
209    * The type of the message. The type is used to determine
210    * the image that is shown in the dialog, unless the image is 
211    * explicitly set by the ::image property.
212    */
213   g_object_class_install_property (gobject_class,
214                                    PROP_MESSAGE_TYPE,
215                                    g_param_spec_enum ("message-type",
216                                                       P_("Message Type"),
217                                                       P_("The type of message"),
218                                                       GTK_TYPE_MESSAGE_TYPE,
219                                                       GTK_MESSAGE_INFO,
220                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
221   g_object_class_install_property (gobject_class,
222                                    PROP_BUTTONS,
223                                    g_param_spec_enum ("buttons",
224                                                       P_("Message Buttons"),
225                                                       P_("The buttons shown in the message dialog"),
226                                                       GTK_TYPE_BUTTONS_TYPE,
227                                                       GTK_BUTTONS_NONE,
228                                                       GTK_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
229
230   /**
231    * GtkMessageDialog:text:
232    * 
233    * The primary text of the message dialog. If the dialog has 
234    * a secondary text, this will appear as the title.
235    *
236    * Since: 2.10
237    */
238   g_object_class_install_property (gobject_class,
239                                    PROP_TEXT,
240                                    g_param_spec_string ("text",
241                                                         P_("Text"),
242                                                         P_("The primary text of the message dialog"),
243                                                         "",
244                                                         GTK_PARAM_READWRITE));
245
246   /**
247    * GtkMessageDialog:use-markup:
248    * 
249    * %TRUE if the primary text of the dialog includes Pango markup. 
250    * See pango_parse_markup(). 
251    *
252    * Since: 2.10
253    */
254   g_object_class_install_property (gobject_class,
255                                    PROP_USE_MARKUP,
256                                    g_param_spec_boolean ("use-markup",
257                                                          P_("Use Markup"),
258                                                          P_("The primary text of the title includes Pango markup."),
259                                                          FALSE,
260                                                          GTK_PARAM_READWRITE));
261   
262   /**
263    * GtkMessageDialog:secondary-text:
264    * 
265    * The secondary text of the message dialog. 
266    *
267    * Since: 2.10
268    */
269   g_object_class_install_property (gobject_class,
270                                    PROP_SECONDARY_TEXT,
271                                    g_param_spec_string ("secondary-text",
272                                                         P_("Secondary Text"),
273                                                         P_("The secondary text of the message dialog"),
274                                                         NULL,
275                                                         GTK_PARAM_READWRITE));
276
277   /**
278    * GtkMessageDialog:secondary-use-markup:
279    * 
280    * %TRUE if the secondary text of the dialog includes Pango markup. 
281    * See pango_parse_markup(). 
282    *
283    * Since: 2.10
284    */
285   g_object_class_install_property (gobject_class,
286                                    PROP_SECONDARY_USE_MARKUP,
287                                    g_param_spec_boolean ("secondary-use-markup",
288                                                          P_("Use Markup in secondary"),
289                                                          P_("The secondary text includes Pango markup."),
290                                                          FALSE,
291                                                          GTK_PARAM_READWRITE));
292
293   /**
294    * GtkMessageDialog:image:
295    * 
296    * The image for this dialog.
297    *
298    * Since: 2.10
299    */
300   g_object_class_install_property (gobject_class,
301                                    PROP_IMAGE,
302                                    g_param_spec_object ("image",
303                                                         P_("Image"),
304                                                         P_("The image"),
305                                                         GTK_TYPE_WIDGET,
306                                                         GTK_PARAM_READWRITE));
307
308   /**
309    * GtkMessageDialog:message-area
310    *
311    * The #GtkVBox that corresponds to the message area of this dialog.  See
312    * gtk_message_dialog_get_message_area() for a detailed description of this
313    * area.
314    *
315    * Since: 2.22
316    */
317   g_object_class_install_property (gobject_class,
318                                    PROP_MESSAGE_AREA,
319                                    g_param_spec_object ("message-area",
320                                                         P_("Message area"),
321                                                         P_("GtkVBox that holds the dialog's primary and secondary labels"),
322                                                         GTK_TYPE_WIDGET,
323                                                         GTK_PARAM_READABLE));
324
325   g_type_class_add_private (gobject_class,
326                             sizeof (GtkMessageDialogPrivate));
327 }
328
329 static void
330 gtk_message_dialog_init (GtkMessageDialog *dialog)
331 {
332   GtkWidget *hbox;
333   GtkMessageDialogPrivate *priv;
334
335   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
336
337   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
338   gtk_window_set_title (GTK_WINDOW (dialog), "");
339   gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), TRUE);
340
341   priv->has_primary_markup = FALSE;
342   priv->has_secondary_text = FALSE;
343   priv->secondary_label = gtk_label_new (NULL);
344   gtk_widget_set_no_show_all (priv->secondary_label, TRUE);
345   
346   dialog->label = gtk_label_new (NULL);
347   dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
348   gtk_misc_set_alignment (GTK_MISC (dialog->image), 0.5, 0.0);
349   
350   gtk_label_set_line_wrap  (GTK_LABEL (dialog->label), TRUE);
351   gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
352   gtk_misc_set_alignment   (GTK_MISC  (dialog->label), 0.0, 0.0);
353   
354   gtk_label_set_line_wrap  (GTK_LABEL (priv->secondary_label), TRUE);
355   gtk_label_set_selectable (GTK_LABEL (priv->secondary_label), TRUE);
356   gtk_misc_set_alignment   (GTK_MISC  (priv->secondary_label), 0.0, 0.0);
357
358   hbox = gtk_hbox_new (FALSE, 12);
359   priv->message_area = gtk_vbox_new (FALSE, 12);
360
361   gtk_box_pack_start (GTK_BOX (priv->message_area), dialog->label,
362                       FALSE, FALSE, 0);
363
364   gtk_box_pack_start (GTK_BOX (priv->message_area), priv->secondary_label,
365                       TRUE, TRUE, 0);
366
367   gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
368                       FALSE, FALSE, 0);
369
370   gtk_box_pack_start (GTK_BOX (hbox), priv->message_area,
371                       TRUE, TRUE, 0);
372
373   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
374                       hbox,
375                       FALSE, FALSE, 0);
376
377   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
378   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
379   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14); /* 14 + 2 * 5 = 24 */
380   gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 5);
381   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->action_area), 6);
382
383   gtk_widget_show_all (hbox);
384
385   _gtk_dialog_set_ignore_separator (GTK_DIALOG (dialog), TRUE);
386 }
387
388 static void
389 setup_primary_label_font (GtkMessageDialog *dialog)
390 {
391   gint size;
392   PangoFontDescription *font_desc;
393   GtkMessageDialogPrivate *priv;
394
395   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
396
397   /* unset the font settings */
398   gtk_widget_modify_font (dialog->label, NULL);
399
400   if (priv->has_secondary_text && !priv->has_primary_markup)
401     {
402       size = pango_font_description_get_size (dialog->label->style->font_desc);
403       font_desc = pango_font_description_new ();
404       pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
405       pango_font_description_set_size (font_desc, size * PANGO_SCALE_LARGE);
406       gtk_widget_modify_font (dialog->label, font_desc);
407       pango_font_description_free (font_desc);
408     }
409 }
410
411 static void
412 setup_type (GtkMessageDialog *dialog,
413             GtkMessageType    type)
414 {
415   GtkMessageDialogPrivate *priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
416   const gchar *stock_id = NULL;
417   AtkObject *atk_obj;
418  
419   priv->message_type = type;
420
421   switch (type)
422     {
423     case GTK_MESSAGE_INFO:
424       stock_id = GTK_STOCK_DIALOG_INFO;
425       break;
426
427     case GTK_MESSAGE_QUESTION:
428       stock_id = GTK_STOCK_DIALOG_QUESTION;
429       break;
430
431     case GTK_MESSAGE_WARNING:
432       stock_id = GTK_STOCK_DIALOG_WARNING;
433       break;
434       
435     case GTK_MESSAGE_ERROR:
436       stock_id = GTK_STOCK_DIALOG_ERROR;
437       break;
438
439     case GTK_MESSAGE_OTHER:
440       break;
441
442     default:
443       g_warning ("Unknown GtkMessageType %u", type);
444       break;
445     }
446
447   if (stock_id)
448     gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
449                               GTK_ICON_SIZE_DIALOG);
450       
451   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (dialog));
452   if (GTK_IS_ACCESSIBLE (atk_obj))
453     {
454       atk_object_set_role (atk_obj, ATK_ROLE_ALERT);
455       if (stock_id)
456         {
457           GtkStockItem item;
458
459           gtk_stock_lookup (stock_id, &item);
460           atk_object_set_name (atk_obj, item.label);
461         }
462     }
463 }
464
465 static void 
466 gtk_message_dialog_set_property (GObject      *object,
467                                  guint         prop_id,
468                                  const GValue *value,
469                                  GParamSpec   *pspec)
470 {
471   GtkMessageDialog *dialog;
472   GtkMessageDialogPrivate *priv;
473
474   dialog = GTK_MESSAGE_DIALOG (object);
475   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
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 (dialog->label), 
488                               g_value_get_string (value));
489       else
490         gtk_label_set_text (GTK_LABEL (dialog->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 (dialog->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;
542   GtkMessageDialogPrivate *priv;
543
544   dialog = GTK_MESSAGE_DIALOG (object);
545   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
546     
547   switch (prop_id)
548     {
549     case PROP_MESSAGE_TYPE:
550       g_value_set_enum (value, (GtkMessageType) priv->message_type);
551       break;
552     case PROP_TEXT:
553       g_value_set_string (value, gtk_label_get_label (GTK_LABEL (dialog->label)));
554       break;
555     case PROP_USE_MARKUP:
556       g_value_set_boolean (value, priv->has_primary_markup);
557       break;
558     case PROP_SECONDARY_TEXT:
559       if (priv->has_secondary_text)
560       g_value_set_string (value, 
561                           gtk_label_get_label (GTK_LABEL (priv->secondary_label)));
562       else
563         g_value_set_string (value, NULL);
564       break;
565     case PROP_SECONDARY_USE_MARKUP:
566       if (priv->has_secondary_text)
567         g_value_set_boolean (value, 
568                              gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)));
569       else
570         g_value_set_boolean (value, FALSE);
571       break;
572     case PROP_IMAGE:
573       g_value_set_object (value, dialog->image);
574       break;
575     case PROP_MESSAGE_AREA:
576       g_value_set_object (value, priv->message_area);
577       break;
578     default:
579       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
580       break;
581     }
582 }
583
584 /**
585  * gtk_message_dialog_new:
586  * @parent: (allow-none): transient parent, or %NULL for none
587  * @flags: flags
588  * @type: type of message
589  * @buttons: set of buttons to use
590  * @message_format: (allow-none): printf()-style format string, or %NULL
591  * @Varargs: arguments for @message_format
592  *
593  * Creates a new message dialog, which is a simple dialog with an icon
594  * indicating the dialog type (error, warning, etc.) and some text the
595  * user may want to see. When the user clicks a button a "response"
596  * signal is emitted with response IDs from #GtkResponseType. See
597  * #GtkDialog for more details.
598  *
599  * Return value: (transfer none): a new #GtkMessageDialog
600  **/
601 GtkWidget*
602 gtk_message_dialog_new (GtkWindow     *parent,
603                         GtkDialogFlags flags,
604                         GtkMessageType type,
605                         GtkButtonsType buttons,
606                         const gchar   *message_format,
607                         ...)
608 {
609   GtkWidget *widget;
610   GtkDialog *dialog;
611   gchar* msg = NULL;
612   va_list args;
613
614   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
615
616   widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
617                          "message-type", type,
618                          "buttons", buttons,
619                          NULL);
620   dialog = GTK_DIALOG (widget);
621
622   if (flags & GTK_DIALOG_NO_SEPARATOR)
623     {
624       g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
625       flags &= ~GTK_DIALOG_NO_SEPARATOR;
626     }
627
628   if (message_format)
629     {
630       va_start (args, message_format);
631       msg = g_strdup_vprintf (message_format, args);
632       va_end (args);
633
634       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
635                           msg);
636
637       g_free (msg);
638     }
639
640   if (parent != NULL)
641     gtk_window_set_transient_for (GTK_WINDOW (widget),
642                                   GTK_WINDOW (parent));
643   
644   if (flags & GTK_DIALOG_MODAL)
645     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
646
647   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
648     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
649
650   return widget;
651 }
652
653 /**
654  * gtk_message_dialog_new_with_markup:
655  * @parent: (allow-none): transient parent, or %NULL for none 
656  * @flags: flags
657  * @type: type of message
658  * @buttons: set of buttons to use
659  * @message_format: (allow-none): printf()-style format string, or %NULL
660  * @Varargs: arguments for @message_format
661  * 
662  * Creates a new message dialog, which is a simple dialog with an icon
663  * indicating the dialog type (error, warning, etc.) and some text which
664  * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
665  * When the user clicks a button a "response" signal is emitted with
666  * response IDs from #GtkResponseType. See #GtkDialog for more details.
667  *
668  * Special XML characters in the printf() arguments passed to this
669  * function will automatically be escaped as necessary.
670  * (See g_markup_printf_escaped() for how this is implemented.)
671  * Usually this is what you want, but if you have an existing
672  * Pango markup string that you want to use literally as the
673  * label, then you need to use gtk_message_dialog_set_markup()
674  * instead, since you can't pass the markup string either
675  * as the format (it might contain '%' characters) or as a string
676  * argument.
677  * |[
678  *  GtkWidget *dialog;
679  *  dialog = gtk_message_dialog_new (main_application_window,
680  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
681  *                                   GTK_MESSAGE_ERROR,
682  *                                   GTK_BUTTONS_CLOSE,
683  *                                   NULL);
684  *  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
685  *                                 markup);
686  * ]|
687  * 
688  * Return value: a new #GtkMessageDialog
689  *
690  * Since: 2.4
691  **/
692 GtkWidget*
693 gtk_message_dialog_new_with_markup (GtkWindow     *parent,
694                                     GtkDialogFlags flags,
695                                     GtkMessageType type,
696                                     GtkButtonsType buttons,
697                                     const gchar   *message_format,
698                                     ...)
699 {
700   GtkWidget *widget;
701   va_list args;
702   gchar *msg = NULL;
703
704   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
705
706   widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
707
708   if (message_format)
709     {
710       va_start (args, message_format);
711       msg = g_markup_vprintf_escaped (message_format, args);
712       va_end (args);
713
714       gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
715
716       g_free (msg);
717     }
718
719   return widget;
720 }
721
722 /**
723  * gtk_message_dialog_set_image:
724  * @dialog: a #GtkMessageDialog
725  * @image: the image
726  * 
727  * Sets the dialog's image to @image.
728  *
729  * Since: 2.10
730  **/
731 void
732 gtk_message_dialog_set_image (GtkMessageDialog *dialog,
733                               GtkWidget        *image)
734 {
735   GtkMessageDialogPrivate *priv;
736   GtkWidget *parent;
737
738   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (dialog));
739   g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
740
741   if (image == NULL)
742     {
743       image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
744       gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
745     }
746
747   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
748
749   priv->message_type = GTK_MESSAGE_OTHER;
750   
751   parent = dialog->image->parent;
752   gtk_container_add (GTK_CONTAINER (parent), image);
753   gtk_container_remove (GTK_CONTAINER (parent), dialog->image);
754   gtk_box_reorder_child (GTK_BOX (parent), image, 0);
755
756   dialog->image = image;
757
758   g_object_notify (G_OBJECT (dialog), "image");
759 }
760
761 /**
762  * gtk_message_dialog_get_image:
763  * @dialog: a #GtkMessageDialog
764  *
765  * Gets the dialog's image.
766  *
767  * Return value: the dialog's image
768  *
769  * Since: 2.14
770  **/
771 GtkWidget *
772 gtk_message_dialog_get_image (GtkMessageDialog *dialog)
773 {
774   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), NULL);
775
776   return dialog->image;
777 }
778
779 /**
780  * gtk_message_dialog_set_markup:
781  * @message_dialog: a #GtkMessageDialog
782  * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
783  * 
784  * Sets the text of the message dialog to be @str, which is marked
785  * up with the <link linkend="PangoMarkupFormat">Pango text markup
786  * language</link>.
787  *
788  * Since: 2.4
789  **/
790 void
791 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
792                                const gchar      *str)
793 {
794   GtkMessageDialogPrivate *priv;
795
796   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
797
798   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
799   priv->has_primary_markup = TRUE;
800   gtk_label_set_markup (GTK_LABEL (message_dialog->label), str);
801 }
802
803 /**
804  * gtk_message_dialog_format_secondary_text:
805  * @message_dialog: a #GtkMessageDialog
806  * @message_format: (allow-none): printf()-style format string, or %NULL
807  * @Varargs: arguments for @message_format
808  * 
809  * Sets the secondary text of the message dialog to be @message_format 
810  * (with printf()-style).
811  *
812  * Note that setting a secondary text makes the primary text become
813  * bold, unless you have provided explicit markup.
814  *
815  * Since: 2.6
816  **/
817 void
818 gtk_message_dialog_format_secondary_text (GtkMessageDialog *message_dialog,
819                                           const gchar      *message_format,
820                                           ...)
821 {
822   va_list args;
823   gchar *msg = NULL;
824   GtkMessageDialogPrivate *priv;
825
826   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
827
828   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
829
830   if (message_format)
831     {
832       priv->has_secondary_text = TRUE;
833
834       va_start (args, message_format);
835       msg = g_strdup_vprintf (message_format, args);
836       va_end (args);
837
838       gtk_widget_show (priv->secondary_label);
839       gtk_label_set_text (GTK_LABEL (priv->secondary_label), msg);
840
841       g_free (msg);
842     }
843   else
844     {
845       priv->has_secondary_text = FALSE;
846       gtk_widget_hide (priv->secondary_label);
847     }
848
849   setup_primary_label_font (message_dialog);
850 }
851
852 /**
853  * gtk_message_dialog_format_secondary_markup:
854  * @message_dialog: a #GtkMessageDialog
855  * @message_format: printf()-style markup string (see 
856      <link linkend="PangoMarkupFormat">Pango markup format</link>), or %NULL
857  * @Varargs: arguments for @message_format
858  * 
859  * Sets the secondary text of the message dialog to be @message_format (with 
860  * printf()-style), which is marked up with the 
861  * <link linkend="PangoMarkupFormat">Pango text markup language</link>.
862  *
863  * Note that setting a secondary text makes the primary text become
864  * bold, unless you have provided explicit markup.
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 = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
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  * Return value: A #GtkVBox corresponding to the "message area" in the
921  * @message_dialog.  This is the box where the dialog's primary and secondary
922  * labels are packed.  You can add your own extra content to that box and it
923  * will appear below those labels, on the right side of the dialog's image (or
924  * on the left for right-to-left languages).  See gtk_dialog_get_content_area()
925  * for the corresponding function in the parent #GtkDialog.
926  *
927  * Since: 2.22
928  **/
929 GtkWidget *
930 gtk_message_dialog_get_message_area (GtkMessageDialog *message_dialog)
931 {
932   GtkMessageDialogPrivate *priv;
933
934   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog), NULL);
935
936   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
937
938   return priv->message_area;
939 }
940
941 static void
942 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
943                                 GtkButtonsType buttons)
944 {
945   GtkDialog* dialog = GTK_DIALOG (message_dialog);
946
947   switch (buttons)
948     {
949     case GTK_BUTTONS_NONE:
950       /* nothing */
951       break;
952
953     case GTK_BUTTONS_OK:
954       gtk_dialog_add_button (dialog,
955                              GTK_STOCK_OK,
956                              GTK_RESPONSE_OK);
957       break;
958
959     case GTK_BUTTONS_CLOSE:
960       gtk_dialog_add_button (dialog,
961                              GTK_STOCK_CLOSE,
962                              GTK_RESPONSE_CLOSE);
963       break;
964
965     case GTK_BUTTONS_CANCEL:
966       gtk_dialog_add_button (dialog,
967                              GTK_STOCK_CANCEL,
968                              GTK_RESPONSE_CANCEL);
969       break;
970
971     case GTK_BUTTONS_YES_NO:
972       gtk_dialog_add_button (dialog,
973                              GTK_STOCK_NO,
974                              GTK_RESPONSE_NO);
975       gtk_dialog_add_button (dialog,
976                              GTK_STOCK_YES,
977                              GTK_RESPONSE_YES);
978       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
979                                                GTK_RESPONSE_YES,
980                                                GTK_RESPONSE_NO,
981                                                -1);
982       break;
983
984     case GTK_BUTTONS_OK_CANCEL:
985       gtk_dialog_add_button (dialog,
986                              GTK_STOCK_CANCEL,
987                              GTK_RESPONSE_CANCEL);
988       gtk_dialog_add_button (dialog,
989                              GTK_STOCK_OK,
990                              GTK_RESPONSE_OK);
991       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
992                                                GTK_RESPONSE_OK,
993                                                GTK_RESPONSE_CANCEL,
994                                                -1);
995       break;
996       
997     default:
998       g_warning ("Unknown GtkButtonsType");
999       break;
1000     } 
1001
1002   g_object_notify (G_OBJECT (message_dialog), "buttons");
1003 }
1004
1005 static void
1006 gtk_message_dialog_style_set (GtkWidget *widget,
1007                               GtkStyle  *prev_style)
1008 {
1009   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (widget);
1010   gboolean use_separator;
1011   GtkWidget *parent;
1012   gint border_width;
1013
1014   parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
1015
1016   if (parent)
1017     {
1018       gtk_widget_style_get (widget, "message-border",
1019                             &border_width, NULL);
1020       
1021       gtk_container_set_border_width (GTK_CONTAINER (parent),
1022                                       MAX (0, border_width - 7));
1023     }
1024
1025   gtk_widget_style_get (widget,
1026                         "use-separator", &use_separator,
1027                         NULL);
1028
1029   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), FALSE);
1030   gtk_dialog_set_has_separator (GTK_DIALOG (widget), use_separator);
1031   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), TRUE);
1032
1033   setup_primary_label_font (dialog);
1034
1035   GTK_WIDGET_CLASS (gtk_message_dialog_parent_class)->style_set (widget, prev_style);
1036 }
1037
1038 #define __GTK_MESSAGE_DIALOG_C__
1039 #include "gtkaliasdef.c"