/** DoRev Header ** Do not edit! **
*
* Name             :  fClock.c
* Copyright        :  Free Software
* Creation date    :  12-May-93
* Translator       :  SAS/C 6.2
* Compiler opts.   :  See Makefile
*
* Date       Rev  Author               Comment
* ---------  ---  -------------------  ----------------------------------------
* 12-May-93    0  Michael D. Bayne     Floating clock blanker module
*
*** DoRev End **/

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/datetime.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <libraries/asl.h>
#include <graphics/text.h>
#include <graphics/gfxmacros.h>
#include <graphics/copper.h>
#include <graphics/videocontrol.h>
#include <hardware/custom.h>

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Clock.h"
#include "/defs.h"

extern	struct	Custom far custom;

struct cPrefObject {
	UBYTE fName[64], Speed, Cycle, Secs, Military;
	struct TextAttr Font;
};

struct	cPrefObject	nP;

STATIC	const	UBYTE	VersTag[] = "$VER:Clock 1.1 (5.12.93)";
		UBYTE	infoString[] = "Floating Clock Module";

void setClockPrefs( void )
{
	UBYTE fName[256];

	strcpy( fName, nP.fName );
	strcat( fName, " " );
	if( nP.Font.ta_Style & FSF_BOLD ) strcat( fName, "(B)" );
	if( nP.Font.ta_Style & FSF_ITALIC ) strcat( fName, "(I)" );
	if( nP.Font.ta_Style & FSF_UNDERLINED ) strcat( fName, "(U)" );

        GT_SetGadgetAttrs( ClockGadgets[GD_SPEED], ClockWnd, 0L, GTSL_Level, nP.Speed, 0L );
	GT_SetGadgetAttrs( ClockGadgets[GD_FTXT], ClockWnd, 0L, GTTX_Text, fName, 0L );
	GT_SetGadgetAttrs( ClockGadgets[GD_FSIZE], ClockWnd, 0L, GTNM_Number, nP.Font.ta_YSize, 0L );
	GT_SetGadgetAttrs( ClockGadgets[GD_MIL], ClockWnd, 0L, GTCY_Active, nP.Military, 0L );
	GT_SetGadgetAttrs( ClockGadgets[GD_CYCLE], ClockWnd, 0L, GTCY_Active, nP.Cycle, 0L );
	GT_SetGadgetAttrs( ClockGadgets[GD_SECS], ClockWnd, 0L, GTCY_Active, nP.Secs, 0L );
}

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

int CANCELClicked( void )
{
	return( CANCEL );
}

int SPEEDClicked( void )
{
	nP.Speed = ClockMsg.Code;
	return( OK );
}

int MILClicked( void )
{
	nP.Military = ClockMsg.Code;
	return( OK );
}

int CYCLEClicked( void )
{
	nP.Cycle = ClockMsg.Code;
	return( OK );
}

int SECSClicked( void )
{
	nP.Secs = ClockMsg.Code;
	return( OK );
}

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, ClockWnd, ASLFO_SleepWindow, TRUE, ASLFO_TitleText,
			(ULONG)"Please choose a font...", 0L )) {
			strcpy( nP.fName, fReq->fo_Attr.ta_Name );
			nP.Font.ta_YSize = fReq->fo_Attr.ta_YSize;
		}
		FreeAslRequest( fReq );
	}
	setClockPrefs();
	return( OK );
}

int ClockVanillaKey( void )
{
	switch( ClockMsg.Code ) {
	case 'o':
		return( QUIT );
	case 'c':
		return( QUIT );
	case 'f':
		return( FONTClicked());
	case 's':
		GT_SetGadgetAttrs( ClockGadgets[GD_SPEED], ClockWnd, 0L, GTSL_Level, ++(nP.Speed) > 50 ?
			nP.Speed = 50 : nP.Speed, 0L );
		return( OK );
	case 'S':
		GT_SetGadgetAttrs( ClockGadgets[GD_SPEED], ClockWnd, 0L, GTSL_Level, --(nP.Speed) < 1 ?
			nP.Speed = 1 : nP.Speed, 0L );
		return( OK );
	default:
		return( OK );
	}
}

