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