replace wx_{const,static,reinterpret}_cast with their standard C++ equivalents
[wxWidgets.git] / src / os2 / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/palette.cpp
3 // Purpose: wxPalette
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include <stdio.h>
17 #include "wx/string.h"
18 #include "wx/os2/private.h"
19 #include "wx/palette.h"
20 #include "wx/app.h"
21 #endif
22
23 #define INCL_PM
24 #define INCL_GPI
25
26 #include "assert.h"
27
28 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
29
30 /*
31 * Palette
32 *
33 */
34
35 wxPaletteRefData::wxPaletteRefData()
36 {
37 m_hPalette = NULLHANDLE;
38 m_hPS = NULLHANDLE;
39 } // end of wxPaletteRefData::wxPaletteRefData
40
41 wxPaletteRefData::~wxPaletteRefData()
42 {
43 if ( m_hPalette )
44 return;
45 } // end of wxPaletteRefData::~wxPaletteRefData
46
47 wxPalette::wxPalette()
48 {
49 } // end of wxPalette::wxPalette
50
51 wxPalette::wxPalette(
52 int n
53 , const unsigned char* pRed
54 , const unsigned char* pGreen
55 , const unsigned char* pBlue
56 )
57 {
58 Create( n
59 ,pRed
60 ,pGreen
61 ,pBlue
62 );
63 } // end of wxPalette::wxPalette
64
65 wxPalette::~wxPalette()
66 {
67 } // end of wxPalette::~wxPalette
68
69 bool wxPalette::FreeResource( bool WXUNUSED(bForce) )
70 {
71 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
72 {
73 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, NULLHANDLE);
74 ::GpiDeletePalette((HPAL)M_PALETTEDATA->m_hPalette);
75 }
76 return true;
77 } // end of wxPalette::FreeResource
78
79 bool wxPalette::Create( int n,
80 const unsigned char* pRed,
81 const unsigned char* pGreen,
82 const unsigned char* pBlue )
83 {
84 PULONG pualTable;
85
86 UnRef();
87
88 m_refData = new wxPaletteRefData;
89 pualTable = new ULONG[n];
90 if (!pualTable)
91 return false;
92
93 for (int i = 0; i < n; i ++)
94 {
95 pualTable[i] = (PC_RESERVED * 16777216) + ((int)pRed[i] * 65536) + ((int)pGreen[i] * 256) + (int)pBlue[i];
96 }
97 M_PALETTEDATA->m_hPalette = (WXHPALETTE)::GpiCreatePalette( vHabmain
98 ,LCOL_PURECOLOR
99 ,LCOLF_CONSECRGB
100 ,(LONG)n
101 ,pualTable
102 );
103 delete [] pualTable;
104 return true;
105 } // end of wxPalette::Create
106
107 wxGDIRefData *wxPalette::CreateGDIRefData() const
108 {
109 return new wxPaletteRefData;
110 }
111
112 wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
113 {
114 return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data));
115 }
116
117 int wxPalette::GetPixel( unsigned char cRed,
118 unsigned char cGreen,
119 unsigned char cBlue) const
120 {
121 bool bFound = false;
122 PULONG pualTable = NULL;
123 ULONG ulNumEntries;
124 ULONG ulRGB = (PC_RESERVED * 16777216) +
125 ((int)cRed * 65536) +
126 ((int)cGreen * 256) +
127 (int)cBlue;
128
129 if (!m_refData)
130 return wxNOT_FOUND;
131
132 //
133 // Get number of entries first
134 //
135 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
136 ,M_PALETTEDATA->m_hPS
137 ,0 // No options
138 ,0 // No start index
139 ,0 // Force return of number entries
140 ,NULL // No array
141 );
142
143 pualTable = new ULONG[ulNumEntries];
144
145 //
146 // Now get the entries
147 //
148 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
149 ,M_PALETTEDATA->m_hPS
150 ,0 // No options
151 ,0 // start at 0
152 ,ulNumEntries // Force return of number entries
153 ,pualTable // Palette entry array with RGB values
154 );
155 //
156 // Now loop through and find the matching entry
157 //
158 ULONG i;
159 for (i = 0; i < ulNumEntries; i++)
160 {
161 if (pualTable[i] == ulRGB)
162 {
163 bFound = true;
164 break;
165 }
166 }
167 if (!bFound)
168 return wxNOT_FOUND;
169 return (i + 1);
170 } // end of wxPalette::GetPixel
171
172 bool wxPalette::GetRGB( int nIndex,
173 unsigned char* pRed,
174 unsigned char* pGreen,
175 unsigned char* pBlue) const
176 {
177 PULONG pualTable = NULL;
178 RGB2 vRGB;
179 ULONG ulNumEntries;
180
181 if (!m_refData)
182 return false;
183
184 if (nIndex < 0 || nIndex > 255)
185 return false;
186 //
187 // Get number of entries first
188 //
189 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
190 ,M_PALETTEDATA->m_hPS
191 ,0 // No options
192 ,0 // No start index
193 ,0 // Force return of number entries
194 ,NULL // No array
195 );
196
197 pualTable = new ULONG[ulNumEntries];
198
199 //
200 // Now get the entries
201 //
202 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
203 ,M_PALETTEDATA->m_hPS
204 ,0 // No options
205 ,0 // start at 0
206 ,ulNumEntries // Force return of number entries
207 ,pualTable // Palette entry array with RGB values
208 );
209
210 memcpy(&vRGB, &pualTable[nIndex], sizeof(RGB2));
211 *pBlue = vRGB.bBlue;
212 *pGreen = vRGB.bGreen;
213 *pRed = vRGB.bRed;
214 return true;
215 } // end of wxPalette::GetRGB
216
217 void wxPalette::SetHPALETTE(
218 WXHPALETTE hPal
219 )
220 {
221 if ( !m_refData )
222 m_refData = new wxPaletteRefData;
223
224 M_PALETTEDATA->m_hPalette = hPal;
225 } // end of wxPalette::SetHPALETTE
226
227 void wxPalette::SetPS(
228 HPS hPS
229 )
230 {
231 if ( !m_refData )
232 m_refData = new wxPaletteRefData;
233
234 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette);
235 M_PALETTEDATA->m_hPS = hPS;
236 } // end of wxPalette::SetHPALETTE