Revert "doing some find+sed cleanup"

This reverts commit f4c7e129df8cdab960a08987930cf85f0e5b88b5.
This commit is contained in:
Dan Buch 2009-11-18 19:37:27 -05:00
parent f4c7e129df
commit c77722c60f
51 changed files with 3373 additions and 3373 deletions

View File

@ -9,20 +9,20 @@ SRC_DIR=.
EXE_DIR=../demo/exe EXE_DIR=../demo/exe
EXES = \ EXES = \
${EXE_DIR}/hanoi \ ${EXE_DIR}/hanoi \
${EXE_DIR}/life\ ${EXE_DIR}/life\
${EXE_DIR}/magic \ ${EXE_DIR}/magic \
${EXE_DIR}/queens \ ${EXE_DIR}/queens \
${EXE_DIR}/shuffle \ ${EXE_DIR}/shuffle \
${EXE_DIR}/tt ${EXE_DIR}/tt
${EXE_DIR}/%: %.o ${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS} ${CC} -o $@ $< ${LIBS}
%.o: ${SRC_DIR}/%.c %.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $< ${CC} -o $@ -c $<
all: ${EXES} all: ${EXES}
clean: clean:
@rm -f ${EXES} @rm -f ${EXES}

View File

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

View File

@ -7,9 +7,9 @@
#define TIME_OUT 300 #define TIME_OUT 300
typedef struct _peg_struct { typedef struct _peg_struct {
int n_discs; /* Number of discs at present */ int n_discs; /* Number of discs at present */
int bottomx, bottomy; /* bottom x, bottom y co-ord */ int bottomx, bottomy; /* bottom x, bottom y co-ord */
int *sizes; /* The disc sizes array */ int *sizes; /* The disc sizes array */
}peg; }peg;
void init_pegs(peg *p_my_pegs, int n_discs); void init_pegs(peg *p_my_pegs, int n_discs);
@ -24,155 +24,155 @@ int store_n_discs;
char *welcome_string = "Enter the number of discs you want to be solved: "; char *welcome_string = "Enter the number of discs you want to be solved: ";
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ int n_discs; { int n_discs;
peg my_pegs[3]; peg my_pegs[3];
initscr(); /* Start curses mode */ initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled. Pass on every thing */ cbreak(); /* Line buffering disabled. Pass on every thing */
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
curs_set(FALSE); curs_set(FALSE);
print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); print_in_middle(0, LINES / 2, COLS, welcome_string, NULL);
scanw("%d", &n_discs); scanw("%d", &n_discs);
timeout(TIME_OUT); timeout(TIME_OUT);
noecho(); noecho();
store_n_discs = n_discs; store_n_discs = n_discs;
init_pegs(my_pegs, n_discs); init_pegs(my_pegs, n_discs);
show_pegs(stdscr, my_pegs, n_discs); show_pegs(stdscr, my_pegs, n_discs);
solve_hanoi(my_pegs, n_discs, 0, 1, 2); solve_hanoi(my_pegs, n_discs, 0, 1, 2);
free_pegs(my_pegs, n_discs); free_pegs(my_pegs, n_discs);
endwin(); /* End curses mode */ endwin(); /* End curses mode */
return 0; return 0;
} }
void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst)
{ if(n_discs == 0) { if(n_discs == 0)
return; return;
solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux);
move_disc(p_my_pegs, store_n_discs, src, dst); move_disc(p_my_pegs, store_n_discs, src, dst);
show_pegs(stdscr, p_my_pegs, store_n_discs); show_pegs(stdscr, p_my_pegs, store_n_discs);
check_usr_response(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); solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst);
} }
void check_usr_response(peg *p_my_pegs, int n_discs) void check_usr_response(peg *p_my_pegs, int n_discs)
{ int ch; { int ch;
ch = getch(); /* Waits for TIME_OUT milliseconds */ ch = getch(); /* Waits for TIME_OUT milliseconds */
if(ch == ERR) if(ch == ERR)
return; return;
else else
if(ch == KEY_F(1)) if(ch == KEY_F(1))
{ free_pegs(p_my_pegs, n_discs); { free_pegs(p_my_pegs, n_discs);
endwin(); endwin();
exit(0); exit(0);
} }
} }
void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) void move_disc(peg *p_my_pegs, int n_discs, int src, int dst)
{ int temp, index; { int temp, index;
--p_my_pegs[src].n_discs; --p_my_pegs[src].n_discs;
index = 0; index = 0;
while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) while(p_my_pegs[src].sizes[index] == 0 && index != n_discs)
++index; ++index;
temp = p_my_pegs[src].sizes[index]; temp = p_my_pegs[src].sizes[index];
p_my_pegs[src].sizes[index] = 0; p_my_pegs[src].sizes[index] = 0;
index = 0; index = 0;
while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs)
++index; ++index;
--index; --index;
p_my_pegs[dst].sizes[index] = temp; p_my_pegs[dst].sizes[index] = temp;
++p_my_pegs[dst].n_discs; ++p_my_pegs[dst].n_discs;
} }
void init_pegs(peg *p_my_pegs, int n_discs) void init_pegs(peg *p_my_pegs, int n_discs)
{ int size, temp, i; { int size, temp, i;
p_my_pegs[0].n_discs = n_discs; p_my_pegs[0].n_discs = n_discs;
/* Allocate memory for size array /* Allocate memory for size array
* atmost the number of discs on a peg can be n_discs * atmost the number of discs on a peg can be n_discs
*/ */
for(i = 0; i < n_discs; ++i) for(i = 0; i < n_discs; ++i)
p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int));
size = 3; size = 3;
for(i = 0;i < n_discs; ++i, size += 2) for(i = 0;i < n_discs; ++i, size += 2)
p_my_pegs[0].sizes[i] = size; p_my_pegs[0].sizes[i] = size;
temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); temp = (p_my_pegs[0].sizes[n_discs - 1] / 2);
p_my_pegs[0].bottomx = POSX + 1 + temp; p_my_pegs[0].bottomx = POSX + 1 + temp;
p_my_pegs[0].bottomy = POSY + 2 + n_discs; 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].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp;
p_my_pegs[1].bottomy = POSY + 2 + n_discs; 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].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp;
p_my_pegs[2].bottomy = POSY + 2 + n_discs; p_my_pegs[2].bottomy = POSY + 2 + n_discs;
} }
void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs)
{ int i, j, k, x, y, size; { int i, j, k, x, y, size;
wclear(win); wclear(win);
attron(A_REVERSE); attron(A_REVERSE);
mvprintw(24, 0, "Press F1 to Exit"); mvprintw(24, 0, "Press F1 to Exit");
attroff(A_REVERSE); attroff(A_REVERSE);
for(i = 0;i < 3; ++i) for(i = 0;i < 3; ++i)
mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1,
p_my_pegs[i].bottomx, "%c", PEG_CHAR); p_my_pegs[i].bottomx, "%c", PEG_CHAR);
y = p_my_pegs[0].bottomy - n_discs; y = p_my_pegs[0].bottomy - n_discs;
for(i = 0; i < 3; ++i) /* For each peg */ for(i = 0; i < 3; ++i) /* For each peg */
{ for(j = 0; j < n_discs; ++ j) /* For each row */ { for(j = 0; j < n_discs; ++ j) /* For each row */
{ if(p_my_pegs[i].sizes[j] != 0) { if(p_my_pegs[i].sizes[j] != 0)
{ size = p_my_pegs[i].sizes[j]; { size = p_my_pegs[i].sizes[j];
x = p_my_pegs[i].bottomx - (size / 2); x = p_my_pegs[i].bottomx - (size / 2);
for(k = 0; k < size; ++k) for(k = 0; k < size; ++k)
mvwprintw(win, y, x + k, "%c", DISC_CHAR); mvwprintw(win, y, x + k, "%c", DISC_CHAR);
} }
else else
mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR);
++y; ++y;
} }
y = p_my_pegs[0].bottomy - n_discs; y = p_my_pegs[0].bottomy - n_discs;
} }
wrefresh(win); wrefresh(win);
} }
void free_pegs(peg *p_my_pegs, int n_discs) void free_pegs(peg *p_my_pegs, int n_discs)
{ int i; { int i;
for(i = 0;i < n_discs; ++i) for(i = 0;i < n_discs; ++i)
free(p_my_pegs[i].sizes); free(p_my_pegs[i].sizes);
} }
/* -------------------------------------------------------------* /* -------------------------------------------------------------*
* startx = 0 means at present x * * startx = 0 means at present x *
* starty = 0 means at present y * * starty = 0 means at present y *
* win = NULL means take stdscr * * win = NULL means take stdscr *
* -------------------------------------------------------------*/ * -------------------------------------------------------------*/
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
refresh(); refresh();
} }

View File

@ -9,8 +9,8 @@ int ENDY = 24;
#define TIME_OUT 300 #define TIME_OUT 300
typedef struct _state { typedef struct _state {
int oldstate; int oldstate;
int newstate; int newstate;
}state; }state;
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy);
@ -18,90 +18,90 @@ void calc(state **area, int x, int y);
void update_state(state **area, int startx, int starty, int endx, int endy); void update_state(state **area, int startx, int starty, int endx, int endy);
int main() int main()
{ state **workarea; { state **workarea;
int i, j; int i, j;
initscr(); initscr();
cbreak(); cbreak();
timeout(TIME_OUT); timeout(TIME_OUT);
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
ENDX = COLS - 1; ENDX = COLS - 1;
ENDY = LINES - 1; ENDY = LINES - 1;
workarea = (state **)calloc(COLS, sizeof(state *)); workarea = (state **)calloc(COLS, sizeof(state *));
for(i = 0;i < COLS; ++i) for(i = 0;i < COLS; ++i)
workarea[i] = (state *)calloc(LINES, sizeof(state)); workarea[i] = (state *)calloc(LINES, sizeof(state));
/* For inverted U */ /* For inverted U */
workarea[39][15].newstate = TRUE; workarea[39][15].newstate = TRUE;
workarea[40][15].newstate = TRUE; workarea[40][15].newstate = TRUE;
workarea[41][15].newstate = TRUE; workarea[41][15].newstate = TRUE;
workarea[39][16].newstate = TRUE; workarea[39][16].newstate = TRUE;
workarea[39][17].newstate = TRUE; workarea[39][17].newstate = TRUE;
workarea[41][16].newstate = TRUE; workarea[41][16].newstate = TRUE;
workarea[41][17].newstate = TRUE; workarea[41][17].newstate = TRUE;
update_state(workarea, STARTX, STARTY, ENDX, ENDY); update_state(workarea, STARTX, STARTY, ENDX, ENDY);
/* For block */ /* For block */
/* /*
workarea[37][13].newstate = TRUE; workarea[37][13].newstate = TRUE;
workarea[37][14].newstate = TRUE; workarea[37][14].newstate = TRUE;
workarea[38][13].newstate = TRUE; workarea[38][13].newstate = TRUE;
workarea[38][14].newstate = TRUE; workarea[38][14].newstate = TRUE;
update_state(workarea, STARTX, STARTY, ENDX, ENDY); update_state(workarea, STARTX, STARTY, ENDX, ENDY);
*/ */
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
while(getch() != KEY_F(1)) while(getch() != KEY_F(1))
{ for(i = STARTX; i <= ENDX; ++i) { for(i = STARTX; i <= ENDX; ++i)
for(j = STARTY; j <= ENDY; ++j) for(j = STARTY; j <= ENDY; ++j)
calc(workarea, i, j); calc(workarea, i, j);
update_state(workarea, STARTX, STARTY, ENDX, ENDY); update_state(workarea, STARTX, STARTY, ENDX, ENDY);
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
} }
endwin(); endwin();
return 0; return 0;
} }
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy)
{ int i, j; { int i, j;
wclear(win); wclear(win);
for(i = startx; i <= endx; ++i) for(i = startx; i <= endx; ++i)
for(j = starty;j <= endy; ++j) for(j = starty;j <= endy; ++j)
if(area[i][j].newstate == TRUE) if(area[i][j].newstate == TRUE)
mvwaddch(win, j, i, CELL_CHAR); mvwaddch(win, j, i, CELL_CHAR);
wrefresh(win); wrefresh(win);
} }
void calc(state **area, int i, int j) void calc(state **area, int i, int j)
{ int neighbours; { int neighbours;
int newstate; int newstate;
neighbours = neighbours =
area[(i - 1 + COLS) % COLS][j].oldstate + 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) % LINES].oldstate +
area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate +
area[(i + 1) % COLS][j].oldstate + area[(i + 1) % COLS][j].oldstate +
area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate +
area[(i + 1) % COLS][(j + 1) % LINES].oldstate + area[(i + 1) % COLS][(j + 1) % LINES].oldstate +
area[i][(j - 1 + LINES) % LINES].oldstate + area[i][(j - 1 + LINES) % LINES].oldstate +
area[i][(j + 1) % LINES].oldstate; area[i][(j + 1) % LINES].oldstate;
newstate = FALSE; newstate = FALSE;
if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3))
newstate = TRUE; newstate = TRUE;
else else
if(area[i][j].oldstate == FALSE && neighbours == 3) if(area[i][j].oldstate == FALSE && neighbours == 3)
newstate = TRUE; newstate = TRUE;
area[i][j].newstate = newstate; area[i][j].newstate = newstate;
} }
void update_state(state **area, int startx, int starty, int endx, int endy) void update_state(state **area, int startx, int starty, int endx, int endy)
{ int i, j; { int i, j;
for(i = startx; i <= endx; ++i) for(i = startx; i <= endx; ++i)
for(j = starty; j <= endy; ++j) for(j = starty; j <= endy; ++j)
area[i][j].oldstate = area[i][j].newstate; area[i][j].oldstate = area[i][j].newstate;
} }

View File

