;/* Execute me to compile with DICE v3.0
dcc testarea.c -proto -mi -ms -lareaclass.o -lbgui
quit
*/
/*
**	       File: testarea.c
**	Description: Very simple test program for the area class.
**	  Copyright: (C) Copyright 1994-1995 Jaba Development.
**		     (C) Copyright 1994-1995 Jan van den Baard.
**		     All Rights Reserved.
**/

#include <libraries/bgui.h>
#include <libraries/bgui_macros.h>
#include <intuition/icclass.h>

#include <clib/alib_protos.h>

#include <proto/exec.h>
#include <proto/bgui.h>
#include <proto/intuition.h>
#include <proto/graphics.h>

#include <stdio.h>
#include <stdlib.h>

#include "areaclass.h"

/*
**	Compiler stuff.
**/
#ifdef _DCC
#define SAVEDS __geta4
#define ASM
#define REG(x) __ ## x
#else
#define SAVEDS __saveds
#define ASM __asm
#define REG(x) register __ ## x
#endif

struct Library *BGUIBase;
Class	       *AreaClass;

#define ID_REDRAW_AREA		1	/* When this ID is encountered we (re-)render the area. */
#define ID_QUIT                 2

/*
**	Simply took from the old BGUIDemo. Renders a simple
**	integer mandel in the area.
**/
ULONG RenderMandel( struct Window *win, struct IBox *ibox, Object *area, Object *wd )
{
	LONG	zr, zi, ar, ai, dr, di, sr, si, st, x, y, i;
	LONG	xsize, ysize, depth, rc;

	depth = win->WScreen->BitMap.Depth;
	xsize = ibox->Width;
	ysize = ibox->Height;

	sr = 0x400000 / xsize;
	si = 0x300000 / ysize;
	st = 0x140000 * -2;
	zi = 0x180000;

	for ( y = ysize - 1; y >= 0; y-- ) {
		zi -= si;
		zr = st;
		for ( x = 0; x < xsize; x++ ) {
			i = 0;
			ar = zr;
			ai = zi;
			do {
				dr = ar >> 10;
				di = ai >> 10;
				ai = dr * 2 * di + zi;
				dr *= dr;
				di *= di;
				ar = dr - di + zr;
				i++;
			} while (( i <= 25 ) && (( dr + di ) <= 0x400000 ));
			SetAPen( win->RPort, i % ( 1 << depth ));
			WritePixel( win->RPort, x + ibox->Left, y + ibox->Top );
			/*
			**	To keep things simple I sortof duplicated
			**	the event handler here. It simply returns
			**	to the main event handler (below) when
			**	necessary.
			**/
			while (( rc = HandleEvent( wd )) != WMHI_NOMORE ) {
				if ( rc == ID_REDRAW_AREA || rc == ID_QUIT || rc == WMHI_CLOSEWINDOW )
					return( rc );
			}

			zr += sr;
		}
	}
	return( 0L );
}

/*
**	Here we go...
**/
void main( int ac, char **args )
{
	struct Window	*w;
	Object		*Win, *Area, *But;
	ULONG		 signal, rc;
	struct IBox	*area_box;
	BOOL		 running = TRUE;

	if ( BGUIBase = OpenLibrary( BGUINAME, BGUIVERSION )) {
		/*
		**	Initalize the class.
		**/
		if ( AreaClass = InitAreaClass()) {
			/*
			**	Create AreaClass object.
			**
			**	Note the usage of the ICA_TARGET attribute. This is
			**	required otherwise the object will never notify you
			**	of size changes!!
			**/
			Area = NewObject( AreaClass, NULL, FRM_Type,		FRTYPE_BUTTON,
							   FRM_EdgesOnly,	TRUE,
							   AREA_MinWidth,	40,
							   AREA_MinHeight,	10,
							   GA_ID,		ID_REDRAW_AREA,
							   ICA_TARGET,		ICTARGET_IDCMP,
							   TAG_END );
			/*
			**	Make a window.
			**/
			Win = WindowObject,
				WINDOW_Title,		"AreaClass demo.",
				WINDOW_AutoAspect,	TRUE,
				WINDOW_SmartRefresh,	TRUE,
				WINDOW_MasterGroup,
					VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
						GROUP_BackFill,         SHINE_RASTER,
						StartMember, Area, EndMember,
						StartMember, But = KeyButton( "_Quit", ID_QUIT ), FixMinHeight, EndMember,
					EndObject,
			EndObject;

			/*
			**	OK?
			**/
			if ( Win ) {
				/*
				**	Add gadget key.
				**/
				GadgetKey( Win, But, "q" );
				/*
				**	Open the window.
				**/
				if ( w = WindowOpen( Win )) {
					/*
					**	Get window signal.
					**/
					GetAttr( WINDOW_SigMask, Win, &signal );
					/*
					**	Poll messages...
					**/
					do {
						Wait( signal );
						while (( rc = HandleEvent( Win )) != WMHI_NOMORE ) {

							handleMsg:

							switch ( rc ) {

								case	ID_REDRAW_AREA:
									/*
									**	Were signalled to redraw the
									**	area. Obtain the area bounds.
									**/
									GetAttr( AREA_AreaBox, Area, ( ULONG * )&area_box );
									/*
									**	Render inside the area.
									**	When this routine returns we
									**	evaluate the return code.
									**/
									if ( rc = RenderMandel( w, area_box, Area, Win ))
										goto handleMsg;

									break;

								case	WMHI_CLOSEWINDOW:
								case	ID_QUIT:
									running = FALSE;
									break;
							}
						}
					} while ( running );
				}
				DisposeObject( Win );
			}
			FreeAreaClass( AreaClass );
		}
		CloseLibrary( BGUIBase );
	}
}

#ifdef _DCC
int wbmain( struct WBStartup *wbs )
{
	return( main( 0, wbs ));
}
#endif

