//		  ÚÄÄÄÄÄÄÄ¿
//	ÄÄÄÄÄÄÄÄÄ>³ 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 - headers.
// 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_H
#define SNAV2_H

#include <string.h>
#ifndef SNAV1_H
#include "/mtad/snav/snav1.hpp"
#endif
#ifndef NULL
#include <stdlib.h>
#endif

class point_pos {
// position on bi-dimensional panel

	friend class square_pos;
	friend class panel;

	private :
		int ypos, xpos, 				    //position
		    yofst, xofst;	    // ofst for the increment operator

	public :
		point_pos(int y=1, int x=1)
		 { ypos=y; xpos=x; yofst=0; xofst=1; }
		point_pos(int y, int x, direction &dir);
		point_pos(point_pos &other)
		 {	 ypos=other.ypos; xpos=other.xpos;
			 yofst=other.yofst; xofst=other.xofst; }
		point_pos &operator=(point_pos &other)
		 {	 ypos=other.ypos; xpos=other.xpos;
			 yofst=other.yofst; xofst=other.xofst;
			 return *this; }
		point_pos(direction &dir, point_pos &other);
		void move(direction &dir, int len=1);
		 // increment/decrement y/x positions as implied by direction
		void chg_dir(direction &dir, int increment=1);
		 // modify internal offset for ++ operator
		void chg_dir(int yoffset, int xoffset)
		 // modify internal offset for ++ operator
		 { yofst=yoffset; xofst=xoffset; }
		boolean operator==(point_pos &other)
		 { return(ypos==other.ypos && xpos==other.xpos); }
		boolean operator!=(point_pos &other)
		  { return(ypos!=other.ypos || xpos!=other.xpos); }
		int y(void)  { return(ypos); }
		int x(void)  { return(xpos); }
		void sety(int y)  { ypos=y; }
		void setx(int x)  { xpos=x; }
		point_pos &operator+=(point_pos &offset);
		 // shift by user-specified offset
		point_pos &operator-=(point_pos &shifted);
		 // shift conter to user-specified offset
		point_pos &operator++(void)
		 // shift by internally-held offset
		 { ypos+=yofst; xpos+=xofst; return(*this); }
		point_pos &operator--(void)
		 // shift counter to internally-held offset
		 { ypos-=yofst; xpos-=xofst; return(*this); }
		char *name(void);
		int how_far(direction &dir);
};

class square_pos
 { // position measured by pair of extreme corners.

	private :
		point_pos top,		 // up-left corner
		      bot;		 // right-down corner
		int curlen, curwd;	 // current dimensions
		point_pos tmpt; 	 // temp store for ret by ref

	public :
		square_pos(point_pos &start, point_pos &end)  {
			top=start; bot=end;
			curlen=bot.ypos-top.ypos+1;
			curwd=bot.xpos-top.xpos+1;
		}
		void set_start(point_pos &start);
		void set_end(point_pos &end);
		void set_end_y(int len);
		void set_end_x(int wd);
		void move_limit(direction &dir, int ofst=1);
		void set_limit(direction &dir, int pos);
		void move(direction &dir, int len=1);
		int len(axis &dim);
		point_pos &start(void)
		 { tmpt=top; return tmpt; } // (temporary result)
		point_pos &end(void)
		 { tmpt=bot; return tmpt; } // (temporary result)
		int sty(void)  { return top.ypos; }
		int eny(void)  { return bot.ypos; }
		int stx(void)  { return top.xpos; }
		int enx(void)  { return bot.xpos; }
		char *name(void);
};

enum vd_attr  {
	VD_OFF=0,				      //  "void" - leave alone
	VD_HI=1,			      // to be ORed int an integer-set
	VD_UNDLN=2,
	VD_BLNK=4,
	VD_REV=8,
	VD_HID=16
};

enum vd_clr  { // pc oriented ordering
	VD_BLK,
	VD_BLU,
	VD_GRN,
	VD_CYN,
	VD_RED,
	VD_PNK,
	VD_YLW,
	VD_WHT
};

