]> git.saurik.com Git - wxWidgets.git/blob - src/msw/palette.cpp
document On{Open,Save}Document()
[wxWidgets.git] / src / msw / palette.cpp
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 unsigned char *red,
36 unsigned char *green,
37 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 {
59 Init();
60
61 const UINT n = data.GetEntries();
62 if ( !n )
63 return;
64
65 LOGPALETTE *pPal = Alloc(n);
66 if ( !pPal )
67 return;
68
69 if ( ::GetPaletteEntries(data.m_hPalette, 0, n, pPal->palPalEntry) )
70 m_hPalette = ::CreatePalette(pPal);
71
72 free(pPal);
73 }
74
75 virtual ~wxPaletteRefData()
76 {
77 if ( m_hPalette )
78 ::DeleteObject(m_hPalette);
79 }
80
81 virtual bool IsOk() const { return m_hPalette != 0; }
82
83 UINT GetEntries() const
84 {
85 return ::GetPaletteEntries(m_hPalette, 0, 0, NULL);
86 }
87
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 }
102
103 void Init() { m_hPalette = 0; }
104
105 HPALETTE m_hPalette;
106
107 friend class WXDLLIMPEXP_FWD_CORE wxPalette;
108 };
109
110 // ============================================================================
111 // wxPalette
112 // ============================================================================
113
114 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
115
116 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
117
118 bool wxPalette::Create(int n,
119 unsigned char *red,
120 unsigned char *green,
121 unsigned char *blue)
122 {
123 m_refData = new wxPaletteRefData(n, red, green, blue);
124
125 return IsOk();
126 }
127
128 wxGDIRefData *wxPalette::CreateGDIRefData() const
129 {
130 return new wxPaletteRefData;
131 }
132
133 wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
134 {
135 return new wxPaletteRefData(*wx_static_cast(const wxPaletteRefData *, data));
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
146 {
147 if ( !m_refData )
148 return wxNOT_FOUND;
149
150 return ::GetNearestPaletteIndex(M_PALETTEDATA->m_hPalette,
151 PALETTERGB(red, green, blue));
152 }
153
154 bool wxPalette::GetRGB(int index,
155 unsigned char *red,
156 unsigned char *green,
157 unsigned char *blue) const
158 {
159 if ( !m_refData )
160 return false;
161
162 if (index < 0 || index > 255)
163 return false;
164
165 PALETTEENTRY entry;
166 if ( !::GetPaletteEntries(M_PALETTEDATA->m_hPalette, index, 1, &entry) )
167 return false;
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;
179 }
180
181 void wxPalette::SetHPALETTE(WXHPALETTE pal)
182 {
183 AllocExclusive();
184
185 M_PALETTEDATA->m_hPalette = (HPALETTE)pal;
186 }
187
188 #endif // wxUSE_PALETTE