home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CGWorld.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  7.6 KB  |  257 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  20. //
  21. //    This is a replacement class for PowerPlant's LGWorld.  It allows for the
  22. //    resizing and reorienting of the offscreen world, which LGWorld won't let
  23. //    you do.  Because the CGWorld can be updated, the pixmap can be made
  24. //    purgeable by passing in the pixPurge flag in the constructor.  LGWorld
  25. //    would let you do this, however, if the pixels ever got purged you'd be
  26. //    completely hosed.
  27. //
  28. //    This class has ben submitted to metrowerks for a potential replacement
  29. //    of LGWorld.  In the event that metrowerks adds the extended functionality
  30. //    that CGWorld offers, we can make this module obsolete.
  31. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  32.  
  33. #ifdef PowerPlant_PCH
  34. #include PowerPlant_PCH
  35. #endif
  36.  
  37. #include "CGWorld.h"
  38. #include "UGraphicGizmos.h"
  39.  
  40. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  41. //    Ñ    CGWorld
  42. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  43.  
  44. CGWorld::CGWorld(
  45.     const Rect&        inBounds,
  46.     Int16             inPixelDepth,
  47.     GWorldFlags     inFlags,
  48.     CTabHandle         inCTableH,
  49.     GDHandle         inGDeviceH)
  50. {
  51.     ::GetGWorld(&mSavePort, &mSaveDevice);
  52.  
  53.     mMacGWorld = nil;
  54.     mBounds = inBounds;
  55.     mDepth = inPixelDepth;
  56.  
  57.         // NewGWorld interprets the bounds in global coordinates
  58.         // when specifying a zero pixel depth. It uses the maximum
  59.         // depth of all screen devices intersected by the bounds.
  60.     
  61.     Rect    gwRect = inBounds;
  62.     if (inPixelDepth == 0)
  63.         {
  64.         LocalToGlobal(&topLeft(gwRect));
  65.         LocalToGlobal(&botRight(gwRect));
  66.         }
  67.  
  68.     mGWorldStatus = ::NewGWorld(&mMacGWorld, inPixelDepth, &gwRect,
  69.                                 inCTableH, inGDeviceH, inFlags);
  70.                         
  71.     ThrowIfOSErr_(mGWorldStatus);
  72.     ThrowIfNil_(mMacGWorld);
  73.     
  74.         // Set up coordinates and erase pixels in GWorld
  75.     
  76.     ::SetGWorld(mMacGWorld, nil);
  77.     ::SetOrigin(mBounds.left, mBounds.top);
  78.     ::LockPixels(::GetGWorldPixMap(mMacGWorld));
  79.     ::EraseRect(&mBounds);
  80.     ::UnlockPixels(::GetGWorldPixMap(mMacGWorld));
  81.     ::SetGWorld(mSavePort, mSaveDevice);
  82. }
  83.  
  84. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  85. //    Ñ    CGWorld
  86. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  87. // This does not initialize the GWorld.  Must be overriden.
  88. CGWorld::CGWorld()
  89. {
  90. }
  91.  
  92. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  93. //    Ñ    ~CGWorld
  94. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  95.  
  96. CGWorld::~CGWorld()
  97. {
  98.     if (mMacGWorld != nil)
  99.         ::DisposeGWorld(mMacGWorld);
  100. }
  101.  
  102. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  103. //    Ñ    BeginDrawing
  104. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  105.  
  106. Boolean CGWorld::BeginDrawing(void)
  107. {
  108.     Boolean bReadyToDraw = false;
  109.  
  110.         // The mac GWolrd will be inconsistent if a previous call to UpdateGWorld()
  111.         // has failed.  We get one more shot a fixing that here before we draw.
  112.  
  113.     if (!IsConsistent())
  114.         AdjustGWorld(mBounds);
  115.     
  116.     if (IsConsistent())
  117.         {
  118.         if (!::LockPixels(::GetGWorldPixMap(mMacGWorld)))
  119.             {
  120.             // This means we're consistentlty sized and oriented, but our pixMap
  121.             // data has been purged.
  122.             AdjustGWorld(mBounds);
  123.             
  124.             if (IsConsistent())
  125.                 bReadyToDraw = ::LockPixels(::GetGWorldPixMap(mMacGWorld));
  126.             }
  127.         else
  128.             bReadyToDraw = true;
  129.         }
  130.  
  131.     if (bReadyToDraw)
  132.         {
  133.         ::GetGWorld(&mSavePort, &mSaveDevice);
  134.         ::SetGWorld(mMacGWorld, nil);
  135.         }
  136.         
  137.     return bReadyToDraw;
  138. }
  139.  
  140. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  141. //    Ñ    EndDrawing
  142. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  143.  
  144. void CGWorld::EndDrawing(void)
  145. {
  146.     Assert_(IsConsistent());            // You should have noticed that
  147.                                         // BeginDrawing() returned false
  148.                                         
  149.     ::UnlockPixels(::GetGWorldPixMap(mMacGWorld));
  150.     ::SetGWorld(mSavePort, mSaveDevice);
  151. }
  152.  
  153. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  154. //    Ñ    CopyImage
  155. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  156.  
  157. void CGWorld::CopyImage(
  158.     GrafPtr     inDestPort,
  159.     const Rect&    inDestRect,
  160.     Int16         inXferMode,
  161.     RgnHandle     inMaskRegion)
  162. {
  163.     ::CopyBits(&((GrafPtr)mMacGWorld)->portBits,
  164.                 &inDestPort->portBits,
  165.                 &mBounds, &inDestRect, inXferMode, inMaskRegion);
  166. }
  167.  
  168. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  169. //    Ñ    SetBounds
  170. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  171.  
  172. void CGWorld::SetBounds(const Rect &inBounds)
  173. {
  174.     if (::EqualRect(&inBounds, &mBounds))
  175.         return;
  176.     
  177.     UpdateBounds(inBounds);
  178.         
  179.         // Note that even if we're not consistent, we still remember the bounds so that
  180.         // the next time through BeginDrawing() we can attempt to realloc for the
  181.         // size it's supposed to be.
  182.         
  183.     mBounds = inBounds;        
  184.         
  185.         // We've reallocted successfully.  Now lets get the origin set properly.
  186.         
  187.     if (IsConsistent())
  188.         UpdateOrigin(topLeft(mBounds));
  189. }
  190.  
  191. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  192. //    Ñ    AdjustGWorld
  193. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  194.  
  195. void CGWorld::AdjustGWorld(const Rect& inBounds)
  196. {
  197.     UpdateBounds(inBounds);
  198.     if (IsConsistent())
  199.         UpdateOrigin(topLeft(inBounds));
  200. }
  201.  
  202. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  203. //    Ñ    UpdateBounds
  204. //
  205. //    Internal method which is called when the offscreen world needs to change
  206. //    it's dimensions.
  207. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  208.  
  209. void CGWorld::UpdateBounds(const Rect& inBounds)
  210. {
  211.     Rect theNewBounds;
  212.  
  213.     if (mDepth == 0)
  214.         {
  215.         theNewBounds = inBounds;
  216.         ::LocalToGlobal(&topLeft(theNewBounds));
  217.         ::LocalToGlobal(&botRight(theNewBounds));
  218.         }
  219.     else
  220.         theNewBounds = inBounds;
  221.     
  222.     GWorldFlags theFlags = ::UpdateGWorld(&mMacGWorld, mDepth, &theNewBounds, NULL, NULL, 0);
  223.                                                     
  224.     if (theFlags & gwFlagErr)
  225.         mGWorldStatus = ::QDError();
  226.     else
  227.         mGWorldStatus = noErr;
  228. }
  229.  
  230. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  231. //    Ñ    UpdateOrigin
  232. //
  233. //    Internal method in which the origin on the offscreen world needs to be
  234. //    offset to match the on-screen representation.
  235. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  236.  
  237. void CGWorld::UpdateOrigin(const Point& inOrigin)
  238. {
  239.     ::GetGWorld(&mSavePort, &mSaveDevice);
  240.     ::SetGWorld(mMacGWorld, nil);
  241.     ::SetOrigin(inOrigin.h, inOrigin.v);
  242.     ::SetGWorld(mSavePort, mSaveDevice);
  243. }
  244.  
  245. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  246. //    Ñ    
  247. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  248.  
  249. CSharedWorld::CSharedWorld(
  250.     const Rect&     inFrame,
  251.     Int16             inDepth,
  252.     GWorldFlags     inFlags)
  253.     :    CGWorld(inFrame, inDepth, inFlags)
  254. {
  255. }
  256.  
  257.