]> git.saurik.com Git - wxWidgets.git/blame - src/msw/palette.cpp
Test using wxString::ToCDouble() in wxAny.
[wxWidgets.git] / src / msw / palette.cpp
CommitLineData
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
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
d275c7eb 16 #pragma hdrstop
2bda0e17
KB
17#endif
18
d275c7eb
VZ
19#if wxUSE_PALETTE
20
559a723c
WS
21#include "wx/palette.h"
22
d275c7eb 23#include "wx/msw/private.h"
2bda0e17 24
8f884a0d
VZ
25// ============================================================================
26// wxPaletteRefData
27// ============================================================================
2bda0e17 28
8f884a0d 29class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData
2bda0e17 30{
8f884a0d
VZ
31public:
32 wxPaletteRefData() { Init(); }
2bda0e17 33
8f884a0d 34 wxPaletteRefData(int n,
913ce299
VZ
35 const unsigned char *red,
36 const unsigned char *green,
37 const unsigned char *blue)
8f884a0d
VZ
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 }
2bda0e17 56
8f884a0d 57 wxPaletteRefData(const wxPaletteRefData& data)
4f271814 58 : wxGDIRefData()
8f884a0d
VZ
59 {
60 Init();
2bda0e17 61
8f884a0d
VZ
62 const UINT n = data.GetEntries();
63 if ( !n )
64 return;
2bda0e17 65
8f884a0d
VZ
66 LOGPALETTE *pPal = Alloc(n);
67 if ( !pPal )
68 return;
2bda0e17 69
8f884a0d
VZ
70 if ( ::GetPaletteEntries(data.m_hPalette, 0, n, pPal->palPalEntry) )
71 m_hPalette = ::CreatePalette(pPal);
72
73 free(pPal);
74 }
75
76 virtual ~wxPaletteRefData()
d275c7eb 77 {
8f884a0d
VZ
78 if ( m_hPalette )
79 ::DeleteObject(m_hPalette);
d275c7eb 80 }
f4322df6 81
8f884a0d 82 virtual bool IsOk() const { return m_hPalette != 0; }
2bda0e17 83
8f884a0d 84 UINT GetEntries() const
5633b968 85 {
8f884a0d 86 return ::GetPaletteEntries(m_hPalette, 0, 0, NULL);
5633b968 87 }
f4322df6 88
8f884a0d
VZ
89private:
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 }
5633b968 103
8f884a0d 104 void Init() { m_hPalette = 0; }
2bda0e17 105
8f884a0d 106 HPALETTE m_hPalette;
5ea105e0 107
8f884a0d
VZ
108 friend class WXDLLIMPEXP_FWD_CORE wxPalette;
109};
33ac7e6f 110
8f884a0d
VZ
111// ============================================================================
112// wxPalette
113// ============================================================================
5ea105e0 114
8f884a0d 115IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
2bda0e17 116
8f884a0d 117#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
2bda0e17 118
8f884a0d 119bool wxPalette::Create(int n,
913ce299
VZ
120 const unsigned char *red,
121 const unsigned char *green,
122 const unsigned char *blue)
8f884a0d
VZ
123{
124 m_refData = new wxPaletteRefData(n, red, green, blue);
88ef3a57 125
8f884a0d
VZ
126 return IsOk();
127}
33ac7e6f 128
8f884a0d
VZ
129wxGDIRefData *wxPalette::CreateGDIRefData() const
130{
131 return new wxPaletteRefData;
2bda0e17
KB
132}
133
8f884a0d
VZ
134wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
135{
5c33522f 136 return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data));
8f884a0d
VZ
137}
138
139int wxPalette::GetColoursCount() const
140{
141 return IsOk() ? M_PALETTEDATA->GetEntries() : 0;
142}
143
144int wxPalette::GetPixel(unsigned char red,
145 unsigned char green,
146 unsigned char blue) const
2bda0e17 147{
88ef3a57
WS
148 if ( !m_refData )
149 return wxNOT_FOUND;
2bda0e17 150
8f884a0d
VZ
151 return ::GetNearestPaletteIndex(M_PALETTEDATA->m_hPalette,
152 PALETTERGB(red, green, blue));
2bda0e17
KB
153}
154
8f884a0d
VZ
155bool wxPalette::GetRGB(int index,
156 unsigned char *red,
157 unsigned char *green,
158 unsigned char *blue) const
2bda0e17 159{
88ef3a57
WS
160 if ( !m_refData )
161 return false;
162
163 if (index < 0 || index > 255)
164 return false;
2bda0e17 165
88ef3a57 166 PALETTEENTRY entry;
8f884a0d 167 if ( !::GetPaletteEntries(M_PALETTEDATA->m_hPalette, index, 1, &entry) )
88ef3a57 168 return false;
8f884a0d
VZ
169
170 *red = entry.peRed;
171 *green = entry.peGreen;
172 *blue = entry.peBlue;
173
174 return true;
175}
176
177WXHPALETTE wxPalette::GetHPALETTE() const
178{
179 return M_PALETTEDATA ? (WXHPALETTE)M_PALETTEDATA->m_hPalette : 0;
2bda0e17
KB
180}
181
182void wxPalette::SetHPALETTE(WXHPALETTE pal)
183{
8f884a0d 184 AllocExclusive();
2bda0e17 185
8f884a0d 186 M_PALETTEDATA->m_hPalette = (HPALETTE)pal;
2bda0e17
KB
187}
188
d275c7eb 189#endif // wxUSE_PALETTE