/*
 *  Source machine generated by GadToolsBox V2.0b
 *  which is (c) Copyright 1991-1993 Jaba Development
 *
 *  GUI Designed by : Michael D. Bayne
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <string.h>
#include <clib/diskfont_protos.h>

#include "Puzzle.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *PuzzleWnd = NULL;
struct Gadget         *PuzzleGList = NULL;
struct IntuiMessage    PuzzleMsg;
struct Gadget         *PuzzleGadgets[7];
UWORD                  PuzzleLeft = 0;
UWORD                  PuzzleTop = 12;
UWORD                  PuzzleWidth = 190;
UWORD                  PuzzleHeight = 81;
UBYTE                 *PuzzleWdt = (UBYTE *)"Puzzle Prefs";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;
struct TextFont       *PuzzleFont = NULL;

UWORD PuzzleGTypes[] = {
	SLIDER_KIND,
	SLIDER_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	SLIDER_KIND,
	SLIDER_KIND
};

struct NewGadget PuzzleNGad[] = {
	68, 4, 89, 11, (UBYTE *)"_Horiz.", NULL, GD_SL_HORIZ, PLACETEXT_LEFT, NULL, (APTR)SL_HORIZClicked,
	68, 19, 89, 11, (UBYTE *)"_Vert.", NULL, GD_SL_VERT, PLACETEXT_LEFT, NULL, (APTR)SL_VERTClicked,
	4, 64, 58, 14, (UBYTE *)"_Save", NULL, GD_BT_SAVE, PLACETEXT_IN, NULL, (APTR)BT_SAVEClicked,
	128, 64, 58, 14, (UBYTE *)"_Cancel", NULL, GD_BT_CANCEL, PLACETEXT_IN, NULL, (APTR)BT_CANCELClicked,
	66, 64, 58, 14, (UBYTE *)"_Test", NULL, GD_BT_TEST, PLACETEXT_IN, NULL, (APTR)BT_TESTClicked,
	68, 34, 89, 11, (UBYTE *)"_Divisions", NULL, GD_SL_DIVISIONS, PLACETEXT_LEFT, NULL, (APTR)SL_DIVISIONSClicked,
	68, 49, 89, 11, (UBYTE *)"De_lay", NULL, GD_SL_DELAY, PLACETEXT_LEFT, NULL, (APTR)SL_DELAYClicked
};

ULONG PuzzleGTags[] = {
	(GTSL_Min), 2, (GTSL_Max), 14, (GTSL_Level), 7, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%2ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, (GT_Underscore), '_', (TAG_DONE),
	(GTSL_Min), 2, (GTSL_Max), 10, (GTSL_Level), 5, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%2ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, (GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTSL_Min), 2, (GTSL_Max), 10, (GTSL_Level), 5, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%2ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, (GT_Underscore), '_', (TAG_DONE),
	(GTSL_Max), 20, (GTSL_Level), 5, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%2ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, (GT_Underscore), '_', (TAG_DONE)
};

static UWORD ComputeX( UWORD value )
{
	return(( UWORD )((( FontX * value ) + 3 ) / 6 ));
}

static UWORD ComputeY( UWORD value )
{
	return(( UWORD )((( FontY * value ) + 4 ) / 9 ));
}

static void ComputeFont( UWORD width, UWORD height )
{
	Forbid();
	Font = &Attr;
	Font->ta_Name = (STRPTR)GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name;
	Font->ta_YSize = FontY = GfxBase->DefaultFont->tf_YSize;
	FontX = GfxBase->DefaultFont->tf_XSize;
	Permit();

	OffX = Scr->WBorLeft;
	OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;

	if ( width && height ) {
		if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
			goto UseTopaz;
		if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
			goto UseTopaz;
	}
	return;

UseTopaz:
	Font->ta_Name = (STRPTR)"topaz.font";
	FontX = FontY = Font->ta_YSize = 8;
}

int SetupScreen( void )
{
	if ( ! ( Scr = LockPubScreen( PubScreenName )))
		return( 1L );

	ComputeFont( 0, 0 );

	if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
		return( 2L );

	return( 0L );
}

void CloseDownScreen( void )
{
	if ( VisualInfo ) {
		FreeVisualInfo( VisualInfo );
		VisualInfo = NULL;
	}

	if ( Scr        ) {
		UnlockPubScreen( NULL, Scr );
		Scr = NULL;
	}
}

int HandlePuzzleIDCMP( void )
{
	struct IntuiMessage	*m;
	int			(*func)();
	BOOL			running = TRUE;

	while( m = GT_GetIMsg( PuzzleWnd->UserPort )) {

		CopyMem(( char * )m, ( char * )&PuzzleMsg, (long)sizeof( struct IntuiMessage ));

		GT_ReplyIMsg( m );

		switch ( PuzzleMsg.Class ) {

			case	IDCMP_REFRESHWINDOW:
				GT_BeginRefresh( PuzzleWnd );
				GT_EndRefresh( PuzzleWnd, TRUE );
				break;

			case	IDCMP_VANILLAKEY:
				running = PuzzleVanillaKey();
				break;

			case	IDCMP_GADGETUP:
			case	IDCMP_GADGETDOWN:
				func = ( void * )(( struct Gadget * )PuzzleMsg.IAddress )->UserData;
				running = func();
				break;
		}
	}
	return( running );
}

int OpenPuzzleWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		wleft = PuzzleLeft, wtop = PuzzleTop, ww, wh;

	ComputeFont( PuzzleWidth, PuzzleHeight );

	ww = ComputeX( PuzzleWidth );
	wh = ComputeY( PuzzleHeight );

	if (( wleft + ww + OffX + Scr->WBorRight ) > Scr->Width ) wleft = Scr->Width - ww;
	if (( wtop + wh + OffY + Scr->WBorBottom ) > Scr->Height ) wtop = Scr->Height - wh;

	if ( ! ( PuzzleFont = OpenDiskFont( Font )))
		return( 5L );

	if ( ! ( g = CreateContext( &PuzzleGList )))
		return( 1L );

	for( lc = 0, tc = 0; lc < Puzzle_CNT; lc++ ) {

		CopyMem((char * )&PuzzleNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));

		ng.ng_VisualInfo = VisualInfo;
		ng.ng_TextAttr   = Font;
		ng.ng_LeftEdge   = OffX + ComputeX( ng.ng_LeftEdge );
		ng.ng_TopEdge    = OffY + ComputeY( ng.ng_TopEdge );
		ng.ng_Width      = ComputeX( ng.ng_Width );
		ng.ng_Height     = ComputeY( ng.ng_Height);

		PuzzleGadgets[ lc ] = g = CreateGadgetA((ULONG)PuzzleGTypes[ lc ], g, &ng, ( struct TagItem * )&PuzzleGTags[ tc ] );

		while( PuzzleGTags[ tc ] ) tc += 2;
		tc++;

		if ( NOT g )
			return( 2L );
	}

	if ( ! ( PuzzleWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	SLIDERIDCMP|BUTTONIDCMP|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_RMBTRAP,
				WA_Gadgets,	PuzzleGList,
				WA_Title,	PuzzleWdt,
				WA_ScreenTitle,	"Puzzle Prefs",
				WA_PubScreen,	Scr,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( PuzzleWnd, NULL );

	return( 0L );
}

void ClosePuzzleWindow( void )
{
	if ( PuzzleWnd        ) {
		CloseWindow( PuzzleWnd );
		PuzzleWnd = NULL;
	}

	if ( PuzzleGList      ) {
		FreeGadgets( PuzzleGList );
		PuzzleGList = NULL;
	}

	if ( PuzzleFont ) {
		CloseFont( PuzzleFont );
		PuzzleFont = NULL;
	}
}

