161 changes: 161 additions & 0 deletions JustForFun/magic.c
@@ -0,0 +1,161 @@
#include <curses.h>
#include <stdlib.h>

#define STARTX 9
#define STARTY 3
#define WIDTH 6
#define HEIGHT 4

#define TRACE_VALUE TRACE_MAXIMUM

void board( WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
void magic(int **, int);
void print(int **, int);
void magic_board(int **a,int n);

int main(int argc, char *argv[])
{

int **a,n,i;

if(argc != 2)
{ printf("Usage: %s <magic sqaure order>\n", argv[0]);
exit(0);
}
n = atoi(argv[1]);
if(n % 2 == 0)
{ printf("Sorry !!! I don't know how to create magic square of even order\n");
printf("The order should be an odd number\n");
exit(0);
}
a = (int **) malloc(n * sizeof(int*));
for(i = 0;i < n;++i)
a[i] = (int *)malloc(n * sizeof(int));

magic(a,n);

initscr();
curs_set(0);
noecho();
magic_board(a,n);
getch();
endwin();

return;
}

void magic(int **a, int n)
{
int i,j,k;
int row,col;
for(i = 0;i < n;++i)
for(j = 0;j < n;++j)
a[i][j] = -1;
row = 0;
col = n / 2;

k = 1;
a[row][col] = k;

while(k != n * n)
{
if(row == 0 && col != n - 1)
{ row = n - 1;
col ++;
a[row][col] = ++k;
}
else if(row != 0 && col != n - 1)
{ if(a[row - 1][col + 1] == -1)
{ row --;
col ++;
a[row][col] = ++k;
}
else
{
row ++;
a[row][col] = ++k;
}
}
else if(row != 0 && col == n - 1)
{
row --;
col = 0;
a[row][col] = ++k;
}
else if(row == 0 && col == n - 1)
{ row ++;
a[row][col] = ++k;
}

}
return;
}

void print(int **a,int n)
{ int i,j;
int x,y;
x = STARTX;
y = STARTY;
mvprintw(1,30,"MAGIC SQUARE");
for(i = 0;i < n;++i)
{ for(j = 0;j < n;++j)
{ mvprintw(y,x,"%d",a[i][j]);
if(n > 9)
x += 4;
else
x += 6;
}
x = STARTX;
if(n > 7)
y += 2;
else
y += 3;
}
refresh();
}
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;

endy = starty + lines * tile_height;
endx = startx + cols * tile_width;

for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}

void magic_board(int **a,int n)
{ int i,j, deltax, deltay;
int startx, starty;

starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2;
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2;
deltax = WIDTH / 2;
for(i = 0;i < n; ++i)
for(j = 0; j < n; ++j)
mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax,
"%d", a[i][j]);
}
122 changes: 122 additions & 0 deletions JustForFun/queens.c
@@ -0,0 +1,122 @@
#include <stdio.h>
#include <curses.h>

#define QUEEN_CHAR '*'

int *nqueens(int num);
int place(int current, int *position);
int print(int *positions, int num_queens);
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);

int main(int argc, char *argv[])
{
int num_queens, *positions, count;

if(argc != 2)
{ printf("Usage: %s <number of queens (chess board order)>\n", argv[0]);
exit(1);
}

num_queens = atoi(argv[1]);
initscr();
cbreak();
keypad(stdscr, TRUE);
positions = nqueens(num_queens);
free(positions);
endwin();
return 0;
}

int *nqueens(int num)
{
int current, *position, num_solutions = 0;

position = (int *) calloc(num + 1, sizeof(int));

position[1] = 0;
current = 1; /* current queen is being checked */
/* position[current] is the coloumn*/
while(current > 0){
position[current] += 1;
while(position[current] <= num && !place(current, position) )
position[current] += 1;
if(position[current] <= num){
if(current == num) {
++num_solutions;
print(position, num);
}
else {
current += 1;
position[current] = 0;
}
}
else current -= 1; /* backtrack */
}
printf("Total Number of Solutions : %d\n", num_solutions);
return(position);
}

int place(int current, int *position)
{
int i;
if(current == 1) return(1);
for(i = 1; i < current; ++i)
if(position[i] == position[current]) return(0);
else if(abs(position[i] - position[current]) ==
abs(i - current))
return(0);

return(1);
}

int print(int *positions, int num_queens)
{ int count;
int y = 2, x = 2, w = 4, h = 2;
static int solution = 1;

mvprintw(0, 0, "Solution No: %d", solution++);
board(stdscr, y, x, num_queens, num_queens, w, h);
for(count = 1; count <= num_queens; ++count)
{ int tempy = y + (count - 1) * h + h / 2;
int tempx = x + (positions[count] - 1) * w + w / 2;
mvaddch(tempy, tempx, QUEEN_CHAR);
}
refresh();
mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)");
if(getch() == KEY_F(1))
{ endwin();
exit(0);
}
clear();
}

