1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2003. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkmessagedialog.h"
32 #include "gtkiconfactory.h"
36 static void gtk_message_dialog_class_init (GtkMessageDialogClass *klass);
37 static void gtk_message_dialog_init (GtkMessageDialog *dialog);
38 static void gtk_message_dialog_style_set (GtkWidget *widget,
39 GtkStyle *prev_style);
41 static void gtk_message_dialog_set_property (GObject *object,
45 static void gtk_message_dialog_get_property (GObject *object,
49 static void gtk_message_dialog_add_buttons (GtkMessageDialog *message_dialog,
50 GtkButtonsType buttons);
59 static gpointer parent_class;
62 gtk_message_dialog_get_type (void)
64 static GType dialog_type = 0;
68 static const GTypeInfo dialog_info =
70 sizeof (GtkMessageDialogClass),
72 NULL, /* base_finalize */
73 (GClassInitFunc) gtk_message_dialog_class_init,
74 NULL, /* class_finalize */
75 NULL, /* class_data */
76 sizeof (GtkMessageDialog),
78 (GInstanceInitFunc) gtk_message_dialog_init,
81 dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkMessageDialog",
89 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
91 GtkWidgetClass *widget_class;
92 GObjectClass *gobject_class;
94 widget_class = GTK_WIDGET_CLASS (class);
95 gobject_class = G_OBJECT_CLASS (class);
97 parent_class = g_type_class_peek_parent (class);
99 widget_class->style_set = gtk_message_dialog_style_set;
101 gobject_class->set_property = gtk_message_dialog_set_property;
102 gobject_class->get_property = gtk_message_dialog_get_property;
104 gtk_widget_class_install_style_property (widget_class,
105 g_param_spec_int ("message_border",
106 _("Image/label border"),
107 _("Width of border around the label and image in the message dialog"),
112 g_object_class_install_property (gobject_class,
114 g_param_spec_enum ("message_type",
116 _("The type of message"),
117 GTK_TYPE_MESSAGE_TYPE,
119 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
120 g_object_class_install_property (gobject_class,
122 g_param_spec_enum ("buttons",
123 _("Message Buttons"),
124 _("The buttons shown in the message dialog"),
125 GTK_TYPE_BUTTONS_TYPE,
127 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
132 gtk_message_dialog_init (GtkMessageDialog *dialog)
136 dialog->label = gtk_label_new (NULL);
137 dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
138 gtk_misc_set_alignment (GTK_MISC (dialog->image), 0.5, 0.0);
140 gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
141 gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
143 hbox = gtk_hbox_new (FALSE, 6);
145 gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
148 gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
151 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
155 gtk_widget_show_all (hbox);
158 static GtkMessageType
159 gtk_message_dialog_get_message_type (GtkMessageDialog *dialog)
161 const gchar* stock_id = NULL;
163 g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), GTK_MESSAGE_INFO);
164 g_return_val_if_fail (GTK_IS_IMAGE(dialog->image), GTK_MESSAGE_INFO);
166 stock_id = GTK_IMAGE(dialog->image)->data.stock.stock_id;
168 /* Look at the stock id of the image to guess the
169 * GtkMessageType value that was used to choose it
172 if (strcmp (stock_id, GTK_STOCK_DIALOG_INFO) == 0)
173 return GTK_MESSAGE_INFO;
174 else if (strcmp (stock_id, GTK_STOCK_DIALOG_QUESTION) == 0)
175 return GTK_MESSAGE_QUESTION;
176 else if (strcmp (stock_id, GTK_STOCK_DIALOG_WARNING) == 0)
177 return GTK_MESSAGE_WARNING;
178 else if (strcmp (stock_id, GTK_STOCK_DIALOG_ERROR) == 0)
179 return GTK_MESSAGE_ERROR;
182 g_assert_not_reached ();
183 return GTK_MESSAGE_INFO;
188 setup_type (GtkMessageDialog *dialog,
191 const gchar *stock_id = NULL;
196 case GTK_MESSAGE_INFO:
197 stock_id = GTK_STOCK_DIALOG_INFO;
200 case GTK_MESSAGE_QUESTION:
201 stock_id = GTK_STOCK_DIALOG_QUESTION;
204 case GTK_MESSAGE_WARNING:
205 stock_id = GTK_STOCK_DIALOG_WARNING;
208 case GTK_MESSAGE_ERROR:
209 stock_id = GTK_STOCK_DIALOG_ERROR;
213 g_warning ("Unknown GtkMessageType %d", type);
217 if (stock_id == NULL)
218 stock_id = GTK_STOCK_DIALOG_INFO;
220 if (gtk_stock_lookup (stock_id, &item))
222 gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
223 GTK_ICON_SIZE_DIALOG);
225 gtk_window_set_title (GTK_WINDOW (dialog), item.label);
228 g_warning ("Stock dialog ID doesn't exist?");
232 gtk_message_dialog_set_property (GObject *object,
237 GtkMessageDialog *dialog;
239 dialog = GTK_MESSAGE_DIALOG (object);
243 case PROP_MESSAGE_TYPE:
244 setup_type (dialog, g_value_get_enum (value));
247 gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 gtk_message_dialog_get_property (GObject *object,
261 GtkMessageDialog *dialog;
263 dialog = GTK_MESSAGE_DIALOG (object);
267 case PROP_MESSAGE_TYPE:
268 g_value_set_enum (value, gtk_message_dialog_get_message_type (dialog));
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 * gtk_message_dialog_new:
279 * @parent: transient parent, or NULL for none
281 * @type: type of message
282 * @buttons: set of buttons to use
283 * @message_format: printf()-style format string, or NULL
284 * @Varargs: arguments for @message_format
286 * Creates a new message dialog, which is a simple dialog with an icon
287 * indicating the dialog type (error, warning, etc.) and some text the
288 * user may want to see. When the user clicks a button a "response"
289 * signal is emitted with response IDs from #GtkResponseType. See
290 * #GtkDialog for more details.
292 * Return value: a new #GtkMessageDialog
295 gtk_message_dialog_new (GtkWindow *parent,
296 GtkDialogFlags flags,
298 GtkButtonsType buttons,
299 const gchar *message_format,
307 g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
308 g_return_val_if_fail (message_format != NULL, NULL);
310 widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
311 "message_type", type,
314 dialog = GTK_DIALOG (widget);
316 if (flags & GTK_DIALOG_NO_SEPARATOR)
318 g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
319 flags &= ~GTK_DIALOG_NO_SEPARATOR;
324 va_start (args, message_format);
325 msg = g_strdup_vprintf (message_format, args);
329 gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
336 gtk_window_set_transient_for (GTK_WINDOW (widget),
337 GTK_WINDOW (parent));
339 if (flags & GTK_DIALOG_MODAL)
340 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
342 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
343 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
345 if (flags & GTK_DIALOG_NO_SEPARATOR)
346 gtk_dialog_set_has_separator (dialog, FALSE);
352 * gtk_message_dialog_new_with_markup:
353 * @parent: transient parent, or %NULL for none
355 * @type: type of message
356 * @buttons: set of buttons to use
357 * @message_format: printf()-style format string, or %NULL
358 * @Varargs: arguments for @message_format
360 * Creates a new message dialog, which is a simple dialog with an icon
361 * indicating the dialog type (error, warning, etc.) and some text which
362 * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
363 * When the user clicks a button a "response" signal is emitted with
364 * response IDs from #GtkResponseType. See #GtkDialog for more details.
366 * Please note that if you have strings in the printf() arguments
367 * passed to this function, you might need to protect against
368 * them being interpreted as markup. You can do this using
369 * g_markup_escape_text() as in the following example:
370 * <informalexample><programlisting>
371 * const gchar *error_text =
372 * "<span weight=\"bold\" size=\"larger\">"
373 * "Could not open document '%s'."
374 * "</span>\n\n"
375 * "You do not have appropriate permission to access this file.";
379 * tmp = g_markup_escape_text (filename, -1);
380 * dialog = gtk_message_dialog_new_with_markup (main_application_window,
381 * GTK_DIALOG_DESTROY_WITH_PARENT,
386 * </programlisting></informalexample>
388 * Return value: a new #GtkMessageDialog
393 gtk_message_dialog_new_with_markup (GtkWindow *parent,
394 GtkDialogFlags flags,
396 GtkButtonsType buttons,
397 const gchar *message_format,
404 g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
405 g_return_val_if_fail (message_format != NULL, NULL);
407 widget = gtk_message_dialog_new (parent, flags, type, buttons, "");
411 va_start (args, message_format);
412 msg = g_strdup_vprintf(message_format, args);
415 gtk_label_set_markup (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
425 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
426 GtkButtonsType buttons)
428 GtkDialog* dialog = GTK_DIALOG (message_dialog);
432 case GTK_BUTTONS_NONE:
437 gtk_dialog_add_button (dialog,
442 case GTK_BUTTONS_CLOSE:
443 gtk_dialog_add_button (dialog,
448 case GTK_BUTTONS_CANCEL:
449 gtk_dialog_add_button (dialog,
451 GTK_RESPONSE_CANCEL);
454 case GTK_BUTTONS_YES_NO:
455 gtk_dialog_add_button (dialog,
458 gtk_dialog_add_button (dialog,
463 case GTK_BUTTONS_OK_CANCEL:
464 gtk_dialog_add_button (dialog,
466 GTK_RESPONSE_CANCEL);
467 gtk_dialog_add_button (dialog,
473 g_warning ("Unknown GtkButtonsType");
477 g_object_notify (G_OBJECT (message_dialog), "buttons");
481 gtk_message_dialog_style_set (GtkWidget *widget,
482 GtkStyle *prev_style)
485 gint border_width = 0;
487 parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
491 gtk_widget_style_get (widget, "message_border",
492 &border_width, NULL);
494 gtk_container_set_border_width (GTK_CONTAINER (parent),
498 if (GTK_WIDGET_CLASS (parent_class)->style_set)
499 (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);