Add some comments about the types of alert boxes you get with the
[obnox/wireshark/wip.git] / gtk / simple_dialog.c
1 /* simple_dialog.c
2  * Simple message dialog box routines.
3  *
4  * $Id: simple_dialog.c,v 1.24 2004/02/04 01:10:37 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30
31 #include <stdio.h>
32
33 #ifdef NEED_SNPRINTF_H
34 # include "snprintf.h"
35 #endif
36
37 #include "gtkglobals.h"
38 #include "simple_dialog.h"
39 #include "dlg_utils.h"
40 #include "compat_macros.h"
41
42 #include "image/stock_dialog_error_48.xpm"
43 #include "image/stock_dialog_info_48.xpm"
44 #include "image/stock_dialog_question_48.xpm"
45 #include "image/stock_dialog_warning_48.xpm"
46
47 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
48
49 #define CALLBACK_FCT_KEY    "ESD_Callback_Fct"
50 #define CALLBACK_BTN_KEY    "ESD_Callback_Btn"
51 #define CALLBACK_DATA_KEY   "ESD_Callback_Data"
52
53 /* Simple dialog function - Displays a dialog box with the supplied message
54  * text.
55  *
56  * Args:
57  * type       : One of ESD_TYPE_*.
58  * btn_mask   : The value passed in determines which buttons are displayed.
59  * msg_format : Sprintf-style format of the text displayed in the dialog.
60  * ...        : Argument list for msg_format
61  *
62  */
63
64 gpointer
65 simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...) {
66   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
67               *bbox, *ok_bt, *bt;
68   GdkPixmap   *pixmap;
69   GdkBitmap   *mask;
70   GtkStyle    *style;
71   GdkColormap *cmap;
72   va_list      ap;
73   gchar        message[2048];
74   gchar      **icon;
75
76   /* Main window */
77   switch (type & ~ESD_TYPE_MODAL) {
78   case ESD_TYPE_WARN :
79     icon = stock_dialog_warning_48_xpm;
80     win = dlg_window_new("Ethereal: Warning");
81     break;
82   case ESD_TYPE_QUESTION:
83     icon = stock_dialog_question_48_xpm;
84     win = dlg_window_new("Ethereal: Question");
85     break;
86   case ESD_TYPE_ERROR:
87     icon = stock_dialog_error_48_xpm;
88     win = dlg_window_new("Ethereal: Error");
89     break;
90   case ESD_TYPE_INFO :
91   default :
92     icon = stock_dialog_info_48_xpm;
93     win = dlg_window_new("Ethereal: Information");
94     break;
95   }
96
97 #if GTK_MAJOR_VERSION >= 2
98   /* the GNOME HIG suggest to keep the title empty for simple dialogs */
99   /* at least on win32 systems, this isn't possible, so use the programs name */
100   gtk_window_set_title(GTK_WINDOW(win), "Ethereal");
101 #endif
102
103   if (type & ESD_TYPE_MODAL)
104     gtk_window_set_modal(GTK_WINDOW(win), TRUE);
105   gtk_container_border_width(GTK_CONTAINER(win), 6);
106
107   /* Container for our rows */
108   main_vb = gtk_vbox_new(FALSE, 12);
109   gtk_container_add(GTK_CONTAINER(win), main_vb);
110   gtk_widget_show(main_vb);
111
112   /* Top row: Icon and message text */
113   top_hb = gtk_hbox_new(FALSE, 12);
114   gtk_container_border_width(GTK_CONTAINER(main_vb), 6);
115   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
116   gtk_widget_show(top_hb);
117
118   style = gtk_widget_get_style(win);
119   cmap  = gdk_colormap_get_system();
120   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
121     &style->bg[GTK_STATE_NORMAL], icon);
122   type_pm = gtk_pixmap_new(pixmap, mask);
123   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
124   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
125   gtk_widget_show(type_pm);
126
127   /* Load our vararg list into the message string */
128   va_start(ap, msg_format);
129   vsnprintf(message, sizeof(message), msg_format, ap);
130   va_end(ap);
131
132   msg_label = gtk_label_new(message);
133
134 #if GTK_MAJOR_VERSION >= 2
135   gtk_label_set_markup(GTK_LABEL(msg_label), message);
136   gtk_label_set_selectable(GTK_LABEL(msg_label), TRUE);
137 #endif
138
139   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
140   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
141   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
142   gtk_widget_show(msg_label);
143
144   /* Button row */
145   switch(btn_mask) {
146   case(ESD_BTN_OK):
147     bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
148     break;
149   case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
150     bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
151     break;
152   case(ESD_BTNS_YES_NO_CANCEL):
153     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
154     break;
155   default:
156     g_assert_not_reached();
157     bbox = NULL;
158     break;
159   }
160   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
161   gtk_widget_show(bbox);
162
163   ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
164   if(ok_bt) {
165       OBJECT_SET_DATA(ok_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
166       SIGNAL_CONNECT(ok_bt, "clicked", simple_dialog_cancel_cb, win);
167   }
168
169   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
170   if(bt) {
171       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
172       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
173   }
174
175   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
176   if(bt) {
177       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
178       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
179   }
180
181   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
182   if(bt) {
183       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
184       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
185   }
186
187   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
188   if(bt) {
189       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
190       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
191     /* Catch the "key_press_event" signal in the window, so that we can catch
192        the ESC key being pressed and act as if the "OK" button had
193        been selected. */
194       dlg_set_cancel(win, bt);
195       gtk_widget_grab_default(bt);
196   }
197
198   if(!bt) {
199       /* Catch the "key_press_event" signal in the window, so that we can catch
200        the ESC key being pressed and act as if the "OK" button had
201        been selected. */
202     dlg_set_cancel(win, ok_bt);
203     gtk_widget_grab_default(ok_bt);
204   }
205
206   gtk_widget_show(win);
207
208   return win;
209 }
210
211 static void
212 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
213   gint button       = GPOINTER_TO_INT(    OBJECT_GET_DATA(w,   CALLBACK_BTN_KEY));
214   simple_dialog_cb_t    callback_fct    = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
215   gpointer              data            = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
216
217   gtk_widget_destroy(GTK_WIDGET(win));
218
219   if (callback_fct)
220     (callback_fct) (win, button, data);
221 }
222
223 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
224 {
225
226     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
227     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
228 }
229
230 char *
231 simple_dialog_primary_start(void) {
232     return PRIMARY_TEXT_START;
233 }
234
235 char *
236 simple_dialog_primary_end(void) {
237     return PRIMARY_TEXT_END;
238 }
239
240