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