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

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