Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
menu.c
Go to the documentation of this file.
1#include "internal.h"
2
3// ncmenu_item and ncmenu_section have internal and (minimal) external forms
4typedef struct ncmenu_int_item {
5 char* desc; // utf-8 menu item, NULL for horizontal separator
6 ncinput shortcut; // shortcut, all should be distinct
7 int shortcut_offset; // column offset with desc of shortcut EGC
8 char* shortdesc; // description of shortcut, can be NULL
9 int shortdesccols; // columns occupied by shortcut description
10 bool disabled; // disabled?
12
13typedef struct ncmenu_int_section {
14 char* name; // utf-8 c string
15 unsigned itemcount;
16 ncmenu_int_item* items; // items, NULL iff itemcount == 0
17 ncinput shortcut; // shortcut, will be underlined if present in name
18 int xoff; // column offset from beginning of menu bar
19 int bodycols; // column width of longest item
20 int itemselected; // current item selected, -1 for no selection
21 int shortcut_offset; // column offset within name of shortcut EGC
22 int enabled_item_count; // number of enabled items: section is disabled iff 0
24
25typedef struct ncmenu {
27 int sectioncount; // must be positive
28 ncmenu_int_section* sections; // NULL iff sectioncount == 0
29 int unrolledsection; // currently unrolled section, -1 if none
30 int headerwidth; // minimum space necessary to display all sections
31 uint64_t headerchannels; // styling for header
32 uint64_t dissectchannels; // styling for disabled section headers
33 uint64_t sectionchannels; // styling for sections
34 uint64_t disablechannels; // styling for disabled entries
35 bool bottom; // are we on the bottom (vs top)?
37
38// Search the provided multibyte (UTF8) string 's' for the provided unicode
39// codepoint 'cp'. If found, return the column offset of the EGC in which the
40// codepoint appears in 'col', and the byte offset as the return value. If not
41// found, -1 is returned, and 'col' is meaningless.
42static int
43mbstr_find_codepoint(const char* s, uint32_t cp, int* col){
44 mbstate_t ps;
45 memset(&ps, 0, sizeof(ps));
46 size_t bytes = 0;
47 size_t r;
48 wchar_t w;
49 *col = 0;
50 while((r = mbrtowc(&w, s + bytes, MB_CUR_MAX, &ps)) != (size_t)-1 && r != (size_t)-2){
51 if(r == 0){
52 break;
53 }
54 if(towlower(cp) == towlower(w)){
55 return bytes;
56 }
57 *col += wcwidth(w);
58 bytes += r;
59 }
60 return -1;
61}
62
63static void
64free_menu_section(ncmenu_int_section* ms){
65 for(unsigned i = 0 ; i < ms->itemcount ; ++i){
66 free(ms->items[i].desc);
67 free(ms->items[i].shortdesc);
68 }
69 free(ms->items);
70 free(ms->name);
71}
72
73static void
74free_menu_sections(ncmenu* ncm){
75 for(int i = 0 ; i < ncm->sectioncount ; ++i){
76 free_menu_section(&ncm->sections[i]);
77 }
78 free(ncm->sections);
79}
80
81static int
82dup_menu_item(ncmenu_int_item* dst, const struct ncmenu_item* src){
83#define ALTMOD "Alt+"
84#define CTLMOD "Ctrl+"
85 dst->disabled = false;
86 if((dst->desc = strdup(src->desc)) == NULL){
87 return -1;
88 }
89 if(!src->shortcut.id){
90 dst->shortdesccols = 0;
91 dst->shortdesc = NULL;
92 return 0;
93 }
94 size_t bytes = 1; // NUL terminator
95 if(ncinput_alt_p(&src->shortcut)){
96 bytes += strlen(ALTMOD);
97 }
98 if(ncinput_ctrl_p(&src->shortcut)){
99 bytes += strlen(CTLMOD);
100 }
101 mbstate_t ps;
102 memset(&ps, 0, sizeof(ps));
103 size_t shortsize = wcrtomb(NULL, src->shortcut.id, &ps);
104 if(shortsize == (size_t)-1){
105 free(dst->desc);
106 return -1;
107 }
108 bytes += shortsize + 1;
109 char* sdup = malloc(bytes);
110 int n = snprintf(sdup, bytes, "%s%s", ncinput_alt_p(&src->shortcut) ? ALTMOD : "",
111 ncinput_ctrl_p(&src->shortcut) ? CTLMOD : "");
112 if(n < 0 || (size_t)n >= bytes){
113 free(sdup);
114 free(dst->desc);
115 return -1;
116 }
117 memset(&ps, 0, sizeof(ps));
118 size_t mbbytes = wcrtomb(sdup + n, src->shortcut.id, &ps);
119 if(mbbytes == (size_t)-1){ // shouldn't happen
120 free(sdup);
121 free(dst->desc);
122 return -1;
123 }
124 sdup[n + mbbytes] = '\0';
125 dst->shortdesc = sdup;
127 return 0;
128#undef CTLMOD
129#undef ALTMOD
130}
131
132static int
133dup_menu_section(ncmenu_int_section* dst, const struct ncmenu_section* src){
134 // we must reject any empty section
135 if(src->itemcount == 0 || src->items == NULL){
136 return -1;
137 }
138 dst->bodycols = 0;
139 dst->itemselected = -1;
140 dst->items = NULL;
141 // we must reject any section which is entirely separators
142 bool gotitem = false;
143 dst->itemcount = 0;
144 dst->enabled_item_count = 0;
145 dst->items = malloc(sizeof(*dst->items) * src->itemcount);
146 if(dst->items == NULL){
147 return -1;
148 }
149 for(int i = 0 ; i < src->itemcount ; ++i){
150 if(src->items[i].desc){
151 if(dup_menu_item(&dst->items[i], &src->items[i])){
152 while(i--){
153 free(dst->items[i].desc);
154 }
155 free(dst->items);
156 return -1;
157 }
158 gotitem = true;
159 int cols = ncstrwidth(dst->items[i].desc, NULL, NULL);
160 if(dst->items[i].shortdesc){
161 cols += 2 + dst->items[i].shortdesccols; // two spaces minimum
162 }
163 if(cols > dst->bodycols){
164 dst->bodycols = cols;
165 }
166 memcpy(&dst->items[i].shortcut, &src->items[i].shortcut, sizeof(dst->items[i].shortcut));
167 if(mbstr_find_codepoint(dst->items[i].desc,
168 dst->items[i].shortcut.id,
169 &dst->items[i].shortcut_offset) < 0){
170 dst->items[i].shortcut_offset = -1;
171 }
172 }else{
173 dst->items[i].desc = NULL;
174 dst->items[i].shortdesc = NULL;
175 }
176 ++dst->itemcount;
177 }
178 dst->enabled_item_count = dst->itemcount;
179 if(!gotitem){
180 while(dst->itemcount){
181 free(dst->items[--dst->itemcount].desc);
182 }
183 free(dst->items);
184 return -1;
185 }
186 return 0;
187}
188
189// Duplicates all menu sections in opts, adding their length to '*totalwidth'.
190static int
191dup_menu_sections(ncmenu* ncm, const ncmenu_options* opts, unsigned* totalwidth, unsigned* totalheight){
192 if(opts->sectioncount == 0){
193 return -1;
194 }
195 ncm->sections = malloc(sizeof(*ncm->sections) * opts->sectioncount);
196 if(ncm->sections == NULL){
197 return -1;
198 }
199 bool rightaligned = false; // can only right-align once. twice is error.
200 unsigned maxheight = 0;
201 unsigned maxwidth = *totalwidth;
202 unsigned xoff = 2;
203 int i;
204 for(i = 0 ; i < opts->sectioncount ; ++i){
205 if(opts->sections[i].name){
206 int cols = ncstrwidth(opts->sections[i].name, NULL, NULL);
207 if(rightaligned){ // FIXME handle more than one right-aligned section
208 ncm->sections[i].xoff = -(cols + 2);
209 }else{
210 ncm->sections[i].xoff = xoff;
211 }
212 if(cols < 0 || (ncm->sections[i].name = strdup(opts->sections[i].name)) == NULL){
213 goto err;
214 }
215 if(dup_menu_section(&ncm->sections[i], &opts->sections[i])){
216 free(ncm->sections[i].name);
217 goto err;
218 }
219 if(ncm->sections[i].itemcount > maxheight){
220 maxheight = ncm->sections[i].itemcount;
221 }
222 if(*totalwidth + cols + 2 > maxwidth){
223 maxwidth = *totalwidth + cols + 2;
224 }
225 if(*totalwidth + ncm->sections[i].bodycols + 2 > maxwidth){
226 maxwidth = *totalwidth + ncm->sections[i].bodycols + 2;
227 }
228 *totalwidth += cols + 2;
229 memcpy(&ncm->sections[i].shortcut, &opts->sections[i].shortcut, sizeof(ncm->sections[i].shortcut));
230 if(mbstr_find_codepoint(ncm->sections[i].name,
231 ncm->sections[i].shortcut.id,
232 &ncm->sections[i].shortcut_offset) < 0){
233 ncm->sections[i].shortcut_offset = -1;
234 }
235 xoff += cols + 2;
236 }else{ // divider; remaining sections are right-aligned
237 if(rightaligned){
238 goto err;
239 }
240 rightaligned = true;
241 ncm->sections[i].name = NULL;
242 ncm->sections[i].items = NULL;
243 ncm->sections[i].itemcount = 0;
244 ncm->sections[i].xoff = -1;
245 ncm->sections[i].bodycols = 0;
246 ncm->sections[i].itemselected = -1;
247 ncm->sections[i].shortcut_offset = -1;
248 ncm->sections[i].enabled_item_count = 0;
249 }
250 }
251 if(ncm->sectioncount == 1 && rightaligned){
252 goto err;
253 }
254 *totalwidth = maxwidth;
255 *totalheight += maxheight + 2; // two rows of border
256 return 0;
257
258err:
259 while(i--){
260 free_menu_section(&ncm->sections[i]);
261 }
262 free(ncm->sections);
263 return -1;
264}
265
266// what section header, if any, is living at the provided x coordinate? solves
267// by replaying the write_header() algorithm. returns -1 if no such section.
268static int
269section_x(const ncmenu* ncm, int x){
270 int dimx = ncplane_dim_x(ncm->ncp);
271 for(int i = 0 ; i < ncm->sectioncount ; ++i){
272 if(!ncm->sections[i].name){
273 continue;
274 }
275 if(ncm->sections[i].xoff < 0){ // right-aligned
276 int pos = dimx + ncm->sections[i].xoff;
277 if(x < pos){
278 break;
279 }
280 if(x < pos + ncstrwidth(ncm->sections[i].name, NULL, NULL)){
281 return i;
282 }
283 }else{
284 if(x < ncm->sections[i].xoff){
285 break;
286 }
287 if(x < ncm->sections[i].xoff + ncstrwidth(ncm->sections[i].name, NULL, NULL)){
288 return i;
289 }
290 }
291 }
292 return -1;
293}
294
295static int
296write_header(ncmenu* ncm){
298 unsigned dimy, dimx;
299 ncplane_dim_yx(ncm->ncp, &dimy, &dimx);
300 unsigned xoff = 0; // 2-column margin on left
301 int ypos = ncm->bottom ? dimy - 1 : 0;
302 if(ncplane_cursor_move_yx(ncm->ncp, ypos, 0)){
303 return -1;
304 }
306 ncplane_set_styles(ncm->ncp, 0);
307 if(ncplane_putc(ncm->ncp, &c) < 0){
308 return -1;
309 }
310 if(ncplane_putc(ncm->ncp, &c) < 0){
311 return -1;
312 }
313 for(int i = 0 ; i < ncm->sectioncount ; ++i){
314 if(ncm->sections[i].name){
315 ncplane_cursor_move_yx(ncm->ncp, ypos, xoff);
316 int spaces = ncm->sections[i].xoff - xoff;
317 if(ncm->sections[i].xoff < 0){ // right-aligned
318 spaces = dimx + ncm->sections[i].xoff - xoff;
319 if(spaces < 0){
320 spaces = 0;
321 }
322 }
323 xoff += spaces;
324 while(spaces--){
325 if(ncplane_putc(ncm->ncp, &c) < 0){
326 return -1;
327 }
328 }
329 if(ncm->sections[i].enabled_item_count <= 0){
331 }else{
333 }
334 if(ncplane_putstr_yx(ncm->ncp, ypos, xoff, ncm->sections[i].name) < 0){
335 return -1;
336 }
337 if(ncm->sections[i].shortcut_offset >= 0){
339 if(ncplane_at_yx_cell(ncm->ncp, ypos, xoff + ncm->sections[i].shortcut_offset, &cl) < 0){
340 return -1;
341 }
342 nccell_on_styles(&cl, NCSTYLE_UNDERLINE|NCSTYLE_BOLD);
343 if(ncplane_putc_yx(ncm->ncp, ypos, xoff + ncm->sections[i].shortcut_offset, &cl) < 0){
344 return -1;
345 }
346 nccell_release(ncm->ncp, &cl);
347 }
348 xoff += ncstrwidth(ncm->sections[i].name, NULL, NULL);
349 }
350 }
351 while(xoff < dimx){
352 if(ncplane_putc_yx(ncm->ncp, ypos, xoff, &c) < 0){
353 return -1;
354 }
355 ++xoff;
356 }
357 return 0;
358}
359
360static int
361resize_menu(ncplane* n){
362 const ncplane* parent = ncplane_parent_const(n);
363 int dimx = ncplane_dim_x(parent);
364 int dimy = ncplane_dim_y(n);
365 if(ncplane_resize_simple(n, dimy, dimx)){
366 return -1;
367 }
368 ncmenu* menu = ncplane_userptr(n);
369 int unrolled = menu->unrolledsection;
370 if(unrolled < 0){
371 return write_header(menu);
372 }
373 ncplane_erase(n); // "rolls up" section without resetting unrolledsection
374 return ncmenu_unroll(menu, unrolled);
375}
376
378 ncmenu_options zeroed = {0};
379 if(!opts){
380 opts = &zeroed;
381 }
382 if(opts->sectioncount <= 0 || !opts->sections){
383 logerror("invalid %d-ary section information", opts->sectioncount);
384 return NULL;
385 }
386 if(opts->flags >= (NCMENU_OPTION_HIDING << 1u)){
387 logwarn("provided unsupported flags %016" PRIx64, opts->flags);
388 }
389 unsigned totalheight = 1;
390 unsigned totalwidth = 2; // start with two-character margin on the left
391 ncmenu* ret = malloc(sizeof(*ret));
392 ret->sectioncount = opts->sectioncount;
393 ret->sections = NULL;
394 unsigned dimy, dimx;
395 ncplane_dim_yx(n, &dimy, &dimx);
396 if(ret){
398 if(dup_menu_sections(ret, opts, &totalwidth, &totalheight) == 0){
399 ret->headerwidth = totalwidth;
400 if(totalwidth < dimx){
401 totalwidth = dimx;
402 }
403 struct ncplane_options nopts = {
404 .y = ret->bottom ? dimy - totalheight : 0,
405 .x = 0,
406 .rows = totalheight,
407 .cols = totalwidth,
408 .userptr = ret,
409 .name = "menu",
410 .resizecb = resize_menu,
411 .flags = NCPLANE_OPTION_FIXED,
412 };
413 ret->ncp = ncplane_create(n, &nopts);
414 if(ret->ncp){
415 if(ncplane_set_widget(ret->ncp, ret, (void(*)(void*))ncmenu_destroy) == 0){
416 ret->unrolledsection = -1;
417 ret->headerchannels = opts->headerchannels;
418 ret->dissectchannels = opts->headerchannels;
419 ncchannels_set_fg_rgb(&ret->dissectchannels, 0xdddddd);
420 ret->sectionchannels = opts->sectionchannels;
422 ncchannels_set_fg_rgb(&ret->disablechannels, 0xdddddd);
424 nccell_set_fg_alpha(&c, NCALPHA_TRANSPARENT);
425 nccell_set_bg_alpha(&c, NCALPHA_TRANSPARENT);
427 nccell_release(ret->ncp, &c);
428 if(write_header(ret) == 0){
429 return ret;
430 }
431 }
432 ncplane_destroy(ret->ncp);
433 }
434 free_menu_sections(ret);
435 }
436 free(ret);
437 }
438 logerror("error creating ncmenu");
439 return NULL;
440}
441
442static inline int
443section_height(const ncmenu* n, int sectionidx){
444 return n->sections[sectionidx].itemcount + 2;
445}
446
447static inline int
448section_width(const ncmenu* n, int sectionidx){
449 return n->sections[sectionidx].bodycols + 2;
450}
451
452int ncmenu_unroll(ncmenu* n, int sectionidx){
453 if(ncmenu_rollup(n)){ // roll up any unrolled section
454 return -1;
455 }
456 if(sectionidx < 0 || sectionidx >= n->sectioncount){
457 logerror("unrolled invalid sectionidx %d", sectionidx);
458 return -1;
459 }
460 if(n->sections[sectionidx].enabled_item_count <= 0){
461 return 0;
462 }
463 if(n->sections[sectionidx].name == NULL){
464 return -1;
465 }
466 n->unrolledsection = sectionidx;
467 unsigned dimy, dimx;
468 ncplane_dim_yx(n->ncp, &dimy, &dimx);
469 const int height = section_height(n, sectionidx);
470 const int width = section_width(n, sectionidx);
471 int xpos = n->sections[sectionidx].xoff < 0 ?
472 (int)dimx + (n->sections[sectionidx].xoff - 2) : n->sections[sectionidx].xoff;
473 if(xpos + width >= (int)dimx){
474 xpos = dimx - (width + 2);
475 }
476 int ypos = n->bottom ? dimy - height - 1 : 1;
477 if(ncplane_cursor_move_yx(n->ncp, ypos, xpos)){
478 return -1;
479 }
480 if(ncplane_rounded_box_sized(n->ncp, 0, n->headerchannels, height, width, 0)){
481 return -1;
482 }
483 ncmenu_int_section* sec = &n->sections[sectionidx];
484 for(unsigned i = 0 ; i < sec->itemcount ; ++i){
485 ++ypos;
486 if(sec->items[i].desc){
487 // FIXME the user ought be able to configure the disabled channel
488 if(!sec->items[i].disabled){
489 ncplane_set_channels(n->ncp, n->sectionchannels);
490 if(sec->itemselected < 0){
491 sec->itemselected = i;
492 }
493 }else{
494 ncplane_set_channels(n->ncp, n->disablechannels);
495 }
496 if(sec->itemselected >= 0){
497 if(i == (unsigned)sec->itemselected){
498 ncplane_set_channels(n->ncp, ncchannels_reverse(ncplane_channels(n->ncp)));
499 }
500 }
501 ncplane_set_styles(n->ncp, 0);
502 int cols = ncplane_putstr_yx(n->ncp, ypos, xpos + 1, sec->items[i].desc);
503 if(cols < 0){
504 return -1;
505 }
506 // we need pad out the remaining columns of this line with spaces. if
507 // there's a shortcut description, we align it to the right, printing
508 // spaces only through the start of the aligned description.
509 int thiswidth = width;
510 if(sec->items[i].shortdesc){
511 thiswidth -= sec->items[i].shortdesccols;
512 }
513 // print any necessary padding spaces
514 for(int j = cols + 1 ; j < thiswidth - 1 ; ++j){
515 if(ncplane_putchar(n->ncp, ' ') < 0){
516 return -1;
517 }
518 }
519 if(sec->items[i].shortdesc){
520 if(ncplane_putstr(n->ncp, sec->items[i].shortdesc) < 0){
521 return -1;
522 }
523 }
524 if(sec->items[i].shortcut_offset >= 0){
526 if(ncplane_at_yx_cell(n->ncp, ypos, xpos + 1 + sec->items[i].shortcut_offset, &cl) < 0){
527 return -1;
528 }
529 nccell_on_styles(&cl, NCSTYLE_UNDERLINE|NCSTYLE_BOLD);
530 if(ncplane_putc_yx(n->ncp, ypos, xpos + 1 + sec->items[i].shortcut_offset, &cl) < 0){
531 return -1;
532 }
533 nccell_release(n->ncp, &cl);
534 }
535 }else{
536 n->ncp->channels = n->headerchannels;
537 ncplane_set_styles(n->ncp, 0);
538 if(ncplane_putegc_yx(n->ncp, ypos, xpos, "├", NULL) < 0){
539 return -1;
540 }
541 for(int j = 1 ; j < width - 1 ; ++j){
542 if(ncplane_putegc(n->ncp, "─", NULL) < 0){
543 return -1;
544 }
545 }
546 if(ncplane_putegc(n->ncp, "┤", NULL) < 0){
547 return -1;
548 }
549 }
550 }
551 return 0;
552}
553
555 if(n->unrolledsection < 0){
556 return 0;
557 }
558 n->unrolledsection = -1;
559 ncplane_erase(n->ncp);
560 return write_header(n);
561}
562
564 int nextsection = n->unrolledsection;
565 int origselected = n->unrolledsection;
566 do{
567 if(++nextsection == n->sectioncount){
568 nextsection = 0;
569 }
570 if(nextsection == origselected){
571 break;
572 }
573 }while(n->sections[nextsection].name == NULL ||
574 n->sections[nextsection].enabled_item_count == 0);
575 return ncmenu_unroll(n, nextsection);
576}
577
579 int prevsection = n->unrolledsection;
580 int origselected = n->unrolledsection;
581 do{
582 if(--prevsection < 0){
583 prevsection = n->sectioncount - 1;
584 }
585 if(prevsection == origselected){
586 break;
587 }
588 }while(n->sections[prevsection].name == NULL ||
589 n->sections[prevsection].enabled_item_count == 0);
590 return ncmenu_unroll(n, prevsection);
591}
592
594 if(n->unrolledsection == -1){
595 if(ncmenu_unroll(n, 0)){
596 return -1;
597 }
598 }
599 ncmenu_int_section* sec = &n->sections[n->unrolledsection];
600 int origselected = sec->itemselected;
601 if(origselected >= 0){
602 do{
603 if((unsigned)++sec->itemselected == sec->itemcount){
604 sec->itemselected = 0;
605 }
606 if(sec->itemselected == origselected){
607 break;
608 }
609 }while(!sec->items[sec->itemselected].desc || sec->items[sec->itemselected].disabled);
610 }
611 return ncmenu_unroll(n, n->unrolledsection);
612}
613
615 if(n->unrolledsection == -1){
616 if(ncmenu_unroll(n, 0)){
617 return -1;
618 }
619 }
620 ncmenu_int_section* sec = &n->sections[n->unrolledsection];
621 int origselected = sec->itemselected;
622 if(origselected >= 0){
623 do{
624 if(sec->itemselected-- == 0){
625 sec->itemselected = sec->itemcount - 1;
626 }
627 if(sec->itemselected == origselected){
628 break;
629 }
630 }while(!sec->items[sec->itemselected].desc || sec->items[sec->itemselected].disabled);
631 }
632 return ncmenu_unroll(n, n->unrolledsection);
633}
634
635const char* ncmenu_selected(const ncmenu* n, ncinput* ni){
636 if(n->unrolledsection < 0){
637 return NULL;
638 }
639 const struct ncmenu_int_section* sec = &n->sections[n->unrolledsection];
640 const int itemidx = sec->itemselected;
641 if(itemidx < 0){
642 return NULL;
643 }
644 if(ni){
645 memcpy(ni, &sec->items[itemidx].shortcut, sizeof(*ni));
646 }
647 return sec->items[itemidx].desc;
648}
649
650// given the active section, return the line on which we clicked, or -1 if the
651// click was not within said section. |y| and |x| ought be translated for the
652// menu plane |n|->ncp.
653static int
654ncsection_click_index(const ncmenu* n, const ncmenu_int_section* sec,
655 unsigned dimy, unsigned dimx, int y, int x){
656 // don't allow a click on the side boundaries
657 if(sec->xoff < 0){
658 if(x > (int)dimx - 4 || x <= (int)dimx - 4 - sec->bodycols){
659 return -1;
660 }
661 }else{
662 if(x <= sec->xoff || x > sec->xoff + sec->bodycols){
663 return -1;
664 }
665 }
666 const int itemidx = n->bottom ? y - ((int)dimy - (int)sec->itemcount) + 2 : y - 2;
667 if(itemidx < 0 || itemidx >= (int)sec->itemcount){
668 return -1;
669 }
670 return itemidx;
671}
672
673const char* ncmenu_mouse_selected(const ncmenu* n, const ncinput* click,
674 ncinput* ni){
675 if(click->id != NCKEY_BUTTON1){
676 return NULL;
677 }
678 if(click->evtype != NCTYPE_RELEASE){
679 return NULL;
680 }
681 struct ncplane* nc = n->ncp;
682 int y = click->y;
683 int x = click->x;
684 unsigned dimy, dimx;
685 ncplane_dim_yx(nc, &dimy, &dimx);
686 if(!ncplane_translate_abs(nc, &y, &x)){
687 return NULL;
688 }
689 if(n->unrolledsection < 0){
690 return NULL;
691 }
692 const struct ncmenu_int_section* sec = &n->sections[n->unrolledsection];
693 int itemidx = ncsection_click_index(n, sec, dimy, dimx, y, x);
694 if(itemidx < 0){
695 return NULL;
696 }
697 // don't allow a disabled item to be selected
698 if(sec->items[itemidx].disabled){
699 return NULL;
700 }
701 if(ni){
702 memcpy(ni, &sec->items[itemidx].shortcut, sizeof(*ni));
703 }
704 return sec->items[itemidx].desc;
705}
706
708 // we can't actually select menu items in this function, since we need to
709 // invoke an arbitrary function as a result.
710 if(nc->id == NCKEY_BUTTON1 && nc->evtype == NCTYPE_RELEASE){
711 int y = nc->y;
712 int x = nc->x;
713 unsigned dimy, dimx;
714 ncplane_dim_yx(n->ncp, &dimy, &dimx);
715 if(!ncplane_translate_abs(n->ncp, &y, &x)){
716 return false;
717 }
718 if(n->unrolledsection >= 0){
719 struct ncmenu_int_section* sec = &n->sections[n->unrolledsection];
720 int itemidx = ncsection_click_index(n, sec, dimy, dimx, y, x);
721 if(itemidx >= 0){
722 if(!sec->items[itemidx].disabled){
723 sec->itemselected = itemidx;
724 ncmenu_unroll(n, n->unrolledsection);
725 return false;
726 }
727 }
728 }
729 if(y != (n->bottom ? (int)dimy - 1 : 0)){
730 return false;
731 }
732 int i = section_x(n, x);
733 if(i < 0 || i == n->unrolledsection){
735 }else{
736 ncmenu_unroll(n, i);
737 }
738 return true;
739 }else if(nc->evtype == NCTYPE_RELEASE){
740 return false;
741 }
742 for(int si = 0 ; si < n->sectioncount ; ++si){
743 const ncmenu_int_section* sec = &n->sections[si];
744 if(sec->enabled_item_count == 0){
745 continue;
746 }
747 if(!ncinput_equal_p(&sec->shortcut, nc)){
748 continue;
749 }
750 ncmenu_unroll(n, si);
751 return true;
752 }
753 if(n->unrolledsection < 0){ // all following need an unrolled section
754 return false;
755 }
756 if(nc->id == NCKEY_LEFT){
758 return false;
759 }
760 return true;
761 }else if(nc->id == NCKEY_RIGHT){
763 return false;
764 }
765 return true;
766 }else if(nc->id == NCKEY_UP || nc->id == NCKEY_SCROLL_UP){
767 if(ncmenu_previtem(n)){
768 return false;
769 }
770 return true;
771 }else if(nc->id == NCKEY_DOWN || nc->id == NCKEY_SCROLL_DOWN){
772 if(ncmenu_nextitem(n)){
773 return false;
774 }
775 return true;
776 }else if(nc->id == NCKEY_ESC){
778 return true;
779 }
780 return false;
781}
782
783// FIXME we probably ought implement this with a trie or something
784int ncmenu_item_set_status(ncmenu* n, const char* section, const char* item,
785 bool enabled){
786 for(int si = 0 ; si < n->sectioncount ; ++si){
787 struct ncmenu_int_section* sec = &n->sections[si];
788 if(strcmp(sec->name, section) == 0){
789 for(unsigned ii = 0 ; ii < sec->itemcount ; ++ii){
790 struct ncmenu_int_item* i = &sec->items[ii];
791 if(strcmp(i->desc, item) == 0){
792 const bool changed = (i->disabled != enabled);
793 i->disabled = !enabled;
794 if(changed){
795 if(i->disabled){
796 if(--sec->enabled_item_count == 0){
797 write_header(n);
798 }
799 }else{
800 if(++sec->enabled_item_count == 1){
801 write_header(n);
802 }
803 }
804 if(n->unrolledsection == si){
805 if(sec->enabled_item_count == 0){
807 }else{
808 ncmenu_unroll(n, n->unrolledsection);
809 }
810 }
811 }
812 return 0;
813 }
814 }
815 break;
816 }
817 }
818 return -1;
819}
820
822 return menu->ncp;
823}
824
826 if(n){
827 free_menu_sections(n);
828 if(ncplane_set_widget(n->ncp, NULL, NULL) == 0){
829 ncplane_destroy(n->ncp);
830 }
831 free(n);
832 }
833}
const nccell * c
Definition egcpool.h:296
free(duplicated)
int r
Definition fbuf.h:226
#define logerror(fmt,...)
Definition logging.h:32
#define logwarn(fmt,...)
Definition logging.h:37
int ncmenu_rollup(ncmenu *n)
Definition menu.c:554
ncmenu * ncmenu_create(ncplane *n, const ncmenu_options *opts)
Definition menu.c:377
ncplane * ncmenu_plane(ncmenu *menu)
Definition menu.c:821
int ncmenu_nextsection(ncmenu *n)
Definition menu.c:563
int ncmenu_prevsection(ncmenu *n)
Definition menu.c:578
#define ALTMOD
const char * ncmenu_mouse_selected(const ncmenu *n, const ncinput *click, ncinput *ni)
Definition menu.c:673
int ncmenu_item_set_status(ncmenu *n, const char *section, const char *item, bool enabled)
Definition menu.c:784
int ncmenu_unroll(ncmenu *n, int sectionidx)
Definition menu.c:452
int ncmenu_previtem(ncmenu *n)
Definition menu.c:614
int ncmenu_nextitem(ncmenu *n)
Definition menu.c:593
void ncmenu_destroy(ncmenu *n)
Definition menu.c:825
const char * ncmenu_selected(const ncmenu *n, ncinput *ni)
Definition menu.c:635
#define CTLMOD
bool ncmenu_offer_input(ncmenu *n, const ncinput *nc)
Definition menu.c:707
#define NCKEY_SCROLL_UP
Definition nckeys.h:192
#define NCKEY_SCROLL_DOWN
Definition nckeys.h:193
#define NCKEY_UP
Definition nckeys.h:37
#define NCKEY_DOWN
Definition nckeys.h:39
#define NCKEY_RIGHT
Definition nckeys.h:38
#define NCKEY_ESC
Definition nckeys.h:196
#define NCKEY_BUTTON1
Definition nckeys.h:166
#define NCKEY_LEFT
Definition nckeys.h:40
int ncplane_cursor_move_yx(ncplane *n, int y, int x)
Definition notcurses.c:720
void * ncplane_userptr(ncplane *n)
Definition notcurses.c:192
void ncplane_set_channels(ncplane *n, uint64_t channels)
Definition notcurses.c:1494
void ncplane_set_styles(ncplane *n, unsigned stylebits)
Definition notcurses.c:2071
int ncplane_destroy(ncplane *ncp)
Definition notcurses.c:1018
int ncplane_putegc_yx(ncplane *n, int y, int x, const char *gclust, size_t *sbytes)
Definition notcurses.c:1995
const ncplane * ncplane_parent_const(const ncplane *n)
Definition notcurses.c:2655
int ncplane_at_yx_cell(ncplane *n, int y, int x, nccell *c)
Definition notcurses.c:270
int ncstrwidth(const char *egcs, int *validbytes, int *validwidth)
Definition notcurses.c:3309
bool ncplane_translate_abs(const ncplane *n, int *restrict y, int *restrict x)
Definition notcurses.c:2590
uint64_t ncplane_channels(const ncplane *n)
Definition notcurses.c:1490
ncplane * ncplane_create(ncplane *n, const ncplane_options *nopts)
Definition notcurses.c:707
void ncplane_erase(ncplane *n)
Definition notcurses.c:2458
int ncplane_putc_yx(ncplane *n, int y, int x, const nccell *c)
Definition notcurses.c:1980
int ncplane_set_base_cell(ncplane *ncp, const nccell *c)
Definition notcurses.c:1558
void ncplane_dim_yx(const ncplane *n, unsigned *rows, unsigned *cols)
Definition notcurses.c:301
int y
Definition notcurses.h:1905
#define NCMENU_OPTION_HIDING
Definition notcurses.h:4128
#define NCSTYLE_UNDERLINE
Definition notcurses.h:771
const struct ncplane_options * opts
Definition notcurses.h:3483
@ NCTYPE_RELEASE
Definition notcurses.h:1198
#define NCALPHA_TRANSPARENT
Definition notcurses.h:106
vopts n
Definition notcurses.h:3502
#define NCMENU_OPTION_BOTTOM
Definition notcurses.h:4127
#define NCSTYLE_BOLD
Definition notcurses.h:773
int int x
Definition notcurses.h:1905
#define NCCELL_TRIVIAL_INITIALIZER
Definition notcurses.h:737
#define NCCELL_INITIALIZER(c, s, chan)
Definition notcurses.h:731
#define NCPLANE_OPTION_FIXED
Definition notcurses.h:1454
void nccell_release(ncplane *n, nccell *c)
Definition render.c:128
ncintype_e evtype
Definition notcurses.h:1218
uint32_t id
Definition notcurses.h:1210
int shortdesccols
Definition menu.c:9
char * desc
Definition menu.c:5
bool disabled
Definition menu.c:10
ncinput shortcut
Definition menu.c:6
char * shortdesc
Definition menu.c:8
int shortcut_offset
Definition menu.c:7
ncinput shortcut
Definition menu.c:17
ncmenu_int_item * items
Definition menu.c:16
char * name
Definition menu.c:14
unsigned itemcount
Definition menu.c:15
int enabled_item_count
Definition menu.c:22
int shortcut_offset
Definition menu.c:21
ncinput shortcut
Definition notcurses.h:4117
const char * desc
Definition notcurses.h:4116
struct ncmenu_item * items
Definition notcurses.h:4123
Definition menu.c:25
int unrolledsection
Definition menu.c:29
uint64_t disablechannels
Definition menu.c:34
ncplane * ncp
Definition menu.c:26
uint64_t sectionchannels
Definition menu.c:33
ncmenu_int_section * sections
Definition menu.c:28
int headerwidth
Definition menu.c:30
int sectioncount
Definition menu.c:27
uint64_t headerchannels
Definition menu.c:31
bool bottom
Definition menu.c:35
uint64_t dissectchannels
Definition menu.c:32
const char * name
Definition notcurses.h:1470
return NULL
Definition termdesc.h:229