Avoid calling some free() on WIN32 on memory that may be allocated in
[obnox/wireshark/wip.git] / gtk / dlg_utils.c
index 747db9e9cd592cc175400730dee18b915a71d5ee..d2455c030e2bac5013bb106f90b3696f5653989f 100644 (file)
@@ -1,10 +1,10 @@
 /* dlg_utils.c
  * Utilities to use when constructing dialogs
  *
- * $Id: dlg_utils.c,v 1.24 2004/03/29 22:55:13 guy Exp $
+ * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
 #include <gdk/gdkkeysyms.h>
 
 #include "gtkglobals.h"
-#include "ui_util.h"
+#include "gui_utils.h"
 #include "dlg_utils.h"
 #include "compat_macros.h"
 
 #include <string.h>
-#include <stdio.h>
 #include <stdarg.h>
 
 static void
 dlg_activate (GtkWidget *widget, gpointer ok_button);
 
-static gint
-dlg_key_press (GtkWidget *widget, GdkEventKey *event, gpointer cancel_button);
-
-
-
 /* create a button for the button row (helper for dlg_button_row_new) */
 static GtkWidget *
-dlg_button_new(GtkWidget *hbox, GtkWidget *button_hbox, gchar *stock_id)
+dlg_button_new(GtkWidget *hbox, GtkWidget *button_hbox, const gchar *stock_id)
 {
     GtkWidget *button;
 
@@ -60,11 +54,41 @@ dlg_button_new(GtkWidget *hbox, GtkWidget *button_hbox, gchar *stock_id)
     return button;
 }
 
+/*
+ * Set the focus and default for the nth item in a button row, with
+ * 0 being the first item.
+ */
+#define BUTTON_HBOX_KEY "button_hbox"
+void
+dlg_button_focus_nth(GtkWidget *hbox, gint focus_item) {
+    GtkWidget *button_hbox, *button;
+    GList *children;
+    gint cur_item = 0;
+
+    if (!hbox)
+       return;
+
+    button_hbox = OBJECT_GET_DATA(hbox, BUTTON_HBOX_KEY);
+    children = gtk_container_children(GTK_CONTAINER(button_hbox));
+
+    while (children) {
+       if (cur_item == focus_item) {
+           button = children->data;
+           gtk_widget_grab_focus(button);
+           gtk_widget_grab_default(button);
+           break;
+       }
+       children = g_list_next(children);
+       cur_item++;
+    }
+
+    g_list_free(children);
+}
 
 /* create a button row for a dialog */
 
-/* The purpose of this is, to have one place available, where all button rows 
- * from all dialogs are layouted. This will:
+/* The purpose of this is, to have one place available, where all button rows
+ * from all dialogs are laid out. This will:
  *
  * a.) keep the button layout more consistent over the different dialogs
  * b.) being able to switch between different button layouts, e.g.:
@@ -72,30 +96,36 @@ dlg_button_new(GtkWidget *hbox, GtkWidget *button_hbox, gchar *stock_id)
  *     GTK2 (e.g. GNOME) "Apply" "Cancel" "OK"
  */
 GtkWidget *
