Remove "text2pcap-scanner.obj" and "tools\lemon\lemon.obj" when a "nmake
[obnox/wireshark/wip.git] / gtk / progress_dlg.c
1 /* progress_dlg.c
2  * Routines for progress-bar (modal) dialog
3  *
4  * $Id: progress_dlg.c,v 1.9 2001/03/24 02:07:22 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
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.
15  * 
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.
20  * 
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "gtkglobals.h"
31 #include "dlg_utils.h"
32 #include "progress_dlg.h"
33
34 #define PROG_BAR_KEY    "progress_bar"
35
36 static gint delete_event_cb(GtkWidget *w, GdkEvent *event, gpointer data);
37 static void stop_cb(GtkWidget *w, gpointer data);
38
39 /*
40  * Define the structure describing a progress dialog.
41  */
42 struct progdlg {
43         GtkWidget *dlg_w;       /* top-level window widget */
44 };
45
46 /*
47  * Create and pop up the progress dialog; allocate a "progdlg_t"
48  * and initialize it to contain all information the implementation
49  * needs in order to manipulate the dialog, and return a pointer to
50  * it.
51  *
52  * The first argument is the title to give the dialog box; the second
53  * argument is the string to put in the "stop this operation" button;
54  * the third argument is a pointer to a Boolean variable that will be
55  * set to TRUE if the user hits that button.
56  *
57  * XXX - provide a way to specify the progress in units, with the total
58  * number of units specified as an argument when the progress dialog
59  * is created; updates would be given in units, with the progress dialog
60  * code computing the percentage, and the progress bar would have a
61  * label "0" on the left and <total number of units> on the right, with
62  * a label in the middle giving the number of units we've processed
63  * so far.  This could be used when filtering packets, for example; we
64  * wouldn't always use it, as we have no idea how many packets are to
65  * be read.
66  */
67 progdlg_t *
68 create_progress_dlg(const gchar *title, const gchar *stop_title,
69     gboolean *stop_flag)
70 {
71         progdlg_t *dlg;
72         GtkWidget *dlg_w, *main_vb, *title_lb, *prog_bar, *bbox, *stop_bt;
73
74         dlg = g_malloc(sizeof (progdlg_t));
75
76         dlg_w = dlg_window_new(title);
77         gtk_window_set_modal(GTK_WINDOW(dlg_w), TRUE);
78
79         /*
80          * Container for dialog widgets.
81          */
82         main_vb = gtk_vbox_new(FALSE, 1);
83         gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
84         gtk_container_add(GTK_CONTAINER(dlg_w), main_vb);
85
86         /*
87          * Put the title here as a label indicating what we're
88          * doing; set its alignment and padding so it's aligned on the
89          * left.
90          */
91         title_lb = gtk_label_new(title);
92         gtk_box_pack_start(GTK_BOX(main_vb), title_lb, FALSE, TRUE, 3);
93         gtk_misc_set_alignment(GTK_MISC(title_lb), 0.0, 0.0);
94         gtk_misc_set_padding(GTK_MISC(title_lb), 0.0, 0.0);
95
96         /*
97          * The progress bar.
98          */
99         prog_bar = gtk_progress_bar_new();
100         gtk_progress_set_activity_mode(GTK_PROGRESS(prog_bar), FALSE);
101         gtk_box_pack_start(GTK_BOX(main_vb), prog_bar, FALSE, TRUE, 3);
102
103         /*
104          * Attach a pointer to the progress bar widget to the top-level
105          * widget.
106          */
107         gtk_object_set_data(GTK_OBJECT(dlg_w), PROG_BAR_KEY, prog_bar);
108
109         /*
110          * Button row: cancel button.
111          * (We put it in an HButtonBox, even though there's only one
112          * of them, so that it doesn't expand to the width of the window.)
113          */
114         bbox = gtk_hbutton_box_new();
115         gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
116         gtk_container_add(GTK_CONTAINER(main_vb), bbox);
117   
118         /*
119          * Allow user to either click a "stop this operation" button, or
120          * the close button on the window, to stop an operation in
121          * progress.
122          */
123         stop_bt = gtk_button_new_with_label(stop_title);
124         gtk_box_pack_start(GTK_BOX (bbox), stop_bt, TRUE, TRUE, 0);
125         gtk_signal_connect(GTK_OBJECT(stop_bt), "clicked",
126             GTK_SIGNAL_FUNC(stop_cb), (gpointer) stop_flag);
127         gtk_signal_connect(GTK_OBJECT(dlg_w), "delete_event",
128             GTK_SIGNAL_FUNC(delete_event_cb), (gpointer) stop_flag);
129         GTK_WIDGET_SET_FLAGS(stop_bt, GTK_CAN_DEFAULT);
130         gtk_widget_grab_default(stop_bt);
131         GTK_WIDGET_SET_FLAGS(stop_bt, GTK_CAN_DEFAULT);
132         gtk_widget_grab_default(stop_bt);
133
134         gtk_widget_show_all(dlg_w);
135
136         dlg->dlg_w = dlg_w;
137
138         return dlg;
139 }
140
141 /*
142  * Called when the dialog box is to be deleted.
143  * Set the "stop" flag to TRUE, and return TRUE - we don't want the dialog
144  * box deleted now, our caller will do so when they see that the
145  * "stop" flag is TRUE and abort the operation.
146  */
147 static gint
148 delete_event_cb(GtkWidget *w, GdkEvent *event, gpointer data)
149 {
150         gboolean *stop_flag = (gboolean *) data;
151   
152         *stop_flag = TRUE;
153         return TRUE;
154 }
155
156 /*
157  * Called when the "stop this operation" button is clicked.
158  * Set the "stop" flag to TRUE; we don't have to destroy the dialog
159  * box, as our caller will do so when they see that the "stop" flag is
160  * true and abort the operation.
161  */
162 static void
163 stop_cb(GtkWidget *w, gpointer data)
164 {
165         gboolean *stop_flag = (gboolean *) data;
166   
167         *stop_flag = TRUE;
168 }
169
170 /*
171  * Set the percentage value of the progress bar.
172  */
173 void
174 update_progress_dlg(progdlg_t *dlg, gfloat percentage)
175 {
176         GtkWidget *dlg_w = dlg->dlg_w;
177         GtkWidget *prog_bar;
178
179         prog_bar = gtk_object_get_data(GTK_OBJECT(dlg_w), PROG_BAR_KEY);
180         gtk_progress_bar_update(GTK_PROGRESS_BAR(prog_bar), percentage);
181
182         /*
183          * Flush out the update and process any input events.
184          */
185         while (gtk_events_pending())
186                 gtk_main_iteration();
187 }
188
189 /*
190  * Destroy the progress dialog.
191  */
192 void
193 destroy_progress_dlg(progdlg_t *dlg)
194 {
195         GtkWidget *dlg_w = dlg->dlg_w;
196
197         gtk_widget_destroy(GTK_WIDGET(dlg_w));
198         g_free(dlg);
199 }