fix #568: disable event "activate" handler for the range entry field. see the added...
[obnox/wireshark/wip.git] / gtk / progress_dlg.c
1 /* progress_dlg.c
2  * Routines for progress-bar (modal) dialog
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32 #include "gtkglobals.h"
33 #include "dlg_utils.h"
34 #include "gui_utils.h"
35 #include "progress_dlg.h"
36 #include "compat_macros.h"
37
38 #define PROG_BAR_KEY    "progress_bar"
39
40 static gint delete_event_cb(GtkWidget *w, GdkEvent *event, gpointer data);
41 static void stop_cb(GtkWidget *w, gpointer data);
42
43 /*
44  * Define the structure describing a progress dialog.
45  */
46 struct progdlg {
47         GtkWidget *dlg_w;       /* top-level window widget */
48         GTimeVal   start_time;
49         GTimeVal   last_time;   /* last time it was updated */
50         
51         GtkLabel  *status_lb;
52         GtkLabel  *elapsed_lb;
53         GtkLabel  *time_left_lb;
54         GtkLabel  *percentage_lb;
55         gchar     *title;
56 };
57
58 /*
59  * Create and pop up the progress dialog; allocate a "progdlg_t"
60  * and initialize it to contain all information the implementation
61  * needs in order to manipulate the dialog, and return a pointer to
62  * it.
63  *
64  * The first argument is the task to do, e.g. "Loading".
65  * The second argument is the item to do, e.g. "capture.cap".
66  * The third argument is TRUE if the "terminate this operation" button should
67  * be a "Stop" button (meaning that the operation is stopped, but not undone),
68  * and FALSE if it should be a "Cancel" button (meaning that it's stopped
69  * and anything it's done would be undone)
70  * The fourth argument is a pointer to a Boolean variable that will be
71  *   set to TRUE if the user hits that button.
72  *
73  * XXX - provide a way to specify the progress in units, with the total
74  * number of units specified as an argument when the progress dialog
75  * is created; updates would be given in units, with the progress dialog
76  * code computing the percentage, and the progress bar would have a
77  * label "0" on the left and <total number of units> on the right, with
78  * a label in the middle giving the number of units we've processed
79  * so far.  This could be used when filtering packets, for example; we
80  * wouldn't always use it, as we have no idea how many packets are to
81  * be read.
82  */
83 progdlg_t *
84 create_progress_dlg(const gchar *task_title, const gchar *item_title,
85                     gboolean terminate_is_stop, gboolean *stop_flag)
86 {
87     progdlg_t *dlg;
88     GtkWidget *dlg_w, *main_vb, *title_lb, *status_lb, *elapsed_lb, *time_left_lb, *percentage_lb;
89     GtkWidget *prog_bar, *bbox, *cancel_bt;
90     GtkWidget *static_vb, *tmp_lb, *main_hb, *dynamic_vb, *percentage_hb;
91     gchar     *task_title_dup;
92     gchar     *item_title_dup;
93
94     dlg = g_malloc(sizeof (progdlg_t));
95
96     /* limit the item_title to some reasonable length */
97     item_title_dup = g_strdup(item_title);
98     if (strlen(item_title_dup) > 110) {
99         strcpy(&item_title_dup[100], "...");
100     }
101
102     dlg->title = g_strdup_printf("%s: %s", task_title, item_title_dup);
103
104     dlg_w = dlg_window_new(dlg->title);
105     gtk_window_set_modal(GTK_WINDOW(dlg_w), TRUE);
106
107     /*
108      * Container for dialog widgets.
109      */
110     main_vb = gtk_vbox_new(FALSE, 1);
111     gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
112     gtk_container_add(GTK_CONTAINER(dlg_w), main_vb);
113
114     /*
115      * Static labels (left dialog side, labels aligned to the right)
116      */
117     static_vb = gtk_vbox_new(FALSE, 1);
118     task_title_dup = g_strdup_printf ("%s:", task_title);
119     tmp_lb = gtk_label_new(task_title_dup);
120     gtk_misc_set_alignment(GTK_MISC(tmp_lb), 1.0, 0.0);
121     gtk_box_pack_start(GTK_BOX(static_vb), tmp_lb, FALSE, TRUE, 3);
122     tmp_lb = gtk_label_new("Status:");
123     gtk_misc_set_alignment(GTK_MISC(tmp_lb), 1.0, 0.0);
124     gtk_box_pack_start(GTK_BOX(static_vb), tmp_lb, FALSE, TRUE, 3);
125     tmp_lb = gtk_label_new("Elapsed Time:");
126     gtk_misc_set_alignment(GTK_MISC(tmp_lb), 1.0, 0.0);
127     gtk_box_pack_start(GTK_BOX(static_vb), tmp_lb, FALSE, TRUE, 3);
128     tmp_lb = gtk_label_new("Time Left:");
129     gtk_misc_set_alignment(GTK_MISC(tmp_lb), 1.0, 0.0);
130     gtk_box_pack_start(GTK_BOX(static_vb), tmp_lb, FALSE, TRUE, 3);
131     tmp_lb = gtk_label_new("Progress:");
132     gtk_misc_set_alignment(GTK_MISC(tmp_lb), 1.0, 0.0);
133     gtk_box_pack_start(GTK_BOX(static_vb), tmp_lb, FALSE, TRUE, 3);
134
135
136     /*
137      * Dynamic labels (right dialog side, labels aligned to the left)
138      */
139     dynamic_vb = gtk_vbox_new(FALSE, 1);
140
141     /*
142      * Put the item_title here as a label indicating what we're
143      * doing; set its alignment and padding so it's aligned on the
144      * left.
145      */
146     title_lb = gtk_label_new(item_title_dup);
147     gtk_box_pack_start(GTK_BOX(dynamic_vb), title_lb, FALSE, TRUE, 3);
148     gtk_misc_set_alignment(GTK_MISC(title_lb), 0.0, 0.0);
149     gtk_misc_set_padding(GTK_MISC(title_lb), 0, 0);
150
151     /* same for "Status" */
152     status_lb = gtk_label_new("");
153     gtk_box_pack_start(GTK_BOX(dynamic_vb), status_lb, FALSE, TRUE, 3);
154     gtk_misc_set_alignment(GTK_MISC(status_lb), 0.0, 0.0);
155     gtk_misc_set_padding(GTK_MISC(status_lb), 0, 0);
156     dlg->status_lb = (GtkLabel *) status_lb;
157
158     /* same for "Elapsed Time" */
159     elapsed_lb = gtk_label_new("00:00");
160     gtk_box_pack_start(GTK_BOX(dynamic_vb), elapsed_lb, FALSE, TRUE, 3);
161     gtk_misc_set_alignment(GTK_MISC(elapsed_lb), 0.0, 0.0);
162     gtk_misc_set_padding(GTK_MISC(elapsed_lb), 0, 0);
163     dlg->elapsed_lb = (GtkLabel *) elapsed_lb;
164
165     /* same for "Time Left" */
166     time_left_lb = gtk_label_new("--:--");
167     gtk_box_pack_start(GTK_BOX(dynamic_vb), time_left_lb, FALSE, TRUE, 3);
168     gtk_misc_set_alignment(GTK_MISC(time_left_lb), 0.0, 0.0);
169     gtk_misc_set_padding(GTK_MISC(time_left_lb), 0, 0);
170     dlg->time_left_lb = (GtkLabel *) time_left_lb;
171
172     /*
173      * The progress bar (in its own horizontal box, including
174      * percentage value)
175      */
176     percentage_hb = gtk_hbox_new(FALSE, 1);
177     gtk_box_pack_start(GTK_BOX(dynamic_vb), percentage_hb, FALSE, TRUE, 3);
178
179     prog_bar = gtk_progress_bar_new();
180 #if GTK_MAJOR_VERSION < 2
181     gtk_progress_set_activity_mode(GTK_PROGRESS(prog_bar), FALSE);
182 #endif
183     gtk_box_pack_start(GTK_BOX(percentage_hb), prog_bar, FALSE, TRUE, 3);
184
185     percentage_lb = gtk_label_new("  0%");
186     gtk_misc_set_alignment(GTK_MISC(percentage_lb), 0.0, 0.0);
187     gtk_box_pack_start(GTK_BOX(percentage_hb), percentage_lb, FALSE, TRUE, 3);
188     dlg->percentage_lb = (GtkLabel *) percentage_lb;
189
190     /*
191      * Attach a pointer to the progress bar widget to the top-level widget.
192      */
193     OBJECT_SET_DATA(dlg_w, PROG_BAR_KEY, prog_bar);
194
195     /*
196      * Static and dynamic boxes are now complete
197      */
198     main_hb = gtk_hbox_new(FALSE, 1);
199     gtk_box_pack_start(GTK_BOX(main_hb), static_vb, FALSE, TRUE, 3);
200     gtk_box_pack_start(GTK_BOX(main_hb), dynamic_vb, FALSE, TRUE, 3);
201     gtk_box_pack_start(GTK_BOX(main_vb), main_hb, FALSE, TRUE, 3);
202
203     /* Button row */
204     bbox = dlg_button_row_new(terminate_is_stop ? GTK_STOCK_STOP :
205                               GTK_STOCK_CANCEL, NULL);
206     gtk_container_add(GTK_CONTAINER(main_vb), bbox);
207     gtk_widget_show(bbox);
208
209     cancel_bt = OBJECT_GET_DATA(bbox, terminate_is_stop ? GTK_STOCK_STOP :
210                                 GTK_STOCK_CANCEL);
211     gtk_widget_grab_default(cancel_bt);
212
213     /*
214      * Allow user to either click the "Cancel"/"Stop" button, or
215      * the close button on the window, to stop an operation in
216      * progress.
217      */
218     SIGNAL_CONNECT(cancel_bt, "clicked", stop_cb, stop_flag);
219     SIGNAL_CONNECT(dlg_w, "delete_event", delete_event_cb, stop_flag);
220
221     gtk_widget_show_all(dlg_w);
222
223     dlg->dlg_w = dlg_w;
224
225     g_get_current_time(&dlg->start_time);
226     memset(&dlg->last_time, 0, sizeof(dlg->last_time));
227
228     g_free(task_title_dup);
229     g_free(item_title_dup);
230
231     return dlg;
232 }
233
234 progdlg_t *
235 delayed_create_progress_dlg(const gchar *task_title, const gchar *item_title,
236                             gboolean terminate_is_stop, gboolean *stop_flag,
237                             const GTimeVal *start_time, gfloat progress)
238 {
239     GTimeVal    time_now;
240     gdouble     delta_time;
241     gdouble     min_display;
242     progdlg_t  *dlg;
243
244 #define INIT_DELAY          0.1 * 1e6
245 #define MIN_DISPLAY_DEFAULT 2.0 * 1e6
246
247     /* Create a progress dialog, but only if it's not likely to disappear
248      * immediately, which can be disconcerting for the user.
249      *
250      * Arguments are as for create_progress_dlg(), plus:
251      *
252      * (a) A pointer to a GTimeVal structure which holds the time at which
253      *     the caller started to process the data.
254      * (b) The current progress as a real number between 0 and 1.
255      */
256
257     g_get_current_time(&time_now);
258
259     /* Get the time elapsed since the caller started processing the data */
260
261     delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
262         time_now.tv_usec - start_time->tv_usec;
263
264     /* Do nothing for the first INIT_DELAY microseconds */
265
266     if (delta_time < INIT_DELAY)
267         return NULL;
268
269     /* If we create the progress dialog we want it to be displayed for a
270      * minimum of MIN_DISPLAY_DEFAULT microseconds.  However, if we
271      * previously estimated that the progress dialog didn't need to be
272      * created and the caller's processing is slowing down (perhaps due
273      * to the action of the operating system's scheduler on a compute-
274      * intensive task), we tail off the minimum display time such that
275      * the progress dialog will always be created after
276      * 2*MIN_DISPLAY_DEFAULT microseconds.
277      */
278
279     if (delta_time <= INIT_DELAY + MIN_DISPLAY_DEFAULT)
280         min_display = MIN_DISPLAY_DEFAULT;
281     else
282         min_display = 2 * MIN_DISPLAY_DEFAULT - delta_time;
283     /* = MIN_DISPLAY_DEFAULT - (delta_time - MIN_DISPLAY_DEFAULT) */
284
285     /* Assuming the progress increases linearly, see if the progress
286      * dialog would be displayed for at least min_display microseconds if
287      * we created it now.
288      */
289
290     if (progress >= (delta_time / (delta_time + min_display)))
291         return NULL;
292
293     dlg = create_progress_dlg(task_title, item_title, terminate_is_stop,
294                               stop_flag);
295
296     /* set dialog start_time to the start of processing, not box creation */
297     dlg->start_time = *start_time;
298
299     return dlg;
300 }
301
302 /*
303  * Called when the dialog box is to be deleted.
304  * Set the "stop" flag to TRUE, and return TRUE - we don't want the dialog
305  * box deleted now, our caller will do so when they see that the
306  * "stop" flag is TRUE and abort the operation.
307  */
308 static gint
309 delete_event_cb(GtkWidget *w _U_, GdkEvent *event _U_, gpointer data)
310 {
311         gboolean *stop_flag = (gboolean *) data;
312
313         *stop_flag = TRUE;
314         return TRUE;
315 }
316
317 /*
318  * Called when the "stop this operation" button is clicked.
319  * Set the "stop" flag to TRUE; we don't have to destroy the dialog
320  * box, as our caller will do so when they see that the "stop" flag is
321  * true and abort the operation.
322  */
323 static void
324 stop_cb(GtkWidget *w _U_, gpointer data)
325 {
326         gboolean *stop_flag = (gboolean *) data;
327
328         *stop_flag = TRUE;
329 }
330
331 /*
332  * Update the progress information of the progress dialog box.
333  */
334 void
335 update_progress_dlg(progdlg_t *dlg, gfloat percentage, const gchar *status)
336 {
337         GtkWidget *dlg_w = dlg->dlg_w;
338         GtkWidget *prog_bar;
339         GTimeVal   time_now;
340         gdouble    delta_time;
341         gulong     ul_left;
342         gulong     ul_elapsed;
343         gulong     ul_percentage;
344         gchar      tmp[100];
345
346
347         /* calculate some timing values */
348         g_get_current_time(&time_now);
349         
350         delta_time = (time_now.tv_sec - dlg->last_time.tv_sec) * 1e6 +
351                 time_now.tv_usec - dlg->last_time.tv_usec;
352
353         /* after the first time don't update more than every 100ms */
354         if (dlg->last_time.tv_sec && delta_time < 100*1000) 
355                 return;
356
357         dlg->last_time = time_now;
358         delta_time = (time_now.tv_sec - dlg->start_time.tv_sec) * 1e6 +
359                 time_now.tv_usec - dlg->start_time.tv_usec;
360                 
361         ul_percentage = (gulong) (percentage * 100);
362         ul_elapsed = (gulong) (delta_time / 1000 / 1000);
363         
364         /* update labels */
365         g_snprintf(tmp, sizeof(tmp), "%lu%% of %s", ul_percentage, dlg->title);
366         gtk_window_set_title(GTK_WINDOW(dlg_w), tmp);
367
368         gtk_label_set_text(dlg->status_lb, status);
369
370         g_snprintf(tmp, sizeof(tmp), "%lu%%", ul_percentage);
371         gtk_label_set_text(dlg->percentage_lb, tmp);
372
373         g_snprintf(tmp, sizeof(tmp), "%02lu:%02lu", ul_elapsed / 60,
374                    ul_elapsed % 60);
375         gtk_label_set_text(dlg->elapsed_lb, tmp);
376
377         /* show "Time Left" only,
378          * if at least 5% and 3 seconds running (to get a useful estimation) */
379         if (ul_percentage >= 5 && delta_time >= 3 * 1e6) {
380                 ul_left = (gulong) ((delta_time / percentage - delta_time) / 1000 / 1000);
381
382                 g_snprintf(tmp, sizeof(tmp), "%02lu:%02lu", ul_left / 60,
383                            ul_left % 60);
384                 gtk_label_set_text(dlg->time_left_lb, tmp);
385         }
386
387         /* update progress bar */
388         prog_bar = OBJECT_GET_DATA(dlg_w, PROG_BAR_KEY);
389 #if GTK_MAJOR_VERSION < 2
390         gtk_progress_bar_update(GTK_PROGRESS_BAR(prog_bar), percentage);
391 #else
392         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(prog_bar), percentage);
393 #endif
394
395         /*
396          * Flush out the update and process any input events.
397          */
398         while (gtk_events_pending())
399                 gtk_main_iteration();
400 }
401
402 /*
403  * Destroy the progress dialog.
404  */
405 void
406 destroy_progress_dlg(progdlg_t *dlg)
407 {
408     GtkWidget *dlg_w = dlg->dlg_w;
409
410     window_destroy(GTK_WIDGET(dlg_w));
411     g_free(dlg->title);
412     g_free(dlg);
413 }