Sources/MSAppleEvents.c

// SVAppleEvents.c
//
// Original version by Jon Lansdell and Nigel Humphreys.
// 4.0 and 3.1 updates by Greg Sutton.
// ©Apple Computer Inc 1996, all rights reserved.
 
#include "MSAppleEvents.h"
 
#include <Resources.h>
 
#include "MSGlobals.h"
#include "MSUtils.h"
#include "MSAEUtils.h"
#include "MSWindow.h"
#include "MSFile.h"
#include "MSMain.h"
 
#include "MSAERecording.h"
#include "MSGXPrinting.h"
 
#include "MSAECompare.h"
#include "MSAECountElements.h"
#include "MSAccessors.h"
#include "MSAECoercions.h"
#include "MSAEObjectsExist.h"
#include "MSAECreate.h"
#include "MSAEClone.h"
#include "MSAEMove.h"
#include "MSAEDelete.h"
#include "MSAECopy.h"
#include "MSAECut.h"
#include "MSAEPaste.h"
#include "MSAESelect.h"
#include "MSAEClose.h"
#include "MSAESave.h"
#include "MSAERevert.h"
#include "MSAEGetData.h"
#include "MSAESetData.h"
#include "MSAEGetDataSize.h"
 
short       gRefNum;
 
 
#pragma segment Main
 
//-----------------------------------------------
//---------- APPLE EVENT HANDLING ---------------
//-----------------------------------------------
 
 
// -----------------------------------------------------------------------
//      Name:           DoAppleEvent
//      Purpose:        Process and despatch the AppleEvent
// -----------------------------------------------------------------------
 
void DoAppleEvent(EventRecord theEvent)
{
    OSErr err;
 
  // should check for your own event message types here - if you have any
    
    err = AEProcessAppleEvent(&theEvent);
}
 
// -----------------------------------------------------------------------
//      Name:           DoOpenApp
//      Purpose:        Called on startup, creates a new document.
// -----------------------------------------------------------------------
 
pascal OSErr DoOpenApp(const AppleEvent *message,const AppleEvent *reply,long refcon)
{
#ifdef __MWERKS__
    #pragma unused (reply,refcon, message)
#endif
 
    DoMenuItem( fileID, fmNew );
 
    return noErr;
}
 
// -----------------------------------------------------------------------
//      Name:           DoOpenDocument
//      Purpose:        Open all the documents passed in the Open AppleEvent.
// -----------------------------------------------------------------------
 
pascal OSErr DoOpenDocument( const AppleEvent *theEvent, const AppleEvent *theReply, long refcon )          
{
#ifdef __MWERKS__
    #pragma unused( theReply, refcon )
#endif
 
    long        index;
    long        itemsInList;
    AEKeyword   keywd;
    AEDescList  docList = { typeNull, NULL };
    long        actSize;
    DescType    typeCode;
    FSSpec      theFSSpec;
    OSErr       anErr;
    
    // Open the specified documents
    
    anErr = AEGetParamDesc( theEvent, keyDirectObject, typeAEList, &docList );
    if ( noErr != anErr ) goto done;
    
    anErr = AECountItems( &docList, &itemsInList );
        
    for ( index = 1; index <= itemsInList; index++ )
    {
        anErr = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
                                             (Ptr)&theFSSpec, sizeof( theFSSpec) , &actSize ) ;
        if ( noErr != anErr ) goto done;
 
        anErr = OpenOld( theFSSpec );
        if ( noErr != anErr ) goto done;
    }
 
done:
    (void)AEDisposeDesc( &docList );
        
    return anErr;
}
 
// -----------------------------------------------------------------------
//      Name:           MyQuit
//      Purpose:        Quit event received- exit the program.
// -----------------------------------------------------------------------
 
pascal OSErr MyQuit(const AppleEvent *message,const AppleEvent *reply,long refcon)          
{
#ifdef __MWERKS__
    #pragma unused (reply,refcon)
#endif
    
        DescType    saveOpt;
        DescType    returnedType;
        long        actSize;
        OSErr       anErr;
        
        saveOpt = kAEAsk; // the default
        (void)AEGetParamPtr( message, keyAESaveOptions, typeEnumerated,
                                &returnedType, (Ptr)&saveOpt, sizeof( saveOpt ), &actSize );
        
        if ( saveOpt != kAENo )
            anErr = AEInteractWithUser( kAEDefaultTimeout, NULL, NULL );
 
      if ( noErr == anErr )
            DoQuit( saveOpt );
        
        return anErr;
}
 
 
// -----------------------------------------------------------------------
//  Name:           DoPrintDocuments
//  Purpose:        Print a list of documents (or windows).
// -----------------------------------------------------------------------
 
