home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / OutOfContextMenus / Source / UOffscreenUtils.h < prev    next >
Encoding:
Text File  |  1999-06-22  |  2.0 KB  |  103 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    UOffscreenUtils.h                 ©1999 Eric Traut
  3. // ===========================================================================
  4.  
  5. /*------------------------------------------------------------------
  6.     StGWorldLocker
  7.     
  8.     This class sets up the GWorld for drawing. Its constructor
  9.     and destructor automatically handle the chore of saving and
  10.     restoring the old GWorld and GDevice.
  11. ------------------------------------------------------------------*/
  12.  
  13. class StGWorldLocker
  14. {
  15.     public:
  16.         inline
  17.         StGWorldLocker (GWorldPtr inGWorld, Boolean inSetGWorld = true)
  18.         {
  19.             mActiveGWorldPixMap = ::GetGWorldPixMap (inGWorld);
  20.             mSavedPixelsState = ::GetPixelsState (mActiveGWorldPixMap);
  21.             ::LockPixels (mActiveGWorldPixMap);
  22.             
  23.             if (inSetGWorld)
  24.             {
  25.                 ::GetGWorld (&mSavedGWorld, &mSavedDevice);
  26.                 ::SetGWorld (inGWorld, NULL);
  27.             }
  28.             else
  29.             {
  30.                 mSavedGWorld = NULL;
  31.                 mSavedDevice = NULL;
  32.             }
  33.         }
  34.         
  35.         inline
  36.         ~StGWorldLocker (void)
  37.         {
  38.             if (mSavedGWorld != NULL)
  39.                 ::SetGWorld (mSavedGWorld, mSavedDevice);
  40.             ::SetPixelsState (mActiveGWorldPixMap, mSavedPixelsState);
  41.         }
  42.         
  43.         inline UInt16
  44.         GetRowBytes(void)
  45.         {
  46.             return (mActiveGWorldPixMap[0]->rowBytes & 0x3FFF);
  47.         }
  48.         
  49.         inline UInt8 *
  50.         GetBasePointer(void)
  51.         {
  52.             return (UInt8 *)(mActiveGWorldPixMap[0]->baseAddr);
  53.         }
  54.         
  55.         inline PixMapHandle
  56.         GetPixMap(void)
  57.         {
  58.             return mActiveGWorldPixMap;
  59.         }
  60.         
  61.         inline void
  62.         RestoreOldPort(void)
  63.         {
  64.             if (mSavedGWorld != NULL)
  65.                 ::SetGWorld (mSavedGWorld, mSavedDevice);
  66.             mSavedGWorld = NULL;
  67.             mSavedDevice = NULL;
  68.         }
  69.         
  70.     private:
  71.         GWorldFlags        mSavedPixelsState;
  72.         PixMapHandle    mActiveGWorldPixMap;
  73.         GWorldPtr        mSavedGWorld;
  74.         GDHandle        mSavedDevice;
  75. };
  76.  
  77.  
  78. class StCPortSaver
  79. {
  80.     public:
  81.         StCPortSaver(void)
  82.         {
  83.             ::GetPort(&mSavedPort);
  84.         }
  85.         
  86.         ~StCPortSaver(void)
  87.         {
  88.             ::SetPort(mSavedPort);
  89.         }
  90.     
  91.         CGrafPtr
  92.         GetSavedPort(void)
  93.         {
  94.             return reinterpret_cast<CGrafPtr>(mSavedPort);
  95.         }
  96.     
  97.     private:
  98.         GrafPtr            mSavedPort;
  99. };
  100.  
  101.  
  102.  
  103.