Debian Codegraph @

gdraw/gradio.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 "ustring.h"
34
35 #include <math.h>
36
37 static GBox radio_box = GBOX_EMPTY; /* Don't initialize here */
38 static GBox radio_on_box = GBOX_EMPTY; /* Don't initialize here */
39 static GBox radio_off_box = GBOX_EMPTY; /* Don't initialize here */
40 static GBox checkbox_box = GBOX_EMPTY; /* Don't initialize here */
41 static GBox checkbox_on_box = GBOX_EMPTY; /* Don't initialize here */
42 static GBox checkbox_off_box = GBOX_EMPTY; /* Don't initialize here */
43 static GBox visibility_box = GBOX_EMPTY; /* Don't initialize here */
44 static GBox visibility_on_box = GBOX_EMPTY; /* Don't initialize here */
45 static GBox visibility_off_box = GBOX_EMPTY; /* Don't initialize here */
46 static GResImage radon = GRESIMAGE_INIT("radio_on.png");
47 static GResImage radoff = GRESIMAGE_INIT("radio_off.png");
48 static GResImage raddison = GRESIMAGE_INIT("radio_on_disabled.png");
49 static GResImage raddisoff = GRESIMAGE_INIT("radio_off_disabled.png");
50 static GResImage checkon = GRESIMAGE_INIT("check_on.png");
51 static GResImage checkoff = GRESIMAGE_INIT("check_off.png");
52 static GResImage checkdison = GRESIMAGE_INIT("check_on_disabled.png");
53 static GResImage checkdisoff = GRESIMAGE_INIT("check_off_disabled.png");
54 static GResImage visibilityon = GRESIMAGE_INIT("check_on.png");
55 static GResImage visibilityoff = GRESIMAGE_INIT("check_off.png");
56 static GResImage visibilitydison = GRESIMAGE_INIT("check_on_disabled.png");
57 static GResImage visibilitydisoff = GRESIMAGE_INIT("check_off_disabled.png");
58 static GResFont checkbox_font = GRESFONT_INIT("400 10pt " SANS_UI_FAMILIES);
59 static int gradio_inited = false;
60
61 GResInfo gradio_ri;
62 static GResInfo gradioon_ri, gradiooff_ri;
63 static GResInfo gcheckbox_ri, gcheckboxon_ri, gcheckboxoff_ri;
64 static GResInfo gvisibility_ri, gvisibilityon_ri, gvisibilityoff_ri;
65 static GTextInfo radio_lab[] = {
66 { (unichar_t *) "Disabled On", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
67 { (unichar_t *) "Disabled Off", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
68 { (unichar_t *) "Enabled" , NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
69 { (unichar_t *) "Enabled 2" , NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }
70 };
71 static GGadgetCreateData radio_gcd[] = {
72 { GRadioCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &radio_lab[0], { NULL }, gg_visible, NULL, NULL }, NULL, NULL },
73 { GRadioCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &radio_lab[1], { NULL }, gg_visible|gg_cb_on, NULL, NULL }, NULL, NULL },
74 { GRadioCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &radio_lab[2], { NULL }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL },
75 { GRadioCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &radio_lab[3], { NULL }, gg_visible|gg_enabled|gg_cb_on, NULL, NULL }, NULL, NULL }
76 };
77 static GGadgetCreateData *rarray[] = { GCD_Glue, &radio_gcd[0], GCD_Glue, &radio_gcd[1], GCD_Glue, &radio_gcd[2], GCD_Glue, &radio_gcd[3], GCD_Glue, NULL, NULL };
78 static GGadgetCreateData radiobox =
79 { GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) rarray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
80 GResInfo gradio_ri = {
81 &gradioon_ri, &ggadget_ri, &gradioon_ri, &gradiooff_ri,
82 &radio_box,
83 NULL,
84 &radiobox,
85 NULL,
86 N_("Radio Button"),
87 N_("Radio Button"),
88 "GRadio",
89 "Gdraw",
90 false,
91 false,
92 omf_border_type|omf_padding,
93 { bt_none, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
94 GBOX_EMPTY,
95 NULL,
96 NULL,
97 NULL
98 };
99 static struct resed gradioon_re[] = {
100 { N_("Image"), "Image", rt_image, &radon, N_("Image used instead of the Radio On Mark"), NULL, { 0 }, 0, 0 },
101 { N_("Disabled Image"), "DisabledImage", rt_image, &raddison, N_("Image used instead of the Radio On Mark (when the radio is disabled)"), NULL, { 0 }, 0, 0 },
102 RESED_EMPTY
103 };
104 static GResInfo gradioon_ri = {
105 &gradiooff_ri, &ggadget_ri,&gradiooff_ri, &gradio_ri,
106 &radio_on_box,
107 NULL,
108 &radiobox,
109 gradioon_re,
110 N_("Radio On Mark"),
111 N_("The mark showing a radio button is on (depressed, selected)"),
112 "GRadioOn",
113 "Gdraw",
114 false,
115 false,
116 omf_border_type|omf_border_shape|box_do_depressed_background,
117 { bt_raised, bs_diamond, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
118 GBOX_EMPTY,
119 NULL,
120 NULL,
121 NULL
122 };
123 static struct resed gradiooff_re[] = {
124 { N_("Image"), "Image", rt_image, &radoff, N_("Image used instead of the Radio Off Mark"), NULL, { 0 }, 0, 0 },
125 { N_("Disabled Image"), "DisabledImage", rt_image, &raddisoff, N_("Image used instead of the Radio Off Mark (when the radio is disabled)"), NULL, { 0 }, 0, 0 },
126 RESED_EMPTY
127 };
128 static GResInfo gradiooff_ri = {
129 &gcheckbox_ri, &ggadget_ri,&gradioon_ri, &gradio_ri,
130 &radio_off_box,
131 NULL,
132 &radiobox,
133 gradiooff_re,
134 N_("Radio Off Mark"),
135 N_("The mark showing a radio button is off (up, not selected)"),
136 "GRadioOff",
137 "Gdraw",
138 false,
139 false,
140 omf_border_type|omf_border_shape|box_do_depressed_background,
141 { bt_lowered, bs_diamond, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
142 GBOX_EMPTY,
143 NULL,
144 NULL,
145 NULL
146 };
147 static GTextInfo checkbox_lab[] = {
148 { (unichar_t *) "Disabled On", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
149 { (unichar_t *) "Disabled Off", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
150 { (unichar_t *) "Enabled" , NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }
151 };
152 static GGadgetCreateData checkbox_gcd[] = {
153 { GCheckBoxCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &checkbox_lab[0], { NULL }, gg_visible, NULL, NULL }, NULL, NULL },
154 { GCheckBoxCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &checkbox_lab[1], { NULL }, gg_visible|gg_cb_on, NULL, NULL }, NULL, NULL },
155 { GCheckBoxCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &checkbox_lab[1], { NULL }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }
156 };
157 static GGadgetCreateData *carray[] = { GCD_Glue, &checkbox_gcd[0], GCD_Glue, &checkbox_gcd[1], GCD_Glue, &checkbox_gcd[2], GCD_Glue, NULL, NULL };
158 static GGadgetCreateData checkboxbox =
159 { GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) carray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
160 static GResInfo gcheckbox_ri = {
161 &gcheckboxon_ri, &ggadget_ri,&gcheckboxon_ri, &gcheckboxoff_ri,
162 &checkbox_box,
163 &checkbox_font,
164 &checkboxbox,
165 NULL,
166 N_("Check Box"),
167 N_("Check Box"),
168 "GCheckBox",
169 "Gdraw",
170 false,
171 false,
172 omf_border_type|omf_padding,
173 { bt_none, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
174 GBOX_EMPTY,
175 NULL,
176 NULL,
177 NULL
178 };
179 static struct resed gcheckboxon_re[] = {
180 {N_("Image"), "Image", rt_image, &checkon, N_("Image used instead of the Radio On Mark"), NULL, { 0 }, 0, 0 },
181 {N_("Disabled Image"), "DisabledImage", rt_image, &checkdison, N_("Image used instead of the Radio On Mark (when the radio is disabled)"), NULL, { 0 }, 0, 0 },
182 RESED_EMPTY
183 };
184 static GResInfo gcheckboxon_ri = {
185 &gcheckboxoff_ri, &ggadget_ri,&gcheckboxoff_ri, &gcheckbox_ri,
186 &checkbox_on_box,
187 NULL,
188 &checkboxbox,
189 gcheckboxon_re,
190 N_("Check Box On Mark"),
191 N_("The mark showing a checkbox is on (depressed, selected)"),
192 "GCheckBoxOn",
193 "Gdraw",
194 false,
195 false,
196 omf_border_type|omf_border_shape|box_do_depressed_background,
197 { bt_raised, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
198 GBOX_EMPTY,
199 NULL,
200 NULL,
201 NULL
202
203 };
204 static struct resed gcheckboxoff_re[] = {
205 {N_("Image"), "Image", rt_image, &checkoff, N_("Image used instead of the Check Box Off Mark"), NULL, { 0 }, 0, 0 },
206 {N_("Disabled Image"), "DisabledImage", rt_image, &checkdisoff, N_("Image used instead of the Check Box Off Mark (when the checkbox is disabled)"), NULL, { 0 }, 0, 0 },
207 RESED_EMPTY
208 };
209 static GResInfo gcheckboxoff_ri = {
210 &gvisibility_ri, &ggadget_ri,&gcheckboxon_ri, &gcheckbox_ri,
211 &checkbox_off_box,
212 NULL,
213 &checkboxbox,
214 gcheckboxoff_re,
215 N_("Check Box Off Mark"),
216 N_("The mark showing a checkbox is off (up, not selected)"),
217 "GCheckBoxOff",
218 "Gdraw",
219 false,
220 false,
221 omf_border_type|omf_border_shape|box_do_depressed_background,
222 { bt_lowered, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
223 GBOX_EMPTY,
224 NULL,
225 NULL,
226 NULL
227 };
228 static GTextInfo visibility_lab[] = {
229 { (unichar_t *) "Disabled On", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
230 { (unichar_t *) "Disabled Off", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
231 { (unichar_t *) "Enabled" , NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }
232 };
233 static GGadgetCreateData visibility_gcd[] = {
234 { GVisibilityBoxCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &visibility_lab[0], { NULL }, gg_visible, NULL, NULL }, NULL, NULL },
235 { GVisibilityBoxCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &visibility_lab[1], { NULL }, gg_visible|gg_cb_on, NULL, NULL }, NULL, NULL },
236 { GVisibilityBoxCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &visibility_lab[2], { NULL }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }
237 };
238 static GGadgetCreateData *varray[] = { GCD_Glue, &visibility_gcd[0], GCD_Glue, &visibility_gcd[1], GCD_Glue, &visibility_gcd[2], GCD_Glue, NULL, NULL };
239 static GGadgetCreateData visibilitybox =
240 { GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) varray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL };
241 static GResInfo gvisibility_ri = {
242 &gvisibilityon_ri, &ggadget_ri,&gvisibilityon_ri, &gvisibilityoff_ri,
243 &visibility_box,
244 NULL,
245 &visibilitybox,
246 NULL,
247 N_("Visibility Box"),
248 N_("Visibility Box"),
249 "GVisibilityBox",
250 "Gdraw",
251 false,
252 false,
253 omf_border_type|omf_padding,
254 { bt_none, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
255 GBOX_EMPTY,
256 NULL,
257 NULL,
258 NULL
259 };
260 static struct resed gvisibilityon_re[] = {
261 {N_("Image"), "Image", rt_image, &visibilityon, N_("Image used instead of the Visibility Box On Mark"), NULL, { 0 }, 0, 0 },
262 {N_("Disabled Image"), "DisabledImage", rt_image, &visibilitydison, N_("Image used instead of the Visibility Box On Mark (when the mark is disabled)"), NULL, { 0 }, 0, 0 },
263 RESED_EMPTY
264 };
265 static GResInfo gvisibilityon_ri = {
266 &gvisibilityoff_ri, &ggadget_ri,&gvisibilityoff_ri,&gvisibility_ri,
267 &visibility_on_box,
268 NULL,
269 &visibilitybox,
270 gvisibilityon_re,
271 N_("Visibility Box On Mark"),
272 N_("The mark showing visibliity is On (up, not selected)"),
273 "GVisibilityBoxOn",
274 "Gdraw",
275 false,
276 false,
277 omf_border_type|omf_padding,
278 { bt_none, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
279 GBOX_EMPTY,
280 NULL,
281 NULL,
282 NULL
283 };
284 static struct resed gvisibilityoff_re[] = {
285 {N_("Image"), "Image", rt_image, &visibilityoff, N_("Image used instead of the Visibility Box Off Mark"), NULL, { 0 }, 0, 0 },
286 {N_("Disabled Image"), "DisabledImage", rt_image, &visibilitydisoff, N_("Image used instead of the Visibility Box Off Mark (when the mark is disabled)"), NULL, { 0 }, 0, 0 },
287 RESED_EMPTY
288 };
289 extern GResInfo gtextfield_ri;
290 static GResInfo gvisibilityoff_ri = {
291 &gtextfield_ri, &ggadget_ri,&gvisibilityon_ri,&gvisibility_ri,
292 &visibility_off_box,
293 NULL,
294 &visibilitybox,
295 gvisibilityoff_re,
296 N_("Visibility Box Off Mark"),
297 N_("The mark showing visibility is off"),
298 "GVisibilityBoxOff",
299 "Gdraw",
300 false,
301 false,
302 omf_border_type|omf_padding,
303 { bt_none, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
304 GBOX_EMPTY,
305 NULL,
306 NULL,
307 NULL
308 };
309
310 static void GRadioChanged(GRadio *gr) {
311 GEvent e;
312
313 if ( gr->isradio && gr->ison )
314 return; /* Do Nothing, it's already on */
315 else if ( gr->isradio ) {
316 GRadio *other;
317 for ( other=gr->post; other!=gr; other = other->post ) {
318 if ( other->ison ) {
319 other->ison = false;
320 _ggadget_redraw((GGadget *) other);
321 }
322 }
323 } else {
324 /* Checkboxes just default down */
325 ;
326 }
327 gr->ison = !gr->ison;
328 e.type = et_controlevent;
329 e.w = gr->g.base;
330 e.u.control.subtype = et_radiochanged;
331 e.u.control.g = &gr->g;
332 if ( gr->g.handle_controlevent != NULL )
333 (gr->g.handle_controlevent)(&gr->g,&e);
334 else
335 GDrawPostEvent(&e);
336 }
337
338 /* Return the number of lines in the label of a radio button. */
339 static int gradio_linecount(GRadio *gr) {
340 int lcnt;
341 unichar_t *pt;
342
343 lcnt = 0;
344 if ( gr->label!=NULL ) {
345 for ( pt = gr->label; ; ) {
346 for ( ; *pt!='\0' && *pt!='\n'; ++pt );
347 ++lcnt;
348 if ( *pt=='\0' )
349 break;
350 ++pt;
351 }
352 }
353 return( lcnt );
354 }
355
356 /* Called on expose events, this renders the button. */
357 static int gradio_expose(GWindow pixmap, GGadget *g, GEvent *event) {
358 GRadio *gr = (GRadio *) g;
359 int x;
360 GImage *img = gr->image; /* the optional image tied to the label */
361 GResImage *mark;
362 GRect old1, old2, old3;
363 int yoff = (g->inner.height-(gr->fh))/2;
364
365 if ( g->state == gs_invisible )
366 return( false );
367
368 /* First blank out the button area. */
369 GDrawPushClip(pixmap,&g->r,&old1);
370
371 GBoxDrawBackground(pixmap,&g->r,g->box,
372 g->state==gs_enabled? gs_pressedactive: g->state,false);
373 GBoxDrawBorder(pixmap,&g->r,g->box,g->state,false);
374
375 GDrawPushClip(pixmap,&gr->onoffrect,&old2);
376 GBoxDrawBackground(pixmap,&gr->onoffrect,gr->ison?gr->onbox:gr->offbox,
377 gs_pressedactive,false);
378 if (gr->ison && gr->onbox->border_type!=bt_none)
379 GBoxDrawBorder(pixmap,&gr->onoffrect,gr->onbox, gs_pressedactive,false);
380 else if (!gr->ison && gr->offbox->border_type!=bt_none)
381 GBoxDrawBorder(pixmap,&gr->onoffrect,gr->offbox,gs_pressedactive,false);
382
383 /* Next draw either the right image or draw in an on or off indicator. */
384 mark = NULL;
385 if ( g->state == gs_disabled )
386 mark = gr->ison ? gr->ondis : gr->offdis; /* note: ondis or offdis may be NULL! */
387 if ( mark==NULL || GResImageGetImage(mark)==NULL )
388 mark = gr->ison ? gr->on : gr->off; /* note: on or off may be NULL! */
389 if ( mark!=NULL && GResImageGetImage(mark)==NULL ) /* when there's a reference to a special image, but no actual image */
390 mark = NULL;
391 if ( mark!=NULL ) {
392 GDrawPushClip(pixmap,&gr->onoffinner,&old3);
393 GDrawDrawScaledImage(pixmap,GResImageGetImage(mark),
394 gr->onoffinner.x,gr->onoffinner.y);
395 GDrawPopClip(pixmap,&old3);
396 } else if ( gr->ison && gr->onbox == &checkbox_on_box ) {
397 /* for radio buttons where the on is a checkbox style, draw an X */
398 Color fg = g->state==gs_disabled?g->box->disabled_foreground:
399 g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
400 g->box->main_foreground;
401 int bp = GDrawPointsToPixels(pixmap,gr->onbox->border_width);
402 GDrawDrawLine(pixmap, gr->onoffrect.x+bp,gr->onoffrect.y+bp,
403 gr->onoffrect.x+gr->onoffrect.width-1-bp,gr->onoffrect.y+gr->onoffrect.height-1-bp,
404 fg);
405 GDrawDrawLine(pixmap, gr->onoffrect.x+gr->onoffrect.width-1-bp,gr->onoffrect.y+bp,
406 gr->onoffrect.x+bp,gr->onoffrect.y+gr->onoffrect.height-1-bp,
407 fg);
408 } else if ( gr->ison && gr->onbox == &visibility_on_box ) {
409 /* draw open white of eye */
410 GPoint pts[15];
411 Color fg;
412 double angle;
413 int c,i;
414 int bp = gr->onbox->border_type==bt_none ? 0 : GDrawPointsToPixels(pixmap,gr->onbox->border_width);
415 int x=gr->onoffrect.x+bp;
416 int y=gr->onoffrect.y+bp;
417 int w=gr->onoffrect.width -1-2*bp;
418 int h=gr->onoffrect.height-1-2*bp;
419 GRect rect;
420 for (c=0, i=0; c<7; c++) {
421 angle=(30+c/6.*120)*FF_PI/180;
422 pts[i].x=.5*w*cos(angle)+x+w/2;
423 pts[i].y=.5*h*sin(angle)+y+h/4;
424 ++i;
425 }
426 for (c=1; c<6; c++) {
427 angle=(180+30+c/6.*120)*FF_PI/180;
428 pts[i].x=.5*w*cos(angle)+x+w/2;
429 pts[i].y=.5*h*sin(angle)+y+h*3/4;
430 ++i;
431 }
432 pts[i].x=pts[0].x;
433 pts[i].y=pts[0].y;
434 ++i;
435 fg=0x00ffffff; /* white */
436 GDrawFillPoly(pixmap, pts, i, fg);
437 fg = g->state==gs_disabled?g->box->disabled_foreground:
438 g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
439 g->box->main_foreground;
440 GDrawDrawPoly(pixmap, pts, i, fg);
441
442 /* draw pupil */
443 rect.x=gr->onoffrect.x+bp+w*.3;
444 rect.y=gr->onoffrect.y+bp+h*.3;
445 rect.width =.4*w;
446 rect.height=.4*h;
447 fg=0; /* black */
448 GDrawFillElipse(pixmap, &rect, fg);
449
450 } else if ( (!gr->ison) && gr->onbox == &visibility_on_box ) {
451 /* draw closed eye */
452 GPoint pts[7];
453 double angle;
454 int bp = gr->onbox->border_type==bt_none ? 0 : GDrawPointsToPixels(pixmap,gr->onbox->border_width);
455 int x=gr->onoffrect.x+bp;
456 int y=gr->onoffrect.y+bp;
457 int w=gr->onoffrect.width -1-2*bp;
458 int h=gr->onoffrect.height-1-2*bp;
459 Color fg = g->state==gs_disabled?g->box->disabled_foreground:
460 g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
461 g->box->main_foreground;
462 for (int i = 0; i <= 6; i++) {
463 angle=(30+i/6.*120)*FF_PI/180;
464 pts[i].x=.5*w*cos(angle)+x+w/2;
465 pts[i].y=.5*h*sin(angle)+y+h/4;
466
467 /* draw lashes */
468 if (i>0 && i<6) GDrawDrawLine(pixmap, pts[i].x,pts[i].y, .75*w*cos(angle)+x+w/2, .75*h*sin(angle)+y+h/4, fg);
469 }
470 GDrawDrawPoly(pixmap, pts, sizeof(pts)/sizeof(pts[0]), fg);
471 }
472
473 GDrawPopClip(pixmap,&old2);
474 x = gr->onoffrect.x + gr->onoffrect.width + GDrawPointsToPixels(pixmap,4);
475
476 /* Finally write out the label if any. */
477 GDrawPushClip(pixmap,&g->inner,&old2);
478 if ( gr->font!=NULL )
479 GDrawSetFont(pixmap,gr->font);
480 if ( gr->image_precedes && img!=NULL ) {
481 GDrawDrawScaledImage(pixmap,img,x,g->inner.y);
482 x += GImageGetScaledWidth(pixmap,img) + GDrawPointsToPixels(pixmap,_GGadget_TextImageSkip);
483 }
484 if ( gr->label!=NULL ) {
485 Color fg = g->state==gs_disabled?g->box->disabled_foreground:
486 g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
487 g->box->main_foreground;
488 int lcnt = gradio_linecount(gr);
489 if ( lcnt>1 )
490 yoff = (g->inner.height-lcnt*gr->fh)/2;
491 _ggadget_underlineMnemonic(pixmap,x,g->inner.y + gr->as + yoff,gr->label,
492 g->mnemonic,fg,g->inner.y+g->inner.height);
493 x += GDrawDrawText(pixmap,x,g->inner.y + gr->as + yoff,gr->label,-1,fg);
494 x += GDrawPointsToPixels(pixmap,_GGadget_TextImageSkip);
495 }
496 if ( !gr->image_precedes && img!=NULL )
497 GDrawDrawScaledImage(pixmap,img,x,g->inner.y);
498
499 GDrawPopClip(pixmap,&old2);
500 GDrawPopClip(pixmap,&old1);
501 return( true );
502 }
503
504 static int gradio_mouse(GGadget *g, GEvent *event) {
505 GRadio *gr = (GRadio *) g;
506 int within = gr->within, pressed = gr->pressed;
507
508 if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused ))
509 return( false );
510
511 if ( event->type == et_crossing ) {
512 if ( gr->within && !event->u.crossing.entered )
513 gr->within = false;
514 } else if ( gr->pressed && event->type!=et_mousemove ) {
515 if ( event->type == et_mousedown ) /* They pressed 2 mouse buttons? */
516 gr->pressed = false;
517 else if ( GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) {
518 gr->pressed = false;
519 if ( !gr->isradio || !gr->ison )
520 GRadioChanged(gr);
521 } else if ( event->type == et_mouseup )
522 gr->pressed = false;
523 else
524 gr->within = true;
525 } else if ( event->type == et_mousedown &&
526 GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) {
527 gr->pressed = true;
528 gr->within = true;
529 } else if ( event->type == et_mousemove &&
530 GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) {
531 gr->within = true;
532 if ( !gr->pressed && g->popup_msg )
533 GGadgetPreparePopup(g->base,g->popup_msg);
534 } else if ( event->type == et_mousemove && gr->within ) {
535 gr->within = false;
536 } else {
537 return( false );
538 }
539 if ( within != gr->within )
540 g->state = gr->within? gs_active : gs_enabled;
541 if ( within != gr->within || pressed != gr->pressed )
542 _ggadget_redraw(g);
543 return( gr->within );
544 }
545
546 static int gradio_key(GGadget *g, GEvent *event) {
547 GRadio *gr = (GRadio *) g;
548
549 if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused ))
550 return(false);
551
552 if ( event->u.chr.keysym == GK_Return || event->u.chr.keysym == GK_Tab ||
553 event->u.chr.keysym == GK_BackTab || event->u.chr.keysym == GK_Escape )
554 return( false );
555
556 if (event->type == et_char && event->u.chr.chars[0]==' ' ) {
557 GRadioChanged(gr);
558 _ggadget_redraw(g);
559 return( true );
560 }
561 return( false );
562 }
563
564 static int gradio_focus(GGadget *g, GEvent *event) {
565 GRadio *gr = (GRadio *) g;
566
567 if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active ))
568 return(false);
569
570 if ( event->u.focus.mnemonic_focus==mf_shortcut ) {
571 GRadioChanged(gr);
572 }
573 return( true );
574 }
575
576 static void gradio_destroy(GGadget *g) {
577 GRadio *gr = (GRadio *) g;
578
579 if ( gr==NULL )
580 return;
581 if ( gr->isradio && gr->post!=gr ) {
582 GRadio *prev;
583 for ( prev=gr->post; prev->post!=gr; prev = prev->post );
584 prev->post = gr->post;
585 }
586 free(gr->label);
587 _ggadget_destroy(g);
588 }
589
590 static void GRadioSetTitle(GGadget *g,const unichar_t *tit) {
591 GRadio *b = (GRadio *) g;
592 free(b->label);
593 b->label = u_copy(tit);
594 }
595
596 static const unichar_t *_GRadioGetTitle(GGadget *g) {
597 GRadio *b = (GRadio *) g;
598 return( b->label );
599 }
600
601 static void GRadioSetImageTitle(GGadget *g,GImage *img,const unichar_t *tit, int before) {
602 GRadio *b = (GRadio *) g;
603 if ( b->g.free_box )
604 free( b->g.box );
605 free(b->label);
606 b->label = u_copy(tit);
607 b->image = img;
608 b->image_precedes = before;
609 _ggadget_redraw(g);
610 }
611
612 static GImage *GRadioGetImage(GGadget *g) {
613 GRadio *b = (GRadio *) g;
614 return( b->image );
615 }
616
617 static void GRadioSetFont(GGadget *g,FontInstance *new) {
618 GRadio *b = (GRadio *) g;
619 b->font = new;
620 }
621
622 static FontInstance *GRadioGetFont(GGadget *g) {
623 GRadio *b = (GRadio *) g;
624 return( b->font );
625 }
626
627 static void _gradio_move(GGadget *g, int32_t x, int32_t y ) {
628 GRadio *b = (GRadio *) g;
629 b->onoffrect.x = x+(b->onoffrect.x-g->r.x);
630 b->onoffinner.x = x+(b->onoffinner.x-g->r.x);
631 _ggadget_move(g,x,y);
632 b->onoffrect.y = g->r.y+(g->r.height-b->onoffrect.height)/2;
633 b->onoffinner.y = g->r.y+(g->r.height-b->onoffinner.height)/2;
634 }
635
636 static void GRadioGetDesiredSize(GGadget *g, GRect *outer, GRect *inner) {
637 GCheckBox *gl = (GCheckBox *) g;
638 int iwidth=0, iheight=0;
639 GTextBounds bounds;
640 int as=0, ds, ld, fh=0, width=0;
641
642 if ( gl->image!=NULL ) {
643 iwidth = GImageGetScaledWidth(gl->g.base,gl->image);
644 iheight = GImageGetScaledHeight(gl->g.base,gl->image);
645 }
646 GDrawWindowFontMetrics(g->base,gl->font,&as, &ds, &ld);
647 if ( gl->label!=NULL ) {
648 FontInstance *old = GDrawSetFont(gl->g.base,gl->font);
649 width = GDrawGetTextBounds(gl->g.base,gl->label, -1, &bounds);
650 GDrawSetFont(gl->g.base,old);
651 if ( as<bounds.as ) as = bounds.as;
652 if ( ds<bounds.ds ) ds = bounds.ds;
653 }
654 fh = as+ds;
655
656 if ( width!=0 && iwidth!=0 )
657 width += GDrawPointsToPixels(gl->g.base,_GGadget_TextImageSkip);
658 width += iwidth;
659 if ( iheight<fh )
660 iheight = fh;
661 if ( iheight < gl->onoffrect.height )
662 iheight = gl->onoffrect.height;
663 width += gl->onoffrect.width + GDrawPointsToPixels(gl->g.base,5);
664 if ( g->desired_width>0 ) width = g->desired_width;
665 if ( g->desired_height>0 ) iheight = g->desired_height;
666 if ( inner!=NULL ) {
667 inner->x = inner->y = 0;
668 inner->width = width;
669 inner->height = iheight;
670 }
671 if ( outer!=NULL ) {
672 outer->x = outer->y = 0;
673 outer->width = width;
674 outer->height = iheight;
675 /*_ggadgetFigureSize(gl->g.base,gl->g.box,outer,false);*/
676 }
677 }
678
679 void GVisibilityBoxSetToMinWH(GGadget *g)
680 {
681 GRect size;
682 GRect outer;
683 GRect inner;
684 GRadioGetDesiredSize(g, &outer, &inner );
685 outer.width = MIN(outer.width,outer.height);
686 outer.width -= 3;
687 size.width = outer.width;
688 size.height = outer.height;
689 GGadgetSetSize(g,&size);
690 }
691
692
693 struct gfuncs gradio_funcs = {
694 0,
695 sizeof(struct gfuncs),
696
697 gradio_expose,
698 gradio_mouse,
699 gradio_key,
700 NULL,
701 gradio_focus,
702 NULL,
703 NULL,
704
705 _ggadget_redraw,
706 _gradio_move,
707 _ggadget_resize,
708 _ggadget_setvisible,
709 _ggadget_setenabled,
710 _ggadget_getsize,
711 _ggadget_getinnersize,
712
713 gradio_destroy,
714
715 GRadioSetTitle,
716 _GRadioGetTitle,
717 NULL,
718 GRadioSetImageTitle,
719 GRadioGetImage,
720
721 GRadioSetFont,
722 GRadioGetFont,
723
724 NULL,
725 NULL,
726 NULL,
727 NULL,
728 NULL,
729 NULL,
730 NULL,
731 NULL,
732 NULL,
733 NULL,
734 NULL,
735
736 GRadioGetDesiredSize,
737 _ggadget_setDesiredSize,
738 NULL,
739 NULL
740 };
741
742 extern int GImageWriteGImage(GImage *gi, char *filename);
743 static void GRadioInit() {
744 if ( !gradio_inited ) {
745 GResEditDoInit(&gradio_ri);
746 GResEditDoInit(&gradioon_ri);
747 GResEditDoInit(&gradiooff_ri);
748 GResEditDoInit(&gcheckbox_ri);
749 GResEditDoInit(&gcheckboxon_ri);
750 GResEditDoInit(&gcheckboxoff_ri);
751 GResEditDoInit(&gvisibility_ri);
752 GResEditDoInit(&gvisibilityon_ri);
753 GResEditDoInit(&gvisibilityoff_ri);
754 }
755 if ( radio_on_box.depressed_background == radio_off_box.depressed_background ) {
756 radio_on_box.depressed_background = radio_on_box.active_border;
757 radio_off_box.depressed_background = radio_off_box.main_background;
758 }
759 if ( checkbox_on_box.depressed_background == checkbox_off_box.depressed_background ) {
760 checkbox_on_box.depressed_background = checkbox_on_box.active_border;
761 checkbox_off_box.depressed_background = checkbox_off_box.main_background;
762 }
763 gradio_inited = true;
764 }
765
766 static void GCheckBoxFit(GCheckBox *gl) {
767 int as=0, ds, ld;
768 GRect outer, inner, needed;
769 int iwidth, iheight;
770
771 needed.x = needed.y = 0;
772 needed.width = needed.height = 1;
773 if ( gl->on!=NULL && GResImageGetImage(gl->on)!=NULL ) {
774 if (( iwidth = GImageGetScaledWidth(gl->g.base,GResImageGetImage(gl->on)))>needed.width )
775 needed.width = iwidth;
776 if (( iheight = GImageGetScaledHeight(gl->g.base,GResImageGetImage(gl->on)))>needed.height )
777 needed.height = iheight;
778 }
779 if ( gl->off!=NULL && GResImageGetImage(gl->off)!=NULL ) {
780 if (( iwidth = GImageGetScaledWidth(gl->g.base,GResImageGetImage(gl->off)))>needed.width )
781 needed.width = iwidth;
782 if (( iheight = GImageGetScaledHeight(gl->g.base,GResImageGetImage(gl->off)))>needed.height )
783 needed.height = iheight;
784 }
785 gl->onoffinner = needed;
786 _ggadgetFigureSize(gl->g.base,gl->onbox,&needed,false);
787 gl->onoffrect = needed;
788
789 GDrawWindowFontMetrics(gl->g.base,gl->font,&as, &ds, &ld);
790 GRadioGetDesiredSize(&gl->g,&outer,&inner);
791 _ggadgetSetRects(&gl->g,&outer, &inner, -1, 0);
792
793 gl->as = as;
794 gl->fh = as+ds;
795
796 gl->onoffrect.x = gl->g.inner.x;
797 gl->onoffrect.y = (gl->as>gl->onoffrect.height)?gl->g.inner.y+gl->as-gl->onoffrect.height:
798 gl->g.inner.y+(gl->g.inner.height-gl->onoffrect.height)/2;
799 gl->onoffinner.x = gl->onoffrect.x + (gl->onoffrect.width-gl->onoffinner.width)/2;
800 gl->onoffinner.y = gl->onoffrect.y + (gl->onoffrect.height-gl->onoffinner.height)/2;
801 }
802
803 static GCheckBox *_GCheckBoxCreate(GCheckBox *gl, struct gwindow *base, GGadgetData *gd,void *data, GBox *def) {
804
805 GRadioInit();
806 gl->g.funcs = &gradio_funcs;
807 _GGadget_Create(&gl->g,base,gd,data,def);
808
809 gl->g.takes_input = true; gl->g.takes_keyboard = true; gl->g.focusable = true;
810 gl->font = checkbox_font.fi;
811 if ( gd->label!=NULL ) {
812 gl->image_precedes = gd->label->image_precedes;
813 if ( gd->label->font!=NULL )
814 gl->font = gd->label->font;
815 if ( gd->label->text_is_1byte )
816 gl->label = utf82u_mncopy((char *) gd->label->text,&gl->g.mnemonic);
817 else
818 gl->label = u_copy(gd->label->text);
819 gl->image = gd->label->image;
820 }
821 if ( gd->flags & gg_cb_on )
822 gl->ison = true;
823 if ( gl->isradio ) {
824 gl->onbox = &radio_on_box;
825 gl->offbox = &radio_off_box;
826 gl->on = &radon;
827 gl->off = &radoff;
828 gl->ondis = &raddison;
829 gl->offdis = &raddisoff;
830 } else {
831 gl->onbox = &checkbox_on_box;
832 gl->offbox = &checkbox_off_box;
833 gl->on = &checkon;
834 gl->off = &checkoff;
835 gl->ondis = &checkdison;
836 gl->offdis = &checkdisoff;
837 }
838 GCheckBoxFit(gl);
839 _GGadget_FinalPosition(&gl->g,base,gd);
840
841 if ( gd->flags & gg_group_end )
842 _GGadgetCloseGroup(&gl->g);
843 return( gl );
844 }
845
846 GGadget *GCheckBoxCreate(struct gwindow *base, GGadgetData *gd,void *data) {
847 GCheckBox *gl = _GCheckBoxCreate(calloc(1,sizeof(GCheckBox)),base,gd,data,&checkbox_box);
848
849 return( &gl->g );
850 }
851
852 GGadget *GVisibilityBoxCreate(struct gwindow *base, GGadgetData *gd,void *data) {
853 GCheckBox *gl = _GCheckBoxCreate(calloc(1,sizeof(GCheckBox)),base,gd,data,&visibility_box);
854 gl->onbox = &visibility_on_box;
855 gl->offbox = &visibility_off_box;
856 gl->on = &visibilityon;
857 gl->off = &visibilityoff;
858 gl->ondis = &visibilitydison;
859 gl->offdis = &visibilitydisoff;
860
861 return( &gl->g );
862 }
863
864 GGadget *GRadioCreate(struct gwindow *base, GGadgetData *gd,void *data) {
865 GRadio *gl = (GRadio *) calloc(1,sizeof(GRadio));
866 GGadget *gr;
867
868 gl->isradio = true;
869 gl->radiogroup = gd->u.radiogroup;
870 _GCheckBoxCreate((GCheckBox *) gl,base,gd,data,&radio_box);
871
872 gl->post = gl;
873 if ( gd->flags & gg_rad_startnew )
874 /* Done */;
875 else if ( gl->g.prev!=NULL && gl->radiogroup!=0 ) {
876 for ( gr=gl->g.prev; gr!=NULL; gr = gr->prev ) {
877 if ( gr->funcs==&gradio_funcs && ((GRadio *) gr)->radiogroup == gl->radiogroup ) {
878 gl->post = ((GRadio *) gr)->post;
879 ((GRadio *) gr)->post = gl;
880 break;
881 }
882 }
883 } else if ( gl->g.prev!=NULL && gl->g.prev->funcs==&gradio_funcs &&
884 ((GRadio *) (gl->g.prev))->isradio ) {
885 gl->post = ((GRadio *) (gl->g.prev))->post;
886 ((GRadio *) (gl->g.prev))->post = gl;
887 } else if ( gd->flags & gg_rad_continueold ) {
888 for ( gr=gl->g.prev; gr!=NULL && (gr->funcs!=&gradio_funcs ||
889 !((GRadio *) gr)->isradio); gr = gr->prev );
890 if ( gr!=NULL ) {
891 gl->post = ((GRadio *) gr)->post;
892 ((GRadio *) gr)->post = gl;
893 }
894 }
895
896 return( &gl->g );
897 }
898
899 void GGadgetSetChecked(GGadget *g, int ison) {
900 GRadio *gr = (GRadio *) g;
901
902 if ( gr->isradio ) {
903 if ( ison && !gr->ison ) {
904 GRadio *other;
905 for ( other=gr->post; other!=gr; other = other->post ) {
906 if ( other->ison ) {
907 other->ison = false;
908 _ggadget_redraw((GGadget *) other);
909 }
910 }
911 }
912 }
913 gr->ison = ison?1:0;
914 _ggadget_redraw(g);
915 }
916
917 int GGadgetIsChecked(GGadget *g) {
918 return( ((GRadio *) g)->ison );
919 }