void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;

endy = starty + lines * tile_height;
endx = startx + cols * tile_width;

for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}
205 changes: 205 additions & 0 deletions JustForFun/shuffle.c
@@ -0,0 +1,205 @@
#include <curses.h>

#define STARTX 9
#define STARTY 3
#define WIDTH 6
#define HEIGHT 4

#define BLANK 0

typedef struct _tile {
int x;
int y;
}tile;

void init_board(int **board, int n, tile *blank);
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
void shuffle_board(int **board, int n);
void move_blank(int direction, int **s_board, int n, tile *blank);
int check_win(int **s_board, int n, tile *blank);

enum { LEFT, RIGHT, UP, DOWN };

int main(int argc, char *argv[])
{ int **s_board;
int n, i, ch;
tile blank;

if(argc != 2)
{ printf("Usage: %s <shuffle board order>\n", argv[0]);
exit(1);
}
n = atoi(argv[1]);

s_board = (int **)calloc(n, sizeof(int *));
for(i = 0;i < n; ++i)
s_board[i] = (int *)calloc(n, sizeof(int));
init_board(s_board, n, &blank);
initscr();
keypad(stdscr, TRUE);
cbreak();
shuffle_board(s_board, n);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
move_blank(RIGHT, s_board, n, &blank);
break;
case KEY_RIGHT:
move_blank(LEFT, s_board, n, &blank);
break;
case KEY_UP:
move_blank(DOWN, s_board, n, &blank);
break;
case KEY_DOWN:
move_blank(UP, s_board, n, &blank);
break;
}
shuffle_board(s_board, n);
if(check_win(s_board, n, &blank) == TRUE)
{ mvprintw(24, 0, "You Win !!!\n");
refresh();
break;
}
}
endwin();
return 0;
}