void prefs( UBYTE *prefData )
{
	LONG	retval = OK;

	CopyMem(( struct cPrefObject * )prefData, &nP, sizeof( struct cPrefObject ));
	nP.Font.ta_Name = nP.fName;
	if( !SetupScreen() ) { if( !OpenClockWindow()) {
		CloseDownScreen();
		setClockPrefs();
		while( 1 ) {
			WaitPort( ClockWnd->UserPort );
			retval = HandleClockIDCMP();
			if( retval == QUIT ) {
				CopyMem( &nP, prefData, sizeof( struct cPrefObject ));
				break;
			}
			if( retval == CANCEL ) break;
		}
		CloseClockWindow();
	} } else CloseDownScreen();
}

void getTime( UBYTE *time, struct cPrefObject *nP )
{
	struct	DateTime dt;
	ULONG	h,m,s;

	CopyMem( DateStamp( &dt.dat_Stamp ), &dt.dat_Stamp, sizeof( struct DateStamp ));
	dt.dat_StrDay = 0L;
	dt.dat_StrDate = 0L;
	dt.dat_StrTime = time;

	if( !DateToStr( &dt )) strcpy( time, "Half past skin-thirty" );
	else {
		sscanf( time, "%d:%d:%d", &h, &m, &s );
		if( nP->Secs ) sprintf( time, "%2d:%02d:%02d", nP->Military?h:(!h?12:(h%13)+(h/13)), m, s );
		else sprintf( time, "%2d:%02d", nP->Military?h:(!h?12:(h%13)+(h/13)), m );
		if( !nP->Military ) strcat( time, h>11?"pm":"am" );
	}
}

VOID newCopperList( LONG height, struct ViewPort *vp )
{
	#define NUMCOLORS 45

	struct	UCopList	*uCopList;
	struct	TagItem	uCopTags[] = { { VTAG_USERCLIP_SET, 0L }, { VTAG_END_CM, 0L } };
	register	USHORT	i, index = rand()%NUMCOLORS, spc;
        UWORD	spectrum[] = {  0x0F00, 0x0E10, 0x0D20, 0x0C30, 0x0B40, 0x0A50, 0x0960, 0x0870, 0x0780, 0x0690,
				0x05A0, 0x04B0, 0x03C0, 0x02D0, 0x01E0, 0x00F0, 0x00E1, 0x00D2, 0x00C3, 0x00B4,
				0x00A5, 0x0096, 0x0087, 0x0078, 0x0069, 0x005A, 0x004B, 0x003C, 0x002D, 0x001E,
				0x000F, 0x010E, 0x020D, 0x030C, 0x040B, 0x050A, 0x0609, 0x0708, 0x0807, 0x0906,
				0x0A05, 0x0B04, 0x0C03, 0x0D02, 0x0E01 };
    
	if( uCopList = ( struct UCopList * )AllocMem( sizeof( struct UCopList ), MEMF_PUBLIC|MEMF_CLEAR )) {

		spc = height/NUMCOLORS;

		CINIT( uCopList, NUMCOLORS );

		for( i = 0; i<NUMCOLORS; ++i ) {
			CWAIT( uCopList, i*spc, 0 );
			CMOVE( uCopList, custom.color[1], spectrum[(i+index)%NUMCOLORS] );
		}

		CEND( uCopList );

		Forbid();
		vp->UCopIns = uCopList;
		Permit();

		VideoControl( vp->ColorMap, uCopTags );

		RethinkDisplay();
	}
}

