Pick up from Ethereal's "configure.in" the test to check whether we're
[obnox/wireshark/wip.git] / util.c
1 /* util.c
2  * Utility routines
3  *
4  * $Id: util.c,v 1.7 1998/10/28 21:22:33 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 <strings.h>
36
37 #ifdef NEED_SNPRINTF_H
38 # ifdef HAVE_STDARG_H
39 #  include <stdarg.h>
40 # else
41 #  include <varargs.h>
42 # endif
43 # include "snprintf.h"
44 #endif
45
46 #include "util.h"
47
48 #include "image/icon-excl.xpm"
49 #include "image/icon-ethereal.xpm"
50
51 const gchar *bm_key = "button mask";
52
53 /* Simple dialog function - Displays a dialog box with the supplied message
54  * text.
55  * 
56  * Args:
57  * type       : One of ESD_TYPE_*.
58  * btn_mask   : The address of a gint.  The value passed in determines if
59  *              the 'Cancel' button is displayed.  The button pressed by the 
60  *              user is passed back.
61  * msg_format : Sprintf-style format of the text displayed in the dialog.
62  * ...        : Argument list for msg_format
63  *
64  */
65  
66 #define ESD_MAX_MSG_LEN 1024
67 void
68 simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
69   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
70               *bbox, *ok_btn, *cancel_btn;
71   GdkPixmap   *pixmap;
72   GdkBitmap   *mask;
73   GtkStyle    *style;
74   GdkColormap *cmap;
75   va_list      ap;
76   gchar        message[ESD_MAX_MSG_LEN];
77   gchar      **icon;
78
79   /* Main window */
80   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
81   gtk_container_border_width(GTK_CONTAINER(win), 7);
82
83   switch (type) {
84   case ESD_TYPE_WARN :
85     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Warning");
86     icon = icon_excl_xpm;
87     break;
88   case ESD_TYPE_CRIT :
89     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Critical");
90     icon = icon_excl_xpm;
91     break;
92   case ESD_TYPE_INFO :
93   default :
94     icon = icon_ethereal_xpm;
95     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Information");
96     break;
97   }
98
99   gtk_object_set_data(GTK_OBJECT(win), bm_key, btn_mask);
100
101   /* Container for our rows */
102   main_vb = gtk_vbox_new(FALSE, 5);
103   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
104   gtk_container_add(GTK_CONTAINER(win), main_vb);
105   gtk_widget_show(main_vb);
106
107   /* Top row: Icon and message text */
108   top_hb = gtk_hbox_new(FALSE, 10);
109   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
110   gtk_widget_show(top_hb);
111   
112   style = gtk_widget_get_style(win);
113   cmap  = gdk_colormap_get_system();
114   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
115     &style->bg[GTK_STATE_NORMAL], icon);
116   type_pm = gtk_pixmap_new(pixmap, mask);
117   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
118   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
119   gtk_widget_show(type_pm);
120
121   /* Load our vararg list into the message string */
122   va_start(ap, msg_format);
123   vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
124
125   msg_label = gtk_label_new(message);
126   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
127   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
128   gtk_widget_show(msg_label);
129   
130   /* Button row */
131   bbox = gtk_hbutton_box_new();
132   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
133   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
134   gtk_widget_show(bbox);
135
136   ok_btn = gtk_button_new_with_label ("OK");
137   gtk_signal_connect_object(GTK_OBJECT(ok_btn), "clicked",
138     GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT (win)); 
139   gtk_container_add(GTK_CONTAINER(bbox), ok_btn);
140   GTK_WIDGET_SET_FLAGS(ok_btn, GTK_CAN_DEFAULT);
141   gtk_widget_grab_default(ok_btn);
142   gtk_widget_show(ok_btn);
143
144   if (btn_mask && *btn_mask == ESD_BTN_CANCEL) {
145     cancel_btn = gtk_button_new_with_label("Cancel");
146     gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
147       GTK_SIGNAL_FUNC(simple_dialog_cancel_cb), (gpointer) win);
148     gtk_container_add(GTK_CONTAINER(bbox), cancel_btn);
149     GTK_WIDGET_SET_FLAGS(cancel_btn, GTK_CAN_DEFAULT);
150     gtk_widget_show(cancel_btn);
151   }
152
153   if (btn_mask)
154     *btn_mask = ESD_BTN_OK;
155
156   gtk_widget_show(win);
157 }
158
159 void
160 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
161   gint *btn_mask = (gint *) gtk_object_get_data(win, bm_key);
162   
163   if (btn_mask)
164     *btn_mask = ESD_BTN_CANCEL;
165   gtk_widget_destroy(GTK_WIDGET(win));
166 }