//		  ÚÄÄÄÄÄÄÄ¿
//	ÄÄÄÄÄÄÄÄÄ>³ AVNER ³
//	ÄÄÄÄÄÄÄÄÄ>³  BEN  ³ÄÄÄÄÄÄ> Software Engineering Method
//		  ÀÄÄÄÄÄÄÄÙ
//	10 Dov-Hoz st. Tel-Aviv 63416 Israel tel. 972-3-221535

// The Screen NAVigator, ver 1.20 August 1990
// Copyright (c) 1990 by Avner Ben
// Snav is not, and never was, free software.
// for conditions for use refer to file "copyrigh.txt"

// The Screen Navigator is an object-oriented device-independent
// character-graphics driver package, written in the C++ language,
// distributed in the form of C++ source code.
// For further information refer to the documentation files.

// The user may not omit this text, from the beginning of the file, to
// the line of asterisks below.

/***************************************************************************/

// package nucleus part 3 - source code.
// This file defines the generic "panel" (formatted output medium).

// History:
// 25.10.89 avner ben coded.
// 11.9.90   avner ben - last update.
// for further details refer to documentation.

// Site history (of this copy):
// __.__.__ ____________ : __________________.

#ifndef SNAV2_C
#define SNAV2_C
#endif

#include <stdio.h>
#include "/mtad/snav/snav0.hpp"
#include "/mtad/snav/snav1.hpp"
#include "/mtad/snav/snav2.hpp"
#include "/mtad/snav/snav3.hpp"

///////////////////// window dimensions:

point_pos :: point_pos(int y, int x, direction &dir)
{ ypos=y; xpos=x; chg_dir(dir,1); }

point_pos :: point_pos(direction &dir, point_pos &other)
{ ypos=other.ypos; xpos=other.xpos; chg_dir(dir,1); }

point_pos & point_pos :: operator+=(point_pos &offset)
// shift by user-specified offset
{ ypos+=offset.ypos; xpos+=offset.xpos; return(*this); }

point_pos & point_pos :: operator-=(point_pos &shifted)
// shift conter to user-specified offset
{ ypos-=shifted.ypos; xpos-=shifted.xpos; return(*this); }

int point_pos :: how_far(direction &dir)
{ // only positive result counts

	int result=0;
	switch (dir()) {
		case RTDIR :	if (xpos>0) result=xpos; break;
		case UPDIR :	if (ypos<0) result=-ypos; break;
		case LTDIR :	if (xpos<0) result=-xpos; break;
		case DNDIR :	if (ypos>0) result=ypos; break;
	}
	return result;
}

void point_pos :: chg_dir(direction &dir, int len)
{
	switch (dir()) {
		case RTDIR :	xofst=len; yofst=0; break;
		case UPDIR :	xofst=0; yofst=-len; break;
		case LTDIR :	xofst=-len; yofst=0; break;
		case DNDIR :	xofst=0; yofst=len; break;
	}
}

char * point_pos :: name()
{
	static char result[12];
	sprintf(result,"%-d:",ypos);
	sprintf(result+strlen(result),"%-d",xpos);
	return result;
}

void point_pos :: move(direction &dir, int len)
{
	switch (dir()) {
		case RTDIR :	xpos+=len; break;
		case UPDIR :	ypos-=len; break;
		case LTDIR :	xpos-=len; break;
		case DNDIR :	ypos+=len; break;
	}
}

void square_pos :: move_limit(direction &dir, int ofst)
{
	switch (dir()) {
		case RTDIR :	bot.xpos+=ofst; break;
		case UPDIR :	top.ypos-=ofst; break;
		case LTDIR :	top.xpos-=ofst; break;
		case DNDIR :	bot.ypos+=ofst; break;
	}
}

void square_pos :: set_limit(direction &dir, int pos)
{ // modify window dimensions

	switch(dir()) {
		case RTDIR :	bot.xpos=pos; break;
		case UPDIR :	top.ypos=pos; break;
		case LTDIR :	top.xpos=pos; break;
		case DNDIR :	bot.ypos=pos; break;
	}
}

void square_pos :: set_start(point_pos &start)
{
	top=start;
	curlen=bot.ypos-top.ypos+1;
	curwd=bot.xpos-top.xpos+1;
}

void square_pos :: set_end(point_pos &end)
{
	bot=end;
	curlen=bot.ypos-top.ypos+1;
	curwd=bot.xpos-top.xpos+1;
}

