NCURSES - How to identify which selection was made in C -
i wanted make simple gui ncurses. i'm using c, , mac os x os. have code make gui, "buttons" , highlight selection, can moved arrow keys. here's code:
#include <stdio.h> #include <ncurses.h> #define width 30 #define height 10 int startx = 0; int starty = 0; char *choices[] = { "choice 1", "choice 2", "choice 3", "choice 4", "exit", }; int n_choices = sizeof(choices) / sizeof(char *); void print_menu(window *menu_win, int highlight); int main() { window *menu_win; int highlight = 1; int choice = 0; int c; initscr(); clear(); noecho(); cbreak(); /* line buffering disabled. pass on */ startx = (80 - width) / 2; starty = (24 - height) / 2; menu_win = newwin(height, width, starty, startx); keypad(menu_win, true); mvprintw(0, 0, "use arrow keys go , down, press enter select choice"); refresh(); print_menu(menu_win, highlight); while(1) { c = wgetch(menu_win); switch(c) { case key_up: if(highlight == 1) highlight = n_choices; else --highlight; break; case key_down: if(highlight == n_choices) highlight = 1; else ++highlight; break; case 10: choice = highlight; break; default: mvprintw(24, 0, "charcter pressed = %3d can printed '%c'", c, c); refresh(); break; } print_menu(menu_win, highlight); if(choice != 0) /* user did choice come out of infinite loop */ break; } mvprintw(23, 0, "you chose choice %d choice string %s\n", choice, choices[choice - 1]); clrtoeol(); refresh(); endwin(); return 0; } void print_menu(window *menu_win, int highlight) { int x, y, i; x = 2; y = 2; box(menu_win, 0, 0); for(i = 0; < n_choices; ++i) { if(highlight == + 1) /* high light present choice */ { wattron(menu_win, a_reverse); mvwprintw(menu_win, y, x, "%s", choices[i]); wattroff(menu_win, a_reverse); } else mvwprintw(menu_win, y, x, "%s", choices[i]); ++y; } wrefresh(menu_win); }
ok, code works perfectly. question how can identify label has user selected? mean, how can know if presses "choice 1" or "choice 2", etc.?
i know it's pretty dumb question, i'm new library , can't figure out. reading, hope can me out!
Comments
Post a Comment