/*
    C source code created by Interface Editor
    Copyright © 1994-1996 by Simone Tellini

    Generator:  C.generator 37.0 (16.1.96)

    Copy registered to :  Simone Tellini
    Serial Number      : #0
*/


/*
    NOTE:   this has been generated by the C.generator, but then
	    I've modified it a little bit by hand, since the generator
	    is a shared library and thus cannot use global variables.
*/


#include <exec/types.h>
#include <exec/nodes.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <ctype.h>
#include <string.h>

#include "DEV_IE:Include/IEditor.h"
#include "DEV_IE:Generators/C/Config.h"

UWORD ConfGTypes[] = {
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	STRING_KIND,
	CHECKBOX_KIND,
	NULL };

struct TextAttr topaz8_065 = {
	(STRPTR)"topaz.font", 8, 0x0, 0x41 };

struct NewGadget ConfNGad[] = {
	122, 3, 26, 11, "Intui_Message", NULL, GD_Msg, NULL, NULL, (APTR)MsgClicked,
	274, 3, 26, 11, "Clicked _Ptr", NULL, GD_Click, NULL, NULL, (APTR)ClickClicked,
	7, 87, 152, 14, "_Ok", NULL, GD_Ok, NULL, NULL, (APTR)OkClicked,
	161, 87, 152, 14, "_Cancel", NULL, GD_Canc, NULL, NULL, (APTR)CancClicked,
	122, 15, 26, 11, "IDCMP _Handler", NULL, GD_Handler, NULL, NULL, (APTR)HandlerClicked,
	274, 15, 26, 11, "_Key Handler", NULL, GD_KeyHandler, NULL, NULL, (APTR)KeyHandlerClicked,
	122, 27, 26, 11, "_Template", NULL, GD_Template, NULL, NULL, (APTR)TemplateClicked,
	274, 27, 26, 11, "To Lo_wer", NULL, GD_ToLower, NULL, NULL, (APTR)ToLowerClicked,
	7, 67, 306, 14, "_UWORD chip:", NULL, GD_Chip, PLACETEXT_ABOVE, NULL, (APTR)ChipClicked,
	122, 39, 26, 11, "_Smart String", NULL, GD_SmartStr, NULL, NULL, (APTR)SmartStrClicked
};

ULONG ConfGTags[] = {
	(GT_Underscore), '_', (GTCB_Scaled), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (GTCB_Scaled), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (GTCB_Scaled), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (GTCB_Scaled), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (GTCB_Scaled), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (GTCB_Scaled), TRUE, (TAG_DONE),
	(GT_Underscore), '_', (GTST_MaxChars), 24, (TAG_DONE),
	(GT_Underscore), '_', (GTCB_Scaled), TRUE, (TAG_DONE)
};

UWORD ScaleX( UWORD FontX, UWORD value )
{
	return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
}

UWORD ScaleY( UWORD FontY, UWORD value )
{
	return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
}


