Debian Codegraph @

gdraw/gaskdlg.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 "gdraw.h"
31 #include "ggadget.h"
32 #include "ggadgetP.h"
33 #include "gwidget.h"
34 #include "ustring.h"
35
36 #include <stdarg.h>
37
38 extern void GTextFieldInit();
39
40 static GWindow last;
41 static const char *last_title;
42
43 struct dlg_info {
44 int done;
45 int ret;
46 int multi;
47 int exposed;
48 int size_diff;
49 int bcnt;
50 };
51
52 static int d_e_h(GWindow gw, GEvent *event) {
53 struct dlg_info *d = GDrawGetUserData(gw);
54
55 if ( event->type==et_close ) {
56 d->done = true;
57 /* d->ret is initialized to cancel */
58 } else if ( event->type==et_controlevent &&
59 event->u.control.subtype == et_buttonactivate ) {
60 d->done = true;
61 d->ret = GGadgetGetCid(event->u.control.g);
62 } else if ( event->type == et_expose ) {
63 d->exposed = true;
64 } else if ( event->type == et_char ) {
65 return( false );
66 } else if ( event->type == et_resize && !d->exposed ) {
67 GRect pos,rootsize;
68 GDrawGetSize(gw,&pos);
69 GDrawGetSize(GDrawGetRoot(NULL),&rootsize);
70 if ( pos.x+pos.width>=rootsize.width || pos.y+pos.height>=rootsize.height ) {
71 if ( pos.x+pos.width>=rootsize.width )
72 if (( pos.x = rootsize.width - pos.width )<0 ) pos.x = 0;
73 if ( pos.y+pos.height>=rootsize.height )
74 if (( pos.y = rootsize.height - pos.height )<0 ) pos.y = 0;
75 GDrawMove(gw,pos.x,pos.y);
76 }
77 } else if ( event->type == et_map ) {
78 /* Above palettes */
79 GDrawRaise(gw);
80 }
81 return( true );
82 }
83
84 static int w_e_h(GWindow gw, GEvent *event) {
85
86 if ( event->type==et_close ||
87 (event->type==et_controlevent &&
88 event->u.control.subtype == et_buttonactivate ) ||
89 event->type==et_timer )
90 GDrawDestroyWindow(gw);
91 else if ( event->type == et_map ) {
92 /* Above palettes */
93 GDrawRaise(gw);
94 } else if ( event->type == et_char ) {
95 return( false );
96 } else if ( event->type == et_destroy ) {
97 if ( last==gw ) {
98 last = NULL;
99 last_title = NULL;
100 }
101 }
102 return( true );
103 }
104
105 #define GLINE_MAX 20
106
107 static int FindLineBreaks(const unichar_t *question, GTextInfo linebreaks[GLINE_MAX+1]) {
108 int lb, i;
109 const unichar_t *pt, *last;
110
111 /* Find good places to break the question into bits */
112 last = pt = question;
113 linebreaks[0].text = (unichar_t *) question;
114 lb=0;
115 while ( *pt!='\0' && lb<GLINE_MAX-1 ) {
116 last = pt;
117 /* break lines around 60 characters (or if there are no spaces at 90 chars) */
118 while ( pt-linebreaks[lb].text<60 ||
119 (pt-linebreaks[lb].text<90 && last==linebreaks[lb].text)) {
120 if ( *pt=='\n' || *pt=='\0' ) {
121 last = pt;
122 break;
123 }
124 if ( *pt==' ' )
125 last = pt;
126 ++pt;
127 }
128 if ( last==linebreaks[lb].text )
129 last = pt;
130 if ( *last==' ' || *last=='\n' ) ++last;
131 linebreaks[++lb].text = (unichar_t *) last;
132 pt = last;
133 }
134 if ( *pt!='\0' )
135 linebreaks[++lb].text = (unichar_t *) pt+u_strlen(pt);
136 for ( i=0; i<lb; ++i ) {
137 int temp = linebreaks[i+1].text - linebreaks[i].text;
138 if ( linebreaks[i+1].text[-1]==' ' || linebreaks[i+1].text[-1]=='\n' )
139 --temp;
140 linebreaks[i].text = u_copyn(linebreaks[i].text,temp);
141 }
142
143 if ( question[u_strlen(question)-1]=='\n' )
144 --lb;
145 return( lb );
146 }
147
148 /* ************************************************************************** */
149
150 #define CID_Cancel 0
151 #define CID_OK 1
152 #define CID_List 2
153 #define CID_SelectAll 3
154 #define CID_SelectNone 4
155
156 static int GCD_Select(GGadget *g, GEvent *e) {
157
158 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
159 GWindow gw = GGadgetGetWindow(g);
160 GGadget *list = GWidgetGetControl(gw,CID_List);
161
162 GGadgetSelectListItem(list,-1,GGadgetGetCid(g)==CID_SelectAll?1:0 );
163 }
164 return( true );
165 }
166
167 static int c_e_h(GWindow gw, GEvent *event) {
168 struct dlg_info *d = GDrawGetUserData(gw);
169
170 if ( event->type==et_close ) {
171 d->done = true;
172 d->ret = -1;
173 } else if ( event->type==et_resize ) {
174 GDrawRequestExpose(gw,NULL,false);
175 } else if ( event->type==et_controlevent &&
176 (event->u.control.subtype == et_buttonactivate ||
177 event->u.control.subtype == et_listdoubleclick )) {
178 d->done = true;
179 if ( GGadgetGetCid(event->u.control.g)==CID_Cancel )
180 d->ret = -1;
181 else
182 d->ret = GGadgetGetFirstListSelectedItem(GWidgetGetControl(gw,CID_List));
183 } else if ( event->type == et_char ) {
184 return( false );
185 } else if ( event->type == et_map ) {
186 /* Above palettes */
187 GDrawRaise(gw);
188 }
189 return( true );
190 }
191
192 /* ************** Parallel routines using utf8 arguments ******************** */
193
194 static GWindow DlgCreate8(const char *title,const char *question,va_list ap,
195 const char **answers, int def, int cancel,
196 struct dlg_info *d, int add_text, const char *defstr,
197 int restrict_input, int center) {
198 GTextInfo qlabels[GLINE_MAX+1], *blabels;
199 GGadgetCreateData *gcd, *array[2*(GLINE_MAX+5)+1], boxes[5], **barray, *labarray[4];
200 int lb, bcnt=0, l;
201 GRect pos;
202 GWindow gw;
203 GWindowAttrs wattrs;
204 int as, ds, ld, fh;
205 int w, maxw, bw, bspace;
206 int i, y;
207 char *buf;
208 unichar_t *ubuf;
209 extern GBox _GGadget_defaultbutton_box;
210
211 if ( d!=NULL )
212 memset(d,0,sizeof(*d));
213 buf = vsmprintf(question, ap);
214 if ( screen_display==NULL ) {
215 fprintf(stderr, "%s\n", buf);
216 if ( d!=NULL ) d->done = true;
217 free(buf);
218 return( NULL );
219 }
220 ubuf = utf82u_copy(buf);
221 free(buf);
222 if( !ubuf ) {
223 fprintf( stderr, "Failed to convert question string in DlgCreate8()\n" );
224 return( NULL );
225 }
226
227 GGadgetInit();
228 GProgressPauseTimer();
229 memset(qlabels,'\0',sizeof(qlabels));
230 lb = FindLineBreaks(ubuf,qlabels);
231 for ( bcnt=0; answers[bcnt]!=NULL; ++bcnt);
232 blabels = calloc(bcnt+1,sizeof(GTextInfo));
233 barray = calloc(2*bcnt+3,sizeof(GGadgetCreateData *));
234 for ( bcnt=0; answers[bcnt]!=NULL; ++bcnt) {
235 blabels[bcnt].text = (unichar_t *) answers[bcnt];
236 blabels[bcnt].text_is_1byte = true;
237 blabels[bcnt].text_in_resource = true; /* Look for mnemonics in the utf8 string (preceded by _) */
238 }
239
240 memset(&wattrs,0,sizeof(wattrs));
241 /* If we have many questions in quick succession the dlg will jump around*/
242 /* as it tracks the cursor (which moves to the buttons). That's not good*/
243 /* So I don't do undercursor here */
244 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_isdlg;
245 if ( restrict_input )
246 wattrs.mask |= wam_restrict;
247 else
248 wattrs.mask |= wam_notrestricted;
249 if ( center )
250 wattrs.mask |= wam_centered;
251 else
252 wattrs.mask |= wam_undercursor;
253 wattrs.not_restricted = true;
254 wattrs.restrict_input_to_me = 1;
255 wattrs.is_dlg = true;
256 wattrs.event_masks = ~(1<<et_charup);
257 wattrs.undercursor = 1;
258 wattrs.centered = 2;
259 wattrs.cursor = ct_pointer;
260 wattrs.utf8_window_title = (char *) title;
261 pos.x = pos.y = 0;
262 pos.width = 200; pos.height = 60; /* We'll figure size later */
263 /* but if we get the size too small, the cursor isn't in dlg */
264 gw = GDrawCreateTopWindow(NULL,&pos,restrict_input?d_e_h:w_e_h,d,&wattrs);
265
266 GGadgetInit();
267 GDrawSetDefaultFont(gw);
268 GDrawDefaultFontMetrics(gw,&as,&ds,&ld);
269 fh = as+ds;
270 maxw = 0;
271 for ( i=0; i<lb; ++i ) {
272 w = GDrawGetTextWidth(gw,qlabels[i].text,-1);
273 if ( w>maxw ) maxw = w;
274 }
275 if ( add_text && defstr!=NULL ) {
276 extern GResFont _gtextfield_font;
277
278 GTextFieldInit();
279 if ( _gtextfield_font.fi!=NULL ) {
280 GDrawSetFont(gw,_gtextfield_font.fi);
281 w = GDrawGetText8Width(gw,defstr,-1);
282 GDrawSetDefaultFont(gw);
283 } else
284 w = 8*GDrawGetText8Width(gw,defstr,-1)/5;
285 w += GDrawPointsToPixels(gw,40);
286 if ( w >1000 ) w = 1000;
287 if ( w>maxw ) maxw = w;
288 }
289 bw = 0;
290 for ( i=0; i<bcnt; ++i ) {
291 w = GDrawGetText8Width(gw,answers[i],-1);
292 if ( w>bw ) bw = w;
293 }
294 bw += GDrawPointsToPixels(gw,20);
295 bspace = GDrawPointsToPixels(gw,6);
296 if ( (bw+bspace) * bcnt > maxw )
297 maxw = (bw+bspace)*bcnt;
298 if ( bcnt!=1 )
299 bspace = (maxw-bcnt*bw)/(bcnt-1);
300 maxw += GDrawPointsToPixels(gw,16);
301
302 gcd = calloc(lb+bcnt+2,sizeof(GGadgetCreateData));
303 memset(boxes,0,sizeof(boxes));
304 l = 0;
305 if ( lb==1 ) {
306 gcd[0].gd.pos.width = GDrawGetTextWidth(gw,qlabels[0].text,-1);
307 gcd[0].gd.pos.x = (maxw-gcd[0].gd.pos.width)/2;
308 gcd[0].gd.pos.y = GDrawPointsToPixels(gw,6);
309 gcd[0].gd.pos.height = fh;
310 gcd[0].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
311 gcd[0].gd.label = &qlabels[0];
312 gcd[0].creator = GLabelCreate;
313 labarray[0] = GCD_Glue; labarray[1] = &gcd[0]; labarray[2] = GCD_Glue; labarray[3] = NULL;
314 boxes[2].gd.flags = gg_visible|gg_enabled;
315 boxes[2].gd.u.boxelements = labarray;
316 boxes[2].creator = GHBoxCreate;
317 array[0] = &boxes[2]; array[1] = NULL;
318 l = 1;
319 } else for ( i=0; i<lb; ++i ) {
320 gcd[i].gd.pos.x = GDrawPointsToPixels(gw,8);
321 gcd[i].gd.pos.y = GDrawPointsToPixels(gw,6)+i*fh;
322 gcd[i].gd.pos.width = GDrawGetTextWidth(gw,qlabels[i].text,-1);
323 gcd[i].gd.pos.height = fh;
324 gcd[i].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
325 gcd[i].gd.label = &qlabels[i];
326 gcd[i].creator = GLabelCreate;
327 array[2*l] = &gcd[i]; array[2*l++ +1] = NULL;
328 }
329 y = GDrawPointsToPixels(gw,12)+lb*fh;
330 if ( add_text ) {
331 gcd[bcnt+lb].gd.pos.x = GDrawPointsToPixels(gw,8);
332 gcd[bcnt+lb].gd.pos.y = y;
333 gcd[bcnt+lb].gd.pos.width = maxw-2*GDrawPointsToPixels(gw,6);
334 gcd[bcnt+lb].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0 | gg_text_xim;
335 gcd[bcnt+lb].gd.cid = bcnt;
336 gcd[bcnt+lb].creator = GTextFieldCreate;
337 if ( add_text==2 )
338 gcd[bcnt+lb].creator = GPasswordCreate;
339 y += fh + GDrawPointsToPixels(gw,6) + GDrawPointsToPixels(gw,10);
340 array[2*l] = &gcd[bcnt+lb]; array[2*l++ +1] = NULL;
341 }
342 y += GDrawPointsToPixels(gw,2);
343 for ( i=0; i<bcnt; ++i ) {
344 gcd[i+lb].gd.pos.x = GDrawPointsToPixels(gw,8) + i*(bw+bspace);
345 gcd[i+lb].gd.pos.y = y;
346 gcd[i+lb].gd.pos.width = bw;
347 gcd[i+lb].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
348 if ( i==def ) {
349 gcd[i+lb].gd.flags |= gg_but_default;
350 if ( _GGadget_defaultbutton_box.flags&box_draw_default ) {
351 gcd[i+lb].gd.pos.x -= GDrawPointsToPixels(gw,3);
352 gcd[i+lb].gd.pos.y -= GDrawPointsToPixels(gw,3);
353 gcd[i+lb].gd.pos.width += 2*GDrawPointsToPixels(gw,3);
354 }
355 }
356 if ( i==cancel )
357 gcd[i+lb].gd.flags |= gg_but_cancel;
358 gcd[i+lb].gd.cid = i;
359 gcd[i+lb].gd.label = &blabels[i];
360 gcd[i+lb].creator = GButtonCreate;
361 barray[2*i] = GCD_Glue; barray[2*i+1] = &gcd[i+lb];
362 }
363 barray[2*i] = GCD_Glue; barray[2*i+1] = NULL;
364 boxes[3].gd.flags = gg_visible|gg_enabled;
365 boxes[3].gd.u.boxelements = barray;
366 boxes[3].creator = GHBoxCreate;
367 array[2*l] = &boxes[3]; array[2*l++ +1] = NULL;
368 array[2*l] = NULL;
369
370 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
371 boxes[0].gd.flags = gg_visible|gg_enabled;
372 boxes[0].gd.u.boxelements = array;
373 boxes[0].creator = GHVGroupCreate;
374
375 GGadgetsCreate(gw,boxes);
376 GHVBoxSetExpandableCol(boxes[3].ret,gb_expandgluesame);
377 if ( boxes[2].ret!=NULL )
378 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandglue);
379 GHVBoxFitWindow(boxes[0].ret);
380
381 if ( d!=NULL ) {
382 d->ret = cancel;
383 d->bcnt = bcnt;
384 }
385 GDrawSetVisible(gw,true);
386 free(blabels);
387 free(gcd);
388 free(barray);
389 for ( i=0; i<lb; ++i )
390 free(qlabels[i].text);
391 free(ubuf);
392 GProgressResumeTimer();
393 return( gw );
394 }
395
396 int GWidgetAsk8(const char *title,
397 const char **answers, int def, int cancel,
398 const char *question,...) {
399 struct dlg_info d;
400 GWindow gw;
401 va_list ap;
402
403 if ( screen_display==NULL )
404 return( def );
405
406 va_start(ap,question);
407 gw = DlgCreate8(title,question,ap,answers,def,cancel,&d,false,NULL,true,false);
408 va_end(ap);
409
410 while ( !d.done )
411 GDrawProcessOneEvent(NULL);
412 GDrawDestroyWindow(gw);
413 GDrawSync(NULL);
414 GDrawProcessPendingEvents(NULL);
415 return(d.ret);
416 }
417
418 int GWidgetAskCentered8(const char *title,
419 const char **answers, int def, int cancel,
420 const char *question, ... ) {
421 struct dlg_info d;
422 GWindow gw;
423 va_list ap;
424
425 if ( screen_display==NULL )
426 return( def );
427
428 va_start(ap,question);
429 gw = DlgCreate8(title,question,ap,answers,def,cancel,&d,false,NULL,true,true);
430 va_end(ap);
431
432 while ( !d.done )
433 GDrawProcessOneEvent(NULL);
434 GDrawDestroyWindow(gw);
435 GDrawSync(NULL);
436 GDrawProcessPendingEvents(NULL);
437 return(d.ret);
438 }
439
440 char *GWidgetAskString8(const char *title,const char *def,
441 const char *question,...) {
442 struct dlg_info d;
443 GWindow gw;
444 char *ret = NULL;
445 char *ocb[3];
446 va_list ap;
447
448 if ( screen_display==NULL )
449 return( copy(def ));
450
451 ocb[2]=NULL;
452 ocb[0] = _("_OK");
453 ocb[1] = _("_Cancel");
454 va_start(ap,question);
455 gw = DlgCreate8(title,question,ap,(const char **) ocb,0,1,&d,true,def,true,false);
456 va_end(ap);
457 if ( def!=NULL && *def!='\0' )
458 GGadgetSetTitle8(GWidgetGetControl(gw,2),def);
459 while ( !d.done )
460 GDrawProcessOneEvent(NULL);
461 if ( d.ret==0 )
462 ret = GGadgetGetTitle8(GWidgetGetControl(gw,2));
463 GDrawDestroyWindow(gw);
464 GDrawSync(NULL);
465 GDrawProcessPendingEvents(NULL);
466 return(ret);
467 }
468
469 char *GWidgetAskPassword8(const char *title,const char *def,
470 const char *question,...) {
471 struct dlg_info d;
472 GWindow gw;
473 char *ret = NULL;
474 char *ocb[3];
475 va_list ap;
476
477 if ( screen_display==NULL )
478 return( copy(def ));
479
480 ocb[0] = _("_OK");
481 ocb[1] = _("_Cancel");
482 ocb[2]=NULL;
483
484 va_start(ap,question);
485 gw = DlgCreate8(title,question,ap,(const char **) ocb,0,1,&d,2,def,true,false);
486 va_end(ap);
487 if ( def!=NULL && *def!='\0' )
488 GGadgetSetTitle8(GWidgetGetControl(gw,2),def);
489 while ( !d.done )
490 GDrawProcessOneEvent(NULL);
491 if ( d.ret==0 )
492 ret = GGadgetGetTitle8(GWidgetGetControl(gw,2));
493 GDrawDestroyWindow(gw);
494 GDrawSync(NULL);
495 GDrawProcessPendingEvents(NULL);
496 return(ret);
497 }
498
499 void _GWidgetPostNotice8(const char *title,const char *statement,va_list ap, int timeout) {
500 GWindow gw;
501 char *ob[2];
502
503 /* Force an old notice to disappear */
504 if ( title==NULL ) {
505 if ( last!=NULL )
506 GDrawDestroyWindow(last);
507 return;
508 }
509
510 ob[0] = _("_OK");
511 ob[1]=NULL;
512 gw = DlgCreate8(title,statement,ap,(const char **) ob,0,0,NULL,false,NULL,false,true);
513 if ( gw!=NULL && timeout>0 )
514 GDrawRequestTimer(gw,timeout*1000,0,NULL);
515 /* Continue merrily on our way. Window will destroy itself in 40 secs */
516 /* or when user kills it. We can ignore it */
517 last = gw;
518 last_title = title;
519 }
520
521 void GWidgetPostNotice8(const char *title,const char *statement,...) {
522 va_list ap;
523
524 /* Force an old notice to disappear */
525 if ( title==NULL ) {
526 if ( last!=NULL )
527 GDrawDestroyWindow(last);
528 return;
529 }
530
531 va_start(ap,statement);
532 _GWidgetPostNotice8(title,statement,ap,40);
533 va_end(ap);
534 }
535
536 void GWidgetPostNoticeTimeout8(int timeout,const char *title,const char *statement,...) {
537 va_list ap;
538
539 /* Force an old notice to disappear */
540 if ( title==NULL ) {
541 if ( last!=NULL )
542 GDrawDestroyWindow(last);
543 return;
544 }
545
546 va_start(ap,statement);
547 _GWidgetPostNotice8(title,statement,ap,timeout);
548 va_end(ap);
549 }
550
551 int GWidgetPostNoticeActive8(const char *title) {
552 return( last!=NULL && last_title == title );
553 }
554
555 void GWidgetError8(const char *title,const char *statement, ...) {
556 struct dlg_info d;
557 GWindow gw;
558 char *ob[2];
559 va_list ap;
560
561 ob[0] = _("_OK");
562 ob[1]=NULL;
563 va_start(ap,statement);
564 gw = DlgCreate8(title,statement,ap,(const char **) ob,0,0,&d,false,NULL,true,true);
565 va_end(ap);
566 if ( gw!=NULL ) {
567 while ( !d.done )
568 GDrawProcessOneEvent(NULL);
569 GDrawDestroyWindow(gw);
570 }
571 }
572
573 static GWindow ChoiceDlgCreate8(struct dlg_info *d,const char *title,
574 const char *question, va_list ap,
575 const char **choices, int cnt, const char *multisel,
576 char *buts[2], int def,
577 int restrict_input, int center) {
578 GTextInfo qlabels[GLINE_MAX+1], *llabels, blabel[4];
579 GGadgetCreateData *gcd, **array, boxes[5], *labarray[4], *barray[10], *barray2[8];
580 int lb,l;
581 GRect pos;
582 GWindow gw;
583 GWindowAttrs wattrs;
584 int as, ds, ld, fh;
585 int w, maxw;
586 int i, y, listi;
587 char buf[600];
588 unichar_t *ubuf;
589 extern GBox _GGadget_defaultbutton_box;
590
591 memset(d,0,sizeof(*d));
592 GProgressPauseTimer();
593 vsnprintf(buf,sizeof(buf)/sizeof(buf[0]),question,ap);
594 ubuf = utf82u_copy(buf);
595 memset(qlabels,'\0',sizeof(qlabels));
596 lb = FindLineBreaks(ubuf,qlabels);
597 llabels = calloc(cnt+1,sizeof(GTextInfo));
598 for ( i=0; i<cnt; ++i) {
599 if ( choices[i][0]=='-' && choices[i][1]=='\0' )
600 llabels[i].line = true;
601 else {
602 llabels[i].text = (unichar_t *) choices[i];
603 llabels[i].text_is_1byte = true;
604 }
605 if ( multisel )
606 llabels[i].selected = multisel[i];
607 else
608 llabels[i].selected = (i==def);
609 }
610
611 memset(&wattrs,0,sizeof(wattrs));
612 /* If we have many questions in quick succession the dlg will jump around*/
613 /* as it tracks the cursor (which moves to the buttons). That's not good*/
614 /* So I don't do undercursor here */
615 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_isdlg;
616 if ( restrict_input )
617 wattrs.mask |= wam_restrict;
618 else
619 wattrs.mask |= wam_notrestricted;
620 if ( center )
621 wattrs.mask |= wam_centered;
622 else
623 wattrs.mask |= wam_undercursor;
624 wattrs.not_restricted = true;
625 wattrs.restrict_input_to_me = 1;
626 wattrs.is_dlg = true;
627 wattrs.event_masks = ~(1<<et_charup);
628 wattrs.undercursor = 1;
629 wattrs.centered = 2;
630 wattrs.cursor = ct_pointer;
631 wattrs.utf8_window_title = (char *) title;
632 pos.x = pos.y = 0;
633 pos.width = 220; pos.height = 60; /* We'll figure size later */
634 /* but if we get the size too small, the cursor isn't in dlg */
635 gw = GDrawCreateTopWindow(NULL,&pos,c_e_h,d,&wattrs);
636
637 GGadgetInit();
638 GDrawSetDefaultFont(gw);
639 GDrawDefaultFontMetrics(gw,&as,&ds,&ld);
640 fh = as+ds;
641 maxw = 220;
642 for ( i=0; i<cnt; ++i) {
643 if ( llabels[i].text!=NULL ) { /* lines */
644 w = GDrawGetText8Width(gw,(char *) llabels[i].text,-1);
645 if ( w>900 ) w = 900;
646 if ( w>maxw ) maxw = w;
647 }
648 }
649 for ( i=0; i<lb; ++i ) {
650 w = GDrawGetTextWidth(gw,qlabels[i].text,-1);
651 if ( w>maxw ) maxw = w;
652 }
653 maxw += GDrawPointsToPixels(gw,20);
654
655 gcd = calloc(lb+1+2+2+2,sizeof(GGadgetCreateData));
656 array = calloc(2*(lb+1+2+2+2+1),sizeof(GGadgetCreateData *));
657 memset(boxes,0,sizeof(boxes));
658 l=0;
659 if ( lb==1 ) {
660 gcd[0].gd.pos.width = GDrawGetTextWidth(gw,qlabels[0].text,-1);
661 gcd[0].gd.pos.x = (maxw-gcd[0].gd.pos.width)/2;
662 gcd[0].gd.pos.y = GDrawPointsToPixels(gw,6);
663 gcd[0].gd.pos.height = fh;
664 gcd[0].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
665 gcd[0].gd.label = &qlabels[0];
666 gcd[0].creator = GLabelCreate;
667 labarray[0] = GCD_Glue; labarray[1] = &gcd[0]; labarray[2] = GCD_Glue; labarray[3] = NULL;
668 boxes[2].gd.flags = gg_visible|gg_enabled;
669 boxes[2].gd.u.boxelements = labarray;
670 boxes[2].creator = GHBoxCreate;
671 array[0] = &boxes[2]; array[1] = NULL;
672 i = l = 1;
673 } else for ( i=0; i<lb; ++i ) {
674 gcd[i].gd.pos.x = GDrawPointsToPixels(gw,8);
675 gcd[i].gd.pos.y = GDrawPointsToPixels(gw,6)+i*fh;
676 gcd[i].gd.pos.width = GDrawGetTextWidth(gw,qlabels[i].text,-1);
677 gcd[i].gd.pos.height = fh;
678 gcd[i].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
679 gcd[i].gd.label = &qlabels[i];
680 gcd[i].creator = GLabelCreate;
681 array[2*l] = &gcd[i]; array[2*l++ +1] = NULL;
682 }
683
684 y = GDrawPointsToPixels(gw,12)+lb*fh;
685 gcd[i].gd.pos.x = GDrawPointsToPixels(gw,8); gcd[i].gd.pos.y = y;
686 gcd[i].gd.pos.width = maxw - 2*GDrawPointsToPixels(gw,8);
687 gcd[i].gd.pos.height = (cnt<4?4:cnt<8?cnt:8)*fh + 2*GDrawPointsToPixels(gw,6);
688 gcd[i].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
689 if ( multisel )
690 gcd[i].gd.flags |= gg_list_multiplesel;
691 else
692 gcd[i].gd.flags |= gg_list_exactlyone;
693 gcd[i].gd.u.list = llabels;
694 gcd[i].gd.cid = CID_List;
695 listi = i;
696 gcd[i++].creator = GListCreate;
697 y += gcd[i-1].gd.pos.height + GDrawPointsToPixels(gw,10);
698 array[2*l] = &gcd[i-1]; array[2*l++ +1] = NULL;
699
700 memset(blabel,'\0',sizeof(blabel));
701 if ( multisel ) {
702 y -= GDrawPointsToPixels(gw,5);
703 gcd[i].gd.pos.x = GDrawPointsToPixels(gw,15); gcd[i].gd.pos.y = y;
704 gcd[i].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0;
705 gcd[i].gd.label = &blabel[2];
706
707 blabel[2].text = (unichar_t *) _("Select _All");
708 blabel[2].text_is_1byte = true;
709 blabel[2].text_in_resource = true;
710 gcd[i].gd.cid = CID_SelectAll;
711 gcd[i].gd.handle_controlevent = GCD_Select;
712 gcd[i++].creator = GButtonCreate;
713 barray2[0] = GCD_Glue; barray2[1] = &gcd[i-1];
714
715 gcd[i].gd.pos.x = maxw-GDrawPointsToPixels(gw,15)-
716 GDrawPointsToPixels(gw,GIntGetResource(_NUM_Buttonsize));
717 gcd[i].gd.pos.y = y;
718 gcd[i].gd.pos.width = -1;
719 gcd[i].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0 ;
720 gcd[i].gd.label = &blabel[3];
721 blabel[3].text = (unichar_t *) _("_None");
722 blabel[3].text_is_1byte = true;
723 blabel[3].text_in_resource = true;
724 gcd[i].gd.cid = CID_SelectNone;
725 gcd[i].gd.handle_controlevent = GCD_Select;
726 gcd[i++].creator = GButtonCreate;
727 y += GDrawPointsToPixels(gw,30);
728 barray2[2] = GCD_Glue; barray2[3] = &gcd[i-1]; barray2[4] = GCD_Glue; barray2[5] = NULL;
729 boxes[3].gd.flags = gg_visible|gg_enabled;
730 boxes[3].gd.u.boxelements = barray2;
731 boxes[3].creator = GHBoxCreate;
732 array[2*l] = &boxes[3]; array[2*l++ +1] = NULL;
733 }
734
735 if ( _GGadget_defaultbutton_box.flags&box_draw_default ) {
736 gcd[i].gd.pos.x = GDrawPointsToPixels(gw,15)-3; gcd[i].gd.pos.y = y-3;
737 } else {
738 gcd[i].gd.pos.x = GDrawPointsToPixels(gw,15); gcd[i].gd.pos.y = y;
739 }
740 gcd[i].gd.pos.width = -1;
741 gcd[i].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels |gg_but_default | gg_pos_use0;
742 gcd[i].gd.label = &blabel[0];
743 blabel[0].text = (unichar_t *) buts[0];
744 blabel[0].text_is_1byte = true;
745 blabel[0].text_in_resource = true;
746 gcd[i].gd.cid = CID_OK;
747 gcd[i++].creator = GButtonCreate;
748 barray[0] = GCD_Glue; barray[1] = &gcd[i-1]; barray[2] = GCD_Glue; barray[3] = GCD_Glue;
749
750 gcd[i].gd.pos.x = maxw-GDrawPointsToPixels(gw,15)-
751 GDrawPointsToPixels(gw,GIntGetResource(_NUM_Buttonsize));
752 gcd[i].gd.pos.y = y;
753 gcd[i].gd.pos.width = -1;
754 gcd[i].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels |gg_but_cancel | gg_pos_use0;
755 gcd[i].gd.label = &blabel[1];
756 blabel[1].text = (unichar_t *) buts[1];
757 blabel[1].text_is_1byte = true;
758 blabel[1].text_in_resource = true;
759 gcd[i].gd.cid = CID_Cancel;
760 gcd[i++].creator = GButtonCreate;
761 barray[4] = barray[5] = barray[6] = GCD_Glue; barray[7] = &gcd[i-1]; barray[8] = GCD_Glue; barray[9] = NULL;
762
763 boxes[4].gd.flags = gg_visible|gg_enabled;
764 boxes[4].gd.u.boxelements = barray;
765 boxes[4].creator = GHBoxCreate;
766 array[2*l] = &boxes[4]; array[2*l++ +1] = NULL;
767 array[2*l] = NULL;
768
769 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
770 boxes[0].gd.flags = gg_visible|gg_enabled;
771 boxes[0].gd.u.boxelements = array;
772 boxes[0].creator = GHVGroupCreate;
773
774 GGadgetsCreate(gw,boxes);
775 GHVBoxSetExpandableCol(boxes[4].ret,gb_expandgluesame);
776 if ( boxes[3].ret!=NULL )
777 GHVBoxSetExpandableCol(boxes[3].ret,gb_expandgluesame);
778 if ( boxes[2].ret!=NULL )
779 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandglue);
780 GHVBoxFitWindow(boxes[0].ret);
781 GDrawSetVisible(gw,true);
782 d->ret = -1;
783 d->size_diff = pos.height - gcd[listi].gd.pos.height;
784 free(llabels);
785 free(array);
786 free(gcd);
787 for ( i=0; i<lb; ++i )
788 free(qlabels[i].text);
789 free(ubuf);
790 GProgressResumeTimer();
791 return( gw );
792 }
793
794 int GWidgetChoices8(const char *title, const char **choices,int cnt, int def,
795 const char *question,...) {
796 struct dlg_info d;
797 GWindow gw;
798 va_list ap;
799 char *buts[3];
800
801 if ( screen_display==NULL )
802 return( -2 );
803
804 va_start(ap,question);
805 buts[0] = _("_OK");
806 buts[1] = _("_Cancel");
807 buts[2] = NULL;
808 gw = ChoiceDlgCreate8(&d,title,question,ap,
809 choices,cnt,NULL, buts,def,true,false);
810 va_end(ap);
811 while ( !d.done )
812 GDrawProcessOneEvent(NULL);
813 GDrawDestroyWindow(gw);
814 GDrawSync(NULL);
815 GDrawProcessPendingEvents(NULL);
816 return(d.ret);
817 }
818
819 int GWidgetChoicesB8(char *title, const char **choices, int cnt, int def,
820 char *buts[2], const char *question,...) {
821 struct dlg_info d;
822 GWindow gw;
823 va_list ap;
824
825 if ( screen_display==NULL )
826 return( -2 );
827
828 va_start(ap,question);
829 gw = ChoiceDlgCreate8(&d,title,question,ap,
830 choices,cnt,NULL,buts,def,true,false);
831 va_end(ap);
832 while ( !d.done )
833 GDrawProcessOneEvent(NULL);
834 GDrawDestroyWindow(gw);
835 GDrawSync(NULL);
836 GDrawProcessPendingEvents(NULL);
837 return(d.ret);
838 }
839
840 int GWidgetChoicesBM8(const char *title, const char **choices,char *sel,
841 int cnt, char *buts[2], const char *question,...) {
842 struct dlg_info d;
843 GWindow gw;
844 va_list ap;
845 GGadget *list;
846 GTextInfo **lsel;
847 int i; int32_t len;
848 char *buttons[3];
849
850 if ( screen_display==NULL )
851 return( -2 );
852
853 if ( buts==NULL ) {
854 buts = buttons;
855 buttons[2] = NULL;
856 buts[0] = _("_OK");
857 buts[1] = _("_Cancel");
858 }
859 va_start(ap,question);
860 gw = ChoiceDlgCreate8(&d,title,question,ap,
861 choices,cnt,sel,buts,-1,true,false);
862 va_end(ap);
863 while ( !d.done )
864 GDrawProcessOneEvent(NULL);
865 if ( d.ret==-1 ) {
866 for ( i=0; i<cnt; ++i )
867 sel[i] = 0;
868 } else {
869 list = GWidgetGetControl(gw,CID_List);
870 lsel = GGadgetGetList(list,&len);
871 for ( i=0; i<len; ++i )
872 sel[i] = lsel[i]->selected;
873 }
874 GDrawDestroyWindow(gw);
875 GDrawSync(NULL);
876 GDrawProcessPendingEvents(NULL);
877 return(d.ret);
878 }