pascal OSErr DoPrintDocuments(const AppleEvent *message, AppleEvent *reply, long refcon)
{
#ifdef __MWERKS__
    #pragma unused (reply, refcon)
#endif
 
    long          index;
    long          itemsInList;
    AEKeyword     keywd;
    OSErr         err;
    AEDescList    docList;
    Size          actSize;
    DescType      typeCode;
    FSSpec        theFSSpec;
    WindowToken   theWindowToken;
    OSErr         forgetErr;
    Boolean       talkToUser;
        
    err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
                                             
    err = AECountItems(&docList, &itemsInList);
        
    for (index = 1; index<=itemsInList; index++)
        if (err == noErr) 
        {
            forgetErr = AEGetNthPtr( &docList, index, typeFSS, &keywd,
                                                             &typeCode, (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize);
                                                                
            talkToUser = false;
 
            if (forgetErr == noErr) 
            {
                if (err == noErr) 
                    err = IssueAEOpenDoc(theFSSpec);
                    
                if (err == noErr) 
                    IssuePrintWindow(FrontWindow(), talkToUser);
                    
                if (err == noErr) 
                    IssueCloseCommand(FrontWindow());
            }
            else
            { // wasn't a file - was it a window ?
                err = AEGetNthPtr(&docList,
                                    index,
                                    typeMyWndw,
                                    &keywd,
                                    &typeCode,
                                    (Ptr)&theWindowToken,
                                    sizeof(WindowToken),
                                    &actSize);
                                                        
                if (err == noErr)
                    if (gGXIsPresent)
                        err = GXPrintDocument(DPtrFromWindowPtr(theWindowToken.tokenWindow), talkToUser);
                    else
                        PrintWindow(DPtrFromWindowPtr(theWindowToken.tokenWindow), talkToUser);
            }
        }
    
    if (docList.dataHandle)
        forgetErr = AEDisposeDesc(&docList);
        
    return(err);
} // DoPrintDocuments
 
 
// -----------------------------------------------------------------------
//  Name:           InitAppleEvents
//  Purpose:        Initialise the AppleEvent despatch table
// -----------------------------------------------------------------------
 
void    InitAppleEvents(void)
{
    OSErr aevtErr;
    
    gRefNum = CurResFile();     // Needed for getting application property
    
        // set up the dispatch table for the four standard AppleEvents
    
    aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoOpenApp), noRefCon, false) ;
    aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(DoOpenDocument), noRefCon, false) ;
    aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(DoPrintDocuments), noRefCon, false) ;
    aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(MyQuit), noRefCon, false) ;
    
        // set up the dispatch table for the core AppleEvents
 
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAESetData,          NewAEEventHandlerProc(DoSetData),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetData,          NewAEEventHandlerProc(DoGetData),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetDataSize,      NewAEEventHandlerProc(DoGetDataSize),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAECountElements,    NewAEEventHandlerProc(DoCountElements),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,   NewAEEventHandlerProc(DoObjectsExist),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAECreateElement,    NewAEEventHandlerProc(DoNewElement),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClone,            NewAEEventHandlerProc(DoClone),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDelete,           NewAEEventHandlerProc(DoDelete),noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAEMove,             NewAEEventHandlerProc(DoMove),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClose,            NewAEEventHandlerProc(DoCloseWindow),noRefCon, false);
    aevtErr = AEInstallEventHandler( kAECoreSuite, kAESave,             NewAEEventHandlerProc(DoSaveWindow),noRefCon, false);
 
        // set up the dispatch table for the miscellaneous AppleEvents
    
    aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECut,    NewAEEventHandlerProc(DoCut),   noRefCon, false);
    aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECopy,   NewAEEventHandlerProc(DoCopy),  noRefCon, false);
    aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEPaste,  NewAEEventHandlerProc(DoPaste), noRefCon, false);
    aevtErr = AEInstallEventHandler( kAEMiscStandards, kAERevert, NewAEEventHandlerProc(DoRevert),noRefCon, false);
    aevtErr = AEInstallEventHandler( kAEMiscStandards, kAESelect, NewAEEventHandlerProc(DoSelect),noRefCon, false);
 
        // Install recording handlers
    aevtErr = InstallRecordingHandlers();
    
        // Install callbacks for count and compare procedures
    aevtErr = InstallObjectCallbacks();
    
        // Now install our object accessors
    aevtErr = InstallAccessors();
 
        // Now the coercion handlers
    aevtErr = InstallCoercions();
 
} // InitAppleEvents