chiark / gitweb /
Switch to GPL v3
[disorder] / disobedience / progress.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006, 2007 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file disobedience/progress.c
19  * @brief Progress bar support
20  */
21
22 #include "disobedience.h"
23
24 /** @brief State for progress windows */
25 struct progress_window {
26   /** @brief The window */
27   GtkWidget *window;
28   /** @brief The bar */
29   GtkWidget *bar;
30 };
31
32 /** @brief Create a progress window */
33 struct progress_window *progress_window_new(const char *title) {
34   struct progress_window *pw = xmalloc(sizeof *pw);
35
36   pw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
37   gtk_window_set_transient_for(GTK_WINDOW(pw->window),
38                                GTK_WINDOW(toplevel));
39   g_signal_connect(pw->window, "destroy",
40                    G_CALLBACK(gtk_widget_destroyed), &pw->window);
41   gtk_window_set_default_size(GTK_WINDOW(pw->window), 360, -1);
42   gtk_window_set_title(GTK_WINDOW(pw->window), title);
43   pw->bar = gtk_progress_bar_new();
44   gtk_container_add(GTK_CONTAINER(pw->window), pw->bar);
45   gtk_widget_show_all(pw->window);
46   return pw;
47 }
48
49 /** @brief Report current progress
50  * The window is automatically destroyed if @p progress >= @p limit.
51  * To cancel a window just call with both set to 0.
52  */
53 void progress_window_progress(struct progress_window *pw,
54                               int progress,
55                               int limit) {
56   if(!pw)
57     return;
58   /* Maybe the user closed the window */
59   if(!pw->window)
60     return;
61   /* Clamp insane or inconvenient values */
62   if(limit <= 0)
63     progress = limit = 1;
64   if(progress < 0)
65     progress = 0;
66   /* Maybe we're done */
67   if(progress >= limit) {
68     gtk_widget_destroy(pw->window);
69     pw->window = pw->bar = 0;
70     return;
71   }
72   /* Display current progress */
73   gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pw->bar),
74                                 (double)progress / limit);
75 }
76
77 /*
78 Local Variables:
79 c-basic-offset:2
80 comment-column:40
81 fill-column:79
82 indent-tabs-mode:nil
83 End:
84 */