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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   source\owl\palette.cpp
  4. //   Implementation of GDI Palette object class
  5. //----------------------------------------------------------------------------
  6. #include <owl\owlpch.h>
  7. #include <owl\gdiobjec.h>
  8. #include <owl\clipboar.h>
  9. #include <memory.h>
  10.  
  11. DIAG_DECLARE_GROUP(OwlGDI);        // General GDI diagnostic group
  12. DIAG_DECLARE_GROUP(OwlGDIOrphan);  // Orphan control tracing group
  13.  
  14. //
  15. // Constructors
  16. //
  17. TPalette::TPalette(HPALETTE handle, TAutoDelete autoDelete)
  18.   : TGdiObject(handle, autoDelete)
  19. {
  20.   #if !defined(NO_GDI_ORPHAN_CONTROL)
  21.     if (ShouldDelete)
  22.       OBJ_REF_ADD(Handle, Palette);
  23.   #endif
  24. }
  25.  
  26. TPalette::TPalette(const TClipboard& clipboard)
  27.   : TGdiObject(clipboard.GetClipboardData(CF_PALETTE))
  28. {
  29.   OBJ_REF_ADD(Handle, Palette);
  30.   OBJ_REF_INC(Handle);
  31. }
  32.  
  33. TPalette::TPalette(const LOGPALETTE far* logPalette)
  34. {
  35.   PRECONDITION(logPalette);
  36.   Handle = ::CreatePalette(logPalette);
  37.   WARNX(OwlGDI, !Handle, 0, "Cannot create palette from logPalette @" << 
  38.         hex << DWORD(LPVOID(logPalette)));
  39.   CheckValid();
  40.   OBJ_REF_ADD(Handle, Palette);
  41. }
  42.  
  43. TPalette::TPalette(const TPalette& palette)
  44. {
  45.   WORD  nColors;
  46.   palette.GetObject(nColors);
  47.   if (nColors) {
  48.     LOGPALETTE* logPal = (LOGPALETTE*) new
  49.       BYTE[sizeof(LOGPALETTE)+(nColors-1)*sizeof(PALETTEENTRY)];
  50.  
  51.     logPal->palVersion = 0x300;
  52.     logPal->palNumEntries = nColors;
  53.     palette.GetPaletteEntries(0, nColors, logPal->palPalEntry);
  54.     Handle = ::CreatePalette(logPal);
  55.     delete logPal;
  56.   } else
  57.     Handle = 0;
  58.  
  59.   WARNX(OwlGDI, !Handle, 0, "Cannot create palette from palette " << 
  60.         UINT(HPALETTE(palette)));
  61.   CheckValid();
  62.   OBJ_REF_ADD(Handle, Palette);
  63. }
  64.  
  65. TPalette::TPalette(const BITMAPINFO far* info, UINT flags)
  66. {
  67.   Create(info, flags);
  68. }
  69.  
  70. TPalette::TPalette(const BITMAPCOREINFO far* core, UINT flags)
  71. {
  72.   Create(core, flags);
  73. }
  74.  
  75. TPalette::TPalette(const TDib& dib, UINT flags)
  76. {
  77.   if (dib.IsPM())
  78.     Create((const BITMAPCOREINFO far*)dib.GetInfo(), flags);
  79.   else
  80.     Create(dib.GetInfo(), flags);
  81. }
  82.  
  83. TPalette::TPalette(const PALETTEENTRY far* entries, int count)
  84. {
  85.   LOGPALETTE* logPal = (LOGPALETTE*)new BYTE[
  86.      sizeof(LOGPALETTE)+(count-1)*sizeof(PALETTEENTRY) ];
  87.  
  88.   logPal->palVersion  = 0x300;
  89.   logPal->palNumEntries = (WORD)count;
  90.   memcpy(logPal->palPalEntry, entries, count*sizeof(PALETTEENTRY));
  91.   Handle = ::CreatePalette(logPal);
  92.   delete logPal;
  93.  
  94.   WARNX(OwlGDI, !Handle, 0, "Cannot create palette from " << count <<
  95.         "palette entries @" << hex << DWORD(LPVOID(entries)));
  96.   CheckValid();
  97.   OBJ_REF_ADD(Handle, Palette);
  98. }
  99.  
  100. //
  101. // Accept a pointer to a BITMAPINFO structure and create a GDI logical
  102. // palette from the color table which follows it, for 2, 16 and 256 color
  103. // bitmaps. Fail for all others, including 24-bit DIB's
  104. //
  105. void
  106. TPalette::Create(const BITMAPINFO far* info, UINT flags)
  107. {
  108.   const RGBQUAD far* rgb = info->bmiColors;
  109.  
  110.   // if the ClrUsed field of the header is non-zero,
  111.   // it means that we could have have a short color table.
  112.   //
  113.   WORD  nColors = info->bmiHeader.biClrUsed ?
  114.            (WORD)info->bmiHeader.biClrUsed :
  115.            (WORD)NColors(info->bmiHeader.biBitCount);
  116.  
  117.   if (nColors) {
  118.     LOGPALETTE* logPal = (LOGPALETTE*)
  119.        new BYTE[sizeof(LOGPALETTE) + (nColors-1)*sizeof(PALETTEENTRY)];
  120.  
  121.     logPal->palVersion  = 0x300;      // Windows 3.0 version
  122.     logPal->palNumEntries = nColors;
  123.     for (WORD n = 0; n < nColors; n++) {
  124.       logPal->palPalEntry[n].peRed   = rgb[n].rgbRed;
  125.       logPal->palPalEntry[n].peGreen = rgb[n].rgbGreen;
  126.       logPal->palPalEntry[n].peBlue  = rgb[n].rgbBlue;
  127.       logPal->palPalEntry[n].peFlags = (BYTE)flags;
  128.     }
  129.     Handle = ::CreatePalette(logPal);
  130.     delete logPal;
  131.   } else
  132.     Handle = 0;
  133.  
  134.   WARNX(OwlGDI, !Handle, 0, "Cannot create palette from bitmapinfo @" << 
  135.         hex << DWORD(LPVOID(info)));
  136.   CheckValid();
  137.   OBJ_REF_ADD(Handle, Palette);
  138. }
  139.  
  140. //
  141. // Accept a pointer to a BITMAPCORE structure and create a GDI logical
  142. // palette from the color table which follows it, for 2, 16 and 256 color
  143. // bitmaps. Fail for all others, including 24-bit DIB's
  144. //
  145. // It differs from the windows DIB routine in two respects:
  146. //
  147. //   1) The PM 1.x DIB must have complete color tables, since there is no
  148. //      ClrUsed field in the header
  149. //
  150. //   2) The size of the color table entries is 3 bytes, not 4 bytes.
  151. //
  152. void
  153. TPalette::Create(const BITMAPCOREINFO far* coreInfo, UINT flags)
  154. {
  155.   const RGBTRIPLE far* rgb = coreInfo->bmciColors;
  156.  
  157.   WORD nColors = (WORD)NColors(coreInfo->bmciHeader.bcBitCount);
  158.   if (nColors) {
  159.     LOGPALETTE* logPal = (LOGPALETTE*)
  160.        new BYTE[sizeof(LOGPALETTE) + (nColors-1)*sizeof(PALETTEENTRY)];
  161.  
  162.     logPal->palVersion  = 0x300; // Windows 3.0 version
  163.     logPal->palNumEntries = nColors;
  164.     for (short n = 0; n < nColors; n++) {
  165.       logPal->palPalEntry[n].peRed   = rgb[n].rgbtRed;
  166.       logPal->palPalEntry[n].peGreen = rgb[n].rgbtGreen;
  167.       logPal->palPalEntry[n].peBlue  = rgb[n].rgbtBlue;
  168.       logPal->palPalEntry[n].peFlags = (BYTE)flags;
  169.     }
  170.     Handle = ::CreatePalette(logPal);
  171.     delete logPal;
  172.   } else
  173.     Handle = 0;
  174.  
  175.   WARNX(OwlGDI, !Handle, 0, "Cannot create palette from coreinfo @" << 
  176.         hex << DWORD(LPVOID(coreInfo)));
  177.   CheckValid();
  178.   OBJ_REF_ADD(Handle, Palette);
  179. }
  180.  
  181. //
  182. // Move this logical palette to the clipboard.  Clipboard assumes ownership
  183. //
  184. void
  185. TPalette::ToClipboard(TClipboard& clipboard)
  186. {
  187.   if (Handle) {
  188.     clipboard.SetClipboardData(CF_PALETTE, Handle);
  189.     ShouldDelete = FALSE; // GDI object now owned by Clipboard
  190.     OBJ_REF_REMOVE(Handle);
  191.   }
  192. }
  193.