//		  ÚÄÄÄÄÄÄÄ¿
//	ÄÄÄÄÄÄÄÄÄ>³ 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 1 - source code.
// This file defines the basic terms of "character geometry", to be used
// in the more specific other parts.
// for data-structure logic, refer to file "article".

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

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

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define  SNAV0_C
#include "/mtad/snav/snav0.hpp"

boolean env_modify=FALSE;		// governs the behaviour of some methods

/****************************** constants ************************************/

// predefined instances ("figurative constants")
weight_d	  // intended to be read only!
	twgwgt(0),
	h1v1(1,1),
	h1v2(1,2),
	h2v1(2,1),
	h2v2(2,2),
	fullwgt(3,3);
axis		  // intended to be read only!
	nodim(NODIM),
	hdim(HDIM),
	vdim(VDIM);
intersection	  // intended to be read only!
	nodirs(NODIR),
	hdirs(hdim.whereto()),
	vdirs(vdim.whereto()),
	alldirs((int)RTDIR|UPDIR|LTDIR|DNDIR);
direction	  // intended to be read only!
	nodir(NODIR),
	dir1st,
	rt(RTDIR),
	up(UPDIR),
	lt(LTDIR),
	dn(DNDIR);

/******************************** directions *********************************/

connective direction :: opposite()
{ // "opposite" direction. other direction on same axis

	intersection result((axis(*this)).directions());
	result.exclude(*this);
	return (where=(connective)result());
}

direction :: direction(char c)
{ // construct direction from letter code */

	where=DEF_DIR;
	c=toupper(c);
	for (; where; succ())
		if (code()==c) break;
}

char *direction :: name()
{
	static char result[6];
	switch (where) {
		case RTDIR : strcpy(result,"Right"); break;
		case UPDIR : strcpy(result,"Up"); break;
		case LTDIR : strcpy(result,"Left"); break;
		case DNDIR : strcpy(result,"Down"); break;
		default    : *result='\0'; break;
	}
	return result;
}

char direction :: code()
{ /* first letter of direction name */

	return(*name());
}

direction& direction :: operator!()
{ // non-destructive opposite()

	static direction result;
	result=*this;
	result.opposite();
	return(result); 					     // copied
}

int direction :: serial()
{
	int i=4, dirval=(int)MAX_DIR;
	for (; i; i--, dirval>>=1)
		if (dirval==where) return(i);
	return(0);
}

connective direction :: clockwise()
// circular movement clockwise
{ pred(); if (!where) where=MAX_DIR; return where; }

/**************************** direction sets ********************************/

intersection :: intersection(char *name)
{ // construct by name

	if (!strcmp(name+1,"arrow"))
		value=(!(direction(*name))).whereto();
	else if (!strcmp(name,"dash"))
		value=hdirs();
	else if (!strcmp(name,"bar"))
		value=vdirs();
	else if (!strcmp(name+2,"corner"))
		value=(!(direction(*name))).whereto()
		| (!(direction(*(name+1)))).whereto();
	else if (!strcmp(name+1,"fork")) {
		value=hdirs()|vdirs();
		exclude(!(direction(*name)));
	} else if (!strcmp(name,"cross"))
		value=hdirs()|vdirs();
	else value=NODIR;
}

boolean intersection :: includes(direction &member)
{ // test if direction is included

	if (value & member.where) {
		for (direction dir=dir1st; dir.where; dir++)
			if (dir==member && (value & dir.where))
				return(TRUE);
	}
	return(FALSE);
}

boolean intersection :: includes(intersection &subset)
{ // test if all target directions are included

	intersection subs=subset;
	for (direction dir=dir1st; dir.where; dir++)
		if (subs.includes(dir) && !includes(dir))
			return(FALSE);
	return(TRUE);
}

boolean intersection :: intersects(intersection &paralel)
// test if at least one of the target directions is included

	{ return(value & paralel.value); }

boolean intersection :: unary()
// test if set conains only one member
	{ return(type()==J_ARROW); }

char *intersection :: names()
{ /* decode names of directions in set */

	static char nm[20]; *nm='\0';
	int save=value;
	for (direction dir=dir1st; dir.where; dir++)
		if (includes(dir)) {
			strcat(nm,dir.name());
			exclude(dir);
			if (value) strcat(nm,",");
		}
	value=save;
	return(nm);
}

intersection_type intersection :: type()
{
	int numdir=0;
	for (direction dir=dir1st; dir(); dir++)
		if (includes(dir)) numdir++;
	switch (numdir) {
		case 1 : return(J_ARROW);
		case 2 : if (value==hdirs() || value==vdirs()) return J_LINE;
			 else return(J_CORNER);
		case 3 : return(J_FORK);
		case 4 : return(J_CROSS);
		default: return(J_NUL);
	}
}

