Add all the necessary bits to use color if the terminal allows it.

You can now specify separate attributes for selected/not selected
cases individually for each field and also an attr for the form as
a whole so you can now have colored backgrounds for the form and
different coloured fields etc.

Update the example.

Change the copyright to a BSD style one.
This commit is contained in:
Paul Richards 1995-01-30 02:41:29 +00:00
parent 31892b894c
commit 0d595e6acd
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=6020
4 changed files with 118 additions and 53 deletions

View file

@ -1,5 +1,10 @@
Colors example {
pair = red, yellow
pair = blue, white
}
Field Title {
attributes = A_BOLD
attributes = COLOR_PAIR(2)
text = "A Simple Demo"
}
@ -37,13 +42,13 @@ Field field7 {
Field field8 {
width = 6
attributes = A_BOLD|A_REVERSE
action = "EXIT"
function = exit_form
}
Field field9 {
attributes = A_BOLD|A_REVERSE
attributes = COLOR_PAIR(1)
highlight = COLOR_PAIR(1)|A_BOLD
action = "CANCEL"
function = cancel_form
}
@ -80,6 +85,8 @@ Link cancel as field9 {
Form example at 0,0 {
height = 24
width = 80
colortable = example
attributes = COLOR_PAIR(1)
Field Title at 0,30

View file

@ -1,24 +1,46 @@
/*
* Copyright (c) 1995 Paul Richards.
/*-
* Copyright (c) 1995
* Paul Richards. All rights reserved.
*
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* verbatim and that no modifications are made prior to this
* point in the file.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Paul Richards.
* 4. The name Paul Richards may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This software may be used, modified, copied, distributed, and
* sold, in both source and binary form provided that the above
* copyright and these terms are retained, verbatim, as the first
* lines of this file. Under no circumstances is the author
* responsible for the proper functioning of this software, nor does
* the author assume any responsibility for damages incurred with
* its use.
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <forms.h>
#include <ncurses.h>
#include "internal.h"
unsigned int keymap[] = {
unsigned int f_keymap[] = {
KEY_UP, /* F_UP */
KEY_DOWN, /* F_DOWN */
9, /* F_RIGHT */
@ -28,8 +50,9 @@ unsigned int keymap[] = {
KEY_RIGHT, /* F_CRIGHT */
KEY_HOME, /* F_CHOME */
KEY_END, /* F_CEND */
263, /* F_CBS */
330 /* F_CDEL */
263, /* F_CBS */
330, /* F_CDEL */
10 /* F_ACCEPT */
};
int done=0;
@ -39,7 +62,15 @@ initfrm(struct form *form)
{
struct field *field = &form->field[0];
int i;
if (has_colors()) {
start_color();
if (form->color_table)
for (i=0; form->color_table[i].f != -1; i++) {
init_pair(i+1, form->color_table[i].f, form->color_table[i].b);
}
}
cbreak();
noecho();
@ -52,6 +83,7 @@ initfrm(struct form *form)
keypad(form->window, TRUE);
while (field->type != F_END) {
if (field->type == F_INPUT) {
field->field.input->input = malloc(field->field.input->limit);
@ -81,6 +113,7 @@ initfrm(struct form *form)
form->no_fields++;
field = &form->field[form->no_fields];
}
form->current_field = form->start_field;
show_form(form);
return (OK);
}
@ -104,12 +137,14 @@ endfrm(struct form *form)
int
update_form(struct form *form)
{
int selattr;
show_form(form);
if (form->current_field == -1)
return (F_CANCEL);
wattrset(form->window, form->field[form->current_field].selattr);
switch (form->field[form->current_field].type) {
case F_MENU:
field_menu(form);
@ -125,6 +160,7 @@ update_form(struct form *form)
print_status("Error, current field is invalid");
return (F_CANCEL);
}
wattrset(form->window, 0);
return (done);
}
@ -133,9 +169,15 @@ static void
show_form(struct form *form)
{
int i;
int y, x;
wattrset(form->window, form->attr);
for (y = 0; y < form->height; y++)
for (x = 0; x < form->width; x++)
mvwaddch(form->window, y, x, ' ');
for (i=0; i < form->no_fields; i++) {
wattrset(form->window, 0);
wattrset(form->window, form->field[i].attr);
wmove(form->window, form->field[i].y, form->field[i].x);
switch (form->field[i].type) {
case F_TEXT:
@ -155,6 +197,7 @@ show_form(struct form *form)
break;
}
}
wattrset(form->window, 0);
wrefresh(form->window);
}
@ -164,8 +207,6 @@ disp_text(struct form *form, int index)
struct field *field = &form->field[index];
wattron(form->window, field->attr);
if (print_string(form->window, field->y, field->x,
field->width, field->field.text->text) == ERR)
print_status("Illegal scroll in print_string");
@ -177,8 +218,6 @@ disp_input(struct form *form, int index)
struct field *field = &form->field[index];
wattron(form->window, field->attr);
if (field->field.input->lbl_flag) {
if (print_string(form->window, field->y, field->x,
field->width, field->field.input->label) == ERR)
@ -187,7 +226,6 @@ disp_input(struct form *form, int index)
if (print_string(form->window, field->y, field->x,
field->width, field->field.input->input) == ERR)
print_status("Illegal scroll in print_string");
}
static void
@ -195,8 +233,6 @@ disp_menu(struct form *form, int index)
{
struct field *field = &form->field[index];
wattron(form->window, field->attr);
if (print_string(form->window, field->y, field->x,
field->width,
field->field.menu->options[field->field.menu->selected]) == ERR)
@ -208,14 +244,10 @@ disp_action(struct form *form, int index)
{
struct field *field = &form->field[index];
wattron(form->window, field->attr);
if (print_string(form->window, field->y, field->x,
field->width,
field->field.action->text) == ERR)
print_status("Illegal scroll in print_string");
}
static void
@ -226,7 +258,6 @@ field_action(struct form *form)
int ch;
for (;;) {
wattron(form->window, F_SELATTR);
disp_action(form, form->current_field);
wmove(form->window, field->y, field->x);
ch = wgetch(form->window);
@ -247,7 +278,6 @@ field_menu(struct form *form)
int ch;
for (;;) {
wattron(form->window, F_SELATTR);
disp_menu(form, form->current_field);
wmove(form->window, field->y, field->x);
switch (ch = wgetch(form->window)) {
@ -367,7 +397,6 @@ field_input(struct form *form)
#define CURSPOS ((len < field->width) ? len : field->width)
len = strlen(field->field.input->input);
wattron(form->window, F_SELATTR);
disp_input(form, form->current_field);
cursor = CURSPOS;

View file

@ -1,15 +1,35 @@
/*
* Copyright (c) 1995 Paul Richards.
/*-
* Copyright (c) 1995
* Paul Richards. All rights reserved.
*
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* verbatim and that no modifications are made prior to this
* point in the file.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Paul Richards.
* 4. The name Paul Richards may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This software may be used, modified, copied, distributed, and
* sold, in both source and binary form provided that the above
* copyright and these terms are retained, verbatim, as the first
* lines of this file. Under no circumstances is the author
* responsible for the proper functioning of this software, nor does
* the author assume any responsibility for damages incurred with
* its use.
*/
#include <ncurses.h>
@ -26,14 +46,22 @@
#define F_DONE 1
#define F_CANCEL -1
struct col_pair {
int f;
int b;
};
struct form {
int no_fields;
int start_field;
int current_field;
struct field *field;
int height;
int width;
int y;
int x;
int attr;
struct col_pair *color_table;
WINDOW *window;
};
@ -68,6 +96,7 @@ struct field {
int x;
int width;
int attr;
int selattr;
int next;
int up;
int down;

View file

@ -12,18 +12,18 @@
* its use.
*/
#define F_UP keymap[0]
#define F_DOWN keymap[1]
#define F_RIGHT keymap[2]
#define F_LEFT keymap[3]
#define F_NEXT keymap[4]
#define F_CLEFT keymap[5]
#define F_CRIGHT keymap[6]
#define F_CHOME keymap[7]
#define F_CEND keymap[8]
#define F_CBS keymap[9]
#define F_CDEL keymap[10]
#define F_ACCEPT F_NEXT
#define F_UP f_keymap[0]
#define F_DOWN f_keymap[1]
#define F_RIGHT f_keymap[2]
#define F_LEFT f_keymap[3]
#define F_NEXT f_keymap[4]
#define F_CLEFT f_keymap[5]
#define F_CRIGHT f_keymap[6]
#define F_CHOME f_keymap[7]
#define F_CEND f_keymap[8]
#define F_CBS f_keymap[9]
#define F_CDEL f_keymap[10]
#define F_ACCEPT f_keymap[11]
/* Private function declarations */
static void show_form(struct form *);