home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / MEMORYDC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  3.2 KB  |  143 lines

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