]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
Warn
[~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 (flags & GTK_DIALOG_NO_SEPARATOR)
195     {
196       g_warning ("The GTK_DIALOG_NO_SEPARATOR flag cannot be used for GtkMessageDialog");
197       flags &= ~GTK_DIALOG_NO_SEPARATOR;
198     }
199
200   if (message_format)
201     {
202       va_start (args, message_format);
203       msg = g_strdup_vprintf(message_format, args);
204       va_end (args);
205       
206       
207       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
208                           msg);
209       
210       g_free (msg);
211     }
212
213   if (parent != NULL)
214     gtk_window_set_transient_for (GTK_WINDOW (widget),
215                                   GTK_WINDOW (parent));
216   
217
218   if (flags & GTK_DIALOG_MODAL)
219     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
220
221   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
222     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
223
224   if (flags & GTK_DIALOG_NO_SEPARATOR)
225     gtk_dialog_set_has_separator (dialog, FALSE);
226   
227   setup_type (GTK_MESSAGE_DIALOG (dialog), type);
228   
229   switch (buttons)
230     {
231     case GTK_BUTTONS_NONE:
232       /* nothing */
233       break;
234
235     case GTK_BUTTONS_OK:
236       gtk_dialog_add_button (dialog,
237                              GTK_STOCK_OK,
238                              GTK_RESPONSE_OK);
239       break;
240
241     case GTK_BUTTONS_CLOSE:
242       gtk_dialog_add_button (dialog,
243                              GTK_STOCK_CLOSE,
244                              GTK_RESPONSE_CLOSE);
245       break;
246
247     case GTK_BUTTONS_CANCEL:
248       gtk_dialog_add_button (dialog,
249                              GTK_STOCK_CANCEL,
250                              GTK_RESPONSE_CANCEL);
251       break;
252
253     case GTK_BUTTONS_YES_NO:
254       gtk_dialog_add_button (dialog,
255                              GTK_STOCK_NO,
256                              GTK_RESPONSE_NO);
257       gtk_dialog_add_button (dialog,
258                              GTK_STOCK_YES,
259                              GTK_RESPONSE_YES);
260       break;
261
262     case GTK_BUTTONS_OK_CANCEL:
263       gtk_dialog_add_button (dialog,
264                              GTK_STOCK_CANCEL,
265                              GTK_RESPONSE_CANCEL);
266       gtk_dialog_add_button (dialog,
267                              GTK_STOCK_OK,
268                              GTK_RESPONSE_OK);
269       break;
270       
271     default:
272       g_warning ("Unknown GtkButtonsType");
273       break;
274     }
275
276   return widget;
277 }
278
279 static void
280 gtk_message_dialog_style_set (GtkWidget *widget,
281                               GtkStyle  *prev_style)
282 {
283   GtkWidget *parent;
284   gint border_width = 0;
285
286   parent = GTK_WIDGET (GTK_MESSAGE_DIALOG (widget)->image->parent);
287
288   if (parent)
289     {
290       gtk_widget_style_get (widget, "message_border",
291                             &border_width, NULL);
292       
293       gtk_container_set_border_width (GTK_CONTAINER (parent),
294                                       border_width);
295     }
296
297   if (GTK_WIDGET_CLASS (parent_class)->style_set)
298     (GTK_WIDGET_CLASS (parent_class)->style_set) (widget, prev_style);
299 }