implemented dlg_button_row_new to get a standard function for
[obnox/wireshark/wip.git] / gtk / goto_dlg.c
1 /* goto_dlg.c
2  * Routines for "go to packet" window
3  *
4  * $Id: goto_dlg.c,v 1.23 2004/01/21 21:19:33 ulfl Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31
32 #include <epan/proto.h>
33 #include "globals.h"
34
35 #include "goto_dlg.h"
36 #include "simple_dialog.h"
37 #include "dlg_utils.h"
38 #include "compat_macros.h"
39
40 /* Capture callback data keys */
41 #define E_GOTO_FNUMBER_KEY     "goto_fnumber_te"
42
43 static void
44 goto_frame_ok_cb(GtkWidget *ok_bt, gpointer parent_w);
45
46 static void
47 goto_frame_close_cb(GtkWidget *close_bt, gpointer parent_w);
48
49 void
50 goto_frame_cb(GtkWidget *w _U_, gpointer d _U_)
51 {
52   GtkWidget     *goto_frame_w, *main_vb, *fnumber_hb, *fnumber_lb, *fnumber_te,
53                 *bbox, *ok_bt, *cancel_bt;
54
55   goto_frame_w = dlg_window_new("Ethereal: Go To Packet");
56
57   /* Container for each row of widgets */
58   main_vb = gtk_vbox_new(FALSE, 3);
59   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
60   gtk_container_add(GTK_CONTAINER(goto_frame_w), main_vb);
61   gtk_widget_show(main_vb);
62
63   /* Frame number row */
64   fnumber_hb = gtk_hbox_new(FALSE, 3);
65   gtk_container_add(GTK_CONTAINER(main_vb), fnumber_hb);
66   gtk_widget_show(fnumber_hb);
67
68   fnumber_lb = gtk_label_new("Packet number:");
69   gtk_box_pack_start(GTK_BOX(fnumber_hb), fnumber_lb, FALSE, FALSE, 0);
70   gtk_widget_show(fnumber_lb);
71
72   fnumber_te = gtk_entry_new();
73   gtk_box_pack_start(GTK_BOX(fnumber_hb), fnumber_te, FALSE, FALSE, 0);
74   gtk_widget_show(fnumber_te);
75
76   /* Button row: OK and cancel buttons */
77   bbox = dlg_button_row_new(GTK_STOCK_JUMP_TO, GTK_STOCK_CANCEL, NULL);
78   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
79   gtk_widget_show(bbox);
80
81   ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_JUMP_TO);
82   SIGNAL_CONNECT(ok_bt, "clicked", goto_frame_ok_cb, goto_frame_w);
83   gtk_widget_grab_default(ok_bt);
84
85   cancel_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
86   SIGNAL_CONNECT(cancel_bt, "clicked", goto_frame_close_cb, goto_frame_w);
87
88   /* Attach pointers to needed widgets to the capture prefs window/object */
89   OBJECT_SET_DATA(goto_frame_w, E_GOTO_FNUMBER_KEY, fnumber_te);
90
91   /* Catch the "activate" signal on the frame number text entry, so that
92      if the user types Return there, we act as if the "OK" button
93      had been selected, as happens if Return is typed if some widget
94      that *doesn't* handle the Return key has the input focus. */
95   dlg_set_activate(fnumber_te, ok_bt);
96
97   /* Catch the "key_press_event" signal in the window, so that we can catch
98      the ESC key being pressed and act as if the "Cancel" button had
99      been selected. */
100   dlg_set_cancel(goto_frame_w, cancel_bt);
101
102   /* Give the initial focus to the "Packet number" entry box. */
103   gtk_widget_grab_focus(fnumber_te);
104
105   gtk_widget_show(goto_frame_w);
106 }
107
108 static void
109 goto_frame_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w)
110 {
111   GtkWidget   *fnumber_te;
112   const gchar *fnumber_text;
113   guint        fnumber;
114   char        *p;
115
116   fnumber_te = (GtkWidget *)OBJECT_GET_DATA(parent_w, E_GOTO_FNUMBER_KEY);
117
118   fnumber_text = gtk_entry_get_text(GTK_ENTRY(fnumber_te));
119   fnumber = strtoul(fnumber_text, &p, 10);
120   if (p == fnumber_text || *p != '\0') {
121     /* Illegal number.
122        XXX - what about negative numbers (which "strtoul()" allows)?
123        Can we hack up signal handlers for the widget to make it
124        reject attempts to type in characters other than digits? */
125     simple_dialog(ESD_TYPE_CRIT, NULL,
126                 "The packet number you entered isn't a valid number.");
127     return;
128   }
129
130   if (goto_frame(&cfile, fnumber)) {
131     /* We succeeded in going to that frame; we're done. */
132     gtk_widget_destroy(GTK_WIDGET(parent_w));
133   }
134 }
135
136 static void
137 goto_frame_close_cb(GtkWidget *close_bt _U_, gpointer parent_w)
138 {
139   gtk_grab_remove(GTK_WIDGET(parent_w));
140   gtk_widget_destroy(GTK_WIDGET(parent_w));
141 }