/****************************/
/*        EVENTS            */
/* (c)2002 Pangea Software  */
/* By Brian Greenstone      */
/****************************/


/***************/
/* EXTERNALS   */
/***************/


extern Rect						gDragRect,gSizeRect;
extern	long					gNumMaterials;
extern	MaterialType			gMaterialsList[];



/****************************/
/*    PROTOTYPES            */
/****************************/

static void DoHighLevelEvent(void);


/****************************/
/*    CONSTANTS             */
/****************************/

#define MIN_SLEEP			0L
#define	NIL_MOUSE_REGION	0L


/**********************/
/*     VARIABLES      */
/**********************/

EventRecord	gTheEvent;


/******************/
/* HANDLE EVENTS  */
/******************/

void HandleEvents()
{
char	theChar;
GrafPtr	oldPort;
WindowPtr	whichWindow;
		
	WaitNextEvent(everyEvent,&gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION);
		
	switch (gTheEvent.what)
	{
		case	mouseDown:
				HandleMouseDown();
				break;
		case	keyDown:
		case	autoKey:
				theChar	= gTheEvent.message & charCodeMask;
				if	((gTheEvent.modifiers & cmdKey) != 0)
					HandleMenuChoice(MenuKey(theChar));
				break;

		case	updateEvt:
				whichWindow = (WindowPtr)gTheEvent.message;
				GetPort (&oldPort);
				SetPort (GetWindowPort(whichWindow));
				BeginUpdate(whichWindow);
				DrawControls(whichWindow);
							
				EndUpdate(whichWindow);
				SetPort(oldPort); 
				break;
				
		case	kHighLevelEvent:
				DoHighLevelEvent();
				break;
	}
}


/**************** DO HIGH LEVEL EVENT *****************/

static void DoHighLevelEvent(void)
{
OSErr	myErr;

		myErr = AEProcessAppleEvent(&gTheEvent);
}


/**********************/
/* HANDLE MOUSE DOWN  */
/**********************/

void HandleMouseDown()
{
WindowPtr		whichWindow;
short		thePart;
long			menuChoice, windSize;
RgnHandle		grayRegion;
		
	thePart	=	FindWindow(gTheEvent.where, &whichWindow);


	switch(thePart)
	{
		case	inMenuBar:
				menuChoice	=	MenuSelect(gTheEvent.where);
				HandleMenuChoice(menuChoice);
				break;
				
		case	inSysWindow:
				break;
				
		case	inDrag:
				grayRegion = GetGrayRgn();
				DragWindow(whichWindow,gTheEvent.where, nil);
				break;
				
		case	inGoAway:
				break;
				
		case	inContent:
				SelectWindow(whichWindow);
				break;
				
		case	inGrow:
				windSize = GrowWindow(whichWindow, gTheEvent.where,	nil);
				if (windSize != 0)
				{
					GrafPtr graf = (GrafPtr)GetWindowPort(whichWindow);
					Rect	r;
					
					
					SetPort(graf);
					GetPortBounds(graf, &r);
					EraseRect(&r);
					SizeWindow(whichWindow,LoWord(windSize),
								HiWord(windSize), true);
					InvalWindowRect(whichWindow, &r);
				} 
				break;
	}
}


/**********************/
/* HANDLE NULL EVENT  */
/**********************/

void HandleNullEvent()
{

}





/*********************** MY APPLE EVENT: QUIT APPLICATION *************************/

pascal OSErr MyAE_QuitApplication(AppleEvent *theAppleEvent, AppleEvent *reply,
							SInt32 handlerRefcon)
{
#pragma unused (theAppleEvent, reply,	handlerRefcon)
	CleanQuit();
	return(noErr);
}




