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