fix #568: disable event "activate" handler for the range entry field. see the added...
[obnox/wireshark/wip.git] / gtk / simple_dialog.c
1 /* simple_dialog.c
2  * Simple message dialog box routines.
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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
29 #include <gtk/gtk.h>
30
31 #include <stdio.h>
32
33 #include "gtkglobals.h"
34 #include "simple_dialog.h"
35 #include "dlg_utils.h"
36 #include "gui_utils.h"
37 #include "compat_macros.h"
38
39 #include <epan/strutil.h>
40
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"
45
46 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
47
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"
52
53 /*
54  * Queue for messages requested before we have a main window.
55  */
56 typedef struct {
57         gint    type;
58         gint    btn_mask;
59         char    *message;
60 } queued_message_t;
61         
62 static GSList *message_queue;
63
64 static GtkWidget *
65 display_simple_dialog(gint type, gint btn_mask, char *message)
66 {
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;
69   GdkPixmap   *pixmap;
70   GdkBitmap   *mask;
71   GtkStyle    *style;
72   GdkColormap *cmap;
73   const gchar **icon;
74
75   /* Main window */
76   switch (type) {
77   case ESD_TYPE_WARN :
78     icon = stock_dialog_warning_48_xpm;
79     break;
80   case ESD_TYPE_CONFIRMATION:
81     icon = stock_dialog_warning_48_xpm;
82     break;
83   case ESD_TYPE_ERROR:
84     icon = stock_dialog_error_48_xpm;
85     break;
86   case ESD_TYPE_STOP :
87     icon = stock_dialog_stop_48_xpm;
88     break;
89   case ESD_TYPE_INFO :
90   default :
91     icon = stock_dialog_info_48_xpm;
92     break;
93   }
94
95   /*
96    * The GNOME HIG:
97    *
98    *    http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
99    *
100    * says that the title should be empty for alert boxes, so there's "less
101    * visual noise and confounding text."
102    *
103    * The Windows HIG:
104    *
105    *    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
106    *
107    * says it should
108    *
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
114    *    application name."
115    *
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).
120    *
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.
124    *
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.
128    */
129   if(btn_mask == ESD_BTN_NONE) {
130         win = splash_window_new();
131   } else {
132 #ifdef _WIN32
133         win = dlg_window_new("Ethereal");
134 #else
135     win = dlg_window_new("");
136 #endif
137   }
138
139   gtk_window_set_modal(GTK_WINDOW(win), TRUE);
140   gtk_container_border_width(GTK_CONTAINER(win), 6);
141
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);
146
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);
152
153   /* icon */
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);
162
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);
168
169   /* message */
170   msg_label = gtk_label_new(message);
171
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);
175 #endif
176
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);
181
182   if(btn_mask == ESD_BTN_NONE) {
183         gtk_widget_show(win);
184         return win;
185   }
186
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);
191
192   /* Button row */
193   switch(btn_mask) {
194   case(ESD_BTN_OK):
195     bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
196     break;
197   case(ESD_BTN_OK | ESD_BTN_CANCEL):
198     bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
199     break;
200   case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
201     bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
202     break;
203   case(ESD_BTNS_YES_NO_CANCEL):
204     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
205     break;
206   case(ESD_BTNS_SAVE_DONTSAVE_CANCEL):
207     bbox = dlg_button_row_new(GTK_STOCK_SAVE, ETHEREAL_STOCK_DONT_SAVE, GTK_STOCK_CANCEL, NULL);
208     break;
209   case(ESD_BTNS_YES_NO):
210     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, NULL);
211     break;
212   default:
213     g_assert_not_reached();
214     bbox = NULL;
215     break;
216   }
217   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
218   gtk_widget_show(bbox);
219
220   ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
221   if(ok_bt) {
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);
224   }
225
226   save_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_SAVE);
227   if (save_bt) {
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);
230   }
231   
232   dont_save_bt = OBJECT_GET_DATA(bbox, ETHEREAL_STOCK_DONT_SAVE);
233   if (dont_save_bt) {
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);    
236   }      
237   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
238   if(bt) {
239       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
240       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
241   }
242
243   yes_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
244   if(yes_bt) {
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);
247   }
248
249   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
250   if(bt) {
251       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
252       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
253   }
254
255   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
256   if(bt) {
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);
259   }
260
261   if(!bt) {
262       if(yes_bt) {
263           window_set_cancel_button(win, yes_bt, simple_dialog_cancel_cb);
264       } else {
265           window_set_cancel_button(win, ok_bt, simple_dialog_cancel_cb);
266       }
267   }
268
269   gtk_widget_show(win);
270
271   return win;
272 }
273
274 void
275 display_queued_messages(void)
276 {
277   queued_message_t *queued_message;
278
279   while (message_queue != NULL) {
280     queued_message = message_queue->data;
281     message_queue = g_slist_remove(message_queue, queued_message);
282
283     display_simple_dialog(queued_message->type, queued_message->btn_mask,
284                           queued_message->message);
285
286     g_free(queued_message->message);
287     g_free(queued_message);
288   }
289 }
290
291 /* Simple dialog function - Displays a dialog box with the supplied message
292  * text.
293  *
294  * Args:
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
299  */
300
301 gpointer
302 vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap)
303 {
304   gchar             *vmessage;
305   gchar             *message;
306   queued_message_t *queued_message;
307   GtkWidget        *win;
308 #if GTK_MAJOR_VERSION >= 2
309   GdkWindowState state = 0;
310 #endif
311
312   /* Format the message. */
313   vmessage = g_strdup_vprintf(msg_format, ap);
314
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);
318   g_free(vmessage);
319 #else
320   message = vmessage;
321 #endif
322
323 #if GTK_MAJOR_VERSION >= 2
324   if (top_level != NULL) {
325     state = gdk_window_get_state(top_level->window);
326   }
327
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) {
332 #else
333
334   /* If we don't yet have a main window, queue up the message for later
335      display. */
336   if (top_level == NULL) {
337 #endif
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);
343     return NULL;
344   }
345
346   /*
347    * Do we have any queued up messages?  If so, pop them up.
348    */
349   display_queued_messages();
350
351   win = display_simple_dialog(type, btn_mask, message);
352
353   g_free(message);
354
355   return win;
356 }
357
358 gpointer
359 simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...)
360 {
361   va_list ap;
362   gpointer ret;
363
364   va_start(ap, msg_format);
365   ret = vsimple_dialog(type, btn_mask, msg_format, ap);
366   va_end(ap);
367   return ret;
368 }
369
370 static void
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);
375
376   window_destroy(GTK_WIDGET(win));
377
378   if (callback_fct)
379     (callback_fct) (win, button, data);
380 }
381
382 void
383 simple_dialog_close(gpointer dialog)
384 {
385         window_destroy(GTK_WIDGET(dialog));
386 }
387
388 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
389 {
390
391     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
392     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
393 }
394
395 void simple_dialog_check_set(gpointer dialog, gchar *text) {
396     GtkWidget *ask_cb = OBJECT_GET_DATA(dialog, CHECK_BUTTON);
397
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);
401 #endif
402     gtk_widget_show(ask_cb);
403 }
404
405 gboolean simple_dialog_check_get(gpointer dialog) {
406     GtkWidget *ask_cb = OBJECT_GET_DATA(dialog, CHECK_BUTTON);
407
408     return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ask_cb));
409 }
410
411 char *
412 simple_dialog_primary_start(void) {
413     return PRIMARY_TEXT_START;
414 }
415
416 char *
417 simple_dialog_primary_end(void) {
418     return PRIMARY_TEXT_END;
419 }
420
421 char *
422 simple_dialog_format_message(const char *msg)
423 {
424     char *str;
425
426     if (msg) {
427 #if GTK_MAJOR_VERSION < 2
428         str = g_strdup(msg);
429 #else
430         str = xml_escape(msg);
431 #endif
432     } else {
433         str = NULL;
434     }
435     return str;
436 }