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

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

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *ALoadWnd = NULL;
struct IntuiMessage    ALoadMsg;
UWORD                  ALoadZoom[4];
UWORD                  ALoadLeft = 574;
UWORD                  ALoadTop = 12;
UWORD                  ALoadWidth = 121;
UWORD                  ALoadHeight = 72;
UBYTE                 *ALoadWdt = (UBYTE *)"ALoad";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;
struct TextFont       *ALoadFont = NULL;

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

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

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

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

		GT_ReplyIMsg( m );

		switch ( ALoadMsg.Class ) {

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

			case	IDCMP_CLOSEWINDOW:
				running = ALoadCloseWindow();
				break;

				break;
		}
	}
	return( running );
}

int OpenALoadWindow( void )
{
	UWORD		wleft = ALoadLeft, wtop = ALoadTop, ww, wh;

	ComputeFont( ALoadWidth, ALoadHeight );

	ww = ComputeX( ALoadWidth );
	wh = ComputeY( ALoadHeight );

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

	ALoadZoom[0] = ALoadLeft;
	ALoadZoom[1] = ALoadTop;
	if ( ALoadWdt )
		ALoadZoom[2] = TextLength( &Scr->RastPort, (UBYTE *)ALoadWdt, strlen((char *)ALoadWdt )) + 80;
	else
		ALoadZoom[2]  = 80L;
		ALoadZoom[3] = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

	if ( ! ( ALoadWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
				WA_Title,	ALoadWdt,
				WA_ScreenTitle,	"ALoad V1.0 Copyright 1993 Stephen Norris",
				WA_PubScreen,	Scr,
				WA_Zoom,	ALoadZoom,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( ALoadWnd, NULL );

	return( 0L );
}

void CloseALoadWindow( void )
{
	if ( ALoadWnd        ) {
		CloseWindow( ALoadWnd );
		ALoadWnd = NULL;
	}

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

