/****************************/
/*      EVENTS.C			*/
/* (c)2004 Pangea Software  */
/* By Brian Greenstone      */
/****************************/


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

extern	IBNibRef 			gNibs;
extern	Boolean				gQuitApplication;


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

static pascal OSStatus MyEventHandler(EventHandlerCallRef eventhandler, EventRef event, void *userdata);


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




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

EventLoopTimerRef		gMainLoopTimer  = nil;

static	EventHandlerUPP			gMyEventHandlerUPP = nil;
static	EventHandlerRef			gMyEventHandlerRef = 0;


/******************** INIT MY COMMAND EVENT HANDLER *********************/
//
// Installs and event handler to process 'xxxx' commands from Interface Builder elements
// which are not handled by other handlers.  Things such as Menu handlers, etc.
//

void InitMyCommandEventHandler(void)
{
	OSStatus  iErr;
	EventTypeSpec	events[1] = {kEventClassCommand, kEventCommandProcess};		// types of events to handle


			/*******************************************/
			/* LOAD AND SET MENU BAR FROM OUR NIB FILE */
			/*******************************************/
			
	iErr = SetMenuBarFromNib(gNibs, CFSTR("MainMenu"));
	if (iErr != noErr)
		DoFatalAlert("\pSetMenuBarFromNib failed!");


			/************************/
			/* CREATE EVENT HANDLER */
			/************************/

	gMyEventHandlerUPP = NewEventHandlerUPP(MyEventHandler);
	
	InstallEventHandler(GetApplicationEventTarget(), gMyEventHandlerUPP, 1, events, nil, &gMyEventHandlerRef);
}

/**************** MY EVENT HANDLER *************************/
//
// 
//

static pascal OSStatus MyEventHandler(EventHandlerCallRef eventhandler, EventRef event, void *userdata)
{
OSStatus	result;
HICommand   command;

#pragma unused (eventhandler, userdata)

			/* EXTRACT COMMAND INFO FROM EVENT */
			
	GetEventParameter(event, kEventParamDirectObject, typeHICommand, nil,
					sizeof(HICommand), nil, &command);

			/* HANDLE THE COMMAND */
				
	switch (command.commandID)
	{	 
		case	'quit':							// Quit menu item
				gQuitApplication = true;
				result = noErr;
				break;
				
		case	'pref':							// Preferences... menu item
				break;
				
		case	'helo':							// Say Hello menu item
				DoAlert("\pHello");
				break;
				
				
				/* DO PLAY EFFECT MENU ITEMS */
				
		case	'snd1':
				PlayEffect(0, kFullVolume/3, kFullVolume, kNormalRate);
				break;
		case	'snd2':
				PlayEffect(1, kFullVolume, kFullVolume/2, kNormalRate);
				break;
		case	'snd3':
				PlayEffect(2, kFullVolume, kFullVolume, kNormalRate);
				break;
		case	'snd4':
				PlayEffect(3, kFullVolume/8, kFullVolume, kNormalRate);
				break;
	
		default:								// some other command we don't handle
				result = eventNotHandledErr;
	}	 
	
	return(result);
}


/******************* REMOVE MY COMMAND EVENT HANDLER *************************/
//
// Call this if you ever need to remove the event handler callback.
//

void RemoveMyCommandEventHandlers(void)
{

	//	if the handler has been installed, remove it

	if (gMyEventHandlerRef != 0)
	{
		RemoveEventHandler(gMyEventHandlerRef);
		gMyEventHandlerRef = 0;
		
		DisposeEventHandlerUPP(gMyEventHandlerUPP);
		gMyEventHandlerUPP = nil;
	}
	
}





#pragma mark -


/***************** SET MY MAIN LOOP EVENT TIMER ***********************/

void SetMyMainLoopEventTimer(EventLoopTimerProcPtr myMainLoopFunc)
{
EventLoopTimerUPP		eventLoopTimerUPP;
EventLoopRef			mainLoop;

		/* MAKE SURE WE DONÕT ALREADY HAVE AN EVENT TIMER GOING */

	if (gMainLoopTimer != nil)
		DoFatalAlert("\ptimer already set!");


		/* GET A REFERENCE TO OUR APPÕS MAIN EVENT LOOP */

	mainLoop = GetMainEventLoop();

			/* CREATE A NEW EVENT LOOP TIMER */
			//
			// This inserts our main loop callback into a
			// new event timer
			//

	eventLoopTimerUPP  =  NewEventLoopTimerUPP(myMainLoopFunc);

	InstallEventLoopTimer(mainLoop,					
				kEventDurationNoWait,		// delay before first shot
				kEventDurationMillisecond,	// delay until next shot
				eventLoopTimerUPP,			// which event loop timer to install
				nil,						// no user data
				 &gMainLoopTimer);			// timer reference that gets returned
						
		/* CLEANUP */
 
	DisposeEventLoopTimerUPP(eventLoopTimerUPP);
}


/*********************** REMOVE MY MAIN LOOP EVENT TIMER *********************/

void RemoveMyMainLoopEventTimer(void)
{
	if (gMainLoopTimer != nil)
	{
		RemoveEventLoopTimer(gMainLoopTimer);
		gMainLoopTimer = nil;
	}
}

