-
Notifications
You must be signed in to change notification settings - Fork 149
/
dropdown.c
253 lines (215 loc) · 6.56 KB
/
dropdown.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
#include "main.h"
static DROPDOWN *active;
static int active_x, active_y, active_width, active_height;
#define index(b, i) (i == 0 ? b->selected : ((i > b->selected) ? i : i - 1))
// Draw background rectangles for a dropdown
void dropdown_drawactive(void)
{
DROPDOWN *b = active;
if(!b) {
return;
}
// load colors for this style
uint32_t color_bg,
color_border,
color_aoptbg,
color_aopttext,
color_text;
switch(b->style) {
case AUXILIARY_STYLE:
color_bg = COLOR_AUX_BACKGROUND;
color_border = COLOR_AUX_EDGE_ACTIVE;
color_aoptbg = COLOR_AUX_ACTIVEOPTION_BACKGROUND;
color_aopttext = COLOR_AUX_ACTIVEOPTION_TEXT;
color_text = COLOR_AUX_TEXT;
break;
default:
color_bg = COLOR_MAIN_BACKGROUND;
color_border = COLOR_EDGE_ACTIVE;
color_aoptbg = COLOR_ACTIVEOPTION_BACKGROUND;
color_aopttext = COLOR_ACTIVEOPTION_TEXT;
color_text = COLOR_MAIN_TEXT;
break;
}
int x = active_x, y = active_y, w = active_width, h = active_height;
int i, sign = 1;
// Increase width if needed, so that all menu items fit.
for(i = 0; i != b->dropcount; i++) {
STRING* e = b->ondisplay(i, b);
int needed_w = textwidth(e->str, e->length) + 4 * SCALE;
if(w < needed_w) {
w = needed_w;
}
}
if(y + h * b->dropcount > utox_window_height) {
y -= h * (b->dropcount - 1);
sign = -1;
}
drawrect(x, y, x + w, y + h * b->dropcount, color_bg);
framerect(x, y, x + w, y + h * b->dropcount, color_border);
if(sign == -1) {
y += h * (b->dropcount - 1);
}
for(i = 0; i != b->dropcount; i++) {
int j = index(b, i);
STRING* e = b->ondisplay(j, b);
if(j == b->over) {
drawrectw(x + 1, y + 1, w - 2, h - 2, color_aoptbg);
setcolor(color_aopttext);
} else {
setcolor(color_text);
}
setfont(FONT_TEXT);
drawtext(x + 2 * SCALE, y + 2 * SCALE, e->str, e->length);
y += sign * h;
}
}
// Draw collapsed dropdown
void dropdown_draw(DROPDOWN *b, int x, int y, int width, int height)
{
if(!b->open) {
// load colors for this style
uint32_t color_bg,
color_border,
color_border_h,
color_text;
switch(b->style) {
case AUXILIARY_STYLE:
color_bg = COLOR_AUX_BACKGROUND;
color_border = COLOR_AUX_EDGE_NORMAL;
color_border_h = COLOR_AUX_EDGE_HOVER;
color_text = COLOR_AUX_TEXT;
break;
default:
color_bg = COLOR_MAIN_BACKGROUND;
color_border = COLOR_EDGE_NORMAL;
color_border_h = COLOR_EDGE_HOVER;
color_text = COLOR_MAIN_TEXT;
break;
}
framerect(x, y, x + width, y + height, (b->mouseover ? color_border_h : color_border));
drawrect(x + 1, y + 1, x + width - 1, y + height - 1, color_bg);
if(b->dropcount) {
setfont(FONT_TEXT);
setcolor(color_text);
STRING* e = b->ondisplay(b->selected, b);
drawtextwidth(x + 2 * SCALE, width - 4 * SCALE, y + 2 * SCALE, e->str, e->length);
}
} else {
active_x = x;
active_y = y;
active_width = width;
active_height = height;
}
}
_Bool dropdown_mmove(DROPDOWN *b, int UNUSED(x), int y, int w, int h, int mx, int my, int UNUSED(dx), int UNUSED(dy))
{
if(b->open) {
int over = my / h;
if(y + h * b->dropcount > utox_window_height) {
over = my > 0 ? 0 : ((-my) / h + 1);
}
if(over < b->dropcount) {
over = index(b, over);
if(over != b->over) {
b->over = over;
return 1;
}
}
} else {
_Bool mouseover = inrect(mx, my, 0, 0, w, h);
if(mouseover != b->mouseover) {
b->mouseover = mouseover;
return 1;
}
}
return 0;
}
_Bool dropdown_mdown(DROPDOWN *b)
{
if(b->mouseover && b->dropcount) {
b->open = 1;
active = b;
return 1;
}
return 0;
}
_Bool dropdown_mright(DROPDOWN *UNUSED(b))
{
return 0;
}
_Bool dropdown_mwheel(DROPDOWN *UNUSED(b), int UNUSED(height), double UNUSED(d))
{
return 0;
}
_Bool dropdown_mup(DROPDOWN *b)
{
if(b->open) {
b->open = 0;
active = NULL;
if(b->over < b->dropcount) {
b->selected = b->over;
b->onselect(b->selected, b);
}
return 1;
}
return 0;
}
_Bool dropdown_mleave(DROPDOWN *b)
{
if(b->mouseover) {
b->mouseover = 0;
return 1;
}
return 0;
}
/***** list-based dropdown menu start *****/
// Appends localization-independent menu item.
void list_dropdown_add_hardcoded(DROPDOWN *b, uint8_t* name, void *handle)
{
void *p = realloc(b->userdata, (b->dropcount + 1) * sizeof(DROP_ELEMENT));
if(!p) {
return;
}
b->userdata = p;
DROP_ELEMENT *e = &((DROP_ELEMENT*)b->userdata)[b->dropcount++];
maybe_i18nal_string_set_plain(&e->name, name, strlen((char*)name));
e->handle = handle;
}
// Appends localized menu item.
void list_dropdown_add_localized(DROPDOWN *b, UI_STRING_ID string_id, void *handle)
{
void *p = realloc(b->userdata, (b->dropcount + 1) * sizeof(DROP_ELEMENT));
if(!p) {
return;
}
b->userdata = p;
DROP_ELEMENT *e = &((DROP_ELEMENT*)b->userdata)[b->dropcount++];
maybe_i18nal_string_set_i18nal(&e->name, string_id);
e->handle = handle;
}
// Clears menu (removes all menu items of a list-based dropdown).
void list_dropdown_clear(DROPDOWN *b)
{
free(b->userdata);
b->userdata = NULL;
b->dropcount = 0;
b->over = 0;
b->selected = 0;
}
// Generic display function for list-based dropdowns,
// userdata of which is an array of DROP_ELEMENTs.
STRING* list_dropdown_ondisplay(uint16_t i, const DROPDOWN* dm)
{
DROP_ELEMENT *e = &((DROP_ELEMENT*) dm->userdata)[i];
return maybe_i18nal_string_get(&e->name);
}
/***** list-based dropdown menu end *****/
/***** simple localized dropdown menu start *****/
// Generic display function for simple dropdowns,
// userdata of which is a simple array of UI_STRING_IDs.
STRING* simple_dropdown_ondisplay(uint16_t i, const DROPDOWN* dm)
{
return SPTRFORLANG(LANG, ((UI_STRING_ID*) dm->userdata)[i]);
}
/***** simple localized dropdown menu end *****/