/** DoRev Header ** Do not edit! **
*
* Name             :  fText.c
* Copyright        :  Free Software
* Creation date    :  12-May-93
* Translator       :  SAS/C 6.2
* Compiler opts.   :  See Makefile
*
* Date       Rev  Author               Comment
* ---------  ---  -------------------  ----------------------------------------
* 12-May-93    1  Michael D. Bayne     Fixed problem with x pos = 0 at start
* 12-May-93    0  Michael D. Bayne     Floating text blanker module
*
*** DoRev End **/

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <libraries/asl.h>
#include <graphics/text.h>
#include <graphics/gfxmacros.h>
#include <graphics/copper.h>
#include <graphics/videocontrol.h>
#include <hardware/custom.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/dos_protos.h>
#include <clib/diskfont_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/utility_protos.h>
#include <clib/asl_protos.h>
#include <clib/alib_protos.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Text.h"
#include "/defs.h"

extern	struct	Custom far custom;

struct tPrefObject {
	UBYTE text[128];
	UBYTE fName[64];
	UBYTE Speed;
	UBYTE State;
	UBYTE Cycle;
	struct TextAttr Font;
};

struct	tPrefObject	nP;

STATIC	const	UBYTE	VersTag[] = "$VER:Text 1.4 (5.12.93)";
		UBYTE	infoString[] = "Bouncing Text Module";

void setTextPrefs( void )
{
        GT_SetGadgetAttrs( TextGadgets[GD_SPEED], TextWnd, 0L, GTSL_Level, nP.Speed, 0L );
	GT_SetGadgetAttrs( TextGadgets[GD_FTXT], TextWnd, 0L, GTTX_Text, nP.fName, 0L );
	if( nP.State ) GT_SetGadgetAttrs( TextGadgets[GD_FONT], TextWnd, 0L, GA_Disabled, TRUE, 0L );
	else GT_SetGadgetAttrs( TextGadgets[GD_FONT], TextWnd, 0L, GA_Disabled, FALSE, 0L );
	GT_SetGadgetAttrs( TextGadgets[GD_FSIZE], TextWnd, 0L, GTNM_Number, nP.Font.ta_YSize, 0L );
	GT_SetGadgetAttrs( TextGadgets[GD_TXT], TextWnd, 0L, GTST_String, nP.text, 0L );
	GT_SetGadgetAttrs( TextGadgets[GD_SWITCH], TextWnd, 0L, GTCY_Active, nP.State, 0L );
	GT_SetGadgetAttrs( TextGadgets[GD_CYCLE], TextWnd, 0L, GTCY_Active, nP.Cycle, 0L );
}

int OKClicked( void )
{
	return( QUIT );
}

int CANCELClicked( void )
{
	return( CANCEL );
}

int SPEEDClicked( void )
{
	nP.Speed = TextMsg.Code;
	return( OK );
}

int SWITCHClicked( void )
{
	nP.State = TextMsg.Code;
	setTextPrefs();
	return( OK );
}

int CYCLEClicked( void )
{
	nP.Cycle = TextMsg.Code;
	return( OK );
}

int FONTClicked( void )
{
	struct FontRequester *fReq;

	if( fReq = ( struct FontRequester * )AllocAslRequestTags( ASL_FontRequest, ASL_FontName, (ULONG)nP.fName,
		ASL_FontHeight, nP.Font.ta_YSize, ASL_MaxHeight, 100, TAG_DONE )) {
		if( AslRequestTags( fReq, ASLFO_Window, TextWnd, ASLFO_SleepWindow, TRUE, ASLFO_TitleText,
			(ULONG)"Please choose a font...", 0L )) {
			strcpy( nP.fName, fReq->fo_Attr.ta_Name );
			nP.Font.ta_YSize = fReq->fo_Attr.ta_YSize;
		}
		FreeAslRequest( fReq );
	}
	setTextPrefs();
	return( OK );
}

