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