@ -8,8 +8,8 @@
#define TRACE_VALUE TRACE_MAXIMUM #define TRACE_VALUE TRACE_MAXIMUM
void board( WINDOW *win, int starty, int startx, int lines, int cols, void board( WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height); int tile_width, int tile_height);
void magic(int **, int); void magic(int **, int);
void print(int **, int); void print(int **, int);
void magic_board(int **a,int n); void magic_board(int **a,int n);
@ -17,145 +17,145 @@ void magic_board(int **a,int n);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int **a,n,i; int **a,n,i;
if(argc != 2) if(argc != 2)
{ printf("Usage: %s <magic sqaure order>\n", argv[0]); { printf("Usage: %s <magic sqaure order>\n", argv[0]);
exit(0); exit(0);
} }
n = atoi(argv[1]); n = atoi(argv[1]);
if(n % 2 == 0) if(n % 2 == 0)
{ printf("Sorry !!! I don't know how to create magic square of even order\n"); { printf("Sorry !!! I don't know how to create magic square of even order\n");
printf("The order should be an odd number\n"); printf("The order should be an odd number\n");
exit(0); exit(0);
} }
a = (int **) malloc(n * sizeof(int*)); a = (int **) malloc(n * sizeof(int*));
for(i = 0;i < n;++i) for(i = 0;i < n;++i)
a[i] = (int *)malloc(n * sizeof(int)); a[i] = (int *)malloc(n * sizeof(int));
magic(a,n); magic(a,n);
initscr(); initscr();
curs_set(0); curs_set(0);
noecho(); noecho();
magic_board(a,n); magic_board(a,n);
getch(); getch();
endwin(); endwin();
return; return;
} }
void magic(int **a, int n) void magic(int **a, int n)
{ {
int i,j,k; int i,j,k;
int row,col; int row,col;
for(i = 0;i < n;++i) for(i = 0;i < n;++i)
for(j = 0;j < n;++j) for(j = 0;j < n;++j)
a[i][j] = -1; a[i][j] = -1;
row = 0; row = 0;
col = n / 2; col = n / 2;
k = 1; k = 1;
a[row][col] = k; a[row][col] = k;
while(k != n * n) while(k != n * n)
{ {
if(row == 0 && col != n - 1) if(row == 0 && col != n - 1)
{ row = n - 1; { row = n - 1;
col ++; col ++;
a[row][col] = ++k; a[row][col] = ++k;
} }
else if(row != 0 && col != n - 1) else if(row != 0 && col != n - 1)
{ if(a[row - 1][col + 1] == -1) { if(a[row - 1][col + 1] == -1)
{ row --; { row --;
col ++; col ++;
a[row][col] = ++k; a[row][col] = ++k;
} }
else else
{ {
row ++; row ++;
a[row][col] = ++k; a[row][col] = ++k;
} }
} }
else if(row != 0 && col == n - 1) else if(row != 0 && col == n - 1)
{ {
row --; row --;
col = 0; col = 0;
a[row][col] = ++k; a[row][col] = ++k;
} }
else if(row == 0 && col == n - 1) else if(row == 0 && col == n - 1)
{ row ++; { row ++;
a[row][col] = ++k; a[row][col] = ++k;
} }
} }
return; return;
} }
void print(int **a,int n) void print(int **a,int n)
{ int i,j; { int i,j;
int x,y; int x,y;
x = STARTX; x = STARTX;
y = STARTY; y = STARTY;
mvprintw(1,30,"MAGIC SQUARE"); mvprintw(1,30,"MAGIC SQUARE");
for(i = 0;i < n;++i) for(i = 0;i < n;++i)
{ for(j = 0;j < n;++j) { for(j = 0;j < n;++j)
{ mvprintw(y,x,"%d",a[i][j]); { mvprintw(y,x,"%d",a[i][j]);
if(n > 9) if(n > 9)
x += 4; x += 4;
else else
x += 6; x += 6;
} }
x = STARTX; x = STARTX;
if(n > 7) if(n > 7)
y += 2; y += 2;
else else
y += 3; y += 3;
} }
refresh(); refresh();
} }
void board(WINDOW *win, int starty, int startx, int lines, int cols, void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height) int tile_width, int tile_height)
{ int endy, endx, i, j; { int endy, endx, i, j;
endy = starty + lines * tile_height; endy = starty + lines * tile_height;
endx = startx + cols * tile_width; endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height) for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i) for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE); mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width) for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j) for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE); mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER); mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER); mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER); mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER); mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE); { mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE); mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS); mvwaddch(win, j, i, ACS_PLUS);
} }
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE); { mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE); mvwaddch(win, endy, i, ACS_BTEE);
} }
wrefresh(win); wrefresh(win);
} }
void magic_board(int **a,int n) void magic_board(int **a,int n)
{ int i,j, deltax, deltay; { int i,j, deltax, deltay;
int startx, starty; int startx, starty;
starty = (LINES - n * HEIGHT) / 2; starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2; startx = (COLS - n * WIDTH) / 2;
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2; deltay = HEIGHT / 2;
deltax = WIDTH / 2; deltax = WIDTH / 2;
for(i = 0;i < n; ++i) for(i = 0;i < n; ++i)
for(j = 0; j < n; ++j) for(j = 0; j < n; ++j)
mvprintw(starty + j * HEIGHT + deltay, mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax, startx + i * WIDTH + deltax,
"%d", a[i][j]); "%d", a[i][j]);
} }

View File

@ -7,116 +7,116 @@ int *nqueens(int num);
int place(int current, int *position); int place(int current, int *position);
int print(int *positions, int num_queens); int print(int *positions, int num_queens);
void board(WINDOW *win, int starty, int startx, int lines, int cols, void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height); int tile_width, int tile_height);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int num_queens, *positions, count; int num_queens, *positions, count;
if(argc != 2) if(argc != 2)
{ printf("Usage: %s <number of queens (chess board order)>\n", argv[0]); { printf("Usage: %s <number of queens (chess board order)>\n", argv[0]);
exit(1); exit(1);
} }
num_queens = atoi(argv[1]); num_queens = atoi(argv[1]);
initscr(); initscr();
cbreak(); cbreak();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
positions = nqueens(num_queens); positions = nqueens(num_queens);
free(positions); free(positions);
endwin(); endwin();
return 0; return 0;
} }
int *nqueens(int num) int *nqueens(int num)
{ {
int current, *position, num_solutions = 0; int current, *position, num_solutions = 0;
position = (int *) calloc(num + 1, sizeof(int)); position = (int *) calloc(num + 1, sizeof(int));
position[1] = 0; position[1] = 0;
current = 1; /* current queen is being checked */ current = 1; /* current queen is being checked */
/* position[current] is the coloumn*/ /* position[current] is the coloumn*/
while(current > 0){ while(current > 0){
position[current] += 1; position[current] += 1;
while(position[current] <= num && !place(current, position) ) while(position[current] <= num && !place(current, position) )
position[current] += 1; position[current] += 1;
if(position[current] <= num){ if(position[current] <= num){
if(current == num) { if(current == num) {
++num_solutions; ++num_solutions;
print(position, num); print(position, num);
} }
else { else {
current += 1; current += 1;
position[current] = 0; position[current] = 0;
} }
} }
else current -= 1; /* backtrack */ else current -= 1; /* backtrack */
} }
printf("Total Number of Solutions : %d\n", num_solutions); printf("Total Number of Solutions : %d\n", num_solutions);
return(position); return(position);
} }
int place(int current, int *position) int place(int current, int *position)
{ {
int i; int i;
if(current == 1) return(1); if(current == 1) return(1);
for(i = 1; i < current; ++i) for(i = 1; i < current; ++i)
if(position[i] == position[current]) return(0); if(position[i] == position[current]) return(0);
else if(abs(position[i] - position[current]) == else if(abs(position[i] - position[current]) ==
abs(i - current)) abs(i - current))
return(0); return(0);
return(1); return(1);
} }
int print(int *positions, int num_queens) int print(int *positions, int num_queens)
{ int count; { int count;
int y = 2, x = 2, w = 4, h = 2; int y = 2, x = 2, w = 4, h = 2;
static int solution = 1; static int solution = 1;
mvprintw(0, 0, "Solution No: %d", solution++); mvprintw(0, 0, "Solution No: %d", solution++);
board(stdscr, y, x, num_queens, num_queens, w, h); board(stdscr, y, x, num_queens, num_queens, w, h);
for(count = 1; count <= num_queens; ++count) for(count = 1; count <= num_queens; ++count)
{ int tempy = y + (count - 1) * h + h / 2; { int tempy = y + (count - 1) * h + h / 2;
int tempx = x + (positions[count] - 1) * w + w / 2; int tempx = x + (positions[count] - 1) * w + w / 2;
mvaddch(tempy, tempx, QUEEN_CHAR); mvaddch(tempy, tempx, QUEEN_CHAR);
} }
refresh(); refresh();
mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)");
if(getch() == KEY_F(1)) if(getch() == KEY_F(1))
{ endwin(); { endwin();
exit(0); exit(0);
} }
clear(); clear();
} }
void board(WINDOW *win, int starty, int startx, int lines, int cols, void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height) int tile_width, int tile_height)
{ int endy, endx, i, j; { int endy, endx, i, j;
endy = starty + lines * tile_height; endy = starty + lines * tile_height;
endx = startx + cols * tile_width; endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height) for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i) for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE); mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width) for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j) for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE); mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER); mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER); mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER); mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER); mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE); { mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE); mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS); mvwaddch(win, j, i, ACS_PLUS);
} }
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE); { mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE); mvwaddch(win, endy, i, ACS_BTEE);
} }
wrefresh(win); wrefresh(win);
} }

View File

