]> git.saurik.com Git - wxWidgets.git/blob - src/msw/palette.cpp
correction to last commit: don't test unsetenv() return value, it's void under Darwin
[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 #ifndef WX_PRECOMP
24 #endif
25
26 #include "wx/msw/private.h"
27
28 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
29
30 /*
31 * Palette
32 *
33 */
34
35 wxPaletteRefData::wxPaletteRefData(void)
36 {
37 m_hPalette = 0;
38 }
39
40 wxPaletteRefData::~wxPaletteRefData(void)
41 {
42 if ( m_hPalette )
43 ::DeleteObject((HPALETTE) m_hPalette);
44 }
45
46 wxPalette::wxPalette()
47 {
48 }
49
50 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
51 {
52 Create(n, red, green, blue);
53 }
54
55 wxPalette::~wxPalette(void)
56 {
57 // FreeResource(true);
58 }
59
60 bool wxPalette::FreeResource(bool WXUNUSED(force))
61 {
62 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
63 {
64 DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette);
65 }
66
67 return true;
68 }
69
70 int wxPalette::GetColoursCount() const
71 {
72 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
73 {
74 return ::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, 0, 0, NULL );
75 }
76
77 return 0;
78 }
79
80 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
81 {
82 UnRef();
83
84 #if defined(__WXMICROWIN__)
85
86 return false;
87
88 #else
89
90 m_refData = new wxPaletteRefData;
91
92 NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
93 (WORD)n * sizeof(PALETTEENTRY));
94 if (!npPal)
95 return false;
96
97 npPal->palVersion = 0x300;
98 npPal->palNumEntries = (WORD)n;
99
100 int i;
101 for (i = 0; i < n; i ++)
102 {
103 npPal->palPalEntry[i].peRed = red[i];
104 npPal->palPalEntry[i].peGreen = green[i];
105 npPal->palPalEntry[i].peBlue = blue[i];
106 npPal->palPalEntry[i].peFlags = 0;
107 }
108 M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal);
109 LocalFree((HANDLE)npPal);
110 return true;
111
112 #endif
113 }
114
115 int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
116 {
117 #ifdef __WXMICROWIN__
118 return wxNOT_FOUND;
119 #else
120 if ( !m_refData )
121 return wxNOT_FOUND;
122
123 return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue));
124 #endif
125 }
126
127 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
128 {
129 #ifdef __WXMICROWIN__
130 return false;
131 #else
132 if ( !m_refData )
133 return false;
134
135 if (index < 0 || index > 255)
136 return false;
137
138 PALETTEENTRY entry;
139 if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry))
140 {
141 *red = entry.peRed;
142 *green = entry.peGreen;
143 *blue = entry.peBlue;
144 return true;
145 }
146 else
147 return false;
148 #endif
149 }
150
151 void wxPalette::SetHPALETTE(WXHPALETTE pal)
152 {
153 if ( !m_refData )
154 m_refData = new wxPaletteRefData;
155
156 M_PALETTEDATA->m_hPalette = pal;
157 }
158
159 #endif // wxUSE_PALETTE