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.
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.
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.
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.
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/.
31 #include "gtkmessagedialog.h"
37 #include "gtkiconfactory.h"
39 #include "gtkprivate.h"
42 #define GTK_MESSAGE_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MESSAGE_DIALOG, GtkMessageDialogPrivate))
44 typedef struct _GtkMessageDialogPrivate GtkMessageDialogPrivate;
46 struct _GtkMessageDialogPrivate
48 GtkWidget *secondary_label;
49 guint message_type : 3;
50 guint has_primary_markup : 1;
51 guint has_secondary_text : 1;
54 static void gtk_message_dialog_style_set (GtkWidget *widget,
55 GtkStyle *prev_style);
57 static void gtk_message_dialog_set_property (GObject *object,
61 static void gtk_message_dialog_get_property (GObject *object,
65 static void gtk_message_dialog_add_buttons (GtkMessageDialog *message_dialog,
66 GtkButtonsType buttons);
75 PROP_SECONDARY_USE_MARKUP,
79 G_DEFINE_TYPE (GtkMessageDialog, gtk_message_dialog, GTK_TYPE_DIALOG)
82 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
84 GtkWidgetClass *widget_class;
85 GObjectClass *gobject_class;
87 widget_class = GTK_WIDGET_CLASS (class);
88 gobject_class = G_OBJECT_CLASS (class);
90 widget_class->style_set = gtk_message_dialog_style_set;
92 gobject_class->set_property = gtk_message_dialog_set_property;
93 gobject_class->get_property = gtk_message_dialog_get_property;
95 gtk_widget_class_install_style_property (widget_class,
96 g_param_spec_int ("message-border",
97 P_("Image/label border"),
98 P_("Width of border around the label and image in the message dialog"),
102 GTK_PARAM_READABLE));
104 * GtkMessageDialog:use-separator:
106 * Whether to draw a separator line between the message label and the buttons
111 gtk_widget_class_install_style_property (widget_class,
112 g_param_spec_boolean ("use-separator",
114 P_("Whether to put a separator between the message dialog's text and the buttons"),
116 GTK_PARAM_READABLE));
118 * GtkMessageDialog:message-type:
120 * The type of the message. The type is used to determine
121 * the image that is shown in the dialog, unless the image is
122 * explicitly set by the ::image property.
124 g_object_class_install_property (gobject_class,
126 g_param_spec_enum ("message-type",
128 P_("The type of message"),
129 GTK_TYPE_MESSAGE_TYPE,
131 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
132 g_object_class_install_property (gobject_class,
134 g_param_spec_enum ("buttons",
135 P_("Message Buttons"),
136 P_("The buttons shown in the message dialog"),
137 GTK_TYPE_BUTTONS_TYPE,
139 GTK_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
142 * GtkMessageDialog:text:
144 * The primary text of the message dialog. If the dialog has
145 * a secondary text, this will appear as the title.
149 g_object_class_install_property (gobject_class,
151 g_param_spec_string ("text",
153 P_("The primary text of the message dialog"),
155 GTK_PARAM_READWRITE));
158 * GtkMessageDialog:use-markup:
160 * %TRUE if the primary text of the dialog includes Pango markup.
161 * See pango_parse_markup().
165 g_object_class_install_property (gobject_class,
167 g_param_spec_boolean ("use-markup",
169 P_("The primary text of the title includes Pango markup."),
171 GTK_PARAM_READWRITE));
174 * GtkMessageDialog:secondary-text:
176 * The secondary text of the message dialog.
180 g_object_class_install_property (gobject_class,
182 g_param_spec_string ("secondary-text",
183 P_("Secondary Text"),
184 P_("The secondary text of the message dialog"),
186 GTK_PARAM_READWRITE));
189 * GtkMessageDialog:secondary-use-markup:
191 * %TRUE if the secondary text of the dialog includes Pango markup.
192 * See pango_parse_markup().
196 g_object_class_install_property (gobject_class,
197 PROP_SECONDARY_USE_MARKUP,
198 g_param_spec_boolean ("secondary-use-markup",
199 P_("Use Markup in secondary"),
200 P_("The secondary text includes Pango markup."),
202 GTK_PARAM_READWRITE));
205 * GtkMessageDialog:image:
207 * The image for this dialog.
211 g_object_class_install_property (gobject_class,
213 g_param_spec_object ("image",
217 GTK_PARAM_READWRITE));
219 g_type_class_add_private (gobject_class,
220 sizeof (GtkMessageDialogPrivate));
224 gtk_message_dialog_init (GtkMessageDialog *dialog)
226 GtkWidget *hbox, *vbox;
227 GtkMessageDialogPrivate *priv;
229 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
231 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
232 gtk_window_set_title (GTK_WINDOW (dialog), "");
233 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), TRUE);
235 priv->has_primary_markup = FALSE;
236 priv->has_secondary_text = FALSE;
237 priv->secondary_label = gtk_label_new (NULL);
238 gtk_widget_set_no_show_all (priv->secondary_label, TRUE);
240 dialog->label = gtk_label_new (NULL);
241 dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
242 gtk_misc_set_alignment (GTK_MISC (dialog->image), 0.5, 0.0);
244 gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
245 gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
246 gtk_misc_set_alignment (GTK_MISC (dialog->label), 0.0, 0.0);
248 gtk_label_set_line_wrap (GTK_LABEL (priv->secondary_label), TRUE);
249 gtk_label_set_selectable (GTK_LABEL (priv->secondary_label), TRUE);
250 gtk_misc_set_alignment (GTK_MISC (priv->secondary_label), 0.0, 0.0);
252 hbox = gtk_hbox_new (FALSE, 12);
253 vbox = gtk_vbox_new (FALSE, 12);
255 gtk_box_pack_start (GTK_BOX (vbox), dialog->label,
258 gtk_box_pack_start (GTK_BOX (vbox), priv->secondary_label,
261 gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
264 gtk_box_pack_start (GTK_BOX (hbox), vbox,
267 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
271 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
272 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
273 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14); /* 14 + 2 * 5 = 24 */
274 gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 5);
275 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->action_area), 6);
277 gtk_widget_show_all (hbox);
279 _gtk_dialog_set_ignore_separator (GTK_DIALOG (dialog), TRUE);
283 setup_primary_label_font (GtkMessageDialog *dialog)
286 PangoFontDescription *font_desc;
287 GtkMessageDialogPrivate *priv;
289 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
291 /* unset the font settings */
292 gtk_widget_modify_font (dialog->label, NULL);
294 if (priv->has_secondary_text && !priv->has_primary_markup)
296 size = pango_font_description_get_size (dialog->label->style->font_desc);
297 font_desc = pango_font_description_new ();
298 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
299 pango_font_description_set_size (font_desc, size * PANGO_SCALE_LARGE);
300 gtk_widget_modify_font (dialog->label, font_desc);
301 pango_font_description_free (font_desc);
306 setup_type (GtkMessageDialog *dialog,
309 GtkMessageDialogPrivate *priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
310 const gchar *stock_id = NULL;
312 priv->message_type = type;
316 case GTK_MESSAGE_INFO:
317 stock_id = GTK_STOCK_DIALOG_INFO;
320 case GTK_MESSAGE_QUESTION:
321 stock_id = GTK_STOCK_DIALOG_QUESTION;
324 case GTK_MESSAGE_WARNING:
325 stock_id = GTK_STOCK_DIALOG_WARNING;
328 case GTK_MESSAGE_ERROR:
329 stock_id = GTK_STOCK_DIALOG_ERROR;
332 case GTK_MESSAGE_OTHER:
336 g_warning ("Unknown GtkMessageType %u", type);
341 gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
342 GTK_ICON_SIZE_DIALOG);
346 gtk_message_dialog_set_property (GObject *object,
351 GtkMessageDialog *dialog;
352 GtkMessageDialogPrivate *priv;
354 dialog = GTK_MESSAGE_DIALOG (object);
355 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
359 case PROP_MESSAGE_TYPE:
360 setup_type (dialog, g_value_get_enum (value));
363 gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
366 if (priv->has_primary_markup)
367 gtk_label_set_markup (GTK_LABEL (dialog->label),
368 g_value_get_string (value));
370 gtk_label_set_text (GTK_LABEL (dialog->label),
371 g_value_get_string (value));
373 case PROP_USE_MARKUP:
374 priv->has_primary_markup = g_value_get_boolean (value) != FALSE;
375 gtk_label_set_use_markup (GTK_LABEL (dialog->label),
376 priv->has_primary_markup);
377 setup_primary_label_font (dialog);
379 case PROP_SECONDARY_TEXT:
381 const gchar *txt = g_value_get_string (value);
383 if (gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)))
384 gtk_label_set_markup (GTK_LABEL (priv->secondary_label), txt);
386 gtk_label_set_text (GTK_LABEL (priv->secondary_label), txt);
390 priv->has_secondary_text = TRUE;
391 gtk_widget_show (priv->secondary_label);
395 priv->has_secondary_text = FALSE;
396 gtk_widget_hide (priv->secondary_label);
398 setup_primary_label_font (dialog);
401 case PROP_SECONDARY_USE_MARKUP:
402 gtk_label_set_use_markup (GTK_LABEL (priv->secondary_label),
403 g_value_get_boolean (value));
406 gtk_message_dialog_set_image (dialog, (GtkWidget *)g_value_get_object (value));
410 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416 gtk_message_dialog_get_property (GObject *object,
421 GtkMessageDialog *dialog;
422 GtkMessageDialogPrivate *priv;
424 dialog = GTK_MESSAGE_DIALOG (object);
425 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
429 case PROP_MESSAGE_TYPE:
430 g_value_set_enum (value, (GtkMessageType) priv->message_type);
433 g_value_set_string (value, gtk_label_get_label (GTK_LABEL (dialog->label)));
435 case PROP_USE_MARKUP:
436 g_value_set_boolean (value, priv->has_primary_markup);
438 case PROP_SECONDARY_TEXT:
439 if (priv->has_secondary_text)
440 g_value_set_string (value,
441 gtk_label_get_label (GTK_LABEL (priv->secondary_label)));
443 g_value_set_string (value, NULL);
445 case PROP_SECONDARY_USE_MARKUP:
446 if (priv->has_secondary_text)
447 g_value_set_boolean (value,
448 gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)));
450 g_value_set_boolean (value, FALSE);
453 g_value_set_object (value, dialog->image);
456 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462 * gtk_message_dialog_new:
463 * @parent: transient parent, or %NULL for none
465 * @type: type of message
466 * @buttons: set of buttons to use
467 * @message_format: printf()-style format string, or %NULL
468 * @Varargs: arguments for @message_format
470 * Creates a new message dialog, which is a simple dialog with an icon
471 * indicating the dialog type (error, warning, etc.) and some text the
472 * user may want to see. When the user clicks a button a "response"
473 * signal is emitted with response IDs from #GtkResponseType. See
474 * #GtkDialog for more details.
476 * Return value: a new #GtkMessageDialog
479 gtk_message_dialog_new (GtkWindow *parent,
480 GtkDialogFlags flags,
482 GtkButtonsType buttons,
483 const gchar *message_format,
491 g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
493 widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
494 "message-type", type,
497 dialog = GTK_DIALOG (widget);
499 if (flags & GTK_DIALOG_NO_SEPARATOR)
501 g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
502 flags &= ~GTK_DIALOG_NO_SEPARATOR;
507 va_start (args, message_format);
508 msg = g_strdup_vprintf (message_format, args);
511 gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
518 gtk_window_set_transient_for (GTK_WINDOW (widget),
519 GTK_WINDOW (parent));
521 if (flags & GTK_DIALOG_MODAL)
522 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
524 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
525 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
531 * gtk_message_dialog_new_with_markup:
532 * @parent: transient parent, or %NULL for none
534 * @type: type of message
535 * @buttons: set of buttons to use
536 * @message_format: printf()-style format string, or %NULL
537 * @Varargs: arguments for @message_format
539 * Creates a new message dialog, which is a simple dialog with an icon
540 * indicating the dialog type (error, warning, etc.) and some text which
541 * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
542 * When the user clicks a button a "response" signal is emitted with
543 * response IDs from #GtkResponseType. See #GtkDialog for more details.
545 * Special XML characters in the printf() arguments passed to this
546 * function will automatically be escaped as necessary.
547 * (See g_markup_printf_escaped() for how this is implemented.)
548 * Usually this is what you want, but if you have an existing
549 * Pango markup string that you want to use literally as the
550 * label, then you need to use gtk_message_dialog_set_markup()
551 * instead, since you can't pass the markup string either
552 * as the format (it might contain '%' characters) or as a string
555 * <informalexample><programlisting>
557 * dialog = gtk_message_dialog_new (main_application_window,
558 * GTK_DIALOG_DESTROY_WITH_PARENT,
562 * gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
564 * </programlisting></informalexample>
566 * Return value: a new #GtkMessageDialog
571 gtk_message_dialog_new_with_markup (GtkWindow *parent,
572 GtkDialogFlags flags,
574 GtkButtonsType buttons,
575 const gchar *message_format,
582 g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
584 widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
588 va_start (args, message_format);
589 msg = g_markup_vprintf_escaped (message_format, args);
592 gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
601 * gtk_message_dialog_set_image:
602 * @dialog: a #GtkMessageDialog
605 * Sets the dialog's image to @image.
610 gtk_message_dialog_set_image (GtkMessageDialog *dialog,
613 GtkMessageDialogPrivate *priv;
616 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (dialog));
618 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
620 priv->message_type = GTK_MESSAGE_OTHER;
622 parent = dialog->image->parent;
623 gtk_container_add (GTK_CONTAINER (parent), image);
624 gtk_container_remove (GTK_CONTAINER (parent), dialog->image);
625 gtk_box_reorder_child (GTK_BOX (parent), image, 0);
627 dialog->image = image;
629 g_object_notify (G_OBJECT (dialog), "image");
633 * gtk_message_dialog_set_markup:
634 * @message_dialog: a #GtkMessageDialog
635 * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
637 * Sets the text of the message dialog to be @str, which is marked
638 * up with the <link linkend="PangoMarkupFormat">Pango text markup
644 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
647 GtkMessageDialogPrivate *priv;
649 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
651 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
652 priv->has_primary_markup = TRUE;
653 gtk_label_set_markup (GTK_LABEL (message_dialog->label), str);
657 * gtk_message_dialog_format_secondary_text:
658 * @message_dialog: a #GtkMessageDialog
659 * @message_format: printf()-style format string, or %NULL
660 * @Varargs: arguments for @message_format
662 * Sets the secondary text of the message dialog to be @message_format
663 * (with printf()-style).
665 * Note that setting a secondary text makes the primary text become
666 * bold, unless you have provided explicit markup.
671 gtk_message_dialog_format_secondary_text (GtkMessageDialog *message_dialog,
672 const gchar *message_format,
677 GtkMessageDialogPrivate *priv;
679 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
681 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
685 priv->has_secondary_text = TRUE;
687 va_start (args, message_format);
688 msg = g_strdup_vprintf (message_format, args);
691 gtk_widget_show (priv->secondary_label);
692 gtk_label_set_text (GTK_LABEL (priv->secondary_label), msg);
698 priv->has_secondary_text = FALSE;
699 gtk_widget_hide (priv->secondary_label);
702 setup_primary_label_font (message_dialog);
706 * gtk_message_dialog_format_secondary_markup:
707 * @message_dialog: a #GtkMessageDialog
708 * @message_format: printf()-style markup string (see
709 <link linkend="PangoMarkupFormat">Pango markup format</link>), or %NULL
710 * @Varargs: arguments for @message_format
712 * Sets the secondary text of the message dialog to be @message_format (with
713 * printf()-style), which is marked up with the
714 * <link linkend="PangoMarkupFormat">Pango text markup language</link>.
716 * Note that setting a secondary text makes the primary text become
717 * bold, unless you have provided explicit markup.
719 * Due to an oversight, this function does not escape special XML characters
720 * like gtk_message_dialog_new_with_markup() does. Thus, if the arguments
721 * may contain special XML characters, you should use g_markup_printf_escaped()
724 * <informalexample><programlisting>
727 * msg = g_markup_printf_escaped (message_format, ...);
728 * gtk_message_dialog_format_secondary_markup (message_dialog, "%s", msg);
730 * </programlisting></informalexample>
735 gtk_message_dialog_format_secondary_markup (GtkMessageDialog *message_dialog,
736 const gchar *message_format,
741 GtkMessageDialogPrivate *priv;
743 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
745 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
749 priv->has_secondary_text = TRUE;
751 va_start (args, message_format);
752 msg = g_strdup_vprintf (message_format, args);
755 gtk_widget_show (priv->secondary_label);
756 gtk_label_set_markup (GTK_LABEL (priv->secondary_label), msg);
762 priv->has_secondary_text = FALSE;
763 gtk_widget_hide (priv->secondary_label);
766 setup_primary_label_font (message_dialog);
770 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
771 GtkButtonsType buttons)
773 GtkDialog* dialog = GTK_DIALOG (message_dialog);
777 case GTK_BUTTONS_NONE:
782 gtk_dialog_add_button (dialog,
787 case GTK_BUTTONS_CLOSE:
788 gtk_dialog_add_button (dialog,
793 case GTK_BUTTONS_CANCEL:
794 gtk_dialog_add_button (dialog,
796 GTK_RESPONSE_CANCEL);
799 case GTK_BUTTONS_YES_NO:
800 gtk_dialog_add_button (dialog,
803 gtk_dialog_add_button (dialog,
806 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
812 case GTK_BUTTONS_OK_CANCEL:
813 gtk_dialog_add_button (dialog,
815 GTK_RESPONSE_CANCEL);
816 gtk_dialog_add_button (dialog,
819 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
826 g_warning ("Unknown GtkButtonsType");
830 g_object_notify (G_OBJECT (message_dialog), "buttons");
834 gtk_message_dialog_style_set (GtkWidget *widget,
835 GtkStyle *prev_style)
837 GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (widget);
838 gboolean use_separator;
842 parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
846 gtk_widget_style_get (widget, "message-border",
847 &border_width, NULL);
849 gtk_container_set_border_width (GTK_CONTAINER (parent),
850 MAX (0, border_width - 7));
853 gtk_widget_style_get (widget,
854 "use-separator", &use_separator,
857 _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), FALSE);
858 gtk_dialog_set_has_separator (GTK_DIALOG (widget), use_separator);
859 _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), TRUE);
861 setup_primary_label_font (dialog);
863 if (GTK_WIDGET_CLASS (gtk_message_dialog_parent_class)->style_set)
864 (GTK_WIDGET_CLASS (gtk_message_dialog_parent_class)->style_set) (widget, prev_style);
867 #define __GTK_MESSAGE_DIALOG_C__
868 #include "gtkaliasdef.c"