Add a method to fetch protocol description.
[obnox/wireshark/wip.git] / gtk / simple_dialog.c
1 /* simple_dialog.c
2  * Simple message dialog box routines.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 #include <stdio.h>
29
30 #include <gtk/gtk.h>
31
32 #include <epan/strutil.h>
33
34 #include "../simple_dialog.h"
35
36 #include "gtk/gtkglobals.h"
37 #include "gtk/dlg_utils.h"
38 #include "gtk/gui_utils.h"
39 #include "gtk/stock_icons.h"
40
41 #include "gtk/old-gtk-compat.h"
42
43 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
44
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"
49
50 /*
51  * Queue for messages requested before we have a main window.
52  */
53 typedef struct {
54         gint    type;
55         gint    btn_mask;
56         char    *message;
57 } queued_message_t;
58
59 static GSList *message_queue;
60
61 static GtkWidget *
62 display_simple_dialog(gint type, gint btn_mask, char *message)
63 {
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;
66
67   /* Main window */
68   switch (type) {
69   case ESD_TYPE_WARN :
70     type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
71     break;
72   case ESD_TYPE_CONFIRMATION:
73     type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
74     break;
75   case ESD_TYPE_ERROR:
76     type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
77     break;
78   case ESD_TYPE_STOP :
79     type_pm = gtk_image_new_from_stock( GTK_STOCK_STOP, GTK_ICON_SIZE_DIALOG);
80     break;
81   case ESD_TYPE_INFO :
82   default :
83     type_pm = gtk_image_new_from_stock( GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
84     break;
85   }
86
87   /*
88    * The GNOME HIG:
89    *
90    *    http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
91    *
92    * says that the title should be empty for alert boxes, so there's "less
93    * visual noise and confounding text."
94    *
95    * The Windows HIG:
96    *
97    *    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
98    *
99    * says it should
100    *
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
106    *    application name."
107    *
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).
112    *
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.
116    *
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.
120    */
121   if(btn_mask == ESD_BTN_NONE) {
122         win = splash_window_new();
123   } else {
124 #ifdef _WIN32
125     win = dlg_window_new("Wireshark");
126 #else
127     win = dlg_window_new("");
128 #endif
129   }
130
131   gtk_window_set_modal(GTK_WINDOW(win), TRUE);
132   gtk_container_set_border_width(GTK_CONTAINER(win), 6);
133
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);
138
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);
144
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);
148
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);
154
155   /* message */
156   msg_label = gtk_label_new(message);
157
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);
162
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);
168
169   if(btn_mask == ESD_BTN_NONE) {
170         gtk_widget_show(win);
171         return win;
172   }
173
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);
178
179   /* Button row */
180   switch(btn_mask) {
181   case(ESD_BTN_OK):
182     bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
183     break;
184   case(ESD_BTN_OK | ESD_BTN_CANCEL):
185     bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
186     break;
187   case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
188     bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
189     break;
190   case(ESD_BTNS_YES_NO_CANCEL):
191     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
192     break;
193   case(ESD_BTNS_SAVE_DONTSAVE_CANCEL):
194     bbox = dlg_button_row_new(GTK_STOCK_SAVE, WIRESHARK_STOCK_DONT_SAVE, GTK_STOCK_CANCEL, NULL);
195     break;
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);
198     break;
199   case(ESD_BTNS_YES_NO):
200     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, NULL);
201     break;
202   default:
203     g_assert_not_reached();
204     bbox = NULL;
205     break;
206   }
207   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
208   gtk_widget_show(bbox);
209
210   ok_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_OK);
211   if(ok_bt) {
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);
214   }
215
216   save_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_SAVE);
217   if (save_bt) {
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);
220   }
221
222   dont_save_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_DONT_SAVE);
223   if (dont_save_bt) {
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);
226   }
227
228   dont_save_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_QUIT_DONT_SAVE);
229   if (dont_save_bt) {
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);
232   }
233   bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLEAR);
234   if(bt) {
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);
237   }
238
239   yes_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_YES);
240   if(yes_bt) {
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);
243   }
244
245   bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_NO);
246   if(bt) {
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);
249   }
250
251   bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CANCEL);
252   if(bt) {
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);
255   }
256
257   if(!bt) {
258       if(yes_bt) {
259           window_set_cancel_button(win, yes_bt, simple_dialog_cancel_cb);
260       } else {
261           window_set_cancel_button(win, ok_bt, simple_dialog_cancel_cb);
262       }
263   }
264
265   dlg_button_focus_nth(bbox, 0);
266
267   gtk_widget_show(win);
268
269   return win;
270 }
271
272 void
273 display_queued_messages(void)
274 {
275   queued_message_t *queued_message;
276
277   while (message_queue != NULL) {
278     queued_message = message_queue->data;
279     message_queue = g_slist_remove(message_queue, queued_message);
280
281     display_simple_dialog(queued_message->type, queued_message->btn_mask,
282                           queued_message->message);
283
284     g_free(queued_message->message);
285     g_free(queued_message);
286   }
287 }
288
289 /* Simple dialog function - Displays a dialog box with the supplied message
290  * text.
291  *
292  * Args:
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
297  */
298
299 gpointer
300 vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap)
301 {
302   gchar             *message;
303   queued_message_t *queued_message;
304   GtkWidget        *win;
305   GdkWindowState state = 0;
306
307   /* Format the message. */
308   message = g_strdup_vprintf(msg_format, ap);
309
310   if (top_level != NULL) {
311     state = gdk_window_get_state(gtk_widget_get_window(top_level));
312   }
313
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) {
318
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);
324     return NULL;
325   }
326
327   /*
328    * Do we have any queued up messages?  If so, pop them up.
329    */
330   display_queued_messages();
331
332   win = display_simple_dialog(type, btn_mask, message);
333
334   g_free(message);
335
336   return win;
337 }
338
339 gpointer
340 simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...)
341 {
342   va_list ap;
343   gpointer ret;
344
345   va_start(ap, msg_format);
346   ret = vsimple_dialog(type, btn_mask, msg_format, ap);
347   va_end(ap);
348   return ret;
349 }
350
351 static void
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);
356
357   if (callback_fct)
358     (callback_fct) (win, button, data);
359
360     window_destroy(GTK_WIDGET(win));
361 }
362
363 void
364 simple_dialog_close(gpointer dialog)
365 {
366     window_destroy(GTK_WIDGET(dialog));
367 }
368
369 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
370 {
371
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);
374 }
375
376 void simple_dialog_check_set(gpointer dialog, gchar *text _U_) {
377     GtkWidget *ask_cb = g_object_get_data(G_OBJECT(dialog), CHECK_BUTTON);
378
379     gtk_button_set_label(GTK_BUTTON(ask_cb), text);
380     gtk_widget_show(ask_cb);
381 }
382
383 gboolean simple_dialog_check_get(gpointer dialog) {
384     GtkWidget *ask_cb = g_object_get_data(G_OBJECT(GTK_WIDGET(dialog)), CHECK_BUTTON);
385
386     return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ask_cb));
387 }
388
389 char *
390 simple_dialog_primary_start(void) {
391     return "<span weight=\"bold\" size=\"larger\">";
392 }
393
394 char *
395 simple_dialog_primary_end(void) {
396     return "</span>";
397 }
398
399 char *
400 simple_dialog_format_message(const char *msg)
401 {
402     char *str;
403
404     if (msg) {
405         str = xml_escape(msg);
406     } else {
407         str = NULL;
408     }
409     return str;
410 }