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