2 * Simple message dialog box routines.
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
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.
32 #include <epan/strutil.h>
34 #include "../simple_dialog.h"
36 #include "gtk/gtkglobals.h"
37 #include "gtk/dlg_utils.h"
38 #include "gtk/gui_utils.h"
39 #include "gtk/stock_icons.h"
41 #include "gtk/old-gtk-compat.h"
43 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
45 #define CALLBACK_FCT_KEY "ESD_Callback_Fct"
46 #define CALLBACK_BTN_KEY "ESD_Callback_Btn"
47 #define CALLBACK_DATA_KEY "ESD_Callback_Data"
48 #define CHECK_BUTTON "ESD_Check_Button"
51 * Queue for messages requested before we have a main window.
59 static GSList *message_queue;
62 display_simple_dialog(gint type, gint btn_mask, char *message)
64 GtkWidget *win, *main_vb, *top_hb, *msg_vb, *type_pm, *msg_label, *ask_cb,
65 *bbox, *ok_bt, *yes_bt, *bt, *save_bt, *dont_save_bt;
70 type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
72 case ESD_TYPE_CONFIRMATION:
73 type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
76 type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
79 type_pm = gtk_image_new_from_stock( GTK_STOCK_STOP, GTK_ICON_SIZE_DIALOG);
83 type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
90 * http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
92 * says that the title should be empty for alert boxes, so there's "less
93 * visual noise and confounding text."
97 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
101 * ...appropriately identify the source of the message -- usually
102 * the name of the object. For example, if the message results
103 * from editing a document, the title text is the name of the
104 * document, optionally followed by the application name. If the
105 * message results from a non-document object, then use the
108 * and notes that the title is important "because message boxes might
109 * not always the the result of current user interaction" (e.g., some
110 * app might randomly pop something up, e.g. some browser letting you
111 * know that it couldn't fetch something because of a timeout).
113 * It also says not to use "warning" or "caution", as there's already
114 * an icon that tells you what type of alert it is, and that you
115 * shouldn't say "error", as that provides no useful information.
117 * So we give it a title on Win32, and don't give it one on UN*X.
118 * For now, we give it a Win32 title of just "Wireshark"; we should
119 * arguably take an argument for the title.
121 if(btn_mask == ESD_BTN_NONE) {
122 win = splash_window_new();
125 win = dlg_window_new("Wireshark");
127 win = dlg_window_new("");
131 gtk_window_set_modal(GTK_WINDOW(win), TRUE);
132 gtk_container_set_border_width(GTK_CONTAINER(win), 6);
134 /* Container for our rows */
135 main_vb = gtk_vbox_new(FALSE, 12);
136 gtk_container_add(GTK_CONTAINER(win), main_vb);
137 gtk_widget_show(main_vb);
139 /* Top row: Icon and message text */
140 top_hb = gtk_hbox_new(FALSE, 12);
141 gtk_container_set_border_width(GTK_CONTAINER(main_vb), 6);
142 gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
143 gtk_widget_show(top_hb);
145 gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5f, 0.0f);
146 gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
147 gtk_widget_show(type_pm);
149 /* column for message and optional check button */
150 msg_vb = gtk_vbox_new(FALSE, 6);
151 gtk_box_set_spacing(GTK_BOX(msg_vb), 24);
152 gtk_container_add(GTK_CONTAINER(top_hb), msg_vb);
153 gtk_widget_show(msg_vb);
156 msg_label = gtk_label_new(message);
158 gtk_label_set_markup(GTK_LABEL(msg_label), message);
159 gtk_label_set_selectable(GTK_LABEL(msg_label), TRUE);
160 g_object_set(gtk_widget_get_settings(msg_label),
161 "gtk-label-select-on-focus", FALSE, NULL);
163 gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
164 gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5f, 0.0f);
165 gtk_container_add(GTK_CONTAINER(msg_vb), msg_label);
166 gtk_label_set_line_wrap(GTK_LABEL(msg_label), TRUE);
167 gtk_widget_show(msg_label);
169 if(btn_mask == ESD_BTN_NONE) {
170 gtk_widget_show(win);
174 /* optional check button */
175 ask_cb = gtk_check_button_new_with_label("replace with text...");
176 gtk_container_add(GTK_CONTAINER(msg_vb), ask_cb);
177 g_object_set_data(G_OBJECT(win), CHECK_BUTTON, ask_cb);
182 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
184 case(ESD_BTN_OK | ESD_BTN_CANCEL):
185 bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
187 case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
188 bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
190 case(ESD_BTNS_YES_NO_CANCEL):
191 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
193 case(ESD_BTNS_SAVE_DONTSAVE_CANCEL):
194 bbox = dlg_button_row_new(GTK_STOCK_SAVE, WIRESHARK_STOCK_DONT_SAVE, GTK_STOCK_CANCEL, NULL);
196 case(ESD_BTNS_SAVE_QUIT_DONTSAVE_CANCEL):
197 bbox = dlg_button_row_new(GTK_STOCK_SAVE, WIRESHARK_STOCK_QUIT_DONT_SAVE, GTK_STOCK_CANCEL, NULL);
199 case(ESD_BTNS_YES_NO):
200 bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, NULL);
203 g_assert_not_reached();
207 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
208 gtk_widget_show(bbox);
210 ok_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_OK);
212 g_object_set_data(G_OBJECT(ok_bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
213 g_signal_connect(ok_bt, "clicked", G_CALLBACK(simple_dialog_cancel_cb), win);
216 save_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_SAVE);
218 g_object_set_data(G_OBJECT(save_bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_SAVE));
219 g_signal_connect(save_bt, "clicked", G_CALLBACK(simple_dialog_cancel_cb), win);
222 dont_save_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_DONT_SAVE);
224 g_object_set_data(G_OBJECT(dont_save_bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_DONT_SAVE));
225 g_signal_connect(dont_save_bt, "clicked", G_CALLBACK(simple_dialog_cancel_cb), win);
228 dont_save_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_QUIT_DONT_SAVE);
230 g_object_set_data(G_OBJECT(dont_save_bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_QUIT_DONT_SAVE));
231 g_signal_connect(dont_save_bt, "clicked", G_CALLBACK(simple_dialog_cancel_cb), win);
233 bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLEAR);
235 g_object_set_data(G_OBJECT(bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
236 g_signal_connect(bt, "clicked", G_CALLBACK(simple_dialog_cancel_cb), win);
239 yes_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_YES);
241 g_object_set_data(G_OBJECT(yes_bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
242 g_signal_connect(yes_bt, "clicked", G_CALLBACK(simple_dialog_cancel_cb), win);
245 bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_NO);
247 g_object_set_data(G_OBJECT(bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
248 g_signal_connect(bt, "clicked", G_CALLBACK(simple_dialog_cancel_cb), win);
251 bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CANCEL);
253 g_object_set_data(G_OBJECT(bt), CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
254 window_set_cancel_button(win, bt, simple_dialog_cancel_cb);
259 window_set_cancel_button(win, yes_bt, simple_dialog_cancel_cb);
261 window_set_cancel_button(win, ok_bt, simple_dialog_cancel_cb);
265 dlg_button_focus_nth(bbox, 0);
267 gtk_widget_show(win);
273 display_queued_messages(void)
275 queued_message_t *queued_message;
277 while (message_queue != NULL) {
278 queued_message = message_queue->data;
279 message_queue = g_slist_remove(message_queue, queued_message);
281 display_simple_dialog(queued_message->type, queued_message->btn_mask,
282 queued_message->message);
284 g_free(queued_message->message);
285 g_free(queued_message);
289 /* Simple dialog function - Displays a dialog box with the supplied message
293 * type : One of ESD_TYPE_*.
294 * btn_mask : The value passed in determines which buttons are displayed.
295 * msg_format : Sprintf-style format of the text displayed in the dialog.
296 * ... : Argument list for msg_format
300 vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap)
303 queued_message_t *queued_message;
305 GdkWindowState state = 0;
307 /* Format the message. */
308 message = g_strdup_vprintf(msg_format, ap);
310 if (top_level != NULL) {
311 state = gdk_window_get_state(gtk_widget_get_window(top_level));
314 /* If we don't yet have a main window or it's iconified, don't show the
315 dialog. If showing up a dialog, while main window is iconified, program
316 will become unresponsive! */
317 if (top_level == NULL || state & GDK_WINDOW_STATE_ICONIFIED) {
319 queued_message = g_malloc(sizeof (queued_message_t));
320 queued_message->type = type;
321 queued_message->btn_mask = btn_mask;
322 queued_message->message = message;
323 message_queue = g_slist_append(message_queue, queued_message);
328 * Do we have any queued up messages? If so, pop them up.
330 display_queued_messages();
332 win = display_simple_dialog(type, btn_mask, message);
340 simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...)
345 va_start(ap, msg_format);
346 ret = vsimple_dialog(type, btn_mask, msg_format, ap);
352 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
353 gint button = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w), CALLBACK_BTN_KEY));
354 simple_dialog_cb_t callback_fct = g_object_get_data(G_OBJECT(win), CALLBACK_FCT_KEY);
355 gpointer data = g_object_get_data(G_OBJECT(win), CALLBACK_DATA_KEY);
358 (callback_fct) (win, button, data);
360 window_destroy(GTK_WIDGET(win));
364 simple_dialog_close(gpointer dialog)
366 window_destroy(GTK_WIDGET(dialog));
369 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
372 g_object_set_data(G_OBJECT(GTK_WIDGET(dialog)), CALLBACK_FCT_KEY, callback_fct);
373 g_object_set_data(G_OBJECT(GTK_WIDGET(dialog)), CALLBACK_DATA_KEY, data);
376 void simple_dialog_check_set(gpointer dialog, gchar *text _U_) {
377 GtkWidget *ask_cb = g_object_get_data(G_OBJECT(dialog), CHECK_BUTTON);
379 gtk_button_set_label(GTK_BUTTON(ask_cb), text);
380 gtk_widget_show(ask_cb);
383 gboolean simple_dialog_check_get(gpointer dialog) {
384 GtkWidget *ask_cb = g_object_get_data(G_OBJECT(GTK_WIDGET(dialog)), CHECK_BUTTON);
386 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ask_cb));
390 simple_dialog_primary_start(void) {
391 return "<span weight=\"bold\" size=\"larger\">";
395 simple_dialog_primary_end(void) {
400 simple_dialog_format_message(const char *msg)
405 str = xml_escape(msg);