]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
Fix #137520.
[~andy/gtk] / gtk / gtkmessagedialog.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <config.h>
28 #include "gtkmessagedialog.h"
29 #include "gtklabel.h"
30 #include "gtkhbox.h"
31 #include "gtkimage.h"
32 #include "gtkstock.h"
33 #include "gtkiconfactory.h"
34 #include "gtkintl.h"
35 #include <string.h>
36
37 static void gtk_message_dialog_class_init (GtkMessageDialogClass *klass);
38 static void gtk_message_dialog_init       (GtkMessageDialog      *dialog);
39 static void gtk_message_dialog_style_set  (GtkWidget             *widget,
40                                            GtkStyle              *prev_style);
41
42 static void gtk_message_dialog_set_property (GObject          *object,
43                                              guint             prop_id,
44                                              const GValue     *value,
45                                              GParamSpec       *pspec);
46 static void gtk_message_dialog_get_property (GObject          *object,
47                                              guint             prop_id,
48                                              GValue           *value,
49                                              GParamSpec       *pspec);
50 static void gtk_message_dialog_add_buttons  (GtkMessageDialog *message_dialog,
51                                              GtkButtonsType    buttons);
52
53
54 enum {
55   PROP_0,
56   PROP_MESSAGE_TYPE,
57   PROP_BUTTONS
58 };
59
60 static gpointer parent_class;
61
62 GType
63 gtk_message_dialog_get_type (void)
64 {
65   static GType dialog_type = 0;
66
67   if (!dialog_type)
68     {
69       static const GTypeInfo dialog_info =
70       {
71         sizeof (GtkMessageDialogClass),
72         NULL,           /* base_init */
73         NULL,           /* base_finalize */
74         (GClassInitFunc) gtk_message_dialog_class_init,
75         NULL,           /* class_finalize */
76         NULL,           /* class_data */
77         sizeof (GtkMessageDialog),
78         0,              /* n_preallocs */
79         (GInstanceInitFunc) gtk_message_dialog_init,
80       };
81
82       dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkMessageDialog",
83                                             &dialog_info, 0);
84     }
85
86   return dialog_type;
87 }
88
89 static void
90 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
91 {
92   GtkWidgetClass *widget_class;
93   GObjectClass *gobject_class;
94
95   widget_class = GTK_WIDGET_CLASS (class);
96   gobject_class = G_OBJECT_CLASS (class);
97
98   parent_class = g_type_class_peek_parent (class);
99   
100   widget_class->style_set = gtk_message_dialog_style_set;
101
102   gobject_class->set_property = gtk_message_dialog_set_property;
103   gobject_class->get_property = gtk_message_dialog_get_property;
104   
105   gtk_widget_class_install_style_property (widget_class,
106                                            g_param_spec_int ("message_border",
107                                                              P_("Image/label border"),
108                                                              P_("Width of border around the label and image in the message dialog"),
109                                                              0,
110                                                              G_MAXINT,
111                                                              8,
112                                                              G_PARAM_READABLE));
113   /**
114    * GtkMessageDialog::use_separator
115    *
116    * Whether to draw a separator line between the message label and the buttons
117    * in the dialog.
118    *
119    * Since: 2.4
120    */
121   gtk_widget_class_install_style_property (widget_class,
122                                            g_param_spec_boolean ("use_separator",
123                                                                  P_("Use separator"),
124                                                                  P_("Whether to put a separator between the message dialog's text and the buttons"),
125                                                                  FALSE,
126                                                                  G_PARAM_READABLE));
127   g_object_class_install_property (gobject_class,
128                                    PROP_MESSAGE_TYPE,
129                                    g_param_spec_enum ("message_type",
130                                                       P_("Message Type"),
131                                                       P_("The type of message"),
132                                                       GTK_TYPE_MESSAGE_TYPE,
133                                                       GTK_MESSAGE_INFO,
134                                                       G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
135   g_object_class_install_property (gobject_class,
136                                    PROP_BUTTONS,
137                                    g_param_spec_enum ("buttons",
138                                                       P_("Message Buttons"),
139                                                       P_("The buttons shown in the message dialog"),
140                                                       GTK_TYPE_BUTTONS_TYPE,
141                                                       GTK_BUTTONS_NONE,
142                                                       G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
143
144 }
145
146 static void
147 gtk_message_dialog_init (GtkMessageDialog *dialog)
148 {
149   GtkWidget *hbox;
150
151   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
152   
153   dialog->label = gtk_label_new (NULL);
154   dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
155   gtk_misc_set_alignment (GTK_MISC (dialog->image), 0.5, 0.0);
156   
157   gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
158   gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
159   
160   hbox = gtk_hbox_new (FALSE, 6);
161
162   gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
163                       FALSE, FALSE, 0);
164
165   gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
166                       TRUE, TRUE, 0);
167
168   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
169                       hbox,
170                       FALSE, FALSE, 0);
171
172   gtk_widget_show_all (hbox);
173
174   _gtk_dialog_set_ignore_separator (GTK_DIALOG (dialog), TRUE);
175 }
176
177 static GtkMessageType
178 gtk_message_dialog_get_message_type (GtkMessageDialog *dialog)
179 {
180   const gchar* stock_id = NULL;
181
182   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), GTK_MESSAGE_INFO);
183   g_return_val_if_fail (GTK_IS_IMAGE(dialog->image), GTK_MESSAGE_INFO);
184
185   stock_id = GTK_IMAGE(dialog->image)->data.stock.stock_id;
186
187   /* Look at the stock id of the image to guess the
188    * GtkMessageType value that was used to choose it
189    * in setup_type()
190    */
191   if (strcmp (stock_id, GTK_STOCK_DIALOG_INFO) == 0)
192     return GTK_MESSAGE_INFO;
193   else if (strcmp (stock_id, GTK_STOCK_DIALOG_QUESTION) == 0)
194     return GTK_MESSAGE_QUESTION;
195   else if (strcmp (stock_id, GTK_STOCK_DIALOG_WARNING) == 0)
196     return GTK_MESSAGE_WARNING;
197   else if (strcmp (stock_id, GTK_STOCK_DIALOG_ERROR) == 0)
198     return GTK_MESSAGE_ERROR;
199   else
200     {
201       g_assert_not_reached (); 
202       return GTK_MESSAGE_INFO;
203     }
204 }
205
206 static void
207 setup_type (GtkMessageDialog *dialog,
208             GtkMessageType    type)
209 {
210   const gchar *stock_id = NULL;
211   GtkStockItem item;
212   
213   switch (type)
214     {
215     case GTK_MESSAGE_INFO:
216       stock_id = GTK_STOCK_DIALOG_INFO;
217       break;
218
219     case GTK_MESSAGE_QUESTION:
220       stock_id = GTK_STOCK_DIALOG_QUESTION;
221       break;
222
223     case GTK_MESSAGE_WARNING:
224       stock_id = GTK_STOCK_DIALOG_WARNING;
225       break;
226       
227     case GTK_MESSAGE_ERROR:
228       stock_id = GTK_STOCK_DIALOG_ERROR;
229       break;
230       
231     default:
232       g_warning ("Unknown GtkMessageType %d", type);
233       break;
234     }
235
236   if (stock_id == NULL)
237     stock_id = GTK_STOCK_DIALOG_INFO;
238
239   if (gtk_stock_lookup (stock_id, &item))
240     {
241       gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
242                                 GTK_ICON_SIZE_DIALOG);
243       
244       gtk_window_set_title (GTK_WINDOW (dialog), item.label);
245     }
246   else
247     g_warning ("Stock dialog ID doesn't exist?");  
248 }
249
250 static void 
251 gtk_message_dialog_set_property (GObject      *object,
252                                  guint         prop_id,
253                                  const GValue *value,
254                                  GParamSpec   *pspec)
255 {
256   GtkMessageDialog *dialog;
257   
258   dialog = GTK_MESSAGE_DIALOG (object);
259   
260   switch (prop_id)
261     {
262     case PROP_MESSAGE_TYPE:
263       setup_type (dialog, g_value_get_enum (value));
264       break;
265     case PROP_BUTTONS:
266       gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
267       break;
268     default:
269       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270       break;
271     }
272 }
273
274 static void 
275 gtk_message_dialog_get_property (GObject     *object,
276                                  guint        prop_id,
277                                  GValue      *value,
278                                  GParamSpec  *pspec)
279 {
280   GtkMessageDialog *dialog;
281   
282   dialog = GTK_MESSAGE_DIALOG (object);
283   
284   switch (prop_id)
285     {
286     case PROP_MESSAGE_TYPE:
287       g_value_set_enum (value, gtk_message_dialog_get_message_type (dialog));
288       break;
289     default:
290       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291       break;
292     }
293 }
294
295
296 /**
297  * gtk_message_dialog_new:
298  * @parent: transient parent, or NULL for none 
299  * @flags: flags
300  * @type: type of message
301  * @buttons: set of buttons to use
302  * @message_format: printf()-style format string, or NULL
303  * @Varargs: arguments for @message_format
304  * 
305  * Creates a new message dialog, which is a simple dialog with an icon
306  * indicating the dialog type (error, warning, etc.) and some text the
307  * user may want to see. When the user clicks a button a "response"
308  * signal is emitted with response IDs from #GtkResponseType. See
309  * #GtkDialog for more details.
310  * 
311  * Return value: a new #GtkMessageDialog
312  **/
313 GtkWidget*
314 gtk_message_dialog_new (GtkWindow     *parent,
315                         GtkDialogFlags flags,
316                         GtkMessageType type,
317                         GtkButtonsType buttons,
318                         const gchar   *message_format,
319                         ...)
320 {
321   GtkWidget *widget;
322   GtkDialog *dialog;
323   gchar* msg = 0;
324   va_list args;
325
326   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
327
328   widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
329                          "message_type", type,
330                          "buttons", buttons,
331                          NULL);
332   dialog = GTK_DIALOG (widget);
333
334   if (flags & GTK_DIALOG_NO_SEPARATOR)
335     {
336       g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
337       flags &= ~GTK_DIALOG_NO_SEPARATOR;
338     }
339
340   if (message_format)
341     {
342       va_start (args, message_format);
343       msg = g_strdup_vprintf (message_format, args);
344       va_end (args);
345       
346       
347       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
348                           msg);
349       
350       g_free (msg);
351     }
352
353   if (parent != NULL)
354     gtk_window_set_transient_for (GTK_WINDOW (widget),
355                                   GTK_WINDOW (parent));
356   
357   if (flags & GTK_DIALOG_MODAL)
358     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
359
360   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
361     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
362
363   if (flags & GTK_DIALOG_NO_SEPARATOR)
364     gtk_dialog_set_has_separator (dialog, FALSE);
365
366   return widget;
367 }
368
369 /**
370  * gtk_message_dialog_new_with_markup:
371  * @parent: transient parent, or %NULL for none 
372  * @flags: flags
373  * @type: type of message
374  * @buttons: set of buttons to use
375  * @message_format: printf()-style format string, or %NULL
376  * @Varargs: arguments for @message_format
377  * 
378  * Creates a new message dialog, which is a simple dialog with an icon
379  * indicating the dialog type (error, warning, etc.) and some text which
380  * is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
381  * When the user clicks a button a "response" signal is emitted with
382  * response IDs from #GtkResponseType. See #GtkDialog for more details.
383  *
384  * Special XML characters in the printf() arguments passed to this
385  * function will automatically be escaped as necessary.
386  * (See g_markup_printf_escaped() for how this is implemented.)
387  * Usually this is what you want, but if you have an existing
388  * Pango markup string that you want to use literally as the
389  * label, then you need to use gtk_message_dialog_set_markup()
390  * instead, since you can't pass the markup string either
391  * as the format (it might contain '%' characters) or as a string
392  * argument.
393  *
394  * <informalexample><programlisting>
395  *  GtkWidget *dialog;
396  *  dialog = gtk_message_dialog_new (main_application_window,
397  *                                   GTK_DIALOG_DESTROY_WITH_PARENT,
398  *                                   GTK_MESSAGE_ERROR,
399  *                                   GTK_BUTTON_CLOSE,
400  *                                   NULL);
401  *  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
402  *                                 markup);
403  * </programlisting></informalexample>
404  * 
405  * Return value: a new #GtkMessageDialog
406  *
407  * Since: 2.4
408  **/
409 GtkWidget*
410 gtk_message_dialog_new_with_markup (GtkWindow     *parent,
411                                     GtkDialogFlags flags,
412                                     GtkMessageType type,
413                                     GtkButtonsType buttons,
414                                     const gchar   *message_format,
415                                     ...)
416 {
417   GtkWidget *widget;
418   gchar* msg = 0;
419   va_list args;
420
421   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
422
423   widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL);
424
425   if (message_format)
426     {
427       va_start (args, message_format);
428       msg = g_markup_vprintf_escaped (message_format, args);
429       va_end (args);
430
431       gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), msg);
432
433       g_free (msg);
434     }
435
436   return widget;
437 }
438
439 /**
440  * gtk_message_dialog_set_markup:
441  * @message_dialog: a #GtkMessageDialog
442  * @str: markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
443  * 
444  * Sets the text of the message dialog to be @str, which is marked
445  * up with the <link linkend="PangoMarkupFormat">Pango text markup
446  * language</link>.
447  *
448  * Since: 2.4
449  **/
450 void
451 gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog,
452                                const gchar      *str)
453 {
454   g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog));
455   
456   gtk_label_set_markup (GTK_LABEL (message_dialog->label), str);
457 }
458
459 static void
460 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
461                                 GtkButtonsType buttons)
462 {
463   GtkDialog* dialog = GTK_DIALOG (message_dialog);
464
465   switch (buttons)
466     {
467     case GTK_BUTTONS_NONE:
468       /* nothing */
469       break;
470
471     case GTK_BUTTONS_OK:
472       gtk_dialog_add_button (dialog,
473                              GTK_STOCK_OK,
474                              GTK_RESPONSE_OK);
475       break;
476
477     case GTK_BUTTONS_CLOSE:
478       gtk_dialog_add_button (dialog,
479                              GTK_STOCK_CLOSE,
480                              GTK_RESPONSE_CLOSE);
481       break;
482
483     case GTK_BUTTONS_CANCEL:
484       gtk_dialog_add_button (dialog,
485                              GTK_STOCK_CANCEL,
486                              GTK_RESPONSE_CANCEL);
487       break;
488
489     case GTK_BUTTONS_YES_NO:
490       gtk_dialog_add_button (dialog,
491                              GTK_STOCK_NO,
492                              GTK_RESPONSE_NO);
493       gtk_dialog_add_button (dialog,
494                              GTK_STOCK_YES,
495                              GTK_RESPONSE_YES);
496       break;
497
498     case GTK_BUTTONS_OK_CANCEL:
499       gtk_dialog_add_button (dialog,
500                              GTK_STOCK_CANCEL,
501                              GTK_RESPONSE_CANCEL);
502       gtk_dialog_add_button (dialog,
503                              GTK_STOCK_OK,
504                              GTK_RESPONSE_OK);
505       break;
506       
507     default:
508       g_warning ("Unknown GtkButtonsType");
509       break;
510     } 
511
512   g_object_notify (G_OBJECT (message_dialog), "buttons");
513 }
514
515 static void
516 gtk_message_dialog_style_set (GtkWidget *widget,
517                               GtkStyle  *prev_style)
518 {
519   GtkWidget *parent;
520   gint border_width = 0;
521   gboolean use_separator;
522
523   parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
524
525   if (parent)
526     {
527       gtk_widget_style_get (widget, "message_border",
528                             &border_width, NULL);
529       
530       gtk_container_set_border_width (GTK_CONTAINER (parent),
531                                       border_width);
532     }
533
534   gtk_widget_style_get (widget,
535                         "use_separator", &use_separator,
536                         NULL);
537   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), FALSE);
538   gtk_dialog_set_has_separator (GTK_DIALOG (widget), use_separator);
539   _gtk_dialog_set_ignore_separator (GTK_DIALOG (widget), TRUE);
540
541   if (GTK_WIDGET_CLASS (parent_class)->style_set)
542     (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);
543 }