forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listview.c
338 lines (272 loc) · 7.65 KB
/
listview.c
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
#include <stdlib.h>
#include "listview.h"
#include "framebuffer.h"
#include "util.h"
#include "log.h"
#include "checkbox.h"
#include "multirom_ui.h"
#define MARK_W 10
#define MARK_H 50
#define PADDING 20
#define LINE_W 2
#define SCROLL_DIST 20
void listview_init_ui(listview *view)
{
int x = view->x + view->w - PADDING/2 - LINE_W/2;
fb_rect *scroll_line = fb_add_rect(x, view->y, LINE_W, view->h, GRAYISH);
list_add(scroll_line, &view->ui_items);
view->touch.id = -1;
view->touch.last_y = -1;
add_touch_handler(&listview_touch_handler, view);
}
void listview_destroy(listview *view)
{
rm_touch_handler(&listview_touch_handler, view);
listview_clear(view);
list_clear(&view->ui_items, &fb_remove_item);
fb_rm_rect(view->scroll_mark);
free(view);
}
listview_item *listview_add_item(listview *view, int id, void *data)
{
listview_item *it = malloc(sizeof(listview_item));
it->id = id;
it->data = data;
it->flags = 0;
list_add(it, &view->items);
return it;
}
void listview_clear(listview *view)
{
list_clear(&view->items, view->item_destroy);
view->selected = NULL;
}
void listview_update_ui(listview *view)
{
int y = 0;
int i, it_h, visible;
listview_item *it;
for(i = 0; view->items && view->items[i]; ++i)
{
it = view->items[i];
it_h = (*view->item_height)(it->data);
visible = (int)(view->pos <= y && y+it_h-view->pos <= view->h);
if(!visible && (it->flags & IT_VISIBLE))
(*view->item_hide)(it->data);
else if(visible)
(*view->item_draw)(view->x, view->y+y-view->pos, view->w - PADDING, it);
if(visible)
it->flags |= IT_VISIBLE;
else
it->flags &= ~(IT_VISIBLE);
y += it_h;
}
view->fullH = y;
listview_enable_scroll(view, (int)(y > view->h));
if(y > view->h)
listview_update_scroll_mark(view);
fb_draw();
}
void listview_enable_scroll(listview *view, int enable)
{
if(!((view->scroll_mark != NULL) ^ (enable)))
return;
if(enable)
{
int x = view->x + view->w - PADDING/2 - MARK_W/2;
view->scroll_mark = fb_add_rect(x, view->y, MARK_W, MARK_H, GRAYISH);
}
else
{
fb_rm_rect(view->scroll_mark);
view->scroll_mark = NULL;
}
}
void listview_update_scroll_mark(listview *view)
{
if(!view->scroll_mark)
return;
int pct = (view->pos*100)/(view->fullH-view->h);
int y = view->y + ((view->h - MARK_H)*pct)/100;
view->scroll_mark->head.y = y;
}
int listview_touch_handler(touch_event *ev, void *data)
{
listview *view = (listview*)data;
if(view->touch.id == -1 && (ev->changed & TCHNG_ADDED))
{
if (ev->x < view->x || ev->y < view->y ||
ev->x > view->x+view->w || ev->y > view->y+view->h)
return -1;
view->touch.id = ev->id;
view->touch.last_y = ev->y;
view->touch.start_y = ev->y;
view->touch.us_diff = 0;
view->touch.hover = listview_item_at(view, ev->y);
if(view->touch.hover)
{
view->touch.hover->flags |= IT_HOVER;
listview_update_ui(view);
}
listview_update_ui(view);
return 0;
}
if(view->touch.id != ev->id)
return -1;
if(ev->changed & TCHNG_POS)
{
view->touch.us_diff += ev->us_diff;
if(view->touch.us_diff >= 10000)
{
if(view->touch.hover && abs(ev->y - view->touch.start_y) > SCROLL_DIST)
{
view->touch.hover->flags &= ~(IT_HOVER);
view->touch.hover = NULL;
}
if(!view->touch.hover)
{
if(ev->x > view->x + view->w - PADDING*2 && ev->x < view->x + view->w)
listview_scroll_to(view, ((ev->y-view->y)*100)/(view->h));
else
listview_scroll_by(view, view->touch.last_y - ev->y);
}
view->touch.last_y = ev->y;
view->touch.us_diff = 0;
}
}
if(ev->changed & TCHNG_REMOVED)
{
if(view->touch.hover)
{
listview_select_item(view, view->touch.hover);
view->touch.hover->flags &= ~(IT_HOVER);
}
view->touch.id = -1;
listview_update_ui(view);
}
return 0;
}
void listview_select_item(listview *view, listview_item *it)
{
if(view->item_selected)
(*view->item_selected)(view->selected, it);
if(view->selected)
view->selected->flags &= ~(IT_SELECTED);
it->flags |= IT_SELECTED;
view->selected = it;
}
void listview_scroll_by(listview *view, int y)
{
if(!view->scroll_mark)
return;
view->pos += y;
if(view->pos < 0)
view->pos = 0;
else if(view->pos > (view->fullH - view->h))
view->pos = (view->fullH - view->h);
listview_update_ui(view);
}
void listview_scroll_to(listview *view, int pct)
{
if(!view->scroll_mark)
return;
view->pos = ((view->fullH - view->h)*pct)/100;
if(view->pos < 0)
view->pos = 0;
else if(view->pos > (view->fullH - view->h))
view->pos = (view->fullH - view->h);
listview_update_ui(view);
}
listview_item *listview_item_at(listview *view, int y_pos)
{
int y = -view->pos + view->y;
int i, it_h;
listview_item *it;
for(i = 0; view->items && view->items[i]; ++i)
{
it = view->items[i];
it_h = (*view->item_height)(it->data);
if(y < y_pos && y+it_h > y_pos)
return it;
y += it_h;
}
return NULL;
}
#define ROM_ITEM_H 100
typedef struct
{
char *text;
char *partition;
fb_text *text_it;
fb_text *part_it;
fb_rect *bottom_line;
fb_rect *hover_rect;
checkbox *box;
} rom_item_data;
void *rom_item_create(const char *text, const char *partition)
{
rom_item_data *data = malloc(sizeof(rom_item_data));
memset(data, 0, sizeof(rom_item_data));
data->text = strdup(text);
if(partition)
data->partition = strdup(partition);
return data;
}
void rom_item_draw(int x, int y, int w, listview_item *it)
{
rom_item_data *d = (rom_item_data*)it->data;
if(!d->text_it)
{
d->text_it = fb_add_text(x+100, 0, WHITE, SIZE_BIG, d->text);
d->bottom_line = fb_add_rect(x, 0, w, 1, 0xFF1B1B1B);
d->box = checkbox_create(0, 0, NULL);
if(d->partition)
d->part_it = fb_add_text(x+100, 0, GRAY, SIZE_SMALL, d->partition);
}
d->text_it->head.y = center_y(y, ROM_ITEM_H, SIZE_BIG);
d->bottom_line->head.y = y+ROM_ITEM_H-2;
if(d->part_it)
d->part_it->head.y = d->text_it->head.y + SIZE_BIG*16 + 2;
if(it->flags & IT_HOVER)
{
if(!d->hover_rect)
d->hover_rect = fb_add_rect(x, 0, w, rom_item_height(it->data), CLR_SECONDARY);
d->hover_rect->head.y = y;
}
else if(d->hover_rect)
{
fb_rm_rect(d->hover_rect);
d->hover_rect = NULL;
}
checkbox_set_pos(d->box, x+30, y + (ROM_ITEM_H/2 - 30/2));
checkbox_select(d->box, (it->flags & IT_SELECTED));
}
void rom_item_hide(void *data)
{
rom_item_data *d = (rom_item_data*)data;
if(!d->text_it)
return;
fb_rm_text(d->text_it);
fb_rm_text(d->part_it);
fb_rm_rect(d->bottom_line);
fb_rm_rect(d->hover_rect);
checkbox_destroy(d->box);
d->text_it = NULL;
d->part_it = NULL;
d->bottom_line = NULL;
d->hover_rect = NULL;
d->box = NULL;
}
int rom_item_height(void *data)
{
return ROM_ITEM_H;
}
void rom_item_destroy(listview_item *it)
{
rom_item_hide(it->data);
rom_item_data *d = (rom_item_data*)it->data;
free(d->text);
free(d->partition);
free(it->data);
free(it);
}