]> git.saurik.com Git - wxWidgets.git/blob - src/msw/palette.cpp
new sample
[wxWidgets.git] / src / msw / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "palette.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/palette.h"
27 #endif
28
29 #include <windows.h>
30
31 #include "assert.h"
32
33 #if !USE_SHARED_LIBRARIES
34 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
35 #endif
36
37 /*
38 * Palette
39 *
40 */
41
42 wxPaletteRefData::wxPaletteRefData(void)
43 {
44 m_hPalette = 0;
45 }
46
47 wxPaletteRefData::~wxPaletteRefData(void)
48 {
49 if ( m_hPalette )
50 ::DeleteObject((HPALETTE) m_hPalette);
51 }
52
53 wxPalette::wxPalette(void)
54 {
55 }
56
57 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
58 {
59 Create(n, red, green, blue);
60 }
61
62 wxPalette::~wxPalette(void)
63 {
64 // FreeResource(TRUE);
65 }
66
67 bool wxPalette::FreeResource(bool force)
68 {
69 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
70 {
71 DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette);
72 }
73 return TRUE;
74 }
75
76 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
77 {
78 UnRef();
79
80 #ifdef __WXWINE__
81
82 return (FALSE);
83
84 #else
85
86 m_refData = new wxPaletteRefData;
87
88 NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
89 (WORD)n * sizeof(PALETTEENTRY));
90 if (!npPal)
91 return(FALSE);
92
93 npPal->palVersion = 0x300;
94 npPal->palNumEntries = n;
95
96 int i;
97 for (i = 0; i < n; i ++)
98 {
99 npPal->palPalEntry[i].peRed = red[i];
100 npPal->palPalEntry[i].peGreen = green[i];
101 npPal->palPalEntry[i].peBlue = blue[i];
102 npPal->palPalEntry[i].peFlags = 0;
103 }
104 M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal);
105 LocalFree((HANDLE)npPal);
106 return TRUE;
107
108 #endif
109 }
110
111 int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
112 {
113 if ( !m_refData )
114 return FALSE;
115
116 return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue));
117 }
118
119 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
120 {
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 } else
135 return FALSE;
136 }
137
138 void wxPalette::SetHPALETTE(WXHPALETTE pal)
139 {
140 if ( !m_refData )
141 m_refData = new wxPaletteRefData;
142
143 M_PALETTEDATA->m_hPalette = pal;
144 }
145