@ -8,13 +8,13 @@
#define BLANK 0 #define BLANK 0
typedef struct _tile { typedef struct _tile {
int x; int x;
int y; int y;
}tile; }tile;
void init_board(int **board, int n, tile *blank); void init_board(int **board, int n, tile *blank);
void board(WINDOW *win, int starty, int startx, int lines, int cols, void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height); int tile_width, int tile_height);
void shuffle_board(int **board, int n); void shuffle_board(int **board, int n);
void move_blank(int direction, int **s_board, int n, tile *blank); void move_blank(int direction, int **s_board, int n, tile *blank);
int check_win(int **s_board, int n, tile *blank); int check_win(int **s_board, int n, tile *blank);
@ -22,184 +22,184 @@ int check_win(int **s_board, int n, tile *blank);
enum { LEFT, RIGHT, UP, DOWN }; enum { LEFT, RIGHT, UP, DOWN };
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ int **s_board; { int **s_board;
int n, i, ch; int n, i, ch;
tile blank; tile blank;
if(argc != 2) if(argc != 2)
{ printf("Usage: %s <shuffle board order>\n", argv[0]); { printf("Usage: %s <shuffle board order>\n", argv[0]);
exit(1); exit(1);
} }
n = atoi(argv[1]); n = atoi(argv[1]);
s_board = (int **)calloc(n, sizeof(int *)); s_board = (int **)calloc(n, sizeof(int *));
for(i = 0;i < n; ++i) for(i = 0;i < n; ++i)
s_board[i] = (int *)calloc(n, sizeof(int)); s_board[i] = (int *)calloc(n, sizeof(int));
init_board(s_board, n, &blank); init_board(s_board, n, &blank);
initscr(); initscr();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
cbreak(); cbreak();
shuffle_board(s_board, n); shuffle_board(s_board, n);
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case KEY_LEFT: { case KEY_LEFT:
move_blank(RIGHT, s_board, n, &blank); move_blank(RIGHT, s_board, n, &blank);
break; break;
case KEY_RIGHT: case KEY_RIGHT:
move_blank(LEFT, s_board, n, &blank); move_blank(LEFT, s_board, n, &blank);
break; break;
case KEY_UP: case KEY_UP:
move_blank(DOWN, s_board, n, &blank); move_blank(DOWN, s_board, n, &blank);
break; break;
case KEY_DOWN: case KEY_DOWN:
move_blank(UP, s_board, n, &blank); move_blank(UP, s_board, n, &blank);
break; break;
} }
shuffle_board(s_board, n); shuffle_board(s_board, n);
if(check_win(s_board, n, &blank) == TRUE) if(check_win(s_board, n, &blank) == TRUE)
{ mvprintw(24, 0, "You Win !!!\n"); { mvprintw(24, 0, "You Win !!!\n");
refresh(); refresh();
break; break;
} }
} }
endwin(); endwin();
return 0; return 0;
} }
void move_blank(int direction, int **s_board, int n, tile *blank) void move_blank(int direction, int **s_board, int n, tile *blank)
{ int temp; { int temp;
switch(direction) switch(direction)
{ case LEFT: { case LEFT:
{ if(blank->x != 0) { if(blank->x != 0)
{ --blank->x; { --blank->x;
temp = s_board[blank->x][blank->y]; temp = s_board[blank->x][blank->y];
s_board[blank->x + 1][blank->y] = temp; s_board[blank->x + 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK; s_board[blank->x][blank->y] = BLANK;
} }
} }
break; break;
case RIGHT: case RIGHT:
{ if(blank->x != n - 1) { if(blank->x != n - 1)
{ ++blank->x; { ++blank->x;
temp = s_board[blank->x][blank->y]; temp = s_board[blank->x][blank->y];
s_board[blank->x - 1][blank->y] = temp; s_board[blank->x - 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK; s_board[blank->x][blank->y] = BLANK;
} }
} }
break; break;
case UP: case UP:
{ if(blank->y != 0) { if(blank->y != 0)
{ --blank->y; { --blank->y;
temp = s_board[blank->x][blank->y]; temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y + 1] = temp; s_board[blank->x][blank->y + 1] = temp;
s_board[blank->x][blank->y] = BLANK; s_board[blank->x][blank->y] = BLANK;
} }
} }
break; break;
case DOWN: case DOWN:
{ if(blank->y != n - 1) { if(blank->y != n - 1)
{ ++blank->y; { ++blank->y;
temp = s_board[blank->x][blank->y]; temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y - 1] = temp; s_board[blank->x][blank->y - 1] = temp;
s_board[blank->x][blank->y] = BLANK; s_board[blank->x][blank->y] = BLANK;
} }
} }
break; break;
} }
} }
int check_win(int **s_board, int n, tile *blank) int check_win(int **s_board, int n, tile *blank)
{ int i, j; { int i, j;
s_board[blank->x][blank->y] = n * n; s_board[blank->x][blank->y] = n * n;
for(i = 0;i < n; ++i) for(i = 0;i < n; ++i)
for(j = 0;j < n; ++j) for(j = 0;j < n; ++j)
if(s_board[i][j] != j * n + i + 1) if(s_board[i][j] != j * n + i + 1)
{ s_board[blank->x][blank->y] = BLANK; { s_board[blank->x][blank->y] = BLANK;
return FALSE; return FALSE;
} }
s_board[blank->x][blank->y] = BLANK; s_board[blank->x][blank->y] = BLANK;
return TRUE; return TRUE;
} }
void init_board(int **s_board, int n, tile *blank) void init_board(int **s_board, int n, tile *blank)
{ int i, j, k; { int i, j, k;
int *temp_board; int *temp_board;
temp_board = (int *)calloc(n * n, sizeof(int)); temp_board = (int *)calloc(n * n, sizeof(int));
srand(time(NULL)); srand(time(NULL));
for(i = 0;i < n * n; ++i) for(i = 0;i < n * n; ++i)
{ {
repeat : repeat :
k = rand() % (n * n); k = rand() % (n * n);
for(j = 0;j <= i - 1; ++j) for(j = 0;j <= i - 1; ++j)
if (k == temp_board[j]) if (k == temp_board[j])
goto repeat; goto repeat;
else else
temp_board[i] = k; temp_board[i] = k;
} }
k = 0; k = 0;
for (i = 0;i < n;++i) for (i = 0;i < n;++i)
for(j = 0;j < n; ++j,++k) for(j = 0;j < n; ++j,++k)
{ if(temp_board[k] == 0) { if(temp_board[k] == 0)
{ blank->x = i; { blank->x = i;
blank->y = j; blank->y = j;
} }
s_board[i][j] = temp_board[k]; s_board[i][j] = temp_board[k];
} }
free(temp_board); free(temp_board);
} }
void board(WINDOW *win, int starty, int startx, int lines, int cols, void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height) int tile_width, int tile_height)
{ int endy, endx, i, j; { int endy, endx, i, j;
endy = starty + lines * tile_height; endy = starty + lines * tile_height;
endx = startx + cols * tile_width; endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height) for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i) for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE); mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width) for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j) for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE); mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER); mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER); mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER); mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER); mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE); { mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE); mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS); mvwaddch(win, j, i, ACS_PLUS);
} }
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE); { mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE); mvwaddch(win, endy, i, ACS_BTEE);
} }
wrefresh(win); wrefresh(win);
} }
void shuffle_board(int **s_board, int n) void shuffle_board(int **s_board, int n)
{ int i,j, deltax, deltay; { int i,j, deltax, deltay;
int startx, starty; int startx, starty;
starty = (LINES - n * HEIGHT) / 2; starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2; startx = (COLS - n * WIDTH) / 2;
clear(); clear();
mvprintw(24, 0, "Press F1 to Exit"); mvprintw(24, 0, "Press F1 to Exit");
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2; deltay = HEIGHT / 2;
deltax = WIDTH / 2; deltax = WIDTH / 2;
for(j = 0; j < n; ++j) for(j = 0; j < n; ++j)
for(i = 0;i < n; ++i) for(i = 0;i < n; ++i)
if(s_board[i][j] != BLANK) if(s_board[i][j] != BLANK)
mvprintw(starty + j * HEIGHT + deltay, mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax, startx + i * WIDTH + deltax,
"%-2d", s_board[i][j]); "%-2d", s_board[i][j]);
refresh(); refresh();
} }

View File

@ -20,204 +20,204 @@ 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); void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win);
char *groups[] = { "`123456" , char *groups[] = { "`123456" ,
"7890-=" , "7890-=" ,
"~!@#$%^" , "~!@#$%^" ,
"&*()_+" , "&*()_+" ,
"<>?" , "<>?" ,
",./\\" , ",./\\" ,
"asdfg", "asdfg",
"jkl;'", "jkl;'",
"qwer", "qwer",
"uiop", "uiop",
"tyur", "tyur",
"zxcv", "zxcv",
"bnm", "bnm",
}; };
int n_groups; int n_groups;
int main() int main()
{ int choice, i; { int choice, i;
char *test_array; char *test_array;
int ch = KEY_F1; int ch = KEY_F1;
int mistakes; int mistakes;
int x, y; int x, y;
time_t start_t, end_t; time_t start_t, end_t;
WINDOW *typing_win; WINDOW *typing_win;
char string[80]; char string[80];
string[0] = '\0'; string[0] = '\0';
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
intrflush(stdscr, FALSE); intrflush(stdscr, FALSE);
srandom(time(NULL)); srandom(time(NULL));
n_groups = sizeof(groups) / sizeof(char *); n_groups = sizeof(groups) / sizeof(char *);
test_array = (char *)calloc(HSIZE + 1, sizeof(char)); test_array = (char *)calloc(HSIZE + 1, sizeof(char));
while(1) while(1)
{ {
if(ch == KEY_F1) if(ch == KEY_F1)
{ choice = print_menu(); { choice = print_menu();
choice -= 1; choice -= 1;
if(choice == n_groups) if(choice == n_groups)
{ print_byebye(); { print_byebye();
free(test_array); free(test_array);
endwin(); endwin();
exit(0); exit(0);
} }
} }
clear(); clear();
strcpy(string, "Typing window"); strcpy(string, "Typing window");
print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL);
attron(A_REVERSE); attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu");
refresh(); refresh();
attroff(A_REVERSE); attroff(A_REVERSE);
create_test_string(test_array, choice); create_test_string(test_array, choice);
typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX);
keypad(typing_win, TRUE); keypad(typing_win, TRUE);
intrflush(typing_win, FALSE); intrflush(typing_win, FALSE);
box(typing_win, 0, 0); box(typing_win, 0, 0);
x = 1; x = 1;
y = 1; y = 1;
mvwprintw(typing_win, y, x, "%s", test_array); mvwprintw(typing_win, y, x, "%s", test_array);
wrefresh(typing_win); wrefresh(typing_win);
y += 1; y += 1;
mistakes = 0; mistakes = 0;
i = 0; i = 0;
time(&start_t); time(&start_t);
wmove(typing_win, y, x); wmove(typing_win, y, x);
wrefresh(typing_win); wrefresh(typing_win);
ch = 0; ch = 0;
while(ch != KEY_F1 && i != HSIZE + 1) while(ch != KEY_F1 && i != HSIZE + 1)
{ ch = wgetch(typing_win); { ch = wgetch(typing_win);
mvwprintw(typing_win, y, x, "%c", ch); mvwprintw(typing_win, y, x, "%c", ch);
wrefresh(typing_win); wrefresh(typing_win);
++x; ++x;
if(ch == test_array[i]) if(ch == test_array[i])
{ ++i; { ++i;
continue; continue;
} }
else else
{ ++mistakes; { ++mistakes;
++i; ++i;
} }
} }
time(&end_t); time(&end_t);
print_time(start_t, end_t, mistakes); print_time(start_t, end_t, mistakes);
} }
free(test_array); free(test_array);
endwin(); endwin();
return 0; return 0;
} }
int print_menu() int print_menu()
{ int choice, i; { int choice, i;
choice = 0; choice = 0;
while(1) while(1)
{ clear(); { clear();
printw("\n\n"); printw("\n\n");
print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL);
printw("\n\n\n"); printw("\n\n\n");
for(i = 0;i <= n_groups - 1; ++i) for(i = 0;i <= n_groups - 1; ++i)
printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); printw("\t%3d: \tPractice %s\n", i + 1, groups[i]);
printw("\t%3d: \tExit\n", i + 1); printw("\t%3d: \tExit\n", i + 1);
printw("\n\n\tChoice: "); printw("\n\n\tChoice: ");
refresh(); refresh();
echo(); echo();
scanw("%d", &choice); scanw("%d", &choice);
noecho(); noecho();
if(choice >= 1 && choice <= n_groups + 1) if(choice >= 1 && choice <= n_groups + 1)
break; break;
else else
{ attron(A_REVERSE); { attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue");
attroff(A_REVERSE); attroff(A_REVERSE);
getch(); getch();
} }
} }
return choice; return choice;
} }
void create_test_string(char *test_array, int choice) void create_test_string(char *test_array, int choice)
{ int i, index, length; { int i, index, length;
length = strlen(groups[choice]); length = strlen(groups[choice]);
for(i = 0;i <= HSIZE - 1; ++i) for(i = 0;i <= HSIZE - 1; ++i)
{ if(i%5 == 0) { if(i%5 == 0)
test_array[i] = ' '; test_array[i] = ' ';
else else
{ index = (int)(random() % length); { index = (int)(random() % length);
test_array[i] = groups[choice][index]; test_array[i] = groups[choice][index];
} }
} }
test_array[i] = '\0'; test_array[i] = '\0';
} }
void print_byebye() void print_byebye()
{ printw("\n"); { printw("\n");
print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); 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); print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL);
refresh(); refresh();
} }
void print_time(time_t start_t, time_t end_t, int mistakes) void print_time(time_t start_t, time_t end_t, int mistakes)
{ long int diff; { long int diff;
int h,m,s; int h,m,s;
float wpm; float wpm;
diff = end_t - start_t; diff = end_t - start_t;
wpm = ((HSIZE / 5)/(double)diff)*60; wpm = ((HSIZE / 5)/(double)diff)*60;
h = (int)(diff / 3600); h = (int)(diff / 3600);
diff -= h * 3600; diff -= h * 3600;
m = (int)(diff / 60); m = (int)(diff / 60);
diff -= m * 60; diff -= m * 60;
s = (int)diff; s = (int)diff;
attron(A_REVERSE); 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); 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); attroff(A_REVERSE);
refresh(); refresh();
getch(); getch();
} }
/* ---------------------------------------------------------------- * /* ---------------------------------------------------------------- *
* startx = 0 means at present x * * startx = 0 means at present x *
* starty = 0 means at present y * * starty = 0 means at present y *
* win = NULL means take stdscr * * win = NULL means take stdscr *
* ---------------------------------------------------------------- */ * ---------------------------------------------------------------- */
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
refresh(); refresh();
} }

View File

@ -10,26 +10,26 @@ EXE_DIR=../demo/exe
EXES = \ EXES = \
${EXE_DIR}/hello_world \ ${EXE_DIR}/hello_world \
${EXE_DIR}/init_func_example \ ${EXE_DIR}/init_func_example \
${EXE_DIR}/key_code \ ${EXE_DIR}/key_code \
${EXE_DIR}/mouse_menu \ ${EXE_DIR}/mouse_menu \
${EXE_DIR}/other_border \ ${EXE_DIR}/other_border \
${EXE_DIR}/printw_example \ ${EXE_DIR}/printw_example \
${EXE_DIR}/scanw_example \ ${EXE_DIR}/scanw_example \
${EXE_DIR}/simple_attr \ ${EXE_DIR}/simple_attr \
${EXE_DIR}/simple_color \ ${EXE_DIR}/simple_color \
${EXE_DIR}/simple_key \ ${EXE_DIR}/simple_key \
${EXE_DIR}/temp_leave \ ${EXE_DIR}/temp_leave \
${EXE_DIR}/win_border \ ${EXE_DIR}/win_border \
${EXE_DIR}/with_chgat ${EXE_DIR}/with_chgat
${EXE_DIR}/%: %.o ${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS} ${CC} -o $@ $< ${LIBS}
%.o: ${SRC_DIR}/%.c %.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $< ${CC} -o $@ -c $<
all: ${EXES} all: ${EXES}
clean: clean:
@rm -f ${EXES} @rm -f ${EXES}

View File

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

View File

@ -40,5 +40,5 @@ int main()
getch(); getch();
endwin(); endwin();
return 0; return 0;
} }

View File

@ -2,11 +2,11 @@
int main() int main()
{ {
initscr(); /* Start curses mode */ initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */ printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */ refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */ getch(); /* Wait for user input */
endwin(); /* End curses mode */ endwin(); /* End curses mode */
return 0; return 0;
} }

View File

@ -1,31 +1,31 @@
#include <ncurses.h> #include <ncurses.h>
int main() int main()
{ int ch; { int ch;
initscr(); /* Start curses mode */ initscr(); /* Start curses mode */
raw(); /* Line buffering disabled */ raw(); /* Line buffering disabled */
keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
noecho(); /* Don't echo() while we do getch */ noecho(); /* Don't echo() while we do getch */
printw("Type any character to see it in bold\n"); printw("Type any character to see it in bold\n");
ch = getch(); /* If raw() hadn't been called ch = getch(); /* If raw() hadn't been called
* we have to press enter before it * we have to press enter before it
* gets to the program */ * gets to the program */
if(ch == KEY_F(1)) /* Without keypad enabled this will */ if(ch == KEY_F(1)) /* Without keypad enabled this will */
printw("F1 Key pressed");/* not get to us either */ printw("F1 Key pressed");/* not get to us either */
/* Without noecho() some ugly escape /* Without noecho() some ugly escape
* charachters might have been printed * charachters might have been printed
* on screen */ * on screen */
else else
{ printw("The pressed key is "); { printw("The pressed key is ");
attron(A_BOLD); attron(A_BOLD);
printw("%c", ch); printw("%c", ch);
attroff(A_BOLD); attroff(A_BOLD);
} }
refresh(); /* Print it on to the real screen */ refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */ getch(); /* Wait for user input */
endwin(); /* End curses mode */ endwin(); /* End curses mode */
return 0; return 0;
} }

