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

#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 <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/utility_pragmas.h>
#include <pragmas/diskfont_pragmas.h>

#include "Control.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *ControlWnd = NULL;
struct Gadget         *ControlGList = NULL;
struct IntuiMessage    ControlMsg;
struct Gadget         *ControlGadgets[10];
UWORD                  ControlLeft = 150;
UWORD                  ControlTop = 41;
UWORD                  ControlWidth = 298;
UWORD                  ControlHeight = 91;
UBYTE                 *ControlWdt = (UBYTE *)"Player Control";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;
struct TextFont       *ControlFont = NULL;

UWORD ControlGTypes[] = {
	TEXT_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	NUMBER_KIND,
	NUMBER_KIND,
	CHECKBOX_KIND
};

struct NewGadget ControlNGad[] = {
	4, 13, 289, 13, (UBYTE *)"Module", NULL, GD_Module, PLACETEXT_ABOVE, NULL, NULL,
	12, 49, 81, 17, (UBYTE *)"_Play", NULL, GD_Play, PLACETEXT_IN, NULL, (APTR)PlayClicked,
	204, 49, 81, 17, (UBYTE *)"_Stop", NULL, GD_Stop, PLACETEXT_IN, NULL, (APTR)StopClicked,
	108, 69, 81, 17, (UBYTE *)"_Quit", NULL, GD_Quit, PLACETEXT_IN, NULL, (APTR)QuitClicked,
	108, 49, 81, 17, (UBYTE *)"P_ause", NULL, GD_Pause, PLACETEXT_IN, NULL, (APTR)PauseClicked,
	12, 69, 81, 17, (UBYTE *)"_Load", NULL, GD_Load, PLACETEXT_IN, NULL, (APTR)LoadClicked,
	204, 69, 81, 17, (UBYTE *)"_Fade", NULL, GD_Fade, PLACETEXT_IN, NULL, (APTR)FadeClicked,
	60, 29, 33, 13, (UBYTE *)"Length", NULL, GD_Length, PLACETEXT_LEFT, NULL, NULL,
	152, 29, 33, 13, (UBYTE *)"Pos", NULL, GD_Pos, PLACETEXT_LEFT, NULL, NULL,
	267, 30, 26, 11, (UBYTE *)"Loop", NULL, GD_Loop, PLACETEXT_LEFT, NULL, (APTR)LoopClicked
};

ULONG ControlGTags[] = {
	(GTTX_Text), (ULONG)"None", (GTTX_Border), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTNM_Border), TRUE, (TAG_DONE),
	(GTNM_Border), TRUE, (TAG_DONE),
	(GTCB_Checked), TRUE, (GT_Underscore), '_', (TAG_DONE)
};

static UWORD ComputeX( UWORD value )
{
	return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
}

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

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;
	}
}

void ControlRender( void )
{
	ComputeFont( ControlWidth, ControlHeight );

	DrawBevelBox( ControlWnd->RPort, OffX + ComputeX( 4 ),
					OffY + ComputeY( 45 ),
					ComputeX( 289 ),
					ComputeY( 44 ),
					GT_VisualInfo, VisualInfo, GTBB_Recessed, TRUE, TAG_DONE );
}

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

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

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

		GT_ReplyIMsg( m );

		switch ( ControlMsg.Class ) {

			case	IDCMP_REFRESHWINDOW:
				GT_BeginRefresh( ControlWnd );
				ControlRender();
				GT_EndRefresh( ControlWnd, TRUE );
				break;

			case	IDCMP_CLOSEWINDOW:
				running = ControlCloseWindow();
				break;

			case	IDCMP_VANILLAKEY:
				running = ControlVanillaKey();
				break;

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

int OpenControlWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		wleft = ControlLeft, wtop = ControlTop, ww, wh;

	ComputeFont( ControlWidth, ControlHeight );

	ww = ComputeX( ControlWidth );
	wh = ComputeY( ControlHeight );

	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 ( ! ( ControlFont = OpenDiskFont( Font )))
		return( 5L );

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

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

		CopyMem((char * )&ControlNGad[ 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);

		ControlGadgets[ lc ] = g = CreateGadgetA((ULONG)ControlGTypes[ lc ], g, &ng, ( struct TagItem * )&ControlGTags[ tc ] );

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

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

	if ( ! ( ControlWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	TEXTIDCMP|BUTTONIDCMP|NUMBERIDCMP|CHECKBOXIDCMP|IDCMP_GADGETDOWN|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
				WA_Gadgets,	ControlGList,
				WA_Title,	ControlWdt,
				WA_ScreenTitle,	"PT-Player 2.0  © 1994 BetaSoft",
				WA_PubScreen,	Scr,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( ControlWnd, NULL );

	ControlRender();

	return( 0L );
}

void CloseControlWindow( void )
{
	if ( ControlWnd        ) {
		CloseWindow( ControlWnd );
		ControlWnd = NULL;
	}

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

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

