]> git.saurik.com Git - wxWidgets.git/blame - src/os2/palette.cpp
Correctly restore the originally used C locale in wxLocale dtor.
[wxWidgets.git] / src / os2 / palette.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
88ef3a57 2// Name: src/os2/palette.cpp
0e320a79
DW
3// Purpose: wxPalette
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
6670f564 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
cdf1e714
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
0e320a79 14
cdf1e714 15#ifndef WX_PRECOMP
7520f3da
WS
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"
0e320a79
DW
21#endif
22
cdf1e714
DW
23#define INCL_PM
24#define INCL_GPI
cdf1e714
DW
25
26#include "assert.h"
27
004fd0c8 28IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
cdf1e714 29
0e320a79
DW
30/*
31 * Palette
32 *
33 */
34
35wxPaletteRefData::wxPaletteRefData()
36{
deb63b95
DW
37 m_hPalette = NULLHANDLE;
38 m_hPS = NULLHANDLE;
39} // end of wxPaletteRefData::wxPaletteRefData
0e320a79
DW
40
41wxPaletteRefData::~wxPaletteRefData()
42{
cdf1e714
DW
43 if ( m_hPalette )
44 return;
deb63b95 45} // end of wxPaletteRefData::~wxPaletteRefData
0e320a79
DW
46
47wxPalette::wxPalette()
48{
deb63b95
DW
49} // end of wxPalette::wxPalette
50
51wxPalette::wxPalette(
52 int n
53, const unsigned char* pRed
54, const unsigned char* pGreen
55, const unsigned char* pBlue
56)
0e320a79 57{
deb63b95
DW
58 Create( n
59 ,pRed
60 ,pGreen
61 ,pBlue
62 );
63} // end of wxPalette::wxPalette
0e320a79
DW
64
65wxPalette::~wxPalette()
66{
deb63b95 67} // end of wxPalette::~wxPalette
0e320a79 68
6670f564 69bool wxPalette::FreeResource( bool WXUNUSED(bForce) )
cdf1e714
DW
70{
71 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
72 {
deb63b95
DW
73 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, NULLHANDLE);
74 ::GpiDeletePalette((HPAL)M_PALETTEDATA->m_hPalette);
cdf1e714 75 }
6670f564 76 return true;
deb63b95
DW
77} // end of wxPalette::FreeResource
78
6670f564
WS
79bool wxPalette::Create( int n,
80 const unsigned char* pRed,
81 const unsigned char* pGreen,
82 const unsigned char* pBlue )
0e320a79 83{
deb63b95 84 PULONG pualTable;
0e320a79 85
deb63b95 86 UnRef();
0e320a79 87
deb63b95
DW
88 m_refData = new wxPaletteRefData;
89 pualTable = new ULONG[n];
90 if (!pualTable)
6670f564 91 return false;
0e320a79 92
deb63b95
DW
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;
6670f564 104 return true;
deb63b95 105} // end of wxPalette::Create
0e320a79 106
4b3f61d1
SN
107wxGDIRefData *wxPalette::CreateGDIRefData() const
108{
109 return new wxPaletteRefData;
110}
111
112wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
113{
5c33522f 114 return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data));
4b3f61d1
SN
115}
116
88ef3a57
WS
117int wxPalette::GetPixel( unsigned char cRed,
118 unsigned char cGreen,
119 unsigned char cBlue) const
0e320a79 120{
88ef3a57
WS
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;
deb63b95
DW
128
129 if (!m_refData)
88ef3a57 130 return wxNOT_FOUND;
0e320a79 131
deb63b95
DW
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 //
6670f564 158 ULONG i;
6d57cea9 159 for (i = 0; i < ulNumEntries; i++)
deb63b95
DW
160 {
161 if (pualTable[i] == ulRGB)
162 {
6670f564 163 bFound = true;
deb63b95
DW
164 break;
165 }
166 }
167 if (!bFound)
88ef3a57 168 return wxNOT_FOUND;
deb63b95
DW
169 return (i + 1);
170} // end of wxPalette::GetPixel
171
88ef3a57
WS
172bool wxPalette::GetRGB( int nIndex,
173 unsigned char* pRed,
174 unsigned char* pGreen,
175 unsigned char* pBlue) const
deb63b95
DW
176{
177 PULONG pualTable = NULL;
178 RGB2 vRGB;
179 ULONG ulNumEntries;
180
181 if (!m_refData)
88ef3a57 182 return false;
deb63b95
DW
183
184 if (nIndex < 0 || nIndex > 255)
88ef3a57 185 return false;
deb63b95
DW
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;
88ef3a57 214 return true;
deb63b95
DW
215} // end of wxPalette::GetRGB
216
217void wxPalette::SetHPALETTE(
218 WXHPALETTE hPal
219)
cdf1e714 220{
deb63b95
DW
221 if ( !m_refData )
222 m_refData = new wxPaletteRefData;
223
224 M_PALETTEDATA->m_hPalette = hPal;
225} // end of wxPalette::SetHPALETTE
cdf1e714 226
deb63b95
DW
227void wxPalette::SetPS(
228 HPS hPS
229)
230{
231 if ( !m_refData )
232 m_refData = new wxPaletteRefData;
0e320a79 233
deb63b95
DW
234 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette);
235 M_PALETTEDATA->m_hPS = hPS;
236} // end of wxPalette::SetHPALETTE