View File

@ -1,14 +1,14 @@
#include <ncurses.h> #include <ncurses.h>
int main() int main()
{ int ch; { int ch;
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
ch = getch(); ch = getch();
endwin(); endwin();
printf("The key pressed is %d\n", ch); printf("The key pressed is %d\n", ch);
} }

View File

@ -6,12 +6,12 @@
int startx = 0; int startx = 0;
int starty = 0; int starty = 0;
char *choices[] = { "Choice 1", char *choices[] = { "Choice 1",
"Choice 2", "Choice 2",
"Choice 3", "Choice 3",
"Choice 4", "Choice 4",
"Exit", "Exit",
}; };
int n_choices = sizeof(choices) / sizeof(char *); int n_choices = sizeof(choices) / sizeof(char *);
@ -19,88 +19,88 @@ void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice); void report_choice(int mouse_x, int mouse_y, int *p_choice);
int main() int main()
{ int c, choice = 0; { int c, choice = 0;
WINDOW *menu_win; WINDOW *menu_win;
MEVENT event; MEVENT event;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
clear(); clear();
noecho(); noecho();
cbreak(); //Line buffering disabled. pass on everything cbreak(); //Line buffering disabled. pass on everything
/* Try to put the window in the middle of screen */ /* Try to put the window in the middle of screen */
startx = (80 - WIDTH) / 2; startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2; starty = (24 - HEIGHT) / 2;
attron(A_REVERSE); attron(A_REVERSE);
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
refresh(); refresh();
attroff(A_REVERSE); attroff(A_REVERSE);
/* Print the menu for the first time */ /* Print the menu for the first time */
menu_win = newwin(HEIGHT, WIDTH, starty, startx); menu_win = newwin(HEIGHT, WIDTH, starty, startx);
print_menu(menu_win, 1); print_menu(menu_win, 1);
/* Get all the mouse events */ /* Get all the mouse events */
mousemask(ALL_MOUSE_EVENTS, NULL); mousemask(ALL_MOUSE_EVENTS, NULL);
while(1) while(1)
{ c = wgetch(menu_win); { c = wgetch(menu_win);
switch(c) switch(c)
{ case KEY_MOUSE: { case KEY_MOUSE:
if(getmouse(&event) == OK) if(getmouse(&event) == OK)
{ /* When the user clicks left mouse button */ { /* When the user clicks left mouse button */
if(event.bstate & BUTTON1_PRESSED) if(event.bstate & BUTTON1_PRESSED)
{ report_choice(event.x + 1, event.y + 1, &choice); { report_choice(event.x + 1, event.y + 1, &choice);
if(choice == -1) //Exit chosen if(choice == -1) //Exit chosen
goto end; goto end;
mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
refresh(); refresh();
} }
} }
print_menu(menu_win, choice); print_menu(menu_win, choice);
break; break;
} }
} }
end: end:
endwin(); endwin();
return 0; return 0;
} }
void print_menu(WINDOW *menu_win, int highlight) void print_menu(WINDOW *menu_win, int highlight)
{ {
int x, y, i; int x, y, i;
x = 2; x = 2;
y = 2; y = 2;
box(menu_win, 0, 0); box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1) { if(highlight == i + 1)
{ wattron(menu_win, A_REVERSE); { wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]); mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE); wattroff(menu_win, A_REVERSE);
} }
else else
mvwprintw(menu_win, y, x, "%s", choices[i]); mvwprintw(menu_win, y, x, "%s", choices[i]);
++y; ++y;
} }
wrefresh(menu_win); wrefresh(menu_win);
} }
/* Report the choice according to mouse position */ /* Report the choice according to mouse position */
void report_choice(int mouse_x, int mouse_y, int *p_choice) void report_choice(int mouse_x, int mouse_y, int *p_choice)
{ int i,j, choice; { int i,j, choice;
i = startx + 2; i = startx + 2;
j = starty + 3; j = starty + 3;
for(choice = 0; choice < n_choices; ++choice) for(choice = 0; choice < n_choices; ++choice)
if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))
{ if(choice == n_choices - 1) { if(choice == n_choices - 1)
*p_choice = -1; *p_choice = -1;
else else
*p_choice = choice + 1; *p_choice = choice + 1;
break; break;
} }
} }

View File

@ -1,15 +1,15 @@
#include <ncurses.h> #include <ncurses.h>
typedef struct _win_border_struct { typedef struct _win_border_struct {
chtype ls, rs, ts, bs, chtype ls, rs, ts, bs,
tl, tr, bl, br; tl, tr, bl, br;
}WIN_BORDER; }WIN_BORDER;
typedef struct _WIN_struct { typedef struct _WIN_struct {
int startx, starty; int startx, starty;
int height, width; int height, width;
WIN_BORDER border; WIN_BORDER border;
}WIN; }WIN;
void init_win_params(WIN *p_win); void init_win_params(WIN *p_win);
@ -17,104 +17,104 @@ void print_win_params(WIN *p_win);
void create_box(WIN *win, bool flag); void create_box(WIN *win, bool flag);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ WIN win; { WIN win;
int ch; int ch;
initscr(); /* Start curses mode */ initscr(); /* Start curses mode */
start_color(); /* Start the color functionality */ start_color(); /* Start the color functionality */
cbreak(); /* Line buffering disabled, Pass on cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */ * everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */ keypad(stdscr, TRUE); /* I need that nifty F1 */
noecho(); noecho();
init_pair(1, COLOR_CYAN, COLOR_BLACK); init_pair(1, COLOR_CYAN, COLOR_BLACK);
/* Initialize the window parameters */ /* Initialize the window parameters */
init_win_params(&win); init_win_params(&win);
print_win_params(&win); print_win_params(&win);
attron(COLOR_PAIR(1)); attron(COLOR_PAIR(1));
printw("Press F1 to exit"); printw("Press F1 to exit");
refresh(); refresh();
attroff(COLOR_PAIR(1)); attroff(COLOR_PAIR(1));
create_box(&win, TRUE); create_box(&win, TRUE);
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case KEY_LEFT: { case KEY_LEFT:
create_box(&win, FALSE); create_box(&win, FALSE);
--win.startx; --win.startx;
create_box(&win, TRUE); create_box(&win, TRUE);
break; break;
case KEY_RIGHT: case KEY_RIGHT:
create_box(&win, FALSE); create_box(&win, FALSE);
++win.startx; ++win.startx;
create_box(&win, TRUE); create_box(&win, TRUE);
break; break;
case KEY_UP: case KEY_UP:
create_box(&win, FALSE); create_box(&win, FALSE);
--win.starty; --win.starty;
create_box(&win, TRUE); create_box(&win, TRUE);
break; break;
case KEY_DOWN: case KEY_DOWN:
create_box(&win, FALSE); create_box(&win, FALSE);
++win.starty; ++win.starty;
create_box(&win, TRUE); create_box(&win, TRUE);
break; break;
} }
} }
endwin(); /* End curses mode */ endwin(); /* End curses mode */
return 0; return 0;
} }
void init_win_params(WIN *p_win) void init_win_params(WIN *p_win)
{ {
p_win->height = 3; p_win->height = 3;
p_win->width = 10; p_win->width = 10;
p_win->starty = (LINES - p_win->height)/2; p_win->starty = (LINES - p_win->height)/2;
p_win->startx = (COLS - p_win->width)/2; p_win->startx = (COLS - p_win->width)/2;
p_win->border.ls = '|'; p_win->border.ls = '|';
p_win->border.rs = '|'; p_win->border.rs = '|';
p_win->border.ts = '-'; p_win->border.ts = '-';
p_win->border.bs = '-'; p_win->border.bs = '-';
p_win->border.tl = '+'; p_win->border.tl = '+';
p_win->border.tr = '+'; p_win->border.tr = '+';
p_win->border.bl = '+'; p_win->border.bl = '+';
p_win->border.br = '+'; p_win->border.br = '+';
} }
void print_win_params(WIN *p_win) void print_win_params(WIN *p_win)
{ {
#ifdef _DEBUG #ifdef _DEBUG
mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
p_win->width, p_win->height); p_win->width, p_win->height);
refresh(); refresh();
#endif #endif
} }
void create_box(WIN *p_win, bool flag) void create_box(WIN *p_win, bool flag)
{ int i, j; { int i, j;
int x, y, w, h; int x, y, w, h;
x = p_win->startx; x = p_win->startx;
y = p_win->starty; y = p_win->starty;
w = p_win->width; w = p_win->width;
h = p_win->height; h = p_win->height;
if(flag == TRUE) if(flag == TRUE)
{ mvaddch(y, x, p_win->border.tl); { mvaddch(y, x, p_win->border.tl);
mvaddch(y, x + w, p_win->border.tr); mvaddch(y, x + w, p_win->border.tr);
mvaddch(y + h, x, p_win->border.bl); mvaddch(y + h, x, p_win->border.bl);
mvaddch(y + h, x + w, p_win->border.br); mvaddch(y + h, x + w, p_win->border.br);
mvhline(y, x + 1, p_win->border.ts, w - 1); mvhline(y, x + 1, p_win->border.ts, w - 1);
mvhline(y + h, x + 1, p_win->border.bs, 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, p_win->border.ls, h - 1);
mvvline(y + 1, x + w, p_win->border.rs, h - 1); mvvline(y + 1, x + w, p_win->border.rs, h - 1);
} }
else else
for(j = y; j <= y + h; ++j) for(j = y; j <= y + h; ++j)
for(i = x; i <= x + w; ++i) for(i = x; i <= x + w; ++i)
mvaddch(j, i, ' '); mvaddch(j, i, ' ');
refresh(); refresh();
} }

View File

@ -1,15 +1,15 @@
#include <ncurses.h> /* ncurses.h includes stdio.h */ #include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h> #include <string.h>
int main() int main()
{ {
char mesg[]="Just a string"; /* message to be appeared on the screen */ char mesg[]="Just a string"; /* message to be appeared on the screen */
int row,col; /* to store the number of rows and * int row,col; /* to store the number of rows and *
* the number of colums of the screen */ * the number of colums of the screen */
initscr(); /* start the curses mode */ initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */ getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */ /* print the message at the center of the screen */
mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); 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"); printw("Try resizing your window(if possible) and then run this program again");
refresh(); refresh();

View File

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

View File

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

View File

@ -2,39 +2,39 @@
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */ { initscr(); /* Start curses mode */
if(has_colors() == FALSE) if(has_colors() == FALSE)
{ endwin(); { endwin();
printf("Your terminal does not support color\n"); printf("Your terminal does not support color\n");
exit(1); exit(1);
} }
start_color(); /* Start color */ start_color(); /* Start color */
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1)); attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
attroff(COLOR_PAIR(1)); attroff(COLOR_PAIR(1));
getch(); getch();
endwin(); endwin();
} }
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
refresh(); refresh();
} }

View File

@ -8,85 +8,85 @@ int startx = 0;
int starty = 0; int starty = 0;
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
"Choice 2", "Choice 2",
"Choice 3", "Choice 3",
"Choice 4", "Choice 4",
"Exit", "Exit",
}; };
int n_choices = sizeof(choices) / sizeof(char *); int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight); void print_menu(WINDOW *menu_win, int highlight);
int main() int main()
{ WINDOW *menu_win; { WINDOW *menu_win;
int highlight = 1; int highlight = 1;
int choice = 0; int choice = 0;
int c; int c;
initscr(); initscr();
clear(); clear();
noecho(); noecho();
cbreak(); /* Line buffering disabled. pass on everything */ cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2; startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2; starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx); menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE); keypad(menu_win, TRUE);
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
refresh(); refresh();
print_menu(menu_win, highlight); print_menu(menu_win, highlight);
while(1) while(1)
{ c = wgetch(menu_win); { c = wgetch(menu_win);
switch(c) switch(c)
{ case KEY_UP: { case KEY_UP:
if(highlight == 1) if(highlight == 1)
highlight = n_choices; highlight = n_choices;
else else
--highlight; --highlight;
break; break;
case KEY_DOWN: case KEY_DOWN:
if(highlight == n_choices) if(highlight == n_choices)
highlight = 1; highlight = 1;
else else
++highlight; ++highlight;
break; break;
case 10: case 10:
choice = highlight; choice = highlight;
break; break;
default: default:
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
refresh(); refresh();
break; break;
} }
print_menu(menu_win, highlight); print_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */ if(choice != 0) /* User did a choice come out of the infinite loop */
break; break;
} }
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
clrtoeol(); clrtoeol();
refresh(); refresh();
endwin(); endwin();
return 0; return 0;
} }
void print_menu(WINDOW *menu_win, int highlight) void print_menu(WINDOW *menu_win, int highlight)
{ {
int x, y, i; int x, y, i;
x = 2; x = 2;
y = 2; y = 2;
box(menu_win, 0, 0); box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1) /* High light the present choice */ { if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE); { wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]); mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE); wattroff(menu_win, A_REVERSE);
} }
else else
mvwprintw(menu_win, y, x, "%s", choices[i]); mvwprintw(menu_win, y, x, "%s", choices[i]);
++y; ++y;
} }
wrefresh(menu_win); wrefresh(menu_win);
} }

View File

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

View File

