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

#include <exec/types.h>
#include <intuition/intuitionbase.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 "gadtools.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *MainWnd = NULL;
struct Gadget         *MainGList = NULL;
struct Menu           *MainMenus = NULL;
struct Gadget         *MainGadgets[5];
UWORD                  MainLeft = 202;
UWORD                  MainTop = 72;
UWORD                  MainWidth = 268;
UWORD                  MainHeight = 72;
UBYTE                 *MainWdt = (UBYTE *)"HDSleep v1.4";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

struct IntuiText MainIText[] = {
	1, 0, JAM2,133, 8, NULL, (UBYTE *)" Time To Go ", NULL };

#define Main_TNUM 1

struct NewMenu MainNewMenu[] = {
	NM_TITLE, (STRPTR)"Project", NULL, 0, NULL, NULL,
	NM_ITEM, (STRPTR)"Sleep", (STRPTR)"S", 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Abort", (STRPTR)"A", 0, 0L, NULL,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"About", (STRPTR)"B", 0, 0L, NULL,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Hide", (STRPTR)"H", 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Quit", (STRPTR)"Q", 0, 0L, NULL,
	NM_END, NULL, NULL, 0, 0L, NULL };

UWORD MainGTypes[] = {
	NUMBER_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	NUMBER_KIND,
	BUTTON_KIND
};

struct NewGadget MainNGad[] = {
	147, 14, 45, 14, (UBYTE *)"Master:", NULL, GD_MASTEROFF, PLACETEXT_LEFT, NULL, NULL,
	11, 51, 79, 15, (UBYTE *)"_Sleep", NULL, GD_SLEEP, PLACETEXT_IN, NULL, NULL,
	94, 51, 79, 15, (UBYTE *)"_Abort", NULL, GD_ABORT, PLACETEXT_IN, NULL, NULL,
	147, 27, 45, 14, (UBYTE *)"Slave:", NULL, GD_SLAVEOFF, PLACETEXT_LEFT, NULL, NULL,
	177, 51, 79, 15, (UBYTE *)"_Quit", NULL, GD_QUIT, PLACETEXT_IN, NULL, NULL
};

ULONG MainGTags[] = {
	(TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(TAG_DONE),
	(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;
}

struct Screen *OldScr=NULL;

int SetupScreen( void )
{
	ULONG ibaselock;

	ibaselock=LockIBase(0);
	OldScr = IntuitionBase->FirstScreen;
	UnlockIBase(ibaselock);

	if ( ! ( Scr = LockPubScreen( PubScreenName )))
		return( 1L );

	ComputeFont( 0, 0 );

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

	return( 0L );
}

void CloseDownScreen( void )
{
	ULONG ibaselock;
	struct Screen *ScrList;

	ibaselock=LockIBase(0);
	ScrList = IntuitionBase->FirstScreen;
	while(ScrList)
	{
		if(ScrList==OldScr)
		{
			UnlockIBase(ibaselock);
			ScreenToFront(OldScr);
			OldScr=NULL;
			goto skipit;
		}
		ScrList=ScrList->NextScreen;
	}
	UnlockIBase(ibaselock);

skipit:

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

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

void MainRender( void )
{
	struct IntuiText	it;
	UWORD			cnt;

	ComputeFont( MainWidth, MainHeight );

	DrawBevelBox( MainWnd->RPort, OffX + ComputeX( 11 ),
					OffY + ComputeY( 7 ),
					ComputeX( 245 ),
					ComputeY( 40 ),
					GT_VisualInfo, VisualInfo, GTBB_Recessed, TRUE, TAG_DONE );

	for ( cnt = 0; cnt < Main_TNUM; cnt++ ) {
		CopyMem(( char * )&MainIText[ cnt ], ( char * )&it, (long)sizeof( struct IntuiText ));
		it.ITextFont = Font;
		it.LeftEdge  = OffX + ComputeX( it.LeftEdge ) - ( IntuiTextLength( &it ) >> 1 );
		it.TopEdge   = OffY + ComputeY( it.TopEdge ) - ( Font->ta_YSize >> 1 );
		PrintIText( MainWnd->RPort, &it, 0, 0 );
	}
}

int OpenMainWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	struct DrawInfo *DRI;
	UWORD		lc, tc;
	UWORD		wleft = MainLeft, wtop = MainTop, ww, wh;
	ULONG Pen;

	ComputeFont( MainWidth, MainHeight );

	ww = ComputeX( MainWidth );
	wh = ComputeY( MainHeight );

	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( &MainGList )))
		return( 1L );

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

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

		MainGadgets[ lc ] = g = CreateGadgetA((ULONG)MainGTypes[ lc ], g, &ng, ( struct TagItem * )&MainGTags[ tc ] );

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

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

	if(DRI=GetScreenDrawInfo(Scr))
	{
		Pen=DRI->dri_Pens[BARDETAILPEN];
		FreeScreenDrawInfo(Scr,DRI);
	}
	else Pen=1;

	if ( ! ( MainMenus = CreateMenus( MainNewMenu, GTMN_NewLookMenus, TRUE, GTMN_FrontPen, Pen, TAG_DONE )))
		return( 3L );

	LayoutMenus( MainMenus, VisualInfo, TAG_DONE );

	if ( ! ( MainWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	NUMBERIDCMP|BUTTONIDCMP|IDCMP_MENUPICK|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_NEWLOOKMENUS,
				WA_Gadgets,	MainGList,
				WA_Title,	MainWdt,
				WA_ScreenTitle,	"HDSleep v1.4 - by Tomasz Muszynski (Thom/Union)",
				WA_PubScreen,	Scr,
				TAG_DONE )))
	return( 4L );

	SetMenuStrip( MainWnd, MainMenus );
	GT_RefreshWindow( MainWnd, NULL );

	MainRender();

	return( 0L );
}

void CloseMainWindow( void )
{
	if ( MainMenus      ) {
		ClearMenuStrip( MainWnd );
		FreeMenus( MainMenus );
		MainMenus = NULL;	}

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

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

