On Windows, put Ethereal configuration files under the "Application
[obnox/wireshark/wip.git] / gtk / prefs_dlg.c
index 804126973a38ee277bb0ca3b8ba06da842ae9dfc..e1512ccfbaab68fd3ee3a032a263a88d7d588567 100644 (file)
@@ -1,12 +1,11 @@
 /* prefs_dlg.c
  * Routines for handling preferences
  *
- * $Id: prefs_dlg.c,v 1.22 2000/08/23 06:56:11 guy Exp $
+ * $Id: prefs_dlg.c,v 1.32 2001/10/24 06:13:07 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- *
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include <sys/stat.h>
 #endif
 
+#include <epan/filesystem.h>
+
 #include "main.h"
+#include "gtkglobals.h"
 #include "packet.h"
 #include "file.h"
 #include "prefs.h"
@@ -56,7 +58,6 @@
 #include "print_prefs.h"
 #include "stream_prefs.h"
 #include "gui_prefs.h"
-#include "util.h"
 #include "ui_util.h"
 #include "dlg_utils.h"
 #include "simple_dialog.h"
@@ -69,6 +70,7 @@ static void     prefs_main_save_cb(GtkWidget *, gpointer);
 static void     prefs_main_cancel_cb(GtkWidget *, gpointer);
 static gboolean prefs_main_delete_cb(GtkWidget *, gpointer);
 static void     prefs_main_destroy_cb(GtkWidget *, gpointer);
+static void    prefs_tree_select_cb(GtkCTree *, GtkCTreeNode *, gint, gpointer);
 
 #define E_PRINT_PAGE_KEY  "printer_options_page"
 #define E_COLUMN_PAGE_KEY "column_options_page"
@@ -98,6 +100,13 @@ static GtkWidget *prefs_w;
  */
 static e_prefs saved_prefs;
 
+struct ct_struct {
+  GtkWidget    *notebook;
+  GtkWidget    *ctree;
+  GtkCTreeNode *node;
+  gint         page;
+};
+
 static void
 pref_show(pref_t *pref, gpointer user_data)
 {
@@ -107,7 +116,7 @@ pref_show(pref_t *pref, gpointer user_data)
   GtkWidget *label, *menu, *menu_item, *widget, *button;
   GSList *rb_group;
   char uint_str[10+1];
-  const enum_val *enum_valp;
+  const enum_val_t *enum_valp;
   int menu_index, index;
 
   /* Give this preference a label which is its title, followed by a colon,
@@ -188,6 +197,7 @@ pref_show(pref_t *pref, gpointer user_data)
        gtk_menu_append(GTK_MENU(menu), menu_item);
        if (enum_valp->value == pref->saved_val.enumval)
          menu_index = index;
+       gtk_widget_show(menu_item);
       }
 
       /* Create the option menu from the option */
@@ -220,15 +230,23 @@ pref_show(pref_t *pref, gpointer user_data)
                                pref->ordinal+1);
 }
 
