]> git.saurik.com Git - wxWidgets.git/blame - src/os2/palette.cpp
code cleanup
[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
DW
15#ifndef WX_PRECOMP
16#include <stdio.h>
3417f661 17#include "wx/defs.h"
cdf1e714 18#include "wx/setup.h"
3417f661
SN
19#include "wx/string.h"
20#include "wx/os2/private.h"
0e320a79 21#include "wx/palette.h"
3417f661 22#include "wx/app.h"
0e320a79
DW
23#endif
24
cdf1e714
DW
25#define INCL_PM
26#define INCL_GPI
cdf1e714
DW
27
28#include "assert.h"
29
004fd0c8 30IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
cdf1e714 31
0e320a79
DW
32/*
33 * Palette
34 *
35 */
36
37wxPaletteRefData::wxPaletteRefData()
38{
deb63b95
DW
39 m_hPalette = NULLHANDLE;
40 m_hPS = NULLHANDLE;
41} // end of wxPaletteRefData::wxPaletteRefData
0e320a79
DW
42
43wxPaletteRefData::~wxPaletteRefData()
44{
cdf1e714
DW
45 if ( m_hPalette )
46 return;
deb63b95 47} // end of wxPaletteRefData::~wxPaletteRefData
0e320a79
DW
48
49wxPalette::wxPalette()
50{
deb63b95
DW
51} // end of wxPalette::wxPalette
52
53wxPalette::wxPalette(
54 int n
55, const unsigned char* pRed
56, const unsigned char* pGreen
57, const unsigned char* pBlue
58)
0e320a79 59{
deb63b95
DW
60 Create( n
61 ,pRed
62 ,pGreen
63 ,pBlue
64 );
65} // end of wxPalette::wxPalette
0e320a79
DW
66
67wxPalette::~wxPalette()
68{
deb63b95 69} // end of wxPalette::~wxPalette
0e320a79 70
6670f564 71bool wxPalette::FreeResource( bool WXUNUSED(bForce) )
cdf1e714
DW
72{
73 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
74 {
deb63b95
DW
75 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, NULLHANDLE);
76 ::GpiDeletePalette((HPAL)M_PALETTEDATA->m_hPalette);
cdf1e714 77 }
6670f564 78 return true;
deb63b95
DW
79} // end of wxPalette::FreeResource
80
6670f564
WS
81bool wxPalette::Create( int n,
82 const unsigned char* pRed,
83 const unsigned char* pGreen,
84 const unsigned char* pBlue )
0e320a79 85{
deb63b95 86 PULONG pualTable;
0e320a79 87
deb63b95 88 UnRef();
0e320a79 89
deb63b95
DW
90 m_refData = new wxPaletteRefData;
91 pualTable = new ULONG[n];
92 if (!pualTable)
6670f564 93 return false;
0e320a79 94
deb63b95
DW
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;
6670f564 106 return true;
deb63b95 107} // end of wxPalette::Create
0e320a79 108
88ef3a57
WS
109int wxPalette::GetPixel( unsigned char cRed,
110 unsigned char cGreen,
111 unsigned char cBlue) const
0e320a79 112{
88ef3a57
WS
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;
deb63b95
DW
120
121 if (!m_refData)
88ef3a57 122 return wxNOT_FOUND;
0e320a79 123
deb63b95
DW
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 //
6670f564 150 ULONG i;
6d57cea9 151 for (i = 0; i < ulNumEntries; i++)
deb63b95
DW
152 {
153 if (pualTable[i] == ulRGB)
154 {
6670f564 155 bFound = true;
deb63b95
DW
156 break;
157 }
158 }
159 if (!bFound)
88ef3a57 160 return wxNOT_FOUND;
deb63b95
DW
161 return (i + 1);
162} // end of wxPalette::GetPixel
163
88ef3a57
WS
164bool wxPalette::GetRGB( int nIndex,
165 unsigned char* pRed,
166 unsigned char* pGreen,
167 unsigned char* pBlue) const
deb63b95
DW
168{
169 PULONG pualTable = NULL;
170 RGB2 vRGB;
171 ULONG ulNumEntries;
172
173 if (!m_refData)
88ef3a57 174 return false;
deb63b95
DW
175
176 if (nIndex < 0 || nIndex > 255)
88ef3a57 177 return false;
deb63b95
DW
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;
88ef3a57 206 return true;
deb63b95
DW
207} // end of wxPalette::GetRGB
208
209void wxPalette::SetHPALETTE(
210 WXHPALETTE hPal
211)
cdf1e714 212{
deb63b95
DW
213 if ( !m_refData )
214 m_refData = new wxPaletteRefData;
215
216 M_PALETTEDATA->m_hPalette = hPal;
217} // end of wxPalette::SetHPALETTE
cdf1e714 218
deb63b95
DW
219void wxPalette::SetPS(
220 HPS hPS
221)
222{
223 if ( !m_refData )
224 m_refData = new wxPaletteRefData;
0e320a79 225
deb63b95
DW
226 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette);
227 M_PALETTEDATA->m_hPS = hPS;
228} // end of wxPalette::SetHPALETTE