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