#include // http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/printw.html #include #include #define WIDTH 50 #define HEIGHT 20 int startx = 0; int starty = 0; /* COLOR_BLACK 0 COLOR_RED 1 COLOR_GREEN 2 COLOR_YELLOW 3 COLOR_BLUE 4 COLOR_MAGENTA 5 COLOR_CYAN 6 COLOR_WHITE 7 */ 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 everything */ startx = (80 - WIDTH) / 2; starty = (24 - HEIGHT) / 2; start_color(); /* Start color */ init_pair(1, COLOR_BLACK, COLOR_WHITE); init_pair(2, COLOR_WHITE, COLOR_BLUE); menu_win = newwin(HEIGHT, WIDTH, starty, startx); wbkgd(menu_win, COLOR_PAIR(2)); keypad(menu_win, TRUE); /* nodelay(menu_win,FALSE); notimeout(menu_win,FALSE); wtimeout(menu_win,-1); */ mvprintw(0, 0, "Use W/S/A/D to go up and down, Press enter to select a choice"); refresh(); print_menu(menu_win, highlight); while(1) { choice=0; int y=0; while(1) { c = wgetch(menu_win); mvprintw(y++,0, "%3d / '%c'", c, c); y%=24; refresh(); switch(c) { case 'w': case KEY_UP: if(highlight == 1) highlight = n_choices; else --highlight; break; case 's': case KEY_DOWN: if(highlight == n_choices) highlight = 1; else ++highlight; break; case 10: choice = highlight; break; } print_menu(menu_win, highlight); if(choice != 0) /* User did a choice come out of the infinite loop */ break; } mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); char *argv1[]={"xterm","/bin/fsh",0}; char *env1[]={"HOME=/home/miguel","PS1=\033[34m$\033[37m","PWD=/home/miguel","PATH=/bin","TERM=fool-term",0}; int pid=_fork(); if(pid==0) { _execve("/bin/xterm",argv1,env1); } } 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; i < n_choices; ++i) { if(highlight == i + 1) /* High light the present choice */ { wattron(menu_win, COLOR_PAIR(1)); mvwprintw(menu_win, y, x, ">> %40s <<", choices[i]); wattroff(menu_win, COLOR_PAIR(1)); } else mvwprintw(menu_win, y, x, " %40s ", choices[i]); ++y; } wrefresh(menu_win); }