/*
 *  Source machine generated by GadToolsBox V2.0b
 *  which is (c) Copyright 1991-1993 Jaba Development
 *
 *  GUI Designed by : Hans "Tim Cane" Buehler, Codex Design
 */

#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 <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/utility_pragmas.h>

#include "PalisViewGUI.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = "Workbench";
APTR                   VisualInfo = NULL;
struct Window         *MainWnd = NULL;
struct Gadget         *MainGList = NULL;
struct IntuiMessage    MainMsg;
UWORD                  MainZoom[4];
struct Gadget         *MainGadgets[5];
UWORD                  MainLeft = 20;
UWORD                  MainTop = 20;
UWORD                  MainWidth = 414;
UWORD                  MainHeight = 171;
UBYTE                 *MainWdt = (UBYTE *)"x";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;
struct TextFont       *MainFont = NULL;

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

struct NewGadget MainNGad[] = {
	15, 18, 382, 136, (UBYTE *)"Current patches:", NULL, GD_GadList, PLACETEXT_ABOVE|NG_HIGHLABEL, NULL, (APTR)GadListClicked,
	127, 154, 78, 13, (UBYTE *)"Update", NULL, GD_GadUpDate, PLACETEXT_IN, NULL, (APTR)GadUpDateClicked,
	208, 154, 78, 13, (UBYTE *)"About", NULL, GD_GadAbout, PLACETEXT_IN, NULL, (APTR)GadAboutClicked,
	310, 154, 78, 13, (UBYTE *)"Close", NULL, GD_GadClose, PLACETEXT_IN, NULL, (APTR)GadCloseClicked,
	25, 154, 78, 13, (UBYTE *)"Hide", NULL, GD_GadHide, PLACETEXT_IN, NULL, (APTR)GadHideClicked
};

ULONG MainGTags[] = {
	(GTLV_ScrollWidth), 18, (GTLV_ReadOnly), TRUE, (TAG_DONE),
	(TAG_DONE),
	(TAG_DONE),
	(TAG_DONE),
	(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;
	}
}

int HandleMainIDCMP( void )
{
	struct IntuiMessage	*m;
	int			(*func)();
	BOOL			running = TRUE;

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

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

		GT_ReplyIMsg( m );

		switch ( MainMsg.Class ) {

			case	IDCMP_REFRESHWINDOW:
				GT_BeginRefresh( MainWnd );
				GT_EndRefresh( MainWnd, TRUE );
				break;

			case	IDCMP_CLOSEWINDOW:
				running = MainCloseWindow();
				break;

			case	IDCMP_GADGETUP:
			case	IDCMP_GADGETDOWN:
				func = ( void * )(( struct Gadget * )MainMsg.IAddress )->UserData;
				running = func();
				break;
		}
	}
	return( running );
}

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 ( ! ( MainFont = OpenDiskFont( Font )))
		return( 5L );

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

	MainZoom[0] = MainLeft;
	MainZoom[1] = MainTop;
	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|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_RMBTRAP,
				WA_Gadgets,	MainGList,
				WA_Title,	MainWdt,
				WA_ScreenTitle,	"The PatchLib-Solution PALIS (c)1995 by Hans Bühler: Codex Design Software",
				WA_Zoom,	MainZoom,
				WA_AutoAdjust,	TRUE,
				WA_PubScreenFallBack,	TRUE,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( MainWnd, NULL );

	return( 0L );
}

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

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

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

