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