home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / appleevents.c next >
Encoding:
C/C++ Source or Header  |  1992-05-04  |  7.9 KB  |  306 lines  |  [TEXT/KAHL]

  1. /*
  2. *********************************************************************
  3. *    
  4. *    AppleEvents.c
  5. *    Handling of required AppleEvents (based on DTS sample code)
  6. *
  7. *    Rainer Fuchs
  8. *    EMBL Data Library
  9. *    Postfach 10.2209
  10. *    D-6900 Heidelberg, FRG
  11. *    E-mail: fuchs@embl-heidelberg.de
  12. *
  13. *    Copyright © 1992 EMBL Data Library
  14. *        
  15. **********************************************************************
  16. *    
  17. */ 
  18.  
  19. #include <AppleEvents.h>
  20.  
  21. #include "EMBL-Search.h"
  22. #include "EMBL-Search.rsrc.h"
  23.  
  24.  
  25. /*
  26. ******************************* Prototypes ***************************
  27. */
  28.  
  29. #include "appleevents.h"
  30. #include "util.h"
  31. #include "checkapp.h"
  32. #include "window.h"
  33.  
  34. static Boolean MissedAnyParameters(AppleEvent *message);
  35. static pascal OSErr DoAEOpenApplication(AppleEvent *message, AppleEvent *reply, long refcon);
  36. static pascal OSErr DoAEOpenDocuments(AppleEvent *message, AppleEvent *reply, long refcon);
  37. static pascal OSErr DoAEPrintDocuments(AppleEvent *message, AppleEvent *reply, long refcon);
  38. static pascal OSErr DoAEQuitApplication(AppleEvent *message, AppleEvent *reply, long refcon);
  39.  
  40.  
  41. /*
  42. ******************************** Global variables *****************
  43. */
  44.  
  45. typedef struct {
  46.     AEEventClass    theEventClass;
  47.     AEEventID        theEventID;
  48.     ProcPtr            theHandler;
  49. } triplets;
  50.  
  51. triplets keywordsToInstall[] = {
  52.     { kCoreEventClass,    kAEOpenApplication,    (ProcPtr) DoAEOpenApplication },
  53.     { kCoreEventClass,    kAEOpenDocuments,    (ProcPtr) DoAEOpenDocuments },
  54.     { kCoreEventClass,    kAEPrintDocuments,    (ProcPtr) DoAEPrintDocuments },
  55.     { kCoreEventClass,    kAEQuitApplication,    (ProcPtr) DoAEQuitApplication }
  56.         /* The above are the four required AppleEvents. */
  57. };
  58.  
  59. extern Boolean gHasAppleEvents;
  60. extern Boolean gQuitApplication;
  61.  
  62.  
  63.  
  64. /**************************************
  65. *    Simply calls AEProcessAppleEvent
  66. */
  67.  
  68. void DoHighLevelEvent(EventRecord event)
  69. {
  70.     AEProcessAppleEvent(&event);
  71. }
  72.  
  73.  
  74. /**************************************
  75. *    Initialize our AppleEvent dispatcher table.
  76. *    For every triplet of entries in keywordsToInstall, we make a call to
  77. *    AEInstallEventHandler().
  78. */
  79.  
  80. void InitAppleEvents(void)
  81. {
  82.     OSErr    err;
  83.     long    result;
  84.     short    i;
  85.  
  86.     if (gHasAppleEvents) {
  87.         for (i = 0; i < (sizeof(keywordsToInstall) / sizeof(triplets)); ++i) {
  88.             err = AEInstallEventHandler(
  89.                 keywordsToInstall[i].theEventClass,    /* What class to install.  */
  90.                 keywordsToInstall[i].theEventID,    /* Keywords to install.    */
  91.                 keywordsToInstall[i].theHandler,    /* The AppleEvent handler. */
  92.                 0L,                                    /* Unused refcon.           */
  93.                 FALSE                                /* Only for our app.       */
  94.             );
  95.  
  96.             if ( err != noErr ) {
  97.                 ErrorMsg(ERR_AEINSTALL);
  98.                 return;
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104. /**************************************
  105. *    Used to check for any unread required parameters. Returns TRUE if we
  106. *    missed at least one.
  107. */
  108.  
  109. static Boolean MissedAnyParameters(AppleEvent *message)
  110. {
  111.     OSErr        err;
  112.     DescType    ignoredActualType;
  113.     AEKeyword    missedKeyword;
  114.     Size        ignoredActualSize;
  115.     EventRecord    event;
  116.  
  117.     err = AEGetAttributePtr(    /* SEE IF PARAMETERS ARE ALL USED UP.          */
  118.         message,                /* AppleEvent to check.                          */
  119.         keyMissedKeywordAttr,    /* Look for unread parameters.                  */
  120.         typeKeyword,            /* So we can see what type we missed, if any. */
  121.         &ignoredActualType,        /* What is would have been if not coerced.      */
  122.         (Ptr)&missedKeyword,    /* Data area.  (Keyword not handled.)          */
  123.         sizeof(missedKeyword),    /* Size of data area.                          */
  124.         &ignoredActualSize        /* Actual data size.                          */
  125.     );
  126.  
  127. /* No error means that we found some unused parameters. */
  128.  
  129.     if (err == noErr) {
  130.         event.message = *(long *) &ignoredActualType;
  131.         event.where = *(Point *) &missedKeyword;
  132.         err = errAEEventNotHandled;
  133.     }
  134.  
  135. /* errAEDescNotFound means that there are no more parameters.  If we get
  136. an error code other than that, flag it. */
  137.  
  138.     return(err != errAEDescNotFound);
  139. }
  140.  
  141.  
  142. /**************************************
  143. *    Open Application AppleEvent
  144. */
  145.  
  146. static pascal OSErr    DoAEOpenApplication(AppleEvent *message, AppleEvent *reply,
  147.                                         long refcon)
  148. {
  149.     return(noErr);    /* no action required */
  150. }
  151.  
  152.  
  153. /**************************************
  154. *    Open Document AppleEvent
  155. */
  156. static pascal OSErr    DoAEOpenDocuments(AppleEvent *message, AppleEvent *reply,
  157.                                         long refcon)
  158. {
  159.     OSErr        err,err2;
  160.     AEDescList    theDesc;
  161.     FSSpec        theFSS;
  162.     short        loop;
  163.     long        numFilesToOpen;
  164.     AEKeyword    ignoredKeyWord;
  165.     DescType    ignoredType;
  166.     Size        ignoredSize;
  167.     short        wdRefNum;
  168.     Str255        fName;
  169.     OSType        fType;
  170.     FInfo        fndrInfo;
  171.     WDPtr        dummy;
  172.     
  173.     if ( (err = AEGetParamDesc(message, keyDirectObject, typeAEList, &theDesc))
  174.             != noErr)
  175.         return(err);
  176.  
  177.     if (!MissedAnyParameters(message)) {
  178.  
  179.     /* Got all the parameters we need.  Now, go through the direct object,
  180.         see what type it is, and parse it up. */
  181.  
  182.         err = AECountItems(&theDesc, &numFilesToOpen);
  183.         if ( err == noErr ) {
  184.             /* We have numFilesToOpen that need opening. Go to it... */
  185.  
  186.             for (loop = 1; loop <= numFilesToOpen && err == noErr; ++loop) {
  187.                 err = AEGetNthPtr(        /* GET NEXT IN THE LIST...         */
  188.                     &theDesc,            /* List of file names.             */
  189.                     loop,                /* Item # in the list.             */
  190.                     typeFSS,            /* Item is of type FSSpec.         */
  191.                     &ignoredKeyWord,    /* Returned keyword -- we know.  */
  192.                     &ignoredType,        /* Returned type -- we know.     */
  193.                     (Ptr)&theFSS,        /* Where to put the FSSpec info. */
  194.                     sizeof(theFSS),        /* Size of the FSSpec info.         */
  195.                     &ignoredSize        /* Actual size -- we know.         */
  196.                 );
  197.                 if (err != noErr) break;
  198.                 
  199.                 /* Convert FSSpec to HFS working dir and file name */
  200.                 if( FSSpecToHFS(&theFSS,&wdRefNum,fName) != noErr) {
  201.                     ErrorMsg(ERR_AEOPEN);
  202.                     break;
  203.                 }
  204.                 
  205.                 /* get file type */
  206.                 if( FSpGetFInfo(&theFSS,&fndrInfo) != noErr ) {
  207.                     ErrorMsg(ERR_AEOPEN);
  208.                     break;
  209.                 }
  210.                 else fType = fndrInfo.fdType;
  211.             
  212.                 /* open document */
  213.                 if( !DoDocOpen(fType, wdRefNum,fName, &dummy)) {
  214.                     err=errAEEventNotHandled;
  215.                     break;
  216.                 }
  217.             }
  218.         }
  219.     }
  220.     err2 = AEDisposeDesc(&theDesc);
  221.     return( (err != noErr) ? err : err2);
  222. }
  223.  
  224. /**************************************
  225. *    Print Document AppleEvent
  226. */
  227.  
  228. static pascal OSErr    DoAEPrintDocuments(AppleEvent *message, AppleEvent *reply, long refcon)
  229. {
  230.     OSErr        err,err2;
  231.     AEDescList    theDesc;
  232.     FSSpec        theFSS;
  233.     short        loop;
  234.     long        numFilesToPrint;
  235.     AEKeyword    ignoredKeyWord;
  236.     DescType    ignoredType;
  237.     Size        ignoredSize;
  238.     short        wdRefNum;
  239.     Str255        fName;
  240.     OSType        fType;
  241.     FInfo        fndrInfo;
  242.     
  243.     if ( (err = AEGetParamDesc(message, keyDirectObject, typeAEList, &theDesc))
  244.             != noErr)
  245.         return(err);
  246.  
  247.     if (!MissedAnyParameters(message)) {
  248.  
  249.     /* Got all the parameters we need.  Now, go through the direct object,
  250.         see what type it is, and parse it up. */
  251.  
  252.         err = AECountItems(&theDesc, &numFilesToPrint);
  253.         if ( err == noErr ) {
  254.             /* We have numFilesToPrint that need printing. Go to it... */
  255.  
  256.             for (loop = 1; loop <= numFilesToPrint && err == noErr; ++loop) {
  257.                 err = AEGetNthPtr(        /* GET NEXT IN THE LIST...         */
  258.                     &theDesc,            /* List of file names.             */
  259.                     loop,                /* Item # in the list.             */
  260.                     typeFSS,            /* Item is of type FSSpec.         */
  261.                     &ignoredKeyWord,    /* Returned keyword -- we know.  */
  262.                     &ignoredType,        /* Returned type -- we know.     */
  263.                     (Ptr)&theFSS,        /* Where to put the FSSpec info. */
  264.                     sizeof(theFSS),        /* Size of the FSSpec info.         */
  265.                     &ignoredSize        /* Actual size -- we know.         */
  266.                 );
  267.                 if (err != noErr) break;
  268.                 
  269.                 /* Convert FSSpec to HFS working dir and file name */
  270.                 if( FSSpecToHFS(&theFSS,&wdRefNum,fName) != noErr) {
  271.                     ErrorMsg(ERR_AEOPEN);
  272.                     break;
  273.                 }
  274.                 
  275.                 /* get file type */
  276.                 if( FSpGetFInfo(&theFSS,&fndrInfo) != noErr ) {
  277.                     ErrorMsg(ERR_AEOPEN);
  278.                     break;
  279.                 }
  280.                 else fType = fndrInfo.fdType;
  281.             
  282.                 /* print document */
  283.                 if( !DoDocPrint(fType, wdRefNum,fName)) {
  284.                     err=errAEEventNotHandled;
  285.                     break;
  286.                 }
  287.             }
  288.         }
  289.     }
  290.     err2 = AEDisposeDesc(&theDesc);
  291.     return( (err != noErr) ? err : err2);
  292. }
  293.  
  294.  
  295. /**************************************
  296. *    Quit Application AppleEvent
  297. */
  298.  
  299. static pascal OSErr    DoAEQuitApplication(AppleEvent *message, AppleEvent *reply, long refcon)
  300. {
  301.     if(CloseAllWindows(FALSE)) {
  302.         gQuitApplication=TRUE;
  303.         LoadScrap();
  304.     }
  305.     return(noErr);
  306. }