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

#include "Finder_GUI.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *FinderWnd = NULL;
struct Gadget         *FinderGList = NULL;
struct IntuiMessage    FinderMsg;
UWORD                  FinderZoom[4];
struct Gadget         *FinderGadgets[7];
UWORD                  FinderLeft = 120;
UWORD                  FinderTop = 104;
UWORD                  FinderWidth = 397;
UWORD                  FinderHeight = 300;
UBYTE                 *FinderWdt = (UBYTE *)"Finder V2.06";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

UWORD FinderGTypes[] = {
	LISTVIEW_KIND,
	STRING_KIND,
	STRING_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND
};

struct NewGadget FinderNGad[] = {
	5, 18, 386, 231, (UBYTE *)"F_iles found:", NULL, GD_filelister, PLACETEXT_ABOVE|NG_HIGHLABEL, NULL, NULL,
	200, 252, 191, 19, (UBYTE *)"_Search Path", NULL, GD_path, PLACETEXT_LEFT|NG_HIGHLABEL, NULL, NULL,
	200, 275, 191, 19, (UBYTE *)"_Pattern", NULL, GD_pattern, PLACETEXT_LEFT|NG_HIGHLABEL, NULL, NULL,
	9, 252, 58, 19, (UBYTE *)"_Find", NULL, GD_find, PLACETEXT_IN, NULL, NULL,
	68, 252, 58, 19, (UBYTE *)"_Quit", NULL, GD_quit, PLACETEXT_IN, NULL, NULL,
	9, 275, 58, 19, (UBYTE *)"_Help", NULL, GD_help, PLACETEXT_IN, NULL, NULL,
	68, 275, 58, 19, (UBYTE *)"_About", NULL, GD_about, PLACETEXT_IN, NULL, NULL
};

ULONG FinderGTags[] = {
	(GTLV_ShowSelected), NULL, (GTLV_ScrollWidth), 17, (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)
};

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 OpenFinderWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		wleft = FinderLeft, wtop = FinderTop, ww, wh;

	ComputeFont( FinderWidth, FinderHeight );

	ww = ComputeX( FinderWidth );
	wh = ComputeY( FinderHeight );

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

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

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

		FinderGadgets[ lc ] = g = CreateGadgetA((ULONG)FinderGTypes[ lc ], g, &ng, ( struct TagItem * )&FinderGTags[ tc ] );

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

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

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

	if ( ! ( FinderWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	LISTVIEWIDCMP|STRINGIDCMP|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_RAWKEY|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_RMBTRAP,
				WA_Gadgets,	FinderGList,
				WA_Title,	FinderWdt,
				WA_ScreenTitle,	"Finder V2.06 by Chris Jones ©1996 Dynamic Software. All rights reserved.",
				WA_PubScreen,	Scr,
				WA_Zoom,	FinderZoom,
				WA_AutoAdjust,	TRUE,
				WA_PubScreenFallBack,	TRUE,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( FinderWnd, NULL );

	return( 0L );
}

void CloseFinderWindow( void )
{
	if ( FinderWnd        ) {
		CloseWindow( FinderWnd );
		FinderWnd = NULL;
	}

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

