]> git.saurik.com Git - wxWidgets.git/blame - src/os2/palette.cpp
added pragmas to disable icc warning when va_arg is used with a pointer type
[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
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
deb63b95
DW
109int wxPalette::GetPixel(
110 const unsigned char cRed
111, const unsigned char cGreen
112, const unsigned char cBlue
113) const
0e320a79 114{
deb63b95
DW
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;
0e320a79 125
deb63b95
DW
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 //
6670f564 152 ULONG i;
6d57cea9 153 for (i = 0; i < ulNumEntries; i++)
deb63b95
DW
154 {
155 if (pualTable[i] == ulRGB)
156 {
6670f564 157 bFound = true;
deb63b95
DW
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