#include <exec/types.h>
#include <exec/memory.h>

#include <dos/dos.h>

#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <intuition/screens.h>
#include <libraries/gadtools.h>
#include <libraries/asl.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/asl_protos.h>
#include <clib/alib_protos.h>

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

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

VOID blank( VOID );

	struct	tPrefObject	nP;
STATIC	const	UBYTE	VersTag[] = VERSTAG;
extern	struct	Task	**Task;
extern		UBYTE   *prefData;

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 )
{
	CopyMem( &nP, prefData, sizeof( struct tPrefObject ));
	return( QUIT );
}

int TESTClicked( VOID )
{
	*Task = FindTask( 0L );
	blank();
	return( CONTINUE );
}

int CANCELClicked( VOID )
{
	return( QUIT );
}

int SPEEDClicked( VOID )
{
	nP.Speed = TextMsg.Code;
	return( CONTINUE );
}

int SWITCHClicked( VOID )
{
	nP.State = TextMsg.Code;
	setTextPrefs();
	return( CONTINUE );
}

int CYCLEClicked( VOID )
{
	nP.Cycle = TextMsg.Code;
	return( CONTINUE );
}

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 )) {
			CopyMem( fReq->fo_Attr.ta_Name, nP.fName, 31 );
			nP.Font.ta_YSize = fReq->fo_Attr.ta_YSize;
		}
		FreeAslRequest( fReq );
	}
	setTextPrefs();
	return( CONTINUE );
}

int TXTClicked( VOID )
{
	CopyMem( (( struct StringInfo * )( TextGadgets[GD_TXT] )->SpecialInfo )->Buffer, nP.text, 128 );
	return( CONTINUE );
}

int TextVanillaKey( VOID )
{
	switch( TextMsg.Code ) {
	case 'o':
		return( OKClicked() );
	case 't':
		return( TESTClicked() );
	case 'c':
		return( CANCELClicked() );
	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( CONTINUE );
	case 'S':
		GT_SetGadgetAttrs( TextGadgets[GD_SPEED], TextWnd, 0L, GTSL_Level, --(nP.Speed) < 1 ?
			nP.Speed = 1 : nP.Speed, 0L );
		return( CONTINUE );
	default:
		return( CONTINUE );
	}
}

VOID prefs( LONG command )
{
	switch( command ) {
	case STARTUP:
		CopyMem( prefData, &nP, sizeof( struct tPrefObject ));
		nP.Font.ta_Name = nP.fName;
		if( !SetupScreen() ) {
			if( !OpenTextWindow()) setTextPrefs();
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleTextIDCMP() != QUIT ) break;
	case KILL:
		CloseTextWindow();
	}
}

LONG winSig( VOID )
{
	return( TextWnd ? ( 1L << TextWnd->UserPort->mp_SigBit ) : 0L );
}