void square_pos :: set_end_y(int len)
{
	bot.sety(top.ypos+len-1);
	curlen=len;
}

void square_pos :: set_end_x(int wd)
{
	bot.setx(top.xpos+wd-1);
	curwd=wd;
}

void square_pos :: move(direction &dir, int len)
{
	top.move(dir,len); bot.move(dir,len);
}

int square_pos :: len(axis &dim)
{
	if (dim==vdim) return curlen; else return(curwd);
}

char * square_pos :: name()
{
	static char result[24];
	sprintf(result,"%sx",top.name());
	sprintf(result+strlen(result),"%s",bot.name());
	return result;
}

void color_ind :: toggle_attr(vd_attr at)
{ if (ask_attr(at)) attr_off(at); else attr_on(at); }

void color_ind :: set_for_clr(vd_clr color)
{ if (attr==VD_REV) backgnd=color; else forgnd=color; }

void color_ind :: set_back_clr(vd_clr color)
{ if (attr==VD_REV) forgnd=color; else backgnd=color; }

vd_clr color_ind :: ask_for_clr()
{ if (attr==VD_REV) return backgnd; else return forgnd; }

vd_clr color_ind :: ask_back_clr()
{ if (attr==VD_REV) return forgnd; else return backgnd; }

void color_ind :: normalize()
{
	if (attr==VD_REV) {
		vd_clr t=forgnd;
		forgnd=backgnd; backgnd=t;
		attr_off(VD_REV);
	}
}

boolean color_ind :: operator()()
{ return attr && (int)backgnd && (int)forgnd; }

boolean color_ind :: operator==(color_ind &other)
{ return (attr==other.attr && backgnd==other.backgnd && forgnd==other.forgnd); }

boolean color_ind :: operator!=(color_ind &other)
{ return (attr!=other.attr || backgnd!=other.backgnd || forgnd!=other.forgnd); }

/////////////////////////// the generic panel

panel *master_screen=NULL;
 // allocate this, to force all consequent panels to area defined by this
static point_pos top_pos(1,1), bot_pos(25,80);

panel :: panel(square_pos *window, color_ind *clr, direction *dir, boolean ingraph, boolean inwrap)
 : win(top_pos,bot_pos)
{
	lang=frame_level=save_level=0;
	if (window) win=*window;
	fix_limits();
	if (clr) color=def_color=*clr;
	if (dir) curdir=*dir;			    // default direction right
	takes_graph=ingraph; wrap_around=inwrap;
	fix();
}

void panel :: fix()
{
	top=win.start();
	bot=win.end();
	hlen=bot.xpos-top.xpos+1;
	vlen=bot.ypos-top.ypos+1;
}

boolean panel :: ask_legal(point_pos *pt)
{
	return(
		pt->xpos>=top.xpos
		 && pt->xpos<=bot.xpos
		 && pt->ypos>=top.ypos
		 && pt->ypos<=bot.ypos
	);
}

boolean panel :: move(direction *dir, point_pos *pt0)
{ // next cursor position, if movement possible. wrap-around not practiced

	if (!dir) dir=&curdir;
	point_pos *pt=(pt0? pt0 : &cursor);
	switch ((*dir)()) {
		case RTDIR :	if (pt->xpos>=bot.xpos) return FALSE;
				pt->move(rt); break;
		case UPDIR :	if (pt->ypos<=top.ypos) return FALSE;
				pt->move(up); break;
		case LTDIR :	if (pt->xpos<=top.xpos) return FALSE;
				pt->move(lt); break;
		case DNDIR :	if (pt->ypos>=bot.ypos) return FALSE;
				pt->move(dn); break;
	}
	if (!pt0) posit();
	return TRUE;
}

boolean panel :: wpmove(direction *dir, point_pos *pt0)
{ // next cursor position. wrap-around is practiced if allowed by panel's setup
///////////////// experimental code! //////////////////////////

	if (!dir) dir=&curdir;
	point_pos *pt=(pt0? pt0 : &cursor);
	connective dirval=(*dir)();
	if (dirval==RTDIR) {
		if (pt->xpos<bot.xpos) pt->move(rt);
		else if (wrap_around) {
			pt->xpos=top.xpos;
			pt->move(dn);
			if (pt->ypos>bot.ypos) pt->ypos=top.ypos;
		} else return FALSE;
	} else if (dirval==UPDIR) {
		if (pt->ypos>top.ypos) pt->move(up);
		else if (wrap_around) {
			pt->ypos=bot.ypos;
			pt->move(lt);
			if (pt->xpos<top.xpos) pt->xpos=bot.xpos;
		} else return FALSE;
	} else if (dirval==LTDIR) {
		if (pt->xpos>top.xpos) pt->move(lt);
		else if (wrap_around) {
			pt->setx(bot.xpos);
			pt->move(dn);
			if (pt->ypos>bot.ypos) pt->ypos=top.ypos;
		} else return FALSE;
	} else if (dirval==DNDIR) {
		if (pt->ypos<bot.ypos) pt->move(dn);
		else if (wrap_around) {
			pt->ypos=top.ypos;
			pt->move(rt);
			if (pt->xpos>bot.xpos) pt->xpos=top.xpos;
		} else return FALSE;
	}
	if (!pt0) posit();
	return TRUE;
}