@ -5,78 +5,78 @@ WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win); void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ WINDOW *my_win; { WINDOW *my_win;
int startx, starty, width, height; int startx, starty, width, height;
int ch; int ch;
initscr(); /* Start curses mode */ initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */ * everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */ keypad(stdscr, TRUE); /* I need that nifty F1 */
height = 3; height = 3;
width = 10; width = 10;
starty = (LINES - height) / 2; /* Calculating for a center placement */ starty = (LINES - height) / 2; /* Calculating for a center placement */
startx = (COLS - width) / 2; /* of the window */ startx = (COLS - width) / 2; /* of the window */
printw("Press F1 to exit"); printw("Press F1 to exit");
refresh(); refresh();
my_win = create_newwin(height, width, starty, startx); my_win = create_newwin(height, width, starty, startx);
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case KEY_LEFT: { case KEY_LEFT:
destroy_win(my_win); destroy_win(my_win);
my_win = create_newwin(height, width, starty,--startx); my_win = create_newwin(height, width, starty,--startx);
break; break;
case KEY_RIGHT: case KEY_RIGHT:
destroy_win(my_win); destroy_win(my_win);
my_win = create_newwin(height, width, starty,++startx); my_win = create_newwin(height, width, starty,++startx);
break; break;
case KEY_UP: case KEY_UP:
destroy_win(my_win); destroy_win(my_win);
my_win = create_newwin(height, width, --starty,startx); my_win = create_newwin(height, width, --starty,startx);
break; break;
case KEY_DOWN: case KEY_DOWN:
destroy_win(my_win); destroy_win(my_win);
my_win = create_newwin(height, width, ++starty,startx); my_win = create_newwin(height, width, ++starty,startx);
break; break;
} }
} }
endwin(); /* End curses mode */ endwin(); /* End curses mode */
return 0; return 0;
} }
WINDOW *create_newwin(int height, int width, int starty, int startx) WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win; { WINDOW *local_win;
local_win = newwin(height, width, starty, startx); local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0); /* 0, 0 gives default characters box(local_win, 0 , 0); /* 0, 0 gives default characters
* for the vertical and horizontal * for the vertical and horizontal
* lines */ * lines */
wrefresh(local_win); /* Show that box */ wrefresh(local_win); /* Show that box */
return local_win; return local_win;
} }
void destroy_win(WINDOW *local_win) void destroy_win(WINDOW *local_win)
{ {
/* box(local_win, ' ', ' '); : This won't produce the desired /* box(local_win, ' ', ' '); : This won't produce the desired
* result of erasing the window. It will leave it's four corners * result of erasing the window. It will leave it's four corners
* and so an ugly remnant of window. * and so an ugly remnant of window.
*/ */
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* The parameters taken are /* The parameters taken are
* 1. win: the window on which to operate * 1. win: the window on which to operate
* 2. ls: character to be used for the left side of the window * 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 * 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 * 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 * 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 * 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 * 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 * 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 * 9. br: character to be used for the bottom right corner of the window
*/ */
wrefresh(local_win); wrefresh(local_win);
delwin(local_win); delwin(local_win);
} }

View File

@ -1,24 +1,24 @@
#include <ncurses.h> #include <ncurses.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */ { initscr(); /* Start curses mode */
start_color(); /* Start color functionality */ start_color(); /* Start color functionality */
init_pair(1, COLOR_CYAN, COLOR_BLACK); init_pair(1, COLOR_CYAN, COLOR_BLACK);
printw("A Big string which i didn't care to type fully "); printw("A Big string which i didn't care to type fully ");
mvchgat(0, 0, -1, A_BLINK, 1, NULL); mvchgat(0, 0, -1, A_BLINK, 1, NULL);
/* /*
* First two parameters specify the position at which to start * First two parameters specify the position at which to start
* Third parameter number of characters to update. -1 means till * Third parameter number of characters to update. -1 means till
* end of line * end of line
* Forth parameter is the normal attribute you wanted to give * Forth parameter is the normal attribute you wanted to give
* to the charcter * to the charcter
* Fifth is the color index. It is the index given during init_pair() * Fifth is the color index. It is the index given during init_pair()
* use 0 if you didn't want color * use 0 if you didn't want color
* Sixth one is always NULL * Sixth one is always NULL
*/ */
refresh(); refresh();
getch(); getch();
endwin(); /* End curses mode */ endwin(); /* End curses mode */
return 0; return 0;
} }

View File

@ -9,19 +9,19 @@ SRC_DIR=.
EXE_DIR=../demo/exe EXE_DIR=../demo/exe
EXES = \ EXES = \
${EXE_DIR}/form_attrib\ ${EXE_DIR}/form_attrib\
${EXE_DIR}/form_options\ ${EXE_DIR}/form_options\
${EXE_DIR}/form_simple\ ${EXE_DIR}/form_simple\
${EXE_DIR}/form_win \ ${EXE_DIR}/form_win \
${EXE_DIR}/%: %.o ${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS} ${CC} -o $@ $< ${LIBS}
%.o: ${SRC_DIR}/%.c %.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $< ${CC} -o $@ -c $<
all: ${EXES} all: ${EXES}
clean: clean:
@rm -f ${EXES} @rm -f ${EXES}

View File

@ -2,8 +2,8 @@ Description of files
-------------------- --------------------
forms forms
| |
|----> form_attrib.c -- Usage of field attributes |----> form_attrib.c -- Usage of field attributes
|----> form_options.c -- Usage of field options |----> form_options.c -- Usage of field options
|----> form_simple.c -- A simple form example |----> form_simple.c -- A simple form example
|----> form_win.c -- Demo of windows associated with forms |----> form_win.c -- Demo of windows associated with forms

View File

@ -1,75 +1,75 @@
#include <form.h> #include <form.h>
int main() int main()
{ FIELD *field[3]; { FIELD *field[3];
FORM *my_form; FORM *my_form;
int ch; int ch;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize few color pairs */ /* Initialize few color pairs */
init_pair(1, COLOR_WHITE, COLOR_BLUE); init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_WHITE, COLOR_BLUE); init_pair(2, COLOR_WHITE, COLOR_BLUE);
/* Initialize the fields */ /* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0); field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0); field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL; field[2] = NULL;
/* Set field options */ /* Set field options */
set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ 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 */ set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */
/* are printed in white */ /* are printed in white */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */ /* Field is filled up */
set_field_back(field[1], A_UNDERLINE); set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP); field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */ /* Create the form and post it */
my_form = new_form(field); my_form = new_form(field);
post_form(my_form); post_form(my_form);
refresh(); refresh();
set_current_field(my_form, field[0]); /* Set focus to the colored field */ set_current_field(my_form, field[0]); /* Set focus to the colored field */
mvprintw(4, 10, "Value 1:"); mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:"); mvprintw(6, 10, "Value 2:");
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh(); refresh();
/* Loop through to get user requests */ /* Loop through to get user requests */
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case KEY_DOWN: { case KEY_DOWN:
/* Go to next field */ /* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD); form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */ /* Go to the end of the present buffer */
/* Leaves nicely at the last character */ /* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
case KEY_UP: case KEY_UP:
/* Go to previous field */ /* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD); form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
default: default:
/* If this is a normal character, it gets */ /* If this is a normal character, it gets */
/* Printed */ /* Printed */
form_driver(my_form, ch); form_driver(my_form, ch);
break; break;
} }
} }
/* Un post form and free the memory */ /* Un post form and free the memory */
unpost_form(my_form); unpost_form(my_form);
free_form(my_form); free_form(my_form);
free_field(field[0]); free_field(field[0]);
free_field(field[1]); free_field(field[1]);
endwin(); endwin();
return 0; return 0;
} }

View File

@ -7,70 +7,70 @@
#define N_FIELDS 3 #define N_FIELDS 3
int main() int main()
{ FIELD *field[N_FIELDS]; { FIELD *field[N_FIELDS];
FORM *my_form; FORM *my_form;
int ch, i; int ch, i;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize the fields */ /* Initialize the fields */
for(i = 0; i < N_FIELDS - 1; ++i) for(i = 0; i < N_FIELDS - 1; ++i)
field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
field[N_FIELDS - 1] = NULL; field[N_FIELDS - 1] = NULL;
/* Set field options */ /* Set field options */
set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ 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[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_PUBLIC); /* This filed is like a password field*/
field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */
/* after last character is entered */ /* after last character is entered */
/* Create the form and post it */ /* Create the form and post it */
my_form = new_form(field); my_form = new_form(field);
post_form(my_form); post_form(my_form);
refresh(); refresh();
set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */
set_field_buffer(field[0], 0, "This is a static Field"); set_field_buffer(field[0], 0, "This is a static Field");
/* Initialize the field */ /* Initialize the field */
mvprintw(STARTY, STARTX - 10, "Field 1:"); mvprintw(STARTY, STARTX - 10, "Field 1:");
mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
refresh(); refresh();
/* Loop through to get user requests */ /* Loop through to get user requests */
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case KEY_DOWN: { case KEY_DOWN:
/* Go to next field */ /* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD); form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */ /* Go to the end of the present buffer */
/* Leaves nicely at the last character */ /* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
case KEY_UP: case KEY_UP:
/* Go to previous field */ /* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD); form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
default: default:
/* If this is a normal character, it gets */ /* If this is a normal character, it gets */
/* Printed */ /* Printed */
form_driver(my_form, ch); form_driver(my_form, ch);
break; break;
} }
} }
/* Un post form and free the memory */ /* Un post form and free the memory */
unpost_form(my_form); unpost_form(my_form);
free_form(my_form); free_form(my_form);
free_field(field[0]); free_field(field[0]);
free_field(field[1]); free_field(field[1]);
endwin(); endwin();
return 0; return 0;
} }

View File

@ -1,66 +1,66 @@
#include <form.h> #include <form.h>
int main() int main()
{ FIELD *field[3]; { FIELD *field[3];
FORM *my_form; FORM *my_form;
int ch; int ch;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize the fields */ /* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0); field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0); field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL; field[2] = NULL;
/* Set field options */ /* Set field options */
set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ 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_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */ /* Field is filled up */
set_field_back(field[1], A_UNDERLINE); set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP); field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */ /* Create the form and post it */
my_form = new_form(field); my_form = new_form(field);
post_form(my_form); post_form(my_form);
refresh(); refresh();
mvprintw(4, 10, "Value 1:"); mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:"); mvprintw(6, 10, "Value 2:");
refresh(); refresh();
/* Loop through to get user requests */ /* Loop through to get user requests */
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case KEY_DOWN: { case KEY_DOWN:
/* Go to next field */ /* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD); form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */ /* Go to the end of the present buffer */
/* Leaves nicely at the last character */ /* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
case KEY_UP: case KEY_UP:
/* Go to previous field */ /* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD); form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
default: default:
/* If this is a normal character, it gets */ /* If this is a normal character, it gets */
/* Printed */ /* Printed */
form_driver(my_form, ch); form_driver(my_form, ch);
break; break;
} }
} }
/* Un post form and free the memory */ /* Un post form and free the memory */
unpost_form(my_form); unpost_form(my_form);
free_form(my_form); free_form(my_form);
free_field(field[0]); free_field(field[0]);
free_field(field[1]); free_field(field[1]);
endwin(); endwin();
return 0; return 0;
} }

View File

@ -4,109 +4,109 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
int main() int main()
{ {
FIELD *field[3]; FIELD *field[3];
FORM *my_form; FORM *my_form;
WINDOW *my_form_win; WINDOW *my_form_win;
int ch, rows, cols; int ch, rows, cols;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize few color pairs */ /* Initialize few color pairs */
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
/* Initialize the fields */ /* Initialize the fields */
field[0] = new_field(1, 10, 6, 1, 0, 0); field[0] = new_field(1, 10, 6, 1, 0, 0);
field[1] = new_field(1, 10, 8, 1, 0, 0); field[1] = new_field(1, 10, 8, 1, 0, 0);
field[2] = NULL; field[2] = NULL;
/* Set field options */ /* Set field options */
set_field_back(field[0], A_UNDERLINE); set_field_back(field[0], A_UNDERLINE);
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */ /* Field is filled up */
set_field_back(field[1], A_UNDERLINE); set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP); field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */ /* Create the form and post it */
my_form = new_form(field); my_form = new_form(field);
/* Calculate the area required for the form */ /* Calculate the area required for the form */
scale_form(my_form, &rows, &cols); scale_form(my_form, &rows, &cols);
/* Create the window to be associated with the form */ /* Create the window to be associated with the form */
my_form_win = newwin(rows + 4, cols + 4, 4, 4); my_form_win = newwin(rows + 4, cols + 4, 4, 4);
keypad(my_form_win, TRUE); keypad(my_form_win, TRUE);
/* Set main window and sub window */ /* Set main window and sub window */
set_form_win(my_form, my_form_win); set_form_win(my_form, my_form_win);
set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));
/* Print a border around the main window and print a title */ /* Print a border around the main window and print a title */
box(my_form_win, 0, 0); box(my_form_win, 0, 0);
print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));
post_form(my_form); post_form(my_form);
wrefresh(my_form_win); wrefresh(my_form_win);
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh(); refresh();
/* Loop through to get user requests */ /* Loop through to get user requests */
while((ch = wgetch(my_form_win)) != KEY_F(1)) while((ch = wgetch(my_form_win)) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case KEY_DOWN: { case KEY_DOWN:
/* Go to next field */ /* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD); form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */ /* Go to the end of the present buffer */
/* Leaves nicely at the last character */ /* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
case KEY_UP: case KEY_UP:
/* Go to previous field */ /* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD); form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE); form_driver(my_form, REQ_END_LINE);
break; break;
default: default:
/* If this is a normal character, it gets */ /* If this is a normal character, it gets */
/* Printed */ /* Printed */
form_driver(my_form, ch); form_driver(my_form, ch);
break; break;
} }
} }
/* Un post form and free the memory */ /* Un post form and free the memory */
unpost_form(my_form); unpost_form(my_form);
free_form(my_form); free_form(my_form);
free_field(field[0]); free_field(field[0]);
free_field(field[1]); free_field(field[1]);
endwin(); endwin();
return 0; return 0;
} }
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
wattron(win, color); wattron(win, color);
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
wattroff(win, color); wattroff(win, color);
refresh(); refresh();
} }

