Put in a comment noting a problem with dialog boxes popped up before 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.28 2004/02/23 00:05:50 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_warning_48.xpm"
45
46 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
47
48 #define CALLBACK_FCT_KEY    "ESD_Callback_Fct"
49 #define CALLBACK_BTN_KEY    "ESD_Callback_Btn"
50 #define CALLBACK_DATA_KEY   "ESD_Callback_Data"
51
52 /* Simple dialog function - Displays a dialog box with the supplied message
53  * text.
54  *
55  * Args:
56  * type       : One of ESD_TYPE_*.
57  * btn_mask   : The value passed in determines which buttons are displayed.
58  * msg_format : Sprintf-style format of the text displayed in the dialog.
59  * ...        : Argument list for msg_format
60  *
61  *
62  * XXX - if we haven't yet put up the main window, we should just
63  * queue up the message, etc., and wait until the main window pops up
64  * (or until we figure out that we're in a capture child and aren't
65  * going to pop up a main window) and pop up the alert boxes then, so
66  * that even stuff popped up before we put up the main window (such
67  * as file-open or file-read errors from dissectors' init routines)
68  * shows up on top.
69  */
70
71 gpointer
72 simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...) {
73   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
74               *bbox, *ok_bt, *bt;
75   GdkPixmap   *pixmap;
76   GdkBitmap   *mask;
77   GtkStyle    *style;
78   GdkColormap *cmap;
79   va_list      ap;
80   gchar        message[2048];
81   gchar      **icon;
82
83   /* Main window */
84   switch (type) {
85   case ESD_TYPE_WARN :
86     icon = stock_dialog_warning_48_xpm;
87     break;
88   case ESD_TYPE_CONFIRMATION:
89     icon = stock_dialog_warning_48_xpm;
90     break;
91   case ESD_TYPE_ERROR:
92     icon = stock_dialog_error_48_xpm;
93     break;
94   case ESD_TYPE_INFO :
95   default :
96     icon = stock_dialog_info_48_xpm;
97     break;
98   }
99
100   /*
101    * The GNOME HIG:
102    *
103    *    http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
104    *
105    * says that the title should be empty for alert boxes, so there's "less
106    * visual noise and confounding text."
107    *
108    * The Windows HIG:
109    *
110    *    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
111    *
112    * says it should
113    *
114    *    ...appropriately identify the source of the message -- usually
115    *    the name of the object.  For example, if the message results
116    *    from editing a document, the title text is the name of the
117    *    document, optionally followed by the application name.  If the
118    *    message results from a non-document object, then use the
119    *    application name."
120    *
121    * and notes that the title is important "because message boxes might
122    * not always the the result of current user interaction" (e.g., some
123    * app might randomly pop something up, e.g. some browser letting you
124    * know that it couldn't fetch something because of a timeout).
125    *
126    * It also says not to use "warning" or "caution", as there's already
127    * an icon that tells you what type of alert it is, and that you
128    * shouldn't say "error", as that provides no useful information.
129    *
130    * So we give it a title on Win32, and don't give it one on UN*X.
131    * For now, we give it a Win32 title of just "Ethereal"; we should
132    * arguably take an argument for the title.
133    */
134 #ifdef _WIN32
135   win = dlg_window_new("Ethereal");
136 #else
137   win = dlg_window_new("");
138 #endif
139
140   gtk_window_set_modal(GTK_WINDOW(win), TRUE);
141   gtk_container_border_width(GTK_CONTAINER(win), 6);
142
143   /* Container for our rows */
144   main_vb = gtk_vbox_new(FALSE, 12);
145   gtk_container_add(GTK_CONTAINER(win), main_vb);
146   gtk_widget_show(main_vb);
147
148   /* Top row: Icon and message text */
149   top_hb = gtk_hbox_new(FALSE, 12);
150   gtk_container_border_width(GTK_CONTAINER(main_vb), 6);
151   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
152   gtk_widget_show(top_hb);
153
154   style = gtk_widget_get_style(win);
155   cmap  = gdk_colormap_get_system();
156   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
157     &style->bg[GTK_STATE_NORMAL], icon);
158   type_pm = gtk_pixmap_new(pixmap, mask);
159   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
160   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
161   gtk_widget_show(type_pm);
162
163   /* Load our vararg list into the message string */
164   va_start(ap, msg_format);
165   vsnprintf(message, sizeof(message), msg_format, ap);
166   va_end(ap);
167
168   msg_label = gtk_label_new(message);
169
170 #if GTK_MAJOR_VERSION >= 2
171   gtk_label_set_markup(GTK_LABEL(msg_label), message);
172   gtk_label_set_selectable(GTK_LABEL(msg_label), TRUE);
173 #endif
174
175   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
176   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
177   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
178   gtk_widget_show(msg_label);
179
180   /* Button row */
181   switch(btn_mask) {
182   case(ESD_BTN_OK):
183     bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
184     break;
185   case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
186     bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
187     break;
188   case(ESD_BTNS_YES_NO_CANCEL):
189     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
190     break;
191   default:
192     g_assert_not_reached();
193     bbox = NULL;
194     break;
195   }
196   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
197   gtk_widget_show(bbox);
198
199   ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
200   if(ok_bt) {
201       OBJECT_SET_DATA(ok_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
202       SIGNAL_CONNECT(ok_bt, "clicked", simple_dialog_cancel_cb, win);
203   }
204
205   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
206   if(bt) {
207       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
208       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
209   }
210
211   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
212   if(bt) {
213       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
214       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
215   }
216
217   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
218   if(bt) {
219       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
220       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
221   }
222
223   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
224   if(bt) {
225       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
226       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
227     /* Catch the "key_press_event" signal in the window, so that we can catch
228        the ESC key being pressed and act as if the "OK" button had
229        been selected. */
230       dlg_set_cancel(win, bt);
231       gtk_widget_grab_default(bt);
232   }
233
234   if(!bt) {
235       /* Catch the "key_press_event" signal in the window, so that we can catch
236        the ESC key being pressed and act as if the "OK" button had
237        been selected. */
238     dlg_set_cancel(win, ok_bt);
239     gtk_widget_grab_default(ok_bt);
240   }
241
242   gtk_widget_show(win);
243
244   return win;
245 }
246
247 static void
248 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
249   gint button       = GPOINTER_TO_INT(    OBJECT_GET_DATA(w,   CALLBACK_BTN_KEY));
250   simple_dialog_cb_t    callback_fct    = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
251   gpointer              data            = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
252
253   gtk_widget_destroy(GTK_WIDGET(win));
254
255   if (callback_fct)
256     (callback_fct) (win, button, data);
257 }
258
259 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
260 {
261
262     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
263     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
264 }
265
266 char *
267 simple_dialog_primary_start(void) {
268     return PRIMARY_TEXT_START;
269 }
270
271 char *
272 simple_dialog_primary_end(void) {
273     return PRIMARY_TEXT_END;
274 }
275
276