void move_blank(int direction, int **s_board, int n, tile *blank)
{ int temp;

switch(direction)
{ case LEFT:
{ if(blank->x != 0)
{ --blank->x;
temp = s_board[blank->x][blank->y];
s_board[blank->x + 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case RIGHT:
{ if(blank->x != n - 1)
{ ++blank->x;
temp = s_board[blank->x][blank->y];
s_board[blank->x - 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case UP:
{ if(blank->y != 0)
{ --blank->y;
temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y + 1] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case DOWN:
{ if(blank->y != n - 1)
{ ++blank->y;
temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y - 1] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
}
}

int check_win(int **s_board, int n, tile *blank)
{ int i, j;

s_board[blank->x][blank->y] = n * n;
for(i = 0;i < n; ++i)
for(j = 0;j < n; ++j)
if(s_board[i][j] != j * n + i + 1)
{ s_board[blank->x][blank->y] = BLANK;
return FALSE;
}

s_board[blank->x][blank->y] = BLANK;
return TRUE;
}

void init_board(int **s_board, int n, tile *blank)
{ int i, j, k;
int *temp_board;

temp_board = (int *)calloc(n * n, sizeof(int));
srand(time(NULL));
for(i = 0;i < n * n; ++i)
{
repeat :
k = rand() % (n * n);
for(j = 0;j <= i - 1; ++j)
if (k == temp_board[j])
goto repeat;
else
temp_board[i] = k;
}
k = 0;
for (i = 0;i < n;++i)
for(j = 0;j < n; ++j,++k)
{ if(temp_board[k] == 0)
{ blank->x = i;
blank->y = j;
}
s_board[i][j] = temp_board[k];
}
free(temp_board);
}

void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;

endy = starty + lines * tile_height;
endx = startx + cols * tile_width;

for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}

void shuffle_board(int **s_board, int n)
{ int i,j, deltax, deltay;
int startx, starty;

starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2;
clear();
mvprintw(24, 0, "Press F1 to Exit");
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2;
deltax = WIDTH / 2;
for(j = 0; j < n; ++j)
for(i = 0;i < n; ++i)
if(s_board[i][j] != BLANK)
mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax,
"%-2d", s_board[i][j]);
refresh();
}


223 changes: 223 additions & 0 deletions JustForFun/tt.c
@@ -0,0 +1,223 @@
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define HSIZE 60
#define LENGTH 75
#define WIDTH 10
#define STARTX 1
#define STARTY 5
#define STATUSX 1
#define STATUSY 25

#define KEY_F1 265

int print_menu();
void print_byebye();
void create_test_string();
void print_time(time_t startt, time_t endt, int mistakes);
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win);

char *groups[] = { "`123456" ,
"7890-=" ,
"~!@#$%^" ,
"&*()_+" ,
"<>?" ,
",./\\" ,
"asdfg",
"jkl;'",
"qwer",
"uiop",
"tyur",
"zxcv",
"bnm",
};
int n_groups;

int main()
{ int choice, i;
char *test_array;
int ch = KEY_F1;
int mistakes;
int x, y;
time_t start_t, end_t;
WINDOW *typing_win;
char string[80];

string[0] = '\0';

initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
intrflush(stdscr, FALSE);

srandom(time(NULL));
n_groups = sizeof(groups) / sizeof(char *);
test_array = (char *)calloc(HSIZE + 1, sizeof(char));

while(1)
{
if(ch == KEY_F1)
{ choice = print_menu();
choice -= 1;
if(choice == n_groups)
{ print_byebye();
free(test_array);
endwin();
exit(0);
}
}
clear();
strcpy(string, "Typing window");
print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL);
attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu");
refresh();
attroff(A_REVERSE);

create_test_string(test_array, choice);
typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX);
keypad(typing_win, TRUE);
intrflush(typing_win, FALSE);
box(typing_win, 0, 0);

x = 1;
y = 1;
mvwprintw(typing_win, y, x, "%s", test_array);
wrefresh(typing_win);
y += 1;

mistakes = 0;
i = 0;
time(&start_t);
wmove(typing_win, y, x);
wrefresh(typing_win);
ch = 0;
while(ch != KEY_F1 && i != HSIZE + 1)
{ ch = wgetch(typing_win);
mvwprintw(typing_win, y, x, "%c", ch);
wrefresh(typing_win);
++x;
if(ch == test_array[i])
{ ++i;
continue;
}
else
{ ++mistakes;
++i;
}
}

time(&end_t);
print_time(start_t, end_t, mistakes);
}
free(test_array);
endwin();
return 0;
}


int print_menu()
{ int choice, i;

choice = 0;
while(1)
{ clear();
printw("\n\n");
print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL);
printw("\n\n\n");
for(i = 0;i <= n_groups - 1; ++i)
printw("\t%3d: \tPractice %s\n", i + 1, groups[i]);
printw("\t%3d: \tExit\n", i + 1);

printw("\n\n\tChoice: ");
refresh();
echo();
scanw("%d", &choice);
noecho();

if(choice >= 1 && choice <= n_groups + 1)
break;
else
{ attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue");
attroff(A_REVERSE);
getch();
}
}
return choice;
}

void create_test_string(char *test_array, int choice)
{ int i, index, length;

length = strlen(groups[choice]);
for(i = 0;i <= HSIZE - 1; ++i)
{ if(i%5 == 0)
test_array[i] = ' ';
else
{ index = (int)(random() % length);
test_array[i] = groups[choice][index];
}
}
test_array[i] = '\0';
}

void print_byebye()
{ printw("\n");
print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL);
print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL);
refresh();
}

void print_time(time_t start_t, time_t end_t, int mistakes)
{ long int diff;
int h,m,s;
float wpm;

diff = end_t - start_t;
wpm = ((HSIZE / 5)/(double)diff)*60;

h = (int)(diff / 3600);
diff -= h * 3600;
m = (int)(diff / 60);
diff -= m * 60;
s = (int)diff;

attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm);
attroff(A_REVERSE);

refresh();
getch();

}

/* ---------------------------------------------------------------- *
* startx = 0 means at present x *
* starty = 0 means at present y *
* win = NULL means take stdscr *
* ---------------------------------------------------------------- */

void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win)
{ 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;
mvwprintw(win, y, x, "%s", string);
refresh();
}
21 changes: 21 additions & 0 deletions Makefile
@@ -0,0 +1,21 @@
# The top level Makefile

all:
cd JustForFun && $(MAKE)
cd basics && $(MAKE)
cd forms && $(MAKE)
cd menus && $(MAKE)
cd panels && $(MAKE)
@echo
@echo "*********************************************"
@echo "All files Built"
@echo "Please move to demo/exe directory"
@echo "Execute each file to see examples in action"
@echo "*********************************************"
@echo
clean:
cd JustForFun && $(MAKE) clean
cd basics && $(MAKE) clean
cd forms && $(MAKE) clean
cd menus && $(MAKE) clean
cd panels && $(MAKE) clean
87 changes: 87 additions & 0 deletions README
@@ -0,0 +1,87 @@
This is the top level README file.
This directory is structured as follows:

ncurses
|
|----> JustForFun -- just for fun programs
|----> basics -- basic programs
|----> demo -- output files go into this directory after make
| |
| |----> exe -- exe files of all example programs
|----> forms -- programs related to form library
|----> menus -- programs related to menus library
|----> panels -- programs related to panels library
|----> perl -- perl equivalents of the examples (contributed
| by Anuradha Ratnaweera)
|----> Makefile -- the top level Makefile
|----> README -- the top level README file. contains instructions
| -- to create executables for example programs

