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

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

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = "Workbench";
APTR                   VisualInfo = NULL;
struct Window         *MainWnd = NULL;
struct Gadget         *MainGList = NULL;
struct IntuiMessage    MainMsg;
struct Gadget         *MainGadgets[20];
UWORD                  MainLeft = 0;
UWORD                  MainTop = 0;
UWORD                  MainWidth = 632;
UWORD                  MainHeight = 187;
UBYTE                 *MainWdt = (UBYTE *)"Reminder V1.10 © 1993 Matti Rintala";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;
struct TextFont       *MainFont = NULL;

UBYTE *ARexxMode0Labels[] = {
	(UBYTE *)"Not_hing",
	(UBYTE *)"Scr_ipt",
	(UBYTE *)"_Command",
	NULL };

extern struct MinList Weekday0List;

struct Node Weekday0Nodes[] = {
	&Weekday0Nodes[1], ( struct Node * )&Weekday0List.mlh_Head, 0, 0, "Monday",
	&Weekday0Nodes[2], &Weekday0Nodes[0], 0, 0, "Tuesday",
	&Weekday0Nodes[3], &Weekday0Nodes[1], 0, 0, "Wednesday",
	&Weekday0Nodes[4], &Weekday0Nodes[2], 0, 0, "Thursday",
	&Weekday0Nodes[5], &Weekday0Nodes[3], 0, 0, "Friday",
	&Weekday0Nodes[6], &Weekday0Nodes[4], 0, 0, "Saturday",
	&Weekday0Nodes[7], &Weekday0Nodes[5], 0, 0, "Sunday",
	( struct Node * )&Weekday0List.mlh_Tail, &Weekday0Nodes[6], 0, 0, "ANY" };

struct MinList Weekday0List = {
	( struct MinNode * )&Weekday0Nodes[0], ( struct MinNode * )NULL, ( struct MinNode * )&Weekday0Nodes[7] };

extern struct MinList MonthList0List;

struct Node MonthList0Nodes[] = {
	&MonthList0Nodes[1], ( struct Node * )&MonthList0List.mlh_Head, 0, 0, "January",
	&MonthList0Nodes[2], &MonthList0Nodes[0], 0, 0, "February",
	&MonthList0Nodes[3], &MonthList0Nodes[1], 0, 0, "March",
	&MonthList0Nodes[4], &MonthList0Nodes[2], 0, 0, "April",
	&MonthList0Nodes[5], &MonthList0Nodes[3], 0, 0, "May",
	&MonthList0Nodes[6], &MonthList0Nodes[4], 0, 0, "June",
	&MonthList0Nodes[7], &MonthList0Nodes[5], 0, 0, "July",
	&MonthList0Nodes[8], &MonthList0Nodes[6], 0, 0, "August",
	&MonthList0Nodes[9], &MonthList0Nodes[7], 0, 0, "September",
	&MonthList0Nodes[10], &MonthList0Nodes[8], 0, 0, "October",
	&MonthList0Nodes[11], &MonthList0Nodes[9], 0, 0, "November",
	&MonthList0Nodes[12], &MonthList0Nodes[10], 0, 0, "December",
	( struct Node * )&MonthList0List.mlh_Tail, &MonthList0Nodes[11], 0, 0, "ANY" };

struct MinList MonthList0List = {
	( struct MinNode * )&MonthList0Nodes[0], ( struct MinNode * )NULL, ( struct MinNode * )&MonthList0Nodes[12] };

struct IntuiText MainIText[] = {
	2, 0, JAM1,451, 53, NULL, (UBYTE *)"Remind # days before and after:", NULL,
	2, 0, JAM1,451, 94, NULL, (UBYTE *)"Date of event:", NULL,
	2, 0, JAM1,425, 10, NULL, (UBYTE *)"ARexx:", NULL };

#define Main_TNUM 3

UWORD MainGTypes[] = {
	LISTVIEW_KIND,
	CHECKBOX_KIND,
	LISTVIEW_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	TEXT_KIND,
	MX_KIND,
	BUTTON_KIND,
	STRING_KIND,
	LISTVIEW_KIND,
	INTEGER_KIND,
	INTEGER_KIND,
	INTEGER_KIND,
	STRING_KIND,
	STRING_KIND,
	STRING_KIND,
	INTEGER_KIND
};

