2 * Simple message dialog box routines.
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 #include "gtkglobals.h"
34 #include "simple_dialog.h"
35 #include "dlg_utils.h"
37 #include "compat_macros.h"
39 #include <epan/strutil.h>
41 #include "image/stock_dialog_error_48.xpm"
42 #include "image/stock_dialog_info_48.xpm"
43 #include "image/stock_dialog_warning_48.xpm"
45 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
47 #define CALLBACK_FCT_KEY "ESD_Callback_Fct"
48 #define CALLBACK_BTN_KEY "ESD_Callback_Btn"
49 #define CALLBACK_DATA_KEY "ESD_Callback_Data"
52 * Queue for messages requested before we have a main window.
60 static GSList *message_queue;
63 display_simple_dialog(gint type, gint btn_mask, char *message)
65 GtkWidget *win, *main_vb, *top_hb, *type_pm, *msg_label,
66 *bbox, *ok_bt, *yes_bt, *bt, *save_bt, *dont_save_bt;
76 icon = stock_dialog_warning_48_xpm;
78 case ESD_TYPE_CONFIRMATION:
79 icon = stock_dialog_warning_48_xpm;
82 icon = stock_dialog_error_48_xpm;
86 icon = stock_dialog_info_48_xpm;
93 * http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
95 * says that the title should be empty for alert boxes, so there's "less
96 * visual noise and confounding text."
100 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
104 * ...appropriately identify the source of the message -- usually
105 * the name of the object. For example, if the message results
106 * from editing a document, the title text is the name of the
107 * document, optionally followed by the application name. If the
108 * message results from a non-document object, then use the
111 * and notes that the title is important "because message boxes might
112 * not always the the result of current user interaction" (e.g., some
113 * app might randomly pop something up, e.g. some browser letting you
114 * know that it couldn't fetch something because of a timeout).
116 * It also says not to use "warning" or "caution", as there's already
117 * an icon that tells you what type of alert it is, and that you
118 * shouldn't say "error", as that provides no useful information.
120 * So we give it a title on Win32, and don't give it one on UN*X.
121 * For now, we give it a Win32 title of just "Ethereal"; we should
122 * arguably take an argument for the title.
125 win = dlg_window_new("Ethereal");
127 win = dlg_window_new("");
130 gtk_window_set_modal(GTK_WINDOW(win), TRUE);
131 gtk_container_border_width(GTK_CONTAINER(win), 6);
133 /* Container for our rows */
134 main_vb = gtk_vbox_new(FALSE, 12);
135 gtk_container_add(GTK_CONTAINER(win), main_vb);
136 gtk_widget_show(main_vb);
138 /* Top row: Icon and message text */
139 top_hb = gtk_hbox_new(FALSE, 12);
140 gtk_container_border_width(GTK_CONTAINER(main_vb), 6);
141 gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
142 gtk_widget_show(top_hb);
144 style = gtk_widget_get_style(win);
145 cmap = gdk_colormap_get_system();
146 pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap, &mask,
147 &style->bg[GTK_STATE_NORMAL], icon);
148 type_pm = gtk_pixmap_new(pixmap, mask);
149 gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
150 gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
151 gtk_widget_show(type_pm);
153 msg_label = gtk_label_new(message);
155 #if GTK_MAJOR_VERSION >= 2
156 gtk_label_set_markup(GTK_LABEL(msg_label), message);
157 gtk_label_set_selectable(GTK_LABEL(msg_label), TRUE);
160 gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
161 gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
162 gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
163 gtk_widget_show(msg_label);
168 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
170 case(ESD_BTN_OK | ESD_BTN_CANCEL):
171 bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
173 case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
174 bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
176 case(ESD_BTNS_YES_NO_CANCEL):
177 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
179 case(ESD_BTNS_SAVE_DONTSAVE_CANCEL):
180 bbox = dlg_button_row_new(GTK_STOCK_SAVE, ETHEREAL_STOCK_DONT_SAVE, GTK_STOCK_CANCEL, NULL);
182 case(ESD_BTNS_YES_NO):
183 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, NULL);
186 g_assert_not_reached();
190 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
191 gtk_widget_show(bbox);
193 ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
195 OBJECT_SET_DATA(ok_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
196 SIGNAL_CONNECT(ok_bt, "clicked", simple_dialog_cancel_cb, win);
199 save_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_SAVE);
201 OBJECT_SET_DATA(save_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_SAVE));
202 SIGNAL_CONNECT(save_bt, "clicked", simple_dialog_cancel_cb, win);
205 dont_save_bt = OBJECT_GET_DATA(bbox, ETHEREAL_STOCK_DONT_SAVE);
207 OBJECT_SET_DATA(dont_save_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_DONT_SAVE));
208 SIGNAL_CONNECT(dont_save_bt, "clicked", simple_dialog_cancel_cb, win);
210 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
212 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
213 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
216 yes_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
218 OBJECT_SET_DATA(yes_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
219 SIGNAL_CONNECT(yes_bt, "clicked", simple_dialog_cancel_cb, win);
222 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
224 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
225 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
228 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
230 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
231 window_set_cancel_button(win, bt, simple_dialog_cancel_cb);
236 window_set_cancel_button(win, yes_bt, simple_dialog_cancel_cb);
238 window_set_cancel_button(win, ok_bt, simple_dialog_cancel_cb);
242 gtk_widget_show(win);
248 display_queued_messages(void)
250 queued_message_t *queued_message;
252 while (message_queue != NULL) {
253 queued_message = message_queue->data;
254 message_queue = g_slist_remove(message_queue, queued_message);
256 display_simple_dialog(queued_message->type, queued_message->btn_mask,
257 queued_message->message);
259 g_free(queued_message->message);
260 g_free(queued_message);
264 /* Simple dialog function - Displays a dialog box with the supplied message
268 * type : One of ESD_TYPE_*.
269 * btn_mask : The value passed in determines which buttons are displayed.
270 * msg_format : Sprintf-style format of the text displayed in the dialog.
271 * ... : Argument list for msg_format
275 vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap)
279 queued_message_t *queued_message;
281 #if GTK_MAJOR_VERSION >= 2
282 GdkWindowState state = 0;
285 /* Format the message. */
286 vmessage = g_strdup_vprintf(msg_format, ap);
288 #if GTK_MAJOR_VERSION >= 2
289 /* convert character encoding from locale to UTF8 (using iconv) */
290 message = g_locale_to_utf8(vmessage, -1, NULL, NULL, NULL);
296 #if GTK_MAJOR_VERSION >= 2
297 if (top_level != NULL) {
298 state = gdk_window_get_state(top_level->window);
301 /* If we don't yet have a main window or it's iconified, don't show the
302 dialog. If showing up a dialog, while main window is iconified, program
303 will become unresponsive! */
304 if (top_level == NULL || state & GDK_WINDOW_STATE_ICONIFIED) {
307 /* If we don't yet have a main window, queue up the message for later
309 if (top_level == NULL) {
311 queued_message = g_malloc(sizeof (queued_message_t));
312 queued_message->type = type;
313 queued_message->btn_mask = btn_mask;
314 queued_message->message = message;
315 message_queue = g_slist_append(message_queue, queued_message);
320 * Do we have any queued up messages? If so, pop them up.
322 display_queued_messages();
324 win = display_simple_dialog(type, btn_mask, message);
332 simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...)
337 va_start(ap, msg_format);
338 ret = vsimple_dialog(type, btn_mask, msg_format, ap);
344 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
345 gint button = GPOINTER_TO_INT( OBJECT_GET_DATA(w, CALLBACK_BTN_KEY));
346 simple_dialog_cb_t callback_fct = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
347 gpointer data = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
349 window_destroy(GTK_WIDGET(win));
352 (callback_fct) (win, button, data);
355 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
358 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
359 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
363 simple_dialog_primary_start(void) {
364 return PRIMARY_TEXT_START;
368 simple_dialog_primary_end(void) {
369 return PRIMARY_TEXT_END;
373 simple_dialog_format_message(const char *msg)
378 #if GTK_MAJOR_VERSION < 2
381 str = xml_escape(msg);