/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/ 


#include "iacinternal.h"

#undef useA4

#if !__powerc

	#ifdef THINK_C
	
		#if __option (a4_globals)
			#define useA4 1
		#endif
	
		#define PrepareCallback() RememberA4() 
		#define EnterCallback() SetUpA4()
		#define ExitCallback() RestoreA4()
		
	#endif

	#ifdef __MWERKS__
	
		#if !__A5__
			#define useA4 1
		#endif
	
	#endif

#endif

#if useA4

	#include <SetupA4.h>

#endif



/*
8/24/92 DW: This file includes routines that are likely to be used by code 
that sends Apple Events.

6/6/93 JWB: Change #ifdef coderesource instances to #if __option (a4_globals)
to make this automatic.
            Change a4IACwaitroutine () to forward the value returned by
            IACwaitroutine ()

7/16/94 dmb: Updated for Univeral Headers, PowerPC.
1/15/96 dmb: Updated for current CW SetupA4.h
*/


Boolean IACnewverb (OSType receiver, OSType vclass, OSType vtoken, AppleEvent *event) {

	/*
	create a new AppleEvent record addressed to the recipient, of the indicated class,
	with the indicated token identifier. return true if it worked, false otherwise.
	*/
	
	AEAddressDesc adr; 
	OSErr errcode;
	
	AECreateDesc (typeApplSignature, (Ptr) &receiver, sizeof (receiver), &adr);
	
	errcode = AECreateAppleEvent (vclass, vtoken, &adr, kAutoGenerateReturnID, kAnyTransactionID, event);
	
	AEDisposeDesc (&adr);
	
	IACglobals.errorcode = errcode;
	
	return (errcode == noErr);
	} /*IACnewverb*/


#if useA4

	static pascal short a4IACwaitroutine (EventRecord *ev, long *sleep, RgnHandle *mousergn) {
		
		short result;
		
		EnterCallback ();
		
		result = IACwaitroutine (ev, sleep, mousergn);
		
		ExitCallback ();
		
		return (result);
		} /*a4IACwaitroutine*/
	
	#if GENERATINGCFM
		
		static RoutineDescriptor a4IACwaitroutineDesc = BUILD_ROUTINE_DESCRIPTOR (uppAEIdleProcInfo, a4IACwaitroutine);
		
		#define a4IACwaitroutineUPP (&a4IACwaitroutineDesc)
		
	#else
		
		#define a4IACwaitroutineUPP ((AEIdleUPP) a4IACwaitroutine)
	
	#endif

#endif


Boolean IACsendverb (AppleEvent *event, AppleEvent *reply) {

	/*
	caller must dispose of the reply.
	*/

	register OSErr ec;
	long mode;
	
	mode = kAEWaitReply + kAECanInteract + kAECanSwitchLayer;
	
	#if useA4
	
		PrepareCallback ();
		
		ec = AESend (
			
			event, reply, mode, kAENormalPriority, kNoTimeOut, 
			
			a4IACwaitroutineUPP, nil);
		
	#else
	
		ec = AESend (
			
			event, reply, mode, kAENormalPriority, kNoTimeOut, 
			
			IACwaitroutineUPP, nil);
		
	#endif
	
	AEDisposeDesc (event);	
	
	IACglobals.errorcode = ec;
	
	return (ec == noErr);
	} /*IACsendverb*/


Boolean IACsendverbnoreply (AppleEvent *event, AppleEvent *reply) {

	OSErr ec;
	long mode;
 
	mode = kAENoReply + kAENeverInteract;
 
	ec = AESend (event, reply, mode, kAEHighPriority, kNoTimeOut, nil, nil);
 
	AEDisposeDesc (event); 
 
	IACglobals.errorcode = ec;
	
	return (ec == noErr);
	} /*IACsendverbnoreply*/


Boolean IACiserrorreply (Str255 errorstring) {
	
	/*
	return true if the reply is an error -- if so it has an 'errn' value
	and an 'errs' value. if we return false, the reply is not an error.
	*/
	
	OSErr ec;
	AEDesc desc;
	
	ec = AEGetParamDesc (IACglobals.reply, (AEKeyword) 'errn', 'shor', &desc);
	
	if (ec != noErr) /*the reply isn't an error*/
		return (false);

	AEDisposeDesc (&desc);
	
	ec = AEGetParamDesc (IACglobals.reply, (AEKeyword) 'errs', 'TEXT', &desc);
	
	if (ec != noErr)
		IACcopystring ("\pUnknown error.", errorstring);
	else {
		long lenstring = GetHandleSize (desc.dataHandle);
		
		if (lenstring > 255)
			lenstring = 255;
			
		errorstring [0] = (char) lenstring;
		
		BlockMove (*desc.dataHandle, &errorstring [1], lenstring);
		}
	
	AEDisposeDesc (&desc);
	
	return (true); /*there was an error returned*/
	} /*IACiserrorreply*/


Boolean IACdisposeverb (AppleEvent *event) {
	
	IACglobals.errorcode = AEDisposeDesc (event);

	return (IACglobals.errorcode == noErr);
	} /*IACdisposeverb*/