From 8bbc2c1ea5a128e8eed5da6f76f77ba6777d984c Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:01:16 -0500 Subject: [PATCH 01/21] adding copy of ncurses programs --- ncurses_programs/COPYING | 23 ++ ncurses_programs/JustForFun/Makefile | 28 +++ ncurses_programs/JustForFun/README | 10 + ncurses_programs/JustForFun/hanoi.c | 178 +++++++++++++++ ncurses_programs/JustForFun/life.c | 107 +++++++++ ncurses_programs/JustForFun/magic.c | 161 ++++++++++++++ ncurses_programs/JustForFun/queens.c | 122 ++++++++++ ncurses_programs/JustForFun/shuffle.c | 205 +++++++++++++++++ ncurses_programs/JustForFun/tt.c | 223 +++++++++++++++++++ ncurses_programs/Makefile | 21 ++ ncurses_programs/README | 87 ++++++++ ncurses_programs/basics/Makefile | 35 +++ ncurses_programs/basics/README | 20 ++ ncurses_programs/basics/acs_vars.c | 44 ++++ ncurses_programs/basics/hello_world.c | 12 + ncurses_programs/basics/init_func_example.c | 31 +++ ncurses_programs/basics/key_code.c | 14 ++ ncurses_programs/basics/mouse_menu.c | 106 +++++++++ ncurses_programs/basics/other_border.c | 120 ++++++++++ ncurses_programs/basics/printw_example.c | 20 ++ ncurses_programs/basics/scanw_example.c | 20 ++ ncurses_programs/basics/simple_attr.c | 54 +++++ ncurses_programs/basics/simple_color.c | 40 ++++ ncurses_programs/basics/simple_key.c | 92 ++++++++ ncurses_programs/basics/temp_leave.c | 20 ++ ncurses_programs/basics/win_border.c | 82 +++++++ ncurses_programs/basics/with_chgat.c | 24 ++ ncurses_programs/forms/Makefile | 27 +++ ncurses_programs/forms/README | 9 + ncurses_programs/forms/form_attrib.c | 75 +++++++ ncurses_programs/forms/form_options.c | 76 +++++++ ncurses_programs/forms/form_simple.c | 66 ++++++ ncurses_programs/forms/form_win.c | 112 ++++++++++ ncurses_programs/menus/Makefile | 31 +++ ncurses_programs/menus/README | 13 ++ ncurses_programs/menus/menu_attrib.c | 80 +++++++ ncurses_programs/menus/menu_item_data.c | 66 ++++++ ncurses_programs/menus/menu_multi_column.c | 97 ++++++++ ncurses_programs/menus/menu_scroll.c | 124 +++++++++++ ncurses_programs/menus/menu_simple.c | 56 +++++ ncurses_programs/menus/menu_toggle.c | 84 +++++++ ncurses_programs/menus/menu_userptr.c | 87 ++++++++ ncurses_programs/menus/menu_win.c | 105 +++++++++ ncurses_programs/panels/Makefile | 27 +++ ncurses_programs/panels/README | 8 + ncurses_programs/panels/panel_browse.c | 117 ++++++++++ ncurses_programs/panels/panel_hide.c | 156 +++++++++++++ ncurses_programs/panels/panel_resize.c | 234 ++++++++++++++++++++ ncurses_programs/panels/panel_simple.c | 38 ++++ ncurses_programs/perl/01.pl | 17 ++ ncurses_programs/perl/02.pl | 33 +++ ncurses_programs/perl/03.pl | 23 ++ ncurses_programs/perl/04.pl | 23 ++ ncurses_programs/perl/05.pl | 32 +++ ncurses_programs/perl/06.pl | 22 ++ ncurses_programs/perl/07.pl | 58 +++++ ncurses_programs/perl/08.pl | 112 ++++++++++ ncurses_programs/perl/09.pl | 47 ++++ ncurses_programs/perl/10.pl | 98 ++++++++ ncurses_programs/perl/COPYING | 2 + ncurses_programs/perl/README | 6 + 61 files changed, 4060 insertions(+) create mode 100644 ncurses_programs/COPYING create mode 100644 ncurses_programs/JustForFun/Makefile create mode 100644 ncurses_programs/JustForFun/README create mode 100644 ncurses_programs/JustForFun/hanoi.c create mode 100644 ncurses_programs/JustForFun/life.c create mode 100644 ncurses_programs/JustForFun/magic.c create mode 100644 ncurses_programs/JustForFun/queens.c create mode 100644 ncurses_programs/JustForFun/shuffle.c create mode 100644 ncurses_programs/JustForFun/tt.c create mode 100644 ncurses_programs/Makefile create mode 100644 ncurses_programs/README create mode 100644 ncurses_programs/basics/Makefile create mode 100644 ncurses_programs/basics/README create mode 100644 ncurses_programs/basics/acs_vars.c create mode 100644 ncurses_programs/basics/hello_world.c create mode 100644 ncurses_programs/basics/init_func_example.c create mode 100644 ncurses_programs/basics/key_code.c create mode 100644 ncurses_programs/basics/mouse_menu.c create mode 100644 ncurses_programs/basics/other_border.c create mode 100644 ncurses_programs/basics/printw_example.c create mode 100644 ncurses_programs/basics/scanw_example.c create mode 100644 ncurses_programs/basics/simple_attr.c create mode 100644 ncurses_programs/basics/simple_color.c create mode 100644 ncurses_programs/basics/simple_key.c create mode 100644 ncurses_programs/basics/temp_leave.c create mode 100644 ncurses_programs/basics/win_border.c create mode 100644 ncurses_programs/basics/with_chgat.c create mode 100644 ncurses_programs/forms/Makefile create mode 100644 ncurses_programs/forms/README create mode 100644 ncurses_programs/forms/form_attrib.c create mode 100644 ncurses_programs/forms/form_options.c create mode 100644 ncurses_programs/forms/form_simple.c create mode 100644 ncurses_programs/forms/form_win.c create mode 100644 ncurses_programs/menus/Makefile create mode 100644 ncurses_programs/menus/README create mode 100644 ncurses_programs/menus/menu_attrib.c create mode 100644 ncurses_programs/menus/menu_item_data.c create mode 100644 ncurses_programs/menus/menu_multi_column.c create mode 100644 ncurses_programs/menus/menu_scroll.c create mode 100644 ncurses_programs/menus/menu_simple.c create mode 100644 ncurses_programs/menus/menu_toggle.c create mode 100644 ncurses_programs/menus/menu_userptr.c create mode 100644 ncurses_programs/menus/menu_win.c create mode 100644 ncurses_programs/panels/Makefile create mode 100644 ncurses_programs/panels/README create mode 100644 ncurses_programs/panels/panel_browse.c create mode 100644 ncurses_programs/panels/panel_hide.c create mode 100644 ncurses_programs/panels/panel_resize.c create mode 100644 ncurses_programs/panels/panel_simple.c create mode 100755 ncurses_programs/perl/01.pl create mode 100755 ncurses_programs/perl/02.pl create mode 100755 ncurses_programs/perl/03.pl create mode 100755 ncurses_programs/perl/04.pl create mode 100755 ncurses_programs/perl/05.pl create mode 100755 ncurses_programs/perl/06.pl create mode 100755 ncurses_programs/perl/07.pl create mode 100755 ncurses_programs/perl/08.pl create mode 100755 ncurses_programs/perl/09.pl create mode 100755 ncurses_programs/perl/10.pl create mode 100644 ncurses_programs/perl/COPYING create mode 100644 ncurses_programs/perl/README diff --git a/ncurses_programs/COPYING b/ncurses_programs/COPYING new file mode 100644 index 0000000..3e34ea6 --- /dev/null +++ b/ncurses_programs/COPYING @@ -0,0 +1,23 @@ +Copyright (c) 2001, Pradeep Padala (ppadala@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, distribute with +modifications, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name(s) of the above copyright holders +shall not be used in advertising or otherwise to promote the sale, use or +other dealings in this Software without prior written authorization. diff --git a/ncurses_programs/JustForFun/Makefile b/ncurses_programs/JustForFun/Makefile new file mode 100644 index 0000000..3f0890e --- /dev/null +++ b/ncurses_programs/JustForFun/Makefile @@ -0,0 +1,28 @@ +# Makefile for JustForFun Files + +# A few variables + +CC=gcc +LIBS=-lncurses + +SRC_DIR=. +EXE_DIR=../demo/exe + +EXES = \ + ${EXE_DIR}/hanoi \ + ${EXE_DIR}/life\ + ${EXE_DIR}/magic \ + ${EXE_DIR}/queens \ + ${EXE_DIR}/shuffle \ + ${EXE_DIR}/tt + +${EXE_DIR}/%: %.o + ${CC} -o $@ $< ${LIBS} + +%.o: ${SRC_DIR}/%.c + ${CC} -o $@ -c $< + +all: ${EXES} + +clean: + @rm -f ${EXES} diff --git a/ncurses_programs/JustForFun/README b/ncurses_programs/JustForFun/README new file mode 100644 index 0000000..a5d743f --- /dev/null +++ b/ncurses_programs/JustForFun/README @@ -0,0 +1,10 @@ +Description of files +-------------------- +JustForFun + | + |----> hanoi.c -- The Towers of Hanoi Solver + |----> life.c -- The Game of Life demo + |----> magic.c -- An Odd Order Magic Square builder + |----> queens.c -- The famous N-Queens Solver + |----> shuffle.c -- A fun game, if you have time to kill + |----> tt.c -- A very trivial typing tutor diff --git a/ncurses_programs/JustForFun/hanoi.c b/ncurses_programs/JustForFun/hanoi.c new file mode 100644 index 0000000..b57b0b7 --- /dev/null +++ b/ncurses_programs/JustForFun/hanoi.c @@ -0,0 +1,178 @@ +#include + +#define POSX 10 +#define POSY 5 +#define DISC_CHAR '*' +#define PEG_CHAR '#' +#define TIME_OUT 300 + +typedef struct _peg_struct { + int n_discs; /* Number of discs at present */ + int bottomx, bottomy; /* bottom x, bottom y co-ord */ + int *sizes; /* The disc sizes array */ +}peg; + +void init_pegs(peg *p_my_pegs, int n_discs); +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs); +void free_pegs(peg *p_my_pegs, int n_discs); +void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst); +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); +void check_usr_response(peg *p_my_pegs, int n_discs); + +int store_n_discs; +char *welcome_string = "Enter the number of discs you want to be solved: "; + +int main(int argc, char *argv[]) +{ int n_discs; + peg my_pegs[3]; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled. Pass on every thing */ + keypad(stdscr, TRUE); + curs_set(FALSE); + + print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); + scanw("%d", &n_discs); + + timeout(TIME_OUT); + noecho(); + store_n_discs = n_discs; + + init_pegs(my_pegs, n_discs); + show_pegs(stdscr, my_pegs, n_discs); + solve_hanoi(my_pegs, n_discs, 0, 1, 2); + + free_pegs(my_pegs, n_discs); + endwin(); /* End curses mode */ + return 0; +} + +void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) +{ if(n_discs == 0) + return; + solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); + move_disc(p_my_pegs, store_n_discs, src, dst); + show_pegs(stdscr, p_my_pegs, store_n_discs); + check_usr_response(p_my_pegs, store_n_discs); + solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst); +} + +void check_usr_response(peg *p_my_pegs, int n_discs) +{ int ch; + + ch = getch(); /* Waits for TIME_OUT milliseconds */ + if(ch == ERR) + return; + else + if(ch == KEY_F(1)) + { free_pegs(p_my_pegs, n_discs); + endwin(); + exit(0); + } +} + +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) +{ int temp, index; + + --p_my_pegs[src].n_discs; + index = 0; + while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) + ++index; + temp = p_my_pegs[src].sizes[index]; + p_my_pegs[src].sizes[index] = 0; + + index = 0; + while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) + ++index; + --index; + p_my_pegs[dst].sizes[index] = temp; + ++p_my_pegs[dst].n_discs; +} + +void init_pegs(peg *p_my_pegs, int n_discs) +{ int size, temp, i; + + p_my_pegs[0].n_discs = n_discs; + + /* Allocate memory for size array + * atmost the number of discs on a peg can be n_discs + */ + for(i = 0; i < n_discs; ++i) + p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); + size = 3; + for(i = 0;i < n_discs; ++i, size += 2) + p_my_pegs[0].sizes[i] = size; + + temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); + p_my_pegs[0].bottomx = POSX + 1 + temp; + p_my_pegs[0].bottomy = POSY + 2 + n_discs; + + p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp; + p_my_pegs[1].bottomy = POSY + 2 + n_discs; + + p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp; + p_my_pegs[2].bottomy = POSY + 2 + n_discs; +} + +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) +{ int i, j, k, x, y, size; + + wclear(win); + attron(A_REVERSE); + mvprintw(24, 0, "Press F1 to Exit"); + attroff(A_REVERSE); + for(i = 0;i < 3; ++i) + mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, + p_my_pegs[i].bottomx, "%c", PEG_CHAR); + y = p_my_pegs[0].bottomy - n_discs; + for(i = 0; i < 3; ++i) /* For each peg */ + { for(j = 0; j < n_discs; ++ j) /* For each row */ + { if(p_my_pegs[i].sizes[j] != 0) + { size = p_my_pegs[i].sizes[j]; + x = p_my_pegs[i].bottomx - (size / 2); + for(k = 0; k < size; ++k) + mvwprintw(win, y, x + k, "%c", DISC_CHAR); + } + else + mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); + ++y; + } + y = p_my_pegs[0].bottomy - n_discs; + } + wrefresh(win); +} + +void free_pegs(peg *p_my_pegs, int n_discs) +{ int i; + + for(i = 0;i < n_discs; ++i) + free(p_my_pegs[i].sizes); +} + +/* -------------------------------------------------------------* + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * -------------------------------------------------------------*/ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/ncurses_programs/JustForFun/life.c b/ncurses_programs/JustForFun/life.c new file mode 100644 index 0000000..eb6a039 --- /dev/null +++ b/ncurses_programs/JustForFun/life.c @@ -0,0 +1,107 @@ +#include + +int STARTX = 0; +int STARTY = 0; +int ENDX = 79; +int ENDY = 24; + +#define CELL_CHAR '#' +#define TIME_OUT 300 + +typedef struct _state { + int oldstate; + int newstate; +}state; + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); +void calc(state **area, int x, int y); +void update_state(state **area, int startx, int starty, int endx, int endy); + +int main() +{ state **workarea; + int i, j; + + initscr(); + cbreak(); + timeout(TIME_OUT); + keypad(stdscr, TRUE); + + ENDX = COLS - 1; + ENDY = LINES - 1; + + workarea = (state **)calloc(COLS, sizeof(state *)); + for(i = 0;i < COLS; ++i) + workarea[i] = (state *)calloc(LINES, sizeof(state)); + + /* For inverted U */ + workarea[39][15].newstate = TRUE; + workarea[40][15].newstate = TRUE; + workarea[41][15].newstate = TRUE; + workarea[39][16].newstate = TRUE; + workarea[39][17].newstate = TRUE; + workarea[41][16].newstate = TRUE; + workarea[41][17].newstate = TRUE; + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + + /* For block */ +/* + workarea[37][13].newstate = TRUE; + workarea[37][14].newstate = TRUE; + workarea[38][13].newstate = TRUE; + workarea[38][14].newstate = TRUE; + + update_state(workarea, STARTX, STARTY, ENDX, ENDY); +*/ + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + while(getch() != KEY_F(1)) + { for(i = STARTX; i <= ENDX; ++i) + for(j = STARTY; j <= ENDY; ++j) + calc(workarea, i, j); + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + } + + endwin(); + return 0; +} + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) +{ int i, j; + wclear(win); + for(i = startx; i <= endx; ++i) + for(j = starty;j <= endy; ++j) + if(area[i][j].newstate == TRUE) + mvwaddch(win, j, i, CELL_CHAR); + wrefresh(win); +} + +void calc(state **area, int i, int j) +{ int neighbours; + int newstate; + + neighbours = + area[(i - 1 + COLS) % COLS][j].oldstate + + area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + + area[(i + 1) % COLS][j].oldstate + + area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i + 1) % COLS][(j + 1) % LINES].oldstate + + area[i][(j - 1 + LINES) % LINES].oldstate + + area[i][(j + 1) % LINES].oldstate; + + newstate = FALSE; + if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) + newstate = TRUE; + else + if(area[i][j].oldstate == FALSE && neighbours == 3) + newstate = TRUE; + area[i][j].newstate = newstate; +} + +void update_state(state **area, int startx, int starty, int endx, int endy) +{ int i, j; + + for(i = startx; i <= endx; ++i) + for(j = starty; j <= endy; ++j) + area[i][j].oldstate = area[i][j].newstate; +} diff --git a/ncurses_programs/JustForFun/magic.c b/ncurses_programs/JustForFun/magic.c new file mode 100644 index 0000000..6561941 --- /dev/null +++ b/ncurses_programs/JustForFun/magic.c @@ -0,0 +1,161 @@ +#include +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define TRACE_VALUE TRACE_MAXIMUM + +void board( WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void magic(int **, int); +void print(int **, int); +void magic_board(int **a,int n); + +int main(int argc, char *argv[]) +{ + + int **a,n,i; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(0); + } + n = atoi(argv[1]); + if(n % 2 == 0) + { printf("Sorry !!! I don't know how to create magic square of even order\n"); + printf("The order should be an odd number\n"); + exit(0); + } + a = (int **) malloc(n * sizeof(int*)); + for(i = 0;i < n;++i) + a[i] = (int *)malloc(n * sizeof(int)); + + magic(a,n); + + initscr(); + curs_set(0); + noecho(); + magic_board(a,n); + getch(); + endwin(); + + return; +} + +void magic(int **a, int n) +{ + int i,j,k; + int row,col; + for(i = 0;i < n;++i) + for(j = 0;j < n;++j) + a[i][j] = -1; + row = 0; + col = n / 2; + + k = 1; + a[row][col] = k; + + while(k != n * n) + { + if(row == 0 && col != n - 1) + { row = n - 1; + col ++; + a[row][col] = ++k; + } + else if(row != 0 && col != n - 1) + { if(a[row - 1][col + 1] == -1) + { row --; + col ++; + a[row][col] = ++k; + } + else + { + row ++; + a[row][col] = ++k; + } + } + else if(row != 0 && col == n - 1) + { + row --; + col = 0; + a[row][col] = ++k; + } + else if(row == 0 && col == n - 1) + { row ++; + a[row][col] = ++k; + } + + } + return; +} + +void print(int **a,int n) +{ int i,j; + int x,y; + x = STARTX; + y = STARTY; + mvprintw(1,30,"MAGIC SQUARE"); + for(i = 0;i < n;++i) + { for(j = 0;j < n;++j) + { mvprintw(y,x,"%d",a[i][j]); + if(n > 9) + x += 4; + else + x += 6; + } + x = STARTX; + if(n > 7) + y += 2; + else + y += 3; + } + refresh(); +} +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void magic_board(int **a,int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(i = 0;i < n; ++i) + for(j = 0; j < n; ++j) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%d", a[i][j]); +} diff --git a/ncurses_programs/JustForFun/queens.c b/ncurses_programs/JustForFun/queens.c new file mode 100644 index 0000000..2457ccc --- /dev/null +++ b/ncurses_programs/JustForFun/queens.c @@ -0,0 +1,122 @@ +#include +#include + +#define QUEEN_CHAR '*' + +int *nqueens(int num); +int place(int current, int *position); +int print(int *positions, int num_queens); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); + +int main(int argc, char *argv[]) +{ + int num_queens, *positions, count; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + + num_queens = atoi(argv[1]); + initscr(); + cbreak(); + keypad(stdscr, TRUE); + positions = nqueens(num_queens); + free(positions); + endwin(); + return 0; +} + +int *nqueens(int num) +{ + int current, *position, num_solutions = 0; + + position = (int *) calloc(num + 1, sizeof(int)); + + position[1] = 0; + current = 1; /* current queen is being checked */ + /* position[current] is the coloumn*/ + while(current > 0){ + position[current] += 1; + while(position[current] <= num && !place(current, position) ) + position[current] += 1; + if(position[current] <= num){ + if(current == num) { + ++num_solutions; + print(position, num); + } + else { + current += 1; + position[current] = 0; + } + } + else current -= 1; /* backtrack */ + } + printf("Total Number of Solutions : %d\n", num_solutions); + return(position); +} + +int place(int current, int *position) +{ + int i; + if(current == 1) return(1); + for(i = 1; i < current; ++i) + if(position[i] == position[current]) return(0); + else if(abs(position[i] - position[current]) == + abs(i - current)) + return(0); + + return(1); +} + +int print(int *positions, int num_queens) +{ int count; + int y = 2, x = 2, w = 4, h = 2; + static int solution = 1; + + mvprintw(0, 0, "Solution No: %d", solution++); + board(stdscr, y, x, num_queens, num_queens, w, h); + for(count = 1; count <= num_queens; ++count) + { int tempy = y + (count - 1) * h + h / 2; + int tempx = x + (positions[count] - 1) * w + w / 2; + mvaddch(tempy, tempx, QUEEN_CHAR); + } + refresh(); + mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); + if(getch() == KEY_F(1)) + { endwin(); + exit(0); + } + clear(); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} diff --git a/ncurses_programs/JustForFun/shuffle.c b/ncurses_programs/JustForFun/shuffle.c new file mode 100644 index 0000000..0b8150b --- /dev/null +++ b/ncurses_programs/JustForFun/shuffle.c @@ -0,0 +1,205 @@ +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define BLANK 0 + +typedef struct _tile { + int x; + int y; +}tile; + +void init_board(int **board, int n, tile *blank); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void shuffle_board(int **board, int n); +void move_blank(int direction, int **s_board, int n, tile *blank); +int check_win(int **s_board, int n, tile *blank); + +enum { LEFT, RIGHT, UP, DOWN }; + +int main(int argc, char *argv[]) +{ int **s_board; + int n, i, ch; + tile blank; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + n = atoi(argv[1]); + + s_board = (int **)calloc(n, sizeof(int *)); + for(i = 0;i < n; ++i) + s_board[i] = (int *)calloc(n, sizeof(int)); + init_board(s_board, n, &blank); + initscr(); + keypad(stdscr, TRUE); + cbreak(); + shuffle_board(s_board, n); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + move_blank(RIGHT, s_board, n, &blank); + break; + case KEY_RIGHT: + move_blank(LEFT, s_board, n, &blank); + break; + case KEY_UP: + move_blank(DOWN, s_board, n, &blank); + break; + case KEY_DOWN: + move_blank(UP, s_board, n, &blank); + break; + } + shuffle_board(s_board, n); + if(check_win(s_board, n, &blank) == TRUE) + { mvprintw(24, 0, "You Win !!!\n"); + refresh(); + break; + } + } + endwin(); + return 0; +} + +void move_blank(int direction, int **s_board, int n, tile *blank) +{ int temp; + + switch(direction) + { case LEFT: + { if(blank->x != 0) + { --blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x + 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case RIGHT: + { if(blank->x != n - 1) + { ++blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x - 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case UP: + { if(blank->y != 0) + { --blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y + 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case DOWN: + { if(blank->y != n - 1) + { ++blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y - 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + } +} + +int check_win(int **s_board, int n, tile *blank) +{ int i, j; + + s_board[blank->x][blank->y] = n * n; + for(i = 0;i < n; ++i) + for(j = 0;j < n; ++j) + if(s_board[i][j] != j * n + i + 1) + { s_board[blank->x][blank->y] = BLANK; + return FALSE; + } + + s_board[blank->x][blank->y] = BLANK; + return TRUE; +} + +void init_board(int **s_board, int n, tile *blank) +{ int i, j, k; + int *temp_board; + + temp_board = (int *)calloc(n * n, sizeof(int)); + srand(time(NULL)); + for(i = 0;i < n * n; ++i) + { +repeat : + k = rand() % (n * n); + for(j = 0;j <= i - 1; ++j) + if (k == temp_board[j]) + goto repeat; + else + temp_board[i] = k; + } + k = 0; + for (i = 0;i < n;++i) + for(j = 0;j < n; ++j,++k) + { if(temp_board[k] == 0) + { blank->x = i; + blank->y = j; + } + s_board[i][j] = temp_board[k]; + } + free(temp_board); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void shuffle_board(int **s_board, int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + clear(); + mvprintw(24, 0, "Press F1 to Exit"); + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(j = 0; j < n; ++j) + for(i = 0;i < n; ++i) + if(s_board[i][j] != BLANK) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%-2d", s_board[i][j]); + refresh(); +} + + diff --git a/ncurses_programs/JustForFun/tt.c b/ncurses_programs/JustForFun/tt.c new file mode 100644 index 0000000..4378e67 --- /dev/null +++ b/ncurses_programs/JustForFun/tt.c @@ -0,0 +1,223 @@ +#include +#include +#include +#include + +#define HSIZE 60 +#define LENGTH 75 +#define WIDTH 10 +#define STARTX 1 +#define STARTY 5 +#define STATUSX 1 +#define STATUSY 25 + +#define KEY_F1 265 + +int print_menu(); +void print_byebye(); +void create_test_string(); +void print_time(time_t startt, time_t endt, int mistakes); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); + +char *groups[] = { "`123456" , + "7890-=" , + "~!@#$%^" , + "&*()_+" , + "<>?" , + ",./\\" , + "asdfg", + "jkl;'", + "qwer", + "uiop", + "tyur", + "zxcv", + "bnm", + }; +int n_groups; + +int main() +{ int choice, i; + char *test_array; + int ch = KEY_F1; + int mistakes; + int x, y; + time_t start_t, end_t; + WINDOW *typing_win; + char string[80]; + + string[0] = '\0'; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + intrflush(stdscr, FALSE); + + srandom(time(NULL)); + n_groups = sizeof(groups) / sizeof(char *); + test_array = (char *)calloc(HSIZE + 1, sizeof(char)); + + while(1) + { + if(ch == KEY_F1) + { choice = print_menu(); + choice -= 1; + if(choice == n_groups) + { print_byebye(); + free(test_array); + endwin(); + exit(0); + } + } + clear(); + strcpy(string, "Typing window"); + print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); + refresh(); + attroff(A_REVERSE); + + create_test_string(test_array, choice); + typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); + keypad(typing_win, TRUE); + intrflush(typing_win, FALSE); + box(typing_win, 0, 0); + + x = 1; + y = 1; + mvwprintw(typing_win, y, x, "%s", test_array); + wrefresh(typing_win); + y += 1; + + mistakes = 0; + i = 0; + time(&start_t); + wmove(typing_win, y, x); + wrefresh(typing_win); + ch = 0; + while(ch != KEY_F1 && i != HSIZE + 1) + { ch = wgetch(typing_win); + mvwprintw(typing_win, y, x, "%c", ch); + wrefresh(typing_win); + ++x; + if(ch == test_array[i]) + { ++i; + continue; + } + else + { ++mistakes; + ++i; + } + } + + time(&end_t); + print_time(start_t, end_t, mistakes); + } + free(test_array); + endwin(); + return 0; +} + + +int print_menu() +{ int choice, i; + + choice = 0; + while(1) + { clear(); + printw("\n\n"); + print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); + printw("\n\n\n"); + for(i = 0;i <= n_groups - 1; ++i) + printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); + printw("\t%3d: \tExit\n", i + 1); + + printw("\n\n\tChoice: "); + refresh(); + echo(); + scanw("%d", &choice); + noecho(); + + if(choice >= 1 && choice <= n_groups + 1) + break; + else + { attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); + attroff(A_REVERSE); + getch(); + } + } + return choice; +} + +void create_test_string(char *test_array, int choice) +{ int i, index, length; + + length = strlen(groups[choice]); + for(i = 0;i <= HSIZE - 1; ++i) + { if(i%5 == 0) + test_array[i] = ' '; + else + { index = (int)(random() % length); + test_array[i] = groups[choice][index]; + } + } + test_array[i] = '\0'; +} + +void print_byebye() +{ printw("\n"); + print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); + print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL); + refresh(); +} + +void print_time(time_t start_t, time_t end_t, int mistakes) +{ long int diff; + int h,m,s; + float wpm; + + diff = end_t - start_t; + wpm = ((HSIZE / 5)/(double)diff)*60; + + h = (int)(diff / 3600); + diff -= h * 3600; + m = (int)(diff / 60); + diff -= m * 60; + s = (int)diff; + + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm); + attroff(A_REVERSE); + + refresh(); + getch(); + +} + +/* ---------------------------------------------------------------- * + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * ---------------------------------------------------------------- */ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/ncurses_programs/Makefile b/ncurses_programs/Makefile new file mode 100644 index 0000000..dff988e --- /dev/null +++ b/ncurses_programs/Makefile @@ -0,0 +1,21 @@ +# The top level Makefile + +all: + cd JustForFun && $(MAKE) + cd basics && $(MAKE) + cd forms && $(MAKE) + cd menus && $(MAKE) + cd panels && $(MAKE) + @echo + @echo "*********************************************" + @echo "All files Built" + @echo "Please move to demo/exe directory" + @echo "Execute each file to see examples in action" + @echo "*********************************************" + @echo +clean: + cd JustForFun && $(MAKE) clean + cd basics && $(MAKE) clean + cd forms && $(MAKE) clean + cd menus && $(MAKE) clean + cd panels && $(MAKE) clean diff --git a/ncurses_programs/README b/ncurses_programs/README new file mode 100644 index 0000000..e3a535a --- /dev/null +++ b/ncurses_programs/README @@ -0,0 +1,87 @@ +This is the top level README file. +This directory is structured as follows: + +ncurses + | + |----> JustForFun -- just for fun programs + |----> basics -- basic programs + |----> demo -- output files go into this directory after make + | | + | |----> exe -- exe files of all example programs + |----> forms -- programs related to form library + |----> menus -- programs related to menus library + |----> panels -- programs related to panels library + |----> perl -- perl equivalents of the examples (contributed + | by Anuradha Ratnaweera) + |----> Makefile -- the top level Makefile + |----> README -- the top level README file. contains instructions + | -- to create executables for example programs + +To compile and install all example programs, just run make in this directory. + + make + +It installs all the exe files in demo/exe directory. You can go to that direcory +and see the examples in action. + +Have Fun !!! + -- Pradeep Padala + +Description of files in each directory +-------------------------------------- +JustForFun + | + |----> hanoi.c -- The Towers of Hanoi Solver + |----> life.c -- The Game of Life demo + |----> magic.c -- An Odd Order Magic Square builder + |----> queens.c -- The famous N-Queens Solver + |----> shuffle.c -- A fun game, if you have time to kill + |----> tt.c -- A very trivial typing tutor + + basics + | + |----> acs_vars.c -- ACS_ variables example + |----> hello_world.c -- Simple "Hello World" Program + |----> init_func_example.c -- Initialization functions example + |----> key_code.c -- Shows the scan code of the key pressed + |----> mouse_menu.c -- A menu accessible by mouse + |----> other_border.c -- Shows usage of other border functions apart + | -- box() + |----> printw_example.c -- A very simple printw() example + |----> scanw_example.c -- A very simple getstr() example + |----> simple_attr.c -- A program that can print a c file with comments + | -- in attribute + |----> simple_color.c -- A simple example demonstrating colors + |----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows + |----> temp_leave.c -- Demonstrates temporarily leaving curses mode + |----> win_border.c -- Shows Creation of windows and borders + |----> with_chgat.c -- chgat() usage example + + forms + | + |----> form_attrib.c -- Usage of field attributes + |----> form_options.c -- Usage of field options + |----> form_simple.c -- A simple form example + |----> form_win.c -- Demo of windows associated with forms + + menus + | + |----> menu_attrib.c -- Usage of menu attributes + |----> menu_item_data.c -- Usage of item_name() etc.. functions + |----> menu_multi_column.c -- Creates multi columnar menus + |----> menu_scroll.c -- Demonstrates scrolling capability of menus + |----> menu_simple.c -- A simple menu accessed by arrow keys + |----> menu_toggle.c -- Creates multi valued menus and explains + | -- REQ_TOGGLE_ITEM + |----> menu_userptr.c -- Usage of user pointer + |----> menu_win.c -- Demo of windows associated with menus + + panels + | + |----> panel_browse.c -- Panel browsing through tab. Usage of user pointer + |----> panel_hide.c -- Hiding and Un hiding of panels + |----> panel_resize.c -- Moving and resizing of panels + |----> panel_simple.c -- A simple panel example + + perl + |----> 01-10.pl -- Perl equivalents of first ten example programs diff --git a/ncurses_programs/basics/Makefile b/ncurses_programs/basics/Makefile new file mode 100644 index 0000000..90ec1fb --- /dev/null +++ b/ncurses_programs/basics/Makefile @@ -0,0 +1,35 @@ +# Makefile for JustForFun Files + +# A few variables + +CC=gcc +LIBS=-lncurses + +SRC_DIR=. +EXE_DIR=../demo/exe + +EXES = \ + ${EXE_DIR}/hello_world \ + ${EXE_DIR}/init_func_example \ + ${EXE_DIR}/key_code \ + ${EXE_DIR}/mouse_menu \ + ${EXE_DIR}/other_border \ + ${EXE_DIR}/printw_example \ + ${EXE_DIR}/scanw_example \ + ${EXE_DIR}/simple_attr \ + ${EXE_DIR}/simple_color \ + ${EXE_DIR}/simple_key \ + ${EXE_DIR}/temp_leave \ + ${EXE_DIR}/win_border \ + ${EXE_DIR}/with_chgat + +${EXE_DIR}/%: %.o + ${CC} -o $@ $< ${LIBS} + +%.o: ${SRC_DIR}/%.c + ${CC} -o $@ -c $< + +all: ${EXES} + +clean: + @rm -f ${EXES} diff --git a/ncurses_programs/basics/README b/ncurses_programs/basics/README new file mode 100644 index 0000000..0760867 --- /dev/null +++ b/ncurses_programs/basics/README @@ -0,0 +1,20 @@ +Description of files +-------------------- + basics + | + |----> acs_vars.c -- ACS_ variables example + |----> hello_world.c -- Simple "Hello World" Program + |----> init_func_example.c -- Initialization functions example + |----> key_code.c -- Shows the scan code of the key pressed + |----> mouse_menu.c -- A menu accessible by mouse + |----> other_border.c -- Shows usage of other border functions apart + | -- box() + |----> printw_example.c -- A very simple printw() example + |----> scanw_example.c -- A very simple getstr() example + |----> simple_attr.c -- A program that can print a c file with comments + | -- in attribute + |----> simple_color.c -- A simple example demonstrating colors + |----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows + |----> temp_leave.c -- Demonstrates temporarily leaving curses mode + |----> win_border.c -- Shows Creation of windows and borders + |----> with_chgat.c -- chgat() usage example diff --git a/ncurses_programs/basics/acs_vars.c b/ncurses_programs/basics/acs_vars.c new file mode 100644 index 0000000..c1c72a3 --- /dev/null +++ b/ncurses_programs/basics/acs_vars.c @@ -0,0 +1,44 @@ +#include + +int main() +{ + initscr(); + + printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); + printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); + printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); + printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); + printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); + printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); + printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); + printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); + printw("Vertical line "); addch(ACS_VLINE); printw("\n"); + printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); + printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); + printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); + printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); + printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); + printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); + printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); + printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); + printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); + printw("Bullet "); addch(ACS_BULLET); printw("\n"); + printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); + printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); + printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); + printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); + printw("Board of squares "); addch(ACS_BOARD); printw("\n"); + printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); + printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); + printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); + printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); + printw("Pi "); addch(ACS_PI); printw("\n"); + printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); + printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); + + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/ncurses_programs/basics/hello_world.c b/ncurses_programs/basics/hello_world.c new file mode 100644 index 0000000..55b24df --- /dev/null +++ b/ncurses_programs/basics/hello_world.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/ncurses_programs/basics/init_func_example.c b/ncurses_programs/basics/init_func_example.c new file mode 100644 index 0000000..2eb19b7 --- /dev/null +++ b/ncurses_programs/basics/init_func_example.c @@ -0,0 +1,31 @@ +#include + +int main() +{ int ch; + + initscr(); /* Start curses mode */ + raw(); /* Line buffering disabled */ + keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ + noecho(); /* Don't echo() while we do getch */ + + printw("Type any character to see it in bold\n"); + ch = getch(); /* If raw() hadn't been called + * we have to press enter before it + * gets to the program */ + if(ch == KEY_F(1)) /* Without keypad enabled this will */ + printw("F1 Key pressed");/* not get to us either */ + /* Without noecho() some ugly escape + * charachters might have been printed + * on screen */ + else + { printw("The pressed key is "); + attron(A_BOLD); + printw("%c", ch); + attroff(A_BOLD); + } + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/ncurses_programs/basics/key_code.c b/ncurses_programs/basics/key_code.c new file mode 100644 index 0000000..8c5d06c --- /dev/null +++ b/ncurses_programs/basics/key_code.c @@ -0,0 +1,14 @@ +#include + +int main() +{ int ch; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + ch = getch(); + endwin(); + printf("The key pressed is %d\n", ch); +} diff --git a/ncurses_programs/basics/mouse_menu.c b/ncurses_programs/basics/mouse_menu.c new file mode 100644 index 0000000..5b92e1e --- /dev/null +++ b/ncurses_programs/basics/mouse_menu.c @@ -0,0 +1,106 @@ +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int n_choices = sizeof(choices) / sizeof(char *); + +void print_menu(WINDOW *menu_win, int highlight); +void report_choice(int mouse_x, int mouse_y, int *p_choice); + +int main() +{ int c, choice = 0; + WINDOW *menu_win; + MEVENT event; + + /* Initialize curses */ + initscr(); + clear(); + noecho(); + cbreak(); //Line buffering disabled. pass on everything + + /* Try to put the window in the middle of screen */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + attron(A_REVERSE); + mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); + refresh(); + attroff(A_REVERSE); + + /* Print the menu for the first time */ + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + print_menu(menu_win, 1); + /* Get all the mouse events */ + mousemask(ALL_MOUSE_EVENTS, NULL); + + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_MOUSE: + if(getmouse(&event) == OK) + { /* When the user clicks left mouse button */ + if(event.bstate & BUTTON1_PRESSED) + { report_choice(event.x + 1, event.y + 1, &choice); + if(choice == -1) //Exit chosen + goto end; + mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); + refresh(); + } + } + print_menu(menu_win, choice); + break; + } + } +end: + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + +/* Report the choice according to mouse position */ +void report_choice(int mouse_x, int mouse_y, int *p_choice) +{ int i,j, choice; + + i = startx + 2; + j = starty + 3; + + for(choice = 0; choice < n_choices; ++choice) + if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) + { if(choice == n_choices - 1) + *p_choice = -1; + else + *p_choice = choice + 1; + break; + } +} diff --git a/ncurses_programs/basics/other_border.c b/ncurses_programs/basics/other_border.c new file mode 100644 index 0000000..725d67a --- /dev/null +++ b/ncurses_programs/basics/other_border.c @@ -0,0 +1,120 @@ +#include + +typedef struct _win_border_struct { + chtype ls, rs, ts, bs, + tl, tr, bl, br; +}WIN_BORDER; + +typedef struct _WIN_struct { + + int startx, starty; + int height, width; + WIN_BORDER border; +}WIN; + +void init_win_params(WIN *p_win); +void print_win_params(WIN *p_win); +void create_box(WIN *win, bool flag); + +int main(int argc, char *argv[]) +{ WIN win; + int ch; + + initscr(); /* Start curses mode */ + start_color(); /* Start the color functionality */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + noecho(); + init_pair(1, COLOR_CYAN, COLOR_BLACK); + + /* Initialize the window parameters */ + init_win_params(&win); + print_win_params(&win); + + attron(COLOR_PAIR(1)); + printw("Press F1 to exit"); + refresh(); + attroff(COLOR_PAIR(1)); + + create_box(&win, TRUE); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + create_box(&win, FALSE); + --win.startx; + create_box(&win, TRUE); + break; + case KEY_RIGHT: + create_box(&win, FALSE); + ++win.startx; + create_box(&win, TRUE); + break; + case KEY_UP: + create_box(&win, FALSE); + --win.starty; + create_box(&win, TRUE); + break; + case KEY_DOWN: + create_box(&win, FALSE); + ++win.starty; + create_box(&win, TRUE); + break; + } + } + endwin(); /* End curses mode */ + return 0; +} +void init_win_params(WIN *p_win) +{ + p_win->height = 3; + p_win->width = 10; + p_win->starty = (LINES - p_win->height)/2; + p_win->startx = (COLS - p_win->width)/2; + + p_win->border.ls = '|'; + p_win->border.rs = '|'; + p_win->border.ts = '-'; + p_win->border.bs = '-'; + p_win->border.tl = '+'; + p_win->border.tr = '+'; + p_win->border.bl = '+'; + p_win->border.br = '+'; + +} +void print_win_params(WIN *p_win) +{ +#ifdef _DEBUG + mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, + p_win->width, p_win->height); + refresh(); +#endif +} +void create_box(WIN *p_win, bool flag) +{ int i, j; + int x, y, w, h; + + x = p_win->startx; + y = p_win->starty; + w = p_win->width; + h = p_win->height; + + if(flag == TRUE) + { mvaddch(y, x, p_win->border.tl); + mvaddch(y, x + w, p_win->border.tr); + mvaddch(y + h, x, p_win->border.bl); + mvaddch(y + h, x + w, p_win->border.br); + mvhline(y, x + 1, p_win->border.ts, w - 1); + mvhline(y + h, x + 1, p_win->border.bs, w - 1); + mvvline(y + 1, x, p_win->border.ls, h - 1); + mvvline(y + 1, x + w, p_win->border.rs, h - 1); + + } + else + for(j = y; j <= y + h; ++j) + for(i = x; i <= x + w; ++i) + mvaddch(j, i, ' '); + + refresh(); + +} diff --git a/ncurses_programs/basics/printw_example.c b/ncurses_programs/basics/printw_example.c new file mode 100644 index 0000000..ae1345e --- /dev/null +++ b/ncurses_programs/basics/printw_example.c @@ -0,0 +1,20 @@ +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Just a string"; /* message to be appeared on the screen */ + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); + printw("Try resizing your window(if possible) and then run this program again"); + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/ncurses_programs/basics/scanw_example.c b/ncurses_programs/basics/scanw_example.c new file mode 100644 index 0000000..cc0bf7b --- /dev/null +++ b/ncurses_programs/basics/scanw_example.c @@ -0,0 +1,20 @@ +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Enter a string: "; /* message to be appeared on the screen */ + char str[80]; + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + getstr(str); + mvprintw(LINES - 2, 0, "You Entered: %s", str); + getch(); + endwin(); + + return 0; +} diff --git a/ncurses_programs/basics/simple_attr.c b/ncurses_programs/basics/simple_attr.c new file mode 100644 index 0000000..3efef73 --- /dev/null +++ b/ncurses_programs/basics/simple_attr.c @@ -0,0 +1,54 @@ +/* pager functionality by Joseph Spainhour" */ +#include +#include + +int main(int argc, char *argv[]) +{ + int ch, prev, row, col; + prev = EOF; + FILE *fp; + int y, x; + + if(argc != 2) + { + printf("Usage: %s \n", argv[0]); + exit(1); + } + fp = fopen(argv[1], "r"); + if(fp == NULL) + { + perror("Cannot open input file"); + exit(1); + } + initscr(); /* Start curses mode */ + getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ + while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ + { + getyx(stdscr, y, x); /* get the current curser position */ + if(y == (row - 1)) /* are we are at the end of the screen */ + { + printw("<-Press Any Key->"); /* tell the user to press a key */ + getch(); + clear(); /* clear the screen */ + move(0, 0); /* start at the beginning of the screen */ + } + if(prev == '/' && ch == '*') /* If it is / and * then only + * switch bold on */ + { + attron(A_BOLD); /* cut bold on */ + getyx(stdscr, y, x); /* get the current curser position */ + move(y, x - 1); /* back up one space */ + printw("%c%c", '/', ch); /* The actual printing is done here */ + } + else + printw("%c", ch); + refresh(); + if(prev == '*' && ch == '/') + attroff(A_BOLD); /* Switch it off once we got * + * and then / */ + prev = ch; + } + endwin(); /* End curses mode */ + fclose(fp); + return 0; +} diff --git a/ncurses_programs/basics/simple_color.c b/ncurses_programs/basics/simple_color.c new file mode 100644 index 0000000..59fb020 --- /dev/null +++ b/ncurses_programs/basics/simple_color.c @@ -0,0 +1,40 @@ +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + if(has_colors() == FALSE) + { endwin(); + printf("Your terminal does not support color\n"); + exit(1); + } + start_color(); /* Start color */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + attron(COLOR_PAIR(1)); + print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); + attroff(COLOR_PAIR(1)); + getch(); + endwin(); +} +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} + diff --git a/ncurses_programs/basics/simple_key.c b/ncurses_programs/basics/simple_key.c new file mode 100644 index 0000000..5197646 --- /dev/null +++ b/ncurses_programs/basics/simple_key.c @@ -0,0 +1,92 @@ +#include +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; +int n_choices = sizeof(choices) / sizeof(char *); +void print_menu(WINDOW *menu_win, int highlight); + +int main() +{ WINDOW *menu_win; + int highlight = 1; + int choice = 0; + int c; + + initscr(); + clear(); + noecho(); + cbreak(); /* Line buffering disabled. pass on everything */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + keypad(menu_win, TRUE); + mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); + refresh(); + print_menu(menu_win, highlight); + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_UP: + if(highlight == 1) + highlight = n_choices; + else + --highlight; + break; + case KEY_DOWN: + if(highlight == n_choices) + highlight = 1; + else + ++highlight; + break; + case 10: + choice = highlight; + break; + default: + mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); + refresh(); + break; + } + print_menu(menu_win, highlight); + if(choice != 0) /* User did a choice come out of the infinite loop */ + break; + } + mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); + clrtoeol(); + refresh(); + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) /* High light the present choice */ + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + diff --git a/ncurses_programs/basics/temp_leave.c b/ncurses_programs/basics/temp_leave.c new file mode 100644 index 0000000..fdcd5c5 --- /dev/null +++ b/ncurses_programs/basics/temp_leave.c @@ -0,0 +1,20 @@ +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!\n"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + def_prog_mode(); /* Save the tty modes */ + endwin(); /* End curses mode temporarily */ + system("/bin/sh"); /* Do whatever you like in cooked mode */ + reset_prog_mode(); /* Return to the previous tty mode*/ + /* stored by def_prog_mode() */ + refresh(); /* Do refresh() to restore the */ + /* Screen contents */ + printw("Another String\n"); /* Back to curses use the full */ + refresh(); /* capabilities of curses */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/ncurses_programs/basics/win_border.c b/ncurses_programs/basics/win_border.c new file mode 100644 index 0000000..41e1a7e --- /dev/null +++ b/ncurses_programs/basics/win_border.c @@ -0,0 +1,82 @@ +#include + + +WINDOW *create_newwin(int height, int width, int starty, int startx); +void destroy_win(WINDOW *local_win); + +int main(int argc, char *argv[]) +{ WINDOW *my_win; + int startx, starty, width, height; + int ch; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + + height = 3; + width = 10; + starty = (LINES - height) / 2; /* Calculating for a center placement */ + startx = (COLS - width) / 2; /* of the window */ + printw("Press F1 to exit"); + refresh(); + my_win = create_newwin(height, width, starty, startx); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,--startx); + break; + case KEY_RIGHT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,++startx); + break; + case KEY_UP: + destroy_win(my_win); + my_win = create_newwin(height, width, --starty,startx); + break; + case KEY_DOWN: + destroy_win(my_win); + my_win = create_newwin(height, width, ++starty,startx); + break; + } + } + + endwin(); /* End curses mode */ + return 0; +} + +WINDOW *create_newwin(int height, int width, int starty, int startx) +{ WINDOW *local_win; + + local_win = newwin(height, width, starty, startx); + box(local_win, 0 , 0); /* 0, 0 gives default characters + * for the vertical and horizontal + * lines */ + wrefresh(local_win); /* Show that box */ + + return local_win; +} + +void destroy_win(WINDOW *local_win) +{ + /* box(local_win, ' ', ' '); : This won't produce the desired + * result of erasing the window. It will leave it's four corners + * and so an ugly remnant of window. + */ + wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); + /* The parameters taken are + * 1. win: the window on which to operate + * 2. ls: character to be used for the left side of the window + * 3. rs: character to be used for the right side of the window + * 4. ts: character to be used for the top side of the window + * 5. bs: character to be used for the bottom side of the window + * 6. tl: character to be used for the top left corner of the window + * 7. tr: character to be used for the top right corner of the window + * 8. bl: character to be used for the bottom left corner of the window + * 9. br: character to be used for the bottom right corner of the window + */ + wrefresh(local_win); + delwin(local_win); +} diff --git a/ncurses_programs/basics/with_chgat.c b/ncurses_programs/basics/with_chgat.c new file mode 100644 index 0000000..4255561 --- /dev/null +++ b/ncurses_programs/basics/with_chgat.c @@ -0,0 +1,24 @@ +#include + +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + start_color(); /* Start color functionality */ + + init_pair(1, COLOR_CYAN, COLOR_BLACK); + printw("A Big string which i didn't care to type fully "); + mvchgat(0, 0, -1, A_BLINK, 1, NULL); + /* + * First two parameters specify the position at which to start + * Third parameter number of characters to update. -1 means till + * end of line + * Forth parameter is the normal attribute you wanted to give + * to the charcter + * Fifth is the color index. It is the index given during init_pair() + * use 0 if you didn't want color + * Sixth one is always NULL + */ + refresh(); + getch(); + endwin(); /* End curses mode */ + return 0; +} diff --git a/ncurses_programs/forms/Makefile b/ncurses_programs/forms/Makefile new file mode 100644 index 0000000..4ed1b98 --- /dev/null +++ b/ncurses_programs/forms/Makefile @@ -0,0 +1,27 @@ +# Makefile for JustForFun Files + +# A few variables + +CC=gcc +LIBS=-lform -lncurses + +SRC_DIR=. +EXE_DIR=../demo/exe + +EXES = \ + ${EXE_DIR}/form_attrib\ + ${EXE_DIR}/form_options\ + ${EXE_DIR}/form_simple\ + ${EXE_DIR}/form_win \ + +${EXE_DIR}/%: %.o + ${CC} -o $@ $< ${LIBS} + +%.o: ${SRC_DIR}/%.c + ${CC} -o $@ -c $< + +all: ${EXES} + + +clean: + @rm -f ${EXES} diff --git a/ncurses_programs/forms/README b/ncurses_programs/forms/README new file mode 100644 index 0000000..6e9a218 --- /dev/null +++ b/ncurses_programs/forms/README @@ -0,0 +1,9 @@ +Description of files +-------------------- + forms + | + |----> form_attrib.c -- Usage of field attributes + |----> form_options.c -- Usage of field options + |----> form_simple.c -- A simple form example + |----> form_win.c -- Demo of windows associated with forms + diff --git a/ncurses_programs/forms/form_attrib.c b/ncurses_programs/forms/form_attrib.c new file mode 100644 index 0000000..9974157 --- /dev/null +++ b/ncurses_programs/forms/form_attrib.c @@ -0,0 +1,75 @@ +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_WHITE, COLOR_BLUE); + init_pair(2, COLOR_WHITE, COLOR_BLUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ + set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ + /* are printed in white */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_current_field(my_form, field[0]); /* Set focus to the colored field */ + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/ncurses_programs/forms/form_options.c b/ncurses_programs/forms/form_options.c new file mode 100644 index 0000000..5b67dcc --- /dev/null +++ b/ncurses_programs/forms/form_options.c @@ -0,0 +1,76 @@ +#include + +#define STARTX 15 +#define STARTY 4 +#define WIDTH 25 + +#define N_FIELDS 3 + +int main() +{ FIELD *field[N_FIELDS]; + FORM *my_form; + int ch, i; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + for(i = 0; i < N_FIELDS - 1; ++i) + field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); + field[N_FIELDS - 1] = NULL; + + /* Set field options */ + set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ + + field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ + field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ + field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ + /* after last character is entered */ + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ + set_field_buffer(field[0], 0, "This is a static Field"); + /* Initialize the field */ + mvprintw(STARTY, STARTX - 10, "Field 1:"); + mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/ncurses_programs/forms/form_simple.c b/ncurses_programs/forms/form_simple.c new file mode 100644 index 0000000..520fc92 --- /dev/null +++ b/ncurses_programs/forms/form_simple.c @@ -0,0 +1,66 @@ +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/ncurses_programs/forms/form_win.c b/ncurses_programs/forms/form_win.c new file mode 100644 index 0000000..fb00a46 --- /dev/null +++ b/ncurses_programs/forms/form_win.c @@ -0,0 +1,112 @@ +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ + FIELD *field[3]; + FORM *my_form; + WINDOW *my_form_win; + int ch, rows, cols; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 6, 1, 0, 0); + field[1] = new_field(1, 10, 8, 1, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + + /* Calculate the area required for the form */ + scale_form(my_form, &rows, &cols); + + /* Create the window to be associated with the form */ + my_form_win = newwin(rows + 4, cols + 4, 4, 4); + keypad(my_form_win, TRUE); + + /* Set main window and sub window */ + set_form_win(my_form, my_form_win); + set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); + + /* Print a border around the main window and print a title */ + box(my_form_win, 0, 0); + print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); + + post_form(my_form); + wrefresh(my_form_win); + + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = wgetch(my_form_win)) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/ncurses_programs/menus/Makefile b/ncurses_programs/menus/Makefile new file mode 100644 index 0000000..73626e7 --- /dev/null +++ b/ncurses_programs/menus/Makefile @@ -0,0 +1,31 @@ +# Makefile for JustForFun Files + +# A few variables + +CC=gcc +LIBS=-lmenu -lncurses + +SRC_DIR=. +EXE_DIR=../demo/exe + +EXES = \ + ${EXE_DIR}/menu_attrib\ + ${EXE_DIR}/menu_item_data\ + ${EXE_DIR}/menu_multi_column \ + ${EXE_DIR}/menu_scroll \ + ${EXE_DIR}/menu_simple \ + ${EXE_DIR}/menu_toggle \ + ${EXE_DIR}/menu_userptr \ + ${EXE_DIR}/menu_win + +${EXE_DIR}/%: %.o + ${CC} -o $@ $< ${LIBS} + +%.o: ${SRC_DIR}/%.c + ${CC} -o $@ -c $< + +all: ${EXES} + + +clean: + @rm -f ${EXES} diff --git a/ncurses_programs/menus/README b/ncurses_programs/menus/README new file mode 100644 index 0000000..211bfa3 --- /dev/null +++ b/ncurses_programs/menus/README @@ -0,0 +1,13 @@ +Description of files +-------------------- + menus + | + |----> menu_attrib.c -- Usage of menu attributes + |----> menu_item_data.c -- Usage of item_name() etc.. functions + |----> menu_multi_column.c -- Creates multi columnar menus + |----> menu_scroll.c -- Demonstrates scrolling capability of menus + |----> menu_simple.c -- A simple menu accessed by arrow keys + |----> menu_toggle.c -- Creates multi valued menus and explains + | -- REQ_TOGGLE_ITEM + |----> menu_userptr.c -- Usage of user pointer + |----> menu_win.c -- Demo of windows associated with menus diff --git a/ncurses_programs/menus/menu_attrib.c b/ncurses_programs/menus/menu_attrib.c new file mode 100644 index 0000000..fdfdd2c --- /dev/null +++ b/ncurses_programs/menus/menu_attrib.c @@ -0,0 +1,80 @@ +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + item_opts_off(my_items[3], O_SELECTABLE); + item_opts_off(my_items[6], O_SELECTABLE); + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set fore ground and back ground of the menu */ + set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); + set_menu_back(my_menu, COLOR_PAIR(2)); + set_menu_grey(my_menu, COLOR_PAIR(3)); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", + item_name(current_item(my_menu))); + pos_menu_cursor(my_menu); + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + diff --git a/ncurses_programs/menus/menu_item_data.c b/ncurses_programs/menus/menu_item_data.c new file mode 100644 index 0000000..c51ebc1 --- /dev/null +++ b/ncurses_programs/menus/menu_item_data.c @@ -0,0 +1,66 @@ +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + cur_item = current_item(my_menu); + move(LINES - 2, 0); + clrtoeol(); + mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", + item_index(cur_item) + 1, item_name(cur_item), + item_description(cur_item)); + + refresh(); + pos_menu_cursor(my_menu); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/ncurses_programs/menus/menu_multi_column.c b/ncurses_programs/menus/menu_multi_column.c new file mode 100644 index 0000000..d507610 --- /dev/null +++ b/ncurses_programs/menus/menu_multi_column.c @@ -0,0 +1,97 @@ +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", + "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", + "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", + "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", + "Exit", + (char *)NULL, + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set menu option not to show the description */ + menu_opts_off(my_menu, O_SHOWDESC); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 70, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); + set_menu_format(my_menu, 5, 3); + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); + mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_LEFT: + menu_driver(my_menu, REQ_LEFT_ITEM); + break; + case KEY_RIGHT: + menu_driver(my_menu, REQ_RIGHT_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} diff --git a/ncurses_programs/menus/menu_scroll.c b/ncurses_programs/menus/menu_scroll.c new file mode 100644 index 0000000..2d9c627 --- /dev/null +++ b/ncurses_programs/menus/menu_scroll.c @@ -0,0 +1,124 @@ +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Choice 8", + "Choice 9", + "Choice 10", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + set_menu_format(my_menu, 5, 1); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); + mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/ncurses_programs/menus/menu_simple.c b/ncurses_programs/menus/menu_simple.c new file mode 100644 index 0000000..1222c7d --- /dev/null +++ b/ncurses_programs/menus/menu_simple.c @@ -0,0 +1,56 @@ +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + mvprintw(LINES - 2, 0, "F1 to Exit"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/ncurses_programs/menus/menu_toggle.c b/ncurses_programs/menus/menu_toggle.c new file mode 100644 index 0000000..f6e8d7e --- /dev/null +++ b/ncurses_programs/menus/menu_toggle.c @@ -0,0 +1,84 @@ +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + + /* Make the menu multi valued */ + menu_opts_off(my_menu, O_ONEVALUE); + + mvprintw(LINES - 3, 0, "Use to select or unselect an item."); + mvprintw(LINES - 2, 0, " to see presently selected items(F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case ' ': + menu_driver(my_menu, REQ_TOGGLE_ITEM); + break; + case 10: /* Enter */ + { char temp[200]; + ITEM **items; + + items = menu_items(my_menu); + temp[0] = '\0'; + for(i = 0; i < item_count(my_menu); ++i) + if(item_value(items[i]) == TRUE) + { strcat(temp, item_name(items[i])); + strcat(temp, " "); + } + move(20, 0); + clrtoeol(); + mvprintw(20, 0, temp); + refresh(); + } + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/ncurses_programs/menus/menu_userptr.c b/ncurses_programs/menus/menu_userptr.c new file mode 100644 index 0000000..baf6a6c --- /dev/null +++ b/ncurses_programs/menus/menu_userptr.c @@ -0,0 +1,87 @@ +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; +void func(char *name); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + { my_items[i] = new_item(choices[i], choices[i]); + /* Set the user pointer */ + set_item_userptr(my_items[i], func); + } + my_items[n_choices] = (ITEM *)NULL; + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + { ITEM *cur; + void (*p)(char *); + + cur = current_item(my_menu); + p = item_userptr(cur); + p((char *)item_name(cur)); + pos_menu_cursor(my_menu); + break; + } + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + +void func(char *name) +{ move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", name); +} diff --git a/ncurses_programs/menus/menu_win.c b/ncurses_programs/menus/menu_win.c new file mode 100644 index 0000000..3514086 --- /dev/null +++ b/ncurses_programs/menus/menu_win.c @@ -0,0 +1,105 @@ +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + mvprintw(LINES - 2, 0, "F1 to exit"); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/ncurses_programs/panels/Makefile b/ncurses_programs/panels/Makefile new file mode 100644 index 0000000..242df7c --- /dev/null +++ b/ncurses_programs/panels/Makefile @@ -0,0 +1,27 @@ +# Makefile for JustForFun Files + +# A few variables + +CC=gcc +LIBS=-lpanel -lncurses + +SRC_DIR=. +EXE_DIR=../demo/exe + +EXES = \ + ${EXE_DIR}/panel_browse \ + ${EXE_DIR}/panel_hide \ + ${EXE_DIR}/panel_resize \ + ${EXE_DIR}/panel_simple + +${EXE_DIR}/%: %.o + ${CC} -o $@ $< ${LIBS} + +%.o: ${SRC_DIR}/%.c + ${CC} -o $@ -c $< + +all: ${EXES} + + +clean: + @rm -f ${EXES} diff --git a/ncurses_programs/panels/README b/ncurses_programs/panels/README new file mode 100644 index 0000000..ed34678 --- /dev/null +++ b/ncurses_programs/panels/README @@ -0,0 +1,8 @@ +Description of files +-------------------- + panels + | + |----> panel_browse.c -- Panel browsing through tab. Usage of user pointer + |----> panel_hide.c -- Hiding and Un hiding of panels + |----> panel_resize.c -- Moving and resizing of panels + |----> panel_simple.c -- A simple panel example diff --git a/ncurses_programs/panels/panel_browse.c b/ncurses_programs/panels/panel_browse.c new file mode 100644 index 0000000..45fd839 --- /dev/null +++ b/ncurses_programs/panels/panel_browse.c @@ -0,0 +1,117 @@ +#include + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL *top; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Set up the user pointers to the next panel */ + set_panel_userptr(my_panels[0], my_panels[1]); + set_panel_userptr(my_panels[1], my_panels[2]); + set_panel_userptr(my_panels[2], my_panels[0]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + top = my_panels[2]; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: + top = (PANEL *)panel_userptr(top); + top_panel(top); + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/ncurses_programs/panels/panel_hide.c b/ncurses_programs/panels/panel_hide.c new file mode 100644 index 0000000..40f6c94 --- /dev/null +++ b/ncurses_programs/panels/panel_hide.c @@ -0,0 +1,156 @@ +#include + +typedef struct _PANEL_DATA { + int hide; /* TRUE if panel is hidden */ +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA panel_datas[3]; + PANEL_DATA *temp; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Initialize panel datas saying that nothing is hidden */ + panel_datas[0].hide = FALSE; + panel_datas[1].hide = FALSE; + panel_datas[2].hide = FALSE; + + set_panel_userptr(my_panels[0], &panel_datas[0]); + set_panel_userptr(my_panels[1], &panel_datas[1]); + set_panel_userptr(my_panels[2], &panel_datas[2]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); + mvprintw(LINES - 2, 0, "F1 to Exit"); + + attroff(COLOR_PAIR(4)); + doupdate(); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 'a': + temp = (PANEL_DATA *)panel_userptr(my_panels[0]); + if(temp->hide == FALSE) + { hide_panel(my_panels[0]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[0]); + temp->hide = FALSE; + } + break; + case 'b': + temp = (PANEL_DATA *)panel_userptr(my_panels[1]); + if(temp->hide == FALSE) + { hide_panel(my_panels[1]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[1]); + temp->hide = FALSE; + } + break; + case 'c': + temp = (PANEL_DATA *)panel_userptr(my_panels[2]); + if(temp->hide == FALSE) + { hide_panel(my_panels[2]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[2]); + temp->hide = FALSE; + } + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/ncurses_programs/panels/panel_resize.c b/ncurses_programs/panels/panel_resize.c new file mode 100644 index 0000000..71d331f --- /dev/null +++ b/ncurses_programs/panels/panel_resize.c @@ -0,0 +1,234 @@ +#include + +typedef struct _PANEL_DATA { + int x, y, w, h; + char label[80]; + int label_color; + PANEL *next; +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); +void set_user_ptrs(PANEL **panels, int n); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA *top; + PANEL *stack_top; + WINDOW *temp_win, *old_win; + int ch; + int newx, newy, neww, newh; + int size = FALSE, move = FALSE; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + set_user_ptrs(my_panels, 3); + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + stack_top = my_panels[2]; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: /* Tab */ + top = (PANEL_DATA *)panel_userptr(stack_top); + top_panel(top->next); + stack_top = top->next; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + break; + case 'r': /* Re-Size*/ + size = TRUE; + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press to end resizing"); + refresh(); + attroff(COLOR_PAIR(4)); + break; + case 'm': /* Move */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press to end moving"); + refresh(); + attroff(COLOR_PAIR(4)); + move = TRUE; + break; + case KEY_LEFT: + if(size == TRUE) + { --newx; + ++neww; + } + if(move == TRUE) + --newx; + break; + case KEY_RIGHT: + if(size == TRUE) + { ++newx; + --neww; + } + if(move == TRUE) + ++newx; + break; + case KEY_UP: + if(size == TRUE) + { --newy; + ++newh; + } + if(move == TRUE) + --newy; + break; + case KEY_DOWN: + if(size == TRUE) + { ++newy; + --newh; + } + if(move == TRUE) + ++newy; + break; + case 10: /* Enter */ + move(LINES - 4, 0); + clrtoeol(); + refresh(); + if(size == TRUE) + { old_win = panel_window(stack_top); + temp_win = newwin(newh, neww, newy, newx); + replace_panel(stack_top, temp_win); + win_show(temp_win, top->label, top->label_color); + delwin(old_win); + size = FALSE; + } + if(move == TRUE) + { move_panel(stack_top, newy, newx); + move = FALSE; + } + break; + + } + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + refresh(); + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Set the PANEL_DATA structures for individual panels */ +void set_user_ptrs(PANEL **panels, int n) +{ PANEL_DATA *ptrs; + WINDOW *win; + int x, y, w, h, i; + char temp[80]; + + ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); + + for(i = 0;i < n; ++i) + { win = panel_window(panels[i]); + getbegyx(win, y, x); + getmaxyx(win, h, w); + ptrs[i].x = x; + ptrs[i].y = y; + ptrs[i].w = w; + ptrs[i].h = h; + sprintf(temp, "Window Number %d", i + 1); + strcpy(ptrs[i].label, temp); + ptrs[i].label_color = i + 1; + if(i + 1 == n) + ptrs[i].next = panels[0]; + else + ptrs[i].next = panels[i + 1]; + set_panel_userptr(panels[i], &ptrs[i]); + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/ncurses_programs/panels/panel_simple.c b/ncurses_programs/panels/panel_simple.c new file mode 100644 index 0000000..d98aeef --- /dev/null +++ b/ncurses_programs/panels/panel_simple.c @@ -0,0 +1,38 @@ +#include + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + int lines = 10, cols = 40, y = 2, x = 4, i; + + initscr(); + cbreak(); + noecho(); + + /* Create windows for the panels */ + my_wins[0] = newwin(lines, cols, y, x); + my_wins[1] = newwin(lines, cols, y + 1, x + 5); + my_wins[2] = newwin(lines, cols, y + 2, x + 10); + + /* + * Create borders around the windows so that you can see the effect + * of panels + */ + for(i = 0; i < 3; ++i) + box(my_wins[i], 0, 0); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + doupdate(); + + getch(); + endwin(); +} + diff --git a/ncurses_programs/perl/01.pl b/ncurses_programs/perl/01.pl new file mode 100755 index 0000000..a4592d7 --- /dev/null +++ b/ncurses_programs/perl/01.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +use Curses; + +initscr(); +printw("Hello world!"); +refresh(); +getch(); +endwin(); + diff --git a/ncurses_programs/perl/02.pl b/ncurses_programs/perl/02.pl new file mode 100755 index 0000000..9862d7e --- /dev/null +++ b/ncurses_programs/perl/02.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +use Curses; + +initscr(); +raw(); +keypad(1); +noecho(); + +printw("Type any character to see it in bold\n"); +$ch = getch(); + +if ($ch == KEY_F(1)) { + printw("F1 Key pressed"); +} +else { + printw("The pressed key is "); + attron(A_BOLD); + printw($ch); + attroff(A_BOLD); +} + +refresh(); +getch(); +endwin(); + diff --git a/ncurses_programs/perl/03.pl b/ncurses_programs/perl/03.pl new file mode 100755 index 0000000..5ef6c89 --- /dev/null +++ b/ncurses_programs/perl/03.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +# We use addstr() instead of printw() + +use Curses; + +$mesg = "Just a string"; + +initscr(); +getmaxyx($row, $col); +addstr($row / 2, ($col - length($mesg)) / 2, $mesg); +addstr($row - 2, 0, "This screen has $row rows and $col columns\n"); +refresh(); +getch(); +endwin(); + diff --git a/ncurses_programs/perl/04.pl b/ncurses_programs/perl/04.pl new file mode 100755 index 0000000..91cde01 --- /dev/null +++ b/ncurses_programs/perl/04.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +# We use addstr() instead of printw() + +use Curses; + +$mesg = "Enter a string: "; + +initscr(); +getmaxyx($row, $col); +addstr($row / 2, ($col - length($mesg)) / 2, $mesg); +getstr($str); +addstr($LINES - 2, 0, "You Entered: $str"); +getch(); +endwin(); + diff --git a/ncurses_programs/perl/05.pl b/ncurses_programs/perl/05.pl new file mode 100755 index 0000000..dc61845 --- /dev/null +++ b/ncurses_programs/perl/05.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +# We first read all the input into an array and join +# it to a single string with newlines. + +use Curses; + +initscr(); + +@lines = <>; +$lines = join "", @lines; + +while ($lines =~ /\G(.*?)(\/\*.*?\*\/)?/gs) { + addstr($1); + if ($2) { + attron(A_BOLD); + addstr($2); + attroff(A_BOLD); + } +} + +refresh(); +getch(); +endwin(); + diff --git a/ncurses_programs/perl/06.pl b/ncurses_programs/perl/06.pl new file mode 100755 index 0000000..ac949c6 --- /dev/null +++ b/ncurses_programs/perl/06.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +use Curses; + +initscr(); +start_color(); + +init_pair(1, COLOR_CYAN, COLOR_BLACK); +printw("A Big string which i didn't care to type fully "); +chgat(0, 0, -1, A_BLINK, 1, NULL); + +refresh(); +getch(); +endwin(); + diff --git a/ncurses_programs/perl/07.pl b/ncurses_programs/perl/07.pl new file mode 100755 index 0000000..06a7549 --- /dev/null +++ b/ncurses_programs/perl/07.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +use Curses; + +initscr(); +cbreak(); +keypad(1); + +$height = 3; +$width = 10; +$starty = ($LINES - $height) / 2; +$startx = ($COLS - $width) / 2; +printw("Press F1 to exit"); +refresh(); +$my_win = create_newwin($height, $width, $starty, $startx); + +while (($ch = getch()) != KEY_F(1)) { + if ($ch == KEY_LEFT) { + destroy_win($my_win); + $my_win = create_newwin($height, $width, $starty, --$startx); + } + elsif ($ch == KEY_RIGHT) { + destroy_win($my_win); + $my_win = create_newwin($height, $width, $starty, ++$startx); + } + elsif ($ch == KEY_UP) { + destroy_win($my_win); + $my_win = create_newwin($height, $width, --$starty, $startx); + } + elsif ($ch == KEY_DOWN) { + destroy_win($my_win); + $my_win = create_newwin($height, $width, ++$starty, $startx); + } +} + +endwin(); + +sub create_newwin { + $local_win = newwin(shift, shift, shift, shift); + box($local_win, 0, 0); + refresh($local_win); + return $local_win; +} + +sub destroy_win { + $local_win = shift; + border($local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); + refresh($local_win); + delwin($local_win); +} + diff --git a/ncurses_programs/perl/08.pl b/ncurses_programs/perl/08.pl new file mode 100755 index 0000000..4eb044f --- /dev/null +++ b/ncurses_programs/perl/08.pl @@ -0,0 +1,112 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +# We use %win hash to store window parameters + +use Curses; + +initscr(); +start_color(); +cbreak(); + +keypad(1); +noecho(); +init_pair(1, COLOR_CYAN, COLOR_BLACK); + +%win = (); +init_win_params(\%win); +print_win_params(\%win); + +attron(COLOR_PAIR(1)); +printw("Press F1 to exit"); +refresh(); +attroff(COLOR_PAIR(1)); + +create_box(\%win, 1); + +while (($ch = getch()) != KEY_F(1)) { + if ($ch == KEY_LEFT) { + create_box(\%win, 0); + $win{'startx'}--; + create_box(\%win, 1); + } + elsif ($ch == KEY_RIGHT) { + create_box(\%win, 0); + $win{'startx'}++; + create_box(\%win, 1); + } + elsif ($ch == KEY_UP) { + create_box(\%win, 0); + $win{'starty'}--; + create_box(\%win, 1); + } + elsif ($ch == KEY_DOWN) { + create_box(\%win, 0); + $win{'starty'}++; + create_box(\%win, 1); + } +} + +endwin(); + +sub init_win_params { + $p_win = shift; + + $$p_win{'height'} = 3; + $$p_win{'width'} = 10; + $$p_win{'starty'} = ($LINES - $p_win{'height'}) / 2; + $$p_win{'startx'} = ($COLS - $p_win{'width'}) / 2; + $$p_win{'ls'} = '|'; + $$p_win{'rs'} = '|'; + $$p_win{'ts'} = '-'; + $$p_win{'bs'} = '-'; + $$p_win{'tl'} = '+'; + $$p_win{'tr'} = '+'; + $$p_win{'bl'} = '+'; + $$p_win{'br'} = '+'; +} + +sub print_win_params { + if ($_DEBUG) { + $p_win = shift; + addstr(25, 0, + "$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}"); + refresh(); + } +} + +sub create_box { + $p_win = shift; + $bool = shift; + + $x = $$p_win{'startx'}; + $y = $$p_win{'starty'}; + $w = $$p_win{'width'}; + $h = $$p_win{'height'}; + + if ($bool) { + addch($y, $x, $$p_win{'tl'}); + addch($y, $x + $w, $$p_win{'tr'}); + addch($y + $h, $x, $$p_win{'bl'}); + addch($y + $h, $x + $w, $$p_win{'br'}); + hline($y, $x + 1, $$p_win{'ts'}, $w - 1); + hline($y + $h, $x + 1, $$p_win{'bs'}, $w - 1); + vline($y + 1, $x, $$p_win{'ls'}, $h - 1); + vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1); + } + else { + for ($j = $y; $j <= $y + $h; $j++) { + for ($i = $x; $i <= $x + $w; $i++) { + addch($j, $i, ' '); + } + } + } + refresh(); +} + diff --git a/ncurses_programs/perl/09.pl b/ncurses_programs/perl/09.pl new file mode 100755 index 0000000..235fc82 --- /dev/null +++ b/ncurses_programs/perl/09.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +use Curses; + +initscr(); +unless (has_colors()) { + endwin(); + print "Your terminal does not support color\n"; + exit 1; +} +start_color(); +init_pair(1, COLOR_RED, COLOR_BLACK); + +attron(COLOR_PAIR(1)); +print_in_middle(stdscr, $LINES / 2, 0, 0, "Viola !!! In color ..."); +attroff(COLOR_PAIR(1)); +getch(); +endwin(); + +sub print_in_middle { + $win = shift; + $starty = shift; + $startx = shift; + $width = shift; + $string = shift; + + $win = stdscr unless ($win); + + getyx($win, $y, $x); + + $x = $startx if ($startx); + $y = $starty if ($starty); + $width = $COLS unless ($width); + $length = length($string); + $temp = ($width - $length) / 2; + $x = $startx + $temp; + addstr($y, $x, $string); + refresh(); +} + diff --git a/ncurses_programs/perl/10.pl b/ncurses_programs/perl/10.pl new file mode 100755 index 0000000..7353ddb --- /dev/null +++ b/ncurses_programs/perl/10.pl @@ -0,0 +1,98 @@ +#!/usr/bin/perl +# +# Copyright (C) 2003 by Virtusa Corporation +# http://www.virtusa.com +# +# Anuradha Ratnaweera +# http://www.linux.lk/~anuradha/ +# + +use Curses; + +$width = 30; +$height = 10; +$startx = 0; +$starty = 0; + +@choices = ( + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit" +); + +$n_choices = @choices; + +$highlight = 1; +$choice = 0; + +initscr(); +clear(); +noecho(); +cbreak(); +$startx = ($COLS - $width) / 2; +$starty = ($LINES - $height) / 2; + +$menu_win = newwin($height, $width, $starty, $startx); +keypad(1); +keypad($menu_win, 1); +addstr(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); +refresh(); +print_menu($menu_win, $highlight); + +while (1) { + $c = getch($menu_win); + if ($c == KEY_UP) { + if ($highlight == 1) { + $highlight = $n_choices; + } + else { + $highlight--; + } + } + elsif ($c == KEY_DOWN) { + if ($highlight == $n_choices) { + $highlight = 1; + } + else { + $highlight++; + } + } + elsif ($c == '\n') { + $choice = $highlight; + } + else { + addstr($LINES - 2, 0, "Character pressed is $c"); + refresh(); + } + print_menu($menu_win, $highlight); + last if ($choice); +} + +addstr($LINES - 2, 0, "You chose choice $choice with choice string $choices[$choice-1]"); +clrtoeol(); +refresh(); +endwin(); + +sub print_menu { + $menu_win = shift; + $highlight = shift; + + $x = 2; + $y = 2; + box($menu_win, 0, 0); + for ($i = 0; $i < $n_choices; $i++) { + if ($highlight == $i + 1) { + attron($menu_win, A_REVERSE); + addstr($menu_win, $y, $x, $choices[$i]); + attroff($menu_win, A_REVERSE); + } + else { + addstr($menu_win, $y, $x, $choices[$i]); + } + $y++; + } + refresh($menu_win); +} + diff --git a/ncurses_programs/perl/COPYING b/ncurses_programs/perl/COPYING new file mode 100644 index 0000000..23574ed --- /dev/null +++ b/ncurses_programs/perl/COPYING @@ -0,0 +1,2 @@ +These programmes can be distributed under the same terms as +ncurses (MIT style license). diff --git a/ncurses_programs/perl/README b/ncurses_programs/perl/README new file mode 100644 index 0000000..f283923 --- /dev/null +++ b/ncurses_programs/perl/README @@ -0,0 +1,6 @@ +The license was changed from GNU/GPL to MIT style license +which is of course compatible with GPL, in order to include +them in the official ncurses-HOWTO. + +Anuradha Ratnaweera (anuradha at gnu org OR gnu.slash.linux at +gmail com OR anuradha at linux lk). From 58609828241eb867c36dc64ae73cebacc334c07c Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:06:19 -0500 Subject: [PATCH 02/21] pulling everything up a level --- ncurses_programs/COPYING => COPYING | 0 {ncurses_programs/JustForFun => JustForFun}/Makefile | 0 {ncurses_programs/JustForFun => JustForFun}/README | 0 {ncurses_programs/JustForFun => JustForFun}/hanoi.c | 0 {ncurses_programs/JustForFun => JustForFun}/life.c | 0 {ncurses_programs/JustForFun => JustForFun}/magic.c | 0 {ncurses_programs/JustForFun => JustForFun}/queens.c | 0 {ncurses_programs/JustForFun => JustForFun}/shuffle.c | 0 {ncurses_programs/JustForFun => JustForFun}/tt.c | 0 ncurses_programs/Makefile => Makefile | 0 ncurses_programs/README => README | 0 {ncurses_programs/basics => basics}/Makefile | 0 {ncurses_programs/basics => basics}/README | 0 {ncurses_programs/basics => basics}/acs_vars.c | 0 {ncurses_programs/basics => basics}/hello_world.c | 0 {ncurses_programs/basics => basics}/init_func_example.c | 0 {ncurses_programs/basics => basics}/key_code.c | 0 {ncurses_programs/basics => basics}/mouse_menu.c | 0 {ncurses_programs/basics => basics}/other_border.c | 0 {ncurses_programs/basics => basics}/printw_example.c | 0 {ncurses_programs/basics => basics}/scanw_example.c | 0 {ncurses_programs/basics => basics}/simple_attr.c | 0 {ncurses_programs/basics => basics}/simple_color.c | 0 {ncurses_programs/basics => basics}/simple_key.c | 0 {ncurses_programs/basics => basics}/temp_leave.c | 0 {ncurses_programs/basics => basics}/win_border.c | 0 {ncurses_programs/basics => basics}/with_chgat.c | 0 demo/exe/.placeholder | 0 {ncurses_programs/forms => forms}/Makefile | 0 {ncurses_programs/forms => forms}/README | 0 {ncurses_programs/forms => forms}/form_attrib.c | 0 {ncurses_programs/forms => forms}/form_options.c | 0 {ncurses_programs/forms => forms}/form_simple.c | 0 {ncurses_programs/forms => forms}/form_win.c | 0 {ncurses_programs/menus => menus}/Makefile | 0 {ncurses_programs/menus => menus}/README | 0 {ncurses_programs/menus => menus}/menu_attrib.c | 0 {ncurses_programs/menus => menus}/menu_item_data.c | 0 {ncurses_programs/menus => menus}/menu_multi_column.c | 0 {ncurses_programs/menus => menus}/menu_scroll.c | 0 {ncurses_programs/menus => menus}/menu_simple.c | 0 {ncurses_programs/menus => menus}/menu_toggle.c | 0 {ncurses_programs/menus => menus}/menu_userptr.c | 0 {ncurses_programs/menus => menus}/menu_win.c | 0 {ncurses_programs/panels => panels}/Makefile | 0 {ncurses_programs/panels => panels}/README | 0 {ncurses_programs/panels => panels}/panel_browse.c | 0 {ncurses_programs/panels => panels}/panel_hide.c | 0 {ncurses_programs/panels => panels}/panel_resize.c | 0 {ncurses_programs/panels => panels}/panel_simple.c | 0 {ncurses_programs/perl => perl}/01.pl | 0 {ncurses_programs/perl => perl}/02.pl | 0 {ncurses_programs/perl => perl}/03.pl | 0 {ncurses_programs/perl => perl}/04.pl | 0 {ncurses_programs/perl => perl}/05.pl | 0 {ncurses_programs/perl => perl}/06.pl | 0 {ncurses_programs/perl => perl}/07.pl | 0 {ncurses_programs/perl => perl}/08.pl | 0 {ncurses_programs/perl => perl}/09.pl | 0 {ncurses_programs/perl => perl}/10.pl | 0 {ncurses_programs/perl => perl}/COPYING | 0 {ncurses_programs/perl => perl}/README | 0 62 files changed, 0 insertions(+), 0 deletions(-) rename ncurses_programs/COPYING => COPYING (100%) rename {ncurses_programs/JustForFun => JustForFun}/Makefile (100%) rename {ncurses_programs/JustForFun => JustForFun}/README (100%) rename {ncurses_programs/JustForFun => JustForFun}/hanoi.c (100%) rename {ncurses_programs/JustForFun => JustForFun}/life.c (100%) rename {ncurses_programs/JustForFun => JustForFun}/magic.c (100%) rename {ncurses_programs/JustForFun => JustForFun}/queens.c (100%) rename {ncurses_programs/JustForFun => JustForFun}/shuffle.c (100%) rename {ncurses_programs/JustForFun => JustForFun}/tt.c (100%) rename ncurses_programs/Makefile => Makefile (100%) rename ncurses_programs/README => README (100%) rename {ncurses_programs/basics => basics}/Makefile (100%) rename {ncurses_programs/basics => basics}/README (100%) rename {ncurses_programs/basics => basics}/acs_vars.c (100%) rename {ncurses_programs/basics => basics}/hello_world.c (100%) rename {ncurses_programs/basics => basics}/init_func_example.c (100%) rename {ncurses_programs/basics => basics}/key_code.c (100%) rename {ncurses_programs/basics => basics}/mouse_menu.c (100%) rename {ncurses_programs/basics => basics}/other_border.c (100%) rename {ncurses_programs/basics => basics}/printw_example.c (100%) rename {ncurses_programs/basics => basics}/scanw_example.c (100%) rename {ncurses_programs/basics => basics}/simple_attr.c (100%) rename {ncurses_programs/basics => basics}/simple_color.c (100%) rename {ncurses_programs/basics => basics}/simple_key.c (100%) rename {ncurses_programs/basics => basics}/temp_leave.c (100%) rename {ncurses_programs/basics => basics}/win_border.c (100%) rename {ncurses_programs/basics => basics}/with_chgat.c (100%) create mode 100644 demo/exe/.placeholder rename {ncurses_programs/forms => forms}/Makefile (100%) rename {ncurses_programs/forms => forms}/README (100%) rename {ncurses_programs/forms => forms}/form_attrib.c (100%) rename {ncurses_programs/forms => forms}/form_options.c (100%) rename {ncurses_programs/forms => forms}/form_simple.c (100%) rename {ncurses_programs/forms => forms}/form_win.c (100%) rename {ncurses_programs/menus => menus}/Makefile (100%) rename {ncurses_programs/menus => menus}/README (100%) rename {ncurses_programs/menus => menus}/menu_attrib.c (100%) rename {ncurses_programs/menus => menus}/menu_item_data.c (100%) rename {ncurses_programs/menus => menus}/menu_multi_column.c (100%) rename {ncurses_programs/menus => menus}/menu_scroll.c (100%) rename {ncurses_programs/menus => menus}/menu_simple.c (100%) rename {ncurses_programs/menus => menus}/menu_toggle.c (100%) rename {ncurses_programs/menus => menus}/menu_userptr.c (100%) rename {ncurses_programs/menus => menus}/menu_win.c (100%) rename {ncurses_programs/panels => panels}/Makefile (100%) rename {ncurses_programs/panels => panels}/README (100%) rename {ncurses_programs/panels => panels}/panel_browse.c (100%) rename {ncurses_programs/panels => panels}/panel_hide.c (100%) rename {ncurses_programs/panels => panels}/panel_resize.c (100%) rename {ncurses_programs/panels => panels}/panel_simple.c (100%) rename {ncurses_programs/perl => perl}/01.pl (100%) rename {ncurses_programs/perl => perl}/02.pl (100%) rename {ncurses_programs/perl => perl}/03.pl (100%) rename {ncurses_programs/perl => perl}/04.pl (100%) rename {ncurses_programs/perl => perl}/05.pl (100%) rename {ncurses_programs/perl => perl}/06.pl (100%) rename {ncurses_programs/perl => perl}/07.pl (100%) rename {ncurses_programs/perl => perl}/08.pl (100%) rename {ncurses_programs/perl => perl}/09.pl (100%) rename {ncurses_programs/perl => perl}/10.pl (100%) rename {ncurses_programs/perl => perl}/COPYING (100%) rename {ncurses_programs/perl => perl}/README (100%) diff --git a/ncurses_programs/COPYING b/COPYING similarity index 100% rename from ncurses_programs/COPYING rename to COPYING diff --git a/ncurses_programs/JustForFun/Makefile b/JustForFun/Makefile similarity index 100% rename from ncurses_programs/JustForFun/Makefile rename to JustForFun/Makefile diff --git a/ncurses_programs/JustForFun/README b/JustForFun/README similarity index 100% rename from ncurses_programs/JustForFun/README rename to JustForFun/README diff --git a/ncurses_programs/JustForFun/hanoi.c b/JustForFun/hanoi.c similarity index 100% rename from ncurses_programs/JustForFun/hanoi.c rename to JustForFun/hanoi.c diff --git a/ncurses_programs/JustForFun/life.c b/JustForFun/life.c similarity index 100% rename from ncurses_programs/JustForFun/life.c rename to JustForFun/life.c diff --git a/ncurses_programs/JustForFun/magic.c b/JustForFun/magic.c similarity index 100% rename from ncurses_programs/JustForFun/magic.c rename to JustForFun/magic.c diff --git a/ncurses_programs/JustForFun/queens.c b/JustForFun/queens.c similarity index 100% rename from ncurses_programs/JustForFun/queens.c rename to JustForFun/queens.c diff --git a/ncurses_programs/JustForFun/shuffle.c b/JustForFun/shuffle.c similarity index 100% rename from ncurses_programs/JustForFun/shuffle.c rename to JustForFun/shuffle.c diff --git a/ncurses_programs/JustForFun/tt.c b/JustForFun/tt.c similarity index 100% rename from ncurses_programs/JustForFun/tt.c rename to JustForFun/tt.c diff --git a/ncurses_programs/Makefile b/Makefile similarity index 100% rename from ncurses_programs/Makefile rename to Makefile diff --git a/ncurses_programs/README b/README similarity index 100% rename from ncurses_programs/README rename to README diff --git a/ncurses_programs/basics/Makefile b/basics/Makefile similarity index 100% rename from ncurses_programs/basics/Makefile rename to basics/Makefile diff --git a/ncurses_programs/basics/README b/basics/README similarity index 100% rename from ncurses_programs/basics/README rename to basics/README diff --git a/ncurses_programs/basics/acs_vars.c b/basics/acs_vars.c similarity index 100% rename from ncurses_programs/basics/acs_vars.c rename to basics/acs_vars.c diff --git a/ncurses_programs/basics/hello_world.c b/basics/hello_world.c similarity index 100% rename from ncurses_programs/basics/hello_world.c rename to basics/hello_world.c diff --git a/ncurses_programs/basics/init_func_example.c b/basics/init_func_example.c similarity index 100% rename from ncurses_programs/basics/init_func_example.c rename to basics/init_func_example.c diff --git a/ncurses_programs/basics/key_code.c b/basics/key_code.c similarity index 100% rename from ncurses_programs/basics/key_code.c rename to basics/key_code.c diff --git a/ncurses_programs/basics/mouse_menu.c b/basics/mouse_menu.c similarity index 100% rename from ncurses_programs/basics/mouse_menu.c rename to basics/mouse_menu.c diff --git a/ncurses_programs/basics/other_border.c b/basics/other_border.c similarity index 100% rename from ncurses_programs/basics/other_border.c rename to basics/other_border.c diff --git a/ncurses_programs/basics/printw_example.c b/basics/printw_example.c similarity index 100% rename from ncurses_programs/basics/printw_example.c rename to basics/printw_example.c diff --git a/ncurses_programs/basics/scanw_example.c b/basics/scanw_example.c similarity index 100% rename from ncurses_programs/basics/scanw_example.c rename to basics/scanw_example.c diff --git a/ncurses_programs/basics/simple_attr.c b/basics/simple_attr.c similarity index 100% rename from ncurses_programs/basics/simple_attr.c rename to basics/simple_attr.c diff --git a/ncurses_programs/basics/simple_color.c b/basics/simple_color.c similarity index 100% rename from ncurses_programs/basics/simple_color.c rename to basics/simple_color.c diff --git a/ncurses_programs/basics/simple_key.c b/basics/simple_key.c similarity index 100% rename from ncurses_programs/basics/simple_key.c rename to basics/simple_key.c diff --git a/ncurses_programs/basics/temp_leave.c b/basics/temp_leave.c similarity index 100% rename from ncurses_programs/basics/temp_leave.c rename to basics/temp_leave.c diff --git a/ncurses_programs/basics/win_border.c b/basics/win_border.c similarity index 100% rename from ncurses_programs/basics/win_border.c rename to basics/win_border.c diff --git a/ncurses_programs/basics/with_chgat.c b/basics/with_chgat.c similarity index 100% rename from ncurses_programs/basics/with_chgat.c rename to basics/with_chgat.c diff --git a/demo/exe/.placeholder b/demo/exe/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/ncurses_programs/forms/Makefile b/forms/Makefile similarity index 100% rename from ncurses_programs/forms/Makefile rename to forms/Makefile diff --git a/ncurses_programs/forms/README b/forms/README similarity index 100% rename from ncurses_programs/forms/README rename to forms/README diff --git a/ncurses_programs/forms/form_attrib.c b/forms/form_attrib.c similarity index 100% rename from ncurses_programs/forms/form_attrib.c rename to forms/form_attrib.c diff --git a/ncurses_programs/forms/form_options.c b/forms/form_options.c similarity index 100% rename from ncurses_programs/forms/form_options.c rename to forms/form_options.c diff --git a/ncurses_programs/forms/form_simple.c b/forms/form_simple.c similarity index 100% rename from ncurses_programs/forms/form_simple.c rename to forms/form_simple.c diff --git a/ncurses_programs/forms/form_win.c b/forms/form_win.c similarity index 100% rename from ncurses_programs/forms/form_win.c rename to forms/form_win.c diff --git a/ncurses_programs/menus/Makefile b/menus/Makefile similarity index 100% rename from ncurses_programs/menus/Makefile rename to menus/Makefile diff --git a/ncurses_programs/menus/README b/menus/README similarity index 100% rename from ncurses_programs/menus/README rename to menus/README diff --git a/ncurses_programs/menus/menu_attrib.c b/menus/menu_attrib.c similarity index 100% rename from ncurses_programs/menus/menu_attrib.c rename to menus/menu_attrib.c diff --git a/ncurses_programs/menus/menu_item_data.c b/menus/menu_item_data.c similarity index 100% rename from ncurses_programs/menus/menu_item_data.c rename to menus/menu_item_data.c diff --git a/ncurses_programs/menus/menu_multi_column.c b/menus/menu_multi_column.c similarity index 100% rename from ncurses_programs/menus/menu_multi_column.c rename to menus/menu_multi_column.c diff --git a/ncurses_programs/menus/menu_scroll.c b/menus/menu_scroll.c similarity index 100% rename from ncurses_programs/menus/menu_scroll.c rename to menus/menu_scroll.c diff --git a/ncurses_programs/menus/menu_simple.c b/menus/menu_simple.c similarity index 100% rename from ncurses_programs/menus/menu_simple.c rename to menus/menu_simple.c diff --git a/ncurses_programs/menus/menu_toggle.c b/menus/menu_toggle.c similarity index 100% rename from ncurses_programs/menus/menu_toggle.c rename to menus/menu_toggle.c diff --git a/ncurses_programs/menus/menu_userptr.c b/menus/menu_userptr.c similarity index 100% rename from ncurses_programs/menus/menu_userptr.c rename to menus/menu_userptr.c diff --git a/ncurses_programs/menus/menu_win.c b/menus/menu_win.c similarity index 100% rename from ncurses_programs/menus/menu_win.c rename to menus/menu_win.c diff --git a/ncurses_programs/panels/Makefile b/panels/Makefile similarity index 100% rename from ncurses_programs/panels/Makefile rename to panels/Makefile diff --git a/ncurses_programs/panels/README b/panels/README similarity index 100% rename from ncurses_programs/panels/README rename to panels/README diff --git a/ncurses_programs/panels/panel_browse.c b/panels/panel_browse.c similarity index 100% rename from ncurses_programs/panels/panel_browse.c rename to panels/panel_browse.c diff --git a/ncurses_programs/panels/panel_hide.c b/panels/panel_hide.c similarity index 100% rename from ncurses_programs/panels/panel_hide.c rename to panels/panel_hide.c diff --git a/ncurses_programs/panels/panel_resize.c b/panels/panel_resize.c similarity index 100% rename from ncurses_programs/panels/panel_resize.c rename to panels/panel_resize.c diff --git a/ncurses_programs/panels/panel_simple.c b/panels/panel_simple.c similarity index 100% rename from ncurses_programs/panels/panel_simple.c rename to panels/panel_simple.c diff --git a/ncurses_programs/perl/01.pl b/perl/01.pl similarity index 100% rename from ncurses_programs/perl/01.pl rename to perl/01.pl diff --git a/ncurses_programs/perl/02.pl b/perl/02.pl similarity index 100% rename from ncurses_programs/perl/02.pl rename to perl/02.pl diff --git a/ncurses_programs/perl/03.pl b/perl/03.pl similarity index 100% rename from ncurses_programs/perl/03.pl rename to perl/03.pl diff --git a/ncurses_programs/perl/04.pl b/perl/04.pl similarity index 100% rename from ncurses_programs/perl/04.pl rename to perl/04.pl diff --git a/ncurses_programs/perl/05.pl b/perl/05.pl similarity index 100% rename from ncurses_programs/perl/05.pl rename to perl/05.pl diff --git a/ncurses_programs/perl/06.pl b/perl/06.pl similarity index 100% rename from ncurses_programs/perl/06.pl rename to perl/06.pl diff --git a/ncurses_programs/perl/07.pl b/perl/07.pl similarity index 100% rename from ncurses_programs/perl/07.pl rename to perl/07.pl diff --git a/ncurses_programs/perl/08.pl b/perl/08.pl similarity index 100% rename from ncurses_programs/perl/08.pl rename to perl/08.pl diff --git a/ncurses_programs/perl/09.pl b/perl/09.pl similarity index 100% rename from ncurses_programs/perl/09.pl rename to perl/09.pl diff --git a/ncurses_programs/perl/10.pl b/perl/10.pl similarity index 100% rename from ncurses_programs/perl/10.pl rename to perl/10.pl diff --git a/ncurses_programs/perl/COPYING b/perl/COPYING similarity index 100% rename from ncurses_programs/perl/COPYING rename to perl/COPYING diff --git a/ncurses_programs/perl/README b/perl/README similarity index 100% rename from ncurses_programs/perl/README rename to perl/README From 0252c2ff4c39a0c8f421061dcbde60d022220e53 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:20:21 -0500 Subject: [PATCH 03/21] ignoring built executables --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2dc1559 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +demo/exe/* From f4c7e129df8cdab960a08987930cf85f0e5b88b5 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:36:45 -0500 Subject: [PATCH 04/21] doing some find+sed cleanup --- JustForFun/Makefile | 18 +- JustForFun/README | 14 +- JustForFun/hanoi.c | 356 ++++++++++++++-------------- JustForFun/life.c | 214 ++++++++--------- JustForFun/magic.c | 322 ++++++++++++------------- JustForFun/queens.c | 244 +++++++++---------- JustForFun/shuffle.c | 410 ++++++++++++++++---------------- JustForFun/tt.c | 446 +++++++++++++++++------------------ basics/Makefile | 30 +-- basics/README | 34 +-- basics/acs_vars.c | 88 +++---- basics/hello_world.c | 24 +- basics/init_func_example.c | 62 ++--- basics/key_code.c | 28 +-- basics/mouse_menu.c | 212 ++++++++--------- basics/other_border.c | 240 +++++++++---------- basics/printw_example.c | 40 ++-- basics/scanw_example.c | 40 ++-- basics/simple_attr.c | 36 +-- basics/simple_color.c | 80 +++---- basics/simple_key.c | 184 +++++++-------- basics/temp_leave.c | 40 ++-- basics/win_border.c | 164 ++++++------- basics/with_chgat.c | 48 ++-- forms/Makefile | 14 +- forms/README | 12 +- forms/form_attrib.c | 150 ++++++------ forms/form_options.c | 152 ++++++------ forms/form_simple.c | 132 +++++------ forms/form_win.c | 224 +++++++++--------- menus/Makefile | 22 +- menus/README | 22 +- menus/menu_attrib.c | 160 ++++++------- menus/menu_item_data.c | 132 +++++------ menus/menu_multi_column.c | 194 +++++++-------- menus/menu_scroll.c | 248 ++++++++++---------- menus/menu_simple.c | 112 ++++----- menus/menu_toggle.c | 168 ++++++------- menus/menu_userptr.c | 174 +++++++------- menus/menu_win.c | 210 ++++++++--------- panels/Makefile | 14 +- panels/README | 12 +- panels/panel_browse.c | 234 +++++++++---------- panels/panel_hide.c | 312 ++++++++++++------------- panels/panel_resize.c | 468 ++++++++++++++++++------------------- panels/panel_simple.c | 76 +++--- perl/05.pl | 6 +- perl/07.pl | 16 +- perl/08.pl | 58 ++--- perl/09.pl | 2 +- perl/10.pl | 48 ++-- 51 files changed, 3373 insertions(+), 3373 deletions(-) diff --git a/JustForFun/Makefile b/JustForFun/Makefile index 3f0890e..b0a1058 100644 --- a/JustForFun/Makefile +++ b/JustForFun/Makefile @@ -9,20 +9,20 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/hanoi \ - ${EXE_DIR}/life\ - ${EXE_DIR}/magic \ - ${EXE_DIR}/queens \ - ${EXE_DIR}/shuffle \ - ${EXE_DIR}/tt + ${EXE_DIR}/hanoi \ + ${EXE_DIR}/life\ + ${EXE_DIR}/magic \ + ${EXE_DIR}/queens \ + ${EXE_DIR}/shuffle \ + ${EXE_DIR}/tt ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/JustForFun/README b/JustForFun/README index a5d743f..5355c9c 100644 --- a/JustForFun/README +++ b/JustForFun/README @@ -1,10 +1,10 @@ -Description of files +Description of files -------------------- JustForFun | - |----> hanoi.c -- The Towers of Hanoi Solver - |----> life.c -- The Game of Life demo - |----> magic.c -- An Odd Order Magic Square builder - |----> queens.c -- The famous N-Queens Solver - |----> shuffle.c -- A fun game, if you have time to kill - |----> tt.c -- A very trivial typing tutor + |----> hanoi.c -- The Towers of Hanoi Solver + |----> life.c -- The Game of Life demo + |----> magic.c -- An Odd Order Magic Square builder + |----> queens.c -- The famous N-Queens Solver + |----> shuffle.c -- A fun game, if you have time to kill + |----> tt.c -- A very trivial typing tutor diff --git a/JustForFun/hanoi.c b/JustForFun/hanoi.c index b57b0b7..4085a93 100644 --- a/JustForFun/hanoi.c +++ b/JustForFun/hanoi.c @@ -1,178 +1,178 @@ -#include - -#define POSX 10 -#define POSY 5 -#define DISC_CHAR '*' -#define PEG_CHAR '#' -#define TIME_OUT 300 - -typedef struct _peg_struct { - int n_discs; /* Number of discs at present */ - int bottomx, bottomy; /* bottom x, bottom y co-ord */ - int *sizes; /* The disc sizes array */ -}peg; - -void init_pegs(peg *p_my_pegs, int n_discs); -void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs); -void free_pegs(peg *p_my_pegs, int n_discs); -void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst); -void move_disc(peg *p_my_pegs, int n_discs, int src, int dst); -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); -void check_usr_response(peg *p_my_pegs, int n_discs); - -int store_n_discs; -char *welcome_string = "Enter the number of discs you want to be solved: "; - -int main(int argc, char *argv[]) -{ int n_discs; - peg my_pegs[3]; - - initscr(); /* Start curses mode */ - cbreak(); /* Line buffering disabled. Pass on every thing */ - keypad(stdscr, TRUE); - curs_set(FALSE); - - print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); - scanw("%d", &n_discs); - - timeout(TIME_OUT); - noecho(); - store_n_discs = n_discs; - - init_pegs(my_pegs, n_discs); - show_pegs(stdscr, my_pegs, n_discs); - solve_hanoi(my_pegs, n_discs, 0, 1, 2); - - free_pegs(my_pegs, n_discs); - endwin(); /* End curses mode */ - return 0; -} - -void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) -{ if(n_discs == 0) - return; - solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); - move_disc(p_my_pegs, store_n_discs, src, dst); - show_pegs(stdscr, p_my_pegs, store_n_discs); - check_usr_response(p_my_pegs, store_n_discs); - solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst); -} - -void check_usr_response(peg *p_my_pegs, int n_discs) -{ int ch; - - ch = getch(); /* Waits for TIME_OUT milliseconds */ - if(ch == ERR) - return; - else - if(ch == KEY_F(1)) - { free_pegs(p_my_pegs, n_discs); - endwin(); - exit(0); - } -} - -void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) -{ int temp, index; - - --p_my_pegs[src].n_discs; - index = 0; - while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) - ++index; - temp = p_my_pegs[src].sizes[index]; - p_my_pegs[src].sizes[index] = 0; - - index = 0; - while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) - ++index; - --index; - p_my_pegs[dst].sizes[index] = temp; - ++p_my_pegs[dst].n_discs; -} - -void init_pegs(peg *p_my_pegs, int n_discs) -{ int size, temp, i; - - p_my_pegs[0].n_discs = n_discs; - - /* Allocate memory for size array - * atmost the number of discs on a peg can be n_discs - */ - for(i = 0; i < n_discs; ++i) - p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); - size = 3; - for(i = 0;i < n_discs; ++i, size += 2) - p_my_pegs[0].sizes[i] = size; - - temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); - p_my_pegs[0].bottomx = POSX + 1 + temp; - p_my_pegs[0].bottomy = POSY + 2 + n_discs; - - p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp; - p_my_pegs[1].bottomy = POSY + 2 + n_discs; - - p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp; - p_my_pegs[2].bottomy = POSY + 2 + n_discs; -} - -void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) -{ int i, j, k, x, y, size; - - wclear(win); - attron(A_REVERSE); - mvprintw(24, 0, "Press F1 to Exit"); - attroff(A_REVERSE); - for(i = 0;i < 3; ++i) - mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, - p_my_pegs[i].bottomx, "%c", PEG_CHAR); - y = p_my_pegs[0].bottomy - n_discs; - for(i = 0; i < 3; ++i) /* For each peg */ - { for(j = 0; j < n_discs; ++ j) /* For each row */ - { if(p_my_pegs[i].sizes[j] != 0) - { size = p_my_pegs[i].sizes[j]; - x = p_my_pegs[i].bottomx - (size / 2); - for(k = 0; k < size; ++k) - mvwprintw(win, y, x + k, "%c", DISC_CHAR); - } - else - mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); - ++y; - } - y = p_my_pegs[0].bottomy - n_discs; - } - wrefresh(win); -} - -void free_pegs(peg *p_my_pegs, int n_discs) -{ int i; - - for(i = 0;i < n_discs; ++i) - free(p_my_pegs[i].sizes); -} - -/* -------------------------------------------------------------* - * startx = 0 means at present x * - * starty = 0 means at present y * - * win = NULL means take stdscr * - * -------------------------------------------------------------*/ - -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} +#include + +#define POSX 10 +#define POSY 5 +#define DISC_CHAR '*' +#define PEG_CHAR '#' +#define TIME_OUT 300 + +typedef struct _peg_struct { + int n_discs; /* Number of discs at present */ + int bottomx, bottomy; /* bottom x, bottom y co-ord */ + int *sizes; /* The disc sizes array */ +}peg; + +void init_pegs(peg *p_my_pegs, int n_discs); +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs); +void free_pegs(peg *p_my_pegs, int n_discs); +void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst); +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); +void check_usr_response(peg *p_my_pegs, int n_discs); + +int store_n_discs; +char *welcome_string = "Enter the number of discs you want to be solved: "; + +int main(int argc, char *argv[]) +{ int n_discs; + peg my_pegs[3]; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled. Pass on every thing */ + keypad(stdscr, TRUE); + curs_set(FALSE); + + print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); + scanw("%d", &n_discs); + + timeout(TIME_OUT); + noecho(); + store_n_discs = n_discs; + + init_pegs(my_pegs, n_discs); + show_pegs(stdscr, my_pegs, n_discs); + solve_hanoi(my_pegs, n_discs, 0, 1, 2); + + free_pegs(my_pegs, n_discs); + endwin(); /* End curses mode */ + return 0; +} + +void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) +{ if(n_discs == 0) + return; + solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); + move_disc(p_my_pegs, store_n_discs, src, dst); + show_pegs(stdscr, p_my_pegs, store_n_discs); + check_usr_response(p_my_pegs, store_n_discs); + solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst); +} + +void check_usr_response(peg *p_my_pegs, int n_discs) +{ int ch; + + ch = getch(); /* Waits for TIME_OUT milliseconds */ + if(ch == ERR) + return; + else + if(ch == KEY_F(1)) + { free_pegs(p_my_pegs, n_discs); + endwin(); + exit(0); + } +} + +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) +{ int temp, index; + + --p_my_pegs[src].n_discs; + index = 0; + while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) + ++index; + temp = p_my_pegs[src].sizes[index]; + p_my_pegs[src].sizes[index] = 0; + + index = 0; + while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) + ++index; + --index; + p_my_pegs[dst].sizes[index] = temp; + ++p_my_pegs[dst].n_discs; +} + +void init_pegs(peg *p_my_pegs, int n_discs) +{ int size, temp, i; + + p_my_pegs[0].n_discs = n_discs; + + /* Allocate memory for size array + * atmost the number of discs on a peg can be n_discs + */ + for(i = 0; i < n_discs; ++i) + p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); + size = 3; + for(i = 0;i < n_discs; ++i, size += 2) + p_my_pegs[0].sizes[i] = size; + + temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); + p_my_pegs[0].bottomx = POSX + 1 + temp; + p_my_pegs[0].bottomy = POSY + 2 + n_discs; + + p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp; + p_my_pegs[1].bottomy = POSY + 2 + n_discs; + + p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp; + p_my_pegs[2].bottomy = POSY + 2 + n_discs; +} + +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) +{ int i, j, k, x, y, size; + + wclear(win); + attron(A_REVERSE); + mvprintw(24, 0, "Press F1 to Exit"); + attroff(A_REVERSE); + for(i = 0;i < 3; ++i) + mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, + p_my_pegs[i].bottomx, "%c", PEG_CHAR); + y = p_my_pegs[0].bottomy - n_discs; + for(i = 0; i < 3; ++i) /* For each peg */ + { for(j = 0; j < n_discs; ++ j) /* For each row */ + { if(p_my_pegs[i].sizes[j] != 0) + { size = p_my_pegs[i].sizes[j]; + x = p_my_pegs[i].bottomx - (size / 2); + for(k = 0; k < size; ++k) + mvwprintw(win, y, x + k, "%c", DISC_CHAR); + } + else + mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); + ++y; + } + y = p_my_pegs[0].bottomy - n_discs; + } + wrefresh(win); +} + +void free_pegs(peg *p_my_pegs, int n_discs) +{ int i; + + for(i = 0;i < n_discs; ++i) + free(p_my_pegs[i].sizes); +} + +/* -------------------------------------------------------------* + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * -------------------------------------------------------------*/ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/JustForFun/life.c b/JustForFun/life.c index eb6a039..99da8a3 100644 --- a/JustForFun/life.c +++ b/JustForFun/life.c @@ -1,107 +1,107 @@ -#include - -int STARTX = 0; -int STARTY = 0; -int ENDX = 79; -int ENDY = 24; - -#define CELL_CHAR '#' -#define TIME_OUT 300 - -typedef struct _state { - int oldstate; - int newstate; -}state; - -void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); -void calc(state **area, int x, int y); -void update_state(state **area, int startx, int starty, int endx, int endy); - -int main() -{ state **workarea; - int i, j; - - initscr(); - cbreak(); - timeout(TIME_OUT); - keypad(stdscr, TRUE); - - ENDX = COLS - 1; - ENDY = LINES - 1; - - workarea = (state **)calloc(COLS, sizeof(state *)); - for(i = 0;i < COLS; ++i) - workarea[i] = (state *)calloc(LINES, sizeof(state)); - - /* For inverted U */ - workarea[39][15].newstate = TRUE; - workarea[40][15].newstate = TRUE; - workarea[41][15].newstate = TRUE; - workarea[39][16].newstate = TRUE; - workarea[39][17].newstate = TRUE; - workarea[41][16].newstate = TRUE; - workarea[41][17].newstate = TRUE; - update_state(workarea, STARTX, STARTY, ENDX, ENDY); - - /* For block */ -/* - workarea[37][13].newstate = TRUE; - workarea[37][14].newstate = TRUE; - workarea[38][13].newstate = TRUE; - workarea[38][14].newstate = TRUE; - - update_state(workarea, STARTX, STARTY, ENDX, ENDY); -*/ - display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); - while(getch() != KEY_F(1)) - { for(i = STARTX; i <= ENDX; ++i) - for(j = STARTY; j <= ENDY; ++j) - calc(workarea, i, j); - update_state(workarea, STARTX, STARTY, ENDX, ENDY); - display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); - } - - endwin(); - return 0; -} - -void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) -{ int i, j; - wclear(win); - for(i = startx; i <= endx; ++i) - for(j = starty;j <= endy; ++j) - if(area[i][j].newstate == TRUE) - mvwaddch(win, j, i, CELL_CHAR); - wrefresh(win); -} - -void calc(state **area, int i, int j) -{ int neighbours; - int newstate; - - neighbours = - area[(i - 1 + COLS) % COLS][j].oldstate + - area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate + - area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + - area[(i + 1) % COLS][j].oldstate + - area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + - area[(i + 1) % COLS][(j + 1) % LINES].oldstate + - area[i][(j - 1 + LINES) % LINES].oldstate + - area[i][(j + 1) % LINES].oldstate; - - newstate = FALSE; - if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) - newstate = TRUE; - else - if(area[i][j].oldstate == FALSE && neighbours == 3) - newstate = TRUE; - area[i][j].newstate = newstate; -} - -void update_state(state **area, int startx, int starty, int endx, int endy) -{ int i, j; - - for(i = startx; i <= endx; ++i) - for(j = starty; j <= endy; ++j) - area[i][j].oldstate = area[i][j].newstate; -} +#include + +int STARTX = 0; +int STARTY = 0; +int ENDX = 79; +int ENDY = 24; + +#define CELL_CHAR '#' +#define TIME_OUT 300 + +typedef struct _state { + int oldstate; + int newstate; +}state; + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); +void calc(state **area, int x, int y); +void update_state(state **area, int startx, int starty, int endx, int endy); + +int main() +{ state **workarea; + int i, j; + + initscr(); + cbreak(); + timeout(TIME_OUT); + keypad(stdscr, TRUE); + + ENDX = COLS - 1; + ENDY = LINES - 1; + + workarea = (state **)calloc(COLS, sizeof(state *)); + for(i = 0;i < COLS; ++i) + workarea[i] = (state *)calloc(LINES, sizeof(state)); + + /* For inverted U */ + workarea[39][15].newstate = TRUE; + workarea[40][15].newstate = TRUE; + workarea[41][15].newstate = TRUE; + workarea[39][16].newstate = TRUE; + workarea[39][17].newstate = TRUE; + workarea[41][16].newstate = TRUE; + workarea[41][17].newstate = TRUE; + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + + /* For block */ +/* + workarea[37][13].newstate = TRUE; + workarea[37][14].newstate = TRUE; + workarea[38][13].newstate = TRUE; + workarea[38][14].newstate = TRUE; + + update_state(workarea, STARTX, STARTY, ENDX, ENDY); +*/ + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + while(getch() != KEY_F(1)) + { for(i = STARTX; i <= ENDX; ++i) + for(j = STARTY; j <= ENDY; ++j) + calc(workarea, i, j); + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + } + + endwin(); + return 0; +} + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) +{ int i, j; + wclear(win); + for(i = startx; i <= endx; ++i) + for(j = starty;j <= endy; ++j) + if(area[i][j].newstate == TRUE) + mvwaddch(win, j, i, CELL_CHAR); + wrefresh(win); +} + +void calc(state **area, int i, int j) +{ int neighbours; + int newstate; + + neighbours = + area[(i - 1 + COLS) % COLS][j].oldstate + + area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + + area[(i + 1) % COLS][j].oldstate + + area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i + 1) % COLS][(j + 1) % LINES].oldstate + + area[i][(j - 1 + LINES) % LINES].oldstate + + area[i][(j + 1) % LINES].oldstate; + + newstate = FALSE; + if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) + newstate = TRUE; + else + if(area[i][j].oldstate == FALSE && neighbours == 3) + newstate = TRUE; + area[i][j].newstate = newstate; +} + +void update_state(state **area, int startx, int starty, int endx, int endy) +{ int i, j; + + for(i = startx; i <= endx; ++i) + for(j = starty; j <= endy; ++j) + area[i][j].oldstate = area[i][j].newstate; +} diff --git a/JustForFun/magic.c b/JustForFun/magic.c index 6561941..c93b5d8 100644 --- a/JustForFun/magic.c +++ b/JustForFun/magic.c @@ -1,161 +1,161 @@ -#include -#include - -#define STARTX 9 -#define STARTY 3 -#define WIDTH 6 -#define HEIGHT 4 - -#define TRACE_VALUE TRACE_MAXIMUM - -void board( WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); -void magic(int **, int); -void print(int **, int); -void magic_board(int **a,int n); - -int main(int argc, char *argv[]) -{ - - int **a,n,i; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(0); - } - n = atoi(argv[1]); - if(n % 2 == 0) - { printf("Sorry !!! I don't know how to create magic square of even order\n"); - printf("The order should be an odd number\n"); - exit(0); - } - a = (int **) malloc(n * sizeof(int*)); - for(i = 0;i < n;++i) - a[i] = (int *)malloc(n * sizeof(int)); - - magic(a,n); - - initscr(); - curs_set(0); - noecho(); - magic_board(a,n); - getch(); - endwin(); - - return; -} - -void magic(int **a, int n) -{ - int i,j,k; - int row,col; - for(i = 0;i < n;++i) - for(j = 0;j < n;++j) - a[i][j] = -1; - row = 0; - col = n / 2; - - k = 1; - a[row][col] = k; - - while(k != n * n) - { - if(row == 0 && col != n - 1) - { row = n - 1; - col ++; - a[row][col] = ++k; - } - else if(row != 0 && col != n - 1) - { if(a[row - 1][col + 1] == -1) - { row --; - col ++; - a[row][col] = ++k; - } - else - { - row ++; - a[row][col] = ++k; - } - } - else if(row != 0 && col == n - 1) - { - row --; - col = 0; - a[row][col] = ++k; - } - else if(row == 0 && col == n - 1) - { row ++; - a[row][col] = ++k; - } - - } - return; -} - -void print(int **a,int n) -{ int i,j; - int x,y; - x = STARTX; - y = STARTY; - mvprintw(1,30,"MAGIC SQUARE"); - for(i = 0;i < n;++i) - { for(j = 0;j < n;++j) - { mvprintw(y,x,"%d",a[i][j]); - if(n > 9) - x += 4; - else - x += 6; - } - x = STARTX; - if(n > 7) - y += 2; - else - y += 3; - } - refresh(); -} -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} - -void magic_board(int **a,int n) -{ int i,j, deltax, deltay; - int startx, starty; - - starty = (LINES - n * HEIGHT) / 2; - startx = (COLS - n * WIDTH) / 2; - board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); - deltay = HEIGHT / 2; - deltax = WIDTH / 2; - for(i = 0;i < n; ++i) - for(j = 0; j < n; ++j) - mvprintw(starty + j * HEIGHT + deltay, - startx + i * WIDTH + deltax, - "%d", a[i][j]); -} +#include +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define TRACE_VALUE TRACE_MAXIMUM + +void board( WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void magic(int **, int); +void print(int **, int); +void magic_board(int **a,int n); + +int main(int argc, char *argv[]) +{ + + int **a,n,i; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(0); + } + n = atoi(argv[1]); + if(n % 2 == 0) + { printf("Sorry !!! I don't know how to create magic square of even order\n"); + printf("The order should be an odd number\n"); + exit(0); + } + a = (int **) malloc(n * sizeof(int*)); + for(i = 0;i < n;++i) + a[i] = (int *)malloc(n * sizeof(int)); + + magic(a,n); + + initscr(); + curs_set(0); + noecho(); + magic_board(a,n); + getch(); + endwin(); + + return; +} + +void magic(int **a, int n) +{ + int i,j,k; + int row,col; + for(i = 0;i < n;++i) + for(j = 0;j < n;++j) + a[i][j] = -1; + row = 0; + col = n / 2; + + k = 1; + a[row][col] = k; + + while(k != n * n) + { + if(row == 0 && col != n - 1) + { row = n - 1; + col ++; + a[row][col] = ++k; + } + else if(row != 0 && col != n - 1) + { if(a[row - 1][col + 1] == -1) + { row --; + col ++; + a[row][col] = ++k; + } + else + { + row ++; + a[row][col] = ++k; + } + } + else if(row != 0 && col == n - 1) + { + row --; + col = 0; + a[row][col] = ++k; + } + else if(row == 0 && col == n - 1) + { row ++; + a[row][col] = ++k; + } + + } + return; +} + +void print(int **a,int n) +{ int i,j; + int x,y; + x = STARTX; + y = STARTY; + mvprintw(1,30,"MAGIC SQUARE"); + for(i = 0;i < n;++i) + { for(j = 0;j < n;++j) + { mvprintw(y,x,"%d",a[i][j]); + if(n > 9) + x += 4; + else + x += 6; + } + x = STARTX; + if(n > 7) + y += 2; + else + y += 3; + } + refresh(); +} +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void magic_board(int **a,int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(i = 0;i < n; ++i) + for(j = 0; j < n; ++j) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%d", a[i][j]); +} diff --git a/JustForFun/queens.c b/JustForFun/queens.c index 2457ccc..82b11c1 100644 --- a/JustForFun/queens.c +++ b/JustForFun/queens.c @@ -1,122 +1,122 @@ -#include -#include - -#define QUEEN_CHAR '*' - -int *nqueens(int num); -int place(int current, int *position); -int print(int *positions, int num_queens); -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); - -int main(int argc, char *argv[]) -{ - int num_queens, *positions, count; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(1); - } - - num_queens = atoi(argv[1]); - initscr(); - cbreak(); - keypad(stdscr, TRUE); - positions = nqueens(num_queens); - free(positions); - endwin(); - return 0; -} - -int *nqueens(int num) -{ - int current, *position, num_solutions = 0; - - position = (int *) calloc(num + 1, sizeof(int)); - - position[1] = 0; - current = 1; /* current queen is being checked */ - /* position[current] is the coloumn*/ - while(current > 0){ - position[current] += 1; - while(position[current] <= num && !place(current, position) ) - position[current] += 1; - if(position[current] <= num){ - if(current == num) { - ++num_solutions; - print(position, num); - } - else { - current += 1; - position[current] = 0; - } - } - else current -= 1; /* backtrack */ - } - printf("Total Number of Solutions : %d\n", num_solutions); - return(position); -} - -int place(int current, int *position) -{ - int i; - if(current == 1) return(1); - for(i = 1; i < current; ++i) - if(position[i] == position[current]) return(0); - else if(abs(position[i] - position[current]) == - abs(i - current)) - return(0); - - return(1); -} - -int print(int *positions, int num_queens) -{ int count; - int y = 2, x = 2, w = 4, h = 2; - static int solution = 1; - - mvprintw(0, 0, "Solution No: %d", solution++); - board(stdscr, y, x, num_queens, num_queens, w, h); - for(count = 1; count <= num_queens; ++count) - { int tempy = y + (count - 1) * h + h / 2; - int tempx = x + (positions[count] - 1) * w + w / 2; - mvaddch(tempy, tempx, QUEEN_CHAR); - } - refresh(); - mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); - if(getch() == KEY_F(1)) - { endwin(); - exit(0); - } - clear(); -} - -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} +#include +#include + +#define QUEEN_CHAR '*' + +int *nqueens(int num); +int place(int current, int *position); +int print(int *positions, int num_queens); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); + +int main(int argc, char *argv[]) +{ + int num_queens, *positions, count; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + + num_queens = atoi(argv[1]); + initscr(); + cbreak(); + keypad(stdscr, TRUE); + positions = nqueens(num_queens); + free(positions); + endwin(); + return 0; +} + +int *nqueens(int num) +{ + int current, *position, num_solutions = 0; + + position = (int *) calloc(num + 1, sizeof(int)); + + position[1] = 0; + current = 1; /* current queen is being checked */ + /* position[current] is the coloumn*/ + while(current > 0){ + position[current] += 1; + while(position[current] <= num && !place(current, position) ) + position[current] += 1; + if(position[current] <= num){ + if(current == num) { + ++num_solutions; + print(position, num); + } + else { + current += 1; + position[current] = 0; + } + } + else current -= 1; /* backtrack */ + } + printf("Total Number of Solutions : %d\n", num_solutions); + return(position); +} + +int place(int current, int *position) +{ + int i; + if(current == 1) return(1); + for(i = 1; i < current; ++i) + if(position[i] == position[current]) return(0); + else if(abs(position[i] - position[current]) == + abs(i - current)) + return(0); + + return(1); +} + +int print(int *positions, int num_queens) +{ int count; + int y = 2, x = 2, w = 4, h = 2; + static int solution = 1; + + mvprintw(0, 0, "Solution No: %d", solution++); + board(stdscr, y, x, num_queens, num_queens, w, h); + for(count = 1; count <= num_queens; ++count) + { int tempy = y + (count - 1) * h + h / 2; + int tempx = x + (positions[count] - 1) * w + w / 2; + mvaddch(tempy, tempx, QUEEN_CHAR); + } + refresh(); + mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); + if(getch() == KEY_F(1)) + { endwin(); + exit(0); + } + clear(); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} diff --git a/JustForFun/shuffle.c b/JustForFun/shuffle.c index 0b8150b..d1431bd 100644 --- a/JustForFun/shuffle.c +++ b/JustForFun/shuffle.c @@ -1,205 +1,205 @@ -#include - -#define STARTX 9 -#define STARTY 3 -#define WIDTH 6 -#define HEIGHT 4 - -#define BLANK 0 - -typedef struct _tile { - int x; - int y; -}tile; - -void init_board(int **board, int n, tile *blank); -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); -void shuffle_board(int **board, int n); -void move_blank(int direction, int **s_board, int n, tile *blank); -int check_win(int **s_board, int n, tile *blank); - -enum { LEFT, RIGHT, UP, DOWN }; - -int main(int argc, char *argv[]) -{ int **s_board; - int n, i, ch; - tile blank; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(1); - } - n = atoi(argv[1]); - - s_board = (int **)calloc(n, sizeof(int *)); - for(i = 0;i < n; ++i) - s_board[i] = (int *)calloc(n, sizeof(int)); - init_board(s_board, n, &blank); - initscr(); - keypad(stdscr, TRUE); - cbreak(); - shuffle_board(s_board, n); - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - move_blank(RIGHT, s_board, n, &blank); - break; - case KEY_RIGHT: - move_blank(LEFT, s_board, n, &blank); - break; - case KEY_UP: - move_blank(DOWN, s_board, n, &blank); - break; - case KEY_DOWN: - move_blank(UP, s_board, n, &blank); - break; - } - shuffle_board(s_board, n); - if(check_win(s_board, n, &blank) == TRUE) - { mvprintw(24, 0, "You Win !!!\n"); - refresh(); - break; - } - } - endwin(); - return 0; -} - -void move_blank(int direction, int **s_board, int n, tile *blank) -{ int temp; - - switch(direction) - { case LEFT: - { if(blank->x != 0) - { --blank->x; - temp = s_board[blank->x][blank->y]; - s_board[blank->x + 1][blank->y] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case RIGHT: - { if(blank->x != n - 1) - { ++blank->x; - temp = s_board[blank->x][blank->y]; - s_board[blank->x - 1][blank->y] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case UP: - { if(blank->y != 0) - { --blank->y; - temp = s_board[blank->x][blank->y]; - s_board[blank->x][blank->y + 1] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case DOWN: - { if(blank->y != n - 1) - { ++blank->y; - temp = s_board[blank->x][blank->y]; - s_board[blank->x][blank->y - 1] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - } -} - -int check_win(int **s_board, int n, tile *blank) -{ int i, j; - - s_board[blank->x][blank->y] = n * n; - for(i = 0;i < n; ++i) - for(j = 0;j < n; ++j) - if(s_board[i][j] != j * n + i + 1) - { s_board[blank->x][blank->y] = BLANK; - return FALSE; - } - - s_board[blank->x][blank->y] = BLANK; - return TRUE; -} - -void init_board(int **s_board, int n, tile *blank) -{ int i, j, k; - int *temp_board; - - temp_board = (int *)calloc(n * n, sizeof(int)); - srand(time(NULL)); - for(i = 0;i < n * n; ++i) - { -repeat : - k = rand() % (n * n); - for(j = 0;j <= i - 1; ++j) - if (k == temp_board[j]) - goto repeat; - else - temp_board[i] = k; - } - k = 0; - for (i = 0;i < n;++i) - for(j = 0;j < n; ++j,++k) - { if(temp_board[k] == 0) - { blank->x = i; - blank->y = j; - } - s_board[i][j] = temp_board[k]; - } - free(temp_board); -} - -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} - -void shuffle_board(int **s_board, int n) -{ int i,j, deltax, deltay; - int startx, starty; - - starty = (LINES - n * HEIGHT) / 2; - startx = (COLS - n * WIDTH) / 2; - clear(); - mvprintw(24, 0, "Press F1 to Exit"); - board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); - deltay = HEIGHT / 2; - deltax = WIDTH / 2; - for(j = 0; j < n; ++j) - for(i = 0;i < n; ++i) - if(s_board[i][j] != BLANK) - mvprintw(starty + j * HEIGHT + deltay, - startx + i * WIDTH + deltax, - "%-2d", s_board[i][j]); - refresh(); -} - - +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define BLANK 0 + +typedef struct _tile { + int x; + int y; +}tile; + +void init_board(int **board, int n, tile *blank); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void shuffle_board(int **board, int n); +void move_blank(int direction, int **s_board, int n, tile *blank); +int check_win(int **s_board, int n, tile *blank); + +enum { LEFT, RIGHT, UP, DOWN }; + +int main(int argc, char *argv[]) +{ int **s_board; + int n, i, ch; + tile blank; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + n = atoi(argv[1]); + + s_board = (int **)calloc(n, sizeof(int *)); + for(i = 0;i < n; ++i) + s_board[i] = (int *)calloc(n, sizeof(int)); + init_board(s_board, n, &blank); + initscr(); + keypad(stdscr, TRUE); + cbreak(); + shuffle_board(s_board, n); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + move_blank(RIGHT, s_board, n, &blank); + break; + case KEY_RIGHT: + move_blank(LEFT, s_board, n, &blank); + break; + case KEY_UP: + move_blank(DOWN, s_board, n, &blank); + break; + case KEY_DOWN: + move_blank(UP, s_board, n, &blank); + break; + } + shuffle_board(s_board, n); + if(check_win(s_board, n, &blank) == TRUE) + { mvprintw(24, 0, "You Win !!!\n"); + refresh(); + break; + } + } + endwin(); + return 0; +} + +void move_blank(int direction, int **s_board, int n, tile *blank) +{ int temp; + + switch(direction) + { case LEFT: + { if(blank->x != 0) + { --blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x + 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case RIGHT: + { if(blank->x != n - 1) + { ++blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x - 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case UP: + { if(blank->y != 0) + { --blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y + 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case DOWN: + { if(blank->y != n - 1) + { ++blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y - 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + } +} + +int check_win(int **s_board, int n, tile *blank) +{ int i, j; + + s_board[blank->x][blank->y] = n * n; + for(i = 0;i < n; ++i) + for(j = 0;j < n; ++j) + if(s_board[i][j] != j * n + i + 1) + { s_board[blank->x][blank->y] = BLANK; + return FALSE; + } + + s_board[blank->x][blank->y] = BLANK; + return TRUE; +} + +void init_board(int **s_board, int n, tile *blank) +{ int i, j, k; + int *temp_board; + + temp_board = (int *)calloc(n * n, sizeof(int)); + srand(time(NULL)); + for(i = 0;i < n * n; ++i) + { +repeat : + k = rand() % (n * n); + for(j = 0;j <= i - 1; ++j) + if (k == temp_board[j]) + goto repeat; + else + temp_board[i] = k; + } + k = 0; + for (i = 0;i < n;++i) + for(j = 0;j < n; ++j,++k) + { if(temp_board[k] == 0) + { blank->x = i; + blank->y = j; + } + s_board[i][j] = temp_board[k]; + } + free(temp_board); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void shuffle_board(int **s_board, int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + clear(); + mvprintw(24, 0, "Press F1 to Exit"); + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(j = 0; j < n; ++j) + for(i = 0;i < n; ++i) + if(s_board[i][j] != BLANK) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%-2d", s_board[i][j]); + refresh(); +} + + diff --git a/JustForFun/tt.c b/JustForFun/tt.c index 4378e67..178eefa 100644 --- a/JustForFun/tt.c +++ b/JustForFun/tt.c @@ -1,223 +1,223 @@ -#include -#include -#include -#include - -#define HSIZE 60 -#define LENGTH 75 -#define WIDTH 10 -#define STARTX 1 -#define STARTY 5 -#define STATUSX 1 -#define STATUSY 25 - -#define KEY_F1 265 - -int print_menu(); -void print_byebye(); -void create_test_string(); -void print_time(time_t startt, time_t endt, int mistakes); -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); - -char *groups[] = { "`123456" , - "7890-=" , - "~!@#$%^" , - "&*()_+" , - "<>?" , - ",./\\" , - "asdfg", - "jkl;'", - "qwer", - "uiop", - "tyur", - "zxcv", - "bnm", - }; -int n_groups; - -int main() -{ int choice, i; - char *test_array; - int ch = KEY_F1; - int mistakes; - int x, y; - time_t start_t, end_t; - WINDOW *typing_win; - char string[80]; - - string[0] = '\0'; - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - intrflush(stdscr, FALSE); - - srandom(time(NULL)); - n_groups = sizeof(groups) / sizeof(char *); - test_array = (char *)calloc(HSIZE + 1, sizeof(char)); - - while(1) - { - if(ch == KEY_F1) - { choice = print_menu(); - choice -= 1; - if(choice == n_groups) - { print_byebye(); - free(test_array); - endwin(); - exit(0); - } - } - clear(); - strcpy(string, "Typing window"); - print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); - attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); - refresh(); - attroff(A_REVERSE); - - create_test_string(test_array, choice); - typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); - keypad(typing_win, TRUE); - intrflush(typing_win, FALSE); - box(typing_win, 0, 0); - - x = 1; - y = 1; - mvwprintw(typing_win, y, x, "%s", test_array); - wrefresh(typing_win); - y += 1; - - mistakes = 0; - i = 0; - time(&start_t); - wmove(typing_win, y, x); - wrefresh(typing_win); - ch = 0; - while(ch != KEY_F1 && i != HSIZE + 1) - { ch = wgetch(typing_win); - mvwprintw(typing_win, y, x, "%c", ch); - wrefresh(typing_win); - ++x; - if(ch == test_array[i]) - { ++i; - continue; - } - else - { ++mistakes; - ++i; - } - } - - time(&end_t); - print_time(start_t, end_t, mistakes); - } - free(test_array); - endwin(); - return 0; -} - - -int print_menu() -{ int choice, i; - - choice = 0; - while(1) - { clear(); - printw("\n\n"); - print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); - printw("\n\n\n"); - for(i = 0;i <= n_groups - 1; ++i) - printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); - printw("\t%3d: \tExit\n", i + 1); - - printw("\n\n\tChoice: "); - refresh(); - echo(); - scanw("%d", &choice); - noecho(); - - if(choice >= 1 && choice <= n_groups + 1) - break; - else - { attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); - attroff(A_REVERSE); - getch(); - } - } - return choice; -} - -void create_test_string(char *test_array, int choice) -{ int i, index, length; - - length = strlen(groups[choice]); - for(i = 0;i <= HSIZE - 1; ++i) - { if(i%5 == 0) - test_array[i] = ' '; - else - { index = (int)(random() % length); - test_array[i] = groups[choice][index]; - } - } - test_array[i] = '\0'; -} - -void print_byebye() -{ printw("\n"); - print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); - print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL); - refresh(); -} - -void print_time(time_t start_t, time_t end_t, int mistakes) -{ long int diff; - int h,m,s; - float wpm; - - diff = end_t - start_t; - wpm = ((HSIZE / 5)/(double)diff)*60; - - h = (int)(diff / 3600); - diff -= h * 3600; - m = (int)(diff / 60); - diff -= m * 60; - s = (int)diff; - - attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm); - attroff(A_REVERSE); - - refresh(); - getch(); - -} - -/* ---------------------------------------------------------------- * - * startx = 0 means at present x * - * starty = 0 means at present y * - * win = NULL means take stdscr * - * ---------------------------------------------------------------- */ - -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} +#include +#include +#include +#include + +#define HSIZE 60 +#define LENGTH 75 +#define WIDTH 10 +#define STARTX 1 +#define STARTY 5 +#define STATUSX 1 +#define STATUSY 25 + +#define KEY_F1 265 + +int print_menu(); +void print_byebye(); +void create_test_string(); +void print_time(time_t startt, time_t endt, int mistakes); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); + +char *groups[] = { "`123456" , + "7890-=" , + "~!@#$%^" , + "&*()_+" , + "<>?" , + ",./\\" , + "asdfg", + "jkl;'", + "qwer", + "uiop", + "tyur", + "zxcv", + "bnm", + }; +int n_groups; + +int main() +{ int choice, i; + char *test_array; + int ch = KEY_F1; + int mistakes; + int x, y; + time_t start_t, end_t; + WINDOW *typing_win; + char string[80]; + + string[0] = '\0'; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + intrflush(stdscr, FALSE); + + srandom(time(NULL)); + n_groups = sizeof(groups) / sizeof(char *); + test_array = (char *)calloc(HSIZE + 1, sizeof(char)); + + while(1) + { + if(ch == KEY_F1) + { choice = print_menu(); + choice -= 1; + if(choice == n_groups) + { print_byebye(); + free(test_array); + endwin(); + exit(0); + } + } + clear(); + strcpy(string, "Typing window"); + print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); + refresh(); + attroff(A_REVERSE); + + create_test_string(test_array, choice); + typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); + keypad(typing_win, TRUE); + intrflush(typing_win, FALSE); + box(typing_win, 0, 0); + + x = 1; + y = 1; + mvwprintw(typing_win, y, x, "%s", test_array); + wrefresh(typing_win); + y += 1; + + mistakes = 0; + i = 0; + time(&start_t); + wmove(typing_win, y, x); + wrefresh(typing_win); + ch = 0; + while(ch != KEY_F1 && i != HSIZE + 1) + { ch = wgetch(typing_win); + mvwprintw(typing_win, y, x, "%c", ch); + wrefresh(typing_win); + ++x; + if(ch == test_array[i]) + { ++i; + continue; + } + else + { ++mistakes; + ++i; + } + } + + time(&end_t); + print_time(start_t, end_t, mistakes); + } + free(test_array); + endwin(); + return 0; +} + + +int print_menu() +{ int choice, i; + + choice = 0; + while(1) + { clear(); + printw("\n\n"); + print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); + printw("\n\n\n"); + for(i = 0;i <= n_groups - 1; ++i) + printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); + printw("\t%3d: \tExit\n", i + 1); + + printw("\n\n\tChoice: "); + refresh(); + echo(); + scanw("%d", &choice); + noecho(); + + if(choice >= 1 && choice <= n_groups + 1) + break; + else + { attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); + attroff(A_REVERSE); + getch(); + } + } + return choice; +} + +void create_test_string(char *test_array, int choice) +{ int i, index, length; + + length = strlen(groups[choice]); + for(i = 0;i <= HSIZE - 1; ++i) + { if(i%5 == 0) + test_array[i] = ' '; + else + { index = (int)(random() % length); + test_array[i] = groups[choice][index]; + } + } + test_array[i] = '\0'; +} + +void print_byebye() +{ printw("\n"); + print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); + print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL); + refresh(); +} + +void print_time(time_t start_t, time_t end_t, int mistakes) +{ long int diff; + int h,m,s; + float wpm; + + diff = end_t - start_t; + wpm = ((HSIZE / 5)/(double)diff)*60; + + h = (int)(diff / 3600); + diff -= h * 3600; + m = (int)(diff / 60); + diff -= m * 60; + s = (int)diff; + + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm); + attroff(A_REVERSE); + + refresh(); + getch(); + +} + +/* ---------------------------------------------------------------- * + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * ---------------------------------------------------------------- */ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/basics/Makefile b/basics/Makefile index 90ec1fb..85f1de7 100644 --- a/basics/Makefile +++ b/basics/Makefile @@ -10,26 +10,26 @@ EXE_DIR=../demo/exe EXES = \ ${EXE_DIR}/hello_world \ - ${EXE_DIR}/init_func_example \ - ${EXE_DIR}/key_code \ - ${EXE_DIR}/mouse_menu \ - ${EXE_DIR}/other_border \ - ${EXE_DIR}/printw_example \ - ${EXE_DIR}/scanw_example \ - ${EXE_DIR}/simple_attr \ - ${EXE_DIR}/simple_color \ - ${EXE_DIR}/simple_key \ - ${EXE_DIR}/temp_leave \ - ${EXE_DIR}/win_border \ - ${EXE_DIR}/with_chgat + ${EXE_DIR}/init_func_example \ + ${EXE_DIR}/key_code \ + ${EXE_DIR}/mouse_menu \ + ${EXE_DIR}/other_border \ + ${EXE_DIR}/printw_example \ + ${EXE_DIR}/scanw_example \ + ${EXE_DIR}/simple_attr \ + ${EXE_DIR}/simple_color \ + ${EXE_DIR}/simple_key \ + ${EXE_DIR}/temp_leave \ + ${EXE_DIR}/win_border \ + ${EXE_DIR}/with_chgat ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/basics/README b/basics/README index 0760867..ac5f396 100644 --- a/basics/README +++ b/basics/README @@ -1,20 +1,20 @@ -Description of files +Description of files -------------------- basics | - |----> acs_vars.c -- ACS_ variables example - |----> hello_world.c -- Simple "Hello World" Program - |----> init_func_example.c -- Initialization functions example - |----> key_code.c -- Shows the scan code of the key pressed - |----> mouse_menu.c -- A menu accessible by mouse - |----> other_border.c -- Shows usage of other border functions apart - | -- box() - |----> printw_example.c -- A very simple printw() example - |----> scanw_example.c -- A very simple getstr() example - |----> simple_attr.c -- A program that can print a c file with comments - | -- in attribute - |----> simple_color.c -- A simple example demonstrating colors - |----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows - |----> temp_leave.c -- Demonstrates temporarily leaving curses mode - |----> win_border.c -- Shows Creation of windows and borders - |----> with_chgat.c -- chgat() usage example + |----> acs_vars.c -- ACS_ variables example + |----> hello_world.c -- Simple "Hello World" Program + |----> init_func_example.c -- Initialization functions example + |----> key_code.c -- Shows the scan code of the key pressed + |----> mouse_menu.c -- A menu accessible by mouse + |----> other_border.c -- Shows usage of other border functions apart + | -- box() + |----> printw_example.c -- A very simple printw() example + |----> scanw_example.c -- A very simple getstr() example + |----> simple_attr.c -- A program that can print a c file with comments + | -- in attribute + |----> simple_color.c -- A simple example demonstrating colors + |----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows + |----> temp_leave.c -- Demonstrates temporarily leaving curses mode + |----> win_border.c -- Shows Creation of windows and borders + |----> with_chgat.c -- chgat() usage example diff --git a/basics/acs_vars.c b/basics/acs_vars.c index c1c72a3..92db58c 100644 --- a/basics/acs_vars.c +++ b/basics/acs_vars.c @@ -1,44 +1,44 @@ -#include - -int main() -{ - initscr(); - - printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); - printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); - printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); - printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); - printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); - printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); - printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); - printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); - printw("Vertical line "); addch(ACS_VLINE); printw("\n"); - printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); - printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); - printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); - printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); - printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); - printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); - printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); - printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); - printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); - printw("Bullet "); addch(ACS_BULLET); printw("\n"); - printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); - printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); - printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); - printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); - printw("Board of squares "); addch(ACS_BOARD); printw("\n"); - printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); - printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); - printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); - printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); - printw("Pi "); addch(ACS_PI); printw("\n"); - printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); - printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); - - refresh(); - getch(); - endwin(); - - return 0; -} +#include + +int main() +{ + initscr(); + + printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); + printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); + printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); + printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); + printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); + printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); + printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); + printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); + printw("Vertical line "); addch(ACS_VLINE); printw("\n"); + printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); + printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); + printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); + printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); + printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); + printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); + printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); + printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); + printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); + printw("Bullet "); addch(ACS_BULLET); printw("\n"); + printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); + printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); + printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); + printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); + printw("Board of squares "); addch(ACS_BOARD); printw("\n"); + printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); + printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); + printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); + printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); + printw("Pi "); addch(ACS_PI); printw("\n"); + printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); + printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); + + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/basics/hello_world.c b/basics/hello_world.c index 55b24df..794ae22 100644 --- a/basics/hello_world.c +++ b/basics/hello_world.c @@ -1,12 +1,12 @@ -#include - -int main() -{ - initscr(); /* Start curses mode */ - printw("Hello World !!!"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/init_func_example.c b/basics/init_func_example.c index 2eb19b7..e01ecb3 100644 --- a/basics/init_func_example.c +++ b/basics/init_func_example.c @@ -1,31 +1,31 @@ -#include - -int main() -{ int ch; - - initscr(); /* Start curses mode */ - raw(); /* Line buffering disabled */ - keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ - noecho(); /* Don't echo() while we do getch */ - - printw("Type any character to see it in bold\n"); - ch = getch(); /* If raw() hadn't been called - * we have to press enter before it - * gets to the program */ - if(ch == KEY_F(1)) /* Without keypad enabled this will */ - printw("F1 Key pressed");/* not get to us either */ - /* Without noecho() some ugly escape - * charachters might have been printed - * on screen */ - else - { printw("The pressed key is "); - attron(A_BOLD); - printw("%c", ch); - attroff(A_BOLD); - } - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ int ch; + + initscr(); /* Start curses mode */ + raw(); /* Line buffering disabled */ + keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ + noecho(); /* Don't echo() while we do getch */ + + printw("Type any character to see it in bold\n"); + ch = getch(); /* If raw() hadn't been called + * we have to press enter before it + * gets to the program */ + if(ch == KEY_F(1)) /* Without keypad enabled this will */ + printw("F1 Key pressed");/* not get to us either */ + /* Without noecho() some ugly escape + * charachters might have been printed + * on screen */ + else + { printw("The pressed key is "); + attron(A_BOLD); + printw("%c", ch); + attroff(A_BOLD); + } + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/key_code.c b/basics/key_code.c index 8c5d06c..5ad80eb 100644 --- a/basics/key_code.c +++ b/basics/key_code.c @@ -1,14 +1,14 @@ -#include - -int main() -{ int ch; - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - ch = getch(); - endwin(); - printf("The key pressed is %d\n", ch); -} +#include + +int main() +{ int ch; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + ch = getch(); + endwin(); + printf("The key pressed is %d\n", ch); +} diff --git a/basics/mouse_menu.c b/basics/mouse_menu.c index 5b92e1e..6008092 100644 --- a/basics/mouse_menu.c +++ b/basics/mouse_menu.c @@ -1,106 +1,106 @@ -#include - -#define WIDTH 30 -#define HEIGHT 10 - -int startx = 0; -int starty = 0; - -char *choices[] = { "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int n_choices = sizeof(choices) / sizeof(char *); - -void print_menu(WINDOW *menu_win, int highlight); -void report_choice(int mouse_x, int mouse_y, int *p_choice); - -int main() -{ int c, choice = 0; - WINDOW *menu_win; - MEVENT event; - - /* Initialize curses */ - initscr(); - clear(); - noecho(); - cbreak(); //Line buffering disabled. pass on everything - - /* Try to put the window in the middle of screen */ - startx = (80 - WIDTH) / 2; - starty = (24 - HEIGHT) / 2; - - attron(A_REVERSE); - mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); - refresh(); - attroff(A_REVERSE); - - /* Print the menu for the first time */ - menu_win = newwin(HEIGHT, WIDTH, starty, startx); - print_menu(menu_win, 1); - /* Get all the mouse events */ - mousemask(ALL_MOUSE_EVENTS, NULL); - - while(1) - { c = wgetch(menu_win); - switch(c) - { case KEY_MOUSE: - if(getmouse(&event) == OK) - { /* When the user clicks left mouse button */ - if(event.bstate & BUTTON1_PRESSED) - { report_choice(event.x + 1, event.y + 1, &choice); - if(choice == -1) //Exit chosen - goto end; - mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); - refresh(); - } - } - print_menu(menu_win, choice); - break; - } - } -end: - endwin(); - return 0; -} - - -void print_menu(WINDOW *menu_win, int highlight) -{ - int x, y, i; - - x = 2; - y = 2; - box(menu_win, 0, 0); - for(i = 0; i < n_choices; ++i) - { if(highlight == i + 1) - { wattron(menu_win, A_REVERSE); - mvwprintw(menu_win, y, x, "%s", choices[i]); - wattroff(menu_win, A_REVERSE); - } - else - mvwprintw(menu_win, y, x, "%s", choices[i]); - ++y; - } - wrefresh(menu_win); -} - -/* Report the choice according to mouse position */ -void report_choice(int mouse_x, int mouse_y, int *p_choice) -{ int i,j, choice; - - i = startx + 2; - j = starty + 3; - - for(choice = 0; choice < n_choices; ++choice) - if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) - { if(choice == n_choices - 1) - *p_choice = -1; - else - *p_choice = choice + 1; - break; - } -} +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int n_choices = sizeof(choices) / sizeof(char *); + +void print_menu(WINDOW *menu_win, int highlight); +void report_choice(int mouse_x, int mouse_y, int *p_choice); + +int main() +{ int c, choice = 0; + WINDOW *menu_win; + MEVENT event; + + /* Initialize curses */ + initscr(); + clear(); + noecho(); + cbreak(); //Line buffering disabled. pass on everything + + /* Try to put the window in the middle of screen */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + attron(A_REVERSE); + mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); + refresh(); + attroff(A_REVERSE); + + /* Print the menu for the first time */ + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + print_menu(menu_win, 1); + /* Get all the mouse events */ + mousemask(ALL_MOUSE_EVENTS, NULL); + + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_MOUSE: + if(getmouse(&event) == OK) + { /* When the user clicks left mouse button */ + if(event.bstate & BUTTON1_PRESSED) + { report_choice(event.x + 1, event.y + 1, &choice); + if(choice == -1) //Exit chosen + goto end; + mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); + refresh(); + } + } + print_menu(menu_win, choice); + break; + } + } +end: + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + +/* Report the choice according to mouse position */ +void report_choice(int mouse_x, int mouse_y, int *p_choice) +{ int i,j, choice; + + i = startx + 2; + j = starty + 3; + + for(choice = 0; choice < n_choices; ++choice) + if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) + { if(choice == n_choices - 1) + *p_choice = -1; + else + *p_choice = choice + 1; + break; + } +} diff --git a/basics/other_border.c b/basics/other_border.c index 725d67a..8252631 100644 --- a/basics/other_border.c +++ b/basics/other_border.c @@ -1,120 +1,120 @@ -#include - -typedef struct _win_border_struct { - chtype ls, rs, ts, bs, - tl, tr, bl, br; -}WIN_BORDER; - -typedef struct _WIN_struct { - - int startx, starty; - int height, width; - WIN_BORDER border; -}WIN; - -void init_win_params(WIN *p_win); -void print_win_params(WIN *p_win); -void create_box(WIN *win, bool flag); - -int main(int argc, char *argv[]) -{ WIN win; - int ch; - - initscr(); /* Start curses mode */ - start_color(); /* Start the color functionality */ - cbreak(); /* Line buffering disabled, Pass on - * everty thing to me */ - keypad(stdscr, TRUE); /* I need that nifty F1 */ - noecho(); - init_pair(1, COLOR_CYAN, COLOR_BLACK); - - /* Initialize the window parameters */ - init_win_params(&win); - print_win_params(&win); - - attron(COLOR_PAIR(1)); - printw("Press F1 to exit"); - refresh(); - attroff(COLOR_PAIR(1)); - - create_box(&win, TRUE); - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - create_box(&win, FALSE); - --win.startx; - create_box(&win, TRUE); - break; - case KEY_RIGHT: - create_box(&win, FALSE); - ++win.startx; - create_box(&win, TRUE); - break; - case KEY_UP: - create_box(&win, FALSE); - --win.starty; - create_box(&win, TRUE); - break; - case KEY_DOWN: - create_box(&win, FALSE); - ++win.starty; - create_box(&win, TRUE); - break; - } - } - endwin(); /* End curses mode */ - return 0; -} -void init_win_params(WIN *p_win) -{ - p_win->height = 3; - p_win->width = 10; - p_win->starty = (LINES - p_win->height)/2; - p_win->startx = (COLS - p_win->width)/2; - - p_win->border.ls = '|'; - p_win->border.rs = '|'; - p_win->border.ts = '-'; - p_win->border.bs = '-'; - p_win->border.tl = '+'; - p_win->border.tr = '+'; - p_win->border.bl = '+'; - p_win->border.br = '+'; - -} -void print_win_params(WIN *p_win) -{ -#ifdef _DEBUG - mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, - p_win->width, p_win->height); - refresh(); -#endif -} -void create_box(WIN *p_win, bool flag) -{ int i, j; - int x, y, w, h; - - x = p_win->startx; - y = p_win->starty; - w = p_win->width; - h = p_win->height; - - if(flag == TRUE) - { mvaddch(y, x, p_win->border.tl); - mvaddch(y, x + w, p_win->border.tr); - mvaddch(y + h, x, p_win->border.bl); - mvaddch(y + h, x + w, p_win->border.br); - mvhline(y, x + 1, p_win->border.ts, w - 1); - mvhline(y + h, x + 1, p_win->border.bs, w - 1); - mvvline(y + 1, x, p_win->border.ls, h - 1); - mvvline(y + 1, x + w, p_win->border.rs, h - 1); - - } - else - for(j = y; j <= y + h; ++j) - for(i = x; i <= x + w; ++i) - mvaddch(j, i, ' '); - - refresh(); - -} +#include + +typedef struct _win_border_struct { + chtype ls, rs, ts, bs, + tl, tr, bl, br; +}WIN_BORDER; + +typedef struct _WIN_struct { + + int startx, starty; + int height, width; + WIN_BORDER border; +}WIN; + +void init_win_params(WIN *p_win); +void print_win_params(WIN *p_win); +void create_box(WIN *win, bool flag); + +int main(int argc, char *argv[]) +{ WIN win; + int ch; + + initscr(); /* Start curses mode */ + start_color(); /* Start the color functionality */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + noecho(); + init_pair(1, COLOR_CYAN, COLOR_BLACK); + + /* Initialize the window parameters */ + init_win_params(&win); + print_win_params(&win); + + attron(COLOR_PAIR(1)); + printw("Press F1 to exit"); + refresh(); + attroff(COLOR_PAIR(1)); + + create_box(&win, TRUE); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + create_box(&win, FALSE); + --win.startx; + create_box(&win, TRUE); + break; + case KEY_RIGHT: + create_box(&win, FALSE); + ++win.startx; + create_box(&win, TRUE); + break; + case KEY_UP: + create_box(&win, FALSE); + --win.starty; + create_box(&win, TRUE); + break; + case KEY_DOWN: + create_box(&win, FALSE); + ++win.starty; + create_box(&win, TRUE); + break; + } + } + endwin(); /* End curses mode */ + return 0; +} +void init_win_params(WIN *p_win) +{ + p_win->height = 3; + p_win->width = 10; + p_win->starty = (LINES - p_win->height)/2; + p_win->startx = (COLS - p_win->width)/2; + + p_win->border.ls = '|'; + p_win->border.rs = '|'; + p_win->border.ts = '-'; + p_win->border.bs = '-'; + p_win->border.tl = '+'; + p_win->border.tr = '+'; + p_win->border.bl = '+'; + p_win->border.br = '+'; + +} +void print_win_params(WIN *p_win) +{ +#ifdef _DEBUG + mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, + p_win->width, p_win->height); + refresh(); +#endif +} +void create_box(WIN *p_win, bool flag) +{ int i, j; + int x, y, w, h; + + x = p_win->startx; + y = p_win->starty; + w = p_win->width; + h = p_win->height; + + if(flag == TRUE) + { mvaddch(y, x, p_win->border.tl); + mvaddch(y, x + w, p_win->border.tr); + mvaddch(y + h, x, p_win->border.bl); + mvaddch(y + h, x + w, p_win->border.br); + mvhline(y, x + 1, p_win->border.ts, w - 1); + mvhline(y + h, x + 1, p_win->border.bs, w - 1); + mvvline(y + 1, x, p_win->border.ls, h - 1); + mvvline(y + 1, x + w, p_win->border.rs, h - 1); + + } + else + for(j = y; j <= y + h; ++j) + for(i = x; i <= x + w; ++i) + mvaddch(j, i, ' '); + + refresh(); + +} diff --git a/basics/printw_example.c b/basics/printw_example.c index ae1345e..3f6d2e5 100644 --- a/basics/printw_example.c +++ b/basics/printw_example.c @@ -1,20 +1,20 @@ -#include /* ncurses.h includes stdio.h */ -#include - -int main() -{ - char mesg[]="Just a string"; /* message to be appeared on the screen */ - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); - printw("Try resizing your window(if possible) and then run this program again"); - refresh(); - getch(); - endwin(); - - return 0; -} +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Just a string"; /* message to be appeared on the screen */ + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); + printw("Try resizing your window(if possible) and then run this program again"); + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/basics/scanw_example.c b/basics/scanw_example.c index cc0bf7b..4a91faf 100644 --- a/basics/scanw_example.c +++ b/basics/scanw_example.c @@ -1,20 +1,20 @@ -#include /* ncurses.h includes stdio.h */ -#include - -int main() -{ - char mesg[]="Enter a string: "; /* message to be appeared on the screen */ - char str[80]; - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - getstr(str); - mvprintw(LINES - 2, 0, "You Entered: %s", str); - getch(); - endwin(); - - return 0; -} +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Enter a string: "; /* message to be appeared on the screen */ + char str[80]; + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + getstr(str); + mvprintw(LINES - 2, 0, "You Entered: %s", str); + getch(); + endwin(); + + return 0; +} diff --git a/basics/simple_attr.c b/basics/simple_attr.c index 3efef73..5b4b9ed 100644 --- a/basics/simple_attr.c +++ b/basics/simple_attr.c @@ -3,7 +3,7 @@ #include int main(int argc, char *argv[]) -{ +{ int ch, prev, row, col; prev = EOF; FILE *fp; @@ -20,35 +20,35 @@ int main(int argc, char *argv[]) perror("Cannot open input file"); exit(1); } - initscr(); /* Start curses mode */ - getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ - while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ + initscr(); /* Start curses mode */ + getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ + while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ { - getyx(stdscr, y, x); /* get the current curser position */ - if(y == (row - 1)) /* are we are at the end of the screen */ + getyx(stdscr, y, x); /* get the current curser position */ + if(y == (row - 1)) /* are we are at the end of the screen */ { - printw("<-Press Any Key->"); /* tell the user to press a key */ + printw("<-Press Any Key->"); /* tell the user to press a key */ getch(); - clear(); /* clear the screen */ - move(0, 0); /* start at the beginning of the screen */ + clear(); /* clear the screen */ + move(0, 0); /* start at the beginning of the screen */ } - if(prev == '/' && ch == '*') /* If it is / and * then only - * switch bold on */ + if(prev == '/' && ch == '*') /* If it is / and * then only + * switch bold on */ { - attron(A_BOLD); /* cut bold on */ - getyx(stdscr, y, x); /* get the current curser position */ - move(y, x - 1); /* back up one space */ - printw("%c%c", '/', ch); /* The actual printing is done here */ + attron(A_BOLD); /* cut bold on */ + getyx(stdscr, y, x); /* get the current curser position */ + move(y, x - 1); /* back up one space */ + printw("%c%c", '/', ch); /* The actual printing is done here */ } else printw("%c", ch); refresh(); if(prev == '*' && ch == '/') - attroff(A_BOLD); /* Switch it off once we got * - * and then / */ + attroff(A_BOLD); /* Switch it off once we got * + * and then / */ prev = ch; } - endwin(); /* End curses mode */ + endwin(); /* End curses mode */ fclose(fp); return 0; } diff --git a/basics/simple_color.c b/basics/simple_color.c index 59fb020..45223db 100644 --- a/basics/simple_color.c +++ b/basics/simple_color.c @@ -1,40 +1,40 @@ -#include - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); -int main(int argc, char *argv[]) -{ initscr(); /* Start curses mode */ - if(has_colors() == FALSE) - { endwin(); - printf("Your terminal does not support color\n"); - exit(1); - } - start_color(); /* Start color */ - init_pair(1, COLOR_RED, COLOR_BLACK); - - attron(COLOR_PAIR(1)); - print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); - attroff(COLOR_PAIR(1)); - getch(); - endwin(); -} -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} - +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + if(has_colors() == FALSE) + { endwin(); + printf("Your terminal does not support color\n"); + exit(1); + } + start_color(); /* Start color */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + attron(COLOR_PAIR(1)); + print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); + attroff(COLOR_PAIR(1)); + getch(); + endwin(); +} +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} + diff --git a/basics/simple_key.c b/basics/simple_key.c index 5197646..23ff44c 100644 --- a/basics/simple_key.c +++ b/basics/simple_key.c @@ -1,92 +1,92 @@ -#include -#include - -#define WIDTH 30 -#define HEIGHT 10 - -int startx = 0; -int starty = 0; - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; -int n_choices = sizeof(choices) / sizeof(char *); -void print_menu(WINDOW *menu_win, int highlight); - -int main() -{ WINDOW *menu_win; - int highlight = 1; - int choice = 0; - int c; - - initscr(); - clear(); - noecho(); - cbreak(); /* Line buffering disabled. pass on everything */ - startx = (80 - WIDTH) / 2; - starty = (24 - HEIGHT) / 2; - - menu_win = newwin(HEIGHT, WIDTH, starty, startx); - keypad(menu_win, TRUE); - mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); - refresh(); - print_menu(menu_win, highlight); - while(1) - { c = wgetch(menu_win); - switch(c) - { case KEY_UP: - if(highlight == 1) - highlight = n_choices; - else - --highlight; - break; - case KEY_DOWN: - if(highlight == n_choices) - highlight = 1; - else - ++highlight; - break; - case 10: - choice = highlight; - break; - default: - mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); - refresh(); - break; - } - print_menu(menu_win, highlight); - if(choice != 0) /* User did a choice come out of the infinite loop */ - break; - } - mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); - clrtoeol(); - refresh(); - endwin(); - return 0; -} - - -void print_menu(WINDOW *menu_win, int highlight) -{ - int x, y, i; - - x = 2; - y = 2; - box(menu_win, 0, 0); - for(i = 0; i < n_choices; ++i) - { if(highlight == i + 1) /* High light the present choice */ - { wattron(menu_win, A_REVERSE); - mvwprintw(menu_win, y, x, "%s", choices[i]); - wattroff(menu_win, A_REVERSE); - } - else - mvwprintw(menu_win, y, x, "%s", choices[i]); - ++y; - } - wrefresh(menu_win); -} - +#include +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; +int n_choices = sizeof(choices) / sizeof(char *); +void print_menu(WINDOW *menu_win, int highlight); + +int main() +{ WINDOW *menu_win; + int highlight = 1; + int choice = 0; + int c; + + initscr(); + clear(); + noecho(); + cbreak(); /* Line buffering disabled. pass on everything */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + keypad(menu_win, TRUE); + mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); + refresh(); + print_menu(menu_win, highlight); + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_UP: + if(highlight == 1) + highlight = n_choices; + else + --highlight; + break; + case KEY_DOWN: + if(highlight == n_choices) + highlight = 1; + else + ++highlight; + break; + case 10: + choice = highlight; + break; + default: + mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); + refresh(); + break; + } + print_menu(menu_win, highlight); + if(choice != 0) /* User did a choice come out of the infinite loop */ + break; + } + mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); + clrtoeol(); + refresh(); + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) /* High light the present choice */ + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + diff --git a/basics/temp_leave.c b/basics/temp_leave.c index fdcd5c5..8ba21c1 100644 --- a/basics/temp_leave.c +++ b/basics/temp_leave.c @@ -1,20 +1,20 @@ -#include - -int main() -{ - initscr(); /* Start curses mode */ - printw("Hello World !!!\n"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ - def_prog_mode(); /* Save the tty modes */ - endwin(); /* End curses mode temporarily */ - system("/bin/sh"); /* Do whatever you like in cooked mode */ - reset_prog_mode(); /* Return to the previous tty mode*/ - /* stored by def_prog_mode() */ - refresh(); /* Do refresh() to restore the */ - /* Screen contents */ - printw("Another String\n"); /* Back to curses use the full */ - refresh(); /* capabilities of curses */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!\n"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + def_prog_mode(); /* Save the tty modes */ + endwin(); /* End curses mode temporarily */ + system("/bin/sh"); /* Do whatever you like in cooked mode */ + reset_prog_mode(); /* Return to the previous tty mode*/ + /* stored by def_prog_mode() */ + refresh(); /* Do refresh() to restore the */ + /* Screen contents */ + printw("Another String\n"); /* Back to curses use the full */ + refresh(); /* capabilities of curses */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/win_border.c b/basics/win_border.c index 41e1a7e..b0e8d61 100644 --- a/basics/win_border.c +++ b/basics/win_border.c @@ -1,82 +1,82 @@ -#include - - -WINDOW *create_newwin(int height, int width, int starty, int startx); -void destroy_win(WINDOW *local_win); - -int main(int argc, char *argv[]) -{ WINDOW *my_win; - int startx, starty, width, height; - int ch; - - initscr(); /* Start curses mode */ - cbreak(); /* Line buffering disabled, Pass on - * everty thing to me */ - keypad(stdscr, TRUE); /* I need that nifty F1 */ - - height = 3; - width = 10; - starty = (LINES - height) / 2; /* Calculating for a center placement */ - startx = (COLS - width) / 2; /* of the window */ - printw("Press F1 to exit"); - refresh(); - my_win = create_newwin(height, width, starty, startx); - - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - destroy_win(my_win); - my_win = create_newwin(height, width, starty,--startx); - break; - case KEY_RIGHT: - destroy_win(my_win); - my_win = create_newwin(height, width, starty,++startx); - break; - case KEY_UP: - destroy_win(my_win); - my_win = create_newwin(height, width, --starty,startx); - break; - case KEY_DOWN: - destroy_win(my_win); - my_win = create_newwin(height, width, ++starty,startx); - break; - } - } - - endwin(); /* End curses mode */ - return 0; -} - -WINDOW *create_newwin(int height, int width, int starty, int startx) -{ WINDOW *local_win; - - local_win = newwin(height, width, starty, startx); - box(local_win, 0 , 0); /* 0, 0 gives default characters - * for the vertical and horizontal - * lines */ - wrefresh(local_win); /* Show that box */ - - return local_win; -} - -void destroy_win(WINDOW *local_win) -{ - /* box(local_win, ' ', ' '); : This won't produce the desired - * result of erasing the window. It will leave it's four corners - * and so an ugly remnant of window. - */ - wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); - /* The parameters taken are - * 1. win: the window on which to operate - * 2. ls: character to be used for the left side of the window - * 3. rs: character to be used for the right side of the window - * 4. ts: character to be used for the top side of the window - * 5. bs: character to be used for the bottom side of the window - * 6. tl: character to be used for the top left corner of the window - * 7. tr: character to be used for the top right corner of the window - * 8. bl: character to be used for the bottom left corner of the window - * 9. br: character to be used for the bottom right corner of the window - */ - wrefresh(local_win); - delwin(local_win); -} +#include + + +WINDOW *create_newwin(int height, int width, int starty, int startx); +void destroy_win(WINDOW *local_win); + +int main(int argc, char *argv[]) +{ WINDOW *my_win; + int startx, starty, width, height; + int ch; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + + height = 3; + width = 10; + starty = (LINES - height) / 2; /* Calculating for a center placement */ + startx = (COLS - width) / 2; /* of the window */ + printw("Press F1 to exit"); + refresh(); + my_win = create_newwin(height, width, starty, startx); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,--startx); + break; + case KEY_RIGHT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,++startx); + break; + case KEY_UP: + destroy_win(my_win); + my_win = create_newwin(height, width, --starty,startx); + break; + case KEY_DOWN: + destroy_win(my_win); + my_win = create_newwin(height, width, ++starty,startx); + break; + } + } + + endwin(); /* End curses mode */ + return 0; +} + +WINDOW *create_newwin(int height, int width, int starty, int startx) +{ WINDOW *local_win; + + local_win = newwin(height, width, starty, startx); + box(local_win, 0 , 0); /* 0, 0 gives default characters + * for the vertical and horizontal + * lines */ + wrefresh(local_win); /* Show that box */ + + return local_win; +} + +void destroy_win(WINDOW *local_win) +{ + /* box(local_win, ' ', ' '); : This won't produce the desired + * result of erasing the window. It will leave it's four corners + * and so an ugly remnant of window. + */ + wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); + /* The parameters taken are + * 1. win: the window on which to operate + * 2. ls: character to be used for the left side of the window + * 3. rs: character to be used for the right side of the window + * 4. ts: character to be used for the top side of the window + * 5. bs: character to be used for the bottom side of the window + * 6. tl: character to be used for the top left corner of the window + * 7. tr: character to be used for the top right corner of the window + * 8. bl: character to be used for the bottom left corner of the window + * 9. br: character to be used for the bottom right corner of the window + */ + wrefresh(local_win); + delwin(local_win); +} diff --git a/basics/with_chgat.c b/basics/with_chgat.c index 4255561..96e23d7 100644 --- a/basics/with_chgat.c +++ b/basics/with_chgat.c @@ -1,24 +1,24 @@ -#include - -int main(int argc, char *argv[]) -{ initscr(); /* Start curses mode */ - start_color(); /* Start color functionality */ - - init_pair(1, COLOR_CYAN, COLOR_BLACK); - printw("A Big string which i didn't care to type fully "); - mvchgat(0, 0, -1, A_BLINK, 1, NULL); - /* - * First two parameters specify the position at which to start - * Third parameter number of characters to update. -1 means till - * end of line - * Forth parameter is the normal attribute you wanted to give - * to the charcter - * Fifth is the color index. It is the index given during init_pair() - * use 0 if you didn't want color - * Sixth one is always NULL - */ - refresh(); - getch(); - endwin(); /* End curses mode */ - return 0; -} +#include + +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + start_color(); /* Start color functionality */ + + init_pair(1, COLOR_CYAN, COLOR_BLACK); + printw("A Big string which i didn't care to type fully "); + mvchgat(0, 0, -1, A_BLINK, 1, NULL); + /* + * First two parameters specify the position at which to start + * Third parameter number of characters to update. -1 means till + * end of line + * Forth parameter is the normal attribute you wanted to give + * to the charcter + * Fifth is the color index. It is the index given during init_pair() + * use 0 if you didn't want color + * Sixth one is always NULL + */ + refresh(); + getch(); + endwin(); /* End curses mode */ + return 0; +} diff --git a/forms/Makefile b/forms/Makefile index 4ed1b98..e120e37 100644 --- a/forms/Makefile +++ b/forms/Makefile @@ -9,19 +9,19 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/form_attrib\ - ${EXE_DIR}/form_options\ - ${EXE_DIR}/form_simple\ - ${EXE_DIR}/form_win \ + ${EXE_DIR}/form_attrib\ + ${EXE_DIR}/form_options\ + ${EXE_DIR}/form_simple\ + ${EXE_DIR}/form_win \ ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/forms/README b/forms/README index 6e9a218..d2c1b87 100644 --- a/forms/README +++ b/forms/README @@ -1,9 +1,9 @@ -Description of files +Description of files -------------------- - forms + forms | - |----> form_attrib.c -- Usage of field attributes - |----> form_options.c -- Usage of field options - |----> form_simple.c -- A simple form example - |----> form_win.c -- Demo of windows associated with forms + |----> form_attrib.c -- Usage of field attributes + |----> form_options.c -- Usage of field options + |----> form_simple.c -- A simple form example + |----> form_win.c -- Demo of windows associated with forms diff --git a/forms/form_attrib.c b/forms/form_attrib.c index 9974157..93e1632 100644 --- a/forms/form_attrib.c +++ b/forms/form_attrib.c @@ -1,75 +1,75 @@ -#include - -int main() -{ FIELD *field[3]; - FORM *my_form; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize few color pairs */ - init_pair(1, COLOR_WHITE, COLOR_BLUE); - init_pair(2, COLOR_WHITE, COLOR_BLUE); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 4, 18, 0, 0); - field[1] = new_field(1, 10, 6, 18, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ - set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ - /* are printed in white */ - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - set_current_field(my_form, field[0]); /* Set focus to the colored field */ - mvprintw(4, 10, "Value 1:"); - mvprintw(6, 10, "Value 2:"); - mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_WHITE, COLOR_BLUE); + init_pair(2, COLOR_WHITE, COLOR_BLUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ + set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ + /* are printed in white */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_current_field(my_form, field[0]); /* Set focus to the colored field */ + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_options.c b/forms/form_options.c index 5b67dcc..b9ef831 100644 --- a/forms/form_options.c +++ b/forms/form_options.c @@ -1,76 +1,76 @@ -#include - -#define STARTX 15 -#define STARTY 4 -#define WIDTH 25 - -#define N_FIELDS 3 - -int main() -{ FIELD *field[N_FIELDS]; - FORM *my_form; - int ch, i; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize the fields */ - for(i = 0; i < N_FIELDS - 1; ++i) - field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); - field[N_FIELDS - 1] = NULL; - - /* Set field options */ - set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ - - field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ - field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ - field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ - /* after last character is entered */ - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ - set_field_buffer(field[0], 0, "This is a static Field"); - /* Initialize the field */ - mvprintw(STARTY, STARTX - 10, "Field 1:"); - mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +#define STARTX 15 +#define STARTY 4 +#define WIDTH 25 + +#define N_FIELDS 3 + +int main() +{ FIELD *field[N_FIELDS]; + FORM *my_form; + int ch, i; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + for(i = 0; i < N_FIELDS - 1; ++i) + field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); + field[N_FIELDS - 1] = NULL; + + /* Set field options */ + set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ + + field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ + field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ + field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ + /* after last character is entered */ + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ + set_field_buffer(field[0], 0, "This is a static Field"); + /* Initialize the field */ + mvprintw(STARTY, STARTX - 10, "Field 1:"); + mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_simple.c b/forms/form_simple.c index 520fc92..63f7fe5 100644 --- a/forms/form_simple.c +++ b/forms/form_simple.c @@ -1,66 +1,66 @@ -#include - -int main() -{ FIELD *field[3]; - FORM *my_form; - int ch; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 4, 18, 0, 0); - field[1] = new_field(1, 10, 6, 18, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - mvprintw(4, 10, "Value 1:"); - mvprintw(6, 10, "Value 2:"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_win.c b/forms/form_win.c index fb00a46..8cd3b16 100644 --- a/forms/form_win.c +++ b/forms/form_win.c @@ -1,112 +1,112 @@ -#include - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ - FIELD *field[3]; - FORM *my_form; - WINDOW *my_form_win; - int ch, rows, cols; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize few color pairs */ - init_pair(1, COLOR_RED, COLOR_BLACK); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 6, 1, 0, 0); - field[1] = new_field(1, 10, 8, 1, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_back(field[0], A_UNDERLINE); - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - - /* Calculate the area required for the form */ - scale_form(my_form, &rows, &cols); - - /* Create the window to be associated with the form */ - my_form_win = newwin(rows + 4, cols + 4, 4, 4); - keypad(my_form_win, TRUE); - - /* Set main window and sub window */ - set_form_win(my_form, my_form_win); - set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); - - /* Print a border around the main window and print a title */ - box(my_form_win, 0, 0); - print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); - - post_form(my_form); - wrefresh(my_form_win); - - mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); - refresh(); - - /* Loop through to get user requests */ - while((ch = wgetch(my_form_win)) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ + FIELD *field[3]; + FORM *my_form; + WINDOW *my_form_win; + int ch, rows, cols; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 6, 1, 0, 0); + field[1] = new_field(1, 10, 8, 1, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + + /* Calculate the area required for the form */ + scale_form(my_form, &rows, &cols); + + /* Create the window to be associated with the form */ + my_form_win = newwin(rows + 4, cols + 4, 4, 4); + keypad(my_form_win, TRUE); + + /* Set main window and sub window */ + set_form_win(my_form, my_form_win); + set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); + + /* Print a border around the main window and print a title */ + box(my_form_win, 0, 0); + print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); + + post_form(my_form); + wrefresh(my_form_win); + + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = wgetch(my_form_win)) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/menus/Makefile b/menus/Makefile index 73626e7..b21f8b2 100644 --- a/menus/Makefile +++ b/menus/Makefile @@ -9,23 +9,23 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/menu_attrib\ - ${EXE_DIR}/menu_item_data\ - ${EXE_DIR}/menu_multi_column \ - ${EXE_DIR}/menu_scroll \ - ${EXE_DIR}/menu_simple \ - ${EXE_DIR}/menu_toggle \ - ${EXE_DIR}/menu_userptr \ - ${EXE_DIR}/menu_win + ${EXE_DIR}/menu_attrib\ + ${EXE_DIR}/menu_item_data\ + ${EXE_DIR}/menu_multi_column \ + ${EXE_DIR}/menu_scroll \ + ${EXE_DIR}/menu_simple \ + ${EXE_DIR}/menu_toggle \ + ${EXE_DIR}/menu_userptr \ + ${EXE_DIR}/menu_win ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/menus/README b/menus/README index 211bfa3..cc08f98 100644 --- a/menus/README +++ b/menus/README @@ -1,13 +1,13 @@ -Description of files +Description of files -------------------- - menus + menus | - |----> menu_attrib.c -- Usage of menu attributes - |----> menu_item_data.c -- Usage of item_name() etc.. functions - |----> menu_multi_column.c -- Creates multi columnar menus - |----> menu_scroll.c -- Demonstrates scrolling capability of menus - |----> menu_simple.c -- A simple menu accessed by arrow keys - |----> menu_toggle.c -- Creates multi valued menus and explains - | -- REQ_TOGGLE_ITEM - |----> menu_userptr.c -- Usage of user pointer - |----> menu_win.c -- Demo of windows associated with menus + |----> menu_attrib.c -- Usage of menu attributes + |----> menu_item_data.c -- Usage of item_name() etc.. functions + |----> menu_multi_column.c -- Creates multi columnar menus + |----> menu_scroll.c -- Demonstrates scrolling capability of menus + |----> menu_simple.c -- A simple menu accessed by arrow keys + |----> menu_toggle.c -- Creates multi valued menus and explains + | -- REQ_TOGGLE_ITEM + |----> menu_userptr.c -- Usage of user pointer + |----> menu_win.c -- Demo of windows associated with menus diff --git a/menus/menu_attrib.c b/menus/menu_attrib.c index fdfdd2c..56fad90 100644 --- a/menus/menu_attrib.c +++ b/menus/menu_attrib.c @@ -1,80 +1,80 @@ -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_MAGENTA, COLOR_BLACK); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - item_opts_off(my_items[3], O_SELECTABLE); - item_opts_off(my_items[6], O_SELECTABLE); - - /* Create menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Set fore ground and back ground of the menu */ - set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); - set_menu_back(my_menu, COLOR_PAIR(2)); - set_menu_grey(my_menu, COLOR_PAIR(3)); - - /* Post the menu */ - mvprintw(LINES - 3, 0, "Press to see the option selected"); - mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - move(20, 0); - clrtoeol(); - mvprintw(20, 0, "Item selected is : %s", - item_name(current_item(my_menu))); - pos_menu_cursor(my_menu); - break; - } - } - unpost_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - free_menu(my_menu); - endwin(); -} - +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + item_opts_off(my_items[3], O_SELECTABLE); + item_opts_off(my_items[6], O_SELECTABLE); + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set fore ground and back ground of the menu */ + set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); + set_menu_back(my_menu, COLOR_PAIR(2)); + set_menu_grey(my_menu, COLOR_PAIR(3)); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", + item_name(current_item(my_menu))); + pos_menu_cursor(my_menu); + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_item_data.c b/menus/menu_item_data.c index c51ebc1..d33998e 100644 --- a/menus/menu_item_data.c +++ b/menus/menu_item_data.c @@ -1,66 +1,66 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - cur_item = current_item(my_menu); - move(LINES - 2, 0); - clrtoeol(); - mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", - item_index(cur_item) + 1, item_name(cur_item), - item_description(cur_item)); - - refresh(); - pos_menu_cursor(my_menu); - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + cur_item = current_item(my_menu); + move(LINES - 2, 0); + clrtoeol(); + mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", + item_index(cur_item) + 1, item_name(cur_item), + item_description(cur_item)); + + refresh(); + pos_menu_cursor(my_menu); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_multi_column.c b/menus/menu_multi_column.c index d507610..1fa6e02 100644 --- a/menus/menu_multi_column.c +++ b/menus/menu_multi_column.c @@ -1,97 +1,97 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", - "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", - "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", - "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", - "Exit", - (char *)NULL, - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_CYAN, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Set menu option not to show the description */ - menu_opts_off(my_menu, O_SHOWDESC); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 70, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); - set_menu_format(my_menu, 5, 3); - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - - attron(COLOR_PAIR(2)); - mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); - mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); - attroff(COLOR_PAIR(2)); - refresh(); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case KEY_LEFT: - menu_driver(my_menu, REQ_LEFT_ITEM); - break; - case KEY_RIGHT: - menu_driver(my_menu, REQ_RIGHT_ITEM); - break; - case KEY_NPAGE: - menu_driver(my_menu, REQ_SCR_DPAGE); - break; - case KEY_PPAGE: - menu_driver(my_menu, REQ_SCR_UPAGE); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", + "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", + "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", + "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", + "Exit", + (char *)NULL, + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set menu option not to show the description */ + menu_opts_off(my_menu, O_SHOWDESC); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 70, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); + set_menu_format(my_menu, 5, 3); + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); + mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_LEFT: + menu_driver(my_menu, REQ_LEFT_ITEM); + break; + case KEY_RIGHT: + menu_driver(my_menu, REQ_RIGHT_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} diff --git a/menus/menu_scroll.c b/menus/menu_scroll.c index 2d9c627..40618f2 100644 --- a/menus/menu_scroll.c +++ b/menus/menu_scroll.c @@ -1,124 +1,124 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Choice 8", - "Choice 9", - "Choice 10", - "Exit", - (char *)NULL, - }; -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_CYAN, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 40, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); - set_menu_format(my_menu, 5, 1); - - /* Set menu mark to the string " * " */ - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); - mvwaddch(my_menu_win, 2, 0, ACS_LTEE); - mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); - mvwaddch(my_menu_win, 2, 39, ACS_RTEE); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - attron(COLOR_PAIR(2)); - mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); - mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); - attroff(COLOR_PAIR(2)); - refresh(); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case KEY_NPAGE: - menu_driver(my_menu, REQ_SCR_DPAGE); - break; - case KEY_PPAGE: - menu_driver(my_menu, REQ_SCR_UPAGE); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Choice 8", + "Choice 9", + "Choice 10", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + set_menu_format(my_menu, 5, 1); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); + mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/menus/menu_simple.c b/menus/menu_simple.c index 1222c7d..086fdf7 100644 --- a/menus/menu_simple.c +++ b/menus/menu_simple.c @@ -1,56 +1,56 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - mvprintw(LINES - 2, 0, "F1 to Exit"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + mvprintw(LINES - 2, 0, "F1 to Exit"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_toggle.c b/menus/menu_toggle.c index f6e8d7e..60a052f 100644 --- a/menus/menu_toggle.c +++ b/menus/menu_toggle.c @@ -1,84 +1,84 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - - /* Make the menu multi valued */ - menu_opts_off(my_menu, O_ONEVALUE); - - mvprintw(LINES - 3, 0, "Use to select or unselect an item."); - mvprintw(LINES - 2, 0, " to see presently selected items(F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case ' ': - menu_driver(my_menu, REQ_TOGGLE_ITEM); - break; - case 10: /* Enter */ - { char temp[200]; - ITEM **items; - - items = menu_items(my_menu); - temp[0] = '\0'; - for(i = 0; i < item_count(my_menu); ++i) - if(item_value(items[i]) == TRUE) - { strcat(temp, item_name(items[i])); - strcat(temp, " "); - } - move(20, 0); - clrtoeol(); - mvprintw(20, 0, temp); - refresh(); - } - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + + /* Make the menu multi valued */ + menu_opts_off(my_menu, O_ONEVALUE); + + mvprintw(LINES - 3, 0, "Use to select or unselect an item."); + mvprintw(LINES - 2, 0, " to see presently selected items(F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case ' ': + menu_driver(my_menu, REQ_TOGGLE_ITEM); + break; + case 10: /* Enter */ + { char temp[200]; + ITEM **items; + + items = menu_items(my_menu); + temp[0] = '\0'; + for(i = 0; i < item_count(my_menu); ++i) + if(item_value(items[i]) == TRUE) + { strcat(temp, item_name(items[i])); + strcat(temp, " "); + } + move(20, 0); + clrtoeol(); + mvprintw(20, 0, temp); + refresh(); + } + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_userptr.c b/menus/menu_userptr.c index baf6a6c..d2e02d4 100644 --- a/menus/menu_userptr.c +++ b/menus/menu_userptr.c @@ -1,87 +1,87 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; -void func(char *name); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_MAGENTA, COLOR_BLACK); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - { my_items[i] = new_item(choices[i], choices[i]); - /* Set the user pointer */ - set_item_userptr(my_items[i], func); - } - my_items[n_choices] = (ITEM *)NULL; - - /* Create menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Post the menu */ - mvprintw(LINES - 3, 0, "Press to see the option selected"); - mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - { ITEM *cur; - void (*p)(char *); - - cur = current_item(my_menu); - p = item_userptr(cur); - p((char *)item_name(cur)); - pos_menu_cursor(my_menu); - break; - } - break; - } - } - unpost_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - free_menu(my_menu); - endwin(); -} - -void func(char *name) -{ move(20, 0); - clrtoeol(); - mvprintw(20, 0, "Item selected is : %s", name); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; +void func(char *name); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + { my_items[i] = new_item(choices[i], choices[i]); + /* Set the user pointer */ + set_item_userptr(my_items[i], func); + } + my_items[n_choices] = (ITEM *)NULL; + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + { ITEM *cur; + void (*p)(char *); + + cur = current_item(my_menu); + p = item_userptr(cur); + p((char *)item_name(cur)); + pos_menu_cursor(my_menu); + break; + } + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + +void func(char *name) +{ move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", name); +} diff --git a/menus/menu_win.c b/menus/menu_win.c index 3514086..c3387ed 100644 --- a/menus/menu_win.c +++ b/menus/menu_win.c @@ -1,105 +1,105 @@ -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - (char *)NULL, - }; -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 40, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); - - /* Set menu mark to the string " * " */ - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); - mvwaddch(my_menu_win, 2, 0, ACS_LTEE); - mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); - mvwaddch(my_menu_win, 2, 39, ACS_RTEE); - mvprintw(LINES - 2, 0, "F1 to exit"); - refresh(); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + mvprintw(LINES - 2, 0, "F1 to exit"); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/Makefile b/panels/Makefile index 242df7c..41a7d88 100644 --- a/panels/Makefile +++ b/panels/Makefile @@ -9,19 +9,19 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/panel_browse \ - ${EXE_DIR}/panel_hide \ - ${EXE_DIR}/panel_resize \ - ${EXE_DIR}/panel_simple + ${EXE_DIR}/panel_browse \ + ${EXE_DIR}/panel_hide \ + ${EXE_DIR}/panel_resize \ + ${EXE_DIR}/panel_simple ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/panels/README b/panels/README index ed34678..90a3bb8 100644 --- a/panels/README +++ b/panels/README @@ -1,8 +1,8 @@ -Description of files +Description of files -------------------- - panels + panels | - |----> panel_browse.c -- Panel browsing through tab. Usage of user pointer - |----> panel_hide.c -- Hiding and Un hiding of panels - |----> panel_resize.c -- Moving and resizing of panels - |----> panel_simple.c -- A simple panel example + |----> panel_browse.c -- Panel browsing through tab. Usage of user pointer + |----> panel_hide.c -- Hiding and Un hiding of panels + |----> panel_resize.c -- Moving and resizing of panels + |----> panel_simple.c -- A simple panel example diff --git a/panels/panel_browse.c b/panels/panel_browse.c index 45fd839..013c066 100644 --- a/panels/panel_browse.c +++ b/panels/panel_browse.c @@ -1,117 +1,117 @@ -#include - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL *top; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Set up the user pointers to the next panel */ - set_panel_userptr(my_panels[0], my_panels[1]); - set_panel_userptr(my_panels[1], my_panels[2]); - set_panel_userptr(my_panels[2], my_panels[0]); - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - doupdate(); - - top = my_panels[2]; - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 9: - top = (PANEL *)panel_userptr(top); - top_panel(top); - break; - } - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL *top; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Set up the user pointers to the next panel */ + set_panel_userptr(my_panels[0], my_panels[1]); + set_panel_userptr(my_panels[1], my_panels[2]); + set_panel_userptr(my_panels[2], my_panels[0]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + top = my_panels[2]; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: + top = (PANEL *)panel_userptr(top); + top_panel(top); + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_hide.c b/panels/panel_hide.c index 40f6c94..457199c 100644 --- a/panels/panel_hide.c +++ b/panels/panel_hide.c @@ -1,156 +1,156 @@ -#include - -typedef struct _PANEL_DATA { - int hide; /* TRUE if panel is hidden */ -}PANEL_DATA; - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL_DATA panel_datas[3]; - PANEL_DATA *temp; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Initialize panel datas saying that nothing is hidden */ - panel_datas[0].hide = FALSE; - panel_datas[1].hide = FALSE; - panel_datas[2].hide = FALSE; - - set_panel_userptr(my_panels[0], &panel_datas[0]); - set_panel_userptr(my_panels[1], &panel_datas[1]); - set_panel_userptr(my_panels[2], &panel_datas[2]); - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); - mvprintw(LINES - 2, 0, "F1 to Exit"); - - attroff(COLOR_PAIR(4)); - doupdate(); - - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 'a': - temp = (PANEL_DATA *)panel_userptr(my_panels[0]); - if(temp->hide == FALSE) - { hide_panel(my_panels[0]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[0]); - temp->hide = FALSE; - } - break; - case 'b': - temp = (PANEL_DATA *)panel_userptr(my_panels[1]); - if(temp->hide == FALSE) - { hide_panel(my_panels[1]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[1]); - temp->hide = FALSE; - } - break; - case 'c': - temp = (PANEL_DATA *)panel_userptr(my_panels[2]); - if(temp->hide == FALSE) - { hide_panel(my_panels[2]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[2]); - temp->hide = FALSE; - } - break; - } - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +typedef struct _PANEL_DATA { + int hide; /* TRUE if panel is hidden */ +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA panel_datas[3]; + PANEL_DATA *temp; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Initialize panel datas saying that nothing is hidden */ + panel_datas[0].hide = FALSE; + panel_datas[1].hide = FALSE; + panel_datas[2].hide = FALSE; + + set_panel_userptr(my_panels[0], &panel_datas[0]); + set_panel_userptr(my_panels[1], &panel_datas[1]); + set_panel_userptr(my_panels[2], &panel_datas[2]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); + mvprintw(LINES - 2, 0, "F1 to Exit"); + + attroff(COLOR_PAIR(4)); + doupdate(); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 'a': + temp = (PANEL_DATA *)panel_userptr(my_panels[0]); + if(temp->hide == FALSE) + { hide_panel(my_panels[0]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[0]); + temp->hide = FALSE; + } + break; + case 'b': + temp = (PANEL_DATA *)panel_userptr(my_panels[1]); + if(temp->hide == FALSE) + { hide_panel(my_panels[1]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[1]); + temp->hide = FALSE; + } + break; + case 'c': + temp = (PANEL_DATA *)panel_userptr(my_panels[2]); + if(temp->hide == FALSE) + { hide_panel(my_panels[2]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[2]); + temp->hide = FALSE; + } + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_resize.c b/panels/panel_resize.c index 71d331f..e77ba0d 100644 --- a/panels/panel_resize.c +++ b/panels/panel_resize.c @@ -1,234 +1,234 @@ -#include - -typedef struct _PANEL_DATA { - int x, y, w, h; - char label[80]; - int label_color; - PANEL *next; -}PANEL_DATA; - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); -void set_user_ptrs(PANEL **panels, int n); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL_DATA *top; - PANEL *stack_top; - WINDOW *temp_win, *old_win; - int ch; - int newx, newy, neww, newh; - int size = FALSE, move = FALSE; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - set_user_ptrs(my_panels, 3); - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - doupdate(); - - stack_top = my_panels[2]; - top = (PANEL_DATA *)panel_userptr(stack_top); - newx = top->x; - newy = top->y; - neww = top->w; - newh = top->h; - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 9: /* Tab */ - top = (PANEL_DATA *)panel_userptr(stack_top); - top_panel(top->next); - stack_top = top->next; - top = (PANEL_DATA *)panel_userptr(stack_top); - newx = top->x; - newy = top->y; - neww = top->w; - newh = top->h; - break; - case 'r': /* Re-Size*/ - size = TRUE; - attron(COLOR_PAIR(4)); - mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press to end resizing"); - refresh(); - attroff(COLOR_PAIR(4)); - break; - case 'm': /* Move */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press to end moving"); - refresh(); - attroff(COLOR_PAIR(4)); - move = TRUE; - break; - case KEY_LEFT: - if(size == TRUE) - { --newx; - ++neww; - } - if(move == TRUE) - --newx; - break; - case KEY_RIGHT: - if(size == TRUE) - { ++newx; - --neww; - } - if(move == TRUE) - ++newx; - break; - case KEY_UP: - if(size == TRUE) - { --newy; - ++newh; - } - if(move == TRUE) - --newy; - break; - case KEY_DOWN: - if(size == TRUE) - { ++newy; - --newh; - } - if(move == TRUE) - ++newy; - break; - case 10: /* Enter */ - move(LINES - 4, 0); - clrtoeol(); - refresh(); - if(size == TRUE) - { old_win = panel_window(stack_top); - temp_win = newwin(newh, neww, newy, newx); - replace_panel(stack_top, temp_win); - win_show(temp_win, top->label, top->label_color); - delwin(old_win); - size = FALSE; - } - if(move == TRUE) - { move_panel(stack_top, newy, newx); - move = FALSE; - } - break; - - } - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - refresh(); - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Set the PANEL_DATA structures for individual panels */ -void set_user_ptrs(PANEL **panels, int n) -{ PANEL_DATA *ptrs; - WINDOW *win; - int x, y, w, h, i; - char temp[80]; - - ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); - - for(i = 0;i < n; ++i) - { win = panel_window(panels[i]); - getbegyx(win, y, x); - getmaxyx(win, h, w); - ptrs[i].x = x; - ptrs[i].y = y; - ptrs[i].w = w; - ptrs[i].h = h; - sprintf(temp, "Window Number %d", i + 1); - strcpy(ptrs[i].label, temp); - ptrs[i].label_color = i + 1; - if(i + 1 == n) - ptrs[i].next = panels[0]; - else - ptrs[i].next = panels[i + 1]; - set_panel_userptr(panels[i], &ptrs[i]); - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +typedef struct _PANEL_DATA { + int x, y, w, h; + char label[80]; + int label_color; + PANEL *next; +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); +void set_user_ptrs(PANEL **panels, int n); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA *top; + PANEL *stack_top; + WINDOW *temp_win, *old_win; + int ch; + int newx, newy, neww, newh; + int size = FALSE, move = FALSE; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + set_user_ptrs(my_panels, 3); + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + stack_top = my_panels[2]; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: /* Tab */ + top = (PANEL_DATA *)panel_userptr(stack_top); + top_panel(top->next); + stack_top = top->next; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + break; + case 'r': /* Re-Size*/ + size = TRUE; + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press to end resizing"); + refresh(); + attroff(COLOR_PAIR(4)); + break; + case 'm': /* Move */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press to end moving"); + refresh(); + attroff(COLOR_PAIR(4)); + move = TRUE; + break; + case KEY_LEFT: + if(size == TRUE) + { --newx; + ++neww; + } + if(move == TRUE) + --newx; + break; + case KEY_RIGHT: + if(size == TRUE) + { ++newx; + --neww; + } + if(move == TRUE) + ++newx; + break; + case KEY_UP: + if(size == TRUE) + { --newy; + ++newh; + } + if(move == TRUE) + --newy; + break; + case KEY_DOWN: + if(size == TRUE) + { ++newy; + --newh; + } + if(move == TRUE) + ++newy; + break; + case 10: /* Enter */ + move(LINES - 4, 0); + clrtoeol(); + refresh(); + if(size == TRUE) + { old_win = panel_window(stack_top); + temp_win = newwin(newh, neww, newy, newx); + replace_panel(stack_top, temp_win); + win_show(temp_win, top->label, top->label_color); + delwin(old_win); + size = FALSE; + } + if(move == TRUE) + { move_panel(stack_top, newy, newx); + move = FALSE; + } + break; + + } + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + refresh(); + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Set the PANEL_DATA structures for individual panels */ +void set_user_ptrs(PANEL **panels, int n) +{ PANEL_DATA *ptrs; + WINDOW *win; + int x, y, w, h, i; + char temp[80]; + + ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); + + for(i = 0;i < n; ++i) + { win = panel_window(panels[i]); + getbegyx(win, y, x); + getmaxyx(win, h, w); + ptrs[i].x = x; + ptrs[i].y = y; + ptrs[i].w = w; + ptrs[i].h = h; + sprintf(temp, "Window Number %d", i + 1); + strcpy(ptrs[i].label, temp); + ptrs[i].label_color = i + 1; + if(i + 1 == n) + ptrs[i].next = panels[0]; + else + ptrs[i].next = panels[i + 1]; + set_panel_userptr(panels[i], &ptrs[i]); + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_simple.c b/panels/panel_simple.c index d98aeef..2ce8c18 100644 --- a/panels/panel_simple.c +++ b/panels/panel_simple.c @@ -1,38 +1,38 @@ -#include - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - int lines = 10, cols = 40, y = 2, x = 4, i; - - initscr(); - cbreak(); - noecho(); - - /* Create windows for the panels */ - my_wins[0] = newwin(lines, cols, y, x); - my_wins[1] = newwin(lines, cols, y + 1, x + 5); - my_wins[2] = newwin(lines, cols, y + 2, x + 10); - - /* - * Create borders around the windows so that you can see the effect - * of panels - */ - for(i = 0; i < 3; ++i) - box(my_wins[i], 0, 0); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - doupdate(); - - getch(); - endwin(); -} - +#include + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + int lines = 10, cols = 40, y = 2, x = 4, i; + + initscr(); + cbreak(); + noecho(); + + /* Create windows for the panels */ + my_wins[0] = newwin(lines, cols, y, x); + my_wins[1] = newwin(lines, cols, y + 1, x + 5); + my_wins[2] = newwin(lines, cols, y + 2, x + 10); + + /* + * Create borders around the windows so that you can see the effect + * of panels + */ + for(i = 0; i < 3; ++i) + box(my_wins[i], 0, 0); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + doupdate(); + + getch(); + endwin(); +} + diff --git a/perl/05.pl b/perl/05.pl index dc61845..dd08535 100755 --- a/perl/05.pl +++ b/perl/05.pl @@ -20,9 +20,9 @@ $lines = join "", @lines; while ($lines =~ /\G(.*?)(\/\*.*?\*\/)?/gs) { addstr($1); if ($2) { - attron(A_BOLD); - addstr($2); - attroff(A_BOLD); + attron(A_BOLD); + addstr($2); + attroff(A_BOLD); } } diff --git a/perl/07.pl b/perl/07.pl index 06a7549..eea4cd8 100755 --- a/perl/07.pl +++ b/perl/07.pl @@ -23,20 +23,20 @@ $my_win = create_newwin($height, $width, $starty, $startx); while (($ch = getch()) != KEY_F(1)) { if ($ch == KEY_LEFT) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, $starty, --$startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, $starty, --$startx); } elsif ($ch == KEY_RIGHT) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, $starty, ++$startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, $starty, ++$startx); } elsif ($ch == KEY_UP) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, --$starty, $startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, --$starty, $startx); } elsif ($ch == KEY_DOWN) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, ++$starty, $startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, ++$starty, $startx); } } diff --git a/perl/08.pl b/perl/08.pl index 4eb044f..37077ec 100755 --- a/perl/08.pl +++ b/perl/08.pl @@ -32,24 +32,24 @@ create_box(\%win, 1); while (($ch = getch()) != KEY_F(1)) { if ($ch == KEY_LEFT) { - create_box(\%win, 0); - $win{'startx'}--; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'startx'}--; + create_box(\%win, 1); } elsif ($ch == KEY_RIGHT) { - create_box(\%win, 0); - $win{'startx'}++; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'startx'}++; + create_box(\%win, 1); } elsif ($ch == KEY_UP) { - create_box(\%win, 0); - $win{'starty'}--; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'starty'}--; + create_box(\%win, 1); } elsif ($ch == KEY_DOWN) { - create_box(\%win, 0); - $win{'starty'}++; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'starty'}++; + create_box(\%win, 1); } } @@ -74,10 +74,10 @@ sub init_win_params { sub print_win_params { if ($_DEBUG) { - $p_win = shift; - addstr(25, 0, - "$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}"); - refresh(); + $p_win = shift; + addstr(25, 0, + "$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}"); + refresh(); } } @@ -91,21 +91,21 @@ sub create_box { $h = $$p_win{'height'}; if ($bool) { - addch($y, $x, $$p_win{'tl'}); - addch($y, $x + $w, $$p_win{'tr'}); - addch($y + $h, $x, $$p_win{'bl'}); - addch($y + $h, $x + $w, $$p_win{'br'}); - hline($y, $x + 1, $$p_win{'ts'}, $w - 1); - hline($y + $h, $x + 1, $$p_win{'bs'}, $w - 1); - vline($y + 1, $x, $$p_win{'ls'}, $h - 1); - vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1); + addch($y, $x, $$p_win{'tl'}); + addch($y, $x + $w, $$p_win{'tr'}); + addch($y + $h, $x, $$p_win{'bl'}); + addch($y + $h, $x + $w, $$p_win{'br'}); + hline($y, $x + 1, $$p_win{'ts'}, $w - 1); + hline($y + $h, $x + 1, $$p_win{'bs'}, $w - 1); + vline($y + 1, $x, $$p_win{'ls'}, $h - 1); + vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1); } else { - for ($j = $y; $j <= $y + $h; $j++) { - for ($i = $x; $i <= $x + $w; $i++) { - addch($j, $i, ' '); - } - } + for ($j = $y; $j <= $y + $h; $j++) { + for ($i = $x; $i <= $x + $w; $i++) { + addch($j, $i, ' '); + } + } } refresh(); } diff --git a/perl/09.pl b/perl/09.pl index 235fc82..c89af93 100755 --- a/perl/09.pl +++ b/perl/09.pl @@ -34,7 +34,7 @@ sub print_in_middle { $win = stdscr unless ($win); getyx($win, $y, $x); - + $x = $startx if ($startx); $y = $starty if ($starty); $width = $COLS unless ($width); diff --git a/perl/10.pl b/perl/10.pl index 7353ddb..1a59d50 100755 --- a/perl/10.pl +++ b/perl/10.pl @@ -44,27 +44,27 @@ print_menu($menu_win, $highlight); while (1) { $c = getch($menu_win); if ($c == KEY_UP) { - if ($highlight == 1) { - $highlight = $n_choices; - } - else { - $highlight--; - } + if ($highlight == 1) { + $highlight = $n_choices; + } + else { + $highlight--; + } } elsif ($c == KEY_DOWN) { - if ($highlight == $n_choices) { - $highlight = 1; - } - else { - $highlight++; - } + if ($highlight == $n_choices) { + $highlight = 1; + } + else { + $highlight++; + } } elsif ($c == '\n') { - $choice = $highlight; + $choice = $highlight; } else { - addstr($LINES - 2, 0, "Character pressed is $c"); - refresh(); + addstr($LINES - 2, 0, "Character pressed is $c"); + refresh(); } print_menu($menu_win, $highlight); last if ($choice); @@ -83,15 +83,15 @@ sub print_menu { $y = 2; box($menu_win, 0, 0); for ($i = 0; $i < $n_choices; $i++) { - if ($highlight == $i + 1) { - attron($menu_win, A_REVERSE); - addstr($menu_win, $y, $x, $choices[$i]); - attroff($menu_win, A_REVERSE); - } - else { - addstr($menu_win, $y, $x, $choices[$i]); - } - $y++; + if ($highlight == $i + 1) { + attron($menu_win, A_REVERSE); + addstr($menu_win, $y, $x, $choices[$i]); + attroff($menu_win, A_REVERSE); + } + else { + addstr($menu_win, $y, $x, $choices[$i]); + } + $y++; } refresh($menu_win); } From c77722c60f3fd871ca6a2eb014dad68de2f070b2 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:37:27 -0500 Subject: [PATCH 05/21] Revert "doing some find+sed cleanup" This reverts commit f4c7e129df8cdab960a08987930cf85f0e5b88b5. --- JustForFun/Makefile | 18 +- JustForFun/README | 14 +- JustForFun/hanoi.c | 356 ++++++++++++++-------------- JustForFun/life.c | 214 ++++++++--------- JustForFun/magic.c | 322 ++++++++++++------------- JustForFun/queens.c | 244 +++++++++---------- JustForFun/shuffle.c | 410 ++++++++++++++++---------------- JustForFun/tt.c | 446 +++++++++++++++++------------------ basics/Makefile | 30 +-- basics/README | 34 +-- basics/acs_vars.c | 88 +++---- basics/hello_world.c | 24 +- basics/init_func_example.c | 62 ++--- basics/key_code.c | 28 +-- basics/mouse_menu.c | 212 ++++++++--------- basics/other_border.c | 240 +++++++++---------- basics/printw_example.c | 40 ++-- basics/scanw_example.c | 40 ++-- basics/simple_attr.c | 36 +-- basics/simple_color.c | 80 +++---- basics/simple_key.c | 184 +++++++-------- basics/temp_leave.c | 40 ++-- basics/win_border.c | 164 ++++++------- basics/with_chgat.c | 48 ++-- forms/Makefile | 14 +- forms/README | 12 +- forms/form_attrib.c | 150 ++++++------ forms/form_options.c | 152 ++++++------ forms/form_simple.c | 132 +++++------ forms/form_win.c | 224 +++++++++--------- menus/Makefile | 22 +- menus/README | 22 +- menus/menu_attrib.c | 160 ++++++------- menus/menu_item_data.c | 132 +++++------ menus/menu_multi_column.c | 194 +++++++-------- menus/menu_scroll.c | 248 ++++++++++---------- menus/menu_simple.c | 112 ++++----- menus/menu_toggle.c | 168 ++++++------- menus/menu_userptr.c | 174 +++++++------- menus/menu_win.c | 210 ++++++++--------- panels/Makefile | 14 +- panels/README | 12 +- panels/panel_browse.c | 234 +++++++++---------- panels/panel_hide.c | 312 ++++++++++++------------- panels/panel_resize.c | 468 ++++++++++++++++++------------------- panels/panel_simple.c | 76 +++--- perl/05.pl | 6 +- perl/07.pl | 16 +- perl/08.pl | 58 ++--- perl/09.pl | 2 +- perl/10.pl | 48 ++-- 51 files changed, 3373 insertions(+), 3373 deletions(-) diff --git a/JustForFun/Makefile b/JustForFun/Makefile index b0a1058..3f0890e 100644 --- a/JustForFun/Makefile +++ b/JustForFun/Makefile @@ -9,20 +9,20 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/hanoi \ - ${EXE_DIR}/life\ - ${EXE_DIR}/magic \ - ${EXE_DIR}/queens \ - ${EXE_DIR}/shuffle \ - ${EXE_DIR}/tt + ${EXE_DIR}/hanoi \ + ${EXE_DIR}/life\ + ${EXE_DIR}/magic \ + ${EXE_DIR}/queens \ + ${EXE_DIR}/shuffle \ + ${EXE_DIR}/tt ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/JustForFun/README b/JustForFun/README index 5355c9c..a5d743f 100644 --- a/JustForFun/README +++ b/JustForFun/README @@ -1,10 +1,10 @@ -Description of files +Description of files -------------------- JustForFun | - |----> hanoi.c -- The Towers of Hanoi Solver - |----> life.c -- The Game of Life demo - |----> magic.c -- An Odd Order Magic Square builder - |----> queens.c -- The famous N-Queens Solver - |----> shuffle.c -- A fun game, if you have time to kill - |----> tt.c -- A very trivial typing tutor + |----> hanoi.c -- The Towers of Hanoi Solver + |----> life.c -- The Game of Life demo + |----> magic.c -- An Odd Order Magic Square builder + |----> queens.c -- The famous N-Queens Solver + |----> shuffle.c -- A fun game, if you have time to kill + |----> tt.c -- A very trivial typing tutor diff --git a/JustForFun/hanoi.c b/JustForFun/hanoi.c index 4085a93..b57b0b7 100644 --- a/JustForFun/hanoi.c +++ b/JustForFun/hanoi.c @@ -1,178 +1,178 @@ -#include - -#define POSX 10 -#define POSY 5 -#define DISC_CHAR '*' -#define PEG_CHAR '#' -#define TIME_OUT 300 - -typedef struct _peg_struct { - int n_discs; /* Number of discs at present */ - int bottomx, bottomy; /* bottom x, bottom y co-ord */ - int *sizes; /* The disc sizes array */ -}peg; - -void init_pegs(peg *p_my_pegs, int n_discs); -void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs); -void free_pegs(peg *p_my_pegs, int n_discs); -void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst); -void move_disc(peg *p_my_pegs, int n_discs, int src, int dst); -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); -void check_usr_response(peg *p_my_pegs, int n_discs); - -int store_n_discs; -char *welcome_string = "Enter the number of discs you want to be solved: "; - -int main(int argc, char *argv[]) -{ int n_discs; - peg my_pegs[3]; - - initscr(); /* Start curses mode */ - cbreak(); /* Line buffering disabled. Pass on every thing */ - keypad(stdscr, TRUE); - curs_set(FALSE); - - print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); - scanw("%d", &n_discs); - - timeout(TIME_OUT); - noecho(); - store_n_discs = n_discs; - - init_pegs(my_pegs, n_discs); - show_pegs(stdscr, my_pegs, n_discs); - solve_hanoi(my_pegs, n_discs, 0, 1, 2); - - free_pegs(my_pegs, n_discs); - endwin(); /* End curses mode */ - return 0; -} - -void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) -{ if(n_discs == 0) - return; - solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); - move_disc(p_my_pegs, store_n_discs, src, dst); - show_pegs(stdscr, p_my_pegs, store_n_discs); - check_usr_response(p_my_pegs, store_n_discs); - solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst); -} - -void check_usr_response(peg *p_my_pegs, int n_discs) -{ int ch; - - ch = getch(); /* Waits for TIME_OUT milliseconds */ - if(ch == ERR) - return; - else - if(ch == KEY_F(1)) - { free_pegs(p_my_pegs, n_discs); - endwin(); - exit(0); - } -} - -void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) -{ int temp, index; - - --p_my_pegs[src].n_discs; - index = 0; - while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) - ++index; - temp = p_my_pegs[src].sizes[index]; - p_my_pegs[src].sizes[index] = 0; - - index = 0; - while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) - ++index; - --index; - p_my_pegs[dst].sizes[index] = temp; - ++p_my_pegs[dst].n_discs; -} - -void init_pegs(peg *p_my_pegs, int n_discs) -{ int size, temp, i; - - p_my_pegs[0].n_discs = n_discs; - - /* Allocate memory for size array - * atmost the number of discs on a peg can be n_discs - */ - for(i = 0; i < n_discs; ++i) - p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); - size = 3; - for(i = 0;i < n_discs; ++i, size += 2) - p_my_pegs[0].sizes[i] = size; - - temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); - p_my_pegs[0].bottomx = POSX + 1 + temp; - p_my_pegs[0].bottomy = POSY + 2 + n_discs; - - p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp; - p_my_pegs[1].bottomy = POSY + 2 + n_discs; - - p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp; - p_my_pegs[2].bottomy = POSY + 2 + n_discs; -} - -void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) -{ int i, j, k, x, y, size; - - wclear(win); - attron(A_REVERSE); - mvprintw(24, 0, "Press F1 to Exit"); - attroff(A_REVERSE); - for(i = 0;i < 3; ++i) - mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, - p_my_pegs[i].bottomx, "%c", PEG_CHAR); - y = p_my_pegs[0].bottomy - n_discs; - for(i = 0; i < 3; ++i) /* For each peg */ - { for(j = 0; j < n_discs; ++ j) /* For each row */ - { if(p_my_pegs[i].sizes[j] != 0) - { size = p_my_pegs[i].sizes[j]; - x = p_my_pegs[i].bottomx - (size / 2); - for(k = 0; k < size; ++k) - mvwprintw(win, y, x + k, "%c", DISC_CHAR); - } - else - mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); - ++y; - } - y = p_my_pegs[0].bottomy - n_discs; - } - wrefresh(win); -} - -void free_pegs(peg *p_my_pegs, int n_discs) -{ int i; - - for(i = 0;i < n_discs; ++i) - free(p_my_pegs[i].sizes); -} - -/* -------------------------------------------------------------* - * startx = 0 means at present x * - * starty = 0 means at present y * - * win = NULL means take stdscr * - * -------------------------------------------------------------*/ - -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} +#include + +#define POSX 10 +#define POSY 5 +#define DISC_CHAR '*' +#define PEG_CHAR '#' +#define TIME_OUT 300 + +typedef struct _peg_struct { + int n_discs; /* Number of discs at present */ + int bottomx, bottomy; /* bottom x, bottom y co-ord */ + int *sizes; /* The disc sizes array */ +}peg; + +void init_pegs(peg *p_my_pegs, int n_discs); +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs); +void free_pegs(peg *p_my_pegs, int n_discs); +void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst); +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); +void check_usr_response(peg *p_my_pegs, int n_discs); + +int store_n_discs; +char *welcome_string = "Enter the number of discs you want to be solved: "; + +int main(int argc, char *argv[]) +{ int n_discs; + peg my_pegs[3]; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled. Pass on every thing */ + keypad(stdscr, TRUE); + curs_set(FALSE); + + print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); + scanw("%d", &n_discs); + + timeout(TIME_OUT); + noecho(); + store_n_discs = n_discs; + + init_pegs(my_pegs, n_discs); + show_pegs(stdscr, my_pegs, n_discs); + solve_hanoi(my_pegs, n_discs, 0, 1, 2); + + free_pegs(my_pegs, n_discs); + endwin(); /* End curses mode */ + return 0; +} + +void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) +{ if(n_discs == 0) + return; + solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); + move_disc(p_my_pegs, store_n_discs, src, dst); + show_pegs(stdscr, p_my_pegs, store_n_discs); + check_usr_response(p_my_pegs, store_n_discs); + solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst); +} + +void check_usr_response(peg *p_my_pegs, int n_discs) +{ int ch; + + ch = getch(); /* Waits for TIME_OUT milliseconds */ + if(ch == ERR) + return; + else + if(ch == KEY_F(1)) + { free_pegs(p_my_pegs, n_discs); + endwin(); + exit(0); + } +} + +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) +{ int temp, index; + + --p_my_pegs[src].n_discs; + index = 0; + while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) + ++index; + temp = p_my_pegs[src].sizes[index]; + p_my_pegs[src].sizes[index] = 0; + + index = 0; + while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) + ++index; + --index; + p_my_pegs[dst].sizes[index] = temp; + ++p_my_pegs[dst].n_discs; +} + +void init_pegs(peg *p_my_pegs, int n_discs) +{ int size, temp, i; + + p_my_pegs[0].n_discs = n_discs; + + /* Allocate memory for size array + * atmost the number of discs on a peg can be n_discs + */ + for(i = 0; i < n_discs; ++i) + p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); + size = 3; + for(i = 0;i < n_discs; ++i, size += 2) + p_my_pegs[0].sizes[i] = size; + + temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); + p_my_pegs[0].bottomx = POSX + 1 + temp; + p_my_pegs[0].bottomy = POSY + 2 + n_discs; + + p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp; + p_my_pegs[1].bottomy = POSY + 2 + n_discs; + + p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp; + p_my_pegs[2].bottomy = POSY + 2 + n_discs; +} + +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) +{ int i, j, k, x, y, size; + + wclear(win); + attron(A_REVERSE); + mvprintw(24, 0, "Press F1 to Exit"); + attroff(A_REVERSE); + for(i = 0;i < 3; ++i) + mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, + p_my_pegs[i].bottomx, "%c", PEG_CHAR); + y = p_my_pegs[0].bottomy - n_discs; + for(i = 0; i < 3; ++i) /* For each peg */ + { for(j = 0; j < n_discs; ++ j) /* For each row */ + { if(p_my_pegs[i].sizes[j] != 0) + { size = p_my_pegs[i].sizes[j]; + x = p_my_pegs[i].bottomx - (size / 2); + for(k = 0; k < size; ++k) + mvwprintw(win, y, x + k, "%c", DISC_CHAR); + } + else + mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); + ++y; + } + y = p_my_pegs[0].bottomy - n_discs; + } + wrefresh(win); +} + +void free_pegs(peg *p_my_pegs, int n_discs) +{ int i; + + for(i = 0;i < n_discs; ++i) + free(p_my_pegs[i].sizes); +} + +/* -------------------------------------------------------------* + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * -------------------------------------------------------------*/ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/JustForFun/life.c b/JustForFun/life.c index 99da8a3..eb6a039 100644 --- a/JustForFun/life.c +++ b/JustForFun/life.c @@ -1,107 +1,107 @@ -#include - -int STARTX = 0; -int STARTY = 0; -int ENDX = 79; -int ENDY = 24; - -#define CELL_CHAR '#' -#define TIME_OUT 300 - -typedef struct _state { - int oldstate; - int newstate; -}state; - -void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); -void calc(state **area, int x, int y); -void update_state(state **area, int startx, int starty, int endx, int endy); - -int main() -{ state **workarea; - int i, j; - - initscr(); - cbreak(); - timeout(TIME_OUT); - keypad(stdscr, TRUE); - - ENDX = COLS - 1; - ENDY = LINES - 1; - - workarea = (state **)calloc(COLS, sizeof(state *)); - for(i = 0;i < COLS; ++i) - workarea[i] = (state *)calloc(LINES, sizeof(state)); - - /* For inverted U */ - workarea[39][15].newstate = TRUE; - workarea[40][15].newstate = TRUE; - workarea[41][15].newstate = TRUE; - workarea[39][16].newstate = TRUE; - workarea[39][17].newstate = TRUE; - workarea[41][16].newstate = TRUE; - workarea[41][17].newstate = TRUE; - update_state(workarea, STARTX, STARTY, ENDX, ENDY); - - /* For block */ -/* - workarea[37][13].newstate = TRUE; - workarea[37][14].newstate = TRUE; - workarea[38][13].newstate = TRUE; - workarea[38][14].newstate = TRUE; - - update_state(workarea, STARTX, STARTY, ENDX, ENDY); -*/ - display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); - while(getch() != KEY_F(1)) - { for(i = STARTX; i <= ENDX; ++i) - for(j = STARTY; j <= ENDY; ++j) - calc(workarea, i, j); - update_state(workarea, STARTX, STARTY, ENDX, ENDY); - display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); - } - - endwin(); - return 0; -} - -void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) -{ int i, j; - wclear(win); - for(i = startx; i <= endx; ++i) - for(j = starty;j <= endy; ++j) - if(area[i][j].newstate == TRUE) - mvwaddch(win, j, i, CELL_CHAR); - wrefresh(win); -} - -void calc(state **area, int i, int j) -{ int neighbours; - int newstate; - - neighbours = - area[(i - 1 + COLS) % COLS][j].oldstate + - area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate + - area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + - area[(i + 1) % COLS][j].oldstate + - area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + - area[(i + 1) % COLS][(j + 1) % LINES].oldstate + - area[i][(j - 1 + LINES) % LINES].oldstate + - area[i][(j + 1) % LINES].oldstate; - - newstate = FALSE; - if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) - newstate = TRUE; - else - if(area[i][j].oldstate == FALSE && neighbours == 3) - newstate = TRUE; - area[i][j].newstate = newstate; -} - -void update_state(state **area, int startx, int starty, int endx, int endy) -{ int i, j; - - for(i = startx; i <= endx; ++i) - for(j = starty; j <= endy; ++j) - area[i][j].oldstate = area[i][j].newstate; -} +#include + +int STARTX = 0; +int STARTY = 0; +int ENDX = 79; +int ENDY = 24; + +#define CELL_CHAR '#' +#define TIME_OUT 300 + +typedef struct _state { + int oldstate; + int newstate; +}state; + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); +void calc(state **area, int x, int y); +void update_state(state **area, int startx, int starty, int endx, int endy); + +int main() +{ state **workarea; + int i, j; + + initscr(); + cbreak(); + timeout(TIME_OUT); + keypad(stdscr, TRUE); + + ENDX = COLS - 1; + ENDY = LINES - 1; + + workarea = (state **)calloc(COLS, sizeof(state *)); + for(i = 0;i < COLS; ++i) + workarea[i] = (state *)calloc(LINES, sizeof(state)); + + /* For inverted U */ + workarea[39][15].newstate = TRUE; + workarea[40][15].newstate = TRUE; + workarea[41][15].newstate = TRUE; + workarea[39][16].newstate = TRUE; + workarea[39][17].newstate = TRUE; + workarea[41][16].newstate = TRUE; + workarea[41][17].newstate = TRUE; + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + + /* For block */ +/* + workarea[37][13].newstate = TRUE; + workarea[37][14].newstate = TRUE; + workarea[38][13].newstate = TRUE; + workarea[38][14].newstate = TRUE; + + update_state(workarea, STARTX, STARTY, ENDX, ENDY); +*/ + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + while(getch() != KEY_F(1)) + { for(i = STARTX; i <= ENDX; ++i) + for(j = STARTY; j <= ENDY; ++j) + calc(workarea, i, j); + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + } + + endwin(); + return 0; +} + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) +{ int i, j; + wclear(win); + for(i = startx; i <= endx; ++i) + for(j = starty;j <= endy; ++j) + if(area[i][j].newstate == TRUE) + mvwaddch(win, j, i, CELL_CHAR); + wrefresh(win); +} + +void calc(state **area, int i, int j) +{ int neighbours; + int newstate; + + neighbours = + area[(i - 1 + COLS) % COLS][j].oldstate + + area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + + area[(i + 1) % COLS][j].oldstate + + area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i + 1) % COLS][(j + 1) % LINES].oldstate + + area[i][(j - 1 + LINES) % LINES].oldstate + + area[i][(j + 1) % LINES].oldstate; + + newstate = FALSE; + if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) + newstate = TRUE; + else + if(area[i][j].oldstate == FALSE && neighbours == 3) + newstate = TRUE; + area[i][j].newstate = newstate; +} + +void update_state(state **area, int startx, int starty, int endx, int endy) +{ int i, j; + + for(i = startx; i <= endx; ++i) + for(j = starty; j <= endy; ++j) + area[i][j].oldstate = area[i][j].newstate; +} diff --git a/JustForFun/magic.c b/JustForFun/magic.c index c93b5d8..6561941 100644 --- a/JustForFun/magic.c +++ b/JustForFun/magic.c @@ -1,161 +1,161 @@ -#include -#include - -#define STARTX 9 -#define STARTY 3 -#define WIDTH 6 -#define HEIGHT 4 - -#define TRACE_VALUE TRACE_MAXIMUM - -void board( WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); -void magic(int **, int); -void print(int **, int); -void magic_board(int **a,int n); - -int main(int argc, char *argv[]) -{ - - int **a,n,i; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(0); - } - n = atoi(argv[1]); - if(n % 2 == 0) - { printf("Sorry !!! I don't know how to create magic square of even order\n"); - printf("The order should be an odd number\n"); - exit(0); - } - a = (int **) malloc(n * sizeof(int*)); - for(i = 0;i < n;++i) - a[i] = (int *)malloc(n * sizeof(int)); - - magic(a,n); - - initscr(); - curs_set(0); - noecho(); - magic_board(a,n); - getch(); - endwin(); - - return; -} - -void magic(int **a, int n) -{ - int i,j,k; - int row,col; - for(i = 0;i < n;++i) - for(j = 0;j < n;++j) - a[i][j] = -1; - row = 0; - col = n / 2; - - k = 1; - a[row][col] = k; - - while(k != n * n) - { - if(row == 0 && col != n - 1) - { row = n - 1; - col ++; - a[row][col] = ++k; - } - else if(row != 0 && col != n - 1) - { if(a[row - 1][col + 1] == -1) - { row --; - col ++; - a[row][col] = ++k; - } - else - { - row ++; - a[row][col] = ++k; - } - } - else if(row != 0 && col == n - 1) - { - row --; - col = 0; - a[row][col] = ++k; - } - else if(row == 0 && col == n - 1) - { row ++; - a[row][col] = ++k; - } - - } - return; -} - -void print(int **a,int n) -{ int i,j; - int x,y; - x = STARTX; - y = STARTY; - mvprintw(1,30,"MAGIC SQUARE"); - for(i = 0;i < n;++i) - { for(j = 0;j < n;++j) - { mvprintw(y,x,"%d",a[i][j]); - if(n > 9) - x += 4; - else - x += 6; - } - x = STARTX; - if(n > 7) - y += 2; - else - y += 3; - } - refresh(); -} -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} - -void magic_board(int **a,int n) -{ int i,j, deltax, deltay; - int startx, starty; - - starty = (LINES - n * HEIGHT) / 2; - startx = (COLS - n * WIDTH) / 2; - board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); - deltay = HEIGHT / 2; - deltax = WIDTH / 2; - for(i = 0;i < n; ++i) - for(j = 0; j < n; ++j) - mvprintw(starty + j * HEIGHT + deltay, - startx + i * WIDTH + deltax, - "%d", a[i][j]); -} +#include +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define TRACE_VALUE TRACE_MAXIMUM + +void board( WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void magic(int **, int); +void print(int **, int); +void magic_board(int **a,int n); + +int main(int argc, char *argv[]) +{ + + int **a,n,i; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(0); + } + n = atoi(argv[1]); + if(n % 2 == 0) + { printf("Sorry !!! I don't know how to create magic square of even order\n"); + printf("The order should be an odd number\n"); + exit(0); + } + a = (int **) malloc(n * sizeof(int*)); + for(i = 0;i < n;++i) + a[i] = (int *)malloc(n * sizeof(int)); + + magic(a,n); + + initscr(); + curs_set(0); + noecho(); + magic_board(a,n); + getch(); + endwin(); + + return; +} + +void magic(int **a, int n) +{ + int i,j,k; + int row,col; + for(i = 0;i < n;++i) + for(j = 0;j < n;++j) + a[i][j] = -1; + row = 0; + col = n / 2; + + k = 1; + a[row][col] = k; + + while(k != n * n) + { + if(row == 0 && col != n - 1) + { row = n - 1; + col ++; + a[row][col] = ++k; + } + else if(row != 0 && col != n - 1) + { if(a[row - 1][col + 1] == -1) + { row --; + col ++; + a[row][col] = ++k; + } + else + { + row ++; + a[row][col] = ++k; + } + } + else if(row != 0 && col == n - 1) + { + row --; + col = 0; + a[row][col] = ++k; + } + else if(row == 0 && col == n - 1) + { row ++; + a[row][col] = ++k; + } + + } + return; +} + +void print(int **a,int n) +{ int i,j; + int x,y; + x = STARTX; + y = STARTY; + mvprintw(1,30,"MAGIC SQUARE"); + for(i = 0;i < n;++i) + { for(j = 0;j < n;++j) + { mvprintw(y,x,"%d",a[i][j]); + if(n > 9) + x += 4; + else + x += 6; + } + x = STARTX; + if(n > 7) + y += 2; + else + y += 3; + } + refresh(); +} +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void magic_board(int **a,int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(i = 0;i < n; ++i) + for(j = 0; j < n; ++j) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%d", a[i][j]); +} diff --git a/JustForFun/queens.c b/JustForFun/queens.c index 82b11c1..2457ccc 100644 --- a/JustForFun/queens.c +++ b/JustForFun/queens.c @@ -1,122 +1,122 @@ -#include -#include - -#define QUEEN_CHAR '*' - -int *nqueens(int num); -int place(int current, int *position); -int print(int *positions, int num_queens); -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); - -int main(int argc, char *argv[]) -{ - int num_queens, *positions, count; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(1); - } - - num_queens = atoi(argv[1]); - initscr(); - cbreak(); - keypad(stdscr, TRUE); - positions = nqueens(num_queens); - free(positions); - endwin(); - return 0; -} - -int *nqueens(int num) -{ - int current, *position, num_solutions = 0; - - position = (int *) calloc(num + 1, sizeof(int)); - - position[1] = 0; - current = 1; /* current queen is being checked */ - /* position[current] is the coloumn*/ - while(current > 0){ - position[current] += 1; - while(position[current] <= num && !place(current, position) ) - position[current] += 1; - if(position[current] <= num){ - if(current == num) { - ++num_solutions; - print(position, num); - } - else { - current += 1; - position[current] = 0; - } - } - else current -= 1; /* backtrack */ - } - printf("Total Number of Solutions : %d\n", num_solutions); - return(position); -} - -int place(int current, int *position) -{ - int i; - if(current == 1) return(1); - for(i = 1; i < current; ++i) - if(position[i] == position[current]) return(0); - else if(abs(position[i] - position[current]) == - abs(i - current)) - return(0); - - return(1); -} - -int print(int *positions, int num_queens) -{ int count; - int y = 2, x = 2, w = 4, h = 2; - static int solution = 1; - - mvprintw(0, 0, "Solution No: %d", solution++); - board(stdscr, y, x, num_queens, num_queens, w, h); - for(count = 1; count <= num_queens; ++count) - { int tempy = y + (count - 1) * h + h / 2; - int tempx = x + (positions[count] - 1) * w + w / 2; - mvaddch(tempy, tempx, QUEEN_CHAR); - } - refresh(); - mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); - if(getch() == KEY_F(1)) - { endwin(); - exit(0); - } - clear(); -} - -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} +#include +#include + +#define QUEEN_CHAR '*' + +int *nqueens(int num); +int place(int current, int *position); +int print(int *positions, int num_queens); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); + +int main(int argc, char *argv[]) +{ + int num_queens, *positions, count; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + + num_queens = atoi(argv[1]); + initscr(); + cbreak(); + keypad(stdscr, TRUE); + positions = nqueens(num_queens); + free(positions); + endwin(); + return 0; +} + +int *nqueens(int num) +{ + int current, *position, num_solutions = 0; + + position = (int *) calloc(num + 1, sizeof(int)); + + position[1] = 0; + current = 1; /* current queen is being checked */ + /* position[current] is the coloumn*/ + while(current > 0){ + position[current] += 1; + while(position[current] <= num && !place(current, position) ) + position[current] += 1; + if(position[current] <= num){ + if(current == num) { + ++num_solutions; + print(position, num); + } + else { + current += 1; + position[current] = 0; + } + } + else current -= 1; /* backtrack */ + } + printf("Total Number of Solutions : %d\n", num_solutions); + return(position); +} + +int place(int current, int *position) +{ + int i; + if(current == 1) return(1); + for(i = 1; i < current; ++i) + if(position[i] == position[current]) return(0); + else if(abs(position[i] - position[current]) == + abs(i - current)) + return(0); + + return(1); +} + +int print(int *positions, int num_queens) +{ int count; + int y = 2, x = 2, w = 4, h = 2; + static int solution = 1; + + mvprintw(0, 0, "Solution No: %d", solution++); + board(stdscr, y, x, num_queens, num_queens, w, h); + for(count = 1; count <= num_queens; ++count) + { int tempy = y + (count - 1) * h + h / 2; + int tempx = x + (positions[count] - 1) * w + w / 2; + mvaddch(tempy, tempx, QUEEN_CHAR); + } + refresh(); + mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); + if(getch() == KEY_F(1)) + { endwin(); + exit(0); + } + clear(); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} diff --git a/JustForFun/shuffle.c b/JustForFun/shuffle.c index d1431bd..0b8150b 100644 --- a/JustForFun/shuffle.c +++ b/JustForFun/shuffle.c @@ -1,205 +1,205 @@ -#include - -#define STARTX 9 -#define STARTY 3 -#define WIDTH 6 -#define HEIGHT 4 - -#define BLANK 0 - -typedef struct _tile { - int x; - int y; -}tile; - -void init_board(int **board, int n, tile *blank); -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); -void shuffle_board(int **board, int n); -void move_blank(int direction, int **s_board, int n, tile *blank); -int check_win(int **s_board, int n, tile *blank); - -enum { LEFT, RIGHT, UP, DOWN }; - -int main(int argc, char *argv[]) -{ int **s_board; - int n, i, ch; - tile blank; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(1); - } - n = atoi(argv[1]); - - s_board = (int **)calloc(n, sizeof(int *)); - for(i = 0;i < n; ++i) - s_board[i] = (int *)calloc(n, sizeof(int)); - init_board(s_board, n, &blank); - initscr(); - keypad(stdscr, TRUE); - cbreak(); - shuffle_board(s_board, n); - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - move_blank(RIGHT, s_board, n, &blank); - break; - case KEY_RIGHT: - move_blank(LEFT, s_board, n, &blank); - break; - case KEY_UP: - move_blank(DOWN, s_board, n, &blank); - break; - case KEY_DOWN: - move_blank(UP, s_board, n, &blank); - break; - } - shuffle_board(s_board, n); - if(check_win(s_board, n, &blank) == TRUE) - { mvprintw(24, 0, "You Win !!!\n"); - refresh(); - break; - } - } - endwin(); - return 0; -} - -void move_blank(int direction, int **s_board, int n, tile *blank) -{ int temp; - - switch(direction) - { case LEFT: - { if(blank->x != 0) - { --blank->x; - temp = s_board[blank->x][blank->y]; - s_board[blank->x + 1][blank->y] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case RIGHT: - { if(blank->x != n - 1) - { ++blank->x; - temp = s_board[blank->x][blank->y]; - s_board[blank->x - 1][blank->y] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case UP: - { if(blank->y != 0) - { --blank->y; - temp = s_board[blank->x][blank->y]; - s_board[blank->x][blank->y + 1] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case DOWN: - { if(blank->y != n - 1) - { ++blank->y; - temp = s_board[blank->x][blank->y]; - s_board[blank->x][blank->y - 1] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - } -} - -int check_win(int **s_board, int n, tile *blank) -{ int i, j; - - s_board[blank->x][blank->y] = n * n; - for(i = 0;i < n; ++i) - for(j = 0;j < n; ++j) - if(s_board[i][j] != j * n + i + 1) - { s_board[blank->x][blank->y] = BLANK; - return FALSE; - } - - s_board[blank->x][blank->y] = BLANK; - return TRUE; -} - -void init_board(int **s_board, int n, tile *blank) -{ int i, j, k; - int *temp_board; - - temp_board = (int *)calloc(n * n, sizeof(int)); - srand(time(NULL)); - for(i = 0;i < n * n; ++i) - { -repeat : - k = rand() % (n * n); - for(j = 0;j <= i - 1; ++j) - if (k == temp_board[j]) - goto repeat; - else - temp_board[i] = k; - } - k = 0; - for (i = 0;i < n;++i) - for(j = 0;j < n; ++j,++k) - { if(temp_board[k] == 0) - { blank->x = i; - blank->y = j; - } - s_board[i][j] = temp_board[k]; - } - free(temp_board); -} - -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} - -void shuffle_board(int **s_board, int n) -{ int i,j, deltax, deltay; - int startx, starty; - - starty = (LINES - n * HEIGHT) / 2; - startx = (COLS - n * WIDTH) / 2; - clear(); - mvprintw(24, 0, "Press F1 to Exit"); - board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); - deltay = HEIGHT / 2; - deltax = WIDTH / 2; - for(j = 0; j < n; ++j) - for(i = 0;i < n; ++i) - if(s_board[i][j] != BLANK) - mvprintw(starty + j * HEIGHT + deltay, - startx + i * WIDTH + deltax, - "%-2d", s_board[i][j]); - refresh(); -} - - +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define BLANK 0 + +typedef struct _tile { + int x; + int y; +}tile; + +void init_board(int **board, int n, tile *blank); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void shuffle_board(int **board, int n); +void move_blank(int direction, int **s_board, int n, tile *blank); +int check_win(int **s_board, int n, tile *blank); + +enum { LEFT, RIGHT, UP, DOWN }; + +int main(int argc, char *argv[]) +{ int **s_board; + int n, i, ch; + tile blank; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + n = atoi(argv[1]); + + s_board = (int **)calloc(n, sizeof(int *)); + for(i = 0;i < n; ++i) + s_board[i] = (int *)calloc(n, sizeof(int)); + init_board(s_board, n, &blank); + initscr(); + keypad(stdscr, TRUE); + cbreak(); + shuffle_board(s_board, n); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + move_blank(RIGHT, s_board, n, &blank); + break; + case KEY_RIGHT: + move_blank(LEFT, s_board, n, &blank); + break; + case KEY_UP: + move_blank(DOWN, s_board, n, &blank); + break; + case KEY_DOWN: + move_blank(UP, s_board, n, &blank); + break; + } + shuffle_board(s_board, n); + if(check_win(s_board, n, &blank) == TRUE) + { mvprintw(24, 0, "You Win !!!\n"); + refresh(); + break; + } + } + endwin(); + return 0; +} + +void move_blank(int direction, int **s_board, int n, tile *blank) +{ int temp; + + switch(direction) + { case LEFT: + { if(blank->x != 0) + { --blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x + 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case RIGHT: + { if(blank->x != n - 1) + { ++blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x - 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case UP: + { if(blank->y != 0) + { --blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y + 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case DOWN: + { if(blank->y != n - 1) + { ++blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y - 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + } +} + +int check_win(int **s_board, int n, tile *blank) +{ int i, j; + + s_board[blank->x][blank->y] = n * n; + for(i = 0;i < n; ++i) + for(j = 0;j < n; ++j) + if(s_board[i][j] != j * n + i + 1) + { s_board[blank->x][blank->y] = BLANK; + return FALSE; + } + + s_board[blank->x][blank->y] = BLANK; + return TRUE; +} + +void init_board(int **s_board, int n, tile *blank) +{ int i, j, k; + int *temp_board; + + temp_board = (int *)calloc(n * n, sizeof(int)); + srand(time(NULL)); + for(i = 0;i < n * n; ++i) + { +repeat : + k = rand() % (n * n); + for(j = 0;j <= i - 1; ++j) + if (k == temp_board[j]) + goto repeat; + else + temp_board[i] = k; + } + k = 0; + for (i = 0;i < n;++i) + for(j = 0;j < n; ++j,++k) + { if(temp_board[k] == 0) + { blank->x = i; + blank->y = j; + } + s_board[i][j] = temp_board[k]; + } + free(temp_board); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void shuffle_board(int **s_board, int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + clear(); + mvprintw(24, 0, "Press F1 to Exit"); + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(j = 0; j < n; ++j) + for(i = 0;i < n; ++i) + if(s_board[i][j] != BLANK) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%-2d", s_board[i][j]); + refresh(); +} + + diff --git a/JustForFun/tt.c b/JustForFun/tt.c index 178eefa..4378e67 100644 --- a/JustForFun/tt.c +++ b/JustForFun/tt.c @@ -1,223 +1,223 @@ -#include -#include -#include -#include - -#define HSIZE 60 -#define LENGTH 75 -#define WIDTH 10 -#define STARTX 1 -#define STARTY 5 -#define STATUSX 1 -#define STATUSY 25 - -#define KEY_F1 265 - -int print_menu(); -void print_byebye(); -void create_test_string(); -void print_time(time_t startt, time_t endt, int mistakes); -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); - -char *groups[] = { "`123456" , - "7890-=" , - "~!@#$%^" , - "&*()_+" , - "<>?" , - ",./\\" , - "asdfg", - "jkl;'", - "qwer", - "uiop", - "tyur", - "zxcv", - "bnm", - }; -int n_groups; - -int main() -{ int choice, i; - char *test_array; - int ch = KEY_F1; - int mistakes; - int x, y; - time_t start_t, end_t; - WINDOW *typing_win; - char string[80]; - - string[0] = '\0'; - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - intrflush(stdscr, FALSE); - - srandom(time(NULL)); - n_groups = sizeof(groups) / sizeof(char *); - test_array = (char *)calloc(HSIZE + 1, sizeof(char)); - - while(1) - { - if(ch == KEY_F1) - { choice = print_menu(); - choice -= 1; - if(choice == n_groups) - { print_byebye(); - free(test_array); - endwin(); - exit(0); - } - } - clear(); - strcpy(string, "Typing window"); - print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); - attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); - refresh(); - attroff(A_REVERSE); - - create_test_string(test_array, choice); - typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); - keypad(typing_win, TRUE); - intrflush(typing_win, FALSE); - box(typing_win, 0, 0); - - x = 1; - y = 1; - mvwprintw(typing_win, y, x, "%s", test_array); - wrefresh(typing_win); - y += 1; - - mistakes = 0; - i = 0; - time(&start_t); - wmove(typing_win, y, x); - wrefresh(typing_win); - ch = 0; - while(ch != KEY_F1 && i != HSIZE + 1) - { ch = wgetch(typing_win); - mvwprintw(typing_win, y, x, "%c", ch); - wrefresh(typing_win); - ++x; - if(ch == test_array[i]) - { ++i; - continue; - } - else - { ++mistakes; - ++i; - } - } - - time(&end_t); - print_time(start_t, end_t, mistakes); - } - free(test_array); - endwin(); - return 0; -} - - -int print_menu() -{ int choice, i; - - choice = 0; - while(1) - { clear(); - printw("\n\n"); - print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); - printw("\n\n\n"); - for(i = 0;i <= n_groups - 1; ++i) - printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); - printw("\t%3d: \tExit\n", i + 1); - - printw("\n\n\tChoice: "); - refresh(); - echo(); - scanw("%d", &choice); - noecho(); - - if(choice >= 1 && choice <= n_groups + 1) - break; - else - { attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); - attroff(A_REVERSE); - getch(); - } - } - return choice; -} - -void create_test_string(char *test_array, int choice) -{ int i, index, length; - - length = strlen(groups[choice]); - for(i = 0;i <= HSIZE - 1; ++i) - { if(i%5 == 0) - test_array[i] = ' '; - else - { index = (int)(random() % length); - test_array[i] = groups[choice][index]; - } - } - test_array[i] = '\0'; -} - -void print_byebye() -{ printw("\n"); - print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); - print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL); - refresh(); -} - -void print_time(time_t start_t, time_t end_t, int mistakes) -{ long int diff; - int h,m,s; - float wpm; - - diff = end_t - start_t; - wpm = ((HSIZE / 5)/(double)diff)*60; - - h = (int)(diff / 3600); - diff -= h * 3600; - m = (int)(diff / 60); - diff -= m * 60; - s = (int)diff; - - attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm); - attroff(A_REVERSE); - - refresh(); - getch(); - -} - -/* ---------------------------------------------------------------- * - * startx = 0 means at present x * - * starty = 0 means at present y * - * win = NULL means take stdscr * - * ---------------------------------------------------------------- */ - -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} +#include +#include +#include +#include + +#define HSIZE 60 +#define LENGTH 75 +#define WIDTH 10 +#define STARTX 1 +#define STARTY 5 +#define STATUSX 1 +#define STATUSY 25 + +#define KEY_F1 265 + +int print_menu(); +void print_byebye(); +void create_test_string(); +void print_time(time_t startt, time_t endt, int mistakes); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); + +char *groups[] = { "`123456" , + "7890-=" , + "~!@#$%^" , + "&*()_+" , + "<>?" , + ",./\\" , + "asdfg", + "jkl;'", + "qwer", + "uiop", + "tyur", + "zxcv", + "bnm", + }; +int n_groups; + +int main() +{ int choice, i; + char *test_array; + int ch = KEY_F1; + int mistakes; + int x, y; + time_t start_t, end_t; + WINDOW *typing_win; + char string[80]; + + string[0] = '\0'; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + intrflush(stdscr, FALSE); + + srandom(time(NULL)); + n_groups = sizeof(groups) / sizeof(char *); + test_array = (char *)calloc(HSIZE + 1, sizeof(char)); + + while(1) + { + if(ch == KEY_F1) + { choice = print_menu(); + choice -= 1; + if(choice == n_groups) + { print_byebye(); + free(test_array); + endwin(); + exit(0); + } + } + clear(); + strcpy(string, "Typing window"); + print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); + refresh(); + attroff(A_REVERSE); + + create_test_string(test_array, choice); + typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); + keypad(typing_win, TRUE); + intrflush(typing_win, FALSE); + box(typing_win, 0, 0); + + x = 1; + y = 1; + mvwprintw(typing_win, y, x, "%s", test_array); + wrefresh(typing_win); + y += 1; + + mistakes = 0; + i = 0; + time(&start_t); + wmove(typing_win, y, x); + wrefresh(typing_win); + ch = 0; + while(ch != KEY_F1 && i != HSIZE + 1) + { ch = wgetch(typing_win); + mvwprintw(typing_win, y, x, "%c", ch); + wrefresh(typing_win); + ++x; + if(ch == test_array[i]) + { ++i; + continue; + } + else + { ++mistakes; + ++i; + } + } + + time(&end_t); + print_time(start_t, end_t, mistakes); + } + free(test_array); + endwin(); + return 0; +} + + +int print_menu() +{ int choice, i; + + choice = 0; + while(1) + { clear(); + printw("\n\n"); + print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); + printw("\n\n\n"); + for(i = 0;i <= n_groups - 1; ++i) + printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); + printw("\t%3d: \tExit\n", i + 1); + + printw("\n\n\tChoice: "); + refresh(); + echo(); + scanw("%d", &choice); + noecho(); + + if(choice >= 1 && choice <= n_groups + 1) + break; + else + { attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); + attroff(A_REVERSE); + getch(); + } + } + return choice; +} + +void create_test_string(char *test_array, int choice) +{ int i, index, length; + + length = strlen(groups[choice]); + for(i = 0;i <= HSIZE - 1; ++i) + { if(i%5 == 0) + test_array[i] = ' '; + else + { index = (int)(random() % length); + test_array[i] = groups[choice][index]; + } + } + test_array[i] = '\0'; +} + +void print_byebye() +{ printw("\n"); + print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); + print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL); + refresh(); +} + +void print_time(time_t start_t, time_t end_t, int mistakes) +{ long int diff; + int h,m,s; + float wpm; + + diff = end_t - start_t; + wpm = ((HSIZE / 5)/(double)diff)*60; + + h = (int)(diff / 3600); + diff -= h * 3600; + m = (int)(diff / 60); + diff -= m * 60; + s = (int)diff; + + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm); + attroff(A_REVERSE); + + refresh(); + getch(); + +} + +/* ---------------------------------------------------------------- * + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * ---------------------------------------------------------------- */ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/basics/Makefile b/basics/Makefile index 85f1de7..90ec1fb 100644 --- a/basics/Makefile +++ b/basics/Makefile @@ -10,26 +10,26 @@ EXE_DIR=../demo/exe EXES = \ ${EXE_DIR}/hello_world \ - ${EXE_DIR}/init_func_example \ - ${EXE_DIR}/key_code \ - ${EXE_DIR}/mouse_menu \ - ${EXE_DIR}/other_border \ - ${EXE_DIR}/printw_example \ - ${EXE_DIR}/scanw_example \ - ${EXE_DIR}/simple_attr \ - ${EXE_DIR}/simple_color \ - ${EXE_DIR}/simple_key \ - ${EXE_DIR}/temp_leave \ - ${EXE_DIR}/win_border \ - ${EXE_DIR}/with_chgat + ${EXE_DIR}/init_func_example \ + ${EXE_DIR}/key_code \ + ${EXE_DIR}/mouse_menu \ + ${EXE_DIR}/other_border \ + ${EXE_DIR}/printw_example \ + ${EXE_DIR}/scanw_example \ + ${EXE_DIR}/simple_attr \ + ${EXE_DIR}/simple_color \ + ${EXE_DIR}/simple_key \ + ${EXE_DIR}/temp_leave \ + ${EXE_DIR}/win_border \ + ${EXE_DIR}/with_chgat ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/basics/README b/basics/README index ac5f396..0760867 100644 --- a/basics/README +++ b/basics/README @@ -1,20 +1,20 @@ -Description of files +Description of files -------------------- basics | - |----> acs_vars.c -- ACS_ variables example - |----> hello_world.c -- Simple "Hello World" Program - |----> init_func_example.c -- Initialization functions example - |----> key_code.c -- Shows the scan code of the key pressed - |----> mouse_menu.c -- A menu accessible by mouse - |----> other_border.c -- Shows usage of other border functions apart - | -- box() - |----> printw_example.c -- A very simple printw() example - |----> scanw_example.c -- A very simple getstr() example - |----> simple_attr.c -- A program that can print a c file with comments - | -- in attribute - |----> simple_color.c -- A simple example demonstrating colors - |----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows - |----> temp_leave.c -- Demonstrates temporarily leaving curses mode - |----> win_border.c -- Shows Creation of windows and borders - |----> with_chgat.c -- chgat() usage example + |----> acs_vars.c -- ACS_ variables example + |----> hello_world.c -- Simple "Hello World" Program + |----> init_func_example.c -- Initialization functions example + |----> key_code.c -- Shows the scan code of the key pressed + |----> mouse_menu.c -- A menu accessible by mouse + |----> other_border.c -- Shows usage of other border functions apart + | -- box() + |----> printw_example.c -- A very simple printw() example + |----> scanw_example.c -- A very simple getstr() example + |----> simple_attr.c -- A program that can print a c file with comments + | -- in attribute + |----> simple_color.c -- A simple example demonstrating colors + |----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows + |----> temp_leave.c -- Demonstrates temporarily leaving curses mode + |----> win_border.c -- Shows Creation of windows and borders + |----> with_chgat.c -- chgat() usage example diff --git a/basics/acs_vars.c b/basics/acs_vars.c index 92db58c..c1c72a3 100644 --- a/basics/acs_vars.c +++ b/basics/acs_vars.c @@ -1,44 +1,44 @@ -#include - -int main() -{ - initscr(); - - printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); - printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); - printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); - printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); - printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); - printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); - printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); - printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); - printw("Vertical line "); addch(ACS_VLINE); printw("\n"); - printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); - printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); - printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); - printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); - printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); - printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); - printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); - printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); - printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); - printw("Bullet "); addch(ACS_BULLET); printw("\n"); - printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); - printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); - printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); - printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); - printw("Board of squares "); addch(ACS_BOARD); printw("\n"); - printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); - printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); - printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); - printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); - printw("Pi "); addch(ACS_PI); printw("\n"); - printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); - printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); - - refresh(); - getch(); - endwin(); - - return 0; -} +#include + +int main() +{ + initscr(); + + printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); + printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); + printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); + printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); + printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); + printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); + printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); + printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); + printw("Vertical line "); addch(ACS_VLINE); printw("\n"); + printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); + printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); + printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); + printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); + printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); + printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); + printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); + printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); + printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); + printw("Bullet "); addch(ACS_BULLET); printw("\n"); + printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); + printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); + printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); + printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); + printw("Board of squares "); addch(ACS_BOARD); printw("\n"); + printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); + printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); + printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); + printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); + printw("Pi "); addch(ACS_PI); printw("\n"); + printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); + printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); + + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/basics/hello_world.c b/basics/hello_world.c index 794ae22..55b24df 100644 --- a/basics/hello_world.c +++ b/basics/hello_world.c @@ -1,12 +1,12 @@ -#include - -int main() -{ - initscr(); /* Start curses mode */ - printw("Hello World !!!"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/init_func_example.c b/basics/init_func_example.c index e01ecb3..2eb19b7 100644 --- a/basics/init_func_example.c +++ b/basics/init_func_example.c @@ -1,31 +1,31 @@ -#include - -int main() -{ int ch; - - initscr(); /* Start curses mode */ - raw(); /* Line buffering disabled */ - keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ - noecho(); /* Don't echo() while we do getch */ - - printw("Type any character to see it in bold\n"); - ch = getch(); /* If raw() hadn't been called - * we have to press enter before it - * gets to the program */ - if(ch == KEY_F(1)) /* Without keypad enabled this will */ - printw("F1 Key pressed");/* not get to us either */ - /* Without noecho() some ugly escape - * charachters might have been printed - * on screen */ - else - { printw("The pressed key is "); - attron(A_BOLD); - printw("%c", ch); - attroff(A_BOLD); - } - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ int ch; + + initscr(); /* Start curses mode */ + raw(); /* Line buffering disabled */ + keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ + noecho(); /* Don't echo() while we do getch */ + + printw("Type any character to see it in bold\n"); + ch = getch(); /* If raw() hadn't been called + * we have to press enter before it + * gets to the program */ + if(ch == KEY_F(1)) /* Without keypad enabled this will */ + printw("F1 Key pressed");/* not get to us either */ + /* Without noecho() some ugly escape + * charachters might have been printed + * on screen */ + else + { printw("The pressed key is "); + attron(A_BOLD); + printw("%c", ch); + attroff(A_BOLD); + } + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/key_code.c b/basics/key_code.c index 5ad80eb..8c5d06c 100644 --- a/basics/key_code.c +++ b/basics/key_code.c @@ -1,14 +1,14 @@ -#include - -int main() -{ int ch; - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - ch = getch(); - endwin(); - printf("The key pressed is %d\n", ch); -} +#include + +int main() +{ int ch; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + ch = getch(); + endwin(); + printf("The key pressed is %d\n", ch); +} diff --git a/basics/mouse_menu.c b/basics/mouse_menu.c index 6008092..5b92e1e 100644 --- a/basics/mouse_menu.c +++ b/basics/mouse_menu.c @@ -1,106 +1,106 @@ -#include - -#define WIDTH 30 -#define HEIGHT 10 - -int startx = 0; -int starty = 0; - -char *choices[] = { "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int n_choices = sizeof(choices) / sizeof(char *); - -void print_menu(WINDOW *menu_win, int highlight); -void report_choice(int mouse_x, int mouse_y, int *p_choice); - -int main() -{ int c, choice = 0; - WINDOW *menu_win; - MEVENT event; - - /* Initialize curses */ - initscr(); - clear(); - noecho(); - cbreak(); //Line buffering disabled. pass on everything - - /* Try to put the window in the middle of screen */ - startx = (80 - WIDTH) / 2; - starty = (24 - HEIGHT) / 2; - - attron(A_REVERSE); - mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); - refresh(); - attroff(A_REVERSE); - - /* Print the menu for the first time */ - menu_win = newwin(HEIGHT, WIDTH, starty, startx); - print_menu(menu_win, 1); - /* Get all the mouse events */ - mousemask(ALL_MOUSE_EVENTS, NULL); - - while(1) - { c = wgetch(menu_win); - switch(c) - { case KEY_MOUSE: - if(getmouse(&event) == OK) - { /* When the user clicks left mouse button */ - if(event.bstate & BUTTON1_PRESSED) - { report_choice(event.x + 1, event.y + 1, &choice); - if(choice == -1) //Exit chosen - goto end; - mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); - refresh(); - } - } - print_menu(menu_win, choice); - break; - } - } -end: - endwin(); - return 0; -} - - -void print_menu(WINDOW *menu_win, int highlight) -{ - int x, y, i; - - x = 2; - y = 2; - box(menu_win, 0, 0); - for(i = 0; i < n_choices; ++i) - { if(highlight == i + 1) - { wattron(menu_win, A_REVERSE); - mvwprintw(menu_win, y, x, "%s", choices[i]); - wattroff(menu_win, A_REVERSE); - } - else - mvwprintw(menu_win, y, x, "%s", choices[i]); - ++y; - } - wrefresh(menu_win); -} - -/* Report the choice according to mouse position */ -void report_choice(int mouse_x, int mouse_y, int *p_choice) -{ int i,j, choice; - - i = startx + 2; - j = starty + 3; - - for(choice = 0; choice < n_choices; ++choice) - if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) - { if(choice == n_choices - 1) - *p_choice = -1; - else - *p_choice = choice + 1; - break; - } -} +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int n_choices = sizeof(choices) / sizeof(char *); + +void print_menu(WINDOW *menu_win, int highlight); +void report_choice(int mouse_x, int mouse_y, int *p_choice); + +int main() +{ int c, choice = 0; + WINDOW *menu_win; + MEVENT event; + + /* Initialize curses */ + initscr(); + clear(); + noecho(); + cbreak(); //Line buffering disabled. pass on everything + + /* Try to put the window in the middle of screen */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + attron(A_REVERSE); + mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); + refresh(); + attroff(A_REVERSE); + + /* Print the menu for the first time */ + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + print_menu(menu_win, 1); + /* Get all the mouse events */ + mousemask(ALL_MOUSE_EVENTS, NULL); + + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_MOUSE: + if(getmouse(&event) == OK) + { /* When the user clicks left mouse button */ + if(event.bstate & BUTTON1_PRESSED) + { report_choice(event.x + 1, event.y + 1, &choice); + if(choice == -1) //Exit chosen + goto end; + mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); + refresh(); + } + } + print_menu(menu_win, choice); + break; + } + } +end: + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + +/* Report the choice according to mouse position */ +void report_choice(int mouse_x, int mouse_y, int *p_choice) +{ int i,j, choice; + + i = startx + 2; + j = starty + 3; + + for(choice = 0; choice < n_choices; ++choice) + if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) + { if(choice == n_choices - 1) + *p_choice = -1; + else + *p_choice = choice + 1; + break; + } +} diff --git a/basics/other_border.c b/basics/other_border.c index 8252631..725d67a 100644 --- a/basics/other_border.c +++ b/basics/other_border.c @@ -1,120 +1,120 @@ -#include - -typedef struct _win_border_struct { - chtype ls, rs, ts, bs, - tl, tr, bl, br; -}WIN_BORDER; - -typedef struct _WIN_struct { - - int startx, starty; - int height, width; - WIN_BORDER border; -}WIN; - -void init_win_params(WIN *p_win); -void print_win_params(WIN *p_win); -void create_box(WIN *win, bool flag); - -int main(int argc, char *argv[]) -{ WIN win; - int ch; - - initscr(); /* Start curses mode */ - start_color(); /* Start the color functionality */ - cbreak(); /* Line buffering disabled, Pass on - * everty thing to me */ - keypad(stdscr, TRUE); /* I need that nifty F1 */ - noecho(); - init_pair(1, COLOR_CYAN, COLOR_BLACK); - - /* Initialize the window parameters */ - init_win_params(&win); - print_win_params(&win); - - attron(COLOR_PAIR(1)); - printw("Press F1 to exit"); - refresh(); - attroff(COLOR_PAIR(1)); - - create_box(&win, TRUE); - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - create_box(&win, FALSE); - --win.startx; - create_box(&win, TRUE); - break; - case KEY_RIGHT: - create_box(&win, FALSE); - ++win.startx; - create_box(&win, TRUE); - break; - case KEY_UP: - create_box(&win, FALSE); - --win.starty; - create_box(&win, TRUE); - break; - case KEY_DOWN: - create_box(&win, FALSE); - ++win.starty; - create_box(&win, TRUE); - break; - } - } - endwin(); /* End curses mode */ - return 0; -} -void init_win_params(WIN *p_win) -{ - p_win->height = 3; - p_win->width = 10; - p_win->starty = (LINES - p_win->height)/2; - p_win->startx = (COLS - p_win->width)/2; - - p_win->border.ls = '|'; - p_win->border.rs = '|'; - p_win->border.ts = '-'; - p_win->border.bs = '-'; - p_win->border.tl = '+'; - p_win->border.tr = '+'; - p_win->border.bl = '+'; - p_win->border.br = '+'; - -} -void print_win_params(WIN *p_win) -{ -#ifdef _DEBUG - mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, - p_win->width, p_win->height); - refresh(); -#endif -} -void create_box(WIN *p_win, bool flag) -{ int i, j; - int x, y, w, h; - - x = p_win->startx; - y = p_win->starty; - w = p_win->width; - h = p_win->height; - - if(flag == TRUE) - { mvaddch(y, x, p_win->border.tl); - mvaddch(y, x + w, p_win->border.tr); - mvaddch(y + h, x, p_win->border.bl); - mvaddch(y + h, x + w, p_win->border.br); - mvhline(y, x + 1, p_win->border.ts, w - 1); - mvhline(y + h, x + 1, p_win->border.bs, w - 1); - mvvline(y + 1, x, p_win->border.ls, h - 1); - mvvline(y + 1, x + w, p_win->border.rs, h - 1); - - } - else - for(j = y; j <= y + h; ++j) - for(i = x; i <= x + w; ++i) - mvaddch(j, i, ' '); - - refresh(); - -} +#include + +typedef struct _win_border_struct { + chtype ls, rs, ts, bs, + tl, tr, bl, br; +}WIN_BORDER; + +typedef struct _WIN_struct { + + int startx, starty; + int height, width; + WIN_BORDER border; +}WIN; + +void init_win_params(WIN *p_win); +void print_win_params(WIN *p_win); +void create_box(WIN *win, bool flag); + +int main(int argc, char *argv[]) +{ WIN win; + int ch; + + initscr(); /* Start curses mode */ + start_color(); /* Start the color functionality */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + noecho(); + init_pair(1, COLOR_CYAN, COLOR_BLACK); + + /* Initialize the window parameters */ + init_win_params(&win); + print_win_params(&win); + + attron(COLOR_PAIR(1)); + printw("Press F1 to exit"); + refresh(); + attroff(COLOR_PAIR(1)); + + create_box(&win, TRUE); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + create_box(&win, FALSE); + --win.startx; + create_box(&win, TRUE); + break; + case KEY_RIGHT: + create_box(&win, FALSE); + ++win.startx; + create_box(&win, TRUE); + break; + case KEY_UP: + create_box(&win, FALSE); + --win.starty; + create_box(&win, TRUE); + break; + case KEY_DOWN: + create_box(&win, FALSE); + ++win.starty; + create_box(&win, TRUE); + break; + } + } + endwin(); /* End curses mode */ + return 0; +} +void init_win_params(WIN *p_win) +{ + p_win->height = 3; + p_win->width = 10; + p_win->starty = (LINES - p_win->height)/2; + p_win->startx = (COLS - p_win->width)/2; + + p_win->border.ls = '|'; + p_win->border.rs = '|'; + p_win->border.ts = '-'; + p_win->border.bs = '-'; + p_win->border.tl = '+'; + p_win->border.tr = '+'; + p_win->border.bl = '+'; + p_win->border.br = '+'; + +} +void print_win_params(WIN *p_win) +{ +#ifdef _DEBUG + mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, + p_win->width, p_win->height); + refresh(); +#endif +} +void create_box(WIN *p_win, bool flag) +{ int i, j; + int x, y, w, h; + + x = p_win->startx; + y = p_win->starty; + w = p_win->width; + h = p_win->height; + + if(flag == TRUE) + { mvaddch(y, x, p_win->border.tl); + mvaddch(y, x + w, p_win->border.tr); + mvaddch(y + h, x, p_win->border.bl); + mvaddch(y + h, x + w, p_win->border.br); + mvhline(y, x + 1, p_win->border.ts, w - 1); + mvhline(y + h, x + 1, p_win->border.bs, w - 1); + mvvline(y + 1, x, p_win->border.ls, h - 1); + mvvline(y + 1, x + w, p_win->border.rs, h - 1); + + } + else + for(j = y; j <= y + h; ++j) + for(i = x; i <= x + w; ++i) + mvaddch(j, i, ' '); + + refresh(); + +} diff --git a/basics/printw_example.c b/basics/printw_example.c index 3f6d2e5..ae1345e 100644 --- a/basics/printw_example.c +++ b/basics/printw_example.c @@ -1,20 +1,20 @@ -#include /* ncurses.h includes stdio.h */ -#include - -int main() -{ - char mesg[]="Just a string"; /* message to be appeared on the screen */ - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); - printw("Try resizing your window(if possible) and then run this program again"); - refresh(); - getch(); - endwin(); - - return 0; -} +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Just a string"; /* message to be appeared on the screen */ + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); + printw("Try resizing your window(if possible) and then run this program again"); + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/basics/scanw_example.c b/basics/scanw_example.c index 4a91faf..cc0bf7b 100644 --- a/basics/scanw_example.c +++ b/basics/scanw_example.c @@ -1,20 +1,20 @@ -#include /* ncurses.h includes stdio.h */ -#include - -int main() -{ - char mesg[]="Enter a string: "; /* message to be appeared on the screen */ - char str[80]; - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - getstr(str); - mvprintw(LINES - 2, 0, "You Entered: %s", str); - getch(); - endwin(); - - return 0; -} +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Enter a string: "; /* message to be appeared on the screen */ + char str[80]; + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + getstr(str); + mvprintw(LINES - 2, 0, "You Entered: %s", str); + getch(); + endwin(); + + return 0; +} diff --git a/basics/simple_attr.c b/basics/simple_attr.c index 5b4b9ed..3efef73 100644 --- a/basics/simple_attr.c +++ b/basics/simple_attr.c @@ -3,7 +3,7 @@ #include int main(int argc, char *argv[]) -{ +{ int ch, prev, row, col; prev = EOF; FILE *fp; @@ -20,35 +20,35 @@ int main(int argc, char *argv[]) perror("Cannot open input file"); exit(1); } - initscr(); /* Start curses mode */ - getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ - while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ + initscr(); /* Start curses mode */ + getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ + while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ { - getyx(stdscr, y, x); /* get the current curser position */ - if(y == (row - 1)) /* are we are at the end of the screen */ + getyx(stdscr, y, x); /* get the current curser position */ + if(y == (row - 1)) /* are we are at the end of the screen */ { - printw("<-Press Any Key->"); /* tell the user to press a key */ + printw("<-Press Any Key->"); /* tell the user to press a key */ getch(); - clear(); /* clear the screen */ - move(0, 0); /* start at the beginning of the screen */ + clear(); /* clear the screen */ + move(0, 0); /* start at the beginning of the screen */ } - if(prev == '/' && ch == '*') /* If it is / and * then only - * switch bold on */ + if(prev == '/' && ch == '*') /* If it is / and * then only + * switch bold on */ { - attron(A_BOLD); /* cut bold on */ - getyx(stdscr, y, x); /* get the current curser position */ - move(y, x - 1); /* back up one space */ - printw("%c%c", '/', ch); /* The actual printing is done here */ + attron(A_BOLD); /* cut bold on */ + getyx(stdscr, y, x); /* get the current curser position */ + move(y, x - 1); /* back up one space */ + printw("%c%c", '/', ch); /* The actual printing is done here */ } else printw("%c", ch); refresh(); if(prev == '*' && ch == '/') - attroff(A_BOLD); /* Switch it off once we got * - * and then / */ + attroff(A_BOLD); /* Switch it off once we got * + * and then / */ prev = ch; } - endwin(); /* End curses mode */ + endwin(); /* End curses mode */ fclose(fp); return 0; } diff --git a/basics/simple_color.c b/basics/simple_color.c index 45223db..59fb020 100644 --- a/basics/simple_color.c +++ b/basics/simple_color.c @@ -1,40 +1,40 @@ -#include - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); -int main(int argc, char *argv[]) -{ initscr(); /* Start curses mode */ - if(has_colors() == FALSE) - { endwin(); - printf("Your terminal does not support color\n"); - exit(1); - } - start_color(); /* Start color */ - init_pair(1, COLOR_RED, COLOR_BLACK); - - attron(COLOR_PAIR(1)); - print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); - attroff(COLOR_PAIR(1)); - getch(); - endwin(); -} -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} - +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + if(has_colors() == FALSE) + { endwin(); + printf("Your terminal does not support color\n"); + exit(1); + } + start_color(); /* Start color */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + attron(COLOR_PAIR(1)); + print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); + attroff(COLOR_PAIR(1)); + getch(); + endwin(); +} +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} + diff --git a/basics/simple_key.c b/basics/simple_key.c index 23ff44c..5197646 100644 --- a/basics/simple_key.c +++ b/basics/simple_key.c @@ -1,92 +1,92 @@ -#include -#include - -#define WIDTH 30 -#define HEIGHT 10 - -int startx = 0; -int starty = 0; - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; -int n_choices = sizeof(choices) / sizeof(char *); -void print_menu(WINDOW *menu_win, int highlight); - -int main() -{ WINDOW *menu_win; - int highlight = 1; - int choice = 0; - int c; - - initscr(); - clear(); - noecho(); - cbreak(); /* Line buffering disabled. pass on everything */ - startx = (80 - WIDTH) / 2; - starty = (24 - HEIGHT) / 2; - - menu_win = newwin(HEIGHT, WIDTH, starty, startx); - keypad(menu_win, TRUE); - mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); - refresh(); - print_menu(menu_win, highlight); - while(1) - { c = wgetch(menu_win); - switch(c) - { case KEY_UP: - if(highlight == 1) - highlight = n_choices; - else - --highlight; - break; - case KEY_DOWN: - if(highlight == n_choices) - highlight = 1; - else - ++highlight; - break; - case 10: - choice = highlight; - break; - default: - mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); - refresh(); - break; - } - print_menu(menu_win, highlight); - if(choice != 0) /* User did a choice come out of the infinite loop */ - break; - } - mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); - clrtoeol(); - refresh(); - endwin(); - return 0; -} - - -void print_menu(WINDOW *menu_win, int highlight) -{ - int x, y, i; - - x = 2; - y = 2; - box(menu_win, 0, 0); - for(i = 0; i < n_choices; ++i) - { if(highlight == i + 1) /* High light the present choice */ - { wattron(menu_win, A_REVERSE); - mvwprintw(menu_win, y, x, "%s", choices[i]); - wattroff(menu_win, A_REVERSE); - } - else - mvwprintw(menu_win, y, x, "%s", choices[i]); - ++y; - } - wrefresh(menu_win); -} - +#include +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; +int n_choices = sizeof(choices) / sizeof(char *); +void print_menu(WINDOW *menu_win, int highlight); + +int main() +{ WINDOW *menu_win; + int highlight = 1; + int choice = 0; + int c; + + initscr(); + clear(); + noecho(); + cbreak(); /* Line buffering disabled. pass on everything */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + keypad(menu_win, TRUE); + mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); + refresh(); + print_menu(menu_win, highlight); + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_UP: + if(highlight == 1) + highlight = n_choices; + else + --highlight; + break; + case KEY_DOWN: + if(highlight == n_choices) + highlight = 1; + else + ++highlight; + break; + case 10: + choice = highlight; + break; + default: + mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); + refresh(); + break; + } + print_menu(menu_win, highlight); + if(choice != 0) /* User did a choice come out of the infinite loop */ + break; + } + mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); + clrtoeol(); + refresh(); + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) /* High light the present choice */ + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + diff --git a/basics/temp_leave.c b/basics/temp_leave.c index 8ba21c1..fdcd5c5 100644 --- a/basics/temp_leave.c +++ b/basics/temp_leave.c @@ -1,20 +1,20 @@ -#include - -int main() -{ - initscr(); /* Start curses mode */ - printw("Hello World !!!\n"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ - def_prog_mode(); /* Save the tty modes */ - endwin(); /* End curses mode temporarily */ - system("/bin/sh"); /* Do whatever you like in cooked mode */ - reset_prog_mode(); /* Return to the previous tty mode*/ - /* stored by def_prog_mode() */ - refresh(); /* Do refresh() to restore the */ - /* Screen contents */ - printw("Another String\n"); /* Back to curses use the full */ - refresh(); /* capabilities of curses */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!\n"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + def_prog_mode(); /* Save the tty modes */ + endwin(); /* End curses mode temporarily */ + system("/bin/sh"); /* Do whatever you like in cooked mode */ + reset_prog_mode(); /* Return to the previous tty mode*/ + /* stored by def_prog_mode() */ + refresh(); /* Do refresh() to restore the */ + /* Screen contents */ + printw("Another String\n"); /* Back to curses use the full */ + refresh(); /* capabilities of curses */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/win_border.c b/basics/win_border.c index b0e8d61..41e1a7e 100644 --- a/basics/win_border.c +++ b/basics/win_border.c @@ -1,82 +1,82 @@ -#include - - -WINDOW *create_newwin(int height, int width, int starty, int startx); -void destroy_win(WINDOW *local_win); - -int main(int argc, char *argv[]) -{ WINDOW *my_win; - int startx, starty, width, height; - int ch; - - initscr(); /* Start curses mode */ - cbreak(); /* Line buffering disabled, Pass on - * everty thing to me */ - keypad(stdscr, TRUE); /* I need that nifty F1 */ - - height = 3; - width = 10; - starty = (LINES - height) / 2; /* Calculating for a center placement */ - startx = (COLS - width) / 2; /* of the window */ - printw("Press F1 to exit"); - refresh(); - my_win = create_newwin(height, width, starty, startx); - - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - destroy_win(my_win); - my_win = create_newwin(height, width, starty,--startx); - break; - case KEY_RIGHT: - destroy_win(my_win); - my_win = create_newwin(height, width, starty,++startx); - break; - case KEY_UP: - destroy_win(my_win); - my_win = create_newwin(height, width, --starty,startx); - break; - case KEY_DOWN: - destroy_win(my_win); - my_win = create_newwin(height, width, ++starty,startx); - break; - } - } - - endwin(); /* End curses mode */ - return 0; -} - -WINDOW *create_newwin(int height, int width, int starty, int startx) -{ WINDOW *local_win; - - local_win = newwin(height, width, starty, startx); - box(local_win, 0 , 0); /* 0, 0 gives default characters - * for the vertical and horizontal - * lines */ - wrefresh(local_win); /* Show that box */ - - return local_win; -} - -void destroy_win(WINDOW *local_win) -{ - /* box(local_win, ' ', ' '); : This won't produce the desired - * result of erasing the window. It will leave it's four corners - * and so an ugly remnant of window. - */ - wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); - /* The parameters taken are - * 1. win: the window on which to operate - * 2. ls: character to be used for the left side of the window - * 3. rs: character to be used for the right side of the window - * 4. ts: character to be used for the top side of the window - * 5. bs: character to be used for the bottom side of the window - * 6. tl: character to be used for the top left corner of the window - * 7. tr: character to be used for the top right corner of the window - * 8. bl: character to be used for the bottom left corner of the window - * 9. br: character to be used for the bottom right corner of the window - */ - wrefresh(local_win); - delwin(local_win); -} +#include + + +WINDOW *create_newwin(int height, int width, int starty, int startx); +void destroy_win(WINDOW *local_win); + +int main(int argc, char *argv[]) +{ WINDOW *my_win; + int startx, starty, width, height; + int ch; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + + height = 3; + width = 10; + starty = (LINES - height) / 2; /* Calculating for a center placement */ + startx = (COLS - width) / 2; /* of the window */ + printw("Press F1 to exit"); + refresh(); + my_win = create_newwin(height, width, starty, startx); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,--startx); + break; + case KEY_RIGHT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,++startx); + break; + case KEY_UP: + destroy_win(my_win); + my_win = create_newwin(height, width, --starty,startx); + break; + case KEY_DOWN: + destroy_win(my_win); + my_win = create_newwin(height, width, ++starty,startx); + break; + } + } + + endwin(); /* End curses mode */ + return 0; +} + +WINDOW *create_newwin(int height, int width, int starty, int startx) +{ WINDOW *local_win; + + local_win = newwin(height, width, starty, startx); + box(local_win, 0 , 0); /* 0, 0 gives default characters + * for the vertical and horizontal + * lines */ + wrefresh(local_win); /* Show that box */ + + return local_win; +} + +void destroy_win(WINDOW *local_win) +{ + /* box(local_win, ' ', ' '); : This won't produce the desired + * result of erasing the window. It will leave it's four corners + * and so an ugly remnant of window. + */ + wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); + /* The parameters taken are + * 1. win: the window on which to operate + * 2. ls: character to be used for the left side of the window + * 3. rs: character to be used for the right side of the window + * 4. ts: character to be used for the top side of the window + * 5. bs: character to be used for the bottom side of the window + * 6. tl: character to be used for the top left corner of the window + * 7. tr: character to be used for the top right corner of the window + * 8. bl: character to be used for the bottom left corner of the window + * 9. br: character to be used for the bottom right corner of the window + */ + wrefresh(local_win); + delwin(local_win); +} diff --git a/basics/with_chgat.c b/basics/with_chgat.c index 96e23d7..4255561 100644 --- a/basics/with_chgat.c +++ b/basics/with_chgat.c @@ -1,24 +1,24 @@ -#include - -int main(int argc, char *argv[]) -{ initscr(); /* Start curses mode */ - start_color(); /* Start color functionality */ - - init_pair(1, COLOR_CYAN, COLOR_BLACK); - printw("A Big string which i didn't care to type fully "); - mvchgat(0, 0, -1, A_BLINK, 1, NULL); - /* - * First two parameters specify the position at which to start - * Third parameter number of characters to update. -1 means till - * end of line - * Forth parameter is the normal attribute you wanted to give - * to the charcter - * Fifth is the color index. It is the index given during init_pair() - * use 0 if you didn't want color - * Sixth one is always NULL - */ - refresh(); - getch(); - endwin(); /* End curses mode */ - return 0; -} +#include + +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + start_color(); /* Start color functionality */ + + init_pair(1, COLOR_CYAN, COLOR_BLACK); + printw("A Big string which i didn't care to type fully "); + mvchgat(0, 0, -1, A_BLINK, 1, NULL); + /* + * First two parameters specify the position at which to start + * Third parameter number of characters to update. -1 means till + * end of line + * Forth parameter is the normal attribute you wanted to give + * to the charcter + * Fifth is the color index. It is the index given during init_pair() + * use 0 if you didn't want color + * Sixth one is always NULL + */ + refresh(); + getch(); + endwin(); /* End curses mode */ + return 0; +} diff --git a/forms/Makefile b/forms/Makefile index e120e37..4ed1b98 100644 --- a/forms/Makefile +++ b/forms/Makefile @@ -9,19 +9,19 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/form_attrib\ - ${EXE_DIR}/form_options\ - ${EXE_DIR}/form_simple\ - ${EXE_DIR}/form_win \ + ${EXE_DIR}/form_attrib\ + ${EXE_DIR}/form_options\ + ${EXE_DIR}/form_simple\ + ${EXE_DIR}/form_win \ ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/forms/README b/forms/README index d2c1b87..6e9a218 100644 --- a/forms/README +++ b/forms/README @@ -1,9 +1,9 @@ -Description of files +Description of files -------------------- - forms + forms | - |----> form_attrib.c -- Usage of field attributes - |----> form_options.c -- Usage of field options - |----> form_simple.c -- A simple form example - |----> form_win.c -- Demo of windows associated with forms + |----> form_attrib.c -- Usage of field attributes + |----> form_options.c -- Usage of field options + |----> form_simple.c -- A simple form example + |----> form_win.c -- Demo of windows associated with forms diff --git a/forms/form_attrib.c b/forms/form_attrib.c index 93e1632..9974157 100644 --- a/forms/form_attrib.c +++ b/forms/form_attrib.c @@ -1,75 +1,75 @@ -#include - -int main() -{ FIELD *field[3]; - FORM *my_form; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize few color pairs */ - init_pair(1, COLOR_WHITE, COLOR_BLUE); - init_pair(2, COLOR_WHITE, COLOR_BLUE); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 4, 18, 0, 0); - field[1] = new_field(1, 10, 6, 18, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ - set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ - /* are printed in white */ - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - set_current_field(my_form, field[0]); /* Set focus to the colored field */ - mvprintw(4, 10, "Value 1:"); - mvprintw(6, 10, "Value 2:"); - mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_WHITE, COLOR_BLUE); + init_pair(2, COLOR_WHITE, COLOR_BLUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ + set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ + /* are printed in white */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_current_field(my_form, field[0]); /* Set focus to the colored field */ + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_options.c b/forms/form_options.c index b9ef831..5b67dcc 100644 --- a/forms/form_options.c +++ b/forms/form_options.c @@ -1,76 +1,76 @@ -#include - -#define STARTX 15 -#define STARTY 4 -#define WIDTH 25 - -#define N_FIELDS 3 - -int main() -{ FIELD *field[N_FIELDS]; - FORM *my_form; - int ch, i; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize the fields */ - for(i = 0; i < N_FIELDS - 1; ++i) - field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); - field[N_FIELDS - 1] = NULL; - - /* Set field options */ - set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ - - field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ - field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ - field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ - /* after last character is entered */ - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ - set_field_buffer(field[0], 0, "This is a static Field"); - /* Initialize the field */ - mvprintw(STARTY, STARTX - 10, "Field 1:"); - mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +#define STARTX 15 +#define STARTY 4 +#define WIDTH 25 + +#define N_FIELDS 3 + +int main() +{ FIELD *field[N_FIELDS]; + FORM *my_form; + int ch, i; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + for(i = 0; i < N_FIELDS - 1; ++i) + field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); + field[N_FIELDS - 1] = NULL; + + /* Set field options */ + set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ + + field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ + field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ + field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ + /* after last character is entered */ + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ + set_field_buffer(field[0], 0, "This is a static Field"); + /* Initialize the field */ + mvprintw(STARTY, STARTX - 10, "Field 1:"); + mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_simple.c b/forms/form_simple.c index 63f7fe5..520fc92 100644 --- a/forms/form_simple.c +++ b/forms/form_simple.c @@ -1,66 +1,66 @@ -#include - -int main() -{ FIELD *field[3]; - FORM *my_form; - int ch; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 4, 18, 0, 0); - field[1] = new_field(1, 10, 6, 18, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - mvprintw(4, 10, "Value 1:"); - mvprintw(6, 10, "Value 2:"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_win.c b/forms/form_win.c index 8cd3b16..fb00a46 100644 --- a/forms/form_win.c +++ b/forms/form_win.c @@ -1,112 +1,112 @@ -#include - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ - FIELD *field[3]; - FORM *my_form; - WINDOW *my_form_win; - int ch, rows, cols; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize few color pairs */ - init_pair(1, COLOR_RED, COLOR_BLACK); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 6, 1, 0, 0); - field[1] = new_field(1, 10, 8, 1, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_back(field[0], A_UNDERLINE); - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - - /* Calculate the area required for the form */ - scale_form(my_form, &rows, &cols); - - /* Create the window to be associated with the form */ - my_form_win = newwin(rows + 4, cols + 4, 4, 4); - keypad(my_form_win, TRUE); - - /* Set main window and sub window */ - set_form_win(my_form, my_form_win); - set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); - - /* Print a border around the main window and print a title */ - box(my_form_win, 0, 0); - print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); - - post_form(my_form); - wrefresh(my_form_win); - - mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); - refresh(); - - /* Loop through to get user requests */ - while((ch = wgetch(my_form_win)) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ + FIELD *field[3]; + FORM *my_form; + WINDOW *my_form_win; + int ch, rows, cols; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 6, 1, 0, 0); + field[1] = new_field(1, 10, 8, 1, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + + /* Calculate the area required for the form */ + scale_form(my_form, &rows, &cols); + + /* Create the window to be associated with the form */ + my_form_win = newwin(rows + 4, cols + 4, 4, 4); + keypad(my_form_win, TRUE); + + /* Set main window and sub window */ + set_form_win(my_form, my_form_win); + set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); + + /* Print a border around the main window and print a title */ + box(my_form_win, 0, 0); + print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); + + post_form(my_form); + wrefresh(my_form_win); + + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = wgetch(my_form_win)) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/menus/Makefile b/menus/Makefile index b21f8b2..73626e7 100644 --- a/menus/Makefile +++ b/menus/Makefile @@ -9,23 +9,23 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/menu_attrib\ - ${EXE_DIR}/menu_item_data\ - ${EXE_DIR}/menu_multi_column \ - ${EXE_DIR}/menu_scroll \ - ${EXE_DIR}/menu_simple \ - ${EXE_DIR}/menu_toggle \ - ${EXE_DIR}/menu_userptr \ - ${EXE_DIR}/menu_win + ${EXE_DIR}/menu_attrib\ + ${EXE_DIR}/menu_item_data\ + ${EXE_DIR}/menu_multi_column \ + ${EXE_DIR}/menu_scroll \ + ${EXE_DIR}/menu_simple \ + ${EXE_DIR}/menu_toggle \ + ${EXE_DIR}/menu_userptr \ + ${EXE_DIR}/menu_win ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/menus/README b/menus/README index cc08f98..211bfa3 100644 --- a/menus/README +++ b/menus/README @@ -1,13 +1,13 @@ -Description of files +Description of files -------------------- - menus + menus | - |----> menu_attrib.c -- Usage of menu attributes - |----> menu_item_data.c -- Usage of item_name() etc.. functions - |----> menu_multi_column.c -- Creates multi columnar menus - |----> menu_scroll.c -- Demonstrates scrolling capability of menus - |----> menu_simple.c -- A simple menu accessed by arrow keys - |----> menu_toggle.c -- Creates multi valued menus and explains - | -- REQ_TOGGLE_ITEM - |----> menu_userptr.c -- Usage of user pointer - |----> menu_win.c -- Demo of windows associated with menus + |----> menu_attrib.c -- Usage of menu attributes + |----> menu_item_data.c -- Usage of item_name() etc.. functions + |----> menu_multi_column.c -- Creates multi columnar menus + |----> menu_scroll.c -- Demonstrates scrolling capability of menus + |----> menu_simple.c -- A simple menu accessed by arrow keys + |----> menu_toggle.c -- Creates multi valued menus and explains + | -- REQ_TOGGLE_ITEM + |----> menu_userptr.c -- Usage of user pointer + |----> menu_win.c -- Demo of windows associated with menus diff --git a/menus/menu_attrib.c b/menus/menu_attrib.c index 56fad90..fdfdd2c 100644 --- a/menus/menu_attrib.c +++ b/menus/menu_attrib.c @@ -1,80 +1,80 @@ -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_MAGENTA, COLOR_BLACK); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - item_opts_off(my_items[3], O_SELECTABLE); - item_opts_off(my_items[6], O_SELECTABLE); - - /* Create menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Set fore ground and back ground of the menu */ - set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); - set_menu_back(my_menu, COLOR_PAIR(2)); - set_menu_grey(my_menu, COLOR_PAIR(3)); - - /* Post the menu */ - mvprintw(LINES - 3, 0, "Press to see the option selected"); - mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - move(20, 0); - clrtoeol(); - mvprintw(20, 0, "Item selected is : %s", - item_name(current_item(my_menu))); - pos_menu_cursor(my_menu); - break; - } - } - unpost_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - free_menu(my_menu); - endwin(); -} - +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + item_opts_off(my_items[3], O_SELECTABLE); + item_opts_off(my_items[6], O_SELECTABLE); + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set fore ground and back ground of the menu */ + set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); + set_menu_back(my_menu, COLOR_PAIR(2)); + set_menu_grey(my_menu, COLOR_PAIR(3)); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", + item_name(current_item(my_menu))); + pos_menu_cursor(my_menu); + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_item_data.c b/menus/menu_item_data.c index d33998e..c51ebc1 100644 --- a/menus/menu_item_data.c +++ b/menus/menu_item_data.c @@ -1,66 +1,66 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - cur_item = current_item(my_menu); - move(LINES - 2, 0); - clrtoeol(); - mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", - item_index(cur_item) + 1, item_name(cur_item), - item_description(cur_item)); - - refresh(); - pos_menu_cursor(my_menu); - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + cur_item = current_item(my_menu); + move(LINES - 2, 0); + clrtoeol(); + mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", + item_index(cur_item) + 1, item_name(cur_item), + item_description(cur_item)); + + refresh(); + pos_menu_cursor(my_menu); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_multi_column.c b/menus/menu_multi_column.c index 1fa6e02..d507610 100644 --- a/menus/menu_multi_column.c +++ b/menus/menu_multi_column.c @@ -1,97 +1,97 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", - "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", - "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", - "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", - "Exit", - (char *)NULL, - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_CYAN, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Set menu option not to show the description */ - menu_opts_off(my_menu, O_SHOWDESC); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 70, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); - set_menu_format(my_menu, 5, 3); - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - - attron(COLOR_PAIR(2)); - mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); - mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); - attroff(COLOR_PAIR(2)); - refresh(); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case KEY_LEFT: - menu_driver(my_menu, REQ_LEFT_ITEM); - break; - case KEY_RIGHT: - menu_driver(my_menu, REQ_RIGHT_ITEM); - break; - case KEY_NPAGE: - menu_driver(my_menu, REQ_SCR_DPAGE); - break; - case KEY_PPAGE: - menu_driver(my_menu, REQ_SCR_UPAGE); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", + "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", + "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", + "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", + "Exit", + (char *)NULL, + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set menu option not to show the description */ + menu_opts_off(my_menu, O_SHOWDESC); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 70, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); + set_menu_format(my_menu, 5, 3); + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); + mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_LEFT: + menu_driver(my_menu, REQ_LEFT_ITEM); + break; + case KEY_RIGHT: + menu_driver(my_menu, REQ_RIGHT_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} diff --git a/menus/menu_scroll.c b/menus/menu_scroll.c index 40618f2..2d9c627 100644 --- a/menus/menu_scroll.c +++ b/menus/menu_scroll.c @@ -1,124 +1,124 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Choice 8", - "Choice 9", - "Choice 10", - "Exit", - (char *)NULL, - }; -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_CYAN, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 40, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); - set_menu_format(my_menu, 5, 1); - - /* Set menu mark to the string " * " */ - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); - mvwaddch(my_menu_win, 2, 0, ACS_LTEE); - mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); - mvwaddch(my_menu_win, 2, 39, ACS_RTEE); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - attron(COLOR_PAIR(2)); - mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); - mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); - attroff(COLOR_PAIR(2)); - refresh(); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case KEY_NPAGE: - menu_driver(my_menu, REQ_SCR_DPAGE); - break; - case KEY_PPAGE: - menu_driver(my_menu, REQ_SCR_UPAGE); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Choice 8", + "Choice 9", + "Choice 10", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + set_menu_format(my_menu, 5, 1); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); + mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/menus/menu_simple.c b/menus/menu_simple.c index 086fdf7..1222c7d 100644 --- a/menus/menu_simple.c +++ b/menus/menu_simple.c @@ -1,56 +1,56 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - mvprintw(LINES - 2, 0, "F1 to Exit"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + mvprintw(LINES - 2, 0, "F1 to Exit"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_toggle.c b/menus/menu_toggle.c index 60a052f..f6e8d7e 100644 --- a/menus/menu_toggle.c +++ b/menus/menu_toggle.c @@ -1,84 +1,84 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - - /* Make the menu multi valued */ - menu_opts_off(my_menu, O_ONEVALUE); - - mvprintw(LINES - 3, 0, "Use to select or unselect an item."); - mvprintw(LINES - 2, 0, " to see presently selected items(F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case ' ': - menu_driver(my_menu, REQ_TOGGLE_ITEM); - break; - case 10: /* Enter */ - { char temp[200]; - ITEM **items; - - items = menu_items(my_menu); - temp[0] = '\0'; - for(i = 0; i < item_count(my_menu); ++i) - if(item_value(items[i]) == TRUE) - { strcat(temp, item_name(items[i])); - strcat(temp, " "); - } - move(20, 0); - clrtoeol(); - mvprintw(20, 0, temp); - refresh(); - } - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + + /* Make the menu multi valued */ + menu_opts_off(my_menu, O_ONEVALUE); + + mvprintw(LINES - 3, 0, "Use to select or unselect an item."); + mvprintw(LINES - 2, 0, " to see presently selected items(F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case ' ': + menu_driver(my_menu, REQ_TOGGLE_ITEM); + break; + case 10: /* Enter */ + { char temp[200]; + ITEM **items; + + items = menu_items(my_menu); + temp[0] = '\0'; + for(i = 0; i < item_count(my_menu); ++i) + if(item_value(items[i]) == TRUE) + { strcat(temp, item_name(items[i])); + strcat(temp, " "); + } + move(20, 0); + clrtoeol(); + mvprintw(20, 0, temp); + refresh(); + } + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_userptr.c b/menus/menu_userptr.c index d2e02d4..baf6a6c 100644 --- a/menus/menu_userptr.c +++ b/menus/menu_userptr.c @@ -1,87 +1,87 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; -void func(char *name); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_MAGENTA, COLOR_BLACK); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - { my_items[i] = new_item(choices[i], choices[i]); - /* Set the user pointer */ - set_item_userptr(my_items[i], func); - } - my_items[n_choices] = (ITEM *)NULL; - - /* Create menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Post the menu */ - mvprintw(LINES - 3, 0, "Press to see the option selected"); - mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - { ITEM *cur; - void (*p)(char *); - - cur = current_item(my_menu); - p = item_userptr(cur); - p((char *)item_name(cur)); - pos_menu_cursor(my_menu); - break; - } - break; - } - } - unpost_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - free_menu(my_menu); - endwin(); -} - -void func(char *name) -{ move(20, 0); - clrtoeol(); - mvprintw(20, 0, "Item selected is : %s", name); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; +void func(char *name); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + { my_items[i] = new_item(choices[i], choices[i]); + /* Set the user pointer */ + set_item_userptr(my_items[i], func); + } + my_items[n_choices] = (ITEM *)NULL; + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + { ITEM *cur; + void (*p)(char *); + + cur = current_item(my_menu); + p = item_userptr(cur); + p((char *)item_name(cur)); + pos_menu_cursor(my_menu); + break; + } + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + +void func(char *name) +{ move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", name); +} diff --git a/menus/menu_win.c b/menus/menu_win.c index c3387ed..3514086 100644 --- a/menus/menu_win.c +++ b/menus/menu_win.c @@ -1,105 +1,105 @@ -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - (char *)NULL, - }; -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 40, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); - - /* Set menu mark to the string " * " */ - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); - mvwaddch(my_menu_win, 2, 0, ACS_LTEE); - mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); - mvwaddch(my_menu_win, 2, 39, ACS_RTEE); - mvprintw(LINES - 2, 0, "F1 to exit"); - refresh(); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + mvprintw(LINES - 2, 0, "F1 to exit"); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/Makefile b/panels/Makefile index 41a7d88..242df7c 100644 --- a/panels/Makefile +++ b/panels/Makefile @@ -9,19 +9,19 @@ SRC_DIR=. EXE_DIR=../demo/exe EXES = \ - ${EXE_DIR}/panel_browse \ - ${EXE_DIR}/panel_hide \ - ${EXE_DIR}/panel_resize \ - ${EXE_DIR}/panel_simple + ${EXE_DIR}/panel_browse \ + ${EXE_DIR}/panel_hide \ + ${EXE_DIR}/panel_resize \ + ${EXE_DIR}/panel_simple ${EXE_DIR}/%: %.o - ${CC} -o $@ $< ${LIBS} + ${CC} -o $@ $< ${LIBS} %.o: ${SRC_DIR}/%.c - ${CC} -o $@ -c $< + ${CC} -o $@ -c $< all: ${EXES} clean: - @rm -f ${EXES} + @rm -f ${EXES} diff --git a/panels/README b/panels/README index 90a3bb8..ed34678 100644 --- a/panels/README +++ b/panels/README @@ -1,8 +1,8 @@ -Description of files +Description of files -------------------- - panels + panels | - |----> panel_browse.c -- Panel browsing through tab. Usage of user pointer - |----> panel_hide.c -- Hiding and Un hiding of panels - |----> panel_resize.c -- Moving and resizing of panels - |----> panel_simple.c -- A simple panel example + |----> panel_browse.c -- Panel browsing through tab. Usage of user pointer + |----> panel_hide.c -- Hiding and Un hiding of panels + |----> panel_resize.c -- Moving and resizing of panels + |----> panel_simple.c -- A simple panel example diff --git a/panels/panel_browse.c b/panels/panel_browse.c index 013c066..45fd839 100644 --- a/panels/panel_browse.c +++ b/panels/panel_browse.c @@ -1,117 +1,117 @@ -#include - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL *top; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Set up the user pointers to the next panel */ - set_panel_userptr(my_panels[0], my_panels[1]); - set_panel_userptr(my_panels[1], my_panels[2]); - set_panel_userptr(my_panels[2], my_panels[0]); - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - doupdate(); - - top = my_panels[2]; - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 9: - top = (PANEL *)panel_userptr(top); - top_panel(top); - break; - } - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL *top; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Set up the user pointers to the next panel */ + set_panel_userptr(my_panels[0], my_panels[1]); + set_panel_userptr(my_panels[1], my_panels[2]); + set_panel_userptr(my_panels[2], my_panels[0]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + top = my_panels[2]; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: + top = (PANEL *)panel_userptr(top); + top_panel(top); + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_hide.c b/panels/panel_hide.c index 457199c..40f6c94 100644 --- a/panels/panel_hide.c +++ b/panels/panel_hide.c @@ -1,156 +1,156 @@ -#include - -typedef struct _PANEL_DATA { - int hide; /* TRUE if panel is hidden */ -}PANEL_DATA; - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL_DATA panel_datas[3]; - PANEL_DATA *temp; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Initialize panel datas saying that nothing is hidden */ - panel_datas[0].hide = FALSE; - panel_datas[1].hide = FALSE; - panel_datas[2].hide = FALSE; - - set_panel_userptr(my_panels[0], &panel_datas[0]); - set_panel_userptr(my_panels[1], &panel_datas[1]); - set_panel_userptr(my_panels[2], &panel_datas[2]); - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); - mvprintw(LINES - 2, 0, "F1 to Exit"); - - attroff(COLOR_PAIR(4)); - doupdate(); - - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 'a': - temp = (PANEL_DATA *)panel_userptr(my_panels[0]); - if(temp->hide == FALSE) - { hide_panel(my_panels[0]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[0]); - temp->hide = FALSE; - } - break; - case 'b': - temp = (PANEL_DATA *)panel_userptr(my_panels[1]); - if(temp->hide == FALSE) - { hide_panel(my_panels[1]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[1]); - temp->hide = FALSE; - } - break; - case 'c': - temp = (PANEL_DATA *)panel_userptr(my_panels[2]); - if(temp->hide == FALSE) - { hide_panel(my_panels[2]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[2]); - temp->hide = FALSE; - } - break; - } - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +typedef struct _PANEL_DATA { + int hide; /* TRUE if panel is hidden */ +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA panel_datas[3]; + PANEL_DATA *temp; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Initialize panel datas saying that nothing is hidden */ + panel_datas[0].hide = FALSE; + panel_datas[1].hide = FALSE; + panel_datas[2].hide = FALSE; + + set_panel_userptr(my_panels[0], &panel_datas[0]); + set_panel_userptr(my_panels[1], &panel_datas[1]); + set_panel_userptr(my_panels[2], &panel_datas[2]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); + mvprintw(LINES - 2, 0, "F1 to Exit"); + + attroff(COLOR_PAIR(4)); + doupdate(); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 'a': + temp = (PANEL_DATA *)panel_userptr(my_panels[0]); + if(temp->hide == FALSE) + { hide_panel(my_panels[0]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[0]); + temp->hide = FALSE; + } + break; + case 'b': + temp = (PANEL_DATA *)panel_userptr(my_panels[1]); + if(temp->hide == FALSE) + { hide_panel(my_panels[1]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[1]); + temp->hide = FALSE; + } + break; + case 'c': + temp = (PANEL_DATA *)panel_userptr(my_panels[2]); + if(temp->hide == FALSE) + { hide_panel(my_panels[2]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[2]); + temp->hide = FALSE; + } + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_resize.c b/panels/panel_resize.c index e77ba0d..71d331f 100644 --- a/panels/panel_resize.c +++ b/panels/panel_resize.c @@ -1,234 +1,234 @@ -#include - -typedef struct _PANEL_DATA { - int x, y, w, h; - char label[80]; - int label_color; - PANEL *next; -}PANEL_DATA; - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); -void set_user_ptrs(PANEL **panels, int n); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL_DATA *top; - PANEL *stack_top; - WINDOW *temp_win, *old_win; - int ch; - int newx, newy, neww, newh; - int size = FALSE, move = FALSE; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - set_user_ptrs(my_panels, 3); - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - doupdate(); - - stack_top = my_panels[2]; - top = (PANEL_DATA *)panel_userptr(stack_top); - newx = top->x; - newy = top->y; - neww = top->w; - newh = top->h; - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 9: /* Tab */ - top = (PANEL_DATA *)panel_userptr(stack_top); - top_panel(top->next); - stack_top = top->next; - top = (PANEL_DATA *)panel_userptr(stack_top); - newx = top->x; - newy = top->y; - neww = top->w; - newh = top->h; - break; - case 'r': /* Re-Size*/ - size = TRUE; - attron(COLOR_PAIR(4)); - mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press to end resizing"); - refresh(); - attroff(COLOR_PAIR(4)); - break; - case 'm': /* Move */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press to end moving"); - refresh(); - attroff(COLOR_PAIR(4)); - move = TRUE; - break; - case KEY_LEFT: - if(size == TRUE) - { --newx; - ++neww; - } - if(move == TRUE) - --newx; - break; - case KEY_RIGHT: - if(size == TRUE) - { ++newx; - --neww; - } - if(move == TRUE) - ++newx; - break; - case KEY_UP: - if(size == TRUE) - { --newy; - ++newh; - } - if(move == TRUE) - --newy; - break; - case KEY_DOWN: - if(size == TRUE) - { ++newy; - --newh; - } - if(move == TRUE) - ++newy; - break; - case 10: /* Enter */ - move(LINES - 4, 0); - clrtoeol(); - refresh(); - if(size == TRUE) - { old_win = panel_window(stack_top); - temp_win = newwin(newh, neww, newy, newx); - replace_panel(stack_top, temp_win); - win_show(temp_win, top->label, top->label_color); - delwin(old_win); - size = FALSE; - } - if(move == TRUE) - { move_panel(stack_top, newy, newx); - move = FALSE; - } - break; - - } - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - refresh(); - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Set the PANEL_DATA structures for individual panels */ -void set_user_ptrs(PANEL **panels, int n) -{ PANEL_DATA *ptrs; - WINDOW *win; - int x, y, w, h, i; - char temp[80]; - - ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); - - for(i = 0;i < n; ++i) - { win = panel_window(panels[i]); - getbegyx(win, y, x); - getmaxyx(win, h, w); - ptrs[i].x = x; - ptrs[i].y = y; - ptrs[i].w = w; - ptrs[i].h = h; - sprintf(temp, "Window Number %d", i + 1); - strcpy(ptrs[i].label, temp); - ptrs[i].label_color = i + 1; - if(i + 1 == n) - ptrs[i].next = panels[0]; - else - ptrs[i].next = panels[i + 1]; - set_panel_userptr(panels[i], &ptrs[i]); - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +typedef struct _PANEL_DATA { + int x, y, w, h; + char label[80]; + int label_color; + PANEL *next; +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); +void set_user_ptrs(PANEL **panels, int n); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA *top; + PANEL *stack_top; + WINDOW *temp_win, *old_win; + int ch; + int newx, newy, neww, newh; + int size = FALSE, move = FALSE; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + set_user_ptrs(my_panels, 3); + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + stack_top = my_panels[2]; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: /* Tab */ + top = (PANEL_DATA *)panel_userptr(stack_top); + top_panel(top->next); + stack_top = top->next; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + break; + case 'r': /* Re-Size*/ + size = TRUE; + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press to end resizing"); + refresh(); + attroff(COLOR_PAIR(4)); + break; + case 'm': /* Move */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press to end moving"); + refresh(); + attroff(COLOR_PAIR(4)); + move = TRUE; + break; + case KEY_LEFT: + if(size == TRUE) + { --newx; + ++neww; + } + if(move == TRUE) + --newx; + break; + case KEY_RIGHT: + if(size == TRUE) + { ++newx; + --neww; + } + if(move == TRUE) + ++newx; + break; + case KEY_UP: + if(size == TRUE) + { --newy; + ++newh; + } + if(move == TRUE) + --newy; + break; + case KEY_DOWN: + if(size == TRUE) + { ++newy; + --newh; + } + if(move == TRUE) + ++newy; + break; + case 10: /* Enter */ + move(LINES - 4, 0); + clrtoeol(); + refresh(); + if(size == TRUE) + { old_win = panel_window(stack_top); + temp_win = newwin(newh, neww, newy, newx); + replace_panel(stack_top, temp_win); + win_show(temp_win, top->label, top->label_color); + delwin(old_win); + size = FALSE; + } + if(move == TRUE) + { move_panel(stack_top, newy, newx); + move = FALSE; + } + break; + + } + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + refresh(); + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Set the PANEL_DATA structures for individual panels */ +void set_user_ptrs(PANEL **panels, int n) +{ PANEL_DATA *ptrs; + WINDOW *win; + int x, y, w, h, i; + char temp[80]; + + ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); + + for(i = 0;i < n; ++i) + { win = panel_window(panels[i]); + getbegyx(win, y, x); + getmaxyx(win, h, w); + ptrs[i].x = x; + ptrs[i].y = y; + ptrs[i].w = w; + ptrs[i].h = h; + sprintf(temp, "Window Number %d", i + 1); + strcpy(ptrs[i].label, temp); + ptrs[i].label_color = i + 1; + if(i + 1 == n) + ptrs[i].next = panels[0]; + else + ptrs[i].next = panels[i + 1]; + set_panel_userptr(panels[i], &ptrs[i]); + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_simple.c b/panels/panel_simple.c index 2ce8c18..d98aeef 100644 --- a/panels/panel_simple.c +++ b/panels/panel_simple.c @@ -1,38 +1,38 @@ -#include - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - int lines = 10, cols = 40, y = 2, x = 4, i; - - initscr(); - cbreak(); - noecho(); - - /* Create windows for the panels */ - my_wins[0] = newwin(lines, cols, y, x); - my_wins[1] = newwin(lines, cols, y + 1, x + 5); - my_wins[2] = newwin(lines, cols, y + 2, x + 10); - - /* - * Create borders around the windows so that you can see the effect - * of panels - */ - for(i = 0; i < 3; ++i) - box(my_wins[i], 0, 0); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - doupdate(); - - getch(); - endwin(); -} - +#include + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + int lines = 10, cols = 40, y = 2, x = 4, i; + + initscr(); + cbreak(); + noecho(); + + /* Create windows for the panels */ + my_wins[0] = newwin(lines, cols, y, x); + my_wins[1] = newwin(lines, cols, y + 1, x + 5); + my_wins[2] = newwin(lines, cols, y + 2, x + 10); + + /* + * Create borders around the windows so that you can see the effect + * of panels + */ + for(i = 0; i < 3; ++i) + box(my_wins[i], 0, 0); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + doupdate(); + + getch(); + endwin(); +} + diff --git a/perl/05.pl b/perl/05.pl index dd08535..dc61845 100755 --- a/perl/05.pl +++ b/perl/05.pl @@ -20,9 +20,9 @@ $lines = join "", @lines; while ($lines =~ /\G(.*?)(\/\*.*?\*\/)?/gs) { addstr($1); if ($2) { - attron(A_BOLD); - addstr($2); - attroff(A_BOLD); + attron(A_BOLD); + addstr($2); + attroff(A_BOLD); } } diff --git a/perl/07.pl b/perl/07.pl index eea4cd8..06a7549 100755 --- a/perl/07.pl +++ b/perl/07.pl @@ -23,20 +23,20 @@ $my_win = create_newwin($height, $width, $starty, $startx); while (($ch = getch()) != KEY_F(1)) { if ($ch == KEY_LEFT) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, $starty, --$startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, $starty, --$startx); } elsif ($ch == KEY_RIGHT) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, $starty, ++$startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, $starty, ++$startx); } elsif ($ch == KEY_UP) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, --$starty, $startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, --$starty, $startx); } elsif ($ch == KEY_DOWN) { - destroy_win($my_win); - $my_win = create_newwin($height, $width, ++$starty, $startx); + destroy_win($my_win); + $my_win = create_newwin($height, $width, ++$starty, $startx); } } diff --git a/perl/08.pl b/perl/08.pl index 37077ec..4eb044f 100755 --- a/perl/08.pl +++ b/perl/08.pl @@ -32,24 +32,24 @@ create_box(\%win, 1); while (($ch = getch()) != KEY_F(1)) { if ($ch == KEY_LEFT) { - create_box(\%win, 0); - $win{'startx'}--; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'startx'}--; + create_box(\%win, 1); } elsif ($ch == KEY_RIGHT) { - create_box(\%win, 0); - $win{'startx'}++; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'startx'}++; + create_box(\%win, 1); } elsif ($ch == KEY_UP) { - create_box(\%win, 0); - $win{'starty'}--; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'starty'}--; + create_box(\%win, 1); } elsif ($ch == KEY_DOWN) { - create_box(\%win, 0); - $win{'starty'}++; - create_box(\%win, 1); + create_box(\%win, 0); + $win{'starty'}++; + create_box(\%win, 1); } } @@ -74,10 +74,10 @@ sub init_win_params { sub print_win_params { if ($_DEBUG) { - $p_win = shift; - addstr(25, 0, - "$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}"); - refresh(); + $p_win = shift; + addstr(25, 0, + "$$p_win{startx} $$p_win{starty} $$p_win{width} $$p_win{height}"); + refresh(); } } @@ -91,21 +91,21 @@ sub create_box { $h = $$p_win{'height'}; if ($bool) { - addch($y, $x, $$p_win{'tl'}); - addch($y, $x + $w, $$p_win{'tr'}); - addch($y + $h, $x, $$p_win{'bl'}); - addch($y + $h, $x + $w, $$p_win{'br'}); - hline($y, $x + 1, $$p_win{'ts'}, $w - 1); - hline($y + $h, $x + 1, $$p_win{'bs'}, $w - 1); - vline($y + 1, $x, $$p_win{'ls'}, $h - 1); - vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1); + addch($y, $x, $$p_win{'tl'}); + addch($y, $x + $w, $$p_win{'tr'}); + addch($y + $h, $x, $$p_win{'bl'}); + addch($y + $h, $x + $w, $$p_win{'br'}); + hline($y, $x + 1, $$p_win{'ts'}, $w - 1); + hline($y + $h, $x + 1, $$p_win{'bs'}, $w - 1); + vline($y + 1, $x, $$p_win{'ls'}, $h - 1); + vline($y + 1, $x + $w, $$p_win{'rs'}, $h - 1); } else { - for ($j = $y; $j <= $y + $h; $j++) { - for ($i = $x; $i <= $x + $w; $i++) { - addch($j, $i, ' '); - } - } + for ($j = $y; $j <= $y + $h; $j++) { + for ($i = $x; $i <= $x + $w; $i++) { + addch($j, $i, ' '); + } + } } refresh(); } diff --git a/perl/09.pl b/perl/09.pl index c89af93..235fc82 100755 --- a/perl/09.pl +++ b/perl/09.pl @@ -34,7 +34,7 @@ sub print_in_middle { $win = stdscr unless ($win); getyx($win, $y, $x); - + $x = $startx if ($startx); $y = $starty if ($starty); $width = $COLS unless ($width); diff --git a/perl/10.pl b/perl/10.pl index 1a59d50..7353ddb 100755 --- a/perl/10.pl +++ b/perl/10.pl @@ -44,27 +44,27 @@ print_menu($menu_win, $highlight); while (1) { $c = getch($menu_win); if ($c == KEY_UP) { - if ($highlight == 1) { - $highlight = $n_choices; - } - else { - $highlight--; - } + if ($highlight == 1) { + $highlight = $n_choices; + } + else { + $highlight--; + } } elsif ($c == KEY_DOWN) { - if ($highlight == $n_choices) { - $highlight = 1; - } - else { - $highlight++; - } + if ($highlight == $n_choices) { + $highlight = 1; + } + else { + $highlight++; + } } elsif ($c == '\n') { - $choice = $highlight; + $choice = $highlight; } else { - addstr($LINES - 2, 0, "Character pressed is $c"); - refresh(); + addstr($LINES - 2, 0, "Character pressed is $c"); + refresh(); } print_menu($menu_win, $highlight); last if ($choice); @@ -83,15 +83,15 @@ sub print_menu { $y = 2; box($menu_win, 0, 0); for ($i = 0; $i < $n_choices; $i++) { - if ($highlight == $i + 1) { - attron($menu_win, A_REVERSE); - addstr($menu_win, $y, $x, $choices[$i]); - attroff($menu_win, A_REVERSE); - } - else { - addstr($menu_win, $y, $x, $choices[$i]); - } - $y++; + if ($highlight == $i + 1) { + attron($menu_win, A_REVERSE); + addstr($menu_win, $y, $x, $choices[$i]); + attroff($menu_win, A_REVERSE); + } + else { + addstr($menu_win, $y, $x, $choices[$i]); + } + $y++; } refresh($menu_win); } From b1f1195c3d8dbc796efbe24c0dba554ea0557b49 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:38:43 -0500 Subject: [PATCH 06/21] doing some find+sed cleanup --- JustForFun/hanoi.c | 356 ++++++++++++++-------------- JustForFun/life.c | 214 ++++++++--------- JustForFun/magic.c | 322 ++++++++++++------------- JustForFun/queens.c | 244 +++++++++---------- JustForFun/shuffle.c | 410 ++++++++++++++++---------------- JustForFun/tt.c | 446 +++++++++++++++++------------------ basics/acs_vars.c | 88 +++---- basics/hello_world.c | 24 +- basics/init_func_example.c | 62 ++--- basics/key_code.c | 28 +-- basics/mouse_menu.c | 212 ++++++++--------- basics/other_border.c | 240 +++++++++---------- basics/printw_example.c | 40 ++-- basics/scanw_example.c | 40 ++-- basics/simple_attr.c | 36 +-- basics/simple_color.c | 80 +++---- basics/simple_key.c | 184 +++++++-------- basics/temp_leave.c | 40 ++-- basics/win_border.c | 164 ++++++------- basics/with_chgat.c | 48 ++-- forms/form_attrib.c | 150 ++++++------ forms/form_options.c | 152 ++++++------ forms/form_simple.c | 132 +++++------ forms/form_win.c | 224 +++++++++--------- menus/menu_attrib.c | 160 ++++++------- menus/menu_item_data.c | 132 +++++------ menus/menu_multi_column.c | 194 +++++++-------- menus/menu_scroll.c | 248 ++++++++++---------- menus/menu_simple.c | 112 ++++----- menus/menu_toggle.c | 168 ++++++------- menus/menu_userptr.c | 174 +++++++------- menus/menu_win.c | 210 ++++++++--------- panels/panel_browse.c | 234 +++++++++---------- panels/panel_hide.c | 312 ++++++++++++------------- panels/panel_resize.c | 468 ++++++++++++++++++------------------- panels/panel_simple.c | 76 +++--- 36 files changed, 3212 insertions(+), 3212 deletions(-) diff --git a/JustForFun/hanoi.c b/JustForFun/hanoi.c index b57b0b7..4085a93 100644 --- a/JustForFun/hanoi.c +++ b/JustForFun/hanoi.c @@ -1,178 +1,178 @@ -#include - -#define POSX 10 -#define POSY 5 -#define DISC_CHAR '*' -#define PEG_CHAR '#' -#define TIME_OUT 300 - -typedef struct _peg_struct { - int n_discs; /* Number of discs at present */ - int bottomx, bottomy; /* bottom x, bottom y co-ord */ - int *sizes; /* The disc sizes array */ -}peg; - -void init_pegs(peg *p_my_pegs, int n_discs); -void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs); -void free_pegs(peg *p_my_pegs, int n_discs); -void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst); -void move_disc(peg *p_my_pegs, int n_discs, int src, int dst); -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); -void check_usr_response(peg *p_my_pegs, int n_discs); - -int store_n_discs; -char *welcome_string = "Enter the number of discs you want to be solved: "; - -int main(int argc, char *argv[]) -{ int n_discs; - peg my_pegs[3]; - - initscr(); /* Start curses mode */ - cbreak(); /* Line buffering disabled. Pass on every thing */ - keypad(stdscr, TRUE); - curs_set(FALSE); - - print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); - scanw("%d", &n_discs); - - timeout(TIME_OUT); - noecho(); - store_n_discs = n_discs; - - init_pegs(my_pegs, n_discs); - show_pegs(stdscr, my_pegs, n_discs); - solve_hanoi(my_pegs, n_discs, 0, 1, 2); - - free_pegs(my_pegs, n_discs); - endwin(); /* End curses mode */ - return 0; -} - -void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) -{ if(n_discs == 0) - return; - solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); - move_disc(p_my_pegs, store_n_discs, src, dst); - show_pegs(stdscr, p_my_pegs, store_n_discs); - check_usr_response(p_my_pegs, store_n_discs); - solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst); -} - -void check_usr_response(peg *p_my_pegs, int n_discs) -{ int ch; - - ch = getch(); /* Waits for TIME_OUT milliseconds */ - if(ch == ERR) - return; - else - if(ch == KEY_F(1)) - { free_pegs(p_my_pegs, n_discs); - endwin(); - exit(0); - } -} - -void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) -{ int temp, index; - - --p_my_pegs[src].n_discs; - index = 0; - while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) - ++index; - temp = p_my_pegs[src].sizes[index]; - p_my_pegs[src].sizes[index] = 0; - - index = 0; - while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) - ++index; - --index; - p_my_pegs[dst].sizes[index] = temp; - ++p_my_pegs[dst].n_discs; -} - -void init_pegs(peg *p_my_pegs, int n_discs) -{ int size, temp, i; - - p_my_pegs[0].n_discs = n_discs; - - /* Allocate memory for size array - * atmost the number of discs on a peg can be n_discs - */ - for(i = 0; i < n_discs; ++i) - p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); - size = 3; - for(i = 0;i < n_discs; ++i, size += 2) - p_my_pegs[0].sizes[i] = size; - - temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); - p_my_pegs[0].bottomx = POSX + 1 + temp; - p_my_pegs[0].bottomy = POSY + 2 + n_discs; - - p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp; - p_my_pegs[1].bottomy = POSY + 2 + n_discs; - - p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp; - p_my_pegs[2].bottomy = POSY + 2 + n_discs; -} - -void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) -{ int i, j, k, x, y, size; - - wclear(win); - attron(A_REVERSE); - mvprintw(24, 0, "Press F1 to Exit"); - attroff(A_REVERSE); - for(i = 0;i < 3; ++i) - mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, - p_my_pegs[i].bottomx, "%c", PEG_CHAR); - y = p_my_pegs[0].bottomy - n_discs; - for(i = 0; i < 3; ++i) /* For each peg */ - { for(j = 0; j < n_discs; ++ j) /* For each row */ - { if(p_my_pegs[i].sizes[j] != 0) - { size = p_my_pegs[i].sizes[j]; - x = p_my_pegs[i].bottomx - (size / 2); - for(k = 0; k < size; ++k) - mvwprintw(win, y, x + k, "%c", DISC_CHAR); - } - else - mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); - ++y; - } - y = p_my_pegs[0].bottomy - n_discs; - } - wrefresh(win); -} - -void free_pegs(peg *p_my_pegs, int n_discs) -{ int i; - - for(i = 0;i < n_discs; ++i) - free(p_my_pegs[i].sizes); -} - -/* -------------------------------------------------------------* - * startx = 0 means at present x * - * starty = 0 means at present y * - * win = NULL means take stdscr * - * -------------------------------------------------------------*/ - -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} +#include + +#define POSX 10 +#define POSY 5 +#define DISC_CHAR '*' +#define PEG_CHAR '#' +#define TIME_OUT 300 + +typedef struct _peg_struct { + int n_discs; /* Number of discs at present */ + int bottomx, bottomy; /* bottom x, bottom y co-ord */ + int *sizes; /* The disc sizes array */ +}peg; + +void init_pegs(peg *p_my_pegs, int n_discs); +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs); +void free_pegs(peg *p_my_pegs, int n_discs); +void solve_hanoi(peg *p_my_pegs, int n, int src, int aux, int dst); +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); +void check_usr_response(peg *p_my_pegs, int n_discs); + +int store_n_discs; +char *welcome_string = "Enter the number of discs you want to be solved: "; + +int main(int argc, char *argv[]) +{ int n_discs; + peg my_pegs[3]; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled. Pass on every thing */ + keypad(stdscr, TRUE); + curs_set(FALSE); + + print_in_middle(0, LINES / 2, COLS, welcome_string, NULL); + scanw("%d", &n_discs); + + timeout(TIME_OUT); + noecho(); + store_n_discs = n_discs; + + init_pegs(my_pegs, n_discs); + show_pegs(stdscr, my_pegs, n_discs); + solve_hanoi(my_pegs, n_discs, 0, 1, 2); + + free_pegs(my_pegs, n_discs); + endwin(); /* End curses mode */ + return 0; +} + +void solve_hanoi(peg *p_my_pegs, int n_discs, int src, int aux, int dst) +{ if(n_discs == 0) + return; + solve_hanoi(p_my_pegs, n_discs - 1, src, dst, aux); + move_disc(p_my_pegs, store_n_discs, src, dst); + show_pegs(stdscr, p_my_pegs, store_n_discs); + check_usr_response(p_my_pegs, store_n_discs); + solve_hanoi(p_my_pegs, n_discs - 1, aux, src, dst); +} + +void check_usr_response(peg *p_my_pegs, int n_discs) +{ int ch; + + ch = getch(); /* Waits for TIME_OUT milliseconds */ + if(ch == ERR) + return; + else + if(ch == KEY_F(1)) + { free_pegs(p_my_pegs, n_discs); + endwin(); + exit(0); + } +} + +void move_disc(peg *p_my_pegs, int n_discs, int src, int dst) +{ int temp, index; + + --p_my_pegs[src].n_discs; + index = 0; + while(p_my_pegs[src].sizes[index] == 0 && index != n_discs) + ++index; + temp = p_my_pegs[src].sizes[index]; + p_my_pegs[src].sizes[index] = 0; + + index = 0; + while(p_my_pegs[dst].sizes[index] == 0 && index != n_discs) + ++index; + --index; + p_my_pegs[dst].sizes[index] = temp; + ++p_my_pegs[dst].n_discs; +} + +void init_pegs(peg *p_my_pegs, int n_discs) +{ int size, temp, i; + + p_my_pegs[0].n_discs = n_discs; + + /* Allocate memory for size array + * atmost the number of discs on a peg can be n_discs + */ + for(i = 0; i < n_discs; ++i) + p_my_pegs[i].sizes = (int *)calloc(n_discs, sizeof(int)); + size = 3; + for(i = 0;i < n_discs; ++i, size += 2) + p_my_pegs[0].sizes[i] = size; + + temp = (p_my_pegs[0].sizes[n_discs - 1] / 2); + p_my_pegs[0].bottomx = POSX + 1 + temp; + p_my_pegs[0].bottomy = POSY + 2 + n_discs; + + p_my_pegs[1].bottomx = p_my_pegs[0].bottomx + 2 + 2 * temp; + p_my_pegs[1].bottomy = POSY + 2 + n_discs; + + p_my_pegs[2].bottomx = p_my_pegs[1].bottomx + 2 + 2 * temp; + p_my_pegs[2].bottomy = POSY + 2 + n_discs; +} + +void show_pegs(WINDOW *win, peg *p_my_pegs, int n_discs) +{ int i, j, k, x, y, size; + + wclear(win); + attron(A_REVERSE); + mvprintw(24, 0, "Press F1 to Exit"); + attroff(A_REVERSE); + for(i = 0;i < 3; ++i) + mvwprintw( win, p_my_pegs[i].bottomy - n_discs - 1, + p_my_pegs[i].bottomx, "%c", PEG_CHAR); + y = p_my_pegs[0].bottomy - n_discs; + for(i = 0; i < 3; ++i) /* For each peg */ + { for(j = 0; j < n_discs; ++ j) /* For each row */ + { if(p_my_pegs[i].sizes[j] != 0) + { size = p_my_pegs[i].sizes[j]; + x = p_my_pegs[i].bottomx - (size / 2); + for(k = 0; k < size; ++k) + mvwprintw(win, y, x + k, "%c", DISC_CHAR); + } + else + mvwprintw(win, y, p_my_pegs[i].bottomx, "%c", PEG_CHAR); + ++y; + } + y = p_my_pegs[0].bottomy - n_discs; + } + wrefresh(win); +} + +void free_pegs(peg *p_my_pegs, int n_discs) +{ int i; + + for(i = 0;i < n_discs; ++i) + free(p_my_pegs[i].sizes); +} + +/* -------------------------------------------------------------* + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * -------------------------------------------------------------*/ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/JustForFun/life.c b/JustForFun/life.c index eb6a039..99da8a3 100644 --- a/JustForFun/life.c +++ b/JustForFun/life.c @@ -1,107 +1,107 @@ -#include - -int STARTX = 0; -int STARTY = 0; -int ENDX = 79; -int ENDY = 24; - -#define CELL_CHAR '#' -#define TIME_OUT 300 - -typedef struct _state { - int oldstate; - int newstate; -}state; - -void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); -void calc(state **area, int x, int y); -void update_state(state **area, int startx, int starty, int endx, int endy); - -int main() -{ state **workarea; - int i, j; - - initscr(); - cbreak(); - timeout(TIME_OUT); - keypad(stdscr, TRUE); - - ENDX = COLS - 1; - ENDY = LINES - 1; - - workarea = (state **)calloc(COLS, sizeof(state *)); - for(i = 0;i < COLS; ++i) - workarea[i] = (state *)calloc(LINES, sizeof(state)); - - /* For inverted U */ - workarea[39][15].newstate = TRUE; - workarea[40][15].newstate = TRUE; - workarea[41][15].newstate = TRUE; - workarea[39][16].newstate = TRUE; - workarea[39][17].newstate = TRUE; - workarea[41][16].newstate = TRUE; - workarea[41][17].newstate = TRUE; - update_state(workarea, STARTX, STARTY, ENDX, ENDY); - - /* For block */ -/* - workarea[37][13].newstate = TRUE; - workarea[37][14].newstate = TRUE; - workarea[38][13].newstate = TRUE; - workarea[38][14].newstate = TRUE; - - update_state(workarea, STARTX, STARTY, ENDX, ENDY); -*/ - display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); - while(getch() != KEY_F(1)) - { for(i = STARTX; i <= ENDX; ++i) - for(j = STARTY; j <= ENDY; ++j) - calc(workarea, i, j); - update_state(workarea, STARTX, STARTY, ENDX, ENDY); - display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); - } - - endwin(); - return 0; -} - -void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) -{ int i, j; - wclear(win); - for(i = startx; i <= endx; ++i) - for(j = starty;j <= endy; ++j) - if(area[i][j].newstate == TRUE) - mvwaddch(win, j, i, CELL_CHAR); - wrefresh(win); -} - -void calc(state **area, int i, int j) -{ int neighbours; - int newstate; - - neighbours = - area[(i - 1 + COLS) % COLS][j].oldstate + - area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate + - area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + - area[(i + 1) % COLS][j].oldstate + - area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + - area[(i + 1) % COLS][(j + 1) % LINES].oldstate + - area[i][(j - 1 + LINES) % LINES].oldstate + - area[i][(j + 1) % LINES].oldstate; - - newstate = FALSE; - if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) - newstate = TRUE; - else - if(area[i][j].oldstate == FALSE && neighbours == 3) - newstate = TRUE; - area[i][j].newstate = newstate; -} - -void update_state(state **area, int startx, int starty, int endx, int endy) -{ int i, j; - - for(i = startx; i <= endx; ++i) - for(j = starty; j <= endy; ++j) - area[i][j].oldstate = area[i][j].newstate; -} +#include + +int STARTX = 0; +int STARTY = 0; +int ENDX = 79; +int ENDY = 24; + +#define CELL_CHAR '#' +#define TIME_OUT 300 + +typedef struct _state { + int oldstate; + int newstate; +}state; + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy); +void calc(state **area, int x, int y); +void update_state(state **area, int startx, int starty, int endx, int endy); + +int main() +{ state **workarea; + int i, j; + + initscr(); + cbreak(); + timeout(TIME_OUT); + keypad(stdscr, TRUE); + + ENDX = COLS - 1; + ENDY = LINES - 1; + + workarea = (state **)calloc(COLS, sizeof(state *)); + for(i = 0;i < COLS; ++i) + workarea[i] = (state *)calloc(LINES, sizeof(state)); + + /* For inverted U */ + workarea[39][15].newstate = TRUE; + workarea[40][15].newstate = TRUE; + workarea[41][15].newstate = TRUE; + workarea[39][16].newstate = TRUE; + workarea[39][17].newstate = TRUE; + workarea[41][16].newstate = TRUE; + workarea[41][17].newstate = TRUE; + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + + /* For block */ +/* + workarea[37][13].newstate = TRUE; + workarea[37][14].newstate = TRUE; + workarea[38][13].newstate = TRUE; + workarea[38][14].newstate = TRUE; + + update_state(workarea, STARTX, STARTY, ENDX, ENDY); +*/ + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + while(getch() != KEY_F(1)) + { for(i = STARTX; i <= ENDX; ++i) + for(j = STARTY; j <= ENDY; ++j) + calc(workarea, i, j); + update_state(workarea, STARTX, STARTY, ENDX, ENDY); + display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY); + } + + endwin(); + return 0; +} + +void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy) +{ int i, j; + wclear(win); + for(i = startx; i <= endx; ++i) + for(j = starty;j <= endy; ++j) + if(area[i][j].newstate == TRUE) + mvwaddch(win, j, i, CELL_CHAR); + wrefresh(win); +} + +void calc(state **area, int i, int j) +{ int neighbours; + int newstate; + + neighbours = + area[(i - 1 + COLS) % COLS][j].oldstate + + area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate + + area[(i + 1) % COLS][j].oldstate + + area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate + + area[(i + 1) % COLS][(j + 1) % LINES].oldstate + + area[i][(j - 1 + LINES) % LINES].oldstate + + area[i][(j + 1) % LINES].oldstate; + + newstate = FALSE; + if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3)) + newstate = TRUE; + else + if(area[i][j].oldstate == FALSE && neighbours == 3) + newstate = TRUE; + area[i][j].newstate = newstate; +} + +void update_state(state **area, int startx, int starty, int endx, int endy) +{ int i, j; + + for(i = startx; i <= endx; ++i) + for(j = starty; j <= endy; ++j) + area[i][j].oldstate = area[i][j].newstate; +} diff --git a/JustForFun/magic.c b/JustForFun/magic.c index 6561941..c93b5d8 100644 --- a/JustForFun/magic.c +++ b/JustForFun/magic.c @@ -1,161 +1,161 @@ -#include -#include - -#define STARTX 9 -#define STARTY 3 -#define WIDTH 6 -#define HEIGHT 4 - -#define TRACE_VALUE TRACE_MAXIMUM - -void board( WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); -void magic(int **, int); -void print(int **, int); -void magic_board(int **a,int n); - -int main(int argc, char *argv[]) -{ - - int **a,n,i; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(0); - } - n = atoi(argv[1]); - if(n % 2 == 0) - { printf("Sorry !!! I don't know how to create magic square of even order\n"); - printf("The order should be an odd number\n"); - exit(0); - } - a = (int **) malloc(n * sizeof(int*)); - for(i = 0;i < n;++i) - a[i] = (int *)malloc(n * sizeof(int)); - - magic(a,n); - - initscr(); - curs_set(0); - noecho(); - magic_board(a,n); - getch(); - endwin(); - - return; -} - -void magic(int **a, int n) -{ - int i,j,k; - int row,col; - for(i = 0;i < n;++i) - for(j = 0;j < n;++j) - a[i][j] = -1; - row = 0; - col = n / 2; - - k = 1; - a[row][col] = k; - - while(k != n * n) - { - if(row == 0 && col != n - 1) - { row = n - 1; - col ++; - a[row][col] = ++k; - } - else if(row != 0 && col != n - 1) - { if(a[row - 1][col + 1] == -1) - { row --; - col ++; - a[row][col] = ++k; - } - else - { - row ++; - a[row][col] = ++k; - } - } - else if(row != 0 && col == n - 1) - { - row --; - col = 0; - a[row][col] = ++k; - } - else if(row == 0 && col == n - 1) - { row ++; - a[row][col] = ++k; - } - - } - return; -} - -void print(int **a,int n) -{ int i,j; - int x,y; - x = STARTX; - y = STARTY; - mvprintw(1,30,"MAGIC SQUARE"); - for(i = 0;i < n;++i) - { for(j = 0;j < n;++j) - { mvprintw(y,x,"%d",a[i][j]); - if(n > 9) - x += 4; - else - x += 6; - } - x = STARTX; - if(n > 7) - y += 2; - else - y += 3; - } - refresh(); -} -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} - -void magic_board(int **a,int n) -{ int i,j, deltax, deltay; - int startx, starty; - - starty = (LINES - n * HEIGHT) / 2; - startx = (COLS - n * WIDTH) / 2; - board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); - deltay = HEIGHT / 2; - deltax = WIDTH / 2; - for(i = 0;i < n; ++i) - for(j = 0; j < n; ++j) - mvprintw(starty + j * HEIGHT + deltay, - startx + i * WIDTH + deltax, - "%d", a[i][j]); -} +#include +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define TRACE_VALUE TRACE_MAXIMUM + +void board( WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void magic(int **, int); +void print(int **, int); +void magic_board(int **a,int n); + +int main(int argc, char *argv[]) +{ + + int **a,n,i; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(0); + } + n = atoi(argv[1]); + if(n % 2 == 0) + { printf("Sorry !!! I don't know how to create magic square of even order\n"); + printf("The order should be an odd number\n"); + exit(0); + } + a = (int **) malloc(n * sizeof(int*)); + for(i = 0;i < n;++i) + a[i] = (int *)malloc(n * sizeof(int)); + + magic(a,n); + + initscr(); + curs_set(0); + noecho(); + magic_board(a,n); + getch(); + endwin(); + + return; +} + +void magic(int **a, int n) +{ + int i,j,k; + int row,col; + for(i = 0;i < n;++i) + for(j = 0;j < n;++j) + a[i][j] = -1; + row = 0; + col = n / 2; + + k = 1; + a[row][col] = k; + + while(k != n * n) + { + if(row == 0 && col != n - 1) + { row = n - 1; + col ++; + a[row][col] = ++k; + } + else if(row != 0 && col != n - 1) + { if(a[row - 1][col + 1] == -1) + { row --; + col ++; + a[row][col] = ++k; + } + else + { + row ++; + a[row][col] = ++k; + } + } + else if(row != 0 && col == n - 1) + { + row --; + col = 0; + a[row][col] = ++k; + } + else if(row == 0 && col == n - 1) + { row ++; + a[row][col] = ++k; + } + + } + return; +} + +void print(int **a,int n) +{ int i,j; + int x,y; + x = STARTX; + y = STARTY; + mvprintw(1,30,"MAGIC SQUARE"); + for(i = 0;i < n;++i) + { for(j = 0;j < n;++j) + { mvprintw(y,x,"%d",a[i][j]); + if(n > 9) + x += 4; + else + x += 6; + } + x = STARTX; + if(n > 7) + y += 2; + else + y += 3; + } + refresh(); +} +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void magic_board(int **a,int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(i = 0;i < n; ++i) + for(j = 0; j < n; ++j) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%d", a[i][j]); +} diff --git a/JustForFun/queens.c b/JustForFun/queens.c index 2457ccc..82b11c1 100644 --- a/JustForFun/queens.c +++ b/JustForFun/queens.c @@ -1,122 +1,122 @@ -#include -#include - -#define QUEEN_CHAR '*' - -int *nqueens(int num); -int place(int current, int *position); -int print(int *positions, int num_queens); -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); - -int main(int argc, char *argv[]) -{ - int num_queens, *positions, count; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(1); - } - - num_queens = atoi(argv[1]); - initscr(); - cbreak(); - keypad(stdscr, TRUE); - positions = nqueens(num_queens); - free(positions); - endwin(); - return 0; -} - -int *nqueens(int num) -{ - int current, *position, num_solutions = 0; - - position = (int *) calloc(num + 1, sizeof(int)); - - position[1] = 0; - current = 1; /* current queen is being checked */ - /* position[current] is the coloumn*/ - while(current > 0){ - position[current] += 1; - while(position[current] <= num && !place(current, position) ) - position[current] += 1; - if(position[current] <= num){ - if(current == num) { - ++num_solutions; - print(position, num); - } - else { - current += 1; - position[current] = 0; - } - } - else current -= 1; /* backtrack */ - } - printf("Total Number of Solutions : %d\n", num_solutions); - return(position); -} - -int place(int current, int *position) -{ - int i; - if(current == 1) return(1); - for(i = 1; i < current; ++i) - if(position[i] == position[current]) return(0); - else if(abs(position[i] - position[current]) == - abs(i - current)) - return(0); - - return(1); -} - -int print(int *positions, int num_queens) -{ int count; - int y = 2, x = 2, w = 4, h = 2; - static int solution = 1; - - mvprintw(0, 0, "Solution No: %d", solution++); - board(stdscr, y, x, num_queens, num_queens, w, h); - for(count = 1; count <= num_queens; ++count) - { int tempy = y + (count - 1) * h + h / 2; - int tempx = x + (positions[count] - 1) * w + w / 2; - mvaddch(tempy, tempx, QUEEN_CHAR); - } - refresh(); - mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); - if(getch() == KEY_F(1)) - { endwin(); - exit(0); - } - clear(); -} - -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} +#include +#include + +#define QUEEN_CHAR '*' + +int *nqueens(int num); +int place(int current, int *position); +int print(int *positions, int num_queens); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); + +int main(int argc, char *argv[]) +{ + int num_queens, *positions, count; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + + num_queens = atoi(argv[1]); + initscr(); + cbreak(); + keypad(stdscr, TRUE); + positions = nqueens(num_queens); + free(positions); + endwin(); + return 0; +} + +int *nqueens(int num) +{ + int current, *position, num_solutions = 0; + + position = (int *) calloc(num + 1, sizeof(int)); + + position[1] = 0; + current = 1; /* current queen is being checked */ + /* position[current] is the coloumn*/ + while(current > 0){ + position[current] += 1; + while(position[current] <= num && !place(current, position) ) + position[current] += 1; + if(position[current] <= num){ + if(current == num) { + ++num_solutions; + print(position, num); + } + else { + current += 1; + position[current] = 0; + } + } + else current -= 1; /* backtrack */ + } + printf("Total Number of Solutions : %d\n", num_solutions); + return(position); +} + +int place(int current, int *position) +{ + int i; + if(current == 1) return(1); + for(i = 1; i < current; ++i) + if(position[i] == position[current]) return(0); + else if(abs(position[i] - position[current]) == + abs(i - current)) + return(0); + + return(1); +} + +int print(int *positions, int num_queens) +{ int count; + int y = 2, x = 2, w = 4, h = 2; + static int solution = 1; + + mvprintw(0, 0, "Solution No: %d", solution++); + board(stdscr, y, x, num_queens, num_queens, w, h); + for(count = 1; count <= num_queens; ++count) + { int tempy = y + (count - 1) * h + h / 2; + int tempx = x + (positions[count] - 1) * w + w / 2; + mvaddch(tempy, tempx, QUEEN_CHAR); + } + refresh(); + mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)"); + if(getch() == KEY_F(1)) + { endwin(); + exit(0); + } + clear(); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} diff --git a/JustForFun/shuffle.c b/JustForFun/shuffle.c index 0b8150b..d1431bd 100644 --- a/JustForFun/shuffle.c +++ b/JustForFun/shuffle.c @@ -1,205 +1,205 @@ -#include - -#define STARTX 9 -#define STARTY 3 -#define WIDTH 6 -#define HEIGHT 4 - -#define BLANK 0 - -typedef struct _tile { - int x; - int y; -}tile; - -void init_board(int **board, int n, tile *blank); -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height); -void shuffle_board(int **board, int n); -void move_blank(int direction, int **s_board, int n, tile *blank); -int check_win(int **s_board, int n, tile *blank); - -enum { LEFT, RIGHT, UP, DOWN }; - -int main(int argc, char *argv[]) -{ int **s_board; - int n, i, ch; - tile blank; - - if(argc != 2) - { printf("Usage: %s \n", argv[0]); - exit(1); - } - n = atoi(argv[1]); - - s_board = (int **)calloc(n, sizeof(int *)); - for(i = 0;i < n; ++i) - s_board[i] = (int *)calloc(n, sizeof(int)); - init_board(s_board, n, &blank); - initscr(); - keypad(stdscr, TRUE); - cbreak(); - shuffle_board(s_board, n); - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - move_blank(RIGHT, s_board, n, &blank); - break; - case KEY_RIGHT: - move_blank(LEFT, s_board, n, &blank); - break; - case KEY_UP: - move_blank(DOWN, s_board, n, &blank); - break; - case KEY_DOWN: - move_blank(UP, s_board, n, &blank); - break; - } - shuffle_board(s_board, n); - if(check_win(s_board, n, &blank) == TRUE) - { mvprintw(24, 0, "You Win !!!\n"); - refresh(); - break; - } - } - endwin(); - return 0; -} - -void move_blank(int direction, int **s_board, int n, tile *blank) -{ int temp; - - switch(direction) - { case LEFT: - { if(blank->x != 0) - { --blank->x; - temp = s_board[blank->x][blank->y]; - s_board[blank->x + 1][blank->y] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case RIGHT: - { if(blank->x != n - 1) - { ++blank->x; - temp = s_board[blank->x][blank->y]; - s_board[blank->x - 1][blank->y] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case UP: - { if(blank->y != 0) - { --blank->y; - temp = s_board[blank->x][blank->y]; - s_board[blank->x][blank->y + 1] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - case DOWN: - { if(blank->y != n - 1) - { ++blank->y; - temp = s_board[blank->x][blank->y]; - s_board[blank->x][blank->y - 1] = temp; - s_board[blank->x][blank->y] = BLANK; - } - } - break; - } -} - -int check_win(int **s_board, int n, tile *blank) -{ int i, j; - - s_board[blank->x][blank->y] = n * n; - for(i = 0;i < n; ++i) - for(j = 0;j < n; ++j) - if(s_board[i][j] != j * n + i + 1) - { s_board[blank->x][blank->y] = BLANK; - return FALSE; - } - - s_board[blank->x][blank->y] = BLANK; - return TRUE; -} - -void init_board(int **s_board, int n, tile *blank) -{ int i, j, k; - int *temp_board; - - temp_board = (int *)calloc(n * n, sizeof(int)); - srand(time(NULL)); - for(i = 0;i < n * n; ++i) - { -repeat : - k = rand() % (n * n); - for(j = 0;j <= i - 1; ++j) - if (k == temp_board[j]) - goto repeat; - else - temp_board[i] = k; - } - k = 0; - for (i = 0;i < n;++i) - for(j = 0;j < n; ++j,++k) - { if(temp_board[k] == 0) - { blank->x = i; - blank->y = j; - } - s_board[i][j] = temp_board[k]; - } - free(temp_board); -} - -void board(WINDOW *win, int starty, int startx, int lines, int cols, - int tile_width, int tile_height) -{ int endy, endx, i, j; - - endy = starty + lines * tile_height; - endx = startx + cols * tile_width; - - for(j = starty; j <= endy; j += tile_height) - for(i = startx; i <= endx; ++i) - mvwaddch(win, j, i, ACS_HLINE); - for(i = startx; i <= endx; i += tile_width) - for(j = starty; j <= endy; ++j) - mvwaddch(win, j, i, ACS_VLINE); - mvwaddch(win, starty, startx, ACS_ULCORNER); - mvwaddch(win, endy, startx, ACS_LLCORNER); - mvwaddch(win, starty, endx, ACS_URCORNER); - mvwaddch(win, endy, endx, ACS_LRCORNER); - for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) - { mvwaddch(win, j, startx, ACS_LTEE); - mvwaddch(win, j, endx, ACS_RTEE); - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - mvwaddch(win, j, i, ACS_PLUS); - } - for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) - { mvwaddch(win, starty, i, ACS_TTEE); - mvwaddch(win, endy, i, ACS_BTEE); - } - wrefresh(win); -} - -void shuffle_board(int **s_board, int n) -{ int i,j, deltax, deltay; - int startx, starty; - - starty = (LINES - n * HEIGHT) / 2; - startx = (COLS - n * WIDTH) / 2; - clear(); - mvprintw(24, 0, "Press F1 to Exit"); - board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); - deltay = HEIGHT / 2; - deltax = WIDTH / 2; - for(j = 0; j < n; ++j) - for(i = 0;i < n; ++i) - if(s_board[i][j] != BLANK) - mvprintw(starty + j * HEIGHT + deltay, - startx + i * WIDTH + deltax, - "%-2d", s_board[i][j]); - refresh(); -} - - +#include + +#define STARTX 9 +#define STARTY 3 +#define WIDTH 6 +#define HEIGHT 4 + +#define BLANK 0 + +typedef struct _tile { + int x; + int y; +}tile; + +void init_board(int **board, int n, tile *blank); +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height); +void shuffle_board(int **board, int n); +void move_blank(int direction, int **s_board, int n, tile *blank); +int check_win(int **s_board, int n, tile *blank); + +enum { LEFT, RIGHT, UP, DOWN }; + +int main(int argc, char *argv[]) +{ int **s_board; + int n, i, ch; + tile blank; + + if(argc != 2) + { printf("Usage: %s \n", argv[0]); + exit(1); + } + n = atoi(argv[1]); + + s_board = (int **)calloc(n, sizeof(int *)); + for(i = 0;i < n; ++i) + s_board[i] = (int *)calloc(n, sizeof(int)); + init_board(s_board, n, &blank); + initscr(); + keypad(stdscr, TRUE); + cbreak(); + shuffle_board(s_board, n); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + move_blank(RIGHT, s_board, n, &blank); + break; + case KEY_RIGHT: + move_blank(LEFT, s_board, n, &blank); + break; + case KEY_UP: + move_blank(DOWN, s_board, n, &blank); + break; + case KEY_DOWN: + move_blank(UP, s_board, n, &blank); + break; + } + shuffle_board(s_board, n); + if(check_win(s_board, n, &blank) == TRUE) + { mvprintw(24, 0, "You Win !!!\n"); + refresh(); + break; + } + } + endwin(); + return 0; +} + +void move_blank(int direction, int **s_board, int n, tile *blank) +{ int temp; + + switch(direction) + { case LEFT: + { if(blank->x != 0) + { --blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x + 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case RIGHT: + { if(blank->x != n - 1) + { ++blank->x; + temp = s_board[blank->x][blank->y]; + s_board[blank->x - 1][blank->y] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case UP: + { if(blank->y != 0) + { --blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y + 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + case DOWN: + { if(blank->y != n - 1) + { ++blank->y; + temp = s_board[blank->x][blank->y]; + s_board[blank->x][blank->y - 1] = temp; + s_board[blank->x][blank->y] = BLANK; + } + } + break; + } +} + +int check_win(int **s_board, int n, tile *blank) +{ int i, j; + + s_board[blank->x][blank->y] = n * n; + for(i = 0;i < n; ++i) + for(j = 0;j < n; ++j) + if(s_board[i][j] != j * n + i + 1) + { s_board[blank->x][blank->y] = BLANK; + return FALSE; + } + + s_board[blank->x][blank->y] = BLANK; + return TRUE; +} + +void init_board(int **s_board, int n, tile *blank) +{ int i, j, k; + int *temp_board; + + temp_board = (int *)calloc(n * n, sizeof(int)); + srand(time(NULL)); + for(i = 0;i < n * n; ++i) + { +repeat : + k = rand() % (n * n); + for(j = 0;j <= i - 1; ++j) + if (k == temp_board[j]) + goto repeat; + else + temp_board[i] = k; + } + k = 0; + for (i = 0;i < n;++i) + for(j = 0;j < n; ++j,++k) + { if(temp_board[k] == 0) + { blank->x = i; + blank->y = j; + } + s_board[i][j] = temp_board[k]; + } + free(temp_board); +} + +void board(WINDOW *win, int starty, int startx, int lines, int cols, + int tile_width, int tile_height) +{ int endy, endx, i, j; + + endy = starty + lines * tile_height; + endx = startx + cols * tile_width; + + for(j = starty; j <= endy; j += tile_height) + for(i = startx; i <= endx; ++i) + mvwaddch(win, j, i, ACS_HLINE); + for(i = startx; i <= endx; i += tile_width) + for(j = starty; j <= endy; ++j) + mvwaddch(win, j, i, ACS_VLINE); + mvwaddch(win, starty, startx, ACS_ULCORNER); + mvwaddch(win, endy, startx, ACS_LLCORNER); + mvwaddch(win, starty, endx, ACS_URCORNER); + mvwaddch(win, endy, endx, ACS_LRCORNER); + for(j = starty + tile_height; j <= endy - tile_height; j += tile_height) + { mvwaddch(win, j, startx, ACS_LTEE); + mvwaddch(win, j, endx, ACS_RTEE); + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + mvwaddch(win, j, i, ACS_PLUS); + } + for(i = startx + tile_width; i <= endx - tile_width; i += tile_width) + { mvwaddch(win, starty, i, ACS_TTEE); + mvwaddch(win, endy, i, ACS_BTEE); + } + wrefresh(win); +} + +void shuffle_board(int **s_board, int n) +{ int i,j, deltax, deltay; + int startx, starty; + + starty = (LINES - n * HEIGHT) / 2; + startx = (COLS - n * WIDTH) / 2; + clear(); + mvprintw(24, 0, "Press F1 to Exit"); + board(stdscr, starty, startx, n, n, WIDTH, HEIGHT); + deltay = HEIGHT / 2; + deltax = WIDTH / 2; + for(j = 0; j < n; ++j) + for(i = 0;i < n; ++i) + if(s_board[i][j] != BLANK) + mvprintw(starty + j * HEIGHT + deltay, + startx + i * WIDTH + deltax, + "%-2d", s_board[i][j]); + refresh(); +} + + diff --git a/JustForFun/tt.c b/JustForFun/tt.c index 4378e67..178eefa 100644 --- a/JustForFun/tt.c +++ b/JustForFun/tt.c @@ -1,223 +1,223 @@ -#include -#include -#include -#include - -#define HSIZE 60 -#define LENGTH 75 -#define WIDTH 10 -#define STARTX 1 -#define STARTY 5 -#define STATUSX 1 -#define STATUSY 25 - -#define KEY_F1 265 - -int print_menu(); -void print_byebye(); -void create_test_string(); -void print_time(time_t startt, time_t endt, int mistakes); -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); - -char *groups[] = { "`123456" , - "7890-=" , - "~!@#$%^" , - "&*()_+" , - "<>?" , - ",./\\" , - "asdfg", - "jkl;'", - "qwer", - "uiop", - "tyur", - "zxcv", - "bnm", - }; -int n_groups; - -int main() -{ int choice, i; - char *test_array; - int ch = KEY_F1; - int mistakes; - int x, y; - time_t start_t, end_t; - WINDOW *typing_win; - char string[80]; - - string[0] = '\0'; - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - intrflush(stdscr, FALSE); - - srandom(time(NULL)); - n_groups = sizeof(groups) / sizeof(char *); - test_array = (char *)calloc(HSIZE + 1, sizeof(char)); - - while(1) - { - if(ch == KEY_F1) - { choice = print_menu(); - choice -= 1; - if(choice == n_groups) - { print_byebye(); - free(test_array); - endwin(); - exit(0); - } - } - clear(); - strcpy(string, "Typing window"); - print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); - attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); - refresh(); - attroff(A_REVERSE); - - create_test_string(test_array, choice); - typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); - keypad(typing_win, TRUE); - intrflush(typing_win, FALSE); - box(typing_win, 0, 0); - - x = 1; - y = 1; - mvwprintw(typing_win, y, x, "%s", test_array); - wrefresh(typing_win); - y += 1; - - mistakes = 0; - i = 0; - time(&start_t); - wmove(typing_win, y, x); - wrefresh(typing_win); - ch = 0; - while(ch != KEY_F1 && i != HSIZE + 1) - { ch = wgetch(typing_win); - mvwprintw(typing_win, y, x, "%c", ch); - wrefresh(typing_win); - ++x; - if(ch == test_array[i]) - { ++i; - continue; - } - else - { ++mistakes; - ++i; - } - } - - time(&end_t); - print_time(start_t, end_t, mistakes); - } - free(test_array); - endwin(); - return 0; -} - - -int print_menu() -{ int choice, i; - - choice = 0; - while(1) - { clear(); - printw("\n\n"); - print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); - printw("\n\n\n"); - for(i = 0;i <= n_groups - 1; ++i) - printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); - printw("\t%3d: \tExit\n", i + 1); - - printw("\n\n\tChoice: "); - refresh(); - echo(); - scanw("%d", &choice); - noecho(); - - if(choice >= 1 && choice <= n_groups + 1) - break; - else - { attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); - attroff(A_REVERSE); - getch(); - } - } - return choice; -} - -void create_test_string(char *test_array, int choice) -{ int i, index, length; - - length = strlen(groups[choice]); - for(i = 0;i <= HSIZE - 1; ++i) - { if(i%5 == 0) - test_array[i] = ' '; - else - { index = (int)(random() % length); - test_array[i] = groups[choice][index]; - } - } - test_array[i] = '\0'; -} - -void print_byebye() -{ printw("\n"); - print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); - print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL); - refresh(); -} - -void print_time(time_t start_t, time_t end_t, int mistakes) -{ long int diff; - int h,m,s; - float wpm; - - diff = end_t - start_t; - wpm = ((HSIZE / 5)/(double)diff)*60; - - h = (int)(diff / 3600); - diff -= h * 3600; - m = (int)(diff / 60); - diff -= m * 60; - s = (int)diff; - - attron(A_REVERSE); - mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm); - attroff(A_REVERSE); - - refresh(); - getch(); - -} - -/* ---------------------------------------------------------------- * - * startx = 0 means at present x * - * starty = 0 means at present y * - * win = NULL means take stdscr * - * ---------------------------------------------------------------- */ - -void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} +#include +#include +#include +#include + +#define HSIZE 60 +#define LENGTH 75 +#define WIDTH 10 +#define STARTX 1 +#define STARTY 5 +#define STATUSX 1 +#define STATUSY 25 + +#define KEY_F1 265 + +int print_menu(); +void print_byebye(); +void create_test_string(); +void print_time(time_t startt, time_t endt, int mistakes); +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win); + +char *groups[] = { "`123456" , + "7890-=" , + "~!@#$%^" , + "&*()_+" , + "<>?" , + ",./\\" , + "asdfg", + "jkl;'", + "qwer", + "uiop", + "tyur", + "zxcv", + "bnm", + }; +int n_groups; + +int main() +{ int choice, i; + char *test_array; + int ch = KEY_F1; + int mistakes; + int x, y; + time_t start_t, end_t; + WINDOW *typing_win; + char string[80]; + + string[0] = '\0'; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + intrflush(stdscr, FALSE); + + srandom(time(NULL)); + n_groups = sizeof(groups) / sizeof(char *); + test_array = (char *)calloc(HSIZE + 1, sizeof(char)); + + while(1) + { + if(ch == KEY_F1) + { choice = print_menu(); + choice -= 1; + if(choice == n_groups) + { print_byebye(); + free(test_array); + endwin(); + exit(0); + } + } + clear(); + strcpy(string, "Typing window"); + print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL); + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu"); + refresh(); + attroff(A_REVERSE); + + create_test_string(test_array, choice); + typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX); + keypad(typing_win, TRUE); + intrflush(typing_win, FALSE); + box(typing_win, 0, 0); + + x = 1; + y = 1; + mvwprintw(typing_win, y, x, "%s", test_array); + wrefresh(typing_win); + y += 1; + + mistakes = 0; + i = 0; + time(&start_t); + wmove(typing_win, y, x); + wrefresh(typing_win); + ch = 0; + while(ch != KEY_F1 && i != HSIZE + 1) + { ch = wgetch(typing_win); + mvwprintw(typing_win, y, x, "%c", ch); + wrefresh(typing_win); + ++x; + if(ch == test_array[i]) + { ++i; + continue; + } + else + { ++mistakes; + ++i; + } + } + + time(&end_t); + print_time(start_t, end_t, mistakes); + } + free(test_array); + endwin(); + return 0; +} + + +int print_menu() +{ int choice, i; + + choice = 0; + while(1) + { clear(); + printw("\n\n"); + print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL); + printw("\n\n\n"); + for(i = 0;i <= n_groups - 1; ++i) + printw("\t%3d: \tPractice %s\n", i + 1, groups[i]); + printw("\t%3d: \tExit\n", i + 1); + + printw("\n\n\tChoice: "); + refresh(); + echo(); + scanw("%d", &choice); + noecho(); + + if(choice >= 1 && choice <= n_groups + 1) + break; + else + { attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue"); + attroff(A_REVERSE); + getch(); + } + } + return choice; +} + +void create_test_string(char *test_array, int choice) +{ int i, index, length; + + length = strlen(groups[choice]); + for(i = 0;i <= HSIZE - 1; ++i) + { if(i%5 == 0) + test_array[i] = ' '; + else + { index = (int)(random() % length); + test_array[i] = groups[choice][index]; + } + } + test_array[i] = '\0'; +} + +void print_byebye() +{ printw("\n"); + print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL); + print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL); + refresh(); +} + +void print_time(time_t start_t, time_t end_t, int mistakes) +{ long int diff; + int h,m,s; + float wpm; + + diff = end_t - start_t; + wpm = ((HSIZE / 5)/(double)diff)*60; + + h = (int)(diff / 3600); + diff -= h * 3600; + m = (int)(diff / 60); + diff -= m * 60; + s = (int)diff; + + attron(A_REVERSE); + mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm); + attroff(A_REVERSE); + + refresh(); + getch(); + +} + +/* ---------------------------------------------------------------- * + * startx = 0 means at present x * + * starty = 0 means at present y * + * win = NULL means take stdscr * + * ---------------------------------------------------------------- */ + +void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} diff --git a/basics/acs_vars.c b/basics/acs_vars.c index c1c72a3..92db58c 100644 --- a/basics/acs_vars.c +++ b/basics/acs_vars.c @@ -1,44 +1,44 @@ -#include - -int main() -{ - initscr(); - - printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); - printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); - printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); - printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); - printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); - printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); - printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); - printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); - printw("Vertical line "); addch(ACS_VLINE); printw("\n"); - printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); - printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); - printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); - printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); - printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); - printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); - printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); - printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); - printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); - printw("Bullet "); addch(ACS_BULLET); printw("\n"); - printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); - printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); - printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); - printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); - printw("Board of squares "); addch(ACS_BOARD); printw("\n"); - printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); - printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); - printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); - printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); - printw("Pi "); addch(ACS_PI); printw("\n"); - printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); - printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); - - refresh(); - getch(); - endwin(); - - return 0; -} +#include + +int main() +{ + initscr(); + + printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); + printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); + printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); + printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); + printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); + printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); + printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); + printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); + printw("Vertical line "); addch(ACS_VLINE); printw("\n"); + printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); + printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); + printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); + printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); + printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); + printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); + printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); + printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); + printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); + printw("Bullet "); addch(ACS_BULLET); printw("\n"); + printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); + printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); + printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); + printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); + printw("Board of squares "); addch(ACS_BOARD); printw("\n"); + printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); + printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); + printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); + printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); + printw("Pi "); addch(ACS_PI); printw("\n"); + printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); + printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); + + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/basics/hello_world.c b/basics/hello_world.c index 55b24df..794ae22 100644 --- a/basics/hello_world.c +++ b/basics/hello_world.c @@ -1,12 +1,12 @@ -#include - -int main() -{ - initscr(); /* Start curses mode */ - printw("Hello World !!!"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/init_func_example.c b/basics/init_func_example.c index 2eb19b7..e01ecb3 100644 --- a/basics/init_func_example.c +++ b/basics/init_func_example.c @@ -1,31 +1,31 @@ -#include - -int main() -{ int ch; - - initscr(); /* Start curses mode */ - raw(); /* Line buffering disabled */ - keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ - noecho(); /* Don't echo() while we do getch */ - - printw("Type any character to see it in bold\n"); - ch = getch(); /* If raw() hadn't been called - * we have to press enter before it - * gets to the program */ - if(ch == KEY_F(1)) /* Without keypad enabled this will */ - printw("F1 Key pressed");/* not get to us either */ - /* Without noecho() some ugly escape - * charachters might have been printed - * on screen */ - else - { printw("The pressed key is "); - attron(A_BOLD); - printw("%c", ch); - attroff(A_BOLD); - } - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ int ch; + + initscr(); /* Start curses mode */ + raw(); /* Line buffering disabled */ + keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ + noecho(); /* Don't echo() while we do getch */ + + printw("Type any character to see it in bold\n"); + ch = getch(); /* If raw() hadn't been called + * we have to press enter before it + * gets to the program */ + if(ch == KEY_F(1)) /* Without keypad enabled this will */ + printw("F1 Key pressed");/* not get to us either */ + /* Without noecho() some ugly escape + * charachters might have been printed + * on screen */ + else + { printw("The pressed key is "); + attron(A_BOLD); + printw("%c", ch); + attroff(A_BOLD); + } + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/key_code.c b/basics/key_code.c index 8c5d06c..5ad80eb 100644 --- a/basics/key_code.c +++ b/basics/key_code.c @@ -1,14 +1,14 @@ -#include - -int main() -{ int ch; - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - ch = getch(); - endwin(); - printf("The key pressed is %d\n", ch); -} +#include + +int main() +{ int ch; + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + ch = getch(); + endwin(); + printf("The key pressed is %d\n", ch); +} diff --git a/basics/mouse_menu.c b/basics/mouse_menu.c index 5b92e1e..6008092 100644 --- a/basics/mouse_menu.c +++ b/basics/mouse_menu.c @@ -1,106 +1,106 @@ -#include - -#define WIDTH 30 -#define HEIGHT 10 - -int startx = 0; -int starty = 0; - -char *choices[] = { "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int n_choices = sizeof(choices) / sizeof(char *); - -void print_menu(WINDOW *menu_win, int highlight); -void report_choice(int mouse_x, int mouse_y, int *p_choice); - -int main() -{ int c, choice = 0; - WINDOW *menu_win; - MEVENT event; - - /* Initialize curses */ - initscr(); - clear(); - noecho(); - cbreak(); //Line buffering disabled. pass on everything - - /* Try to put the window in the middle of screen */ - startx = (80 - WIDTH) / 2; - starty = (24 - HEIGHT) / 2; - - attron(A_REVERSE); - mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); - refresh(); - attroff(A_REVERSE); - - /* Print the menu for the first time */ - menu_win = newwin(HEIGHT, WIDTH, starty, startx); - print_menu(menu_win, 1); - /* Get all the mouse events */ - mousemask(ALL_MOUSE_EVENTS, NULL); - - while(1) - { c = wgetch(menu_win); - switch(c) - { case KEY_MOUSE: - if(getmouse(&event) == OK) - { /* When the user clicks left mouse button */ - if(event.bstate & BUTTON1_PRESSED) - { report_choice(event.x + 1, event.y + 1, &choice); - if(choice == -1) //Exit chosen - goto end; - mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); - refresh(); - } - } - print_menu(menu_win, choice); - break; - } - } -end: - endwin(); - return 0; -} - - -void print_menu(WINDOW *menu_win, int highlight) -{ - int x, y, i; - - x = 2; - y = 2; - box(menu_win, 0, 0); - for(i = 0; i < n_choices; ++i) - { if(highlight == i + 1) - { wattron(menu_win, A_REVERSE); - mvwprintw(menu_win, y, x, "%s", choices[i]); - wattroff(menu_win, A_REVERSE); - } - else - mvwprintw(menu_win, y, x, "%s", choices[i]); - ++y; - } - wrefresh(menu_win); -} - -/* Report the choice according to mouse position */ -void report_choice(int mouse_x, int mouse_y, int *p_choice) -{ int i,j, choice; - - i = startx + 2; - j = starty + 3; - - for(choice = 0; choice < n_choices; ++choice) - if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) - { if(choice == n_choices - 1) - *p_choice = -1; - else - *p_choice = choice + 1; - break; - } -} +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int n_choices = sizeof(choices) / sizeof(char *); + +void print_menu(WINDOW *menu_win, int highlight); +void report_choice(int mouse_x, int mouse_y, int *p_choice); + +int main() +{ int c, choice = 0; + WINDOW *menu_win; + MEVENT event; + + /* Initialize curses */ + initscr(); + clear(); + noecho(); + cbreak(); //Line buffering disabled. pass on everything + + /* Try to put the window in the middle of screen */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + attron(A_REVERSE); + mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); + refresh(); + attroff(A_REVERSE); + + /* Print the menu for the first time */ + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + print_menu(menu_win, 1); + /* Get all the mouse events */ + mousemask(ALL_MOUSE_EVENTS, NULL); + + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_MOUSE: + if(getmouse(&event) == OK) + { /* When the user clicks left mouse button */ + if(event.bstate & BUTTON1_PRESSED) + { report_choice(event.x + 1, event.y + 1, &choice); + if(choice == -1) //Exit chosen + goto end; + mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); + refresh(); + } + } + print_menu(menu_win, choice); + break; + } + } +end: + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + +/* Report the choice according to mouse position */ +void report_choice(int mouse_x, int mouse_y, int *p_choice) +{ int i,j, choice; + + i = startx + 2; + j = starty + 3; + + for(choice = 0; choice < n_choices; ++choice) + if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) + { if(choice == n_choices - 1) + *p_choice = -1; + else + *p_choice = choice + 1; + break; + } +} diff --git a/basics/other_border.c b/basics/other_border.c index 725d67a..8252631 100644 --- a/basics/other_border.c +++ b/basics/other_border.c @@ -1,120 +1,120 @@ -#include - -typedef struct _win_border_struct { - chtype ls, rs, ts, bs, - tl, tr, bl, br; -}WIN_BORDER; - -typedef struct _WIN_struct { - - int startx, starty; - int height, width; - WIN_BORDER border; -}WIN; - -void init_win_params(WIN *p_win); -void print_win_params(WIN *p_win); -void create_box(WIN *win, bool flag); - -int main(int argc, char *argv[]) -{ WIN win; - int ch; - - initscr(); /* Start curses mode */ - start_color(); /* Start the color functionality */ - cbreak(); /* Line buffering disabled, Pass on - * everty thing to me */ - keypad(stdscr, TRUE); /* I need that nifty F1 */ - noecho(); - init_pair(1, COLOR_CYAN, COLOR_BLACK); - - /* Initialize the window parameters */ - init_win_params(&win); - print_win_params(&win); - - attron(COLOR_PAIR(1)); - printw("Press F1 to exit"); - refresh(); - attroff(COLOR_PAIR(1)); - - create_box(&win, TRUE); - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - create_box(&win, FALSE); - --win.startx; - create_box(&win, TRUE); - break; - case KEY_RIGHT: - create_box(&win, FALSE); - ++win.startx; - create_box(&win, TRUE); - break; - case KEY_UP: - create_box(&win, FALSE); - --win.starty; - create_box(&win, TRUE); - break; - case KEY_DOWN: - create_box(&win, FALSE); - ++win.starty; - create_box(&win, TRUE); - break; - } - } - endwin(); /* End curses mode */ - return 0; -} -void init_win_params(WIN *p_win) -{ - p_win->height = 3; - p_win->width = 10; - p_win->starty = (LINES - p_win->height)/2; - p_win->startx = (COLS - p_win->width)/2; - - p_win->border.ls = '|'; - p_win->border.rs = '|'; - p_win->border.ts = '-'; - p_win->border.bs = '-'; - p_win->border.tl = '+'; - p_win->border.tr = '+'; - p_win->border.bl = '+'; - p_win->border.br = '+'; - -} -void print_win_params(WIN *p_win) -{ -#ifdef _DEBUG - mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, - p_win->width, p_win->height); - refresh(); -#endif -} -void create_box(WIN *p_win, bool flag) -{ int i, j; - int x, y, w, h; - - x = p_win->startx; - y = p_win->starty; - w = p_win->width; - h = p_win->height; - - if(flag == TRUE) - { mvaddch(y, x, p_win->border.tl); - mvaddch(y, x + w, p_win->border.tr); - mvaddch(y + h, x, p_win->border.bl); - mvaddch(y + h, x + w, p_win->border.br); - mvhline(y, x + 1, p_win->border.ts, w - 1); - mvhline(y + h, x + 1, p_win->border.bs, w - 1); - mvvline(y + 1, x, p_win->border.ls, h - 1); - mvvline(y + 1, x + w, p_win->border.rs, h - 1); - - } - else - for(j = y; j <= y + h; ++j) - for(i = x; i <= x + w; ++i) - mvaddch(j, i, ' '); - - refresh(); - -} +#include + +typedef struct _win_border_struct { + chtype ls, rs, ts, bs, + tl, tr, bl, br; +}WIN_BORDER; + +typedef struct _WIN_struct { + + int startx, starty; + int height, width; + WIN_BORDER border; +}WIN; + +void init_win_params(WIN *p_win); +void print_win_params(WIN *p_win); +void create_box(WIN *win, bool flag); + +int main(int argc, char *argv[]) +{ WIN win; + int ch; + + initscr(); /* Start curses mode */ + start_color(); /* Start the color functionality */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + noecho(); + init_pair(1, COLOR_CYAN, COLOR_BLACK); + + /* Initialize the window parameters */ + init_win_params(&win); + print_win_params(&win); + + attron(COLOR_PAIR(1)); + printw("Press F1 to exit"); + refresh(); + attroff(COLOR_PAIR(1)); + + create_box(&win, TRUE); + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + create_box(&win, FALSE); + --win.startx; + create_box(&win, TRUE); + break; + case KEY_RIGHT: + create_box(&win, FALSE); + ++win.startx; + create_box(&win, TRUE); + break; + case KEY_UP: + create_box(&win, FALSE); + --win.starty; + create_box(&win, TRUE); + break; + case KEY_DOWN: + create_box(&win, FALSE); + ++win.starty; + create_box(&win, TRUE); + break; + } + } + endwin(); /* End curses mode */ + return 0; +} +void init_win_params(WIN *p_win) +{ + p_win->height = 3; + p_win->width = 10; + p_win->starty = (LINES - p_win->height)/2; + p_win->startx = (COLS - p_win->width)/2; + + p_win->border.ls = '|'; + p_win->border.rs = '|'; + p_win->border.ts = '-'; + p_win->border.bs = '-'; + p_win->border.tl = '+'; + p_win->border.tr = '+'; + p_win->border.bl = '+'; + p_win->border.br = '+'; + +} +void print_win_params(WIN *p_win) +{ +#ifdef _DEBUG + mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, + p_win->width, p_win->height); + refresh(); +#endif +} +void create_box(WIN *p_win, bool flag) +{ int i, j; + int x, y, w, h; + + x = p_win->startx; + y = p_win->starty; + w = p_win->width; + h = p_win->height; + + if(flag == TRUE) + { mvaddch(y, x, p_win->border.tl); + mvaddch(y, x + w, p_win->border.tr); + mvaddch(y + h, x, p_win->border.bl); + mvaddch(y + h, x + w, p_win->border.br); + mvhline(y, x + 1, p_win->border.ts, w - 1); + mvhline(y + h, x + 1, p_win->border.bs, w - 1); + mvvline(y + 1, x, p_win->border.ls, h - 1); + mvvline(y + 1, x + w, p_win->border.rs, h - 1); + + } + else + for(j = y; j <= y + h; ++j) + for(i = x; i <= x + w; ++i) + mvaddch(j, i, ' '); + + refresh(); + +} diff --git a/basics/printw_example.c b/basics/printw_example.c index ae1345e..3f6d2e5 100644 --- a/basics/printw_example.c +++ b/basics/printw_example.c @@ -1,20 +1,20 @@ -#include /* ncurses.h includes stdio.h */ -#include - -int main() -{ - char mesg[]="Just a string"; /* message to be appeared on the screen */ - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); - printw("Try resizing your window(if possible) and then run this program again"); - refresh(); - getch(); - endwin(); - - return 0; -} +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Just a string"; /* message to be appeared on the screen */ + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); + printw("Try resizing your window(if possible) and then run this program again"); + refresh(); + getch(); + endwin(); + + return 0; +} diff --git a/basics/scanw_example.c b/basics/scanw_example.c index cc0bf7b..4a91faf 100644 --- a/basics/scanw_example.c +++ b/basics/scanw_example.c @@ -1,20 +1,20 @@ -#include /* ncurses.h includes stdio.h */ -#include - -int main() -{ - char mesg[]="Enter a string: "; /* message to be appeared on the screen */ - char str[80]; - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - getstr(str); - mvprintw(LINES - 2, 0, "You Entered: %s", str); - getch(); - endwin(); - - return 0; -} +#include /* ncurses.h includes stdio.h */ +#include + +int main() +{ + char mesg[]="Enter a string: "; /* message to be appeared on the screen */ + char str[80]; + int row,col; /* to store the number of rows and * + * the number of colums of the screen */ + initscr(); /* start the curses mode */ + getmaxyx(stdscr,row,col); /* get the number of rows and columns */ + mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); + /* print the message at the center of the screen */ + getstr(str); + mvprintw(LINES - 2, 0, "You Entered: %s", str); + getch(); + endwin(); + + return 0; +} diff --git a/basics/simple_attr.c b/basics/simple_attr.c index 3efef73..5b4b9ed 100644 --- a/basics/simple_attr.c +++ b/basics/simple_attr.c @@ -3,7 +3,7 @@ #include int main(int argc, char *argv[]) -{ +{ int ch, prev, row, col; prev = EOF; FILE *fp; @@ -20,35 +20,35 @@ int main(int argc, char *argv[]) perror("Cannot open input file"); exit(1); } - initscr(); /* Start curses mode */ - getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ - while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ + initscr(); /* Start curses mode */ + getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ + while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ { - getyx(stdscr, y, x); /* get the current curser position */ - if(y == (row - 1)) /* are we are at the end of the screen */ + getyx(stdscr, y, x); /* get the current curser position */ + if(y == (row - 1)) /* are we are at the end of the screen */ { - printw("<-Press Any Key->"); /* tell the user to press a key */ + printw("<-Press Any Key->"); /* tell the user to press a key */ getch(); - clear(); /* clear the screen */ - move(0, 0); /* start at the beginning of the screen */ + clear(); /* clear the screen */ + move(0, 0); /* start at the beginning of the screen */ } - if(prev == '/' && ch == '*') /* If it is / and * then only - * switch bold on */ + if(prev == '/' && ch == '*') /* If it is / and * then only + * switch bold on */ { - attron(A_BOLD); /* cut bold on */ - getyx(stdscr, y, x); /* get the current curser position */ - move(y, x - 1); /* back up one space */ - printw("%c%c", '/', ch); /* The actual printing is done here */ + attron(A_BOLD); /* cut bold on */ + getyx(stdscr, y, x); /* get the current curser position */ + move(y, x - 1); /* back up one space */ + printw("%c%c", '/', ch); /* The actual printing is done here */ } else printw("%c", ch); refresh(); if(prev == '*' && ch == '/') - attroff(A_BOLD); /* Switch it off once we got * - * and then / */ + attroff(A_BOLD); /* Switch it off once we got * + * and then / */ prev = ch; } - endwin(); /* End curses mode */ + endwin(); /* End curses mode */ fclose(fp); return 0; } diff --git a/basics/simple_color.c b/basics/simple_color.c index 59fb020..45223db 100644 --- a/basics/simple_color.c +++ b/basics/simple_color.c @@ -1,40 +1,40 @@ -#include - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); -int main(int argc, char *argv[]) -{ initscr(); /* Start curses mode */ - if(has_colors() == FALSE) - { endwin(); - printf("Your terminal does not support color\n"); - exit(1); - } - start_color(); /* Start color */ - init_pair(1, COLOR_RED, COLOR_BLACK); - - attron(COLOR_PAIR(1)); - print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); - attroff(COLOR_PAIR(1)); - getch(); - endwin(); -} -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - mvwprintw(win, y, x, "%s", string); - refresh(); -} - +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + if(has_colors() == FALSE) + { endwin(); + printf("Your terminal does not support color\n"); + exit(1); + } + start_color(); /* Start color */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + attron(COLOR_PAIR(1)); + print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); + attroff(COLOR_PAIR(1)); + getch(); + endwin(); +} +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + mvwprintw(win, y, x, "%s", string); + refresh(); +} + diff --git a/basics/simple_key.c b/basics/simple_key.c index 5197646..23ff44c 100644 --- a/basics/simple_key.c +++ b/basics/simple_key.c @@ -1,92 +1,92 @@ -#include -#include - -#define WIDTH 30 -#define HEIGHT 10 - -int startx = 0; -int starty = 0; - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; -int n_choices = sizeof(choices) / sizeof(char *); -void print_menu(WINDOW *menu_win, int highlight); - -int main() -{ WINDOW *menu_win; - int highlight = 1; - int choice = 0; - int c; - - initscr(); - clear(); - noecho(); - cbreak(); /* Line buffering disabled. pass on everything */ - startx = (80 - WIDTH) / 2; - starty = (24 - HEIGHT) / 2; - - menu_win = newwin(HEIGHT, WIDTH, starty, startx); - keypad(menu_win, TRUE); - mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); - refresh(); - print_menu(menu_win, highlight); - while(1) - { c = wgetch(menu_win); - switch(c) - { case KEY_UP: - if(highlight == 1) - highlight = n_choices; - else - --highlight; - break; - case KEY_DOWN: - if(highlight == n_choices) - highlight = 1; - else - ++highlight; - break; - case 10: - choice = highlight; - break; - default: - mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); - refresh(); - break; - } - print_menu(menu_win, highlight); - if(choice != 0) /* User did a choice come out of the infinite loop */ - break; - } - mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); - clrtoeol(); - refresh(); - endwin(); - return 0; -} - - -void print_menu(WINDOW *menu_win, int highlight) -{ - int x, y, i; - - x = 2; - y = 2; - box(menu_win, 0, 0); - for(i = 0; i < n_choices; ++i) - { if(highlight == i + 1) /* High light the present choice */ - { wattron(menu_win, A_REVERSE); - mvwprintw(menu_win, y, x, "%s", choices[i]); - wattroff(menu_win, A_REVERSE); - } - else - mvwprintw(menu_win, y, x, "%s", choices[i]); - ++y; - } - wrefresh(menu_win); -} - +#include +#include + +#define WIDTH 30 +#define HEIGHT 10 + +int startx = 0; +int starty = 0; + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; +int n_choices = sizeof(choices) / sizeof(char *); +void print_menu(WINDOW *menu_win, int highlight); + +int main() +{ WINDOW *menu_win; + int highlight = 1; + int choice = 0; + int c; + + initscr(); + clear(); + noecho(); + cbreak(); /* Line buffering disabled. pass on everything */ + startx = (80 - WIDTH) / 2; + starty = (24 - HEIGHT) / 2; + + menu_win = newwin(HEIGHT, WIDTH, starty, startx); + keypad(menu_win, TRUE); + mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); + refresh(); + print_menu(menu_win, highlight); + while(1) + { c = wgetch(menu_win); + switch(c) + { case KEY_UP: + if(highlight == 1) + highlight = n_choices; + else + --highlight; + break; + case KEY_DOWN: + if(highlight == n_choices) + highlight = 1; + else + ++highlight; + break; + case 10: + choice = highlight; + break; + default: + mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); + refresh(); + break; + } + print_menu(menu_win, highlight); + if(choice != 0) /* User did a choice come out of the infinite loop */ + break; + } + mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); + clrtoeol(); + refresh(); + endwin(); + return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ + int x, y, i; + + x = 2; + y = 2; + box(menu_win, 0, 0); + for(i = 0; i < n_choices; ++i) + { if(highlight == i + 1) /* High light the present choice */ + { wattron(menu_win, A_REVERSE); + mvwprintw(menu_win, y, x, "%s", choices[i]); + wattroff(menu_win, A_REVERSE); + } + else + mvwprintw(menu_win, y, x, "%s", choices[i]); + ++y; + } + wrefresh(menu_win); +} + diff --git a/basics/temp_leave.c b/basics/temp_leave.c index fdcd5c5..8ba21c1 100644 --- a/basics/temp_leave.c +++ b/basics/temp_leave.c @@ -1,20 +1,20 @@ -#include - -int main() -{ - initscr(); /* Start curses mode */ - printw("Hello World !!!\n"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ - def_prog_mode(); /* Save the tty modes */ - endwin(); /* End curses mode temporarily */ - system("/bin/sh"); /* Do whatever you like in cooked mode */ - reset_prog_mode(); /* Return to the previous tty mode*/ - /* stored by def_prog_mode() */ - refresh(); /* Do refresh() to restore the */ - /* Screen contents */ - printw("Another String\n"); /* Back to curses use the full */ - refresh(); /* capabilities of curses */ - endwin(); /* End curses mode */ - - return 0; -} +#include + +int main() +{ + initscr(); /* Start curses mode */ + printw("Hello World !!!\n"); /* Print Hello World */ + refresh(); /* Print it on to the real screen */ + def_prog_mode(); /* Save the tty modes */ + endwin(); /* End curses mode temporarily */ + system("/bin/sh"); /* Do whatever you like in cooked mode */ + reset_prog_mode(); /* Return to the previous tty mode*/ + /* stored by def_prog_mode() */ + refresh(); /* Do refresh() to restore the */ + /* Screen contents */ + printw("Another String\n"); /* Back to curses use the full */ + refresh(); /* capabilities of curses */ + endwin(); /* End curses mode */ + + return 0; +} diff --git a/basics/win_border.c b/basics/win_border.c index 41e1a7e..b0e8d61 100644 --- a/basics/win_border.c +++ b/basics/win_border.c @@ -1,82 +1,82 @@ -#include - - -WINDOW *create_newwin(int height, int width, int starty, int startx); -void destroy_win(WINDOW *local_win); - -int main(int argc, char *argv[]) -{ WINDOW *my_win; - int startx, starty, width, height; - int ch; - - initscr(); /* Start curses mode */ - cbreak(); /* Line buffering disabled, Pass on - * everty thing to me */ - keypad(stdscr, TRUE); /* I need that nifty F1 */ - - height = 3; - width = 10; - starty = (LINES - height) / 2; /* Calculating for a center placement */ - startx = (COLS - width) / 2; /* of the window */ - printw("Press F1 to exit"); - refresh(); - my_win = create_newwin(height, width, starty, startx); - - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_LEFT: - destroy_win(my_win); - my_win = create_newwin(height, width, starty,--startx); - break; - case KEY_RIGHT: - destroy_win(my_win); - my_win = create_newwin(height, width, starty,++startx); - break; - case KEY_UP: - destroy_win(my_win); - my_win = create_newwin(height, width, --starty,startx); - break; - case KEY_DOWN: - destroy_win(my_win); - my_win = create_newwin(height, width, ++starty,startx); - break; - } - } - - endwin(); /* End curses mode */ - return 0; -} - -WINDOW *create_newwin(int height, int width, int starty, int startx) -{ WINDOW *local_win; - - local_win = newwin(height, width, starty, startx); - box(local_win, 0 , 0); /* 0, 0 gives default characters - * for the vertical and horizontal - * lines */ - wrefresh(local_win); /* Show that box */ - - return local_win; -} - -void destroy_win(WINDOW *local_win) -{ - /* box(local_win, ' ', ' '); : This won't produce the desired - * result of erasing the window. It will leave it's four corners - * and so an ugly remnant of window. - */ - wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); - /* The parameters taken are - * 1. win: the window on which to operate - * 2. ls: character to be used for the left side of the window - * 3. rs: character to be used for the right side of the window - * 4. ts: character to be used for the top side of the window - * 5. bs: character to be used for the bottom side of the window - * 6. tl: character to be used for the top left corner of the window - * 7. tr: character to be used for the top right corner of the window - * 8. bl: character to be used for the bottom left corner of the window - * 9. br: character to be used for the bottom right corner of the window - */ - wrefresh(local_win); - delwin(local_win); -} +#include + + +WINDOW *create_newwin(int height, int width, int starty, int startx); +void destroy_win(WINDOW *local_win); + +int main(int argc, char *argv[]) +{ WINDOW *my_win; + int startx, starty, width, height; + int ch; + + initscr(); /* Start curses mode */ + cbreak(); /* Line buffering disabled, Pass on + * everty thing to me */ + keypad(stdscr, TRUE); /* I need that nifty F1 */ + + height = 3; + width = 10; + starty = (LINES - height) / 2; /* Calculating for a center placement */ + startx = (COLS - width) / 2; /* of the window */ + printw("Press F1 to exit"); + refresh(); + my_win = create_newwin(height, width, starty, startx); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_LEFT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,--startx); + break; + case KEY_RIGHT: + destroy_win(my_win); + my_win = create_newwin(height, width, starty,++startx); + break; + case KEY_UP: + destroy_win(my_win); + my_win = create_newwin(height, width, --starty,startx); + break; + case KEY_DOWN: + destroy_win(my_win); + my_win = create_newwin(height, width, ++starty,startx); + break; + } + } + + endwin(); /* End curses mode */ + return 0; +} + +WINDOW *create_newwin(int height, int width, int starty, int startx) +{ WINDOW *local_win; + + local_win = newwin(height, width, starty, startx); + box(local_win, 0 , 0); /* 0, 0 gives default characters + * for the vertical and horizontal + * lines */ + wrefresh(local_win); /* Show that box */ + + return local_win; +} + +void destroy_win(WINDOW *local_win) +{ + /* box(local_win, ' ', ' '); : This won't produce the desired + * result of erasing the window. It will leave it's four corners + * and so an ugly remnant of window. + */ + wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); + /* The parameters taken are + * 1. win: the window on which to operate + * 2. ls: character to be used for the left side of the window + * 3. rs: character to be used for the right side of the window + * 4. ts: character to be used for the top side of the window + * 5. bs: character to be used for the bottom side of the window + * 6. tl: character to be used for the top left corner of the window + * 7. tr: character to be used for the top right corner of the window + * 8. bl: character to be used for the bottom left corner of the window + * 9. br: character to be used for the bottom right corner of the window + */ + wrefresh(local_win); + delwin(local_win); +} diff --git a/basics/with_chgat.c b/basics/with_chgat.c index 4255561..96e23d7 100644 --- a/basics/with_chgat.c +++ b/basics/with_chgat.c @@ -1,24 +1,24 @@ -#include - -int main(int argc, char *argv[]) -{ initscr(); /* Start curses mode */ - start_color(); /* Start color functionality */ - - init_pair(1, COLOR_CYAN, COLOR_BLACK); - printw("A Big string which i didn't care to type fully "); - mvchgat(0, 0, -1, A_BLINK, 1, NULL); - /* - * First two parameters specify the position at which to start - * Third parameter number of characters to update. -1 means till - * end of line - * Forth parameter is the normal attribute you wanted to give - * to the charcter - * Fifth is the color index. It is the index given during init_pair() - * use 0 if you didn't want color - * Sixth one is always NULL - */ - refresh(); - getch(); - endwin(); /* End curses mode */ - return 0; -} +#include + +int main(int argc, char *argv[]) +{ initscr(); /* Start curses mode */ + start_color(); /* Start color functionality */ + + init_pair(1, COLOR_CYAN, COLOR_BLACK); + printw("A Big string which i didn't care to type fully "); + mvchgat(0, 0, -1, A_BLINK, 1, NULL); + /* + * First two parameters specify the position at which to start + * Third parameter number of characters to update. -1 means till + * end of line + * Forth parameter is the normal attribute you wanted to give + * to the charcter + * Fifth is the color index. It is the index given during init_pair() + * use 0 if you didn't want color + * Sixth one is always NULL + */ + refresh(); + getch(); + endwin(); /* End curses mode */ + return 0; +} diff --git a/forms/form_attrib.c b/forms/form_attrib.c index 9974157..93e1632 100644 --- a/forms/form_attrib.c +++ b/forms/form_attrib.c @@ -1,75 +1,75 @@ -#include - -int main() -{ FIELD *field[3]; - FORM *my_form; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize few color pairs */ - init_pair(1, COLOR_WHITE, COLOR_BLUE); - init_pair(2, COLOR_WHITE, COLOR_BLUE); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 4, 18, 0, 0); - field[1] = new_field(1, 10, 6, 18, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ - set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ - /* are printed in white */ - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - set_current_field(my_form, field[0]); /* Set focus to the colored field */ - mvprintw(4, 10, "Value 1:"); - mvprintw(6, 10, "Value 2:"); - mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_WHITE, COLOR_BLUE); + init_pair(2, COLOR_WHITE, COLOR_BLUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ + set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ + /* are printed in white */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_current_field(my_form, field[0]); /* Set focus to the colored field */ + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_options.c b/forms/form_options.c index 5b67dcc..b9ef831 100644 --- a/forms/form_options.c +++ b/forms/form_options.c @@ -1,76 +1,76 @@ -#include - -#define STARTX 15 -#define STARTY 4 -#define WIDTH 25 - -#define N_FIELDS 3 - -int main() -{ FIELD *field[N_FIELDS]; - FORM *my_form; - int ch, i; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize the fields */ - for(i = 0; i < N_FIELDS - 1; ++i) - field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); - field[N_FIELDS - 1] = NULL; - - /* Set field options */ - set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ - - field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ - field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ - field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ - /* after last character is entered */ - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ - set_field_buffer(field[0], 0, "This is a static Field"); - /* Initialize the field */ - mvprintw(STARTY, STARTX - 10, "Field 1:"); - mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +#define STARTX 15 +#define STARTY 4 +#define WIDTH 25 + +#define N_FIELDS 3 + +int main() +{ FIELD *field[N_FIELDS]; + FORM *my_form; + int ch, i; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + for(i = 0; i < N_FIELDS - 1; ++i) + field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); + field[N_FIELDS - 1] = NULL; + + /* Set field options */ + set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ + + field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ + field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ + field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ + /* after last character is entered */ + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ + set_field_buffer(field[0], 0, "This is a static Field"); + /* Initialize the field */ + mvprintw(STARTY, STARTX - 10, "Field 1:"); + mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_simple.c b/forms/form_simple.c index 520fc92..63f7fe5 100644 --- a/forms/form_simple.c +++ b/forms/form_simple.c @@ -1,66 +1,66 @@ -#include - -int main() -{ FIELD *field[3]; - FORM *my_form; - int ch; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 4, 18, 0, 0); - field[1] = new_field(1, 10, 6, 18, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - post_form(my_form); - refresh(); - - mvprintw(4, 10, "Value 1:"); - mvprintw(6, 10, "Value 2:"); - refresh(); - - /* Loop through to get user requests */ - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} +#include + +int main() +{ FIELD *field[3]; + FORM *my_form; + int ch; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 4, 18, 0, 0); + field[1] = new_field(1, 10, 6, 18, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + post_form(my_form); + refresh(); + + mvprintw(4, 10, "Value 1:"); + mvprintw(6, 10, "Value 2:"); + refresh(); + + /* Loop through to get user requests */ + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} diff --git a/forms/form_win.c b/forms/form_win.c index fb00a46..8cd3b16 100644 --- a/forms/form_win.c +++ b/forms/form_win.c @@ -1,112 +1,112 @@ -#include - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ - FIELD *field[3]; - FORM *my_form; - WINDOW *my_form_win; - int ch, rows, cols; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize few color pairs */ - init_pair(1, COLOR_RED, COLOR_BLACK); - - /* Initialize the fields */ - field[0] = new_field(1, 10, 6, 1, 0, 0); - field[1] = new_field(1, 10, 8, 1, 0, 0); - field[2] = NULL; - - /* Set field options */ - set_field_back(field[0], A_UNDERLINE); - field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ - /* Field is filled up */ - set_field_back(field[1], A_UNDERLINE); - field_opts_off(field[1], O_AUTOSKIP); - - /* Create the form and post it */ - my_form = new_form(field); - - /* Calculate the area required for the form */ - scale_form(my_form, &rows, &cols); - - /* Create the window to be associated with the form */ - my_form_win = newwin(rows + 4, cols + 4, 4, 4); - keypad(my_form_win, TRUE); - - /* Set main window and sub window */ - set_form_win(my_form, my_form_win); - set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); - - /* Print a border around the main window and print a title */ - box(my_form_win, 0, 0); - print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); - - post_form(my_form); - wrefresh(my_form_win); - - mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); - refresh(); - - /* Loop through to get user requests */ - while((ch = wgetch(my_form_win)) != KEY_F(1)) - { switch(ch) - { case KEY_DOWN: - /* Go to next field */ - form_driver(my_form, REQ_NEXT_FIELD); - /* Go to the end of the present buffer */ - /* Leaves nicely at the last character */ - form_driver(my_form, REQ_END_LINE); - break; - case KEY_UP: - /* Go to previous field */ - form_driver(my_form, REQ_PREV_FIELD); - form_driver(my_form, REQ_END_LINE); - break; - default: - /* If this is a normal character, it gets */ - /* Printed */ - form_driver(my_form, ch); - break; - } - } - - /* Un post form and free the memory */ - unpost_form(my_form); - free_form(my_form); - free_field(field[0]); - free_field(field[1]); - - endwin(); - return 0; -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ + FIELD *field[3]; + FORM *my_form; + WINDOW *my_form_win; + int ch, rows, cols; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize few color pairs */ + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Initialize the fields */ + field[0] = new_field(1, 10, 6, 1, 0, 0); + field[1] = new_field(1, 10, 8, 1, 0, 0); + field[2] = NULL; + + /* Set field options */ + set_field_back(field[0], A_UNDERLINE); + field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ + /* Field is filled up */ + set_field_back(field[1], A_UNDERLINE); + field_opts_off(field[1], O_AUTOSKIP); + + /* Create the form and post it */ + my_form = new_form(field); + + /* Calculate the area required for the form */ + scale_form(my_form, &rows, &cols); + + /* Create the window to be associated with the form */ + my_form_win = newwin(rows + 4, cols + 4, 4, 4); + keypad(my_form_win, TRUE); + + /* Set main window and sub window */ + set_form_win(my_form, my_form_win); + set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); + + /* Print a border around the main window and print a title */ + box(my_form_win, 0, 0); + print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); + + post_form(my_form); + wrefresh(my_form_win); + + mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); + refresh(); + + /* Loop through to get user requests */ + while((ch = wgetch(my_form_win)) != KEY_F(1)) + { switch(ch) + { case KEY_DOWN: + /* Go to next field */ + form_driver(my_form, REQ_NEXT_FIELD); + /* Go to the end of the present buffer */ + /* Leaves nicely at the last character */ + form_driver(my_form, REQ_END_LINE); + break; + case KEY_UP: + /* Go to previous field */ + form_driver(my_form, REQ_PREV_FIELD); + form_driver(my_form, REQ_END_LINE); + break; + default: + /* If this is a normal character, it gets */ + /* Printed */ + form_driver(my_form, ch); + break; + } + } + + /* Un post form and free the memory */ + unpost_form(my_form); + free_form(my_form); + free_field(field[0]); + free_field(field[1]); + + endwin(); + return 0; +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/menus/menu_attrib.c b/menus/menu_attrib.c index fdfdd2c..56fad90 100644 --- a/menus/menu_attrib.c +++ b/menus/menu_attrib.c @@ -1,80 +1,80 @@ -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_MAGENTA, COLOR_BLACK); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - item_opts_off(my_items[3], O_SELECTABLE); - item_opts_off(my_items[6], O_SELECTABLE); - - /* Create menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Set fore ground and back ground of the menu */ - set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); - set_menu_back(my_menu, COLOR_PAIR(2)); - set_menu_grey(my_menu, COLOR_PAIR(3)); - - /* Post the menu */ - mvprintw(LINES - 3, 0, "Press to see the option selected"); - mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - move(20, 0); - clrtoeol(); - mvprintw(20, 0, "Item selected is : %s", - item_name(current_item(my_menu))); - pos_menu_cursor(my_menu); - break; - } - } - unpost_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - free_menu(my_menu); - endwin(); -} - +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + item_opts_off(my_items[3], O_SELECTABLE); + item_opts_off(my_items[6], O_SELECTABLE); + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set fore ground and back ground of the menu */ + set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); + set_menu_back(my_menu, COLOR_PAIR(2)); + set_menu_grey(my_menu, COLOR_PAIR(3)); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", + item_name(current_item(my_menu))); + pos_menu_cursor(my_menu); + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_item_data.c b/menus/menu_item_data.c index c51ebc1..d33998e 100644 --- a/menus/menu_item_data.c +++ b/menus/menu_item_data.c @@ -1,66 +1,66 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - cur_item = current_item(my_menu); - move(LINES - 2, 0); - clrtoeol(); - mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", - item_index(cur_item) + 1, item_name(cur_item), - item_description(cur_item)); - - refresh(); - pos_menu_cursor(my_menu); - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + cur_item = current_item(my_menu); + move(LINES - 2, 0); + clrtoeol(); + mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", + item_index(cur_item) + 1, item_name(cur_item), + item_description(cur_item)); + + refresh(); + pos_menu_cursor(my_menu); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_multi_column.c b/menus/menu_multi_column.c index d507610..1fa6e02 100644 --- a/menus/menu_multi_column.c +++ b/menus/menu_multi_column.c @@ -1,97 +1,97 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", - "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", - "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", - "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", - "Exit", - (char *)NULL, - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_CYAN, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Set menu option not to show the description */ - menu_opts_off(my_menu, O_SHOWDESC); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 70, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); - set_menu_format(my_menu, 5, 3); - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - - attron(COLOR_PAIR(2)); - mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); - mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); - attroff(COLOR_PAIR(2)); - refresh(); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case KEY_LEFT: - menu_driver(my_menu, REQ_LEFT_ITEM); - break; - case KEY_RIGHT: - menu_driver(my_menu, REQ_RIGHT_ITEM); - break; - case KEY_NPAGE: - menu_driver(my_menu, REQ_SCR_DPAGE); - break; - case KEY_PPAGE: - menu_driver(my_menu, REQ_SCR_UPAGE); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", + "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", + "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", + "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", + "Exit", + (char *)NULL, + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Set menu option not to show the description */ + menu_opts_off(my_menu, O_SHOWDESC); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 70, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); + set_menu_format(my_menu, 5, 3); + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); + mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_LEFT: + menu_driver(my_menu, REQ_LEFT_ITEM); + break; + case KEY_RIGHT: + menu_driver(my_menu, REQ_RIGHT_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} diff --git a/menus/menu_scroll.c b/menus/menu_scroll.c index 2d9c627..40618f2 100644 --- a/menus/menu_scroll.c +++ b/menus/menu_scroll.c @@ -1,124 +1,124 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Choice 8", - "Choice 9", - "Choice 10", - "Exit", - (char *)NULL, - }; -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_CYAN, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 40, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); - set_menu_format(my_menu, 5, 1); - - /* Set menu mark to the string " * " */ - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); - mvwaddch(my_menu_win, 2, 0, ACS_LTEE); - mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); - mvwaddch(my_menu_win, 2, 39, ACS_RTEE); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - attron(COLOR_PAIR(2)); - mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); - mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); - attroff(COLOR_PAIR(2)); - refresh(); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case KEY_NPAGE: - menu_driver(my_menu, REQ_SCR_DPAGE); - break; - case KEY_PPAGE: - menu_driver(my_menu, REQ_SCR_UPAGE); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Choice 8", + "Choice 9", + "Choice 10", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + set_menu_format(my_menu, 5, 1); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); + mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); + attroff(COLOR_PAIR(2)); + refresh(); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/menus/menu_simple.c b/menus/menu_simple.c index 1222c7d..086fdf7 100644 --- a/menus/menu_simple.c +++ b/menus/menu_simple.c @@ -1,56 +1,56 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - mvprintw(LINES - 2, 0, "F1 to Exit"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + mvprintw(LINES - 2, 0, "F1 to Exit"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_toggle.c b/menus/menu_toggle.c index f6e8d7e..60a052f 100644 --- a/menus/menu_toggle.c +++ b/menus/menu_toggle.c @@ -1,84 +1,84 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - my_items[n_choices] = (ITEM *)NULL; - - my_menu = new_menu((ITEM **)my_items); - - /* Make the menu multi valued */ - menu_opts_off(my_menu, O_ONEVALUE); - - mvprintw(LINES - 3, 0, "Use to select or unselect an item."); - mvprintw(LINES - 2, 0, " to see presently selected items(F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case ' ': - menu_driver(my_menu, REQ_TOGGLE_ITEM); - break; - case 10: /* Enter */ - { char temp[200]; - ITEM **items; - - items = menu_items(my_menu); - temp[0] = '\0'; - for(i = 0; i < item_count(my_menu); ++i) - if(item_value(items[i]) == TRUE) - { strcat(temp, item_name(items[i])); - strcat(temp, " "); - } - move(20, 0); - clrtoeol(); - mvprintw(20, 0, temp); - refresh(); - } - break; - } - } - - free_item(my_items[0]); - free_item(my_items[1]); - free_menu(my_menu); - endwin(); -} - +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + my_items[n_choices] = (ITEM *)NULL; + + my_menu = new_menu((ITEM **)my_items); + + /* Make the menu multi valued */ + menu_opts_off(my_menu, O_ONEVALUE); + + mvprintw(LINES - 3, 0, "Use to select or unselect an item."); + mvprintw(LINES - 2, 0, " to see presently selected items(F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case ' ': + menu_driver(my_menu, REQ_TOGGLE_ITEM); + break; + case 10: /* Enter */ + { char temp[200]; + ITEM **items; + + items = menu_items(my_menu); + temp[0] = '\0'; + for(i = 0; i < item_count(my_menu); ++i) + if(item_value(items[i]) == TRUE) + { strcat(temp, item_name(items[i])); + strcat(temp, " "); + } + move(20, 0); + clrtoeol(); + mvprintw(20, 0, temp); + refresh(); + } + break; + } + } + + free_item(my_items[0]); + free_item(my_items[1]); + free_menu(my_menu); + endwin(); +} + diff --git a/menus/menu_userptr.c b/menus/menu_userptr.c index baf6a6c..d2e02d4 100644 --- a/menus/menu_userptr.c +++ b/menus/menu_userptr.c @@ -1,87 +1,87 @@ -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Choice 5", - "Choice 6", - "Choice 7", - "Exit", - }; -void func(char *name); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - int n_choices, i; - ITEM *cur_item; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_MAGENTA, COLOR_BLACK); - - /* Initialize items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - { my_items[i] = new_item(choices[i], choices[i]); - /* Set the user pointer */ - set_item_userptr(my_items[i], func); - } - my_items[n_choices] = (ITEM *)NULL; - - /* Create menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Post the menu */ - mvprintw(LINES - 3, 0, "Press to see the option selected"); - mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); - post_menu(my_menu); - refresh(); - - while((c = getch()) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - case 10: /* Enter */ - { ITEM *cur; - void (*p)(char *); - - cur = current_item(my_menu); - p = item_userptr(cur); - p((char *)item_name(cur)); - pos_menu_cursor(my_menu); - break; - } - break; - } - } - unpost_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - free_menu(my_menu); - endwin(); -} - -void func(char *name) -{ move(20, 0); - clrtoeol(); - mvprintw(20, 0, "Item selected is : %s", name); -} +#include +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Choice 5", + "Choice 6", + "Choice 7", + "Exit", + }; +void func(char *name); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + int n_choices, i; + ITEM *cur_item; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_MAGENTA, COLOR_BLACK); + + /* Initialize items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + { my_items[i] = new_item(choices[i], choices[i]); + /* Set the user pointer */ + set_item_userptr(my_items[i], func); + } + my_items[n_choices] = (ITEM *)NULL; + + /* Create menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Post the menu */ + mvprintw(LINES - 3, 0, "Press to see the option selected"); + mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); + post_menu(my_menu); + refresh(); + + while((c = getch()) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case 10: /* Enter */ + { ITEM *cur; + void (*p)(char *); + + cur = current_item(my_menu); + p = item_userptr(cur); + p((char *)item_name(cur)); + pos_menu_cursor(my_menu); + break; + } + break; + } + } + unpost_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + free_menu(my_menu); + endwin(); +} + +void func(char *name) +{ move(20, 0); + clrtoeol(); + mvprintw(20, 0, "Item selected is : %s", name); +} diff --git a/menus/menu_win.c b/menus/menu_win.c index 3514086..c3387ed 100644 --- a/menus/menu_win.c +++ b/menus/menu_win.c @@ -1,105 +1,105 @@ -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define CTRLD 4 - -char *choices[] = { - "Choice 1", - "Choice 2", - "Choice 3", - "Choice 4", - "Exit", - (char *)NULL, - }; -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ ITEM **my_items; - int c; - MENU *my_menu; - WINDOW *my_menu_win; - int n_choices, i; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - init_pair(1, COLOR_RED, COLOR_BLACK); - - /* Create items */ - n_choices = ARRAY_SIZE(choices); - my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); - for(i = 0; i < n_choices; ++i) - my_items[i] = new_item(choices[i], choices[i]); - - /* Crate menu */ - my_menu = new_menu((ITEM **)my_items); - - /* Create the window to be associated with the menu */ - my_menu_win = newwin(10, 40, 4, 4); - keypad(my_menu_win, TRUE); - - /* Set main window and sub window */ - set_menu_win(my_menu, my_menu_win); - set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); - - /* Set menu mark to the string " * " */ - set_menu_mark(my_menu, " * "); - - /* Print a border around the main window and print a title */ - box(my_menu_win, 0, 0); - print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); - mvwaddch(my_menu_win, 2, 0, ACS_LTEE); - mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); - mvwaddch(my_menu_win, 2, 39, ACS_RTEE); - mvprintw(LINES - 2, 0, "F1 to exit"); - refresh(); - - /* Post the menu */ - post_menu(my_menu); - wrefresh(my_menu_win); - - while((c = wgetch(my_menu_win)) != KEY_F(1)) - { switch(c) - { case KEY_DOWN: - menu_driver(my_menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(my_menu, REQ_UP_ITEM); - break; - } - wrefresh(my_menu_win); - } - - /* Unpost and free all the memory taken up */ - unpost_menu(my_menu); - free_menu(my_menu); - for(i = 0; i < n_choices; ++i) - free_item(my_items[i]); - endwin(); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +char *choices[] = { + "Choice 1", + "Choice 2", + "Choice 3", + "Choice 4", + "Exit", + (char *)NULL, + }; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Crate menu */ + my_menu = new_menu((ITEM **)my_items); + + /* Create the window to be associated with the menu */ + my_menu_win = newwin(10, 40, 4, 4); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, ACS_RTEE); + mvprintw(LINES - 2, 0, "F1 to exit"); + refresh(); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + while((c = wgetch(my_menu_win)) != KEY_F(1)) + { switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_browse.c b/panels/panel_browse.c index 45fd839..013c066 100644 --- a/panels/panel_browse.c +++ b/panels/panel_browse.c @@ -1,117 +1,117 @@ -#include - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL *top; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Set up the user pointers to the next panel */ - set_panel_userptr(my_panels[0], my_panels[1]); - set_panel_userptr(my_panels[1], my_panels[2]); - set_panel_userptr(my_panels[2], my_panels[0]); - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - doupdate(); - - top = my_panels[2]; - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 9: - top = (PANEL *)panel_userptr(top); - top_panel(top); - break; - } - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL *top; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Set up the user pointers to the next panel */ + set_panel_userptr(my_panels[0], my_panels[1]); + set_panel_userptr(my_panels[1], my_panels[2]); + set_panel_userptr(my_panels[2], my_panels[0]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + top = my_panels[2]; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: + top = (PANEL *)panel_userptr(top); + top_panel(top); + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_hide.c b/panels/panel_hide.c index 40f6c94..457199c 100644 --- a/panels/panel_hide.c +++ b/panels/panel_hide.c @@ -1,156 +1,156 @@ -#include - -typedef struct _PANEL_DATA { - int hide; /* TRUE if panel is hidden */ -}PANEL_DATA; - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL_DATA panel_datas[3]; - PANEL_DATA *temp; - int ch; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Initialize panel datas saying that nothing is hidden */ - panel_datas[0].hide = FALSE; - panel_datas[1].hide = FALSE; - panel_datas[2].hide = FALSE; - - set_panel_userptr(my_panels[0], &panel_datas[0]); - set_panel_userptr(my_panels[1], &panel_datas[1]); - set_panel_userptr(my_panels[2], &panel_datas[2]); - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); - mvprintw(LINES - 2, 0, "F1 to Exit"); - - attroff(COLOR_PAIR(4)); - doupdate(); - - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 'a': - temp = (PANEL_DATA *)panel_userptr(my_panels[0]); - if(temp->hide == FALSE) - { hide_panel(my_panels[0]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[0]); - temp->hide = FALSE; - } - break; - case 'b': - temp = (PANEL_DATA *)panel_userptr(my_panels[1]); - if(temp->hide == FALSE) - { hide_panel(my_panels[1]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[1]); - temp->hide = FALSE; - } - break; - case 'c': - temp = (PANEL_DATA *)panel_userptr(my_panels[2]); - if(temp->hide == FALSE) - { hide_panel(my_panels[2]); - temp->hide = TRUE; - } - else - { show_panel(my_panels[2]); - temp->hide = FALSE; - } - break; - } - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +typedef struct _PANEL_DATA { + int hide; /* TRUE if panel is hidden */ +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA panel_datas[3]; + PANEL_DATA *temp; + int ch; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Initialize panel datas saying that nothing is hidden */ + panel_datas[0].hide = FALSE; + panel_datas[1].hide = FALSE; + panel_datas[2].hide = FALSE; + + set_panel_userptr(my_panels[0], &panel_datas[0]); + set_panel_userptr(my_panels[1], &panel_datas[1]); + set_panel_userptr(my_panels[2], &panel_datas[2]); + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); + mvprintw(LINES - 2, 0, "F1 to Exit"); + + attroff(COLOR_PAIR(4)); + doupdate(); + + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 'a': + temp = (PANEL_DATA *)panel_userptr(my_panels[0]); + if(temp->hide == FALSE) + { hide_panel(my_panels[0]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[0]); + temp->hide = FALSE; + } + break; + case 'b': + temp = (PANEL_DATA *)panel_userptr(my_panels[1]); + if(temp->hide == FALSE) + { hide_panel(my_panels[1]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[1]); + temp->hide = FALSE; + } + break; + case 'c': + temp = (PANEL_DATA *)panel_userptr(my_panels[2]); + if(temp->hide == FALSE) + { hide_panel(my_panels[2]); + temp->hide = TRUE; + } + else + { show_panel(my_panels[2]); + temp->hide = FALSE; + } + break; + } + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_resize.c b/panels/panel_resize.c index 71d331f..e77ba0d 100644 --- a/panels/panel_resize.c +++ b/panels/panel_resize.c @@ -1,234 +1,234 @@ -#include - -typedef struct _PANEL_DATA { - int x, y, w, h; - char label[80]; - int label_color; - PANEL *next; -}PANEL_DATA; - -#define NLINES 10 -#define NCOLS 40 - -void init_wins(WINDOW **wins, int n); -void win_show(WINDOW *win, char *label, int label_color); -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); -void set_user_ptrs(PANEL **panels, int n); - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - PANEL_DATA *top; - PANEL *stack_top; - WINDOW *temp_win, *old_win; - int ch; - int newx, newy, neww, newh; - int size = FALSE, move = FALSE; - - /* Initialize curses */ - initscr(); - start_color(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - - /* Initialize all the colors */ - init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); - init_pair(4, COLOR_CYAN, COLOR_BLACK); - - init_wins(my_wins, 3); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - set_user_ptrs(my_panels, 3); - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - doupdate(); - - stack_top = my_panels[2]; - top = (PANEL_DATA *)panel_userptr(stack_top); - newx = top->x; - newy = top->y; - neww = top->w; - newh = top->h; - while((ch = getch()) != KEY_F(1)) - { switch(ch) - { case 9: /* Tab */ - top = (PANEL_DATA *)panel_userptr(stack_top); - top_panel(top->next); - stack_top = top->next; - top = (PANEL_DATA *)panel_userptr(stack_top); - newx = top->x; - newy = top->y; - neww = top->w; - newh = top->h; - break; - case 'r': /* Re-Size*/ - size = TRUE; - attron(COLOR_PAIR(4)); - mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press to end resizing"); - refresh(); - attroff(COLOR_PAIR(4)); - break; - case 'm': /* Move */ - attron(COLOR_PAIR(4)); - mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press to end moving"); - refresh(); - attroff(COLOR_PAIR(4)); - move = TRUE; - break; - case KEY_LEFT: - if(size == TRUE) - { --newx; - ++neww; - } - if(move == TRUE) - --newx; - break; - case KEY_RIGHT: - if(size == TRUE) - { ++newx; - --neww; - } - if(move == TRUE) - ++newx; - break; - case KEY_UP: - if(size == TRUE) - { --newy; - ++newh; - } - if(move == TRUE) - --newy; - break; - case KEY_DOWN: - if(size == TRUE) - { ++newy; - --newh; - } - if(move == TRUE) - ++newy; - break; - case 10: /* Enter */ - move(LINES - 4, 0); - clrtoeol(); - refresh(); - if(size == TRUE) - { old_win = panel_window(stack_top); - temp_win = newwin(newh, neww, newy, newx); - replace_panel(stack_top, temp_win); - win_show(temp_win, top->label, top->label_color); - delwin(old_win); - size = FALSE; - } - if(move == TRUE) - { move_panel(stack_top, newy, newx); - move = FALSE; - } - break; - - } - attron(COLOR_PAIR(4)); - mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); - mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); - attroff(COLOR_PAIR(4)); - refresh(); - update_panels(); - doupdate(); - } - endwin(); - return 0; -} - -/* Put all the windows */ -void init_wins(WINDOW **wins, int n) -{ int x, y, i; - char label[80]; - - y = 2; - x = 10; - for(i = 0; i < n; ++i) - { wins[i] = newwin(NLINES, NCOLS, y, x); - sprintf(label, "Window Number %d", i + 1); - win_show(wins[i], label, i + 1); - y += 3; - x += 7; - } -} - -/* Set the PANEL_DATA structures for individual panels */ -void set_user_ptrs(PANEL **panels, int n) -{ PANEL_DATA *ptrs; - WINDOW *win; - int x, y, w, h, i; - char temp[80]; - - ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); - - for(i = 0;i < n; ++i) - { win = panel_window(panels[i]); - getbegyx(win, y, x); - getmaxyx(win, h, w); - ptrs[i].x = x; - ptrs[i].y = y; - ptrs[i].w = w; - ptrs[i].h = h; - sprintf(temp, "Window Number %d", i + 1); - strcpy(ptrs[i].label, temp); - ptrs[i].label_color = i + 1; - if(i + 1 == n) - ptrs[i].next = panels[0]; - else - ptrs[i].next = panels[i + 1]; - set_panel_userptr(panels[i], &ptrs[i]); - } -} - -/* Show the window with a border and a label */ -void win_show(WINDOW *win, char *label, int label_color) -{ int startx, starty, height, width; - - getbegyx(win, starty, startx); - getmaxyx(win, height, width); - - box(win, 0, 0); - mvwaddch(win, 2, 0, ACS_LTEE); - mvwhline(win, 2, 1, ACS_HLINE, width - 2); - mvwaddch(win, 2, width - 1, ACS_RTEE); - - print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); -} - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == NULL) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = strlen(string); - temp = (width - length)/ 2; - x = startx + (int)temp; - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} +#include + +typedef struct _PANEL_DATA { + int x, y, w, h; + char label[80]; + int label_color; + PANEL *next; +}PANEL_DATA; + +#define NLINES 10 +#define NCOLS 40 + +void init_wins(WINDOW **wins, int n); +void win_show(WINDOW *win, char *label, int label_color); +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); +void set_user_ptrs(PANEL **panels, int n); + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + PANEL_DATA *top; + PANEL *stack_top; + WINDOW *temp_win, *old_win; + int ch; + int newx, newy, neww, newh; + int size = FALSE, move = FALSE; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + + /* Initialize all the colors */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + init_wins(my_wins, 3); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + set_user_ptrs(my_panels, 3); + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + doupdate(); + + stack_top = my_panels[2]; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + while((ch = getch()) != KEY_F(1)) + { switch(ch) + { case 9: /* Tab */ + top = (PANEL_DATA *)panel_userptr(stack_top); + top_panel(top->next); + stack_top = top->next; + top = (PANEL_DATA *)panel_userptr(stack_top); + newx = top->x; + newy = top->y; + neww = top->w; + newh = top->h; + break; + case 'r': /* Re-Size*/ + size = TRUE; + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press to end resizing"); + refresh(); + attroff(COLOR_PAIR(4)); + break; + case 'm': /* Move */ + attron(COLOR_PAIR(4)); + mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press to end moving"); + refresh(); + attroff(COLOR_PAIR(4)); + move = TRUE; + break; + case KEY_LEFT: + if(size == TRUE) + { --newx; + ++neww; + } + if(move == TRUE) + --newx; + break; + case KEY_RIGHT: + if(size == TRUE) + { ++newx; + --neww; + } + if(move == TRUE) + ++newx; + break; + case KEY_UP: + if(size == TRUE) + { --newy; + ++newh; + } + if(move == TRUE) + --newy; + break; + case KEY_DOWN: + if(size == TRUE) + { ++newy; + --newh; + } + if(move == TRUE) + ++newy; + break; + case 10: /* Enter */ + move(LINES - 4, 0); + clrtoeol(); + refresh(); + if(size == TRUE) + { old_win = panel_window(stack_top); + temp_win = newwin(newh, neww, newy, newx); + replace_panel(stack_top, temp_win); + win_show(temp_win, top->label, top->label_color); + delwin(old_win); + size = FALSE; + } + if(move == TRUE) + { move_panel(stack_top, newy, newx); + move = FALSE; + } + break; + + } + attron(COLOR_PAIR(4)); + mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); + mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); + attroff(COLOR_PAIR(4)); + refresh(); + update_panels(); + doupdate(); + } + endwin(); + return 0; +} + +/* Put all the windows */ +void init_wins(WINDOW **wins, int n) +{ int x, y, i; + char label[80]; + + y = 2; + x = 10; + for(i = 0; i < n; ++i) + { wins[i] = newwin(NLINES, NCOLS, y, x); + sprintf(label, "Window Number %d", i + 1); + win_show(wins[i], label, i + 1); + y += 3; + x += 7; + } +} + +/* Set the PANEL_DATA structures for individual panels */ +void set_user_ptrs(PANEL **panels, int n) +{ PANEL_DATA *ptrs; + WINDOW *win; + int x, y, w, h, i; + char temp[80]; + + ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); + + for(i = 0;i < n; ++i) + { win = panel_window(panels[i]); + getbegyx(win, y, x); + getmaxyx(win, h, w); + ptrs[i].x = x; + ptrs[i].y = y; + ptrs[i].w = w; + ptrs[i].h = h; + sprintf(temp, "Window Number %d", i + 1); + strcpy(ptrs[i].label, temp); + ptrs[i].label_color = i + 1; + if(i + 1 == n) + ptrs[i].next = panels[0]; + else + ptrs[i].next = panels[i + 1]; + set_panel_userptr(panels[i], &ptrs[i]); + } +} + +/* Show the window with a border and a label */ +void win_show(WINDOW *win, char *label, int label_color) +{ int startx, starty, height, width; + + getbegyx(win, starty, startx); + getmaxyx(win, height, width); + + box(win, 0, 0); + mvwaddch(win, 2, 0, ACS_LTEE); + mvwhline(win, 2, 1, ACS_HLINE, width - 2); + mvwaddch(win, 2, width - 1, ACS_RTEE); + + print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); +} + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == NULL) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = strlen(string); + temp = (width - length)/ 2; + x = startx + (int)temp; + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/panels/panel_simple.c b/panels/panel_simple.c index d98aeef..2ce8c18 100644 --- a/panels/panel_simple.c +++ b/panels/panel_simple.c @@ -1,38 +1,38 @@ -#include - -int main() -{ WINDOW *my_wins[3]; - PANEL *my_panels[3]; - int lines = 10, cols = 40, y = 2, x = 4, i; - - initscr(); - cbreak(); - noecho(); - - /* Create windows for the panels */ - my_wins[0] = newwin(lines, cols, y, x); - my_wins[1] = newwin(lines, cols, y + 1, x + 5); - my_wins[2] = newwin(lines, cols, y + 2, x + 10); - - /* - * Create borders around the windows so that you can see the effect - * of panels - */ - for(i = 0; i < 3; ++i) - box(my_wins[i], 0, 0); - - /* Attach a panel to each window */ /* Order is bottom up */ - my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ - my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ - my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ - - /* Update the stacking order. 2nd panel will be on top */ - update_panels(); - - /* Show it on the screen */ - doupdate(); - - getch(); - endwin(); -} - +#include + +int main() +{ WINDOW *my_wins[3]; + PANEL *my_panels[3]; + int lines = 10, cols = 40, y = 2, x = 4, i; + + initscr(); + cbreak(); + noecho(); + + /* Create windows for the panels */ + my_wins[0] = newwin(lines, cols, y, x); + my_wins[1] = newwin(lines, cols, y + 1, x + 5); + my_wins[2] = newwin(lines, cols, y + 2, x + 10); + + /* + * Create borders around the windows so that you can see the effect + * of panels + */ + for(i = 0; i < 3; ++i) + box(my_wins[i], 0, 0); + + /* Attach a panel to each window */ /* Order is bottom up */ + my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ + my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ + my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ + + /* Update the stacking order. 2nd panel will be on top */ + update_panels(); + + /* Show it on the screen */ + doupdate(); + + getch(); + endwin(); +} + From b45b438a4fc8c6f56808beecefd05587f1b28675 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:41:30 -0500 Subject: [PATCH 07/21] working through Hello World --- basics/hello_world.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/basics/hello_world.c b/basics/hello_world.c index 794ae22..4ddd229 100644 --- a/basics/hello_world.c +++ b/basics/hello_world.c @@ -2,11 +2,11 @@ int main() { - initscr(); /* Start curses mode */ - printw("Hello World !!!"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ + initscr(); + printw("Hello World !!!"); + refresh(); + getch(); + endwin(); return 0; } From da3ee678a8f4852cdf72de3cb138d1075a11bcf5 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 19:57:50 -0500 Subject: [PATCH 08/21] up through example 4.7 --- basics/init_func_example.c | 48 +++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/basics/init_func_example.c b/basics/init_func_example.c index e01ecb3..a453f99 100644 --- a/basics/init_func_example.c +++ b/basics/init_func_example.c @@ -1,31 +1,37 @@ #include +void _do_setup(){ + initscr(); + raw(); + keypad(stdscr, TRUE); + noecho(); +} + + +void _display_and_quit(){ + refresh(); + getch(); + endwin(); +} + + int main() -{ int ch; - - initscr(); /* Start curses mode */ - raw(); /* Line buffering disabled */ - keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ - noecho(); /* Don't echo() while we do getch */ - - printw("Type any character to see it in bold\n"); - ch = getch(); /* If raw() hadn't been called - * we have to press enter before it - * gets to the program */ - if(ch == KEY_F(1)) /* Without keypad enabled this will */ - printw("F1 Key pressed");/* not get to us either */ - /* Without noecho() some ugly escape - * charachters might have been printed - * on screen */ - else - { printw("The pressed key is "); +{ + int ch; + _do_setup(); + + printw("Type any character to see it in bold\n"); + ch = getch(); + if(ch == KEY_F(1)){ + printw("F1 Key pressed"); + } else { + printw("The pressed key is "); attron(A_BOLD); printw("%c", ch); attroff(A_BOLD); } - refresh(); /* Print it on to the real screen */ - getch(); /* Wait for user input */ - endwin(); /* End curses mode */ + + _display_and_quit(); return 0; } From 0e6152a741ebfec3fa594d3197a207a2414d95c8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 20:22:50 -0500 Subject: [PATCH 09/21] added python version of hello_world --- basics/hello_world.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 basics/hello_world.py diff --git a/basics/hello_world.py b/basics/hello_world.py new file mode 100644 index 0000000..3c3ffc7 --- /dev/null +++ b/basics/hello_world.py @@ -0,0 +1,18 @@ +from __future__ import print_function +import sys +import curses + + +def hello_world(stdscr): + stdscr.addstr(0, 0, "Hello World !!!") + stdscr.refresh() + stdscr.getch() + + +def main(): + curses.wrapper(hello_world) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) From a66477b05f7b3e8f6bfa8e1152946f72be878ef8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 18 Nov 2009 20:33:10 -0500 Subject: [PATCH 10/21] adding python version of init_func_example --- basics/init_func_example.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 basics/init_func_example.py diff --git a/basics/init_func_example.py b/basics/init_func_example.py new file mode 100644 index 0000000..e1d86eb --- /dev/null +++ b/basics/init_func_example.py @@ -0,0 +1,33 @@ +import sys +import curses + + +def main(): + curses.wrapper(init_func_example) + return 0 + + +def init_func_example(stdscr): + curses.raw() + stdscr.keypad(1) + curses.noecho() + stdscr.addstr(0, 0, "Type any character to see it in bold\n") + + ch = stdscr.getch() + if ch == curses.KEY_F1: + stdscr.addstr(1, 0, "F1 Key pressed") + else: + key_pressed = "The pressed key is " + stdscr.addstr(1, 0, key_pressed) + stdscr.attron(curses.A_BOLD) + stdscr.addstr(1, len(key_pressed), chr(ch)) + stdscr.attroff(curses.A_BOLD) + + stdscr.refresh() + stdscr.getch() + + +if __name__ == '__main__': + sys.exit(main()) + +# vim:filetype=python From b13831dc92a3f8393419c4b5aebc4a899e2e62a3 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 19 Nov 2009 20:29:11 -0500 Subject: [PATCH 11/21] worked through the printw_example exercise, added python version --- basics/printw_example.c | 27 +++++++++++++-------------- basics/printw_example.py | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 basics/printw_example.py diff --git a/basics/printw_example.c b/basics/printw_example.c index 3f6d2e5..310c40d 100644 --- a/basics/printw_example.c +++ b/basics/printw_example.c @@ -1,20 +1,19 @@ -#include /* ncurses.h includes stdio.h */ +#include #include int main() { - char mesg[]="Just a string"; /* message to be appeared on the screen */ - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); - printw("Try resizing your window(if possible) and then run this program again"); - refresh(); - getch(); - endwin(); + char mesg[] = "Just a string"; + int row, col; - return 0; + initscr(); + getmaxyx(stdscr, row, col); + mvprintw(row / 2, (col - strlen(mesg)) / 2, "%s", mesg); + mvprintw(row - 2, 0, "This screen has %d rows and %d columns\n", row, col); + printw("Try resizing your window (if possible) and then run this program again"); + refresh(); + getch(); + endwin(); + + return 0; } diff --git a/basics/printw_example.py b/basics/printw_example.py new file mode 100644 index 0000000..1770617 --- /dev/null +++ b/basics/printw_example.py @@ -0,0 +1,25 @@ +import sys +import curses + + +def printw_example(stdscr): + mesg = "Just a string" + row, col = stdscr.getmaxyx() + stdscr.addstr(row / 2, (col - len(mesg)) / 2, mesg) + stdscr.addstr(row - 2, 0, "This screen has {0} rows and {1} " + "columns\n".format(row, col)) + stdscr.addstr(row - 1, 0, "Try resizing your window (if possible) and then" + "run this program again") + stdscr.refresh() + stdscr.getch() + + +def main(): + curses.wrapper(printw_example) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) + +# vim:filetype=python From 791b8d227c1bf494b8beeb93f6950b5778fb6e4b Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 21 Nov 2009 11:00:10 -0500 Subject: [PATCH 12/21] working through C version of scanw example (7.4) --- basics/scanw_example.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/basics/scanw_example.c b/basics/scanw_example.c index 4a91faf..a81f053 100644 --- a/basics/scanw_example.c +++ b/basics/scanw_example.c @@ -1,20 +1,19 @@ -#include /* ncurses.h includes stdio.h */ +#include #include int main() { - char mesg[]="Enter a string: "; /* message to be appeared on the screen */ - char str[80]; - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - getstr(str); - mvprintw(LINES - 2, 0, "You Entered: %s", str); - getch(); - endwin(); + char * mesg = "Enter a string: "; + char str[80]; + int row, col; - return 0; + initscr(); + getmaxyx(stdscr, row, col); + mvprintw(row / 2, (col - strlen(mesg)) / 2, "%s", mesg); + getstr(str); + mvprintw(LINES - 2, 0, "You Entered: %s", str); + getch(); + endwin(); + + return 0; } From 579ad3c15e26c1a00acda6afd4ed4ec36e11f962 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 21 Nov 2009 11:04:40 -0500 Subject: [PATCH 13/21] finished python version of scanw_example (7.4) --- basics/scanw_example.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 basics/scanw_example.py diff --git a/basics/scanw_example.py b/basics/scanw_example.py new file mode 100644 index 0000000..7d68764 --- /dev/null +++ b/basics/scanw_example.py @@ -0,0 +1,24 @@ +import sys +import curses + + +def scanw_example(stdscr): + mesg = 'Enter a string: ' + + row, col = stdscr.getmaxyx() + curses.echo() + stdscr.addstr(row / 2, (col - len(mesg)) / 2, mesg) + instr = stdscr.getstr() + stdscr.addstr(row - 2, 0, 'You Entered: {0}'.format(instr)) + stdscr.getch() + + +def main(): + curses.wrapper(scanw_example) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) + +# vim:filetype=python From e8f71e4fd89237edcbcbdc0195dca0e5e5c651c0 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 21 Nov 2009 11:15:39 -0500 Subject: [PATCH 14/21] mangling the simple attr example (8.) --- basics/simple_attr.c | 83 ++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/basics/simple_attr.c b/basics/simple_attr.c index 5b4b9ed..b6ef1b3 100644 --- a/basics/simple_attr.c +++ b/basics/simple_attr.c @@ -1,54 +1,47 @@ -/* pager functionality by Joseph Spainhour" */ #include #include int main(int argc, char *argv[]) { - int ch, prev, row, col; - prev = EOF; - FILE *fp; - int y, x; + int ch, prev, row, col; + prev = EOF; + FILE *fp; + int y, x; - if(argc != 2) - { - printf("Usage: %s \n", argv[0]); - exit(1); - } - fp = fopen(argv[1], "r"); - if(fp == NULL) - { - perror("Cannot open input file"); - exit(1); - } - initscr(); /* Start curses mode */ - getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ - while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ - { - getyx(stdscr, y, x); /* get the current curser position */ - if(y == (row - 1)) /* are we are at the end of the screen */ - { - printw("<-Press Any Key->"); /* tell the user to press a key */ - getch(); - clear(); /* clear the screen */ - move(0, 0); /* start at the beginning of the screen */ + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + exit(1); } - if(prev == '/' && ch == '*') /* If it is / and * then only - * switch bold on */ - { - attron(A_BOLD); /* cut bold on */ - getyx(stdscr, y, x); /* get the current curser position */ - move(y, x - 1); /* back up one space */ - printw("%c%c", '/', ch); /* The actual printing is done here */ + fp = fopen(argv[1], "r"); + if (fp == NULL) { + perror("Cannot open input file"); + exit(1); } - else - printw("%c", ch); - refresh(); - if(prev == '*' && ch == '/') - attroff(A_BOLD); /* Switch it off once we got * - * and then / */ - prev = ch; - } - endwin(); /* End curses mode */ - fclose(fp); - return 0; + initscr(); + getmaxyx(stdscr, row, col); + while ((ch = fgetc(fp)) != EOF) { + getyx(stdscr, y, x); + if (y == (row - 1)) { + printw("<-Press Any Key->"); + getch(); + clear(); + move(0, 0); + } + if (prev == '/' && ch == '*') { + attron(A_BOLD | COLOR_GREEN); + getyx(stdscr, y, x); + move(y, x - 1); + printw("%c%c", '/', ch); + } else { + printw("%c", ch); + refresh(); + if (prev == '*' && ch == '/') { + attroff(A_BOLD | COLOR_GREEN); + prev = ch; + } + } + } + endwin(); + fclose(fp); + return 0; } From 92c130fcd5455f9df871ae914f9e6d1eef5de602 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 21 Nov 2009 11:15:54 -0500 Subject: [PATCH 15/21] Revert "mangling the simple attr example (8.)" This reverts commit e8f71e4fd89237edcbcbdc0195dca0e5e5c651c0. --- basics/simple_attr.c | 83 ++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/basics/simple_attr.c b/basics/simple_attr.c index b6ef1b3..5b4b9ed 100644 --- a/basics/simple_attr.c +++ b/basics/simple_attr.c @@ -1,47 +1,54 @@ +/* pager functionality by Joseph Spainhour" */ #include #include int main(int argc, char *argv[]) { - int ch, prev, row, col; - prev = EOF; - FILE *fp; - int y, x; + int ch, prev, row, col; + prev = EOF; + FILE *fp; + int y, x; - if (argc != 2) { - printf("Usage: %s \n", argv[0]); - exit(1); + if(argc != 2) + { + printf("Usage: %s \n", argv[0]); + exit(1); + } + fp = fopen(argv[1], "r"); + if(fp == NULL) + { + perror("Cannot open input file"); + exit(1); + } + initscr(); /* Start curses mode */ + getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ + while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ + { + getyx(stdscr, y, x); /* get the current curser position */ + if(y == (row - 1)) /* are we are at the end of the screen */ + { + printw("<-Press Any Key->"); /* tell the user to press a key */ + getch(); + clear(); /* clear the screen */ + move(0, 0); /* start at the beginning of the screen */ } - fp = fopen(argv[1], "r"); - if (fp == NULL) { - perror("Cannot open input file"); - exit(1); + if(prev == '/' && ch == '*') /* If it is / and * then only + * switch bold on */ + { + attron(A_BOLD); /* cut bold on */ + getyx(stdscr, y, x); /* get the current curser position */ + move(y, x - 1); /* back up one space */ + printw("%c%c", '/', ch); /* The actual printing is done here */ } - initscr(); - getmaxyx(stdscr, row, col); - while ((ch = fgetc(fp)) != EOF) { - getyx(stdscr, y, x); - if (y == (row - 1)) { - printw("<-Press Any Key->"); - getch(); - clear(); - move(0, 0); - } - if (prev == '/' && ch == '*') { - attron(A_BOLD | COLOR_GREEN); - getyx(stdscr, y, x); - move(y, x - 1); - printw("%c%c", '/', ch); - } else { - printw("%c", ch); - refresh(); - if (prev == '*' && ch == '/') { - attroff(A_BOLD | COLOR_GREEN); - prev = ch; - } - } - } - endwin(); - fclose(fp); - return 0; + else + printw("%c", ch); + refresh(); + if(prev == '*' && ch == '/') + attroff(A_BOLD); /* Switch it off once we got * + * and then / */ + prev = ch; + } + endwin(); /* End curses mode */ + fclose(fp); + return 0; } From e396c88fd1b863776d3fe5b09b0d6bb42e0c1619 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 21 Nov 2009 11:19:08 -0500 Subject: [PATCH 16/21] a less-mangled version of the simple_attr demo --- basics/simple_attr.c | 86 ++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/basics/simple_attr.c b/basics/simple_attr.c index 5b4b9ed..2a4ec2d 100644 --- a/basics/simple_attr.c +++ b/basics/simple_attr.c @@ -4,51 +4,53 @@ int main(int argc, char *argv[]) { - int ch, prev, row, col; - prev = EOF; - FILE *fp; - int y, x; + int ch, prev, row, col; + prev = EOF; + FILE *fp; + int y, x; - if(argc != 2) - { - printf("Usage: %s \n", argv[0]); - exit(1); - } - fp = fopen(argv[1], "r"); - if(fp == NULL) - { - perror("Cannot open input file"); - exit(1); - } - initscr(); /* Start curses mode */ - getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ - while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ - { - getyx(stdscr, y, x); /* get the current curser position */ - if(y == (row - 1)) /* are we are at the end of the screen */ + if(argc != 2) { - printw("<-Press Any Key->"); /* tell the user to press a key */ - getch(); - clear(); /* clear the screen */ - move(0, 0); /* start at the beginning of the screen */ + printf("Usage: %s \n", argv[0]); + exit(1); } - if(prev == '/' && ch == '*') /* If it is / and * then only - * switch bold on */ + fp = fopen(argv[1], "r"); + if(fp == NULL) { - attron(A_BOLD); /* cut bold on */ - getyx(stdscr, y, x); /* get the current curser position */ - move(y, x - 1); /* back up one space */ - printw("%c%c", '/', ch); /* The actual printing is done here */ + perror("Cannot open input file"); + exit(1); } - else - printw("%c", ch); - refresh(); - if(prev == '*' && ch == '/') - attroff(A_BOLD); /* Switch it off once we got * - * and then / */ - prev = ch; - } - endwin(); /* End curses mode */ - fclose(fp); - return 0; + initscr(); /* Start curses mode */ + getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ + while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ + { + getyx(stdscr, y, x); /* get the current curser position */ + if(y == (row - 1)) /* are we are at the end of the screen */ + { + printw("<-Press Any Key->"); /* tell the user to press a key */ + getch(); + clear(); /* clear the screen */ + move(0, 0); /* start at the beginning of the screen */ + } + if(prev == '/' && ch == '*') /* If it is / and * then only + * switch bold on */ + { + attron(A_BOLD); /* cut bold on */ + getyx(stdscr, y, x); /* get the current curser position */ + move(y, x - 1); /* back up one space */ + printw("%c%c", '/', ch); /* The actual printing is done here */ + } else { + printw("%c", ch); + } + refresh(); + + if(prev == '*' && ch == '/') { + attroff(A_BOLD); /* Switch it off once we got */ + } + /* and then / */ + prev = ch; + } + endwin(); /* End curses mode */ + fclose(fp); + return 0; } From 2cb096e27ed7df2a979908cea9eac5a74f53e8c4 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 21 Nov 2009 11:35:38 -0500 Subject: [PATCH 17/21] busted first stab at python version of simple_attr example --- basics/simple_attr.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 basics/simple_attr.py diff --git a/basics/simple_attr.py b/basics/simple_attr.py new file mode 100644 index 0000000..7bfa11e --- /dev/null +++ b/basics/simple_attr.py @@ -0,0 +1,48 @@ +from __future__ import print_function +import sys +import curses + +FILE = {'fp': None} + + +def simple_attr(stdscr): + row, col = stdscr.getmaxyx() + prev = '' + for ch in FILE['fp'].read(): + y, x = stdscr.getyx() + if y == (row - 1): + stdscr.addstr(row - 1, 0, "<-Press Any Key->") + stdscr.getch() + stdscr.clear() + stdscr.move(0, 0) + if prev == '/' and ch == '*': + stdscr.attron(curses.A_BOLD) + y, x = stdscr.getyx() + stdscr.addstr(y, x - 1, '/{0}'.format(ch)) + else: + stdscr.addstr(y, x, ch) + stdscr.refresh() + + if prev == '*' and ch == '/': + stdscr.attroff(curses.A_BOLD) + + prev = ch + + +def main(sysargs=sys.argv[:]): + if not sysargs[1:]: + print('Usage: {0} '.format(sysargs[0])) + return 1 + try: + FILE['fp'] = open(sysargs[1], 'r') + except (OSError, IOError): + print('Cannot open input file', file=sys.stderr) + return 1 + curses.wrapper(simple_attr) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) + +# vim:filetype=python From 996ade64145a3fa8f3c9754e3a34732bb25f464d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 22 Nov 2009 12:22:34 -0500 Subject: [PATCH 18/21] iterating over each char in the file in a more pythonic way, though still not happy with retaining all of the branches from the C version --- basics/simple_attr.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/basics/simple_attr.py b/basics/simple_attr.py index 7bfa11e..efd61a2 100644 --- a/basics/simple_attr.py +++ b/basics/simple_attr.py @@ -8,25 +8,27 @@ FILE = {'fp': None} def simple_attr(stdscr): row, col = stdscr.getmaxyx() prev = '' - for ch in FILE['fp'].read(): - y, x = stdscr.getyx() - if y == (row - 1): - stdscr.addstr(row - 1, 0, "<-Press Any Key->") - stdscr.getch() - stdscr.clear() - stdscr.move(0, 0) - if prev == '/' and ch == '*': - stdscr.attron(curses.A_BOLD) + fp = FILE['fp'] + for line in fp: + for ch in line: y, x = stdscr.getyx() - stdscr.addstr(y, x - 1, '/{0}'.format(ch)) - else: - stdscr.addstr(y, x, ch) - stdscr.refresh() - - if prev == '*' and ch == '/': - stdscr.attroff(curses.A_BOLD) - - prev = ch + if y == (row - 1): + stdscr.addstr(row - 1, 0, "<-Press Any Key->") + stdscr.getch() + stdscr.clear() + stdscr.move(0, 0) + elif prev == '/' and ch == '*': + stdscr.attron(curses.A_BOLD) + y, x = stdscr.getyx() + stdscr.addstr(y, x - 1, '/{0}'.format(ch)) + else: + stdscr.addstr(y, x, ch) + stdscr.refresh() + + if prev == '*' and ch == '/': + stdscr.attroff(curses.A_BOLD) + + prev = ch def main(sysargs=sys.argv[:]): From 256a92a41d1f5228c9b022d0c527747a0896992b Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 23 Nov 2009 08:40:07 -0500 Subject: [PATCH 19/21] not quite fully functional python version of "with_chgat" --- basics/with_chgat.c | 19 +++++-------------- basics/with_chgat.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 basics/with_chgat.py diff --git a/basics/with_chgat.c b/basics/with_chgat.c index 96e23d7..7d347c6 100644 --- a/basics/with_chgat.c +++ b/basics/with_chgat.c @@ -1,24 +1,15 @@ #include int main(int argc, char *argv[]) -{ initscr(); /* Start curses mode */ - start_color(); /* Start color functionality */ +{ + initscr(); + start_color(); init_pair(1, COLOR_CYAN, COLOR_BLACK); printw("A Big string which i didn't care to type fully "); mvchgat(0, 0, -1, A_BLINK, 1, NULL); - /* - * First two parameters specify the position at which to start - * Third parameter number of characters to update. -1 means till - * end of line - * Forth parameter is the normal attribute you wanted to give - * to the charcter - * Fifth is the color index. It is the index given during init_pair() - * use 0 if you didn't want color - * Sixth one is always NULL - */ refresh(); - getch(); - endwin(); /* End curses mode */ + getch(); + endwin(); return 0; } diff --git a/basics/with_chgat.py b/basics/with_chgat.py new file mode 100644 index 0000000..12ade54 --- /dev/null +++ b/basics/with_chgat.py @@ -0,0 +1,22 @@ +import sys +import curses + + +def with_chgat(stdscr): + curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK) + stdscr.addstr("A Big string which i didn't care to type fully ", + curses.color_pair(1)) +# stdscr.chgat(0, 0, -1, curses.A_BLINK) + stdscr.refresh() + stdscr.getch() + + +def main(): + curses.wrapper(with_chgat) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) + +# vim:filetype=python From 4995171f4d6ad71b40419e0eef5712287f5be3f0 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 30 May 2010 15:59:28 -0400 Subject: [PATCH 20/21] consolidating a bunch of lines in the top-level makefile --- Makefile | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index dff988e..2d103e9 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,16 @@ # The top level Makefile +TOP := $(PWD) +PROJECTS := JustForFun basics forms menus panels + + all: - cd JustForFun && $(MAKE) - cd basics && $(MAKE) - cd forms && $(MAKE) - cd menus && $(MAKE) - cd panels && $(MAKE) + $(foreach project,$(PROJECTS),\ + cd $(project) && \ + $(MAKE) && \ + cd $(TOP) \ + ;\ + ) @echo @echo "*********************************************" @echo "All files Built" @@ -13,9 +18,12 @@ all: @echo "Execute each file to see examples in action" @echo "*********************************************" @echo + + clean: - cd JustForFun && $(MAKE) clean - cd basics && $(MAKE) clean - cd forms && $(MAKE) clean - cd menus && $(MAKE) clean - cd panels && $(MAKE) clean + $(foreach project,$(PROJECTS),\ + cd $(project) && \ + $(MAKE) clean && \ + cd $(TOP) \ + ;\ + ) From 354ea866cdd3dc065180d0d5877e597c8cd268f7 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 30 May 2010 16:25:48 -0400 Subject: [PATCH 21/21] python version of with_chgat now has correctly formatted text --- basics/with_chgat.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/basics/with_chgat.py b/basics/with_chgat.py index 12ade54..8b16b07 100644 --- a/basics/with_chgat.py +++ b/basics/with_chgat.py @@ -5,8 +5,7 @@ import curses def with_chgat(stdscr): curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK) stdscr.addstr("A Big string which i didn't care to type fully ", - curses.color_pair(1)) -# stdscr.chgat(0, 0, -1, curses.A_BLINK) + curses.color_pair(1) | curses.A_BLINK) stdscr.refresh() stdscr.getch()