View File

@ -9,23 +9,23 @@ SRC_DIR=.
EXE_DIR=../demo/exe EXE_DIR=../demo/exe
EXES = \ EXES = \
${EXE_DIR}/menu_attrib\ ${EXE_DIR}/menu_attrib\
${EXE_DIR}/menu_item_data\ ${EXE_DIR}/menu_item_data\
${EXE_DIR}/menu_multi_column \ ${EXE_DIR}/menu_multi_column \
${EXE_DIR}/menu_scroll \ ${EXE_DIR}/menu_scroll \
${EXE_DIR}/menu_simple \ ${EXE_DIR}/menu_simple \
${EXE_DIR}/menu_toggle \ ${EXE_DIR}/menu_toggle \
${EXE_DIR}/menu_userptr \ ${EXE_DIR}/menu_userptr \
${EXE_DIR}/menu_win ${EXE_DIR}/menu_win
${EXE_DIR}/%: %.o ${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS} ${CC} -o $@ $< ${LIBS}
%.o: ${SRC_DIR}/%.c %.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $< ${CC} -o $@ -c $<
all: ${EXES} all: ${EXES}
clean: clean:
@rm -f ${EXES} @rm -f ${EXES}

View File

@ -2,12 +2,12 @@ Description of files
-------------------- --------------------
menus menus
| |
|----> menu_attrib.c -- Usage of menu attributes |----> menu_attrib.c -- Usage of menu attributes
|----> menu_item_data.c -- Usage of item_name() etc.. functions |----> menu_item_data.c -- Usage of item_name() etc.. functions
|----> menu_multi_column.c -- Creates multi columnar menus |----> menu_multi_column.c -- Creates multi columnar menus
|----> menu_scroll.c -- Demonstrates scrolling capability of menus |----> menu_scroll.c -- Demonstrates scrolling capability of menus
|----> menu_simple.c -- A simple menu accessed by arrow keys |----> menu_simple.c -- A simple menu accessed by arrow keys
|----> menu_toggle.c -- Creates multi valued menus and explains |----> menu_toggle.c -- Creates multi valued menus and explains
| -- REQ_TOGGLE_ITEM | -- REQ_TOGGLE_ITEM
|----> menu_userptr.c -- Usage of user pointer |----> menu_userptr.c -- Usage of user pointer
|----> menu_win.c -- Demo of windows associated with menus |----> menu_win.c -- Demo of windows associated with menus

View File

@ -1,80 +1,80 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
"Choice 2", "Choice 2",
"Choice 3", "Choice 3",
"Choice 4", "Choice 4",
"Choice 5", "Choice 5",
"Choice 6", "Choice 6",
"Choice 7", "Choice 7",
"Exit", "Exit",
}; };
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
int n_choices, i; int n_choices, i;
ITEM *cur_item; ITEM *cur_item;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK); init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* Initialize items */ /* Initialize items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]); my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL; my_items[n_choices] = (ITEM *)NULL;
item_opts_off(my_items[3], O_SELECTABLE); item_opts_off(my_items[3], O_SELECTABLE);
item_opts_off(my_items[6], O_SELECTABLE); item_opts_off(my_items[6], O_SELECTABLE);
/* Create menu */ /* Create menu */
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
/* Set fore ground and back ground of the menu */ /* Set fore ground and back ground of the menu */
set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
set_menu_back(my_menu, COLOR_PAIR(2)); set_menu_back(my_menu, COLOR_PAIR(2));
set_menu_grey(my_menu, COLOR_PAIR(3)); set_menu_grey(my_menu, COLOR_PAIR(3));
/* Post the menu */ /* Post the menu */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 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)"); mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu); post_menu(my_menu);
refresh(); refresh();
while((c = getch()) != KEY_F(1)) while((c = getch()) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
case 10: /* Enter */ case 10: /* Enter */
move(20, 0); move(20, 0);
clrtoeol(); clrtoeol();
mvprintw(20, 0, "Item selected is : %s", mvprintw(20, 0, "Item selected is : %s",
item_name(current_item(my_menu))); item_name(current_item(my_menu)));
pos_menu_cursor(my_menu); pos_menu_cursor(my_menu);
break; break;
} }
} }
unpost_menu(my_menu); unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
free_item(my_items[i]); free_item(my_items[i]);
free_menu(my_menu); free_menu(my_menu);
endwin(); endwin();
} }

View File

@ -2,7 +2,7 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
@ -13,54 +13,54 @@ char *choices[] = {
}; };
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
int n_choices, i; int n_choices, i;
ITEM *cur_item; ITEM *cur_item;
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]); my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL; my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
post_menu(my_menu); post_menu(my_menu);
refresh(); refresh();
while((c = getch()) != KEY_F(1)) while((c = getch()) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
case 10: /* Enter */ case 10: /* Enter */
cur_item = current_item(my_menu); cur_item = current_item(my_menu);
move(LINES - 2, 0); move(LINES - 2, 0);
clrtoeol(); clrtoeol();
mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", 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_index(cur_item) + 1, item_name(cur_item),
item_description(cur_item)); item_description(cur_item));
refresh(); refresh();
pos_menu_cursor(my_menu); pos_menu_cursor(my_menu);
break; break;
} }
} }
free_item(my_items[0]); free_item(my_items[0]);
free_item(my_items[1]); free_item(my_items[1]);
free_menu(my_menu); free_menu(my_menu);
endwin(); endwin();
} }

View File

@ -2,96 +2,96 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Exit", "Exit",
(char *)NULL, (char *)NULL,
}; };
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
WINDOW *my_menu_win; WINDOW *my_menu_win;
int n_choices, i; int n_choices, i;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */ /* Create items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]); my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */ /* Crate menu */
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
/* Set menu option not to show the description */ /* Set menu option not to show the description */
menu_opts_off(my_menu, O_SHOWDESC); menu_opts_off(my_menu, O_SHOWDESC);
/* Create the window to be associated with the menu */ /* Create the window to be associated with the menu */
my_menu_win = newwin(10, 70, 4, 4); my_menu_win = newwin(10, 70, 4, 4);
keypad(my_menu_win, TRUE); keypad(my_menu_win, TRUE);
/* Set main window and sub window */ /* Set main window and sub window */
set_menu_win(my_menu, my_menu_win); set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
set_menu_format(my_menu, 5, 3); set_menu_format(my_menu, 5, 3);
set_menu_mark(my_menu, " * "); set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */ /* Print a border around the main window and print a title */
box(my_menu_win, 0, 0); box(my_menu_win, 0, 0);
attron(COLOR_PAIR(2)); attron(COLOR_PAIR(2));
mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2)); attroff(COLOR_PAIR(2));
refresh(); refresh();
/* Post the menu */ /* Post the menu */
post_menu(my_menu); post_menu(my_menu);
wrefresh(my_menu_win); wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1)) while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
case KEY_LEFT: case KEY_LEFT:
menu_driver(my_menu, REQ_LEFT_ITEM); menu_driver(my_menu, REQ_LEFT_ITEM);
break; break;
case KEY_RIGHT: case KEY_RIGHT:
menu_driver(my_menu, REQ_RIGHT_ITEM); menu_driver(my_menu, REQ_RIGHT_ITEM);
break; break;
case KEY_NPAGE: case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE); menu_driver(my_menu, REQ_SCR_DPAGE);
break; break;
case KEY_PPAGE: case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE); menu_driver(my_menu, REQ_SCR_UPAGE);
break; break;
} }
wrefresh(my_menu_win); wrefresh(my_menu_win);
} }
/* Unpost and free all the memory taken up */ /* Unpost and free all the memory taken up */
unpost_menu(my_menu); unpost_menu(my_menu);
free_menu(my_menu); free_menu(my_menu);
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
free_item(my_items[i]); free_item(my_items[i]);
endwin(); endwin();
} }

View File

@ -2,123 +2,123 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
"Choice 2", "Choice 2",
"Choice 3", "Choice 3",
"Choice 4", "Choice 4",
"Choice 5", "Choice 5",
"Choice 6", "Choice 6",
"Choice 7", "Choice 7",
"Choice 8", "Choice 8",
"Choice 9", "Choice 9",
"Choice 10", "Choice 10",
"Exit", "Exit",
(char *)NULL, (char *)NULL,
}; };
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
WINDOW *my_menu_win; WINDOW *my_menu_win;
int n_choices, i; int n_choices, i;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */ /* Create items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]); my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */ /* Crate menu */
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
/* Create the window to be associated with the menu */ /* Create the window to be associated with the menu */
my_menu_win = newwin(10, 40, 4, 4); my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE); keypad(my_menu_win, TRUE);
/* Set main window and sub window */ /* Set main window and sub window */
set_menu_win(my_menu, my_menu_win); set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
set_menu_format(my_menu, 5, 1); set_menu_format(my_menu, 5, 1);
/* Set menu mark to the string " * " */ /* Set menu mark to the string " * " */
set_menu_mark(my_menu, " * "); set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */ /* Print a border around the main window and print a title */
box(my_menu_win, 0, 0); box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE); mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
/* Post the menu */ /* Post the menu */
post_menu(my_menu); post_menu(my_menu);
wrefresh(my_menu_win); wrefresh(my_menu_win);
attron(COLOR_PAIR(2)); attron(COLOR_PAIR(2));
mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); 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)"); mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2)); attroff(COLOR_PAIR(2));
refresh(); refresh();
while((c = wgetch(my_menu_win)) != KEY_F(1)) while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
case KEY_NPAGE: case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE); menu_driver(my_menu, REQ_SCR_DPAGE);
break; break;
case KEY_PPAGE: case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE); menu_driver(my_menu, REQ_SCR_UPAGE);
break; break;
} }
wrefresh(my_menu_win); wrefresh(my_menu_win);
} }
/* Unpost and free all the memory taken up */ /* Unpost and free all the memory taken up */
unpost_menu(my_menu); unpost_menu(my_menu);
free_menu(my_menu); free_menu(my_menu);
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
free_item(my_items[i]); free_item(my_items[i]);
endwin(); endwin();
} }
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
wattron(win, color); wattron(win, color);
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
wattroff(win, color); wattroff(win, color);
refresh(); refresh();
} }

View File

@ -2,7 +2,7 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
@ -13,44 +13,44 @@ char *choices[] = {
}; };
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
int n_choices, i; int n_choices, i;
ITEM *cur_item; ITEM *cur_item;
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]); my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL; my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
mvprintw(LINES - 2, 0, "F1 to Exit"); mvprintw(LINES - 2, 0, "F1 to Exit");
post_menu(my_menu); post_menu(my_menu);
refresh(); refresh();
while((c = getch()) != KEY_F(1)) while((c = getch()) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
} }
} }
free_item(my_items[0]); free_item(my_items[0]);
free_item(my_items[1]); free_item(my_items[1]);
free_menu(my_menu); free_menu(my_menu);
endwin(); endwin();
} }

View File

@ -2,83 +2,83 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
"Choice 2", "Choice 2",
"Choice 3", "Choice 3",
"Choice 4", "Choice 4",
"Choice 5", "Choice 5",
"Choice 6", "Choice 6",
"Choice 7", "Choice 7",
"Exit", "Exit",
}; };
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
int n_choices, i; int n_choices, i;
ITEM *cur_item; ITEM *cur_item;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize items */ /* Initialize items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]); my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL; my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
/* Make the menu multi valued */ /* Make the menu multi valued */
menu_opts_off(my_menu, O_ONEVALUE); menu_opts_off(my_menu, O_ONEVALUE);
mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item."); 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)"); mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)");
post_menu(my_menu); post_menu(my_menu);
refresh(); refresh();
while((c = getch()) != KEY_F(1)) while((c = getch()) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
case ' ': case ' ':
menu_driver(my_menu, REQ_TOGGLE_ITEM); menu_driver(my_menu, REQ_TOGGLE_ITEM);
break; break;
case 10: /* Enter */ case 10: /* Enter */
{ char temp[200]; { char temp[200];
ITEM **items; ITEM **items;
items = menu_items(my_menu); items = menu_items(my_menu);
temp[0] = '\0'; temp[0] = '\0';
for(i = 0; i < item_count(my_menu); ++i) for(i = 0; i < item_count(my_menu); ++i)
if(item_value(items[i]) == TRUE) if(item_value(items[i]) == TRUE)
{ strcat(temp, item_name(items[i])); { strcat(temp, item_name(items[i]));
strcat(temp, " "); strcat(temp, " ");
} }
move(20, 0); move(20, 0);
clrtoeol(); clrtoeol();
mvprintw(20, 0, temp); mvprintw(20, 0, temp);
refresh(); refresh();
} }
break; break;
} }
} }
free_item(my_items[0]); free_item(my_items[0]);
free_item(my_items[1]); free_item(my_items[1]);
free_menu(my_menu); free_menu(my_menu);
endwin(); endwin();
} }

View File

