wxOS2 with Open Watcom: correct PCH usage, missing headers, warning fixes, source...
[wxWidgets.git] / src / os2 / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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(
110 const unsigned char cRed
111 , const unsigned char cGreen
112 , const unsigned char cBlue
113 ) const
114 {
115 bool bFound = FALSE;
116 PULONG pualTable = NULL;
117 ULONG ulNumEntries;
118 ULONG ulRGB = (PC_RESERVED * 16777216) +
119 ((int)cRed * 65536) +
120 ((int)cGreen * 256) +
121 (int)cBlue;
122
123 if (!m_refData)
124 return FALSE;
125
126 //
127 // Get number of entries first
128 //
129 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
130 ,M_PALETTEDATA->m_hPS
131 ,0 // No options
132 ,0 // No start index
133 ,0 // Force return of number entries
134 ,NULL // No array
135 );
136
137 pualTable = new ULONG[ulNumEntries];
138
139 //
140 // Now get the entries
141 //
142 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
143 ,M_PALETTEDATA->m_hPS
144 ,0 // No options
145 ,0 // start at 0
146 ,ulNumEntries // Force return of number entries
147 ,pualTable // Palette entry array with RGB values
148 );
149 //
150 // Now loop through and find the matching entry
151 //
152 ULONG i;
153 for (i = 0; i < ulNumEntries; i++)
154 {
155 if (pualTable[i] == ulRGB)
156 {
157 bFound = true;
158 break;
159 }
160 }
161 if (!bFound)
162 return 0;
163 return (i + 1);
164 } // end of wxPalette::GetPixel
165
166 bool wxPalette::GetRGB(
167 int nIndex
168 , unsigned char* pRed
169 , unsigned char* pGreen
170 , unsigned char* pBlue
171 ) const
172 {
173 PULONG pualTable = NULL;
174 RGB2 vRGB;
175 ULONG ulNumEntries;
176
177 if (!m_refData)
178 return FALSE;
179
180 if (nIndex < 0 || nIndex > 255)
181 return FALSE;
182 //
183 // Get number of entries first
184 //
185 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
186 ,M_PALETTEDATA->m_hPS
187 ,0 // No options
188 ,0 // No start index
189 ,0 // Force return of number entries
190 ,NULL // No array
191 );
192
193 pualTable = new ULONG[ulNumEntries];
194
195 //
196 // Now get the entries
197 //
198 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
199 ,M_PALETTEDATA->m_hPS
200 ,0 // No options
201 ,0 // start at 0
202 ,ulNumEntries // Force return of number entries
203 ,pualTable // Palette entry array with RGB values
204 );
205
206 memcpy(&vRGB, &pualTable[nIndex], sizeof(RGB2));
207 *pBlue = vRGB.bBlue;
208 *pGreen = vRGB.bGreen;
209 *pRed = vRGB.bRed;
210 return TRUE;
211 } // end of wxPalette::GetRGB
212
213 void wxPalette::SetHPALETTE(
214 WXHPALETTE hPal
215 )
216 {
217 if ( !m_refData )
218 m_refData = new wxPaletteRefData;
219
220 M_PALETTEDATA->m_hPalette = hPal;
221 } // end of wxPalette::SetHPALETTE
222
223 void wxPalette::SetPS(
224 HPS hPS
225 )
226 {
227 if ( !m_refData )
228 m_refData = new wxPaletteRefData;
229
230 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette);
231 M_PALETTEDATA->m_hPS = hPS;
232 } // end of wxPalette::SetHPALETTE