Merge remote-tracking (subtree) branch 'PracticingCurses/master'
This commit is contained in:
31
PracticingCurses/menus/Makefile
Normal file
31
PracticingCurses/menus/Makefile
Normal file
@@ -0,0 +1,31 @@
|
||||
# Makefile for JustForFun Files
|
||||
|
||||
# A few variables
|
||||
|
||||
CC=gcc
|
||||
LIBS=-lmenu -lncurses
|
||||
|
||||
SRC_DIR=.
|
||||
EXE_DIR=../demo/exe
|
||||
|
||||
EXES = \
|
||||
${EXE_DIR}/menu_attrib\
|
||||
${EXE_DIR}/menu_item_data\
|
||||
${EXE_DIR}/menu_multi_column \
|
||||
${EXE_DIR}/menu_scroll \
|
||||
${EXE_DIR}/menu_simple \
|
||||
${EXE_DIR}/menu_toggle \
|
||||
${EXE_DIR}/menu_userptr \
|
||||
${EXE_DIR}/menu_win
|
||||
|
||||
${EXE_DIR}/%: %.o
|
||||
${CC} -o $@ $< ${LIBS}
|
||||
|
||||
%.o: ${SRC_DIR}/%.c
|
||||
${CC} -o $@ -c $<
|
||||
|
||||
all: ${EXES}
|
||||
|
||||
|
||||
clean:
|
||||
@rm -f ${EXES}
|
13
PracticingCurses/menus/README
Normal file
13
PracticingCurses/menus/README
Normal file
@@ -0,0 +1,13 @@
|
||||
Description of files
|
||||
--------------------
|
||||
menus
|
||||
|
|
||||
|----> menu_attrib.c -- Usage of menu attributes
|
||||
|----> menu_item_data.c -- Usage of item_name() etc.. functions
|
||||
|----> menu_multi_column.c -- Creates multi columnar menus
|
||||
|----> menu_scroll.c -- Demonstrates scrolling capability of menus
|
||||
|----> menu_simple.c -- A simple menu accessed by arrow keys
|
||||
|----> menu_toggle.c -- Creates multi valued menus and explains
|
||||
| -- REQ_TOGGLE_ITEM
|
||||
|----> menu_userptr.c -- Usage of user pointer
|
||||
|----> menu_win.c -- Demo of windows associated with menus
|
80
PracticingCurses/menus/menu_attrib.c
Normal file
80
PracticingCurses/menus/menu_attrib.c
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Choice 5",
|
||||
"Choice 6",
|
||||
"Choice 7",
|
||||
"Exit",
|
||||
};
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
int n_choices, i;
|
||||
ITEM *cur_item;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(2, COLOR_GREEN, COLOR_BLACK);
|
||||
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
|
||||
|
||||
/* Initialize items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
my_items[i] = new_item(choices[i], choices[i]);
|
||||
my_items[n_choices] = (ITEM *)NULL;
|
||||
item_opts_off(my_items[3], O_SELECTABLE);
|
||||
item_opts_off(my_items[6], O_SELECTABLE);
|
||||
|
||||
/* Create menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
|
||||
/* Set fore ground and back ground of the menu */
|
||||
set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
|
||||
set_menu_back(my_menu, COLOR_PAIR(2));
|
||||
set_menu_grey(my_menu, COLOR_PAIR(3));
|
||||
|
||||
/* Post the menu */
|
||||
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
|
||||
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
|
||||
post_menu(my_menu);
|
||||
refresh();
|
||||
|
||||
while((c = getch()) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case 10: /* Enter */
|
||||
move(20, 0);
|
||||
clrtoeol();
|
||||
mvprintw(20, 0, "Item selected is : %s",
|
||||
item_name(current_item(my_menu)));
|
||||
pos_menu_cursor(my_menu);
|
||||
break;
|
||||
}
|
||||
}
|
||||
unpost_menu(my_menu);
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
free_menu(my_menu);
|
||||
endwin();
|
||||
}
|
||||
|
66
PracticingCurses/menus/menu_item_data.c
Normal file
66
PracticingCurses/menus/menu_item_data.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Exit",
|
||||
};
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
int n_choices, i;
|
||||
ITEM *cur_item;
|
||||
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
|
||||
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
my_items[i] = new_item(choices[i], choices[i]);
|
||||
my_items[n_choices] = (ITEM *)NULL;
|
||||
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
post_menu(my_menu);
|
||||
refresh();
|
||||
|
||||
while((c = getch()) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case 10: /* Enter */
|
||||
cur_item = current_item(my_menu);
|
||||
move(LINES - 2, 0);
|
||||
clrtoeol();
|
||||
mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s",
|
||||
item_index(cur_item) + 1, item_name(cur_item),
|
||||
item_description(cur_item));
|
||||
|
||||
refresh();
|
||||
pos_menu_cursor(my_menu);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free_item(my_items[0]);
|
||||
free_item(my_items[1]);
|
||||
free_menu(my_menu);
|
||||
endwin();
|
||||
}
|
||||
|
97
PracticingCurses/menus/menu_multi_column.c
Normal file
97
PracticingCurses/menus/menu_multi_column.c
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
|
||||
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
|
||||
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
|
||||
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
|
||||
"Exit",
|
||||
(char *)NULL,
|
||||
};
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
WINDOW *my_menu_win;
|
||||
int n_choices, i;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
||||
|
||||
/* Create items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
my_items[i] = new_item(choices[i], choices[i]);
|
||||
|
||||
/* Crate menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
|
||||
/* Set menu option not to show the description */
|
||||
menu_opts_off(my_menu, O_SHOWDESC);
|
||||
|
||||
/* Create the window to be associated with the menu */
|
||||
my_menu_win = newwin(10, 70, 4, 4);
|
||||
keypad(my_menu_win, TRUE);
|
||||
|
||||
/* Set main window and sub window */
|
||||
set_menu_win(my_menu, my_menu_win);
|
||||
set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
|
||||
set_menu_format(my_menu, 5, 3);
|
||||
set_menu_mark(my_menu, " * ");
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(my_menu_win, 0, 0);
|
||||
|
||||
attron(COLOR_PAIR(2));
|
||||
mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
|
||||
mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
|
||||
attroff(COLOR_PAIR(2));
|
||||
refresh();
|
||||
|
||||
/* Post the menu */
|
||||
post_menu(my_menu);
|
||||
wrefresh(my_menu_win);
|
||||
|
||||
while((c = wgetch(my_menu_win)) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
menu_driver(my_menu, REQ_LEFT_ITEM);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
menu_driver(my_menu, REQ_RIGHT_ITEM);
|
||||
break;
|
||||
case KEY_NPAGE:
|
||||
menu_driver(my_menu, REQ_SCR_DPAGE);
|
||||
break;
|
||||
case KEY_PPAGE:
|
||||
menu_driver(my_menu, REQ_SCR_UPAGE);
|
||||
break;
|
||||
}
|
||||
wrefresh(my_menu_win);
|
||||
}
|
||||
|
||||
/* Unpost and free all the memory taken up */
|
||||
unpost_menu(my_menu);
|
||||
free_menu(my_menu);
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
endwin();
|
||||
}
|
124
PracticingCurses/menus/menu_scroll.c
Normal file
124
PracticingCurses/menus/menu_scroll.c
Normal file
@@ -0,0 +1,124 @@
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Choice 5",
|
||||
"Choice 6",
|
||||
"Choice 7",
|
||||
"Choice 8",
|
||||
"Choice 9",
|
||||
"Choice 10",
|
||||
"Exit",
|
||||
(char *)NULL,
|
||||
};
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
WINDOW *my_menu_win;
|
||||
int n_choices, i;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
||||
|
||||
/* Create items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
my_items[i] = new_item(choices[i], choices[i]);
|
||||
|
||||
/* Crate menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
|
||||
/* Create the window to be associated with the menu */
|
||||
my_menu_win = newwin(10, 40, 4, 4);
|
||||
keypad(my_menu_win, TRUE);
|
||||
|
||||
/* Set main window and sub window */
|
||||
set_menu_win(my_menu, my_menu_win);
|
||||
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
|
||||
set_menu_format(my_menu, 5, 1);
|
||||
|
||||
/* Set menu mark to the string " * " */
|
||||
set_menu_mark(my_menu, " * ");
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(my_menu_win, 0, 0);
|
||||
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
|
||||
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
|
||||
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
|
||||
|
||||
/* Post the menu */
|
||||
post_menu(my_menu);
|
||||
wrefresh(my_menu_win);
|
||||
|
||||
attron(COLOR_PAIR(2));
|
||||
mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items");
|
||||
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
|
||||
attroff(COLOR_PAIR(2));
|
||||
refresh();
|
||||
|
||||
while((c = wgetch(my_menu_win)) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case KEY_NPAGE:
|
||||
menu_driver(my_menu, REQ_SCR_DPAGE);
|
||||
break;
|
||||
case KEY_PPAGE:
|
||||
menu_driver(my_menu, REQ_SCR_UPAGE);
|
||||
break;
|
||||
}
|
||||
wrefresh(my_menu_win);
|
||||
}
|
||||
|
||||
/* Unpost and free all the memory taken up */
|
||||
unpost_menu(my_menu);
|
||||
free_menu(my_menu);
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
endwin();
|
||||
}
|
||||
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
|
||||
{ int length, x, y;
|
||||
float temp;
|
||||
|
||||
if(win == NULL)
|
||||
win = stdscr;
|
||||
getyx(win, y, x);
|
||||
if(startx != 0)
|
||||
x = startx;
|
||||
if(starty != 0)
|
||||
y = starty;
|
||||
if(width == 0)
|
||||
width = 80;
|
||||
|
||||
length = strlen(string);
|
||||
temp = (width - length)/ 2;
|
||||
x = startx + (int)temp;
|
||||
wattron(win, color);
|
||||
mvwprintw(win, y, x, "%s", string);
|
||||
wattroff(win, color);
|
||||
refresh();
|
||||
}
|
56
PracticingCurses/menus/menu_simple.c
Normal file
56
PracticingCurses/menus/menu_simple.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Exit",
|
||||
};
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
int n_choices, i;
|
||||
ITEM *cur_item;
|
||||
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
|
||||
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
my_items[i] = new_item(choices[i], choices[i]);
|
||||
my_items[n_choices] = (ITEM *)NULL;
|
||||
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
mvprintw(LINES - 2, 0, "F1 to Exit");
|
||||
post_menu(my_menu);
|
||||
refresh();
|
||||
|
||||
while((c = getch()) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free_item(my_items[0]);
|
||||
free_item(my_items[1]);
|
||||
free_menu(my_menu);
|
||||
endwin();
|
||||
}
|
||||
|
84
PracticingCurses/menus/menu_toggle.c
Normal file
84
PracticingCurses/menus/menu_toggle.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Choice 5",
|
||||
"Choice 6",
|
||||
"Choice 7",
|
||||
"Exit",
|
||||
};
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
int n_choices, i;
|
||||
ITEM *cur_item;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
/* Initialize items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
my_items[i] = new_item(choices[i], choices[i]);
|
||||
my_items[n_choices] = (ITEM *)NULL;
|
||||
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
|
||||
/* Make the menu multi valued */
|
||||
menu_opts_off(my_menu, O_ONEVALUE);
|
||||
|
||||
mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item.");
|
||||
mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)");
|
||||
post_menu(my_menu);
|
||||
refresh();
|
||||
|
||||
while((c = getch()) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case ' ':
|
||||
menu_driver(my_menu, REQ_TOGGLE_ITEM);
|
||||
break;
|
||||
case 10: /* Enter */
|
||||
{ char temp[200];
|
||||
ITEM **items;
|
||||
|
||||
items = menu_items(my_menu);
|
||||
temp[0] = '\0';
|
||||
for(i = 0; i < item_count(my_menu); ++i)
|
||||
if(item_value(items[i]) == TRUE)
|
||||
{ strcat(temp, item_name(items[i]));
|
||||
strcat(temp, " ");
|
||||
}
|
||||
move(20, 0);
|
||||
clrtoeol();
|
||||
mvprintw(20, 0, temp);
|
||||
refresh();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free_item(my_items[0]);
|
||||
free_item(my_items[1]);
|
||||
free_menu(my_menu);
|
||||
endwin();
|
||||
}
|
||||
|
87
PracticingCurses/menus/menu_userptr.c
Normal file
87
PracticingCurses/menus/menu_userptr.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Choice 5",
|
||||
"Choice 6",
|
||||
"Choice 7",
|
||||
"Exit",
|
||||
};
|
||||
void func(char *name);
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
int n_choices, i;
|
||||
ITEM *cur_item;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(2, COLOR_GREEN, COLOR_BLACK);
|
||||
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
|
||||
|
||||
/* Initialize items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
{ my_items[i] = new_item(choices[i], choices[i]);
|
||||
/* Set the user pointer */
|
||||
set_item_userptr(my_items[i], func);
|
||||
}
|
||||
my_items[n_choices] = (ITEM *)NULL;
|
||||
|
||||
/* Create menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
|
||||
/* Post the menu */
|
||||
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
|
||||
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
|
||||
post_menu(my_menu);
|
||||
refresh();
|
||||
|
||||
while((c = getch()) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case 10: /* Enter */
|
||||
{ ITEM *cur;
|
||||
void (*p)(char *);
|
||||
|
||||
cur = current_item(my_menu);
|
||||
p = item_userptr(cur);
|
||||
p((char *)item_name(cur));
|
||||
pos_menu_cursor(my_menu);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
unpost_menu(my_menu);
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
free_menu(my_menu);
|
||||
endwin();
|
||||
}
|
||||
|
||||
void func(char *name)
|
||||
{ move(20, 0);
|
||||
clrtoeol();
|
||||
mvprintw(20, 0, "Item selected is : %s", name);
|
||||
}
|
105
PracticingCurses/menus/menu_win.c
Normal file
105
PracticingCurses/menus/menu_win.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <menu.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define CTRLD 4
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Exit",
|
||||
(char *)NULL,
|
||||
};
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
|
||||
|
||||
int main()
|
||||
{ ITEM **my_items;
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
WINDOW *my_menu_win;
|
||||
int n_choices, i;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
|
||||
/* Create items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
my_items[i] = new_item(choices[i], choices[i]);
|
||||
|
||||
/* Crate menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
|
||||
/* Create the window to be associated with the menu */
|
||||
my_menu_win = newwin(10, 40, 4, 4);
|
||||
keypad(my_menu_win, TRUE);
|
||||
|
||||
/* Set main window and sub window */
|
||||
set_menu_win(my_menu, my_menu_win);
|
||||
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
|
||||
|
||||
/* Set menu mark to the string " * " */
|
||||
set_menu_mark(my_menu, " * ");
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(my_menu_win, 0, 0);
|
||||
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
|
||||
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
|
||||
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
|
||||
mvprintw(LINES - 2, 0, "F1 to exit");
|
||||
refresh();
|
||||
|
||||
/* Post the menu */
|
||||
post_menu(my_menu);
|
||||
wrefresh(my_menu_win);
|
||||
|
||||
while((c = wgetch(my_menu_win)) != KEY_F(1))
|
||||
{ switch(c)
|
||||
{ case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
}
|
||||
wrefresh(my_menu_win);
|
||||
}
|
||||
|
||||
/* Unpost and free all the memory taken up */
|
||||
unpost_menu(my_menu);
|
||||
free_menu(my_menu);
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
endwin();
|
||||
}
|
||||
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
|
||||
{ int length, x, y;
|
||||
float temp;
|
||||
|
||||
if(win == NULL)
|
||||
win = stdscr;
|
||||
getyx(win, y, x);
|
||||
if(startx != 0)
|
||||
x = startx;
|
||||
if(starty != 0)
|
||||
y = starty;
|
||||
if(width == 0)
|
||||
width = 80;
|
||||
|
||||
length = strlen(string);
|
||||
temp = (width - length)/ 2;
|
||||
x = startx + (int)temp;
|
||||
wattron(win, color);
|
||||
mvwprintw(win, y, x, "%s", string);
|
||||
wattroff(win, color);
|
||||
refresh();
|
||||
}
|
Reference in New Issue
Block a user