Go nuts with the 3D logo.
[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.8 2001/12/12 21:38:59 gerald 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 <glib.h>
31
32 #include <gtk/gtk.h>
33
34 #include <stdarg.h>
35 #include <stdio.h>
36
37 #ifdef NEED_SNPRINTF_H
38 # include "snprintf.h"
39 #endif
40
41 #include "gtkglobals.h"
42 #include "simple_dialog.h"
43 #include "dlg_utils.h"
44
45 #include "image/eexcl3d64.xpm"
46 #include "image/eicon3d64.xpm"
47
48 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
49
50 static const gchar bm_key[] = "button mask";
51
52 /* Simple dialog function - Displays a dialog box with the supplied message
53  * text.
54  * 
55  * Args:
56  * type       : One of ESD_TYPE_*.
57  * btn_mask   : The address of a gint.  The value passed in determines if
58  *              the 'Cancel' button is displayed.  The button pressed by the 
59  *              user is passed back.
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 void
67 simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
68   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
69               *bbox, *ok_btn, *cancel_btn;
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 = eexcl3d64_xpm;
82     win = dlg_window_new("Ethereal: Warning");
83     break;
84   case ESD_TYPE_CRIT :
85     icon = eexcl3d64_xpm;
86     win = dlg_window_new("Ethereal: Error");
87     break;
88   case ESD_TYPE_INFO :
89   default :
90     icon = eicon3d64_xpm;
91     win = dlg_window_new("Ethereal: Information");
92     break;
93   }
94
95   if (type & ESD_TYPE_MODAL)
96     gtk_window_set_modal(GTK_WINDOW(win), TRUE);
97
98   gtk_container_border_width(GTK_CONTAINER(win), 7);
99
100   gtk_object_set_data(GTK_OBJECT(win), bm_key, btn_mask);
101
102   /* Container for our rows */
103   main_vb = gtk_vbox_new(FALSE, 5);
104   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
105   gtk_container_add(GTK_CONTAINER(win), main_vb);
106   gtk_widget_show(main_vb);
107
108   /* Top row: Icon and message text */
109   top_hb = gtk_hbox_new(FALSE, 10);
110   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
111   gtk_widget_show(top_hb);
112   
113   style = gtk_widget_get_style(win);
114   cmap  = gdk_colormap_get_system();
115   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
116     &style->bg[GTK_STATE_NORMAL], icon);
117   type_pm = gtk_pixmap_new(pixmap, mask);
118   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
119   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
120   gtk_widget_show(type_pm);
121
122   /* Load our vararg list into the message string */
123   va_start(ap, msg_format);
124   vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
125
126   msg_label = gtk_label_new(message);
127   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
128   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
129   gtk_widget_show(msg_label);
130   
131   /* Button row */
132   bbox = gtk_hbutton_box_new();
133   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
134   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
135   gtk_widget_show(bbox);
136
137   ok_btn = gtk_button_new_with_label ("OK");
138   gtk_signal_connect_object(GTK_OBJECT(ok_btn), "clicked",
139     GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT (win)); 
140   gtk_container_add(GTK_CONTAINER(bbox), ok_btn);
141   GTK_WIDGET_SET_FLAGS(ok_btn, GTK_CAN_DEFAULT);
142   gtk_widget_grab_default(ok_btn);
143   gtk_widget_show(ok_btn);
144
145   if (btn_mask && *btn_mask == ESD_BTN_CANCEL) {
146     cancel_btn = gtk_button_new_with_label("Cancel");
147     gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
148       GTK_SIGNAL_FUNC(simple_dialog_cancel_cb), (gpointer) win);
149     gtk_container_add(GTK_CONTAINER(bbox), cancel_btn);
150     GTK_WIDGET_SET_FLAGS(cancel_btn, GTK_CAN_DEFAULT);
151     gtk_widget_show(cancel_btn);
152
153     /* Catch the "key_press_event" signal in the window, so that we can catch
154        the ESC key being pressed and act as if the "Cancel" button had
155        been selected. */
156     dlg_set_cancel(win, cancel_btn);
157   } else {
158     /* Catch the "key_press_event" signal in the window, so that we can catch
159        the ESC key being pressed and act as if the "OK" button had
160        been selected. */
161     dlg_set_cancel(win, ok_btn);
162   }
163
164   if (btn_mask)
165     *btn_mask = ESD_BTN_OK;
166
167   gtk_widget_show(win);
168 }
169
170 static void
171 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
172   gint *btn_mask = (gint *) gtk_object_get_data(win, bm_key);
173   
174   if (btn_mask)
175     *btn_mask = ESD_BTN_CANCEL;
176   gtk_widget_destroy(GTK_WIDGET(win));
177 }