/************************************************************************
*																								*
*		output																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Module containing the basic screen output routines for the text.	*
*																								*
************************************************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/reqbase.h>
#include <private/help.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/reqproto.h>

#include	"settings.h"
#include "tops.h"

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

					Definitions

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

#define	RP			( FirstWindow->RPort )

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

				External References

***************************************/
/***************************************
					Functions
***************************************/

IMPORT	VOID			RefreshTSize( VOID );
IMPORT	VOID			RefreshRMarg( VOID );
IMPORT	VOID			RefreshLMarg( VOID );
IMPORT	VOID			RefreshPLength( VOID );
IMPORT	VOID			RefreshPWidth( VOID );
IMPORT	VOID			DoRefresh( VOID );
IMPORT	VOID			InitText( VOID );

/***************************************
					Variables
***************************************/

IMPORT	struct Window				  *FirstWindow;

IMPORT	UBYTE								LoadString[];
IMPORT	struct FileInfoBlock		  *FIB;
IMPORT	UBYTE								Dir[],
												File[],
												Path[];
IMPORT	struct FileRequester			FReq;

IMPORT	LONG								Memsize;
IMPORT	UBYTE							  *ActFile;

IMPORT	struct Settings				Sets[];
IMPORT	SHORT								ActSet;
IMPORT	UBYTE								ScTitle[];

IMPORT	UBYTE								LineBuf[];
IMPORT	SHORT								Topline;

IMPORT	UBYTE							  *HashTab[];
IMPORT	SHORT								NumLines;
IMPORT	USHORT							OTSize;

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

					Declarations

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

VOID						ShowText( SHORT );
UBYTE					  *ConvertLine( UBYTE *, SHORT );
VOID						PutLine( UBYTE *, SHORT );
static UBYTE		  *SkipLine( UBYTE * );
VOID						InitText( VOID );

/************************************************************************
*																								*
*		ShowText																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Routine for displaying the text.												*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			num			:	First line to show, or -1 for no changes			*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		ShowText( SHORT num )
{
	SHORT			i;
	UBYTE		  *p;

	/***/

	if( ActFile == NULL )
	{
		SetAPen( RP, 0 );
		RectFill( RP, 0L,0L, 640L,( LONG )( LAYOUT_TOP - 1 ));
		SetAPen( RP, 1 );
		Topline	=	0;
		return;
	}
	if( num > ( NumLines - NUM_LINES ))
		num	=	NumLines - NUM_LINES;
	if( num < 0 )
		num	=	0;
	if(( num == Topline )&&( OTSize == Sets[ ActSet].Tabsize ))
	{
		ClearIDCMP( FirstWindow );
		return;
	}
	p	=	HashTab[num];
	for( i = 0; i < NUM_LINES; i++ )
	{
		p	=	ConvertLine( p, Sets[ ActSet ].Tabsize );
		PutLine( LineBuf, i );
	}
	Topline	=	num;
}

/************************************************************************
*																								*
*		ConvertLine																			*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Converts a line of text for screen output.								*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			p				:	Pointer to the beginning of the line.				*
*			TS				:	Tabsize														*
*																								*
*		Returns			:																	*
*			Pointer to the beginning of the next line.							*
*																								*
************************************************************************/

UBYTE	  *ConvertLine( UBYTE *p, SHORT TS )
{
	SHORT			i,
					j,
					nt;
	UBYTE		  *q;

	/***/

	if( TS == 0 )			/* Don't wanna run into a division by zero */
		TS	=	1;

	/*	First, delete 80 chars, so that the screen output routines can use
		a constant value for the text length. Then, we find our return value.
		*/

	for( i = 0; i < 81; i++ )
		LineBuf[i]	=	' ';

	for( q = p;( *q != '\n' )&&( q < ( ActFile + Memsize )); q++ )
		;
	if( q < ( ActFile + Memsize ))
		q++;

	/*	Now, we process the line, using the tab replacing algorithm, we
		developed for "PS". We stop, when we have written 80 characters.
		*/

	i		=	0;
	while(( p != q )&&( i < 81 ))
	{
		if( *p != '\t' )
			LineBuf[i++]	=	*p++;
		else
		{
			nt	=	TS - ( i % TS );
			for( j = 0; j < nt; j++ )
				LineBuf[i++]	=	' ';
			p++;
		}
	}
	i--;
	LineBuf[i]	=	' ';
	return	q;
}

/************************************************************************
*																								*
*		PutLine																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Put a line to the Screen.														*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			lpt			:	Pointer to the beginning of the line				*
*			offs			:	Offset from the top.										*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		PutLine( UBYTE *lpt, SHORT offs )
{
	Move( RP, 0L, ( LONG )( 8 * offs + 17 ));
	Text( RP, lpt, 81 );
}

/************************************************************************
*																								*
*		SkipLine																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Skip one line of text															*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			p				:	Pointer to the line										*
*																								*
*		Returns			:																	*
*			Pointer to the next line													*
*																								*
************************************************************************/

static UBYTE	  *SkipLine( UBYTE *p )
{
	UBYTE	  *q;

	/***/

	for( q = p;( *q != '\n' )&&( q < ( ActFile + Memsize )); q++ )
		;
	if( q < ( ActFile + Memsize ))
		q++;
	return	q;
}

/************************************************************************
*																								*
*		InitText																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Initialize the hashing table for the line beginnings.					*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
*		Globals used	:																	*
*			HashTab																			*
*																								*
************************************************************************/

VOID		InitText( VOID )
{
	UBYTE		  *p;
	SHORT			i;

	/***/

	if( p	=	ActFile )
	{
		i	=	0;
		while(( p < ( ActFile + Memsize ))&&( i < 5120 ))
		{
			HashTab[i++]	=	p;
			p	=	SkipLine( p );
		}
		NumLines	=	i;
	}
	else
	{
		NumLines	=	0;
	}
}
