-
Notifications
You must be signed in to change notification settings - Fork 1
/
progressbar.c
140 lines (115 loc) · 3.34 KB
/
progressbar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Progress bar
* Sanity 0.3
*/
typedef struct
{
GtkWidget *progressbar;
GtkWidget *window;
int bProgressUp;
int nLastPct;
} typProgressData;
typProgressData *pdata = NULL;
/*
* CanWindowClose
*
* Function that determines that if the dialog
* window can be closed.
*/
gint CanWindowClose (GtkWidget *widget)
{
/* --- TRUE => cannot close --- */
/* --- FALSE => can close --- */
return (pdata->bProgressUp);
}
/*
* UpdateProgress
*
* Update the progress window to reflect the state
* of the file that is being loaded.
*
* pos - how much of the file has been loaded.
* len - length of the file
* (pos / len) = % file has been loaded.
*/
void UpdateProgress (unsigned int pos, unsigned int len)
{
gfloat pvalue;
int pct;
/* --- Prevent divide by zero errors --- */
if (len > 0) {
/* --- Calculate the percentage --- */
pvalue = (gfloat) pos / (gfloat) len;
pct = pvalue * 100;
if (pdata->nLastPct != pct) {
/* --- Update the displayed value --- */
gtk_progress_set_percentage (GTK_PROGRESS (pdata->progressbar),
pvalue);
/* --- Repaint any windows - like the progress bar --- */
while (gtk_events_pending ()) {
gtk_main_iteration ();
}
pdata->nLastPct = pct;
}
}
}
/*
* StartProgress
*
* Create a window for the progress bar
*/
void StartProgress ()
{
GtkWidget *label;
GtkWidget *table;
GtkWidget *window;
GtkAdjustment *adj;
GdkPixbuf *icon;
pdata = g_malloc (sizeof (typProgressData));
pdata->nLastPct = -1;
pdata->bProgressUp = TRUE;
/*
* --- Create the top level window
*/
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
pdata->window = window;
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
/* --- Hook up the destroy --- */
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (CanWindowClose), pdata);
gtk_container_border_width (GTK_CONTAINER (window), 10);
icon = gdk_pixbuf_new_from_file ("share/icons/sanity32.png", NULL);
gtk_window_set_icon(GTK_WINDOW(window), icon);
gtk_window_set_title(GTK_WINDOW (window), "Realizando Testes...");
/* --- Create a table --- */
table = gtk_table_new (3, 2, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
/* --- Add a label to the table --- */
label = gtk_label_new ("Realizando Testes...");
gtk_table_attach_defaults (GTK_TABLE (table), label, 0,2,0,1);
gtk_widget_show (label);
/* --- Add the progress bar to the table --- */
adj = (GtkAdjustment *) gtk_adjustment_new (0, 0, 400, 0, 0, 0);
pdata->progressbar = gtk_progress_bar_new_with_adjustment (adj);
gtk_table_attach_defaults (GTK_TABLE (table),
pdata->progressbar, 0,2,1,2);
gtk_widget_show (pdata->progressbar);
/* --- Show everything --- */
gtk_widget_show (table);
gtk_widget_show (window);
}
/*
* EndProgress
*
* Close down the progress bar.
*/
void EndProgress ()
{
/* --- Allow it to close --- */
pdata->bProgressUp = FALSE;
/* --- Destroy the window --- */
gtk_widget_destroy (pdata->window);
/* --- Free used memory. --- */
g_free (pdata);
pdata = NULL;
}