2 * Dialog box for GUI preferences
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #include "gui_prefs.h"
35 #include "gtkglobals.h"
37 #include "supported_protos_dlg.h"
38 #include <epan/prefs.h>
39 #include "prefs_dlg.h"
40 #include "gui_utils.h"
41 #include "simple_dialog.h"
42 #include "dlg_utils.h"
43 #include "proto_draw.h"
45 #include "main_toolbar.h"
46 #include "font_utils.h"
47 #include "main_packet_list.h"
49 #include "webbrowser.h"
52 static gint fetch_enum_value(gpointer control, const enum_val_t *enumvals);
53 static gint fileopen_dir_changed_cb(GtkWidget *myentry _U_, GdkEvent *event, gpointer parent_w);
54 static gint fileopen_preview_changed_cb(GtkWidget *myentry _U_, GdkEvent *event, gpointer parent_w);
55 static void fileopen_selected_cb(GtkWidget *mybutton_rb _U_, gpointer parent_w);
56 static gint recent_files_count_changed_cb(GtkWidget *recent_files_entry _U_,
57 GdkEvent *event _U_, gpointer parent_w);
58 #define PLIST_SEL_BROWSE_KEY "plist_sel_browse"
59 #define PTREE_SEL_BROWSE_KEY "ptree_sel_browse"
60 #define GEOMETRY_POSITION_KEY "geometry_position"
61 #define GEOMETRY_SIZE_KEY "geometry_size"
62 #define GEOMETRY_MAXIMIZED_KEY "geometry_maximized"
63 #define GEOMETRY_COLUMN_WIDTH_KEY "geometry_column_width"
65 #define GUI_CONSOLE_OPEN_KEY "console_open"
66 #define GUI_FILEOPEN_KEY "fileopen_behavior"
67 #define GUI_FILEOPEN_PREVIEW_KEY "fileopen_preview_timeout"
68 #define GUI_RECENT_FILES_COUNT_KEY "recent_files_count"
69 #define GUI_FILEOPEN_DIR_KEY "fileopen_directory"
70 #define GUI_ASK_UNSAVED_KEY "ask_unsaved"
71 #define GUI_WEBBROWSER_KEY "webbrowser"
72 #define GUI_FIND_WRAP_KEY "find_wrap"
73 #define GUI_USE_PREF_SAVE_KEY "use_pref_save"
75 static const enum_val_t scrollbar_placement_vals[] _U_ = {
76 { "FALSE", "Left", FALSE },
77 { "TRUE", "Right", TRUE },
81 static const enum_val_t selection_mode_vals[] = {
82 { "FALSE", "Selects", FALSE },
83 { "TRUE", "Browses", TRUE },
87 static const enum_val_t altern_colors_vals[] _U_ = {
88 { "FALSE", "No", FALSE },
89 { "TRUE", "Yes", TRUE },
93 static const enum_val_t filter_toolbar_placement_vals[] _U_ = {
94 { "FALSE", "Below the main toolbar", FALSE },
95 { "TRUE", "Insert into statusbar", TRUE },
99 static const enum_val_t highlight_style_vals[] _U_ = {
100 { "FALSE", "Bold", FALSE },
101 { "TRUE", "Inverse", TRUE },
106 static const enum_val_t toolbar_style_vals[] _U_ = {
107 { "ICONS", "Icons only", TB_STYLE_ICONS },
108 { "TEXT", "Text only", TB_STYLE_TEXT },
109 { "BOTH", "Icons & Text", TB_STYLE_BOTH },
114 static const enum_val_t gui_console_open_vals[] = {
115 { "NEVER", "Never", console_open_never },
116 { "AUTOMATIC", "Automatic (advanced user)", console_open_auto },
117 { "ALWAYS", "Always (debugging)", console_open_always },
122 static const enum_val_t gui_fileopen_vals[] = {
123 { "LAST_OPENED", "Remember last directory", FO_STYLE_LAST_OPENED },
124 { "SPECIFIED", "Always start in:", FO_STYLE_SPECIFIED },
128 /* Set to FALSE initially; set to TRUE if the user ever hits "OK" on
129 the "Font..." dialog, so that we know that they (probably) changed
130 the font, and therefore that the "apply" function needs to take care
132 static gboolean font_changed;
134 /* Font name from the font dialog box; if "font_changed" is TRUE, this
135 has been set to the name of the font the user selected. */
136 static gchar *new_font_name;
138 static GtkWidget *font_browse_w;
140 /* Used to contain the string from the Recent Files Count Max pref item */
141 static char recent_files_count_max_str[128] = "";
143 /* Used to contain the string from the Open File preview timeout pref item */
144 static char open_file_preview_str[128] = "";
146 #define GUI_TABLE_ROWS 4
151 GtkWidget *main_tb, *main_vb;
152 GtkWidget *plist_browse_om;
153 GtkWidget *ptree_browse_om;
155 GtkWidget *console_open_om;
157 GtkWidget *fileopen_rb, *fileopen_dir_te, *fileopen_preview_te;
158 GtkWidget *recent_files_count_max_te, *ask_unsaved_cb, *find_wrap_cb;
159 GtkWidget *use_pref_save_cb;
160 GtkWidget *webbrowser_te;
161 GtkWidget *save_position_cb, *save_size_cb, *save_maximized_cb, *save_column_width_cb;
163 GtkTooltips *tooltips = gtk_tooltips_new();
166 char current_val_str[128];
168 /* The font haven't been changed yet. */
169 font_changed = FALSE;
171 /* The columns haven't been changed yet */
172 cfile.cinfo.columns_changed = FALSE;
174 /* Main vertical box */
175 main_vb = gtk_vbox_new(FALSE, 7);
176 gtk_container_border_width( GTK_CONTAINER(main_vb), 5 );
179 main_tb = gtk_table_new(GUI_TABLE_ROWS, 2, FALSE);
180 gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
181 gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
182 gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
184 /* Packet list selection browseable */
185 plist_browse_om = create_preference_option_menu(main_tb, pos++,
186 "Packet list selection mode:", NULL, selection_mode_vals,
187 prefs.gui_plist_sel_browse);
188 gtk_tooltips_set_tip(tooltips, plist_browse_om, "Choose to browse "
189 "or select a packet for detailed dissection.", NULL);
190 g_object_set_data(G_OBJECT(main_vb), PLIST_SEL_BROWSE_KEY, plist_browse_om);
192 /* Proto tree selection browseable */
193 ptree_browse_om = create_preference_option_menu(main_tb, pos++,
194 "Protocol tree selection mode:", NULL, selection_mode_vals,
195 prefs.gui_ptree_sel_browse);
196 gtk_tooltips_set_tip(tooltips, ptree_browse_om, "Choose to browse "
198 g_object_set_data(G_OBJECT(main_vb), PTREE_SEL_BROWSE_KEY, ptree_browse_om);
201 save_position_cb = create_preference_check_button(main_tb, pos++,
202 "Save window position:", NULL, prefs.gui_geometry_save_position);
203 gtk_tooltips_set_tip(tooltips, save_position_cb, "Whether to save the "
204 "position of the main window.", NULL);
205 g_object_set_data(G_OBJECT(main_vb), GEOMETRY_POSITION_KEY, save_position_cb);
207 save_size_cb = create_preference_check_button(main_tb, pos++,
208 "Save window size:", NULL, prefs.gui_geometry_save_size);
209 gtk_tooltips_set_tip(tooltips, save_size_cb, "Whether to save the "
210 "size of the main window.", NULL);
211 g_object_set_data(G_OBJECT(main_vb), GEOMETRY_SIZE_KEY, save_size_cb);
213 save_maximized_cb = create_preference_check_button(main_tb, pos++,
214 "Save maximized state:", NULL, prefs.gui_geometry_save_maximized);
215 gtk_tooltips_set_tip(tooltips, save_maximized_cb, "Whether to save the "
216 "maximed state of the main window.", NULL);
217 g_object_set_data(G_OBJECT(main_vb), GEOMETRY_MAXIMIZED_KEY, save_maximized_cb);
219 save_column_width_cb = create_preference_check_button(main_tb, pos++,
220 "Save column widths:", NULL, prefs.gui_geometry_save_column_width);
221 gtk_tooltips_set_tip(tooltips, save_column_width_cb, "Whether to save the "
222 "column widths.", NULL);
223 g_object_set_data(G_OBJECT(main_vb), GEOMETRY_COLUMN_WIDTH_KEY, save_column_width_cb);
226 /* How the console window should be opened */
227 console_open_om = create_preference_option_menu(main_tb, pos++,
228 "Open a console window", NULL, gui_console_open_vals,
229 prefs.gui_console_open);
230 gtk_tooltips_set_tip(tooltips, console_open_om, "Whether to open a console window "
231 "(Automatic will open a console if messages appear).", NULL);
232 g_object_set_data(G_OBJECT(main_vb), GUI_CONSOLE_OPEN_KEY, console_open_om);
235 /* Allow user to select where they want the File Open dialog to open to
237 fileopen_rb = create_preference_radio_buttons(main_tb, pos++,
238 "\"File Open\" dialog behavior:",
239 "Which directory the \"File Open\" dialog should start with.", gui_fileopen_vals,
240 prefs.gui_fileopen_style);
242 /* Directory to default File Open dialog to */
243 fileopen_dir_te = create_preference_entry(main_tb, pos++,
244 "Directory:", NULL, prefs.gui_fileopen_dir);
245 gtk_tooltips_set_tip(tooltips, fileopen_dir_te,
246 "The \"File Open\" dialog defaults always to this directory.", NULL);
247 g_object_set_data(G_OBJECT(main_vb), GUI_FILEOPEN_KEY, fileopen_rb);
248 g_object_set_data(G_OBJECT(main_vb), GUI_FILEOPEN_DIR_KEY, fileopen_dir_te);
249 g_signal_connect(fileopen_rb, "clicked", G_CALLBACK(fileopen_selected_cb), main_vb);
250 g_signal_connect(fileopen_dir_te, "focus-out-event",
251 G_CALLBACK(fileopen_dir_changed_cb), main_vb);
253 /* File Open dialog preview timeout */
254 fileopen_preview_te = create_preference_entry(main_tb, pos++,
255 "\"File Open\" preview timeout:", "Timeout, until preview gives up scanning the capture file content.", open_file_preview_str);
256 g_snprintf(current_val_str, 128, "%d", prefs.gui_fileopen_preview);
257 gtk_entry_set_text(GTK_ENTRY(fileopen_preview_te), current_val_str);
258 gtk_tooltips_set_tip(tooltips, fileopen_preview_te,
259 "Reading preview data in the \"File Open\" dialog will be stopped after given seconds.", NULL);
260 g_object_set_data(G_OBJECT(main_vb), GUI_FILEOPEN_PREVIEW_KEY, fileopen_preview_te);
261 g_signal_connect(fileopen_preview_te, "focus_out_event", G_CALLBACK(fileopen_preview_changed_cb), main_vb);
263 /* Number of entries in the recent_files list ... */
264 recent_files_count_max_te = create_preference_entry(main_tb, pos++,
265 "\"Open Recent\" max. list entries:", "Maximum number of recent files", recent_files_count_max_str);
266 g_snprintf(current_val_str, 128, "%d", prefs.gui_recent_files_count_max);
267 gtk_entry_set_text(GTK_ENTRY(recent_files_count_max_te), current_val_str);
268 gtk_tooltips_set_tip(tooltips, recent_files_count_max_te,
269 "Maximum number of entries in the \"File/Open Recent\" list.", NULL);
270 g_object_set_data(G_OBJECT(main_vb), GUI_RECENT_FILES_COUNT_KEY, recent_files_count_max_te);
271 g_signal_connect(recent_files_count_max_te, "focus_out_event", G_CALLBACK(recent_files_count_changed_cb), main_vb);
273 fileopen_selected_cb(NULL, main_vb);
275 /* ask for unsaved capture files? */
276 ask_unsaved_cb = create_preference_check_button(main_tb, pos++,
277 "Ask for unsaved capture files:", NULL, prefs.gui_ask_unsaved);
278 gtk_tooltips_set_tip(tooltips, ask_unsaved_cb, "Whether a dialog should "
279 "pop up in case of an unsaved capture file.", NULL);
280 g_object_set_data(G_OBJECT(main_vb), GUI_ASK_UNSAVED_KEY, ask_unsaved_cb);
282 /* do we want to wrap when searching for data? */
283 find_wrap_cb = create_preference_check_button(main_tb, pos++,
284 "Wrap to end/beginning of file during a find:", NULL, prefs.gui_find_wrap);
285 gtk_tooltips_set_tip(tooltips, find_wrap_cb, "Whether a search should "
286 "wrap in a capture file.", NULL);
287 g_object_set_data(G_OBJECT(main_vb), GUI_FIND_WRAP_KEY, find_wrap_cb);
289 /* show an explicit Save button for settings dialogs (preferences and alike)? */
290 use_pref_save_cb = create_preference_check_button(main_tb, pos++,
291 "Settings dialogs show a save button:", NULL, prefs.gui_use_pref_save);
292 gtk_tooltips_set_tip(tooltips, use_pref_save_cb,
293 "Whether the various settings dialogs (e.g. Preferences) should "
294 "use an explicit save button - for advanced users.", NULL);
295 g_object_set_data(G_OBJECT(main_vb), GUI_USE_PREF_SAVE_KEY, use_pref_save_cb);
298 if (browser_needs_pref()) {
299 webbrowser_te = create_preference_entry(main_tb, pos++,
300 "Web browser command:", NULL, prefs.gui_webbrowser);
301 gtk_entry_set_text(GTK_ENTRY(webbrowser_te), prefs.gui_webbrowser);
302 gtk_tooltips_set_tip(tooltips, webbrowser_te, "Command line to "
303 "desired browser.", NULL);
304 g_object_set_data(G_OBJECT(main_vb), GUI_WEBBROWSER_KEY, webbrowser_te);
307 /* Show 'em what we got */
308 gtk_widget_show_all(main_vb);
314 /* Create a font widget for browsing. */
316 gui_font_prefs_show(void)
318 /* Create the font selection widget. */
319 font_browse_w = (GtkWidget *) gtk_font_selection_new();
320 gtk_widget_show(font_browse_w);
322 return font_browse_w;
331 font_name = g_strdup(gtk_font_selection_get_font_name(
332 GTK_FONT_SELECTION(font_browse_w)));
333 if (font_name == NULL) {
334 /* No font was selected; let the user know, but don't
335 tear down the font selection dialog, so they can
337 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
338 "You have not selected a font.");
342 if (!user_font_test(font_name)) {
343 /* The font isn't usable; "user_font_test()" has already
344 told the user why. Don't tear down the font selection
349 new_font_name = font_name;
355 fetch_enum_value(gpointer control, const enum_val_t *enumvals)
357 return fetch_preference_option_menu_val(GTK_WIDGET(control), enumvals);
361 gui_prefs_fetch(GtkWidget *w)
363 prefs.gui_plist_sel_browse = fetch_enum_value(
364 g_object_get_data(G_OBJECT(w), PLIST_SEL_BROWSE_KEY), selection_mode_vals);
365 prefs.gui_ptree_sel_browse = fetch_enum_value(
366 g_object_get_data(G_OBJECT(w), PTREE_SEL_BROWSE_KEY), selection_mode_vals);
367 prefs.gui_geometry_save_position =
368 gtk_toggle_button_get_active(g_object_get_data(G_OBJECT(w), GEOMETRY_POSITION_KEY));
369 prefs.gui_geometry_save_size =
370 gtk_toggle_button_get_active(g_object_get_data(G_OBJECT(w), GEOMETRY_SIZE_KEY));
371 prefs.gui_geometry_save_maximized =
372 gtk_toggle_button_get_active(g_object_get_data(G_OBJECT(w), GEOMETRY_MAXIMIZED_KEY));
373 prefs.gui_geometry_save_column_width =
374 gtk_toggle_button_get_active(g_object_get_data(G_OBJECT(w), GEOMETRY_COLUMN_WIDTH_KEY));
376 prefs.gui_console_open = fetch_enum_value(
377 g_object_get_data(G_OBJECT(w), GUI_CONSOLE_OPEN_KEY), gui_console_open_vals);
379 prefs.gui_fileopen_style = fetch_preference_radio_buttons_val(
380 g_object_get_data(G_OBJECT(w), GUI_FILEOPEN_KEY), gui_fileopen_vals);
382 if (prefs.gui_fileopen_dir != NULL)
383 g_free(prefs.gui_fileopen_dir);
384 prefs.gui_fileopen_dir = g_strdup(gtk_entry_get_text(
385 GTK_ENTRY(g_object_get_data(G_OBJECT(w), GUI_FILEOPEN_DIR_KEY))));
387 prefs.gui_ask_unsaved =
388 gtk_toggle_button_get_active(g_object_get_data(G_OBJECT(w), GUI_ASK_UNSAVED_KEY));
390 prefs.gui_find_wrap =
391 gtk_toggle_button_get_active(g_object_get_data(G_OBJECT(w), GUI_FIND_WRAP_KEY));
393 prefs.gui_use_pref_save =
394 gtk_toggle_button_get_active(g_object_get_data(G_OBJECT(w), GUI_USE_PREF_SAVE_KEY));
396 if (browser_needs_pref()) {
397 g_free(prefs.gui_webbrowser);
398 prefs.gui_webbrowser = g_strdup(gtk_entry_get_text(
399 GTK_ENTRY(g_object_get_data(G_OBJECT(w), GUI_WEBBROWSER_KEY))));
402 * XXX - we need to have a way to fetch the preferences into
403 * local storage and only set the permanent preferences if there
404 * weren't any errors in those fetches, as there are several
405 * places where there *can* be a bad preference value.
408 if (strcmp(new_font_name, prefs.gui_font_name) != 0) {
410 if (prefs.gui_font_name != NULL)
411 g_free(prefs.gui_font_name);
412 prefs.gui_font_name = g_strdup(new_font_name);
420 gui_prefs_apply(GtkWidget *w _U_ , gboolean redissect)
424 /* user immediately wants to see a console */
425 if (prefs.gui_console_open == console_open_always) {
431 /* This redraws the hex dump windows. */
432 switch (user_font_apply(FALSE)) {
437 case FA_FONT_NOT_RESIZEABLE:
438 /* "user_font_apply()" popped up an alert box. */
439 /* turn off zooming - font can't be resized */
440 recent.gui_zoom_level = 0;
443 case FA_FONT_NOT_AVAILABLE:
444 /* We assume this means that the specified size
446 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
447 "That font isn't available at the specified zoom level;\n"
448 "turning zooming off.");
449 recent.gui_zoom_level = 0;
452 } else if (!redissect) {
453 /* Redraw the hex dump windows, in case the
454 highlight style changed, only if we aren't redissecting the whole file.
455 XXX - do it only if the highlight style *did* change. */
456 redraw_hex_dump_all();
459 /* Redraw the help window(s). */
463 /* XXX: redraw the toolbar only, if style changed */
464 toolbar_redraw_all();
466 set_scrollbar_placement_all();
467 packet_list_set_sel_browse(prefs.gui_plist_sel_browse, FALSE);
468 set_ptree_sel_browse_all(prefs.gui_ptree_sel_browse);
469 set_tree_styles_all();
470 main_widgets_rearrange();
474 gui_prefs_destroy(GtkWidget *w _U_)
476 /* Free up any saved font name. */
477 if (new_font_name != NULL) {
478 g_free(new_font_name);
479 new_font_name = NULL;
485 recent_files_count_changed_cb(GtkWidget *recent_files_entry _U_,
486 GdkEvent *event _U_, gpointer parent_w)
488 GtkWidget *recent_files_count_te;
491 recent_files_count_te = (GtkWidget *)g_object_get_data(G_OBJECT(parent_w), GUI_RECENT_FILES_COUNT_KEY);
494 * Now, just convert the string to a number and store it in the prefs
498 newval = strtol(gtk_entry_get_text (GTK_ENTRY(recent_files_count_te)), NULL, 10);
501 prefs.gui_recent_files_count_max = newval;
504 /* We really should pop up a nasty dialog box if newval <= 0 */
510 fileopen_preview_changed_cb(GtkWidget *recent_files_entry _U_,
511 GdkEvent *event _U_, gpointer parent_w)
513 GtkWidget *fileopen_preview_te;
516 fileopen_preview_te = (GtkWidget *)g_object_get_data(G_OBJECT(parent_w), GUI_FILEOPEN_PREVIEW_KEY);
519 * Now, just convert the string to a number and store it in the prefs
523 newval = strtol(gtk_entry_get_text (GTK_ENTRY(fileopen_preview_te)), NULL, 10);
526 prefs.gui_fileopen_preview = newval;
529 /* We really should pop up a nasty dialog box if newval <= 0 */
535 fileopen_dir_changed_cb(GtkWidget *fileopen_entry _U_, GdkEvent *event _U_, gpointer parent_w)
537 GtkWidget *fileopen_dir_te;
539 gint fileopen_dir_te_length;
541 fileopen_dir_te = (GtkWidget *)g_object_get_data(G_OBJECT(parent_w), GUI_FILEOPEN_DIR_KEY);
542 fileopen_dir_te_length = strlen(gtk_entry_get_text (GTK_ENTRY(fileopen_entry)));
543 if (fileopen_dir_te_length == 0)
545 lastchar = gtk_editable_get_chars(GTK_EDITABLE(fileopen_entry), fileopen_dir_te_length-1, -1);
546 if (strcmp(lastchar, G_DIR_SEPARATOR_S) != 0)
547 gtk_entry_append_text(GTK_ENTRY(fileopen_entry), G_DIR_SEPARATOR_S);
552 fileopen_selected_cb(GtkWidget *mybutton_rb _U_, gpointer parent_w)
554 GtkWidget *fileopen_rb, *fileopen_dir_te;
556 fileopen_rb = (GtkWidget *)g_object_get_data(G_OBJECT(parent_w), GUI_FILEOPEN_KEY);
557 fileopen_dir_te = (GtkWidget *)g_object_get_data(G_OBJECT(parent_w), GUI_FILEOPEN_DIR_KEY);
559 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(fileopen_rb)))
561 gtk_widget_set_sensitive(GTK_WIDGET(fileopen_dir_te), TRUE);
565 gtk_widget_set_sensitive(GTK_WIDGET(fileopen_dir_te), FALSE);