/* W4.C		Screen Display					30 Oct 1987 */

#include <ws.h>


void rest_page(ind)		/* displays rest of page		*/
INDEX ind;			/* starting at index ind		*/
{
	extern INDEX	t_end;		/* text end			*/
	extern unsigned	pb_line;	/* page bottom line		*/

	unsigned	line();		/* line of index		*/
	INDEX		rest_line();	/* displays rest of line	*/
	void		clr_screen();	/* clears rest of screen	*/

	static unsigned y;

	y = line(ind);
	while (y++<=pb_line) {
		if (ind>=t_end) break; 
		ind = rest_line(ind) + 1;
	}
	clr_screen();
	return;
}


INDEX rest_line(ind)		/* displays rest of line starting at ind */
INDEX ind;			/* returns index of end of line		*/
{
	void		get_xy();	/* gets cursor coord for index	*/
	void 		CursorTo();	/* positions cursor on screen	*/
	unsigned	disp_index();	/* displays char at index	*/
	void		clr_line();	/* clears rest of line		*/

	static unsigned	x,y;

	get_xy(ind,&x,&y);
	CursorTo(x,y);			/* only at begin of a line	*/
	while (*ind) 			/* all the way thru end of line	*/
		x=disp_index(ind++,x);
	if (x<=LINE_LENGTH) clr_line();
	return ind;
}


unsigned disp_index(ind,x)	/* displays char with index ind at pos x */
INDEX ind;			/* returns new cursor position x	*/
unsigned x;			/* Note: Block display is checked for here */
{
	extern INDEX	t_end;		/* text end			*/
	extern INDEX	b_begin, b_end;	/* block begin, end		*/
	extern FLAG	blk_flag;	/* TRUE iff block active	*/

	unsigned	disp_char();	/* displays char at x position	*/
	void	LoVideo(),HiVideo();	/* sets video attributes	*/

	if (ind>t_end) return x;
	if (blk_flag && b_begin<=ind && ind<=b_end) {
		LoVideo();  x=disp_char(*ind,x);  HiVideo();
		return x;
	} 
	return disp_char(*ind,x);
}


unsigned disp_char(ch,x)	/* displays char ch at position x	*/
char ch;			/* returns new cursor position x	*/
unsigned x;			/* Note: TABs are expanded here		*/
{
	unsigned wrt_char();		/* writes char at position x	*/

	if (ch=='\t') {
		do
			x=wrt_char(' ',x);
		while ((x-1) % TAB_POS != 0);
		return x;
	}
	return wrt_char(ch,x);
}


unsigned wrt_char(ch,x)		/* writes char ch at position x		*/
char ch;			/* returns new cursor position x	*/
unsigned x;			/* Note: LINE_LENGTH is checked for here */
{
	void		OutChar();		/* writes char on screen  */
	void		LoVideo(),HiVideo();	/* sets video attributes  */
	unsigned	ch_exp();		/* expanded char width	  */

	if (x<LINE_LENGTH) 
		OutChar(ch);
	else if (x==LINE_LENGTH) {
		LoVideo(); OutChar('+'); HiVideo();
	}
	return x+ch_exp(ch);
}


void clr_line()		/* clears rest of line from current position	*/
{
	void	ClrEol();

	ClrEol();
	return;
}


void clr_screen()	/* clears rest of screen from current position */
{
	void	ClrEos();

	ClrEos();
	return;
}


void init_scr()			/* inits screen display			*/
{
	void	CursorTo();		/* positions cursor on screen	*/
	void	clr_screen();		/* clears rest of screen	*/

	CursorTo(PX,PY);		/* cursor to prompt position	*/
	clr_screen();
	return;
}


void exit_scr()			/* cleans up screen before exit		*/
{
	void	CursorTo();		/* positions cursor on screen	*/
	void	clr_screen();		/* clears rest of screen	*/

	CursorTo(LEFT_SCREEN,BOTTOM_SCREEN-1);
	clr_screen();
	return;
}
