1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/*
* This file is subject to the terms of the GFX License. If a copy of
* the license was not distributed with this file, you can obtain one at:
*
* http://chibios-gfx.com/license.html
*/
/**
* @file src/gwin/button.c
* @brief GWIN sub-system button code.
*
* @defgroup Button Button
* @ingroup GWIN
*
* @{
*/
#include "gfx.h"
#if (GFX_USE_GWIN && GWIN_NEED_BUTTON) || defined(__DOXYGEN__)
/* Parameters for various shapes */
#define RND_CNR_SIZE 5 // Rounded corner size for rounded buttons
#define ARROWHEAD_DIVIDER 4 // A quarter of the height for the arrow head
#define ARROWBODY_DIVIDER 4 // A quarter of the width for the arrow body
#include <string.h>
#include "gwin/internal.h"
#define GWIN_BUTTON_DEFAULT_SHAPE GBTN_3D
static const GButtonDrawStyle GButtonDefaultStyleUp = {
HTML2COLOR(0x404040), // color_up_edge;
HTML2COLOR(0xE0E0E0), // color_up_fill;
HTML2COLOR(0x000000), // color_up_txt;
};
static const GButtonDrawStyle GButtonDefaultStyleDown = {
HTML2COLOR(0x404040), // color_dn_edge;
HTML2COLOR(0x808080), // color_dn_fill;
HTML2COLOR(0x404040), // color_dn_txt;
};
// Process an event callback
static void gwinButtonCallback(void *param, GEvent *pe) {
GSourceListener *psl;
#define gh ((GHandle)param)
#define gbw ((GButtonObject *)param)
#define gsh ((GSourceHandle)param)
#define pme ((GEventMouse *)pe)
#define pte ((GEventTouch *)pe)
#define pxe ((GEventToggle *)pe)
#define pbe ((GEventGWinButton *)pe)
// check if button is disabled
if (!gh->enabled)
return;
switch (pe->type) {
#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
case GEVENT_MOUSE:
case GEVENT_TOUCH:
// Ignore anything other than the primary mouse button going up or down
if (!((pme->current_buttons ^ pme->last_buttons) & GINPUT_MOUSE_BTN_LEFT))
return;
if (gbw->state == GBTN_UP) {
// Our button is UP: Test for button down over the button
if ((pme->current_buttons & GINPUT_MOUSE_BTN_LEFT)
&& pme->x >= gbw->gwin.x && pme->x < gbw->gwin.x + gbw->gwin.width
&& pme->y >= gbw->gwin.y && pme->y < gbw->gwin.y + gbw->gwin.height) {
gbw->state = GBTN_DOWN;
gwinButtonDraw((GHandle)param);
}
return;
}
// Our button is DOWN
// Skip more mouse downs
if ((pme->current_buttons & GINPUT_MOUSE_BTN_LEFT))
return;
// This must be a mouse up - set the button as UP
gbw->state = GBTN_UP;
gwinButtonDraw((GHandle)param);
#if GWIN_BUTTON_LAZY_RELEASE
break;
#else
// If the mouse up was over the button then create the event
if (pme->x >= gbw->gwin.x && pme->x < gbw->gwin.x + gbw->gwin.width
&& pme->y >= gbw->gwin.y && pme->y < gbw->gwin.y + gbw->gwin.height)
break;
return;
#endif
#endif
#if GFX_USE_GINPUT && GINPUT_NEED_TOGGLE
case GEVENT_TOGGLE:
// State has changed - update the button
gbw->state = pxe->on ? GBTN_DOWN : GBTN_UP;
gwinButtonDraw((GHandle)param);
// Trigger the event on button down (different than for mouse/touch)
if (gbw->state == GBTN_DOWN)
break;
return;
#endif
default:
return;
}
// Trigger a GWIN Button Event
psl = 0;
while ((psl = geventGetSourceListener(gsh, psl))) {
if (!(pe = geventGetEventBuffer(psl)))
continue;
pbe->type = GEVENT_GWIN_BUTTON;
pbe->button = gh;
geventSendEvent(psl);
}
#undef pbe
#undef pme
#undef pte
#undef pxe
#undef gsh
#undef gbw
#undef gh
}
GHandle gwinCreateButton(GButtonObject *gb, coord_t x, coord_t y, coord_t width, coord_t height, font_t font, GButtonType type) {
if (!(gb = (GButtonObject *)_gwinInit((GWindowObject *)gb, x, y, width, height, sizeof(GButtonObject))))
return 0;
gb->gwin.type = GW_BUTTON;
gb->fn = 0;
gb->param = 0;
gwinSetFont(&gb->gwin, font);
gwinSetButtonStyle(&gb->gwin, GWIN_BUTTON_DEFAULT_SHAPE, &GButtonDefaultStyleUp, &GButtonDefaultStyleDown);
gb->type = type;
gb->state = GBTN_UP;
gb->txt = "";
geventListenerInit(&gb->listener);
geventRegisterCallback(&gb->listener, gwinButtonCallback, gb);
// buttons are enabled by default
gb->gwin.enabled = TRUE;
return (GHandle)gb;
}
void gwinSetButtonStyle(GHandle gh, GButtonShape shape, const GButtonDrawStyle *pUp, const GButtonDrawStyle *pDown) {
#define gbw ((GButtonObject *)gh)
if (gh->type != GW_BUTTON)
return;
switch(shape) {
case GBTN_SQUARE: gbw->fn = gwinButtonDraw_Square; break;
#if GDISP_NEED_ARC
case GBTN_ROUNDED: gbw->fn = gwinButtonDraw_Rounded; break;
#endif
#if GDISP_NEED_ELLIPSE
case GBTN_ELLIPSE: gbw->fn = gwinButtonDraw_Ellipse; break;
#endif
#if GDISP_NEED_CONVEX_POLYGON
case GBTN_ARROW_UP: gbw->fn = gwinButtonDraw_ArrowUp; break;
case GBTN_ARROW_DOWN: gbw->fn = gwinButtonDraw_ArrowDown; break;
case GBTN_ARROW_LEFT: gbw->fn = gwinButtonDraw_ArrowLeft; break;
case GBTN_ARROW_RIGHT: gbw->fn = gwinButtonDraw_ArrowRight; break;
#endif
case GBTN_CUSTOM: if (gbw->fn) break; /* Fall Through */
case GBTN_3D: /* Fall through */
default: gbw->fn = gwinButtonDraw_3D; break;
}
if (pUp) {
gbw->up.color_edge = pUp->color_edge;
gbw->up.color_fill = pUp->color_fill;
gbw->up.color_txt = pUp->color_txt;
}
if (pDown) {
gbw->dn.color_edge = pDown->color_edge;
gbw->dn.color_fill = pDown->color_fill;
gbw->dn.color_txt = pDown->color_txt;
}
#undef gbw
}
void gwinSetButtonText(GHandle gh, const char *txt, bool_t useAlloc) {
#define gbw ((GButtonObject *)gh)
if (gh->type != GW_BUTTON)
return;
// Dispose of the old string
if ((gh->flags & GBTN_FLG_ALLOCTXT)) {
gh->flags &= ~GBTN_FLG_ALLOCTXT;
if (gbw->txt) {
gfxFree((void *)gbw->txt);
gbw->txt = "";
}
}
// Alloc the new text if required
if (txt && useAlloc) {
char *str;
if ((str = (char *)gfxAlloc(strlen(txt)+1))) {
gh->flags |= GBTN_FLG_ALLOCTXT;
strcpy(str, txt);
}
txt = (const char *)str;
}
gbw->txt = txt ? txt : "";
#undef gbw
}
void gwinButtonDraw(GHandle gh) {
#define gbw ((GButtonObject *)gh)
if (gh->type != GW_BUTTON)
return;
#if GDISP_NEED_CLIP
gdispSetClip(gh->x, gh->y, gh->width, gh->height);
#endif
gbw->fn(gh,
gbw->gwin.enabled,
gbw->state == GBTN_DOWN,
gh->font && gbw->txt ? gbw->txt : "",
gbw->state == GBTN_DOWN ? &gbw->dn : &gbw->up,
gbw->param);
#undef gbw
}
void gwinSetButtonCustom(GHandle gh, GButtonDrawFunction fn, void *param) {
#define gbw ((GButtonObject *)gh)
if (gh->type != GW_BUTTON)
return;
gbw->fn = fn ? fn : gwinButtonDraw_3D;
gbw->param = param;
#undef gbw
}
void gwinButtonSetEnabled(GHandle gh, bool_t enabled) {
if (gh->type != GW_BUTTON)
return;
gh->enabled = enabled;
}
void gwinButtonDraw_3D(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
gdispFillStringBox(gh->x, gh->y, gh->width-1, gh->height-1, txt, gh->font, pstyle->color_txt, pstyle->color_fill, justifyCenter);
gdispDrawLine(gh->x+gh->width-1, gh->y, gh->x+gh->width-1, gh->y+gh->height-1, pstyle->color_edge);
gdispDrawLine(gh->x, gh->y+gh->height-1, gh->x+gh->width-2, gh->y+gh->height-1, pstyle->color_edge);
}
void gwinButtonDraw_Square(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
gdispFillStringBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, txt, gh->font, pstyle->color_txt, pstyle->color_fill, justifyCenter);
gdispDrawBox(gh->x, gh->y, gh->width, gh->height, pstyle->color_edge);
}
#if GDISP_NEED_ARC
void gwinButtonDraw_Rounded(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
if (gh->width >= 2*RND_CNR_SIZE+10) {
gdispFillRoundedBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, RND_CNR_SIZE-1, pstyle->color_fill);
gdispDrawStringBox(gh->x+1, gh->y+RND_CNR_SIZE, gh->width-2, gh->height-(2*RND_CNR_SIZE), txt, gh->font, pstyle->color_txt, justifyCenter);
gdispDrawRoundedBox(gh->x, gh->y, gh->width, gh->height, RND_CNR_SIZE, pstyle->color_edge);
} else {
gdispFillStringBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, txt, gh->font, pstyle->color_txt, pstyle->color_fill, justifyCenter);
gdispDrawBox(gh->x, gh->y, gh->width, gh->height, pstyle->color_edge);
}
}
#endif
#if GDISP_NEED_ELLIPSE
void gwinButtonDraw_Ellipse(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
gdispFillEllipse(gh->x+1, gh->y+1, gh->width/2-1, gh->height/2-1, pstyle->color_fill);
gdispDrawStringBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, txt, gh->font, pstyle->color_txt, justifyCenter);
gdispDrawEllipse(gh->x, gh->y, gh->width/2, gh->height/2, pstyle->color_edge);
}
#endif
#if GDISP_NEED_CONVEX_POLYGON
void gwinButtonDraw_ArrowUp(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
point arw[7];
arw[0].x = gh->width/2; arw[0].y = 0;
arw[1].x = gh->width-1; arw[1].y = gh->height/ARROWHEAD_DIVIDER;
arw[2].x = (gh->width + gh->width/ARROWBODY_DIVIDER)/2; arw[2].y = gh->height/ARROWHEAD_DIVIDER;
arw[3].x = (gh->width + gh->width/ARROWBODY_DIVIDER)/2; arw[3].y = gh->height-1;
arw[4].x = (gh->width - gh->width/ARROWBODY_DIVIDER)/2; arw[4].y = gh->height-1;
arw[5].x = (gh->width - gh->width/ARROWBODY_DIVIDER)/2; arw[5].y = gh->height/ARROWHEAD_DIVIDER;
arw[6].x = 0; arw[6].y = gh->height/ARROWHEAD_DIVIDER;
gdispFillConvexPoly(gh->x, gh->y, arw, 7, pstyle->color_fill);
gdispDrawPoly(gh->x, gh->y, arw, 7, pstyle->color_edge);
gdispDrawStringBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, txt, gh->font, pstyle->color_txt, justifyCenter);
}
void gwinButtonDraw_ArrowDown(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
point arw[7];
arw[0].x = gh->width/2; arw[0].y = gh->height-1;
arw[1].x = gh->width-1; arw[1].y = gh->height-1-gh->height/ARROWHEAD_DIVIDER;
arw[2].x = (gh->width + gh->width/ARROWBODY_DIVIDER)/2; arw[2].y = gh->height-1-gh->height/ARROWHEAD_DIVIDER;
arw[3].x = (gh->width + gh->width/ARROWBODY_DIVIDER)/2; arw[3].y = 0;
arw[4].x = (gh->width - gh->width/ARROWBODY_DIVIDER)/2; arw[4].y = 0;
arw[5].x = (gh->width - gh->width/ARROWBODY_DIVIDER)/2; arw[5].y = gh->height-1-gh->height/ARROWHEAD_DIVIDER;
arw[6].x = 0; arw[6].y = gh->height-1-gh->height/ARROWHEAD_DIVIDER;
gdispFillConvexPoly(gh->x, gh->y, arw, 7, pstyle->color_fill);
gdispDrawPoly(gh->x, gh->y, arw, 7, pstyle->color_edge);
gdispDrawStringBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, txt, gh->font, pstyle->color_txt, justifyCenter);
}
void gwinButtonDraw_ArrowLeft(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
point arw[7];
arw[0].x = 0; arw[0].y = gh->height/2;
arw[1].x = gh->width/ARROWHEAD_DIVIDER; arw[1].y = 0;
arw[2].x = gh->width/ARROWHEAD_DIVIDER; arw[2].y = (gh->height - gh->height/ARROWBODY_DIVIDER)/2;
arw[3].x = gh->width-1; arw[3].y = (gh->height - gh->height/ARROWBODY_DIVIDER)/2;
arw[4].x = gh->width-1; arw[4].y = (gh->height + gh->height/ARROWBODY_DIVIDER)/2;
arw[5].x = gh->width/ARROWHEAD_DIVIDER; arw[5].y = (gh->height + gh->height/ARROWBODY_DIVIDER)/2;
arw[6].x = gh->width/ARROWHEAD_DIVIDER; arw[6].y = gh->height-1;
gdispFillConvexPoly(gh->x, gh->y, arw, 7, pstyle->color_fill);
gdispDrawPoly(gh->x, gh->y, arw, 7, pstyle->color_edge);
gdispDrawStringBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, txt, gh->font, pstyle->color_txt, justifyCenter);
}
void gwinButtonDraw_ArrowRight(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) enabled;
(void) isdown;
(void) param;
point arw[7];
arw[0].x = gh->width-1; arw[0].y = gh->height/2;
arw[1].x = gh->width-1-gh->width/ARROWHEAD_DIVIDER; arw[1].y = 0;
arw[2].x = gh->width-1-gh->width/ARROWHEAD_DIVIDER; arw[2].y = (gh->height - gh->height/ARROWBODY_DIVIDER)/2;
arw[3].x = 0; arw[3].y = (gh->height - gh->height/ARROWBODY_DIVIDER)/2;
arw[4].x = 0; arw[4].y = (gh->height + gh->height/ARROWBODY_DIVIDER)/2;
arw[5].x = gh->width-1-gh->width/ARROWHEAD_DIVIDER; arw[5].y = (gh->height + gh->height/ARROWBODY_DIVIDER)/2;
arw[6].x = gh->width-1-gh->width/ARROWHEAD_DIVIDER; arw[6].y = gh->height-1;
gdispFillConvexPoly(gh->x, gh->y, arw, 7, pstyle->color_fill);
gdispDrawPoly(gh->x, gh->y, arw, 7, pstyle->color_edge);
gdispDrawStringBox(gh->x+1, gh->y+1, gh->width-2, gh->height-2, txt, gh->font, pstyle->color_txt, justifyCenter);
}
#endif
#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
bool_t gwinAttachButtonMouse(GHandle gh, uint16_t instance) {
GSourceHandle gsh;
if (gh->type != GW_BUTTON || !(gsh = ginputGetMouse(instance)))
return FALSE;
return geventAttachSource(&((GButtonObject *)gh)->listener, gsh, GLISTEN_MOUSEMETA);
}
#endif
#if GFX_USE_GINPUT && GINPUT_NEED_TOGGLE
bool_t gwinAttachButtonToggle(GHandle gh, uint16_t instance) {
GSourceHandle gsh;
if (gh->type != GW_BUTTON || !(gsh = ginputGetToggle(instance)))
return FALSE;
return geventAttachSource(&((GButtonObject *)gh)->listener, gsh, GLISTEN_TOGGLE_OFF|GLISTEN_TOGGLE_ON);
}
#endif
#endif /* GFX_USE_GWIN && GWIN_NEED_BUTTON */
/** @} */
|