home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / BITMAP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.0 KB  |  342 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of GDI Bitmap object class
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_GDIOBJEC_H)
  11. # include <owl/gdiobjec.h>
  12. #endif
  13. #if !defined(OWL_METAFILE_H)
  14. # include <owl/metafile.h>
  15. #endif
  16. #if !defined(OWL_CLIPBOAR_H)
  17. # include <owl/clipboar.h>
  18. #endif
  19.  
  20.  
  21. OWL_DIAGINFO;
  22. DIAG_DECLARE_GROUP(OwlGDI);        // General GDI diagnostic group
  23. DIAG_DECLARE_GROUP(OwlGDIOrphan);  // Orphan control tracing group
  24.  
  25. //
  26. // Construct an alias TBitmap for an existing bitmap handle
  27. //
  28. TBitmap::TBitmap(HBITMAP handle, TAutoDelete autoDelete)
  29. :
  30.   TGdiObject(handle, autoDelete)
  31. {
  32.   if (ShouldDelete)
  33.     RefAdd(Handle, Bitmap);
  34.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this);
  35. }
  36.  
  37. //
  38. // Create an empty bitmap.
  39. //
  40. TBitmap::TBitmap()
  41. :
  42.   TGdiObject()
  43. {
  44.   TRACEX(OwlGDI, OWL_CDLEVEL, "Default TBitmap constructed @" << (void*)this);
  45. }
  46.  
  47. //
  48. // Create a copy of the bitmap found on the clipboard.
  49. //
  50. TBitmap::TBitmap(const TClipboard& clipboard)
  51. :
  52.   TGdiObject(clipboard.GetClipboardData(CF_BITMAP))
  53. {
  54.   RefAdd(Handle, Bitmap);
  55.   RefInc(Handle);
  56.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from clipboard.");
  57. }
  58.  
  59. //
  60. // Convenient constructor to create a bitmap with specified parameters.
  61. //
  62. TBitmap::TBitmap(int width, int height, uint8 planes, uint8 bitCount, const void far* bits)
  63. {
  64.   Handle = ::CreateBitmap(width, height, planes, bitCount, bits);
  65.   WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap (" << width << "x" <<
  66.         height << "x" << planes << ")");
  67.   CheckValid();
  68.   RefAdd(Handle, Bitmap);
  69.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " using parameters (" <<
  70.          dec << width << "x" << height << "x" << planes << ")." << hex);
  71. }
  72.  
  73. //
  74. // Create via an indirect pointer to a BITMAP structure.
  75. //
  76. TBitmap::TBitmap(const BITMAP far* bitmap)
  77. {
  78.   PRECONDITION(bitmap);
  79.   Handle = ::CreateBitmapIndirect((LPBITMAP)bitmap);  // API cast
  80.   WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from BITMAP @" <<
  81.         hex << (void*)bitmap);
  82.   CheckValid();
  83.   RefAdd(Handle, Bitmap);
  84.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from BITMAP @" <<
  85.          (void*)bitmap << ".");
  86. }
  87.  
  88. //
  89. // Create a discardable bitmap from the DC.
  90. //
  91. TBitmap::TBitmap(const TDC& dc, int width, int height, bool discardable)
  92. {
  93.   if (discardable) {
  94.     Handle = ::CreateDiscardableBitmap(dc, width, height);
  95.     WARNX(OwlGDI, !Handle, 0, "Cannot create discardable bitmap (" << width <<
  96.           "x" << height << ") for " << hex << (uint)(HDC)dc);
  97.   }
  98.   else {
  99.     Handle = ::CreateCompatibleBitmap(dc, width, height);
  100.     WARNX(OwlGDI, !Handle, 0, "Cannot create compatible bitmap (" << width <<
  101.           "x" << height << ") for " << hex << (uint)(HDC)dc);
  102.   }
  103.   CheckValid();
  104.   RefAdd(Handle, Bitmap);
  105.  
  106.   if (discardable) {
  107.     TRACEX(OwlGDI, OWL_CDLEVEL, "Discardable TBitmap constructed @" << (void*)this << " (" <<
  108.            dec << width << "x" << height << ") for " << hex << (uint)(HDC)dc << ".");
  109.   }
  110.   else  {
  111.     TRACEX(OwlGDI, OWL_CDLEVEL, "Compatible TBitmap constructed @" << (void*)this << " (" <<
  112.            dec << width << "x" << height << ") for " << hex << (uint)(HDC)dc << ".");
  113.   }
  114. }
  115.  
  116. //
  117. // Create a bitmap from the device independent bitmap.
  118. //
  119. TBitmap::TBitmap(const TDC& dc, const TDib& dib, uint32 usage)
  120. {
  121.   Handle = ::CreateDIBitmap(dc,
  122.                             (BITMAPINFOHEADER far*)dib.GetInfoHeader(),
  123.                             usage, (const uint8 FAR*)dib.GetBits(),
  124.                             (BITMAPINFO far*)dib.GetInfo(),
  125.                             dib.Usage());
  126.                             // API casts
  127.   WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from DIB " << hex <<
  128.         (uint)(HANDLE)dib << " for " << (uint)(HDC)dc);
  129.   CheckValid();
  130.   RefAdd(Handle, Bitmap);
  131.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this <<
  132.          " from DIB for " << (uint)(HDC)dc << ".");
  133. }
  134.  
  135. //
  136. // Load bitmap from resource.
  137. //
  138. TBitmap::TBitmap(HINSTANCE instance, TResId resId)
  139. {
  140.   Handle = ::LoadBitmap(instance, resId);
  141.   WARNX(OwlGDI, !Handle, 0, "Cannot load bitmap " << resId << " from " <<
  142.         hex << (uint)instance);
  143.   CheckValid();
  144.   RefAdd(Handle, Bitmap);
  145.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed from resource @" << (void*)this << ".");
  146. }
  147.  
  148. //
  149. // Copy constructor, duplicates a bitmap, bits and all
  150. //
  151. TBitmap::TBitmap(const TBitmap& src)
  152. {
  153.   Handle = 0;
  154.   Create(src);
  155.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap duplicated @" << (void*)this << " from @" << (void*)&src << ".");
  156. }
  157.  
  158. //
  159. // Construct a bitmap by playing a metafile into it
  160. //
  161. TBitmap::TBitmap(const TMetaFilePict& metaFile, TPalette& palette, const TSize& defSize)
  162. {
  163.   // Adjust size to final metafile size as needed
  164.   //
  165.   TMemoryDC memDC;
  166.   TSize size = metaFile.CalcPlaySize(memDC, defSize);
  167.  
  168.   // Create bitmap, either mono or screen compatible
  169.   //
  170.   uint16  nColors;
  171.   palette.GetObject(nColors);
  172.   if (nColors > 2) {
  173.     TScreenDC dc;
  174.     Handle = ::CreateCompatibleBitmap(dc, size.cx, size.cy);
  175.   }
  176.   else
  177.     Handle = ::CreateBitmap(size.cx, size.cy, 1, 1, 0);
  178.  
  179.   CheckValid();
  180.   RefAdd(Handle, Bitmap);
  181.  
  182.   // clear bitmap, then play metafile onto it
  183.   //
  184.   memDC.SelectObject(*this);
  185.  
  186.   memDC.SelectStockObject(WHITE_BRUSH);
  187.   memDC.PatBlt(0, 0, size.cx, size.cy);
  188.   memDC.SelectObject(palette, false);
  189.  
  190.   metaFile.PlayOnto(memDC, size);
  191.  
  192.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this <<
  193.          " from metafile @" << (void*)&metaFile << ".");
  194. }
  195.  
  196. //
  197. // Construct a Bitmap given a handle to a TDib, and a pointer to a TPalette.
  198. // If the palette is not supplied, a temp one is created and destroyed.
  199. //
  200. TBitmap::TBitmap(const TDib& dib, const TPalette* palette)
  201. {
  202.   if (palette)
  203.     Create(dib, *palette);
  204.   else
  205.     Create(dib, TPalette(dib));
  206.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from DIB @" <<
  207.          (void*)&dib << " and palette @" << (void*)palette << ".");
  208. }
  209.  
  210. //
  211. // Destroy the C++ object.
  212. //
  213. TBitmap::~TBitmap()
  214. {
  215.   TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap destructed @" << (void*)this << ".");
  216. }
  217.  
  218.  
  219. //
  220. // Get and return specific information about the bitmap using GDI's GetObject
  221. //
  222. int
  223. TBitmap::Width() const
  224. {
  225.   BITMAP bm;
  226.   GetObject(bm);
  227.   return bm.bmWidth;
  228. }
  229.  
  230. //
  231. // Return the height of the bitmap.
  232. //
  233. int
  234. TBitmap::Height() const
  235. {
  236.   BITMAP bm;
  237.   GetObject(bm);
  238.   return bm.bmHeight;
  239. }
  240.  
  241. //
  242. // Return the dimension the bitmap.
  243. //
  244. TSize
  245. TBitmap::Size() const
  246. {
  247.   BITMAP bm;
  248.   GetObject(bm);
  249.   return TSize(bm.bmWidth, bm.bmHeight);
  250. }
  251.  
  252. //
  253. // Return the number of planes in this bitmap.
  254. //
  255. int
  256. TBitmap::Planes() const
  257. {
  258.   BITMAP bm;
  259.   GetObject(bm);
  260.   return bm.bmPlanes;
  261. }
  262.  
  263. //
  264. // Return the bits per pixel in this bitmap.
  265. //
  266. int
  267. TBitmap::BitsPixel() const
  268. {
  269.   BITMAP bm;
  270.   GetObject(bm);
  271.   return bm.bmBitsPixel;
  272. }
  273.  
  274. //
  275. // Put a device-dependent bitmap on the clipboard as a (DD)BITMAP. Clipboard
  276. // assumes ownership of the actual bitmap handle & this TBitmap will not delete
  277. // the handle
  278. //
  279. void
  280. TBitmap::ToClipboard(TClipboard& clipboard)
  281. {
  282.   clipboard.SetClipboardData(CF_BITMAP, Handle);
  283.   ShouldDelete = false;
  284.   RefRemove(Handle);
  285. }
  286.  
  287. //
  288. // Create a bitmap & get its handle, given a dib and a palette
  289. // Used by ctors here and in derived classes. Assumes Handle member can be
  290. // over written, & adds handle to reference container.
  291. //
  292. void
  293. TBitmap::Create(const TDib& dib, const TPalette& palette)
  294. {
  295.   TScreenDC  dc;
  296.   dc.SelectObject(palette, false);
  297.   dc.RealizePalette();
  298.  
  299.   Handle = ::CreateDIBitmap(
  300.                dc,
  301.                (LPBITMAPINFOHEADER)dib.GetInfoHeader(),
  302.                CBM_INIT,
  303.                (const uint8 far*)dib.GetBits(),
  304.                (LPBITMAPINFO)dib.GetInfo(),
  305.                dib.Usage()
  306.             );             // API type casts
  307.  
  308.   CheckValid();
  309.   RefAdd(Handle, Bitmap);
  310. }
  311.  
  312. //
  313. // Create a bitmap & get its handle, given an other bitmap
  314. // Used by ctors here and in derived classes. Assumes Handle can be
  315. // written over, & adds handle to reference container.
  316. //
  317. void
  318. TBitmap::Create(const TBitmap& src)
  319. {
  320.   TMemoryDC memDC1;
  321.   TMemoryDC memDC2;
  322.  
  323.   BITMAP  bm;
  324.   src.GetObject(bm);
  325.   if (bm.bmPlanes != 1 || bm.bmBitsPixel != 1) {
  326.     // create a color bitmap (Assume screen compatible)
  327.     TScreenDC dc;
  328.     Handle = ::CreateCompatibleBitmap(dc, bm.bmWidth, bm.bmHeight);
  329.   }
  330.   else
  331.     // create a mono bitmap
  332.     Handle = ::CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, 0);
  333.  
  334.   CheckValid();
  335.   RefAdd(Handle, Bitmap);
  336.  
  337.   memDC1.SelectObject(src);
  338.   memDC2.SelectObject(*this);
  339.   memDC2.BitBlt(TRect(TPoint(0, 0), TSize(bm.bmWidth, bm.bmHeight)),
  340.                 memDC1, TPoint(0, 0), SRCCOPY);
  341. }
  342.