Make the "Preferences" dialog box use the new utilities to make the Esc
[obnox/wireshark/wip.git] / gtk / prefs_dlg.c
1 /* prefs_dlg.c
2  * Routines for handling preferences
3  *
4  * $Id: prefs_dlg.c,v 1.11 2000/05/08 07:58:20 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 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #include <gtk/gtk.h>
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <errno.h>
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include <sys/stat.h>
46
47 #include "main.h"
48 #include "packet.h"
49 #include "file.h"
50 #include "prefs.h"
51 #include "column_prefs.h"
52 #include "print.h"
53 #include "prefs_dlg.h"
54 #include "print_prefs.h"
55 #include "stream_prefs.h"
56 #include "gui_prefs.h"
57 #include "util.h"
58 #include "ui_util.h"
59 #include "dlg_utils.h"
60 #include "simple_dialog.h"
61
62 static void     prefs_main_ok_cb(GtkWidget *, gpointer);
63 static void     prefs_main_save_cb(GtkWidget *, gpointer);
64 static void     prefs_main_cancel_cb(GtkWidget *, gpointer);
65 static gboolean prefs_main_delete_cb(GtkWidget *, gpointer);
66 static void     prefs_main_destroy_cb(GtkWidget *, gpointer);
67
68 #define E_PRINT_PAGE_KEY  "printer_options_page"
69 #define E_COLUMN_PAGE_KEY "column_options_page"
70 #define E_STREAM_PAGE_KEY "tcp_stream_options_page"
71 #define E_GUI_PAGE_KEY    "gui_options_page"
72
73 /*
74  * Keep a static pointer to the current "Preferences" window, if any, so that
75  * if somebody tries to do "Edit:Preferences" while there's already a
76  * "Preferences" window up, we just pop up the existing one, rather than
77  * creating a new one.
78  */
79 static GtkWidget *prefs_w;
80
81 void
82 prefs_cb(GtkWidget *w, gpointer sp) {
83   GtkWidget *main_vb, *top_hb, *bbox, *prefs_nb,
84             *ok_bt, *save_bt, *cancel_bt;
85   GtkWidget *print_pg, *column_pg, *stream_pg, *gui_pg, *label;
86
87   gint       start_page = (gint) sp;
88
89   if (prefs_w != NULL) {
90     /* There's already a "Preferences" dialog box; reactivate it. */
91     reactivate_window(prefs_w);
92     return;
93   }
94
95   prefs_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
96   gtk_window_set_title(GTK_WINDOW(prefs_w), "Ethereal: Preferences");
97   gtk_signal_connect(GTK_OBJECT(prefs_w), "delete-event",
98     GTK_SIGNAL_FUNC(prefs_main_delete_cb), NULL);
99   gtk_signal_connect(GTK_OBJECT(prefs_w), "destroy",
100         GTK_SIGNAL_FUNC(prefs_main_destroy_cb), NULL);
101   
102   /* Container for each row of widgets */
103   main_vb = gtk_vbox_new(FALSE, 5);
104   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
105   gtk_container_add(GTK_CONTAINER(prefs_w), main_vb);
106   gtk_widget_show(main_vb);
107   
108   /* Top row: Preferences notebook */
109   top_hb = gtk_hbox_new(FALSE, 1);
110   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
111   gtk_widget_show(top_hb);
112   
113   prefs_nb = gtk_notebook_new();
114   gtk_container_add(GTK_CONTAINER(main_vb), prefs_nb);
115   gtk_widget_show(prefs_nb);
116   
117   /* Printing prefs */
118   print_pg = printer_prefs_show();
119   gtk_object_set_data(GTK_OBJECT(prefs_w), E_PRINT_PAGE_KEY, print_pg);
120   label = gtk_label_new ("Printing");
121   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), print_pg, label);
122     
123   /* Column prefs */
124   column_pg = column_prefs_show();
125   gtk_object_set_data(GTK_OBJECT(prefs_w), E_COLUMN_PAGE_KEY, column_pg);
126   label = gtk_label_new ("Columns");
127   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), column_pg, label);
128   
129   /* TCP Streams prefs */
130   stream_pg = stream_prefs_show();
131   gtk_object_set_data(GTK_OBJECT(prefs_w), E_STREAM_PAGE_KEY, stream_pg);
132   label = gtk_label_new ("TCP Streams");
133   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), stream_pg, label);
134
135   /* GUI prefs */
136   gui_pg = gui_prefs_show();
137   gtk_object_set_data(GTK_OBJECT(prefs_w), E_GUI_PAGE_KEY, gui_pg);
138   label = gtk_label_new ("GUI");
139   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), gui_pg, label);
140
141   /* Jump to the specified page, if it was supplied */
142   if (start_page > E_PR_PG_NONE)
143     gtk_notebook_set_page(GTK_NOTEBOOK(prefs_nb), start_page);
144     
145   /* Button row: OK and cancel buttons */
146   bbox = gtk_hbutton_box_new();
147   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
148   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
149   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
150   gtk_widget_show(bbox);
151   
152   ok_bt = gtk_button_new_with_label ("OK");
153   gtk_signal_connect(GTK_OBJECT(ok_bt), "clicked",
154     GTK_SIGNAL_FUNC(prefs_main_ok_cb), GTK_OBJECT(prefs_w));
155   GTK_WIDGET_SET_FLAGS(ok_bt, GTK_CAN_DEFAULT);
156   gtk_box_pack_start (GTK_BOX (bbox), ok_bt, TRUE, TRUE, 0);
157   gtk_widget_grab_default(ok_bt);
158   gtk_widget_show(ok_bt);
159
160   save_bt = gtk_button_new_with_label ("Save");
161   gtk_signal_connect(GTK_OBJECT(save_bt), "clicked",
162     GTK_SIGNAL_FUNC(prefs_main_save_cb), GTK_OBJECT(prefs_w));
163   GTK_WIDGET_SET_FLAGS(save_bt, GTK_CAN_DEFAULT);
164   gtk_box_pack_start (GTK_BOX (bbox), save_bt, TRUE, TRUE, 0);
165   gtk_widget_show(save_bt);
166   
167   cancel_bt = gtk_button_new_with_label ("Cancel");
168   gtk_signal_connect(GTK_OBJECT(cancel_bt), "clicked",
169     GTK_SIGNAL_FUNC(prefs_main_cancel_cb), GTK_OBJECT(prefs_w));
170   GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
171   gtk_box_pack_start (GTK_BOX (bbox), cancel_bt, TRUE, TRUE, 0);
172   gtk_widget_show(cancel_bt);
173
174   /* Catch the "key_press_event" signal in the window, so that we can catch
175      the ESC key being pressed and act as if the "Cancel" button had
176      been selected. */
177   dlg_set_cancel(prefs_w, cancel_bt);
178
179   gtk_widget_show(prefs_w);
180 }
181
182 static void
183 prefs_main_ok_cb(GtkWidget *ok_bt, gpointer parent_w)
184 {
185   printer_prefs_ok(gtk_object_get_data(GTK_OBJECT(parent_w), E_PRINT_PAGE_KEY));
186   column_prefs_ok(gtk_object_get_data(GTK_OBJECT(parent_w), E_COLUMN_PAGE_KEY));
187   stream_prefs_ok(gtk_object_get_data(GTK_OBJECT(parent_w), E_STREAM_PAGE_KEY));
188   gui_prefs_ok(gtk_object_get_data(GTK_OBJECT(parent_w), E_GUI_PAGE_KEY));
189   gtk_widget_destroy(GTK_WIDGET(parent_w));
190 }
191
192 static void
193 prefs_main_save_cb(GtkWidget *save_bt, gpointer parent_w)
194 {
195   int err;
196   char *pf_path;
197
198   printer_prefs_save(gtk_object_get_data(GTK_OBJECT(parent_w), E_PRINT_PAGE_KEY));
199   column_prefs_save(gtk_object_get_data(GTK_OBJECT(parent_w), E_COLUMN_PAGE_KEY));
200   stream_prefs_save(gtk_object_get_data(GTK_OBJECT(parent_w), E_STREAM_PAGE_KEY));
201   gui_prefs_save(gtk_object_get_data(GTK_OBJECT(parent_w), E_GUI_PAGE_KEY));
202   err = write_prefs(&pf_path);
203   if (err != 0) {
204      simple_dialog(ESD_TYPE_WARN, NULL,
205       "Can't open preferences file\n\"%s\": %s.", pf_path,
206       strerror(err));
207   }
208 }
209
210 static void
211 prefs_main_cancel_cb(GtkWidget *cancel_bt, gpointer parent_w)
212 {
213   printer_prefs_cancel(gtk_object_get_data(GTK_OBJECT(parent_w), E_PRINT_PAGE_KEY));
214   column_prefs_cancel(gtk_object_get_data(GTK_OBJECT(parent_w), E_COLUMN_PAGE_KEY));
215   stream_prefs_cancel(gtk_object_get_data(GTK_OBJECT(parent_w), E_STREAM_PAGE_KEY));
216   gui_prefs_cancel(gtk_object_get_data(GTK_OBJECT(parent_w), E_GUI_PAGE_KEY));
217   gtk_widget_destroy(GTK_WIDGET(parent_w));
218 }
219
220 static gboolean
221 prefs_main_delete_cb(GtkWidget *prefs_w, gpointer dummy)
222 {
223   printer_prefs_delete(gtk_object_get_data(GTK_OBJECT(prefs_w), E_PRINT_PAGE_KEY));
224   column_prefs_delete(gtk_object_get_data(GTK_OBJECT(prefs_w), E_COLUMN_PAGE_KEY));
225   stream_prefs_delete(gtk_object_get_data(GTK_OBJECT(prefs_w), E_STREAM_PAGE_KEY));
226   gui_prefs_delete(gtk_object_get_data(GTK_OBJECT(prefs_w), E_GUI_PAGE_KEY));
227   return FALSE;
228 }
229
230 static void
231 prefs_main_destroy_cb(GtkWidget *win, gpointer user_data)
232 {
233   /* XXX - call the delete callback?  Or move its stuff here and
234      get rid of it? */
235
236   /* Note that we no longer have a "Preferences" dialog box. */
237   prefs_w = NULL;
238 }