| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/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 |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifdef __BORLANDC__ |
| 16 | #pragma hdrstop |
| 17 | #endif |
| 18 | |
| 19 | #if wxUSE_PALETTE |
| 20 | |
| 21 | #include "wx/palette.h" |
| 22 | |
| 23 | #include "wx/msw/private.h" |
| 24 | |
| 25 | // ============================================================================ |
| 26 | // wxPaletteRefData |
| 27 | // ============================================================================ |
| 28 | |
| 29 | class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData |
| 30 | { |
| 31 | public: |
| 32 | wxPaletteRefData() { Init(); } |
| 33 | |
| 34 | wxPaletteRefData(int n, |
| 35 | const unsigned char *red, |
| 36 | const unsigned char *green, |
| 37 | const unsigned char *blue) |
| 38 | { |
| 39 | Init(); |
| 40 | |
| 41 | LOGPALETTE *pPal = Alloc(n); |
| 42 | if ( !pPal ) |
| 43 | return; |
| 44 | |
| 45 | for ( int i = 0; i < n; i++ ) |
| 46 | { |
| 47 | pPal->palPalEntry[i].peRed = red[i]; |
| 48 | pPal->palPalEntry[i].peGreen = green[i]; |
| 49 | pPal->palPalEntry[i].peBlue = blue[i]; |
| 50 | pPal->palPalEntry[i].peFlags = 0; |
| 51 | } |
| 52 | |
| 53 | m_hPalette = ::CreatePalette(pPal); |
| 54 | free(pPal); |
| 55 | } |
| 56 | |
| 57 | wxPaletteRefData(const wxPaletteRefData& data) |
| 58 | : wxGDIRefData() |
| 59 | { |
| 60 | Init(); |
| 61 | |
| 62 | const UINT n = data.GetEntries(); |
| 63 | if ( !n ) |
| 64 | return; |
| 65 | |
| 66 | LOGPALETTE *pPal = Alloc(n); |
| 67 | if ( !pPal ) |
| 68 | return; |
| 69 | |
| 70 | if ( ::GetPaletteEntries(data.m_hPalette, 0, n, pPal->palPalEntry) ) |
| 71 | m_hPalette = ::CreatePalette(pPal); |
| 72 | |
| 73 | free(pPal); |
| 74 | } |
| 75 | |
| 76 | virtual ~wxPaletteRefData() |
| 77 | { |
| 78 | if ( m_hPalette ) |
| 79 | ::DeleteObject(m_hPalette); |
| 80 | } |
| 81 | |
| 82 | virtual bool IsOk() const { return m_hPalette != 0; } |
| 83 | |
| 84 | UINT GetEntries() const |
| 85 | { |
| 86 | return ::GetPaletteEntries(m_hPalette, 0, 0, NULL); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | // caller must free() the pointer |
| 91 | static LOGPALETTE *Alloc(UINT numEntries) |
| 92 | { |
| 93 | LOGPALETTE *pPal = (LOGPALETTE *) |
| 94 | malloc(sizeof(LOGPALETTE) + numEntries*sizeof(PALETTEENTRY)); |
| 95 | if ( pPal ) |
| 96 | { |
| 97 | pPal->palVersion = 0x300; |
| 98 | pPal->palNumEntries = numEntries; |
| 99 | } |
| 100 | |
| 101 | return pPal; |
| 102 | } |
| 103 | |
| 104 | void Init() { m_hPalette = 0; } |
| 105 | |
| 106 | HPALETTE m_hPalette; |
| 107 | |
| 108 | friend class WXDLLIMPEXP_FWD_CORE wxPalette; |
| 109 | }; |
| 110 | |
| 111 | // ============================================================================ |
| 112 | // wxPalette |
| 113 | // ============================================================================ |
| 114 | |
| 115 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) |
| 116 | |
| 117 | #define M_PALETTEDATA ((wxPaletteRefData *)m_refData) |
| 118 | |
| 119 | bool wxPalette::Create(int n, |
| 120 | const unsigned char *red, |
| 121 | const unsigned char *green, |
| 122 | const unsigned char *blue) |
| 123 | { |
| 124 | m_refData = new wxPaletteRefData(n, red, green, blue); |
| 125 | |
| 126 | return IsOk(); |
| 127 | } |
| 128 | |
| 129 | wxGDIRefData *wxPalette::CreateGDIRefData() const |
| 130 | { |
| 131 | return new wxPaletteRefData; |
| 132 | } |
| 133 | |
| 134 | wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const |
| 135 | { |
| 136 | return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data)); |
| 137 | } |
| 138 | |
| 139 | int wxPalette::GetColoursCount() const |
| 140 | { |
| 141 | return IsOk() ? M_PALETTEDATA->GetEntries() : 0; |
| 142 | } |
| 143 | |
| 144 | int wxPalette::GetPixel(unsigned char red, |
| 145 | unsigned char green, |
| 146 | unsigned char blue) const |
| 147 | { |
| 148 | if ( !m_refData ) |
| 149 | return wxNOT_FOUND; |
| 150 | |
| 151 | return ::GetNearestPaletteIndex(M_PALETTEDATA->m_hPalette, |
| 152 | PALETTERGB(red, green, blue)); |
| 153 | } |
| 154 | |
| 155 | bool wxPalette::GetRGB(int index, |
| 156 | unsigned char *red, |
| 157 | unsigned char *green, |
| 158 | unsigned char *blue) const |
| 159 | { |
| 160 | if ( !m_refData ) |
| 161 | return false; |
| 162 | |
| 163 | if (index < 0 || index > 255) |
| 164 | return false; |
| 165 | |
| 166 | PALETTEENTRY entry; |
| 167 | if ( !::GetPaletteEntries(M_PALETTEDATA->m_hPalette, index, 1, &entry) ) |
| 168 | return false; |
| 169 | |
| 170 | *red = entry.peRed; |
| 171 | *green = entry.peGreen; |
| 172 | *blue = entry.peBlue; |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | WXHPALETTE wxPalette::GetHPALETTE() const |
| 178 | { |
| 179 | return M_PALETTEDATA ? (WXHPALETTE)M_PALETTEDATA->m_hPalette : 0; |
| 180 | } |
| 181 | |
| 182 | void wxPalette::SetHPALETTE(WXHPALETTE pal) |
| 183 | { |
| 184 | AllocExclusive(); |
| 185 | |
| 186 | M_PALETTEDATA->m_hPalette = (HPALETTE)pal; |
| 187 | } |
| 188 | |
| 189 | #endif // wxUSE_PALETTE |