forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontextmenu.c
130 lines (102 loc) · 2.36 KB
/
contextmenu.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
#include "main.h"
static CONTEXTMENU context_menu;
#define CONTEXT_WIDTH (SCALE * 60)
#define CONTEXT_HEIGHT (SCALE * 12)
void contextmenu_draw(void)
{
CONTEXTMENU *b = &context_menu;
if(!b->open) {
return;
}
setfont(FONT_TEXT);
setcolor(COLOR_TEXT);
drawrectw(b->x, b->y, b->width, b->height, WHITE);
drawrectw(b->x, b->y + b->over * CONTEXT_HEIGHT, b->width, CONTEXT_HEIGHT, C_GRAY);
int i;
for(i = 0; i != b->count; i++) {
drawtext(b->x + SCALE * 2, b->y + SCALE * 2 + i * CONTEXT_HEIGHT, b->names[i], strlen((char*)b->names[i]));
}
framerect(b->x, b->y, b->x + b->width, b->y + b->height, BLUE);
}
_Bool contextmenu_mmove(int mx, int my, int dx, int dy)
{
CONTEXTMENU *b = &context_menu;
if(!b->open) {
return 0;
}
cursor = CURSOR_NONE;
_Bool mouseover = inrect(mx, my, b->x, b->y, b->width, b->height);
if(!mouseover) {
if(b->over != 0xFF) {
b->over = 0xFF;
return 1;
}
return 0;
}
uint8_t over = (my - b->y) / CONTEXT_HEIGHT;
if(over >= b->count) {
over = 0xFF;
}
if(over != b->over) {
b->over = over;
return 1;
}
return 0;
}
_Bool contextmenu_mdown(void)
{
CONTEXTMENU *b = &context_menu;
if(!b->open) {
return 0;
}
if(b->over != 0xFF) {
b->down = b->over;
} else {
b->open = 0;
}
return 1;
}
_Bool contextmenu_mup(void)
{
CONTEXTMENU *b = &context_menu;
if(!b->open) {
return 0;
}
if(b->over == b->down) {
b->onselect(b->over);
b->open = 0;
return 1;
}
return 0;
}
_Bool contextmenu_mleave(void)
{
CONTEXTMENU *b = &context_menu;
if(!b->open) {
return 0;
}
if(b->over != 0xFF) {
b->over = 0xFF;
return 1;
}
return 0;
}
void contextmenu_new(uint8_t **names, uint8_t count, void (*onselect)(uint8_t))
{
CONTEXTMENU *b = &context_menu;
b->y = mouse.y;
b->height = CONTEXT_HEIGHT * count;
if(b->y + b->height >= height) {
b->y -= b->height;
}
b->x = mouse.x;
b->width = CONTEXT_WIDTH;
if(b->x + b->width >= width) {
b->x -= b->width;
}
b->open = 1;
b->count = count;
b->over = 0xFF;
b->onselect = onselect;
memcpy(b->names, names, sizeof(uint8_t*) * count);
}