2 * Routines for progress-bar (modal) dialog
4 * $Id: progress_dlg.c,v 1.3 2000/07/05 02:45:41 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
30 #include "gtkglobals.h"
31 #include "dlg_utils.h"
34 #define PROG_BAR_KEY "progress_bar"
36 static void delete_cb(GtkWidget *w, GdkEvent *event, gpointer data);
37 static void cancel_cb(GtkWidget *w, gpointer data);
40 * Create and pop up the progress dialog; return a pointer to it, as
41 * a "void *", so that our caller doesn't have to know the GUI
44 * The first argument is the title to give the dialog box; the second
45 * argument is a pointer to a Boolean variable that will be set to
46 * TRUE if the user hits the "Cancel" button.
48 * XXX - should the button say "Cancel", or "Stop"?
50 * XXX - provide a way to specify the progress in units, with the total
51 * number of units specified as an argument when the progress dialog
52 * is created; updates would be given in units, with the progress dialog
53 * code computing the percentage, and the progress bar would have a
54 * label "0" on the left and <total number of units> on the right, with
55 * a label in the middle giving the number of units we've processed
56 * so far. This could be used when filtering packets, for example; we
57 * wouldn't always use it, as we have no idea how many packets are to
61 create_progress_dlg(gchar *title, gboolean *stop_flag)
63 GtkWidget *dlg_w, *main_vb, *title_lb, *prog_bar, *cancel_al,
66 dlg_w = dlg_window_new();
67 gtk_window_set_title(GTK_WINDOW(dlg_w), title);
70 * Container for dialog widgets.
72 main_vb = gtk_vbox_new(FALSE, 1);
73 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
74 gtk_container_add(GTK_CONTAINER(dlg_w), main_vb);
75 gtk_widget_show(main_vb);
78 * Put the title here as a label indicating what we're
79 * doing; set its alignment and padding so it's aligned on the
82 title_lb = gtk_label_new(title);
83 gtk_box_pack_start(GTK_BOX(main_vb), title_lb, FALSE, TRUE, 3);
84 gtk_misc_set_alignment(GTK_MISC(title_lb), 0.0, 0.0);
85 gtk_misc_set_padding(GTK_MISC(title_lb), 0.0, 0.0);
86 gtk_widget_show(title_lb);
91 prog_bar = gtk_progress_bar_new();
92 gtk_progress_set_activity_mode(GTK_PROGRESS(prog_bar), FALSE);
93 gtk_box_pack_start(GTK_BOX(main_vb), prog_bar, FALSE, TRUE, 3);
94 gtk_widget_show(prog_bar);
97 * Attach a pointer to the progress bar widget to the top-level
100 gtk_object_set_data(GTK_OBJECT(dlg_w), PROG_BAR_KEY, prog_bar);
103 * Allow user to either click a "Cancel" button, or the close button
104 * on the window, to stop an operation in progress.
106 * Put the button in an alignment so it doesn't get any wider than
107 * it needs to to say "Cancel".
109 cancel_al = gtk_alignment_new(0.5, 0.0, 0.0, 0.0);
110 cancel_bt = gtk_button_new_with_label("Cancel");
111 gtk_container_add(GTK_CONTAINER(cancel_al), cancel_bt);
112 gtk_signal_connect(GTK_OBJECT(cancel_bt), "clicked",
113 GTK_SIGNAL_FUNC(cancel_cb), (gpointer) stop_flag);
114 gtk_signal_connect(GTK_OBJECT(dlg_w), "delete_event",
115 GTK_SIGNAL_FUNC(delete_cb), (gpointer) stop_flag);
116 gtk_box_pack_end(GTK_BOX(main_vb), cancel_al, FALSE, FALSE, 3);
117 GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
118 gtk_widget_grab_default(cancel_bt);
119 GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
120 gtk_widget_grab_default(cancel_bt);
121 gtk_widget_show(cancel_bt);
122 gtk_widget_show(cancel_al);
124 gtk_widget_show(dlg_w);
125 gtk_grab_add(dlg_w); /* make it modal */
127 return dlg_w; /* return as opaque pointer */
131 * Called when the dialog box is to be deleted.
132 * We just treat this the same way we treat clicking the "Cancel" button.
135 delete_cb(GtkWidget *w, GdkEvent *event, gpointer data)
137 cancel_cb(NULL, data);
141 * Called when the "Cancel" button is clicked.
142 * Set the Boolean to TRUE.
145 cancel_cb(GtkWidget *w, gpointer data)
147 gboolean *stop_flag = (gboolean *) data;
153 * The cancel button was clicked, so we have to destroy
154 * the dialog box ourselves.
156 toplevel = gtk_widget_get_toplevel(w);
157 destroy_progress_dlg(toplevel);
162 * Set the percentage value of the progress bar.
165 update_progress_dlg(void *dlg, gfloat percentage)
167 GtkWidget *dlg_w = dlg;
170 prog_bar = gtk_object_get_data(GTK_OBJECT(dlg_w), PROG_BAR_KEY);
171 gtk_progress_bar_update(GTK_PROGRESS_BAR(prog_bar), percentage);
174 * Flush out the update and process any input events.
176 while (gtk_events_pending())
177 gtk_main_iteration();
181 * Destroy the progress bar.
184 destroy_progress_dlg(void *dlg)
186 GtkWidget *dlg_w = dlg;
188 gtk_grab_remove(GTK_WIDGET(dlg_w));
189 gtk_widget_destroy(GTK_WIDGET(dlg_w));