]>
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 | |
272ebf16 SN |
111 | int wx2os2PenStyle( |
112 | int nWxStyle | |
113 | ); | |
114 | ||
cdf1e714 DW |
115 | bool wxPen::RealizeResource() |
116 | { | |
26ac77db DW |
117 | BOOL bOk; |
118 | ERRORID vError; | |
119 | wxString sError; | |
120 | ||
121 | if (M_PENDATA && M_PENDATA->m_hPen == 0L) | |
122 | { | |
123 | SIZEL vSize = {0, 0}; | |
124 | DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L}; | |
125 | HDC hDC = ::DevOpenDC( vHabmain | |
126 | ,OD_MEMORY | |
127 | ,"*" | |
128 | ,5L | |
129 | ,(PDEVOPENDATA)&vDop | |
130 | ,NULLHANDLE | |
131 | ); | |
132 | M_PENDATA->m_hPen = (WXHPEN)::GpiCreatePS( vHabmain | |
133 | ,hDC | |
134 | ,&vSize | |
135 | ,PU_PELS | GPIT_MICRO | GPIA_ASSOC | |
136 | ); | |
137 | } | |
138 | if (M_PENDATA) | |
139 | { | |
140 | // | |
141 | // Set the color table to RGB mode | |
142 | // | |
143 | if (!::GpiCreateLogColorTable( (HPS)M_PENDATA->m_hPen | |
144 | ,0L | |
145 | ,LCOLF_RGB | |
146 | ,0L | |
147 | ,0L | |
148 | ,NULL | |
149 | )) | |
150 | { | |
151 | vError = ::WinGetLastError(vHabmain); | |
152 | sError = wxPMErrorToStr(vError); | |
153 | wxLogError("Unable to set current color table to RGB mode. Error: %s\n", sError); | |
154 | return FALSE; | |
155 | } | |
156 | if (M_PENDATA->m_nStyle == wxTRANSPARENT) | |
157 | { | |
158 | return TRUE; | |
159 | } | |
160 | ||
161 | COLORREF vPmColour = 0L; | |
162 | USHORT uLineType = wx2os2PenStyle(M_PENDATA->m_nStyle); | |
163 | ||
164 | vPmColour = M_PENDATA->m_vColour.GetPixel(); | |
165 | ||
166 | USHORT uJoin = 0L; | |
167 | ||
168 | switch(M_PENDATA->m_nJoin) | |
169 | { | |
170 | case wxJOIN_BEVEL: | |
171 | uJoin = LINEJOIN_BEVEL; | |
172 | break; | |
173 | ||
174 | case wxJOIN_MITER: | |
175 | uJoin = LINEJOIN_MITRE; | |
176 | break; | |
177 | ||
178 | case wxJOIN_ROUND: | |
179 | uJoin = LINEJOIN_ROUND; | |
180 | break; | |
181 | } | |
182 | ||
183 | USHORT uCap = 0L; | |
184 | ||
185 | switch(M_PENDATA->m_nCap) | |
186 | { | |
187 | case wxCAP_PROJECTING: | |
188 | uCap = LINEEND_SQUARE; | |
189 | break; | |
190 | ||
191 | case wxCAP_BUTT: | |
192 | uCap = LINEEND_FLAT; | |
193 | break; | |
194 | ||
195 | case wxCAP_ROUND: | |
196 | uCap = LINEEND_ROUND; | |
197 | break; | |
198 | } | |
199 | m_vLineBundle.lColor = (LONG)vPmColour; | |
200 | m_vLineBundle.usMixMode = FM_OVERPAINT; | |
201 | m_vLineBundle.fxWidth = M_PENDATA->m_nWidth; | |
202 | m_vLineBundle.lGeomWidth = M_PENDATA->m_nWidth; | |
203 | m_vLineBundle.usType = uLineType; | |
204 | m_vLineBundle.usEnd = uCap; | |
205 | m_vLineBundle.usJoin = uJoin; | |
206 | ||
207 | bOk = ::GpiSetAttrs( M_PENDATA->m_hPen | |
208 | ,PRIM_LINE | |
209 | ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE | LBB_END | LBB_JOIN | |
210 | ,0L | |
211 | ,&m_vLineBundle | |
212 | ); | |
213 | if (!bOk) | |
214 | { | |
215 | vError = ::WinGetLastError(vHabmain); | |
216 | sError = wxPMErrorToStr(vError); | |
217 | wxLogError("Can't set Gpi attributes for a LINEBUNDLE. Error: %s\n", sError); | |
218 | return FALSE; | |
219 | } | |
220 | ||
d697657f DW |
221 | ULONG flAttrMask = 0L; |
222 | ULONG flDefMask = 0L; | |
26ac77db DW |
223 | switch(M_PENDATA->m_nStyle) |
224 | { | |
225 | case wxSTIPPLE: | |
226 | ::GpiSetBitmapId( M_PENDATA->m_hPen | |
227 | ,(USHORT)M_PENDATA->m_vStipple.GetHBITMAP() | |
228 | ,(USHORT)M_PENDATA->m_vStipple.GetId() | |
229 | ); | |
230 | ::GpiSetPatternSet( M_PENDATA->m_hPen | |
231 | ,(USHORT)M_PENDATA->m_vStipple.GetId() | |
232 | ); | |
d697657f DW |
233 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SET | ABB_SYMBOL; |
234 | flDefMask = ABB_REF_POINT; | |
26ac77db DW |
235 | break; |
236 | ||
237 | case wxBDIAGONAL_HATCH: | |
238 | m_vAreaBundle.usSymbol = PATSYM_DIAG3; | |
239 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
240 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
241 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
242 | break; |
243 | ||
244 | case wxCROSSDIAG_HATCH: | |
245 | m_vAreaBundle.usSymbol = PATSYM_DIAGHATCH; | |
246 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
247 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
248 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
249 | break; |
250 | ||
251 | case wxFDIAGONAL_HATCH: | |
252 | m_vAreaBundle.usSymbol = PATSYM_DIAG1; | |
253 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
254 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
255 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
256 | break; |
257 | ||
258 | case wxCROSS_HATCH: | |
259 | m_vAreaBundle.usSymbol = PATSYM_HATCH; | |
260 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
261 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
262 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
263 | break; |
264 | ||
265 | case wxHORIZONTAL_HATCH: | |
266 | m_vAreaBundle.usSymbol = PATSYM_HORIZ; | |
267 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
268 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
269 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
270 | break; |
271 | ||
272 | case wxVERTICAL_HATCH: | |
273 | m_vAreaBundle.usSymbol = PATSYM_VERT; | |
274 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
275 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
276 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
277 | break; |
278 | ||
279 | default: | |
280 | m_vAreaBundle.usSymbol = PATSYM_SOLID; | |
281 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
282 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
283 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
284 | break; |
285 | } | |
286 | ||
287 | m_vAreaBundle.lColor = vPmColour; | |
8d854fa9 | 288 | m_vAreaBundle.lBackColor = RGB_WHITE; |
26ac77db DW |
289 | m_vAreaBundle.usMixMode = FM_OVERPAINT; |
290 | m_vAreaBundle.usBackMixMode = BM_OVERPAINT; | |
291 | ||
292 | bOk = ::GpiSetAttrs( M_PENDATA->m_hPen | |
293 | ,PRIM_AREA | |
d697657f DW |
294 | ,flAttrMask |
295 | ,flDefMask | |
26ac77db DW |
296 | ,&m_vAreaBundle |
297 | ); | |
298 | if (!bOk) | |
299 | { | |
300 | vError = ::WinGetLastError(vHabmain); | |
301 | sError = wxPMErrorToStr(vError); | |
302 | wxLogError("Can't set Gpi attributes for an AREABUNDLE. Error: %s\n", sError); | |
303 | } | |
304 | return bOk; | |
305 | } | |
cdf1e714 | 306 | return FALSE; |
26ac77db | 307 | } // end of wxPen::RealizeResource |
cdf1e714 DW |
308 | |
309 | WXHANDLE wxPen::GetResourceHandle() | |
310 | { | |
26ac77db | 311 | if (!M_PENDATA) |
cdf1e714 DW |
312 | return 0; |
313 | else | |
314 | return (WXHANDLE)M_PENDATA->m_hPen; | |
26ac77db | 315 | } // end of wxPen::GetResourceHandle |
cdf1e714 | 316 | |
26ac77db DW |
317 | bool wxPen::FreeResource( |
318 | bool bForce | |
319 | ) | |
cdf1e714 DW |
320 | { |
321 | if (M_PENDATA && (M_PENDATA->m_hPen != 0)) | |
322 | { | |
cdf1e714 DW |
323 | M_PENDATA->m_hPen = 0; |
324 | return TRUE; | |
325 | } | |
326 | else return FALSE; | |
26ac77db | 327 | } // end of wxPen::FreeResource |
cdf1e714 | 328 | |
e6ebb514 DW |
329 | bool wxPen::IsFree() const |
330 | { | |
26ac77db | 331 | return (M_PENDATA && M_PENDATA->m_hPen == 0); |
e6ebb514 DW |
332 | } |
333 | ||
0e320a79 DW |
334 | void wxPen::Unshare() |
335 | { | |
cdf1e714 DW |
336 | // Don't change shared data |
337 | if (!m_refData) | |
0e320a79 | 338 | { |
cdf1e714 DW |
339 | m_refData = new wxPenRefData(); |
340 | } | |
0e320a79 DW |
341 | else |
342 | { | |
cdf1e714 DW |
343 | wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData); |
344 | UnRef(); | |
345 | m_refData = ref; | |
346 | } | |
26ac77db | 347 | } // end of wxPen::Unshare |
0e320a79 | 348 | |
26ac77db DW |
349 | void wxPen::SetColour( |
350 | const wxColour& rColour | |
351 | ) | |
0e320a79 DW |
352 | { |
353 | Unshare(); | |
26ac77db | 354 | M_PENDATA->m_vColour = rColour; |
0e320a79 | 355 | RealizeResource(); |
26ac77db | 356 | } // end of wxPen::SetColour |
0e320a79 | 357 | |
26ac77db DW |
358 | void wxPen::SetColour( |
359 | unsigned char cRed | |
360 | , unsigned char cGreen | |
361 | , unsigned char cBlue | |
362 | ) | |
0e320a79 DW |
363 | { |
364 | Unshare(); | |
26ac77db | 365 | M_PENDATA->m_vColour.Set(cRed, cGreen, cBlue); |
0e320a79 | 366 | RealizeResource(); |
26ac77db | 367 | } // end of wxPen::SetColour |
0e320a79 | 368 | |
26ac77db DW |
369 | void wxPen::SetPS( |
370 | HPS hPS | |
371 | ) | |
0e320a79 DW |
372 | { |
373 | Unshare(); | |
26ac77db DW |
374 | if (M_PENDATA->m_hPen) |
375 | ::GpiDestroyPS(M_PENDATA->m_hPen); | |
376 | M_PENDATA->m_hPen = hPS; | |
0e320a79 | 377 | RealizeResource(); |
26ac77db | 378 | } // end of WxWinGdi_CPen::SetPS |
0e320a79 | 379 | |
26ac77db DW |
380 | void wxPen::SetWidth( |
381 | int nWidth | |
382 | ) | |
0e320a79 DW |
383 | { |
384 | Unshare(); | |
26ac77db | 385 | M_PENDATA->m_nWidth = nWidth; |
0e320a79 | 386 | RealizeResource(); |
26ac77db | 387 | } // end of wxPen::SetWidth |
0e320a79 | 388 | |
26ac77db DW |
389 | void wxPen::SetStyle( |
390 | int nStyle | |
391 | ) | |
0e320a79 DW |
392 | { |
393 | Unshare(); | |
26ac77db | 394 | M_PENDATA->m_nStyle = nStyle; |
0e320a79 | 395 | RealizeResource(); |
26ac77db | 396 | } // end of wxPen::SetStyle |
0e320a79 | 397 | |
26ac77db DW |
398 | void wxPen::SetStipple( |
399 | const wxBitmap& rStipple | |
400 | ) | |
0e320a79 DW |
401 | { |
402 | Unshare(); | |
26ac77db DW |
403 | M_PENDATA->m_vStipple = rStipple; |
404 | M_PENDATA->m_nStyle = wxSTIPPLE; | |
0e320a79 | 405 | RealizeResource(); |
26ac77db | 406 | } // end of wxPen::SetStipple |
0e320a79 | 407 | |
26ac77db DW |
408 | void wxPen::SetDashes( |
409 | int nNbDashes | |
410 | , const wxDash* pDash | |
411 | ) | |
412 | { | |
413 | // | |
414 | // Does nothing under OS/2 | |
415 | // | |
416 | } // end of wxPen::SetDashes | |
417 | ||
418 | void wxPen::SetJoin( | |
419 | int nJoin | |
420 | ) | |
0e320a79 DW |
421 | { |
422 | Unshare(); | |
26ac77db | 423 | M_PENDATA->m_nJoin = nJoin; |
0e320a79 | 424 | RealizeResource(); |
26ac77db | 425 | } // end of wxPen::SetJoin |
0e320a79 | 426 | |
26ac77db DW |
427 | void wxPen::SetCap( |
428 | int nCap | |
429 | ) | |
0e320a79 DW |
430 | { |
431 | Unshare(); | |
26ac77db | 432 | M_PENDATA->m_nCap = nCap; |
0e320a79 | 433 | RealizeResource(); |
26ac77db | 434 | } // end of wxPen::SetCap |
0e320a79 | 435 | |
26ac77db DW |
436 | int wx2os2PenStyle( |
437 | int nWxStyle | |
438 | ) | |
cdf1e714 | 439 | { |
26ac77db DW |
440 | int nPMStyle = 0; |
441 | ||
442 | switch (nWxStyle) | |
fb46a9a6 | 443 | { |
26ac77db DW |
444 | case wxDOT: |
445 | nPMStyle = LINETYPE_DOT; | |
446 | break; | |
447 | ||
448 | case wxDOT_DASH: | |
449 | nPMStyle = LINETYPE_DASHDOT; | |
450 | break; | |
451 | ||
452 | case wxSHORT_DASH: | |
453 | nPMStyle = LINETYPE_SHORTDASH; | |
454 | break; | |
455 | ||
456 | case wxLONG_DASH: | |
457 | nPMStyle = LINETYPE_LONGDASH; | |
458 | break; | |
459 | ||
460 | case wxTRANSPARENT: | |
461 | nPMStyle = LINETYPE_INVISIBLE; | |
462 | break; | |
463 | ||
464 | case wxUSER_DASH: | |
465 | nPMStyle = LINETYPE_DASHDOUBLEDOT; // We must make a choice... This is mine! | |
466 | break; | |
467 | ||
468 | case wxSOLID: | |
469 | default: | |
470 | nPMStyle = LINETYPE_SOLID; | |
471 | break; | |
472 | } | |
473 | return nPMStyle; | |
474 | } // end of wx2os2PenStyle | |
0e320a79 DW |
475 | |
476 |