To compile and install all example programs, just run make in this directory.

make

It installs all the exe files in demo/exe directory. You can go to that direcory
and see the examples in action.

Have Fun !!!
-- Pradeep Padala

Description of files in each directory
--------------------------------------
JustForFun
|
|----> hanoi.c -- The Towers of Hanoi Solver
|----> life.c -- The Game of Life demo
|----> magic.c -- An Odd Order Magic Square builder
|----> queens.c -- The famous N-Queens Solver
|----> shuffle.c -- A fun game, if you have time to kill
|----> tt.c -- A very trivial typing tutor

basics
|
|----> acs_vars.c -- ACS_ variables example
|----> hello_world.c -- Simple "Hello World" Program
|----> init_func_example.c -- Initialization functions example
|----> key_code.c -- Shows the scan code of the key pressed
|----> mouse_menu.c -- A menu accessible by mouse
|----> other_border.c -- Shows usage of other border functions apart
| -- box()
|----> printw_example.c -- A very simple printw() example
|----> scanw_example.c -- A very simple getstr() example
|----> simple_attr.c -- A program that can print a c file with comments
| -- in attribute
|----> simple_color.c -- A simple example demonstrating colors
|----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows
|----> temp_leave.c -- Demonstrates temporarily leaving curses mode
|----> win_border.c -- Shows Creation of windows and borders
|----> with_chgat.c -- chgat() usage example

forms
|
|----> form_attrib.c -- Usage of field attributes
|----> form_options.c -- Usage of field options
|----> form_simple.c -- A simple form example
|----> form_win.c -- Demo of windows associated with forms

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

panels
|
|----> panel_browse.c -- Panel browsing through tab. Usage of user pointer
|----> panel_hide.c -- Hiding and Un hiding of panels
|----> panel_resize.c -- Moving and resizing of panels
|----> panel_simple.c -- A simple panel example

perl
|----> 01-10.pl -- Perl equivalents of first ten example programs
35 changes: 35 additions & 0 deletions basics/Makefile
@@ -0,0 +1,35 @@
# Makefile for JustForFun Files

# A few variables

CC=gcc
LIBS=-lncurses

SRC_DIR=.
EXE_DIR=../demo/exe

EXES = \
${EXE_DIR}/hello_world \
${EXE_DIR}/init_func_example \
${EXE_DIR}/key_code \
${EXE_DIR}/mouse_menu \
${EXE_DIR}/other_border \
${EXE_DIR}/printw_example \
${EXE_DIR}/scanw_example \
${EXE_DIR}/simple_attr \
${EXE_DIR}/simple_color \
${EXE_DIR}/simple_key \
${EXE_DIR}/temp_leave \
${EXE_DIR}/win_border \
${EXE_DIR}/with_chgat

${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS}

%.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $<

all: ${EXES}

clean:
@rm -f ${EXES}
20 changes: 20 additions & 0 deletions basics/README
@@ -0,0 +1,20 @@
Description of files
--------------------
basics
|
|----> acs_vars.c -- ACS_ variables example
|----> hello_world.c -- Simple "Hello World" Program
|----> init_func_example.c -- Initialization functions example
|----> key_code.c -- Shows the scan code of the key pressed
|----> mouse_menu.c -- A menu accessible by mouse
|----> other_border.c -- Shows usage of other border functions apart
| -- box()
|----> printw_example.c -- A very simple printw() example
|----> scanw_example.c -- A very simple getstr() example
|----> simple_attr.c -- A program that can print a c file with comments
| -- in attribute
|----> simple_color.c -- A simple example demonstrating colors
|----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows
|----> temp_leave.c -- Demonstrates temporarily leaving curses mode
|----> win_border.c -- Shows Creation of windows and borders
|----> with_chgat.c -- chgat() usage example
44 changes: 44 additions & 0 deletions basics/acs_vars.c
@@ -0,0 +1,44 @@
#include <ncurses.h>

