home *** CD-ROM | disk | FTP | other *** search
- #include "mac.h"
- #include <AppleEvents.h>
-
- #pragma segment Humayan
-
- void HLockHigh( Handle h )
- /* Pre: none */
- /* Post: h has been moved to the top of the heap if possible and then locked */
- {
- MoveHHi(h);
- HLock(h);
- }
-
- void SafeHLock( Handle h, Boolean *saveLock )
- /* Pre: none */
- /* Post: h is locked if it wasn't; *saveLock is set to the status of the lock on entry */
- {
- #define lockBit (1 << 7)
-
- *saveLock = (HGetState(h) & lockBit) != 0;
- if ( !(*saveLock) )
- HLockHigh(h);
- }
-
- void SafeHUnlock( Handle h, Boolean saveLock )
- /* Pre: saveLock is the status of the handle's lock at the last call to SafeHLock */
- /* Post: h is unlocked if saveLock is false */
- {
- if ( !saveLock )
- HUnlock(h);
- }
-
- OSErr GotRequiredAEParams(theAppleEvent)
- AppleEvent *theAppleEvent;
- /* Confirms that all required parameters have been extracted from an Apple event */
- {
- DescType returnedType;
- Size actualSize;
- OSErr resultCode;
-
- resultCode = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, NIL, 0, &actualSize);
- if (resultCode == errAEDescNotFound)
- return noErr;
- else
- return errAEEventNotHandled;
- }
-
- pascal OSErr HandleOAPP(theAppleEvent, reply, handlerRefcon)
- AppleEvent *theAppleEvent, *reply;
- long handlerRefcon;
- /* Pre: none */
- /* Post: Open Application Apple event has been handled */
- {
- OSErr resultCode;
-
- resultCode = GotRequiredAEParams (theAppleEvent);
- if (resultCode != noErr)
- return resultCode;
-
- InitApp();
- }
-
- pascal OSErr HandleODOC(theAppleEvent, reply, handlerRefcon)
- AppleEvent *theAppleEvent, *reply;
- long handlerRefcon;
- /* Pre: none */
- /* Post: Open Documents Apple event has been handled */
- {
- OSErr resultCode;
- AEKeyword keywd;
- AEDescList docList;
- DescType returnedType;
- FSSpec fsspec;
- int items;
- int i;
- Size actualSize;
- FInfo finderinfo;
-
- InitApp();
-
- if((resultCode = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList)) == noErr)
- {
- if((resultCode = AECountItems (&docList, &items)) == noErr)
- {
- for(i=1; i <= items; ++i)
- if(AEGetNthPtr(&docList,i,typeFSS,&keywd,&returnedType,(Ptr)&fsspec,sizeof(fsspec),&actualSize) == noErr)
- {
- HGetFInfo(fsspec.vRefNum,fsspec.parID,fsspec.name,&finderinfo);
- OpenDoc(fsspec,finderinfo.fdType);
- }
- else
- SysBeep(1);
-
- }
- }
-
- if (resultCode != noErr)
- return resultCode;
- }
-
- pascal OSErr HandlePDOC(theAppleEvent, reply, handlerRefcon)
- AppleEvent *theAppleEvent, *reply;
- long handlerRefcon;
- /* Pre: none */
- /* Post: Print Documents Apple event has been handled */
- {
- OSErr resultCode;
- AEKeyword keywd;
- AEDescList docList;
- DescType returnedType;
- FSSpec fsspec;
- int items;
- int i;
- Size actualSize;
-
- if((resultCode = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList)) == noErr)
- {
- if((resultCode = AECountItems (&docList, &items)) == noErr)
- {
- for(i=1; i <= items; ++i)
- if(AEGetNthPtr(&docList,i,typeFSS,&keywd,&returnedType,(Ptr)&fsspec,sizeof(fsspec),&actualSize) == noErr)
- PrintDoc(fsspec);
- else
- SysBeep(1);
- }
- }
-
- if (resultCode != noErr)
- return resultCode;
- }
-
- pascal OSErr HandleQUIT(theAppleEvent, reply, handlerRefcon)
- AppleEvent *theAppleEvent, *reply;
- long handlerRefcon;
- /* Pre: none */
- /* Post: Quit Apple event has been handled */
- {
- OSErr resultCode;
- DescType saveOpt = kAEAskUser;
- DescType returnedType;
- Size actualSize;
- extern OSErr QuitApp();
-
- if((resultCode = AEGetParamPtr(theAppleEvent,keyAESaveOptions,typeEnumerated,
- &returnedType,(Ptr) &saveOpt,sizeof(saveOpt),&actualSize)) == noErr)
- resultCode = GotRequiredAEParams (theAppleEvent);
-
- return( QuitApp(saveOpt) );
- }
-
- OSErr InstallAEHandlers(void)
- /* Pre: The Apple events Manager is present */
- /* Post: If return value is noErr, Apple event handlers have been installed */
- /* If return value is not noErr, a fatal error occurred during installation */
- {
- OSErr resultCode;
-
- resultCode = AEInstallEventHandler (kCoreEventClass, kAEOpenApplication,
- (EventHandlerProcPtr)&HandleOAPP, 0, false);
- if (resultCode != noErr)
- return resultCode;
-
- resultCode = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments,
- (EventHandlerProcPtr)&HandleODOC, 0, false);
- if (resultCode != noErr)
- return resultCode;
-
- resultCode = AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments,
- (EventHandlerProcPtr)&HandlePDOC, 0, false);
- if (resultCode != noErr)
- return resultCode;
-
- resultCode = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
- (EventHandlerProcPtr)&HandleQUIT, 0, false);
- return resultCode;
- }
-
-
- /* C Versions of necessary HFS routines */
- /* Should probably be moved into a separate module */
-
- OSErr hcreate(char *fileName,short vRefNum,long dirID,OSType creator,OSType fileType)
- {
- OSErr resultCode = HCreate(vRefNum,dirID,c2pstr(fileName),creator,fileType);
- fileName = p2cstr((StringPtr)fileName);
- return resultCode;
- }
-
- OSErr hopen(char *fileName,short vRefNum,long dirID,char permission,short *refNum)
- {
- OSErr resultCode = HOpen(vRefNum,dirID,c2pstr(fileName),permission,refNum);
- fileName = p2cstr((StringPtr)fileName);
- return resultCode;
- }
-
- OSErr hrename(char *oldName,short vRefNum,long dirID,char *newName)
- {
- OSErr resultCode = HRename(vRefNum,dirID,c2pstr(oldName),c2pstr(newName));
- oldName = p2cstr((StringPtr)oldName);
- newName = p2cstr((StringPtr)newName);
- return resultCode;
- }
-
- OSErr hdelete(char *fileName,short vRefNum,long dirID)
- {
- OSErr resultCode = HDelete(vRefNum,dirID,c2pstr(fileName));
- fileName = p2cstr((StringPtr)fileName);
- return resultCode;
- }
-
- OSErr hgetfinfo(char *fileName,short vRefNum,long dirID,FInfo *fndrInfo)
- {
- OSErr resultCode = HGetFInfo(vRefNum,dirID,c2pstr(fileName),fndrInfo);
- fileName = p2cstr((StringPtr)fileName);
- return resultCode;
- }
-
- OSErr hsetfinfo(char *fileName,short vRefNum,long dirID,FInfo *fndrInfo)
- {
- OSErr resultCode = HSetFInfo(vRefNum,dirID,c2pstr(fileName),fndrInfo);
- fileName = p2cstr((StringPtr)fileName);
- return resultCode;
- }
-
- OSErr getwdinfo(short theWDRefNum,short *vRefNum,long *dirID)
- {
- long dummyProcID;
-
- return GetWDInfo(theWDRefNum, vRefNum, dirID, &dummyProcID);
- }
-
- OSErr fsmakefsspec(char *fileName,short vRefNum,long dirID,FSSpec *spec)
- {
- OSErr resultCode = FSMakeFSSpec(vRefNum, dirID, c2pstr(fileName), spec);
- fileName = p2cstr((StringPtr)fileName);
- return resultCode;
- }
-
- void pstrcopy(unsigned char *src, unsigned char *dst)
- {
- short count = (short)(*src) + 1;
- while (count--)
- *dst++ = *src++;
- }
-
- void SFReplyToFSSpec(SFReply theReply,FSSpec *theSpec)
- {
- if (getwdinfo(theReply.vRefNum, &theSpec->vRefNum, &theSpec->parID) == noErr)
- pstrcopy(theReply.fName, theSpec->name);
- else {
- theSpec->vRefNum = 0;
- theSpec->parID = 0;
- theSpec->name[0] = 0;
- }
- }
-