File: gwwvprogress.c

package info (click to toggle)
fontforge 1:20170731~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 67,252 kB
  • ctags: 42,582
  • sloc: ansic: 580,893; python: 5,476; sh: 3,081; makefile: 1,269; perl: 315; cpp: 176; ruby: 97; objc: 92; xml: 90; sed: 9
file content (384 lines) | stat: -rw-r--r-- 11,593 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright (C) 2006 by George Williams */
/*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.

 * The name of the author may not be used to endorse or promote products
 * derived from this software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libintl.h>
#define _(str)	gettext(str)

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include <basics.h>
#include "support.h"
#include "gwwvprogress.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)


typedef struct gprogress {
    struct timeval start_time;	/* Don't pop up unless we're after this */
    struct timeval pause_time;
    int sofar;
    int tot;
    gint16 stage, stages;
    unsigned int aborted: 1;
    unsigned int superceded: 1;
    unsigned int visible: 1;
    unsigned int paused: 1;
    unsigned int sawmap: 1;
    unsigned int death_pending: 1;
    GtkWidget *progress;
    struct gprogress *prev;
} GProgress;

static GProgress *current;


static void gwwv_progress__end_indicator(void) {
    GProgress *old = current;

    current=old->prev;
    gtk_widget_destroy(old->progress);
    free(old);
    if ( current!=NULL ) {
	current->superceded = false;
	if ( current->visible )
	    gtk_widget_show(current->progress);
    }
}

static void gwwv_progress__map(GtkWidget *widget, gpointer user_data) {
    if ( current!=NULL ) {
	current->sawmap = true;
	if ( current->death_pending )
	    gwwv_progress__end_indicator();
    }
}


static void gwwv_progress__stop(GtkButton *button, gpointer user_data) {
    if ( current!=NULL )
	current->aborted = true;
}

static GtkWidget* create_Progress(void) {
  GtkWidget *Progress;
  GtkWidget *vbox1;
  GtkWidget *line1;
  GtkWidget *line2;
  GtkWidget *progressbar;
  GtkWidget *hbox1;
  GtkWidget *stop;
  GtkWidget *alignment1;
  GtkWidget *hbox2;
  GtkWidget *image1;
  GtkWidget *label3;

  Progress = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (Progress), _("Progress"));
  gtk_window_set_modal (GTK_WINDOW (Progress), TRUE);

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox1);
  gtk_container_add (GTK_CONTAINER (Progress), vbox1);

  line1 = gtk_label_new (_("line1"));
  gtk_widget_show (line1);
  gtk_box_pack_start (GTK_BOX (vbox1), line1, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (line1), GTK_JUSTIFY_CENTER);

  line2 = gtk_label_new (_("line2"));
  gtk_widget_show (line2);
  gtk_box_pack_start (GTK_BOX (vbox1), line2, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (line2), GTK_JUSTIFY_CENTER);

  progressbar = gtk_progress_bar_new ();
  gtk_widget_show (progressbar);
  gtk_box_pack_start (GTK_BOX (vbox1), progressbar, FALSE, FALSE, 0);

  hbox1 = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox1);
  gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);

  stop = gtk_button_new ();
  gtk_widget_show (stop);
  gtk_box_pack_start (GTK_BOX (hbox1), stop, FALSE, FALSE, 0);

  alignment1 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment1);
  gtk_container_add (GTK_CONTAINER (stop), alignment1);

  hbox2 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox2);
  gtk_container_add (GTK_CONTAINER (alignment1), hbox2);

  image1 = gtk_image_new_from_stock ("gtk-stop", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image1);
  gtk_box_pack_start (GTK_BOX (hbox2), image1, FALSE, FALSE, 0);

  label3 = gtk_label_new_with_mnemonic (_("Stop"));
  gtk_widget_show (label3);
  gtk_box_pack_start (GTK_BOX (hbox2), label3, FALSE, FALSE, 0);

  g_signal_connect ((gpointer) Progress, "map",
                    G_CALLBACK (gwwv_progress__map),
                    NULL);
  g_signal_connect ((gpointer) stop, "activate",
                    G_CALLBACK (gwwv_progress__stop),
                    NULL);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (Progress, Progress, "Progress");
  GLADE_HOOKUP_OBJECT (Progress, vbox1, "vbox1");
  GLADE_HOOKUP_OBJECT (Progress, line1, "line1");
  GLADE_HOOKUP_OBJECT (Progress, line2, "line2");
  GLADE_HOOKUP_OBJECT (Progress, progressbar, "progressbar");
  GLADE_HOOKUP_OBJECT (Progress, hbox1, "hbox1");
  GLADE_HOOKUP_OBJECT (Progress, stop, "stop");
  GLADE_HOOKUP_OBJECT (Progress, alignment1, "alignment1");
  GLADE_HOOKUP_OBJECT (Progress, hbox2, "hbox2");
  GLADE_HOOKUP_OBJECT (Progress, image1, "image1");
  GLADE_HOOKUP_OBJECT (Progress, label3, "label3");

  return Progress;
}

static void gwwv_progress__display(void) {
    if ( current==NULL )
return;
    gtk_widget_show(current->progress);
    current->visible = true;
    if ( current->prev!=NULL && current->prev->visible ) {
	gtk_widget_hide(current->prev->progress);
	current->superceded = true;
    }
}