int main()
{
initscr();

printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n");
printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n");
printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n");
printw("Tee pointing right "); addch(ACS_LTEE); printw("\n");
printw("Tee pointing left "); addch(ACS_RTEE); printw("\n");
printw("Tee pointing up "); addch(ACS_BTEE); printw("\n");
printw("Tee pointing down "); addch(ACS_TTEE); printw("\n");
printw("Horizontal line "); addch(ACS_HLINE); printw("\n");
printw("Vertical line "); addch(ACS_VLINE); printw("\n");
printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n");
printw("Scan Line 1 "); addch(ACS_S1); printw("\n");
printw("Scan Line 3 "); addch(ACS_S3); printw("\n");
printw("Scan Line 7 "); addch(ACS_S7); printw("\n");
printw("Scan Line 9 "); addch(ACS_S9); printw("\n");
printw("Diamond "); addch(ACS_DIAMOND); printw("\n");
printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n");
printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n");
printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n");
printw("Bullet "); addch(ACS_BULLET); printw("\n");
printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n");
printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n");
printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n");
printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n");
printw("Board of squares "); addch(ACS_BOARD); printw("\n");
printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n");
printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n");
printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n");
printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n");
printw("Pi "); addch(ACS_PI); printw("\n");
printw("Not equal "); addch(ACS_NEQUAL); printw("\n");
printw("UK pound sign "); addch(ACS_STERLING); printw("\n");

refresh();
getch();
endwin();

return 0;
}
12 changes: 12 additions & 0 deletions basics/hello_world.c
@@ -0,0 +1,12 @@
#include <ncurses.h>

int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */

return 0;
}
31 changes: 31 additions & 0 deletions basics/init_func_example.c
@@ -0,0 +1,31 @@
#include <ncurses.h>

int main()
{ int ch;

initscr(); /* Start curses mode */
raw(); /* Line buffering disabled */
keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
noecho(); /* Don't echo() while we do getch */

printw("Type any character to see it in bold\n");
ch = getch(); /* If raw() hadn't been called
* we have to press enter before it
* gets to the program */
if(ch == KEY_F(1)) /* Without keypad enabled this will */
printw("F1 Key pressed");/* not get to us either */
/* Without noecho() some ugly escape
* charachters might have been printed
* on screen */
else
{ printw("The pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
}
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */

return 0;
}
14 changes: 14 additions & 0 deletions basics/key_code.c
@@ -0,0 +1,14 @@
#include <ncurses.h>

int main()
{ int ch;

initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);

ch = getch();
endwin();
printf("The key pressed is %d\n", ch);
}
106 changes: 106 additions & 0 deletions basics/mouse_menu.c
@@ -0,0 +1,106 @@
#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);
void report_choice(int mouse_x, int mouse_y, int *p_choice);

int main()
{ int c, choice = 0;
WINDOW *menu_win;
MEVENT event;

/* Initialize curses */
initscr();
clear();
noecho();
cbreak(); //Line buffering disabled. pass on everything

/* Try to put the window in the middle of screen */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;

attron(A_REVERSE);
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
refresh();
attroff(A_REVERSE);

/* Print the menu for the first time */
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
print_menu(menu_win, 1);
/* Get all the mouse events */
mousemask(ALL_MOUSE_EVENTS, NULL);

while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_MOUSE:
if(getmouse(&event) == OK)
{ /* When the user clicks left mouse button */
if(event.bstate & BUTTON1_PRESSED)
{ report_choice(event.x + 1, event.y + 1, &choice);
if(choice == -1) //Exit chosen
goto end;
mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
refresh();
}
}
print_menu(menu_win, choice);
break;
}
}
end:
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)
{ 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);
}

/* Report the choice according to mouse position */
void report_choice(int mouse_x, int mouse_y, int *p_choice)
{ int i,j, choice;

i = startx + 2;
j = starty + 3;

for(choice = 0; choice < n_choices; ++choice)
if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))
{ if(choice == n_choices - 1)
*p_choice = -1;
else
*p_choice = choice + 1;
break;
}
}
120 changes: 120 additions & 0 deletions basics/other_border.c
@@ -0,0 +1,120 @@
#include <ncurses.h>

typedef struct _win_border_struct {
chtype ls, rs, ts, bs,
tl, tr, bl, br;
}WIN_BORDER;

typedef struct _WIN_struct {

int startx, starty;
int height, width;
WIN_BORDER border;
}WIN;

void init_win_params(WIN *p_win);
void print_win_params(WIN *p_win);
void create_box(WIN *win, bool flag);

