2 * Simple message dialog box routines.
4 * $Id: simple_dialog.c,v 1.28 2004/02/23 00:05:50 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
33 #ifdef NEED_SNPRINTF_H
34 # include "snprintf.h"
37 #include "gtkglobals.h"
38 #include "simple_dialog.h"
39 #include "dlg_utils.h"
40 #include "compat_macros.h"
42 #include "image/stock_dialog_error_48.xpm"
43 #include "image/stock_dialog_info_48.xpm"
44 #include "image/stock_dialog_warning_48.xpm"
46 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
48 #define CALLBACK_FCT_KEY "ESD_Callback_Fct"
49 #define CALLBACK_BTN_KEY "ESD_Callback_Btn"
50 #define CALLBACK_DATA_KEY "ESD_Callback_Data"
52 /* Simple dialog function - Displays a dialog box with the supplied message
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
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)
72 simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...) {
73 GtkWidget *win, *main_vb, *top_hb, *type_pm, *msg_label,
86 icon = stock_dialog_warning_48_xpm;
88 case ESD_TYPE_CONFIRMATION:
89 icon = stock_dialog_warning_48_xpm;
92 icon = stock_dialog_error_48_xpm;
96 icon = stock_dialog_info_48_xpm;
103 * http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
105 * says that the title should be empty for alert boxes, so there's "less
106 * visual noise and confounding text."
110 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
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
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).
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.
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.
135 win = dlg_window_new("Ethereal");
137 win = dlg_window_new("");
140 gtk_window_set_modal(GTK_WINDOW(win), TRUE);
141 gtk_container_border_width(GTK_CONTAINER(win), 6);
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);
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);
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);
163 /* Load our vararg list into the message string */
164 va_start(ap, msg_format);
165 vsnprintf(message, sizeof(message), msg_format, ap);
168 msg_label = gtk_label_new(message);
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);
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);
183 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
185 case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
186 bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
188 case(ESD_BTNS_YES_NO_CANCEL):
189 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
192 g_assert_not_reached();
196 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
197 gtk_widget_show(bbox);
199 ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
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);
205 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
207 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
208 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
211 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
213 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
214 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
217 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
219 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
220 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
223 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
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
230 dlg_set_cancel(win, bt);
231 gtk_widget_grab_default(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
238 dlg_set_cancel(win, ok_bt);
239 gtk_widget_grab_default(ok_bt);
242 gtk_widget_show(win);
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);
253 gtk_widget_destroy(GTK_WIDGET(win));
256 (callback_fct) (win, button, data);
259 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
262 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
263 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
267 simple_dialog_primary_start(void) {
268 return PRIMARY_TEXT_START;
272 simple_dialog_primary_end(void) {
273 return PRIMARY_TEXT_END;