@ -2,86 +2,86 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
"Choice 2", "Choice 2",
"Choice 3", "Choice 3",
"Choice 4", "Choice 4",
"Choice 5", "Choice 5",
"Choice 6", "Choice 6",
"Choice 7", "Choice 7",
"Exit", "Exit",
}; };
void func(char *name); void func(char *name);
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
int n_choices, i; int n_choices, i;
ITEM *cur_item; ITEM *cur_item;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK); init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* Initialize items */ /* Initialize items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
{ my_items[i] = new_item(choices[i], choices[i]); { my_items[i] = new_item(choices[i], choices[i]);
/* Set the user pointer */ /* Set the user pointer */
set_item_userptr(my_items[i], func); set_item_userptr(my_items[i], func);
} }
my_items[n_choices] = (ITEM *)NULL; my_items[n_choices] = (ITEM *)NULL;
/* Create menu */ /* Create menu */
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
/* Post the menu */ /* Post the menu */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 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)"); mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu); post_menu(my_menu);
refresh(); refresh();
while((c = getch()) != KEY_F(1)) while((c = getch()) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
case 10: /* Enter */ case 10: /* Enter */
{ ITEM *cur; { ITEM *cur;
void (*p)(char *); void (*p)(char *);
cur = current_item(my_menu); cur = current_item(my_menu);
p = item_userptr(cur); p = item_userptr(cur);
p((char *)item_name(cur)); p((char *)item_name(cur));
pos_menu_cursor(my_menu); pos_menu_cursor(my_menu);
break; break;
} }
break; break;
} }
} }
unpost_menu(my_menu); unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
free_item(my_items[i]); free_item(my_items[i]);
free_menu(my_menu); free_menu(my_menu);
endwin(); endwin();
} }
void func(char *name) void func(char *name)
{ move(20, 0); { move(20, 0);
clrtoeol(); clrtoeol();
mvprintw(20, 0, "Item selected is : %s", name); mvprintw(20, 0, "Item selected is : %s", name);
} }

View File

@ -1,7 +1,7 @@
#include <menu.h> #include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"Choice 1", "Choice 1",
@ -14,92 +14,92 @@ char *choices[] = {
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main() int main()
{ ITEM **my_items; { ITEM **my_items;
int c; int c;
MENU *my_menu; MENU *my_menu;
WINDOW *my_menu_win; WINDOW *my_menu_win;
int n_choices, i; int n_choices, i;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
/* Create items */ /* Create items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]); my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */ /* Crate menu */
my_menu = new_menu((ITEM **)my_items); my_menu = new_menu((ITEM **)my_items);
/* Create the window to be associated with the menu */ /* Create the window to be associated with the menu */
my_menu_win = newwin(10, 40, 4, 4); my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE); keypad(my_menu_win, TRUE);
/* Set main window and sub window */ /* Set main window and sub window */
set_menu_win(my_menu, my_menu_win); set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
/* Set menu mark to the string " * " */ /* Set menu mark to the string " * " */
set_menu_mark(my_menu, " * "); set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */ /* Print a border around the main window and print a title */
box(my_menu_win, 0, 0); box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE); mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
mvprintw(LINES - 2, 0, "F1 to exit"); mvprintw(LINES - 2, 0, "F1 to exit");
refresh(); refresh();
/* Post the menu */ /* Post the menu */
post_menu(my_menu); post_menu(my_menu);
wrefresh(my_menu_win); wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1)) while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c) { switch(c)
{ case KEY_DOWN: { case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM); menu_driver(my_menu, REQ_DOWN_ITEM);
break; break;
case KEY_UP: case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM); menu_driver(my_menu, REQ_UP_ITEM);
break; break;
} }
wrefresh(my_menu_win); wrefresh(my_menu_win);
} }
/* Unpost and free all the memory taken up */ /* Unpost and free all the memory taken up */
unpost_menu(my_menu); unpost_menu(my_menu);
free_menu(my_menu); free_menu(my_menu);
for(i = 0; i < n_choices; ++i) for(i = 0; i < n_choices; ++i)
free_item(my_items[i]); free_item(my_items[i]);
endwin(); endwin();
} }
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
wattron(win, color); wattron(win, color);
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
wattroff(win, color); wattroff(win, color);
refresh(); refresh();
} }

View File

@ -9,19 +9,19 @@ SRC_DIR=.
EXE_DIR=../demo/exe EXE_DIR=../demo/exe
EXES = \ EXES = \
${EXE_DIR}/panel_browse \ ${EXE_DIR}/panel_browse \
${EXE_DIR}/panel_hide \ ${EXE_DIR}/panel_hide \
${EXE_DIR}/panel_resize \ ${EXE_DIR}/panel_resize \
${EXE_DIR}/panel_simple ${EXE_DIR}/panel_simple
${EXE_DIR}/%: %.o ${EXE_DIR}/%: %.o
${CC} -o $@ $< ${LIBS} ${CC} -o $@ $< ${LIBS}
%.o: ${SRC_DIR}/%.c %.o: ${SRC_DIR}/%.c
${CC} -o $@ -c $< ${CC} -o $@ -c $<
all: ${EXES} all: ${EXES}
clean: clean:
@rm -f ${EXES} @rm -f ${EXES}

View File

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

View File

@ -8,110 +8,110 @@ 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 print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main() int main()
{ WINDOW *my_wins[3]; { WINDOW *my_wins[3];
PANEL *my_panels[3]; PANEL *my_panels[3];
PANEL *top; PANEL *top;
int ch; int ch;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize all the colors */ /* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK); init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK); init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3); init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */ /* 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[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[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 */ 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 up the user pointers to the next panel */
set_panel_userptr(my_panels[0], my_panels[1]); set_panel_userptr(my_panels[0], my_panels[1]);
set_panel_userptr(my_panels[1], my_panels[2]); set_panel_userptr(my_panels[1], my_panels[2]);
set_panel_userptr(my_panels[2], my_panels[0]); set_panel_userptr(my_panels[2], my_panels[0]);
/* Update the stacking order. 2nd panel will be on top */ /* Update the stacking order. 2nd panel will be on top */
update_panels(); update_panels();
/* Show it on the screen */ /* Show it on the screen */
attron(COLOR_PAIR(4)); attron(COLOR_PAIR(4));
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(4));
doupdate(); doupdate();
top = my_panels[2]; top = my_panels[2];
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case 9: { case 9:
top = (PANEL *)panel_userptr(top); top = (PANEL *)panel_userptr(top);
top_panel(top); top_panel(top);
break; break;
} }
update_panels(); update_panels();
doupdate(); doupdate();
} }
endwin(); endwin();
return 0; return 0;
} }
/* Put all the windows */ /* Put all the windows */
void init_wins(WINDOW **wins, int n) void init_wins(WINDOW **wins, int n)
{ int x, y, i; { int x, y, i;
char label[80]; char label[80];
y = 2; y = 2;
x = 10; x = 10;
for(i = 0; i < n; ++i) for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x); { wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1); sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1); win_show(wins[i], label, i + 1);
y += 3; y += 3;
x += 7; x += 7;
} }
} }
/* Show the window with a border and a label */ /* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color) void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width; { int startx, starty, height, width;
getbegyx(win, starty, startx); getbegyx(win, starty, startx);
getmaxyx(win, height, width); getmaxyx(win, height, width);
box(win, 0, 0); box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE); mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2); mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE); mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); 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) void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
wattron(win, color); wattron(win, color);
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
wattroff(win, color); wattroff(win, color);
refresh(); refresh();
} }

View File

@ -1,7 +1,7 @@
#include <panel.h> #include <panel.h>
typedef struct _PANEL_DATA { typedef struct _PANEL_DATA {
int hide; /* TRUE if panel is hidden */ int hide; /* TRUE if panel is hidden */
}PANEL_DATA; }PANEL_DATA;
#define NLINES 10 #define NLINES 10
@ -12,145 +12,145 @@ 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 print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main() int main()
{ WINDOW *my_wins[3]; { WINDOW *my_wins[3];
PANEL *my_panels[3]; PANEL *my_panels[3];
PANEL_DATA panel_datas[3]; PANEL_DATA panel_datas[3];
PANEL_DATA *temp; PANEL_DATA *temp;
int ch; int ch;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize all the colors */ /* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK); init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK); init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3); init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */ /* 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[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[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 */ my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
/* Initialize panel datas saying that nothing is hidden */ /* Initialize panel datas saying that nothing is hidden */
panel_datas[0].hide = FALSE; panel_datas[0].hide = FALSE;
panel_datas[1].hide = FALSE; panel_datas[1].hide = FALSE;
panel_datas[2].hide = FALSE; panel_datas[2].hide = FALSE;
set_panel_userptr(my_panels[0], &panel_datas[0]); set_panel_userptr(my_panels[0], &panel_datas[0]);
set_panel_userptr(my_panels[1], &panel_datas[1]); set_panel_userptr(my_panels[1], &panel_datas[1]);
set_panel_userptr(my_panels[2], &panel_datas[2]); set_panel_userptr(my_panels[2], &panel_datas[2]);
/* Update the stacking order. 2nd panel will be on top */ /* Update the stacking order. 2nd panel will be on top */
update_panels(); update_panels();
/* Show it on the screen */ /* Show it on the screen */
attron(COLOR_PAIR(4)); 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 - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)");
mvprintw(LINES - 2, 0, "F1 to Exit"); mvprintw(LINES - 2, 0, "F1 to Exit");
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(4));
doupdate(); doupdate();
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case 'a': { case 'a':
temp = (PANEL_DATA *)panel_userptr(my_panels[0]); temp = (PANEL_DATA *)panel_userptr(my_panels[0]);
if(temp->hide == FALSE) if(temp->hide == FALSE)
{ hide_panel(my_panels[0]); { hide_panel(my_panels[0]);
temp->hide = TRUE; temp->hide = TRUE;
} }
else else
{ show_panel(my_panels[0]); { show_panel(my_panels[0]);
temp->hide = FALSE; temp->hide = FALSE;
} }
break; break;
case 'b': case 'b':
temp = (PANEL_DATA *)panel_userptr(my_panels[1]); temp = (PANEL_DATA *)panel_userptr(my_panels[1]);
if(temp->hide == FALSE) if(temp->hide == FALSE)
{ hide_panel(my_panels[1]); { hide_panel(my_panels[1]);
temp->hide = TRUE; temp->hide = TRUE;
} }
else else
{ show_panel(my_panels[1]); { show_panel(my_panels[1]);
temp->hide = FALSE; temp->hide = FALSE;
} }
break; break;
case 'c': case 'c':
temp = (PANEL_DATA *)panel_userptr(my_panels[2]); temp = (PANEL_DATA *)panel_userptr(my_panels[2]);
if(temp->hide == FALSE) if(temp->hide == FALSE)
{ hide_panel(my_panels[2]); { hide_panel(my_panels[2]);
temp->hide = TRUE; temp->hide = TRUE;
} }
else else
{ show_panel(my_panels[2]); { show_panel(my_panels[2]);
temp->hide = FALSE; temp->hide = FALSE;
} }
break; break;
} }
update_panels(); update_panels();
doupdate(); doupdate();
} }
endwin(); endwin();
return 0; return 0;
} }
/* Put all the windows */ /* Put all the windows */
void init_wins(WINDOW **wins, int n) void init_wins(WINDOW **wins, int n)
{ int x, y, i; { int x, y, i;
char label[80]; char label[80];
y = 2; y = 2;
x = 10; x = 10;
for(i = 0; i < n; ++i) for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x); { wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1); sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1); win_show(wins[i], label, i + 1);
y += 3; y += 3;
x += 7; x += 7;
} }
} }
/* Show the window with a border and a label */ /* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color) void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width; { int startx, starty, height, width;
getbegyx(win, starty, startx); getbegyx(win, starty, startx);
getmaxyx(win, height, width); getmaxyx(win, height, width);
box(win, 0, 0); box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE); mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2); mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE); mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); 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) void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
wattron(win, color); wattron(win, color);
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
wattroff(win, color); wattroff(win, color);
refresh(); refresh();
} }

View File

@ -1,10 +1,10 @@
#include <panel.h> #include <panel.h>
typedef struct _PANEL_DATA { typedef struct _PANEL_DATA {
int x, y, w, h; int x, y, w, h;
char label[80]; char label[80];
int label_color; int label_color;
PANEL *next; PANEL *next;
}PANEL_DATA; }PANEL_DATA;
#define NLINES 10 #define NLINES 10
@ -16,219 +16,219 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
void set_user_ptrs(PANEL **panels, int n); void set_user_ptrs(PANEL **panels, int n);
int main() int main()
{ WINDOW *my_wins[3]; { WINDOW *my_wins[3];
PANEL *my_panels[3]; PANEL *my_panels[3];
PANEL_DATA *top; PANEL_DATA *top;
PANEL *stack_top; PANEL *stack_top;
WINDOW *temp_win, *old_win; WINDOW *temp_win, *old_win;
int ch; int ch;
int newx, newy, neww, newh; int newx, newy, neww, newh;
int size = FALSE, move = FALSE; int size = FALSE, move = FALSE;
/* Initialize curses */ /* Initialize curses */
initscr(); initscr();
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
/* Initialize all the colors */ /* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK); init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK); init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3); init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */ /* 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[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[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 */ my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
set_user_ptrs(my_panels, 3); set_user_ptrs(my_panels, 3);
/* Update the stacking order. 2nd panel will be on top */ /* Update the stacking order. 2nd panel will be on top */
update_panels(); update_panels();
/* Show it on the screen */ /* Show it on the screen */
attron(COLOR_PAIR(4)); attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 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)"); mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(4));
doupdate(); doupdate();
stack_top = my_panels[2]; stack_top = my_panels[2];
top = (PANEL_DATA *)panel_userptr(stack_top); top = (PANEL_DATA *)panel_userptr(stack_top);
newx = top->x; newx = top->x;
newy = top->y; newy = top->y;
neww = top->w; neww = top->w;
newh = top->h; newh = top->h;
while((ch = getch()) != KEY_F(1)) while((ch = getch()) != KEY_F(1))
{ switch(ch) { switch(ch)
{ case 9: /* Tab */ { case 9: /* Tab */
top = (PANEL_DATA *)panel_userptr(stack_top); top = (PANEL_DATA *)panel_userptr(stack_top);
top_panel(top->next); top_panel(top->next);
stack_top = top->next; stack_top = top->next;
top = (PANEL_DATA *)panel_userptr(stack_top); top = (PANEL_DATA *)panel_userptr(stack_top);
newx = top->x; newx = top->x;
newy = top->y; newy = top->y;
neww = top->w; neww = top->w;
newh = top->h; newh = top->h;
break; break;
case 'r': /* Re-Size*/ case 'r': /* Re-Size*/
size = TRUE; size = TRUE;
attron(COLOR_PAIR(4)); attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing"); mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing");
refresh(); refresh();
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(4));
break; break;
case 'm': /* Move */ case 'm': /* Move */
attron(COLOR_PAIR(4)); attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving"); mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving");
refresh(); refresh();
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(4));
move = TRUE; move = TRUE;
break; break;
case KEY_LEFT: case KEY_LEFT:
if(size == TRUE) if(size == TRUE)
{ --newx; { --newx;
++neww; ++neww;
} }
if(move == TRUE) if(move == TRUE)
--newx; --newx;
break; break;
case KEY_RIGHT: case KEY_RIGHT:
if(size == TRUE) if(size == TRUE)
{ ++newx; { ++newx;
--neww; --neww;
} }
if(move == TRUE) if(move == TRUE)
++newx; ++newx;
break; break;
case KEY_UP: case KEY_UP:
if(size == TRUE) if(size == TRUE)
{ --newy; { --newy;
++newh; ++newh;
} }
if(move == TRUE) if(move == TRUE)
--newy; --newy;
break; break;
case KEY_DOWN: case KEY_DOWN:
if(size == TRUE) if(size == TRUE)
{ ++newy; { ++newy;
--newh; --newh;
} }
if(move == TRUE) if(move == TRUE)
++newy; ++newy;
break; break;
case 10: /* Enter */ case 10: /* Enter */
move(LINES - 4, 0); move(LINES - 4, 0);
clrtoeol(); clrtoeol();
refresh(); refresh();
if(size == TRUE) if(size == TRUE)
{ old_win = panel_window(stack_top); { old_win = panel_window(stack_top);
temp_win = newwin(newh, neww, newy, newx); temp_win = newwin(newh, neww, newy, newx);
replace_panel(stack_top, temp_win); replace_panel(stack_top, temp_win);
win_show(temp_win, top->label, top->label_color); win_show(temp_win, top->label, top->label_color);
delwin(old_win); delwin(old_win);
size = FALSE; size = FALSE;
} }
if(move == TRUE) if(move == TRUE)
{ move_panel(stack_top, newy, newx); { move_panel(stack_top, newy, newx);
move = FALSE; move = FALSE;
} }
break; break;
} }
attron(COLOR_PAIR(4)); attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 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)"); mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(4));
refresh(); refresh();
update_panels(); update_panels();
doupdate(); doupdate();
} }
endwin(); endwin();
return 0; return 0;
} }
/* Put all the windows */ /* Put all the windows */
void init_wins(WINDOW **wins, int n) void init_wins(WINDOW **wins, int n)
{ int x, y, i; { int x, y, i;
char label[80]; char label[80];
y = 2; y = 2;
x = 10; x = 10;
for(i = 0; i < n; ++i) for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x); { wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1); sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1); win_show(wins[i], label, i + 1);
y += 3; y += 3;
x += 7; x += 7;
} }
} }
/* Set the PANEL_DATA structures for individual panels */ /* Set the PANEL_DATA structures for individual panels */
void set_user_ptrs(PANEL **panels, int n) void set_user_ptrs(PANEL **panels, int n)
{ PANEL_DATA *ptrs; { PANEL_DATA *ptrs;
WINDOW *win; WINDOW *win;
int x, y, w, h, i; int x, y, w, h, i;
char temp[80]; char temp[80];
ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));
for(i = 0;i < n; ++i) for(i = 0;i < n; ++i)
{ win = panel_window(panels[i]); { win = panel_window(panels[i]);
getbegyx(win, y, x); getbegyx(win, y, x);
getmaxyx(win, h, w); getmaxyx(win, h, w);
ptrs[i].x = x; ptrs[i].x = x;
ptrs[i].y = y; ptrs[i].y = y;
ptrs[i].w = w; ptrs[i].w = w;
ptrs[i].h = h; ptrs[i].h = h;
sprintf(temp, "Window Number %d", i + 1); sprintf(temp, "Window Number %d", i + 1);
strcpy(ptrs[i].label, temp); strcpy(ptrs[i].label, temp);
ptrs[i].label_color = i + 1; ptrs[i].label_color = i + 1;
if(i + 1 == n) if(i + 1 == n)
ptrs[i].next = panels[0]; ptrs[i].next = panels[0];
else else
ptrs[i].next = panels[i + 1]; ptrs[i].next = panels[i + 1];
set_panel_userptr(panels[i], &ptrs[i]); set_panel_userptr(panels[i], &ptrs[i]);
} }
} }
/* Show the window with a border and a label */ /* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color) void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width; { int startx, starty, height, width;
getbegyx(win, starty, startx); getbegyx(win, starty, startx);
getmaxyx(win, height, width); getmaxyx(win, height, width);
box(win, 0, 0); box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE); mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2); mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE); mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); 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) void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y; { int length, x, y;
float temp; float temp;
if(win == NULL) if(win == NULL)
win = stdscr; win = stdscr;
getyx(win, y, x); getyx(win, y, x);
if(startx != 0) if(startx != 0)
x = startx; x = startx;
if(starty != 0) if(starty != 0)
y = starty; y = starty;
if(width == 0) if(width == 0)
width = 80; width = 80;
length = strlen(string); length = strlen(string);
temp = (width - length)/ 2; temp = (width - length)/ 2;
x = startx + (int)temp; x = startx + (int)temp;
wattron(win, color); wattron(win, color);
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
wattroff(win, color); wattroff(win, color);
refresh(); refresh();
} }

View File

@ -1,38 +1,38 @@
#include <panel.h> #include <panel.h>
int main() int main()
{ WINDOW *my_wins[3]; { WINDOW *my_wins[3];
PANEL *my_panels[3]; PANEL *my_panels[3];
int lines = 10, cols = 40, y = 2, x = 4, i; int lines = 10, cols = 40, y = 2, x = 4, i;
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
/* Create windows for the panels */ /* Create windows for the panels */
my_wins[0] = newwin(lines, cols, y, x); my_wins[0] = newwin(lines, cols, y, x);
my_wins[1] = newwin(lines, cols, y + 1, x + 5); my_wins[1] = newwin(lines, cols, y + 1, x + 5);
my_wins[2] = newwin(lines, cols, y + 2, x + 10); my_wins[2] = newwin(lines, cols, y + 2, x + 10);
/* /*
* Create borders around the windows so that you can see the effect * Create borders around the windows so that you can see the effect
* of panels * of panels
*/ */
for(i = 0; i < 3; ++i) for(i = 0; i < 3; ++i)
box(my_wins[i], 0, 0); box(my_wins[i], 0, 0);
/* Attach a panel to each window */ /* Order is bottom up */ /* 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[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[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 */ 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 the stacking order. 2nd panel will be on top */
update_panels(); update_panels();
/* Show it on the screen */ /* Show it on the screen */
doupdate(); doupdate();
getch(); getch();
endwin(); endwin();
} }

