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