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

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

#include <intuition/intuition.h>
#include <intuition/screens.h>

#include <libraries/gadtools.h>

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

#include "Life.h"
#include "Life_rev.h"
#include "/defs.h"

struct lPrefObject {
	LONG Size;
	LONG Generations;
	LONG Density;
};

VOID blank( VOID );

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

VOID setLifePrefs( VOID )
{
	GT_SetGadgetAttrs( LifeGadgets[GD_BSIZE], LifeWnd, 0L, GTSL_Level, nP.Size, 0L );
	GT_SetGadgetAttrs( LifeGadgets[GD_GENS], LifeWnd, 0L, GTSL_Level, nP.Generations, 0L );
	GT_SetGadgetAttrs( LifeGadgets[GD_DENSE], LifeWnd, 0L, GTSL_Level, nP.Density, 0L );
}

int OKClicked( VOID )
{
	CopyMem( &nP, prefData, sizeof( struct lPrefObject ));
	return( QUIT );
}

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

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

int BSIZEClicked( VOID )
{
	nP.Size = LifeMsg.Code;
	return( CONTINUE );
}

int GENSClicked( VOID )
{
	nP.Generations = LifeMsg.Code;
	return( CONTINUE );
}

int DENSEClicked( VOID )
{
	nP.Density = LifeMsg.Code;
	return( CONTINUE );
}

int LifeVanillaKey( VOID )
{
	switch( LifeMsg.Code ) {
	case 'o':
		return( OKClicked() );
	case 'c':
		return( CANCELClicked() );
	case 's':
		GT_SetGadgetAttrs( LifeGadgets[GD_BSIZE], LifeWnd, 0L, GTSL_Level, ++(nP.Size) > 40 ? nP.Size = 40 :
			nP.Size, 0L );
		return( CONTINUE );
	case 'S':
		GT_SetGadgetAttrs( LifeGadgets[GD_BSIZE], LifeWnd, 0L, GTSL_Level, --(nP.Size) < 5 ? nP.Size = 5 :
			nP.Size , 0L );
		return( CONTINUE );
	case 'g':
		GT_SetGadgetAttrs( LifeGadgets[GD_GENS], LifeWnd, 0L, GTSL_Level, ++(nP.Generations) > 999 ?
			nP.Generations = 999 : nP.Generations, 0L );
		return( CONTINUE );
	case 'G':
		GT_SetGadgetAttrs( LifeGadgets[GD_GENS], LifeWnd, 0L, GTSL_Level, --(nP.Generations) < 5 ?
			nP.Generations = 5 : nP.Generations , 0L );
		return( CONTINUE );
	case 'd':
		GT_SetGadgetAttrs( LifeGadgets[GD_DENSE], LifeWnd, 0L, GTSL_Level, ++(nP.Density) > 99 ?
			nP.Density = 99 : nP.Density, 0L );
		return( CONTINUE );
	case 'D':
		GT_SetGadgetAttrs( LifeGadgets[GD_DENSE], LifeWnd, 0L, GTSL_Level, --(nP.Density) < 1
			? nP.Density = 1 : nP.Density , 0L );
		return( CONTINUE );
	case 't':
		return( TESTClicked() );
	default:
		return( CONTINUE );
	}
}

VOID prefs( LONG command )
{
	switch( command ) {
	case STARTUP:
		CopyMem( prefData, &nP, sizeof( struct lPrefObject ));
		if( !SetupScreen() ) {
			if( !OpenLifeWindow()) setLifePrefs();
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleLifeIDCMP() != QUIT ) break;
	case KILL:
		CloseLifeWindow();
		break;
	}		
}

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