using GTK2 stock icons for dialogs instead of Ethereal specific ones
[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.18 2004/01/31 01:28:10 ulfl Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31
32 #include <stdio.h>
33
34 #ifdef NEED_SNPRINTF_H
35 # include "snprintf.h"
36 #endif
37
38 #include "gtkglobals.h"
39 #include "simple_dialog.h"
40 #include "dlg_utils.h"
41 #include "compat_macros.h"
42
43 #include "image/stock_dialog_error_48.xpm"
44 #include "image/stock_dialog_info_48.xpm"
45 #include "image/stock_dialog_question_48.xpm"
46 #include "image/stock_dialog_warning_48.xpm"
47
48 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
49
50 #define CALLBACK_FCT_KEY    "ESD_Callback_Fct"
51 #define CALLBACK_BTN_KEY    "ESD_Callback_Btn"
52 #define CALLBACK_DATA_KEY   "ESD_Callback_Data"
53
54 /* Simple dialog function - Displays a dialog box with the supplied message
55  * text.
56  *
57  * Args:
58  * type       : One of ESD_TYPE_*.
59  * btn_mask   : The value passed in determines which buttons are displayed.
60  * msg_format : Sprintf-style format of the text displayed in the dialog.
61  * ...        : Argument list for msg_format
62  *
63  */
64
65 #define ESD_MAX_MSG_LEN 2048
66 gpointer
67 simple_dialog(gint type, gint btn_mask, gchar *msg_format, ...) {
68   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
69               *bbox, *bt;
70   GdkPixmap   *pixmap;
71   GdkBitmap   *mask;
72   GtkStyle    *style;
73   GdkColormap *cmap;
74   va_list      ap;
75   gchar        message[ESD_MAX_MSG_LEN];
76   gchar      **icon;
77
78   /* Main window */
79   switch (type & ~ESD_TYPE_MODAL) {
80   case ESD_TYPE_WARN :
81     icon = stock_dialog_warning_48_xpm;
82     win = dlg_window_new("Ethereal: Warning");
83     break;
84   case ESD_TYPE_CRIT :
85     icon = stock_dialog_error_48_xpm;
86     win = dlg_window_new("Ethereal: Error");
87     break;
88   case ESD_TYPE_QUEST:
89     icon = stock_dialog_question_48_xpm;
90     win = dlg_window_new("Ethereal: Question");
91     break;
92   case ESD_TYPE_INFO :
93   default :
94     icon = stock_dialog_info_48_xpm;
95     win = dlg_window_new("Ethereal: Information");
96     break;
97   }
98
99   if (type & ESD_TYPE_MODAL)
100     gtk_window_set_modal(GTK_WINDOW(win), TRUE);
101
102   gtk_container_border_width(GTK_CONTAINER(win), 7);
103
104   /* Container for our rows */
105   main_vb = gtk_vbox_new(FALSE, 5);
106   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
107   gtk_container_add(GTK_CONTAINER(win), main_vb);
108   gtk_widget_show(main_vb);
109
110   /* Top row: Icon and message text */
111   top_hb = gtk_hbox_new(FALSE, 10);
112   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
113   gtk_widget_show(top_hb);
114
115   style = gtk_widget_get_style(win);
116   cmap  = gdk_colormap_get_system();
117   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
118     &style->bg[GTK_STATE_NORMAL], icon);
119   type_pm = gtk_pixmap_new(pixmap, mask);
120   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
121   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
122   gtk_widget_show(type_pm);
123
124   /* Load our vararg list into the message string */
125   va_start(ap, msg_format);
126   vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
127   va_end(ap);
128
129   msg_label = gtk_label_new(message);
130   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
131   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
132   gtk_widget_show(msg_label);
133
134   /* Button row */
135   switch(btn_mask) {
136   case(0):
137   case(ESD_BTN_OK):
138     bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
139     break;
140   case(ESD_BTN_OK | ESD_BTN_CANCEL):
141     bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
142     break;
143   case(ESD_BTN_YES | ESD_BTN_NO | ESD_BTN_CANCEL):
144     bbox = dlg_button_row_new(GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, NULL);
145     break;
146   default:
147     g_assert_not_reached();
148   }
149   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
150   gtk_widget_show(bbox);
151
152   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
153   if(bt) {
154       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_OK));
155       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
156       gtk_widget_grab_default(bt);
157     /* Catch the "key_press_event" signal in the window, so that we can catch
158        the ESC key being pressed and act as if the "OK" button had
159        been selected. */
160     dlg_set_cancel(win, bt);
161   }
162
163   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_YES);
164   if(bt) {
165       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_YES));
166       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
167   }
168
169   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_NO);
170   if(bt) {
171       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_NO));
172       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
173   }
174
175   bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
176   if(bt) {
177       OBJECT_SET_DATA(bt, CALLBACK_BTN_KEY, GINT_TO_POINTER(ESD_BTN_CANCEL));
178       SIGNAL_CONNECT(bt, "clicked", simple_dialog_cancel_cb, win);
179     /* Catch the "key_press_event" signal in the window, so that we can catch
180        the ESC key being pressed and act as if the "OK" button had
181        been selected. */
182       dlg_set_cancel(win, bt);
183       gtk_widget_grab_default(bt);
184   }
185
186   gtk_widget_show(win);
187
188   return win;
189 }
190
191 static void
192 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
193   gint button       = GPOINTER_TO_INT(    OBJECT_GET_DATA(w,   CALLBACK_BTN_KEY));
194   simple_dialog_cb_t    callback_fct    = OBJECT_GET_DATA(win, CALLBACK_FCT_KEY);
195   gpointer              data            = OBJECT_GET_DATA(win, CALLBACK_DATA_KEY);
196
197   gtk_widget_destroy(GTK_WIDGET(win));
198
199   if (callback_fct)
200     (callback_fct) (win, button, data);
201 }
202
203 void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data)
204 {
205
206     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_FCT_KEY, callback_fct);
207     OBJECT_SET_DATA(GTK_WIDGET(dialog), CALLBACK_DATA_KEY, data);
208 }