char *intersection :: name()
{ // decode names of intersection value

	static char nm[9]; *nm='\0';
	char	*s=nm;
	direction dir;

	switch (type()) {
		case J_ARROW :	dir.set((connective)value); dir.opposite();
				*nm=dir.code();
				strcpy(nm+1,"arrow");
				break;
		case J_LINE  :	if (value==hdirs()) strcpy(nm,"dash");
				else strcpy(nm,"bar");
				break;
		case J_CORNER:	for (direction dir=dir1st; dir(); dir++)
					if (includes(!dir))
						*(s++)=(dir).code();
				strcpy(s,"corner");
				break;
		case J_FORK  :	if (includes(hdirs))
					dir.set((connective)(value-hdirs()));
				else dir.set((connective)(value-vdirs()));
				*nm=dir.code(); strcpy(nm+1,"fork");
				break;
		case J_CROSS :	strcpy(nm,"cross"); break;
		default      :	strcpy(nm,"nowhere");
	}
	return(nm);
}

direction& intersection :: ask_first()
{ // find first member

	static direction dir;
	for (dir=dir1st; dir.where; dir++)
		if (includes(dir)) return(dir);
	return(dir);						     // copied
}

direction& intersection :: get_first()
{ // extract first member

	static direction dir;
	for (dir=dir1st; dir.where; dir++)
		if (includes(dir))
			{ exclude(dir); return(dir); }

	return(dir);						     // copied
}

/****************************** dimensions of the plane **********************/

void axis :: set_dirs(biconnective newdim)
{ // default empty

	if (newdim==HDIM)
		{ where=(biconnective)newdim; value=RTDIR|LTDIR; }
	else if (newdim==VDIM)
		{ where=(biconnective)newdim; value=DNDIR|UPDIR; }
	else { where=NODIM; value=NODIR; }
}

axis :: axis(direction &dir)
{ // generalize direction

	direction dr=dir;
	if (dr==rt || dr==lt)
		{ where=HDIM; value=RTDIR|LTDIR; }
	else if (dr==dn || dr==up)
		{ where=VDIM; value=DNDIR|UPDIR; }
	else {	where=NODIM; value=NODIR; }
}

biconnective axis :: opposite()
{ // turn the other dimension

	if (where==HDIM)
		{ where=VDIM; value=DNDIR|UPDIR; }
	else if (where==VDIM)
		{ where=HDIM; value=RTDIR|LTDIR; }
	return where;
}

axis& axis :: operator!()
{ // non-destructive opposite()


	static axis result;
	result=*this;
	result.opposite();
	return result;					     // copied
}

intersection& axis :: directions()
// copy pending direction set
{
	static intersection result;
	result=intersection(value);
	return result;
}

char *axis :: name()
{
	static char result[11];
	switch (where) {
		case HDIM : strcpy(result,"Horizontal"); break;
		case VDIM : strcpy(result,"Vertical"); break;
		default   : *result='\0'; break;
	}
	return result;
}

char axis :: code()
// first letter of direction name
	{ return(*name()); }

weight_d :: weight_d(int hwidth, int vwidth)
{
	wgt[HDIM].width=hwidth; wgt[HDIM].set_axis(HDIM);
	wgt[VDIM].width=vwidth; wgt[VDIM].set_axis(VDIM);
	// bitwise operation assumes 2 dimensions
	value=hwidth|(vwidth<<2);
}

int weight_d :: replace(axis &dim, int width)
{ // replace width on one axis

	wgt[dim()].width=width;
	// bitwise operation assumes 2 dimensions
	return (value=wgt[HDIM].width|(wgt[VDIM].width<<2));
}

void weight_d :: set(int weight_combi)
{
	value=weight_combi;
	// bitwise operation assumes 2 dimensions
	wgt[NODIM].width=0; wgt[NODIM].set_axis(NODIM);
	wgt[HDIM].width=value & 0x0003; wgt[HDIM].set_axis(HDIM);
	wgt[VDIM].width=value>>2; wgt[VDIM].set_axis(VDIM);
}

weight_d :: weight_d(weight_d &other)
{
	value=other.value;
	wgt[NODIM].width=0; wgt[NODIM].set_axis(NODIM);
	int h=other.wgt[HDIM].width, v=other.wgt[VDIM].width;
	wgt[HDIM].width=h; wgt[HDIM].set_axis(HDIM);
	wgt[VDIM].width=v; wgt[VDIM].set_axis(VDIM);
}

weight_d & weight_d :: mask(weight_d &other)
{
	if (wgt[HDIM].width==0) wgt[HDIM].width=other.y();
	if (wgt[VDIM].width==0) wgt[VDIM].width=other.x();
	return *this;
}

