home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-05-18 | 29.6 KB | 1,231 lines |
- Newsgroups: comp.sources.unix
- From: lab@techno.sth.cgl.se (Lars Berntzon)
- Subject: v26i058: cforms - forms management front end for curses(3), Part03/03
- Sender: unix-sources-moderator@pa.dec.com
- Approved: vixie@pa.dec.com
-
- Submitted-By: lab@techno.sth.cgl.se (Lars Berntzon)
- Posting-Number: Volume 26, Issue 58
- Archive-Name: cforms/part03
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 3 (of 3)."
- # Contents: src/cflib.c
- # Wrapped by vixie@cognition.pa.dec.com on Tue May 19 19:14:30 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'src/cflib.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'src/cflib.c'\"
- else
- echo shar: Extracting \"'src/cflib.c'\" \(27545 characters\)
- sed "s/^X//" >'src/cflib.c' <<'END_OF_FILE'
- X /*****************************************************************************
- X *
- X * C F O R M S . C
- X * ---------------
- X *
- X * Description:
- X *
- X * Included functions:
- X *
- X * Start/stop functions
- X * --------------------
- X * cforms_init - Initiate CForms
- X * cforms_end - Stop CForms
- X *
- X * Picture functions
- X * -----------------
- X * pic_call - Call picture
- X * pic_clear - Clear all fields for picture
- X * pic_leave - Leave current picture
- X * picture - Get picture with name
- X *
- X * Field functions
- X * ---------------
- X * field - Get field with name
- X * fld_isempty - Check if field is empty
- X * fld_first - Find first field of picture
- X * fld_next - Find next field for picture
- X * fld_previous - Find previous field for picture
- X * fld_left - Find left field
- X * fld_right - Find right field
- X * fld_up - Find field above current
- X * fld_down - Find field below current
- X * fld_set - Set value for field
- X * fld_nset - Set value for field but max n chars
- X * fld_get - Get value of field
- X * fld_len - Return length of field
- X * fld_ismodified - Return true if field has been modified since fld_set
- X * fld_touch - Make field not modified
- X *
- X * General functions
- X * -----------------
- X * message - Give message (line 24)
- X * strequ - Compqare two strings, but case insensitive
- X *
- X * Local functions
- X * ---------------
- X * hdl_field - This is the head function for cforms
- X * getraw - Get raw key
- X * draw_picture - Draw picture wiuth fields
- X * check_int - Checkfunction for integer fields
- X * check_alnum - -''- alphanumeric fields
- X * check_str - -''- string fields
- X * adjust_right - Adjust string to right
- X * generate_event - Raise event and call event functions
- X *
- X * Global variables:
- X * current.picture - Allways points to current picture (can be NULL)
- X * current.field - Allways points to current field (can be NULL)
- X *
- X * Revision:
- X * Ver Date By Reason
- X * --- ---- -- ------
- X * 1.00 900629 Lars Berntzon Created
- X *
- X ****************************************************************************/
- X
- X#include "config.h"
- X
- X#include <stdio.h>
- X#include <string.h>
- X#include <ctype.h>
- X#include <signal.h>
- X#include <stdarg.h>
- X#ifdef STDLIB_H
- X#include <stdlib.h>
- X#endif
- X#ifdef MALLOC_H
- X#include <malloc.h>
- X#endif
- X
- X#include <curses.h>
- X#include "cforms.h"
- X
- X#define KEY_CTRL(c) (toupper(c) - 'A' + 1)
- X
- X /* G l o b a l v a r i a b l e s */
- X
- Xstruct current current; /* Current field and picture */
- X
- X /* L o c a l v a r i a b l e s */
- X
- Xstatic int sigcatch(void);
- Xstatic int _message(char *fmt, va_list arg);
- Xstatic int check_int(int *ch, char *txt, int *pos, int len);
- Xstatic int check_alnum(int *ch, char *txt, int *pos, int len);
- Xstatic int check_str(int *ch, char *txt, int *pos, int len);
- Xstatic int draw_picture(struct picture *pic);
- Xstatic int hdl_field(struct field *fld);
- Xstatic int adjust_right(char *txt, int len);
- X
- Xstatic int _leave_picture;
- Xstatic int cforms_inited = FALSE;
- X
- X
- X/******************************************************************************
- X *
- X * C F O R M S _ I N I T
- X * ---------------------
- X * Description:
- X * Initialize cforms. (currently only initscr).
- X *
- X *****************************************************************************/
- Xint
- Xcforms_init(void)
- X{
- X struct pivture *pp;
- X struct field *fp;
- X int pic, fld;
- X
- X cforms_inited = TRUE;
- X
- X signal(SIGINT, (SIGNAL_TYPE)sigcatch);
- X
- X if (initscr() == NULL) {
- X return FAIL;
- X }
- X keypad(stdscr,TRUE);
- X
- X /*
- X * Save picture pointer for every field.
- X */
- X for(pic = 0; pic < _module.n_pictures; pic++) {
- X for(fld = 0; fld < _module.picture[pic].n_fields; fld++) {
- X _module.picture[pic].field[fld].picture = &_module.picture[pic];
- X }
- X }
- X
- X raw();
- X noecho();
- X
- X return OK;
- X}
- X
- X/******************************************************************************
- X *
- X * C F O R M S _ E N D
- X * -------------------
- X *
- X * Description:
- X * Turns off cforms.
- X *
- X *****************************************************************************/
- Xint
- Xcforms_end(void)
- X{
- X cforms_inited = FALSE;
- X move(23, 1);
- X refresh();
- X endwin();
- X
- X return OK;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * P I C _ C A L L
- X * ---------------
- X *
- X *
- X * Description:
- X * This is the entry point for application into CForms
- X *
- X * Input:
- X * pic - Picture to call
- X * field_name - Name of field to start at, (NULL means first field).
- X *
- X * Global:
- X * current.picture - Is set up to point to this picture
- X *
- X ******************************************************************************/
- Xint
- Xpic_call(struct picture *pic, char *field_name)
- X{
- X struct picture *old_picture = current.picture;
- X struct field *old_field = current.field;
- X
- X if (cforms_inited == FALSE) {
- X fprintf(stderr, "** CForms not initialized **\n");
- X return FAIL;
- X }
- X
- X if (pic == NULL) return FAIL;
- X
- X current.picture = pic;
- X
- X if (current.field) generate_event(EVENT_EXIT, 0);
- X
- X draw_picture(pic);
- X
- X /*
- X * To prevent generation of an event line exit for olf field but on new
- X * picture, current field is set to NULL;
- X */
- X current.field = NULL;
- X
- X generate_event(EVENT_DRAW, 0);
- X
- X if (pic->n_fields != 0) {
- X if (field_name) fld_move(field(field_name));
- X else fld_move(fld_first());
- X
- X while(_leave_picture == FALSE) {
- X hdl_field(current.field);
- X }
- X generate_event(EVENT_EXIT, 0);
- X _leave_picture = FALSE;
- X }
- X
- X if (current.picture = old_picture) {
- X draw_picture(current.picture);
- X generate_event(EVENT_REFRESH, 0);
- X
- X /*
- X * Set to NULL for same reason as above.
- X */
- X current.field = NULL;
- X fld_move(old_field);
- X }
- X return OK;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * P I C T U R E
- X * -------------
- X *
- X * Description:
- X * Finds a picture with name as described by arguments in printf format.
- X *
- X ******************************************************************************/
- Xstruct picture *
- Xpicture(char *fmt, ...)
- X{
- X static char pic_name[100];
- X va_list arg;
- X int i;
- X
- X va_start(arg, fmt);
- X vsprintf(pic_name, fmt, arg);
- X va_end(arg);
- X
- X for(i = 0; i < _module.n_pictures; i++) {
- X if (strequ(pic_name, _module.picture[i].name) == 0) {
- X return &_module.picture[i];
- X }
- X }
- X return NULL;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * P I C _ L E A V E
- X * -----------------
- X *
- X * Description:
- X * Leave picture uppon exit of current event.
- X *
- X ******************************************************************************/
- Xint
- Xpic_leave()
- X{
- X _leave_picture = TRUE;
- X return OK;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * P I C _ C L E A R
- X * -----------------
- X *
- X * Description:
- X * Clear all field of picture 'pic'.
- X *
- X ******************************************************************************/
- Xint
- Xpic_clear(struct picture *pic)
- X{
- X int i, n;
- X
- X if (pic == NULL) pic = current.picture;
- X if (pic == NULL) return FAIL;
- X
- X for(i = 0; i < pic->n_fields; i++) {
- X pic->field[i].data[0] = 0;
- X }
- X
- X if (current.picture == pic) {
- X for(i = 0; i < pic->n_fields; i++) {
- X move(pic->field[i].pos.y + pic->y, pic->field[i].pos.x + pic->x);
- X for(n = 0; n < pic->field[i].len; n++) {
- X addch(' ');
- X }
- X }
- X }
- X return OK;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * H D L _ F I E L D
- X * -----------------
- X *
- X * Description:
- X * Handles the intput of one field.
- X *
- X ******************************************************************************/
- Xstatic int
- Xhdl_field(struct field *fld)
- X{
- X int (*check_p)();
- X int x, y, ch, i, pos = 0;
- X struct field *tmp_fld;
- X
- X move((y = fld->pos.y) + fld->picture->y, (x = fld->pos.x) + fld->picture->x);
- X
- X switch(fld->type & FLD_TYPE) {
- X case FLD_STR:
- X check_p = check_str;
- X break;
- X
- X case FLD_ALNUM:
- X check_p = check_alnum;
- X break;
- X
- X case FLD_INT:
- X check_p = check_int;
- X break;
- X
- X default:
- X message("Illegal type, probably a bug");
- X }
- X
- X i = strlen(fld->data);
- X memset(fld->data + i, ' ', fld->len - i);
- X fld->data[fld->len] = 0;
- X
- X while (refresh(), _leave_picture == FALSE && (ch = getch()) != EOF)
- X {
- X message_nr("");
- X
- X if (generate_event(EVENT_KEY, ch)) return OK;
- X else if (ch == KEY_CTRL('w')) {
- X clear();
- X draw_picture(current.picture);
- X if (generate_event(EVENT_REFRESH, 0)) return OK;
- X }
- X else if (ch == '\n') {
- X if (fld->type & FLD_RIGHT) {
- X adjust_right(fld->data, fld->len);
- X }
- X }
- X else if (ch == KEY_LEFT) {
- X if (pos <= 0) {
- X if (generate_event(EVENT_PREVIOUS, 0)) return OK;
- X if (tmp_fld = fld_left(current.field)) {
- X fld_move(tmp_fld);
- X return OK;
- X }
- X else message("Can't move left");
- X }
- X else {
- X pos--;
- X }
- X }
- X else if (ch == KEY_RIGHT) {
- X if (pos >= fld->len - 1) {
- X if (generate_event(EVENT_NEXT, 0)) return OK;
- X if (tmp_fld = fld_right(current.field)) {
- X fld_move(tmp_fld);
- X return OK;
- X }
- X else message("Can't move right");
- X }
- X else {
- X pos++;
- X }
- X }
- X else if (fld->flags & FLD_PROTECTED) message("No input allowed");
- X else if (ch == '\b') {
- X if (pos > 0) {
- X fld->data[--pos] = ' ';
- X }
- X fld->modified = 1;
- X }
- X else if (!isprint(ch)) message("Unknown key (%d)", ch);
- X else if (check_p(&ch, fld->data, &pos, fld->len)) {
- X if (fld->flags & FLD_UPPERCASE) ch = toupper(ch);
- X fld->data[pos] = ch;
- X if (pos < fld->len - 1) pos++;
- X fld->modified = 1;
- X }
- X move(y + fld->picture->y, x + fld->picture->x);
- X addstr(fld->data);
- X move(y + fld->picture->y, x + fld->picture->x + pos);
- X }
- X return OK;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * M E S S A G E, M E S S A G E _ N R
- X * ----------------------------------
- X *
- X * Description:
- X * Give message on bottom screen line. Work the same way as printf.
- X * message_nr works the same as message but gives no refresh.
- X *
- X ******************************************************************************/
- Xint
- Xmessage(char *fmt, ...)
- X{
- X va_list arg;
- X va_start(arg, fmt);
- X _message(fmt, arg);
- X va_end(arg);
- X refresh();
- X return OK;
- X}
- X
- Xint
- Xmessage_nr(char *fmt, ...)
- X{
- X va_list arg;
- X va_start(arg, fmt);
- X _message(fmt, arg);
- X va_end(arg);
- X return OK;
- X}
- X
- Xstatic int
- X_message(char *fmt, va_list arg)
- X{
- X static char msg_str[160];
- X char tmp_str[160];
- X int x, y;
- X
- X getyx(stdscr, y, x);
- X
- X move(23, 1);
- X if (fmt != NULL) {
- X vsprintf(tmp_str, fmt, arg);
- X sprintf(msg_str, "%-50.50s", tmp_str);
- X }
- X addstr(msg_str);
- X move(y, x);
- X return OK;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * C H E C K *
- X * -----------
- X *
- X * Description:
- X * Various check functions depending of type of field.
- X * If function returnes TRUE, the char ch is put into field.
- X *
- X ******************************************************************************/
- Xstatic int
- Xcheck_int(int *ch, char *txt, int *pos, int len)
- X{
- X int i;
- X if (isdigit(*ch)) {
- X return TRUE;
- X }
- X
- X if (isspace(*ch)) {
- X for(i = 0; i < *pos; i++) {
- X if (!isspace(txt[i])) {
- X message("No space in middle of value");
- X return FALSE;
- X }
- X }
- X return TRUE;
- X }
- X message ("Must be digit");
- X return FALSE;
- X}
- X
- Xstatic int
- Xcheck_alnum(int *ch, char *txt, int *pos, int len)
- X{
- X if (isalnum(*ch) || isspace(*ch)) return TRUE;
- X message("Must be alphanumeric");
- X return FALSE;
- X}
- X
- Xstatic int
- Xcheck_str(int *ch, char *txt, int *pos, int len)
- X{
- X if (isprint(*ch)) return TRUE;
- X message("Must be printable");
- X return FALSE;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * A D J U S T _ R I G H T
- X * -----------------------
- X *
- X * Description:
- X * Adjust a text to the right
- X *
- X ******************************************************************************/
- Xstatic int
- Xadjust_right(char *txt, int len)
- X{
- X int last_space = len - 1;
- X
- X while(isspace(txt[last_space]) && last_space >= 0)
- X last_space--;
- X last_space++;
- X memcpy(txt + len - last_space, txt, last_space);
- X memset(txt, ' ', len - last_space);
- X
- X return OK;
- X}
- X
- X/*******************************************************************************
- X *
- X * G E N E R A T E _ E V E N T
- X * ---------------------------
- X *
- X * Description:
- X * Generate a event of type and with code, and calls first function
- X * for field or picture that should respond to the event.
- X *
- X * Input:
- X * ev_type - Event type, ex. EVENT_KEY, EVENT_ENTRY.
- X * ev_code - Code of event (not for all types), ex KEY_UP.
- X *
- X * Return:
- X * Boolean, TRUE if event code was found and called. Events are not
- X * called for 'event forget' statements (function = NULL).
- X *
- X ******************************************************************************/
- Xint
- Xgenerate_event(ev_type, ev_code)
- X{
- X int i;
- X
- X for(i = 0; current.field && i < current.field->n_events; i++) {
- X if (current.field->event[i].type == ev_type &&
- X current.field->event[i].code == ev_code) {
- X if (current.field->event[i].func == NULL) {
- X return FALSE;
- X }
- X (*current.field->event[i].func)();
- X return TRUE;
- X }
- X }
- X
- X for(i = 0; current.picture && i < current.picture->n_events; i++) {
- X if (current.picture->event[i].type == ev_type &&
- X current.picture->event[i].code == ev_code) {
- X if (current.picture->event[i].func == NULL) {
- X return FALSE;
- X }
- X (*current.picture->event[i].func)();
- X return TRUE;
- X }
- X }
- X
- X for(i = 0; i < _module.n_events; i++) {
- X if (_module.event[i].type == ev_type &&
- X _module.event[i].code == ev_code) {
- X if (_module.event[i].func == NULL) {
- X return FALSE;
- X }
- X (*_module.event[i].func)();
- X return TRUE;
- X }
- X }
- X
- X return FALSE;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F L D _ M O V E
- X * ---------------
- X *
- X * Description:
- X * Move to field
- X *
- X * Input:
- X * fld - Field to move to.
- X *
- X * Return:
- X * field move to.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfld_move(struct field *fld)
- X{
- X if (fld == NULL) return current.field;
- X
- X if (current.field) {
- X generate_event(EVENT_EXIT, 0);
- X }
- X current.field = fld;
- X generate_event(EVENT_ENTRY, 0);
- X
- X return current.field;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F L D _ G E T
- X * -------------
- X *
- X * Description:
- X * Return text in field.
- X *
- X ******************************************************************************/
- Xchar *
- Xfld_get(struct field *fld)
- X{
- X if (fld == NULL) fld = current.field;
- X if (fld) return fld->data;
- X
- X return "";
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F L D _ S E T
- X * -------------
- X *
- X * Description:
- X * Set value for field.
- X *
- X ******************************************************************************/
- Xint
- Xfld_set(struct field *fld, char *data)
- X{
- X int i;
- X
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) return FAIL;
- X
- X memset(fld->data, ' ', fld->len);
- X i = strlen(data);
- X if (i > fld->len) i = fld->len;
- X strncpy(fld->data, data, i);
- X fld->modified = 0;
- X
- X if (fld->picture == current.picture) {
- X move(fld->pos.y + fld->picture->y, fld->pos.x + fld->picture->x);
- X waddstr(stdscr, fld->data);
- X }
- X return OK;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F L D _ N S E T
- X * ---------------
- X *
- X * Description:
- X * Copy at most 'n' chars from input and set field to that.
- X *
- X ******************************************************************************/
- Xint
- Xfld_nset(struct field *fld, char *data, int n)
- X{
- X char tmpstr[200];
- X strncpy(tmpstr, data, n);
- X tmpstr[n] = 0;
- X return fld_set(fld, tmpstr);
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F L D _ L E N
- X * -------------
- X *
- X * Description:
- X * Return length of field.
- X *
- X ******************************************************************************/
- Xint
- Xfld_len(struct field *fld)
- X{
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) return 0;
- X return fld->len;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ I S M O D I F I E D
- X * ---------------------------
- X *
- X * Description:
- X * Return TRUE if field has been modified since last fld_set.
- X *
- X ******************************************************************************/
- Xint
- Xfld_ismodified(struct field *fld)
- X{
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) return 0;
- X return fld->modified;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ T O U C H
- X * -----------------
- X *
- X * Description:
- X * Make field not modified.
- X *
- X ******************************************************************************/
- Xvoid
- Xfld_touch(struct field *fld)
- X{
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) return;
- X fld->modified = 0;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F I E L D
- X * ---------
- X *
- X * Description:
- X * Return pointer to field with name. Name works as printf.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfield(char *fmt, ...)
- X{
- X static char field_name[100];
- X struct picture *pic;
- X va_list arg;
- X char *p;
- X int i;
- X
- X va_start(arg, fmt);
- X vsprintf(field_name, fmt, arg);
- X va_end(arg);
- X
- X if (p = strchr(field_name, ':')) {
- X *p++ = 0;
- X pic = picture(field_name);
- X }
- X else {
- X pic = current.picture;
- X p = field_name;
- X }
- X
- X if (pic == NULL) return NULL;
- X
- X for(i = 0; i < pic->n_fields; i++) {
- X if(strequ(p, pic->field[i].name) == 0) return &pic->field[i];
- X }
- X
- X return NULL;
- X}
- X
- X/*******************************************************************************
- X *
- X * S T R E Q U
- X * -----------
- X *
- X * Description:
- X * Case insensitive strcmp.
- X *
- X ******************************************************************************/
- Xint
- Xstrequ(char *s1, char *s2)
- X{
- X while(*s1 && toupper(*s1) == toupper(*s2))
- X s1++, s2++;
- X
- X return toupper(*s1) - toupper(*s2);
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F L D _ L E F T
- X * ---------------
- X *
- X * Description:
- X * Return pointer to a field left of 'fld'.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfld_left(struct field *fld)
- X{
- X int i;
- X int min_dist = 1000000;
- X int dist;
- X struct field *found = NULL;
- X
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) fld = fld_first();
- X if (fld == NULL) return NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X if (fld != ¤t.picture->field[i] &&
- X current.picture->field[i].pos.x < fld->pos.x)
- X {
- X dist = (fld->pos.x - current.picture->field[i].pos.x) << 9;
- X dist += abs(fld->pos.y - current.picture->field[i].pos.y);
- X if (dist < min_dist)
- X {
- X min_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X }
- X
- X return found;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ R I G H T
- X * -----------------
- X *
- X * Description:
- X * Return pointer to a field right of 'fld'.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfld_right(struct field *fld)
- X{
- X int i;
- X int min_dist = 1000000;
- X int dist;
- X struct field *found = NULL;
- X
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) fld = fld_first();
- X if (fld == NULL) return NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X if (fld != ¤t.picture->field[i] &&
- X current.picture->field[i].pos.x > fld->pos.x)
- X {
- X dist = (current.picture->field[i].pos.x - fld->pos.x) << 9;
- X dist += abs(fld->pos.y - current.picture->field[i].pos.y);
- X if (dist < min_dist)
- X {
- X min_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X }
- X
- X return found;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ D O W N
- X * ---------------
- X *
- X * Description:
- X * Return a pointer to field below 'fld'.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfld_down(struct field *fld)
- X{
- X int i;
- X int min_dist = 1000000;
- X int dist;
- X struct field *found = NULL;
- X
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) fld = fld_first();
- X if (fld == NULL) return NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X if (fld != ¤t.picture->field[i] &&
- X current.picture->field[i].pos.y > fld->pos.y)
- X {
- X dist = (current.picture->field[i].pos.y - fld->pos.y) << 9;
- X dist += abs(current.picture->field[i].pos.x - fld->pos.x);
- X if (dist < min_dist)
- X {
- X min_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X }
- X
- X return found;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ U P
- X * -----------
- X *
- X * Description:
- X * Return pointer to field above 'fld'.
- X *
- X ******************************************************************************/
- Xstruct field *fld_up(struct field *fld)
- X{
- X int i;
- X int min_dist = 1000000;
- X int dist;
- X struct field *found = NULL;
- X
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) fld = fld_first();
- X if (fld == NULL) return NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X if (fld != ¤t.picture->field[i] &&
- X current.picture->field[i].pos.y < fld->pos.y)
- X {
- X dist = (fld->pos.y - current.picture->field[i].pos.y) << 9;
- X dist += abs(current.picture->field[i].pos.x - fld->pos.x);
- X if (dist < min_dist)
- X {
- X min_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X }
- X
- X return found;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ F I R S T
- X * -----------------
- X *
- X * Description:
- X * Find first field in picture.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfld_first(void)
- X{
- X int i;
- X int min_dist = 1000000;
- X int dist;
- X struct field *found = NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X dist = current.picture->field[i].pos.y << 9;
- X dist += current.picture->field[i].pos.x;
- X if (dist < min_dist)
- X {
- X min_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X
- X return found;
- X}
- X
- X
- X/*******************************************************************************
- X *
- X * F L D _ L A S T
- X * ---------------
- X *
- X * Description:
- X * Find last field in picture.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfld_last(void)
- X{
- X int i;
- X int max_dist = -1;
- X int dist;
- X struct field *found = NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X dist = current.picture->field[i].pos.y << 9;
- X dist += current.picture->field[i].pos.x;
- X if (dist > max_dist)
- X {
- X max_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X
- X return found;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ N E X T
- X * ---------------
- X *
- X * Description:
- X * Find next field in picture.
- X *
- X ******************************************************************************/
- Xstruct field *
- Xfld_next(struct field *fld)
- X{
- X int i;
- X int min_dist = 1000000;
- X int dist;
- X struct field *found = NULL;
- X
- X if (fld == NULL) fld = current.field;
- X if (fld == NULL) fld = fld_first();
- X if (fld == NULL) return NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X dist = (current.picture->field[i].pos.y - fld->pos.y)<< 9;
- X dist += current.picture->field[i].pos.x - fld->pos.x;
- X
- X if (dist > 0 && dist < min_dist)
- X {
- X min_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X
- X return found;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ P R E V I O U S
- X * -----------------------
- X *
- X * Description:
- X * Find previoud field in picture.
- X *
- X ******************************************************************************/
- Xstruct field *fld_previous(struct field *fld)
- X{
- X int i;
- X int min_dist = 1000000;
- X int dist;
- X struct field *found = NULL;
- X
- X for(i = 0; i < current.picture->n_fields; i++)
- X {
- X dist = (fld->pos.y - current.picture->field[i].pos.y) << 9;
- X dist += fld->pos.x - current.picture->field[i].pos.x;
- X
- X if (dist > 0 && dist < min_dist)
- X {
- X min_dist = dist;
- X found = ¤t.picture->field[i];
- X }
- X }
- X
- X return found;
- X}
- X
- X/*******************************************************************************
- X *
- X * F L D _ I S E M P T Y
- X * ---------------------
- X *
- X * Description:
- X * Return true if field contains only space or nulls.
- X *
- X ******************************************************************************/
- Xint
- Xfld_isempty(struct field *fld)
- X{
- X int i;
- X
- X if (fld == NULL) return TRUE;
- X
- X for(i = 0; i < fld->len; i++)
- X {
- X if (fld->data[i] == 0) return TRUE;
- X if (!isspace(fld->data[i])) return FALSE;
- X }
- X return TRUE;
- X}
- X
- X/*******************************************************************************
- X *
- X * D R A W _ P I C T U R E
- X * -----------------------
- X *
- X * Description:
- X * Draw all literals and field in a picture.
- X *
- X ******************************************************************************/
- Xstatic int
- Xdraw_picture(struct picture *pic)
- X{
- X int n;
- X int r;
- X char space[100];
- X
- X
- X if (pic == NULL) return FAIL;
- X
- X memset(space, ' ', pic->width);
- X space[pic->width] = 0;
- X
- X for(r = pic->y; r < pic->y + pic->height; r++) {
- X move(r, pic->x);
- X addstr(space);
- X }
- X
- X for(n = 0; n < pic->n_literals; n++) {
- X move(pic->literal[n].pos.y + pic->y, pic->literal[n].pos.x + pic->x);
- X addstr(pic->literal[n].value);
- X }
- X
- X for(n = 0; n < pic->n_fields; n++) {
- X if (pic->field[n].lvalue) {
- X move(pic->field[n].pos.y + pic->y,
- X pic->field[n].pos.x - strlen(pic->field[n].lvalue) + pic->x);
- X addstr(pic->field[n].lvalue);
- X }
- X
- X if (pic->field[n].rvalue) {
- X move(pic->field[n].pos.y + pic->y,
- X pic->field[n].pos.x + pic->field[n].len + pic->x);
- X addstr(pic->field[n].rvalue);
- X }
- X move(pic->field[n].pos.y + pic->y, pic->field[n].pos.x + pic->x);
- X addstr(pic->field[n].data);
- X }
- X
- X message_nr(NULL);
- X
- X return OK;
- X}
- X
- X/******************************************************************************
- X *
- X * S I G C A T C H
- X * ---------------
- X * Description:
- X * Catches a signal, cleans up and exits.
- X *
- X *****************************************************************************/
- Xstatic int
- Xsigcatch(void)
- X{
- X cforms_end();
- X exit(1);
- X}
- END_OF_FILE
- if test 27545 -ne `wc -c <'src/cflib.c'`; then
- echo shar: \"'src/cflib.c'\" unpacked with wrong size!
- fi
- # end of 'src/cflib.c'
- fi
- echo shar: End of archive 3 \(of 3\).
- cp /dev/null ark3isdone
- MISSING=""
- for I in 1 2 3 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked all 3 archives.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-