Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
|
||
#include <stdio.h> | ||
#include <ncurses.h> | ||
|
||
#define WIDTH 30 | ||
#define HEIGHT 10 | ||
|
||
int startx = 0; | ||
int starty = 0; | ||
|
||
char *choices[] = { | ||
"Ehab the java Programmer", | ||
"Ehab the Developer", | ||
"Ehab The Senior Developer", | ||
"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; | ||
|
||
menu_win = newwin(HEIGHT, WIDTH, starty, startx); | ||
keypad(menu_win, TRUE); | ||
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a 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 is = %3d Hopefully it can be printed as '%c'", c, c); | ||
refresh(); | ||
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]); | ||
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, 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); | ||
} |