LONG OpenConfWindow( struct Window **ConfWnd, struct Gadget **ConfGList, struct Gadget *ConfGadgets[], struct IE_Data *IE )
{
	UWORD           tc, FontX, FontY;
	struct TextAttr *Attr;

	FontY = IE->ScreenData->Screen->RastPort.Font->tf_YSize;
	FontX = IE->ScreenData->Screen->RastPort.Font->tf_XSize;
	Attr = IE->ScreenData->Screen->Font;

	if((( ScaleX( FontX, 318 ) + IE->ScreenData->Screen->WBorRight ) > IE->ScreenData->Screen->Width ) ||
	    (( ScaleY( FontY, 103 ) + IE->ScreenData->Screen->WBorBottom + IE->ScreenData->YOffset ) > IE->ScreenData->Screen->Height )) {
		Attr = &topaz8_065;
		FontX = FontY = 8;
	    }

	struct Gadget           *g;
	UWORD                   lc;
	struct NewGadget        ng;

	if(!( g = CreateContext( ConfGList )))
		return( 1L );

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

		CopyMem(( char * )&ConfNGad[ lc ], ( char * )&ng, ( long )sizeof( struct NewGadget ));
		ng.ng_VisualInfo = IE->ScreenData->Visual;
		ng.ng_TextAttr = Attr;
		ng.ng_LeftEdge = IE->ScreenData->XOffset + ScaleX( FontX, ng.ng_LeftEdge );
		ng.ng_TopEdge  = IE->ScreenData->YOffset + ScaleY( FontY, ng.ng_TopEdge  );
		ng.ng_Width    = ScaleX( FontX, ng.ng_Width  );
		ng.ng_Height   = ScaleY( FontY, ng.ng_Height );
		ConfGadgets[ lc ] = g = CreateGadgetA((ULONG)ConfGTypes[ lc ], g, &ng, (struct TagItem *)&ConfGTags[ tc ] );

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

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

	struct TagItem ConfWTags[] = {
		{ WA_Left, 156 },
		{ WA_Top, 80 },
		{ WA_Width, ScaleX( FontX, 318 ) + IE->ScreenData->XOffset + IE->ScreenData->Screen->WBorRight },
		{ WA_Height, ScaleY( FontY, 103 ) + IE->ScreenData->YOffset + IE->ScreenData->Screen->WBorBottom },
		{ WA_PubScreen, IE->ScreenData->Screen },
		{ WA_Title, (ULONG)"C.generator Config" },
		{ WA_Flags, WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SIMPLE_REFRESH|WFLG_ACTIVATE|WFLG_RMBTRAP },
		{ WA_IDCMP, BUTTONIDCMP|CHECKBOXIDCMP|STRINGIDCMP|IDCMP_REFRESHWINDOW|IDCMP_GADGETUP|IDCMP_VANILLAKEY },
		{ WA_Gadgets, *ConfGList },
		{ TAG_DONE, NULL }
	};

	ConfWTags[ WT_LEFT ].ti_Data = (IE->ScreenData->Screen->Width  - ConfWTags[ WT_WIDTH  ].ti_Data) >> 1;
	ConfWTags[ WT_TOP  ].ti_Data = (IE->ScreenData->Screen->Height - ConfWTags[ WT_HEIGHT ].ti_Data) >> 1;

	*ConfWnd = OpenWindowTagList( NULL, &ConfWTags[0] );

	if(!( *ConfWnd ))
		return( 4L );

	GT_RefreshWindow( *ConfWnd, NULL );
	return( 0L );
}

void CloseWnd( struct Window **Wnd, struct Gadget **GList )
{
	if( *Wnd ) {
		CloseWindow( *Wnd );
		*Wnd = NULL;
	}
	if( GList ) {
		FreeGadgets( *GList );
		*GList = NULL;
	}
}

LONG HandleConfIDCMP( struct Window *ConfWnd, struct IE_Data *IE, struct Gadget *ConfGadgets[] )
{
	struct IntuiMessage    *m;
	BOOL                    (*func)(struct IE_Data *, struct Window *, struct Gadgets **);
	BOOL                    running = 0;
	int                     class;
	short                   code;
	struct Gadget          *addr;

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

		class = m->Class;
		code  = m->Code;
		addr  = (struct Gadget *)m->IAddress;

		GT_ReplyIMsg( m );

		switch( class ) {

			case    IDCMP_VANILLAKEY:
				running = HandleConfKeys( code, IE, ConfGadgets, ConfWnd );
				break;

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

			case    IDCMP_GADGETUP:
				func = addr->UserData;
				running = (*func)( IE, ConfWnd, ConfGadgets );
				break;

		}
	}
	return( running );
}

BOOL HandleConfKeys( UBYTE Code, struct IE_Data *IE, struct Gadget *Gadgets[], struct Window *Wnd )
{
	BOOL running = 0;

	switch( tolower( Code )) {

		case    'm':
			running = MsgKeyPressed( IE, Wnd, Gadgets );
			break;

		case    'p':
			running = ClickKeyPressed( IE, Wnd, Gadgets );
			break;

		case    'o':
			running = OkKeyPressed( IE, Wnd, Gadgets );
			break;

		case    'c':
			running = CancKeyPressed( IE, Wnd, Gadgets );
			break;

		case    'h':
			running = HandlerKeyPressed( IE, Wnd, Gadgets );
			break;

		case    'k':
			running = KeyHandlerKeyPressed( IE, Wnd, Gadgets );
			break;

		case    't':
			running = TemplateKeyPressed( IE, Wnd, Gadgets );
			break;

		case    'w':
			running = ToLowerKeyPressed( IE, Wnd, Gadgets );
			break;

		case    'u':
			if(!( Gadgets[ GD_Chip ]->Flags & GFLG_DISABLED ))
				ActivateGadget( Gadgets[ GD_Chip ], Wnd, NULL );
			break;

		case    's':
			running = SmartStrKeyPressed( IE, Wnd, Gadgets );
			break;
	}
	return( running );
}
