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

#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 "ARemote.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = "Workbench";
APTR                   VisualInfo = NULL;
struct Window         *ARemoteWnd = NULL;
struct Gadget         *ARemoteGList = NULL;
struct Menu           *ARemoteMenus = NULL;
struct IntuiMessage    ARemoteMsg;
struct Gadget         *ARemoteGadgets[12];
UWORD                  ARemoteLeft = 119;
UWORD                  ARemoteTop = 66;
UWORD                  ARemoteWidth = 318;
UWORD                  ARemoteHeight = 189;
UBYTE                 *ARemoteWdt = (UBYTE *)"ARemote";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

struct NewMenu ARemoteNewMenu[] = {
	NM_TITLE, (STRPTR)"Project", NULL, 0, NULL, NULL,
	NM_ITEM, (STRPTR)"About", (STRPTR)"A", 0, 0L, (APTR)ARemoteAbout,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Enabled", (STRPTR)"E", CHECKIT, 0L, (APTR)ARemoteEnabled,
	NM_ITEM, (STRPTR)"Save Settings", (STRPTR)"S", 0, 0L, (APTR)ARemoteSaveSettings,
	NM_ITEM, (STRPTR)"Hide", (STRPTR)"H", 0, 0L, (APTR)ARemoteHide,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Quit", (STRPTR)"Q", 0, 0L, (APTR)ARemoteQuit,
	NM_END, NULL, NULL, 0, 0L, NULL };

UWORD ARemoteGTypes[] = {
	STRING_KIND,
	STRING_KIND,
	INTEGER_KIND,
	INTEGER_KIND,
	STRING_KIND,
	STRING_KIND,
	STRING_KIND,
	CHECKBOX_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	CHECKBOX_KIND,
	STRING_KIND
};

struct NewGadget ARemoteNGad[] = {
	98, 8, 211, 16, (UBYTE *)"C_onnect To", NULL, GD_ConnectToString, PLACETEXT_LEFT, NULL, (APTR)ConnectToStringClicked,
	104, 65, 205, 16, (UBYTE *)"_Accept From", NULL, GD_AcceptFromString, PLACETEXT_LEFT, NULL, (APTR)AcceptFromStringClicked,
	49, 29, 65, 16, (UBYTE *)"_Port", NULL, GD_ConnectToPortInt, PLACETEXT_LEFT, NULL, (APTR)ConnectToPortIntClicked,
	48, 85, 65, 16, (UBYTE *)"Po_rt", NULL, GD_AcceptFromPortInt, PLACETEXT_LEFT, NULL, (APTR)AcceptFromPortIntClicked,
	111, 131, 203, 16, (UBYTE *)"_Xmit Trigger", NULL, GD_ToggleString, PLACETEXT_LEFT, NULL, (APTR)ToggleStringClicked,
	195, 29, 114, 16, (UBYTE *)"Pas_sword", NULL, GD_ConnectToPasswordString, PLACETEXT_LEFT, NULL, (APTR)ConnectToPasswordStringClicked,
	195, 85, 114, 16, (UBYTE *)"Pass_word", NULL, GD_AcceptFromPasswordString, PLACETEXT_LEFT, NULL, (APTR)AcceptFromPasswordStringClicked,
	159, 153, 26, 11, (UBYTE *)"Co_nnect on Startup", NULL, GD_ConnectOnStartupCheckbox, PLACETEXT_LEFT, NULL, (APTR)ConnectOnStartupCheckboxClicked,
	4, 169, 149, 16, (UBYTE *)"_Connect Now", NULL, GD_ConnectButton, PLACETEXT_IN, NULL, (APTR)ConnectButtonClicked,
	160, 169, 153, 16, (UBYTE *)"_Disconnect Now", NULL, GD_DisconnectButton, PLACETEXT_IN, NULL, (APTR)DisconnectButtonClicked,
	279, 153, 26, 11, (UBYTE *)"_Flash LED", NULL, GD_XmitCueCheckbox, PLACETEXT_LEFT, NULL, (APTR)XmitCueCheckboxClicked,
	111, 111, 203, 16, (UBYTE *)"Pop_up Hotkey", NULL, GD_PopupHotkeyString, PLACETEXT_LEFT, NULL, (APTR)PopupHotkeyStringClicked
};

