forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdropdown.c
131 lines (111 loc) · 2.97 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
#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))
void dropdown_drawactive(void)
{
DROPDOWN *b = active;
if(!b) {
return;
}
int x = active_x, y = active_y, width = active_width, height = active_height;
setfont(FONT_TEXT);
setcolor(COLOR_TEXT);
framerect(x, y, x + width, y + height * b->dropcount, BLUE);
drawrect(x + 1, y + 1, x + width - 1, y + height * b->dropcount - 1, WHITE);
int i;
for(i = 0; i != b->dropcount; i++) {
int j = index(b, i);
DROP_ELEMENT *e = &b->drop[j];
if(j == b->over) {
drawrectw(x + 1, y + 1 + i * height, width - 2, height - 2, C_GRAY);
}
drawtextwidth(x + 2 * SCALE, width - 4 * SCALE, y + 2 * SCALE + i * height, e->name, strlen((char*)e->name));
}
}
void dropdown_draw(DROPDOWN *b, int x, int y, int width, int height)
{
if(!b->open) {
framerect(x, y, x + width, y + height, (b->mouseover ? C_GRAY2 : C_GRAY));
drawrect(x + 1, y + 1, x + width - 1, y + height - 1, WHITE);
if(b->dropcount) {
setfont(FONT_TEXT);
setcolor(COLOR_TEXT);
DROP_ELEMENT *e = &b->drop[b->selected];
drawtextwidth(x + 2 * SCALE, width - 4 * SCALE, y + 2 * SCALE, e->name, strlen((char*)e->name));
}
} else {
active_x = x;
active_y = y;
active_width = width;
active_height = height;
}
}
_Bool dropdown_mmove(DROPDOWN *b, int x, int y, int width, int height, int mx, int my, int dy)
{
_Bool mouseover = inrect(mx, my, 0, 0, width, height);
if(mouseover != b->mouseover) {
b->mouseover = mouseover;
return 1;
}
if(b->open) {
int over = my / height;
if(over < b->dropcount) {
over = index(b, over);
if(over != b->over) {
b->over = over;
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 *b)
{
return 0;
}
_Bool dropdown_mwheel(DROPDOWN *b, int height, double 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->drop[b->over].handle);
}
return 1;
}
return 0;
}
_Bool dropdown_mleave(DROPDOWN *b)
{
if(b->mouseover) {
b->mouseover = 0;
return 1;
}
return 0;
}
void dropdown_add(DROPDOWN *b, uint8_t *name, void *handle)
{
void *p = realloc(b->drop, (b->dropcount + 1) * sizeof(DROP_ELEMENT));
if(!p) {
return;
}
b->drop = p;
DROP_ELEMENT *e = &b->drop[b->dropcount++];
e->name = name;
e->handle = handle;
}