int TXTClicked( void )
{
	strcpy( nP.text, (( struct StringInfo * )( TextGadgets[GD_TXT] )->SpecialInfo )->Buffer );
	return( OK );
}

int TextVanillaKey( void )
{
	switch( TextMsg.Code ) {
	case 'o':
		return( QUIT );
	case 'c':
		return( QUIT );
	case 'f':
		return( FONTClicked());
	case 's':
		GT_SetGadgetAttrs( TextGadgets[GD_SPEED], TextWnd, 0L, GTSL_Level, ++(nP.Speed) > 50 ?
			nP.Speed = 50 : nP.Speed, 0L );
		return( OK );
	case 'S':
		GT_SetGadgetAttrs( TextGadgets[GD_SPEED], TextWnd, 0L, GTSL_Level, --(nP.Speed) < 1 ?
			nP.Speed = 1 : nP.Speed, 0L );
		return( OK );
	default:
		return( OK );
	}
}

void prefs( UBYTE *prefData )
{
	LONG	retval = OK;

	CopyMem(( struct tPrefObject * )prefData, &nP, sizeof( struct tPrefObject ));
	nP.Font.ta_Name = nP.fName;
	if( !SetupScreen() ) { if( !OpenTextWindow()) {
		CloseDownScreen();
		setTextPrefs();
		while( 1 ) {
			WaitPort( TextWnd->UserPort );
			retval = HandleTextIDCMP();
			if( retval == QUIT ) {
				CopyMem( &nP, prefData, sizeof( struct tPrefObject ));
				break;
			}
			if( retval == CANCEL ) break;
		}
		CloseTextWindow();
	} } else CloseDownScreen();
}

VOID newCopperList( LONG height, struct ViewPort *vp )
{
	#define NUMCOLORS 45

	struct	UCopList	*uCopList;
	struct	TagItem	uCopTags[] = { { VTAG_USERCLIP_SET, 0L }, { VTAG_END_CM, 0L } };
	register	USHORT	i, index = rand()%NUMCOLORS, spc;
        UWORD	spectrum[] = {  0x0F00, 0x0E10, 0x0D20, 0x0C30, 0x0B40, 0x0A50, 0x0960, 0x0870, 0x0780, 0x0690,
				0x05A0, 0x04B0, 0x03C0, 0x02D0, 0x01E0, 0x00F0, 0x00E1, 0x00D2, 0x00C3, 0x00B4,
				0x00A5, 0x0096, 0x0087, 0x0078, 0x0069, 0x005A, 0x004B, 0x003C, 0x002D, 0x001E,
				0x000F, 0x010E, 0x020D, 0x030C, 0x040B, 0x050A, 0x0609, 0x0708, 0x0807, 0x0906,
				0x0A05, 0x0B04, 0x0C03, 0x0D02, 0x0E01 };
    
	if( uCopList = ( struct UCopList * )AllocMem( sizeof( struct UCopList ), MEMF_PUBLIC|MEMF_CLEAR )) {

		spc = height/NUMCOLORS;

		CINIT( uCopList, NUMCOLORS );

		for( i = 0; i<NUMCOLORS; ++i ) {
			CWAIT( uCopList, i*spc, 0 );
			CMOVE( uCopList, custom.color[1], spectrum[(i+index)%NUMCOLORS] );
		}

		CEND( uCopList );

		Forbid();
		vp->UCopIns = uCopList;
		Permit();

		VideoControl( vp->ColorMap, uCopTags );

		RethinkDisplay();
	}
}

