]> git.saurik.com Git - wxWidgets.git/blame - src/os2/palette.cpp
fix for a crash in ~wxFontList
[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>
17#include "wx/setup.h"
0e320a79 18#include "wx/palette.h"
0e320a79
DW
19#endif
20
cdf1e714
DW
21#define INCL_PM
22#define INCL_GPI
23#include <os2.h>
24
25#include "assert.h"
26
004fd0c8 27IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
cdf1e714 28
0e320a79
DW
29/*
30 * Palette
31 *
32 */
33
34wxPaletteRefData::wxPaletteRefData()
35{
deb63b95
DW
36 m_hPalette = NULLHANDLE;
37 m_hPS = NULLHANDLE;
38} // end of wxPaletteRefData::wxPaletteRefData
0e320a79
DW
39
40wxPaletteRefData::~wxPaletteRefData()
41{
cdf1e714
DW
42 if ( m_hPalette )
43 return;
deb63b95 44} // end of wxPaletteRefData::~wxPaletteRefData
0e320a79
DW
45
46wxPalette::wxPalette()
47{
deb63b95
DW
48} // end of wxPalette::wxPalette
49
50wxPalette::wxPalette(
51 int n
52, const unsigned char* pRed
53, const unsigned char* pGreen
54, const unsigned char* pBlue
55)
0e320a79 56{
deb63b95
DW
57 Create( n
58 ,pRed
59 ,pGreen
60 ,pBlue
61 );
62} // end of wxPalette::wxPalette
0e320a79
DW
63
64wxPalette::~wxPalette()
65{
deb63b95 66} // end of wxPalette::~wxPalette
0e320a79 67
deb63b95
DW
68bool wxPalette::FreeResource(
69 bool bForce
70)
cdf1e714
DW
71{
72 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
73 {
deb63b95
DW
74 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, NULLHANDLE);
75 ::GpiDeletePalette((HPAL)M_PALETTEDATA->m_hPalette);
cdf1e714
DW
76 }
77 return TRUE;
deb63b95
DW
78} // end of wxPalette::FreeResource
79
80bool wxPalette::Create(
81 int n
82, const unsigned char* pRed
83, const unsigned char* pGreen
84, const unsigned char* pBlue
85)
0e320a79 86{
deb63b95 87 PULONG pualTable;
0e320a79 88
deb63b95 89 UnRef();
0e320a79 90
deb63b95
DW
91 m_refData = new wxPaletteRefData;
92 pualTable = new ULONG[n];
93 if (!pualTable)
94 return(FALSE);
0e320a79 95
deb63b95
DW
96 for (int i = 0; i < n; i ++)
97 {
98 pualTable[i] = (PC_RESERVED * 16777216) + ((int)pRed[i] * 65536) + ((int)pGreen[i] * 256) + (int)pBlue[i];
99 }
100 M_PALETTEDATA->m_hPalette = (WXHPALETTE)::GpiCreatePalette( vHabmain
101 ,LCOL_PURECOLOR
102 ,LCOLF_CONSECRGB
103 ,(LONG)n
104 ,pualTable
105 );
106 delete [] pualTable;
107 return TRUE;
108} // end of wxPalette::Create
0e320a79 109
deb63b95
DW
110int wxPalette::GetPixel(
111 const unsigned char cRed
112, const unsigned char cGreen
113, const unsigned char cBlue
114) const
0e320a79 115{
deb63b95
DW
116 bool bFound = FALSE;
117 PULONG pualTable = NULL;
118 ULONG ulNumEntries;
119 ULONG ulRGB = (PC_RESERVED * 16777216) +
120 ((int)cRed * 65536) +
121 ((int)cGreen * 256) +
122 (int)cBlue;
123
124 if (!m_refData)
125 return FALSE;
0e320a79 126
deb63b95
DW
127 //
128 // Get number of entries first
129 //
130 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
131 ,M_PALETTEDATA->m_hPS
132 ,0 // No options
133 ,0 // No start index
134 ,0 // Force return of number entries
135 ,NULL // No array
136 );
137
138 pualTable = new ULONG[ulNumEntries];
139
140 //
141 // Now get the entries
142 //
143 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
144 ,M_PALETTEDATA->m_hPS
145 ,0 // No options
146 ,0 // start at 0
147 ,ulNumEntries // Force return of number entries
148 ,pualTable // Palette entry array with RGB values
149 );
150 //
151 // Now loop through and find the matching entry
152 //
153 for (int 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
166bool 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)
0e320a79 178 return FALSE;
deb63b95
DW
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
213void wxPalette::SetHPALETTE(
214 WXHPALETTE hPal
215)
cdf1e714 216{
deb63b95
DW
217 if ( !m_refData )
218 m_refData = new wxPaletteRefData;
219
220 M_PALETTEDATA->m_hPalette = hPal;
221} // end of wxPalette::SetHPALETTE
cdf1e714 222
deb63b95
DW
223void wxPalette::SetPS(
224 HPS hPS
225)
226{
227 if ( !m_refData )
228 m_refData = new wxPaletteRefData;
0e320a79 229
deb63b95
DW
230 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette);
231 M_PALETTEDATA->m_hPS = hPS;
232} // end of wxPalette::SetHPALETTE
0e320a79 233