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 gboolean has_primary_markup;
50 gboolean has_secondary_text;
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);
58 static void gtk_message_dialog_set_property (GObject *object,
62 static void gtk_message_dialog_get_property (GObject *object,
66 static void gtk_message_dialog_add_buttons (GtkMessageDialog *message_dialog,
67 GtkButtonsType buttons);
76 PROP_SECONDARY_USE_MARKUP
79 static gpointer parent_class;
82 gtk_message_dialog_get_type (void)
84 static GType dialog_type = 0;
88 static const GTypeInfo dialog_info =
90 sizeof (GtkMessageDialogClass),
92 NULL, /* base_finalize */
93 (GClassInitFunc) gtk_message_dialog_class_init,
94 NULL, /* class_finalize */
95 NULL, /* class_data */
96 sizeof (GtkMessageDialog),
98 (GInstanceInitFunc) gtk_message_dialog_init,
101 dialog_type = g_type_register_static (GTK_TYPE_DIALOG, I_("GtkMessageDialog"),
109 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
111 GtkWidgetClass *widget_class;
112 GObjectClass *gobject_class;
114 widget_class = GTK_WIDGET_CLASS (class);
115 gobject_class = G_OBJECT_CLASS (class);
117 parent_class = g_type_class_peek_parent (class);
119 widget_class->style_set = gtk_message_dialog_style_set;
121 gobject_class->set_property = gtk_message_dialog_set_property;
122 gobject_class->get_property = gtk_message_dialog_get_property;
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"),
131 GTK_PARAM_READABLE));
133 * GtkMessageDialog::use_separator
135 * Whether to draw a separator line between the message label and the buttons
140 gtk_widget_class_install_style_property (widget_class,
141 g_param_spec_boolean ("use-separator",
143 P_("Whether to put a separator between the message dialog's text and the buttons"),
145 GTK_PARAM_READABLE));
146 g_object_class_install_property (gobject_class,
148 g_param_spec_enum ("message-type",
150 P_("The type of message"),
151 GTK_TYPE_MESSAGE_TYPE,
153 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
154 g_object_class_install_property (gobject_class,
156 g_param_spec_enum ("buttons",
157 P_("Message Buttons"),
158 P_("The buttons shown in the message dialog"),
159 GTK_TYPE_BUTTONS_TYPE,
161 GTK_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
164 * GtkMessageDialog:text:
166 * The primary text of the message dialog. If the dialog has
167 * a secondary text, this will appear as the title.
171 g_object_class_install_property (gobject_class,
173 g_param_spec_string ("text",
175 P_("The primary text of the message dialog"),
177 GTK_PARAM_READWRITE));
180 * GtkMessageDialog:use-markup:
182 * %TRUE if the primary text of the dialog includes Pango markup.
183 * See pango_parse_markup().
187 g_object_class_install_property (gobject_class,
189 g_param_spec_boolean ("use-markup",
191 P_("The primary text of the title includes Pango markup."),
193 GTK_PARAM_READWRITE));
196 * GtkMessageDialog:secondary-text:
198 * The secondary text of the message dialog.
202 g_object_class_install_property (gobject_class,
204 g_param_spec_string ("secondary-text",
205 P_("Secondary Text"),
206 P_("The secondary text of the message dialog"),
208 GTK_PARAM_READWRITE));
211 * GtkMessageDialog:secondary-use-markup:
213 * %TRUE if the secondary text of the dialog includes Pango markup.
214 * See pango_parse_markup().
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."),
224 GTK_PARAM_READWRITE));
226 g_type_class_add_private (gobject_class,
227 sizeof (GtkMessageDialogPrivate));
231 gtk_message_dialog_init (GtkMessageDialog *dialog)
233 GtkWidget *hbox, *vbox;
234 GtkMessageDialogPrivate *priv;
236 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
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);
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);
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);
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);
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);
259 hbox = gtk_hbox_new (FALSE, 12);
260 vbox = gtk_vbox_new (FALSE, 12);
262 gtk_box_pack_start (GTK_BOX (vbox), dialog->label,
265 gtk_box_pack_start (GTK_BOX (vbox), priv->secondary_label,
268 gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
271 gtk_box_pack_start (GTK_BOX (hbox), vbox,
274 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
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);
284 gtk_widget_show_all (hbox);
286 _gtk_dialog_set_ignore_separator (GTK_DIALOG (dialog), TRUE);
289 static GtkMessageType
290 gtk_message_dialog_get_message_type (GtkMessageDialog *dialog)
292 const gchar* stock_id = NULL;
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);
297 stock_id = GTK_IMAGE(dialog->image)->data.stock.stock_id;
299 /* Look at the stock id of the image to guess the
300 * GtkMessageType value that was used to choose it
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;
313 g_assert_not_reached ();
314 return GTK_MESSAGE_INFO;
319 setup_primary_label_font (GtkMessageDialog *dialog)
322 PangoFontDescription *font_desc;
323 GtkMessageDialogPrivate *priv;
325 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
327 /* unset the font settings */
328 gtk_widget_modify_font (dialog->label, NULL);
330 if (priv->has_secondary_text && !priv->has_primary_markup)
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);
342 setup_type (GtkMessageDialog *dialog,
345 const gchar *stock_id = NULL;
350 case GTK_MESSAGE_INFO:
351 stock_id = GTK_STOCK_DIALOG_INFO;
354 case GTK_MESSAGE_QUESTION:
355 stock_id = GTK_STOCK_DIALOG_QUESTION;
358 case GTK_MESSAGE_WARNING:
359 stock_id = GTK_STOCK_DIALOG_WARNING;
362 case GTK_MESSAGE_ERROR:
363 stock_id = GTK_STOCK_DIALOG_ERROR;
367 g_warning ("Unknown GtkMessageType %d", type);
371 if (stock_id == NULL)
372 stock_id = GTK_STOCK_DIALOG_INFO;
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);
378 g_warning ("Stock dialog ID doesn't exist?");
382 gtk_message_dialog_set_property (GObject *object,
387 GtkMessageDialog *dialog;
388 GtkMessageDialogPrivate *priv;
390 dialog = GTK_MESSAGE_DIALOG (object);
391 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
395 case PROP_MESSAGE_TYPE:
396 setup_type (dialog, g_value_get_enum (value));
399 gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
402 if (priv->has_primary_markup)
403 gtk_label_set_markup (GTK_LABEL (dialog->label),
404 g_value_get_string (value));
406 gtk_label_set_text (GTK_LABEL (dialog->label),
407 g_value_get_string (value));
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);
415 case PROP_SECONDARY_TEXT:
417 const gchar *txt = g_value_get_string (value);
419 if (gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)))
420 gtk_label_set_markup (GTK_LABEL (priv->secondary_label), txt);
422 gtk_label_set_text (GTK_LABEL (priv->secondary_label), txt);
426 priv->has_secondary_text = TRUE;
427 gtk_widget_show (priv->secondary_label);
431 priv->has_secondary_text = FALSE;
432 gtk_widget_hide (priv->secondary_label);
434 setup_primary_label_font (dialog);
437 case PROP_SECONDARY_USE_MARKUP:
438 gtk_label_set_use_markup (GTK_LABEL (priv->secondary_label),
439 g_value_get_boolean (value));
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gtk_message_dialog_get_property (GObject *object,
454 GtkMessageDialog *dialog;
455 GtkMessageDialogPrivate *priv;
457 dialog = GTK_MESSAGE_DIALOG (object);
458 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (dialog);
462 case PROP_MESSAGE_TYPE:
463 g_value_set_enum (value, gtk_message_dialog_get_message_type (dialog));
466 g_value_set_string (value, gtk_label_get_label (GTK_LABEL (dialog->label)));
468 case PROP_USE_MARKUP:
469 g_value_set_boolean (value, priv->has_primary_markup);
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)));
476 g_value_set_string (value, NULL);
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)));
483 g_value_set_boolean (value, FALSE);
486 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
492 * gtk_message_dialog_new:
493 * @parent: transient parent, or %NULL for none
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
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.
506 * Return value: a new #GtkMessageDialog
509 gtk_message_dialog_new (GtkWindow *parent,
510 GtkDialogFlags flags,
512 GtkButtonsType buttons,
513 const gchar *message_format,
521 g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
523 widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
524 "message-type", type,
527 dialog = GTK_DIALOG (widget);
529 if (flags & GTK_DIALOG_NO_SEPARATOR)
531 g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
532 flags &= ~GTK_DIALOG_NO_SEPARATOR;
537 va_start (args, message_format);
538 msg = g_strdup_vprintf (message_format, args);
541 gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
548 gtk_window_set_transient_for (GTK_WINDOW (widget),
549 GTK_WINDOW (parent));
551 if (flags & GTK_DIALOG_MODAL)
552 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
554 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
555 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
561 * gtk_message_dialog_new_with_markup:
562 * @parent: transient parent, or %NULL for none
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
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.
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
585 * <informalexample><programlisting>
587 * dialog = gtk_message_dialog_new (main_application_window,
588 * GTK_DIALOG_DESTROY_WITH_PARENT,
592 * gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
594 * </programlisting></informalexample>
596 * Return value: a new #GtkMessageDialog
601 gtk_message_dialog_new_with_markup (GtkWindow *parent,
602 GtkDialogFlags flags,
604 GtkButtonsType buttons,
605 const gchar *message_format,
612 g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
614 widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
618 va_start (args, message_format);
619 msg = g_markup_vprintf_escaped (message_format, args);
622 gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
631 * gtk_message_dialog_set_markup:
632 * @message_dialog: a #GtkMessageDialog
633 * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
635 * Sets the text of the message dialog to be @str, which is marked
636 * up with the <link linkend="PangoMarkupFormat">Pango text markup
642 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
645 GtkMessageDialogPrivate *priv;
647 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
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);
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
660 * Sets the secondary text of the message dialog to be @message_format
661 * (with printf()-style).
663 * Note that setting a secondary text makes the primary text become
664 * bold, unless you have provided explicit markup.
669 gtk_message_dialog_format_secondary_text (GtkMessageDialog *message_dialog,
670 const gchar *message_format,
675 GtkMessageDialogPrivate *priv;
677 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
679 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
683 priv->has_secondary_text = TRUE;
685 va_start (args, message_format);
686 msg = g_strdup_vprintf (message_format, args);
689 gtk_widget_show (priv->secondary_label);
690 gtk_label_set_text (GTK_LABEL (priv->secondary_label), msg);
696 priv->has_secondary_text = FALSE;
697 gtk_widget_hide (priv->secondary_label);
700 setup_primary_label_font (message_dialog);
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
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>.
714 * Note that setting a secondary text makes the primary text become
715 * bold, unless you have provided explicit markup.
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()
722 * <informalexample><programlisting>
725 * msg = g_markup_printf_escaped (message_format, ...);
726 * gtk_message_dialog_format_secondary_markup (message_dialog, "%s", msg);
728 * </programlisting></informalexample>
733 gtk_message_dialog_format_secondary_markup (GtkMessageDialog *message_dialog,
734 const gchar *message_format,
739 GtkMessageDialogPrivate *priv;
741 g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
743 priv = GTK_MESSAGE_DIALOG_GET_PRIVATE (message_dialog);
747 priv->has_secondary_text = TRUE;
749 va_start (args, message_format);
750 msg = g_strdup_vprintf (message_format, args);
753 gtk_widget_show (priv->secondary_label);
754 gtk_label_set_markup (GTK_LABEL (priv->secondary_label), msg);
760 priv->has_secondary_text = FALSE;
761 gtk_widget_hide (priv->secondary_label);
764 setup_primary_label_font (message_dialog);
768 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
769 GtkButtonsType buttons)
771 GtkDialog* dialog = GTK_DIALOG (message_dialog);
775 case GTK_BUTTONS_NONE:
780 gtk_dialog_add_button (dialog,
785 case GTK_BUTTONS_CLOSE:
786 gtk_dialog_add_button (dialog,
791 case GTK_BUTTONS_CANCEL:
792 gtk_dialog_add_button (dialog,
794 GTK_RESPONSE_CANCEL);
797 case GTK_BUTTONS_YES_NO:
798 gtk_dialog_add_button (dialog,
801 gtk_dialog_add_button (dialog,
804 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
810 case GTK_BUTTONS_OK_CANCEL:
811 gtk_dialog_add_button (dialog,
813 GTK_RESPONSE_CANCEL);
814 gtk_dialog_add_button (dialog,
817 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
824 g_warning ("Unknown GtkButtonsType");
828 g_object_notify (G_OBJECT (message_dialog), "buttons");
832 gtk_message_dialog_style_set (GtkWidget *widget,
833 GtkStyle *prev_style)
835 GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (widget);
836 gboolean use_separator;
840 parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
844 gtk_widget_style_get (widget, "message-border",
845 &border_width, NULL);
847 gtk_container_set_border_width (GTK_CONTAINER (parent),
848 MAX (0, border_width - 7));
851 gtk_widget_style_get (widget,
852 "use-separator", &use_separator,
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);
859 setup_primary_label_font (dialog);
861 if (GTK_WIDGET_CLASS (parent_class)->style_set)
862 (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);
865 #define __GTK_MESSAGE_DIALOG_C__
866 #include "gtkaliasdef.c"