/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred."
 *
 */
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

/*
 * Copyright  (c) 1985 AT&T
 *	All Rights Reserved
 */
#ident	"$Header: initfield.c 1.2 89/10/10 $@(#)vm.qued:src/qued/initfield.c	1.1"

#include <stdio.h>
#include "wish.h"
#include "terror.h"
#include "token.h"
#include "vtdefs.h"
#include "winp.h"
#include "fmacs.h"
#include "ctl.h"
#include "attrs.h"

#define FSIZE(x)	(x->rows * (x->cols + 1))

ifield *Cfld = NULL;

ifield *
deffield()
{
	int rows, cols, currow, curcol;
	ifield *newfield();

	/*
	 * Compute default values
	 */
	vt_ctl(VT_UNDEFINED, CTGETSIZ, &rows, &cols);
	vt_ctl(VT_UNDEFINED, CTGETPOS, &currow, &curcol);
	return(newfield(currow, curcol, rows, cols - curcol, 0));
}

ifield *
newfield(firstrow, firstcol, rows, cols, flags)
int firstrow;
int firstcol;
int rows;
int cols;
int flags;
{
	ifield *newfld;

	newfld = new(ifield);
	newfld->frow = firstrow;
	newfld->fcol = firstcol;
	newfld->rows = rows;
	newfld->cols = cols;
	newfld->flags = flags;
	newfld->currow = 0;
	newfld->curcol = 0;
	newfld->scrollbuf = NULL;
	newfld->buffsize = 0;
	newfld->buffoffset = 0;
	newfld->bufflast = 0;
	if (newfld->rows == 1) {
		newfld->currtype = SINGLE;
		if (newfld->flags & I_SCROLL) {
			newfld->flags &= ~(I_WRAP);
			newfld->cols -= 1;
		}
	}
	else {
		newfld->currtype = MULTI;
		newfld->flags &= ~(I_BLANK);
	}
	if (newfld->flags & I_INVISIBLE) {
		/*
		 * bit of a kludge to handle no-echo ... rather
		 * than putting characters into the window map,
		 * these characters are put DIRECTLY into the
		 * field value string and are NOT echoed to the
		 * screen.
		 */
		if ((newfld->value = (char *) malloc(FSIZE(newfld))) == NULL)
			fatal(NOMEM, "");
		newfld->valptr = newfld->value;
	}
	else {
		newfld->value = NULL;
		newfld->valptr = NULL;
	}
	newfld->fieldattr = (newfld->flags & I_FILL ? ATTR_UNDERLINE: ATTR_NORMAL);
	return(newfld);
}

gotofield(fld, row, col)
ifield *fld;
int row;
int col;
{
	if (fld != NULL)
		Cfld = fld;
	else if (!Cfld)
		return;
	if (row < 0 || col < 0)
		fgo(Cfld->currow, Cfld->curcol);
	else
		fgo(row, col);
	setarrows();
}

endfield(fld)
ifield *fld;
{
	if (fld == NULL)
		fld = Cfld;
	if (fld) {
		if (fld->value != NULL) {
			free(fld->value);
			fld->value = fld->valptr = NULL;
		}
		if (fld->scrollbuf != NULL) {
			free(fld->scrollbuf);
			fld->scrollbuf = NULL;
		}
		free(fld);
	}
	if (fld == Cfld)
		Cfld = NULL;	/* terminating current field */
}
