Debian Codegraph @

gdraw/gbuttons.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 "ggadgetP.h"
32 #include "gkeysym.h"
33 #include "gwidget.h"
34 #include "ustring.h"
35
36 static void GListButtonDoPopup(GListButton *);
37
38 GBox _GGadget_button_box = GBOX_EMPTY; /* Don't initialize here */
39 GBox _GGadget_defaultbutton_box = GBOX_EMPTY; /* Don't initialize here */
40 static GBox _GGadget_cancelbutton_box = GBOX_EMPTY; /* Don't initialize here */
41 static GBox _GGadget_colorbutton_box = GBOX_EMPTY; /* Don't initialize here */
42 static GBox _GGadget_droplist_box = GBOX_EMPTY; /* Don't initialize here */
43 static GBox label_box = GBOX_EMPTY; /* Don't initialize here */
44 static int shift_on_press = 0;
45 static GResFont label_font = GRESFONT_INIT("400 10pt " SANS_UI_FAMILIES);
46 static GResFont button_font = GRESFONT_INIT("400 10pt " SANS_UI_FAMILIES);
47 #define COLOR_BUTTON_BOX_LEN 10
48
49 static GResInfo gcancel_ri, gdefault_ri, gbutton_ri, gdroplist_ri, gcolor_ri;
50 GResInfo glabel_ri = {
51 &gbutton_ri, &ggadget_ri,NULL, NULL,
52 &label_box,
53 &label_font,
54 NULL,
55 NULL,
56 N_("Label"),
57 N_("Text Labels"),
58 "GLabel",
59 "Gdraw",
60 false,
61 false,
62 omf_border_type|omf_border_width|omf_padding,
63 { bt_none, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
64 GBOX_EMPTY,
65 NULL,
66 NULL,
67 NULL
68 };
69
70 static GTextInfo button_lab[] = {
71 { (unichar_t *) "Disabled Button", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
72 { (unichar_t *) "Enabled Button" , NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }
73 };
74 static GGadgetCreateData button_gcd[] = {
75 { GButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &button_lab[0], { NULL }, gg_visible, NULL, NULL }, NULL, NULL },
76 { GButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &button_lab[1], { NULL }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }
77 };
78 static GGadgetCreateData *barray[] = { GCD_Glue, &button_gcd[0], GCD_Glue, &button_gcd[1], GCD_Glue, NULL, NULL };
79 static GGadgetCreateData buttonbox =
80 {GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) barray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
81 static struct resed gbutton_re[] = {
82 { N_("Shift On Press"), "ShiftOnPress", rt_bool, &shift_on_press, N_("When true the button will move slightly when pressed"), NULL, { 0 }, 0, 0 },
83 RESED_EMPTY
84 };
85 static GResInfo gbutton_ri = {
86 &gdefault_ri, &ggadget_ri,&gdefault_ri, &gcancel_ri,
87 &_GGadget_button_box,
88 &button_font,
89 &buttonbox,
90 gbutton_re,
91 N_("Button"),
92 N_("Buttons"),
93 "GButton",
94 "Gdraw",
95 true,
96 false,
97 box_foreground_border_inner|box_foreground_border_outer|
98 /*box_active_border_inner|*/box_do_depressed_background|box_draw_default,
99 /* Will be initialized later */
100 { bt_box, bs_roundrect, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
101 GBOX_EMPTY,
102 NULL,
103 NULL,
104 NULL
105 };
106 static GGadgetCreateData def_gcd[] = {
107 { GButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &button_lab[0], { NULL }, gg_visible|gg_but_default, NULL, NULL }, NULL, NULL },
108 { GButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &button_lab[1], { NULL }, gg_visible|gg_enabled|gg_but_default, NULL, NULL }, NULL, NULL }
109 };
110 static GGadgetCreateData *darray[] = { GCD_Glue, &def_gcd[0], GCD_Glue, &def_gcd[1], GCD_Glue, NULL, NULL };
111 static GGadgetCreateData defbox =
112 { GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) darray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
113 static GResInfo gdefault_ri = {
114 &gcancel_ri, &gbutton_ri,&gcancel_ri,NULL,
115 &_GGadget_defaultbutton_box,
116 NULL,
117 &defbox,
118 NULL,
119 N_("Default Button"),
120 N_("Default Buttons"),
121 "GDefaultButton",
122 "Gdraw",
123 true,
124 false,
125 0,
126 GBOX_EMPTY,
127 GBOX_EMPTY,
128 NULL,
129 NULL,
130 NULL
131 };
132 static GGadgetCreateData cancel_gcd[] = {
133 { GButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &button_lab[0], { NULL }, gg_visible|gg_but_cancel, NULL, NULL }, NULL, NULL },
134 { GButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &button_lab[1], { NULL }, gg_visible|gg_enabled|gg_but_cancel, NULL, NULL }, NULL, NULL }
135 };
136 static GGadgetCreateData *carray[] = { GCD_Glue, &cancel_gcd[0], GCD_Glue, &cancel_gcd[1], GCD_Glue, NULL, NULL };
137 static GGadgetCreateData cancelbox =
138 {GHVGroupCreate, { {2, 2, 0, 0}, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) carray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
139 static GResInfo gcancel_ri = {
140 &gcolor_ri, &gbutton_ri,&gdefault_ri,NULL,
141 &_GGadget_cancelbutton_box,
142 NULL,
143 &cancelbox,
144 NULL,
145 N_("Cancel Button"),
146 N_("Cancel Buttons"),
147 "GCancelButton",
148 "Gdraw",
149 true,
150 false,
151 0,
152 GBOX_EMPTY,
153 GBOX_EMPTY,
154 NULL,
155 NULL,
156 NULL
157 };
158 static GGadgetCreateData color_gcd[] = {
159 { GColorButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) (intptr_t) (0x000000) }, gg_visible, NULL, NULL }, NULL, NULL },
160 { GColorButtonCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) (intptr_t) (0x000000) }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }
161 };
162 static GGadgetCreateData *colarray[] = { GCD_Glue, &color_gcd[0], GCD_Glue, &color_gcd[1], GCD_Glue, NULL, NULL };
163 static GGadgetCreateData colorbox =
164 { GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) colarray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
165 static GResInfo gcolor_ri = {
166 &gdroplist_ri, &gbutton_ri,NULL,NULL,
167 &_GGadget_colorbutton_box,
168 NULL,
169 &colorbox,
170 NULL,
171 N_("Color Button"),
172 N_("Color Button"),
173 "GColorButton",
174 "Gdraw",
175 true,
176 false,
177 0,
178 GBOX_EMPTY,
179 GBOX_EMPTY,
180 NULL,
181 NULL,
182 NULL
183 };
184 static GTextInfo list_choices[] = {
185 { (unichar_t *) "1", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
186 { (unichar_t *) "2", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
187 { (unichar_t *) "3", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
188 GTEXTINFO_EMPTY
189 };
190
191 static int GDropListRIInit(GResInfo *ri) {
192 if ( ri->is_initialized )
193 return false;
194 return _GResEditInitialize(ri);
195 }
196
197 static GGadgetCreateData droplist_gcd[] = {
198 { GListButtonCreate, { { 0, 0, 80, 0 }, NULL, 0, 0, 0, 0, 0, &list_choices[0], { list_choices }, gg_visible, NULL, NULL }, NULL, NULL },
199 { GListButtonCreate, { { 0, 0, 80, 0 }, NULL, 0, 0, 0, 0, 0, &list_choices[1], { list_choices }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }
200 };
201 static GGadgetCreateData *dlarray[] = { GCD_Glue, &droplist_gcd[0], GCD_Glue, &droplist_gcd[1], GCD_Glue, NULL, NULL };
202 static GGadgetCreateData droplistbox =
203 { GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) dlarray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
204 extern GResInfo gradio_ri;
205 static GResInfo gdroplist_ri = {
206 &gradio_ri, &gbutton_ri,&listmark_ri,NULL,
207 &_GGadget_droplist_box,
208 NULL,
209 &droplistbox,
210 NULL,
211 N_("Drop List Button"),
212 N_("Drop List Button"),
213 "GDropList",
214 "Gdraw",
215 true,
216 false,
217 0,
218 { bt_none, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 },
219 GBOX_EMPTY,
220 NULL,
221 GDropListRIInit,
222 NULL
223 };
224
225 static void GButtonInvoked(GButton *b,GEvent *ev) {
226 GEvent e;
227
228 if ( b->labeltype==3 ) {
229 struct hslrgba hsl;
230 gColor2Hslrgba(&hsl,((GColorButton *) b)->col);
231 hsl = GWidgetColorA(_("Pick a color"),&hsl,NULL);
232 if ( hsl.rgb ) {
233 ((GColorButton *) b)->col = gHslrgba2Color(&hsl);
234 GGadgetRedraw(&b->g);
235 } else
236 return;
237 }
238 e.type = et_controlevent;
239 e.w = b->g.base;
240 e.u.control.subtype = et_buttonactivate;
241 e.u.control.g = &b->g;
242 if ( ev!=NULL && ev->type==et_mouseup ) {
243 e.u.control.u.button.clicks = ev->u.mouse.clicks;
244 e.u.control.u.button.button = ev->u.mouse.button;
245 e.u.control.u.button.state = ev->u.mouse.state;
246 } else {
247 e.u.control.u.button.clicks = 0;
248 e.u.control.u.button.button = 0;
249 e.u.control.u.button.state = 0;
250 }
251 if ( b->g.handle_controlevent != NULL )
252 (b->g.handle_controlevent)(&b->g,&e);
253 else
254 GDrawPostEvent(&e);
255 }
256
257 static void GButtonPressed(GButton *b) {
258 GEvent e;
259
260 if ( b->labeltype==2 && ((GListButton *) b)->ltot>0 )
261 GListButtonDoPopup((GListButton *) b);
262 else {
263 e.type = et_controlevent;
264 e.w = b->g.base;
265 e.u.control.subtype = et_buttonpress;
266 e.u.control.g = &b->g;
267 if ( b->g.handle_controlevent != NULL )
268 (b->g.handle_controlevent)(&b->g,&e);
269 else
270 GDrawPostEvent(&e);
271 }
272 }
273
274 static int gbutton_stringsize( GButton *gb,unichar_t *label,int *lcnt ) {
275 int maxtextwidth = 0;
276 unichar_t *pt, *start;
277
278 for ( pt = label; ; ) {
279 for ( start=pt; *pt!='\0' && *pt!='\n'; ++pt );
280 if ( pt!=start ) {
281 int w = GDrawGetTextWidth(gb->g.base,start,pt-start);
282 if ( w>maxtextwidth ) maxtextwidth=w;
283 }
284 ++(*lcnt);
285 if ( *pt=='\0' )
286 break;
287 ++pt;
288 }
289 return maxtextwidth;
290 }
291
292 static unichar_t *gbutton_textsize( GButton *gb, int *_maxlcnt, int *_maxw ) {
293 int i, lcnt, maxlcnt, maxtextwidth;
294 unichar_t *ltxt;
295 GFont *old;
296
297 old = GDrawSetFont(gb->g.base,gb->font);
298
299 maxtextwidth = maxlcnt = 0;
300 if ( gb->label!=NULL ) {
301 maxtextwidth = gbutton_stringsize( gb,gb->label,&maxlcnt );
302 ltxt = gb->label;
303 }
304 /* If there is a list of alternate labels, iterate through all */
305 /* and get the maximum width and height */
306 if ( gb->labeltype == 0 && gb->ti!=NULL ) {
307 for ( i=0; i<gb->ltot; i++ ) {
308 lcnt = 0;
309 int w = gbutton_stringsize( gb,gb->ti[i]->text,&lcnt );
310 if ( w>maxtextwidth ) {
311 maxtextwidth=w;
312 ltxt = gb->ti[i]->text;
313 }
314 if ( lcnt>maxlcnt ) maxlcnt=lcnt;
315 }
316 }
317 (void) GDrawSetFont(gb->g.base,old);
318 *_maxlcnt = maxlcnt;
319 *_maxw = maxtextwidth;
320 return( ltxt );
321 }
322
323 static int gbutton_expose(GWindow pixmap, GGadget *g, GEvent *event) {
324 GImageButton *gb = (GImageButton *) g;
325 int off = gb->within && gb->shiftonpress ? gb->pressed : 0;
326 int x = g->inner.x + off;
327 GImage *img = gb->image;
328 GRect old1, old2;
329 int width;
330 int marklen = GDrawPointsToPixels(pixmap,_GListMarkSize),
331 spacing = GDrawPointsToPixels(pixmap,_GGadget_TextImageSkip);
332 int yoff;
333 GRect unpadded_inner;
334 int pad, lcnt, maxtextwidth;
335 unichar_t *pt, *start;
336 int cbbl=0;
337
338 if ( g->state == gs_invisible )
339 return( false );
340 else if ( gb->labeltype!=1 /* Not image buttons */ )
341 /* Do Nothing */;
342 else if ( g->state == gs_disabled ) {
343 if ( gb->disabled != NULL ) img = gb->disabled;
344 } else if ( gb->pressed && gb->within && gb->active!=NULL )
345 img = gb->active;
346 else if ( gb->within )
347 img = gb->img_within;
348
349 GDrawPushClip(pixmap,&g->r,&old1);
350
351 GBoxDrawBackground(pixmap,&g->r,g->box,
352 gb->within && gb->pressed? gs_pressedactive: g->state,gb->is_default);
353 if ( g->box->border_type!=bt_none ||
354 (g->box->flags&(box_foreground_border_inner|box_foreground_border_outer|box_active_border_inner))!=0 ) {
355 GBoxDrawBorder(pixmap,&g->r,g->box,g->state,gb->is_default);
356
357 unpadded_inner = g->inner;
358 pad = GDrawPointsToPixels(g->base,g->box->padding);
359 unpadded_inner.x -= pad; unpadded_inner.y -= pad;
360 unpadded_inner.width += 2*pad; unpadded_inner.height += 2*pad;
361 GDrawPushClip(pixmap,&unpadded_inner,&old2);
362 }
363 if ( gb->font!=NULL )
364 GDrawSetFont(pixmap,gb->font);
365
366 gbutton_textsize((GButton *) gb,&lcnt,&maxtextwidth);
367 yoff = (g->inner.height-lcnt*gb->fh)/2;
368 if ( lcnt>1 && yoff<0 )
369 yoff = 0;
370
371 if ( gb->g.takes_input ) {
372 width = 0;
373 if ( img!=NULL ) {
374 width = GImageGetScaledWidth(pixmap,img);
375 if ( gb->label!=NULL )
376 width += spacing;
377 }
378 if ( gb->label!=NULL )
379 width += maxtextwidth;
380 if ( gb->labeltype==3 ) {
381 cbbl = GDrawPointsToPixels(NULL,COLOR_BUTTON_BOX_LEN);
382 width += cbbl + spacing;
383 }
384 if ( width<=g->inner.width )
385 x += ( g->inner.width-width )/2;
386 else
387 x += (g->inner.y-g->r.y);
388 }
389 if ( gb->labeltype==3 ) {
390 GRect r;
391 Color fg = g->state==gs_disabled?g->box->disabled_foreground:
392 g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
393 g->box->main_foreground;
394 r.x = x+1; r.y = g->inner.y+1;
395 r.width = cbbl-2;
396 if ( img!=NULL )
397 r.height = GImageGetScaledHeight(pixmap,img)-2;
398 else
399 r.height = gb->fh-2;
400 if ( ((GColorButton *) gb)->col!=COLOR_TRANSPARENT )
401 GDrawFillRect(pixmap,&r,((GColorButton *) gb)->col);
402 GDrawDrawRect(pixmap,&r,fg);
403 x += cbbl + spacing;
404 }
405 if ( gb->image_precedes && img!=NULL ) {
406 GDrawDrawScaledImage(pixmap,img,x,g->inner.y + off);
407 x += GImageGetScaledWidth(pixmap,img) + spacing;
408 }
409 if ( gb->label!=NULL ) {
410 Color fg = g->state==gs_disabled?g->box->disabled_foreground:
411 g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
412 g->box->main_foreground;
413 if ( lcnt==1 ) {
414 _ggadget_underlineMnemonic(pixmap,x,g->inner.y + gb->as + yoff + off,gb->label,
415 g->mnemonic,fg,g->inner.y+g->inner.height);
416 x += GDrawDrawText(pixmap,x,g->inner.y + gb->as + yoff + off,gb->label,-1,fg);
417 } else {
418 int y = g->inner.y + gb->as + yoff + off;
419 for ( pt = gb->label; ; ) {
420 for ( start=pt; *pt!='\0' && *pt!='\n'; ++pt );
421 if ( pt!=start )
422 GDrawDrawText(pixmap,x,y,start,pt-start,fg);
423 if ( *pt=='\0' )
424 break;
425 ++pt;
426 y+=gb->fh;
427 }
428 x += maxtextwidth;
429 }
430 x += spacing;
431 }
432 if ( !gb->image_precedes && img!=NULL )
433 GDrawDrawScaledImage(pixmap,img,x,g->inner.y + off);
434
435 if ( g->box->border_type!=bt_none ||
436 (g->box->flags&(box_foreground_border_inner|box_foreground_border_outer|box_active_border_inner))!=0 )
437 GDrawPopClip(pixmap,&old2);
438
439 if ( gb->labeltype==2 ) {
440 int bp = GBoxBorderWidth(g->base,g->box);
441 GListMarkDraw(pixmap,
442 g->r.x + g->r.width - marklen - spacing/2 - bp,
443 g->inner.y,
444 g->inner.height,
445 g->state);
446 }
447
448 GDrawPopClip(pixmap,&old1);
449 return( true );
450 }
451
452 static int gbutton_mouse(GGadget *g, GEvent *event) {
453 GLabel *gb = (GLabel *) g;
454 int within = gb->within, pressed = gb->pressed;
455 int was_state;
456
457 if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused )) {
458 if ( !g->takes_input && event->type == et_mousemove && !gb->pressed &&
459 g->popup_msg )
460 GGadgetPreparePopup(g->base,g->popup_msg);
461 return( false );
462 }
463
464 if ( gb->labeltype==2 && event->type==et_mousedown &&
465 ((GListButton *) gb)->popup!=NULL ) {
466 GDrawDestroyWindow(((GListButton *) gb)->popup);
467 ((GListButton *) gb)->popup = NULL;
468 return( true );
469 }
470 was_state = g->state;
471 if ( event->type == et_crossing ) {
472 if ( gb->within && !event->u.crossing.entered )
473 gb->within = false;
474 if ( gb->pressed && (event->u.crossing.state&ksm_buttons)==0 )
475 gb->pressed = false;
476 } else if ( gb->pressed && event->type==et_mouseup ) {
477 if ( GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) {
478 if ( event->type == et_mouseup ) {
479 gb->pressed = false;
480 GButtonInvoked(gb,event);
481 } else
482 gb->within = false;
483 } else
484 gb->pressed = false;
485 } else if ( event->type == et_mousedown &&
486 GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) {
487 if (event->u.mouse.button == 1) {
488 gb->pressed = true;
489 gb->within = true;
490 GButtonPressed(gb);
491 }
492 } else if ( event->type == et_mousemove &&
493 GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) {
494 gb->within = true;
495 if ( !gb->pressed && g->popup_msg )
496 GGadgetPreparePopup(g->base,g->popup_msg);
497 } else if ( event->type == et_mousemove && gb->within ) {
498 gb->within = false;
499 } else {
500 return( false );
501 }
502 if ( within != gb->within && was_state==g->state )
503 g->state = gb->within? gs_active : gs_enabled;
504 if ( within != gb->within || pressed != gb->pressed )
505 _ggadget_redraw(g);
506 return( event->type==et_mousedown || event->type==et_mouseup || gb->within );
507 }
508
509 static int gbutton_key(GGadget *g, GEvent *event) {
510 GLabel *gb = (GLabel *) g;
511
512 if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active ))
513 return(false);
514 if ( gb->labeltype==2 && ((GListButton *) gb)->popup!=NULL ) {
515 GWindow popup = ((GListButton *) gb)->popup;
516 (GDrawGetEH(popup))(popup,event);
517 return( true );
518 }
519
520 if ( event->u.chr.chars[0]==' ' ) {
521 GButtonInvoked(gb,NULL);
522 return( true );
523 }
524 return(false);
525 }
526
527 static int gbutton_focus(GGadget *g, GEvent *event) {
528 GLabel *gb = (GLabel *) g;
529
530 if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active ))
531 return(false);
532
533 if ( event->u.focus.mnemonic_focus==mf_shortcut ||
534 event->u.focus.mnemonic_focus==mf_mnemonic ) {
535 GButtonInvoked(gb,NULL);
536 return( true );
537 }
538 return( true );
539 }
540
541 static void gbutton_destroy(GGadget *g) {
542 GButton *b = (GButton *) g;
543
544 if ( b==NULL )
545 return;
546 if ( b->labeltype==2 ) {
547 GListButton *glb = (GListButton *) g;
548 if ( glb->popup ) {
549 /* Must cleanup the popup before we die */
550 /* We do this instead of GDrawDestroyWindow because this method is synchronous */
551 GEvent die;
552 die.type = et_close;
553 die.w = glb->popup;
554 GDrawPostEvent(&die);
555 }
556 GTextInfoArrayFree(glb->ti);
557 }
558 free(b->label);
559 _ggadget_destroy(g);
560 }
561
562 static int GButtonGetDesiredWidth(GLabel *gl);
563 static void GButtonSetInner(GButton *b) {
564 int width, mark = 0;
565 int bp = GBoxBorderWidth(b->g.base,b->g.box);
566
567 if ( b->labeltype==2 )
568 mark = GDrawPointsToPixels(b->g.base,_GListMarkSize) +
569 GDrawPointsToPixels(b->g.base,_GGadget_TextImageSkip);
570 width = GButtonGetDesiredWidth(b);
571 if ( width<=b->g.r.width-2*bp-mark )
572 b->g.inner.width = width;
573 else
574 b->g.inner.width = b->g.r.width-2*bp;
575 if ( !b->g.takes_input )
576 b->g.inner.x = b->g.r.x + bp;
577 else
578 b->g.inner.x = b->g.r.x + (b->g.r.width-b->g.inner.width-mark)/2;
579 }
580
581 static void GButtonSetTitle(GGadget *g,const unichar_t *tit) {
582 GButton *b = (GButton *) g;
583
584 if ( b->g.free_box )
585 free( b->g.box );
586 free(b->label);
587 b->label = u_copy(tit);
588 GButtonSetInner(b);
589 _ggadget_redraw(g);
590 }
591
592 static void GButtonSetImageTitle(GGadget *g,GImage *img,const unichar_t *tit, int before) {
593 GButton *b = (GButton *) g;
594
595 if ( b->g.free_box )
596 free( b->g.box );
597 free(b->label);
598 b->label = u_copy(tit);
599 b->image = img;
600 b->image_precedes = before;
601
602 GButtonSetInner(b);
603
604 _ggadget_redraw(g);
605 }
606
607 static const unichar_t *_GButtonGetTitle(GGadget *g) {
608 GButton *b = (GButton *) g;
609 return( b->label );
610 }
611
612 static GImage *GButtonGetImage(GGadget *g) {
613 GButton *b = (GButton *) g;
614 return( b->image );
615 }
616
617 static void GButtonSetFont(GGadget *g,FontInstance *new) {
618 GButton *b = (GButton *) g;
619 b->font = new;
620 }
621
622 static FontInstance *GButtonGetFont(GGadget *g) {
623 GButton *b = (GButton *) g;
624 return( b->font );
625 }
626
627 static void GListBSelectOne(GGadget *g, int32_t pos) {
628 GListButton *gl = (GListButton *) g;
629 int i;
630
631 for ( i=0; i<gl->ltot; ++i )
632 gl->ti[i]->selected = false;
633 if ( pos>=gl->ltot ) pos = gl->ltot-1;
634 if ( pos<0 ) pos = 0;
635 if ( gl->ltot>0 ) {
636 gl->ti[pos]->selected = true;
637 GButtonSetImageTitle(g,gl->ti[pos]->image,gl->ti[pos]->text,gl->ti[pos]->image_precedes);
638 }
639 }
640
641 static int32_t GListBIsSelected(GGadget *g, int32_t pos) {
642 GListButton *gl = (GListButton *) g;
643
644 if ( pos>=gl->ltot )
645 return( false );
646 if ( pos<0 )
647 return( false );
648 if ( gl->ltot>0 )
649 return( gl->ti[pos]->selected );
650
651 return( false );
652 }
653
654 static int32_t GListBGetFirst(GGadget *g) {
655 int i;
656 GListButton *gl = (GListButton *) g;
657
658 for ( i=0; i<gl->ltot; ++i )
659 if ( gl->ti[i]->selected )
660 return( i );
661
662 return( -1 );
663 }
664
665 static GTextInfo **GListBGet(GGadget *g,int32_t *len) {
666 GListButton *gl = (GListButton *) g;
667 if ( len!=NULL ) *len = gl->ltot;
668 return( gl->ti );
669 }
670
671 static GTextInfo *GListBGetItem(GGadget *g,int32_t pos) {
672 GListButton *gl = (GListButton *) g;
673 if ( pos<0 || pos>=gl->ltot )
674 return( NULL );
675
676 return(gl->ti[pos]);
677 }
678
679 static void GListButSet(GGadget *g,GTextInfo **ti,int32_t docopy) {
680 GListButton *gl = (GListButton *) g;
681 int i;
682
683 GTextInfoArrayFree(gl->ti);
684 if ( docopy || ti==NULL )
685 ti = GTextInfoArrayCopy(ti);
686 gl->ti = ti;
687 gl->ltot = GTextInfoArrayCount(ti);
688 for ( i=0; ti[i]->text!=NULL || ti[i]->line; ++i ) {
689 if ( ti[i]->selected && ti[i]->text!=NULL ) {
690 GGadgetSetTitle(g,ti[i]->text);
691 break;
692 }
693 }
694 }
695
696 static void GListButClear(GGadget *g) {
697 GListButSet(g,NULL,true);
698 }
699
700 static int GButtonIsDefault(GGadget *g) {
701 GLabel *gl = (GLabel *) g;
702 return( gl->is_default );
703 }
704
705 static int GButtonGetDesiredWidth(GLabel *gl) {
706 int iwidth=0, width=0;
707 if ( gl->image!=NULL ) {
708 iwidth = GImageGetScaledWidth(gl->g.base,gl->image);
709 }
710 if ( gl->label!=NULL ) {
711 int lcnt;
712 gbutton_textsize(gl,&lcnt,&width);
713 }
714
715 if ( width!=0 && iwidth!=0 )
716 width += GDrawPointsToPixels(gl->g.base,_GGadget_TextImageSkip);
717 width += iwidth;
718
719 if ( gl->labeltype==3 )
720 width += GDrawPointsToPixels(gl->g.base,_GGadget_TextImageSkip) +
721 GDrawPointsToPixels(gl->g.base,COLOR_BUTTON_BOX_LEN);
722 return( width );
723 }
724
725 static void GButtonGetDesiredSize(GGadget *g, GRect *outer, GRect *inner) {
726 GLabel *gl = (GLabel *) g;
727 int iwidth=0, iheight=0;
728 GTextBounds bounds;
729 int as=0, ds, ld, fh=0, width=0;
730 GRect needed;
731 int i;
732 int bp = GBoxBorderWidth(g->base,g->box);
733
734 if ( gl->image!=NULL ) {
735 iwidth = GImageGetScaledWidth(gl->g.base,gl->image);
736 iheight = GImageGetScaledHeight(gl->g.base,gl->image);
737 }
738 GDrawWindowFontMetrics(g->base,gl->font,&as, &ds, &ld);
739 if ( gl->label!=NULL ) {
740 int lcnt;
741 unichar_t *ltxt = NULL;
742 ltxt = gbutton_textsize(gl,&lcnt,&width);
743 if ( lcnt==1 ) {
744 FontInstance *old = GDrawSetFont(gl->g.base,gl->font);
745 width = GDrawGetTextBounds(gl->g.base,ltxt, -1, &bounds);
746 GDrawSetFont(gl->g.base,old);
747 if ( as<bounds.as ) as = bounds.as;
748 if ( ds<bounds.ds ) ds = bounds.ds;
749 fh = as+ds;
750 } else
751 fh = gl->fh*lcnt;
752 } else
753 fh = as+ds;
754
755 if ( gl->labeltype==3 ) {
756 if ( width!=0 )
757 width += GDrawPointsToPixels(NULL,COLOR_BUTTON_BOX_LEN) +
758 GDrawPointsToPixels(gl->g.base,_GGadget_TextImageSkip);
759 else
760 width = GDrawPointsToPixels(NULL,COLOR_BUTTON_BOX_LEN);
761 }
762
763 if ( width!=0 && iwidth!=0 )
764 width += GDrawPointsToPixels(gl->g.base,_GGadget_TextImageSkip);
765 width += iwidth;
766 if ( iheight<fh )
767 iheight = fh;
768
769 if ( gl->labeltype==2 ) {
770 GListButton *glb = (GListButton *) gl;
771 int temp;
772 int extra = GDrawPointsToPixels(gl->g.base,_GListMarkSize) +
773 2*GDrawPointsToPixels(gl->g.base,_GGadget_TextImageSkip) +
774 GBoxBorderWidth(gl->g.base,&_GListMark_Box);
775 for ( i=0; i<glb->ltot; ++i ) {
776 temp = GTextInfoGetWidth(gl->g.base,glb->ti[i],gl->font) + extra;
777 if ( temp>width ) width = temp;
778 temp = GTextInfoGetHeight(gl->g.base,glb->ti[i],gl->font);
779 if ( temp>iheight )
780 iheight = temp;
781 }
782 width += GDrawPointsToPixels(gl->g.base,_GGadget_TextImageSkip) +
783 GDrawPointsToPixels(gl->g.base,_GListMarkSize);
784 }
785
786 if ( gl->shiftonpress ) {
787 ++width; ++iheight; /* one pixel for movement when button is pushed */
788 }
789
790 width += gl->g.takes_input?2*GDrawPointsToPixels(gl->g.base,2):0;
791 needed.x = needed.y = 0;
792 needed.width = width;
793 needed.height = iheight;
794 if ( g->desired_width>2*bp )
795 needed.width = g->desired_width-2*bp;
796 if ( g->desired_height>2*bp )
797 needed.height = g->desired_height-2*bp;
798 if ( inner!=NULL ) {
799 inner->x = inner->y = 0;
800 inner->width = width;
801 inner->height = iheight;
802 }
803
804 _ggadgetFigureSize(gl->g.base,gl->g.box,&needed,gl->is_default);
805
806 if ( outer!=NULL ) {
807 outer->x = outer->y = 0;
808 outer->width = needed.width;
809 outer->height = needed.height;
810 }
811 }
812
813 static void _gbutton_resize(GGadget *g, int32_t width, int32_t height ) {
814 GRect inner;
815 int bp = GBoxBorderWidth(g->base,g->box);
816
817 GButtonGetDesiredSize(g,NULL,&inner);
818 if ( inner.height<height-2*bp ) inner.height = height-2*bp;
819
820 g->inner.height = inner.height;
821 g->inner.y = g->r.y + (height-inner.height)/2;
822 g->r.width = width;
823 g->r.height = height;
824 GButtonSetInner((GButton *) g);
825 }
826
827 struct gfuncs gbutton_funcs = {
828 0,
829 sizeof(struct gfuncs),
830
831 gbutton_expose,
832 gbutton_mouse,
833 gbutton_key,
834 NULL,
835 gbutton_focus,
836 NULL,
837 NULL,
838
839 _ggadget_redraw,
840 _ggadget_move,
841 _gbutton_resize,
842 _ggadget_setvisible,
843 _ggadget_setenabled,
844 _ggadget_getsize,
845 _ggadget_getinnersize,
846
847 gbutton_destroy,
848
849 GButtonSetTitle,
850 _GButtonGetTitle,
851 NULL,
852 GButtonSetImageTitle,
853 GButtonGetImage,
854
855 GButtonSetFont,
856 GButtonGetFont,
857
858 NULL,
859 NULL,
860 NULL,
861 NULL,
862 NULL,
863 NULL,
864 NULL,
865 NULL,
866 NULL,
867 NULL,
868 NULL,
869
870 GButtonGetDesiredSize,
871 _ggadget_setDesiredSize,
872 NULL,
873 GButtonIsDefault
874 };
875
876 struct gfuncs glistbutton_funcs = {
877 0,
878 sizeof(struct gfuncs),
879
880 gbutton_expose,
881 gbutton_mouse,
882 gbutton_key,
883 NULL,
884 gbutton_focus,
885 NULL,
886 NULL,
887
888 _ggadget_redraw,
889 _ggadget_move,
890 _gbutton_resize,
891 _ggadget_setvisible,
892 _ggadget_setenabled,
893 _ggadget_getsize,
894 _ggadget_getinnersize,
895
896 gbutton_destroy,
897
898 GButtonSetTitle,
899 _GButtonGetTitle,
900 NULL,
901 GButtonSetImageTitle,
902 GButtonGetImage,
903
904 GButtonSetFont,
905 GButtonGetFont,
906
907 GListButClear,
908 GListButSet,
909 GListBGet,
910 GListBGetItem,
911 NULL,
912 GListBSelectOne,
913 GListBIsSelected,
914 GListBGetFirst,
915 NULL,
916 NULL,
917 NULL,
918
919 GButtonGetDesiredSize,
920 _ggadget_setDesiredSize, /* GTextField does this right. but this is good enough for now */
921 NULL,
922 NULL
923 };
924
925 void _GButton_SetDefault(GGadget *g,int32_t is_default) {
926 GButton *gb = (GButton *) g;
927 GRect maxr;
928 int scale = GDrawPointsToPixels(g->base,1);
929 int def_size = (g->box->flags & box_draw_default)?scale+GDrawPointsToPixels(g->base,2):0;
930
931 if ( gb->is_default == is_default )
932 return;
933 gb->is_default = is_default;
934 if ( def_size==0 )
935 return;
936 if ( is_default ) {
937 g->r.x -= def_size;
938 g->r.y -= def_size;
939 g->r.width += 2*def_size;
940 g->r.height += 2*def_size;
941 maxr = g->r;
942 } else {
943 maxr = g->r;
944 g->r.x += def_size;
945 g->r.y += def_size;
946 g->r.width -= 2*def_size;
947 g->r.height -= 2*def_size;
948 }
949 ++maxr.width; ++maxr.height;
950 GDrawRequestExpose(g->base, &maxr, false);
951 }
952
953 void _GButtonInit(void) {
954 GResEditDoInit(&glabel_ri);
955 GResEditDoInit(&gbutton_ri);
956 GResEditDoInit(&gdroplist_ri);
957 GResEditDoInit(&gdefault_ri);
958 GResEditDoInit(&gcancel_ri);
959 GResEditDoInit(&gcolor_ri);
960 }
961
962 static void GLabelFit(GLabel *gl) {
963 int as=0, ds, ld;
964 GRect outer,inner;
965
966 if ( gl->g.r.width == -1 ) {
967 gl->g.r.width = GDrawPointsToPixels(gl->g.base,GIntGetResource(_NUM_Buttonsize));
968 if ( gl->is_default )
969 gl->g.r.width += 6;
970 }
971
972 GDrawWindowFontMetrics(gl->g.base,gl->font,&as, &ds, &ld);
973 gl->as = as;
974 gl->fh = as+ds;
975
976 GButtonGetDesiredSize(&gl->g,&outer, &inner);
977 _ggadgetSetRects(&gl->g,&outer, &inner, 0, 0);
978 if ( gl->g.takes_input )
979 GButtonSetInner((GButton *) gl);
980 }
981
982 static GLabel *_GLabelCreate(GLabel *gl, struct gwindow *base, GGadgetData *gd,void *data, GBox *def) {
983 _GButtonInit();
984 gl->g.funcs = &gbutton_funcs;
985 _GGadget_Create(&gl->g,base,gd,data,def);
986
987 if (( gl->is_default = gd->flags&gg_but_default?1:0 ) )
988 _GWidget_SetDefaultButton(&gl->g);
989 if (( gl->is_cancel = gd->flags&gg_but_cancel?1:0 ))
990 _GWidget_SetCancelButton(&gl->g);
991 gl->font = def==&label_box ? label_font.fi : button_font.fi;
992 if ( gd->label!=NULL ) {
993 gl->image_precedes = gd->label->image_precedes;
994 if ( gd->label->font!=NULL )
995 gl->font = gd->label->font;
996 if ( gd->label->text_is_1byte )
997 gl->label = utf82u_mncopy((char *) gd->label->text,&gl->g.mnemonic);
998 else
999 gl->label = u_copy(gd->label->text);
1000 gl->image = gd->label->image;
1001 if ( gd->mnemonic )
1002 gl->g.mnemonic = gd->mnemonic;
1003 }
1004 gl->shiftonpress = shift_on_press;
1005 GLabelFit(gl);
1006 _GGadget_FinalPosition(&gl->g,base,gd);
1007
1008 if ( gd->flags & gg_group_end )
1009 _GGadgetCloseGroup(&gl->g);
1010 return( gl );
1011 }
1012
1013 GGadget *GLabelCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1014 GLabel *gl = calloc(1,sizeof(GListButton));
1015 int i;
1016
1017 if ( gd->u.list!=NULL ) {
1018 gl->ti = GTextInfoArrayFromList(gd->u.list,&gl->ltot);
1019 }
1020 if ( gd->label==NULL && gd->u.list!=NULL ) {
1021 for ( i=0; gd->u.list[i].text == NULL; ++i );
1022 if ( gd->u.list[i].text!=NULL )
1023 gd->label = &gd->u.list[i];
1024 }
1025 gl = _GLabelCreate(gl,base,gd,data,&label_box);
1026 return( &gl->g );
1027 }
1028
1029 GGadget *GButtonCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1030 GLabel *gl = _GLabelCreate(calloc(1,sizeof(GLabel)),base,gd,data,
1031 gd->flags & gg_but_default ? &_GGadget_defaultbutton_box :
1032 gd->flags & gg_but_cancel ? &_GGadget_cancelbutton_box :
1033 &_GGadget_button_box);
1034
1035 gl->g.takes_input = true; gl->g.takes_keyboard = true; gl->g.focusable = true;
1036 return( &gl->g );
1037 }
1038
1039 GGadget *GImageButtonCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1040 GImageButton *gl =
1041 (GImageButton *) _GLabelCreate(calloc(1,sizeof(GImageButton)),base,gd,data,&_GGadget_button_box);
1042
1043 gl->g.takes_input = true;
1044 gl->labeltype = 1;
1045 if ( gd->label!=NULL ) {
1046 gl->img_within = gd->label[1].image;
1047 gl->active = gd->label[2].image;
1048 gl->disabled = gd->label[3].image;
1049 }
1050 return( &gl->g );
1051 }
1052
1053 GGadget *GColorButtonCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1054 GColorButton *gl;
1055 static GTextInfo ti;
1056 Color col = gd->u.col;
1057
1058 if ( ti.image==NULL && ti.text==NULL ) {
1059 ti.image = GGadgetImageCache("colorwheel.png");
1060 if ( ti.image==NULL ) {
1061 ti.text = (unichar_t *) _("Color");
1062 ti.text_is_1byte = true;
1063 }
1064 }
1065 gd->label = &ti;
1066 gl = calloc(1,sizeof(GColorButton));
1067 gl->labeltype = 3;
1068 gl = (GColorButton *) _GLabelCreate((GLabel *) gl,base,gd,data,&_GGadget_colorbutton_box);
1069 gl->g.takes_input = true;
1070 gl->col = col;
1071 return( &gl->g );
1072 }
1073
1074 void GColorButtonSetColor(GGadget *g, Color col) {
1075 GColorButton *gb = (GColorButton *) g;
1076 gb->col = col;
1077 GGadgetRedraw(g);
1078 }
1079
1080 Color GColorButtonGetColor(GGadget *g) {
1081 return( ((GColorButton *) g)->col );
1082 }
1083
1084 static void GListButtonSelected(GGadget *g, int i) {
1085 GListButton *gl = (GListButton *) g;
1086 GEvent e;
1087
1088 gl->popup = NULL;
1089 _GWidget_ClearGrabGadget(&gl->g);
1090 if ( i<0 || i>=gl->ltot )
1091 return;
1092 free(gl->label); gl->label = u_copy(gl->ti[i]->text);
1093 gl->image = gl->ti[i]->image;
1094 gl->image_precedes = gl->ti[i]->image_precedes;
1095 GButtonSetInner((GButton *) gl);
1096 _ggadget_redraw(g);
1097
1098 e.type = et_controlevent;
1099 e.w = g->base;
1100 e.u.control.subtype = et_listselected;
1101 e.u.control.g = g;
1102 e.u.control.u.list.from_mouse = true;
1103 if ( gl->g.handle_controlevent != NULL )
1104 (gl->g.handle_controlevent)(&gl->g,&e);
1105 else
1106 GDrawPostEvent(&e);
1107 }
1108
1109 static void GListButtonDoPopup(GListButton *gl) {
1110 gl->within = false; gl->pressed = false;
1111 gl->popup = GListPopupCreate(&gl->g,GListButtonSelected,gl->ti);
1112 }
1113
1114 static int _GListAlphaCompare(const void *v1, const void *v2) {
1115 GTextInfo * const *pt1 = v1, * const *pt2 = v2;
1116 return( GTextInfoCompare(*pt1,*pt2));
1117 }
1118
1119 GGadget *GListButtonCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1120 GListButton *gl = calloc(1,sizeof(GListButton));
1121 int i;
1122
1123 gl->labeltype = 2;
1124 gl->g.takes_input = true;
1125 if ( gd->u.list!=NULL ) {
1126 gl->ti = GTextInfoArrayFromList(gd->u.list,&gl->ltot);
1127 if ( gd->flags & gg_list_alphabetic )
1128 qsort(gl->ti,gl->ltot,sizeof(GTextInfo *),_GListAlphaCompare);
1129 }
1130 if ( gd->label==NULL && gd->u.list!=NULL ) {
1131 /* find first selected item if there is one */
1132 for ( i=0; gd->u.list[i].text!=NULL || gd->u.list[i].line; ++i )
1133 if ( gd->u.list[i].selected )
1134 break;
1135 /* else first item with text */
1136 if ( gd->u.list[i].text==NULL && !gd->u.list[i].line ) {
1137 for ( i=0; gd->u.list[i].line; ++i );
1138 if ( gd->u.list[i].text==NULL && !gd->u.list[i].line )
1139 i = 0;
1140 }
1141 gd->label = &gd->u.list[i];
1142 }
1143 _GLabelCreate((GLabel *) gl,base,gd,data,&_GGadget_droplist_box);
1144 gl->g.funcs = &glistbutton_funcs;
1145 return( &gl->g );
1146 }