View File

@ -20,9 +20,9 @@ $lines = join "", @lines;
while ($lines =~ /\G(.*?)(\/\*.*?\*\/)?/gs) { while ($lines =~ /\G(.*?)(\/\*.*?\*\/)?/gs) {
addstr($1); addstr($1);
if ($2) { if ($2) {
attron(A_BOLD); attron(A_BOLD);
addstr($2); addstr($2);
attroff(A_BOLD); attroff(A_BOLD);
} }
} }

View File

@ -23,20 +23,20 @@ $my_win = create_newwin($height, $width, $starty, $startx);
while (($ch = getch()) != KEY_F(1)) { while (($ch = getch()) != KEY_F(1)) {
if ($ch == KEY_LEFT) { if ($ch == KEY_LEFT) {
destroy_win($my_win); destroy_win($my_win);
$my_win = create_newwin($height, $width, $starty, --$startx); $my_win = create_newwin($height, $width, $starty, --$startx);
} }
elsif ($ch == KEY_RIGHT) { elsif ($ch == KEY_RIGHT) {
destroy_win($my_win); destroy_win($my_win);
$my_win = create_newwin($height, $width, $starty, ++$startx); $my_win = create_newwin($height, $width, $starty, ++$startx);
} }
elsif ($ch == KEY_UP) { elsif ($ch == KEY_UP) {
destroy_win($my_win); destroy_win($my_win);
$my_win = create_newwin($height, $width, --$starty, $startx); $my_win = create_newwin($height, $width, --$starty, $startx);
} }
elsif ($ch == KEY_DOWN) { elsif ($ch == KEY_DOWN) {
destroy_win($my_win); destroy_win($my_win);
$my_win = create_newwin($height, $width, ++$starty, $startx); $my_win = create_newwin($height, $width, ++$starty, $startx);
} }
} }

View File

@ -32,24 +32,24 @@ create_box(\%win, 1);
while (($ch = getch()) != KEY_F(1)) { while (($ch = getch()) != KEY_F(1)) {
if ($ch == KEY_LEFT) { if ($ch == KEY_LEFT) {
create_box(\%win, 0); create_box(\%win, 0);
$win{'startx'}--; $win{'startx'}--;
create_box(\%win, 1); create_box(\%win, 1);
} }
elsif ($ch == KEY_RIGHT) { elsif ($ch == KEY_RIGHT) {
create_box(\%win, 0); create_box(\%win, 0);
$win{'startx'}++; $win{'startx'}++;
create_box(\%win, 1); create_box(\%win, 1);
} }
elsif ($ch == KEY_UP) { elsif ($ch == KEY_UP) {
create_box(\%win, 0); create_box(\%win, 0);
$win{'starty'}--; $win{'starty'}--;
create_box(\%win, 1); create_box(\%win, 1);
} }
elsif ($ch == KEY_DOWN) { elsif ($ch == KEY_DOWN) {
create_box(\%win, 0); create_box(\%win, 0);
$win{'starty'}++; $win{'starty'}++;
create_box(\%win, 1); create_box(\%win, 1);
} }
} }
@ -74,10 +74,10 @@ sub init_win_params {
sub print_win_params { sub print_win_params {
if ($_DEBUG) { if ($_DEBUG) {
$p_win = shift; $p_win = shift;
addstr(25, 0, addstr(25, 0,
"$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}"); "$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}");
refresh(); refresh();
} }
} }
@ -91,21 +91,21 @@ sub create_box {
$h = $$p_win{'height'}; $h = $$p_win{'height'};
if ($bool) { if ($bool) {
addch($y, $x, $$p_win{'tl'}); addch($y, $x, $$p_win{'tl'});
addch($y, $x + $w, $$p_win{'tr'}); addch($y, $x + $w, $$p_win{'tr'});
addch($y + $h, $x, $$p_win{'bl'}); addch($y + $h, $x, $$p_win{'bl'});
addch($y + $h, $x + $w, $$p_win{'br'}); addch($y + $h, $x + $w, $$p_win{'br'});
hline($y, $x + 1, $$p_win{'ts'}, $w - 1); hline($y, $x + 1, $$p_win{'ts'}, $w - 1);
hline($y + $h, $x + 1, $$p_win{'bs'}, $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, $$p_win{'ls'}, $h - 1);
vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1); vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1);
} }
else { else {
for ($j = $y; $j <= $y + $h; $j++) { for ($j = $y; $j <= $y + $h; $j++) {
for ($i = $x; $i <= $x + $w; $i++) { for ($i = $x; $i <= $x + $w; $i++) {
addch($j, $i, ' '); addch($j, $i, ' ');
} }
} }
} }
refresh(); refresh();
} }

View File

@ -44,27 +44,27 @@ print_menu($menu_win, $highlight);
while (1) { while (1) {
$c = getch($menu_win); $c = getch($menu_win);
if ($c == KEY_UP) { if ($c == KEY_UP) {
if ($highlight == 1) { if ($highlight == 1) {
$highlight = $n_choices; $highlight = $n_choices;
} }
else { else {
$highlight--; $highlight--;
} }
} }
elsif ($c == KEY_DOWN) { elsif ($c == KEY_DOWN) {
if ($highlight == $n_choices) { if ($highlight == $n_choices) {
$highlight = 1; $highlight = 1;
} }
else { else {
$highlight++; $highlight++;
} }
} }
elsif ($c == '\n') { elsif ($c == '\n') {
$choice = $highlight; $choice = $highlight;
} }
else { else {
addstr($LINES - 2, 0, "Character pressed is $c"); addstr($LINES - 2, 0, "Character pressed is $c");
refresh(); refresh();
} }
print_menu($menu_win, $highlight); print_menu($menu_win, $highlight);
last if ($choice); last if ($choice);
@ -83,15 +83,15 @@ sub print_menu {
$y = 2; $y = 2;
box($menu_win, 0, 0); box($menu_win, 0, 0);
for ($i = 0; $i < $n_choices; $i++) { for ($i = 0; $i < $n_choices; $i++) {
if ($highlight == $i + 1) { if ($highlight == $i + 1) {
attron($menu_win, A_REVERSE); attron($menu_win, A_REVERSE);
addstr($menu_win, $y, $x, $choices[$i]); addstr($menu_win, $y, $x, $choices[$i]);
attroff($menu_win, A_REVERSE); attroff($menu_win, A_REVERSE);
} }
else { else {
addstr($menu_win, $y, $x, $choices[$i]); addstr($menu_win, $y, $x, $choices[$i]);
} }
$y++; $y++;
} }
refresh($menu_win); refresh($menu_win);
} }