int main(int argc, char *argv[])
{ WIN win;
int ch;

initscr(); /* Start curses mode */
start_color(); /* Start the color functionality */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */
noecho();
init_pair(1, COLOR_CYAN, COLOR_BLACK);

/* Initialize the window parameters */
init_win_params(&win);
print_win_params(&win);

attron(COLOR_PAIR(1));
printw("Press F1 to exit");
refresh();
attroff(COLOR_PAIR(1));

create_box(&win, TRUE);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
create_box(&win, FALSE);
--win.startx;
create_box(&win, TRUE);
break;
case KEY_RIGHT:
create_box(&win, FALSE);
++win.startx;
create_box(&win, TRUE);
break;
case KEY_UP:
create_box(&win, FALSE);
--win.starty;
create_box(&win, TRUE);
break;
case KEY_DOWN:
create_box(&win, FALSE);
++win.starty;
create_box(&win, TRUE);
break;
}
}
endwin(); /* End curses mode */
return 0;
}
void init_win_params(WIN *p_win)
{
p_win->height = 3;
p_win->width = 10;
p_win->starty = (LINES - p_win->height)/2;
p_win->startx = (COLS - p_win->width)/2;

p_win->border.ls = '|';
p_win->border.rs = '|';
p_win->border.ts = '-';
p_win->border.bs = '-';
p_win->border.tl = '+';
p_win->border.tr = '+';
p_win->border.bl = '+';
p_win->border.br = '+';

}
void print_win_params(WIN *p_win)
{
#ifdef _DEBUG
mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
p_win->width, p_win->height);
refresh();
#endif
}
void create_box(WIN *p_win, bool flag)
{ int i, j;
int x, y, w, h;

x = p_win->startx;
y = p_win->starty;
w = p_win->width;
h = p_win->height;

if(flag == TRUE)
{ mvaddch(y, x, p_win->border.tl);
mvaddch(y, x + w, p_win->border.tr);
mvaddch(y + h, x, p_win->border.bl);
mvaddch(y + h, x + w, p_win->border.br);
mvhline(y, x + 1, p_win->border.ts, w - 1);
mvhline(y + h, x + 1, p_win->border.bs, w - 1);
mvvline(y + 1, x, p_win->border.ls, h - 1);
mvvline(y + 1, x + w, p_win->border.rs, h - 1);

}
else
for(j = y; j <= y + h; ++j)
for(i = x; i <= x + w; ++i)
mvaddch(j, i, ' ');

refresh();

}
20 changes: 20 additions & 0 deletions basics/printw_example.c
@@ -0,0 +1,20 @@
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>

int main()
{
char mesg[]="Just a string"; /* message to be appeared on the screen */
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
printw("Try resizing your window(if possible) and then run this program again");
refresh();
getch();
endwin();

return 0;
}
20 changes: 20 additions & 0 deletions basics/scanw_example.c
@@ -0,0 +1,20 @@
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>

int main()
{
char mesg[]="Enter a string: "; /* message to be appeared on the screen */
char str[80];
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
getstr(str);
mvprintw(LINES - 2, 0, "You Entered: %s", str);
getch();
endwin();

return 0;
}
54 changes: 54 additions & 0 deletions basics/simple_attr.c
@@ -0,0 +1,54 @@
/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */
#include <ncurses.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int ch, prev, row, col;
prev = EOF;
FILE *fp;
int y, x;

if(argc != 2)
{
printf("Usage: %s <a c file name>\n", argv[0]);
exit(1);
}
fp = fopen(argv[1], "r");
if(fp == NULL)
{
perror("Cannot open input file");
exit(1);
}
initscr(); /* Start curses mode */
getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */
while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */
{
getyx(stdscr, y, x); /* get the current curser position */
if(y == (row - 1)) /* are we are at the end of the screen */
{
printw("<-Press Any Key->"); /* tell the user to press a key */
getch();
clear(); /* clear the screen */
move(0, 0); /* start at the beginning of the screen */
}
if(prev == '/' && ch == '*') /* If it is / and * then only
* switch bold on */
{
attron(A_BOLD); /* cut bold on */
getyx(stdscr, y, x); /* get the current curser position */
move(y, x - 1); /* back up one space */
printw("%c%c", '/', ch); /* The actual printing is done here */
}
else
printw("%c", ch);
refresh();
if(prev == '*' && ch == '/')
attroff(A_BOLD); /* Switch it off once we got *
* and then / */
prev = ch;
}
endwin(); /* End curses mode */
fclose(fp);
return 0;
}
40 changes: 40 additions & 0 deletions basics/simple_color.c
@@ -0,0 +1,40 @@
#include <ncurses.h>

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */
if(has_colors() == FALSE)
{ endwin();
printf("Your terminal does not support color\n");
exit(1);
}
start_color(); /* Start color */
init_pair(1, COLOR_RED, COLOR_BLACK);

attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
attroff(COLOR_PAIR(1));
getch();
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
{ 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;
mvwprintw(win, y, x, "%s", string);
refresh();
}

92 changes: 92 additions & 0 deletions basics/simple_key.c
@@ -0,0 +1,92 @@
#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 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);
}

20 changes: 20 additions & 0 deletions basics/temp_leave.c
@@ -0,0 +1,20 @@
#include <ncurses.h>

int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!\n"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
def_prog_mode(); /* Save the tty modes */
endwin(); /* End curses mode temporarily */
system("/bin/sh"); /* Do whatever you like in cooked mode */
reset_prog_mode(); /* Return to the previous tty mode*/
/* stored by def_prog_mode() */
refresh(); /* Do refresh() to restore the */
/* Screen contents */
printw("Another String\n"); /* Back to curses use the full */
refresh(); /* capabilities of curses */
endwin(); /* End curses mode */

