home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Frameworks / Hsoi's App Shell 1.0a4 / Hsoi's App Shell Source / HASAppleEvents.c next >
Encoding:
C/C++ Source or Header  |  1997-01-28  |  7.7 KB  |  308 lines  |  [TEXT/CWIE]

  1. /*    HASAppleEvents.c from Hsoi's App Shell © 1995-1997 John C. Daub.  All rights reserved.
  2.  
  3.     This file contains all the stuff to deal with AppleEvents:  installing them,
  4.     running them, etc.
  5.     
  6.     HAS supports the 4 required AppleEvents (tho it currently does nothing for
  7.     print events) and also has some support for the GET and SET data events
  8.     (see HASScripting.c and the 'aete' resource)
  9. */
  10.  
  11. /*    I need to give thanx to Reinder Verlinde (reinder@neuretp.biol.ruu.nl) for his
  12.     help on getting AppleEvents to work.  It was a simple (dumb) mistake (as a hint
  13.     for better programming, don't do it late at night when you're tired and sleepy,
  14.     cause you mess up like this).
  15.     
  16.     Simply enough, I was setting gQuitting = false; in HandleQUIT() (duh).  Should
  17.     be true.  Thanx Reinder for pointing this out.  Reinder also gave me a few good
  18.     points for inserting DebugStr()'s to help check this out that I was even getting
  19.     AppleEvents.
  20.     
  21.     John Daub March 24, 1995
  22. */
  23.  
  24.  
  25. #pragma mark ••• #includes •••
  26.  
  27. #ifndef _WASTE_
  28. #include "WASTE.h"
  29. #endif
  30. #include "HASGlobals.h"
  31. #ifndef __HSOIS_APP_SHELL__
  32. #include "HASMain.h"
  33. #endif
  34. #include "HASAppleEvents.h"
  35. #include "HASScripting.h"
  36. #include "HASWindows.h"
  37. #include "HASUtilities.h"
  38.  
  39. #pragma mark -
  40. #pragma mark ••• Globals •••
  41.  
  42. // get us some UPP's for the apple event procs
  43.  
  44. static     AEEventHandlerUPP        sODOCProc, sQUITProc, sPDOCProc, sOAPPProc;
  45.  
  46.  
  47. #pragma mark -
  48. #pragma mark ••• AE Installation •••
  49.  
  50. /*
  51.  *    This installs the AppleEvent handles, and the TextServices Manager AppleEvents
  52.  */
  53.  
  54. void    HsoiDoAEInstallation( void )
  55. {
  56.     OSErr                    myErr = noErr;
  57.     
  58.     //    allocate space for the mouse region
  59.     
  60. //    gCursorRgn = NewRgn();
  61.     
  62.     // get the handler procs
  63.     
  64.     if ( sODOCProc == nil )
  65.     {
  66.         sODOCProc = NewAEEventHandlerProc(HsoiHandleODOC);
  67.         sQUITProc = NewAEEventHandlerProc(HsoiHandleQUIT);
  68.         sPDOCProc = NewAEEventHandlerProc(HsoiHandlePDOC);
  69.         sOAPPProc = NewAEEventHandlerProc(HsoiHandleOAPP);
  70.     }
  71.     
  72.     // install the event handers
  73.     
  74.     myErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, sODOCProc, 0, false );
  75.     if ( myErr != noErr )
  76.         HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
  77.         
  78.     myErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, sQUITProc, 0, false );
  79.     if ( myErr != noErr )
  80.         HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
  81.         
  82.     myErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, sPDOCProc, 0, false );
  83.     if ( myErr != noErr )
  84.         HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
  85.         
  86.     myErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, sOAPPProc, 0, false );
  87.     if ( myErr != noErr )
  88.         HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
  89.         
  90.     //    install Apple event handlers for a subset of the Core Suite 
  91.     
  92.     myErr = HsoiInstallCoreHandlers();
  93.     if ( myErr != noErr )
  94.             HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
  95.     
  96.     
  97.     //    install AppleEvent Handlers for inline input
  98.         
  99.     if ( gHasTextServices )
  100.         WEInstallTSMHandlers();
  101.     
  102.     return;
  103. }
  104.  
  105. #pragma mark -
  106. #pragma mark ••• AE Handlers •••
  107.  
  108. /*************************************************************/
  109. /*    AppleEvent Handlers for the 4 required/core apple events */
  110. /*************************************************************/
  111.  
  112. //    Open a Document
  113.  
  114. pascal OSErr HsoiHandleODOC( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
  115. {
  116. #pragma unused ( reply, myRefCon )
  117.  
  118.     OSErr        myErr = noErr;
  119.     AEDescList    docList;
  120.     AEKeyword    keyword;
  121.     DescType    returnedType;
  122.     Size        actualSize;
  123.     long        numberOfDocuments, i;
  124.     FSSpec        fileSpec;
  125.     FInfo        theFInfo;
  126.     Boolean        isStationery;
  127.     
  128.     //    extract direct parameter from the Apple Event
  129.             
  130.     myErr = AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList );
  131.     if ( myErr )
  132.     {
  133.         AEDisposeDesc( &docList );
  134.         return( myErr );
  135.     }
  136.     
  137.     //    perform the recommended check for additional required parameters
  138.     
  139.     myErr = HsoiRequiredCheck( theAppleEvent );
  140.     if (myErr )
  141.     {
  142.         AEDisposeDesc( &docList );
  143.         return( myErr );
  144.     }
  145.     
  146.     //    count the items in the list of aliases
  147.     
  148.     myErr = AECountItems( &docList, &numberOfDocuments );
  149.     if ( myErr )
  150.     {
  151.         AEDisposeDesc( &docList );
  152.         return( myErr );
  153.     }
  154.     
  155.     for ( i = 1; i <= numberOfDocuments; i++ )
  156.     {
  157.         //    coerce the nth alias to a file system specification record
  158.         
  159.         myErr = AEGetNthPtr( &docList, i, typeFSS, &keyword, &returnedType, (Ptr)&fileSpec, sizeof( fileSpec ), &actualSize );
  160.         if ( myErr )
  161.         {
  162.             AEDisposeDesc( &docList );
  163.             return( myErr );
  164.         }
  165.         
  166.         //    open the specified file
  167.         
  168.         // i check for stationary....stationary isn't currently supported in the shell,
  169.         // but will be in the future.  so, this is just forethought.
  170.         
  171.         FSpGetFInfo( &fileSpec, &theFInfo);
  172.         isStationery = ( (theFInfo.fdFlags & 0x0800 ) != 0 );
  173.         myErr = HsoiCreateWindow( &fileSpec );
  174.         if ( myErr )
  175.         {
  176.             AEDisposeDesc( &docList );
  177.             return myErr;
  178.         }
  179.     }    // end for loop
  180.     
  181.     //    dispose of the alias list
  182.     
  183.     myErr = AEDisposeDesc( &docList );
  184.     if ( myErr )
  185.     {
  186.         AEDisposeDesc( &docList ); // silly?  but just to keep with Marco's code...
  187.         return myErr;
  188.     }
  189.     
  190.     return( noErr );
  191. }
  192.  
  193.  
  194.  
  195. // Handle a quit
  196.  
  197. pascal OSErr HsoiHandleQUIT( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
  198. {
  199. #pragma unused ( reply, myRefCon )
  200.  
  201.     AEKeyword    optKey;
  202.     DescType    actualType;
  203.     Size        actualSize;
  204.     SavingOption    saving = savingAsk; //default saving option is savingAsk
  205.     OSErr        myErr;
  206.     
  207.     //    extract the optional save options
  208.     
  209.     myErr = AEGetKeyPtr( theAppleEvent, keyAESaveOptions, typeEnumerated, &actualType, &optKey, sizeof( optKey ), &actualSize );
  210.     
  211.     if ( myErr == noErr )
  212.     {
  213.         // determine if the option key was held down
  214.         
  215.         if ( optKey == kAEYes )
  216.             saving = savingYes;
  217.         else if ( optKey == kAENo )
  218.             saving = savingNo;
  219.         else if ( optKey == kAEAsk )
  220.         {
  221.             myErr = paramErr; // for want of a better code
  222.             return myErr;
  223.         }
  224.     }
  225.     
  226.     
  227.     //    perform the recommended check for additional required parameters
  228.     
  229.     myErr = HsoiRequiredCheck( theAppleEvent );
  230.     if ( myErr )
  231.         return( myErr );
  232.     
  233.     // if clean up worked ok (e.g. all windows closed ok and saved etc...i.e. we do
  234.     // want to quit and do all things to work towards that), then CleanUp should
  235.     // return noErr, which == 0, therefore !0 should be 1, and gQuitting then
  236.     // set to true, and we quit the program just peachy.
  237.     
  238.     gQuitting = !HsoiCleanUp( savingAsk );
  239.     
  240.     return( noErr );
  241. }
  242.  
  243.  
  244.  
  245.  
  246. //    Launch the app
  247.  
  248. pascal OSErr HsoiHandleOAPP( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
  249. {
  250. #pragma unused ( reply, myRefCon )
  251.  
  252.     OSErr        myErr;
  253.     
  254.     //    perform the recommended check for additional required parameters
  255.     
  256.     myErr = HsoiRequiredCheck( theAppleEvent );
  257.     if ( myErr )
  258.         return ( myErr );
  259.     
  260.     //    created a new window from scratch
  261.     
  262.     if ( gMyPrefs.createWindow )
  263.         myErr = HsoiCreateWindow( nil );
  264.     
  265.     return( noErr );
  266. }
  267.  
  268.  
  269.  
  270.  
  271. //    handle a print event, e.g. from the Finder
  272.  
  273. // right now, I haven't written the print appleevent routines, as you can see.  however,
  274. // it would be pretty easy to do.  first, you'll have to put up the style and job dialogs
  275. // (Page Setup, and Print dialogs), get that info, and then open the document, get a
  276. // handle to the WASTE instance, and pass that to the normal printing routines.
  277. // i hope to have this implimented in a soon future release.
  278.  
  279. pascal OSErr HsoiHandlePDOC( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
  280. {
  281. #pragma unused ( theAppleEvent, reply, myRefCon )
  282.  
  283.     return( errAEEventNotHandled );
  284. }
  285.  
  286.  
  287. #pragma mark -
  288. #pragma mark ••• AE Utils •••
  289.  
  290. // this performs the recommended check for additional required parameters
  291.  
  292. OSErr HsoiRequiredCheck( AppleEvent *theAppleEvent )
  293. {
  294.     OSErr        myErr;
  295.     DescType    typeCode;
  296.     Size        actualSize;
  297.     
  298.     myErr = AEGetAttributePtr( theAppleEvent, keyMissedKeywordAttr, typeWildCard, &typeCode, nil, 0, &actualSize );
  299.     
  300.     if ( myErr == errAEDescNotFound)
  301.         return( noErr );
  302.     
  303.     if ( myErr == noErr )
  304.         return( errAEEventNotHandled );
  305.     
  306.     return( myErr );
  307. }
  308.