home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / Level 0 Macintosh 29Sep94 / StartupOpen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  9.4 KB  |  292 lines  |  [TEXT/KAHL]

  1. /* StartupOpen.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Debug.h"
  21. #include "Audit.h"
  22. #include "Definitions.h"
  23.  
  24. #ifdef THINK_C
  25.     #pragma options(pack_enums)
  26. #endif
  27. #include <AppleEvents.h>
  28. #ifdef THINK_C
  29.     #pragma options(!pack_enums)
  30. #endif
  31.  
  32. #include "StartupOpen.h"
  33. #include "Memory.h"
  34. #include "Array.h"
  35. #include "Files.h"
  36.  
  37.  
  38. /* this contains a list of things that have been received for opening.*/
  39. /* if StartupList is NIL, then the open event hasn't been received.  Otherwise, */
  40. /* it contains FileSpecs to be opened */
  41. static ArrayRec*                    StartupList = NIL;
  42.  
  43. /* debugging flag */
  44. EXECUTE(static MyBoolean    Initialized = False;)
  45.  
  46. /* flag that gets set True when a quit signal is received */
  47. static MyBoolean                    QuitPending = False;
  48.  
  49.  
  50. /* function to check to see that all required parameters have been gotten */
  51. static OSErr        MyGotRequiredParams(AppleEvent* theAppleEvent)
  52.     {
  53.         DescType        ReturnedType;
  54.         Size                ActualSize;
  55.         OSErr                Error;
  56.  
  57.         Error = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  58.             &ReturnedType,NIL,0,&ActualSize);
  59.         if (Error == errAEDescNotFound)
  60.             {
  61.                 return noErr;  /* we got all the params, since no more were found */
  62.             }
  63.          else
  64.             {
  65.                 if (Error == noErr)
  66.                     {
  67.                         return errAEEventNotHandled;  /* missed some, so it failed */
  68.                     }
  69.                  else
  70.                     {
  71.                         return Error;  /* AEGetAttributePtr failed, so we return why */
  72.                     }
  73.             }
  74.     }
  75.  
  76.  
  77. /* handler for open application--presents an untitled document */
  78. static pascal    OSErr        MyHandleOApp(AppleEvent* theAppleEvent,
  79.                                                 AppleEvent* reply, long handlerRefcon)
  80.     {
  81.         OSErr        Error;
  82.  
  83.         APRINT(("+MyHandleOApp"));
  84.         Error = MyGotRequiredParams(theAppleEvent);
  85.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleOApp error."));
  86.         if (Error != noErr)
  87.             {
  88.                 APRINT(("-MyHandleOApp Error %s",Error));
  89.                 return Error;
  90.             }
  91.         StartupList = NewArray();
  92.         APRINT(("-MyHandleOApp"));
  93.         return noErr;
  94.     }
  95.  
  96.  
  97. /* handler for open documents */
  98. static pascal    OSErr        MyHandleODoc(AppleEvent* theAppleEvent,
  99.                                                 AppleEvent* reply, long handlerRefcon)
  100.     {
  101.         OSErr                        Error;
  102.         long                        Index,ItemsInList;
  103.         AEDescList            DocList;
  104.         Size                        ActualSize;
  105.         AEKeyword                Keywd;
  106.         DescType                ReturnedType;
  107.  
  108.         APRINT(("+MyHandleODoc"));
  109.         /* get the direct parameter--a descriptor list--and put it into DocList */
  110.         Error = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &DocList);
  111.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  112.         if (Error != noErr) return Error;
  113.         /* check for missing required parameters */
  114.         Error = MyGotRequiredParams(theAppleEvent);
  115.         if (Error != noErr) return Error;
  116.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  117.         /* count the number of descriptor records in the list */
  118.         Error = AECountItems(&DocList,&ItemsInList);
  119.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  120.         /* now get each descriptor record from the list, coerce the returned data */
  121.         /* to an FSSpec record, and open the associated file */
  122.         if (StartupList == NIL)
  123.             {
  124.                 StartupList = NewArray();
  125.             }
  126.         if (StartupList == NIL)
  127.             {
  128.                 /* error -- not enough memory to create the list */
  129.                 goto OutOfMemPoint;
  130.             }
  131.         for (Index=1; Index <= ItemsInList; Index += 1)
  132.             {
  133.                 FSSpec                    MyFSS;
  134.  
  135.                 Error = AEGetNthPtr(&DocList,Index,typeFSS,&Keywd,&ReturnedType,
  136.                     (void*)&MyFSS,sizeof(FSSpec),&ActualSize);
  137.                 ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  138.                 if (Error == noErr)
  139.                     {
  140.                         FSSpec*                    ArrayElement;
  141.                         long                        CharIndex;
  142.  
  143.                         ArrayElement = (FSSpec*)AllocPtrCanFail(sizeof(FSSpec),"StartupOpenFSSpec");
  144.                         if (ArrayElement == NIL)
  145.                             {
  146.                              FailurePoint1:
  147.                                 goto OutOfMemPoint;
  148.                             }
  149.                         *ArrayElement = MyFSS;
  150.                         if (!ArrayAppendElement(StartupList,ArrayElement))
  151.                             {
  152.                              FailurePoint2:
  153.                                 ReleasePtr((char*)ArrayElement);
  154.                                 goto FailurePoint1;
  155.                             }
  156.                         if (!Eep_RegisterFileSpec((FileSpec*)ArrayElement))
  157.                             {
  158.                                 ArrayDeleteElement(StartupList,
  159.                                     ArrayFindElement(StartupList,ArrayElement));
  160.                                 goto FailurePoint2;
  161.                             }
  162.                     }
  163.             }
  164.      OutOfMemPoint:
  165.         Error = AEDisposeDesc(&DocList);
  166.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  167.         APRINT(("-MyHandleODoc"));
  168.         return Error;
  169.     }
  170.  
  171.  
  172. /* handle a quit event */
  173. static pascal    OSErr        MyHandleQuit(AppleEvent* theAppleEvent,
  174.                                                 AppleEvent* reply, long handlerRefcon)
  175.     {
  176.         OSErr            Error;
  177.  
  178.         APRINT(("+MyHandleQuit"));
  179.         /* check for missing required parameters */
  180.         Error = MyGotRequiredParams(theAppleEvent);
  181.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleQuit error."));
  182.         if (Error != noErr) return Error;
  183.         QuitPending = True;
  184.         APRINT(("-MyHandleQuit"));
  185.         return noErr;
  186.     }
  187.  
  188.  
  189. /* compile a list of files that should be opened when the program starts up. */
  190. /* The parameters should be exactly the ones passed into main() upon startup. */
  191. /* It is implementation defined as to whether they will be used; the Macintosh */
  192. /* does not use them, but uses Apple Events for opening startup documents instead. */
  193. void                    PrepareStartupDocuments(int argc, char* argv[])
  194.     {
  195.         APRINT(("+PrepareStartupDocuments"));
  196.         ERROR(Initialized,PRERR(ForceAbort,
  197.             "PrepareStartupDocuments called multiple times"));
  198.         EXECUTE(Initialized = True;)
  199.         StartupList = NIL;
  200.         AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  201.             (EventHandlerProcPtr)&MyHandleOApp,0,False);
  202.         AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  203.             (EventHandlerProcPtr)&MyHandleODoc,0,False);
  204.         AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  205.             (EventHandlerProcPtr)&MyHandleQuit,0,False);
  206.         APRINT(("-PrepareStartupDocuments"));
  207.     }
  208.  
  209.  
  210. /* Get a startup item.  It will initially return False.  Once the open event is */
  211. /* received, it will return True from then on.  If there is a file specification */
  212. /* to get, it will be returned, otherwise NIL will be returned.  The file */
  213. /* specification should be disposed of with DisposeFileSpec.  Here's how you know if */
  214. /* you should open an untitled document:  the first time it returns True, if it */
  215. /* also returns NIL, then do it. */
  216. MyBoolean            GetStartupObject(struct FileSpec** ReturnStuff)
  217.     {
  218.         ERROR(!Initialized,PRERR(ForceAbort,"GetStartupList:  StartupOpen not initialized"));
  219.         if (StartupList == NIL)
  220.             {
  221.                 return False;
  222.             }
  223.          else
  224.             {
  225.                 if (ArrayGetLength(StartupList) != 0)
  226.                     {
  227.                         *ReturnStuff = (FileSpec*)ArrayGetElement(StartupList,0);
  228.                         ArrayDeleteElement(StartupList,0);
  229.                     }
  230.                  else
  231.                     {
  232.                         *ReturnStuff = NIL;
  233.                     }
  234.                 return True;
  235.             }
  236.     }
  237.  
  238.  
  239. /* clean up any internal structures allocated by PrepareStartupDocument. */
  240. void                    ClearStartupDocuments(void)
  241.     {
  242.         APRINT(("+ClearStartupDocuments"));
  243.         ERROR(!Initialized,PRERR(ForceAbort,"GetStartupList:  StartupOpen not initialized"));
  244.         if (StartupList != NIL)
  245.             {
  246.                 while (ArrayGetLength(StartupList) != 0)
  247.                     {
  248.                         ReleasePtr((char*)ArrayGetElement(StartupList,0));
  249.                         ArrayDeleteElement(StartupList,0);
  250.                     }
  251.                 DisposeArray(StartupList);
  252.                 StartupList = NIL;
  253.             }
  254.         AERemoveEventHandler(kCoreEventClass,kAEOpenApplication,
  255.             (EventHandlerProcPtr)&MyHandleOApp,False);
  256.         AERemoveEventHandler(kCoreEventClass,kAEOpenDocuments,
  257.             (EventHandlerProcPtr)&MyHandleODoc,False);
  258.         AERemoveEventHandler(kCoreEventClass,kAEQuitApplication,
  259.             (EventHandlerProcPtr)&MyHandleQuit,False);
  260.         APRINT(("-ClearStartupDocuments"));
  261.     }
  262.  
  263.  
  264. /* this returns True if the system would like the program to quit.  The program */
  265. /* should then ask the user if he wants to save all changed documents. */
  266. /* A Quit event on the Macintosh will cause this to return True */
  267. MyBoolean            CheckQuitPending(void)
  268.     {
  269.         return QuitPending;
  270.     }
  271.  
  272.  
  273. /* If some other signal besides an implementation defined system quit signal */
  274. /* is received, this can be used to indicate such, and cause a normal shutdown */
  275. /* of the program to occur. */
  276. void                    SetQuitPending(void)
  277.     {
  278.         APRINT(("+SetQuitPending"));
  279.         QuitPending = True;
  280.         APRINT(("-SetQuitPending"));
  281.     }
  282.  
  283.  
  284. /* If the user cancels the quit, this should be used to clear the flag and */
  285. /* allow the program to continue running */
  286. void                    AbortQuitInProgress(void)
  287.     {
  288.         APRINT(("+AbortQuitInProgress"));
  289.         QuitPending = False;
  290.         APRINT(("-AbortQuitInProgress"));
  291.     }
  292.