2 * Simple message dialog box routines.
4 * $Id: simple_dialog.c,v 1.18 2004/01/31 01:28:10 ulfl Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #ifdef NEED_SNPRINTF_H
35 # include "snprintf.h"
38 #include "gtkglobals.h"
39 #include "simple_dialog.h"
40 #include "dlg_utils.h"
41 #include "compat_macros.h"
43 #include "image/stock_dialog_error_48.xpm"
44 #include "image/stock_dialog_info_48.xpm"
45 #include "image/stock_dialog_question_48.xpm"
46 #include "image/stock_dialog_warning_48.xpm"
48 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
50 #define CALLBACK_FCT_KEY "ESD_Callback_Fct"
51 #define CALLBACK_BTN_KEY "ESD_Callback_Btn"
52 #define CALLBACK_DATA_KEY "ESD_Callback_Data"
54 /* Simple dialog function - Displays a dialog box with the supplied message
58 * type : One of ESD_TYPE_*.
59 * btn_mask : The value passed in determines which buttons are displayed.
60 * msg_format : Sprintf-style format of the text displayed in the dialog.
61 * ... : Argument list for msg_format
65 #define ESD_MAX_MSG_LEN 2048
67 simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...) {
68 GtkWidget *win, *main_vb, *top_hb, *type_pm, *msg_label,
75 gchar message[ESD_MAX_MSG_LEN];
79 switch (type & ~ESD_TYPE_MODAL) {
81 icon = stock_dialog_warning_48_xpm;
82 win = dlg_window_new("Ethereal: Warning");
85 icon = stock_dialog_error_48_xpm;
86 win = dlg_window_new("Ethereal: Error");
89 icon = stock_dialog_question_48_xpm;
90 win = dlg_window_new("Ethereal: Question");
94 icon = stock_dialog_info_48_xpm;
95 win = dlg_window_new("Ethereal: Information");
99 if (type & ESD_TYPE_MODAL)
100 gtk_window_set_modal(GTK_WINDOW(win), TRUE);
102 gtk_container_border_width(GTK_CONTAINER(win), 7);
104 /* Container for our rows */
105 main_vb = gtk_vbox_new(FALSE, 5);
106 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
107 gtk_container_add(GTK_CONTAINER(win), main_vb);
108 gtk_widget_show(main_vb);
110 /* Top row: Icon and message text */
111 top_hb = gtk_hbox_new(FALSE, 10);
112 gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
113 gtk_widget_show(top_hb);
115 style = gtk_widget_get_style(win);
116 cmap = gdk_colormap_get_system();
117 pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap, &mask,
118 &style->bg[GTK_STATE_NORMAL], icon);
119 type_pm = gtk_pixmap_new(pixmap, mask);
120 gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
121 gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
122 gtk_widget_show(type_pm);
124 /* Load our vararg list into the message string */
125 va_start(ap, msg_format);
126 vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
129 msg_label = gtk_label_new(message);
130 gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
131 gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
132 gtk_widget_show(msg_label);
138 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
140 case(ESD_BTN_OK | ESD_BTN_CANCEL):
141 bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
143 case(ESD_BTN_YES | ESD_BTN_NO | ESD_BTN_CANCEL):
144 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
147 g_assert_not_reached();
149 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
150 gtk_widget_show(bbox);
152 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
154 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
155 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
156 gtk_widget_grab_default(bt);
157 /* Catch the "key_press_event" signal in the window, so that we can catch
158 the ESC key being pressed and act as if the "OK" button had
160 dlg_set_cancel(win, bt);
163 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
165 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
166 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
169 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
171 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
172 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
175 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
177 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
178 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
179 /* Catch the "key_press_event" signal in the window, so that we can catch
180 the ESC key being pressed and act as if the "OK" button had
182 dlg_set_cancel(win, bt);
183 gtk_widget_grab_default(bt);
186 gtk_widget_show(win);
192 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
193 gint button = GPOINTER_TO_INT( OBJECT_GET_DATA(w, CALLBACK_BTN_KEY));
194 simple_dialog_cb_t callback_fct = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
195 gpointer data = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
197 gtk_widget_destroy(GTK_WIDGET(win));
200 (callback_fct) (win, button, data);
203 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
206 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
207 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);