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"
36 #include "gui_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"
44 #include "image/stock_dialog_stop_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"
51 #define CHECK_BUTTON "ESD_Check_Button"
54 * Queue for messages requested before we have a main window.
62 static GSList *message_queue;
65 display_simple_dialog(gint type, gint btn_mask, char *message)
67 GtkWidget *win, *main_vb, *top_hb, *msg_vb, *type_pm, *msg_label, *ask_cb,
68 *bbox, *ok_bt, *yes_bt, *bt, *save_bt, *dont_save_bt;
78 icon = stock_dialog_warning_48_xpm;
80 case ESD_TYPE_CONFIRMATION:
81 icon = stock_dialog_warning_48_xpm;
84 icon = stock_dialog_error_48_xpm;
87 icon = stock_dialog_stop_48_xpm;
91 icon = stock_dialog_info_48_xpm;
98 * http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
100 * says that the title should be empty for alert boxes, so there's "less
101 * visual noise and confounding text."
105 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
109 * ...appropriately identify the source of the message -- usually
110 * the name of the object. For example, if the message results
111 * from editing a document, the title text is the name of the
112 * document, optionally followed by the application name. If the
113 * message results from a non-document object, then use the
116 * and notes that the title is important "because message boxes might
117 * not always the the result of current user interaction" (e.g., some
118 * app might randomly pop something up, e.g. some browser letting you
119 * know that it couldn't fetch something because of a timeout).
121 * It also says not to use "warning" or "caution", as there's already
122 * an icon that tells you what type of alert it is, and that you
123 * shouldn't say "error", as that provides no useful information.
125 * So we give it a title on Win32, and don't give it one on UN*X.
126 * For now, we give it a Win32 title of just "Ethereal"; we should
127 * arguably take an argument for the title.
129 if(btn_mask == ESD_BTN_NONE) {
130 win = splash_window_new();
133 win = dlg_window_new("Ethereal");
135 win = dlg_window_new("");
139 gtk_window_set_modal(GTK_WINDOW(win), TRUE);
140 gtk_container_border_width(GTK_CONTAINER(win), 6);
142 /* Container for our rows */
143 main_vb = gtk_vbox_new(FALSE, 12);
144 gtk_container_add(GTK_CONTAINER(win), main_vb);
145 gtk_widget_show(main_vb);
147 /* Top row: Icon and message text */
148 top_hb = gtk_hbox_new(FALSE, 12);
149 gtk_container_border_width(GTK_CONTAINER(main_vb), 6);
150 gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
151 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], (gchar **) 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 /* column for message and optional check button */
164 msg_vb = gtk_vbox_new(FALSE, 6);
165 gtk_box_set_spacing(GTK_BOX(msg_vb), 24);
166 gtk_container_add(GTK_CONTAINER(top_hb), msg_vb);
167 gtk_widget_show(msg_vb);
170 msg_label = gtk_label_new(message);
172 #if GTK_MAJOR_VERSION >= 2
173 gtk_label_set_markup(GTK_LABEL(msg_label), message);
174 gtk_label_set_selectable(GTK_LABEL(msg_label), TRUE);
177 gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
178 gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
179 gtk_container_add(GTK_CONTAINER(msg_vb), msg_label);
180 gtk_widget_show(msg_label);
182 if(btn_mask == ESD_BTN_NONE) {
183 gtk_widget_show(win);
187 /* optional check button */
188 ask_cb = gtk_check_button_new_with_label("replace with text...");
189 gtk_container_add(GTK_CONTAINER(msg_vb), ask_cb);
190 OBJECT_SET_DATA(win, CHECK_BUTTON, ask_cb);
195 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
197 case(ESD_BTN_OK | ESD_BTN_CANCEL):
198 bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
200 case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
201 bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
203 case(ESD_BTNS_YES_NO_CANCEL):
204 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
206 case(ESD_BTNS_SAVE_DONTSAVE_CANCEL):
207 bbox = dlg_button_row_new(GTK_STOCK_SAVE, ETHEREAL_STOCK_DONT_SAVE, GTK_STOCK_CANCEL, NULL);
209 case(ESD_BTNS_YES_NO):
210 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, NULL);
213 g_assert_not_reached();
217 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
218 gtk_widget_show(bbox);
220 ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
222 OBJECT_SET_DATA(ok_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
223 SIGNAL_CONNECT(ok_bt, "clicked", simple_dialog_cancel_cb, win);
226 save_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_SAVE);
228 OBJECT_SET_DATA(save_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_SAVE));
229 SIGNAL_CONNECT(save_bt, "clicked", simple_dialog_cancel_cb, win);
232 dont_save_bt = OBJECT_GET_DATA(bbox, ETHEREAL_STOCK_DONT_SAVE);
234 OBJECT_SET_DATA(dont_save_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_DONT_SAVE));
235 SIGNAL_CONNECT(dont_save_bt, "clicked", simple_dialog_cancel_cb, win);
237 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
239 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
240 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
243 yes_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
245 OBJECT_SET_DATA(yes_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
246 SIGNAL_CONNECT(yes_bt, "clicked", simple_dialog_cancel_cb, win);
249 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
251 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
252 SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
255 bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
257 OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
258 window_set_cancel_button(win, bt, simple_dialog_cancel_cb);
263 window_set_cancel_button(win, yes_bt, simple_dialog_cancel_cb);
265 window_set_cancel_button(win, ok_bt, simple_dialog_cancel_cb);
269 gtk_widget_show(win);
275 display_queued_messages(void)
277 queued_message_t *queued_message;
279 while (message_queue != NULL) {
280 queued_message = message_queue->data;
281 message_queue = g_slist_remove(message_queue, queued_message);
283 display_simple_dialog(queued_message->type, queued_message->btn_mask,
284 queued_message->message);
286 g_free(queued_message->message);
287 g_free(queued_message);
291 /* Simple dialog function - Displays a dialog box with the supplied message
295 * type : One of ESD_TYPE_*.
296 * btn_mask : The value passed in determines which buttons are displayed.
297 * msg_format : Sprintf-style format of the text displayed in the dialog.
298 * ... : Argument list for msg_format
302 vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap)
306 queued_message_t *queued_message;
308 #if GTK_MAJOR_VERSION >= 2
309 GdkWindowState state = 0;
312 /* Format the message. */
313 vmessage = g_strdup_vprintf(msg_format, ap);
315 #if GTK_MAJOR_VERSION >= 2
316 /* convert character encoding from locale to UTF8 (using iconv) */
317 message = g_locale_to_utf8(vmessage, -1, NULL, NULL, NULL);
323 #if GTK_MAJOR_VERSION >= 2
324 if (top_level != NULL) {
325 state = gdk_window_get_state(top_level->window);
328 /* If we don't yet have a main window or it's iconified, don't show the
329 dialog. If showing up a dialog, while main window is iconified, program
330 will become unresponsive! */
331 if (top_level == NULL || state & GDK_WINDOW_STATE_ICONIFIED) {
334 /* If we don't yet have a main window, queue up the message for later
336 if (top_level == NULL) {
338 queued_message = g_malloc(sizeof (queued_message_t));
339 queued_message->type = type;
340 queued_message->btn_mask = btn_mask;
341 queued_message->message = message;
342 message_queue = g_slist_append(message_queue, queued_message);
347 * Do we have any queued up messages? If so, pop them up.
349 display_queued_messages();
351 win = display_simple_dialog(type, btn_mask, message);
359 simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...)
364 va_start(ap, msg_format);
365 ret = vsimple_dialog(type, btn_mask, msg_format, ap);
371 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
372 gint button = GPOINTER_TO_INT( OBJECT_GET_DATA(w, CALLBACK_BTN_KEY));
373 simple_dialog_cb_t callback_fct = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
374 gpointer data = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
376 window_destroy(GTK_WIDGET(win));
379 (callback_fct) (win, button, data);
383 simple_dialog_close(gpointer dialog)
385 window_destroy(GTK_WIDGET(dialog));
388 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
391 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
392 OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
395 void simple_dialog_check_set(gpointer dialog, gchar *text) {
396 GtkWidget *ask_cb = OBJECT_GET_DATA(dialog, CHECK_BUTTON);
398 #if GTK_MAJOR_VERSION >= 2
399 /* XXX - find a way to set the GtkButton label in GTK 1.x */
400 gtk_button_set_label(GTK_BUTTON(ask_cb), text);
402 gtk_widget_show(ask_cb);
405 gboolean simple_dialog_check_get(gpointer dialog) {
406 GtkWidget *ask_cb = OBJECT_GET_DATA(dialog, CHECK_BUTTON);
408 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ask_cb));
412 simple_dialog_primary_start(void) {
413 return PRIMARY_TEXT_START;
417 simple_dialog_primary_end(void) {
418 return PRIMARY_TEXT_END;
422 simple_dialog_format_message(const char *msg)
427 #if GTK_MAJOR_VERSION < 2
430 str = xml_escape(msg);