]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
get sizes an icon set can render without falling back to missing image
[~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
35 static void gtk_message_dialog_class_init (GtkMessageDialogClass *klass);
36 static void gtk_message_dialog_init       (GtkMessageDialog      *dialog);
37 static void gtk_message_dialog_style_set  (GtkWidget             *widget,
38                                            GtkStyle              *prev_style);
39
40 static gpointer parent_class;
41
42 GtkType
43 gtk_message_dialog_get_type (void)
44 {
45   static GtkType dialog_type = 0;
46
47   if (!dialog_type)
48     {
49       static const GtkTypeInfo dialog_info =
50       {
51         "GtkMessageDialog",
52         sizeof (GtkMessageDialog),
53         sizeof (GtkMessageDialogClass),
54         (GtkClassInitFunc) gtk_message_dialog_class_init,
55         (GtkObjectInitFunc) gtk_message_dialog_init,
56         /* reserved_1 */ NULL,
57         /* reserved_2 */ NULL,
58         (GtkClassInitFunc) NULL,
59       };
60
61       dialog_type = gtk_type_unique (GTK_TYPE_DIALOG, &dialog_info);
62     }
63
64   return dialog_type;
65 }
66
67 static void
68 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
69 {
70   GtkWidgetClass *widget_class;
71
72   widget_class = GTK_WIDGET_CLASS (class);
73
74   parent_class = g_type_class_peek_parent (class);
75   
76   widget_class->style_set = gtk_message_dialog_style_set;
77   
78   gtk_widget_class_install_style_property (widget_class,
79                                            g_param_spec_int ("message_border",
80                                                              _("Image/label border"),
81                                                              _("Width of border around the label and image in the message dialog"),
82                                                              0,
83                                                              G_MAXINT,
84                                                              8,
85                                                              G_PARAM_READABLE));
86 }
87
88 static void
89 gtk_message_dialog_init (GtkMessageDialog *dialog)
90 {
91   GtkWidget *hbox;
92   
93   dialog->label = gtk_label_new (NULL);
94   dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
95   
96   gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
97   gtk_label_set_selectable (GTK_LABEL (dialog->label), TRUE);
98   
99   hbox = gtk_hbox_new (FALSE, 6);
100
101   gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
102                       FALSE, FALSE, 0);
103
104   gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
105                       TRUE, TRUE, 0);
106
107   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
108                       hbox,
109                       FALSE, FALSE, 0);
110
111   gtk_widget_show_all (hbox);
112 }
113
114 static void
115 setup_type(GtkMessageDialog *dialog, GtkMessageType type)
116 {
117   /* Note: this function can be called more than once,
118    * and after showing the dialog, due to object args
119    */
120   
121   const gchar *stock_id = NULL;
122   GtkStockItem item;
123   
124   switch (type)
125     {
126     case GTK_MESSAGE_INFO:
127       stock_id = GTK_STOCK_DIALOG_INFO;
128       break;
129
130     case GTK_MESSAGE_QUESTION:
131       stock_id = GTK_STOCK_DIALOG_QUESTION;
132       break;
133
134     case GTK_MESSAGE_WARNING:
135       stock_id = GTK_STOCK_DIALOG_WARNING;
136       break;
137       
138     case GTK_MESSAGE_ERROR:
139       stock_id = GTK_STOCK_DIALOG_ERROR;
140       break;
141       
142     default:
143       g_warning ("Unknown GtkMessageType %d", type);
144       break;
145     }
146
147   if (stock_id == NULL)
148     stock_id = GTK_STOCK_DIALOG_INFO;
149
150   if (gtk_stock_lookup (stock_id, &item))
151     {
152       gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
153                                 GTK_ICON_SIZE_DIALOG);
154       
155       gtk_window_set_title (GTK_WINDOW (dialog), item.label);
156     }
157   else
158     g_warning ("Stock dialog ID doesn't exist?");  
159 }
160
161 /**
162  * gtk_message_dialog_new:
163  * @parent: transient parent, or NULL for none 
164  * @flags: flags
165  * @type: type of message
166  * @buttons: set of buttons to use
167  * @message_format: printf()-style format string, or NULL
168  * @Varargs: arguments for @message_format
169  * 
170  * Creates a new message dialog, which is a simple dialog with an icon
171  * indicating the dialog type (error, warning, etc.) and some text the
172  * user may want to see. When the user clicks a button a "response"
173  * signal is emitted with response IDs from #GtkResponseType. See
174  * #GtkDialog for more details.
175  * 
176  * Return value: a new #GtkMessageDialog
177  **/
178 GtkWidget*
179 gtk_message_dialog_new (GtkWindow     *parent,
180                         GtkDialogFlags flags,
181                         GtkMessageType type,
182                         GtkButtonsType buttons,
183                         const gchar   *message_format,
184                         ...)
185 {
186   GtkWidget *widget;
187   GtkDialog *dialog;
188   gchar* msg;
189   va_list args;
190   
191   widget = GTK_WIDGET (gtk_type_new (GTK_TYPE_MESSAGE_DIALOG));
192   dialog = GTK_DIALOG (widget);
193
194   if (message_format)
195     {
196       va_start (args, message_format);
197       msg = g_strdup_vprintf(message_format, args);
198       va_end (args);
199       
200       
201       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
202                           msg);
203       
204       g_free (msg);
205     }
206
207   if (parent != NULL)
208     gtk_window_set_transient_for (GTK_WINDOW (widget),
209                                   GTK_WINDOW (parent));
210   
211
212   if (flags & GTK_DIALOG_MODAL)
213     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
214
215   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
216     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
217   
218   setup_type (GTK_MESSAGE_DIALOG (dialog), type);
219   
220   switch (buttons)
221     {
222     case GTK_BUTTONS_NONE:
223       /* nothing */
224       break;
225
226     case GTK_BUTTONS_OK:
227       gtk_dialog_add_button (dialog,
228                              GTK_STOCK_OK,
229                              GTK_RESPONSE_OK);
230       break;
231
232     case GTK_BUTTONS_CLOSE:
233       gtk_dialog_add_button (dialog,
234                              GTK_STOCK_CLOSE,
235                              GTK_RESPONSE_CLOSE);
236       break;
237
238     case GTK_BUTTONS_CANCEL:
239       gtk_dialog_add_button (dialog,
240                              GTK_STOCK_CANCEL,
241                              GTK_RESPONSE_CANCEL);
242       break;
243
244     case GTK_BUTTONS_YES_NO:
245       gtk_dialog_add_button (dialog,
246                              GTK_STOCK_YES,
247                              GTK_RESPONSE_YES);
248       gtk_dialog_add_button (dialog,
249                              GTK_STOCK_NO,
250                              GTK_RESPONSE_NO);
251       break;
252
253     case GTK_BUTTONS_OK_CANCEL:
254       gtk_dialog_add_button (dialog,
255                              GTK_STOCK_OK,
256                              GTK_RESPONSE_OK);
257       gtk_dialog_add_button (dialog,
258                              GTK_STOCK_CANCEL,
259                              GTK_RESPONSE_CANCEL);
260       break;
261       
262     default:
263       g_warning ("Unknown GtkButtonsType");
264       break;
265     }
266
267   return widget;
268 }
269
270 static void
271 gtk_message_dialog_style_set (GtkWidget *widget,
272                               GtkStyle  *prev_style)
273 {
274   GtkWidget *parent;
275   gint border_width = 0;
276
277   parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
278
279   if (parent)
280     {
281       gtk_widget_style_get (widget, "message_border",
282                             &border_width, NULL);
283       
284       gtk_container_set_border_width (GTK_CONTAINER (parent),
285                                       border_width);
286     }
287
288   if (GTK_WIDGET_CLASS (parent_class)->style_set)
289     (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);
290 }