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