Added #include <sys/types.h> for compilation under gtk-1.1.11
[obnox/wireshark/wip.git] / util.c
1 /* util.c
2  * Utility routines
3  *
4  * $Id: util.c,v 1.10 1999/01/01 07:40:34 gram 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 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #include <glib.h>
35
36 #include <gtk/gtk.h>
37
38 #include <stdarg.h>
39 #include <strings.h>
40 #include <stdio.h>
41
42 #ifdef NEED_SNPRINTF_H
43 # ifdef HAVE_STDARG_H
44 #  include <stdarg.h>
45 # else
46 #  include <varargs.h>
47 # endif
48 # include "snprintf.h"
49 #endif
50
51 #include "packet.h"
52 #include "util.h"
53
54 #include "image/icon-excl.xpm"
55 #include "image/icon-ethereal.xpm"
56
57 const gchar *bm_key = "button mask";
58
59 /* Simple dialog function - Displays a dialog box with the supplied message
60  * text.
61  * 
62  * Args:
63  * type       : One of ESD_TYPE_*.
64  * btn_mask   : The address of a gint.  The value passed in determines if
65  *              the 'Cancel' button is displayed.  The button pressed by the 
66  *              user is passed back.
67  * msg_format : Sprintf-style format of the text displayed in the dialog.
68  * ...        : Argument list for msg_format
69  *
70  */
71  
72 #define ESD_MAX_MSG_LEN 1024
73 void
74 simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
75   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
76               *bbox, *ok_btn, *cancel_btn;
77   GdkPixmap   *pixmap;
78   GdkBitmap   *mask;
79   GtkStyle    *style;
80   GdkColormap *cmap;
81   va_list      ap;
82   gchar        message[ESD_MAX_MSG_LEN];
83   gchar      **icon;
84
85   /* Main window */
86   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
87   gtk_container_border_width(GTK_CONTAINER(win), 7);
88
89   switch (type) {
90   case ESD_TYPE_WARN :
91     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Warning");
92     icon = icon_excl_xpm;
93     break;
94   case ESD_TYPE_CRIT :
95     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Critical");
96     icon = icon_excl_xpm;
97     break;
98   case ESD_TYPE_INFO :
99   default :
100     icon = icon_ethereal_xpm;
101     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Information");
102     break;
103   }
104
105   gtk_object_set_data(GTK_OBJECT(win), bm_key, btn_mask);
106
107   /* Container for our rows */
108   main_vb = gtk_vbox_new(FALSE, 5);
109   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
110   gtk_container_add(GTK_CONTAINER(win), main_vb);
111   gtk_widget_show(main_vb);
112
113   /* Top row: Icon and message text */
114   top_hb = gtk_hbox_new(FALSE, 10);
115   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
116   gtk_widget_show(top_hb);
117   
118   style = gtk_widget_get_style(win);
119   cmap  = gdk_colormap_get_system();
120   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
121     &style->bg[GTK_STATE_NORMAL], icon);
122   type_pm = gtk_pixmap_new(pixmap, mask);
123   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
124   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
125   gtk_widget_show(type_pm);
126
127   /* Load our vararg list into the message string */
128   va_start(ap, msg_format);
129   vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
130
131   msg_label = gtk_label_new(message);
132   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
133   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
134   gtk_widget_show(msg_label);
135   
136   /* Button row */
137   bbox = gtk_hbutton_box_new();
138   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
139   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
140   gtk_widget_show(bbox);
141
142   ok_btn = gtk_button_new_with_label ("OK");
143   gtk_signal_connect_object(GTK_OBJECT(ok_btn), "clicked",
144     GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT (win)); 
145   gtk_container_add(GTK_CONTAINER(bbox), ok_btn);
146   GTK_WIDGET_SET_FLAGS(ok_btn, GTK_CAN_DEFAULT);
147   gtk_widget_grab_default(ok_btn);
148   gtk_widget_show(ok_btn);
149
150   if (btn_mask && *btn_mask == ESD_BTN_CANCEL) {
151     cancel_btn = gtk_button_new_with_label("Cancel");
152     gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
153       GTK_SIGNAL_FUNC(simple_dialog_cancel_cb), (gpointer) win);
154     gtk_container_add(GTK_CONTAINER(bbox), cancel_btn);
155     GTK_WIDGET_SET_FLAGS(cancel_btn, GTK_CAN_DEFAULT);
156     gtk_widget_show(cancel_btn);
157   }
158
159   if (btn_mask)
160     *btn_mask = ESD_BTN_OK;
161
162   gtk_widget_show(win);
163 }
164
165 void
166 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
167   gint *btn_mask = (gint *) gtk_object_get_data(win, bm_key);
168   
169   if (btn_mask)
170     *btn_mask = ESD_BTN_CANCEL;
171   gtk_widget_destroy(GTK_WIDGET(win));
172 }
173
174 /* Generate, into "buf", a string showing the bits of a bitfield.
175    Return a pointer to the character after that string. */
176 static char *
177 decode_bitfield_value(char *buf, guint32 val, guint32 mask, int width)
178 {
179   int i;
180   guint32 bit;
181   char *p;
182
183   i = 0;
184   p = buf;
185   bit = 1 << (width - 1);
186   for (;;) {
187     if (mask & bit) {
188       /* This bit is part of the field.  Show its value. */
189       if (val & bit)
190         *p++ = '1';
191       else
192         *p++ = '0';
193     } else {
194       /* This bit is not part of the field. */
195       *p++ = '.';
196     }
197     bit >>= 1;
198     i++;
199     if (i >= width)
200       break;
201     if (i % 4 == 0)
202       *p++ = ' ';
203   }
204   strcpy(p, " = ");
205   p += 3;
206   return p;
207 }
208
209 /* Generate a string describing a Boolean bitfield (a one-bit field that
210    says something is either true of false). */
211 const char *
212 decode_boolean_bitfield(guint32 val, guint32 mask, int width,
213     const char *truedesc, const char *falsedesc)
214 {
215   static char buf[1025];
216   char *p;
217
218   p = decode_bitfield_value(buf, val, mask, width);
219   if (val & mask)
220     strcpy(p, truedesc);
221   else
222     strcpy(p, falsedesc);
223   return buf;
224 }
225
226 /* Generate a string describing an enumerated bitfield (an N-bit field
227    with various specific values having particular names). */
228 const char *
229 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
230     const value_string *tab, const char *fmt)
231 {
232   static char buf[1025];
233   char *p;
234
235   p = decode_bitfield_value(buf, val, mask, width);
236   sprintf(p, fmt, val_to_str(val & mask, tab, "Unknown"));
237   return buf;
238 }
239
240 /* Generate a string describing a numeric bitfield (an N-bit field whose
241    value is just a number). */
242 const char *
243 decode_numeric_bitfield(guint32 val, guint32 mask, int width,
244     const char *fmt)
245 {
246   static char buf[1025];
247   char *p;
248
249   p = decode_bitfield_value(buf, val, mask, width);
250   sprintf(p, fmt, val & mask);
251   return buf;
252 }