/*
 *	Copyright (c) 1993 Michael D. Bayne.
 *	All rights reserved.
 *
 *	Please see the documentation accompanying the distribution for distribution and disclaimer
 *  information.
 */

#include <libraries/gadtools.h>

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

#include "Species.h"
#include "Species_rev.h"
#include "/main.h"
#include "//defs.h"

struct ModulePrefs
{
	LONG Mode;
	LONG Size;
};

struct ModulePrefs nP;
VOID *OldPrefs = 0L;
STATIC const UBYTE VersTag[] = VERSTAG;

#define GETTOPSCREENMODE
#define SCRMODEREQ
#include "/utility.c"

int BT_SAVEClicked( VOID )
{
	if( OldPrefs )
	{
		CopyMem( &nP, OldPrefs, sizeof( struct ModulePrefs ));
		SavePrefs( OldPrefs );
		OldPrefs = 0L;
	}
	
	return QUIT;
}

int BT_TESTClicked( VOID )
{
	MessageServer( BM_SENDBLANK );

	return OK;
}

int BT_SCREENClicked( VOID )
{
	ScreenModeRequest( SpeciesWnd, &nP.Mode, 0L );
	
	return OK;
}

int BT_CANCELClicked( VOID )
{
	OldPrefs = 0L;
	
	return QUIT;
}

int SL_SIZEClicked( VOID )
{
	nP.Size = SpeciesMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( SpeciesGadgets[Gad], SpeciesWnd, 0L, Tag, Val, TAG_END )

int SpeciesVanillaKey( VOID )
{
	switch( SpeciesMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'd':
		return BT_SCREENClicked();
	case 'c':
		return QUIT;
	case 'i':
		BL_SetGadgetAttrs( GD_SL_SIZE, GTSL_Level, Inc( nP.Size, 1, 10 ));
		return OK;
	case 'I':
		BL_SetGadgetAttrs( GD_SL_SIZE, GTSL_Level, Dec( nP.Size, 1, 3 ));
		return OK;
	default:
		return OK;
	}
}

VOID DoPrefs( LONG command, VOID *Prefs )
{
	switch( command )
	{
	case STARTUP:
		OldPrefs = Prefs;
		CopyMem( Prefs, &nP, sizeof( struct ModulePrefs ));
		if( !SetupScreen())
		{
			SpeciesLeft = ( Scr->Width - ComputeX( SpeciesWidth ))/2 - Scr->WBorRight;
			SpeciesTop = ( Scr->Height - ComputeY( SpeciesHeight ) - Font->ta_YSize )/2 -
				Scr->WBorBottom;
			if( !OpenSpeciesWindow())
				BL_SetGadgetAttrs( GD_SL_SIZE, GTSL_Level, nP.Size );
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleSpeciesIDCMP() != QUIT )
			break;
	case KILL:
		CloseSpeciesWindow();
		break;
	}
}

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

VOID FillDefaults( VOID *Prefs )
{
	struct ModulePrefs mPO = { 0L, 5L };

	mPO.Mode = getTopScreenMode();
	CopyMem( &mPO, Prefs, sizeof( struct ModulePrefs ));
}
