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