/*  File:			  AGValues.c  Contains:   Functions for installing and handling AG handlers  						Also Adds the additional apple event handlers  Written by:	Don Swatman  Copyright:	© 1996 by Apple Computer, Inc., all rights reserved.*/#include <AppleGuide.h>#include <AppleEvents.h>#include "AGValues.h"//===============================================================================//        AGValues//// This unit adds context check handlers and apple events to allow apple guides// to store values and then interogate them.// There are two calls into the unit. //      OSErr InitAGStuff();  // This installs the handlers//      void RemoveAGStuff(); // This removes them//// Currently it only sets or clears 3 values. This can be easily expanded.////-------------------------------------------------------------------------------////        Apple Guide - Context Checks//// The helper app has a context check call back for 'vals' context check. More// details can be found below.////-------------------------------------------------------------------------------////        Apple Events//// The helper app understands the 'Vals' class. This is an unregistered class// which I've invented purely for demonstration puposes. The 'Cler' event id is// used to clear all the stored values. 'Clr1', 'Clr2' and 'Clr3' set the values// 1-3 respectively to 0, and 'Set1', 'Set2' and 'Set3' set the values to 1////===============================================================================//==============================================// Constants//==============================================// Maximum number of values that can be stored by app#define kMaxAGEntries 100  //==============================================//  Globals//==============================================// Storage for coach mark and context check referencesAGCoachRefNum   coachRefNum;AGContextRefNum contextRefNum;// Storage for valueslong gAGValues[kMaxAGEntries];//==============================================// Prototypes//==============================================pascal OSErr MyObjectCoachCallBack ( Rect *pRect, Ptr name, long refCon );OSErr MySetContextResult( Boolean result,													Ptr     *outMessage,													Size    *outSize );pascal OSErr MyContextCallBack( Ptr   pInputData,																Size  inputDataSize,																Ptr   *ppOutputData,																Size  *pOutputDataSize,																AGAppInfoHdl hAppInfo );void	ClearAllAGValues();pascal OSErr SetAGValues1	( const AppleEvent *message, const AppleEvent *reply, long refcon);		pascal OSErr ClearAGValues1	( const AppleEvent *message, const AppleEvent *reply, long refcon);			pascal OSErr SetAGValues2	( const AppleEvent *message, const AppleEvent *reply, long refcon);		pascal OSErr ClearAGValues2	( const AppleEvent *message, const AppleEvent *reply, long refcon);			pascal OSErr SetAGValues3	( const AppleEvent *message, const AppleEvent *reply, long refcon);		pascal OSErr ClearAGValues3	( const AppleEvent *message, const AppleEvent *reply, long refcon);			pascal OSErr ClearAGValues	( const AppleEvent *message, const AppleEvent *reply, long refcon);			//==============================================//  Initalize and remove Apple Guide call backs//==============================================//----------------------------------------------//  InitAGStuff//// Clears all the values stored by the application// Install object coach handler// Install context check handler// Initialize the AppleEvent handlers table used//   for helping the guide file//----------------------------------------------OSErr InitAGStuff(){	OSErr theErr = noErr;	short count;	// Clear all the values stored by the application	ClearAllAGValues();		// Install object coach handler	theErr = AGInstallCoachHandler( NewCoachReplyProc(MyObjectCoachCallBack), 0, &coachRefNum );	if (theErr)		DebugStr("\pGot an Err - AGInstallCoachHandler ");			// Install context check handler. MyContextCallBack will get called when a // context check of 'Vals' is queried by the Guide	theErr = AGInstallContextHandler( NewContextReplyProc(MyContextCallBack), 'Vals', 0, &contextRefNum );	if (theErr)		DebugStr("\pGot an Err - AGInstallContextHandler ");	// set up the dispatch table for the AppleEvents	theErr = AEInstallEventHandler( 'Vals', 'Cler', NewAEEventHandlerProc(ClearAGValues), 0, false) ;	theErr = AEInstallEventHandler( 'Vals', 'Clr1', NewAEEventHandlerProc(ClearAGValues1), 0, false) ;	theErr = AEInstallEventHandler( 'Vals', 'Clr2', NewAEEventHandlerProc(ClearAGValues2), 0, false) ;	theErr = AEInstallEventHandler( 'Vals', 'Clr3', NewAEEventHandlerProc(ClearAGValues3), 0, false) ;	theErr = AEInstallEventHandler( 'Vals', 'Set1', NewAEEventHandlerProc(SetAGValues1), 0, false) ;	theErr = AEInstallEventHandler( 'Vals', 'Set2', NewAEEventHandlerProc(SetAGValues2), 0, false) ;	theErr = AEInstallEventHandler( 'Vals', 'Set3', NewAEEventHandlerProc(SetAGValues3), 0, false) ;		return theErr;}//----------------------------------------------//  RemoveAGStuff//// Removes object coach handler// Removes context check handler//----------------------------------------------void RemoveAGStuff(){	OSErr theErr;		theErr = AGRemoveContextHandler( &contextRefNum );	theErr = AGRemoveCoachHandler( &coachRefNum );}//==============================================//  MyObjectCoachCallBack//// This is called whenever a guide requests an object coach// from this application.//   "pRect"  pointer to a rectangle you should retun the coaches rect in//   "name"   contains a null terminated string passed from the Guide //            file. This is the final parameter in Apple Guides //            <Define Object Coach> command.//   "refCon" This is the reference constant passed in AGInstallCoachHandler.//            In this case 0.//// As this is only for demo purposes, this call back just returns a // 100x100 square.//==============================================pascal OSErr MyObjectCoachCallBack ( Rect *pRect, Ptr name, long refCon ){	OSErr theErr = noErr;		SetRect ( pRect, 100,100, 200,200);		return theErr;}//==============================================//  MyContextCallBack//// This is called whenever a 'vals' context check is made. The// additional parameters in Apple Guides <Define Context Check>// are passed in "pInputData". In this case it contains 2 longs// which correspond to the PassedAGParamType structure.//   "command" is either 1 or 0. If it is zero then it tests if //             the specified value is zero, if it is 1 then it //             tests if the specified value is non-zero.//   "index"   is used to specify which of the "gAGValues" is to//             be tested//// Once a result, true or false, has been determined, MySetContextResult()// is used to put the result into ppOutputData and pOutputDataSize. This// is part of a far wider mechanism so a pointer to a boolean must be// created which contains 1 for true and 0 for false////==============================================struct PassedAGParamType{	long command;	long index;};typedef struct PassedAGParamType PassedAGParamType,	*PassedAGParamPtr, **PassedAGParamHdl;pascal OSErr MyContextCallBack( Ptr   pInputData,																Size  inputDataSize,																Ptr   *ppOutputData,																Size  *pOutputDataSize,																AGAppInfoHdl hAppInfo ){	OSErr   theErr = noErr;	Boolean checkResult = false;	long    count;// Check the index is within range	if (((PassedAGParamPtr)pInputData)->index >= kMaxAGEntries)		checkResult = false;	else// Work out whether we need to return true or false		if (((PassedAGParamPtr)pInputData)->command == 1)			checkResult = gAGValues[((PassedAGParamPtr)pInputData)->index - 1] != 0;		else			checkResult = gAGValues[((PassedAGParamPtr)pInputData)->index - 1] == 0;	// Set up the return values ppOutputData and pOutputDataSize	theErr = MySetContextResult ( checkResult, ppOutputData, pOutputDataSize );		return theErr;}//==============================================//  MySetContextResult//// This is sets up the reply, ppOutputData and pOutputDataSize// used by custom context checks. It creates a pointer and moves// the value in result into it.////==============================================OSErr MySetContextResult( Boolean result,													Ptr     *outMessage,													Size    *outSize ){	Ptr   pOut;	Size  theSize = sizeof ( Boolean );	OSErr theErr = noErr;		pOut = NewPtr (theSize);	if ( pOut )	{		BlockMove( (void *)&result, pOut, theSize );		*outSize = theSize;		*outMessage = pOut;	}	else		theErr = MemError();	return theErr;	}//==============================================//  ClearAllAGValues//// Sets all the guides stored values in gAGValues to 0.//==============================================void ClearAllAGValues()			{	long    count;	for (count=0;count < kMaxAGEntries;count++)		gAGValues[count] = 0;}//===============================================================================//   Event Handlers//===============================================================================//-------------------------------------------------------------------------------//       Clear all AG stored values//// //-------------------------------------------------------------------------------pascal OSErr ClearAGValues(const AppleEvent *message, const AppleEvent *reply, long refcon)			 {	ClearAllAGValues();	return( noErr );}//-------------------------------------------------------------------------------//       Set AG stored values to 1//-------------------------------------------------------------------------------pascal OSErr SetAGValues1(const AppleEvent *message, const AppleEvent *reply, long refcon)			{	gAGValues[0] = 1;	return( noErr );}pascal OSErr SetAGValues2(const AppleEvent *message, const AppleEvent *reply, long refcon)			{	gAGValues[1] = 1;	return( noErr );}pascal OSErr SetAGValues3(const AppleEvent *message, const AppleEvent *reply, long refcon)			{	gAGValues[2] = 1;	return( noErr );}//-------------------------------------------------------------------------------//       Clear AG stored values to 0//-------------------------------------------------------------------------------pascal OSErr ClearAGValues1(const AppleEvent *message, const AppleEvent *reply, long refcon)			{	gAGValues[0] = 0;	return( noErr );}pascal OSErr ClearAGValues2(const AppleEvent *message, const AppleEvent *reply, long refcon)			{	gAGValues[1] = 0;	return( noErr );}pascal OSErr ClearAGValues3(const AppleEvent *message, const AppleEvent *reply, long refcon)			{	gAGValues[2] = 0;	return( noErr );}