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