/* *****                                                            *****
** *****           Program created by Ken Farinsky, 1986.           *****
** *****                                                            *****
**
** All material contained herein may be used in any way desired.
**                    >>>>> i.e.  PUBLIC DOMAIN. <<<<<
** Not limited to any single bulletin board - can be freely transferred.
**
** Original distribution through:
**     Slipped Disk, Inc.
**     Madison Heights,  MI  48071
**     (313) 583-9803
**
** Distributed in the hopes of increasing the level of understanding of the
** Amiga personal computer.  Even the best computer can only succeed with
** the proper software support.
*/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <functions.h>
#include <graphics/gfx.h>
#include <graphics/clip.h>
#include "view.h"

extern struct Library	   *IntuitionBase ;
extern struct Library	   *GfxBase ;
extern struct Library	   *LayersBase ;
extern struct Library	   *DosBase ;
extern SHORT				nums_on ;
extern struct TextFont	   *textfont ;


/*----------------------------------------------------------------------
** install_data() -  this program clears the screen, then puts the correct
** text back.  the screen is filled one line too far down and one column too
** far right, relying on the GZZ window to clip.  indenting is also done
** here, depending on the setting of the bottom prop gadget (indent parm).
*/
void			install_data( The_Window, top, win_size, offset, indent)
struct Window  *The_Window ;
UBYTE		  **top ;
SHORT			win_size ;
SHORT			offset ;
SHORT			indent ;
{
SHORT				indent_ktr ;
SHORT				ktr ;
UBYTE				line_buffer[401] ;
UBYTE			   *line_ptr ;
struct RastPort	   *rp ;
void				expand_text() ;
long				font_height ;
long				font_baseline ;


rp = The_Window->RPort ;
font_height = textfont->tf_YSize ;
font_baseline = textfont->tf_Baseline ;

SetAPen(rp,WHTP) ;
RectFill(rp,0L,0L,(long)(The_Window->GZZWidth),(long)(The_Window->GZZHeight));
SetAPen(rp,BLKP) ;

for ( ktr = 0 ;
	  ktr < The_Window->GZZHeight/font_height + 1 ;
	  ktr++)
	{
	++top ;
	if ( *top == NULL)
		return ;
	Move(rp, 0L, (long)((ktr * font_height) + font_baseline)) ;
	expand_text( line_buffer, *top, (SHORT)400,(SHORT)(offset + ktr + 1)) ;

	line_ptr = line_buffer ;

	for ( indent_ktr = 0 ;
		  indent_ktr < indent ;
		  indent_ktr++)
		if ( *line_ptr != '\0')
			line_ptr++ ;

	Text(rp, line_ptr, (long)strlen(line_ptr)) ;
	}
}

/*----------------------------------------------------------------------
** expand_text() - expands out tabs to the proper position depending
** on the tab stops, also adds line numbers.  stops when the line length
** reaches the max_line parm.  passes the data back in out_buffer.
*/
void expand_text( out_buffer, in_buffer, max_line, line_num)
UBYTE	*out_buffer ;
UBYTE	*in_buffer ;
SHORT	 max_line ;
SHORT	 line_num ;
{
SHORT	line_position ;
SHORT	tab_position ;

extern SHORT	tab_width ;

line_position = 1 ;

if ( nums_on == TRUE)
	{
	sprintf( out_buffer, " %4.4d  ", line_num) ;
	out_buffer += 7 ;
	max_line -= 7 ;
	}

while ( max_line-- > 0)
	{
	switch (*in_buffer)
		{
		case '\0':
			*out_buffer = '\0' ;
			return ;
			break ;
		case '\t':
			in_buffer++ ;
			tab_position =
				(tab_width*((line_position-1)/tab_width) + tab_width + 1) ;
			while ( line_position < tab_position)
				{
				*out_buffer++ = ' ' ;
				line_position++ ;
				if (max_line-- <= 0)
					{
					*out_buffer = '\0' ;
					return ;
					}
				}
			break ;
		default:
			*out_buffer++ = *in_buffer++ ;
			line_position++ ;
			break ;
		}
	}
*out_buffer = '\0' ;
}
