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