void blank( struct bMessage *Msg )
{
	struct	TextFont	*font;
	struct	tPrefObject	*nP = ( struct tPrefObject * )Msg->prefData;
	struct	Screen		*TextScr;
	UBYTE	vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
		3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
	UBYTE	scrText[128], c1 = 0, c2 = 14, c3 = 28;
	ULONG	x = 2, y, Wid, Hei, len, numc, base, count;
	LONG	dx = 1, dy = 1, delay, cycle = 0;
	FILE	*textFile;

	srand( (ULONG)VBeamPos() );

	if( nP->State ) {
		if( textFile = fopen( nP->text, "r" )) {
			fscanf( textFile, "%lu", &count );
			numc = rand()%(count+1);
			while((( fgetc( textFile ) == '\n' )?++cycle:cycle ) < numc );
			fscanf( textFile, "%s %d ", nP->fName, &delay );
			nP->Font.ta_YSize = (WORD)delay;
			fgets( scrText, 128, textFile );
			scrText[strlen( scrText )-1] = 0;
			fclose( textFile );
		} else sprintf( scrText, "Could not open: %s", nP->text );
	} else strcpy( scrText, nP->text );

	nP->Font.ta_Name = nP->fName;
	font = OpenDiskFont( &( nP->Font ));
	base = font->tf_Baseline;

	TextScr = OpenScreenTags( 0l, SA_DisplayID, Msg->sMod, SA_Depth, 1, SA_Quiet, TRUE, SA_Overscan,
		OSCAN_STANDARD, SA_Font, &( nP->Font ), TAG_DONE );

	Wid = TextScr->Width;
	Hei = TextScr->Height;

	numc = strlen( scrText );
	while(( len = TextLength( &(TextScr->RastPort), scrText, numc )) >= Wid-5 ) numc--;

	y = rand()%( Hei - nP->Font.ta_YSize );

	SetRGB4( &(TextScr->ViewPort), 0, 0L, 0L, 0L );
	switch( nP->Cycle ) {
	case 0:
		SetRGB4( &(TextScr->ViewPort), 1, vals[c1], vals[c2], vals[c3] );
		break;
	case 1:
		SetRGB4( &(TextScr->ViewPort), 1, rand()%15 + 1, rand()%15 + 1, rand()%15 + 1 );
		break;
	case 2:
		SetRGB4( &(TextScr->ViewPort), 1, 15, 15, 15 );
		break;
	case 3:
		newCopperList( Hei, &( TextScr->ViewPort ));
		break;
	}		

	SetAPen( &(TextScr->RastPort), 1 );
	Move( &(TextScr->RastPort), x, y + base );
	Text( &(TextScr->RastPort), scrText, numc );

	delay = 51 - nP->Speed;

	while(!( SetSignal( 0L, 0L )&SIGBREAKF_CTRL_C )) {
		if(!( ++count%delay )) {
			if( !nP->Cycle && !(count%(10*delay))) SetRGB4( &(TextScr->ViewPort), 1, vals[c1 = ++c1%42],
				vals[c2 = ++c2%42], vals[c3 = ++c3%42] );
			ScrollRaster( &(TextScr->RastPort), dx, dy, x-1, y-1, x+1+len, y+1+nP->Font.ta_YSize );
			x -= dx; y -= dy;
			if( x < 2 ) dx = -1;
			else if( x > Wid-len-2 ) dx = 1;
			if( y < 2 ) dy = -1;
			else if( y > Hei-nP->Font.ta_YSize-2 ) dy = 1;
		}
	}
	SetSignal( 0L, SIGBREAKF_CTRL_C );

	if( font ) CloseFont( font );
	CloseScreen( TextScr );
}

void defaults( UBYTE *prefData )
{
	strcpy( (( struct tPrefObject * )prefData )->text, "Every dog should have a boy" );
	strcpy( (( struct tPrefObject * )prefData )->fName, "topaz.font" );
	(( struct tPrefObject * )prefData )->Font.ta_Name = (( struct tPrefObject * )prefData )->fName;
	(( struct tPrefObject * )prefData )->Font.ta_YSize = 11;
	(( struct tPrefObject * )prefData )->Font.ta_Style = FS_NORMAL;
	(( struct tPrefObject * )prefData )->Font.ta_Flags = FPB_ROMFONT;
	(( struct tPrefObject * )prefData )->Speed = 10;
	(( struct tPrefObject * )prefData )->State = 0;
	(( struct tPrefObject * )prefData )->Cycle = 0;
}
