2 * Simple message dialog box routines.
4 * $Id: simple_dialog.c,v 1.24 2004/02/04 01:10:37 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_question_48.xpm"
45 #include "image/stock_dialog_warning_48.xpm"
47 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
49 #define CALLBACK_FCT_KEY "ESD_Callback_Fct"
50 #define CALLBACK_BTN_KEY "ESD_Callback_Btn"
51 #define CALLBACK_DATA_KEY "ESD_Callback_Data"
53 /* Simple dialog function - Displays a dialog box with the supplied message
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
65 simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...) {
66 GtkWidget *win, *main_vb, *top_hb, *type_pm, *msg_label,
77 switch (type & ~ESD_TYPE_MODAL) {
79 icon = stock_dialog_warning_48_xpm;
80 win = dlg_window_new("Ethereal: Warning");
82 case ESD_TYPE_QUESTION:
83 icon = stock_dialog_question_48_xpm;
84 win = dlg_window_new("Ethereal: Question");
87 icon = stock_dialog_error_48_xpm;
88 win = dlg_window_new("Ethereal: Error");
92 icon = stock_dialog_info_48_xpm;
93 win = dlg_window_new("Ethereal: Information");
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");
103 if (type & ESD_TYPE_MODAL)
104 gtk_window_set_modal(GTK_WINDOW(win), TRUE);
105 gtk_container_border_width(GTK_CONTAINER(win), 6);
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);
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);
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);
127 /* Load our vararg list into the message string */
128 va_start(ap, msg_format);
129 vsnprintf(message, sizeof(message), msg_format, ap);
132 msg_label = gtk_label_new(message);
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);
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);
147 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
149 case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
150 bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
152 case(ESD_BTNS_YES_NO_CANCEL):
153 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
156 g_assert_not_reached();
160 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
161 gtk_widget_show(bbox);
163 ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
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);
169 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
171 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
172 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
175 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
177 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
178 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
181 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
183 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
184 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
187 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
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
194 dlg_set_cancel(win, bt);
195 gtk_widget_grab_default(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
202 dlg_set_cancel(win, ok_bt);
203 gtk_widget_grab_default(ok_bt);
206 gtk_widget_show(win);
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);
217 gtk_widget_destroy(GTK_WIDGET(win));
220 (callback_fct) (win, button, data);
223 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
226 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
227 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
231 simple_dialog_primary_start(void) {
232 return PRIMARY_TEXT_START;
236 simple_dialog_primary_end(void) {
237 return PRIMARY_TEXT_END;