home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / luschsrc.sit / setup.c < prev    next >
Text File  |  1990-05-23  |  6KB  |  258 lines

  1. /********************************************************************************
  2.  *    setup.c
  3.  *
  4.  *    Initialization Segment Routines
  5.  *
  6.  *    Written by Paco Xander Nathan
  7.  *    ⌐1990, Motorola Inc.  Public domain source code.
  8.  ********************************************************************************/
  9.  
  10. #include "applic.h"
  11. #include "window.h"
  12. #include "dialog.h"
  13. #include "context.h"
  14. #include "list.h"
  15. #include "text.h"
  16. #include "document.h"
  17. #include "error.h"
  18. #include "print.h"
  19. #include "about.h"
  20. #include <SegmentLdr.h>
  21. #include <OSUtil.h>
  22. #include "setup.h"
  23.  
  24. #include "test.h"
  25. #include "gnosis.h"
  26. #include "analysis.h"
  27.  
  28.  
  29. #define UnImplementedTrap    0x9f
  30. #define WaitNextEventTrap    0x60
  31.  
  32.  
  33. #ifdef PROTOTYPES
  34. /* Local Function Prototypes
  35.  */
  36. void SetupAntiBug (void);
  37. void SetupRsrc (void);
  38. void SetupLaMachine (void);
  39. void SetupMenus (void);
  40. void SetupWindows (void);
  41. #endif
  42.     
  43.  
  44. /* Initiate various anti-bugging measures
  45.  */
  46. static void
  47. SetupAntiBug ()
  48. {
  49. #ifdef DEVELOPING
  50.     register unsigned long *zero;
  51.  
  52.     /* First, trap NIL references by placing an odd value long integer into 
  53.      * the NULL memory location.  NB: the specified value will cause an address
  54.      * error on an SE or Plus, and a bus error on a II or later model.
  55.      */
  56.     zero = NULL;
  57.     *zero = 0x5FFFC001;
  58. #endif
  59.     
  60.     /* Next, trap potential stack overflows by placing a routine that displays
  61.      * a warning alert as the grow zone callback routine
  62.      */
  63.     SetGrowZone(ErrGrowZone);
  64.  
  65.     /* Make sure that the memory error alert is loaded and locked so that
  66.      * the application can notify the user even if a memory allocation error
  67.      * occurs.
  68.      */
  69.     CouldAlert(yesnoAlertID);
  70.  
  71.     /* Allocate a "avatar block" of memory that can be purged at the last
  72.      * resort.  Blessed be the concept.
  73.      */
  74.     errAvatarHdl = NewHandle(4098L);
  75. }
  76.  
  77.  
  78. /* Load miscellaneous resources
  79.  */
  80. static void
  81. SetupRsrc ()
  82. {
  83.     register SignedByte hdlState;
  84.     register Handle sicnHdl;
  85.     register short i;
  86.  
  87.     /* Load the watch cursor frame pointer 'acur' resource
  88.      */
  89.     if (curList = (acurHandle) GetResource('acur', WatchACUR)) {
  90.         hdlState = HGetState(curList);
  91.         HLock(curList = (acurHandle) GetResource('acur', WatchACUR));
  92.     
  93.         /* Load each "frame" in the animation
  94.          */
  95.         for (i = 0; i < (*curList)->frames; i++)
  96.             (*curList)->curs[i] = GetCursor(HiWord((long) (*curList)->curs[i]));
  97.     
  98.         HSetState(curList, hdlState);
  99.     }
  100. }
  101.  
  102.  
  103. /* Setup the machine configuration
  104.  */
  105. static void
  106. SetupLaMachine ()
  107. {
  108.     SysEnvRec theWorld;
  109.  
  110.     SysEnvirons(1, &theWorld);
  111.  
  112.     laMachine.hasWNE = GetTrapAddress(WaitNextEventTrap) != GetTrapAddress(UnImplementedTrap);
  113.     laMachine.hasSound = (theWorld.machineType == envMachUnknown) || (theWorld.machineType == envMacII) || (theWorld.systemVersion >= 0x600);
  114.     laMachine.hasColor = theWorld.hasColorQD;
  115.     laMachine.inBackground = FALSE;
  116. }
  117.  
  118.  
  119. /* Setup the menu bar and menu handles
  120.  */
  121. static void
  122. SetupMenus ()
  123. {
  124.     register short i;
  125.  
  126.     /* Install all MBar and hierarchical menus into the menu bar
  127.      */
  128.     for (i = 0; i < MAXMENU; i++) {
  129.         ApplSpinCurs(FALSE);
  130.  
  131.         if (menu[i] = GetMenu(RSRCBASE + i))
  132.             InsertMenu(menu[i], 0);
  133.     }
  134.     
  135.     /* Add the desk accessories, etc. from the System and draw the menu bar
  136.      */
  137.     if (menu[appleM])
  138.         AddResMenu(menu[appleM], 'DRVR');
  139.         
  140.     DrawMenuBar();
  141. }
  142.  
  143.  
  144. /* Initialize the application's windows
  145.  */
  146. static void
  147. SetupWindows ()
  148. {
  149.     /* Setup screen/window limits to fit this particular screen
  150.      */
  151.     growArea = screenBits.bounds;
  152.     growArea.top += GetMBarHeight();
  153.     dragArea = growArea;
  154.     InsetRect(&dragArea, 5, 5);
  155. }
  156.  
  157.  
  158. /* Initialize the application and process the Finder launch info
  159.  */
  160. void
  161. SetupAppl ()
  162. {
  163.     register Handle luchHdl = NULL;
  164.     register long waitTime;
  165.     EventRecord theEvent;
  166.     AppFile theFile;
  167.     short theMsg;
  168.     short nDocs;
  169.  
  170.     /* Allocate a reasonably huge heap for our bristling young memory hog
  171.      */
  172.     MaxApplZone();
  173.     MoreMasters();
  174.     MoreMasters();
  175.     MoreMasters();
  176.     MoreMasters();
  177.     MoreMasters();
  178.  
  179.     /* Required Mac Toolbox initialization calls
  180.      */
  181.     FlushEvents(everyEvent, 0);
  182.     InitGraf(&thePort);
  183.     InitCursor();
  184.     InitFonts();
  185.     InitWindows();
  186.     InitMenus();
  187.     TEInit();
  188.     InitDialogs(NULL);
  189.     GetDateTime((unsigned long *) &randSeed);
  190.  
  191.     /* Setup the application resources and data structures
  192.      */
  193.     SetupAntiBug();
  194.     SetupRsrc();
  195.  
  196.     SetupLaMachine();
  197.     ApplSpinCurs(TRUE);
  198.  
  199.     SetupMenus();
  200.     ApplSpinCurs(FALSE);
  201.  
  202.     SetupWindows();
  203.     ApplSpinCurs(FALSE);
  204.  
  205.     /* Application specific initialization
  206.      */
  207.     if (laMachine.hasColor)
  208.         InitPalettes();
  209.  
  210.     dPtrAnal = DlogWindow(analDlogID);
  211.     wPtrText = TextWindow(widText, 5, 5, TRUE);
  212.     wPtrGnos = ListWindow(widGnos, 0, 0, TRUE);
  213.  
  214.     wPtrTest = WindAllocate(widTest, TRUE);
  215.     TestSetup();
  216.     
  217.     /* Present an ominously threatening copyright notice, ie. show the about 
  218.      * dialog as a splash screen.
  219.      */
  220.     AboutAppl();
  221.     waitTime = TickCount() + 120L;
  222.  
  223.     /* Allow the user time to finish reading the copyright notice
  224.      */
  225.     while (TickCount() < waitTime) {
  226.         SystemTask();
  227.         ApplSpinCurs(FALSE);
  228.         (void) GetNextEvent(everyEvent, &theEvent);
  229.  
  230.         if (IsDialogEvent(&theEvent))
  231.             DlogModelessEvent(&theEvent);
  232.     }
  233.  
  234.     WindClose((WindowPtr) dPtrAbout);
  235.     WindSwitch(dPtrAnal, FALSE);
  236.  
  237.     /* Cheap hack to jam the user mode setting
  238.      */
  239.     if (luchHdl = GetResource(LUCH_TAG, RSRCBASE)) {
  240.         testUser = FALSE;
  241.         ReleaseResource(luchHdl);
  242.     }
  243.  
  244.     /* Get the finder launch info then open or print any launched documents
  245.      */
  246.     CountAppFiles(&theMsg, &nDocs);
  247.  
  248.     if (nDocs > 0) {
  249.         GetAppFiles(1, &theFile);
  250.         ApplSpinCurs(FALSE);
  251.  
  252.         DocOpenFile(wPtrGnos, theFile.fName, theFile.vRefNum);
  253.         ClrAppFiles(1);
  254.     }
  255.     else
  256.         DocNew(wPtrGnos);
  257. }
  258.