add implementation of button mask ESD_BTNS_YES_NO
[obnox/wireshark/wip.git] / gtk / simple_dialog.c
1 /* simple_dialog.c
2  * Simple message dialog box routines.
3  *
4  * $Id: simple_dialog.c,v 1.37 2004/06/17 21:34:12 ulfl Exp $
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;
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_CLEAR | ESD_BTN_CANCEL):
171     bbox = dlg_button_row_new(GTK_STOCK_CLEAR, GTK_STOCK_CANCEL, NULL);
172     break;
173   case(ESD_BTNS_YES_NO_CANCEL):
174     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
175     break;
176   case(ESD_BTNS_YES_NO):
177     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, NULL);
178     break;
179   default:
180     g_assert_not_reached();
181     bbox = NULL;
182     break;
183   }
184   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
185   gtk_widget_show(bbox);
186
187   ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
188   if(ok_bt) {
189       OBJECT_SET_DATA(ok_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
190       SIGNAL_CONNECT(ok_bt, "clicked", simple_dialog_cancel_cb, win);
191   }
192
193   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLEAR);
194   if(bt) {
195       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CLEAR));
196       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
197   }
198
199   yes_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
200   if(yes_bt) {
201       OBJECT_SET_DATA(yes_bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
202       SIGNAL_CONNECT(yes_bt, "clicked", simple_dialog_cancel_cb, win);
203   }
204
205   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
206   if(bt) {
207       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
208       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
209   }
210
211   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
212   if(bt) {
213       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
214       window_set_cancel_button(win, bt, simple_dialog_cancel_cb);
215   }
216
217   if(!bt) {
218       if(yes_bt) {
219           window_set_cancel_button(win, yes_bt, simple_dialog_cancel_cb);
220       } else {
221           window_set_cancel_button(win, ok_bt, simple_dialog_cancel_cb);
222       }
223   }
224
225   gtk_widget_show(win);
226
227   return win;
228 }
229
230 void
231 display_queued_messages(void)
232 {
233   queued_message_t *queued_message;
234
235   while (message_queue != NULL) {
236     queued_message = message_queue->data;
237     message_queue = g_slist_remove(message_queue, queued_message);
238
239     display_simple_dialog(queued_message->type, queued_message->btn_mask,
240                           queued_message->message);
241
242     g_free(queued_message->message);
243     g_free(queued_message);
244   }
245 }
246
247 /* Simple dialog function - Displays a dialog box with the supplied message
248  * text.
249  *
250  * Args:
251  * type       : One of ESD_TYPE_*.
252  * btn_mask   : The value passed in determines which buttons are displayed.
253  * msg_format : Sprintf-style format of the text displayed in the dialog.
254  * ...        : Argument list for msg_format
255  */
256
257 gpointer
258 vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap)
259 {
260   gchar             *message;
261   queued_message_t *queued_message;
262   GtkWidget        *win;
263 #if GTK_MAJOR_VERSION >= 2
264   GdkWindowState state = 0;
265 #endif
266
267   /* Format the message. */
268   message = g_strdup_vprintf(msg_format, ap);
269
270 #if GTK_MAJOR_VERSION >= 2
271   if (top_level != NULL) {
272     state = gdk_window_get_state(top_level->window);
273   }
274
275   /* If we don't yet have a main window or it's iconified, don't show the 
276      dialog. If showing up a dialog, while main window is iconified, program 
277      will become unresponsive! */     
278   if (top_level == NULL || state & GDK_WINDOW_STATE_ICONIFIED) {
279 #else
280
281   /* If we don't yet have a main window, queue up the message for later
282      display. */
283   if (top_level == NULL) {
284 #endif
285     queued_message = g_malloc(sizeof (queued_message_t));
286     queued_message->type = type;
287     queued_message->btn_mask = btn_mask;
288     queued_message->message = message;
289     message_queue = g_slist_append(message_queue, queued_message);
290     return NULL;
291   }
292
293   /*
294    * Do we have any queued up messages?  If so, pop them up.
295    */
296   display_queued_messages();
297
298   win = display_simple_dialog(type, btn_mask, message);
299
300   g_free(message);
301
302   return win;
303 }
304
305 gpointer
306 simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...)
307 {
308   va_list ap;
309   gpointer ret;
310
311   va_start(ap, msg_format);
312   ret = vsimple_dialog(type, btn_mask, msg_format, ap);
313   va_end(ap);
314   return ret;
315 }
316
317 static void
318 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
319   gint button       = GPOINTER_TO_INT(    OBJECT_GET_DATA(w,   CALLBACK_BTN_KEY));
320   simple_dialog_cb_t    callback_fct    = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
321   gpointer              data            = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
322
323   window_destroy(GTK_WIDGET(win));
324
325   if (callback_fct)
326     (callback_fct) (win, button, data);
327 }
328
329 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
330 {
331
332     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
333     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
334 }
335
336 char *
337 simple_dialog_primary_start(void) {
338     return PRIMARY_TEXT_START;
339 }
340
341 char *
342 simple_dialog_primary_end(void) {
343     return PRIMARY_TEXT_END;
344 }
345
346 char *
347 simple_dialog_format_message(const char *msg)
348 {
349     char *str;
350
351     if (msg) {
352 #if GTK_MAJOR_VERSION < 2
353         str = g_strdup(msg);
354 #else
355         str = xml_escape(msg);
356 #endif
357     } else {
358         str = NULL;
359     }
360     return str;
361 }