/*
 * Copyright (c) 1993 Michael D. Bayne.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software must display the following
 *    acknowledgement:
 *
 *       This product includes software developed by Michael D. Bayne.
 *
 * 4. My name may not be used to endorse or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY MICHAEL D. BAYNE ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL MICHAEL D. BAYNE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

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

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

#include <libraries/gadtools.h>
#include <dos/dos.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 "Fade.h"
#include "Fade_rev.h"
#include "/defs.h"
#include "/utility.h"

struct fPrefObject { LONG Delay; };

VOID blank( VOID );

struct	fPrefObject	nP;
STATIC	const	UBYTE	VersTag[] = VERSTAG;
extern	struct	Task	**Task;
extern		ULONG	Depth, Mode;
extern		UBYTE	*prefData;

VOID setFadePrefs( VOID )
{
	GT_SetGadgetAttrs( FadeGadgets[GD_DELAY], FadeWnd, 0L, GTSL_Level, nP.Delay, 0L );
}

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

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

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

int DELAYClicked( VOID )
{
	nP.Delay = FadeMsg.Code;
	return( CONTINUE );
}

int FadeVanillaKey( VOID )
{
	switch( FadeMsg.Code ) {
	case 'o':
		return( OKClicked() );
	case 'c':
		return( CANCELClicked() );
	case 'd':
		GT_SetGadgetAttrs( FadeGadgets[GD_DELAY], FadeWnd, 0L, GTSL_Level, ++(nP.Delay) > 59 ?
			nP.Delay = 59 : nP.Delay, 0L );
		return( CONTINUE );
	case 'D':
		GT_SetGadgetAttrs( FadeGadgets[GD_DELAY], FadeWnd, 0L, GTSL_Level, --(nP.Delay) < 0 ? nP.Delay = 0 :
			nP.Delay , 0L );
		return( CONTINUE );
	case 't':
		return( TESTClicked() );
	default:
		return( CONTINUE );
	}
}

VOID prefs( LONG command )
{
	switch( command ) {
	case STARTUP:
		CopyMem( prefData, &nP, sizeof( struct fPrefObject ));
		if( !SetupScreen() ) {
			if( !OpenFadeWindow()) setFadePrefs();
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleFadeIDCMP() != QUIT ) break;
	case KILL:
		CloseFadeWindow();
		break;
	}		
}

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

VOID blank( VOID )
{
	struct	fPrefObject	*fP;
	struct	Screen		*FScr;

	if( FadeWnd ) fP = &nP;
	else fP = ( struct fPrefObject * )prefData;

	if( FScr = cloneTopScreen() ) {
		if( fadeScreen( FScr, fP->Delay )) Wait( SIGBREAKF_CTRL_C );
		else SetSignal( 0L, SIGBREAKF_CTRL_C );
		closeTopScreen( FScr );
	}
}

VOID defaults( struct bMessage *Msg )
{
	static struct fPrefObject fPO = { 15L };

	Msg->bm_Mod = getTopScreenMode();
	Msg->bm_Dep = getTopScreenDepth();

	Msg->bm_Info = "Fade Module";
	Msg->bm_Data = ( UBYTE * )( &fPO );
}
