]> Pileus Git - ~andy/gtk/blob - gtk/gtkmessagedialog.c
create some stock buttons with the default accel group (create_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
34 static void gtk_message_dialog_class_init (GtkMessageDialogClass *klass);
35 static void gtk_message_dialog_init       (GtkMessageDialog      *dialog);
36
37
38 GtkType
39 gtk_message_dialog_get_type (void)
40 {
41   static GtkType dialog_type = 0;
42
43   if (!dialog_type)
44     {
45       static const GtkTypeInfo dialog_info =
46       {
47         "GtkMessageDialog",
48         sizeof (GtkMessageDialog),
49         sizeof (GtkMessageDialogClass),
50         (GtkClassInitFunc) gtk_message_dialog_class_init,
51         (GtkObjectInitFunc) gtk_message_dialog_init,
52         /* reserved_1 */ NULL,
53         /* reserved_2 */ NULL,
54         (GtkClassInitFunc) NULL,
55       };
56
57       dialog_type = gtk_type_unique (GTK_TYPE_DIALOG, &dialog_info);
58     }
59
60   return dialog_type;
61 }
62
63 static void
64 gtk_message_dialog_class_init (GtkMessageDialogClass *class)
65 {
66 }
67
68 static void
69 gtk_message_dialog_init (GtkMessageDialog *dialog)
70 {
71   GtkWidget *hbox;
72   
73   dialog->label = gtk_label_new (NULL);
74   dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
75   
76   gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);
77
78   hbox = gtk_hbox_new (FALSE, 10);
79
80   gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
81   
82   gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
83                       FALSE, FALSE, 2);
84
85   gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
86                       TRUE, TRUE, 2);
87
88   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
89                       hbox,
90                       FALSE, FALSE, 10);
91
92   gtk_widget_show_all (hbox);
93 }
94
95 static void
96 setup_type(GtkMessageDialog *dialog, GtkMessageType type)
97 {
98   /* Note: this function can be called more than once,
99    * and after showing the dialog, due to object args
100    */
101   
102   const gchar *stock_id = NULL;
103   GtkStockItem item;
104   
105   switch (type)
106     {
107     case GTK_MESSAGE_INFO:
108       stock_id = GTK_STOCK_DIALOG_INFO;
109       break;
110
111     case GTK_MESSAGE_QUESTION:
112       stock_id = GTK_STOCK_DIALOG_QUESTION;
113       break;
114
115     case GTK_MESSAGE_WARNING:
116       stock_id = GTK_STOCK_DIALOG_WARNING;
117       break;
118       
119     case GTK_MESSAGE_ERROR:
120       stock_id = GTK_STOCK_DIALOG_ERROR;
121       break;
122       
123     default:
124       g_warning ("Unknown GtkMessageType %d", type);
125       break;
126     }
127
128   if (stock_id == NULL)
129     stock_id = GTK_STOCK_DIALOG_INFO;
130
131   if (gtk_stock_lookup (stock_id, &item))
132     {
133       gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id,
134                                 GTK_ICON_SIZE_DIALOG);
135       
136       gtk_window_set_title (GTK_WINDOW (dialog), item.label);
137     }
138   else
139     g_warning ("Stock dialog ID doesn't exist?");  
140 }
141
142 /**
143  * gtk_message_dialog_new:
144  * @parent: transient parent, or NULL for none 
145  * @flags: flags
146  * @type: type of message
147  * @buttons: set of buttons to use
148  * @message_format: printf()-style format string, or NULL
149  * @Varargs: arguments for @message_format
150  * 
151  * Creates a new message dialog, which is a simple dialog with an icon
152  * indicating the dialog type (error, warning, etc.) and some text the
153  * user may want to see. If the button set you select with the @buttons
154  * argument has positive buttons (OK, Yes) they will result in a response ID
155  * of GTK_RESPONSE_ACCEPT. If it has negative buttons (Cancel, No) they will
156  * result in a response ID of GTK_RESPONSE_REJECT. See #GtkDialog for more
157  * details.
158  * 
159  * Return value: a new #GtkMessageDialog
160  **/
161 GtkWidget*
162 gtk_message_dialog_new (GtkWindow     *parent,
163                         GtkDialogFlags flags,
164                         GtkMessageType type,
165                         GtkButtonsType buttons,
166                         const gchar   *message_format,
167                         ...)
168 {
169   GtkWidget *widget;
170   GtkDialog *dialog;
171   gchar* msg;
172   va_list args;
173   
174   widget = GTK_WIDGET (gtk_type_new (GTK_TYPE_MESSAGE_DIALOG));
175   dialog = GTK_DIALOG (widget);
176
177   if (message_format)
178     {
179       va_start (args, message_format);
180       msg = g_strdup_vprintf(message_format, args);
181       va_end (args);
182       
183       
184       gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
185                           msg);
186       
187       g_free (msg);
188     }
189
190   if (parent != NULL)
191     gtk_window_set_transient_for (GTK_WINDOW (widget),
192                                   GTK_WINDOW (parent));
193   
194
195   if (flags & GTK_DIALOG_MODAL)
196     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
197
198   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
199     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
200   
201   setup_type (GTK_MESSAGE_DIALOG (dialog), type);
202   
203   switch (buttons)
204     {
205     case GTK_BUTTONS_NONE:
206       /* nothing */
207       break;
208
209     case GTK_BUTTONS_OK:
210       gtk_dialog_add_button (dialog,
211                              GTK_STOCK_BUTTON_OK,
212                              GTK_RESPONSE_ACCEPT);
213       break;
214
215     case GTK_BUTTONS_CLOSE:
216       gtk_dialog_add_button (dialog,
217                              GTK_STOCK_BUTTON_CLOSE,
218                              GTK_RESPONSE_ACCEPT);
219       break;
220
221     case GTK_BUTTONS_CANCEL:
222       gtk_dialog_add_button (dialog,
223                              GTK_STOCK_BUTTON_CANCEL,
224                              GTK_RESPONSE_REJECT);
225       break;
226
227     case GTK_BUTTONS_YES_NO:
228       gtk_dialog_add_button (dialog,
229                              GTK_STOCK_BUTTON_YES,
230                              GTK_RESPONSE_ACCEPT);
231       gtk_dialog_add_button (dialog,
232                              GTK_STOCK_BUTTON_NO,
233                              GTK_RESPONSE_REJECT);
234       break;
235
236     case GTK_BUTTONS_OK_CANCEL:
237       gtk_dialog_add_button (dialog,
238                              GTK_STOCK_BUTTON_OK,
239                              GTK_RESPONSE_ACCEPT);
240       gtk_dialog_add_button (dialog,
241                              GTK_STOCK_BUTTON_CANCEL,
242                              GTK_RESPONSE_REJECT);
243       break;
244       
245     default:
246       g_warning ("Unknown GtkButtonsType");
247       break;
248     }
249
250   return widget;
251 }