/* *****                                                            *****
** *****           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 "view.h"

#define GADGET_BASE			70

extern struct Library	   *IntuitionBase ;
extern struct Library	   *GfxBase ;
extern struct Library	   *DosBase ;

extern struct Gadget		my_gad ;
extern struct TextFont	   *textfont ;

/*----------------------------------------------------------------------
** process_gadgetdown() - processes only two gadgets - the arrow gadgets.
** these cause single line movement up and down.
*/
void process_gadgetdown(The_Window,gad_addr,top,tot_recs,win_size,cur_offset)
struct Window  *The_Window ;
struct Gadget  *gad_addr ;
UBYTE		 ***top ;
SHORT			tot_recs ;
SHORT			win_size ;
SHORT		   *cur_offset ;
{
long	width ;
long	height ;
long	font_height ;
long	font_baseline ;
USHORT	gad_id ;
void 	update_prop() ;
void	expand_text() ;
UBYTE	line_buffer[201] ;
struct RastPort *rp ;

rp = The_Window->RPort ;
gad_id = gad_addr->GadgetID ;
width = (long)The_Window->GZZWidth ;
height = (long)The_Window->GZZHeight ;
font_height = textfont->tf_YSize ;
font_baseline = textfont->tf_Baseline ;

/* this is the up arrow.  scroll the raster up by the font_height
** fill the bottom area with white (on my system).  finally put in
** the newly visible lines (if there are any).
**
** ((*cur_offset + win_size) < tot_recs) indicates that there are lines
** left to display.  if no lines, then we are at the bottom.  don't do
** anything.
*/
if (( 21 == gad_id) && ( (*cur_offset + win_size) < tot_recs))
	{
	update_prop( The_Window, &my_gad, tot_recs, win_size, ++*cur_offset) ;
	ScrollRaster(rp,0L,font_height, 0L,0L,width, height) ;
	SetAPen(rp,WHTP) ;
	RectFill(rp,0L,(long)(font_height*(win_size-1)), width,height);
	SetAPen(rp,BLKP) ;

	Move(rp, 0L, (long)(font_height*(win_size-1)+font_baseline)) ;
	expand_text( line_buffer, *((*top)+win_size+1), 200, *cur_offset+win_size);
	Text(rp,line_buffer,(long)strlen(line_buffer)) ;

	if ( ( *cur_offset + win_size) < tot_recs)
		{
		Move(rp, 0L, (long)(font_height*(win_size)+font_baseline)) ;
		expand_text(line_buffer,*((*top)+win_size+2),200,
					*cur_offset+win_size+1);
		Text(rp,line_buffer,(long)strlen(line_buffer)) ;
		}

	(*top)++ ;
	}
/* this is the down arrow.  scroll the raster down by the font_height
** fill the top area with white (on my system).  finally put in
** the newly visible line (if there are any).
** there is only one line possible to be drawn going down (the new top line).
** going up there may be a partially exposed line, so two lines must be drawn.
*/
else if (( 20 == gad_id) && ( *cur_offset > 0))
	{
	update_prop( The_Window, &my_gad, tot_recs, win_size, --*cur_offset) ;
	ScrollRaster(rp,0L,-font_height, 0L,0L,width, height) ;
	SetAPen(rp,WHTP) ;
	RectFill(rp, 0L,0L, width,font_height-1L) ;
	SetAPen(rp,BLKP) ;
	Move(rp, 0L, font_baseline) ;
	expand_text( line_buffer, **top, 200, *cur_offset+1) ;
	Text(rp, line_buffer, (long)strlen(line_buffer)) ;
	(*top)-- ;
	}
}
