home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / Kibitz / Init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-06  |  5.6 KB  |  163 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:            init.c
  5. ** Some code from:  Traffic Light 2.0 (2.0 version by Keith Rollin)
  6. ** Modified by:     Eric Soldan
  7. **
  8. ** Copyright © 1989-1992 Apple Computer, Inc.
  9. ** All rights reserved. */
  10.  
  11.  
  12.  
  13. /*****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  18. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  19. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  20.  
  21. #ifndef __ERRORS__
  22. #include <Errors.h>
  23. #endif
  24.  
  25. #ifndef __GESTALTEQU__
  26. #include <GestaltEqu.h>
  27. #endif
  28.  
  29. #ifndef __UTILITIES__
  30. #include <Utilities.h>
  31. #endif
  32.  
  33.  
  34.  
  35. /*****************************************************************************/
  36.  
  37.  
  38.  
  39. /* Set up the whole world, including global variables, Toolbox managers, and
  40. ** menus.  We also create our one application window at this time.  Since
  41. ** window storage is non-relocateable, how and when to allocate space for
  42. ** windows is very important so that heap fragmentation does not occur. */
  43.  
  44. /* The code that used to be part of ForceEnvirons has been moved into this
  45. ** module.  If an error is detected, instead of merely doing an ExitToShell,
  46. ** which leaves the user without much to go on, we call DeathAlert, which puts
  47. ** up a simple alert that just says an error occurred and then calls
  48. ** ExitToShell.  Since there is no other cleanup needed at this point if an
  49. ** error is detected, this form of error-handling is acceptable.  If more
  50. ** sophisticated error recovery is needed, an exception mechanism, such as is
  51. ** provided by Signals, can be used. */
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. /* NOTE:  The “g” prefix is used to emphasize that a variable is global. */
  60.  
  61. Boolean    gQuitApplication;        /* Set to 0 by Initialize. */
  62.                                 /* Checked by EventLoop. */
  63.  
  64. extern Boolean        gHasAppleEvents;
  65. extern RgnHandle    gCurrentCursorRgn;
  66.  
  67.  
  68. /*****************************************************************************/
  69. /*****************************************************************************/
  70.  
  71.  
  72.  
  73. #pragma segment Initialize
  74. void    Initialize(void)
  75. {
  76.     long        total, contig;
  77.  
  78.     StandardInitialization(1);            /* 1 MoreMasters. */
  79.  
  80.     /* Make sure that the machine has at least 128K ROMs.
  81.     ** If it doesn’t, exit. */
  82.     
  83.     if (gSystemVersion < 0x0700) DeathAlert(rBadNewsStrings, sWimpyMachine);
  84.     
  85.     /* We used to make a check for memory at this point by examining
  86.     ** ApplLimit, ApplicZone, and StackSpace and comparing that to the minimum
  87.     ** size we told MultiFinder we needed.  This did not work well because it
  88.     ** assumed too much about the relationship between what we asked
  89.     ** MultiFinder for and what we would actually get back, as well as how to
  90.     ** measure it.  Instead, we will use an alternate method comprised of
  91.     ** two steps. */
  92.      
  93.     /* It is better to first check the size of the application heap against a
  94.     ** value that you have determined is the smallest heap the application can
  95.     ** reasonably work in.  This number should be derived by examining the
  96.     ** size of the heap that is actually provided by MultiFinder when the
  97.     ** minimum size requested is used.  The derivation of the minimum size
  98.     ** requested from MultiFinder is described in Kibitz.h.  The check should
  99.     ** be made because the preferred size can end up being set smaller than
  100.     ** the minimum size by the user.  This extra check acts to ensure that
  101.     ** your application is starting from a solid memory foundation. */
  102.      
  103.     if ((long) GetApplLimit() - (long) ApplicationZone() < kMinHeap)
  104.         DeathAlert(rBadNewsStrings, sHeapTooSmall);
  105.  
  106.     /* Next, make sure that enough memory is free for your application to run.
  107.     ** It is possible for a situation to arise where the heap may have been of
  108.     ** required size, but a large scrap was loaded which left too little
  109.     ** memory.  To check for this, call PurgeSpace and compare the result with
  110.     ** a value that you have determined is the minimum amount of free memory
  111.     ** your application needs at initialization.  This number can be derived
  112.     ** several different ways.  One way that is fairly straightforward is to
  113.     ** run the application in the minimum size configuration as described
  114.     ** previously.  Call PurgeSpace at initialization and examine the value
  115.     ** returned.  However, you should make sure that this result is not being
  116.     ** modified by the scrap’s presence.  You can do that by calling ZeroScrap
  117.     ** before calling PurgeSpace.  Make sure to remove that call before
  118.     ** shipping, though. */
  119.     
  120.     /* ZeroScrap(); */
  121.  
  122.     PurgeSpace(&total, &contig);
  123.     if (total < kMinSpace)
  124.         DeathAlert(rBadNewsStrings, sNoFreeRoomInHeap);
  125.  
  126.     /* The extra benefit to waiting until after the Toolbox Managers have been
  127.     ** initialized to check memory is that we can now give the user an alert
  128.     ** to tell him/her what happened.  Although it is possible that the memory
  129.     ** situation could be worsened by displaying an alert, MultiFinder would
  130.     ** gracefully exit the application with an informative alert if memory
  131.     ** became critical.  Here we are acting more in a preventative manner to
  132.     ** avoid future disaster from low-memory problems. */
  133.  
  134.     StandardMenuSetup(rMenuBar, mApple);
  135.     AdjustMenus();
  136.  
  137.     InitAppleEvents();
  138.     InitCustomAppleEvents();
  139.  
  140.     if (InitOffscreen()) DeathAlert(rBadNewsStrings, sHeapTooSmall);
  141.     if (InitLogic())     DeathAlert(rBadNewsStrings, sHeapTooSmall);
  142.  
  143.     gCurrentCursorRgn  = NewRgn();        /* The current cursor region. */
  144.     DoSetCursor(&qd.arrow);
  145.  
  146.     qd.randSeed = TickCount();
  147.     gQuitApplication = false;    /* We are only starting.  Don't quit now! */
  148. }
  149.  
  150.  
  151.  
  152. /*****************************************************************************/
  153.  
  154.  
  155.  
  156. #pragma segment Initialize
  157. void    StartDocuments(void)
  158. {
  159. }
  160.  
  161.  
  162.  
  163.