home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Utilities / Siege Watch 2.0 / init.p < prev    next >
Encoding:
Text File  |  1994-04-23  |  8.0 KB  |  328 lines  |  [TEXT/PJMM]

  1. { Initialize.p functions related to starting up the program }
  2. unit Init;
  3. interface
  4.  
  5.     function Initialize: Boolean;
  6.     procedure InitTime;
  7. implementation
  8.     uses
  9.         Preferences, MySpeech, Watch, Utility, AppleEvents, MyAppleEvent, PPCToolBox, GestaltEqu;
  10.     const
  11.         WATCH_PICT_ID = BASE_RES_ID + 1;
  12.         BW_WATCH_PICT_ID = BASE_RES_ID + 2;
  13.         DAY_RECT_PICT_ID = BASE_RES_ID + 3;
  14.         ABOUT_ITEM = 1;
  15.         APPLE_MENU_ID = BASE_RES_ID;
  16.         FILE_MENU_ID = BASE_RES_ID + 1;
  17.         EDIT_MENU_ID = BASE_RES_ID + 2;
  18.         CLOCK_MENU_ID = BASE_RES_ID + 3;
  19.         DAYS_LEFT_SPEECH = 1;
  20.  
  21. (* InitTime, procedure to figure out the seconds value of the last second*)
  22. (*of the Clinton administration*)
  23.  
  24.     procedure InitTime;
  25.         var
  26.             dateRec: DateTimeRec;
  27.             time, endTime: LONGINT;
  28.             nextYear: integer;
  29.     begin
  30.         GetDateTime(time);
  31.         time := time - GetGMTDelta;
  32.         Secs2Date(time, dateRec);
  33.         nextYear := dateRec.year;
  34.         with dateRec do
  35.             begin
  36.                 month := 1;
  37.                 day := 20;
  38.                 hour := 17; {* this is Greenwich mean time *}
  39.                 minute := 0;
  40.                 second := 0;
  41.                 dayOfWeek := 1;
  42.             end;
  43.         case (nextYear mod 4) of
  44.             0: 
  45.                 nextYear := dateRec.year + 1;
  46.             1: 
  47.                 begin
  48.                     dateRec.year := nextYear;
  49.                     Date2Secs(dateRec, endTime);
  50.                     if time > endTime then
  51.                         begin
  52.                             nextYear := nextYear + 4;
  53.                         end;
  54.  
  55.                 end;
  56.             2: 
  57.                 nextYear := dateRec.year + 3;
  58.             3: 
  59.                 nextYear := dateRec.year + 2;
  60.         end;
  61.         dateRec.year := nextYear;
  62.         Date2Secs(dateRec, gEndTime);
  63.         gEndTime := gEndTime + GetGMTDelta;
  64.     end;
  65.  
  66. (* InitMyWindow, just load in my window and return TRUE if everything*)
  67. (*is ok *)
  68.     function InitMyWindow: Boolean;
  69.         var
  70.             startPoint, topPoint, endPoint: Point;
  71.             theGrayRgn: RgnHandle;
  72.     begin
  73.         if gColorAvailable then
  74.             begin
  75.                 gWindow := GetNewCWindow(BASE_RES_ID, nil, WindowPtr(-1));
  76.             end
  77.         else
  78.             begin
  79.                 gWindow := GetNewWindow(BASE_RES_ID, nil, WindowPtr(-1));
  80.             end;
  81.         if gWindow = nil then
  82.             begin
  83.                 InitMyWindow := FALSE;
  84.             end
  85.         else
  86.             begin
  87.                 SetPort(gWindow);
  88.                 startPoint := gPreferences.location;
  89.                 if (startPoint.h = 0) and (startPoint.v = 0) then
  90.                     begin
  91.                         LocalToGlobal(startPoint);
  92.                     end
  93.                 else
  94.                     begin
  95.                         startPoint := gPreferences.location;
  96.                     end;
  97. { is the saved window position valid ? }
  98.                 theGrayRgn := GetGrayRgn;
  99.                 endPoint := startPoint;
  100.                 endPoint.h := endPoint.h + 25;
  101.                 endPoint.v := endPoint.v + 25;
  102.                 topPoint := startPoint;
  103.                 topPoint.v := topPoint.v - 10;
  104.                 if (PtInRgn(endPoint, theGrayRgn)) and (PtInRgn(topPoint, theGrayRgn)) then
  105.                     begin
  106.                         MoveWindow(gWindow, startPoint.h, startPoint.v, TRUE);
  107.                     end;
  108.                 if gPreferences.zoomed = FALSE then
  109.                     begin
  110.                         gZoomed := TRUE;
  111.                     end
  112.                 else
  113.                     gZoomed := FALSE;
  114. { the flag control is used to control zooming }
  115.                 gZoomCtl := GetNewControl(BASE_RES_ID, gWindow);
  116.                 InitMyWindow := TRUE;
  117.             end;
  118.     end;
  119.  
  120. { we store the time strings as handles with starting points }
  121. { we also load the pictures in here too }
  122.     function InitMyStrings: Boolean;
  123.         var
  124.             fontHeight, i, cellHeight, width: integer;
  125.             fInfo: FontInfo;
  126.             currPoint: Point;
  127.             tempString: StringHandle;
  128.             ok: Boolean;
  129.             pictRect, erRect: Rect;
  130.             aString: Str255;
  131.     begin
  132.         ok := TRUE;
  133.         GetFontInfo(fInfo);
  134.         fontHeight := fInfo.ascent + fInfo.descent;
  135.  
  136.         gFramePict := GetPicture(BASE_RES_ID);
  137.         gDayRectPict := GetPicture(DAY_RECT_PICT_ID);
  138.         if gColorAvailable then
  139.             begin
  140.                 gWatchPict := GetPicture(WATCH_PICT_ID);
  141.             end
  142.         else
  143.             begin
  144.                 gWatchPict := GetPicture(BW_WATCH_PICT_ID);
  145.             end;
  146.  
  147.         if (gFramePict <> nil) and (gWatchPict <> nil) and (gDayRectPict <> nil) then
  148.             begin
  149.                 pictRect := gFramePict^^.picFrame;
  150.                 width := pictRect.right - pictRect.left;
  151.                 cellHeight := pictRect.bottom - pictRect.top;
  152.                 gFramePict^^.picFrame := pictRect;
  153.                 cellHeight := (pictRect.bottom - pictRect.top) div NUM_FIELDS;
  154.                 aString := '888';
  155.                 width := StringWidth(aString);
  156.                 currPoint.h := pictRect.right - width - 5;
  157.                 currPoint.v := pictRect.top + fInfo.ascent + 3;
  158.                 erRect.left := currPoint.h;
  159.                 erRect.right := currPoint.h + width - 1;
  160.                 pictRect := gWatchPict^^.picFrame;
  161.                 width := (pictRect.right - pictRect.left) div 2;
  162.                 gLongHandLen := width - 7;
  163.                 gShortHandLen := width div 2 - 2;
  164.                 gLongHandLenSquared := gLongHandLen;
  165.                 gLongHandLenSquared := gLongHandLenSquared * gLongHandLenSquared;
  166.                 gShortHandLenSquared := gShortHandLen;
  167.                 gShortHandLenSquared := gShortHandLenSquared * gShortHandLenSquared;
  168.                 CalcCenter(FALSE);
  169.  
  170.             end
  171.         else
  172.             begin
  173.                 ok := FALSE;
  174.             end;
  175.         i := 1;
  176.         while (i <= NUM_FIELDS) and (ok = TRUE) do
  177.             begin
  178.                 tempString := StringHandle(NewHandleClear(12));
  179.                 with gRectArray[i] do
  180.                     begin
  181.                         startPnt := currPoint;
  182.                         erRect.top := currPoint.v - fInfo.ascent;
  183.                         erRect.bottom := currPoint.v + fInfo.descent;
  184.                         blankRect := erRect;
  185.                         theString := tempString;
  186.                         lastValue := 99;
  187.                     end;
  188.                 currPoint.v := currPoint.v + cellHeight;
  189.                 if tempString = nil then
  190.                     begin
  191.                         ok := FALSE;
  192.                     end;
  193.                 i := i + 1;
  194.             end;
  195.         InitMyStrings := ok;
  196.     end;
  197.  
  198. (* Initialize, function that loads up the window and inits all the*)
  199. (*pertinent global variables.*)
  200.     function InitMyMenu: Boolean;
  201.         var
  202.             myMenuBar: Handle;
  203.             aMenu: MenuHandle;
  204.     begin
  205.         InitMyMenu := TRUE;
  206.         myMenuBar := GetNewMBar(BASE_RES_ID);
  207.         if myMenuBar <> nil then
  208.             begin
  209.                 SetMenuBar(myMenuBar);
  210.                 DisposHandle(myMenuBar);
  211.                 aMenu := GetMenu(CLOCK_MENU_ID);
  212.                 if gSpeechAvailable = FALSE then
  213.                     begin
  214.                         DisableItem(aMenu, 0);
  215.                     end;
  216.  
  217.                 aMenu := GetMHandle(APPLE_MENU_ID);
  218.                 AddResMenu(aMenu, 'DRVR');
  219.                 DrawMenuBar;
  220.             end
  221.         else
  222.             begin
  223.                 InitMyMenu := FALSE;
  224.             end;
  225.  
  226.     end;
  227. { we use the PPC toolbox to communicate with the daemon }
  228.     function InitMyPPC: Boolean;
  229.         var
  230.             PPCAttributes: LONGINT;
  231.             err: OSErr;
  232.     begin
  233.         err := Gestalt(gestaltPPCToolboxAttr, PPCAttributes);
  234.         if (err = noErr) then
  235.             begin
  236.                 if BitTst(@PPCAttributes, 31 - gestaltPPCSupportsRealTime) then
  237.                     begin
  238.                         err := PPCInit;
  239.                     end;
  240.             end;
  241.         if err = noErr then
  242.             begin
  243.                 InitMyPPC := TRUE;
  244.             end
  245.         else
  246.             begin
  247.                 InitMyPPC := FALSE;
  248.             end;
  249.     end;
  250.     function InitEnvirons: Boolean;
  251.         var
  252.             theSysEnv: SysEnvRec;
  253.             iErr: OSErr;
  254.             dummy: integer;
  255.             version: integer;
  256.             globRect: Rect;
  257.             gdh: GDHandle;
  258.             theDevPixMap: PixMapHandle;
  259.             response: LONGINT;
  260.     begin
  261.         gRadDegRatio := 0.0174532952;
  262.         gDone := FALSE;
  263.         gZoomed := TRUE;
  264.         iErr := SysEnvirons(1, theSysEnv);
  265.         version := BitShift(theSysEnv.systemVersion, -8);
  266.         if (iErr = 0) and (version >= 7) and (InitMyPPC) then
  267.             begin
  268.                 InitEnvirons := TRUE;
  269.                 gFPUAvailable := theSysEnv.hasFPU;
  270.                 gColorAvailable := theSysEnv.hasColorQD;
  271.                 gSpeechAvailable := IsSpeechAvailable;
  272.  
  273.             end
  274.         else
  275.             begin
  276. { we can't run in this environment }
  277.                 dummy := StopAlert(401, nil);
  278.                 InitEnvirons := FALSE;
  279.             end;
  280.     end;
  281.  
  282.     procedure InitPrefs;
  283.     begin
  284.         DoPrefDefault(gPreferences);
  285.         GetPreferences(gPreferences);
  286.     end;
  287.     procedure InstallAppEvents;
  288.         var
  289.             myErr: OSErr;
  290.     begin
  291.         myErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleODOC, 0, FALSE);
  292.         myErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQuit, 0, FALSE);
  293.         myErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandlePDOC, 0, FALSE);
  294.         myErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOApp, 0, FALSE);
  295.     end;
  296.     function Initialize: Boolean;
  297.         var
  298.             dummy: Boolean;
  299.             days: LONGINT;
  300.     begin
  301.         InitTime;
  302.         InitPrefs;
  303.         if (InitEnvirons = FALSE) or (InitMyWindow = FALSE) or (InitMyStrings = FALSE) or (InitMyMenu = FALSE) then
  304.             begin
  305.                 Initialize := FALSE;
  306.             end
  307.         else
  308.             begin
  309.                 ValidatePrefs(gPreferences);
  310.                 GetDateTime(days);
  311.                 randSeed := days;
  312.                 if gPreferences.sayDays then
  313.                     begin
  314.  
  315.                         days := abs(days - gEndTime);
  316.                         days := days + 12 * 60 * 60;
  317.                         days := days div (86400);
  318.                         if gSpeechAvailable then
  319.                             SayNumbers(days, DAYS_LEFT_SPEECH);
  320.                     end;
  321.                 InstallAppEvents;
  322.                 dummy := FindDepth;
  323.                 dummy := InitMyGWorlds; (* if this fails, I'll just draw the face manually *)
  324.                 gChimed := FALSE;
  325.                 Initialize := TRUE;
  326.             end;
  327.     end;
  328. end.