home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Pascal / Applications / Flight Stability / Other Source / CBitmapWindow.p next >
Encoding:
Text File  |  1995-07-05  |  8.7 KB  |  202 lines  |  [TEXT/PJMM]

  1. ***********************************************************************}
  2. {        MakeMacWindow (OVERRIDE) }
  3. {}
  4. {        Create a new Toolbox window record using the specified WIND }
  5. {        resource ID. This method creates a non-color window. }
  6. {}
  7. { ******************************************************************************)
  8.  
  9.     procedure CBitmapWindow.MakeMacWindow (WINDid: Integer);
  10.  
  11.         var
  12.             savedAlloc: Boolean;
  13.             behind: WindowPtr;
  14.  
  15.     begin { MakeMacWindow }
  16.         savedAlloc := SetAllocation(kAllocCanFail);
  17.  
  18.         if floating then begin
  19.             behind := WindowPtr(-1);
  20.         end { if }
  21.         else begin
  22.             behind := nil;
  23.         end; { else }
  24.  
  25.         macPort := GetNewWindow(WINDid, nil, behind);
  26.  
  27.         savedAlloc := SetAllocation(savedAlloc);
  28.  
  29.         FailNIL(macPort);
  30.     end; { MakeMacWindow }
  31.  
  32.  
  33. (******************************************************************************}
  34. {}
  35. {        ChangeSize (OVERRIDE) }
  36. {}
  37. {        Change the size of a window by specifying the width and height }
  38. {        in pixels. Window cannot be made larger than the maximum size }
  39. {        specified by its sizeRect instance variable. }
  40. {}
  41. { ******************************************************************************)
  42.  
  43.     procedure CBitmapWindow.ChangeSize (width, height: Integer);
  44.  
  45.         var
  46.             theQDHandle: QDHandle;
  47.  
  48.     begin { ChangeSize }
  49.         { Our offscreen bitmap is no longer the right size. }
  50.         { Remember - Never pass an instance variable by reference. }
  51.         theQDHandle := screenData;
  52.         screenData := nil;
  53.         ForgetHandle(theQDHandle);
  54.  
  55.         inherited ChangeSize(width, height);
  56.     end; { ChangeSize }
  57.  
  58.  
  59. (******************************************************************************}
  60. {}
  61. {        Zoom (OVERRIDE) }
  62. {}
  63. {        Zoom a window out to a standard size or in to its original size. }
  64. {}
  65. {******************************************************************************)
  66.  
  67.     procedure CBitmapWindow.Zoom (direction: integer);
  68.  
  69.         var
  70.             theQDHandle: QDHandle;
  71.  
  72.     begin { Zoom }
  73.         { Our offscreen bitmap is no longer the right size. }
  74.         { Remember - Never pass an instance variable by reference. }
  75.         theQDHandle := screenData;
  76.         screenData := nil;
  77.         ForgetHandle(theQDHandle);
  78.  
  79.         inherited Zoom(direction);
  80.     end; { Zoom }
  81.  
  82.  
  83. (******************************************************************************}
  84. {}
  85. {        MakeOffscreen }
  86. {}
  87. {        Initialize the offscreen bitmap. }
  88. {}
  89. {        For further discussion, refer to THINK Reference Ver2.00, "Creating Offscreen Bitmaps". }
  90. { ******************************************************************************)
  91.  
  92.     procedure CBitmapWindow.MakeOffscreen;
  93.  
  94.     begin {     MakeOffscreen }
  95.         { We don't want to call this twice. }
  96.         ASSERT(screenData = nil);
  97.  
  98.         { We make an offscreen BitMap as large as the window. }
  99.         offscreen.bounds := macPort^.portRect;
  100.  
  101.         { rowBytes is size of row, rounded up to an even number of bytes. }
  102.         offscreen.rowBytes := BSL(BSR((offscreen.bounds.right - offscreen.bounds.left + 15), 4), 1);
  103.  
  104.         { To minimize clogging of the heap, we allocate the offscreen memory here }
  105.         { as a handle. When we draw, we will lock the handle and set offscreen.baseAddr }
  106.         { to point to the block which is being handled. }
  107.         screenData := QDHandle(NewHandleCanFail(longint(offscreen.rowBytes * (offscreen.bounds.bottom - offscreen.bounds.top))));
  108.  
  109.         FailNIL(screenData);
  110.     end; {     MakeOffscreen }
  111.  
  112.  
  113. (******************************************************************************}
  114. {        Update}
  115. {}
  116. {        Update the contents of a window using an offscreen BitMap. (Because we’re }
  117. {        using a BitMap, the window cannot be a color GrafPort). Since Panes refer }
  118. {        to macPort, and expect it to be a WindowPeek, we can’t simply replace the port, }
  119. {        so we replace its portBits. }
  120. {}
  121. {        Drawing seems substantially slower, but it’s without flicker. }
  122. {}
  123. {******************************************************************************)
  124.  
  125.     procedure CBitmapWindow.Update;
  126.  
  127.         var
  128.             savePort: GrafPtr;                { The current port. }
  129.             updateRect: Rect;                    { Bounding box of the update region. }
  130.             savePortBits: BitMap;        { Window's original portBits. }
  131.  
  132.     begin { Update }
  133.         { Save the original port. }
  134.         GetPort(savePort);
  135.  
  136.         { Set up the Window's port. }
  137.         Prepare;
  138.  
  139.         { If we don't have an offscreen bitmap built yet, make it. }
  140.         if screenData = nil then begin
  141.             MakeOffscreen;
  142.         end; { if }
  143.         ASSERT(screenData <> nil);
  144.  
  145.         { Lock the block of screen data, and set the offscreen port }
  146.         { to point to it. }
  147.         HLockHi(screenData);
  148.         offscreen.baseAddr := screenData^;
  149.  
  150.         { Save the previous bitmap. }
  151.         savePortBits := thePort^.portBits;
  152.  
  153.         { Draw offscreen. }
  154.         { Make sure that the window is at corner. }
  155.         SetPortBits(offscreen);
  156.         MovePortTo(0, 0);
  157.  
  158.         { Get the bounding box of the update region, so we can draw only the panes }
  159.         { that intersect it. It's still in screen coordinates, so we have to offset it. }
  160.         updateRect := WindowPeek(macPort)^.updateRgn^^.rgnBBox;
  161.         OffsetRect(updateRect, savePortBits.bounds.left, savePortBits.bounds.top);
  162.  
  163.         { Draw all subviews. }
  164.         if (itsSubviews <> nil) then begin
  165.             { Clip to the update region. }
  166.             ClipRect(updateRect);
  167.  
  168.             { Erase the update region. }
  169.             EraseRect(updateRect);
  170.  
  171.             itsSubviews.DoForEach1(Pane_Draw, @updateRect);
  172.         end; { if }
  173.  
  174.         { Restore the previous bitmap. }
  175.         SetPortBits(savePortBits);
  176.         MovePortTo(-savePortBits.bounds.left, -savePortBits.bounds.top);
  177.  
  178.         { Start the update process. }
  179.         BeginUpdate(macPort);
  180.  
  181.         { Clip to the entire window. }
  182.         SetOrigin(0, 0);
  183.         ClipRect(thePort^.portRect);
  184.  
  185.         { Copy from offscreen to the update region. }
  186.         CopyBits(offscreen, savePortBits, updateRect, updateRect, srcCopy, nil);
  187.  
  188.         { End the update process. }
  189.         EndUpdate(macPort);
  190.  
  191.         { Restore the original port. }
  192.         SetPort(savePort);
  193.  
  194.         ForceNextPrepare;
  195.  
  196.         { Unlock the block of screen data, and reset the offscreen port. }
  197.         offscreen.baseAddr := nil;
  198.         HUnlock(screenData);
  199.     end; { Update }
  200.  
  201.  
  202. end. { CBitmapWindow }