/* AZTEC_EXAMPLE.C written 09/15/86 by Martin Murray
             modified 10-01-87 for PowerWindows 2.0 */

/* *****************************************************************************
*       THIS CODE IS IN NO WAY COPYRIGHT 1987 BY INOVATRONICS, INC.  IN FACT,  *
*       YOU CAN DO ANYTHING WITH IT THAT YOU WANT TO DO.  JUST REMEMBER,       *
*       INOVATRONICS, INC. WILL BEAR ABSOLUTELY NO RESPONSIBILITY FOR THE USE, *
*       MISUSE, INABILITY TO USE OR INABLITY TO UNDERSTAND ANY OR ALL PARTS OF *
*       THIS CODE.  ENJOY IT IN GOOD HEALTH.                                   *
********************************************************************************
********************************************************************************
*       THE PURPOSE OF THE CODE IS TO LET YOU SEE WHAT YOUR PowerWindows       *
*       GENERATED SOURCE CODE WILL LOOK LIKE IN A PROGRAM.  IN MOST CASES, ALL *
*       YOU SHOULD HAVE TO DO IS COMPILE THIS FILE.  IT WILL  AUTOMATICALLY    *
*       INCLUDE YOUR SOURCE FILE, PROVIDED IT IS IN THE DEFAULT DIRECTORY, AND *
*       IS NAMED "example.h".  JUST COMPILE IT, LINK IT AND RUN IT.  IT        *
*       DEFAULTS TO TERMINATING WHEN THE CLOSE GADGET IS HIT, BUT IF YOU LOOK  *
*       BELOW YOU'LL SEE HOW TO MAKE IT TERMINATE ON ANY EVENT AT ALL.  IT     *
*       ONLY LOAD THE FIRST WINDOW, BUT WILL LOAD A SCREEN AND ITS PALETTE.    *
*       AN INTERFACE TO THE EVENT-HANDLER IS ALSO IMPLEMENTED.                 *
***************************************************************************** */

/* INCLUDES ********************************************************** */

#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>

	USHORT quit_flag = FALSE;

/* This is for the event handler */
void quit(object)
APTR object;
{
	quit_flag = TRUE;
}

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

struct Window *OpenWindow();
struct Screen *OpenScreen();
struct IntuiMessage *GetMsg();
struct MenuItem *ItemAddress();
void *OpenLibrary();

/* get the PowerWindows 2.0 code */
#include "srwindow.h"

main()
{
	UWORD code;
	ULONG class;
	APTR object;

#ifdef NEWSCREENSTRUCTURE
	struct Screen *sC;
	struct ViewPort vP;
#endif
	struct Window *wG;	/* we fetch the RastPort pointer from here */
	struct RastPort *rpG;
	struct IntuiMessage *message;	/* the message the IDCMP sends us */
 
	IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0L);
	if (IntuitionBase == NULL)
	{
		printf("intuition is not here.  where are we?\n");
		goto cleanup1;
	}
	GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L);

#ifdef NEWSCREENSTRUCTURE
	sC = OpenScreen(&NewScreenStructure);	/* open screen if present */
	NewWindowStructure1.Screen = sC;
#ifdef PALETTE
	vP = sC->ViewPort;
	LoadRGB4(&vP,&Palette,PaletteColorCount);
#endif
#endif

	wG = OpenWindow(&NewWindowStructure1);	/* open the window */
	if ( wG == NULL )
	{
		printf ("open window failed\n");
		goto cleanup1;
	}

	rpG = wG->RPort;	/* get a rastport pointer for the window */

#ifdef MenuList1
	SetMenuStrip(wG,&MenuList1);	/* attach any Menu */
#endif

#ifdef IntuiTextList1
	PrintIText(rpG,&IntuiTextList1,0L,0L);	/* Print the text if there is
						any */
#endif

#ifdef BorderList1
	DrawBorder(rpG,&BorderList1,0L,0L);	/* Draw the borders if there are
						any */
#endif

#ifdef ImageList1
	DrawImage(rpG,&ImageList1,0L,0L);	/* Draw the images if there are any */
#endif

	do
	{
		WaitPort(wG->UserPort);
			while( (message = (struct IntuiMessage *)
				GetMsg(wG->UserPort) ) != NULL)
			{
				code = message->Code;  /* MENUNUM */
				object = message->IAddress;  /* Gadget */
				class = message->Class;
				ReplyMsg(message);
				if ( class == CLOSEWINDOW ) (quit_flag = TRUE);
#ifdef HANDLEEVENT
				if (( class == GADGETUP ) ||	/* Gagdets */
					( class == GADGETDOWN ))
					HandleEvent(object);
#ifdef MenuList1
				if ( class == MENUPICK )	/* MenuItems */
				HandleEvent(ItemAddress(&MenuList1,(LONG)code));
#endif
#endif
			}
	} while (quit_flag == FALSE);

cleanup3:
#ifdef MenuList1
	ClearMenuStrip(wG);
#endif

cleanup2:
	CloseWindow(wG);
#ifdef NEWSCREENSTRUCTURE
	CloseScreen(sC);
#endif

cleanup1:
	if (GfxBase != NULL) CloseLibrary(GfxBase);
	if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
	return(0);

}

/* dummy event handler */

HandleEvent(object)
char *object;
{
}
