home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / draganddrop shell / toolframework.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.0 KB  |  260 lines

  1. /*
  2.     File:        ToolFramework.c
  3.  
  4.     Contains:    Simple AE framework for QuickTime related tools.
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/28/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include <Fonts.h>
  24.  
  25. // INCLUDES
  26. #include "ToolFramework.h"
  27. #include "DTSQTUtilities.h"
  28. #include "TestFunction.h"
  29.  
  30. // GLOBALS AND CONSTANTS
  31. Boolean gOneShot = true;            // Will we trigger this application just once, or is it OK to keep the app open (need 
  32.                                                 // a later quit AE message then.
  33.  
  34. Boolean gDone = false;
  35. unsigned long gWNEsleep = 0;        
  36. Boolean gHasAppleEvents = false;
  37.  
  38.  
  39. // ______________________________________________________________________
  40. // MAIN
  41. void main(void)
  42. {    
  43.     OSErr anErr;
  44.         
  45.     InitMacEnvironment(10L);        
  46.  
  47.     if (!InitializeAppleEvents())
  48.         ExitToShell();
  49.         
  50.     if( !QTUIsQuickTimeInstalled() )
  51.         ExitToShell();
  52.  
  53. #if powerc    
  54.     if( !QTUIsQuickTimeCFMInstalled() )
  55.         ExitToShell();                                
  56. #endif 
  57.  
  58.     anErr = EnterMovies(); DebugAssert(anErr == noErr);
  59.     if(anErr != noErr)
  60.         ExitToShell();
  61.  
  62.     MainEventLoop();
  63. }
  64.  
  65.  
  66. // ______________________________________________________________________
  67. pascal void InitMacEnvironment(long nMasters)
  68. {
  69.     long    i;
  70.     MaxApplZone();
  71.     
  72.     for(i = 0; i <nMasters; i++)
  73.         MoreMasters();
  74.     
  75.     InitGraf(&qd.thePort);
  76.     InitFonts();
  77.     InitWindows();
  78.     InitMenus();
  79.     FlushEvents(everyEvent, 0);
  80.     TEInit();
  81.     InitCursor();
  82.     InitDialogs(NULL);
  83. }
  84.  
  85.  
  86. // ______________________________________________________________________
  87. pascal Boolean InitializeAppleEvents(void)
  88. {
  89.     OSErr     anErr;
  90.     long        aVersion;
  91.     
  92.     anErr = Gestalt(gestaltAppleEventsAttr, &aVersion); DebugAssert(anErr == noErr);
  93.     if(anErr != noErr)
  94.         return false;        // Apple Event Manager is not present on the system.
  95.         
  96.     if( !(aVersion & (1L << gestaltAppleEventsPresent)))
  97.         return false;        // The current configuration does not support Apple Events.
  98.         
  99.     // Continue installing our core event handlers.
  100.     gHasAppleEvents = true;
  101.  
  102.     anErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, 
  103.                                                     NewAEEventHandlerProc(AEOpenHandler), 0, false);
  104.     DebugAssert(anErr == noErr);
  105.     if(anErr)
  106.         return false;
  107.  
  108.     anErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, 
  109.                                                     NewAEEventHandlerProc(AEOpenDocHandler), 0, false);
  110.     DebugAssert(anErr == noErr);
  111.     if(anErr)
  112.         return false;
  113.  
  114.     anErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  115.                                                      NewAEEventHandlerProc(AEQuitHandler), 0, false);
  116.     DebugAssert(anErr == noErr);
  117.     if(anErr)
  118.         return false;
  119.  
  120.     anErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, 
  121.                                                     NewAEEventHandlerProc(AEPrintHandler), 0, false);
  122.     DebugAssert(anErr == noErr);
  123.     if(anErr)
  124.         return false;
  125.             
  126.     return true;
  127. }
  128.  
  129.  
  130. // ______________________________________________________________________
  131. pascal void MainEventLoop(void)
  132. {
  133.     EventRecord anEvent;
  134.     
  135.     while(!gDone)
  136.     {
  137.         SystemTask();
  138.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  139.         
  140.         switch(anEvent.what)
  141.         {
  142.         // We are only interested in high level events.
  143.             case kHighLevelEvent:
  144.                 if(gHasAppleEvents)
  145.                     AEProcessAppleEvent(&anEvent);
  146.                     break;
  147.             
  148.             default:
  149.                 DebugAssert("we should not get any events here");
  150.                 break;
  151.         }
  152.     }
  153. }
  154.  
  155.  
  156. // ______________________________________________________________________
  157. // THE AE HANDLERS
  158.  
  159. // ______________________________________________________________________
  160. pascal OSErr AEOpenHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  161. {
  162.     #pragma unused(theMessage,theReply,refCon)
  163. // We are calling a stub function that supposedly will handle the open case (usually creating a new entity)
  164. // Default we do nothing.
  165.     return errAEEventNotHandled;
  166. }
  167.  
  168.  
  169. // ______________________________________________________________________
  170. pascal OSErr AEOpenDocHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  171. {
  172.     #pragma unused(theReply,refCon)
  173. // Parse the incoming entries (could be more than one, and call a specific function for each incoming entry.
  174.     OSErr             anErr;
  175.     AEDescList    aDocumentList;
  176.     AEKeyword    aKeyword;
  177.     DescType        aTypeCode;
  178.     Size                actualSize;
  179.     long                nDocuments, index;
  180.     FSSpec            anFSSpec;
  181.     
  182.     anErr = AEGetParamDesc(theMessage, keyDirectObject, typeAEList, &aDocumentList); DebugAssert(anErr == noErr);
  183.     if(anErr != noErr) return anErr;
  184.     
  185.     anErr = CheckForRequiredAEParams(theMessage); DebugAssert(anErr == noErr);
  186.     if(anErr != noErr)
  187.     {
  188.         anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  189.          return anErr;
  190.     }
  191.     anErr = AECountItems(&aDocumentList, &nDocuments); DebugAssert(anErr == noErr);
  192.     if(anErr != noErr) 
  193.     {
  194.         anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  195.         return anErr;
  196.     }
  197.     
  198.     for(index = 1; index <= nDocuments; index++)
  199.     {
  200.         anErr = AEGetNthPtr(&aDocumentList, index, typeFSS, &aKeyword, &aTypeCode,(Ptr)&anFSSpec,
  201.                                             sizeof(FSSpec), &actualSize); DebugAssert(anErr == noErr);
  202.         if(anErr != noErr)
  203.             return anErr;
  204.         
  205.         // @@@ IF YOU NEED TO DO ANYTHING PER EACH FILE PASSED, DO IT HERE.
  206.         anErr = TestFunction(&anFSSpec); DebugAssert(anErr == noErr);
  207.         if(anErr != noErr)
  208.             return anErr;
  209.     }
  210.     
  211.     if(gOneShot)
  212.         gDone = true;
  213.     
  214.     anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  215.     
  216.     return noErr;
  217. }
  218.  
  219.  
  220. // ______________________________________________________________________
  221. pascal OSErr AEPrintHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  222. {
  223.     #pragma unused(theMessage,theReply,refCon)
  224. // We are calling a stub function that supposedly will handle the print case (usually printing a known entity)
  225. // Default we do nothing.
  226.     return errAEEventNotHandled;
  227. }
  228.  
  229.  
  230. // ______________________________________________________________________
  231. pascal OSErr AEQuitHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  232. {
  233.     #pragma unused(theMessage,theReply,refCon)
  234. // If we need to do any cleanup when quit:ing, do it here.
  235.     gDone = true;
  236.     
  237.     return noErr;
  238. }
  239.  
  240.  
  241. // ______________________________________________________________________
  242. // ADDITIONAL AE FUNCTIONS
  243.  
  244. // ______________________________________________________________________
  245. pascal OSErr CheckForRequiredAEParams(AppleEvent *theEvent)
  246. {
  247.     DescType    returnedType;
  248.     Size            actualSize;
  249.     OSErr        anErr;
  250.     
  251.     anErr = AEGetAttributePtr(theEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, 
  252.                                                 NULL, 0, &actualSize); 
  253.     if(anErr == errAEDescNotFound)    // all the parameters were there!
  254.         return noErr;
  255.     else
  256.         if(anErr == noErr)                        // missed parameters
  257.             return errAEParamMissed;
  258.         else
  259.             return anErr;                        // the call to AEGetAttributePtr failed
  260. }