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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   source\owl\bitmap.cpp
  4. //   Implementation of GDI Bitmap object class
  5. //----------------------------------------------------------------------------
  6. #include <owl\owlpch.h>
  7. #include <owl\gdiobjec.h>
  8. #include <owl\metafile.h>
  9. #include <owl\clipboar.h>
  10.  
  11. DIAG_DECLARE_GROUP(OwlGDI);        // General GDI diagnostic group
  12. DIAG_DECLARE_GROUP(OwlGDIOrphan);  // Orphan control tracing group
  13.  
  14. //
  15. // Construct an alias TBitmap for an existing bitmap handle
  16. //
  17. TBitmap::TBitmap(HBITMAP handle, TAutoDelete autoDelete)
  18.   : TGdiObject(handle, autoDelete)
  19. {
  20.   #if !defined(NO_GDI_ORPHAN_CONTROL)
  21.     if (ShouldDelete)
  22.       OBJ_REF_ADD(Handle, Bitmap);
  23.   #endif
  24. }
  25.  
  26. TBitmap::TBitmap()
  27.   : TGdiObject()
  28. {
  29. }
  30.  
  31. TBitmap::TBitmap(const TClipboard& clipboard)
  32.   : TGdiObject(clipboard.GetClipboardData(CF_BITMAP))
  33. {
  34.   OBJ_REF_ADD(Handle, Bitmap);
  35.   OBJ_REF_INC(Handle);
  36. }
  37.  
  38. TBitmap::TBitmap(int width, int height, BYTE planes, BYTE bitCount, void far* bits)
  39. {
  40.   Handle = ::CreateBitmap(width, height, planes, bitCount, bits);
  41.   WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap (" << width << "x" <<
  42.         height << "x" << planes << ")");
  43.   CheckValid();
  44.   OBJ_REF_ADD(Handle, Bitmap);
  45. }
  46.  
  47. TBitmap::TBitmap(const BITMAP far* bitmap)
  48. {
  49.   PRECONDITION(bitmap);
  50.   Handle = ::CreateBitmapIndirect((LPBITMAP)bitmap);  // API cast
  51.   WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from BITMAP @" <<
  52.         hex << DWORD(LPVOID(bitmap)));
  53.   CheckValid();
  54.   OBJ_REF_ADD(Handle, Bitmap);
  55. }
  56.  
  57. TBitmap::TBitmap(const TDC& dc, int width, int height, BOOL discardable)
  58. {
  59.   if (discardable) {
  60.     Handle = ::CreateDiscardableBitmap(dc, width, height);
  61.     WARNX(OwlGDI, !Handle, 0, "Cannot create discardable bitmap (" << width <<
  62.           "x" << height << ") for " << hex << (UINT)(HDC)dc);
  63.   } else {
  64.     Handle = ::CreateCompatibleBitmap(dc, width, height);
  65.     WARNX(OwlGDI, !Handle, 0, "Cannot create compatible bitmap (" << width <<
  66.           "x" << height << ") for " << hex << (UINT)(HDC)dc);
  67.   }
  68.   CheckValid();
  69.   OBJ_REF_ADD(Handle, Bitmap);
  70. }
  71.  
  72. TBitmap::TBitmap(const TDC& dc, const TDib& dib, DWORD usage)
  73. {
  74.   Handle = ::CreateDIBitmap(dc,
  75.                             (BITMAPINFOHEADER far*)dib.GetInfoHeader(),
  76.                             usage, (const BYTE FAR*)dib.GetBits(),
  77.                             (BITMAPINFO far*)dib.GetInfo(),
  78.                             dib.Usage());
  79.                             // API casts
  80.   WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from DIB " << hex <<
  81.         (UINT)(HANDLE)dib << " for " << (UINT)(HDC)dc);
  82.   CheckValid();
  83.   OBJ_REF_ADD(Handle, Bitmap);
  84. }
  85.  
  86. TBitmap::TBitmap(HINSTANCE instance, TResId resId)
  87. {
  88.   Handle = ::LoadBitmap(instance, resId);
  89.   WARNX(OwlGDI, !Handle, 0, "Cannot load bitmap " << resId << " from " <<
  90.         hex << (UINT)instance);
  91.   CheckValid();
  92.   OBJ_REF_ADD(Handle, Bitmap);
  93. }
  94.  
  95. //
  96. // Copy constructor, duplicates a bitmap, bits and all
  97. //
  98. TBitmap::TBitmap(const TBitmap& src)
  99. {
  100.   Handle = 0;
  101.   Create(src);
  102. }
  103.  
  104. //
  105. // Construct a bitmap by playing a metafile into it
  106. //
  107. TBitmap::TBitmap(const TMetaFilePict& metaFile, TPalette& palette, const TSize& defSize)
  108. {
  109.   // Adjust size to final metafile size as needed
  110.   //
  111.   TMemoryDC memDC;
  112.   TSize size = metaFile.CalcPlaySize(memDC, defSize);
  113.  
  114.   // Create bitmap, either mono or screen compatible
  115.   //
  116.   WORD  nColors;
  117.   palette.GetObject(nColors);
  118.   if (nColors > 2) {
  119.     TScreenDC dc;
  120.     Handle = ::CreateCompatibleBitmap(dc, size.cx, size.cy);
  121.   } else
  122.     Handle = ::CreateBitmap(size.cx, size.cy, 1, 1, 0);
  123.  
  124.   CheckValid();
  125.   OBJ_REF_ADD(Handle, Bitmap);
  126.  
  127.   // clear bitmap, then play metafile onto it
  128.   //
  129.   memDC.SelectObject(*this);
  130.  
  131.   memDC.SelectStockObject(WHITE_BRUSH);
  132.   memDC.PatBlt(0, 0, size.cx, size.cy);
  133.   memDC.SelectObject(palette, FALSE);
  134.  
  135.   metaFile.PlayOnto(memDC, size);
  136. }
  137.  
  138. //
  139. // Construct a Bitmap given a handle to a TDib, and a pointer to a TPalette.
  140. // If the palette is not supplied, a temp one is created and destroyed.
  141. //
  142. TBitmap::TBitmap(const TDib& dib, const TPalette* palette)
  143. {
  144.   if (palette)
  145.     Create(dib, *palette);
  146.   else
  147.     Create(dib, TPalette(dib));
  148. }
  149.  
  150. //
  151. // Get and return specific information about the bitmap using GDI's GetObject
  152. //
  153. int
  154. TBitmap::Width() const
  155. {
  156.   BITMAP bm;
  157.   GetObject(bm);
  158.   return bm.bmWidth;
  159. }
  160.  
  161. int
  162. TBitmap::Height() const
  163. {
  164.   BITMAP bm;
  165.   GetObject(bm);
  166.   return bm.bmHeight;
  167. }
  168.  
  169. BYTE
  170. TBitmap::Planes() const
  171. {
  172.   BITMAP bm;
  173.   GetObject(bm);
  174.   return bm.bmPlanes;
  175. }
  176.  
  177. BYTE
  178. TBitmap::BitsPixel() const
  179. {
  180.   BITMAP bm;
  181.   GetObject(bm);
  182.   return bm.bmBitsPixel;
  183. }
  184.  
  185. //
  186. // Put a device-dependent bitmap on the clipboard as a (DD)BITMAP. Clipboard
  187. // assumes ownership of the actual bitmap.
  188. //
  189. void
  190. TBitmap::ToClipboard(TClipboard& clipboard)
  191. {
  192.   clipboard.SetClipboardData(CF_BITMAP, Handle);
  193.   ShouldDelete = FALSE;
  194.   OBJ_REF_REMOVE(Handle);
  195. }
  196.  
  197. //
  198. // Create a bitmap & get its handle, given a dib and a palette
  199. // Used by ctors here and in derived classes. Assumes Handle can be
  200. // written over, & adds handle to reference container.
  201. //
  202. void
  203. TBitmap::Create(const TDib& dib, const TPalette& palette)
  204. {
  205.   TScreenDC  dc;
  206.   dc.SelectObject(palette, FALSE);
  207.   dc.RealizePalette();
  208.  
  209.   Handle = ::CreateDIBitmap(
  210.                dc,
  211.                (LPBITMAPINFOHEADER)dib.GetInfoHeader(),
  212.                CBM_INIT,
  213.                (const BYTE far*)dib.GetBits(),
  214.                (LPBITMAPINFO)dib.GetInfo(),
  215.                dib.Usage()
  216.             );             // API type casts
  217.  
  218.   CheckValid();
  219.   OBJ_REF_ADD(Handle, Bitmap);
  220. }
  221.  
  222. //
  223. // Create a bitmap & get its handle, given an other bitmap
  224. // Used by ctors here and in derived classes. Assumes Handle can be
  225. // written over, & adds handle to reference container.
  226. //
  227. void
  228. TBitmap::Create(const TBitmap& src)
  229. {
  230.   TMemoryDC memDC1;
  231.   TMemoryDC memDC2;
  232.  
  233.   BITMAP  bm;
  234.   src.GetObject(bm);
  235.   if (bm.bmPlanes != 1 || bm.bmBitsPixel != 1) {
  236.     // create a color bitmap (Assume screen compatible)
  237.     TScreenDC dc;
  238.     Handle = ::CreateCompatibleBitmap(dc, bm.bmWidth, bm.bmHeight);
  239.  
  240.   } else
  241.     // create a mono bitmap
  242.     Handle = ::CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, 0);
  243.  
  244.   CheckValid();
  245.   OBJ_REF_ADD(Handle, Bitmap);
  246.  
  247.   memDC1.SelectObject(src);
  248.   memDC2.SelectObject(*this);
  249.   memDC2.BitBlt(TRect(TPoint(0, 0), TSize(bm.bmWidth, bm.bmHeight)),
  250.                 memDC1, TPoint(0, 0), SRCCOPY);
  251. }
  252.