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