]> git.saurik.com Git - wxWidgets.git/blame - src/os2/palette.cpp
precompiled headers fix
[wxWidgets.git] / src / os2 / palette.cpp
CommitLineData
0e320a79
DW
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
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
deb63b95
DW
71bool wxPalette::FreeResource(
72 bool bForce
73)
cdf1e714
DW
74{
75 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
76 {
deb63b95
DW
77 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, NULLHANDLE);
78 ::GpiDeletePalette((HPAL)M_PALETTEDATA->m_hPalette);
cdf1e714
DW
79 }
80 return TRUE;
deb63b95
DW
81} // end of wxPalette::FreeResource
82
83bool wxPalette::Create(
84 int n
85, const unsigned char* pRed
86, const unsigned char* pGreen
87, const unsigned char* pBlue
88)
0e320a79 89{
deb63b95 90 PULONG pualTable;
0e320a79 91
deb63b95 92 UnRef();
0e320a79 93
deb63b95
DW
94 m_refData = new wxPaletteRefData;
95 pualTable = new ULONG[n];
96 if (!pualTable)
97 return(FALSE);
0e320a79 98
deb63b95
DW
99 for (int i = 0; i < n; i ++)
100 {
101 pualTable[i] = (PC_RESERVED * 16777216) + ((int)pRed[i] * 65536) + ((int)pGreen[i] * 256) + (int)pBlue[i];
102 }
103 M_PALETTEDATA->m_hPalette = (WXHPALETTE)::GpiCreatePalette( vHabmain
104 ,LCOL_PURECOLOR
105 ,LCOLF_CONSECRGB
106 ,(LONG)n
107 ,pualTable
108 );
109 delete [] pualTable;
110 return TRUE;
111} // end of wxPalette::Create
0e320a79 112
deb63b95
DW
113int wxPalette::GetPixel(
114 const unsigned char cRed
115, const unsigned char cGreen
116, const unsigned char cBlue
117) const
0e320a79 118{
deb63b95
DW
119 bool bFound = FALSE;
120 PULONG pualTable = NULL;
121 ULONG ulNumEntries;
122 ULONG ulRGB = (PC_RESERVED * 16777216) +
123 ((int)cRed * 65536) +
124 ((int)cGreen * 256) +
125 (int)cBlue;
126
127 if (!m_refData)
128 return FALSE;
0e320a79 129
deb63b95
DW
130 //
131 // Get number of entries first
132 //
133 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
134 ,M_PALETTEDATA->m_hPS
135 ,0 // No options
136 ,0 // No start index
137 ,0 // Force return of number entries
138 ,NULL // No array
139 );
140
141 pualTable = new ULONG[ulNumEntries];
142
143 //
144 // Now get the entries
145 //
146 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
147 ,M_PALETTEDATA->m_hPS
148 ,0 // No options
149 ,0 // start at 0
150 ,ulNumEntries // Force return of number entries
151 ,pualTable // Palette entry array with RGB values
152 );
153 //
154 // Now loop through and find the matching entry
155 //
9923c37d 156 ULONG i;
6d57cea9 157 for (i = 0; i < ulNumEntries; i++)
deb63b95
DW
158 {
159 if (pualTable[i] == ulRGB)
160 {
161 bFound = TRUE;
162 break;
163 }
164 }
165 if (!bFound)
166 return 0;
167 return (i + 1);
168} // end of wxPalette::GetPixel
169
170bool wxPalette::GetRGB(
171 int nIndex
172, unsigned char* pRed
173, unsigned char* pGreen
174, unsigned char* pBlue
175) const
176{
177 PULONG pualTable = NULL;
178 RGB2 vRGB;
179 ULONG ulNumEntries;
180
181 if (!m_refData)
0e320a79 182 return FALSE;
deb63b95
DW
183
184 if (nIndex < 0 || nIndex > 255)
185 return FALSE;
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;
214 return TRUE;
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
0e320a79 237