]>
Commit | Line | Data |
---|---|---|
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 | 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 | |
deb63b95 DW |
68 | bool 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 | ||
80 | bool 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 |
110 | int 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 | // | |
6d57cea9 DW |
153 | int i; |
154 | for (i = 0; i < ulNumEntries; i++) | |
deb63b95 DW |
155 | { |
156 | if (pualTable[i] == ulRGB) | |
157 | { | |
158 | bFound = TRUE; | |
159 | break; | |
160 | } | |
161 | } | |
162 | if (!bFound) | |
163 | return 0; | |
164 | return (i + 1); | |
165 | } // end of wxPalette::GetPixel | |
166 | ||
167 | bool wxPalette::GetRGB( | |
168 | int nIndex | |
169 | , unsigned char* pRed | |
170 | , unsigned char* pGreen | |
171 | , unsigned char* pBlue | |
172 | ) const | |
173 | { | |
174 | PULONG pualTable = NULL; | |
175 | RGB2 vRGB; | |
176 | ULONG ulNumEntries; | |
177 | ||
178 | if (!m_refData) | |
0e320a79 | 179 | return FALSE; |
deb63b95 DW |
180 | |
181 | if (nIndex < 0 || nIndex > 255) | |
182 | return FALSE; | |
183 | // | |
184 | // Get number of entries first | |
185 | // | |
186 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette | |
187 | ,M_PALETTEDATA->m_hPS | |
188 | ,0 // No options | |
189 | ,0 // No start index | |
190 | ,0 // Force return of number entries | |
191 | ,NULL // No array | |
192 | ); | |
193 | ||
194 | pualTable = new ULONG[ulNumEntries]; | |
195 | ||
196 | // | |
197 | // Now get the entries | |
198 | // | |
199 | ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette | |
200 | ,M_PALETTEDATA->m_hPS | |
201 | ,0 // No options | |
202 | ,0 // start at 0 | |
203 | ,ulNumEntries // Force return of number entries | |
204 | ,pualTable // Palette entry array with RGB values | |
205 | ); | |
206 | ||
207 | memcpy(&vRGB, &pualTable[nIndex], sizeof(RGB2)); | |
208 | *pBlue = vRGB.bBlue; | |
209 | *pGreen = vRGB.bGreen; | |
210 | *pRed = vRGB.bRed; | |
211 | return TRUE; | |
212 | } // end of wxPalette::GetRGB | |
213 | ||
214 | void wxPalette::SetHPALETTE( | |
215 | WXHPALETTE hPal | |
216 | ) | |
cdf1e714 | 217 | { |
deb63b95 DW |
218 | if ( !m_refData ) |
219 | m_refData = new wxPaletteRefData; | |
220 | ||
221 | M_PALETTEDATA->m_hPalette = hPal; | |
222 | } // end of wxPalette::SetHPALETTE | |
cdf1e714 | 223 | |
deb63b95 DW |
224 | void wxPalette::SetPS( |
225 | HPS hPS | |
226 | ) | |
227 | { | |
228 | if ( !m_refData ) | |
229 | m_refData = new wxPaletteRefData; | |
0e320a79 | 230 | |
deb63b95 DW |
231 | ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette); |
232 | M_PALETTEDATA->m_hPS = hPS; | |
233 | } // end of wxPalette::SetHPALETTE | |
0e320a79 | 234 |