
/*
 *  File:           test.bc
 *
 *  Description:    test and demonstration code
 *                  for IconifyButtonClass and BCC syntax
 *
 *  Tab Size:       4
 *
 *  Notes:          for performance reasons (executable
 *                  size) no startup code is used.
 *
 *                  BCC rulez!
 *
 *  Copyright:      (c) 1998, Christian Buchner
 *                  flowerp@eikon.e-technik.tu-muenchen.de
 *
 */


/*---------------------------------*/
/* BCC and class specific includes */
/*---------------------------------*/

#include "IconifyButtonClass.bh"

#include "initcl.h"
#include <libraries/bcc.h>

/*------------------*/
/* Library includes */
/*------------------*/

#define __USE_SYSBASE 1
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/intuition.h>

/*---------------*/
/* Library bases */
/*---------------*/

struct DosLibrary		*DOSBase;
struct IntuitionBase	*IntuitionBase;
struct GfxBase			*GfxBase;
struct Library			*UtilityBase;
struct ExecBase			*SysBase;

/*------------*/
/* Prototypes */
/*------------*/

BOOL OpenLibs(void);
void CloseLibs(void);


/*----------------------------------*/
/* The main routine uses BCC syntax */
/*----------------------------------*/

BCCBlock { /*BEG OF BCCBlock*/

LONG __saveds __asm _main(void)
{
	struct Window *Window;
	
	/* This declaration allows us to use BCC (C++) syntax */
	/* for the IconifyButtonClass object */
	
	BOP Class IconifyButtonClass *ICB;
	
	struct IntuiMessage *Msg;
	ULONG Cl;
	UWORD Co;
	APTR IA;
	
	if (OpenLibs())
	{
		if( !_initclasses() )  /* this function is in initcl.c */
		{
			Printf("Unable to initialize the classes.\n");
		}
		else
		{
			if (Window = OpenWindowTags( NULL,
					WA_Title, "Test Window",
					WA_Left, 100,
					WA_Top, 215,
					WA_InnerWidth, 180,
					WA_InnerHeight, 100,
					WA_GimmeZeroZero, TRUE,
					WA_DepthGadget, TRUE,
					WA_SizeGadget, TRUE,
					WA_CloseGadget, TRUE,
					WA_DragBar, TRUE,
					WA_IDCMP, IDCMP_CLOSEWINDOW|IDCMP_GADGETDOWN|IDCMP_GADGETUP,
					TAG_DONE ) )
			{
				/* Create an IconifyButtonClass object, C++ syntax */
				
				if (ICB = New IconifyButtonClass( 
						GA_Left, 20,
						GA_Top,20,
						GA_Width,140,
						GA_Height, 60,
						GA_Immediate,TRUE,
						GA_RelVerify,TRUE,
						GA_Highlight, HIGHCOMP,
						GA_ID,1,
						TAG_END)) 
				{
					/* Link this object into the gadget list */
					
					AddGList(Window,(struct Gadget*)ICB,-1,-1,NULL);
					RefreshGList((struct Gadget*)ICB,Window,NULL,-1);
					
					/* Main InputEvent loop */
					
					for (;;)
					{
						while(!(Msg=(struct IntuiMessage*)GetMsg(Window->UserPort)))
						{
							WaitPort(Window->UserPort);
						}
						Cl=Msg->Class;
						Co=Msg->Code;
						IA=Msg->IAddress;
						ReplyMsg((struct Message*)Msg);
						
						if (Cl==IDCMP_CLOSEWINDOW) break;
						
						if (Cl==IDCMP_GADGETDOWN)
						{
							Printf("Down\n");
						}
						
						if (Cl==IDCMP_GADGETUP)
						{
							Printf("Up\n");
						}
					}
					
					/* Remove the object from the Gadget list */
					
					RemoveGList(Window,(struct Gadget*)ICB,-1);
					
					/* Dispose the BCC object */
					
					Delete ICB;
				}
				else
				{
					Printf("Failed to create IconifyButtonClass gadget!\n");
				}
				CloseWindow(Window);
			}
			else
			{
				Printf("No window!\n");
			}
			
			_freeclasses();
		}
		CloseLibs();
	}
	return(0);
}

} /*END OF BCCBlock*/



/*----------------------------------------*/
/* Libraries want to be opened and closed */
/*----------------------------------------*/

BOOL OpenLibs(void)
{
	BOOL Success=FALSE;
	
	SysBase = *((struct ExecBase**)(0x4));
	
	if (DOSBase=(struct DosLibrary*)OpenLibrary("dos.library",37L))
	{
		if (GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",37L))
		{
			if (IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",37L))
			{
				if (UtilityBase=(struct Library*)OpenLibrary("utility.library",37L))
				{
					Success=TRUE;
				}
			}
		}
	}
	if (!Success) CloseLibs();
	
	return(Success);
}


void CloseLibs(void)
{
	if (UtilityBase)
	{
		CloseLibrary(UtilityBase);
		UtilityBase=NULL;
	}
	
	if (IntuitionBase)
	{
		CloseLibrary((struct Library*)IntuitionBase);
		IntuitionBase=NULL;
	}
	
	if (GfxBase)
	{
		CloseLibrary((struct Library*)GfxBase);
		GfxBase=NULL;
	}
	
	if (DOSBase)
	{
		CloseLibrary((struct Library*)DOSBase);
		DOSBase=NULL;
	}
}
