-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmenu.c
242 lines (193 loc) · 4.79 KB
/
menu.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
#include "menu.h"
/* This supports only a single menu instance to reduce programming clutter.
It is designed for simple LCD displays. */
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
//#define MENU_WRAP
typedef struct
{
menu_t *current;
uint8_t rows;
uint8_t preview;
menu_style_t style;
void (*display)(const char *title, int row,
const char *item_name, bool highlight);
} menu_data_t;
static menu_data_t menu_data;
void
menu_show (void)
{
/* Probably need a callback here. */
int i;
menu_t *menu = menu_data.current;
for (i = 0; i < menu_data.rows; i++)
{
uint8_t item;
// item = (top + i) % menu->size;
item = menu->top + i;
if (item >= menu->size)
break;
menu_data.display (menu->title, i, menu->items[item].name,
item == menu->index);
}
}
void
menu_top_best (menu_t *menu)
{
if (menu->index <= menu_data.preview)
menu->top = 0;
else
menu->top = menu->index - menu_data.preview;
}
/* Display a new menu, saving previous menu to return to later. */
bool
menu_display (menu_t *menu)
{
menu->parent = menu_data.current;
menu_data.current = menu;
menu_top_best (menu_data.current);
menu_show ();
return 0;
}
/* Display a new menu with prompt at top. */
bool
menu_display_top (menu_t *menu)
{
menu->parent = menu_data.current;
menu_data.current = menu;
menu_data.current->index = 0;
menu_top_best (menu_data.current);
menu_show ();
return 0;
}
/* Quit the current menu and return to parent menu. */
void
menu_quit (void)
{
/* Can't exit main menu. */
if (!menu_data.current->parent)
{
/* Redraw. */
menu_show ();
return;
}
menu_data.current = menu_data.current->parent;
menu_show ();
}
/* Go to an item in a menu. */
void
menu_goto (int index)
{
if (index > menu_data.current->size)
index = menu_data.current->size - 1;
else if (index < 0)
index = 0;
menu_data.current->index = index;
menu_top_best (menu_data.current);
menu_show ();
}
/* Set menu index and call action if appropriate. */
void
menu_index_set (menu_t *menu, uint8_t index)
{
menu_t *save;
/* Handle bogus values. */
if (index >= menu->size)
index = 0;
/* Need to set current menu so action can find out which menu
is active. */
save = menu_data.current;
menu_data.current = menu;
menu->index = index;
menu_top_best (menu);
if (menu->items[menu->index].action)
menu->items[menu->index].action ();
menu_data.current = save;
}
/* Perform action for current menu item. */
void
menu_select (void)
{
if (!menu_data.current->items[menu_data.current->index].action
|| menu_data.current->items[menu_data.current->index].action ())
menu_quit ();
}
/* Go to next menu item. */
void
menu_next (void)
{
if (menu_data.current->index >= menu_data.current->size - 1)
{
/* Reached bottom of menu. */
#ifdef MENU_WRAP
/* Wrap back to top of menu. */
menu_data.current->index = 0;
#endif
}
else
{
int spare = menu_data.current->size
- (menu_data.current->top + menu_data.rows);
menu_data.current->index++;
if (menu_data.current->index >= menu_data.rows
+ menu_data.current->top - menu_data.preview
&& spare > 0)
menu_data.current->top++;
}
menu_show ();
}
/* Go to previous menu item. */
void
menu_prev (void)
{
if (menu_data.current->index == 0)
{
/* Reached top of menu. */
#ifdef MENU_WRAP
/* Wrap back to bottom of menu. */
menu_data.current->index = menu_data.current->size - 1;
#endif
}
else
{
int spare = menu_data.current->top;
menu_data.current->index--;
if (spare > 0 && menu_data.current->index - menu_data.current->top
< menu_data.preview)
menu_data.current->top--;
}
menu_show ();
}
void menu_style_set (menu_style_t style)
{
menu_data.style = style;
}
menu_t *
menu_current_get (void)
{
return menu_data.current;
}
uint8_t
menu_current_index_get (void)
{
menu_t *menu;
menu = menu_current_get();
return menu->index;
}
void
menu_init (int rows,
void (*display)(const char *title, int row,
const char *item_name, bool highlight))
{
menu_data.rows = rows;
/* Preview controls how many menu rows are displayed before the
prompt at the top of the screen or after the prompt at the
bottom of the screen. */
if (rows > 2)
menu_data.preview = 1;
else
menu_data.preview = 0;
menu_data.style = MENU_STYLE_SCROLL;
menu_data.display = display;
}