void blank( struct bMessage *Msg )
{
	struct	TextFont	*font;
	struct	cPrefObject	*nP = ( struct cPrefObject * )Msg->prefData;
	struct	Screen		*Scr;
	UBYTE	vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
		3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
	UBYTE	scrClock[128], c1 = 0, c2 = 14, c3 = 28;
	ULONG	x, y, Wid, Hei, len, numc, base, count = 0;
	LONG	dx = 1, dy = 1;

	srand( (ULONG)VBeamPos() );

	nP->Font.ta_Name = nP->fName;
	font = OpenDiskFont( &( nP->Font ));
	base = font->tf_Baseline;

	Scr = OpenScreenTags( 0l, SA_DisplayID, Msg->sMod, SA_Depth, 1, SA_Quiet, TRUE, SA_Overscan,
		OSCAN_STANDARD, SA_Font, &( nP->Font ), TAG_DONE );

	Wid = Scr->Width;
	Hei = Scr->Height;

	SetRGB4( &(Scr->ViewPort), 0, 0L, 0L, 0L );
	switch( nP->Cycle ) {
	case 0:
		SetRGB4( &(Scr->ViewPort), 1, vals[c1], vals[c2], vals[c3] );
		break;
	case 1:
		SetRGB4( &(Scr->ViewPort), 1, rand()%15 + 1, rand()%15 + 1, rand()%15 + 1 );
		break;
	case 2:
		SetRGB4( &(Scr->ViewPort), 1, 15, 15, 15 );
		break;
	case 3:
		newCopperList( Hei, &( Scr->ViewPort ));
		break;
	}

	getTime( scrClock, nP );
	if( nP->Secs ) numc = 8;
	else numc = 5;
	if( !nP->Military ) numc += 2;
	while(( len = TextLength( &(Scr->RastPort), scrClock, numc )) >= Wid ) numc--;

	x = rand()%( Wid - len - 1 ) + 1;
	y = rand()%( Hei - nP->Font.ta_YSize - 1 ) + 1;

	SetAPen( &(Scr->RastPort), 1 );
	Move( &(Scr->RastPort), x, y + base );
	Text( &(Scr->RastPort), scrClock, numc );

	while(!( SetSignal( 0L, 0L )&SIGBREAKF_CTRL_C )) {
		if( !nP->Cycle ) if( !(++count%10 )) SetRGB4( &(Scr->ViewPort), 1, vals[c1 = ++c1%42],
			vals[c2 = ++c2%42], vals[c3 = ++c3%42] );
		getTime( scrClock, nP );
		Move( &(Scr->RastPort), x, y + base );
		Text( &(Scr->RastPort), scrClock, numc );
		if(!( count%(51-nP->Speed ))) {	
			ScrollRaster( &(Scr->RastPort), dx, dy, x-1, y-1, x+len+1, y+nP->Font.ta_YSize+1 );
			x -= dx; y -= dy;
			if( x < 2 ) dx = -1;
			else if( x > Wid-len-2 ) dx = 1;
			if( y < 2 ) dy = -1;
			else if( y > Hei-nP->Font.ta_YSize-2 ) dy = 1;
		}
	}
	SetSignal( 0L, SIGBREAKF_CTRL_C );

	if( font ) CloseFont( font );
	CloseScreen( Scr );
}

void defaults( UBYTE *prefData )
{
	strcpy( (( struct cPrefObject * )prefData )->fName, "topaz.font" );
	(( struct cPrefObject * )prefData )->Font.ta_Name = (( struct cPrefObject * )prefData )->fName;
	(( struct cPrefObject * )prefData )->Font.ta_YSize = 11;
	(( struct cPrefObject * )prefData )->Font.ta_Style = FS_NORMAL;
	(( struct cPrefObject * )prefData )->Font.ta_Flags = FPB_ROMFONT;
	(( struct cPrefObject * )prefData )->Speed = 10;
	(( struct cPrefObject * )prefData )->Cycle = 0;
	(( struct cPrefObject * )prefData )->Military = 0;
	(( struct cPrefObject * )prefData )->Secs = 0;
}
