/*
 *  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 "Blanker.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *BlankerWnd = NULL;
struct Gadget         *BlankerGList = NULL;
struct Menu           *BlankerMenus = NULL;
struct IntuiMessage    BlankerMsg;
struct Gadget         *BlankerGadgets[13];
UWORD                  BlankerLeft = 0;
UWORD                  BlankerTop = 12;
UWORD                  BlankerWidth = 412;
UWORD                  BlankerHeight = 93;
UBYTE                 *BlankerWdt = (UBYTE *)"Blanker";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;
struct TextFont       *BlankerFont = NULL;

UBYTE *MEMRES0Labels[] = {
	(UBYTE *)"Load on request",
	(UBYTE *)"Memory resident",
	NULL };

struct NewMenu BlankerNewMenu[] = {
	NM_TITLE, (STRPTR)"Project", NULL, 0, NULL, NULL,
	NM_ITEM, (STRPTR)"Open...", (STRPTR)"O", 0, 0L, (APTR)BlankerOpen,
	NM_ITEM, (STRPTR)"Save as...", NULL, 0, 0L, (APTR)BlankerSaveAs,
	NM_ITEM, (STRPTR)"Load module...", (STRPTR)"L", 0, 0L, (APTR)BlankerLoadMod,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"About...", (STRPTR)"?", 0, 0L, (APTR)BlankerAbout,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Quit", (STRPTR)"Q", 0, 0L, (APTR)BlankerQuit,
	NM_TITLE, (STRPTR)"Edit", NULL, 0, NULL, NULL,
	NM_ITEM, (STRPTR)"Last Saved", NULL, 0, 0L, (APTR)BlankerLSaved,
	NM_END, NULL, NULL, 0, 0L, NULL };

UWORD BlankerGTypes[] = {
	INTEGER_KIND,
	STRING_KIND,
	STRING_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	TEXT_KIND,
	NUMBER_KIND,
	BUTTON_KIND,
	TEXT_KIND,
	BUTTON_KIND,
	CYCLE_KIND
};

struct NewGadget BlankerNGad[] = {
	9, 42, 101, 13, (UBYTE *)"_Timeout", NULL, GD_TOUT, PLACETEXT_RIGHT, NULL, (APTR)TOUTClicked,
	9, 59, 101, 13, (UBYTE *)"_Blank Key", NULL, GD_BKEY, PLACETEXT_RIGHT, NULL, (APTR)BKEYClicked,
	9, 76, 101, 13, (UBYTE *)"_Pop Key", NULL, GD_PKEY, PLACETEXT_RIGHT, NULL, (APTR)PKEYClicked,
	303, 59, 101, 13, (UBYTE *)"_Hide", NULL, GD_HIDE, PLACETEXT_IN, NULL, (APTR)HIDEClicked,
	197, 76, 101, 13, (UBYTE *)"_Save", NULL, GD_SAVE, PLACETEXT_IN, NULL, (APTR)SAVEClicked,
	303, 76, 101, 13, (UBYTE *)"_Quit", NULL, GD_QUIT, PLACETEXT_IN, NULL, (APTR)QUITClicked,
	9, 6, 101, 13, (UBYTE *)"S_creen...", NULL, GD_SMODE, PLACETEXT_IN, NULL, (APTR)SMODEClicked,
	118, 6, 240, 13, NULL, NULL, GD_MDISPLAY, 0, NULL, NULL,
	361, 6, 39, 13, NULL, NULL, GD_SDEP, 0, NULL, NULL,
	9, 25, 101, 13, (UBYTE *)"P_refs...", NULL, GD_BMOD, PLACETEXT_IN, NULL, (APTR)BMODClicked,
	118, 25, 282, 13, NULL, NULL, GD_MODINFO, 0, NULL, NULL,
	197, 59, 101, 13, (UBYTE *)"_Load...", NULL, GD_LMOD, PLACETEXT_IN, NULL, (APTR)LMODClicked,
	197, 42, 207, 13, NULL, NULL, GD_MEMRES, 0, NULL, (APTR)MEMRESClicked
};

ULONG BlankerGTags[] = {
	(GTIN_Number), 0, (GTIN_MaxChars), 10, (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),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTNM_Number), 256, (GTNM_Border), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTCY_Labels), (ULONG)&MEMRES0Labels[ 0 ], (GTCY_Active), 1, (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 BlankerRender( void )
{
	ComputeFont( BlankerWidth, BlankerHeight );

	DrawBevelBox( BlankerWnd->RPort, OffX + ComputeX( 114 ),
					OffY + ComputeY( 4 ),
					ComputeX( 290 ),
					ComputeY( 17 ),
					GT_VisualInfo, VisualInfo, TAG_DONE );
	DrawBevelBox( BlankerWnd->RPort, OffX + ComputeX( 114 ),
					OffY + ComputeY( 23 ),
					ComputeX( 290 ),
					ComputeY( 17 ),
					GT_VisualInfo, VisualInfo, TAG_DONE );
}

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

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

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

		GT_ReplyIMsg( m );

		switch ( BlankerMsg.Class ) {

			case	IDCMP_REFRESHWINDOW:
				GT_BeginRefresh( BlankerWnd );
				BlankerRender();
				GT_EndRefresh( BlankerWnd, TRUE );
				break;

			case	IDCMP_CLOSEWINDOW:
				running = BlankerCloseWindow();
				break;

			case	IDCMP_VANILLAKEY:
				running = BlankerVanillaKey();
				break;

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

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

int OpenBlankerWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		wleft = BlankerLeft, wtop = BlankerTop, ww, wh;

	ComputeFont( BlankerWidth, BlankerHeight );

	ww = ComputeX( BlankerWidth );
	wh = ComputeY( BlankerHeight );

	wleft = ( Scr->Width - ww )/2 - Scr->WBorRight;
	wtop = ( Scr->Height - wh - 2*Scr->WBorBottom - Font->ta_YSize )/2;

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

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

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

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

		BlankerGadgets[ lc ] = g = CreateGadgetA((ULONG)BlankerGTypes[ lc ], g, &ng, ( struct TagItem * )&BlankerGTags[ tc ] );

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

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

	if ( ! ( BlankerMenus = CreateMenus( BlankerNewMenu, GTMN_FrontPen, 0L, TAG_DONE )))
		return( 3L );

	LayoutMenus( BlankerMenus, VisualInfo, TAG_DONE );

	if ( ! ( BlankerWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	INTEGERIDCMP|STRINGIDCMP|BUTTONIDCMP|TEXTIDCMP|NUMBERIDCMP|CYCLEIDCMP|IDCMP_MENUPICK|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
				WA_Gadgets,	BlankerGList,
				WA_Title,	BlankerWdt,
				WA_ScreenTitle,	"Blanker",
				WA_PubScreen,	Scr,
				TAG_DONE )))
	return( 4L );

	SetMenuStrip( BlankerWnd, BlankerMenus );
	GT_RefreshWindow( BlankerWnd, NULL );

	BlankerRender();

	return( 0L );
}

void CloseBlankerWindow( void )
{
	if ( BlankerMenus      ) {
		ClearMenuStrip( BlankerWnd );
		FreeMenus( BlankerMenus );
		BlankerMenus = NULL;	}

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

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

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

