Damn the torpedos[1], commit it anyway.
[obnox/wireshark/wip.git] / gtk / goto_dlg.c
1 /* goto_dlg.c
2  * Routines for "go to frame" window
3  *
4  * $Id: goto_dlg.c,v 1.12 2001/01/04 04:54:14 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 #include <gtk/gtk.h>
32
33 #include <stdlib.h>
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #include <glib.h>
40
41 #include "proto.h"
42 #include "globals.h"
43
44 #include "goto_dlg.h"
45 #include "prefs_dlg.h"
46 #include "simple_dialog.h"
47 #include "dlg_utils.h"
48
49 /* Capture callback data keys */
50 #define E_GOTO_FNUMBER_KEY     "goto_fnumber_te"
51
52 static void
53 goto_frame_ok_cb(GtkWidget *ok_bt, gpointer parent_w);
54
55 static void
56 goto_frame_close_cb(GtkWidget *close_bt, gpointer parent_w);
57
58 void
59 goto_frame_cb(GtkWidget *w, gpointer d)
60 {
61   GtkWidget     *goto_frame_w, *main_vb, *fnumber_hb, *fnumber_lb, *fnumber_te,
62                 *bbox, *ok_bt, *cancel_bt;
63
64   goto_frame_w = dlg_window_new("Ethereal: Go To Frame");
65   
66   /* Container for each row of widgets */
67   main_vb = gtk_vbox_new(FALSE, 3);
68   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
69   gtk_container_add(GTK_CONTAINER(goto_frame_w), main_vb);
70   gtk_widget_show(main_vb);
71   
72   /* Frame number row */
73   fnumber_hb = gtk_hbox_new(FALSE, 3);
74   gtk_container_add(GTK_CONTAINER(main_vb), fnumber_hb);
75   gtk_widget_show(fnumber_hb);
76   
77   fnumber_lb = gtk_label_new("Frame number:");
78   gtk_box_pack_start(GTK_BOX(fnumber_hb), fnumber_lb, FALSE, FALSE, 0);
79   gtk_widget_show(fnumber_lb);
80   
81   fnumber_te = gtk_entry_new();
82   gtk_box_pack_start(GTK_BOX(fnumber_hb), fnumber_te, FALSE, FALSE, 0);
83   gtk_widget_show(fnumber_te);
84
85   /* Button row: OK and cancel buttons */
86   bbox = gtk_hbutton_box_new();
87   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
88   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
89   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
90   gtk_widget_show(bbox);
91
92   ok_bt = gtk_button_new_with_label ("OK");
93   gtk_signal_connect(GTK_OBJECT(ok_bt), "clicked",
94     GTK_SIGNAL_FUNC(goto_frame_ok_cb), GTK_OBJECT(goto_frame_w));
95   GTK_WIDGET_SET_FLAGS(ok_bt, GTK_CAN_DEFAULT);
96   gtk_box_pack_start (GTK_BOX (bbox), ok_bt, TRUE, TRUE, 0);
97   gtk_widget_grab_default(ok_bt);
98   gtk_widget_show(ok_bt);
99
100   cancel_bt = gtk_button_new_with_label ("Cancel");
101   gtk_signal_connect(GTK_OBJECT(cancel_bt), "clicked",
102     GTK_SIGNAL_FUNC(goto_frame_close_cb), GTK_OBJECT(goto_frame_w));
103   GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
104   gtk_box_pack_start (GTK_BOX (bbox), cancel_bt, TRUE, TRUE, 0);
105   gtk_widget_show(cancel_bt);
106
107   /* Attach pointers to needed widgets to the capture prefs window/object */
108   gtk_object_set_data(GTK_OBJECT(goto_frame_w), E_GOTO_FNUMBER_KEY, fnumber_te);
109
110   /* Catch the "activate" signal on the frame number text entry, so that
111      if the user types Return there, we act as if the "OK" button
112      had been selected, as happens if Return is typed if some widget
113      that *doesn't* handle the Return key has the input focus. */
114   dlg_set_activate(fnumber_te, ok_bt);
115
116   /* Catch the "key_press_event" signal in the window, so that we can catch
117      the ESC key being pressed and act as if the "Cancel" button had
118      been selected. */
119   dlg_set_cancel(goto_frame_w, cancel_bt);
120
121   /* Give the initial focus to the "Frame number" entry box. */
122   gtk_widget_grab_focus(fnumber_te);
123
124   gtk_widget_show(goto_frame_w);
125 }
126
127 static void
128 goto_frame_ok_cb(GtkWidget *ok_bt, gpointer parent_w)
129 {
130   GtkWidget *fnumber_te;
131   gchar *fnumber_text;
132   guint fnumber;
133   char *p;
134
135   fnumber_te = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(parent_w), E_GOTO_FNUMBER_KEY);
136
137   fnumber_text = gtk_entry_get_text(GTK_ENTRY(fnumber_te));
138   fnumber = strtoul(fnumber_text, &p, 10);
139   if (p == fnumber_text || *p != '\0') {
140     /* Illegal number.
141        XXX - what about negative numbers (which "strtoul()" allows)?
142        Can we hack up signal handlers for the widget to make it
143        reject attempts to type in characters other than digits? */
144     simple_dialog(ESD_TYPE_CRIT, NULL,
145                 "The frame number you entered isn't a valid number.");
146     return;
147   }
148
149   switch (goto_frame(&cfile, fnumber)) {
150
151   case NO_SUCH_FRAME:
152     simple_dialog(ESD_TYPE_CRIT, NULL, "There is no frame with that frame number.");
153     return;
154
155   case FRAME_NOT_DISPLAYED:
156     /* XXX - add it to the display filter? */
157     simple_dialog(ESD_TYPE_CRIT, NULL, "The frame with that frame number is not currently being displayed.");
158     return;
159
160   case FOUND_FRAME:
161     gtk_widget_destroy(GTK_WIDGET(parent_w));
162     break;
163   }
164 }
165
166 static void
167 goto_frame_close_cb(GtkWidget *close_bt, gpointer parent_w)
168 {
169   gtk_grab_remove(GTK_WIDGET(parent_w));
170   gtk_widget_destroy(GTK_WIDGET(parent_w));
171 }