Merge remote-tracking (subtree) branch 'PracticingCurses/master'
This commit is contained in:
27
PracticingCurses/forms/Makefile
Normal file
27
PracticingCurses/forms/Makefile
Normal file
@@ -0,0 +1,27 @@
|
||||
# Makefile for JustForFun Files
|
||||
|
||||
# A few variables
|
||||
|
||||
CC=gcc
|
||||
LIBS=-lform -lncurses
|
||||
|
||||
SRC_DIR=.
|
||||
EXE_DIR=../demo/exe
|
||||
|
||||
EXES = \
|
||||
${EXE_DIR}/form_attrib\
|
||||
${EXE_DIR}/form_options\
|
||||
${EXE_DIR}/form_simple\
|
||||
${EXE_DIR}/form_win \
|
||||
|
||||
${EXE_DIR}/%: %.o
|
||||
${CC} -o $@ $< ${LIBS}
|
||||
|
||||
%.o: ${SRC_DIR}/%.c
|
||||
${CC} -o $@ -c $<
|
||||
|
||||
all: ${EXES}
|
||||
|
||||
|
||||
clean:
|
||||
@rm -f ${EXES}
|
9
PracticingCurses/forms/README
Normal file
9
PracticingCurses/forms/README
Normal file
@@ -0,0 +1,9 @@
|
||||
Description of files
|
||||
--------------------
|
||||
forms
|
||||
|
|
||||
|----> form_attrib.c -- Usage of field attributes
|
||||
|----> form_options.c -- Usage of field options
|
||||
|----> form_simple.c -- A simple form example
|
||||
|----> form_win.c -- Demo of windows associated with forms
|
||||
|
75
PracticingCurses/forms/form_attrib.c
Normal file
75
PracticingCurses/forms/form_attrib.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <form.h>
|
||||
|
||||
int main()
|
||||
{ FIELD *field[3];
|
||||
FORM *my_form;
|
||||
int ch;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
/* Initialize few color pairs */
|
||||
init_pair(1, COLOR_WHITE, COLOR_BLUE);
|
||||
init_pair(2, COLOR_WHITE, COLOR_BLUE);
|
||||
|
||||
/* Initialize the fields */
|
||||
field[0] = new_field(1, 10, 4, 18, 0, 0);
|
||||
field[1] = new_field(1, 10, 6, 18, 0, 0);
|
||||
field[2] = NULL;
|
||||
|
||||
/* Set field options */
|
||||
set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */
|
||||
set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */
|
||||
/* are printed in white */
|
||||
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
|
||||
/* Field is filled up */
|
||||
set_field_back(field[1], A_UNDERLINE);
|
||||
field_opts_off(field[1], O_AUTOSKIP);
|
||||
|
||||
/* Create the form and post it */
|
||||
my_form = new_form(field);
|
||||
post_form(my_form);
|
||||
refresh();
|
||||
|
||||
set_current_field(my_form, field[0]); /* Set focus to the colored field */
|
||||
mvprintw(4, 10, "Value 1:");
|
||||
mvprintw(6, 10, "Value 2:");
|
||||
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
|
||||
refresh();
|
||||
|
||||
/* Loop through to get user requests */
|
||||
while((ch = getch()) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case KEY_DOWN:
|
||||
/* Go to next field */
|
||||
form_driver(my_form, REQ_NEXT_FIELD);
|
||||
/* Go to the end of the present buffer */
|
||||
/* Leaves nicely at the last character */
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
case KEY_UP:
|
||||
/* Go to previous field */
|
||||
form_driver(my_form, REQ_PREV_FIELD);
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
default:
|
||||
/* If this is a normal character, it gets */
|
||||
/* Printed */
|
||||
form_driver(my_form, ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Un post form and free the memory */
|
||||
unpost_form(my_form);
|
||||
free_form(my_form);
|
||||
free_field(field[0]);
|
||||
free_field(field[1]);
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
76
PracticingCurses/forms/form_options.c
Normal file
76
PracticingCurses/forms/form_options.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <form.h>
|
||||
|
||||
#define STARTX 15
|
||||
#define STARTY 4
|
||||
#define WIDTH 25
|
||||
|
||||
#define N_FIELDS 3
|
||||
|
||||
int main()
|
||||
{ FIELD *field[N_FIELDS];
|
||||
FORM *my_form;
|
||||
int ch, i;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
/* Initialize the fields */
|
||||
for(i = 0; i < N_FIELDS - 1; ++i)
|
||||
field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
|
||||
field[N_FIELDS - 1] = NULL;
|
||||
|
||||
/* Set field options */
|
||||
set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */
|
||||
|
||||
field_opts_off(field[0], O_ACTIVE); /* This field is a static label */
|
||||
field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/
|
||||
field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */
|
||||
/* after last character is entered */
|
||||
|
||||
/* Create the form and post it */
|
||||
my_form = new_form(field);
|
||||
post_form(my_form);
|
||||
refresh();
|
||||
|
||||
set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */
|
||||
set_field_buffer(field[0], 0, "This is a static Field");
|
||||
/* Initialize the field */
|
||||
mvprintw(STARTY, STARTX - 10, "Field 1:");
|
||||
mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
|
||||
refresh();
|
||||
|
||||
/* Loop through to get user requests */
|
||||
while((ch = getch()) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case KEY_DOWN:
|
||||
/* Go to next field */
|
||||
form_driver(my_form, REQ_NEXT_FIELD);
|
||||
/* Go to the end of the present buffer */
|
||||
/* Leaves nicely at the last character */
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
case KEY_UP:
|
||||
/* Go to previous field */
|
||||
form_driver(my_form, REQ_PREV_FIELD);
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
default:
|
||||
/* If this is a normal character, it gets */
|
||||
/* Printed */
|
||||
form_driver(my_form, ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Un post form and free the memory */
|
||||
unpost_form(my_form);
|
||||
free_form(my_form);
|
||||
free_field(field[0]);
|
||||
free_field(field[1]);
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
66
PracticingCurses/forms/form_simple.c
Normal file
66
PracticingCurses/forms/form_simple.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <form.h>
|
||||
|
||||
int main()
|
||||
{ FIELD *field[3];
|
||||
FORM *my_form;
|
||||
int ch;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
/* Initialize the fields */
|
||||
field[0] = new_field(1, 10, 4, 18, 0, 0);
|
||||
field[1] = new_field(1, 10, 6, 18, 0, 0);
|
||||
field[2] = NULL;
|
||||
|
||||
/* Set field options */
|
||||
set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */
|
||||
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
|
||||
/* Field is filled up */
|
||||
set_field_back(field[1], A_UNDERLINE);
|
||||
field_opts_off(field[1], O_AUTOSKIP);
|
||||
|
||||
/* Create the form and post it */
|
||||
my_form = new_form(field);
|
||||
post_form(my_form);
|
||||
refresh();
|
||||
|
||||
mvprintw(4, 10, "Value 1:");
|
||||
mvprintw(6, 10, "Value 2:");
|
||||
refresh();
|
||||
|
||||
/* Loop through to get user requests */
|
||||
while((ch = getch()) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case KEY_DOWN:
|
||||
/* Go to next field */
|
||||
form_driver(my_form, REQ_NEXT_FIELD);
|
||||
/* Go to the end of the present buffer */
|
||||
/* Leaves nicely at the last character */
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
case KEY_UP:
|
||||
/* Go to previous field */
|
||||
form_driver(my_form, REQ_PREV_FIELD);
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
default:
|
||||
/* If this is a normal character, it gets */
|
||||
/* Printed */
|
||||
form_driver(my_form, ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Un post form and free the memory */
|
||||
unpost_form(my_form);
|
||||
free_form(my_form);
|
||||
free_field(field[0]);
|
||||
free_field(field[1]);
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
112
PracticingCurses/forms/form_win.c
Normal file
112
PracticingCurses/forms/form_win.c
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <form.h>
|
||||
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
|
||||
|
||||
int main()
|
||||
{
|
||||
FIELD *field[3];
|
||||
FORM *my_form;
|
||||
WINDOW *my_form_win;
|
||||
int ch, rows, cols;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
/* Initialize few color pairs */
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
|
||||
/* Initialize the fields */
|
||||
field[0] = new_field(1, 10, 6, 1, 0, 0);
|
||||
field[1] = new_field(1, 10, 8, 1, 0, 0);
|
||||
field[2] = NULL;
|
||||
|
||||
/* Set field options */
|
||||
set_field_back(field[0], A_UNDERLINE);
|
||||
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
|
||||
/* Field is filled up */
|
||||
set_field_back(field[1], A_UNDERLINE);
|
||||
field_opts_off(field[1], O_AUTOSKIP);
|
||||
|
||||
/* Create the form and post it */
|
||||
my_form = new_form(field);
|
||||
|
||||
/* Calculate the area required for the form */
|
||||
scale_form(my_form, &rows, &cols);
|
||||
|
||||
/* Create the window to be associated with the form */
|
||||
my_form_win = newwin(rows + 4, cols + 4, 4, 4);
|
||||
keypad(my_form_win, TRUE);
|
||||
|
||||
/* Set main window and sub window */
|
||||
set_form_win(my_form, my_form_win);
|
||||
set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(my_form_win, 0, 0);
|
||||
print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));
|
||||
|
||||
post_form(my_form);
|
||||
wrefresh(my_form_win);
|
||||
|
||||
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
|
||||
refresh();
|
||||
|
||||
/* Loop through to get user requests */
|
||||
while((ch = wgetch(my_form_win)) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case KEY_DOWN:
|
||||
/* Go to next field */
|
||||
form_driver(my_form, REQ_NEXT_FIELD);
|
||||
/* Go to the end of the present buffer */
|
||||
/* Leaves nicely at the last character */
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
case KEY_UP:
|
||||
/* Go to previous field */
|
||||
form_driver(my_form, REQ_PREV_FIELD);
|
||||
form_driver(my_form, REQ_END_LINE);
|
||||
break;
|
||||
default:
|
||||
/* If this is a normal character, it gets */
|
||||
/* Printed */
|
||||
form_driver(my_form, ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Un post form and free the memory */
|
||||
unpost_form(my_form);
|
||||
free_form(my_form);
|
||||
free_field(field[0]);
|
||||
free_field(field[1]);
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
|
||||
{ int length, x, y;
|
||||
float temp;
|
||||
|
||||
if(win == NULL)
|
||||
win = stdscr;
|
||||
getyx(win, y, x);
|
||||
if(startx != 0)
|
||||
x = startx;
|
||||
if(starty != 0)
|
||||
y = starty;
|
||||
if(width == 0)
|
||||
width = 80;
|
||||
|
||||
length = strlen(string);
|
||||
temp = (width - length)/ 2;
|
||||
x = startx + (int)temp;
|
||||
wattron(win, color);
|
||||
mvwprintw(win, y, x, "%s", string);
|
||||
wattroff(win, color);
|
||||
refresh();
|
||||
}
|
Reference in New Issue
Block a user