+#define MAX_TREE_NODE_NAME_LEN 64
 static void
 module_prefs_show(module_t *module, gpointer user_data)
 {
-  GtkWidget *prefs_nb = user_data;
-  GtkWidget *main_vb, *main_tb, *label;
+  struct ct_struct *cts = user_data;
+  GtkWidget        *main_vb, *main_tb, *frame;
+  gchar            label_str[MAX_TREE_NODE_NAME_LEN], *label_ptr = label_str;
+  GtkCTreeNode     *ct_node;
+
+  /* Frame */
+  frame = gtk_frame_new(module->title);
+  gtk_widget_show(frame);
 
   /* Main vertical box */
   main_vb = gtk_vbox_new(FALSE, 5);
   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
+  gtk_container_add(GTK_CONTAINER(frame), main_vb);
 
   /* Main table */
   main_tb = gtk_table_new(module->numprefs, 2, FALSE);
@@ -239,8 +257,13 @@ module_prefs_show(module_t *module, gpointer user_data)
   /* Add items for each of the preferences */
   prefs_pref_foreach(module, pref_show, main_tb);
 
-  label = gtk_label_new(module->title);
-  gtk_notebook_append_page(GTK_NOTEBOOK(prefs_nb), main_vb, label);
+  gtk_notebook_append_page(GTK_NOTEBOOK(cts->notebook), frame, NULL);
+  strcpy(label_str, module->title);
+  ct_node = gtk_ctree_insert_node(GTK_CTREE(cts->ctree), cts->node, NULL, 
+               &label_ptr, 5, NULL, NULL, NULL, NULL, TRUE, TRUE);
+  gtk_ctree_node_set_row_data(GTK_CTREE(cts->ctree), ct_node,
+               GINT_TO_POINTER(cts->page));
+  cts->page++;
 
   /* Show 'em what we got */
   gtk_widget_show_all(main_vb);
@@ -248,9 +271,13 @@ module_prefs_show(module_t *module, gpointer user_data)
 
 void
 prefs_cb(GtkWidget *w, gpointer dummy) {
-  GtkWidget *main_vb, *top_hb, *bbox, *prefs_nb,
-            *ok_bt, *apply_bt, *save_bt, *cancel_bt;
-  GtkWidget *print_pg, *column_pg, *stream_pg, *gui_pg, *label;
+  GtkWidget        *main_vb, *top_hb, *bbox, *prefs_nb, *ct_sb, *frame,
+                   *ok_bt, *apply_bt, *save_bt, *cancel_bt;
+  GtkWidget        *print_pg, *column_pg, *stream_pg, *gui_pg;
+  gchar            label_str[MAX_TREE_NODE_NAME_LEN], *label_ptr = label_str;
+  GtkCTreeNode     *ct_node;
+  struct ct_struct cts;
+
 
   if (prefs_w != NULL) {
     /* There's already a "Preferences" dialog box; reactivate it. */
@@ -274,41 +301,113 @@ prefs_cb(GtkWidget *w, gpointer dummy) {
   gtk_container_add(GTK_CONTAINER(prefs_w), main_vb);
   gtk_widget_show(main_vb);
   
-  /* Top row: Preferences notebook */
-  top_hb = gtk_hbox_new(FALSE, 1);
+  /* Top row: Preferences tree and notebook */
+  top_hb = gtk_hbox_new(FALSE, 10);
   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
   gtk_widget_show(top_hb);
+
+  /* Place a Ctree on the left for preference categories */
+  ct_sb = gtk_scrolled_window_new(NULL, NULL);
+  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct_sb),
+       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+  set_scrollbar_placement_scrollw(ct_sb, prefs.gui_scrollbar_on_right);
+  remember_scrolled_window(ct_sb);
+  gtk_container_add(GTK_CONTAINER(top_hb), ct_sb);
+  gtk_widget_show(ct_sb);
+
+  cts.ctree = gtk_ctree_new(1, 0);
+  cts.page  = 0;
+  gtk_container_add(GTK_CONTAINER(ct_sb), cts.ctree);
+
+  /* Be consistent with our line/expander styles */
+  g_assert(prefs.gui_ptree_line_style >= GTK_CTREE_LINES_NONE &&
+       prefs.gui_ptree_line_style <= GTK_CTREE_LINES_TABBED);
+  gtk_ctree_set_line_style(GTK_CTREE(cts.ctree), prefs.gui_ptree_line_style);
+  g_assert(prefs.gui_ptree_expander_style >= GTK_CTREE_EXPANDER_NONE &&
+       prefs.gui_ptree_expander_style <= GTK_CTREE_EXPANDER_CIRCULAR);
+  gtk_ctree_set_expander_style(GTK_CTREE(cts.ctree),
+       prefs.gui_ptree_expander_style);
+
+  gtk_clist_set_column_auto_resize(GTK_CLIST(cts.ctree), 0, TRUE);
+  gtk_signal_connect(GTK_OBJECT(cts.ctree), "tree-select-row",
+    GTK_SIGNAL_FUNC(prefs_tree_select_cb), NULL);
+  gtk_widget_show(cts.ctree);
   
+  /* A notebook widget sans tabs is used to flip between prefs */
   notebook = prefs_nb = gtk_notebook_new();
-  gtk_container_add(GTK_CONTAINER(main_vb), prefs_nb);
+  gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
+  gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
+  gtk_container_add(GTK_CONTAINER(top_hb), prefs_nb);
   gtk_widget_show(prefs_nb);
   
   /* Printing prefs */
+  frame = gtk_frame_new("Printing");
+  gtk_widget_show(GTK_WIDGET(frame));
   print_pg = printer_prefs_show();
+  gtk_container_add(GTK_CONTAINER(frame), print_pg);
   gtk_object_set_data(GTK_OBJECT(prefs_w), E_PRINT_PAGE_KEY, print_pg);
-  label = gtk_label_new ("Printing");
-  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), print_pg, label);
+  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), frame, NULL);
+  strcpy(label_str, "Printing");
+  ct_node = gtk_ctree_insert_node(GTK_CTREE(cts.ctree), NULL, NULL, 
+               &label_ptr, 5, NULL, NULL, NULL, NULL, TRUE, TRUE);
+  gtk_ctree_node_set_row_data(GTK_CTREE(cts.ctree), ct_node,
+               GINT_TO_POINTER(cts.page));
+  cts.page++;
     
   /* Column prefs */
+  frame = gtk_frame_new("Columns");
+  gtk_widget_show(GTK_WIDGET(frame));
   column_pg = column_prefs_show();
+  gtk_container_add(GTK_CONTAINER(frame), column_pg);
   gtk_object_set_data(GTK_OBJECT(prefs_w), E_COLUMN_PAGE_KEY, column_pg);
-  label = gtk_label_new ("Columns");
-  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), column_pg, label);
+  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), frame, NULL);
+  strcpy(label_str, "Columns");
+  ct_node = gtk_ctree_insert_node(GTK_CTREE(cts.ctree), NULL, NULL, 
+               &label_ptr, 5, NULL, NULL, NULL, NULL, TRUE, TRUE);
+  gtk_ctree_node_set_row_data(GTK_CTREE(cts.ctree), ct_node,
+               GINT_TO_POINTER(cts.page));
+  cts.page++;
   
   /* TCP Streams prefs */
