Debian Codegraph @

gdraw/gprogress.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "ggadget.h"
31 #include "ggadgetP.h" /* For the font family names */
32 #include "gprogress.h"
33 #include "gwidget.h"
34 #include "ustring.h"
35
36 #include <sys/time.h>
37
38 typedef struct gprogress {
39 struct timeval start_time; /* Don't pop up unless we're after this */
40 struct timeval pause_time;
41 unichar_t *line1;
42 unichar_t *line2;
43 int sofar;
44 int tot;
45 int16_t stage, stages;
46 int16_t width;
47 int16_t l1width, l2width;
48 int16_t l1y, l2y, boxy;
49 int16_t last_amount;
50 unsigned int aborted: 1;
51 unsigned int visible: 1;
52 unsigned int dying: 1;
53 unsigned int paused: 1;
54 unsigned int sawmap: 1;
55 GWindow gw;
56 GFont *font;
57 struct gprogress *prev;
58 } GProgress;
59
60 static Color progress_background = 0xffffff, progress_foreground;
61 static Color progress_fillcol = 0xc0c0ff;
62 static GResFont progress_font = GRESFONT_INIT("400 12pt " MONO_UI_FAMILIES);
63
64 static GProgress *current;
65
66 static void GProgressDisplay(void) {
67 GDrawSetVisible(current->gw,true);
68 current->visible = true;
69 if ( current->prev!=NULL && current->prev->visible ) {
70 GDrawSetVisible(current->prev->gw,false);
71 current->prev->visible = false;
72 }
73 }
74
75 static void GProgressTimeCheck() {
76 struct timeval tv;
77
78 if ( current==NULL || current->visible || current->dying || current->paused )
79 return;
80 gettimeofday(&tv,NULL);
81 if ( tv.tv_sec>current->start_time.tv_sec ||
82 (tv.tv_sec==current->start_time.tv_sec && tv.tv_usec>current->start_time.tv_usec )) {
83 if ( current->tot>0 &&
84 current->sofar+current->stage*current->tot>(9*current->stages*current->tot)/10 )
85 return; /* If it's almost done, no point in making it visible */
86 GProgressDisplay();
87 }
88 }
89
90 static void GProgressDraw(GProgress *p,GWindow pixmap,GRect *rect) {
91 GRect r, old;
92 int width, amount;
93
94 GDrawPushClip(pixmap,rect,&old);
95 GDrawSetFont(pixmap,p->font);
96 if ( p->line1!=NULL )
97 GDrawDrawText(pixmap, (p->width-p->l1width)/2, p->l1y, p->line1, -1,
98 progress_foreground );
99 if ( p->line2!=NULL )
100 GDrawDrawText(pixmap, (p->width-p->l2width)/2, p->l2y, p->line2, -1,
101 progress_foreground );
102
103 r.x = GDrawPointsToPixels(pixmap,10);
104 r.y = p->boxy;
105 r.height = r.x;
106 width = p->width-2*r.x;
107
108 if ( p->tot==0 )
109 amount = 0;
110 else
111 amount = width * (p->stage*p->tot + p->sofar)/(p->stages*p->tot);
112 if ( amount>0 ) {
113 r.width = amount;
114 GDrawFillRect(pixmap,&r,progress_fillcol);
115 } else if ( p->tot==0 ) {
116 r.width = width;
117 GDrawSetStippled(pixmap,1,0,0);
118 GDrawFillRect(pixmap,&r,progress_fillcol);
119 GDrawSetStippled(pixmap,0,0,0);
120 }
121 r.width = width;
122 GDrawDrawRect(pixmap,&r,progress_foreground);
123 GDrawPopClip(pixmap,&old);
124 }
125
126 static int GProgressProcess(GProgress *p) {
127 int width, amount;
128 int tenpt;
129
130 if ( !p->visible )
131 GProgressTimeCheck();
132
133 tenpt = GDrawPointsToPixels(p->gw,10);
134 width = p->width-2*tenpt;
135 if ( p->tot==0 )
136 amount = 0;
137 else
138 amount = width * (p->stage*p->tot + p->sofar)/(p->stages*p->tot);
139 if ( amount!=p->last_amount ) {
140 if ( amount<p->last_amount || p->last_amount==0 )
141 GDrawRequestExpose(p->gw,NULL,false);
142 else {
143 GRect r;
144 r.height = tenpt-1;
145 r.width = width;
146 r.x = tenpt;
147 r.y = p->boxy+1;
148 GDrawRequestExpose(p->gw,&r,false);
149 }
150 p->last_amount = amount;
151 }
152 GDrawProcessPendingEvents(NULL);
153 return( !p->aborted );
154 }
155
156 static int progress_eh(GWindow gw, GEvent *event) {
157 GProgress *p = GDrawGetUserData(gw);
158
159 switch ( event->type ) {
160 case et_destroy:
161 free(p->line1);
162 free(p->line2);
163 free(p);
164 break;
165 case et_close:
166 p->aborted = true;
167 GDrawSetVisible(gw,false);
168 break;
169 case et_expose:
170 GProgressDraw(p,gw,&event->u.expose.rect);
171 break;
172 case et_controlevent:
173 if ( event->u.control.subtype == et_buttonactivate )
174 p->aborted = true;
175 break;
176 case et_char:
177 if ( (event->u.chr.state&ksm_control) && event->u.chr.chars[0]=='.' )
178 p->aborted = true;
179 break;
180 case et_map:
181 p->sawmap = true;
182 break;
183 default: break;
184 }
185 return( true );
186 }
187
188 /* ************************************************************************** */
189 static struct resed progress_re[] = {
190 {N_("Color|Foreground"), "Foreground", rt_color, &progress_foreground, N_("Text color for progress windows"), NULL, { 0 }, 0, 0 },
191 {N_("Color|FillColor"), "FillColor", rt_color, &progress_fillcol, N_("Color used to draw the progress bar"), NULL, { 0 }, 0, 0 },
192 {N_("Color|Background"), "Background", rt_color, &progress_background, N_("Background color for progress windows"), NULL, { 0 }, 0, 0 },
193 RESED_EMPTY
194 };
195 extern GResInfo ggadget_ri;
196 GResInfo gprogress_ri = {
197 &ggadget_ri, NULL, NULL,NULL,
198 NULL, /* No box */
199 &progress_font,
200 NULL,
201 progress_re,
202 N_("Progress"),
203 N_("Progress Bars"),
204 "GProgress",
205 "Gdraw",
206 false,
207 false,
208 0,
209 GBOX_EMPTY,
210 GBOX_EMPTY,
211 NULL,
212 NULL,
213 NULL
214 };
215
216 void GProgressStartIndicator(
217 int delay, /* in tenths of seconds */
218 const unichar_t *win_title, /* for the window decoration */
219 const unichar_t *line1, /* First line of description */
220 const unichar_t *line2, /* Second line */
221 int tot, /* Number of sub-entities in the operation */
222 int stages /* Number of stages, each processing tot sub-entities */
223 ) {
224 GProgress *new;
225 GWindowAttrs wattrs;
226 GWindow root;
227 GGadgetData gd;
228 int ld, as, ds;
229 GRect pos;
230 struct timeval tv;
231 GTextInfo label;
232
233 if ( screen_display==NULL )
234 return;
235
236 GResEditDoInit(&gprogress_ri);
237 new = calloc(1,sizeof(GProgress));
238 new->line1 = u_copy(line1);
239 new->line2 = u_copy(line2);
240 new->tot = tot;
241 new->stages = stages;
242 new->prev = current;
243
244 root = GDrawGetRoot(NULL);
245 GDrawSetFont(root, progress_font.fi);
246 GDrawWindowFontMetrics(root,new->font = progress_font.fi,&as,&ds,&ld);
247
248 if ( new->line1!=NULL )
249 new->l1width = GDrawGetTextWidth(root,new->line1,-1);
250 if ( new->line2!=NULL )
251 new->l2width = GDrawGetTextWidth(root,new->line2,-1);
252 new->l1y = GDrawPointsToPixels(root,5) + as;
253 new->l2y = new->l1y + as+ds;
254 new->boxy = new->l2y + as+ds;
255 pos.width = (new->l1width>new->l2width)?new->l1width:new->l2width;
256 if ( pos.width<GDrawPointsToPixels(root,100) )
257 pos.width = GDrawPointsToPixels(root,100);
258 pos.width += 2 * GDrawPointsToPixels(root,10);
259 pos.height = new->boxy + GDrawPointsToPixels(root,44);
260 new->width = pos.width;
261
262 memset(&wattrs,0,sizeof(wattrs));
263 wattrs.mask = wam_events|wam_cursor|(win_title!=NULL?wam_wtitle:0)|
264 wam_centered|wam_restrict|wam_redirect|wam_isdlg|wam_backcol;
265 wattrs.event_masks = ~(1<<et_charup);
266 wattrs.cursor = ct_watch;
267 wattrs.window_title = u_copy(win_title);
268 wattrs.centered = true;
269 wattrs.restrict_input_to_me = true;
270 wattrs.redirect_chars_to_me = true;
271 wattrs.is_dlg = true;
272 wattrs.redirect_from = NULL;
273 wattrs.background_color = progress_background;
274 pos.x = pos.y = 0;
275 new->gw = GDrawCreateTopWindow(NULL,&pos,progress_eh,new,&wattrs);
276 free((void *) wattrs.window_title);
277
278 memset(&gd,'\0',sizeof(gd)); memset(&label,'\0',sizeof(label));
279 gd.pos.width = GDrawPointsToPixels(new->gw,50);
280 gd.pos.x = pos.width-gd.pos.width-10;
281 gd.pos.y = pos.height-GDrawPointsToPixels(new->gw,29);
282 gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
283 gd.mnemonic = 'S';
284 label.text = (unichar_t *) _("_Stop");
285 label.text_is_1byte = true;
286 label.text_in_resource = true;
287 gd.label = &label;
288 GButtonCreate( new->gw, &gd, NULL);
289
290 /* If there's another progress indicator up, it will not move and ours */
291 /* won't be visible if we have a delay, so force delay to 0 here */
292 if ( current!=NULL ) delay = 0;
293 gettimeofday(&tv,NULL);
294 new->start_time = tv;
295 new->start_time.tv_usec += (delay%10)*100000;
296 new->start_time.tv_sec += delay/10;
297 if ( new->start_time.tv_usec >= 1000000 ) {
298 ++new->start_time.tv_sec;
299 new->start_time.tv_usec -= 1000000;
300 }
301
302 current = new;
303 GProgressTimeCheck();
304 }
305
306 void GProgressEndIndicator(void) {
307 GProgress *old=current;
308
309 if ( old==NULL )
310 return;
311 current = old->prev;
312
313 old->dying = true;
314 /* the X server sometimes crashes if we: */
315 /* Create a window */
316 /* map it */
317 /* but destroy it before the server can send us the map notify event */
318 /* next three lines seem to deal with it */
319 /* unmapping the window also causes a crash */
320 if ( old->visible ) while ( !old->sawmap ) {
321 GDrawSync(NULL);
322 GDrawProcessPendingEvents(NULL);
323 }
324 GDrawDestroyWindow(old->gw);
325 GDrawSync(NULL);
326 GDrawProcessPendingEvents(NULL);
327 }
328
329 void GProgressChangeLine1(const unichar_t *line1) {
330 if ( current==NULL )
331 return;
332 free( current->line1 );
333 current->line1 = u_copy(line1);
334 if ( current->line1!=NULL ) {
335 GDrawSetFont(current->gw,current->font);
336 current->l1width = GDrawGetTextWidth(current->gw,current->line1,-1);
337 }
338 if ( current->visible )
339 GDrawRequestExpose(current->gw,NULL,false);
340 }
341
342 void GProgressChangeLine2(const unichar_t *line2) {
343 if ( current==NULL )
344 return;
345 free( current->line2 );
346 current->line2 = u_copy(line2);
347 if ( current->line2!=NULL ) {
348 GDrawSetFont(current->gw,current->font);
349 current->l2width = GDrawGetTextWidth(current->gw,current->line2,-1);
350 }
351 if ( current->visible )
352 GDrawRequestExpose(current->gw,NULL,false);
353 }
354
355 void GProgressChangeTotal(int tot) {
356 if ( current==NULL )
357 return;
358 current->tot = tot;
359 }
360
361 void GProgressChangeStages(int stages) {
362 if ( current==NULL )
363 return;
364 if ( stages<=0 )
365 stages = 1;
366 current->stages = stages;
367 if ( current->stage>=stages )
368 current->stage = stages-1;
369 }
370
371 void GProgressEnableStop(int enabled) {
372 if ( current==NULL )
373 return;
374 GGadgetSetEnabled(GWidgetGetControl(current->gw,0),enabled);
375 }
376
377 int GProgressNextStage(void) {
378
379 if ( current==NULL )
380 return(true);
381 ++current->stage;
382 current->sofar = 0;
383 if ( current->stage>=current->stages )
384 current->stage = current->stages-1;
385 return( GProgressProcess(current));
386 }
387
388 int GProgressNext(void) {
389
390 if ( current==NULL )
391 return(true);
392 ++current->sofar;
393 if ( current->sofar>=current->tot )
394 current->sofar = current->tot-1;
395 return( GProgressProcess(current));
396 }
397
398 int GProgressIncrementBy(int cnt) {
399
400 if ( current==NULL )
401 return(true);
402 current->sofar += cnt;
403 if ( current->sofar>=current->tot )
404 current->sofar = current->tot-1;
405 return( GProgressProcess(current));
406 }
407
408 int GProgressReset(void) {
409
410 if ( current==NULL )
411 return(true);
412 current->sofar = 0;
413 return( GProgressProcess(current));
414 }
415
416 void GProgressPauseTimer(void) {
417 if ( current==NULL || current->visible || current->dying || current->paused )
418 return;
419 gettimeofday(&current->pause_time,NULL);
420 current->paused = true;
421 }
422
423 void GProgressResumeTimer(void) {
424 struct timeval tv, res;
425
426 if ( current==NULL || current->visible || current->dying || !current->paused )
427 return;
428 current->paused = false;
429 gettimeofday(&tv,NULL);
430 res.tv_sec = tv.tv_sec - current->pause_time.tv_sec;
431 if ( (res.tv_usec = tv.tv_usec - current->pause_time.tv_usec)<0 ) {
432 --res.tv_sec;
433 res.tv_usec += 1000000;
434 }
435 current->start_time.tv_sec += res.tv_sec;
436 if ( (current->start_time.tv_usec += res.tv_usec)>= 1000000 ) {
437 ++current->start_time.tv_sec;
438 current->start_time.tv_usec -= 1000000;
439 }
440 }
441
442 void GProgressShow(void) {
443
444 if ( current==NULL || current->visible || current->dying )
445 return;
446
447 GProgressDisplay();
448 GDrawSync(NULL);
449 GDrawProcessPendingEvents(NULL);
450 GDrawSync(NULL);
451 GDrawProcessPendingEvents(NULL);
452 }
453
454 void GProgressStartIndicator8(int delay, const char *title, const char *line1,
455 const char *line2, int tot, int stages) {
456 unichar_t *tit = utf82u_copy(title),
457 *l1 = utf82u_copy(line1),
458 *l2 = utf82u_copy(line2);
459 GProgressStartIndicator(delay,
460 tit,l1,l2,
461 tot,stages);
462 free(l1); free(l2); free(tit);
463 }
464
465 void GProgressChangeLine1_8(const char *line1) {
466 unichar_t *l1 = utf82u_copy(line1);
467 GProgressChangeLine1(l1);
468 free(l1);
469 }
470
471 void GProgressChangeLine2_8(const char *line2) {
472 unichar_t *l2 = utf82u_copy(line2);
473 GProgressChangeLine2(l2);
474 free(l2);
475 }