struct NewGadget MainNGad[] = {
	286, 99, 105, 56, (UBYTE *)"_Weekday", NULL, GD_Weekday, PLACETEXT_BELOW, NULL, (APTR)WeekdayClicked,
	559, 73, 26, 11, (UBYTE *)"D_elete event when acknowledged", NULL, GD_Autodelete, PLACETEXT_LEFT, NULL, (APTR)AutodeleteClicked,
	8, 15, 264, 112, (UBYTE *)"E_vents in memory:", NULL, GD_Eventlist, PLACETEXT_ABOVE|NG_HIGHLABEL, NULL, (APTR)EventlistClicked,
	76, 130, 60, 13, (UBYTE *)"_Add", NULL, GD_Add, PLACETEXT_IN, NULL, (APTR)AddClicked,
	144, 130, 60, 13, (UBYTE *)"_Remove", NULL, GD_Remove, PLACETEXT_IN, NULL, (APTR)RemoveClicked,
	212, 130, 60, 13, (UBYTE *)"_Update", NULL, GD_Update, PLACETEXT_IN, NULL, (APTR)UpdateClicked,
	16, 151, 117, 13, (UBYTE *)"_Quit, no save", NULL, GD_Cancel, PLACETEXT_IN, NULL, (APTR)CancelClicked,
	148, 151, 117, 13, (UBYTE *)"Quit and _save", NULL, GD_Quit, PLACETEXT_IN, NULL, (APTR)QuitClicked,
	395, 127, 41, 13, NULL, NULL, GD_ShowWDay, 0, NULL, NULL,
	288, 11, 17, 9, NULL, NULL, GD_ARexxMode, PLACETEXT_RIGHT, NULL, (APTR)ARexxModeClicked,
	8, 130, 60, 13, (UBYTE *)"_New", NULL, GD_New, PLACETEXT_IN, NULL, (APTR)NewClicked,
	440, 139, 105, 13, (UBYTE *)"_Month", NULL, GD_Month, PLACETEXT_BELOW, NULL, (APTR)MonthClicked,
	440, 98, 105, 56, NULL, NULL, GD_MonthList, 0, NULL, (APTR)MonthListClicked,
	549, 142, 60, 13, (UBYTE *)"_Year", NULL, GD_Year, PLACETEXT_BELOW, NULL, (APTR)YearClicked,
	367, 58, 54, 13, (UBYTE *)"_Before", NULL, GD_Before, PLACETEXT_LEFT, NULL, (APTR)BeforeClicked,
	531, 58, 54, 13, (UBYTE *)"A_fter", NULL, GD_After, PLACETEXT_LEFT, NULL, (APTR)AfterClicked,
	50, 170, 569, 13, (UBYTE *)"_Text", NULL, GD_Text, PLACETEXT_LEFT, NULL, (APTR)TextClicked,
	381, 21, 231, 12, (UBYTE *)"C_omm/Script", NULL, GD_ARexxCom, PLACETEXT_BELOW, NULL, (APTR)ARexxComClicked,
	512, 7, 100, 12, (UBYTE *)"_Port", NULL, GD_ARexxPort, PLACETEXT_LEFT, NULL, (APTR)ARexxPortClicked,
	396, 142, 41, 13, (UBYTE *)"_Day", NULL, GD_Day, PLACETEXT_BELOW, NULL, (APTR)DayClicked
};

ULONG MainGTags[] = {
	(GTLV_Labels), (ULONG)&Weekday0List, (GTLV_ShowSelected), NULL, (GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTLV_ShowSelected), NULL, (GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTTX_Border), TRUE, (TAG_DONE),
	(GTMX_Labels), (ULONG)&ARexxMode0Labels[ 0 ], (GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTST_String), (ULONG)"January", (GTST_MaxChars), 9, (GT_Underscore), '_', (TAG_DONE),
	(GTLV_Labels), (ULONG)&MonthList0List, (GTLV_ShowSelected), 1L, (TAG_DONE),
	(GTIN_Number), 0, (GTIN_MaxChars), 4, (GT_Underscore), '_', (TAG_DONE),
	(GTIN_Number), 0, (GTIN_MaxChars), 3, (GT_Underscore), '_', (TAG_DONE),
	(GTIN_Number), 0, (GTIN_MaxChars), 3, (GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 90, (STRINGA_Justification), (GACT_STRINGCENTER), (GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 28, (GT_Underscore), '_', (TAG_DONE),
	(GTST_MaxChars), 10, (GT_Underscore), '_', (TAG_DONE),
	(GTIN_Number), 0, (GTIN_MaxChars), 2, (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 )
{
	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;
	}
}

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

	ComputeFont( MainWidth, MainHeight );

	DrawBevelBox( MainWnd->RPort, OffX + ComputeX( 9 ),
					OffY + ComputeY( 147 ),
					ComputeX( 264 ),
					ComputeY( 21 ),
					GT_VisualInfo, VisualInfo, GTBB_Recessed, TRUE, TAG_DONE );
	DrawBevelBox( MainWnd->RPort, OffX + ComputeX( 278 ),
					OffY + ComputeY( 4 ),
					ComputeX( 341 ),
					ComputeY( 41 ),
					GT_VisualInfo, VisualInfo, TAG_DONE );
	DrawBevelBox( MainWnd->RPort, OffX + ComputeX( 278 ),
					OffY + ComputeY( 47 ),
					ComputeX( 341 ),
					ComputeY( 39 ),
					GT_VisualInfo, VisualInfo, TAG_DONE );
	DrawBevelBox( MainWnd->RPort, OffX + ComputeX( 278 ),
					OffY + ComputeY( 88 ),
					ComputeX( 341 ),
					ComputeY( 80 ),
					GT_VisualInfo, VisualInfo, 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 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 );
				MainRender();
				GT_EndRefresh( MainWnd, TRUE );
				break;

			case	IDCMP_CLOSEWINDOW:
				running = MainCloseWindow();
				break;

			case	IDCMP_VANILLAKEY:
				running = MainVanillaKey();
				break;

			case	IDCMP_RAWKEY:
				running = MainRawKey();
				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;
	struct TagItem	*tmp;
	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);

		if ( MainGTypes[ lc ] == LISTVIEW_KIND ) {
			if ( tmp = FindTagItem( GTLV_ShowSelected, ( struct TagItem * )&MainGTags[ tc ] )) {
				if ( tmp->ti_Data ) tmp->ti_Data = (ULONG)g;
			}
		}

		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 ( ! ( MainWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	LISTVIEWIDCMP|CHECKBOXIDCMP|BUTTONIDCMP|TEXTIDCMP|MXIDCMP|STRINGIDCMP|INTEGERIDCMP|IDCMP_CLOSEWINDOW|IDCMP_RAWKEY|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
				WA_Gadgets,	MainGList,
				WA_Title,	MainWdt,
				WA_ScreenTitle,	"Reminder V1.10 © 1993 Matti Rintala",
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( MainWnd, NULL );

	MainRender();

	return( 0L );
}

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

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

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

