]>
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 | ||
d697657f DW |
217 | ULONG flAttrMask = 0L; |
218 | ULONG flDefMask = 0L; | |
26ac77db DW |
219 | switch(M_PENDATA->m_nStyle) |
220 | { | |
221 | case wxSTIPPLE: | |
222 | ::GpiSetBitmapId( M_PENDATA->m_hPen | |
223 | ,(USHORT)M_PENDATA->m_vStipple.GetHBITMAP() | |
224 | ,(USHORT)M_PENDATA->m_vStipple.GetId() | |
225 | ); | |
226 | ::GpiSetPatternSet( M_PENDATA->m_hPen | |
227 | ,(USHORT)M_PENDATA->m_vStipple.GetId() | |
228 | ); | |
d697657f DW |
229 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SET | ABB_SYMBOL; |
230 | flDefMask = ABB_REF_POINT; | |
26ac77db DW |
231 | break; |
232 | ||
233 | case wxBDIAGONAL_HATCH: | |
234 | m_vAreaBundle.usSymbol = PATSYM_DIAG3; | |
235 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
236 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
237 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
238 | break; |
239 | ||
240 | case wxCROSSDIAG_HATCH: | |
241 | m_vAreaBundle.usSymbol = PATSYM_DIAGHATCH; | |
242 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
243 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
244 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
245 | break; |
246 | ||
247 | case wxFDIAGONAL_HATCH: | |
248 | m_vAreaBundle.usSymbol = PATSYM_DIAG1; | |
249 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
250 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
251 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
252 | break; |
253 | ||
254 | case wxCROSS_HATCH: | |
255 | m_vAreaBundle.usSymbol = PATSYM_HATCH; | |
256 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
257 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
258 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
259 | break; |
260 | ||
261 | case wxHORIZONTAL_HATCH: | |
262 | m_vAreaBundle.usSymbol = PATSYM_HORIZ; | |
263 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
264 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
265 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
266 | break; |
267 | ||
268 | case wxVERTICAL_HATCH: | |
269 | m_vAreaBundle.usSymbol = PATSYM_VERT; | |
270 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
271 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
272 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
273 | break; |
274 | ||
275 | default: | |
276 | m_vAreaBundle.usSymbol = PATSYM_SOLID; | |
277 | m_vAreaBundle.usSet = LCID_DEFAULT; | |
d697657f DW |
278 | flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL; |
279 | flDefMask = ABB_SET | ABB_REF_POINT; | |
26ac77db DW |
280 | break; |
281 | } | |
282 | ||
283 | m_vAreaBundle.lColor = vPmColour; | |
8d854fa9 | 284 | m_vAreaBundle.lBackColor = RGB_WHITE; |
26ac77db DW |
285 | m_vAreaBundle.usMixMode = FM_OVERPAINT; |
286 | m_vAreaBundle.usBackMixMode = BM_OVERPAINT; | |
287 | ||
288 | bOk = ::GpiSetAttrs( M_PENDATA->m_hPen | |
289 | ,PRIM_AREA | |
d697657f DW |
290 | ,flAttrMask |
291 | ,flDefMask | |
26ac77db DW |
292 | ,&m_vAreaBundle |
293 | ); | |
294 | if (!bOk) | |
295 | { | |
296 | vError = ::WinGetLastError(vHabmain); | |
297 | sError = wxPMErrorToStr(vError); | |
298 | wxLogError("Can't set Gpi attributes for an AREABUNDLE. Error: %s\n", sError); | |
299 | } | |
300 | return bOk; | |
301 | } | |
cdf1e714 | 302 | return FALSE; |
26ac77db | 303 | } // end of wxPen::RealizeResource |
cdf1e714 DW |
304 | |
305 | WXHANDLE wxPen::GetResourceHandle() | |
306 | { | |
26ac77db | 307 | if (!M_PENDATA) |
cdf1e714 DW |
308 | return 0; |
309 | else | |
310 | return (WXHANDLE)M_PENDATA->m_hPen; | |
26ac77db | 311 | } // end of wxPen::GetResourceHandle |
cdf1e714 | 312 | |
26ac77db DW |
313 | bool wxPen::FreeResource( |
314 | bool bForce | |
315 | ) | |
cdf1e714 DW |
316 | { |
317 | if (M_PENDATA && (M_PENDATA->m_hPen != 0)) | |
318 | { | |
cdf1e714 DW |
319 | M_PENDATA->m_hPen = 0; |
320 | return TRUE; | |
321 | } | |
322 | else return FALSE; | |
26ac77db | 323 | } // end of wxPen::FreeResource |
cdf1e714 | 324 | |
e6ebb514 DW |
325 | bool wxPen::IsFree() const |
326 | { | |
26ac77db | 327 | return (M_PENDATA && M_PENDATA->m_hPen == 0); |
e6ebb514 DW |
328 | } |
329 | ||
0e320a79 DW |
330 | void wxPen::Unshare() |
331 | { | |
cdf1e714 DW |
332 | // Don't change shared data |
333 | if (!m_refData) | |
0e320a79 | 334 | { |
cdf1e714 DW |
335 | m_refData = new wxPenRefData(); |
336 | } | |
0e320a79 DW |
337 | else |
338 | { | |
cdf1e714 DW |
339 | wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData); |
340 | UnRef(); | |
341 | m_refData = ref; | |
342 | } | |
26ac77db | 343 | } // end of wxPen::Unshare |
0e320a79 | 344 | |
26ac77db DW |
345 | void wxPen::SetColour( |
346 | const wxColour& rColour | |
347 | ) | |
0e320a79 DW |
348 | { |
349 | Unshare(); | |
26ac77db | 350 | M_PENDATA->m_vColour = rColour; |
0e320a79 | 351 | RealizeResource(); |
26ac77db | 352 | } // end of wxPen::SetColour |
0e320a79 | 353 | |
26ac77db DW |
354 | void wxPen::SetColour( |
355 | unsigned char cRed | |
356 | , unsigned char cGreen | |
357 | , unsigned char cBlue | |
358 | ) | |
0e320a79 DW |
359 | { |
360 | Unshare(); | |
26ac77db | 361 | M_PENDATA->m_vColour.Set(cRed, cGreen, cBlue); |
0e320a79 | 362 | RealizeResource(); |
26ac77db | 363 | } // end of wxPen::SetColour |
0e320a79 | 364 | |
26ac77db DW |
365 | void wxPen::SetPS( |
366 | HPS hPS | |
367 | ) | |
0e320a79 DW |
368 | { |
369 | Unshare(); | |
26ac77db DW |
370 | if (M_PENDATA->m_hPen) |
371 | ::GpiDestroyPS(M_PENDATA->m_hPen); | |
372 | M_PENDATA->m_hPen = hPS; | |
0e320a79 | 373 | RealizeResource(); |
26ac77db | 374 | } // end of WxWinGdi_CPen::SetPS |
0e320a79 | 375 | |
26ac77db DW |
376 | void wxPen::SetWidth( |
377 | int nWidth | |
378 | ) | |
0e320a79 DW |
379 | { |
380 | Unshare(); | |
26ac77db | 381 | M_PENDATA->m_nWidth = nWidth; |
0e320a79 | 382 | RealizeResource(); |
26ac77db | 383 | } // end of wxPen::SetWidth |
0e320a79 | 384 | |
26ac77db DW |
385 | void wxPen::SetStyle( |
386 | int nStyle | |
387 | ) | |
0e320a79 DW |
388 | { |
389 | Unshare(); | |
26ac77db | 390 | M_PENDATA->m_nStyle = nStyle; |
0e320a79 | 391 | RealizeResource(); |
26ac77db | 392 | } // end of wxPen::SetStyle |
0e320a79 | 393 | |
26ac77db DW |
394 | void wxPen::SetStipple( |
395 | const wxBitmap& rStipple | |
396 | ) | |
0e320a79 DW |
397 | { |
398 | Unshare(); | |
26ac77db DW |
399 | M_PENDATA->m_vStipple = rStipple; |
400 | M_PENDATA->m_nStyle = wxSTIPPLE; | |
0e320a79 | 401 | RealizeResource(); |
26ac77db | 402 | } // end of wxPen::SetStipple |
0e320a79 | 403 | |
26ac77db DW |
404 | void wxPen::SetDashes( |
405 | int nNbDashes | |
406 | , const wxDash* pDash | |
407 | ) | |
408 | { | |
409 | // | |
410 | // Does nothing under OS/2 | |
411 | // | |
412 | } // end of wxPen::SetDashes | |
413 | ||
414 | void wxPen::SetJoin( | |
415 | int nJoin | |
416 | ) | |
0e320a79 DW |
417 | { |
418 | Unshare(); | |
26ac77db | 419 | M_PENDATA->m_nJoin = nJoin; |
0e320a79 | 420 | RealizeResource(); |
26ac77db | 421 | } // end of wxPen::SetJoin |
0e320a79 | 422 | |
26ac77db DW |
423 | void wxPen::SetCap( |
424 | int nCap | |
425 | ) | |
0e320a79 DW |
426 | { |
427 | Unshare(); | |
26ac77db | 428 | M_PENDATA->m_nCap = nCap; |
0e320a79 | 429 | RealizeResource(); |
26ac77db | 430 | } // end of wxPen::SetCap |
0e320a79 | 431 | |
26ac77db DW |
432 | int wx2os2PenStyle( |
433 | int nWxStyle | |
434 | ) | |
cdf1e714 | 435 | { |
26ac77db DW |
436 | int nPMStyle = 0; |
437 | ||
438 | switch (nWxStyle) | |
fb46a9a6 | 439 | { |
26ac77db DW |
440 | case wxDOT: |
441 | nPMStyle = LINETYPE_DOT; | |
442 | break; | |
443 | ||
444 | case wxDOT_DASH: | |
445 | nPMStyle = LINETYPE_DASHDOT; | |
446 | break; | |
447 | ||
448 | case wxSHORT_DASH: | |
449 | nPMStyle = LINETYPE_SHORTDASH; | |
450 | break; | |
451 | ||
452 | case wxLONG_DASH: | |
453 | nPMStyle = LINETYPE_LONGDASH; | |
454 | break; | |
455 | ||
456 | case wxTRANSPARENT: | |
457 | nPMStyle = LINETYPE_INVISIBLE; | |
458 | break; | |
459 | ||
460 | case wxUSER_DASH: | |
461 | nPMStyle = LINETYPE_DASHDOUBLEDOT; // We must make a choice... This is mine! | |
462 | break; | |
463 | ||
464 | case wxSOLID: | |
465 | default: | |
466 | nPMStyle = LINETYPE_SOLID; | |
467 | break; | |
468 | } | |
469 | return nPMStyle; | |
470 | } // end of wx2os2PenStyle | |
0e320a79 DW |
471 | |
472 |