]> git.saurik.com Git - wxWidgets.git/blob - src/msw/palette.cpp
File/dir dialog styles and other changes (patch 1488371):
[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(void)
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 return true;
67 }
68
69 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
70 {
71 UnRef();
72
73 #if defined(__WXMICROWIN__)
74
75 return false;
76
77 #else
78
79 m_refData = new wxPaletteRefData;
80
81 NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
82 (WORD)n * sizeof(PALETTEENTRY));
83 if (!npPal)
84 return false;
85
86 npPal->palVersion = 0x300;
87 npPal->palNumEntries = (WORD)n;
88
89 int i;
90 for (i = 0; i < n; i ++)
91 {
92 npPal->palPalEntry[i].peRed = red[i];
93 npPal->palPalEntry[i].peGreen = green[i];
94 npPal->palPalEntry[i].peBlue = blue[i];
95 npPal->palPalEntry[i].peFlags = 0;
96 }
97 M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal);
98 LocalFree((HANDLE)npPal);
99 return true;
100
101 #endif
102 }
103
104 int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
105 {
106 #ifdef __WXMICROWIN__
107 return wxNOT_FOUND;
108 #else
109 if ( !m_refData )
110 return wxNOT_FOUND;
111
112 return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue));
113 #endif
114 }
115
116 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
117 {
118 #ifdef __WXMICROWIN__
119 return false;
120 #else
121 if ( !m_refData )
122 return false;
123
124 if (index < 0 || index > 255)
125 return false;
126
127 PALETTEENTRY entry;
128 if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry))
129 {
130 *red = entry.peRed;
131 *green = entry.peGreen;
132 *blue = entry.peBlue;
133 return true;
134 }
135 else
136 return false;
137 #endif
138 }
139
140 void wxPalette::SetHPALETTE(WXHPALETTE pal)
141 {
142 if ( !m_refData )
143 m_refData = new wxPaletteRefData;
144
145 M_PALETTEDATA->m_hPalette = pal;
146 }
147
148 #endif // wxUSE_PALETTE