| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: palette.cpp |
| 3 | // Purpose: wxPalette |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "palette.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #if wxUSE_PALETTE |
| 24 | |
| 25 | #ifndef WX_PRECOMP |
| 26 | #include "wx/palette.h" |
| 27 | #endif |
| 28 | |
| 29 | #include "wx/msw/private.h" |
| 30 | |
| 31 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) |
| 32 | |
| 33 | /* |
| 34 | * Palette |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | wxPaletteRefData::wxPaletteRefData(void) |
| 39 | { |
| 40 | m_hPalette = 0; |
| 41 | } |
| 42 | |
| 43 | wxPaletteRefData::~wxPaletteRefData(void) |
| 44 | { |
| 45 | if ( m_hPalette ) |
| 46 | ::DeleteObject((HPALETTE) m_hPalette); |
| 47 | } |
| 48 | |
| 49 | wxPalette::wxPalette(void) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
| 54 | { |
| 55 | Create(n, red, green, blue); |
| 56 | } |
| 57 | |
| 58 | wxPalette::~wxPalette(void) |
| 59 | { |
| 60 | // FreeResource(TRUE); |
| 61 | } |
| 62 | |
| 63 | bool wxPalette::FreeResource(bool WXUNUSED(force)) |
| 64 | { |
| 65 | if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette) |
| 66 | { |
| 67 | DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette); |
| 68 | } |
| 69 | return TRUE; |
| 70 | } |
| 71 | |
| 72 | bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
| 73 | { |
| 74 | UnRef(); |
| 75 | |
| 76 | #if defined(__WXWINE__) || defined(__WXMICROWIN__) |
| 77 | |
| 78 | return (FALSE); |
| 79 | |
| 80 | #else |
| 81 | |
| 82 | m_refData = new wxPaletteRefData; |
| 83 | |
| 84 | NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + |
| 85 | (WORD)n * sizeof(PALETTEENTRY)); |
| 86 | if (!npPal) |
| 87 | return(FALSE); |
| 88 | |
| 89 | npPal->palVersion = 0x300; |
| 90 | npPal->palNumEntries = n; |
| 91 | |
| 92 | int i; |
| 93 | for (i = 0; i < n; i ++) |
| 94 | { |
| 95 | npPal->palPalEntry[i].peRed = red[i]; |
| 96 | npPal->palPalEntry[i].peGreen = green[i]; |
| 97 | npPal->palPalEntry[i].peBlue = blue[i]; |
| 98 | npPal->palPalEntry[i].peFlags = 0; |
| 99 | } |
| 100 | M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal); |
| 101 | LocalFree((HANDLE)npPal); |
| 102 | return TRUE; |
| 103 | |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const |
| 108 | { |
| 109 | #ifdef __WXMICROWIN__ |
| 110 | return FALSE; |
| 111 | #else |
| 112 | if ( !m_refData ) |
| 113 | return FALSE; |
| 114 | |
| 115 | return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue)); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const |
| 120 | { |
| 121 | #ifdef __WXMICROWIN__ |
| 122 | return FALSE; |
| 123 | #else |
| 124 | if ( !m_refData ) |
| 125 | return FALSE; |
| 126 | |
| 127 | if (index < 0 || index > 255) |
| 128 | return FALSE; |
| 129 | |
| 130 | PALETTEENTRY entry; |
| 131 | if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry)) |
| 132 | { |
| 133 | *red = entry.peRed; |
| 134 | *green = entry.peGreen; |
| 135 | *blue = entry.peBlue; |
| 136 | return TRUE; |
| 137 | } else |
| 138 | return FALSE; |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | void wxPalette::SetHPALETTE(WXHPALETTE pal) |
| 143 | { |
| 144 | if ( !m_refData ) |
| 145 | m_refData = new wxPaletteRefData; |
| 146 | |
| 147 | M_PALETTEDATA->m_hPalette = pal; |
| 148 | } |
| 149 | |
| 150 | #endif // wxUSE_PALETTE |
| 151 | |