Move the color-filter related stuff out of "color.h" into
[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 "ui_util.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
45 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
46
47 #define CALLBACK_FCT_KEY    "ESD_Callback_Fct"
48 #define CALLBACK_BTN_KEY    "ESD_Callback_Btn"
49 #define CALLBACK_DATA_KEY   "ESD_Callback_Data"
50
51 /*
52  * Queue for messages requested before we have a main window.
53  */
54 typedef struct {
55         gint    type;
56         gint    btn_mask;
57         char    *message;
58 } queued_message_t;
59         
60 static GSList *message_queue;
61
62 static GtkWidget *
63 display_simple_dialog(gint type, gint btn_mask, char *message)
64 {
65   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
66               *bbox, *ok_bt, *yes_bt, *bt, *save_bt, *dont_save_bt;
67   GdkPixmap   *pixmap;
68   GdkBitmap   *mask;
69   GtkStyle    *style;
70   GdkColormap *cmap;
71   gchar      **icon;
72
73   /* Main window */
74   switch (type) {
75   case ESD_TYPE_WARN :
76     icon = stock_dialog_warning_48_xpm;
77     break;
78   case ESD_TYPE_CONFIRMATION:
79     icon = stock_dialog_warning_48_xpm;
80     break;
81   case ESD_TYPE_ERROR:
82     icon = stock_dialog_error_48_xpm;
83     break;
84   case ESD_TYPE_INFO :
85   default :
86     icon = stock_dialog_info_48_xpm;
87     break;
88   }
89
90   /*
91    * The GNOME HIG:
92    *
93    *    http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-windows
94    *
95    * says that the title should be empty for alert boxes, so there's "less
96    * visual noise and confounding text."
97    *
98    * The Windows HIG:
99    *
100    *    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch09f.asp
101    *
102    * says it should
103    *
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
109    *    application name."
110    *
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).
115    *
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.
119    *
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.
123    */
124 #ifdef _WIN32
125   win = dlg_window_new("Ethereal");
126 #else
127   win = dlg_window_new("");
128 #endif
129
130   gtk_window_set_modal(GTK_WINDOW(win), TRUE);
131   gtk_container_border_width(GTK_CONTAINER(win), 6);
132
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);
137
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);
143
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);
152
153   msg_label = gtk_label_new(message);
154
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);
158 #endif
159
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);
164
165   /* Button row */
166   switch(btn_mask) {
167   case(ESD_BTN_OK):
168     bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
169     break;
170   case(ESD_BTN_OK | ESD_BTN_CANCEL):
171     bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
172     break;
173   case(ESD_BTN_CLEAR | ESD_BTN_CANCEL):
174     bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
175     break;
176   case(ESD_BTNS_YES_NO_CANCEL):
177     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
178     break;
179   case(ESD_BTNS_SAVE_DONTSAVE_CANCEL):
180     bbox = dlg_button_row_new(GTK_STOCK_SAVE, ETHEREAL_STOCK_DONT_SAVE, GTK_STOCK_CANCEL, NULL);
181     break;
182   case(ESD_BTNS_YES_NO):
183     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, NULL);
184     break;
185   default:
186     g_assert_not_reached();
187     bbox = NULL;
188     break;
189   }
190   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
191   gtk_widget_show(bbox);
192
193   ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
194   if(ok_bt) {
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);
197   }
198
199   save_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_SAVE);
200   if (save_bt) {
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);
203   }
204   
205   dont_save_bt = OBJECT_GET_DATA(bbox, ETHEREAL_STOCK_DONT_SAVE);
206   if (dont_save_bt) {
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);    
209   }      
210   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
211   if(bt) {
212       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
213       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
214   }
215
216   yes_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
217   if(yes_bt) {
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);
220   }
221
222   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
223   if(bt) {
224       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
225       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
226   }
227
228   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
229   if(bt) {
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);
232   }
233
234   if(!bt) {
235       if(yes_bt) {
236           window_set_cancel_button(win, yes_bt, simple_dialog_cancel_cb);
237       } else {
238           window_set_cancel_button(win, ok_bt, simple_dialog_cancel_cb);
239       }
240   }
241
242   gtk_widget_show(win);
243
244   return win;
245 }
246
247 void
248 display_queued_messages(void)
249 {
250   queued_message_t *queued_message;
251
252   while (message_queue != NULL) {
253     queued_message = message_queue->data;
254     message_queue = g_slist_remove(message_queue, queued_message);
255
256     display_simple_dialog(queued_message->type, queued_message->btn_mask,
257                           queued_message->message);
258
259     g_free(queued_message->message);
260     g_free(queued_message);
261   }
262 }
263
264 /* Simple dialog function - Displays a dialog box with the supplied message
265  * text.
266  *
267  * Args:
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
272  */
273
274 gpointer
275 vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap)
276 {
277   gchar             *vmessage;
278   gchar             *message;
279   queued_message_t *queued_message;
280   GtkWidget        *win;
281 #if GTK_MAJOR_VERSION >= 2
282   GdkWindowState state = 0;
283 #endif
284
285   /* Format the message. */
286   vmessage = g_strdup_vprintf(msg_format, ap);
287
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);
291   g_free(vmessage);
292 #else
293   message = vmessage;
294 #endif
295
296 #if GTK_MAJOR_VERSION >= 2
297   if (top_level != NULL) {
298     state = gdk_window_get_state(top_level->window);
299   }
300
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) {
305 #else
306
307   /* If we don't yet have a main window, queue up the message for later
308      display. */
309   if (top_level == NULL) {
310 #endif
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);
316     return NULL;
317   }
318
319   /*
320    * Do we have any queued up messages?  If so, pop them up.
321    */
322   display_queued_messages();
323
324   win = display_simple_dialog(type, btn_mask, message);
325
326   g_free(message);
327
328   return win;
329 }
330
331 gpointer
332 simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...)
333 {
334   va_list ap;
335   gpointer ret;
336
337   va_start(ap, msg_format);
338   ret = vsimple_dialog(type, btn_mask, msg_format, ap);
339   va_end(ap);
340   return ret;
341 }
342
343 static void
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);
348
349   window_destroy(GTK_WIDGET(win));
350
351   if (callback_fct)
352     (callback_fct) (win, button, data);
353 }
354
355 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
356 {
357
358     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
359     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
360 }
361
362 char *
363 simple_dialog_primary_start(void) {
364     return PRIMARY_TEXT_START;
365 }
366
367 char *
368 simple_dialog_primary_end(void) {
369     return PRIMARY_TEXT_END;
370 }
371
372 char *
373 simple_dialog_format_message(const char *msg)
374 {
375     char *str;
376
377     if (msg) {
378 #if GTK_MAJOR_VERSION < 2
379         str = g_strdup(msg);
380 #else
381         str = xml_escape(msg);
382 #endif
383     } else {
384         str = NULL;
385     }
386     return str;
387 }