Merge remote-tracking (subtree) branch 'PracticingCurses/master'
This commit is contained in:
commit
cd651d229c
1
PracticingCurses/.gitignore
vendored
Normal file
1
PracticingCurses/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
demo/exe/*
|
23
PracticingCurses/COPYING
Normal file
23
PracticingCurses/COPYING
Normal file
@ -0,0 +1,23 @@
|
||||
Copyright (c) 2001, Pradeep Padala (ppadala@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, distribute with
|
||||
modifications, sublicense, and/or sell copies of the Software, and to permit
|
||||
persons to whom the Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name(s) of the above copyright holders
|
||||
shall not be used in advertising or otherwise to promote the sale, use or
|
||||
other dealings in this Software without prior written authorization.
|
28
PracticingCurses/JustForFun/Makefile
Normal file
28
PracticingCurses/JustForFun/Makefile
Normal file
@ -0,0 +1,28 @@
|
||||
# Makefile for JustForFun Files
|
||||
|
||||
# A few variables
|
||||
|
||||
CC=gcc
|
||||
LIBS=-lncurses
|
||||
|
||||
SRC_DIR=.
|
||||
EXE_DIR=../demo/exe
|
||||
|
||||
EXES = \
|
||||
${EXE_DIR}/hanoi \
|
||||
${EXE_DIR}/life\
|
||||
${EXE_DIR}/magic \
|
||||
${EXE_DIR}/queens \
|
||||
${EXE_DIR}/shuffle \
|
||||
${EXE_DIR}/tt
|
||||
|
||||
${EXE_DIR}/%: %.o
|
||||
${CC} -o $@ $< ${LIBS}
|
||||
|
||||
%.o: ${SRC_DIR}/%.c
|
||||
${CC} -o $@ -c $<
|
||||
|
||||
all: ${EXES}
|
||||
|
||||
clean:
|
||||
@rm -f ${EXES}
|
10
PracticingCurses/JustForFun/README
Normal file
10
PracticingCurses/JustForFun/README
Normal file
@ -0,0 +1,10 @@
|
||||
Description of files
|
||||
--------------------
|
||||
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
|
178
PracticingCurses/JustForFun/hanoi.c
Normal file
178
PracticingCurses/JustForFun/hanoi.c
Normal file
@ -0,0 +1,178 @@
|
||||
#include <curses.h>
|
||||
|
||||
#define POSX 10
|
||||
#define POSY 5
|
||||
#define DISC_CHAR '*'
|
||||
#define PEG_CHAR '#'
|
||||
#define TIME_OUT 300
|
||||
|
||||
typedef struct _peg_struct {
|
||||
int n_discs; /* Number of discs at present */
|
||||
int bottomx, bottomy; /* bottom x, bottom y co-ord */
|
||||
int *sizes; /* The disc sizes array */
|
||||
}peg;
|
||||
|
||||
void init_pegs(peg *p_my_pegs, int n_discs);
|
||||
void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs);
|
||||
void free_pegs(peg *p_my_pegs, int n_discs);
|
||||
void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst);
|
||||
void move_disc(peg *p_my_pegs, int n_discs, int src, int dst);
|
||||
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win);
|
||||
void check_usr_response(peg *p_my_pegs, int n_discs);
|
||||
|
||||
int store_n_discs;
|
||||
char *welcome_string = "Enter the number of discs you want to be solved: ";
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{ int n_discs;
|
||||
peg my_pegs[3];
|
||||
|
||||
initscr(); /* Start curses mode */
|
||||
cbreak(); /* Line buffering disabled. Pass on every thing */
|
||||
keypad(stdscr, TRUE);
|
||||
curs_set(FALSE);
|
||||
|
||||
print_in_middle(0, LINES / 2, COLS, welcome_string, NULL);
|
||||
scanw("%d", &n_discs);
|
||||
|
||||
timeout(TIME_OUT);
|
||||
noecho();
|
||||
store_n_discs = n_discs;
|
||||
|
||||
init_pegs(my_pegs, n_discs);
|
||||
show_pegs(stdscr, my_pegs, n_discs);
|
||||
solve_hanoi(my_pegs, n_discs, 0, 1, 2);
|
||||
|
||||
free_pegs(my_pegs, n_discs);
|
||||
endwin(); /* End curses mode */
|
||||
return 0;
|
||||
}
|
||||
|
||||
void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst)
|
||||
{ if(n_discs == 0)
|
||||
return;
|
||||
solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux);
|
||||
move_disc(p_my_pegs, store_n_discs, src, dst);
|
||||
show_pegs(stdscr, p_my_pegs, store_n_discs);
|
||||
check_usr_response(p_my_pegs, store_n_discs);
|
||||
solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst);
|
||||
}
|
||||
|
||||
void check_usr_response(peg *p_my_pegs, int n_discs)
|
||||
{ int ch;
|
||||
|
||||
ch = getch(); /* Waits for TIME_OUT milliseconds */
|
||||
if(ch == ERR)
|
||||
return;
|
||||
else
|
||||
if(ch == KEY_F(1))
|
||||
{ free_pegs(p_my_pegs, n_discs);
|
||||
endwin();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void move_disc(peg *p_my_pegs, int n_discs, int src, int dst)
|
||||
{ int temp, index;
|
||||
|
||||
--p_my_pegs[src].n_discs;
|
||||
index = 0;
|
||||
while(p_my_pegs[src].sizes[index] == 0 && index != n_discs)
|
||||
++index;
|
||||
temp = p_my_pegs[src].sizes[index];
|
||||
p_my_pegs[src].sizes[index] = 0;
|
||||
|
||||
index = 0;
|
||||
while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs)
|
||||
++index;
|
||||
--index;
|
||||
p_my_pegs[dst].sizes[index] = temp;
|
||||
++p_my_pegs[dst].n_discs;
|
||||
}
|
||||
|
||||
void init_pegs(peg *p_my_pegs, int n_discs)
|
||||
{ int size, temp, i;
|
||||
|
||||
p_my_pegs[0].n_discs = n_discs;
|
||||
|
||||
/* Allocate memory for size array
|
||||
* atmost the number of discs on a peg can be n_discs
|
||||
*/
|
||||
for(i = 0; i < n_discs; ++i)
|
||||
p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int));
|
||||
size = 3;
|
||||
for(i = 0;i < n_discs; ++i, size += 2)
|
||||
p_my_pegs[0].sizes[i] = size;
|
||||
|
||||
temp = (p_my_pegs[0].sizes[n_discs - 1] / 2);
|
||||
p_my_pegs[0].bottomx = POSX + 1 + temp;
|
||||
p_my_pegs[0].bottomy = POSY + 2 + n_discs;
|
||||
|
||||
p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp;
|
||||
p_my_pegs[1].bottomy = POSY + 2 + n_discs;
|
||||
|
||||
p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp;
|
||||
p_my_pegs[2].bottomy = POSY + 2 + n_discs;
|
||||
}
|
||||
|
||||
void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs)
|
||||
{ int i, j, k, x, y, size;
|
||||
|
||||
wclear(win);
|
||||
attron(A_REVERSE);
|
||||
mvprintw(24, 0, "Press F1 to Exit");
|
||||
attroff(A_REVERSE);
|
||||
for(i = 0;i < 3; ++i)
|
||||
mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1,
|
||||
p_my_pegs[i].bottomx, "%c", PEG_CHAR);
|
||||
y = p_my_pegs[0].bottomy - n_discs;
|
||||
for(i = 0; i < 3; ++i) /* For each peg */
|
||||
{ for(j = 0; j < n_discs; ++ j) /* For each row */
|
||||
{ if(p_my_pegs[i].sizes[j] != 0)
|
||||
{ size = p_my_pegs[i].sizes[j];
|
||||
x = p_my_pegs[i].bottomx - (size / 2);
|
||||
for(k = 0; k < size; ++k)
|
||||
mvwprintw(win, y, x + k, "%c", DISC_CHAR);
|
||||
}
|
||||
else
|
||||
mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR);
|
||||
++y;
|
||||
}
|
||||
y = p_my_pegs[0].bottomy - n_discs;
|
||||
}
|
||||
wrefresh(win);
|
||||
}
|
||||
|
||||
void free_pegs(peg *p_my_pegs, int n_discs)
|
||||
{ int i;
|
||||
|
||||
for(i = 0;i < n_discs; ++i)
|
||||
free(p_my_pegs[i].sizes);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------*
|
||||
* 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();
|
||||
}
|
107
PracticingCurses/JustForFun/life.c
Normal file
107
PracticingCurses/JustForFun/life.c
Normal file
@ -0,0 +1,107 @@
|
||||
#include <curses.h>
|
||||
|
||||
int STARTX = 0;
|
||||
int STARTY = 0;
|
||||
int ENDX = 79;
|
||||
int ENDY = 24;
|
||||
|
||||
#define CELL_CHAR '#'
|
||||
#define TIME_OUT 300
|
||||
|
||||
typedef struct _state {
|
||||
int oldstate;
|
||||
int newstate;
|
||||
}state;
|
||||
|
||||
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy);
|
||||
void calc(state **area, int x, int y);
|
||||
void update_state(state **area, int startx, int starty, int endx, int endy);
|
||||
|
||||
int main()
|
||||
{ state **workarea;
|
||||
int i, j;
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
timeout(TIME_OUT);
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
ENDX = COLS - 1;
|
||||
ENDY = LINES - 1;
|
||||
|
||||
workarea = (state **)calloc(COLS, sizeof(state *));
|
||||
for(i = 0;i < COLS; ++i)
|
||||
workarea[i] = (state *)calloc(LINES, sizeof(state));
|
||||
|
||||
/* For inverted U */
|
||||
workarea[39][15].newstate = TRUE;
|
||||
workarea[40][15].newstate = TRUE;
|
||||
workarea[41][15].newstate = TRUE;
|
||||
workarea[39][16].newstate = TRUE;
|
||||
workarea[39][17].newstate = TRUE;
|
||||
workarea[41][16].newstate = TRUE;
|
||||
workarea[41][17].newstate = TRUE;
|
||||
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
|
||||
|
||||
/* For block */
|
||||
/*
|
||||
workarea[37][13].newstate = TRUE;
|
||||
workarea[37][14].newstate = TRUE;
|
||||
workarea[38][13].newstate = TRUE;
|
||||
workarea[38][14].newstate = TRUE;
|
||||
|
||||
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
|
||||
*/
|
||||
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
|
||||
while(getch() != KEY_F(1))
|
||||
{ for(i = STARTX; i <= ENDX; ++i)
|
||||
for(j = STARTY; j <= ENDY; ++j)
|
||||
calc(workarea, i, j);
|
||||
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
|
||||
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
|
||||
}
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy)
|
||||
{ int i, j;
|
||||
wclear(win);
|
||||
for(i = startx; i <= endx; ++i)
|
||||
for(j = starty;j <= endy; ++j)
|
||||
if(area[i][j].newstate == TRUE)
|
||||
mvwaddch(win, j, i, CELL_CHAR);
|
||||
wrefresh(win);
|
||||
}
|
||||
|
||||
void calc(state **area, int i, int j)
|
||||
{ int neighbours;
|
||||
int newstate;
|
||||
|
||||
neighbours =
|
||||
area[(i - 1 + COLS) % COLS][j].oldstate +
|
||||
area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate +
|
||||
area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate +
|
||||
area[(i + 1) % COLS][j].oldstate +
|
||||
area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate +
|
||||
area[(i + 1) % COLS][(j + 1) % LINES].oldstate +
|
||||
area[i][(j - 1 + LINES) % LINES].oldstate +
|
||||
area[i][(j + 1) % LINES].oldstate;
|
||||
|
||||
newstate = FALSE;
|
||||
if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3))
|
||||
newstate = TRUE;
|
||||
else
|
||||
if(area[i][j].oldstate == FALSE && neighbours == 3)
|
||||
newstate = TRUE;
|
||||
area[i][j].newstate = newstate;
|
||||
}
|
||||
|
||||
void update_state(state **area, int startx, int starty, int endx, int endy)
|
||||
{ int i, j;
|
||||
|
||||
for(i = startx; i <= endx; ++i)
|
||||
for(j = starty; j <= endy; ++j)
|
||||
area[i][j].oldstate = area[i][j].newstate;
|
||||
}
|
161
PracticingCurses/JustForFun/magic.c
Normal file
161
PracticingCurses/JustForFun/magic.c
Normal file
@ -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
PracticingCurses/JustForFun/queens.c
Normal file
122
PracticingCurses/JustForFun/queens.c
Normal file
@ -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
PracticingCurses/JustForFun/shuffle.c
Normal file
205
PracticingCurses/JustForFun/shuffle.c
Normal file
@ -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
PracticingCurses/JustForFun/tt.c
Normal file
223
PracticingCurses/JustForFun/tt.c
Normal file
@ -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();
|
||||
}
|
29
PracticingCurses/Makefile
Normal file
29
PracticingCurses/Makefile
Normal file
@ -0,0 +1,29 @@
|
||||
# The top level Makefile
|
||||
|
||||
TOP := $(PWD)
|
||||
PROJECTS := JustForFun basics forms menus panels
|
||||
|
||||
|
||||
all:
|
||||
$(foreach project,$(PROJECTS),\
|
||||
cd $(project) && \
|
||||
$(MAKE) && \
|
||||
cd $(TOP) \
|
||||
;\
|
||||
)
|
||||
@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:
|
||||
$(foreach project,$(PROJECTS),\
|
||||
cd $(project) && \
|
||||
$(MAKE) clean && \
|
||||
cd $(TOP) \
|
||||
;\
|
||||
)
|
87
PracticingCurses/README
Normal file
87
PracticingCurses/README
Normal file
@ -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
PracticingCurses/basics/Makefile
Normal file
35
PracticingCurses/basics/Makefile
Normal file
@ -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
PracticingCurses/basics/README
Normal file
20
PracticingCurses/basics/README
Normal file
@ -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
PracticingCurses/basics/acs_vars.c
Normal file
44
PracticingCurses/basics/acs_vars.c
Normal file
@ -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
PracticingCurses/basics/hello_world.c
Normal file
12
PracticingCurses/basics/hello_world.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
initscr();
|
||||
printw("Hello World !!!");
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
18
PracticingCurses/basics/hello_world.py
Normal file
18
PracticingCurses/basics/hello_world.py
Normal file
@ -0,0 +1,18 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def hello_world(stdscr):
|
||||
stdscr.addstr(0, 0, "Hello World !!!")
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(hello_world)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
37
PracticingCurses/basics/init_func_example.c
Normal file
37
PracticingCurses/basics/init_func_example.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
void _do_setup(){
|
||||
initscr();
|
||||
raw();
|
||||
keypad(stdscr, TRUE);
|
||||
noecho();
|
||||
}
|
||||
|
||||
|
||||
void _display_and_quit(){
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch;
|
||||
_do_setup();
|
||||
|
||||
printw("Type any character to see it in bold\n");
|
||||
ch = getch();
|
||||
if(ch == KEY_F(1)){
|
||||
printw("F1 Key pressed");
|
||||
} else {
|
||||
printw("The pressed key is ");
|
||||
attron(A_BOLD);
|
||||
printw("%c", ch);
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
|
||||
_display_and_quit();
|
||||
|
||||
return 0;
|
||||
}
|
33
PracticingCurses/basics/init_func_example.py
Normal file
33
PracticingCurses/basics/init_func_example.py
Normal file
@ -0,0 +1,33 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(init_func_example)
|
||||
return 0
|
||||
|
||||
|
||||
def init_func_example(stdscr):
|
||||
curses.raw()
|
||||
stdscr.keypad(1)
|
||||
curses.noecho()
|
||||
stdscr.addstr(0, 0, "Type any character to see it in bold\n")
|
||||
|
||||
ch = stdscr.getch()
|
||||
if ch == curses.KEY_F1:
|
||||
stdscr.addstr(1, 0, "F1 Key pressed")
|
||||
else:
|
||||
key_pressed = "The pressed key is "
|
||||
stdscr.addstr(1, 0, key_pressed)
|
||||
stdscr.attron(curses.A_BOLD)
|
||||
stdscr.addstr(1, len(key_pressed), chr(ch))
|
||||
stdscr.attroff(curses.A_BOLD)
|
||||
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
14
PracticingCurses/basics/key_code.c
Normal file
14
PracticingCurses/basics/key_code.c
Normal file
@ -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
PracticingCurses/basics/mouse_menu.c
Normal file
106
PracticingCurses/basics/mouse_menu.c
Normal file
@ -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
PracticingCurses/basics/other_border.c
Normal file
120
PracticingCurses/basics/other_border.c
Normal file
@ -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();
|
||||
|
||||
}
|
19
PracticingCurses/basics/printw_example.c
Normal file
19
PracticingCurses/basics/printw_example.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char mesg[] = "Just a string";
|
||||
int row, col;
|
||||
|
||||
initscr();
|
||||
getmaxyx(stdscr, row, col);
|
||||
mvprintw(row / 2, (col - strlen(mesg)) / 2, "%s", mesg);
|
||||
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;
|
||||
}
|
25
PracticingCurses/basics/printw_example.py
Normal file
25
PracticingCurses/basics/printw_example.py
Normal file
@ -0,0 +1,25 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def printw_example(stdscr):
|
||||
mesg = "Just a string"
|
||||
row, col = stdscr.getmaxyx()
|
||||
stdscr.addstr(row / 2, (col - len(mesg)) / 2, mesg)
|
||||
stdscr.addstr(row - 2, 0, "This screen has {0} rows and {1} "
|
||||
"columns\n".format(row, col))
|
||||
stdscr.addstr(row - 1, 0, "Try resizing your window (if possible) and then"
|
||||
"run this program again")
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(printw_example)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
19
PracticingCurses/basics/scanw_example.c
Normal file
19
PracticingCurses/basics/scanw_example.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char * mesg = "Enter a string: ";
|
||||
char str[80];
|
||||
int row, col;
|
||||
|
||||
initscr();
|
||||
getmaxyx(stdscr, row, col);
|
||||
mvprintw(row / 2, (col - strlen(mesg)) / 2, "%s", mesg);
|
||||
getstr(str);
|
||||
mvprintw(LINES - 2, 0, "You Entered: %s", str);
|
||||
getch();
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
24
PracticingCurses/basics/scanw_example.py
Normal file
24
PracticingCurses/basics/scanw_example.py
Normal file
@ -0,0 +1,24 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def scanw_example(stdscr):
|
||||
mesg = 'Enter a string: '
|
||||
|
||||
row, col = stdscr.getmaxyx()
|
||||
curses.echo()
|
||||
stdscr.addstr(row / 2, (col - len(mesg)) / 2, mesg)
|
||||
instr = stdscr.getstr()
|
||||
stdscr.addstr(row - 2, 0, 'You Entered: {0}'.format(instr))
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(scanw_example)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
56
PracticingCurses/basics/simple_attr.c
Normal file
56
PracticingCurses/basics/simple_attr.c
Normal file
@ -0,0 +1,56 @@
|
||||
/* 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;
|
||||
}
|
50
PracticingCurses/basics/simple_attr.py
Normal file
50
PracticingCurses/basics/simple_attr.py
Normal file
@ -0,0 +1,50 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import curses
|
||||
|
||||
FILE = {'fp': None}
|
||||
|
||||
|
||||
def simple_attr(stdscr):
|
||||
row, col = stdscr.getmaxyx()
|
||||
prev = ''
|
||||
fp = FILE['fp']
|
||||
for line in fp:
|
||||
for ch in line:
|
||||
y, x = stdscr.getyx()
|
||||
if y == (row - 1):
|
||||
stdscr.addstr(row - 1, 0, "<-Press Any Key->")
|
||||
stdscr.getch()
|
||||
stdscr.clear()
|
||||
stdscr.move(0, 0)
|
||||
elif prev == '/' and ch == '*':
|
||||
stdscr.attron(curses.A_BOLD)
|
||||
y, x = stdscr.getyx()
|
||||
stdscr.addstr(y, x - 1, '/{0}'.format(ch))
|
||||
else:
|
||||
stdscr.addstr(y, x, ch)
|
||||
stdscr.refresh()
|
||||
|
||||
if prev == '*' and ch == '/':
|
||||
stdscr.attroff(curses.A_BOLD)
|
||||
|
||||
prev = ch
|
||||
|
||||
|
||||
def main(sysargs=sys.argv[:]):
|
||||
if not sysargs[1:]:
|
||||
print('Usage: {0} <a c file name>'.format(sysargs[0]))
|
||||
return 1
|
||||
try:
|
||||
FILE['fp'] = open(sysargs[1], 'r')
|
||||
except (OSError, IOError):
|
||||
print('Cannot open input file', file=sys.stderr)
|
||||
return 1
|
||||
curses.wrapper(simple_attr)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
40
PracticingCurses/basics/simple_color.c
Normal file
40
PracticingCurses/basics/simple_color.c
Normal file
@ -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
PracticingCurses/basics/simple_key.c
Normal file
92
PracticingCurses/basics/simple_key.c
Normal file
@ -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
PracticingCurses/basics/temp_leave.c
Normal file
20
PracticingCurses/basics/temp_leave.c
Normal file
@ -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
PracticingCurses/basics/win_border.c
Normal file
82
PracticingCurses/basics/win_border.c
Normal file
@ -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);
|
||||
}
|
15
PracticingCurses/basics/with_chgat.c
Normal file
15
PracticingCurses/basics/with_chgat.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
initscr();
|
||||
start_color();
|
||||
|
||||
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);
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
21
PracticingCurses/basics/with_chgat.py
Normal file
21
PracticingCurses/basics/with_chgat.py
Normal file
@ -0,0 +1,21 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def with_chgat(stdscr):
|
||||
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
||||
stdscr.addstr("A Big string which i didn't care to type fully ",
|
||||
curses.color_pair(1) | curses.A_BLINK)
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(with_chgat)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
0
PracticingCurses/demo/exe/.placeholder
Normal file
0
PracticingCurses/demo/exe/.placeholder
Normal file
27
PracticingCurses/forms/Makefile
Normal file
27
PracticingCurses/forms/Makefile
Normal file
@ -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
PracticingCurses/forms/README
Normal file
9
PracticingCurses/forms/README
Normal file
@ -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
PracticingCurses/forms/form_attrib.c
Normal file
75
PracticingCurses/forms/form_attrib.c
Normal file
@ -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
PracticingCurses/forms/form_options.c
Normal file
76
PracticingCurses/forms/form_options.c
Normal file
@ -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
PracticingCurses/forms/form_simple.c
Normal file
66
PracticingCurses/forms/form_simple.c
Normal file
@ -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
PracticingCurses/forms/form_win.c
Normal file
112
PracticingCurses/forms/form_win.c
Normal file
@ -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
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();
|
||||
}
|
27
PracticingCurses/panels/Makefile
Normal file
27
PracticingCurses/panels/Makefile
Normal file
@ -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
PracticingCurses/panels/README
Normal file
8
PracticingCurses/panels/README
Normal file
@ -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
PracticingCurses/panels/panel_browse.c
Normal file
117
PracticingCurses/panels/panel_browse.c
Normal file
@ -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();
|
||||
}
|
156
PracticingCurses/panels/panel_hide.c
Normal file
156
PracticingCurses/panels/panel_hide.c
Normal file
@ -0,0 +1,156 @@
|
||||
#include <panel.h>
|
||||
|
||||
typedef struct _PANEL_DATA {
|
||||
int hide; /* TRUE if panel is hidden */
|
||||
}PANEL_DATA;
|
||||
|
||||
#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_DATA panel_datas[3];
|
||||
PANEL_DATA *temp;
|
||||
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 */
|
||||
|
||||
/* Initialize panel datas saying that nothing is hidden */
|
||||
panel_datas[0].hide = FALSE;
|
||||
panel_datas[1].hide = FALSE;
|
||||
panel_datas[2].hide = FALSE;
|
||||
|
||||
set_panel_userptr(my_panels[0], &panel_datas[0]);
|
||||
set_panel_userptr(my_panels[1], &panel_datas[1]);
|
||||
set_panel_userptr(my_panels[2], &panel_datas[2]);
|
||||
|
||||
/* Update the stacking order. 2nd panel will be on top */
|
||||
update_panels();
|
||||
|
||||
/* Show it on the screen */
|
||||
attron(COLOR_PAIR(4));
|
||||
mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)");
|
||||
mvprintw(LINES - 2, 0, "F1 to Exit");
|
||||
|
||||
attroff(COLOR_PAIR(4));
|
||||
doupdate();
|
||||
|
||||
while((ch = getch()) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case 'a':
|
||||
temp = (PANEL_DATA *)panel_userptr(my_panels[0]);
|
||||
if(temp->hide == FALSE)
|
||||
{ hide_panel(my_panels[0]);
|
||||
temp->hide = TRUE;
|
||||
}
|
||||
else
|
||||
{ show_panel(my_panels[0]);
|
||||
temp->hide = FALSE;
|
||||
}
|
||||
break;
|
||||
case 'b':
|
||||
temp = (PANEL_DATA *)panel_userptr(my_panels[1]);
|
||||
if(temp->hide == FALSE)
|
||||
{ hide_panel(my_panels[1]);
|
||||
temp->hide = TRUE;
|
||||
}
|
||||
else
|
||||
{ show_panel(my_panels[1]);
|
||||
temp->hide = FALSE;
|
||||
}
|
||||
break;
|
||||
case 'c':
|
||||
temp = (PANEL_DATA *)panel_userptr(my_panels[2]);
|
||||
if(temp->hide == FALSE)
|
||||
{ hide_panel(my_panels[2]);
|
||||
temp->hide = TRUE;
|
||||
}
|
||||
else
|
||||
{ show_panel(my_panels[2]);
|
||||
temp->hide = FALSE;
|
||||
}
|
||||
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();
|
||||
}
|
234
PracticingCurses/panels/panel_resize.c
Normal file
234
PracticingCurses/panels/panel_resize.c
Normal file
@ -0,0 +1,234 @@
|
||||
#include <panel.h>
|
||||
|
||||
typedef struct _PANEL_DATA {
|
||||
int x, y, w, h;
|
||||
char label[80];
|
||||
int label_color;
|
||||
PANEL *next;
|
||||
}PANEL_DATA;
|
||||
|
||||
#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);
|
||||
void set_user_ptrs(PANEL **panels, int n);
|
||||
|
||||
int main()
|
||||
{ WINDOW *my_wins[3];
|
||||
PANEL *my_panels[3];
|
||||
PANEL_DATA *top;
|
||||
PANEL *stack_top;
|
||||
WINDOW *temp_win, *old_win;
|
||||
int ch;
|
||||
int newx, newy, neww, newh;
|
||||
int size = FALSE, move = FALSE;
|
||||
|
||||
/* 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_user_ptrs(my_panels, 3);
|
||||
/* Update the stacking order. 2nd panel will be on top */
|
||||
update_panels();
|
||||
|
||||
/* Show it on the screen */
|
||||
attron(COLOR_PAIR(4));
|
||||
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
|
||||
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
|
||||
attroff(COLOR_PAIR(4));
|
||||
doupdate();
|
||||
|
||||
stack_top = my_panels[2];
|
||||
top = (PANEL_DATA *)panel_userptr(stack_top);
|
||||
newx = top->x;
|
||||
newy = top->y;
|
||||
neww = top->w;
|
||||
newh = top->h;
|
||||
while((ch = getch()) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case 9: /* Tab */
|
||||
top = (PANEL_DATA *)panel_userptr(stack_top);
|
||||
top_panel(top->next);
|
||||
stack_top = top->next;
|
||||
top = (PANEL_DATA *)panel_userptr(stack_top);
|
||||
newx = top->x;
|
||||
newy = top->y;
|
||||
neww = top->w;
|
||||
newh = top->h;
|
||||
break;
|
||||
case 'r': /* Re-Size*/
|
||||
size = TRUE;
|
||||
attron(COLOR_PAIR(4));
|
||||
mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing");
|
||||
refresh();
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'm': /* Move */
|
||||
attron(COLOR_PAIR(4));
|
||||
mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving");
|
||||
refresh();
|
||||
attroff(COLOR_PAIR(4));
|
||||
move = TRUE;
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
if(size == TRUE)
|
||||
{ --newx;
|
||||
++neww;
|
||||
}
|
||||
if(move == TRUE)
|
||||
--newx;
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
if(size == TRUE)
|
||||
{ ++newx;
|
||||
--neww;
|
||||
}
|
||||
if(move == TRUE)
|
||||
++newx;
|
||||
break;
|
||||
case KEY_UP:
|
||||
if(size == TRUE)
|
||||
{ --newy;
|
||||
++newh;
|
||||
}
|
||||
if(move == TRUE)
|
||||
--newy;
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
if(size == TRUE)
|
||||
{ ++newy;
|
||||
--newh;
|
||||
}
|
||||
if(move == TRUE)
|
||||
++newy;
|
||||
break;
|
||||
case 10: /* Enter */
|
||||
move(LINES - 4, 0);
|
||||
clrtoeol();
|
||||
refresh();
|
||||
if(size == TRUE)
|
||||
{ old_win = panel_window(stack_top);
|
||||
temp_win = newwin(newh, neww, newy, newx);
|
||||
replace_panel(stack_top, temp_win);
|
||||
win_show(temp_win, top->label, top->label_color);
|
||||
delwin(old_win);
|
||||
size = FALSE;
|
||||
}
|
||||
if(move == TRUE)
|
||||
{ move_panel(stack_top, newy, newx);
|
||||
move = FALSE;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
attron(COLOR_PAIR(4));
|
||||
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
|
||||
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
|
||||
attroff(COLOR_PAIR(4));
|
||||
refresh();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set the PANEL_DATA structures for individual panels */
|
||||
void set_user_ptrs(PANEL **panels, int n)
|
||||
{ PANEL_DATA *ptrs;
|
||||
WINDOW *win;
|
||||
int x, y, w, h, i;
|
||||
char temp[80];
|
||||
|
||||
ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));
|
||||
|
||||
for(i = 0;i < n; ++i)
|
||||
{ win = panel_window(panels[i]);
|
||||
getbegyx(win, y, x);
|
||||
getmaxyx(win, h, w);
|
||||
ptrs[i].x = x;
|
||||
ptrs[i].y = y;
|
||||
ptrs[i].w = w;
|
||||
ptrs[i].h = h;
|
||||
sprintf(temp, "Window Number %d", i + 1);
|
||||
strcpy(ptrs[i].label, temp);
|
||||
ptrs[i].label_color = i + 1;
|
||||
if(i + 1 == n)
|
||||
ptrs[i].next = panels[0];
|
||||
else
|
||||
ptrs[i].next = panels[i + 1];
|
||||
set_panel_userptr(panels[i], &ptrs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* 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();
|
||||
}
|
38
PracticingCurses/panels/panel_simple.c
Normal file
38
PracticingCurses/panels/panel_simple.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <panel.h>
|
||||
|
||||
int main()
|
||||
{ WINDOW *my_wins[3];
|
||||
PANEL *my_panels[3];
|
||||
int lines = 10, cols = 40, y = 2, x = 4, i;
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
|
||||
/* Create windows for the panels */
|
||||
my_wins[0] = newwin(lines, cols, y, x);
|
||||
my_wins[1] = newwin(lines, cols, y + 1, x + 5);
|
||||
my_wins[2] = newwin(lines, cols, y + 2, x + 10);
|
||||
|
||||
/*
|
||||
* Create borders around the windows so that you can see the effect
|
||||
* of panels
|
||||
*/
|
||||
for(i = 0; i < 3; ++i)
|
||||
box(my_wins[i], 0, 0);
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* Update the stacking order. 2nd panel will be on top */
|
||||
update_panels();
|
||||
|
||||
/* Show it on the screen */
|
||||
doupdate();
|
||||
|
||||
getch();
|
||||
endwin();
|
||||
}
|
||||
|
17
PracticingCurses/perl/01.pl
Executable file
17
PracticingCurses/perl/01.pl
Executable file
@ -0,0 +1,17 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
use Curses;
|
||||
|
||||
initscr();
|
||||
printw("Hello world!");
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
33
PracticingCurses/perl/02.pl
Executable file
33
PracticingCurses/perl/02.pl
Executable file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
use Curses;
|
||||
|
||||
initscr();
|
||||
raw();
|
||||
keypad(1);
|
||||
noecho();
|
||||
|
||||
printw("Type any character to see it in bold\n");
|
||||
$ch = getch();
|
||||
|
||||
if ($ch == KEY_F(1)) {
|
||||
printw("F1 Key pressed");
|
||||
}
|
||||
else {
|
||||
printw("The pressed key is ");
|
||||
attron(A_BOLD);
|
||||
printw($ch);
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
23
PracticingCurses/perl/03.pl
Executable file
23
PracticingCurses/perl/03.pl
Executable file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
# We use addstr() instead of printw()
|
||||
|
||||
use Curses;
|
||||
|
||||
$mesg = "Just a string";
|
||||
|
||||
initscr();
|
||||
getmaxyx($row, $col);
|
||||
addstr($row / 2, ($col - length($mesg)) / 2, $mesg);
|
||||
addstr($row - 2, 0, "This screen has $row rows and $col columns\n");
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
23
PracticingCurses/perl/04.pl
Executable file
23
PracticingCurses/perl/04.pl
Executable file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
# We use addstr() instead of printw()
|
||||
|
||||
use Curses;
|
||||
|
||||
$mesg = "Enter a string: ";
|
||||
|
||||
initscr();
|
||||
getmaxyx($row, $col);
|
||||
addstr($row / 2, ($col - length($mesg)) / 2, $mesg);
|
||||
getstr($str);
|
||||
addstr($LINES - 2, 0, "You Entered: $str");
|
||||
getch();
|
||||
endwin();
|
||||
|
32
PracticingCurses/perl/05.pl
Executable file
32
PracticingCurses/perl/05.pl
Executable file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
# We first read all the input into an array and join
|
||||
# it to a single string with newlines.
|
||||
|
||||
use Curses;
|
||||
|
||||
initscr();
|
||||
|
||||
@lines = <>;
|
||||
$lines = join "", @lines;
|
||||
|
||||
while ($lines =~ /\G(.*?)(\/\*.*?\*\/)?/gs) {
|
||||
addstr($1);
|
||||
if ($2) {
|
||||
attron(A_BOLD);
|
||||
addstr($2);
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
}
|
||||
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
22
PracticingCurses/perl/06.pl
Executable file
22
PracticingCurses/perl/06.pl
Executable file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
use Curses;
|
||||
|
||||
initscr();
|
||||
start_color();
|
||||
|
||||
init_pair(1, COLOR_CYAN, COLOR_BLACK);
|
||||
printw("A Big string which i didn't care to type fully ");
|
||||
chgat(0, 0, -1, A_BLINK, 1, NULL);
|
||||
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
58
PracticingCurses/perl/07.pl
Executable file
58
PracticingCurses/perl/07.pl
Executable file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
use Curses;
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
keypad(1);
|
||||
|
||||
$height = 3;
|
||||
$width = 10;
|
||||
$starty = ($LINES - $height) / 2;
|
||||
$startx = ($COLS - $width) / 2;
|
||||
printw("Press F1 to exit");
|
||||
refresh();
|
||||
$my_win = create_newwin($height, $width, $starty, $startx);
|
||||
|
||||
while (($ch = getch()) != KEY_F(1)) {
|
||||
if ($ch == KEY_LEFT) {
|
||||
destroy_win($my_win);
|
||||
$my_win = create_newwin($height, $width, $starty, --$startx);
|
||||
}
|
||||
elsif ($ch == KEY_RIGHT) {
|
||||
destroy_win($my_win);
|
||||
$my_win = create_newwin($height, $width, $starty, ++$startx);
|
||||
}
|
||||
elsif ($ch == KEY_UP) {
|
||||
destroy_win($my_win);
|
||||
$my_win = create_newwin($height, $width, --$starty, $startx);
|
||||
}
|
||||
elsif ($ch == KEY_DOWN) {
|
||||
destroy_win($my_win);
|
||||
$my_win = create_newwin($height, $width, ++$starty, $startx);
|
||||
}
|
||||
}
|
||||
|
||||
endwin();
|
||||
|
||||
sub create_newwin {
|
||||
$local_win = newwin(shift, shift, shift, shift);
|
||||
box($local_win, 0, 0);
|
||||
refresh($local_win);
|
||||
return $local_win;
|
||||
}
|
||||
|
||||
sub destroy_win {
|
||||
$local_win = shift;
|
||||
border($local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
|
||||
refresh($local_win);
|
||||
delwin($local_win);
|
||||
}
|
||||
|
112
PracticingCurses/perl/08.pl
Executable file
112
PracticingCurses/perl/08.pl
Executable file
@ -0,0 +1,112 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
# We use %win hash to store window parameters
|
||||
|
||||
use Curses;
|
||||
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
|
||||
keypad(1);
|
||||
noecho();
|
||||
init_pair(1, COLOR_CYAN, COLOR_BLACK);
|
||||
|
||||
%win = ();
|
||||
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, 1);
|
||||
|
||||
while (($ch = getch()) != KEY_F(1)) {
|
||||
if ($ch == KEY_LEFT) {
|
||||
create_box(\%win, 0);
|
||||
$win{'startx'}--;
|
||||
create_box(\%win, 1);
|
||||
}
|
||||
elsif ($ch == KEY_RIGHT) {
|
||||
create_box(\%win, 0);
|
||||
$win{'startx'}++;
|
||||
create_box(\%win, 1);
|
||||
}
|
||||
elsif ($ch == KEY_UP) {
|
||||
create_box(\%win, 0);
|
||||
$win{'starty'}--;
|
||||
create_box(\%win, 1);
|
||||
}
|
||||
elsif ($ch == KEY_DOWN) {
|
||||
create_box(\%win, 0);
|
||||
$win{'starty'}++;
|
||||
create_box(\%win, 1);
|
||||
}
|
||||
}
|
||||
|
||||
endwin();
|
||||
|
||||
sub init_win_params {
|
||||
$p_win = shift;
|
||||
|
||||
$$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{'ls'} = '|';
|
||||
$$p_win{'rs'} = '|';
|
||||
$$p_win{'ts'} = '-';
|
||||
$$p_win{'bs'} = '-';
|
||||
$$p_win{'tl'} = '+';
|
||||
$$p_win{'tr'} = '+';
|
||||
$$p_win{'bl'} = '+';
|
||||
$$p_win{'br'} = '+';
|
||||
}
|
||||
|
||||
sub print_win_params {
|
||||
if ($_DEBUG) {
|
||||
$p_win = shift;
|
||||
addstr(25, 0,
|
||||
"$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}");
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
|
||||
sub create_box {
|
||||
$p_win = shift;
|
||||
$bool = shift;
|
||||
|
||||
$x = $$p_win{'startx'};
|
||||
$y = $$p_win{'starty'};
|
||||
$w = $$p_win{'width'};
|
||||
$h = $$p_win{'height'};
|
||||
|
||||
if ($bool) {
|
||||
addch($y, $x, $$p_win{'tl'});
|
||||
addch($y, $x + $w, $$p_win{'tr'});
|
||||
addch($y + $h, $x, $$p_win{'bl'});
|
||||
addch($y + $h, $x + $w, $$p_win{'br'});
|
||||
hline($y, $x + 1, $$p_win{'ts'}, $w - 1);
|
||||
hline($y + $h, $x + 1, $$p_win{'bs'}, $w - 1);
|
||||
vline($y + 1, $x, $$p_win{'ls'}, $h - 1);
|
||||
vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1);
|
||||
}
|
||||
else {
|
||||
for ($j = $y; $j <= $y + $h; $j++) {
|
||||
for ($i = $x; $i <= $x + $w; $i++) {
|
||||
addch($j, $i, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
47
PracticingCurses/perl/09.pl
Executable file
47
PracticingCurses/perl/09.pl
Executable file
@ -0,0 +1,47 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
use Curses;
|
||||
|
||||
initscr();
|
||||
unless (has_colors()) {
|
||||
endwin();
|
||||
print "Your terminal does not support color\n";
|
||||
exit 1;
|
||||
}
|
||||
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();
|
||||
|
||||
sub print_in_middle {
|
||||
$win = shift;
|
||||
$starty = shift;
|
||||
$startx = shift;
|
||||
$width = shift;
|
||||
$string = shift;
|
||||
|
||||
$win = stdscr unless ($win);
|
||||
|
||||
getyx($win, $y, $x);
|
||||
|
||||
$x = $startx if ($startx);
|
||||
$y = $starty if ($starty);
|
||||
$width = $COLS unless ($width);
|
||||
$length = length($string);
|
||||
$temp = ($width - $length) / 2;
|
||||
$x = $startx + $temp;
|
||||
addstr($y, $x, $string);
|
||||
refresh();
|
||||
}
|
||||
|
98
PracticingCurses/perl/10.pl
Executable file
98
PracticingCurses/perl/10.pl
Executable file
@ -0,0 +1,98 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Copyright (C) 2003 by Virtusa Corporation
|
||||
# http://www.virtusa.com
|
||||
#
|
||||
# Anuradha Ratnaweera
|
||||
# http://www.linux.lk/~anuradha/
|
||||
#
|
||||
|
||||
use Curses;
|
||||
|
||||
$width = 30;
|
||||
$height = 10;
|
||||
$startx = 0;
|
||||
$starty = 0;
|
||||
|
||||
@choices = (
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Exit"
|
||||
);
|
||||
|
||||
$n_choices = @choices;
|
||||
|
||||
$highlight = 1;
|
||||
$choice = 0;
|
||||
|
||||
initscr();
|
||||
clear();
|
||||
noecho();
|
||||
cbreak();
|
||||
$startx = ($COLS - $width) / 2;
|
||||
$starty = ($LINES - $height) / 2;
|
||||
|
||||
$menu_win = newwin($height, $width, $starty, $startx);
|
||||
keypad(1);
|
||||
keypad($menu_win, 1);
|
||||
addstr(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 = getch($menu_win);
|
||||
if ($c == KEY_UP) {
|
||||
if ($highlight == 1) {
|
||||
$highlight = $n_choices;
|
||||
}
|
||||
else {
|
||||
$highlight--;
|
||||
}
|
||||
}
|
||||
elsif ($c == KEY_DOWN) {
|
||||
if ($highlight == $n_choices) {
|
||||
$highlight = 1;
|
||||
}
|
||||
else {
|
||||
$highlight++;
|
||||
}
|
||||
}
|
||||
elsif ($c == '\n') {
|
||||
$choice = $highlight;
|
||||
}
|
||||
else {
|
||||
addstr($LINES - 2, 0, "Character pressed is $c");
|
||||
refresh();
|
||||
}
|
||||
print_menu($menu_win, $highlight);
|
||||
last if ($choice);
|
||||
}
|
||||
|
||||
addstr($LINES - 2, 0, "You chose choice $choice with choice string $choices[$choice-1]");
|
||||
clrtoeol();
|
||||
refresh();
|
||||
endwin();
|
||||
|
||||
sub print_menu {
|
||||
$menu_win = shift;
|
||||
$highlight = shift;
|
||||
|
||||
$x = 2;
|
||||
$y = 2;
|
||||
box($menu_win, 0, 0);
|
||||
for ($i = 0; $i < $n_choices; $i++) {
|
||||
if ($highlight == $i + 1) {
|
||||
attron($menu_win, A_REVERSE);
|
||||
addstr($menu_win, $y, $x, $choices[$i]);
|
||||
attroff($menu_win, A_REVERSE);
|
||||
}
|
||||
else {
|
||||
addstr($menu_win, $y, $x, $choices[$i]);
|
||||
}
|
||||
$y++;
|
||||
}
|
||||
refresh($menu_win);
|
||||
}
|
||||
|
2
PracticingCurses/perl/COPYING
Normal file
2
PracticingCurses/perl/COPYING
Normal file
@ -0,0 +1,2 @@
|
||||
These programmes can be distributed under the same terms as
|
||||
ncurses (MIT style license).
|
6
PracticingCurses/perl/README
Normal file
6
PracticingCurses/perl/README
Normal file
@ -0,0 +1,6 @@
|
||||
The license was changed from GNU/GPL to MIT style license
|
||||
which is of course compatible with GPL, in order to include
|
||||
them in the official ncurses-HOWTO.
|
||||
|
||||
Anuradha Ratnaweera (anuradha at gnu org OR gnu.slash.linux at
|
||||
gmail com OR anuradha at linux lk).
|
Loading…
Reference in New Issue
Block a user