Give the code that computes protocol statistics a progress dialog box,
[obnox/wireshark/wip.git] / gtk / progress_dlg.c
index 63c0b694b1f743640514e98bf563f1954eed0672..6df6b72a469edf4d3541de9095fa3804a46b3e87 100644 (file)
@@ -1,7 +1,7 @@
 /* progress_dlg.c
  * Routines for progress-bar (modal) dialog
  *
- * $Id: progress_dlg.c,v 1.2 2000/07/03 19:42:36 guy Exp $
+ * $Id: progress_dlg.c,v 1.9 2001/03/24 02:07:22 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
 #endif
 
 #include "gtkglobals.h"
-#include "ui_util.h"
+#include "dlg_utils.h"
+#include "progress_dlg.h"
 
 #define        PROG_BAR_KEY    "progress_bar"
 
-static void delete_cb(GtkWidget *w, GdkEvent *event, gpointer data);
-static void cancel_cb(GtkWidget *w, gpointer data);
+static gint delete_event_cb(GtkWidget *w, GdkEvent *event, gpointer data);
+static void stop_cb(GtkWidget *w, gpointer data);
 
 /*
- * Create and pop up the progress dialog; return a pointer to it, as
- * a "void *", so that our caller doesn't have to know the GUI
- * implementation.
+ * Define the structure describing a progress dialog.
+ */
+struct progdlg {
+       GtkWidget *dlg_w;       /* top-level window widget */
+};
+
+/*
+ * Create and pop up the progress dialog; allocate a "progdlg_t"
+ * and initialize it to contain all information the implementation
+ * needs in order to manipulate the dialog, and return a pointer to
+ * it.
  *
  * The first argument is the title to give the dialog box; the second
- * argument is a pointer to a Boolean variable that will be set to
- * TRUE if the user hits the "Cancel" button.
- *
- * XXX - should the button say "Cancel", or "Stop"?
+ * argument is the string to put in the "stop this operation" button;
+ * the third argument is a pointer to a Boolean variable that will be
+ * set to TRUE if the user hits that button.
  *
  * XXX - provide a way to specify the progress in units, with the total
  * number of units specified as an argument when the progress dialog
@@ -56,14 +64,17 @@ static void cancel_cb(GtkWidget *w, gpointer data);
  * wouldn't always use it, as we have no idea how many packets are to
  * be read.
  */
-void *
-create_progress_dlg(gchar *title, gboolean *stop_flag)
+progdlg_t *
+create_progress_dlg(const gchar *title, const gchar *stop_title,
+    gboolean *stop_flag)
 {
-       GtkWidget *dlg_w, *main_vb, *title_lb, *prog_bar, *cancel_al,
-                 *cancel_bt;
+       progdlg_t *dlg;
+       GtkWidget *dlg_w, *main_vb, *title_lb, *prog_bar, *bbox, *stop_bt;
 
-       dlg_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_title(GTK_WINDOW(dlg_w), title);
+       dlg = g_malloc(sizeof (progdlg_t));
+
+       dlg_w = dlg_window_new(title);
+       gtk_window_set_modal(GTK_WINDOW(dlg_w), TRUE);
 
        /*
         * Container for dialog widgets.
@@ -71,7 +82,6 @@ create_progress_dlg(gchar *title, gboolean *stop_flag)
        main_vb = gtk_vbox_new(FALSE, 1);
        gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
        gtk_container_add(GTK_CONTAINER(dlg_w), main_vb);
-       gtk_widget_show(main_vb);
 
        /*
         * Put the title here as a label indicating what we're
@@ -82,7 +92,6 @@ create_progress_dlg(gchar *title, gboolean *stop_flag)
        gtk_box_pack_start(GTK_BOX(main_vb), title_lb, FALSE, TRUE, 3);
        gtk_misc_set_alignment(GTK_MISC(title_lb), 0.0, 0.0);
        gtk_misc_set_padding(GTK_MISC(title_lb), 0.0, 0.0);
-       gtk_widget_show(title_lb);
 
        /*
         * The progress bar.
@@ -90,7 +99,6 @@ create_progress_dlg(gchar *title, gboolean *stop_flag)
        prog_bar = gtk_progress_bar_new();
        gtk_progress_set_activity_mode(GTK_PROGRESS(prog_bar), FALSE);
        gtk_box_pack_start(GTK_BOX(main_vb), prog_bar, FALSE, TRUE, 3);
-       gtk_widget_show(prog_bar);
 
        /*
         * Attach a pointer to the progress bar widget to the top-level
@@ -99,71 +107,73 @@ create_progress_dlg(gchar *title, gboolean *stop_flag)
        gtk_object_set_data(GTK_OBJECT(dlg_w), PROG_BAR_KEY, prog_bar);
 
        /*
-        * Allow user to either click a "Cancel" button, or the close button
-        * on the window, to stop an operation in progress.
-        *
-        * Put the button in an alignment so it doesn't get any wider than
-        * it needs to to say "Cancel".
+        * Button row: cancel button.
+        * (We put it in an HButtonBox, even though there's only one
+        * of them, so that it doesn't expand to the width of the window.)
+        */
+       bbox = gtk_hbutton_box_new();
+       gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
+       gtk_container_add(GTK_CONTAINER(main_vb), bbox);
+  
+       /*
+        * Allow user to either click a "stop this operation" button, or
+        * the close button on the window, to stop an operation in
+        * progress.
         */
-       cancel_al = gtk_alignment_new(0.5, 0.0, 0.0, 0.0);
-       cancel_bt = gtk_button_new_with_label("Cancel");
-       gtk_container_add(GTK_CONTAINER(cancel_al), cancel_bt);
-       gtk_signal_connect(GTK_OBJECT(cancel_bt), "clicked",
-           GTK_SIGNAL_FUNC(cancel_cb), (gpointer) stop_flag);
+       stop_bt = gtk_button_new_with_label(stop_title);
+       gtk_box_pack_start(GTK_BOX (bbox), stop_bt, TRUE, TRUE, 0);
+       gtk_signal_connect(GTK_OBJECT(stop_bt), "clicked",
+           GTK_SIGNAL_FUNC(stop_cb), (gpointer) stop_flag);
        gtk_signal_connect(GTK_OBJECT(dlg_w), "delete_event",
-           GTK_SIGNAL_FUNC(delete_cb), (gpointer) stop_flag);
-       gtk_box_pack_end(GTK_BOX(main_vb), cancel_al, FALSE, FALSE, 3);
-       GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
-       gtk_widget_grab_default(cancel_bt);
-       GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
-       gtk_widget_grab_default(cancel_bt);
-       gtk_widget_show(cancel_bt);
-       gtk_widget_show(cancel_al);
-
-       gtk_widget_show(dlg_w);
-       gtk_grab_add(dlg_w);            /* make it modal */
-
-       return dlg_w;   /* return as opaque pointer */
+           GTK_SIGNAL_FUNC(delete_event_cb), (gpointer) stop_flag);
+       GTK_WIDGET_SET_FLAGS(stop_bt, GTK_CAN_DEFAULT);
+       gtk_widget_grab_default(stop_bt);
+       GTK_WIDGET_SET_FLAGS(stop_bt, GTK_CAN_DEFAULT);
+       gtk_widget_grab_default(stop_bt);
+
+       gtk_widget_show_all(dlg_w);
+
+       dlg->dlg_w = dlg_w;
+
+       return dlg;
 }
 
 /*
  * Called when the dialog box is to be deleted.
- * We just treat this the same way we treat clicking the "Cancel" button.
+ * Set the "stop" flag to TRUE, and return TRUE - we don't want the dialog
+ * box deleted now, our caller will do so when they see that the
+ * "stop" flag is TRUE and abort the operation.
  */