+  frame = gtk_frame_new("TCP Streams");
+  gtk_widget_show(GTK_WIDGET(frame));
   stream_pg = stream_prefs_show();
+  gtk_container_add(GTK_CONTAINER(frame), stream_pg);
   gtk_object_set_data(GTK_OBJECT(prefs_w), E_STREAM_PAGE_KEY, stream_pg);
-  label = gtk_label_new ("TCP Streams");
-  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), stream_pg, label);
+  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), frame, NULL);
+  strcpy(label_str, "TCP Streams");
+  ct_node = gtk_ctree_insert_node(GTK_CTREE(cts.ctree), NULL, NULL, 
+               &label_ptr, 5, NULL, NULL, NULL, NULL, TRUE, TRUE);
+  gtk_ctree_node_set_row_data(GTK_CTREE(cts.ctree), ct_node,
+               GINT_TO_POINTER(cts.page));
+  cts.page++;
 
   /* GUI prefs */
+  frame = gtk_frame_new("User Interface");
+  gtk_widget_show(GTK_WIDGET(frame));
   gui_pg = gui_prefs_show();
+  gtk_container_add(GTK_CONTAINER(frame), gui_pg);
   gtk_object_set_data(GTK_OBJECT(prefs_w), E_GUI_PAGE_KEY, gui_pg);
-  label = gtk_label_new ("GUI");
-  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), gui_pg, label);
+  gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), frame, NULL);
+  strcpy(label_str, "User Interface");
+  ct_node = gtk_ctree_insert_node(GTK_CTREE(cts.ctree), NULL, NULL, 
+               &label_ptr, 5, NULL, NULL, NULL, NULL, TRUE, TRUE);
+  gtk_ctree_node_set_row_data(GTK_CTREE(cts.ctree), ct_node,
+               GINT_TO_POINTER(cts.page));
+  cts.page++;
+
 
   /* Registered prefs */
-  prefs_module_foreach(module_prefs_show, prefs_nb);
+  cts.notebook = prefs_nb;
+  strcpy(label_str, "Protocols");
+  cts.node = gtk_ctree_insert_node(GTK_CTREE(cts.ctree), NULL, NULL,
+               &label_ptr, 5, NULL, NULL, NULL, NULL, FALSE, FALSE);
+  gtk_ctree_node_set_row_data(GTK_CTREE(cts.ctree), cts.node,
+               GINT_TO_POINTER(-1));
+  gtk_ctree_node_set_selectable(GTK_CTREE(cts.ctree), cts.node, FALSE);
+
+  prefs_module_foreach(module_prefs_show, &cts);
+
 
   /* Button row: OK and cancel buttons */
   bbox = gtk_hbutton_box_new();
@@ -546,7 +645,8 @@ prefs_main_save_cb(GtkWidget *save_bt, gpointer parent_w)
 {
   gboolean must_redissect = FALSE;
   int err;
-  char *pf_path;
+  char *pf_dir_path;
+  const char *pf_path;
 
   /* Fetch the preferences (i.e., make sure all the values set in all of
      the preferences panes have been copied to "prefs" and the registered
@@ -557,12 +657,21 @@ prefs_main_save_cb(GtkWidget *save_bt, gpointer parent_w)
   gui_prefs_fetch(gtk_object_get_data(GTK_OBJECT(parent_w), E_GUI_PAGE_KEY));
   prefs_module_foreach(module_prefs_fetch, &must_redissect);
 
-  /* Write the preferencs out. */
-  err = write_prefs(&pf_path);
-  if (err != 0) {
+  /* Create the directory that holds personal configuration files, if
+     necessary.  */
+  if (create_persconffile_dir(&pf_dir_path) == -1) {
      simple_dialog(ESD_TYPE_WARN, NULL,
-      "Can't open preferences file\n\"%s\": %s.", pf_path,
-      strerror(err));
+      "Can't create directory\n\"%s\"\nfor preferences file: %s.", pf_dir_path,
+      strerror(errno));
+     g_free(pf_dir_path);
+  } else {
+    /* Write the preferencs out. */
+    err = write_prefs(&pf_path);
+    if (err != 0) {
+       simple_dialog(ESD_TYPE_WARN, NULL,
+        "Can't open preferences file\n\"%s\": %s.", pf_path,
+        strerror(err));
+    }
   }
 
   /* Now apply those preferences.
@@ -765,3 +874,15 @@ properties_cb(GtkWidget *w, gpointer dummy)
   prefs_module_foreach(module_search_properties, &p);
 
 }
+
+/* Prefs tree selection callback.  The node data has been loaded with 
+   the proper notebook page to load. */
+static void
+prefs_tree_select_cb(GtkCTree *ct, GtkCTreeNode *node, gint col, gpointer dummy)
+{
+  gint page = GPOINTER_TO_INT(gtk_ctree_node_get_row_data(ct, node));
+  
+  if (page >= 0) 
+    gtk_notebook_set_page(GTK_NOTEBOOK(notebook), page);
+}
+