ULONG ARemoteGTags[] = {
	(GTST_MaxChars), 256, (GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 256, (GT_Underscore), '_', (TAG_DONE),
	(GTIN_Number), 0, (GTIN_MaxChars), 10, (GT_Underscore), '_', (TAG_DONE),
	(GTIN_Number), 0, (GTIN_MaxChars), 10, (GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 256, (GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 256, (GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 256, (GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 256, (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 )
{
	Font = &Attr;
	Font->ta_Name = (STRPTR)Scr->RastPort.Font->tf_Message.mn_Node.ln_Name;
	Font->ta_YSize = FontY = Scr->RastPort.Font->tf_YSize;
	FontX = Scr->RastPort.Font->tf_XSize;

	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 ARemoteRender( void )
{
	ComputeFont( ARemoteWidth, ARemoteHeight );

	DrawBevelBox( ARemoteWnd->RPort, OffX + ComputeX( 3 ),
					OffY + ComputeY( 58 ),
					ComputeX( 311 ),
					ComputeY( 48 ),
					GT_VisualInfo, VisualInfo, TAG_DONE );
	DrawBevelBox( ARemoteWnd->RPort, OffX + ComputeX( 3 ),
					OffY + ComputeY( 3 ),
					ComputeX( 311 ),
					ComputeY( 51 ),
					GT_VisualInfo, VisualInfo, TAG_DONE );
}

int HandleARemoteIDCMP( void )
{
	struct IntuiMessage	*m;
	struct MenuItem		*n;
	int			(*func)();
	BOOL			running = TRUE;

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

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

		GT_ReplyIMsg( m );

		switch ( ARemoteMsg.Class ) {

			case	IDCMP_REFRESHWINDOW:
				GT_BeginRefresh( ARemoteWnd );
				ARemoteRender();
				GT_EndRefresh( ARemoteWnd, TRUE );
				break;

			case	IDCMP_CLOSEWINDOW:
				running = ARemoteCloseWindow();
				break;

			case	IDCMP_VANILLAKEY:
				running = ARemoteVanillaKey();
				break;

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

			case	IDCMP_MENUPICK:
				while( ARemoteMsg.Code != MENUNULL ) {
					n = ItemAddress( ARemoteMenus, ARemoteMsg.Code );
					func = (void *)(GTMENUITEM_USERDATA( n ));
					running = func();
					ARemoteMsg.Code = n->NextSelect;
				}
				break;
		}
	}
	return( running );
}

int OpenARemoteWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		wleft = ARemoteLeft, wtop = ARemoteTop, ww, wh;

	ComputeFont( ARemoteWidth, ARemoteHeight );

	ww = ComputeX( ARemoteWidth );
	wh = ComputeY( ARemoteHeight );

	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 ( ! ( g = CreateContext( &ARemoteGList )))
		return( 1L );

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

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

		ARemoteGadgets[ lc ] = g = CreateGadgetA((ULONG)ARemoteGTypes[ lc ], g, &ng, ( struct TagItem * )&ARemoteGTags[ tc ] );

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

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

	if ( ! ( ARemoteMenus = CreateMenus( ARemoteNewMenu, GTMN_FrontPen, 0L, TAG_DONE )))
		return( 3L );

	LayoutMenus( ARemoteMenus, VisualInfo, TAG_DONE );

	if ( ! ( ARemoteWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	STRINGIDCMP|INTEGERIDCMP|CHECKBOXIDCMP|BUTTONIDCMP|IDCMP_GADGETDOWN|IDCMP_MENUPICK|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SIZEBBOTTOM|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
				WA_Gadgets,	ARemoteGList,
				WA_Title,	ARemoteWdt,
				WA_ScreenTitle,	"ARemote",
				TAG_DONE )))
	return( 4L );

	SetMenuStrip( ARemoteWnd, ARemoteMenus );
	GT_RefreshWindow( ARemoteWnd, NULL );

	ARemoteRender();

	return( 0L );
}

void CloseARemoteWindow( void )
{
	if ( ARemoteMenus      ) {
		ClearMenuStrip( ARemoteWnd );
		FreeMenus( ARemoteMenus );
		ARemoteMenus = NULL;	}

	if ( ARemoteWnd        ) {
		CloseWindow( ARemoteWnd );
		ARemoteWnd = NULL;
	}

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

