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