]>
Commit | Line | Data |
---|---|---|
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" |
3417f661 SN |
18 | #include "wx/string.h" |
19 | #include "wx/os2/private.h" | |
0e320a79 | 20 | #include "wx/palette.h" |
3417f661 | 21 | #include "wx/app.h" |
0e320a79 DW |
22 | #endif |
23 | ||
cdf1e714 DW |
24 | #define INCL_PM |
25 | #define INCL_GPI | |
cdf1e714 DW |
26 | |
27 | #include "assert.h" | |
28 | ||
004fd0c8 | 29 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) |
cdf1e714 | 30 | |
0e320a79 DW |
31 | /* |
32 | * Palette | |
33 | * | |
34 | */ | |
35 | ||
36 | wxPaletteRefData::wxPaletteRefData() | |
37 | { | |
deb63b95 DW |
38 | m_hPalette = NULLHANDLE; |
39 | m_hPS = NULLHANDLE; | |
40 | } // end of wxPaletteRefData::wxPaletteRefData | |
0e320a79 DW |
41 | |
42 | wxPaletteRefData::~wxPaletteRefData() | |
43 | { | |
cdf1e714 DW |
44 | if ( m_hPalette ) |
45 | return; | |
deb63b95 | 46 | } // end of wxPaletteRefData::~wxPaletteRefData |
0e320a79 DW |
47 | |
48 | wxPalette::wxPalette() | |
49 | { | |
deb63b95 DW |
50 | } // end of wxPalette::wxPalette |
51 | ||
52 | wxPalette::wxPalette( | |
53 | int n | |
54 | , const unsigned char* pRed | |
55 | , const unsigned char* pGreen | |
56 | , const unsigned char* pBlue | |
57 | ) | |
0e320a79 | 58 | { |
deb63b95 DW |
59 | Create( n |
60 | ,pRed | |
61 | ,pGreen | |
62 | ,pBlue | |
63 | ); | |
64 | } // end of wxPalette::wxPalette | |
0e320a79 DW |
65 | |
66 | wxPalette::~wxPalette() | |
67 | { | |
deb63b95 | 68 | } // end of wxPalette::~wxPalette |
0e320a79 | 69 | |
6670f564 | 70 | bool wxPalette::FreeResource( bool WXUNUSED(bForce) ) |
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 | 76 | } |
6670f564 | 77 | return true; |
deb63b95 DW |
78 | } // end of wxPalette::FreeResource |
79 | ||
6670f564 WS |
80 | bool wxPalette::Create( int n, |
81 | const unsigned char* pRed, | |
82 | const unsigned char* pGreen, | |
83 | const unsigned char* pBlue ) | |
0e320a79 | 84 | { |
deb63b95 | 85 | PULONG pualTable; |
0e320a79 | 86 | |
deb63b95 | 87 | UnRef(); |
0e320a79 | 88 | |
deb63b95 DW |
89 | m_refData = new wxPaletteRefData; |
90 | pualTable = new ULONG[n]; | |
91 | if (!pualTable) | |
6670f564 | 92 | return false; |
0e320a79 | 93 | |
deb63b95 DW |
94 | for (int i = 0; i < n; i ++) |
95 | { | |
96 | pualTable[i] = (PC_RESERVED * 16777216) + ((int)pRed[i] * 65536) + ((int)pGreen[i] * 256) + (int)pBlue[i]; | |
97 | } | |
98 | M_PALETTEDATA->m_hPalette = (WXHPALETTE)::GpiCreatePalette( vHabmain | |
99 | ,LCOL_PURECOLOR | |
100 | ,LCOLF_CONSECRGB | |
101 | ,(LONG)n | |
102 | ,pualTable | |
103 | ); | |
104 | delete [] pualTable; | |
6670f564 | 105 | return true; |
deb63b95 | 106 | } // end of wxPalette::Create |
0e320a79 | 107 | |
88ef3a57 WS |
108 | int wxPalette::GetPixel( unsigned char cRed, |
109 | unsigned char cGreen, | |
110 | unsigned char cBlue) const | |
0e320a79 | 111 | { |
88ef3a57 WS |
112 | bool bFound = false; |
113 | PULONG pualTable = NULL; | |
114 | ULONG ulNumEntries; | |
115 | ULONG ulRGB = (PC_RESERVED * 16777216) + | |
116 | ((int)cRed * 65536) + | |
117 | ((int)cGreen * 256) + | |
118 | (int)cBlue; | |
deb63b95 DW |
119 | |
120 | if (!m_refData) | |
88ef3a57 | 121 | return wxNOT_FOUND; |
0e320a79 | 122 | |
deb63b95 DW |
123 | // |
124 | // Get number of entries first | |
125 | // | |
126 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette | |
127 | ,M_PALETTEDATA->m_hPS | |
128 | ,0 // No options | |
129 | ,0 // No start index | |
130 | ,0 // Force return of number entries | |
131 | ,NULL // No array | |
132 | ); | |
133 | ||
134 | pualTable = new ULONG[ulNumEntries]; | |
135 | ||
136 | // | |
137 | // Now get the entries | |
138 | // | |
139 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette | |
140 | ,M_PALETTEDATA->m_hPS | |
141 | ,0 // No options | |
142 | ,0 // start at 0 | |
143 | ,ulNumEntries // Force return of number entries | |
144 | ,pualTable // Palette entry array with RGB values | |
145 | ); | |
146 | // | |
147 | // Now loop through and find the matching entry | |
148 | // | |
6670f564 | 149 | ULONG i; |
6d57cea9 | 150 | for (i = 0; i < ulNumEntries; i++) |
deb63b95 DW |
151 | { |
152 | if (pualTable[i] == ulRGB) | |
153 | { | |
6670f564 | 154 | bFound = true; |
deb63b95 DW |
155 | break; |
156 | } | |
157 | } | |
158 | if (!bFound) | |
88ef3a57 | 159 | return wxNOT_FOUND; |
deb63b95 DW |
160 | return (i + 1); |
161 | } // end of wxPalette::GetPixel | |
162 | ||
88ef3a57 WS |
163 | bool wxPalette::GetRGB( int nIndex, |
164 | unsigned char* pRed, | |
165 | unsigned char* pGreen, | |
166 | unsigned char* pBlue) const | |
deb63b95 DW |
167 | { |
168 | PULONG pualTable = NULL; | |
169 | RGB2 vRGB; | |
170 | ULONG ulNumEntries; | |
171 | ||
172 | if (!m_refData) | |
88ef3a57 | 173 | return false; |
deb63b95 DW |
174 | |
175 | if (nIndex < 0 || nIndex > 255) | |
88ef3a57 | 176 | return false; |
deb63b95 DW |
177 | // |
178 | // Get number of entries first | |
179 | // | |
180 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette | |
181 | ,M_PALETTEDATA->m_hPS | |
182 | ,0 // No options | |
183 | ,0 // No start index | |
184 | ,0 // Force return of number entries | |
185 | ,NULL // No array | |
186 | ); | |
187 | ||
188 | pualTable = new ULONG[ulNumEntries]; | |
189 | ||
190 | // | |
191 | // Now get the entries | |
192 | // | |
193 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette | |
194 | ,M_PALETTEDATA->m_hPS | |
195 | ,0 // No options | |
196 | ,0 // start at 0 | |
197 | ,ulNumEntries // Force return of number entries | |
198 | ,pualTable // Palette entry array with RGB values | |
199 | ); | |
200 | ||
201 | memcpy(&vRGB, &pualTable[nIndex], sizeof(RGB2)); | |
202 | *pBlue = vRGB.bBlue; | |
203 | *pGreen = vRGB.bGreen; | |
204 | *pRed = vRGB.bRed; | |
88ef3a57 | 205 | return true; |
deb63b95 DW |
206 | } // end of wxPalette::GetRGB |
207 | ||
208 | void wxPalette::SetHPALETTE( | |
209 | WXHPALETTE hPal | |
210 | ) | |
cdf1e714 | 211 | { |
deb63b95 DW |
212 | if ( !m_refData ) |
213 | m_refData = new wxPaletteRefData; | |
214 | ||
215 | M_PALETTEDATA->m_hPalette = hPal; | |
216 | } // end of wxPalette::SetHPALETTE | |
cdf1e714 | 217 | |
deb63b95 DW |
218 | void wxPalette::SetPS( |
219 | HPS hPS | |
220 | ) | |
221 | { | |
222 | if ( !m_refData ) | |
223 | m_refData = new wxPaletteRefData; | |
0e320a79 | 224 | |
deb63b95 DW |
225 | ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette); |
226 | M_PALETTEDATA->m_hPS = hPS; | |
227 | } // end of wxPalette::SetHPALETTE |