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