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-1999. 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 GtkType dialog_type = 0;
68 static const GtkTypeInfo dialog_info =
71 sizeof (GtkMessageDialog),
72 sizeof (GtkMessageDialogClass),
73 (GtkClassInitFunc) gtk_message_dialog_class_init,
74 (GtkObjectInitFunc) gtk_message_dialog_init,
75 /* reserved_1 */ NULL,
76 /* reserved_2 */ NULL,
77 (GtkClassInitFunc) NULL,
80 dialog_type = gtk_type_unique (GTK_TYPE_DIALOG, &dialog_info);
87 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
89 GtkWidgetClass *widget_class;
90 GObjectClass *gobject_class;
92 widget_class = GTK_WIDGET_CLASS (class);
93 gobject_class = G_OBJECT_CLASS (class);
95 parent_class = g_type_class_peek_parent (class);
97 widget_class->style_set = gtk_message_dialog_style_set;
99 gobject_class->set_property = gtk_message_dialog_set_property;
100 gobject_class->get_property = gtk_message_dialog_get_property;
102 gtk_widget_class_install_style_property (widget_class,
103 g_param_spec_int ("message_border",
104 _("Image/label border"),
105 _("Width of border around the label and image in the message dialog"),
110 g_object_class_install_property (gobject_class,
112 g_param_spec_enum ("message_type",
114 _("The type of message"),
115 GTK_TYPE_MESSAGE_TYPE,
117 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
118 g_object_class_install_property (gobject_class,
120 g_param_spec_enum ("buttons",
121 _("Message Buttons"),
122 _("The buttons shown in the message dialog"),
123 GTK_TYPE_BUTTONS_TYPE,
125 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
130 gtk_message_dialog_init (GtkMessageDialog *dialog)
134 dialog->label = gtk_label_new (NULL);
135 dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
137 gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
138 gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
140 hbox = gtk_hbox_new (FALSE, 6);
142 gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
145 gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
148 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
152 gtk_widget_show_all (hbox);
155 static GtkMessageType
156 gtk_message_dialog_get_message_type (GtkMessageDialog *dialog)
158 const gchar* stock_id = NULL;
160 g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), GTK_MESSAGE_INFO);
161 g_return_val_if_fail (GTK_IS_IMAGE(dialog->image), GTK_MESSAGE_INFO);
163 stock_id = GTK_IMAGE(dialog->image)->data.stock.stock_id;
165 /* Look at the stock id of the image to guess the
166 * GtkMessageType value that was used to choose it
169 if (strcmp (stock_id, GTK_STOCK_DIALOG_INFO) == 0)
170 return GTK_MESSAGE_INFO;
171 else if (strcmp (stock_id, GTK_STOCK_DIALOG_QUESTION) == 0)
172 return GTK_MESSAGE_QUESTION;
173 else if (strcmp (stock_id, GTK_STOCK_DIALOG_WARNING) == 0)
174 return GTK_MESSAGE_WARNING;
175 else if (strcmp (stock_id, GTK_STOCK_DIALOG_ERROR) == 0)
176 return GTK_MESSAGE_ERROR;
179 g_assert_not_reached ();
180 return GTK_MESSAGE_INFO;
185 setup_type (GtkMessageDialog *dialog,
188 const gchar *stock_id = NULL;
193 case GTK_MESSAGE_INFO:
194 stock_id = GTK_STOCK_DIALOG_INFO;
197 case GTK_MESSAGE_QUESTION:
198 stock_id = GTK_STOCK_DIALOG_QUESTION;
201 case GTK_MESSAGE_WARNING:
202 stock_id = GTK_STOCK_DIALOG_WARNING;
205 case GTK_MESSAGE_ERROR:
206 stock_id = GTK_STOCK_DIALOG_ERROR;
210 g_warning ("Unknown GtkMessageType %d", type);
214 if (stock_id == NULL)
215 stock_id = GTK_STOCK_DIALOG_INFO;
217 if (gtk_stock_lookup (stock_id, &item))
219 gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
220 GTK_ICON_SIZE_DIALOG);
222 gtk_window_set_title (GTK_WINDOW (dialog), item.label);
225 g_warning ("Stock dialog ID doesn't exist?");
229 gtk_message_dialog_set_property (GObject *object,
234 GtkMessageDialog *dialog;
236 dialog = GTK_MESSAGE_DIALOG (object);
240 case PROP_MESSAGE_TYPE:
241 setup_type (dialog, g_value_get_enum (value));
244 gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 gtk_message_dialog_get_property (GObject *object,
258 GtkMessageDialog *dialog;
260 dialog = GTK_MESSAGE_DIALOG (object);
264 case PROP_MESSAGE_TYPE:
265 g_value_set_enum (value, gtk_message_dialog_get_message_type (dialog));
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 * gtk_message_dialog_new:
275 * @parent: transient parent, or NULL for none
277 * @type: type of message
278 * @buttons: set of buttons to use
279 * @message_format: printf()-style format string, or NULL
280 * @Varargs: arguments for @message_format
282 * Creates a new message dialog, which is a simple dialog with an icon
283 * indicating the dialog type (error, warning, etc.) and some text the
284 * user may want to see. When the user clicks a button a "response"
285 * signal is emitted with response IDs from #GtkResponseType. See
286 * #GtkDialog for more details.
288 * Return value: a new #GtkMessageDialog
291 gtk_message_dialog_new (GtkWindow *parent,
292 GtkDialogFlags flags,
294 GtkButtonsType buttons,
295 const gchar *message_format,
303 widget = GTK_WIDGET (g_object_new (GTK_TYPE_MESSAGE_DIALOG,
304 "message_type", type,
305 "buttons", buttons, 0));
306 dialog = GTK_DIALOG (widget);
308 if (flags & GTK_DIALOG_NO_SEPARATOR)
310 g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
311 flags &= ~GTK_DIALOG_NO_SEPARATOR;
316 va_start (args, message_format);
317 msg = g_strdup_vprintf(message_format, args);
321 gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
328 gtk_window_set_transient_for (GTK_WINDOW (widget),
329 GTK_WINDOW (parent));
331 if (flags & GTK_DIALOG_MODAL)
332 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
334 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
335 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
337 if (flags & GTK_DIALOG_NO_SEPARATOR)
338 gtk_dialog_set_has_separator (dialog, FALSE);
344 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
345 GtkButtonsType buttons)
347 GtkDialog* dialog = GTK_DIALOG (message_dialog);
351 case GTK_BUTTONS_NONE:
356 gtk_dialog_add_button (dialog,
361 case GTK_BUTTONS_CLOSE:
362 gtk_dialog_add_button (dialog,
367 case GTK_BUTTONS_CANCEL:
368 gtk_dialog_add_button (dialog,
370 GTK_RESPONSE_CANCEL);
373 case GTK_BUTTONS_YES_NO:
374 gtk_dialog_add_button (dialog,
377 gtk_dialog_add_button (dialog,
382 case GTK_BUTTONS_OK_CANCEL:
383 gtk_dialog_add_button (dialog,
385 GTK_RESPONSE_CANCEL);
386 gtk_dialog_add_button (dialog,
392 g_warning ("Unknown GtkButtonsType");
398 gtk_message_dialog_style_set (GtkWidget *widget,
399 GtkStyle *prev_style)
402 gint border_width = 0;
404 parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
408 gtk_widget_style_get (widget, "message_border",
409 &border_width, NULL);
411 gtk_container_set_border_width (GTK_CONTAINER (parent),
415 if (GTK_WIDGET_CLASS (parent_class)->style_set)
416 (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);