| 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 |
#include <fontforge-config.h> |
| 29 |
|
| 30 |
#include "ggadget.h" |
| 31 |
#include "ggadgetP.h" |
| 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; |
| 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; |
| 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, |
| 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, |
| 218 |
const unichar_t *win_title, |
| 219 |
const unichar_t *line1, |
| 220 |
const unichar_t *line2, |
| 221 |
int tot, |
| 222 |
int stages |
| 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 |
|
| 291 |
|
| 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 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 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(¤t->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 |
} |