return 0;
}
82 changes: 82 additions & 0 deletions basics/win_border.c
@@ -0,0 +1,82 @@
#include <ncurses.h>


WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);

int main(int argc, char *argv[])
{ WINDOW *my_win;
int startx, starty, width, height;
int ch;

initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */

height = 3;
width = 10;
starty = (LINES - height) / 2; /* Calculating for a center placement */
startx = (COLS - width) / 2; /* of the window */
printw("Press F1 to exit");
refresh();
my_win = create_newwin(height, width, starty, startx);

while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,--startx);
break;
case KEY_RIGHT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,++startx);
break;
case KEY_UP:
destroy_win(my_win);
my_win = create_newwin(height, width, --starty,startx);
break;
case KEY_DOWN:
destroy_win(my_win);
my_win = create_newwin(height, width, ++starty,startx);
break;
}
}

endwin(); /* End curses mode */
return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win;

local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0); /* 0, 0 gives default characters
* for the vertical and horizontal
* lines */
wrefresh(local_win); /* Show that box */

return local_win;
}

void destroy_win(WINDOW *local_win)
{
/* box(local_win, ' ', ' '); : This won't produce the desired
* result of erasing the window. It will leave it's four corners
* and so an ugly remnant of window.
*/
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* The parameters taken are
* 1. win: the window on which to operate
* 2. ls: character to be used for the left side of the window
* 3. rs: character to be used for the right side of the window
* 4. ts: character to be used for the top side of the window
* 5. bs: character to be used for the bottom side of the window
* 6. tl: character to be used for the top left corner of the window
* 7. tr: character to be used for the top right corner of the window
* 8. bl: character to be used for the bottom left corner of the window
* 9. br: character to be used for the bottom right corner of the window
*/
wrefresh(local_win);
delwin(local_win);
}
24 changes: 24 additions & 0 deletions basics/with_chgat.c
@@ -0,0 +1,24 @@
#include <ncurses.h>

int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */
start_color(); /* Start color functionality */

init_pair(1, COLOR_CYAN, COLOR_BLACK);
printw("A Big string which i didn't care to type fully ");
mvchgat(0, 0, -1, A_BLINK, 1, NULL);
/*
* First two parameters specify the position at which to start
* Third parameter number of characters to update. -1 means till
* end of line
* Forth parameter is the normal attribute you wanted to give
* to the charcter
* Fifth is the color index. It is the index given during init_pair()
* use 0 if you didn't want color
* Sixth one is always NULL
*/
refresh();
getch();
endwin(); /* End curses mode */
return 0;
}
27 changes: 27 additions & 0 deletions forms/Makefile
@@ -0,0 +1,27 @@
# Makefile for JustForFun Files

# A few variables

CC=gcc
LIBS=-lform -lncurses

SRC_DIR=.
EXE_DIR=../demo/exe

EXES = \
${EXE_DIR}/form_attrib\
${EXE_DIR}/form_options\
${EXE_DIR}/form_simple\
${EXE_DIR}/form_win \

${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS}

%.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $<

all: ${EXES}


clean:
@rm -f ${EXES}
9 changes: 9 additions & 0 deletions forms/README
@@ -0,0 +1,9 @@
Description of files
--------------------
forms
|
|----> form_attrib.c -- Usage of field attributes
|----> form_options.c -- Usage of field options
|----> form_simple.c -- A simple form example
|----> form_win.c -- Demo of windows associated with forms

75 changes: 75 additions & 0 deletions forms/form_attrib.c
@@ -0,0 +1,75 @@
#include <form.h>

int main()
{ FIELD *field[3];
FORM *my_form;
int ch;

/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);

/* Initialize few color pairs */
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_WHITE, COLOR_BLUE);

/* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;

/* Set field options */
set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */
set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */
/* are printed in white */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);

/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();

set_current_field(my_form, field[0]); /* Set focus to the colored field */
mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();

/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}

/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);

endwin();
return 0;
}
76 changes: 76 additions & 0 deletions forms/form_options.c
@@ -0,0 +1,76 @@
#include <form.h>

#define STARTX 15
#define STARTY 4
#define WIDTH 25

#define N_FIELDS 3

int main()
{ FIELD *field[N_FIELDS];
FORM *my_form;
int ch, i;

/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);

/* Initialize the fields */
for(i = 0; i < N_FIELDS - 1; ++i)
field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
field[N_FIELDS - 1] = NULL;

/* Set field options */
set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */

