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