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

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

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *EasyElementsWnd = NULL;
struct Gadget         *EasyElementsGList = NULL;
struct Gadget         *EasyElementsGadgets[9];
UWORD                  EasyElementsLeft = 0;
UWORD                  EasyElementsTop = 15;
UWORD                  EasyElementsWidth = 273;
UWORD                  EasyElementsHeight = 304;
UBYTE                 *EasyElementsWdt = (UBYTE *)"$VER: EasyElements 1.1 (04-04-95)";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

UWORD EasyElementsGTypes[] = {
	STRING_KIND,
	TEXT_KIND,
	TEXT_KIND,
	TEXT_KIND,
	TEXT_KIND,
	TEXT_KIND,
	TEXT_KIND,
	TEXT_KIND,
	TEXT_KIND
};

struct NewGadget EasyElementsNGad[] = {
	111, 7, 151, 23, (UBYTE *)"Input", NULL, GD_Input, PLACETEXT_LEFT, NULL, NULL,
	111, 40, 151, 23, (UBYTE *)"Element", NULL, GD_Element, PLACETEXT_LEFT, NULL, NULL,
	111, 73, 151, 23, (UBYTE *)"Atomic Symbol", NULL, GD_Symbol, PLACETEXT_LEFT, NULL, NULL,
	111, 139, 151, 23, (UBYTE *)"Atomic Weight", NULL, GD_Weight, PLACETEXT_LEFT, NULL, NULL,
	111, 172, 151, 23, (UBYTE *)"Specific Gravity", NULL, GD_Gravity, PLACETEXT_LEFT, NULL, NULL,
	111, 205, 151, 23, (UBYTE *)"Melting Pont C°", NULL, GD_Melting, PLACETEXT_LEFT, NULL, NULL,
	111, 238, 151, 23, (UBYTE *)"Boiling Point C°", NULL, GD_Boiling, PLACETEXT_LEFT, NULL, NULL,
	111, 271, 151, 23, (UBYTE *)"Discovered", NULL, GD_Discovered, PLACETEXT_LEFT, NULL, NULL,
	111, 106, 151, 23, (UBYTE *)"Atomic Number", NULL, GD_Number, PLACETEXT_LEFT, NULL, NULL
};

ULONG EasyElementsGTags[] = {
	(GTST_MaxChars), 256, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE)
};

static UWORD ComputeX( UWORD value )
{
	return(( UWORD )((( FontX * value ) + 3 ) / 6 ));
}

static UWORD ComputeY( UWORD value )
{
	return(( UWORD )((( FontY * value ) + 5 ) / 11 ));
}

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

int OpenEasyElementsWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		wleft = EasyElementsLeft, wtop = EasyElementsTop, ww, wh;

	ComputeFont( EasyElementsWidth, EasyElementsHeight );

	ww = ComputeX( EasyElementsWidth );
	wh = ComputeY( EasyElementsHeight );

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

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

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

		EasyElementsGadgets[ lc ] = g = CreateGadgetA((ULONG)EasyElementsGTypes[ lc ], g, &ng, ( struct TagItem * )&EasyElementsGTags[ tc ] );

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

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

	if ( ! ( EasyElementsWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	STRINGIDCMP|TEXTIDCMP|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
				WA_Gadgets,	EasyElementsGList,
				WA_Title,	EasyElementsWdt,
				WA_ScreenTitle,	"EasyElements 1.1 (04-04-95) Copyright Christian Kemp.  All Rights Reserved.",
				WA_PubScreen,	Scr,
				WA_AutoAdjust,	TRUE,
				WA_PubScreenFallBack,	TRUE,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( EasyElementsWnd, NULL );

	return( 0L );
}

void CloseEasyElementsWindow( void )
{
	if ( EasyElementsWnd        ) {
		CloseWindow( EasyElementsWnd );
		EasyElementsWnd = NULL;
	}

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

