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

#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 <pragmas/exec_lib.h>
#include <pragmas/intuition_lib.h>
#include <pragmas/gadtools_lib.h>
#include <pragmas/graphics_lib.h>
#include <pragmas/utility_lib.h>

#include "gadgets.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *MainWnd = NULL;
struct Gadget         *MainGList = NULL;
struct Menu           *MainMenus = NULL;
UWORD                  MainZoom[4];
struct Gadget         *MainGadgets[8];
UWORD                  MainLeft = 0;
UWORD                  MainTop = 12;
UWORD                  MainWidth = 553;
UWORD                  MainHeight = 206;
UBYTE                 *MainWdt = (UBYTE *)"Assign Preferences V1.02";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

UBYTE *ap_AssType0Labels[] = {
	(UBYTE *)"Locked",
	(UBYTE *)"Path",
	(UBYTE *)"Add",
	(UBYTE *)"Late",
	NULL };

struct NewMenu MainNewMenu[] = {
	NM_TITLE, (STRPTR)"Project", NULL, 0, NULL, NULL,
	NM_ITEM, (STRPTR)"Open", (STRPTR)"O", 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Append", NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Save As...", (STRPTR)"A", 0, 0L, NULL,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"About", (STRPTR)"?", 0, 0L, NULL,
	NM_ITEM, (STRPTR)NM_BARLABEL, NULL, 0, 0L, NULL,
	NM_ITEM, (STRPTR)"Quit", (STRPTR)"Q", 0, 0L, NULL,
	NM_TITLE, (STRPTR)"Edit", NULL, 0, NULL, NULL,
	NM_ITEM, (STRPTR)"Last Saved", (STRPTR)"L", 0, 0L, NULL,
	NM_TITLE, (STRPTR)"Settings", NULL, 0, NULL, NULL,
	NM_ITEM, (STRPTR)"Save Icons?", (STRPTR)"I", CHECKIT|CHECKED|MENUTOGGLE, 0L, NULL,
	NM_END, NULL, NULL, 0, 0L, NULL };

UWORD MainGTypes[] = {
	LISTVIEW_KIND,
	STRING_KIND,
	STRING_KIND,
	CYCLE_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND
};

struct NewGadget MainNGad[] = {
	4, 1, 545, 144, NULL, NULL, GD_ap_List, 0, NULL, NULL,
	102, 159, 153, 17, (UBYTE *)"_Name", NULL, GD_ap_AssName, PLACETEXT_ABOVE, NULL, NULL,
	259, 159, 153, 17, (UBYTE *)"_Path", NULL, GD_ap_Path, PLACETEXT_ABOVE, NULL, NULL,
	416, 159, 133, 17, (UBYTE *)"_Type", NULL, GD_ap_AssType, PLACETEXT_ABOVE, NULL, NULL,
	4, 159, 45, 17, (UBYTE *)"_Add", NULL, GD_ap_Add, PLACETEXT_IN, NULL, NULL,
	53, 159, 45, 17, (UBYTE *)"_Del", NULL, GD_ap_Del, PLACETEXT_IN, NULL, NULL,
	4, 181, 269, 17, (UBYTE *)"_Save", NULL, GD_ap_Save, PLACETEXT_IN, NULL, NULL,
	280, 181, 269, 17, (UBYTE *)"_Quit", NULL, GD_ap_Cancel, PLACETEXT_IN, NULL, NULL
};

ULONG *MainGTags[] = {
	(ULONG *)(TAG_DONE),
	(ULONG *)(GTST_MaxChars), (ULONG *)256, (ULONG *)(STRINGA_Justification), (ULONG *)(GACT_STRINGCENTER), (ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(TAG_DONE),
	(ULONG *)(GTST_MaxChars), (ULONG *)256, (ULONG *)(STRINGA_Justification), (ULONG *)(GACT_STRINGCENTER), (ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(TAG_DONE),
	(ULONG * )(GTCY_Labels), (ULONG *)&ap_AssType0Labels[ 0 ], (ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(TAG_DONE),
	(ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(TAG_DONE),
	(ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(TAG_DONE),
	(ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(TAG_DONE),
	(ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(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;
	}
}

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

	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 ( ! ( MainMenus = CreateMenus( MainNewMenu, GTMN_FrontPen, 0L, TAG_DONE )))
		return( 3L );

	LayoutMenus( MainMenus, VisualInfo, TAG_DONE );

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

	if ( ! ( MainWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	LISTVIEWIDCMP|STRINGIDCMP|CYCLEIDCMP|BUTTONIDCMP|IDCMP_SIZEVERIFY|IDCMP_MENUPICK|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
				WA_Gadgets,	MainGList,
				WA_Title,	MainWdt,
				WA_ScreenTitle,	"Assign Preferences V1.02 © 1993 DIgital DImensions. Written by Thomas Frieden",
				WA_PubScreen,	Scr,
				WA_Zoom,	MainZoom,
				TAG_DONE )))
	return( 4L );

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

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

