]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
Apply a patch from Dennis Cranston to make dialogs more consistent
[~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 "gtklabel.h"
33 #include "gtkhbox.h"
34 #include "gtkvbox.h"
35 #include "gtkimage.h"
36 #include "gtkstock.h"
37 #include "gtkiconfactory.h"
38 #include "gtkintl.h"
39 #include "gtkprivate.h"
40 #include "gtkalias.h"
41
42 #define GTK_MESSAGE_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MESSAGE_DIALOG, GtkMessageDialogPrivate))
43
44 typedef struct _GtkMessageDialogPrivate GtkMessageDialogPrivate;
45
46 struct _GtkMessageDialogPrivate
47 {
48   GtkWidget *secondary_label;
49   gboolean   has_primary_markup;
50   gboolean   has_secondary_text;
51 };
52
53 static void gtk_message_dialog_class_init (GtkMessageDialogClass *klass);
54 static void gtk_message_dialog_init       (GtkMessageDialog      *dialog);
55 static void gtk_message_dialog_style_set  (GtkWidget             *widget,
56                                            GtkStyle              *prev_style);
57
58 static void gtk_message_dialog_set_property (GObject          *object,
59                                              guint             prop_id,
60                                              const GValue     *value,
61                                              GParamSpec       *pspec);
62 static void gtk_message_dialog_get_property (GObject          *object,
63                                              guint             prop_id,
64                                              GValue           *value,
65                                              GParamSpec       *pspec);
66 static void gtk_message_dialog_add_buttons  (GtkMessageDialog *message_dialog,
67                                              GtkButtonsType    buttons);
68
69 enum {
70   PROP_0,
71   PROP_MESSAGE_TYPE,
72   PROP_BUTTONS,
73   PROP_TEXT,
74   PROP_USE_MARKUP,
75   PROP_SECONDARY_TEXT,
76   PROP_SECONDARY_USE_MARKUP
77 };
78
79 static gpointer parent_class;
80
81 GType
82 gtk_message_dialog_get_type (void)
83 {
84   static GType dialog_type = 0;
85
86   if (!dialog_type)
87     {
88       static const GTypeInfo dialog_info =
89       {
90         sizeof (GtkMessageDialogClass),
91         NULL,           /* base_init */
92         NULL,           /* base_finalize */
93         (GClassInitFunc) gtk_message_dialog_class_init,
94         NULL,           /* class_finalize */
95         NULL,           /* class_data */
96         sizeof (GtkMessageDialog),
97         0,              /* n_preallocs */
98         (GInstanceInitFunc) gtk_message_dialog_init,
99       };
100
101       dialog_type = g_type_register_static (GTK_TYPE_DIALOG, I_("GtkMessageDialog"),
102                                             &dialog_info, 0);
103     }
104
105   return dialog_type;
106 }
107
108 static void
109 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
110 {
111   GtkWidgetClass *widget_class;
112   GObjectClass *gobject_class;
113
114   widget_class = GTK_WIDGET_CLASS (class);
115   gobject_class = G_OBJECT_CLASS (class);
116
117   parent_class = g_type_class_peek_parent (class);
118   
119   widget_class->style_set = gtk_message_dialog_style_set;
120
121   gobject_class->set_property = gtk_message_dialog_set_property;
122   gobject_class->get_property = gtk_message_dialog_get_property;
123   
124   gtk_widget_class_install_style_property (widget_class,
125                                            g_param_spec_int ("message-border",
126                                                              P_("Image/label border"),
127                                                              P_("Width of border around the label and image in the message dialog"),
128                                                              0,
129                                                              G_MAXINT,
130                                                              12,
131                                                              GTK_PARAM_READABLE));
132   /**
133    * GtkMessageDialog::use_separator
134    *
135    * Whether to draw a separator line between the message label and the buttons
136    * in the dialog.
137    *
138    * Since: 2.4
139    */
140   gtk_widget_class_install_style_property (widget_class,
141                                            g_param_spec_boolean ("use-separator",
142                                                                  P_("Use separator"),
143                                                                  P_("Whether to put a separator between the message dialog's text and the buttons"),
144                                                                  FALSE,
145                                                                  GTK_PARAM_READABLE));
146   g_object_class_install_property (gobject_class,
147                                    PROP_MESSAGE_TYPE,
148                                    g_param_spec_enum ("message-type",
149                                                       P_("Message Type"),
150                                                       P_("The type of message"),
151                                                       GTK_TYPE_MESSAGE_TYPE,
152                                                       GTK_MESSAGE_INFO,
153                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
154   g_object_class_install_property (gobject_class,
155                                    PROP_BUTTONS,
156                                    g_param_spec_enum ("buttons",
157                                                       P_("Message Buttons"),
158                                                       P_("The buttons shown in the message dialog"),
159                                                       GTK_TYPE_BUTTONS_TYPE,
160                                                       GTK_BUTTONS_NONE,
161                                                       GTK_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
162
163   /**
164    * GtkMessageDialog:text:
165    * 
166    * The primary text of the message dialog. If the dialog has 
167    * a secondary text, this will appear as the title.
168    *
169    * Since: 2.10
170    */
171   g_object_class_install_property (gobject_class,
172                                    PROP_TEXT,
173                                    g_param_spec_string ("text",
174                                                         P_("Text"),
175                                                         P_("The primary text of the message dialog"),
176                                                         NULL,
177                                                         GTK_PARAM_READWRITE));
178
179   /**
180    * GtkMessageDialog:use-markup:
181    * 
182    * %TRUE if the primary text of the dialog includes Pango markup. 
183    * See pango_parse_markup(). 
184    *
185    * Since: 2.10
186    */
187   g_object_class_install_property (gobject_class,
188                                    PROP_USE_MARKUP,
189                                    g_param_spec_boolean ("use-markup",
190                                                          P_("Use Markup"),
191                                                          P_("The primary text of the title includes Pango markup."),
192                                                          FALSE,
193                                                          GTK_PARAM_READWRITE));
194   
195   /**
196    * GtkMessageDialog:secondary-text:
197    * 
198    * The secondary text of the message dialog. 
199    *
200    * Since: 2.10
201    */
202   g_object_class_install_property (gobject_class,
203                                    PROP_SECONDARY_TEXT,
204                                    g_param_spec_string ("secondary-text",
205                                                         P_("Secondary Text"),
206                                                         P_("The secondary text of the message dialog"),
207                                                         NULL,
208                                                         GTK_PARAM_READWRITE));
209
210   /**
211    * GtkMessageDialog:secondary-use-markup:
212    * 
213    * %TRUE if the secondary text of the dialog includes Pango markup. 
214    * See pango_parse_markup(). 
215    *
216    * Since: 2.10
217    */
218   g_object_class_install_property (gobject_class,
219                                    PROP_SECONDARY_USE_MARKUP,
220                                    g_param_spec_boolean ("secondary-use-markup",
221                                                          P_("Use Markup in secondary"),
222                                                          P_("The secondary text includes Pango markup."),
223                                                          FALSE,
224                                                          GTK_PARAM_READWRITE));
225
226   g_type_class_add_private (gobject_class,
227                             sizeof (GtkMessageDialogPrivate));
228 }
229
230 static void
231 gtk_message_dialog_init (GtkMessageDialog *dialog)
232 {
233   GtkWidget *hbox, *vbox;
234   GtkMessageDialogPrivate *priv;
235
236   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
237
238   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
239   gtk_window_set_title (GTK_WINDOW (dialog), "");
240   gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), TRUE);
241
242   priv->has_primary_markup = FALSE;
243   priv->has_secondary_text = FALSE;
244   priv->secondary_label = gtk_label_new (NULL);
245   gtk_widget_set_no_show_all (priv->secondary_label, TRUE);
246   
247   dialog->label = gtk_label_new (NULL);
248   dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
249   gtk_misc_set_alignment (GTK_MISC (dialog->image), 0.5, 0.0);
250   
251   gtk_label_set_line_wrap  (GTK_LABEL (dialog->label), TRUE);
252   gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
253   gtk_misc_set_alignment   (GTK_MISC  (dialog->label), 0.0, 0.0);
254   
255   gtk_label_set_line_wrap  (GTK_LABEL (priv->secondary_label), TRUE);
256   gtk_label_set_selectable (GTK_LABEL (priv->secondary_label), TRUE);
257   gtk_misc_set_alignment   (GTK_MISC  (priv->secondary_label), 0.0, 0.0);
258
259   hbox = gtk_hbox_new (FALSE, 12);
260   vbox = gtk_vbox_new (FALSE, 12);
261
262   gtk_box_pack_start (GTK_BOX (vbox), dialog->label,
263                       FALSE, FALSE, 0);
264
265   gtk_box_pack_start (GTK_BOX (vbox), priv->secondary_label,
266                       TRUE, TRUE, 0);
267
268   gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
269                       FALSE, FALSE, 0);
270
271   gtk_box_pack_start (GTK_BOX (hbox), vbox,
272                       TRUE, TRUE, 0);
273
274   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
275                       hbox,
276                       FALSE, FALSE, 0);
277
278   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
279   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
280   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14); /* 14 + 2 * 5 = 24 */
281   gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 5);
282   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->action_area), 6);
283
284   gtk_widget_show_all (hbox);
285
286   _gtk_dialog_set_ignore_separator (GTK_DIALOG (dialog), TRUE);
287 }
288
289 static GtkMessageType
290 gtk_message_dialog_get_message_type (GtkMessageDialog *dialog)
291 {
292   const gchar* stock_id = NULL;
293
294   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), GTK_MESSAGE_INFO);
295   g_return_val_if_fail (GTK_IS_IMAGE(dialog->image), GTK_MESSAGE_INFO);
296
297   stock_id = GTK_IMAGE(dialog->image)->data.stock.stock_id;
298
299   /* Look at the stock id of the image to guess the
300    * GtkMessageType value that was used to choose it
301    * in setup_type()
302    */
303   if (strcmp (stock_id, GTK_STOCK_DIALOG_INFO) == 0)
304     return GTK_MESSAGE_INFO;
305   else if (strcmp (stock_id, GTK_STOCK_DIALOG_QUESTION) == 0)
306     return GTK_MESSAGE_QUESTION;
307   else if (strcmp (stock_id, GTK_STOCK_DIALOG_WARNING) == 0)
308     return GTK_MESSAGE_WARNING;
309   else if (strcmp (stock_id, GTK_STOCK_DIALOG_ERROR) == 0)
310     return GTK_MESSAGE_ERROR;
311   else
312     {
313       g_assert_not_reached (); 
314       return GTK_MESSAGE_INFO;
315     }
316 }
317
318 static void
319 setup_primary_label_font (GtkMessageDialog *dialog)
320 {
321   gint size;
322   PangoFontDescription *font_desc;
323   GtkMessageDialogPrivate *priv;
324
325   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
326
327   /* unset the font settings */
328   gtk_widget_modify_font (dialog->label, NULL);
329
330   if (priv->has_secondary_text && !priv->has_primary_markup)
331     {
332       size = pango_font_description_get_size (dialog->label->style->font_desc);
333       font_desc = pango_font_description_new ();
334       pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
335       pango_font_description_set_size (font_desc, size * PANGO_SCALE_LARGE);
336       gtk_widget_modify_font (dialog->label, font_desc);
337       pango_font_description_free (font_desc);
338     }
339 }
340
341 static void
342 setup_type (GtkMessageDialog *dialog,
343             GtkMessageType    type)
344 {
345   const gchar *stock_id = NULL;
346   GtkStockItem item;
347   
348   switch (type)
349     {
350     case GTK_MESSAGE_INFO:
351       stock_id = GTK_STOCK_DIALOG_INFO;
352       break;
353
354     case GTK_MESSAGE_QUESTION:
355       stock_id = GTK_STOCK_DIALOG_QUESTION;
356       break;
357
358     case GTK_MESSAGE_WARNING:
359       stock_id = GTK_STOCK_DIALOG_WARNING;
360       break;
361       
362     case GTK_MESSAGE_ERROR:
363       stock_id = GTK_STOCK_DIALOG_ERROR;
364       break;
365
366     default:
367       g_warning ("Unknown GtkMessageType %d", type);
368       break;
369     }
370
371   if (stock_id == NULL)
372     stock_id = GTK_STOCK_DIALOG_INFO;
373
374   if (gtk_stock_lookup (stock_id, &item))
375     gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
376                               GTK_ICON_SIZE_DIALOG);
377   else
378     g_warning ("Stock dialog ID doesn't exist?");  
379 }
380
381 static void 
382 gtk_message_dialog_set_property (GObject      *object,
383                                  guint         prop_id,
384                                  const GValue *value,
385                                  GParamSpec   *pspec)
386 {
387   GtkMessageDialog *dialog;
388   GtkMessageDialogPrivate *priv;
389
390   dialog = GTK_MESSAGE_DIALOG (object);
391   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
392   
393   switch (prop_id)
394     {
395     case PROP_MESSAGE_TYPE:
396       setup_type (dialog, g_value_get_enum (value));
397       break;
398     case PROP_BUTTONS:
399       gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
400       break;
401     case PROP_TEXT:
402       if (priv->has_primary_markup)
403         gtk_label_set_markup (GTK_LABEL (dialog->label), 
404                               g_value_get_string (value));
405       else
406         gtk_label_set_text (GTK_LABEL (dialog->label), 
407                             g_value_get_string (value));
408       break;
409     case PROP_USE_MARKUP:
410       priv->has_primary_markup = g_value_get_boolean (value);
411       gtk_label_set_use_markup (GTK_LABEL (dialog->label), 
412                                 priv->has_primary_markup);
413       setup_primary_label_font (dialog);
414       break;
415     case PROP_SECONDARY_TEXT:
416       {
417         const gchar *txt = g_value_get_string (value);
418         
419         if (gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)))
420           gtk_label_set_markup (GTK_LABEL (priv->secondary_label), txt);
421         else
422           gtk_label_set_text (GTK_LABEL (priv->secondary_label), txt);
423
424         if (txt)
425           {
426             priv->has_secondary_text = TRUE;
427             gtk_widget_show (priv->secondary_label);
428           }
429         else
430           {
431             priv->has_secondary_text = FALSE;
432             gtk_widget_hide (priv->secondary_label);
433           }
434         setup_primary_label_font (dialog);
435       }
436       break;
437     case PROP_SECONDARY_USE_MARKUP:
438       gtk_label_set_use_markup (GTK_LABEL (priv->secondary_label), 
439                                 g_value_get_boolean (value));
440       break;
441       
442     default:
443       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
444       break;
445     }
446 }
447
448 static void 
449 gtk_message_dialog_get_property (GObject     *object,
450                                  guint        prop_id,
451                                  GValue      *value,
452                                  GParamSpec  *pspec)
453 {
454   GtkMessageDialog *dialog;
455   GtkMessageDialogPrivate *priv;
456
457   dialog = GTK_MESSAGE_DIALOG (object);
458   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
459     
460   switch (prop_id)
461     {
462     case PROP_MESSAGE_TYPE:
463       g_value_set_enum (value, gtk_message_dialog_get_message_type (dialog));
464       break;
465     case PROP_TEXT:
466       g_value_set_string (value, gtk_label_get_label (GTK_LABEL (dialog->label)));
467       break;
468     case PROP_USE_MARKUP:
469       g_value_set_boolean (value, priv->has_primary_markup);
470       break;
471     case PROP_SECONDARY_TEXT:
472       if (priv->has_secondary_text)
473       g_value_set_string (value, 
474                           gtk_label_get_label (GTK_LABEL (priv->secondary_label)));
475       else
476         g_value_set_string (value, NULL);
477       break;
478     case PROP_SECONDARY_USE_MARKUP:
479       if (priv->has_secondary_text)
480         g_value_set_boolean (value, 
481                              gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)));
482       else
483         g_value_set_boolean (value, FALSE);
484       break;
485     default:
486       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
487       break;
488     }
489 }
490
491 /**
492  * gtk_message_dialog_new:
493  * @parent: transient parent, or %NULL for none 
494  * @flags: flags
495  * @type: type of message
496  * @buttons: set of buttons to use
497  * @message_format: printf()-style format string, or %NULL
498  * @Varargs: arguments for @message_format
499  * 
500  * Creates a new message dialog, which is a simple dialog with an icon
501  * indicating the dialog type (error, warning, etc.) and some text the
502  * user may want to see. When the user clicks a button a "response"
503  * signal is emitted with response IDs from #GtkResponseType. See
504  * #GtkDialog for more details.
505  * 
506  * Return value: a new #GtkMessageDialog
507  **/
508 GtkWidget*
509 gtk_message_dialog_new (GtkWindow     *parent,
510                         GtkDialogFlags flags,
511                         GtkMessageType type,
512                         GtkButtonsType buttons,
513                         const gchar   *message_format,
514                         ...)
515 {
516   GtkWidget *widget;
517   GtkDialog *dialog;
518   gchar* msg = NULL;
519   va_list args;
520
521   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
522
523   widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
524                          "message-type", type,
525                          "buttons", buttons,
526                          NULL);
527   dialog = GTK_DIALOG (widget);
528
529   if (flags & GTK_DIALOG_NO_SEPARATOR)
530     {
531       g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
532       flags &= ~GTK_DIALOG_NO_SEPARATOR;
533     }
534
535   if (message_format)
536     {
537       va_start (args, message_format);
538       msg = g_strdup_vprintf (message_format, args);
539       va_end (args);
540
541       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
542                           msg);
543
544       g_free (msg);
545     }
546
547   if (parent != NULL)
548     gtk_window_set_transient_for (GTK_WINDOW (widget),
549                                   GTK_WINDOW (parent));
550   
551   if (flags & GTK_DIALOG_MODAL)
552     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
553
554   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
555     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
556
557   return widget;
558 }
559
560 /**
561  * gtk_message_dialog_new_with_markup:
562  * @parent: transient parent, or %NULL for none 
563  * @flags: flags
564  * @type: type of message
565  * @buttons: set of buttons to use
566  * @message_format: printf()-style format string, or %NULL
567  * @Varargs: arguments for @message_format
568  * 
569  * Creates a new message dialog, which is a simple dialog with an icon
570  * indicating the dialog type (error, warning, etc.) and some text which
571  * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
572  * When the user clicks a button a "response" signal is emitted with
573  * response IDs from #GtkResponseType. See #GtkDialog for more details.
574  *
575  * Special XML characters in the printf() arguments passed to this
576  * function will automatically be escaped as necessary.
577  * (See g_markup_printf_escaped() for how this is implemented.)
578  * Usually this is what you want, but if you have an existing
579  * Pango markup string that you want to use literally as the
580  * label, then you need to use gtk_message_dialog_set_markup()
581  * instead, since you can't pass the markup string either
582  * as the format (it might contain '%' characters) or as a string
583  * argument.
584  *
585  * <informalexample><programlisting>
586  *  GtkWidget *dialog;
587  *  dialog = gtk_message_dialog_new (main_application_window,
588  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
589  *                                   GTK_MESSAGE_ERROR,
590  *                                   GTK_BUTTONS_CLOSE,
591  *                                   NULL);
592  *  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
593  *                                 markup);
594  * </programlisting></informalexample>
595  * 
596  * Return value: a new #GtkMessageDialog
597  *
598  * Since: 2.4
599  **/
600 GtkWidget*
601 gtk_message_dialog_new_with_markup (GtkWindow     *parent,
602                                     GtkDialogFlags flags,
603                                     GtkMessageType type,
604                                     GtkButtonsType buttons,
605                                     const gchar   *message_format,
606                                     ...)
607 {
608   GtkWidget *widget;
609   va_list args;
610   gchar *msg = NULL;
611
612   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
613
614   widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
615
616   if (message_format)
617     {
618       va_start (args, message_format);
619       msg = g_markup_vprintf_escaped (message_format, args);
620       va_end (args);
621
622       gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
623
624       g_free (msg);
625     }
626
627   return widget;
628 }
629
630 /**
631  * gtk_message_dialog_set_markup:
632  * @message_dialog: a #GtkMessageDialog
633  * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
634  * 
635  * Sets the text of the message dialog to be @str, which is marked
636  * up with the <link linkend="PangoMarkupFormat">Pango text markup
637  * language</link>.
638  *
639  * Since: 2.4
640  **/
641 void
642 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
643                                const gchar      *str)
644 {
645   GtkMessageDialogPrivate *priv;
646
647   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
648
649   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
650   priv->has_primary_markup = TRUE;
651   gtk_label_set_markup (GTK_LABEL (message_dialog->label), str);
652 }
653
654 /**
655  * gtk_message_dialog_format_secondary_text:
656  * @message_dialog: a #GtkMessageDialog
657  * @message_format: printf()-style format string, or %NULL
658  * @Varargs: arguments for @message_format
659  * 
660  * Sets the secondary text of the message dialog to be @message_format 
661  * (with printf()-style).
662  *
663  * Note that setting a secondary text makes the primary text become
664  * bold, unless you have provided explicit markup.
665  *
666  * Since: 2.6
667  **/
668 void
669 gtk_message_dialog_format_secondary_text (GtkMessageDialog *message_dialog,
670                                           const gchar      *message_format,
671                                           ...)
672 {
673   va_list args;
674   gchar *msg = NULL;
675   GtkMessageDialogPrivate *priv;
676
677   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
678
679   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
680
681   if (message_format)
682     {
683       priv->has_secondary_text = TRUE;
684
685       va_start (args, message_format);
686       msg = g_strdup_vprintf (message_format, args);
687       va_end (args);
688
689       gtk_widget_show (priv->secondary_label);
690       gtk_label_set_text (GTK_LABEL (priv->secondary_label), msg);
691
692       g_free (msg);
693     }
694   else
695     {
696       priv->has_secondary_text = FALSE;
697       gtk_widget_hide (priv->secondary_label);
698     }
699
700   setup_primary_label_font (message_dialog);
701 }
702
703 /**
704  * gtk_message_dialog_format_secondary_markup:
705  * @message_dialog: a #GtkMessageDialog
706  * @message_format: printf()-style markup string (see 
707      <link linkend="PangoMarkupFormat">Pango markup format</link>), or %NULL
708  * @Varargs: arguments for @message_format
709  * 
710  * Sets the secondary text of the message dialog to be @message_format (with 
711  * printf()-style), which is marked up with the 
712  * <link linkend="PangoMarkupFormat">Pango text markup language</link>.
713  *
714  * Note that setting a secondary text makes the primary text become
715  * bold, unless you have provided explicit markup.
716  *
717  * Due to an oversight, this function does not escape special XML characters
718  * like gtk_message_dialog_new_with_markup() does. Thus, if the arguments 
719  * may contain special XML characters, you should use g_markup_printf_escaped()
720  * to escape it.
721
722  * <informalexample><programlisting>
723  * gchar *msg;
724  *  
725  * msg = g_markup_printf_escaped (message_format, ...);
726  * gtk_message_dialog_format_secondary_markup (message_dialog, "&percnt;s", msg);
727  * g_free (msg);
728  * </programlisting></informalexample>
729  *
730  * Since: 2.6
731  **/
732 void
733 gtk_message_dialog_format_secondary_markup (GtkMessageDialog *message_dialog,
734                                             const gchar      *message_format,
735                                             ...)
736 {
737   va_list args;
738   gchar *msg = NULL;
739   GtkMessageDialogPrivate *priv;
740
741   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
742
743   priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
744
745   if (message_format)
746     {
747       priv->has_secondary_text = TRUE;
748
749       va_start (args, message_format);
750       msg = g_strdup_vprintf (message_format, args);
751       va_end (args);
752
753       gtk_widget_show (priv->secondary_label);
754       gtk_label_set_markup (GTK_LABEL (priv->secondary_label), msg);
755
756       g_free (msg);
757     }
758   else
759     {
760       priv->has_secondary_text = FALSE;
761       gtk_widget_hide (priv->secondary_label);
762     }
763
764   setup_primary_label_font (message_dialog);
765 }
766
767 static void
768 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
769                                 GtkButtonsType buttons)
770 {
771   GtkDialog* dialog = GTK_DIALOG (message_dialog);
772
773   switch (buttons)
774     {
775     case GTK_BUTTONS_NONE:
776       /* nothing */
777       break;
778
779     case GTK_BUTTONS_OK:
780       gtk_dialog_add_button (dialog,
781                              GTK_STOCK_OK,
782                              GTK_RESPONSE_OK);
783       break;
784
785     case GTK_BUTTONS_CLOSE:
786       gtk_dialog_add_button (dialog,
787                              GTK_STOCK_CLOSE,
788                              GTK_RESPONSE_CLOSE);
789       break;
790
791     case GTK_BUTTONS_CANCEL:
792       gtk_dialog_add_button (dialog,
793                              GTK_STOCK_CANCEL,
794                              GTK_RESPONSE_CANCEL);
795       break;
796
797     case GTK_BUTTONS_YES_NO:
798       gtk_dialog_add_button (dialog,
799                              GTK_STOCK_NO,
800                              GTK_RESPONSE_NO);
801       gtk_dialog_add_button (dialog,
802                              GTK_STOCK_YES,
803                              GTK_RESPONSE_YES);
804       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
805                                                GTK_RESPONSE_YES,
806                                                GTK_RESPONSE_NO,
807                                                -1);
808       break;
809
810     case GTK_BUTTONS_OK_CANCEL:
811       gtk_dialog_add_button (dialog,
812                              GTK_STOCK_CANCEL,
813                              GTK_RESPONSE_CANCEL);
814       gtk_dialog_add_button (dialog,
815                              GTK_STOCK_OK,
816                              GTK_RESPONSE_OK);
817       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
818                                                GTK_RESPONSE_OK,
819                                                GTK_RESPONSE_CANCEL,
820                                                -1);
821       break;
822       
823     default:
824       g_warning ("Unknown GtkButtonsType");
825       break;
826     } 
827
828   g_object_notify (G_OBJECT (message_dialog), "buttons");
829 }
830
831 static void
832 gtk_message_dialog_style_set (GtkWidget *widget,
833                               GtkStyle  *prev_style)
834 {
835   GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (widget);
836   gboolean use_separator;
837   GtkWidget *parent;
838   gint border_width;
839
840   parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
841
842   if (parent)
843     {
844       gtk_widget_style_get (widget, "message-border",
845                             &border_width, NULL);
846       
847       gtk_container_set_border_width (GTK_CONTAINER (parent),
848                                       MAX (0, border_width - 7));
849     }
850
851   gtk_widget_style_get (widget,
852                         "use-separator", &use_separator,
853                         NULL);
854
855   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), FALSE);
856   gtk_dialog_set_has_separator (GTK_DIALOG (widget), use_separator);
857   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), TRUE);
858
859   setup_primary_label_font (dialog);
860
861   if (GTK_WIDGET_CLASS (parent_class)->style_set)
862     (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);
863 }
864
865 #define __GTK_MESSAGE_DIALOG_C__
866 #include "gtkaliasdef.c"