char panel :: succ_c(direction *dir, point_pos *pt0)
{ // get neighbour of cursor/specified char

	if (!dir) dir=&curdir;
	point_pos pt; if (pt0) pt=*pt0; else pt=cursor;
	if (!move(dir,&pt)) return ' ';       // no wrap-around
	return get_c(&pt);
}

char * panel :: env(point_pos *pt)
{ // returns string made of current char, followed by sorounding chars

	envbuf[0]=get_c(pt);
	for (direction dir; dir(); dir++)
		envbuf[dir.serial()]=succ_c(&dir,pt);	   // no wrap-around
	envbuf[5]='\0';
	return(envbuf);
}

void panel :: home()
{
	switch (curdir()) {
		case RTDIR :	// going right and down (normal latin)
				cursor.ypos=top.ypos;
				cursor.xpos=top.xpos;
				break;
///////////////// experimental code! //////////////////////////
		case UPDIR :	// going up and left (whatever that means)
				cursor.ypos=bot.ypos;
				cursor.xpos=bot.xpos;
				break;
		case LTDIR :	// going left and down (semitic languages)
				cursor.ypos=top.ypos;
				cursor.xpos=bot.xpos;
				break;
		case DNDIR :	// going down and right (asian languages?)
				cursor.ypos=top.ypos;
				cursor.xpos=top.xpos;
				break;
	}
	posit();
}

void panel :: clr_eol(point_pos *pt0)
{ // erase to end of line

	point_pos pt;
	if (pt0) pt=*pt0; else pt=cursor;
	if (!ask_legal(&pt)) return;
	color_ind sav=color; reset_color();
	for (; pt.xpos<=bot.xpos; pt.move(rt))
		put_c(' ',&pt);
	color=sav;
}

void panel :: clear()
{ // clear whole screen and position home

	int maxy=bot.ypos;
	reset_color();
	point_pos pt(dn,top);
	for (; pt.ypos<=maxy; pt++)
		clr_eol(&pt);
	home();
}

void panel :: put_s(char *s, point_pos *pt0, direction *dir0)
{ // write a string on screen

	point_pos *pt=NULL; if (pt0) pt=new point_pos(*pt0);
	direction *dir=NULL; if (dir0) dir=new direction(*dir0);
	for (; *s; s++) {
		put_c(*s,pt,dir);
		// put_c advances panel->cursor (if pt==NULL). otherwise...
		if (pt)
			if (!move(dir,pt)) return; // no wrap-around!
	}
	if (pt) delete pt; if (dir) delete dir;
}

int panel :: ask_limit(direction &dir)
{ // beginning/end of window - height/breadth

	switch(dir()) {
		case RTDIR :	return bot.xpos;
		case UPDIR :	return top.ypos;
		case LTDIR :	return top.xpos;
		case DNDIR :	return bot.ypos;
	}
	return 0;
}

void panel :: set_limit(direction &dir, int pos)
{ // modify window dimensions

	win.set_limit(dir,pos);
	fix_limits();
	fix();
}

void panel :: fix_limits()
{
	if (master_screen) {
		if (win.sty()<master_screen->ask_limit(up))
			win.set_limit(up,master_screen->ask_limit(up));
		if (win.stx()<master_screen->ask_limit(lt))
			win.set_limit(lt,master_screen->ask_limit(lt));
		if (win.eny()>master_screen->ask_limit(dn))
			win.set_limit(dn,master_screen->ask_limit(dn));
		if (win.enx()>master_screen->ask_limit(rt))
			win.set_limit(rt,master_screen->ask_limit(rt));
	}
}

void panel :: move_limit(direction &dir, int ofst)
{
	win.move_limit(dir,ofst);
	fix_limits();
	fix();
}