struct color_ind
 { // visual attribute configuartion - ansi style

	int attr;						 // ORed combi
	vd_clr backgnd, forgnd;
	color_ind(int attrs=0, vd_clr for_color=VD_WHT, vd_clr back_color=VD_BLK)
	 { attr=attrs; backgnd=back_color; forgnd=for_color; }
	void set_attr(int attrs)  { attr=attrs; }
	void attr_on(vd_attr at)  { attr|=at; }
	void attr_off(vd_attr at)  { attr^=(attr&at); }
	boolean ask_attr(vd_attr at)  { return attr&at; }
	void toggle_attr(vd_attr at);
	void set_for_clr(vd_clr color);
	void set_back_clr(vd_clr color);
	vd_clr ask_for_clr(void);
	vd_clr ask_back_clr(void);
	void normalize(void);
	boolean operator()(void);
	boolean operator==(color_ind &other);
	boolean operator!=(color_ind &other);
};

class panel  {
// the generic screen object

	// abstract class for specific driver(s) to be implemented by user,
	// using late binding.
	// important: assumed are only console facilities - no keyboard input.
	// primitives 'get' and 'put' apply to screen matrix storage!

	private :
		boolean takes_graph;
		boolean wrap_around;

		void fix_limits(void);

	protected :
		// state data
		square_pos win;
		point_pos cursor;
		color_ind color, def_color;
		int lang;	      // pending language. user-managed values
		direction curdir;     // current cursor direction
		int frame_level, save_level; // save_buf control

		// controlled data redundancy
		point_pos top, bot;
		int hlen, vlen;

		// working storage for member-functions to return by ref
		intersection tmpint;
		color_ind tmpclr1, tmpclr2, tmpclr3, tmpclr4;
		char envbuf[6];
		point_pos tmpt1, tmpt2;

	public :
		// note: arguments defaulting to NULL are requests from the
		// driver to use (and advance) the built-in cursor.
		// otherwise (arguments supplied) "direct cursor adressing"
		// is requested (and built-in cursor is not affected).

		panel(square_pos *wind,color_ind *default_clr, direction *dir=NULL,
		 boolean ingraph=TRUE, boolean inwrap=FALSE);
		virtual ~panel(void) {};

		// the following methods must be redefined by the user
		// to suit the specific device:
		virtual char get_c(point_pos *pt=NULL)=0;
		 // get char in specified/cursor position
		virtual void put_c(char c, point_pos *pt=NULL, direction *dir=NULL)=0;
		 // put char in specified/cursor position,
		 // using pending color.
		 // if cursor position, cursor is advanced one step
		 // in specified/cursor direction.
		 // wrap-around practiced, if enabled
		virtual void put_color(color_ind &clr, point_pos *pt=NULL)=0;
		 // set color in specified/cursor position
		virtual void put_attr(vd_attr at, point_pos *pt=NULL)=0;
		 // toggle attribute in specified/cursor position
		virtual void put_background(vd_clr colornum, point_pos *pt=NULL)=0;
		 // set background color in specified/cursor position
		virtual void put_forground(vd_clr colornum, point_pos *pt=NULL)=0;
		 // set forground color in specified/cursor position
		virtual color_ind& get_color(point_pos *pt=NULL)=0;
		 // retrieve color in specified/cursor position
		virtual boolean get_attr(vd_attr at, point_pos *pt=NULL)=0;
		 // toggle attribute in specified/cursor position
		virtual void posit(point_pos *pt=NULL)=0;
		 // reposition to specified/internal-cursor position
		virtual void save(void)=0;
		 // save whole window content (not stackable!)
		virtual void restore(void)=0;
		 // restore window to state of last save operation, delete buff
		virtual unsigned int ask_cursor(void)=0;
		 // user-managed values
		virtual void set_cursor(unsigned int cursor_type)=0;
		 // user-managed values
		virtual unsigned int hide_cursor(void)=0;
		 // retruns current shape
		virtual void fix(void);
		 // fix controlled data redundancy

