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