int panel :: ask_len(axis &dim)
{
	if (dim==hdim) return hlen;
	if (dim==vdim) return vlen;
	return 0;
}

point_pos& panel :: ask_corner(direction &dir)
{ // only top-left or bottom-right

	if (dir()==UPDIR || dir()==LTDIR)  tmpt1=top;
	else tmpt1=bot;
	return tmpt1;
}

void panel :: set_color(color_ind &clr)
{ color=clr; fix(); }

void panel :: toggle_attr(vd_attr at)
{ color.toggle_attr(at); fix(); }

void panel :: set_def_attr(vd_attr at)
{
	def_color.toggle_attr(at);
	set_color(def_color);
}

void panel :: set_def_background(vd_clr colornum)
{
	def_color.backgnd=colornum;
	set_color(def_color);
	paint_background(colornum);
}

void panel :: paint_background(vd_clr colornum)
{
	int maxy=bot.ypos, maxx=bot.xpos;
	for (point_pos ypt(dn,top); ypt.ypos<=maxy; ypt++) {
		point_pos xpt(rt,ypt);
		for (; xpt.xpos<=maxx; xpt++)
			put_background(colornum,&xpt);
	}
}

void panel :: set_def_forground(vd_clr colornum)
{
	def_color.forgnd=colornum;
	set_color(def_color);
}

boolean panel :: enframe(weight_d &wgt, char *intitle, boolean centered)
{ // enclose window in box, and contract

	if (hlen<3 || vlen<3) return FALSE;
	frame_level++;
	if (!intitle) oblong(hlen,vlen,wgt).list(this,&top);
	else {
		direction align;
		if (centered) align=nodir;
		else align=!curdir;
		display_box(intitle,point_pos(vlen,hlen),wgt,curdir,align).list(this,&top);
	}
	// contract
	move_limit(lt,-1);
	move_limit(rt,-1);
	move_limit(up,-1);
	move_limit(dn,-1);
	home();
	return TRUE;
}

boolean panel :: deframe()
{
	if (!frame_level) return frame_level;
	if (save_level==frame_level) restore();
	frame_level--;
	move_limit(lt);
	move_limit(rt);
	move_limit(up);
	move_limit(dn);
	(oblong(hlen,vlen)).clear(this,&top);
	return frame_level;
}

void panel :: cr()
{ // carriage return

	switch(curdir()) {
		case RTDIR :	posit(&point_pos(cursor.y(),ask_limit(LTDIR))); break;
		case UPDIR :	posit(&point_pos(ask_limit(DNDIR),cursor.x())); break;
		case LTDIR :	posit(&point_pos(cursor.y(),ask_limit(RTDIR))); break;
		case DNDIR :	posit(&point_pos(ask_limit(UPDIR),cursor.x())); break;
	}
}

boolean panel :: twg_c(point_pos *pt)
{ // translate typewriter graphic char to 1h1v semigraphic
  // ("full screen" algorithm)

	char *around=env(pt); char c=*around;
	intersection avdir=alph->cdir(c);
	if (!avdir() || (alph->cweight(c))()) return FALSE; // not a twg char
	// ajust to actual connectivity
	intersection actdir=alph->cdir(c);
	char csemi=alph->translate(c,h1v1);		// semigraphic self
	char ctwg=alph->translate(csemi,twgwgt);	     // semigraphic self
	for (direction dir; dir(); dir++)
		if (actdir>=dir
		 && !alph->ccont(ctwg,around[dir.serial()],dir) // connects as is?
		 && !alph->ccont(csemi,around[dir.serial()],dir)) // connects edited?
					actdir-=dir;	 // no partner in direction
	if (!actdir()) return FALSE;   // lonely char - no arc
	if (actdir.unary()) {	       // arc terminator
		if (avdir.unary()) {   // arrow
			if (ask_grph()) put_c(alph->translate(c,h1v1),pt);
		} else {		// not arrow
			// does arc continue from partner?
			direction opsdir(actdir()); opsdir.opposite();
			point_pos ngbr=*pt;
			if (!move(&direction(actdir()),&ngbr)) return FALSE;
			around=env(&ngbr); char c1=*around;
			char c1semi=alph->translate(c1,h1v1);
			char c1twg=alph->translate(c1semi,twgwgt);
			intersection actdir1=alph->cdir(c1);
			for (direction dir; dir(); dir++)
				if (actdir1>=dir && dir!=opsdir
				 && (alph->ccont(c1twg,around[dir.serial()],dir)
				 || alph->ccont(c1semi,
				  around[dir.serial()],dir))) {
					if (avdir.type()==J_CROSS)
						csemi=alph->ctrim(csemi,opsdir);
					put_c(csemi,pt);
					return TRUE; // more than two in arc
				}
			if ((alph->cdir(c1)).unary()) {   // other is arrow
				if (avdir.type()==J_CROSS)
					csemi=alph->ctrim(csemi,opsdir);
				put_c(csemi,pt);
			}
		}
	} else put_c(alph->get_c(actdir,h1v1),pt); // middle of arc
	return TRUE;
}

