]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: pen.cpp | |
3 | // Purpose: wxPen | |
cdf1e714 | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
cdf1e714 | 6 | // Created: 10/10/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
cdf1e714 DW |
8 | // Copyright: (c) David Webster |
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> | |
0e320a79 | 17 | #include "wx/setup.h" |
cdf1e714 | 18 | #include "wx/list.h" |
0e320a79 | 19 | #include "wx/utils.h" |
cdf1e714 | 20 | #include "wx/app.h" |
0e320a79 | 21 | #include "wx/pen.h" |
0e320a79 DW |
22 | #endif |
23 | ||
cdf1e714 DW |
24 | #include "wx/os2/private.h" |
25 | #include "assert.h" | |
26 | ||
004fd0c8 | 27 | IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject) |
004fd0c8 | 28 | |
0e320a79 DW |
29 | wxPenRefData::wxPenRefData() |
30 | { | |
26ac77db DW |
31 | m_nStyle = wxSOLID; |
32 | m_nWidth = 1; | |
33 | m_nJoin = wxJOIN_ROUND ; | |
34 | m_nCap = wxCAP_ROUND ; | |
35 | m_hPen = 0L; | |
36 | } // end of wxPenRefData::wxPenRefData | |
37 | ||
38 | wxPenRefData::wxPenRefData( | |
39 | const wxPenRefData& rData | |
40 | ) | |
0e320a79 | 41 | { |
26ac77db DW |
42 | m_nStyle = rData.m_nStyle; |
43 | m_nWidth = rData.m_nWidth; | |
44 | m_nJoin = rData.m_nJoin; | |
45 | m_nCap = rData.m_nCap; | |
46 | m_vColour = rData.m_vColour; | |
47 | m_hPen = 0L; | |
48 | } // end of wxPenRefData::wxPenRefData | |
0e320a79 DW |
49 | |
50 | wxPenRefData::~wxPenRefData() | |
51 | { | |
26ac77db | 52 | } // end of wxPenRefData::~wxPenRefData |
0e320a79 | 53 | |
26ac77db | 54 | // |
0e320a79 | 55 | // Pens |
26ac77db | 56 | // |
0e320a79 DW |
57 | wxPen::wxPen() |
58 | { | |
59 | if ( wxThePenList ) | |
60 | wxThePenList->AddPen(this); | |
26ac77db | 61 | } // end of wxPen::wxPen |
0e320a79 DW |
62 | |
63 | wxPen::~wxPen() | |
64 | { | |
65 | if (wxThePenList) | |
66 | wxThePenList->RemovePen(this); | |
26ac77db | 67 | } // end of wxPen::wxPen |
0e320a79 DW |
68 | |
69 | // Should implement Create | |
26ac77db DW |
70 | wxPen::wxPen( |
71 | const wxColour& rColour | |
72 | , int nWidth | |
73 | , int nStyle | |
74 | ) | |
0e320a79 DW |
75 | { |
76 | m_refData = new wxPenRefData; | |
77 | ||
26ac77db DW |
78 | M_PENDATA->m_vColour = rColour; |
79 | M_PENDATA->m_nWidth = nWidth; | |
80 | M_PENDATA->m_nStyle = nStyle; | |
81 | M_PENDATA->m_nJoin = wxJOIN_ROUND ; | |
82 | M_PENDATA->m_nCap = wxCAP_ROUND ; | |
83 | M_PENDATA->m_hPen = 0L; | |
84 | ||
0e320a79 DW |
85 | RealizeResource(); |
86 | ||
87 | if ( wxThePenList ) | |
88 | wxThePenList->AddPen(this); | |
26ac77db | 89 | } // end of wxPen::wxPen |
0e320a79 | 90 | |
26ac77db DW |
91 | wxPen::wxPen( |
92 | const wxBitmap& rStipple | |
93 | , int nWidth | |
94 | ) | |
0e320a79 DW |
95 | { |
96 | m_refData = new wxPenRefData; | |
97 | ||
26ac77db DW |
98 | M_PENDATA->m_vStipple = rStipple; |
99 | M_PENDATA->m_nWidth = nWidth; | |
100 | M_PENDATA->m_nStyle = wxSTIPPLE; | |
101 | M_PENDATA->m_nJoin = wxJOIN_ROUND ; | |
102 | M_PENDATA->m_nCap = wxCAP_ROUND ; | |
103 | M_PENDATA->m_hPen = 0; | |
0e320a79 DW |
104 | |
105 | RealizeResource(); | |
106 | ||
107 | if ( wxThePenList ) | |
108 | wxThePenList->AddPen(this); | |
26ac77db | 109 | } // end of wxPen::wxPen |
0e320a79 | 110 | |
cdf1e714 DW |
111 | bool wxPen::RealizeResource() |
112 | { | |
26ac77db DW |
113 | BOOL bOk; |
114 | ERRORID vError; | |
115 | wxString sError; | |
116 | ||
117 | if (M_PENDATA && M_PENDATA->m_hPen == 0L) | |
118 | { | |
119 | SIZEL vSize = {0, 0}; | |
120 | DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L}; | |
121 | HDC hDC = ::DevOpenDC( vHabmain | |
122 | ,OD_MEMORY | |
123 | ,"*" | |
124 | ,5L | |
125 | ,(PDEVOPENDATA)&vDop | |
126 | ,NULLHANDLE | |
127 | ); | |
128 | M_PENDATA->m_hPen = (WXHPEN)::GpiCreatePS( vHabmain | |
129 | ,hDC | |
130 | ,&vSize | |
131 | ,PU_PELS | GPIT_MICRO | GPIA_ASSOC | |
132 | ); | |
133 | } | |
134 | if (M_PENDATA) | |
135 | { | |
136 | // | |
137 | // Set the color table to RGB mode | |
138 | // | |
139 | if (!::GpiCreateLogColorTable( (HPS)M_PENDATA->m_hPen | |
140 | ,0L | |
141 | ,LCOLF_RGB | |
142 | ,0L | |
143 | ,0L | |
144 | ,NULL | |
145 | )) | |
146 | { | |
147 | vError = ::WinGetLastError(vHabmain); | |
148 | sError = wxPMErrorToStr(vError); | |
149 | wxLogError("Unable to set current color table to RGB mode. Error: %s\n", sError); | |
150 | return FALSE; | |
151 | } | |
152 | if (M_PENDATA->m_nStyle == wxTRANSPARENT) | |
153 | { | |
154 | return TRUE; | |
155 | } | |
156 | ||
157 | COLORREF vPmColour = 0L; | |
158 | USHORT uLineType = wx2os2PenStyle(M_PENDATA->m_nStyle); | |
159 | ||
160 | vPmColour = M_PENDATA->m_vColour.GetPixel(); | |
161 | ||
162 | USHORT uJoin = 0L; | |
163 | ||
164 | switch(M_PENDATA->m_nJoin) | |
165 | { | |
166 | case wxJOIN_BEVEL: | |
167 | uJoin = LINEJOIN_BEVEL; | |
168 | break; | |
169 | ||
170 | case wxJOIN_MITER: | |
171 | uJoin = LINEJOIN_MITRE; | |
172 | break; | |
173 | ||
174 | case wxJOIN_ROUND: | |
175 | uJoin = LINEJOIN_ROUND; | |
176 | break; | |
177 | } | |
178 | ||
179 | USHORT uCap = 0L; | |
180 | ||
181 | switch(M_PENDATA->m_nCap) | |
182 | { | |
183 | case wxCAP_PROJECTING: | |
184 | uCap = LINEEND_SQUARE; | |
185 | break; | |
186 | ||
187 | case wxCAP_BUTT: | |
188 | uCap = LINEEND_FLAT; | |
189 | break; | |
190 | ||
191 | case wxCAP_ROUND: | |
192 | uCap = LINEEND_ROUND; | |
193 | break; | |
194 | } | |
195 | m_vLineBundle.lColor = (LONG)vPmColour; | |
196 | m_vLineBundle.usMixMode = FM_OVERPAINT; | |
197 | m_vLineBundle.fxWidth = M_PENDATA->m_nWidth; | |
198 | m_vLineBundle.lGeomWidth = M_PENDATA->m_nWidth; | |
199 | m_vLineBundle.usType = uLineType; | |
200 | m_vLineBundle.usEnd = uCap; | |
201 | m_vLineBundle.usJoin = uJoin; | |
202 | ||
203 | bOk = ::GpiSetAttrs( M_PENDATA->m_hPen | |
204 | ,PRIM_LINE | |
205 | ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE | LBB_END | LBB_JOIN | |
206 | ,0L | |
207 | ,&m_vLineBundle | |
208 | ); | |
209 | if (!bOk) | |
210 | { | |
211 | vError = ::WinGetLastError(vHabmain); | |
212 | sError = wxPMErrorToStr(vError); | |
213 | wxLogError("Can't set Gpi attributes for a LINEBUNDLE. Error: %s\n", sError); | |
214 | return FALSE; | |
215 | } | |
216 | ||
217 | switch(M_PENDATA->m_nStyle) | |
218 | { | |
219 | case wxSTIPPLE: | |
220 | ::GpiSetBitmapId( M_PENDATA->m_hPen | |
221 | ,(USHORT)M_PENDATA->m_vStipple.GetHBITMAP() | |
222 | ,(USHORT)M_PENDATA->m_vStipple.GetId() | |
223 | ); | |
224 | ::GpiSetPatternSet( M_PENDATA->m_hPen | |
225 | ,(USHORT)M_PENDATA->m_vStipple.GetId() | |
226 | ); | |
227 | break; | |
228 | ||
229 | case wxBDIAGONAL_HATCH: | |
230 | m_vAreaBundle.usSymbol = PATSYM_DIAG3; | |
231 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
232 | break; | |
233 | ||
234 | case wxCROSSDIAG_HATCH: | |
235 | m_vAreaBundle.usSymbol = PATSYM_DIAGHATCH; | |
236 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
237 | break; | |
238 | ||
239 | case wxFDIAGONAL_HATCH: | |
240 | m_vAreaBundle.usSymbol = PATSYM_DIAG1; | |
241 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
242 | break; | |
243 | ||
244 | case wxCROSS_HATCH: | |
245 | m_vAreaBundle.usSymbol = PATSYM_HATCH; | |
246 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
247 | break; | |
248 | ||
249 | case wxHORIZONTAL_HATCH: | |
250 | m_vAreaBundle.usSymbol = PATSYM_HORIZ; | |
251 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
252 | break; | |
253 | ||
254 | case wxVERTICAL_HATCH: | |
255 | m_vAreaBundle.usSymbol = PATSYM_VERT; | |
256 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
257 | break; | |
258 | ||
259 | default: | |
260 | m_vAreaBundle.usSymbol = PATSYM_SOLID; | |
261 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
262 | break; | |
263 | } | |
264 | ||
265 | m_vAreaBundle.lColor = vPmColour; | |
8d854fa9 | 266 | m_vAreaBundle.lBackColor = RGB_WHITE; |
26ac77db DW |
267 | m_vAreaBundle.usMixMode = FM_OVERPAINT; |
268 | m_vAreaBundle.usBackMixMode = BM_OVERPAINT; | |
269 | ||
270 | bOk = ::GpiSetAttrs( M_PENDATA->m_hPen | |
271 | ,PRIM_AREA | |
272 | ,ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | | |
273 | ABB_SET | ABB_SYMBOL | |
274 | ,ABB_REF_POINT | |
275 | ,&m_vAreaBundle | |
276 | ); | |
277 | if (!bOk) | |
278 | { | |
279 | vError = ::WinGetLastError(vHabmain); | |
280 | sError = wxPMErrorToStr(vError); | |
281 | wxLogError("Can't set Gpi attributes for an AREABUNDLE. Error: %s\n", sError); | |
282 | } | |
283 | return bOk; | |
284 | } | |
cdf1e714 | 285 | return FALSE; |
26ac77db | 286 | } // end of wxPen::RealizeResource |
cdf1e714 DW |
287 | |
288 | WXHANDLE wxPen::GetResourceHandle() | |
289 | { | |
26ac77db | 290 | if (!M_PENDATA) |
cdf1e714 DW |
291 | return 0; |
292 | else | |
293 | return (WXHANDLE)M_PENDATA->m_hPen; | |
26ac77db | 294 | } // end of wxPen::GetResourceHandle |
cdf1e714 | 295 | |
26ac77db DW |
296 | bool wxPen::FreeResource( |
297 | bool bForce | |
298 | ) | |
cdf1e714 DW |
299 | { |
300 | if (M_PENDATA && (M_PENDATA->m_hPen != 0)) | |
301 | { | |
cdf1e714 DW |
302 | M_PENDATA->m_hPen = 0; |
303 | return TRUE; | |
304 | } | |
305 | else return FALSE; | |
26ac77db | 306 | } // end of wxPen::FreeResource |
cdf1e714 | 307 | |
e6ebb514 DW |
308 | bool wxPen::IsFree() const |
309 | { | |
26ac77db | 310 | return (M_PENDATA && M_PENDATA->m_hPen == 0); |
e6ebb514 DW |
311 | } |
312 | ||
0e320a79 DW |
313 | void wxPen::Unshare() |
314 | { | |
cdf1e714 DW |
315 | // Don't change shared data |
316 | if (!m_refData) | |
0e320a79 | 317 | { |
cdf1e714 DW |
318 | m_refData = new wxPenRefData(); |
319 | } | |
0e320a79 DW |
320 | else |
321 | { | |
cdf1e714 DW |
322 | wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData); |
323 | UnRef(); | |
324 | m_refData = ref; | |
325 | } | |
26ac77db | 326 | } // end of wxPen::Unshare |
0e320a79 | 327 | |
26ac77db DW |
328 | void wxPen::SetColour( |
329 | const wxColour& rColour | |
330 | ) | |
0e320a79 DW |
331 | { |
332 | Unshare(); | |
26ac77db | 333 | M_PENDATA->m_vColour = rColour; |
0e320a79 | 334 | RealizeResource(); |
26ac77db | 335 | } // end of wxPen::SetColour |
0e320a79 | 336 | |
26ac77db DW |
337 | void wxPen::SetColour( |
338 | unsigned char cRed | |
339 | , unsigned char cGreen | |
340 | , unsigned char cBlue | |
341 | ) | |
0e320a79 DW |
342 | { |
343 | Unshare(); | |
26ac77db | 344 | M_PENDATA->m_vColour.Set(cRed, cGreen, cBlue); |
0e320a79 | 345 | RealizeResource(); |
26ac77db | 346 | } // end of wxPen::SetColour |
0e320a79 | 347 | |
26ac77db DW |
348 | void wxPen::SetPS( |
349 | HPS hPS | |
350 | ) | |
0e320a79 DW |
351 | { |
352 | Unshare(); | |
26ac77db DW |
353 | if (M_PENDATA->m_hPen) |
354 | ::GpiDestroyPS(M_PENDATA->m_hPen); | |
355 | M_PENDATA->m_hPen = hPS; | |
0e320a79 | 356 | RealizeResource(); |
26ac77db | 357 | } // end of WxWinGdi_CPen::SetPS |
0e320a79 | 358 | |
26ac77db DW |
359 | void wxPen::SetWidth( |
360 | int nWidth | |
361 | ) | |
0e320a79 DW |
362 | { |
363 | Unshare(); | |
26ac77db | 364 | M_PENDATA->m_nWidth = nWidth; |
0e320a79 | 365 | RealizeResource(); |
26ac77db | 366 | } // end of wxPen::SetWidth |
0e320a79 | 367 | |
26ac77db DW |
368 | void wxPen::SetStyle( |
369 | int nStyle | |
370 | ) | |
0e320a79 DW |
371 | { |
372 | Unshare(); | |
26ac77db | 373 | M_PENDATA->m_nStyle = nStyle; |
0e320a79 | 374 | RealizeResource(); |
26ac77db | 375 | } // end of wxPen::SetStyle |
0e320a79 | 376 | |
26ac77db DW |
377 | void wxPen::SetStipple( |
378 | const wxBitmap& rStipple | |
379 | ) | |
0e320a79 DW |
380 | { |
381 | Unshare(); | |
26ac77db DW |
382 | M_PENDATA->m_vStipple = rStipple; |
383 | M_PENDATA->m_nStyle = wxSTIPPLE; | |
0e320a79 | 384 | RealizeResource(); |
26ac77db | 385 | } // end of wxPen::SetStipple |
0e320a79 | 386 | |
26ac77db DW |
387 | void wxPen::SetDashes( |
388 | int nNbDashes | |
389 | , const wxDash* pDash | |
390 | ) | |
391 | { | |
392 | // | |
393 | // Does nothing under OS/2 | |
394 | // | |
395 | } // end of wxPen::SetDashes | |
396 | ||
397 | void wxPen::SetJoin( | |
398 | int nJoin | |
399 | ) | |
0e320a79 DW |
400 | { |
401 | Unshare(); | |
26ac77db | 402 | M_PENDATA->m_nJoin = nJoin; |
0e320a79 | 403 | RealizeResource(); |
26ac77db | 404 | } // end of wxPen::SetJoin |
0e320a79 | 405 | |
26ac77db DW |
406 | void wxPen::SetCap( |
407 | int nCap | |
408 | ) | |
0e320a79 DW |
409 | { |
410 | Unshare(); | |
26ac77db | 411 | M_PENDATA->m_nCap = nCap; |
0e320a79 | 412 | RealizeResource(); |
26ac77db | 413 | } // end of wxPen::SetCap |
0e320a79 | 414 | |
26ac77db DW |
415 | int wx2os2PenStyle( |
416 | int nWxStyle | |
417 | ) | |
cdf1e714 | 418 | { |
26ac77db DW |
419 | int nPMStyle = 0; |
420 | ||
421 | switch (nWxStyle) | |
fb46a9a6 | 422 | { |
26ac77db DW |
423 | case wxDOT: |
424 | nPMStyle = LINETYPE_DOT; | |
425 | break; | |
426 | ||
427 | case wxDOT_DASH: | |
428 | nPMStyle = LINETYPE_DASHDOT; | |
429 | break; | |
430 | ||
431 | case wxSHORT_DASH: | |
432 | nPMStyle = LINETYPE_SHORTDASH; | |
433 | break; | |
434 | ||
435 | case wxLONG_DASH: | |
436 | nPMStyle = LINETYPE_LONGDASH; | |
437 | break; | |
438 | ||
439 | case wxTRANSPARENT: | |
440 | nPMStyle = LINETYPE_INVISIBLE; | |
441 | break; | |
442 | ||
443 | case wxUSER_DASH: | |
444 | nPMStyle = LINETYPE_DASHDOUBLEDOT; // We must make a choice... This is mine! | |
445 | break; | |
446 | ||
447 | case wxSOLID: | |
448 | default: | |
449 | nPMStyle = LINETYPE_SOLID; | |
450 | break; | |
451 | } | |
452 | return nPMStyle; | |
453 | } // end of wx2os2PenStyle | |
0e320a79 DW |
454 | |
455 |