1 #include <stdio.h>
2 #include <string.h>
3 #include <locale.h>
4 #include <stdlib.h>
5 #include <notcurses/notcurses.h>
6
7 static int
run_menu(struct notcurses * nc,struct ncmenu * ncm)8 run_menu(struct notcurses* nc, struct ncmenu* ncm){
9 ncplane_options nopts = {
10 .y = 10,
11 .x = NCALIGN_CENTER,
12 .rows = 3,
13 .cols = 40,
14 .flags = NCPLANE_OPTION_HORALIGNED,
15 };
16 struct ncplane* selplane = ncplane_create(notcurses_stdplane(nc), &nopts);
17 if(selplane == NULL){
18 return -1;
19 }
20 ncplane_set_fg_rgb(selplane, 0x0);
21 ncplane_set_bg_rgb(selplane, 0xdddddd);
22 uint64_t channels = 0;
23 ncchannels_set_fg_rgb(&channels, 0x000088);
24 ncchannels_set_bg_rgb(&channels, 0x88aa00);
25 if(ncplane_set_base(selplane, " ", 0, channels) < 0){
26 goto err;
27 }
28 uint32_t keypress;
29 ncinput ni;
30 notcurses_render(nc);
31 while((keypress = notcurses_get_blocking(nc, &ni)) != (uint32_t)-1){
32 if(!ncmenu_offer_input(ncm, &ni)){
33 if(ni.evtype == NCTYPE_RELEASE){
34 continue;
35 }else if(keypress == 'q'){
36 ncmenu_destroy(ncm);
37 ncplane_destroy(selplane);
38 return 0;
39 }else if(keypress == NCKEY_ENTER){ // selected a menu item
40 const char* sel;
41 if( (sel = ncmenu_selected(ncm, &ni)) ){
42 if(strcmp(sel, "Quit") == 0){
43 ncmenu_destroy(ncm);
44 ncplane_destroy(selplane);
45 return 0;
46 }
47 }
48 }
49 }
50 ncplane_erase(selplane);
51 ncinput selni;
52 const char* selitem = ncmenu_selected(ncm, &selni);
53 ncplane_putstr_aligned(selplane, 1, NCALIGN_CENTER, selitem ? selitem : "");
54 notcurses_render(nc);
55 }
56 ncmenu_destroy(ncm);
57 return 0;
58
59 err:
60 ncplane_destroy(selplane);
61 return -1;
62 }
63
main(void)64 int main(void){
65 notcurses_options opts = { };
66 struct notcurses* nc = notcurses_core_init(&opts, NULL);
67 if(nc == NULL){
68 return EXIT_FAILURE;
69 }
70 notcurses_mice_enable(nc, NCMICE_BUTTON_EVENT);
71 struct ncmenu_item demo_items[] = {
72 { .desc = "Restart", .shortcut = { .id = 'r', .ctrl = true, }, },
73 { .desc = "Disabled", .shortcut = { .id = 'd', .ctrl = false, }, },
74 };
75 struct ncmenu_item file_items[] = {
76 { .desc = "New", .shortcut = { .id = 'n', .ctrl = true, }, },
77 { .desc = "Open", .shortcut = { .id = 'o', .ctrl = true, }, },
78 { .desc = "Close", .shortcut = { .id = 'c', .ctrl = true, }, },
79 { .desc = NULL, },
80 { .desc = "Quit", .shortcut = { .id = 'q', }, },
81 };
82 struct ncmenu_item help_items[] = {
83 { .desc = "About", .shortcut = { .id = 'a', .ctrl = true, }, },
84 };
85 struct ncmenu_section sections[] = {
86 { .name = "Schwarzgerät", .items = demo_items,
87 .itemcount = sizeof(demo_items) / sizeof(*demo_items),
88 .shortcut = { .id = 0x00e4, .alt = true, }, },
89 { .name = "File", .items = file_items,
90 .itemcount = sizeof(file_items) / sizeof(*file_items),
91 .shortcut = { .id = 'f', .alt = true, }, },
92 { .name = NULL, .items = NULL, .itemcount = 0, },
93 { .name = "Help", .items = help_items,
94 .itemcount = sizeof(help_items) / sizeof(*help_items),
95 .shortcut = { .id = 'h', .alt = true, }, },
96 };
97 ncmenu_options mopts;
98 memset(&mopts, 0, sizeof(mopts));
99 mopts.sections = sections;
100 mopts.sectioncount = sizeof(sections) / sizeof(*sections);
101 ncchannels_set_fg_rgb(&mopts.headerchannels, 0x00ff00);
102 ncchannels_set_bg_rgb(&mopts.headerchannels, 0x440000);
103 ncchannels_set_fg_rgb(&mopts.sectionchannels, 0xb0d700);
104 ncchannels_set_bg_rgb(&mopts.sectionchannels, 0x002000);
105 unsigned dimy, dimx;
106 struct ncplane* n = notcurses_stddim_yx(nc, &dimy, &dimx);
107 struct ncmenu* top = ncmenu_create(n, &mopts);
108 if(top == NULL){
109 goto err;
110 }
111 if(ncmenu_item_set_status(top, "Schwarzgerät", "Disabled", false)){
112 goto err;
113 }
114 if(ncmenu_item_set_status(top, "Schwarzgerät", "Restart", false)){
115 goto err;
116 }
117 uint64_t channels = 0;
118 ncchannels_set_fg_rgb(&channels, 0x88aa00);
119 ncchannels_set_bg_rgb(&channels, 0x000088);
120 if(ncplane_set_base(n, "x", 0, channels) < 0){
121 return EXIT_FAILURE;
122 }
123
124 notcurses_render(nc);
125 ncplane_set_fg_rgb(n, 0x00dddd);
126 if(ncplane_putstr_aligned(n, dimy - 1, NCALIGN_RIGHT, " -=+ menu poc. press q to exit +=- ") < 0){
127 return EXIT_FAILURE;
128 }
129 run_menu(nc, top);
130
131 ncplane_erase(n);
132
133 mopts.flags |= NCMENU_OPTION_BOTTOM;
134 struct ncmenu* bottom = ncmenu_create(n, &mopts);
135 if(bottom == NULL){
136 goto err;
137 }
138 if(ncplane_putstr_aligned(n, 0, NCALIGN_RIGHT, " -=+ menu poc. press q to exit +=- ") < 0){
139 return EXIT_FAILURE;
140 }
141 run_menu(nc, bottom);
142
143 if(notcurses_stop(nc)){
144 return EXIT_FAILURE;
145 }
146 return EXIT_SUCCESS;
147
148 err:
149 notcurses_stop(nc);
150 return EXIT_FAILURE;
151 }
152