/*
 *	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/gadgetclass.h>
#include <workbench/workbench.h>

#include <libraries/gadtools.h>
#include <libraries/reqtools.h>

#include <graphics/displayinfo.h>

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

#include "defs.h"
#include "Blanker.h"
#include "Blanker_rev.h"

#include "protos/main.h"
#include "protos/cxhand.h"
#include "protos/messaging.h"
#include "protos/prefs.h"

UBYTE	prefName[108] = "", prefDir[108] = "SYS:Prefs/Presets/Blanker", modInfo[64];

ULONG doImage( UBYTE *path )
{
	struct DiskObject *bDO;
	struct Image *Image;

	ULONG x = ( FontX * 6 + 4 ) / 8 + 3, y = ( FontY * 38 + 4 ) / 8 + 2;
	ULONG dx = ( FontX * 85 + 4 ) / 8 - 6, dy = ( FontY * 25 + 4 ) / 8 - 4;

	if(!( bDO = GetDiskObjectNew( path ))) {
		EZRQ( "GetDiskObjectNew() failed. Can't load image." );
		return( 1L );
	}
	
	Image = ( struct Image * )( bDO->do_Gadget.GadgetRender );
	Image->LeftEdge = x + OffX + (dx-Image->Width)/2;
	Image->TopEdge = y + OffY + (dy-Image->Height)/2;
	EraseRect( BlankerWnd->RPort, x + OffX, y + OffY, x + dx + OffX, y + dy + OffY );
	DrawImage( BlankerWnd->RPort, Image, 0, 0 );
	FreeDiskObject( bDO );

	return( 0L );
}

void setPrefs( struct BlankerPrefs *cP, UBYTE gad )
{
	struct NameInfo sInf;
	struct Menu *MenuStrip;
	struct MenuItem *Item;

	if(( gad == GD_TOUT )||( gad == Blanker_CNT )) GT_SetGadgetAttrs( BlankerGadgets[GD_TOUT], BlankerWnd, 0L,
		GTIN_Number, (cP->bp_Timeout)/10, 0L );

	if(( gad == GD_PKEY )||( gad == Blanker_CNT )) GT_SetGadgetAttrs( BlankerGadgets[GD_PKEY], BlankerWnd, 0L,
		GTST_String, cP->bp_PopKey, 0L );

	if(( gad == GD_BKEY )||( gad == Blanker_CNT )) GT_SetGadgetAttrs( BlankerGadgets[GD_BKEY], BlankerWnd, 0L,
		GTST_String, cP->bp_BlankKey, 0L );

	if(( gad == GD_MODINFO )||( gad == Blanker_CNT )) {
		GT_SetGadgetAttrs( BlankerGadgets[GD_BMOD], BlankerWnd, 0L, GA_Disabled, BS_NOPRFS & cP->bp_Flags,
			0L );
		GT_SetGadgetAttrs( BlankerGadgets[GD_MODINFO], BlankerWnd, 0L, GTTX_Text, modInfo, 0L );
		if( BS_VALMOD & cP->bp_Flags ) doImage( doPath( cP->bp_Name, cP->bp_Dir ));
		else doImage( "PROGDIR:Blanker" );
	}

	if(( gad == GD_MDISPLAY )||( gad == Blanker_CNT )) {
		if( GetDisplayInfoData( 0l, ( UBYTE * )&sInf, sizeof( struct NameInfo ), DTAG_NAME, cP->bp_Mode ))
			GT_SetGadgetAttrs( BlankerGadgets[GD_MDISPLAY], BlankerWnd, 0L, GTTX_Text, sInf.Name, 0L );
		else GT_SetGadgetAttrs( BlankerGadgets[GD_MDISPLAY], BlankerWnd, 0L, GTTX_Text, "No Monitor Info", 0L );
		GT_SetGadgetAttrs( BlankerGadgets[GD_SDEP], BlankerWnd, 0L, GTNM_Number, 1<<(cP->bp_Depth), 0L );
		GT_SetGadgetAttrs( BlankerGadgets[GD_SMODE], BlankerWnd, 0L, GA_Disabled,
			!( cP->bp_Flags & BS_ASKSCR ), 0L );
	}

	if( gad == Blanker_CNT ) {
		MenuStrip = BlankerWnd->MenuStrip;
		ClearMenuStrip( BlankerWnd );

		Item = MenuStrip->NextMenu->NextMenu->FirstItem;
		if( bPrefs->bp_Flags & BS_RANDOM ) Item->Flags |= CHECKED;
		else Item->Flags &= ~CHECKED;

		ResetMenuStrip( BlankerWnd, MenuStrip );
	}
}

int TOUTClicked( void )
{
	bPrefs->bp_Timeout = 10*(((struct StringInfo*)(BlankerGadgets[GD_TOUT])->SpecialInfo)->LongInt);
	return( UpdateCX() );
}

int BKEYClicked( void )
{
	CopyMem( ((struct StringInfo *)(BlankerGadgets[GD_BKEY])->SpecialInfo )->Buffer, bPrefs->bp_BlankKey, 256 );
	return( UpdateCX() );
}

int PKEYClicked( void )
{
	CopyMem( ((struct StringInfo *)(BlankerGadgets[GD_PKEY])->SpecialInfo )->Buffer, bPrefs->bp_PopKey, 256 );
	return( UpdateCX() );
}

int HIDEClicked( void )
{
	return( CLOSEWIN );
}

int SAVEClicked( void )
{
	saveDefaultPrefs( bPrefs );

	return( OK );
}

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

int SMODEClicked( void )
{
	struct rtScreenModeRequester *smRequest;

	if( smRequest = rtAllocRequestA( RT_SCREENMODEREQ, 0l )) {
		rtChangeReqAttr( smRequest, RTSC_DisplayID, bPrefs->bp_Mode, RTSC_DisplayDepth, bPrefs->bp_Depth, TAG_END );
		tags[4] = RTSC_Flags;
		if( bPrefs->bp_Flags & BS_ASKDEP ) {
			tags[5] = SCREQF_DEPTHGAD|SCREQF_NONSTDMODES;
			if( rtScreenModeRequestA( smRequest, "Screen Mode", BTAGS )) {
				bPrefs->bp_Depth = smRequest->DisplayDepth;
				bPrefs->bp_Mode = smRequest->DisplayID;
			}
		} else {
			tags[5] = SCREQF_NONSTDMODES;
			if( rtScreenModeRequestA( smRequest, "Screen Mode", BTAGS ))
				bPrefs->bp_Mode = smRequest->DisplayID;
		}
		tags[4] = TAG_END;
		rtFreeRequest( smRequest );
		setPrefs( bPrefs, GD_MDISPLAY );
	} else EZRQ( "Not enough memory to allocate\nscreen mode requester." );
	return( OK );
}

int BMODClicked( void )
{
	sendModMsg( BM_DOPREFS, modulePort );

	return( OK );
}

int LMODClicked( void )
{
	struct rtFileRequester *fRequest;

	if( fRequest = rtAllocRequest( RT_FILEREQ, 0L )) {
		rtChangeReqAttr( fRequest, RTFI_Dir, bPrefs->bp_Dir, TAG_END );
		tags[4] = RTFI_Flags;
		tags[5] = FREQF_PATGAD;
		if( rtFileRequestA( fRequest, bPrefs->bp_Name, "Please choose blanker module...", BTAGS )) {
			CopyMem( fRequest->Dir, bPrefs->bp_Dir, 108 );
			loadModule( 2 );
		}
		tags[4] = TAG_END;
		rtFreeRequest( fRequest );
	} else EZRQ( "Couldn't allocate file requester." );
	return( OK );
}

int BlankerOpen( void )
{
	struct rtFileRequester *fRequest;

	if( fRequest = rtAllocRequest( RT_FILEREQ, 0L )) {
		rtChangeReqAttr( fRequest, RTFI_Dir, prefDir, RTFI_MatchPat, "#?.pre", TAG_END );
		tags[4] = RTFI_Flags;
		tags[5] = FREQF_PATGAD;
		if( rtFileRequestA( fRequest, prefName, "Please choose prefs file...", BTAGS ))
			if( rc = loadBlankerPrefs( prefName, fRequest->Dir, bPrefs )) prefError( rc );
		tags[4] = TAG_END;
		rtFreeRequest( fRequest );
	} else EZRQ( "Couldn't allocate file requester." );
	return( OK );
}

int BlankerSaveAs( void )
{
	struct rtFileRequester *fRequest;

	if( fRequest = rtAllocRequest( RT_FILEREQ, 0L )) {
		rtChangeReqAttr( fRequest, RTFI_Dir, prefDir, RTFI_MatchPat, "#?.pre", TAG_END );
		tags[4] = RTFI_Flags;
		tags[5] = FREQF_PATGAD;
		if( rtFileRequestA( fRequest, prefName, "Please choose a save file...", BTAGS ))
			saveBlankerPrefs( prefName, fRequest->Dir, bPrefs );
		tags[4] = TAG_END;
		rtFreeRequest( fRequest );
	} else EZRQ( "Couldn't allocate file requester." );
	return( OK );
}

int BlankerAbout( void )
{
	EZRQ( VERS"\nby Michael D. Bayne\n\nbaynemd@nextwork.rose-hulman.edu" );
	return( OK );
}

int BlankerLoadMod( void )
{
	return( LMODClicked() );
}

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

int BlankerLSaved( void )
{
	if( rc = loadPrefsFromENVARC( bPrefs )) prefError( rc );
	return( OK );
}

int BlankerRandPrefs( void )
{
	bPrefs->bp_Flags ^= BS_RANDOM;
	return( OK );
}

int BlankerCloseWindow( void )
{
	return( CLOSEWIN );
}

int BlankerVanillaKey( void )
{
	switch( BlankerMsg.Code ) {
	case 'c':
		return( SMODEClicked() );
	case 'r':
		return( BMODClicked() );
	case 'l':
		return( BlankerLoadMod());
	case 't':
		ActivateGadget( BlankerGadgets[GD_TOUT], BlankerWnd, 0l );
		return( OK );
	case 'b':
		ActivateGadget( BlankerGadgets[GD_BKEY], BlankerWnd, 0l );
		return( OK );
	case 'p':
		ActivateGadget( BlankerGadgets[GD_PKEY], BlankerWnd, 0l );
		return( OK );
	case 'h':
		return( HIDEClicked() );
	case 's':
		return( SAVEClicked() );
	case 'q':
		return( QUIT );
	default:
		return( OK );
	}
}
