Have the per-capture-file-type open routines "wtap_open_offline()" calls
[obnox/wireshark/wip.git] / display.c
1 /* display.c
2  * Routines for packet display windows
3  *
4  * $Id: display.c,v 1.9 1999/07/13 02:52:48 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
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <gtk/gtk.h>
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include <time.h>
46
47 #ifdef HAVE_SYS_SOCKET_H
48 #include <sys/socket.h>
49 #endif
50
51 #ifdef HAVE_WINSOCK_H
52 #include <winsock.h>
53 #endif
54
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
57 #endif
58
59 #ifdef HAVE_NET_IF_H
60 #include <net/if.h>
61 #endif
62
63 #include <signal.h>
64 #include <errno.h>
65
66 #ifdef NEED_SNPRINTF_H
67 # ifdef HAVE_STDARG_H
68 #  include <stdarg.h>
69 # else
70 #  include <varargs.h>
71 # endif
72 # include "snprintf.h"
73 #endif
74
75 #ifdef HAVE_SYS_SOCKIO_H
76 # include <sys/sockio.h>
77 #endif
78
79 #include "timestamp.h"
80 #include "packet.h"
81 #include "file.h"
82 #include "display.h"
83
84 extern capture_file  cf;
85 extern GtkWidget *packet_list;
86
87 /* Display callback data keys */
88 #define E_DISPLAY_TIME_ABS_KEY   "display_time_abs"
89 #define E_DISPLAY_TIME_REL_KEY   "display_time_rel"
90 #define E_DISPLAY_TIME_DELTA_KEY "display_time_delta"
91
92 static void display_opt_ok_cb(GtkWidget *, gpointer);
93 static void display_opt_apply_cb(GtkWidget *, gpointer);
94 static void display_opt_close_cb(GtkWidget *, gpointer);
95
96 /*
97  * Keep track of whether the "Display Options" window is active, so that,
98  * if it is, selecting "Display/Options" doesn't pop up another such
99  * window.
100  */
101 static int display_opt_window_active;
102 static ts_type prev_timestamp_type;
103
104 void
105 display_opt_cb(GtkWidget *w, gpointer d) {
106   GtkWidget     *display_opt_w, *button, *main_vb, *bbox, *ok_bt, *apply_bt, *cancel_bt;
107
108   /* If there's already a "Display Options" window active, don't pop
109      up another one.
110
111      XXX - this should arguably give the input focus to the active
112      "Display Options" window, if possible. */
113   if (display_opt_window_active)
114     return;
115
116   /* Save the current timestamp type, so that "Cancel" can put it back
117      if we've changed it with "Apply". */
118   prev_timestamp_type = timestamp_type;
119
120   display_opt_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
121   gtk_window_set_title(GTK_WINDOW(display_opt_w), "Ethereal: Display Options");
122   
123   /* Container for each row of widgets */
124   main_vb = gtk_vbox_new(FALSE, 3);
125   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
126   gtk_container_add(GTK_CONTAINER(display_opt_w), main_vb);
127   gtk_widget_show(main_vb);
128   
129   button = gtk_radio_button_new_with_label(NULL, "Time of day");
130   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button),
131                (timestamp_type == ABSOLUTE));
132   gtk_object_set_data(GTK_OBJECT(display_opt_w), E_DISPLAY_TIME_ABS_KEY,
133                button);
134   gtk_box_pack_start(GTK_BOX(main_vb), button, TRUE, TRUE, 0);
135   gtk_widget_show(button);
136
137   button = gtk_radio_button_new_with_label(
138                gtk_radio_button_group(GTK_RADIO_BUTTON(button)),
139                "Seconds since beginning of capture");
140   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button),
141                (timestamp_type == RELATIVE));
142   gtk_object_set_data(GTK_OBJECT(display_opt_w), E_DISPLAY_TIME_REL_KEY,
143                button);
144   gtk_box_pack_start(GTK_BOX(main_vb), button, TRUE, TRUE, 0);
145   gtk_widget_show(button);
146
147   button = gtk_radio_button_new_with_label(
148                gtk_radio_button_group(GTK_RADIO_BUTTON(button)),
149                "Seconds since previous frame");
150   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button),
151                (timestamp_type == DELTA));
152   gtk_object_set_data(GTK_OBJECT(display_opt_w), E_DISPLAY_TIME_DELTA_KEY,
153                 button);
154   gtk_box_pack_start(GTK_BOX(main_vb), button, TRUE, TRUE, 0);
155   gtk_widget_show(button);
156   
157   /* Button row: OK, Apply, and Cancel buttons */
158   bbox = gtk_hbutton_box_new();
159   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
160   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
161   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
162   gtk_widget_show(bbox);
163
164   ok_bt = gtk_button_new_with_label ("OK");
165   gtk_signal_connect(GTK_OBJECT(ok_bt), "clicked",
166     GTK_SIGNAL_FUNC(display_opt_ok_cb), GTK_OBJECT(display_opt_w));
167   GTK_WIDGET_SET_FLAGS(ok_bt, GTK_CAN_DEFAULT);
168   gtk_box_pack_start (GTK_BOX (bbox), ok_bt, TRUE, TRUE, 0);
169   gtk_widget_grab_default(ok_bt);
170   gtk_widget_show(ok_bt);
171
172   apply_bt = gtk_button_new_with_label ("Apply");
173   gtk_signal_connect(GTK_OBJECT(apply_bt), "clicked",
174     GTK_SIGNAL_FUNC(display_opt_apply_cb), GTK_OBJECT(display_opt_w));
175   GTK_WIDGET_SET_FLAGS(apply_bt, GTK_CAN_DEFAULT);
176   gtk_box_pack_start (GTK_BOX (bbox), apply_bt, TRUE, TRUE, 0);
177   gtk_widget_show(apply_bt);
178
179   cancel_bt = gtk_button_new_with_label ("Cancel");
180   gtk_signal_connect(GTK_OBJECT(cancel_bt), "clicked",
181     GTK_SIGNAL_FUNC(display_opt_close_cb), GTK_OBJECT(display_opt_w));
182   GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
183   gtk_box_pack_start (GTK_BOX (bbox), cancel_bt, TRUE, TRUE, 0);
184   gtk_widget_show(cancel_bt);
185
186   display_opt_window_active = TRUE;
187   gtk_widget_show(display_opt_w);
188 }
189
190 static void
191 display_opt_ok_cb(GtkWidget *ok_bt, gpointer parent_w) {
192   GtkWidget *button;
193
194   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w),
195                                               E_DISPLAY_TIME_ABS_KEY);
196   if (GTK_TOGGLE_BUTTON (button)->active)
197     timestamp_type = ABSOLUTE;
198
199   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w),
200                                               E_DISPLAY_TIME_REL_KEY);
201   if (GTK_TOGGLE_BUTTON (button)->active)
202     timestamp_type = RELATIVE;
203
204   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w),
205                                               E_DISPLAY_TIME_DELTA_KEY);
206   if (GTK_TOGGLE_BUTTON (button)->active)
207     timestamp_type = DELTA;
208
209   gtk_widget_destroy(GTK_WIDGET(parent_w));
210   display_opt_window_active = FALSE;
211
212   change_time_formats(&cf);
213 }
214
215 static void
216 display_opt_apply_cb(GtkWidget *ok_bt, gpointer parent_w) {
217   GtkWidget *button;
218
219   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w),
220                                               E_DISPLAY_TIME_ABS_KEY);
221   if (GTK_TOGGLE_BUTTON (button)->active)
222     timestamp_type = ABSOLUTE;
223
224   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w),
225                                               E_DISPLAY_TIME_REL_KEY);
226   if (GTK_TOGGLE_BUTTON (button)->active)
227     timestamp_type = RELATIVE;
228
229   button = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w),
230                                               E_DISPLAY_TIME_DELTA_KEY);
231   if (GTK_TOGGLE_BUTTON (button)->active)
232     timestamp_type = DELTA;
233
234   change_time_formats(&cf);
235 }
236
237 static void
238 display_opt_close_cb(GtkWidget *close_bt, gpointer parent_w) {
239
240   if (timestamp_type != prev_timestamp_type) {
241     timestamp_type = prev_timestamp_type;
242     change_time_formats(&cf);
243   }
244
245   gtk_grab_remove(GTK_WIDGET(parent_w));
246   gtk_widget_destroy(GTK_WIDGET(parent_w));
247   display_opt_window_active = FALSE;
248 }