-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.c
510 lines (402 loc) · 10.7 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdbool.h>
#include <ncurses.h>
#include "headers/menu.h"
#include "headers/render.h"
#include "headers/utils.h"
#define MENU_PAD_TOP 2
#define MENU_PAD_BOTTOM 2
#define MENU_PAD_LEFT 2
#define MENU_PAD_RIGHT 1
#define MAX_CONTENTS_LENGTH 256
typedef struct MenuItem {
char* title;
char* description;
void* userdata;
} MenuItem;
typedef struct Menu {
char* menu_name;
MenuItem** menu_items;
int menu_length;
int selected_item;
int scroll_offset;
bool focused;
WINDOW* menu_win;
int max_height;
int max_width;
void* userdata;
void (*render_item)(Menu*, int, int);
int (*item_height)(MenuItem*);
} Menu;
int swap_item(Menu* menu, int src_index, int dest_index);
/* insert mode */
int menu_insert_mode(Menu* menu, int insert_index);
/* prob temp for now */
MenuItem* create_blank_menuitem(void);
/* rendering stuff */
void default_render_item(Menu* menu, int item_index, int start_y);
int default_item_height(MenuItem* menuitem);
int items_visible(Menu* menu, int offset);
int items_visible_rev(Menu* menu, int offset);
MenuItem*
create_menuitem(char* title)
{
MenuItem* new_menuitem;
TodoItem* new_userdata;
new_menuitem = malloc(sizeof(MenuItem));
new_menuitem->title = title;
new_menuitem->description = strdup(""); //TEMP FOR NOW
new_userdata = malloc(sizeof(TodoItem));
new_userdata->item_name = title;
new_userdata->description = strdup("");
new_userdata->due = strdup("");
new_userdata->subtask_list = malloc(0);
new_userdata->subtask_count = 0;
new_menuitem->userdata = new_userdata;
return new_menuitem;
}
MenuItem*
create_blank_menuitem(void)
{
return create_menuitem(strdup(""));
}
Menu*
create_menu(char* menu_name, MenuItem** item_list)
{
Menu* new_menu;
new_menu = malloc(sizeof(Menu));
new_menu->menu_name = menu_name;
new_menu->menu_items = item_list;
new_menu->menu_length = array_length(MenuItem*, item_list);
new_menu->selected_item = 0;
new_menu->scroll_offset = 0;
new_menu->focused = false;
new_menu->userdata = NULL;
new_menu->render_item = *default_render_item;
new_menu->item_height = *default_item_height;
set_menu_win(new_menu, stdscr);
return new_menu;
}
/* getters */
WINDOW*
get_menu_win(Menu* menu)
{
return menu->menu_win;
}
MenuItem*
get_menu_item(Menu* menu, int index)
{
if (index < 0 || index >= menu->menu_length) return NULL;
return menu->menu_items[index];
}
MenuItem**
get_menu_itemlist(Menu* menu)
{
return menu->menu_items;
}
int
get_selected_item(Menu* menu)
{
return menu->selected_item;
}
MenuItem*
get_selected_menuitem(Menu* menu)
{
return get_menu_item(menu, get_selected_item(menu));
}
bool
get_menu_focused(Menu* menu)
{
return menu->focused;
}
int
get_menu_length(Menu* menu)
{
return menu->menu_length;
}
char*
get_menu_name(Menu* menu)
{
return menu->menu_name;
}
void*
get_menu_userdata(Menu* menu)
{
return menu->userdata;
}
char*
get_menuitem_title(MenuItem* menuitem)
{
return menuitem->title;
}
char*
get_menuitem_descrip(MenuItem* menuitem)
{
return menuitem->description;
}
void*
get_menuitem_userdata(MenuItem* menuitem)
{
return menuitem->userdata;
}
/* setters */
int
set_menu_win(Menu* menu, WINDOW* win)
{
int height, width;
menu->menu_win = win;
getmaxyx(menu->menu_win, height, width);
menu->max_height = height-MENU_PAD_TOP-MENU_PAD_BOTTOM;
menu->max_width = width-MENU_PAD_LEFT-MENU_PAD_RIGHT;
return 0;
}
int
set_selected_item(Menu* menu, int selected_item)
{
menu->selected_item = selected_item;
return 0;
}
int
set_menu_focus(Menu* menu, bool focus)
{
menu->focused = focus;
return 0;
}
int
set_menu_userdata(Menu* menu, void* userdata)
{
menu->userdata = userdata;
return 0;
}
int
set_menu_renderitem(Menu* menu, void (*render_item)(Menu*, int, int))
{
menu->render_item = render_item;
return 0;
}
int
set_menu_itemheight(Menu* menu, int (*item_height)(MenuItem*))
{
menu->item_height = item_height;
return 0;
}
int
set_menuitem_title(MenuItem* menuitem, char* title)
{
menuitem->title = title;
return 0;
}
int
set_menuitem_descrip(MenuItem* menuitem, char* descrip)
{
menuitem->description = descrip;
return 0;
}
int
set_menuitem_userdata(MenuItem* menuitem, void* userdata)
{
menuitem->userdata = userdata;
return 0;
}
int
swap_item(Menu* menu, int src_index, int dest_index)
{
ar_swap_item((void**)menu->menu_items, src_index, dest_index);
return 0;
}
int
delete_item(Menu* menu, int index)
{
if (index < 0 || index > menu->menu_length-1) return -1;
for (int i = index; i <= menu->menu_length-1; i++) {
menu->menu_items[i] = menu->menu_items[i+1];
}
menu->menu_items = realloc(menu->menu_items, menu->menu_length*sizeof(MenuItem*));
menu->menu_items[menu->menu_length-1] = 0; // preserve null at end
menu->menu_length -= 1;
/* also move the current selected position if it's last */
if (menu->selected_item > menu->menu_length-1) {
menu->selected_item = menu->menu_length-1;
}
return 0;
}
int
insert_item(Menu* menu, MenuItem* menuitem, int index)
{ // note, this func does not validate index
/* resize array and insert */
menu->menu_items = realloc(menu->menu_items, (menu->menu_length+2)*sizeof(MenuItem*));
for (int i = menu->menu_length; i > index; i--) {
menu->menu_items[i] = menu->menu_items[i-1];
}
menu->menu_items[index] = menuitem;
menu->menu_items[menu->menu_length+1] = 0; // remember null at end
menu->menu_length += 1;
/* move cursor pos */
menu->selected_item = index;
return 0;
}
int
menu_insert_mode(Menu* menu, int insert_index)
{
char temp[MAX_CONTENTS_LENGTH+1]; // remember null
char* new_contents;
int insert_pos;
curs_on();
// account for multiline items
insert_pos = menu->scroll_offset;
for (int i = 0; i < insert_index; i++) {
insert_pos += menu->item_height(menu->menu_items[i]);
}
/* move cursor to right spot */
ungetstr(menu->menu_items[insert_index]->title);
mvwgetnstr(menu->menu_win,
insert_pos,
0,
temp,
MAX_CONTENTS_LENGTH
);
curs_off();
/* copy out */
new_contents = strdup(temp);
menu->menu_items[insert_index]->title = new_contents;
/* delete if empty - maybe move this to a cleanup stage */
if (strlen(new_contents) == 0) {
delete_item(menu, insert_index);
}
return 0;
}
int
menu_driver(Menu* menu, MenuAction action)
{
switch (action) {
case MENU_UP:
menu->selected_item = menu->selected_item-1 >= 0 ? menu->selected_item-1 : 0;
break;
case MENU_DOWN:
menu->selected_item = menu->selected_item+1 <= menu->menu_length-1 ? menu->selected_item+1 : menu->menu_length-1;
break;
case MENU_TOP:
menu->selected_item = 0;
break;
case MENU_BOTTOM:
menu->selected_item = menu->menu_length-1;
break;
case MENU_MOVE_UP:
if (menu->selected_item <= 0) break;
swap_item(menu, menu->selected_item, menu->selected_item-1);
menu->selected_item -= 1;
break;
case MENU_MOVE_DOWN:
if (menu->selected_item >= menu->menu_length-1) break;
swap_item(menu, menu->selected_item, menu->selected_item+1);
menu->selected_item += 1;
break;
case MENU_DELETE:
delete_item(menu, menu->selected_item);
break;
case MENU_APPEND:
insert_item(menu, create_blank_menuitem(), menu->menu_length);
render_menu(menu); // refresh after inserting
menu_insert_mode(menu, menu->selected_item);
break;
case MENU_INSERT_ABOVE:
insert_item(menu, create_blank_menuitem(), menu->selected_item);
render_menu(menu);
menu_insert_mode(menu, menu->selected_item);
break;
case MENU_INSERT_BELOW:
insert_item(menu, create_blank_menuitem(), menu->selected_item+1);
render_menu(menu);
menu_insert_mode(menu, menu->selected_item); // inserted item is cur now
break;
case MENU_EDIT:
menu_insert_mode(menu, menu->selected_item);
break;
default: // This is here for debug, disable later
fprintf(stderr, "Invalid menu action");
}
return 0;
}
int
render_menu(Menu* menu)
{
wclear(menu->menu_win);
/* calculate scroll */
int visible;
visible = items_visible(menu, menu->scroll_offset);
if (menu->selected_item >= menu->scroll_offset+visible) {
menu->scroll_offset = clamp(
menu->selected_item-items_visible_rev(menu, menu->selected_item)+1,
0,
floorzero(menu->menu_length-1)
);
} else if (menu->selected_item < menu->scroll_offset) {
menu->scroll_offset = clamp(
menu->selected_item,
0,
floorzero(menu->menu_length-1)
);
}
/* render menu items */
int curline = 0;
for (int i = menu->scroll_offset; i < menu->menu_length; i++) {
menu->render_item(menu, i, curline);
curline += menu->item_height(menu->menu_items[i]);
}
wrefresh(menu->menu_win);
return 0;
}
void
default_render_item(Menu* menu, int item_index, int start_y)
{
MenuItem* curitem;
int hlcolor;
curitem = menu->menu_items[item_index];
/* color selected item */
hlcolor = COLOR_PAIR((item_index == menu->selected_item && menu->focused == true) ? TS_SELECTED : TS_NONSELECTED);
wattron(menu->menu_win, hlcolor);
mvwprintw(menu->menu_win, start_y, 0, curitem->title);
wattroff(menu->menu_win, hlcolor);
}
int
default_item_height(MenuItem* menuitem)
{
return 1; // maybe account for line wrap by default
}
int
items_visible(Menu* menu, int offset)
{
int maxheight;
int maxwidth; // unused
getmaxyx(menu->menu_win, maxheight, maxwidth);
int vis = 0;
int lines = 0;
for (int i = offset ; i < menu->menu_length; i++) {
lines += menu->item_height(menu->menu_items[i]);
if (lines > maxheight) break;
vis += 1;
}
return vis;
}
int
items_visible_rev(Menu* menu, int offset)
{
int maxheight;
int maxwidth; // unused
getmaxyx(menu->menu_win, maxheight, maxwidth);
int vis = 0;
int lines = 0;
for (int i = offset; i > 0; i--) {
lines += menu->item_height(menu->menu_items[i]);
if (lines > maxheight) break;
vis +=1;
}
return vis;
}
int
free_menu(Menu* menu)
{
return 0;
}