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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/*
* 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://ugfx.org/license.html
*/
/**
* @file include/gwin/list.h
* @brief GWIN list widget header file.
*
* @defgroup List List
* @ingroup GWIN
*
* @{
*/
#include "gfx.h"
#if GFX_USE_GWIN && GWIN_NEED_LIST
#include "gwin/class_gwin.h"
#include <string.h>
// user for the default drawing routine
#define BORDER 3 // the border from the the text to the frame
#define BORDER_SCROLL 20 // the border from the scroll buttons to the frame
#define ARROW 10 // arrow side length
#define widget(gh) ((GListObject *)gh)
#define GLIST_FLG_MULTISELECT (GWIN_FIRST_CONTROL_FLAG << 0)
#define GLIST_FLG_HASIMAGES (GWIN_FIRST_CONTROL_FLAG << 1)
#define GLIST_FLG_SELECTED (GWIN_FIRST_CONTROL_FLAG << 2)
typedef struct ListItem {
gfxQueueASyncItem q_item; // This must be the first member in the struct
uint16_t flags;
uint16_t param; // A parameter the user can specify himself
const char* text;
#if GWIN_LIST_IMAGES
gdispImage* pimg;
#endif
} ListItem;
// select the next item in the list
static int _selectDown(GWidgetObject *gw) {
#define gcw ((GListObject *)gw)
gfxQueueASyncItem *qi;
uint16_t i;
for (i = 0, qi = gfxQueueASyncPeek(&gcw->list_head); qi; qi = gfxQueueASyncNext(qi), i++) {
if (((ListItem*)qi)->flags & GLIST_FLG_SELECTED && i < gcw->cnt - 1) {
((ListItem*)qi)->flags &=~ GLIST_FLG_SELECTED;
qi = gfxQueueASyncNext(qi);
((ListItem*)qi)->flags |= GLIST_FLG_SELECTED;
break;
}
}
_gwidgetRedraw((GHandle)gw);
return ++i;
#undef gcw
}
// select the previous item in the list
static int _selectUp(GWidgetObject *gw) {
#define gcw ((GListObject *)gw)
gfxQueueASyncItem *qi;
gfxQueueASyncItem *qi2;
uint16_t i;
qi = gfxQueueASyncPeek(&gcw->list_head);
qi2 = qi;
for (i = 0; qi2; qi2 = gfxQueueASyncNext(qi), i++) {
if (((ListItem*)qi2)->flags & GLIST_FLG_SELECTED) {
((ListItem*)qi2)->flags &=~ GLIST_FLG_SELECTED;
((ListItem*)qi)->flags |= GLIST_FLG_SELECTED;
break;
}
qi = qi2;
}
_gwidgetRedraw((GHandle)gw);
return --i;
#undef gcw
}
static void sendListEvent(GWidgetObject *gw, int item) {
GSourceListener* psl;
GEvent* pe;
#define pse ((GEventGWinList *)pe)
// Trigger a GWIN list event
psl = 0;
while ((psl = geventGetSourceListener(GWIDGET_SOURCE, psl))) {
if (!(pe = geventGetEventBuffer(psl)))
continue;
pse->type = GEVENT_GWIN_LIST;
pse->list = (GHandle)gw;
pse->item = item;
geventSendEvent(psl);
}
#undef pse
}
static void gwinListDefaultDraw(GWidgetObject* gw, void* param) {
#define gcw ((GListObject *)gw)
(void)param;
uint16_t i, fheight;
const point upArrow[] = { {0, ARROW}, {ARROW, ARROW}, {ARROW/2, 0} };
const point downArrow[] = { {0, 0}, {ARROW, 0}, {ARROW/2, ARROW} };
const gfxQueueASyncItem* qi;
fheight = gdispGetFontMetric(gwinGetDefaultFont(), fontHeight);
// the list frame
gdispDrawBox(gw->g.x, gw->g.y, gw->g.width, gw->g.height, gw->pstyle->enabled.edge);
gdispDrawLine(gw->g.x + gw->g.width - BORDER_SCROLL, gw->g.y, gw->g.x + gw->g.width - BORDER_SCROLL, gw->g.y + gw->g.height, gw->pstyle->enabled.edge);
gdispDrawLine(gw->g.x + gw->g.width - BORDER_SCROLL, gw->g.y + BORDER_SCROLL, gw->g.x + gw->g.width, gw->g.y + BORDER_SCROLL, gw->pstyle->enabled.edge);
gdispDrawLine(gw->g.x + gw->g.width - BORDER_SCROLL, gw->g.y + gw->g.height - BORDER_SCROLL, gw->g.x + gw->g.width, gw->g.y + gw->g.height - BORDER_SCROLL, gw->pstyle->enabled.edge);
// the up-button
gdispFillConvexPoly(gw->g.x + gw->g.width - BORDER_SCROLL + ARROW/2, gw->g.y + BORDER_SCROLL - ARROW - ARROW/2, upArrow, 3, gw->pstyle->enabled.edge);
// the down-button
gdispFillConvexPoly(gw->g.x + gw->g.width - BORDER_SCROLL + ARROW/2, gw->g.y + gw->g.height - BORDER_SCROLL + ARROW/2, downArrow, 3, gw->pstyle->enabled.edge);
for (qi = gfxQueueASyncPeek(&gcw->list_head), i = 0; qi; qi = gfxQueueASyncNext(qi), i += fheight + 2*BORDER) {
if (((ListItem*)qi)->flags & GLIST_FLG_SELECTED) {
gdispFillStringBox(gw->g.x + BORDER, gw->g.y + BORDER + i, gw->g.width - 2*BORDER - BORDER_SCROLL, fheight, ((ListItem*)qi)->text, gwinGetDefaultFont(), gw->pstyle->background, gw->pstyle->enabled.text, justifyLeft);
} else {
gdispFillStringBox(gw->g.x + BORDER, gw->g.y + BORDER + i, gw->g.width - 2*BORDER - BORDER_SCROLL, fheight, ((ListItem*)qi)->text, gwinGetDefaultFont(), gw->pstyle->enabled.text, gw->pstyle->background, justifyLeft);
}
}
#undef gcw
}
#if GINPUT_NEED_MOUSE
// a mouse down has occured over the list area
static void MouseDown(GWidgetObject* gw, coord_t x, coord_t y) {
#define gcw ((GListObject *)gw)
#define li ((ListItem *)qi)
(void) x;
uint16_t i, item_id, item_height;
const gfxQueueASyncItem* qi;
item_id = -1;
if (x > gw->g.width - BORDER_SCROLL) {
if (y < BORDER_SCROLL)
item_id = _selectUp(gw);
else if (y > gw->g.height - BORDER_SCROLL)
item_id = _selectDown(gw);
} else if (x < gw->g.width - BORDER_SCROLL - BORDER) {
item_height = gdispGetFontMetric(gwinGetDefaultFont(), fontHeight) + 2*BORDER;
item_id = (y) / item_height;
for(qi = gfxQueueASyncPeek(&gcw->list_head), i = 0; qi; qi = gfxQueueASyncNext(qi), i++) {
if (item_id == i)
li->flags |= GLIST_FLG_SELECTED;
else
li->flags &=~ GLIST_FLG_SELECTED;
}
}
_gwidgetRedraw((GHandle)gw);
// do not generate an event if an empty section of the list has has been selected
if (item_id < 0 || item_id > gcw->cnt - 1)
return;
sendListEvent(gw, item_id);
#undef gcw
#undef li
}
#endif
#if GINPUT_NEED_TOGGLE
// a toggle-on has occurred
static void ToggleOn(GWidgetObject *gw, uint16_t role) {
#define gcw ((GListObject *)gw)
switch (role) {
// select up
case 0:
_selectUp(gw);
break;
// select down
case 1:
_selectDown(gw);
break;
}
#undef gcw
}
#endif
static void _destroy(GHandle gh) {
const gfxQueueASyncItem* qi;
while((qi = gfxQueueASyncGet(&((GListObject *)gh)->list_head)))
gfxFree((void *)qi);
_gwidgetDestroy(gh);
}
static const gwidgetVMT listVMT = {
{
"List", // The class name
sizeof(GListObject), // The object size
_destroy, // The destroy routine
_gwidgetRedraw, // The redraw routine
0, // The after-clear routine
},
gwinListDefaultDraw, // default drawing routine
#if GINPUT_NEED_MOUSE
{
MouseDown,
0,
0,
},
#endif
#if GINPUT_NEED_TOGGLE
{
2, // two toggle roles
ToggleAssin, // Assign toggles
ToggleGet, // get toggles
0,
ToggleOn, // process toggle on event
},
#endif
#if GINPUT_NEED_DIAL
{
0,
0,
0,
0,
},
#endif
};
GHandle gwinListCreate(GListObject* widget, GWidgetInit* pInit) {
if (!(widget = (GListObject *)_gwidgetCreate(&widget->w, pInit, &listVMT)))
return 0;
// initialize the item queue
gfxQueueASyncInit(&(widget->list_head));
widget->cnt = 0;
gwinSetVisible(&widget->w.g, pInit->g.show);
return (GHandle)widget;
}
int gwinListAddItem(GHandle gh, const char* item_name, bool_t useAlloc) {
size_t sz;
ListItem *newItem;
sz = useAlloc ? sizeof(ListItem)+strlen(item_name)+1 : sizeof(ListItem);
if (!(newItem = (ListItem *)gfxAlloc(sz)))
return -1;
if (useAlloc) {
strcpy((char *)(newItem+1), item_name);
item_name = (const char *)(newItem+1);
}
// the item is not selected when added
newItem->flags = 0;
newItem->param = 0;
newItem->text = item_name;
// select the item if it's the first in the list
if (widget(gh)->cnt == 0)
newItem->flags |= GLIST_FLG_SELECTED;
// add the new item to the list
gfxQueueASyncPut(&widget(gh)->list_head, &newItem->q_item);
// increment the total amount of entries in the list widget
widget(gh)->cnt++;
// return the position in the list (-1 because we start with index 0)
return widget(gh)->cnt-1;
}
char* gwinListItemGetText(GHandle gh, int item) {
const gfxQueueASyncItem* qi;
uint16_t i;
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return 0;
// watch out for an invalid item
if (item < 0 || item > (widget(gh)->cnt) - 1)
return 0;
qi = gfxQueueASyncPeek(&widget(gh)->list_head);
for (i = 0; i < item; i++) {
qi = gfxQueueASyncNext(qi);
}
return ((ListItem*)qi)->text;
}
int gwinListFindText(GHandle gh, const char* text) {
const gfxQueueASyncItem* qi;
uint16_t i;
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return -1;
// watch out for NULL pointers
if (!text)
return -1;
qi = gfxQueueASyncPeek(&widget(gh)->list_head);
for(qi = gfxQueueASyncPeek(&widget(gh)->list_head), i = 0; qi; qi = gfxQueueASyncNext(qi), i++) {
if (strcmp(((ListItem *)qi)->text, text) == 0)
return i;
}
return -1;
}
int gwinListGetSelected(GHandle gh) {
const gfxQueueASyncItem *qi;
int i;
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return -1;
for(qi = gfxQueueASyncPeek(&widget(gh)->list_head), i = 0; qi; qi = gfxQueueASyncNext(qi), i++) {
if (((ListItem*)qi)->flags & GLIST_FLG_SELECTED)
return i;
}
return -1;
}
void gwinListItemSetParam(GHandle gh, int item, uint16_t param) {
const gfxQueueASyncItem* qi;
uint16_t i;
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return;
// watch out for an invalid item
if (item < 0 || item > (widget(gh)->cnt) - 1)
return;
qi = gfxQueueASyncPeek(&widget(gh)->list_head);
for (i = 0; i < item; i++) {
((ListItem*)qi)->param = param;
}
return;
}
void gwinListDeleteAll(GHandle gh) {
const gfxQueueASyncItem* qi;
while((qi = gfxQueueASyncGet(&((GListObject *)gh)->list_head)))
gfxFree((void *)qi);
}
void gwinListItemDelete(GHandle gh, int item) {
const gfxQueueASyncItem* qi;
uint16_t i;
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return;
// watch out for an invalid item
if (item < 0 || item > (widget(gh)->cnt) - 1)
return;
qi = gfxQueueASyncPeek(&widget(gh)->list_head);
// get our item pointer
for (i = 0; i < item; i++) {
qi = gfxQueueASyncNext(qi);
}
gfxQueueASyncRemove(&widget(gh)->list_head, qi);
gfxFree((void*)qi);
}
uint16_t gwinListItemGetParam(GHandle gh, int item) {
const gfxQueueASyncItem* qi;
uint16_t i;
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return 0;
// watch out for an invalid item
if (item < 0 || item > (widget(gh)->cnt) - 1)
return 0;
qi = gfxQueueASyncPeek(&widget(gh)->list_head);
for (i = 0; i < item; i++) {
qi = gfxQueueASyncNext(qi);
}
return ((ListItem*)qi)->param;
}
bool_t gwinListItemIsSelected(GHandle gh, int item) {
const gfxQueueASyncItem* qi;
uint16_t i;
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return FALSE;
// watch out for an invalid item
if (item < 0 || item > (widget(gh)->cnt) - 1)
return FALSE;
qi = gfxQueueASyncPeek(&widget(gh)->list_head);
for (i = 0; i < item; i++) {
qi = gfxQueueASyncNext(qi);
}
if (((ListItem*)qi)->flags & GLIST_FLG_SELECTED)
return TRUE;
else
return FALSE;
}
int gwinListItemCount(GHandle gh) {
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&listVMT)
return 0;
return widget(gh)->cnt;
}
#endif // GFX_USE_GWIN && GWIN_NEED_LIST
/** @} */
|