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

  1. program Clinton;
  2.     uses
  3.         Init, Sane, Sound, AppleEvents, QDOffscreen, Globals, Strings, MySpeech, Preferences, Watch, MenuStuff;
  4.  
  5.     const
  6.  
  7.  
  8.         RECT_TOP = 20;
  9.         RECT_LEFT = 20;
  10.  
  11.         SLEEP = 60;
  12.         WNE_TRAP_NUM = $60;
  13.         UNIMPL_TRAP_NUM = $9F;
  14.  
  15.         BYE_BYE_SPEECH = 3;
  16.         OCLOCK_SPEECH = 2;
  17.     var
  18.  
  19.         gTheEvent: EventRecord;
  20.  
  21.     procedure PlayMySound (resID: integer);
  22.         var
  23.             lFreeBytes: LONGINT;
  24.             iErr: OSErr;
  25.             mySndChan: SndChannelPtr;
  26.     begin
  27.         mySndChan := nil;
  28.         iErr := SndNewChannel(mySndChan, sampledSynth, initMono, nil);
  29.         if iErr = 0 then
  30.             begin
  31.  
  32.                 lFreeBytes := MaxBlock;
  33.                 if lFreeBytes > 20000 then
  34.                     begin
  35.                         lFreeBytes := 20000;
  36.                         iErr := SndStartFilePlay(mySndChan, 0, resID, lFreeBytes, nil, nil, nil, FALSE);
  37.                     end;
  38.                 if mySndChan <> nil then
  39.                     begin
  40.                         iErr := SndDisposeChannel(mySndChan, TRUE);
  41.                     end;
  42.             end;
  43.     end;
  44.  
  45.  
  46.  
  47.     procedure HandleActivate;
  48.         var
  49.             theWindow: WindowPtr;
  50.     begin
  51.         theWindow := WindowPtr(gTheEvent.message);
  52.         if (BitAnd(gTheEvent.modifiers, activeFlag) <> 0) then
  53.             begin
  54.                 SetPort(theWindow);
  55.             end;
  56.     end;
  57.  
  58.  
  59.     procedure HandleUpdate;
  60.         var
  61.             scratch: PixMapHandle;
  62.             worldRect: Rect;
  63.             theWindow: WindowPtr;
  64.     begin
  65.         theWindow := WindowPtr(gTheEvent.message);
  66.  
  67.         BeginUpdate(theWindow);
  68.         if theWindow = gWindow then
  69.             begin
  70.                 SetPort(theWindow);
  71.                 HLock(Handle(gFramePict));
  72.                 DrawPicture(gFramePict, gFramePict^^.picFrame);
  73.                 HUnlock(Handle(gFramePict));
  74.  
  75.                 if (CheckGWorlds) or (gScratchWorld = nil) then
  76.                     begin
  77.                         DrawTheWatch;
  78.                     end
  79.                 else (* the scratch world should hold a valid picture *)
  80.                     begin
  81.                         scratch := GetGWorldPixMap(gScratchWorld);
  82.                         if (LockPixels(scratch)) then
  83.                             begin
  84.                                 worldRect := gWatchPict^^.picFrame;
  85.                                 OffsetRect(worldRect, -worldRect.left, -worldRect.top);
  86.                                 CopyBits(BitMapHandle(scratch)^^, thePort^.portBits, worldRect, gWatchPict^^.picFrame, srcCopy, nil);
  87.                                 UnLockPixels(scratch);
  88.                             end
  89.                         else
  90.                             begin
  91.                                 DrawTheWatch;
  92.                             end;
  93.                     end;
  94.                 DrawMyStrings;
  95.                 if (gZoomCtl <> nil) then
  96.                     DrawControls(theWindow);
  97.             end;
  98.         EndUpdate(theWindow);
  99.     end;
  100.  
  101.  
  102.     procedure HandleNull;
  103.         var
  104.             currentTime, deltaTime: LONGINT;
  105.             hour: integer;
  106.             dateRec: DateTimeRec;
  107.             needed: Boolean;
  108.             oldPort: GrafPtr;
  109.             iErr: OSErr;
  110.     begin
  111.         GetDateTime(currentTime);
  112.         if gLastTime <> currentTime then
  113.             begin
  114.                 GetPort(oldPort);
  115.                 SetPort(gWindow);
  116.                 gLastTime := currentTime;
  117.                 deltaTime := gEndTime - currentTime;
  118.                 if (gEndTime < currentTime) and (gChimed = FALSE) then
  119.                     begin
  120.                         gChimed := TRUE;
  121.                         PlayMySound(BASE_RES_ID);
  122.                         if gSpeechAvailable then
  123.                             PlayMySpeech(BYE_BYE_SPEECH);
  124.                         InitTime;
  125.                         deltaTime := gEndTime - currentTime;
  126.                     end;
  127.                 Secs2Date(deltaTime, dateRec);
  128.  
  129.                 needed := DrawIfNeeded(dateRec.second, gRectArray[SECOND_NUM]);
  130.  
  131.                 if DrawIfNeeded(dateRec.minute, gRectArray[MINUTE_NUM]) = TRUE then
  132.                     begin
  133.  
  134.                         needed := DrawIfNeeded(dateRec.year - 1904, gRectArray[YEAR_NUM]);
  135.                         needed := DrawIfNeeded(dateRec.month - 1, gRectArray[MONTH_NUM]);
  136.                         needed := DrawIfNeeded(dateRec.day - 1, gRectArray[DAY_NUM]);
  137.                         if DrawIfNeeded(dateRec.hour, gRectArray[HOUR_NUM]) then
  138.                             begin
  139.                                 RefreshWatch; (* just redraw the watch with the proper day *)
  140.                                 if (gPreferences.sayHours) and (dateRec.minute = 59) then
  141.                                     begin
  142.                                         Secs2Date(currentTime, dateRec);
  143.                                         hour := dateRec.hour mod 12;
  144.                                         if hour = 0 then
  145.                                             hour := 12;
  146.                                         if gSpeechAvailable then
  147.                                             SayNumbers(hour, OCLOCK_SPEECH);
  148.                                     end;
  149.                             end
  150.                         else
  151.                             begin
  152.                                 PlayBites;
  153.                             end;
  154.  
  155.                         CalcHands(currentTime);
  156.                         DrawTheWatch;
  157.                         DrawControls(gWindow);
  158.                     end;
  159.                 SetPort(oldPort);
  160.             end;
  161.     end;
  162.  
  163.     procedure HandleZoom;
  164.         var
  165.             updateFlag: Boolean;
  166.             wide, high: integer;
  167.     begin
  168.         wide := gWindow^.portRect.right - gWindow^.portRect.left;
  169.         if (gZoomed) then
  170.             begin
  171.                 high := gFramePict^^.picFrame.top - 1;
  172.                 updateFlag := FALSE;
  173.                 gZoomed := FALSE;
  174.                 SetCtlValue(gZoomCtl, 0);
  175.             end
  176.         else
  177.             begin
  178.                 high := gFramePict^^.picFrame.bottom + 5;
  179.                 updateFlag := TRUE;
  180.                 gZoomed := TRUE;
  181.                 SetCtlValue(gZoomCtl, 1);
  182.             end;
  183.         SizeWindow(gWindow, wide, high, updateFlag);
  184.     end;
  185.  
  186.     procedure HandleMouseDown;
  187.         var
  188.             whichWindow: WindowPtr;
  189.             thePart: INTEGER;
  190.             dummy: Boolean;
  191.             menuChoice, windSize: LONGINT;
  192.             aControl: ControlHandle;
  193.             iCtlPart: integer;
  194.     begin
  195.         thePart := FindWindow(gTheEvent.where, whichWindow);
  196.         case thePart of
  197.             inMenuBar: 
  198.                 begin
  199.                     menuChoice := MenuSelect(gTheEvent.where);
  200.                     HandleMenuChoice(menuChoice);
  201.                 end;
  202.             inSysWindow: 
  203.                 SystemClick(gTheEvent, whichWindow);
  204.             inZoomIn: 
  205.                 if (TrackBox(gWindow, gTheEvent.where, inZoomIn)) then
  206.                     HandleZoom;
  207.             inZoomOut: 
  208.                 if (TrackBox(gWindow, gTheEvent.where, inZoomOut)) then
  209.                     HandleZoom;
  210.             inDrag: 
  211.                 begin
  212.                     DragWindow(whichWindow, gTheEvent.where, screenBits.bounds);
  213.                     dummy := CheckGWorlds;
  214.                 end;
  215.             inGoAway: 
  216.                 if (TrackGoAway(whichWindow, gTheEvent.where)) then
  217.                     begin
  218.                         gDone := TRUE;
  219.                     end;
  220.             inContent: 
  221.                 if whichWindow <> FrontWindow then
  222.                     begin
  223.                         SelectWindow(whichWindow);
  224.                     end
  225.                 else if whichWindow = gWindow then
  226.                     begin
  227.                         GlobalToLocal(gTheEvent.where);
  228.                         iCtlPart := FindControl(gTheEvent.where, gWindow, aControl);
  229.                         if ((iCtlPart = inCheckBox) and (aControl = gZoomCtl)) then
  230.                             begin
  231.                                 if inCheckBox = TrackControl(aControl, gTheEvent.where, nil) then
  232.                                     begin
  233.                                         HandleZoom;
  234.                                     end;
  235.                             end;
  236.                     end;
  237.         end;
  238.     end;
  239.  
  240.     procedure HandleEvent;
  241.         var
  242.             theChar: CHAR;
  243.             myErr: OSErr;
  244.     begin
  245.         case gTheEvent.what of
  246.             nullEvent: 
  247.                 HandleNull;
  248.             mouseDown: 
  249.                 HandleMouseDown;
  250.             kHighLevelEvent: 
  251.                 myErr := AEProcessAppleEvent(gTheEvent);
  252.             keyDown, autoKey: 
  253.                 begin
  254.                     theChar := CHR(BitAnd(gTheEvent.message, charCodeMask));
  255.                     if (BitAnd(gTheEvent.modifiers, cmdKey) <> 0) then
  256.                         HandleMenuChoice(MenuKey(theChar));
  257.                 end;
  258.             updateEvt: 
  259.                 begin
  260.                     HandleUpdate;
  261.                 end;
  262.             activateEvt: 
  263.                 HandleActivate;
  264.         end;
  265.     end;
  266. (* MainLoop, routine which keeps keeps calling for the next event and acting accordingly *)
  267.     procedure MainLoop;
  268.         var
  269.             wNEImplemented, dummy: Boolean;
  270.             endPoint: Point;
  271.             iErr: OSErr;
  272.     begin
  273.  
  274.         dummy := gPreferences.sayHours;
  275.         gPreferences.sayHours := FALSE;
  276.         HandleNull;
  277.         gPreferences.sayHours := dummy;
  278.         SetCursor(arrow);
  279.         HandleZoom;
  280.         ShowWindow(gWindow);
  281.         dummy := FindDepth;
  282.         DrawTheWatch;
  283.         while (gDone = FALSE) do
  284.             begin
  285.                 dummy := WaitNextEvent(everyEvent, gTheEvent, SLEEP, nil);
  286.                 HandleEvent;
  287.             end;
  288.         endPoint.h := 0;
  289.         endPoint.v := 0;
  290.         LocalToGlobal(endPoint);
  291.         gPreferences.zoomed := gZoomed;
  292.         gPreferences.location := endPoint;
  293.         SaveMyPrefs(gPreferences);
  294.     end;
  295.  
  296. begin (* main routine *)
  297.     if Initialize = TRUE then
  298.         begin
  299.             MainLoop;
  300.         end;
  301. end.