/* Demo of IObject.lib
 * Thu Feb  4 12:18:55 1993
 */


#define  INTUITION_PREFERENCES_H 0
#include <intuition/intuition.h>
#include "IObject.h"

/* I use only ROM font so i don't need to open the diskfont.library so i put
 DiskfontBase to NULL */
CPTR DiskfontBase=NULL;

struct Window *window=NULL;

struct NewWindow new_window=
{
	50, 25,      /* Position of window */
	480, 125,    /* Size of window */
	0, 1,
	CLOSEWINDOW|IDCMP_CYCLE|IDCMP_CHECK|IDCMP_STRING|IDCMP_BUTTON|IDCMP_STRING|
	IDCMP_INTEGER|IDCMP_SCROLLER|VANILLAKEY,   /* IDCMPFlags */
	SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|ACTIVATE|RMBTRAP,	/* Flags*/
	NULL,
	NULL,
	"Test de IObject.lib", /* Title of window */
	NULL,
	NULL,
	0, 0,
	0, 0,
	WBENCHSCREEN   /* Type: Workbench window. */
};


struct TextAttr font=
{
	"topaz.font",
	8,
	FS_NORMAL,
	FPF_ROMFONT
};


STRPTR text[]=	/* string array for cycle */
{
	"IObject.lib",
	"is very",
	"useful.",
	NULL	/* NOTEZ-BIEN: you MUST put a NULL at the of the string array */
};

struct NewCycle newcycle=
{
	203, 17,
	122, 14,
	LABEL_LEFT|LABEL_HOT_KEY,
	0,
	"C_ycle",
	&font,
	text		/* string array */
};

struct NewScroller newscroller=
{
	358, 28,
	106, 11,
	LABEL_ABOVE,
	0,
	"Scroller",
	&font,
	0,		/* Pos = 0 */
	25,		/* visible 25 */
	100		/* Maximum 100 */
};


struct NewButton newbutton=
{
	22, 17,
	94, 14,
	LABEL_INSIDE|LABEL_HOT_KEY,
	0,
	"_Button",
	&font
};


struct NewButton newtoggle=
{
	331, 97,
	140, 14,
	LABEL_INSIDE|LABEL_HOT_KEY|LABEL_PEN2,
	BT_TOGGLE,		/* button is a switch */
	"_Toggle Button",
	&font
};


struct NewCheck newcheck=
{
	403, 74,
	20, 16,
	LABEL_LEFT|LABEL_HOT_KEY,
	0,
	"_Check",
	&font
};


struct NewInteger newint=
{
	256, 42,
	72, 14,
	LABEL_RIGHT|LABEL_HOT_KEY,
	STR_CENTER,		/* integer centered */
	"_Integer",
	&font
};

struct NewString newstr=
{
	8, 38,
	162, 12,
	LABEL_RIGHT|LABEL_HOT_KEY,
	STR_SINGLEBORD,		/* single Border */
	"_String",
	&font,
	"Welcome to IObject.lib",		/* initial text */
	40		/* 40 characters maximum */
};

/* ID for the IObject */
#define ID_CYCLE		0
#define ID_SCROLLER	1
#define ID_BUTTON		2
#define ID_CHECK		3
#define ID_INTEGER	4
#define ID_STRING		5
#define ID_TGBUTTON	6

#define ID_MIN			ID_CYCLE
#define ID_MAX			ID_TGBUTTON

CPTR EZGadget[ID_MAX + 1]={0};		/* IObject array */

struct NewTextArea newtextarea=
{
	10, 64,
	312, 57,
	LABEL_ABOVE,
	TAF_CLIP_TEXT,	/* if the text is larger than the TextArea, jump to next line */
	"TextArea",
	&font,
	1,3,
	1,			/* a pixel between two lines */
	0x03		/* bitplanes 1 and 2 used */
};

CPTR textarea=NULL;		/* pointer to TextAera */

UBYTE HotKeyTable[ID_MAX+1];	/* array for hot keys */


/*
 * Look at a char in a char array
 * Returns index of char in the array or -1 if not in the array
*/
WORD FindHotKey(UBYTE hotkey, UBYTE *table, UWORD len)
{
	UWORD ind=0;

	while (len--)
	{
		/* toupper if you don't worry about upcase or lowcase */
		if (toupper(*table++) == toupper(hotkey))
			return (ind);
		ind++;
	}
	return (-1);
}