-dlg_button_row_new(gchar *stock_id_first, ...)
+dlg_button_row_new(const gchar *stock_id_first, ...)
 {
     gint        buttons = 0;
     va_list     stock_id_list;
-    gchar       *stock_id = stock_id_first;
+    const gchar *stock_id = stock_id_first;
     GtkWidget   *hbox;
     GtkWidget   *button_hbox;
     GtkWidget   *help_hbox;
     GtkWidget   *button;
 
-    gchar *ok           = NULL;
-    gchar *apply        = NULL;
-    gchar *save         = NULL;
-    gchar *cancel       = NULL;
-    gchar *close        = NULL;
-    gchar *clear        = NULL;
-    gchar *stop         = NULL;
-    gchar *create_stat  = NULL;
-    gchar *help         = NULL;
-    gchar *print        = NULL;
-    gchar *find         = NULL;
-    gchar *jump         = NULL;
-    gchar *yes          = NULL;
-    gchar *no           = NULL;
+    const gchar *apply        = NULL;
+    const gchar *cancel       = NULL;
+    const gchar *cap_start    = NULL;
+    const gchar *cap_stop     = NULL;
+    const gchar *clear        = NULL;
+    const gchar *close        = NULL;
+    const gchar *copy         = NULL;
+    const gchar *create_stat  = NULL;
+    const gchar *delete       = NULL;
+    const gchar *dont_save    = NULL;
+    const gchar *filter_stream= NULL;
+    const gchar *find         = NULL;
+    const gchar *help         = NULL;
+    const gchar *jump         = NULL;
+    const gchar *no           = NULL;
+    const gchar *ok           = NULL;
+    const gchar *print        = NULL;
+    const gchar *save         = NULL;
+    const gchar *stop         = NULL;
+    const gchar *yes          = NULL;
 
 
     va_start(stock_id_list, stock_id_first);
@@ -104,18 +134,26 @@ dlg_button_row_new(gchar *stock_id_first, ...)
     while(stock_id != NULL) {
         if (strcmp(stock_id, GTK_STOCK_OK) == 0) {
             ok = stock_id;
-        } else if (strcmp(stock_id, ETHEREAL_STOCK_CREATE_STAT) == 0) {
+        } else if (strcmp(stock_id, WIRESHARK_STOCK_CREATE_STAT) == 0) {
             create_stat = stock_id;
         } else if (strcmp(stock_id, GTK_STOCK_APPLY) == 0) {
             apply = stock_id;
         } else if (strcmp(stock_id, GTK_STOCK_SAVE) == 0) {
             save = stock_id;
+        } else if (strcmp(stock_id, WIRESHARK_STOCK_DONT_SAVE) == 0) {
+               dont_save = stock_id;
         } else if (strcmp(stock_id, GTK_STOCK_CANCEL) == 0) {
             cancel = stock_id;
         } else if (strcmp(stock_id, GTK_STOCK_CLOSE) == 0) {
             close = stock_id;
         } else if (strcmp(stock_id, GTK_STOCK_CLEAR) == 0) {
             clear = stock_id;
+#ifdef HAVE_LIBPCAP
+        } else if (strcmp(stock_id, WIRESHARK_STOCK_CAPTURE_START) == 0) {
+            cap_start = stock_id;
+        } else if (strcmp(stock_id, WIRESHARK_STOCK_CAPTURE_STOP) == 0) {
+            cap_stop = stock_id;
+#endif /* HAVE_LIBPCAP */
         } else if (strcmp(stock_id, GTK_STOCK_STOP) == 0) {
             stop = stock_id;
         } else if (strcmp(stock_id, GTK_STOCK_HELP) == 0) {
@@ -130,6 +168,12 @@ dlg_button_row_new(gchar *stock_id_first, ...)
             yes = stock_id;
         } else if (strcmp(stock_id, GTK_STOCK_NO) == 0) {
             no = stock_id;
+        } else if (strcmp(stock_id, WIRESHARK_STOCK_FILTER_OUT_STREAM) == 0) {
+            filter_stream = stock_id;
+        } else if (strcmp(stock_id, GTK_STOCK_DELETE) == 0) {
+            delete = stock_id;
+        } else if (strcmp(stock_id, GTK_STOCK_COPY) == 0) {
+            copy = stock_id;
         } else {
             /* we don't know that button! */
             g_assert_not_reached();
@@ -139,21 +183,23 @@ dlg_button_row_new(gchar *stock_id_first, ...)
     }
     va_end(stock_id_list);
 
-    /* we should have at least one button */
-    g_assert(buttons);
-
-
     hbox = gtk_hbox_new(FALSE, 0);
     gtk_widget_show(hbox);
 
     button_hbox = gtk_hbutton_box_new();
     gtk_box_pack_end(GTK_BOX(hbox), button_hbox, TRUE, TRUE, 0);
+    OBJECT_SET_DATA(hbox, BUTTON_HBOX_KEY, button_hbox);
     gtk_widget_show(button_hbox);
 
     help_hbox = gtk_hbutton_box_new();
     gtk_box_pack_end(GTK_BOX(hbox), help_hbox, FALSE, FALSE, 0);
     gtk_widget_show(help_hbox);
 
+    if (buttons == 0) {
+        /* if no buttons wanted, simply do nothing */
+        return hbox;
+    }
+
     if (buttons == 1) {
         /* if only one button, simply put it in the middle (default) */
         dlg_button_new(hbox, button_hbox, stock_id_first);
@@ -175,7 +221,8 @@ dlg_button_row_new(gchar *stock_id_first, ...)
     gtk_button_box_set_layout (GTK_BUTTON_BOX(button_hbox), GTK_BUTTONBOX_END);
     gtk_button_box_set_spacing(GTK_BUTTON_BOX(button_hbox), 5);
 
-#if !WIN32 && GTK_MAJOR_VERSION >= 2
+/* GTK+ 1.3 and later - on Win32, we use 1.3[.x] or 2.x, not 1.2[.x] */
+#if !defined(_WIN32) && GTK_MAJOR_VERSION >= 2
     /* beware: sequence of buttons are important! */
 
     /* XXX: this can be implemented more elegant of course, but it works as it should */
@@ -220,6 +267,21 @@ dlg_button_row_new(gchar *stock_id_first, ...)
             dlg_button_new(hbox, button_hbox, create_stat);
             return hbox;
         }
+        if (cap_start && cancel) {
+            dlg_button_new(hbox, button_hbox, cancel);
+            dlg_button_new(hbox, button_hbox, cap_start);
+            return hbox;
+        }
+        if (cap_stop && cancel) {
+            dlg_button_new(hbox, button_hbox, cancel);
+            dlg_button_new(hbox, button_hbox, cap_stop);
+            return hbox;
+        }
+        if (delete && cancel) {
+            dlg_button_new(hbox, button_hbox, cancel);
+            dlg_button_new(hbox, button_hbox, delete);
+            return hbox;
+        }
     }
     if (buttons == 3) {
         if (ok && save && close) {
@@ -241,11 +303,17 @@ dlg_button_row_new(gchar *stock_id_first, ...)
             return hbox;
         }
         if (yes && no && cancel) {
-            dlg_button_new(hbox, button_hbox, yes);
             dlg_button_new(hbox, button_hbox, no);
             dlg_button_new(hbox, button_hbox, cancel);
+            dlg_button_new(hbox, button_hbox, yes);
             return hbox;
         }
+        if (save && dont_save && cancel) {
+               dlg_button_new(hbox, button_hbox, dont_save);
+               dlg_button_new(hbox, button_hbox, cancel);
+               dlg_button_new(hbox, button_hbox, save);
+               return hbox;
+        }
     }
     if (buttons == 4) {
         if (ok && apply && save && cancel) {
@@ -267,18 +335,24 @@ dlg_button_row_new(gchar *stock_id_first, ...)
 
     /* beware: sequence of buttons is important! */
     if (ok      != NULL) dlg_button_new(hbox, button_hbox, ok);
+    if (delete  != NULL) dlg_button_new(hbox, button_hbox, delete);
     if (jump    != NULL) dlg_button_new(hbox, button_hbox, jump);
     if (find    != NULL) dlg_button_new(hbox, button_hbox, find);
     if (print   != NULL) dlg_button_new(hbox, button_hbox, print);
+    if (copy    != NULL) dlg_button_new(hbox, button_hbox, copy);
     if (create_stat != NULL) dlg_button_new(hbox, button_hbox, create_stat);
     if (apply   != NULL) dlg_button_new(hbox, button_hbox, apply);
     if (yes     != NULL) dlg_button_new(hbox, button_hbox, yes);
     if (no      != NULL) dlg_button_new(hbox, button_hbox, no);
     if (save    != NULL) dlg_button_new(hbox, button_hbox, save);
+    if (dont_save   != NULL) dlg_button_new(hbox, button_hbox, dont_save);
+    if (cap_start   != NULL) dlg_button_new(hbox, button_hbox, cap_start);
+    if (cap_stop    != NULL) dlg_button_new(hbox, button_hbox, cap_stop);
     if (stop    != NULL) dlg_button_new(hbox, button_hbox, stop);
     if (close   != NULL) dlg_button_new(hbox, button_hbox, close);
     if (clear   != NULL) dlg_button_new(hbox, button_hbox, clear);
     if (cancel  != NULL) dlg_button_new(hbox, button_hbox, cancel);
+    if (filter_stream!= NULL) dlg_button_new(hbox, button_hbox, filter_stream);
 
     /* GTK2: we don't know that button combination, add it to the above list! */
     /* g_assert_not_reached(); */
@@ -286,7 +360,20 @@ dlg_button_row_new(gchar *stock_id_first, ...)
 }
 
 
-/* Create a dialog box window that belongs to Ethereal's main window. */
+/* this is called, when a dialog was closed */
+static void dlg_destroy_cb(GtkWidget *dialog _U_, gpointer data        _U_)
+{
+#if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
+    if(top_level) {
+        /* bring main window back to front (workaround for a bug in win32 GTK2.x)
+           XXX - do this only on Windows? */
+        gtk_window_present(GTK_WINDOW(top_level));
+    }
+#endif
+}
+
+
+/* Create a dialog box window that belongs to Wireshark's main window. */
 GtkWidget *
 dlg_window_new(const gchar *title)
 {
@@ -297,6 +384,7 @@ dlg_window_new(const gchar *title)
 #else
   win = window_new(GTK_WINDOW_TOPLEVEL, title);
 #endif
+
   /*
    * XXX - if we're running in the capture child process, we can't easily
    * make this window transient for the main process's window.  We just
@@ -314,82 +402,11 @@ dlg_window_new(const gchar *title)
   if (top_level) {
     gtk_window_set_transient_for(GTK_WINDOW(win), GTK_WINDOW(top_level));
   }
-#if GTK_MAJOR_VERSION >= 2
-  gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER_ON_PARENT);
-#endif
-  return win;
-}
-
-/* Create a file selection dialog box window that belongs to Ethereal's
-   main window. */
-#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
-GtkWidget *
-file_selection_new(const gchar *title, file_selection_action_t action)
-{
-  GtkWidget *win;
-  GtkFileChooserAction gtk_action;
-
-  switch (action) {
-
-  case FILE_SELECTION_OPEN:
-    gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
-    break;
 
-  case FILE_SELECTION_SAVE:
-    gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
-    break;
-
-  default:
-    g_assert_not_reached();
-    gtk_action = -1;
-    break;
-  }
-  win = gtk_file_chooser_dialog_new(title, GTK_WINDOW(top_level), gtk_action,
-                                    GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
-                                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-                                    NULL);
-  gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER_ON_PARENT);
-  return win;
-}
-#else
-GtkWidget *
-file_selection_new(const gchar *title, file_selection_action_t action _U_)
-{
-  GtkWidget *win;
+  SIGNAL_CONNECT(win, "destroy", dlg_destroy_cb, NULL);
 
-  win = gtk_file_selection_new(title);
-#if GTK_MAJOR_VERSION >= 2
-  gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER_ON_PARENT);
-#endif
-  gtk_window_set_transient_for(GTK_WINDOW(win), GTK_WINDOW(top_level));
   return win;
 }
-#endif
-
-/* Set the current folder for a file selection dialog. */
-gboolean
-file_selection_set_current_folder(GtkWidget *fs, const gchar *filename)
-{
-#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
-  return gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fs), filename);
-#else
-  gtk_file_selection_set_filename(GTK_FILE_SELECTION(fs), filename);
-  return TRUE;
-#endif
-}
-
-/* Set the "extra" widget for a file selection dialog, with user-supplied
-   options. */
-void
-file_selection_set_extra_widget(GtkWidget *fs, GtkWidget *extra)
-{
-#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
-  gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(fs), extra);
-#else
-  gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(fs)->action_area), extra,
-                     FALSE, FALSE, 0);
-#endif
-}
 
 /* Set the "activate" signal for a widget to call a routine to
    activate the "OK" button for a dialog box.
@@ -412,37 +429,6 @@ dlg_activate (GtkWidget *widget _U_, gpointer ok_button)
   gtk_widget_activate(GTK_WIDGET(ok_button));
 }
 
-/* Set the "key_press_event" signal for a top-level dialog window to
-   call a routine to activate the "Cancel" button for a dialog box if
-   the key being pressed is the <Esc> key.
-
-   XXX - there should be a GTK+ widget that'll do that for you, and
-   let you specify a "Cancel" button.  It should also not impose
-   a requirement that there be a separator in the dialog box, as
-   the GtkDialog widget does; the visual convention that there's
-   such a separator between the rest of the dialog boxes and buttons
-   such as "OK" and "Cancel" is, for better or worse, not universal
-   (not even in GTK+ - look at the GtkFileSelection dialog!). */
-void
-dlg_set_cancel(GtkWidget *widget, GtkWidget *cancel_button)
-{
-  SIGNAL_CONNECT(widget, "key_press_event", dlg_key_press, cancel_button);
-}
-
-static gint
-dlg_key_press (GtkWidget *widget, GdkEventKey *event, gpointer cancel_button)
-{
-  g_return_val_if_fail (widget != NULL, FALSE);
-  g_return_val_if_fail (event != NULL, FALSE);
-
-  if (event->keyval == GDK_Escape) {
-    gtk_widget_activate(GTK_WIDGET(cancel_button));
-    return TRUE;
-  }
-
-  return FALSE;
-}
-
 #if GTK_MAJOR_VERSION < 2
 /* Sigh.  GTK+ appears not to acknowledge that it should be possible
    to attach mnemonics to anything other than menu items; provide