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