]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
Patch from Murray Cumming to add "message_type" and "buttons" CONSTRUCT
[~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-1999.  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 GtkType
62 gtk_message_dialog_get_type (void)
63 {
64   static GtkType dialog_type = 0;
65
66   if (!dialog_type)
67     {
68       static const GtkTypeInfo dialog_info =
69       {
70         "GtkMessageDialog",
71         sizeof (GtkMessageDialog),
72         sizeof (GtkMessageDialogClass),
73         (GtkClassInitFunc) gtk_message_dialog_class_init,
74         (GtkObjectInitFunc) gtk_message_dialog_init,
75         /* reserved_1 */ NULL,
76         /* reserved_2 */ NULL,
77         (GtkClassInitFunc) NULL,
78       };
79
80       dialog_type = gtk_type_unique (GTK_TYPE_DIALOG, &dialog_info);
81     }
82
83   return dialog_type;
84 }
85
86 static void
87 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
88 {
89   GtkWidgetClass *widget_class;
90   GObjectClass *gobject_class;
91
92   widget_class = GTK_WIDGET_CLASS (class);
93   gobject_class = G_OBJECT_CLASS (class);
94
95   parent_class = g_type_class_peek_parent (class);
96   
97   widget_class->style_set = gtk_message_dialog_style_set;
98
99   gobject_class->set_property = gtk_message_dialog_set_property;
100   gobject_class->get_property = gtk_message_dialog_get_property;
101   
102   gtk_widget_class_install_style_property (widget_class,
103                                            g_param_spec_int ("message_border",
104                                                              _("Image/label border"),
105                                                              _("Width of border around the label and image in the message dialog"),
106                                                              0,
107                                                              G_MAXINT,
108                                                              8,
109                                                              G_PARAM_READABLE));
110   g_object_class_install_property (gobject_class,
111                                    PROP_MESSAGE_TYPE,
112                                    g_param_spec_enum ("message_type",
113                                                       _("Message Type"),
114                                                       _("The type of message"),
115                                                       GTK_TYPE_MESSAGE_TYPE,
116                                                       GTK_MESSAGE_INFO,
117                                                       G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
118   g_object_class_install_property (gobject_class,
119                                    PROP_BUTTONS,
120                                    g_param_spec_enum ("buttons",
121                                                       _("Message Buttons"),
122                                                       _("The buttons shown in the message dialog"),
123                                                       GTK_TYPE_BUTTONS_TYPE,
124                                                       GTK_BUTTONS_NONE,
125                                                       G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
126
127 }
128
129 static void
130 gtk_message_dialog_init (GtkMessageDialog *dialog)
131 {
132   GtkWidget *hbox;
133   
134   dialog->label = gtk_label_new (NULL);
135   dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
136   
137   gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
138   gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
139   
140   hbox = gtk_hbox_new (FALSE, 6);
141
142   gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
143                       FALSE, FALSE, 0);
144
145   gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
146                       TRUE, TRUE, 0);
147
148   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
149                       hbox,
150                       FALSE, FALSE, 0);
151
152   gtk_widget_show_all (hbox);
153 }
154
155 static GtkMessageType
156 gtk_message_dialog_get_message_type (GtkMessageDialog *dialog)
157 {
158   const gchar* stock_id = NULL;
159
160   g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (dialog), GTK_MESSAGE_INFO);
161   g_return_val_if_fail (GTK_IS_IMAGE(dialog->image), GTK_MESSAGE_INFO);
162
163   stock_id = GTK_IMAGE(dialog->image)->data.stock.stock_id;
164
165   /* Look at the stock id of the image to guess the
166    * GtkMessageType value that was used to choose it
167    * in setup_type()
168    */
169   if (strcmp (stock_id, GTK_STOCK_DIALOG_INFO) == 0)
170     return GTK_MESSAGE_INFO;
171   else if (strcmp (stock_id, GTK_STOCK_DIALOG_QUESTION) == 0)
172     return GTK_MESSAGE_QUESTION;
173   else if (strcmp (stock_id, GTK_STOCK_DIALOG_WARNING) == 0)
174     return GTK_MESSAGE_WARNING;
175   else if (strcmp (stock_id, GTK_STOCK_DIALOG_ERROR) == 0)
176     return GTK_MESSAGE_ERROR;
177   else
178     {
179       g_assert_not_reached (); 
180       return GTK_MESSAGE_INFO;
181     }
182 }
183
184 static void
185 setup_type (GtkMessageDialog *dialog,
186             GtkMessageType    type)
187 {
188   const gchar *stock_id = NULL;
189   GtkStockItem item;
190   
191   switch (type)
192     {
193     case GTK_MESSAGE_INFO:
194       stock_id = GTK_STOCK_DIALOG_INFO;
195       break;
196
197     case GTK_MESSAGE_QUESTION:
198       stock_id = GTK_STOCK_DIALOG_QUESTION;
199       break;
200
201     case GTK_MESSAGE_WARNING:
202       stock_id = GTK_STOCK_DIALOG_WARNING;
203       break;
204       
205     case GTK_MESSAGE_ERROR:
206       stock_id = GTK_STOCK_DIALOG_ERROR;
207       break;
208       
209     default:
210       g_warning ("Unknown GtkMessageType %d", type);
211       break;
212     }
213
214   if (stock_id == NULL)
215     stock_id = GTK_STOCK_DIALOG_INFO;
216
217   if (gtk_stock_lookup (stock_id, &item))
218     {
219       gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
220                                 GTK_ICON_SIZE_DIALOG);
221       
222       gtk_window_set_title (GTK_WINDOW (dialog), item.label);
223     }
224   else
225     g_warning ("Stock dialog ID doesn't exist?");  
226 }
227
228 static void 
229 gtk_message_dialog_set_property (GObject      *object,
230                                  guint         prop_id,
231                                  const GValue *value,
232                                  GParamSpec   *pspec)
233 {
234   GtkMessageDialog *dialog;
235   
236   dialog = GTK_MESSAGE_DIALOG (object);
237   
238   switch (prop_id)
239     {
240     case PROP_MESSAGE_TYPE:
241       setup_type (dialog, g_value_get_enum (value));
242       break;
243     case PROP_BUTTONS:
244       gtk_message_dialog_add_buttons (dialog, g_value_get_enum (value));
245       break;
246     default:
247       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248       break;
249     }
250 }
251
252 static void 
253 gtk_message_dialog_get_property (GObject     *object,
254                                  guint        prop_id,
255                                  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       g_value_set_enum (value, gtk_message_dialog_get_message_type (dialog));
266     default:
267       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268       break;
269     }
270 }
271
272
273 /**
274  * gtk_message_dialog_new:
275  * @parent: transient parent, or NULL for none 
276  * @flags: flags
277  * @type: type of message
278  * @buttons: set of buttons to use
279  * @message_format: printf()-style format string, or NULL
280  * @Varargs: arguments for @message_format
281  * 
282  * Creates a new message dialog, which is a simple dialog with an icon
283  * indicating the dialog type (error, warning, etc.) and some text the
284  * user may want to see. When the user clicks a button a "response"
285  * signal is emitted with response IDs from #GtkResponseType. See
286  * #GtkDialog for more details.
287  * 
288  * Return value: a new #GtkMessageDialog
289  **/
290 GtkWidget*
291 gtk_message_dialog_new (GtkWindow     *parent,
292                         GtkDialogFlags flags,
293                         GtkMessageType type,
294                         GtkButtonsType buttons,
295                         const gchar   *message_format,
296                         ...)
297 {
298   GtkWidget *widget;
299   GtkDialog *dialog;
300   gchar* msg = 0;
301   va_list args;
302   
303   widget = GTK_WIDGET (g_object_new (GTK_TYPE_MESSAGE_DIALOG,
304                                      "message_type", type,
305                                      "buttons", buttons, 0));
306   dialog = GTK_DIALOG (widget);
307
308   if (flags & GTK_DIALOG_NO_SEPARATOR)
309     {
310       g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
311       flags &= ~GTK_DIALOG_NO_SEPARATOR;
312     }
313
314   if (message_format)
315     {
316       va_start (args, message_format);
317       msg = g_strdup_vprintf(message_format, args);
318       va_end (args);
319       
320       
321       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
322                           msg);
323       
324       g_free (msg);
325     }
326
327   if (parent != NULL)
328     gtk_window_set_transient_for (GTK_WINDOW (widget),
329                                   GTK_WINDOW (parent));
330   
331   if (flags & GTK_DIALOG_MODAL)
332     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
333
334   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
335     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
336
337   if (flags & GTK_DIALOG_NO_SEPARATOR)
338     gtk_dialog_set_has_separator (dialog, FALSE);
339
340   return widget;
341 }
342
343 static void
344 gtk_message_dialog_add_buttons (GtkMessageDialog* message_dialog,
345                                 GtkButtonsType buttons)
346 {
347   GtkDialog* dialog = GTK_DIALOG (message_dialog);
348
349   switch (buttons)
350     {
351     case GTK_BUTTONS_NONE:
352       /* nothing */
353       break;
354
355     case GTK_BUTTONS_OK:
356       gtk_dialog_add_button (dialog,
357                              GTK_STOCK_OK,
358                              GTK_RESPONSE_OK);
359       break;
360
361     case GTK_BUTTONS_CLOSE:
362       gtk_dialog_add_button (dialog,
363                              GTK_STOCK_CLOSE,
364                              GTK_RESPONSE_CLOSE);
365       break;
366
367     case GTK_BUTTONS_CANCEL:
368       gtk_dialog_add_button (dialog,
369                              GTK_STOCK_CANCEL,
370                              GTK_RESPONSE_CANCEL);
371       break;
372
373     case GTK_BUTTONS_YES_NO:
374       gtk_dialog_add_button (dialog,
375                              GTK_STOCK_NO,
376                              GTK_RESPONSE_NO);
377       gtk_dialog_add_button (dialog,
378                              GTK_STOCK_YES,
379                              GTK_RESPONSE_YES);
380       break;
381
382     case GTK_BUTTONS_OK_CANCEL:
383       gtk_dialog_add_button (dialog,
384                              GTK_STOCK_CANCEL,
385                              GTK_RESPONSE_CANCEL);
386       gtk_dialog_add_button (dialog,
387                              GTK_STOCK_OK,
388                              GTK_RESPONSE_OK);
389       break;
390       
391     default:
392       g_warning ("Unknown GtkButtonsType");
393       break;
394     } 
395 }
396
397 static void
398 gtk_message_dialog_style_set (GtkWidget *widget,
399                               GtkStyle  *prev_style)
400 {
401   GtkWidget *parent;
402   gint border_width = 0;
403
404   parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
405
406   if (parent)
407     {
408       gtk_widget_style_get (widget, "message_border",
409                             &border_width, NULL);
410       
411       gtk_container_set_border_width (GTK_CONTAINER (parent),
412                                       border_width);
413     }
414
415   if (GTK_WIDGET_CLASS (parent_class)->style_set)
416     (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);
417 }