home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / ColorSync / Sample Code / CSDemo 2.1 / ShellSources / appAEvts.c < prev    next >
Encoding:
Text File  |  1996-04-05  |  15.4 KB  |  567 lines  |  [TEXT/CWIE]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // David Hayward and Nick Thompson 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1995, Apple Computer,Inc
  8. //
  9. // This file contains the framework's AppleEvent handlers
  10. // 
  11. // 9/13/94    nick    first cut
  12. // 12/13/94    david    several modifications
  13.  
  14.  
  15. #include <Types.h>
  16. #include <Memory.h>
  17. #include <AppleEvents.h>
  18. #include <SegLoad.h>
  19. #include <Errors.h>
  20.  
  21. #include "aeUtils.h"
  22.  
  23. #include "appGlobals.h"
  24. #include "appAEvts.h"
  25. #include "appPrint.h"
  26. #include "appErrors.h"
  27.  
  28. #include "win.h"
  29. #include "winTables.h"
  30.  
  31. #ifndef PIGS_SHELL_NOPRINT
  32. #include "gxPrintUtils.h"
  33. #endif
  34.  
  35. /**\
  36. |**| ==============================================================================
  37. |**| PRIVATE FUNCTION PROTOTYPES
  38. |**| ==============================================================================
  39. \**/
  40. OSErr appHandleOAPP     ( void ) ;
  41. OSErr appHandleODOC        ( FSSpec *spec ) ;
  42. OSErr appHandlePDOC     ( FSSpec *spec, FSSpec *dtp ) ;
  43. OSErr appHandleQUIT     ( void ) ;
  44.  
  45.  
  46. /**\
  47. |**| ==============================================================================
  48. |**| PUBLIC FUNCTIONS
  49. |**| ==============================================================================
  50. \**/
  51.  
  52.  
  53. //-----------------------------------------------------------------------
  54. // called to register our appleevent handlers
  55.  
  56. OSErr RegisterAppleEvents ( void )
  57. {
  58.     OSErr err ;
  59.     
  60.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
  61.                                  NewAEEventHandlerProc(HandleOAPP),
  62.                                  0L, false ) ;
  63.     if ( err ) return err ;
  64.                 
  65.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
  66.                                  NewAEEventHandlerProc(HandleODOC),
  67.                                  0L, false ) ;
  68.     if ( err ) return err ;
  69.                 
  70. #ifndef PIGS_SHELL_NOPRINT
  71.     err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
  72.                                  NewAEEventHandlerProc(HandlePDOC),
  73.                                  0L, false ) ;
  74.     if ( err ) return err ;
  75. #endif
  76.                 
  77.     err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
  78.                                  NewAEEventHandlerProc(HandleQUIT),
  79.                                  0L, false ) ;
  80.     return err ;
  81. }
  82.  
  83.  
  84. /*------------------------------------------------------------------------------*\
  85.     HandleOAPP
  86.  *------------------------------------------------------------------------------*
  87.         This routine processes the 'OAPP' AppleEvent
  88.         and passes it to the framework.
  89. \*------------------------------------------------------------------------------*/
  90. pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  91. {
  92.     OSErr err ;
  93.  
  94.     err = MyGotRequiredParams(theAppleEvent) ;
  95.  
  96.     if (err == noErr)                
  97.         err = appHandleOAPP() ;
  98.  
  99.     return err ;
  100. }
  101.  
  102.  
  103. /*------------------------------------------------------------------------------*\
  104.     HandleODOC
  105.  *------------------------------------------------------------------------------*
  106.         This routine processes the 'ODOC' AppleEvent
  107.         and passes it to the framework.
  108. \*------------------------------------------------------------------------------*/
  109. pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  110. {
  111.     FSSpec         myFSS ;
  112.     AEDescList    docList ;
  113.     OSErr        err, ignoreErr ;
  114.     long        index, itemsInList ;
  115.     Size         actualSize ;
  116.     AEKeyword    keywd ;
  117.     DescType    returnedType ;
  118.     
  119.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList ) ;
  120.     if (err == noErr)
  121.     {
  122.         // check for missing required parameters
  123.         err = MyGotRequiredParams(theAppleEvent) ;
  124.         if (err == noErr)
  125.         {
  126.             // see how many descriptor items are in the list
  127.             // this is the number of documents we want to open
  128.             err = AECountItems(&docList,&itemsInList) ;
  129.     
  130.             // now get each descriptor record from the list
  131.             // coerce the returned data to an FSSpec record, and
  132.             // open the asoociated file
  133.             
  134.             for (index=1; index <= itemsInList && err == noErr; index++)
  135.             {
  136.                 err = AEGetNthPtr(    &docList, 
  137.                                     index,
  138.                                     typeFSS,
  139.                                     &keywd,
  140.                                     &returnedType,
  141.                                     (Ptr)&myFSS,
  142.                                     sizeof(myFSS),
  143.                                     &actualSize) ;
  144.         
  145.                 if (err == noErr)
  146.                     err = appHandleODOC( &myFSS ) ;
  147.             }
  148.         }
  149.         ignoreErr = AEDisposeDesc(&docList) ;
  150.     }
  151.     return err ;
  152. }
  153.  
  154.  
  155. #ifndef PIGS_SHELL_NOPRINT
  156. /*------------------------------------------------------------------------------*\
  157.     HandlePDOC
  158.  *------------------------------------------------------------------------------*
  159.         This routine processes the 'PDOC' AppleEvent
  160.         and passes it to the framework.
  161. \*------------------------------------------------------------------------------*/
  162. pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  163. {
  164.     FSSpec         myFSS, dtpFSS ;
  165.     AEDescList    docList, dtpList ;
  166.     OSErr        err ;
  167.     long        index, itemsInList ;
  168.     Size         actualSize ;
  169.     AEKeyword    keywd ;
  170.     DescType    returnedType ;
  171.     Boolean        draggedToDTP = false ;
  172.     
  173.     err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList) ;
  174.     if (err == noErr)
  175.     {
  176.         // Check to see if the user dragged the document to a desktop printer.
  177.         err = AEGetParamDesc(theAppleEvent, keyOptionalKeywordAttr,typeAEList,&dtpList) ;
  178.         if (err == noErr) draggedToDTP = true ;
  179.  
  180.         // check for missing required parameters
  181.         err = MyGotRequiredParams(theAppleEvent) ;
  182.         if (err == noErr)
  183.         {
  184.             // get the desktop printer that the file(s) were draged to
  185.             if (draggedToDTP)
  186.             {
  187.                 err = AEGetNthPtr(&dtpList,
  188.                                     1,
  189.                                     typeFSS,
  190.                                     &keywd, 
  191.                                     &returnedType,
  192.                                     (Ptr) &dtpFSS, 
  193.                                     sizeof(FSSpec),
  194.                                     &actualSize) ;
  195.                 AEDisposeDesc(&dtpList) ;
  196.             }
  197.             
  198.             // see how many descriptor items are in the list
  199.             // this is the number of documents we want to open
  200.             err = AECountItems(&docList,&itemsInList) ;
  201.     
  202.             // now get each descriptor record from the list
  203.             // coerce the returned data to an FSSpec record, and
  204.             // open the asoociated file
  205.             
  206.             for (index=1; index <= itemsInList && err == noErr; index++)
  207.             {
  208.                 err = AEGetNthPtr(    &docList, 
  209.                                     index,
  210.                                     typeFSS,
  211.                                     &keywd,
  212.                                     &returnedType,
  213.                                     (Ptr)&myFSS,
  214.                                     sizeof(myFSS),
  215.                                     &actualSize) ;
  216.                 if (err == noErr)
  217.                 {
  218.                     if (draggedToDTP)            
  219.                         err = appHandlePDOC( &myFSS, &dtpFSS ) ;
  220.                     else
  221.                         err = appHandlePDOC( &myFSS, nil ) ;
  222.                 }
  223.             }
  224.         }
  225.         err = AEDisposeDesc(&docList) ;
  226.     }
  227.     return err ;
  228. }
  229. #endif
  230.  
  231.  
  232. /*------------------------------------------------------------------------------*\
  233.     HandleQUIT
  234.  *------------------------------------------------------------------------------*
  235.         This routine processes the 'QUIT' AppleEvent
  236.         and passes it to the framework.
  237. \*------------------------------------------------------------------------------*/
  238. pascal OSErr HandleQUIT ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  239. {
  240.     OSErr err ;
  241.  
  242.     err = MyGotRequiredParams(theAppleEvent) ;
  243.  
  244.     if (err == noErr)                
  245.         err = appHandleQUIT() ;
  246.  
  247.     if (err == noErr)    
  248.     {            
  249.           AEDisposeDesc(&gSelfAddress) ;            // Dispose of my self-addressed descriptor.
  250.           AEDisposeDesc(&gSelfPSNAddress) ;        // Dispose of my self-addressed descriptor.
  251.     }
  252.     return err ;
  253. }
  254.  
  255.  
  256. /*------------------------------------------------------------------------------*\
  257.     SendQUIT
  258.  *------------------------------------------------------------------------------*
  259.         This routine sends the 'QUIT' AppleEvent to this application to
  260.         terminate this app.  This routine is called by DoAppQuitCommand.
  261. \*------------------------------------------------------------------------------*/
  262. void SendQUIT ( void )
  263. {
  264.     AppleEvent    myAppleEvent, reply ;
  265.     
  266.     //    Create the Apple Event.
  267.     FailIfErr(AECreateAppleEvent( kCoreEventClass, 
  268.                                   kAEQuitApplication, 
  269.                                   &gSelfAddress,
  270.                                   kAutoGenerateReturnID, 
  271.                                   kAnyTransactionID, 
  272.                                   &myAppleEvent)) ;
  273.                                   
  274.     //    Send the Apple Event.
  275.       FailIfErr(AESend( &myAppleEvent, 
  276.                         &reply, 
  277.                         kAENoReply+kAENeverInteract, 
  278.                         kAENormalPriority,
  279.                         kAEDefaultTimeout, 
  280.                         nil, 
  281.                         nil)) ;
  282.                         
  283.       AEDisposeDesc(&myAppleEvent) ;                // Dispose of the Apple Event.
  284. }
  285.  
  286.  
  287.  
  288. /*------------------------------------------------------------------------------*\
  289.     SendODOC
  290.  *------------------------------------------------------------------------------*
  291.         This routine sends the 'ODOC' AppleEvent to this application to
  292.         open a document.  This routine is called by DoAppOpenCommand.
  293. \*------------------------------------------------------------------------------*/
  294. void SendODOC (FSSpec *myFSSpec)
  295. {
  296.      AppleEvent    myAppleEvent ;
  297.     AppleEvent    defReply ;
  298.     AEDescList    docList ;
  299.     OSErr         ignoreErr ;
  300.     
  301.     myAppleEvent.dataHandle = nil ;
  302.     docList.dataHandle  = nil ;
  303.     defReply.dataHandle = nil ;
  304.         
  305.     // Create empty list and add one file spec
  306.     FailIfErr(AECreateList(nil,0,false, &docList)) ;
  307.     
  308.     FailIfErr(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec))) ;
  309.         
  310.     FailIfErr(AECreateAppleEvent(    kCoreEventClass,
  311.                                     kAEOpenDocuments,
  312.                                     &gSelfAddress,
  313.                                     kAutoGenerateReturnID,
  314.                                     kAnyTransactionID,
  315.                                     &myAppleEvent)) ;
  316.  
  317.     // Put Params into our event and send it
  318.  
  319.     FailIfErr(AEPutParamDesc( &myAppleEvent,
  320.                               keyDirectObject,
  321.                               &docList)) ;
  322.  
  323.     FailIfErr(AESend( &myAppleEvent,
  324.                       &defReply,
  325.                       kAENoReply+kAENeverInteract,
  326.                       kAENormalPriority,
  327.                       kAEDefaultTimeout,
  328.                       nil,
  329.                       nil)) ;
  330.         
  331.         
  332.     if (myAppleEvent.dataHandle) 
  333.         ignoreErr = AEDisposeDesc(&myAppleEvent) ;
  334.         
  335.     if (docList.dataHandle) 
  336.         ignoreErr = AEDisposeDesc(&docList) ;
  337. }
  338.  
  339.  
  340.  
  341. #ifndef PIGS_SHELL_NOPRINT
  342. /*------------------------------------------------------------------------------*\
  343.     SendPDOC
  344.  *------------------------------------------------------------------------------*
  345.         This routine sends the 'PDOC' AppleEvent to this application to
  346.         print a document.  This routine is called by DoAppPrintCommand and
  347.         DoAppPrintOneCommand.
  348. \*------------------------------------------------------------------------------*/
  349. void SendPDOC (FSSpec *myFSSpec)
  350. {
  351.      AppleEvent    myAppleEvent ;
  352.     AppleEvent    defReply ;
  353.     AEDescList    docList ;
  354.     OSErr         ignoreErr ;
  355.     
  356.     myAppleEvent.dataHandle = nil ;
  357.     docList.dataHandle  = nil ;
  358.     defReply.dataHandle = nil ;
  359.         
  360.     // Create empty list and add one file spec
  361.     FailIfErr(AECreateList(nil,0,false, &docList)) ;
  362.     
  363.     FailIfErr(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec))) ;
  364.         
  365.     FailIfErr(AECreateAppleEvent(    kCoreEventClass,
  366.                                     kAEPrintDocuments,
  367.                                     &gSelfAddress,
  368.                                     kAutoGenerateReturnID,
  369.                                     kAnyTransactionID,
  370.                                     &myAppleEvent)) ;
  371.  
  372.     // Put Params into our event and send it
  373.  
  374.     FailIfErr(AEPutParamDesc( &myAppleEvent,
  375.                               keyDirectObject,
  376.                               &docList)) ;
  377.  
  378.     FailIfErr(AESend( &myAppleEvent,
  379.                       &defReply,
  380.                       kAENoReply+kAENeverInteract,
  381.                       kAENormalPriority,
  382.                       kAEDefaultTimeout,
  383.                       nil,
  384.                       nil)) ;
  385.         
  386.         
  387.     if (myAppleEvent.dataHandle) 
  388.         ignoreErr = AEDisposeDesc(&myAppleEvent) ;
  389.         
  390.     if (docList.dataHandle) 
  391.         ignoreErr = AEDisposeDesc(&docList) ;
  392. }
  393. #endif
  394.  
  395.  
  396. /**\
  397. |**| ==============================================================================
  398. |**| PRIVATE FUNCTIONS
  399. |**| ==============================================================================
  400. \**/
  401.  
  402.  
  403. /*------------------------------------------------------------------------------*\
  404.     appHandleOAPP
  405.  *------------------------------------------------------------------------------*
  406.         This routine handles the 'OAPP' AppleEvent for the framework.
  407.         This routine is called by HandleOAPP.
  408. \*------------------------------------------------------------------------------*/
  409. static OSErr appHandleOAPP ( void ) 
  410. {
  411.     OSErr            err = noErr ;
  412.     winHandle        win;
  413.     AllocProcPtr    allocProc = nil ;
  414.     short            i ;
  415.     
  416.     // determine what type of winHandles we need to auto-new
  417.     for ( i=0; !err && i<gAllocProcMapCount; i++ )
  418.     {
  419.         if (gAllocProcMap[i].autoNew)
  420.         {
  421.             allocProc = gAllocProcMap[i].AllocProc ;
  422.  
  423.             // create a new winHandle of the propper type
  424.             err = NewWinHandle( &win, allocProc ) ;
  425.             WarnIfErr( err ) ;
  426.             if (err) return err;
  427.         
  428.             err = CallWinNewProc( win ) ;
  429.             if ( err != noErr )
  430.                 DisposeWinHandle( win ) ;
  431.         
  432.             if ( err == kWasAlreadyOpen )
  433.                 err = noErr ;
  434.         }
  435.     }
  436.  
  437.     return err ;
  438. }
  439.  
  440.  
  441. #ifndef PIGS_SHELL_NOPRINT
  442. /*------------------------------------------------------------------------------*\
  443.     appHandlePDOC
  444.  *------------------------------------------------------------------------------*
  445.         This routine handles the 'PDOC' AppleEvent for the framework.
  446.         This routine is called by HandlePDOC.
  447. \*------------------------------------------------------------------------------*/
  448. static OSErr appHandlePDOC ( FSSpec *spec, FSSpec *dtp ) 
  449. {
  450.     OSErr            err = noErr ;
  451.     winHandle        win ;
  452.     FInfo            info ;
  453.     AllocProcPtr    allocProc = nil ;
  454.     Boolean            wasAlreadyOpen = false ;
  455.     short            i ;
  456.     
  457.     // get the file info for the FSSpec
  458.     err = FSpGetFInfo(spec, &info) ;
  459.     if ( err != noErr ) return err ;
  460.     
  461.     // determine what type of winHandle we need
  462.     for ( i=0; i<gAllocProcMapCount; i++ )
  463.     {
  464.         if (  (gAllocProcMap[i].type == info.fdType)
  465.            && (gAllocProcMap[i].printable == true ) )
  466.             allocProc = gAllocProcMap[i].AllocProc ;
  467.     }
  468.     if ( allocProc==nil )
  469.         return WarnIfErr( eWarnCantOpenFile ) ;
  470.  
  471.     // create a new winHandle of the proper type
  472.     err = NewWinHandle( &win, allocProc ) ;
  473.     WarnIfErr( err ) ;
  474.     if (err) return err ;
  475.  
  476.     // set this field of the winHandle so that the doc can be opened
  477.     SetWinFSSpec ( win, spec ) ;
  478.     
  479.     err = CallWinOpenProc ( win ) ;
  480.     if ( err != noErr )                        //if an error occured
  481.         DisposeWinHandle( win ) ;
  482.  
  483.     if ( err == kWasAlreadyOpen )
  484.     {
  485.         wasAlreadyOpen = true ;
  486.         err = noErr ;
  487.     }
  488.     
  489.     if ( err ) return err ;
  490.         
  491.     // we know the document window is frontmost because we opened it
  492.     win = GetWindowWinHandle( FrontWindow() ) ;
  493.     if ( win == nil ) return nilHandleErr ;
  494.     
  495.     if ( dtp && GXPrinting_present()==noErr )
  496.         GXSelectJobOutputPrinter( GetWinGXJob(win) , dtp->name) ;
  497.     DoAppPrintLoop( win ) ;                    // print w/o dialogs
  498.     
  499.     if( !wasAlreadyOpen )                    // do we need to close it?
  500.         CallWinCloseProc( win ) ;            // close the window
  501.     
  502.     return err ;
  503. }
  504. #endif
  505.  
  506.  
  507. /*------------------------------------------------------------------------------*\
  508.     appHandleODOC
  509.  *------------------------------------------------------------------------------*
  510.         This routine handles the 'ODOC' AppleEvent for the framework.
  511.         This routine is called by HandleODOC.
  512. \*------------------------------------------------------------------------------*/
  513. static OSErr appHandleODOC ( FSSpec *spec ) 
  514. {
  515.     OSErr            err = noErr ;
  516.     winHandle        win ;
  517.     FInfo            info ;
  518.     AllocProcPtr    allocProc = nil ;
  519.     short            i ;
  520.     
  521.     // get the file info for the FSSpec
  522.     err = FSpGetFInfo(spec, &info) ;
  523.     if ( err != noErr ) return err ;
  524.     
  525.     // determine what type of winHandle we need
  526.     for ( i=0; i<gAllocProcMapCount; i++ )
  527.     {
  528.         if (gAllocProcMap[i].type == info.fdType)
  529.             allocProc = gAllocProcMap[i].AllocProc ;
  530.     }
  531.     if ( allocProc==nil )
  532.         return WarnIfErr( eWarnCantOpenFile ) ;
  533.  
  534.     // create a new winHandle of the proper type
  535.     err = NewWinHandle( &win, allocProc ) ;
  536.     WarnIfErr( err ) ;
  537.     if (err) return err ;
  538.  
  539.     // set this field of the winHandle so that the doc can be opened
  540.     SetWinFSSpec ( win, spec ) ;
  541.     
  542.     err = CallWinOpenProc ( win ) ;
  543.     if ( err != noErr )                        //if an error occured
  544.         DisposeWinHandle( win ) ;
  545.     
  546.     if ( err == kWasAlreadyOpen )
  547.         err = noErr ;
  548.  
  549.     return err ;
  550. }
  551.  
  552.  
  553. /*------------------------------------------------------------------------------*\
  554.     appHandleQUIT
  555.  *------------------------------------------------------------------------------*
  556.         This routine handles the 'QUIT' AppleEvent for the framework.
  557.         This routine is called by HandleQUIT.
  558. \*------------------------------------------------------------------------------*/
  559. static OSErr appHandleQUIT ( void ) 
  560. {
  561.     // close all windows and signal to Quit
  562.     // ask to save all dirty files here
  563.     
  564.     gQuitFlag = true ;
  565.     return noErr ;
  566. }
  567.