static void gwwv_progress__time_check(void) {
    struct timeval tv;

    if ( current==NULL || current->visible || current->death_pending || current->paused )
return;
    gettimeofday(&tv,NULL);
    if ( tv.tv_sec>current->start_time.tv_sec ||
	    (tv.tv_sec==current->start_time.tv_sec && tv.tv_usec>current->start_time.tv_usec )) {
	if ( current->tot>0 &&
		current->sofar+current->stage*current->tot>(9*current->stages*current->tot)/10 )
return;		/* If it's almost done, no point in making it visible */
	gwwv_progress__display();
    }
}

static int gwwv_progress__process(void) {
    double frac;
    GtkWidget *bar;

    if ( current==NULL || current->paused )
return( true );
    if ( !current->visible )
	gwwv_progress__time_check();
    if ( !current->visible )
return( true );

    if ( current->tot==0 || current->stages==0 )
	frac = 0;
    else
	frac = (current->stage*current->tot + current->sofar)/
		(double) (current->stages*current->tot);
    bar = lookup_widget(current->progress,"progressbar");
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar),frac);

    g_main_context_iteration(NULL,FALSE);
return( !current->aborted );
}


/* ************************************************************************** */
/* *************************** External Interface *************************** */
/* ************************************************************************** */
void gwwv_progress_start_indicator(int delay, const char *title, const char *line1,
	const char *line2, int tot, int stages) {
    GProgress *new;
    struct timeval tv;
    GtkWidget *line;

    new = calloc(1,sizeof(GProgress));
    new->progress = create_Progress();
    gtk_window_set_title(GTK_WINDOW(new->progress),title);
    line = lookup_widget(new->progress,"line1");
    gtk_label_set_text(GTK_LABEL(line),line1);
    line = lookup_widget(new->progress,"line2");
    gtk_label_set_text(GTK_LABEL(line),line2);

    new->tot = tot;
    new->stages = stages;
    new->prev = current;

    /* If there's another progress indicator up, it will not move and ours */
    /*  won't be visible if we have a delay, so force delay to 0 here */
    if ( current!=NULL ) delay = 0;
    gettimeofday(&tv,NULL);
    new->start_time = tv;
    new->start_time.tv_usec += (delay%10)*100000;
    new->start_time.tv_sec += delay/10;
    if ( new->start_time.tv_usec >= 1000000 ) {
	++new->start_time.tv_sec;
	new->start_time.tv_usec -= 1000000;
    }

    current = new;
    gwwv_progress__time_check();
}

void gwwv_progress_end_indicator(void) {

    if ( current==NULL )
return;
    if ( !current->sawmap )
	current->death_pending = true;
    else
	gwwv_progress__end_indicator();
}

void gwwv_progress_show(void) {

    if ( current==NULL || current->visible || current->death_pending )
return;
    gwwv_progress__display();
}

int gwwv_progress_next(void) {

    if ( current==NULL )
return(true);
    ++current->sofar;
    if ( current->sofar>=current->tot )
	current->sofar = current->tot-1;
return( gwwv_progress__process());
}

int gwwv_progress_increment(int cnt) {

    if ( current==NULL )
return(true);
    current->sofar += cnt;
    if ( current->sofar>=current->tot )
	current->sofar = current->tot-1;
return( gwwv_progress__process());
}

int gwwv_progress_next_stage(void) {

    if ( current==NULL )
return(true);
    ++current->stage;
    current->sofar = 0;
    if ( current->stage>=current->stages )
	current->stage = current->stages-1;
return( gwwv_progress__process());
}

void gwwv_progress_change_line1(const char *line1) {
    GtkWidget *line;
    line = lookup_widget(current->progress,"line1");
    gtk_label_set_text(GTK_LABEL(line),line1);
}

void gwwv_progress_change_line2(const char *line2) {
    GtkWidget *line;
    line = lookup_widget(current->progress,"line2");
    gtk_label_set_text(GTK_LABEL(line),line2);
}

void gwwv_progress_enable_stop(int enabled) {
    GtkWidget *stop;
    stop = lookup_widget(current->progress,"stop");
    gtk_widget_set_sensitive(stop,enabled);
}

void gwwv_progress_change_total(int tot) {
    if ( current==NULL || tot<=0 )
return;
    current->tot = tot;
    if ( current->sofar>current->tot )
	current->sofar = current->tot;
}
 
void gwwv_progress_change_stages(int stages) {
    if ( current==NULL )
return;
    if ( stages<=0 )
	stages = 1;
    current->stages = stages;
    if ( current->stage>=stages )
	current->stage = stages-1;
}

void gwwv_progress_pause_timer(void) {
    if ( current==NULL || current->visible || current->death_pending || current->paused )
return;
    gettimeofday(&current->pause_time,NULL);
    current->paused = true;
}

void gwwv_progress_resume_timer(void) {
    struct timeval tv, res;

    if ( current==NULL || current->visible || current->death_pending || !current->paused )
return;
    current->paused = false;
    gettimeofday(&tv,NULL);
    res.tv_sec = tv.tv_sec - current->pause_time.tv_sec;
    if ( (res.tv_usec = tv.tv_usec - current->pause_time.tv_usec)<0 ) {
	--res.tv_sec;
	res.tv_usec += 1000000;
    }
    current->start_time.tv_sec += res.tv_sec;
    if ( (current->start_time.tv_usec += res.tv_usec)>= 1000000 ) {
	++current->start_time.tv_sec;
	current->start_time.tv_usec -= 1000000;
    }
}

int gwwv_progress_reset(void) {

    if ( current==NULL )
return(true);
    current->sofar = 0;
return( gwwv_progress__process());
}