field_opts_off(field[0], O_ACTIVE); /* This field is a static label */
field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/
field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */
/* after last character is entered */

/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();

set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */
set_field_buffer(field[0], 0, "This is a static Field");
/* Initialize the field */
mvprintw(STARTY, STARTX - 10, "Field 1:");
mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
refresh();

/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}

/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);

endwin();
return 0;
}
66 changes: 66 additions & 0 deletions forms/form_simple.c
@@ -0,0 +1,66 @@
#include <form.h>

int main()
{ FIELD *field[3];
FORM *my_form;
int ch;

/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);

/* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;

/* Set field options */
set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);

/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();

mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
refresh();

/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}

/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);

endwin();
return 0;
}
112 changes: 112 additions & 0 deletions forms/form_win.c
@@ -0,0 +1,112 @@
#include <form.h>

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);

int main()
{
FIELD *field[3];
FORM *my_form;
WINDOW *my_form_win;
int ch, rows, cols;

/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);

/* Initialize few color pairs */
init_pair(1, COLOR_RED, COLOR_BLACK);

/* Initialize the fields */
field[0] = new_field(1, 10, 6, 1, 0, 0);
field[1] = new_field(1, 10, 8, 1, 0, 0);
field[2] = NULL;

/* Set field options */
set_field_back(field[0], A_UNDERLINE);
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);

/* Create the form and post it */
my_form = new_form(field);

/* Calculate the area required for the form */
scale_form(my_form, &rows, &cols);

/* Create the window to be associated with the form */
my_form_win = newwin(rows + 4, cols + 4, 4, 4);
keypad(my_form_win, TRUE);

/* Set main window and sub window */
set_form_win(my_form, my_form_win);
set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));

/* Print a border around the main window and print a title */
box(my_form_win, 0, 0);
print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));

post_form(my_form);
wrefresh(my_form_win);

mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();

/* Loop through to get user requests */
while((ch = wgetch(my_form_win)) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}

/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);

endwin();
return 0;
}

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();
}
31 changes: 31 additions & 0 deletions menus/Makefile
@@ -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 changes: 13 additions & 0 deletions menus/README
@@ -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 changes: 80 additions & 0 deletions menus/menu_attrib.c
@@ -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 changes: 66 additions & 0 deletions menus/menu_item_data.c
@@ -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 changes: 97 additions & 0 deletions menus/menu_multi_column.c
@@ -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 changes: 124 additions & 0 deletions menus/menu_scroll.c
@@ -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 changes: 56 additions & 0 deletions menus/menu_simple.c
@@ -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 changes: 84 additions & 0 deletions menus/menu_toggle.c
@@ -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 changes: 87 additions & 0 deletions menus/menu_userptr.c
@@ -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 changes: 105 additions & 0 deletions menus/menu_win.c
@@ -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();
}
27 changes: 27 additions & 0 deletions panels/Makefile
@@ -0,0 +1,27 @@
# Makefile for JustForFun Files

# A few variables

CC=gcc
LIBS=-lpanel -lncurses

SRC_DIR=.
EXE_DIR=../demo/exe

EXES = \
${EXE_DIR}/panel_browse \
${EXE_DIR}/panel_hide \
${EXE_DIR}/panel_resize \
${EXE_DIR}/panel_simple

${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS}

%.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $<

all: ${EXES}


clean:
@rm -f ${EXES}
8 changes: 8 additions & 0 deletions panels/README
@@ -0,0 +1,8 @@
Description of files
--------------------
panels
|
|----> panel_browse.c -- Panel browsing through tab. Usage of user pointer
|----> panel_hide.c -- Hiding and Un hiding of panels
|----> panel_resize.c -- Moving and resizing of panels
|----> panel_simple.c -- A simple panel example
117 changes: 117 additions & 0 deletions panels/panel_browse.c
@@ -0,0 +1,117 @@
#include <panel.h>

#define NLINES 10
#define NCOLS 40

void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);

int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL *top;
int ch;

/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);

/* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);

init_wins(my_wins, 3);

/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */

/* Set up the user pointers to the next panel */
set_panel_userptr(my_panels[0], my_panels[1]);
set_panel_userptr(my_panels[1], my_panels[2]);
set_panel_userptr(my_panels[2], my_panels[0]);

/* Update the stacking order. 2nd panel will be on top */
update_panels();

/* Show it on the screen */
attron(COLOR_PAIR(4));
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
doupdate();

top = my_panels[2];
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 9:
top = (PANEL *)panel_userptr(top);
top_panel(top);
break;
}
update_panels();
doupdate();
}
endwin();
return 0;
}

/* Put all the windows */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];

y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}

/* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;

getbegyx(win, starty, startx);
getmaxyx(win, height, width);

box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);

print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}

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();
}