int panel :: twg()
{ // translate typewriter graphics to 1h1v semigraphics

	int changes=0;
	point_pos pt(rt,top);
	for (; pt.ypos<=bot.ypos; pt.move(dn))
		for (pt.setx(top.xpos); pt.xpos<=bot.xpos; pt++)
			changes+=twg_c(&pt);
	return changes;
}

void panel :: arclist(point_pos *pt0, int len, direction &dir,
 weight_d &wgt, boolean tipped, color_ind *color)
{ // draw straight line graphic primitive. interface for arc::list()

	point_pos pt(dir,*pt0);
	color_ind savcolor=ask_color();
	if (color) {					    // forground only!
		color_ind newclr=ask_color();
		newclr.set_for_clr(color->forgnd);
		set_color(newclr);
	}
	for (int i=1; i<=len; ++i) {
#ifndef ENV_MODIFY				     // draw current char only
		put_c(alph->carrw(env(&pt),dir,wgt,len,i,tipped),&pt);
#else								// trim excess
		char around[6]; strcpy(around,env(&pt));
		alph->cearrw(around,dir,wgt,len,i,tipped);
		put_c(*around,&pt);
		for (direction tdir; tdir(); tdir++)
			if (around[tdir.serial()]!=' ') {
				point_pos ngbr=pt;
				if (move(&tdir,&ngbr)) {
					color_ind tclr=get_color(&ngbr);
					put_c(around[tdir.serial()],&ngbr);
					put_color(tclr,&ngbr);
				}
			}
#endif
		pt++;				    // legality of pos ignored
	}
	set_color(savcolor);
}

void panel :: arcclr(point_pos *pt0, int len, direction &dir)
{ // clear space formerly occupied by straight line

	point_pos pt(dir,*pt0);
	color_ind savcolor=ask_color(); reset_color();
	for (int i=1; i<=len; ++i) {
		put_c(' ',&pt);
		pt++;				    // legality of pos ignored
	}
	set_color(savcolor);
}

intersection &panel :: gowhere(point_pos *junction)
{ // directions of arcs available from specified point on screen

	tmpint=intersection((int)NODIR);
	char around[6]; strcpy(around,env(junction));
	for (direction dir; dir(); dir++)
		if (alph->ccont(*around,*(around+dir.serial()),dir))
			tmpint+=dir;
	return(tmpint);
}

square_pos& panel :: ask_window(void)
{
	static point_pos pt;
	static square_pos result(pt,pt);
	result=win;
	return result;
}

color_ind& panel :: ask_color(void)
{
	tmpclr2=color;
	return tmpclr2;
}

color_ind& panel :: ask_def_color(void)
{
	tmpclr3=def_color;
	return tmpclr3;
}

boolean panel :: ask_attr(vd_attr at)
{ return color.ask_attr(at); }

void panel :: reset_color(void)
{ set_color(def_color); }

void panel :: set_def_color(color_ind &clr)
{ def_color=clr; set_color(clr); set_def_background(clr.backgnd); }

boolean panel :: ask_grph(void)
{ return takes_graph; }

boolean panel :: ask_wrap(void)
{ return wrap_around; }

void panel :: set_dir(direction &dir)
{ curdir=dir; }

void panel :: turn(void)
{ curdir.clockwise(); }

int panel :: ask_lang(void)
{ return lang; }

void panel :: set_lang(int inlang)
{ lang=inlang; }

direction& panel :: ask_dir(void)
{
	static direction result;
	result=curdir;
	return result;
}

point_pos& panel :: ask_where(void)
{
	tmpt2=cursor;
	return tmpt2;
}

////////////////////////// screen type selection /////////////////////

screen_driver_manager :: screen_driver_manager(int def)
{ default_screen=def; }

int screen_driver_manager :: ask_default(void)
{ return default_screen; }

void screen_driver_manager :: set_default(int def)
{ default_screen=def; }

