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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   source\owl\brush.cpp
  4. //   Implementation for GDI Brush object
  5. //----------------------------------------------------------------------------
  6. #include <owl\owlpch.h>
  7. #include <owl\gdiobjec.h>
  8.  
  9. DIAG_DECLARE_GROUP(OwlGDI);        // General GDI diagnostic group
  10.  
  11. //
  12. // internal brush cache to keep most common brushes around cached by color
  13. //
  14.  
  15. class TBrushCache {
  16.   public:
  17.     struct TEntry {
  18.       HBRUSH  Handle;
  19.       TColor  Color;
  20.       
  21.       TEntry(HANDLE handle) {Handle = (HBRUSH)handle;}
  22.     };
  23.     
  24.     TBrushCache(TEntry* entries, int numEntries);
  25.     HBRUSH Lookup(TColor color);
  26.  
  27.   private:
  28.     TEntry*  Entries;
  29.     int      NumEntries;
  30. };
  31.  
  32. TBrushCache::TBrushCache(TEntry* entries, int numEntries)
  33. {
  34.   Entries = entries;
  35.   NumEntries = numEntries;
  36.  
  37.   for (int i = 0; i < NumEntries; i++) {
  38.     LOGBRUSH logBrush;
  39.     if (GetObject(Entries[i].Handle, sizeof(LOGBRUSH), &logBrush))
  40.       Entries[i].Color = logBrush.lbColor;
  41.   }
  42. }
  43.  
  44. HBRUSH
  45. TBrushCache::Lookup(TColor color)
  46. {
  47.   for (int i = 0; i < NumEntries; i++)
  48.     if (color == Entries[i].Color)
  49.       return Entries[i].Handle;
  50.   return 0;
  51. }
  52.  
  53. //
  54. // Static instance of a brush cache table & the cache itself
  55. //
  56. static TBrushCache::TEntry BrushCacheEntries[] = {
  57.   {::GetStockObject(BLACK_BRUSH)},
  58.   {::GetStockObject(DKGRAY_BRUSH)},
  59.   {::GetStockObject(GRAY_BRUSH)},
  60.   {::GetStockObject(LTGRAY_BRUSH)},
  61.   {::GetStockObject(WHITE_BRUSH)}
  62. };
  63. static TBrushCache BrushCache(BrushCacheEntries, COUNTOF(BrushCacheEntries));
  64.  
  65. //
  66. // Constructors
  67. //
  68. TBrush::TBrush(HBRUSH handle, TAutoDelete autoDelete)
  69.   : TGdiObject(handle, autoDelete)
  70. {
  71.   #if !defined(NO_GDI_ORPHAN_CONTROL)
  72.     if (ShouldDelete)
  73.       OBJ_REF_ADD(Handle, Brush);
  74.   #endif
  75. }
  76.  
  77. TBrush::TBrush(TColor color)
  78. {
  79.   if ((Handle = BrushCache.Lookup(color)) != 0) {
  80.     ShouldDelete = FALSE;
  81.     return;
  82.   }
  83.   Handle = ::CreateSolidBrush(color);
  84.   WARNX(OwlGDI, !Handle, 0, "Cannot create solid TBrush " << hex << color);
  85.   CheckValid();
  86.   OBJ_REF_ADD(Handle, Brush);
  87. }
  88.  
  89. TBrush::TBrush(TColor color, int style)
  90. {
  91.   Handle = ::CreateHatchBrush(style, color);
  92.   WARNX(OwlGDI, !Handle, 0, "Cannot create hatch TBrush " << hex << color <<
  93.         " " << style);
  94.   CheckValid();
  95.   OBJ_REF_ADD(Handle, Brush);
  96. }
  97.  
  98. TBrush::TBrush(const TBitmap& pattern)
  99. {
  100.   Handle = ::CreatePatternBrush(pattern);
  101.   WARNX(OwlGDI, !Handle, 0, "Cannot create pattern TBrush from bitmap " <<
  102.         hex << (UINT)(HBITMAP)pattern);
  103.   CheckValid();
  104.   OBJ_REF_ADD(Handle, Brush);
  105. }
  106.  
  107. TBrush::TBrush(const TDib& pattern)
  108. {
  109.   #if defined(__WIN32__)
  110.     Handle = ::CreateDIBPatternBrushPt((LPVOID)(const BITMAPINFO far*)pattern,
  111.                                        pattern.Usage());
  112.   #else
  113.     Handle = ::CreateDIBPatternBrush(pattern, pattern.Usage());
  114.   #endif
  115.   WARNX(OwlGDI, !Handle, 0, "Cannot create pattern TBrush from DIB " <<
  116.         hex << (UINT)(HANDLE)pattern);
  117.   CheckValid();
  118.   OBJ_REF_ADD(Handle, Brush);
  119. }
  120.  
  121. TBrush::TBrush(const LOGBRUSH far* logBrush)
  122. {
  123.   PRECONDITION(logBrush);
  124.   Handle = ::CreateBrushIndirect((LPLOGBRUSH)logBrush);  // API cast
  125.   WARNX(OwlGDI, !Handle, 0, "Cannot create TBrush from logBrush @" <<
  126.         hex << DWORD(LPVOID(logBrush)));
  127.   CheckValid();
  128.   OBJ_REF_ADD(Handle, Brush);
  129. }
  130.  
  131. TBrush::TBrush(const TBrush& src)
  132. {
  133.   LOGBRUSH logBrush;
  134.   src.GetObject(logBrush);
  135.   Handle = ::CreateBrushIndirect(&logBrush);
  136.   WARNX(OwlGDI, !Handle, 0, "Cannot create TBrush from TBrush @" <<
  137.         hex << DWORD(LPVOID(&src)));
  138.   CheckValid();
  139.   OBJ_REF_ADD(Handle, Brush);
  140. }
  141.