/*
 ----------------------------------------------------------------
 |    Perlin Production Corporation, February-April 1993 (c).	|##
 |   WINDOW PACKAGE Source Code for SuperSoft C 1.2.3 compiler.	|##
 |    V e r s i o n  3.1 , part 5.  LAST CHANGES: 08.05.1993.	|##
 ----------------------------------------------------------------##
  #################################################################

			LIST OF FUNCTIONS:
	set_wn
	cl_wn
	draw_brd
*/

#include "stdio.h"
#include "conio.h"
#include "wios.h"
#include "stddef.h"

char set_wn(wn)
WINDOW *wn;
{
	char i,j;

	if (wn->xs<0 || wn->ys<0 || wn->xe<=wn->xs || wn->ye<=wn->ys ||
	    wn->xe>MAXX || wn->ye>MAXY) return (werrno=WECOOSC);
	cl_wn(wn);	/* Clear window */
	draw_brd(wn);	/* Draw border  */
	j=wn->ye+1;	/* Fill shadow  */
	if ( (wn->shadow != NO_SHADOW) && (wn->xe < MAXX) && (j <= MAXY) )
	{
		wrt_esc(wn->bgrnd);
		i=wn->xs+1;
		goto_xy(i,j);
		for ( ; i <= wn->xe+1 ; i++ ) putchar (wn->shadow);
		for (i-- ; j > wn->ys ; j-- )
		{
			goto_xy(i,j);
			putchar(wn->shadow);
		}
		wrt_esc(NORM_VID);
		wn->col=wn->row=0;
		goto_xy(wn->t_xs,wn->t_ys);
		return (werrno=WENOERR);
	}
	wn->col=wn->row=0;
	goto_xy(wn->t_xs,wn->t_ys);
	return (werrno=WESHAD);
}


cl_wn(wn)
WINDOW *wn;
{
	register char i;
	char winame[MAXX];

	wrt_esc(wn->bgrnd);
	for ( i=0 ; i < wn->xe-wn->xs-1 ; i++ ) winame[i]=wn->fill;
	winame [i] = NULL;
	for (i=wn->ys+1; i<wn->ye ; i++)
	{
		goto_xy(wn->xs+1,i);
		puts(winame);
	}
	wrt_esc(NORM_VID);
	wn->col=wn->row=0;
	goto_xy(wn->t_xs,wn->t_ys);
}


static draw_brd(wn)
WINDOW *wn;
{
	register char i;
	char xst,yst,xen,yen,j,hsym,vsym,uls,urs,drs,dls,*wn_nam;

	xst=wn->xs;
	yst=wn->ys;
	xen=wn->xe;
	yen=wn->ye;
	if (wn->brd != NULL)
	{
		hsym=wn->brd->hor;
		vsym=wn->brd->vert;
		uls=wn->brd->ul;
		urs=wn->brd->ur;
		drs=wn->brd->dr;
		dls=wn->brd->dl;
	}
	else hsym=vsym=uls=urs=drs=dls=BLANK;
	wrt_esc(LATIN);
	wrt_esc(wn->bdr_att);
	j=xst+1;
	goto_xy(j,yst);
	for (i=j;i<xen;i++) putchar(hsym);
	goto_xy(j,yen);
	for (i=j;i<xen;i++) putchar(hsym);
	for (i=yst+1; i<yen; i++)
	{
		goto_xy(xst,i); putchar(vsym);
		goto_xy(xen,i); putchar(vsym);
	}
	goto_xy(xst,yst); putchar(uls);
	goto_xy(xst,yen); putchar(dls);
	goto_xy(xen,yst); putchar(urs);
	goto_xy(xen,yen); putchar(drs);
	if ((wn_nam=wn->name) != NULL)
	{
		goto_xy(xst+2,yst);
		for(i=wn->xe-wn->xs-3 ; *wn_nam && i ; i--) putchar(*wn_nam++);
	}
	wrt_esc(NORM_VID);
}