/*
 * Free ressources
*/
VOID clean_exit(void)
{
	COUNT i;

	if (window) CloseWindow(window);
	for (i = ID_MIN; i <= ID_MAX; i++)		/* free all the objects */
		if (EZGadget[i])
			FreeObject(EZGadget[i]);

	if (textarea)	FreeTextArea(textarea);
	ExitEasyGadget();		/* you MUST call ExitEasyGadget() before exiting */
	exit();
}


main()
{
	struct IntuiMessage *message;
	BOOL close_me;
	COUNT i;

	/* Open the window */
	window = (struct Window *) OpenWindow( &new_window );

	/* window open ? */
	if(!window)
		clean_exit();

	/* Init IObject.lib */
	if (!InitEasyGadget())
		clean_exit();

	/* create the TextArea, exit if error */
	if (!(textarea = CreateTextArea(&newtextarea, window->RPort)))
		clean_exit();

	/* Create objets */
	EZGadget[ID_CYCLE]    = CreateCycle(&newcycle, ID_CYCLE);
	EZGadget[ID_SCROLLER] = CreateScroller(&newscroller, ID_SCROLLER);
	EZGadget[ID_BUTTON]   = CreateButton(&newbutton, ID_BUTTON);
	EZGadget[ID_CHECK]    = CreateCheck(&newcheck, ID_CHECK);
	EZGadget[ID_INTEGER]  = CreateInteger(&newint, ID_INTEGER);
	EZGadget[ID_TGBUTTON] = CreateButton(&newtoggle, ID_TGBUTTON);
	EZGadget[ID_STRING]   = CreateString(&newstr, ID_STRING);

	/* Verify if all were objects created */
	for (i = ID_MIN; i <= ID_MAX; i++)
		if (EZGadget[i] == NULL)
			clean_exit();

	/* add object to window and display them */
	for (i = ID_MIN; i <= ID_MAX; i++)
		if (EZGadget[i])
		{
			AddObjectToWindow(EZGadget[i], window, NULL);
			DisplayObject(EZGadget[i]);
		}

	/* welcome message */
	TAPuts(textarea, "Welcome in IObject.lib");

	/* fill the hot keys array for short-cut so with hot keys you can find object */
	for (i = ID_MIN; i <= ID_MAX; i++)
		HotKeyTable[i] = GetHotKey(EZGadget[i]);

	/* integer between -5000 and 5000 */
	ModifyObject(EZGadget[ID_INTEGER], 0, -5000, 5000);

	/* loop until user click on closewindow gadget */
	close_me = FALSE;
	while(close_me == FALSE)
	{
		/* wait an event */
		Wait(1 << window->UserPort->mp_SigBit);

		/* get IntuiMessage */
		while(message = (struct IntuiMessage *) GetMsg(window->UserPort))
		{
			struct IntuiMessage msg;
			BOOL changed;
			CPTR obj;
			UWORD ID;

			changed = FALSE;
			msg = *message;			/* copy message */
			ReplyMsg(message);

			/* process event */
			switch( msg.Class )
			{
				case CLOSEWINDOW:  /* CloseWindow gadget => go away */
					close_me=TRUE;
					break;

				case VANILLAKEY:	/* short cut */
					if ((ID = FindHotKey(msg.Code, HotKeyTable, ID_MAX+1)) != -1)
					{
						obj = EZGadget[ID];
						ActivateObject(obj);
						changed = TRUE;
					}
					break;

				default:
					if (obj = FindObjectMsg(&msg))
					{
						ID = GetObjectID(obj);
						changed = SendMsgToObject(obj, &msg);
					}
					break;
			}

			if (changed)
			{
				switch (ID)
				{
					case ID_CYCLE:
						TAPrintf(textarea, "Option %s selected.\n", text[ObjectValue(obj)]);
						break;
					case ID_SCROLLER:
						TAPrintf(textarea, "Position = %ld.\n", ObjectValue(obj));
						break;
					case ID_BUTTON:
						TAPuts(textarea, "Button pressed."); 
						break;
					case ID_CHECK:
						if (ObjectValue(obj))
							TAPuts(textarea, "Check is marked.");
						break;
					case ID_INTEGER:
						TAPrintf(textarea, "Integer Value is %ld.\n", ObjectValue(obj));
						break;
					case ID_STRING:
						TAPrintf(textarea, "You have entered the text: %s.\n", ObjectValue(obj));
						break;
					case ID_TGBUTTON:
						TAPrintf(textarea, "Button is %s.\n", ObjectValue(obj) ? "On" : "Off");
						break;
				}
			}
		}
	}
	clean_exit();
}
