forked from 400plus/400plus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
menupage.c
228 lines (171 loc) · 5.2 KB
/
menupage.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
#include <vxworks.h>
#include <stdio.h>
#include "firmware.h"
#include "macros.h"
#include "languages.h"
#include "menu.h"
#include "menuitem.h"
#include "settings.h"
#include "utils.h"
#include "menupage.h"
int item_grabbed;
void menupage_display_line(menupage_t *page, const int line);
int get_item_id(menupage_t *page, int item_pos);
int get_real_id(menupage_t *page, int item_pos);
menuitem_t *get_item(menupage_t *page, int item_id);
void menupage_initialize(menupage_t *page) {
// Correct position if menu has shrunk since last time
int offset = page->current_line - (page->items.size - 1);
if (offset > 0) {
page->current_posn -= offset;
page->current_line -= offset;
}
// Release item if was grabbed last time we left menu
item_grabbed = FALSE;
}
void menupage_display(menu_t *menu) {
int i;
char buffer[LP_MAX_WORD];
menupage_t *page = menu->current_page;
int pad1, pad2, len = strlen_utf8(page->name);
if (page->sibilings) {
pad1 = ( MENU_WIDTH - 2 - len) / 2;
pad2 = (1 + MENU_WIDTH - 4 - len) / 2;
sprintf(buffer, "<<%*s%s%*s>>", pad1, "", page->name, pad2, "");
} else {
pad1 = ( MENU_WIDTH - 0 - len) / 2;
pad2 = (1 + MENU_WIDTH - 2 - len) / 2;
sprintf(buffer, "%*s%s%*s", pad1, "", page->name, pad2, "");
}
menu_set_text(7, buffer);
for(i = 0; i < MENU_HEIGHT; i++)
menupage_display_line(page, i);
menu_highlight(page->current_line);
}
void menupage_up(menu_t *menu) {
int display = FALSE;
menupage_t *page = menu->current_page;
if ((page->items.size > MENU_HEIGHT && settings.menu_wrap) || page->current_posn > 0) {
page->current_posn--;
if (item_grabbed) {
SWAP(page->ordering[get_item_id(page, page->current_posn)], page->ordering[get_item_id(page, page->current_posn + 1)]);
display = TRUE;
}
}
if (page->current_line > 0) {
page->current_line--;
menu_highlight(page->current_line);
} else {
display = TRUE;
}
if (display)
menu_event_display();
}
void menupage_down(menu_t *menu) {
int display = FALSE;
menupage_t *page = menu->current_page;
if ((page->items.size > MENU_HEIGHT && settings.menu_wrap) || page->current_posn < page->items.size - 1) {
page->current_posn++;
if (item_grabbed) {
SWAP(page->ordering[get_item_id(page, page->current_posn)], page->ordering[get_item_id(page, page->current_posn - 1)]);
display = TRUE;
}
}
if (page->current_line < MIN(MENU_HEIGHT, page->items.size) - 1) {
page->current_line++;
menu_highlight(page->current_line);
} else {
display = TRUE;
}
if (display)
menu_event_display();
}
void menupage_pgup(menu_t *menu) {
int display = FALSE;
menupage_t *page = menu->current_page;
if (item_grabbed)
return;
page->current_posn -= MENU_HEIGHT - 1;
if (! (page->items.size > MENU_HEIGHT && settings.menu_wrap))
page->current_posn = MAX(page->current_posn, 0);
if (page->current_line > 0) {
page->current_line = 0;
menu_highlight(page->current_line);
} else {
display = TRUE;
}
if (display)
menu_event_display();
}
void menupage_pgdown(menu_t *menu) {
int display = FALSE;
menupage_t *page = menu->current_page;
if (item_grabbed)
return;
page->current_posn += MENU_HEIGHT - 1;
if (! (page->items.size > MENU_HEIGHT && settings.menu_wrap)) {
page->current_posn = MIN(page->current_posn, page->items.size - 1);
}
if (page->current_line < MIN(MENU_HEIGHT, page->items.size) - 1) {
page->current_line = MIN(MENU_HEIGHT, page->items.size) - 1;
menu_highlight(page->current_line);
} else {
display = TRUE;
}
if (display)
menu_event_display();
}
void menupage_drag_drop(menu_t *menu) {
menupage_t *page = menu->current_page;
if (page->ordering) {
item_grabbed = ! item_grabbed;
menu->changed = TRUE;
menu_event_refresh();
}
}
void menupage_refresh(menu_t *menu) {
menupage_t *page = menu->current_page;
menupage_display_line(page, page->current_line);
menu_redraw();
}
void menupage_display_line(menupage_t *page, const int line) {
int i = 0;
char message[LP_MAX_WORD] = "";
int item_id = line + page->current_posn - page->current_line;
menuitem_t *item = get_item(page, item_id);
if (item) {
if (page->ordering && item_grabbed && get_item_id(page, item_id) == get_item_id(page, page->current_posn))
message[i++] = '>';
else if (page->highlight && page->highlighted_item == get_real_id(page, item_id))
message[i++] = '*';
else
message[i++] = ' ';
if (page->show_id) {
sprintf(message + i, "%X:", item->id);
i += 2;
}
if (item->action)
message[i++] = '!';
if (item->display)
item->display(item, &message[i], MENU_WIDTH - i);
}
menu_set_text(line, message);
}
menuitem_t *get_current_item(menupage_t *page) {
return get_item(page, page->current_posn);
}
menuitem_t *get_item(menupage_t *page, int item_pos) {
const int item_id = get_real_id(page, item_pos);
return (item_id < page->items.size) ? &page->items.data[item_id] : NULL;
}
int get_real_id(menupage_t *page, int item_pos) {
if (page->ordering)
return page->ordering[get_item_id(page, item_pos)];
else
return get_item_id(page, item_pos);
}
int get_item_id(menupage_t *page, int item_pos) {
const int max_pos = MAX(page->items.size, MENU_HEIGHT);
const int item_id = item_pos - max_pos * (item_pos / max_pos);
return (item_id < 0) ? (item_id + max_pos) : item_id;
}