/*============================================================*/
/*============================================================*/
/*==														==*/
/*==					Idle Routines						==*/
/*==														==*/
/*==				(event handling routines)				==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


/*========================================================  Includes  */

#include "AppleEvents.h"
#include "Globals.h"
#include "UnivUtilities.h"
#include "Idle.h"
#include "Menu.h"
#include "MainWindow.h"
#include "SoundUtils.h"
#include <Sound.h>
#include "Show_help.h"


/*========================================================  DoMouseEvent  */

void DoMouseEvent (void)

{
	WindowPtr		whichWindow;
	GrafPtr			wasPort;
	Point			tempPt;
	Rect			sizeRect;
	long			menuChoice, windSize;
	short			thePart;
	
	thePart = FindWindow(theEvent.where, &whichWindow);
	switch (thePart)
	{
		case inMenuBar:
			menuChoice = MenuSelect(theEvent.where);
			DoMenuChoice(menuChoice);
			break;
		case inSysWindow:
			SystemClick(&theEvent, whichWindow);
			break;
		case inDrag:
			break;
		case inContent:
			break;
		case inGrow:
			break;
		case inGoAway:
			break;
		case inZoomIn:
			break;
		case inZoomOut:
			break;
		default:
			break;
	}
}


/*========================================================  DoKeyDown  */

void DoKeyDown (void)

{
	register char	theChar;
	
	theChar = theEvent.message & charCodeMask;
	if ((theEvent.modifiers & cmdKey) != 0) 
		DoMenuChoice(MenuKey(theChar));
	else 
	{
		if ((theChar == kTabKeyASCII) && (primaryMode == kPlayMode) && (pausing))
		{
			pausing = FALSE;
		}
		else if (theChar == kHelpKeyASCII)
		{
			Show_help(rHelpDialogID, (StringPtr)"\pParaHelp", rHelpBasePictID, 
					(StringPtr)"\pPararena Help", (StringPtr)"\p");
		}
	}
}


/*========================================================  DoUpdateEvent  */

void DoUpdateEvent (WindowPtr theWindow)

{
	GrafPtr		wasPort;
	
	GetPort(&wasPort);
	SetPort((GrafPtr)theWindow);

	BeginUpdate((GrafPtr)theWindow);
	if (theWindow == mainWndo)
	{
		if (splashIsUp)
			DoSplashScreen();
		else
			RefreshMainWindow();
	}
	EndUpdate(theWindow);

	SetPort((GrafPtr)wasPort);
}


/*========================================================  DoDiskEvent  */

void DoDiskEvent (void)

{
	Point		dialogPt;
	short		theErr;
	
	if (HiWrd(theEvent.message) != noErr)
	{
		SetPt(&dialogPt, kDILeft, kDITop);
		theErr = DIBadMount(dialogPt, theEvent.message);
	}
}


/*========================================================  WindowType  */

short WindowType (WindowPtr theWindow)

{
	if (theWindow == kNilPointer)
		return (kIsNilWindow);
		
	if (((WindowPeek)theWindow)->windowKind < 0)
		return (kIsDAWindow);
		
	return (kIsUnknownWindow);
}


/*========================================================  DoActivate  */

void DoActivate (WindowPtr theWindow, Boolean isToActivate)

{
	short		typeIs;
	
	typeIs = WindowType(theWindow);
	
}


/*========================================================  DoActivateEvent  */

void DoActivateEvent (void)

{
	DoActivate((GrafPtr)theEvent.message, (theEvent.modifiers & activeFlag) != 0);
}


/*========================================================  DoSuspendResumeEvent  */

void DoSuspendResumeEvent (void)

{
	short		nowVolume;
	
	switch ((theEvent.message >> 24) & 0x0FF)			/* high byte of message */
	{
		case kSuspendResumeMssg:
			inBackground = ((theEvent.message & kResumeMask) == 0);
			DoActivate(FrontWindow(), !inBackground);
			
			if (inBackground)
			{
				TurnSMSOff();
			}
			else
			{
				GetSoundVol(&nowVolume);
				if (nowVolume != soundVolume)
				{
					wasSoundVolume = nowVolume;
				}
			}
			break;
		default:
			break;
	}
}


/*========================================================  HandleEvent  */

void HandleEvent (void)

{
	RgnHandle	mouseRgn;
	Boolean		gotEvent;
	
	if ( thisMac.hasWNE )
	{
		mouseRgn = kNilPointer;
		gotEvent = WaitNextEvent(everyEvent, &theEvent, kSleepTime, mouseRgn);
	}
	else
	{
		SystemTask();
		gotEvent = GetNextEvent(everyEvent, &theEvent);
	}
	
	if (gotEvent)
	{
		switch (theEvent.what)
		{
			case nullEvent:
				break;
			case mouseDown:
				DoMouseEvent();
				break;
			case mouseUp:
				DoMouseEvent();
				break;
			case autoKey:
				DoKeyDown();
				break;
			case keyDown:
				DoKeyDown();
				break;
			case keyUp:
				break;
			case updateEvt:
				DoUpdateEvent((GrafPtr)theEvent.message);
				break;
			case diskEvt:
				DoDiskEvent();
				break;
			case activateEvt:
				DoActivateEvent();
				break;
			case app4Evt:
				DoSuspendResumeEvent();
				break;
			case kHighLevelEvent:
				break;
			default:
				break;
		}
	}
}


