home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Library Source / AppEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-11  |  5.1 KB  |  242 lines  |  [TEXT/KAHL]

  1. #include <AppleEvents.h>
  2. #include "AppEventDefs.h"
  3. #include "AppEvents.h"
  4. #include "MyHandlers.h"
  5. #include "TestBed.h"
  6. #include "Gripe.h"
  7.  
  8. static StringPtr gServiceName;
  9.  
  10. // Apple Event Handlers
  11. static AEEventHandlerUPP gOAPPHandlerUPP = (AEEventHandlerUPP)NULL;
  12. static AEEventHandlerUPP gODOCHandlerUPP = (AEEventHandlerUPP)NULL;
  13. static AEEventHandlerUPP gPDOCHandlerUPP = (AEEventHandlerUPP)NULL;
  14. static AEEventHandlerUPP gQUITHandlerUPP = (AEEventHandlerUPP)NULL;
  15. static AEEventHandlerUPP gAnswerHandlerUPP = (AEEventHandlerUPP)NULL;
  16.  
  17. OSErr InitReqHandlers( void )
  18. {
  19.     OSErr err;
  20.  
  21.     gOAPPHandlerUPP = NewAEEventHandlerProc( MyOAPPHandler );
  22.     if ( !gOAPPHandlerUPP )
  23.         return memFullErr;
  24.  
  25.     if ( err = AEInstallEventHandler( kCoreEventClass,
  26.                                 kAEOpenApplication,
  27.                                 (AEEventHandlerUPP)gOAPPHandlerUPP,
  28.                                 0,
  29.                                 false ) ){
  30.         Gripe( "\poapp install failed" );
  31.         return err;
  32.     }
  33.  
  34.     gODOCHandlerUPP = NewAEEventHandlerProc( MyODocHandler );
  35.     if ( !gODOCHandlerUPP )
  36.         return memFullErr;
  37.  
  38.     if ( err = AEInstallEventHandler( kCoreEventClass,
  39.                                 kAEOpenDocuments,
  40.                                 (AEEventHandlerUPP)gODOCHandlerUPP,
  41.                                 0,
  42.                                 false ) ){
  43.         Gripe( "\podoc install failed" );
  44.         return err;
  45.     }
  46.     
  47.     gPDOCHandlerUPP = NewAEEventHandlerProc( MyPDocHandler );
  48.     if ( !gPDOCHandlerUPP )
  49.         return memFullErr;
  50.  
  51.     if ( err = AEInstallEventHandler( kCoreEventClass,
  52.                                 kAEPrintDocuments,
  53.                                 (AEEventHandlerUPP)gPDOCHandlerUPP,
  54.                                 0,
  55.                                 false ) ){
  56.         Gripe( "\ppdoc install failed" );
  57.         return err;
  58.     }
  59.     
  60.     gQUITHandlerUPP = NewAEEventHandlerProc( MyQuitHandler );
  61.     if ( !gQUITHandlerUPP )
  62.         return memFullErr;
  63.  
  64.     if ( err = AEInstallEventHandler( kCoreEventClass,
  65.                                 kAEQuitApplication,
  66.                                 (AEEventHandlerUPP)gQUITHandlerUPP,
  67.                                 0,
  68.                                 false ) ){
  69.         Gripe( "\pquit install failed" );
  70.         return err;
  71.     }
  72.  
  73.     gAnswerHandlerUPP = NewAEEventHandlerProc( MyAnswerHandler );
  74.     if ( !gAnswerHandlerUPP )
  75.         return memFullErr;
  76.  
  77.     if ( err = AEInstallEventHandler( kCoreEventClass,
  78.                                 kAEAnswer,
  79.                                 (AEEventHandlerUPP)gAnswerHandlerUPP,
  80.                                 0,
  81.                                 false ) ){
  82.         Gripe( "\pAnswer install failed" );
  83.         return err;
  84.     }
  85.  
  86.     return noErr;
  87. }
  88.  
  89. OSErr TearDownReqHandlers( void )
  90. {
  91. #ifdef GENERATINGCFM
  92.     if ( gOAPPHandlerUPP )
  93.         DisposeRoutineDescriptor( gOAPPHandlerUPP );
  94.  
  95.     if ( gODOCHandlerUPP )
  96.         DisposeRoutineDescriptor( gODOCHandlerUPP );
  97.  
  98.     if ( gPDOCHandlerUPP )
  99.         DisposeRoutineDescriptor( gPDOCHandlerUPP );
  100.  
  101.     if ( gQUITHandlerUPP )
  102.         DisposeRoutineDescriptor( gQUITHandlerUPP );
  103.  
  104.     if ( gAnswerHandlerUPP )
  105.         DisposeRoutineDescriptor( gAnswerHandlerUPP );
  106.  
  107. #endif
  108.     return noErr;
  109. }
  110.  
  111. /* This gets a connection using the PPC browser.  From IM VI p. 6-58 */
  112.  
  113. OSErr GetTargetAddress( StringPtr myPrompt,
  114.                         StringPtr myAppStr,
  115.                         PortInfoRec *myPortInfoPtr,
  116.                         AEAddressDesc *targetAddressPtr,
  117.                         StringPtr serviceName,
  118.                         TargetID *toTargetIDPtr )
  119. {
  120.     OSErr err;
  121.     PPCFilterUPP    browseProc;
  122.     
  123.     gServiceName = serviceName;
  124.  
  125.     browseProc = NewPPCFilterProc( MyPPCBrowseProc );
  126.     if ( !browseProc )
  127.         return memFullErr;
  128.  
  129.     err = PPCBrowser( myPrompt,
  130.                         myAppStr,
  131.                         false,
  132.                         &( toTargetIDPtr->location ),
  133.                         myPortInfoPtr,
  134.                         (PPCFilterUPP)browseProc,
  135.                         (StringPtr)"\p" );
  136.                         
  137. #ifdef GENERATINGCFM
  138.     DisposeRoutineDescriptor( browseProc );
  139. #endif
  140.  
  141.     if ( err )
  142.         return err;
  143.     
  144.     BlockMove( &(myPortInfoPtr->name),
  145.                 &(toTargetIDPtr->name),
  146.                 sizeof(myPortInfoPtr->name) );
  147.     
  148.     err = AECreateDesc( typeTargetID,
  149.                         (Ptr)toTargetIDPtr,
  150.                         sizeof( *toTargetIDPtr ), 
  151.                         targetAddressPtr );
  152.     
  153.     return err;
  154. }
  155.  
  156. pascal Boolean MyPPCBrowseProc( LocationNamePtr theLoc, PortInfoPtr thePortInfo )
  157. {
  158.     return true;
  159. #ifdef NEVER
  160.     if ( thePortInfo->name.portKindSelector == ppcByString ){
  161.         if ( PStrCmp( thePortInfo->name.u.portTypeStr, (StringPtr)"\pWORDSERVICES" ) )
  162.             return true;
  163.         else
  164.             return false;
  165.     }
  166.     return false;
  167. #endif
  168. }
  169.  
  170. Boolean PStrCmp( StringPtr foo, StringPtr bar )
  171. {
  172.     short len;
  173.     
  174.     len = (short)( foo[ 0 ] );
  175.     
  176.     len += 1;            /* include the length byte itself */
  177.  
  178.     while ( len-- )
  179.         if ( *foo++ != *bar++ )
  180.             return false;
  181.  
  182.     return true;
  183. }
  184.  
  185. /* GetEventType is a utility routine to get the event class and ID out of an apple event */
  186.  
  187. OSErr GetEventType( AppleEvent *theAppleEventPtr,
  188.                     AEEventClass *theClassPtr,
  189.                     AEEventID *theIDPtr )
  190. {
  191.     OSErr        err;
  192.     Size        realSize;
  193.     DescType    realType;
  194.     
  195.     /* Get the event class */
  196.     
  197.     err = AEGetAttributePtr( theAppleEventPtr,
  198.                                 keyEventClassAttr,
  199.                                 typeType,
  200.                                 &realType,
  201.                                 (Ptr)theClassPtr,
  202.                                 (Size)sizeof( AEEventClass ),
  203.                                 &realSize );
  204.     
  205.     if ( err )
  206.         return err;
  207.  
  208.     /* Get the event ID */
  209.     
  210.     err = AEGetAttributePtr( theAppleEventPtr,
  211.                                 keyEventIDAttr,
  212.                                 typeType,
  213.                                 &realType,
  214.                                 (Ptr)theIDPtr,
  215.                                 (Size)sizeof( AEEventID ),
  216.                                 &realSize );
  217.     
  218.     
  219.     return err;
  220. }
  221.  
  222. OSErr GetEventID( AppleEvent *theAppleEventPtr,
  223.                     AEEventID *theIDPtr )
  224. {
  225.     OSErr        err;
  226.     Size        realSize;
  227.     DescType    realType;
  228.     
  229.     /* Get the event ID */
  230.     
  231.     err = AEGetAttributePtr( theAppleEventPtr,
  232.                                 keyEventIDAttr,
  233.                                 typeType,
  234.                                 &realType,
  235.                                 (Ptr)theIDPtr,
  236.                                 (Size)sizeof( AEEventID ),
  237.                                 &realSize );
  238.     
  239.     
  240.     return err;
  241. }
  242.