-static void
-delete_cb(GtkWidget *w, GdkEvent *event, gpointer data)
+static gint
+delete_event_cb(GtkWidget *w, GdkEvent *event, gpointer data)
 {
-       cancel_cb(NULL, data);
+       gboolean *stop_flag = (gboolean *) data;
+  
+       *stop_flag = TRUE;
+       return TRUE;
 }
 
 /*
- * Called when the "Cancel" button is clicked.
- * Set the Boolean to TRUE.
+ * Called when the "stop this operation" button is clicked.
+ * Set the "stop" flag to TRUE; we don't have to destroy the dialog
+ * box, as our caller will do so when they see that the "stop" flag is
+ * true and abort the operation.
  */
 static void
-cancel_cb(GtkWidget *w, gpointer data)
+stop_cb(GtkWidget *w, gpointer data)
 {
        gboolean *stop_flag = (gboolean *) data;
-       GtkWidget *toplevel;
   
        *stop_flag = TRUE;
-       if (w != NULL) {
-               /*
-                * The cancel button was clicked, so we have to destroy
-                * the dialog box ourselves.
-                */
-               toplevel = gtk_widget_get_toplevel(w);
-               destroy_progress_dlg(toplevel);
-       }
 }
 
 /*
  * Set the percentage value of the progress bar.
  */
 void
-update_progress_dlg(void *dlg, gfloat percentage)
+update_progress_dlg(progdlg_t *dlg, gfloat percentage)
 {
-       GtkWidget *dlg_w = dlg;
+       GtkWidget *dlg_w = dlg->dlg_w;
        GtkWidget *prog_bar;
 
        prog_bar = gtk_object_get_data(GTK_OBJECT(dlg_w), PROG_BAR_KEY);
@@ -177,13 +187,13 @@ update_progress_dlg(void *dlg, gfloat percentage)
 }
 
 /*
- * Destroy the progress bar.
+ * Destroy the progress dialog.
  */
 void
-destroy_progress_dlg(void *dlg)
+destroy_progress_dlg(progdlg_t *dlg)
 {
-       GtkWidget *dlg_w = dlg;
+       GtkWidget *dlg_w = dlg->dlg_w;
 
-       gtk_grab_remove(GTK_WIDGET(dlg_w));
        gtk_widget_destroy(GTK_WIDGET(dlg_w));
+       g_free(dlg);
 }