home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / MEMORYDC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  3.2 KB  |  147 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TMemoryDC and TDibDC encapsulation classes
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/dc.h>
  9.  
  10. DIAG_DECLARE_GROUP(OwlGDI);        // General GDI diagnostic group
  11.  
  12. const int MemDCCacheSize = 2;      // Number of mem DCs allocated in the cache
  13.  
  14. //
  15. // Cache of screen-compatible memory HDCs used automatically by TMemoryDC when
  16. // the default ctor is called. Reduces the amount of HDC creations & allows
  17. // centralized sharing of these memory HDCs
  18. //
  19. class TMemDCCache {
  20.   public:
  21.     struct TEntry {
  22.       HDC   Handle;
  23.       bool  Busy;
  24.     };
  25.     
  26.     TMemDCCache(int numEntries);
  27.    ~TMemDCCache();
  28.     HDC  Get();
  29.     void Release(HDC hDC);
  30.  
  31.   private:
  32.     TEntry*  Entries;
  33.     int      NumEntries;
  34. };
  35.  
  36. TMemDCCache::TMemDCCache(int numEntries)
  37. {
  38.   NumEntries = numEntries;
  39.   Entries = new TEntry[NumEntries];
  40.  
  41.   for (int i = 0; i < NumEntries; i++) {
  42.     Entries[i].Handle = ::CreateCompatibleDC(0);
  43.     Entries[i].Busy = false;
  44.   }
  45. }
  46.  
  47. TMemDCCache::~TMemDCCache()
  48. {
  49.   for (int i = 0; i < NumEntries; i++) {
  50.     WARNX(OwlGDI, Entries[i].Busy, 0, "Unreleased DC " << 
  51.           hex << uint(Entries[i].Handle) << " in MemDCCache");
  52.     if (Entries[i].Handle)
  53.       ::DeleteDC(Entries[i].Handle);
  54.   }
  55.   delete [] Entries;
  56. }
  57.  
  58. HDC
  59. TMemDCCache::Get()
  60. {
  61.   for (int i = 0; i < NumEntries; i++)
  62.     if (!Entries[i].Busy) {
  63.       Entries[i].Busy = true;
  64.       return Entries[i].Handle;
  65.     }
  66.   return 0;
  67. }
  68.  
  69. void
  70. TMemDCCache::Release(HDC hDC)
  71. {
  72.   for (int i = 0; i < NumEntries; i++)
  73.     if (Entries[i].Handle == hDC) {
  74.       WARNX(OwlGDI, !Entries[i].Busy, 0, "Releasing non-busy DC " << 
  75.             hex << uint(Entries[i].Handle) << " in MemDCCache");
  76.       Entries[i].Busy = false;
  77.       return;
  78.     }
  79. }
  80.  
  81. static TMemDCCache MemDCCache(MemDCCacheSize);
  82.  
  83. //
  84. // construct a screen-compatible memory DC. Try to get one from the cache first
  85. //
  86. TMemoryDC::TMemoryDC()
  87. :
  88.   TCreatedDC()
  89. {
  90.   Handle = MemDCCache.Get();
  91.   if (Handle)
  92.     ShouldDelete = false;
  93.  
  94.   else {
  95.     Handle = ::CreateCompatibleDC(0);
  96.     CheckValid();
  97.   }
  98.   OrgBitmap = 0;
  99. }
  100.  
  101. TMemoryDC::TMemoryDC(const TDC& dc) : TCreatedDC()
  102. {
  103.   Handle = ::CreateCompatibleDC(dc);
  104.   CheckValid();
  105.   OrgBitmap = 0;
  106. }
  107.  
  108. //
  109. // Use an existing HDC. Delete it on destruction as requested. Allow bitmap
  110. // selection
  111. //
  112. TMemoryDC::TMemoryDC(HDC handle, TAutoDelete autoDelete)
  113. :
  114.   TCreatedDC(handle, autoDelete)
  115. {
  116. }
  117.  
  118. void
  119. TMemoryDC::SelectObject(const TBitmap& bitmap)
  120. {
  121.   HBITMAP oldBitmap = (HBITMAP)::SelectObject(HDC(Handle), bitmap);
  122.   if (oldBitmap) {
  123.     OBJ_REF_INC(bitmap);
  124.     if ((bool)oldBitmap > 1)
  125.       if (!OrgBitmap)
  126.         OrgBitmap = oldBitmap;
  127.       else
  128.         OBJ_REF_DEC(oldBitmap, false);
  129.   }
  130. }
  131.  
  132. void
  133. TMemoryDC::RestoreBitmap()
  134. {
  135.   if (OrgBitmap) {
  136.     OBJ_REF_DEC(::SelectObject(HDC(Handle), OrgBitmap), false);
  137.     OrgBitmap = 0;
  138.   }
  139. }
  140.  
  141. TMemoryDC::~TMemoryDC()
  142. {
  143.   RestoreBitmap();
  144.   RestoreObjects();
  145.   MemDCCache.Release(HDC(Handle));
  146. }
  147.