		 // the following are "macros" based on the above having been
		// defined in the working object:
		boolean ask_legal(point_pos *pt);
		 // tell if point_pos is within window range
		 // (default implementation supplied)
		boolean move(direction *dir=NULL, point_pos *pt=NULL);
		 // move cursor/specified-point_pos one position
		 // in pending/specified direction, if movement possible
		boolean wpmove(direction *dir=NULL, point_pos *pt=NULL);
		 // move cursor/specified-point_pos one position
		 // in pending/specified direction. wrap-around, if movement
		 // impossible (if enabled in panel setup)
		char succ_c(direction *dir=NULL, point_pos *pt=NULL);
		 // get neighbour of cursor/specified char,
		 // in pending/specified direction, no movement.
		virtual void clr_eol(point_pos *pt=NULL);
		 // clear from specified/cursor position to end-of-line
		 // supplied default implementation ignores efficiency!
		virtual void clear(void);
		 // clear whole screen and posit home
		 // supplied default implementation ignores efficiency!
		virtual void put_s(char *s, point_pos *pt=NULL, direction *dir=NULL);
		 // write a string on screen starting at cursor/specified point.
		 // if cursor, it  is advanced, with wrap-around if enabled.
		 // not cursor, wrap around not practiced.
		 // supplied default implementation ignores efficiency!
		virtual void home(void);
		 // position to start corner of screen, depending on
		 // pending cursor direction.
		int ask_limit(direction &dir);
		 // beginning/end of window - height/breadth
		void set_limit(direction &dir, int pos);
		 // modify window dimensions
		void move_limit(direction &dir, int ofst=1);
		 // modify window dimensions, relative

		// full screen operations and navigation
		virtual void arclist(point_pos *pt, int len, direction &dir,
		 weight_d &wgt, boolean tipped=FALSE, color_ind *clr=NULL);
		 // the graphic primitive (simmilar to shape "arc").
		 // draw straight line from y:x in direction and weight
		 // specified, possibly tipped by arrowhead.
		 // semigraphics intersected are "welded", where possible.
		 // for "welding" logic, refer to computer_alphabet::carrw()
		 // in snav-1.
		 // warning: arc listing ignores cursor, and does not wrap!
		virtual void arcclr(point_pos *pt, int len, direction &dir);
		 // clear space formerly occupied by arc
		virtual boolean twg_c(point_pos *pt);
		 // translate one typewriter graphic char to 1h1v semigraphic
		 // ("full screen" algorithm)
		virtual int twg(void);
		 // translate panel from typewriter graphics to 1h1v semigraphics
		virtual char *env(point_pos *pt=NULL);
		 // returns string made of current char, followed
		 // by the four sorrounding chars, in direction++ order.
		virtual intersection &gowhere(point_pos *junction);
		 // directions of arcs available from specified point on screen
		 // (if point is currently occupied by a semigraphic). if result
		 // is null, one is allocated. based upon env().
		 // for association logic see alph::ccont() in snav-1.

		// management
		virtual void set_color(color_ind &new_color);
		color_ind &ask_color(void); // temp result
		color_ind &ask_def_color(void); // temp result
		virtual void toggle_attr(vd_attr at);
		boolean ask_attr(vd_attr at);
		void reset_color(void);
		void set_def_color(color_ind &clr);
		void set_def_attr(vd_attr at);
		void set_def_background(vd_clr color);
		void set_def_forground(vd_clr color);
		virtual void paint_background(vd_clr colornum);
		point_pos &ask_where(void); // temp result
		boolean ask_grph(void);
		boolean ask_wrap(void);
		direction &ask_dir(void); // temp result
		void set_dir(direction &dir);
		void turn(void);
		int ask_len(axis &dim);
		int ask_lang(void);
		void set_lang(int inlang);
		point_pos &ask_corner(direction &dir); // temp result
		 // current screen window corner - either top-left or bot-right
		square_pos &ask_window(void); // temp result

		 // misc
		virtual boolean enframe(weight_d &wgt=h1v1, char *title="\0",
		 boolean centered=FALSE);
		 // draw oblong around panel and contract
		virtual boolean deframe(void);
		 // expand (if frame_level>0)
		void cr(void); // carriage return
} ;

#ifndef SNAV2_C
extern panel *master_screen;
// maximum size for all screens in system (to be allocated by user application)
#endif

class screen_driver_manager
 { // run-time panel-implementation selector

	private:
		int default_screen;
	public:
		screen_driver_manager(int def=0);
		int ask_default(void);
		void set_default(int def);
		panel *allocate(square_pos &window, int typ=0);
		 // to be implemented by user, reflecting the local driver repertory.
		 // for example, see file "demo0.hpp"
};

#endif

