]> git.saurik.com Git - wxWidgets.git/blame - src/os2/pen.cpp
fix for non precomp
[wxWidgets.git] / src / os2 / pen.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
46562151 2// Name: src/os2/pen.cpp
0e320a79 3// Purpose: wxPen
cdf1e714 4// Author: David Webster
0e320a79 5// Modified by:
cdf1e714 6// Created: 10/10/99
0e320a79 7// RCS-ID: $Id$
cdf1e714 8// Copyright: (c) David Webster
65571936 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 15#ifndef WX_PRECOMP
521bf4ff
WS
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"
0e320a79
DW
22#endif
23
cdf1e714
DW
24#include "wx/os2/private.h"
25#include "assert.h"
26
004fd0c8 27IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
004fd0c8 28
0e320a79
DW
29wxPenRefData::wxPenRefData()
30{
26ac77db
DW
31 m_nStyle = wxSOLID;
32 m_nWidth = 1;
33 m_nJoin = wxJOIN_ROUND ;
34 m_nCap = wxCAP_ROUND ;
9e878707
SN
35 m_nbDash = 0 ;
36 m_dash = (wxDash*)NULL;
26ac77db
DW
37 m_hPen = 0L;
38} // end of wxPenRefData::wxPenRefData
39
40wxPenRefData::wxPenRefData(
41 const wxPenRefData& rData
42)
0e320a79 43{
26ac77db
DW
44 m_nStyle = rData.m_nStyle;
45 m_nWidth = rData.m_nWidth;
46 m_nJoin = rData.m_nJoin;
47 m_nCap = rData.m_nCap;
9e878707
SN
48 m_nbDash = rData.m_nbDash;
49 m_dash = rData.m_dash;
26ac77db
DW
50 m_vColour = rData.m_vColour;
51 m_hPen = 0L;
52} // end of wxPenRefData::wxPenRefData
0e320a79
DW
53
54wxPenRefData::~wxPenRefData()
55{
26ac77db 56} // end of wxPenRefData::~wxPenRefData
0e320a79 57
26ac77db 58//
0e320a79 59// Pens
26ac77db 60//
0e320a79
DW
61wxPen::wxPen()
62{
63 if ( wxThePenList )
64 wxThePenList->AddPen(this);
26ac77db 65} // end of wxPen::wxPen
0e320a79
DW
66
67wxPen::~wxPen()
68{
69 if (wxThePenList)
70 wxThePenList->RemovePen(this);
26ac77db 71} // end of wxPen::wxPen
0e320a79
DW
72
73// Should implement Create
26ac77db
DW
74wxPen::wxPen(
75 const wxColour& rColour
76, int nWidth
77, int nStyle
78)
0e320a79
DW
79{
80 m_refData = new wxPenRefData;
81
26ac77db
DW
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
0e320a79
DW
89 RealizeResource();
90
91 if ( wxThePenList )
92 wxThePenList->AddPen(this);
26ac77db 93} // end of wxPen::wxPen
0e320a79 94
26ac77db
DW
95wxPen::wxPen(
96 const wxBitmap& rStipple
97, int nWidth
98)
0e320a79
DW
99{
100 m_refData = new wxPenRefData;
101
26ac77db
DW
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;
0e320a79
DW
108
109 RealizeResource();
110
111 if ( wxThePenList )
112 wxThePenList->AddPen(this);
26ac77db 113} // end of wxPen::wxPen
0e320a79 114
272ebf16
SN
115int wx2os2PenStyle(
116 int nWxStyle
117);
118
cdf1e714
DW
119bool wxPen::RealizeResource()
120{
26ac77db
DW
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);
0fba44b4 157 wxLogError(_T("Unable to set current color table to RGB mode. Error: %s\n"), sError.c_str());
46562151 158 return false;
26ac77db
DW
159 }
160 if (M_PENDATA->m_nStyle == wxTRANSPARENT)
161 {
6670f564 162 return true;
26ac77db
DW
163 }
164
6670f564
WS
165 COLORREF vPmColour = 0L;
166 USHORT uLineType = (USHORT)wx2os2PenStyle(M_PENDATA->m_nStyle);
26ac77db
DW
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);
0fba44b4 221 wxLogError(_T("Can't set Gpi attributes for a LINEBUNDLE. Error: %s\n"), sError.c_str());
46562151 222 return false;
26ac77db
DW
223 }
224
d697657f
DW
225 ULONG flAttrMask = 0L;
226 ULONG flDefMask = 0L;
26ac77db
DW
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 );
d697657f
DW
237 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SET | ABB_SYMBOL;
238 flDefMask = ABB_REF_POINT;
26ac77db
DW
239 break;
240
241 case wxBDIAGONAL_HATCH:
242 m_vAreaBundle.usSymbol = PATSYM_DIAG3;
243 m_vAreaBundle.usSet = LCID_DEFAULT;
d697657f
DW
244 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL;
245 flDefMask = ABB_SET | ABB_REF_POINT;
26ac77db
DW
246 break;
247
248 case wxCROSSDIAG_HATCH:
249 m_vAreaBundle.usSymbol = PATSYM_DIAGHATCH;
250 m_vAreaBundle.usSet = LCID_DEFAULT;
d697657f
DW
251 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL;
252 flDefMask = ABB_SET | ABB_REF_POINT;
26ac77db
DW
253 break;
254
255 case wxFDIAGONAL_HATCH:
256 m_vAreaBundle.usSymbol = PATSYM_DIAG1;
257 m_vAreaBundle.usSet = LCID_DEFAULT;
d697657f
DW
258 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL;
259 flDefMask = ABB_SET | ABB_REF_POINT;
26ac77db
DW
260 break;
261
262 case wxCROSS_HATCH:
263 m_vAreaBundle.usSymbol = PATSYM_HATCH;
264 m_vAreaBundle.usSet = LCID_DEFAULT;
d697657f
DW
265 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL;
266 flDefMask = ABB_SET | ABB_REF_POINT;
26ac77db
DW
267 break;
268
269 case wxHORIZONTAL_HATCH:
270 m_vAreaBundle.usSymbol = PATSYM_HORIZ;
271 m_vAreaBundle.usSet = LCID_DEFAULT;
d697657f
DW
272 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL;
273 flDefMask = ABB_SET | ABB_REF_POINT;
26ac77db
DW
274 break;
275
276 case wxVERTICAL_HATCH:
277 m_vAreaBundle.usSymbol = PATSYM_VERT;
278 m_vAreaBundle.usSet = LCID_DEFAULT;
d697657f
DW
279 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL;
280 flDefMask = ABB_SET | ABB_REF_POINT;
26ac77db
DW
281 break;
282
283 default:
284 m_vAreaBundle.usSymbol = PATSYM_SOLID;
285 m_vAreaBundle.usSet = LCID_DEFAULT;
d697657f
DW
286 flAttrMask = ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE | ABB_SYMBOL;
287 flDefMask = ABB_SET | ABB_REF_POINT;
26ac77db
DW
288 break;
289 }
290
291 m_vAreaBundle.lColor = vPmColour;
8d854fa9 292 m_vAreaBundle.lBackColor = RGB_WHITE;
26ac77db
DW
293 m_vAreaBundle.usMixMode = FM_OVERPAINT;
294 m_vAreaBundle.usBackMixMode = BM_OVERPAINT;
295
296 bOk = ::GpiSetAttrs( M_PENDATA->m_hPen
297 ,PRIM_AREA
d697657f
DW
298 ,flAttrMask
299 ,flDefMask
26ac77db
DW
300 ,&m_vAreaBundle
301 );
302 if (!bOk)
303 {
304 vError = ::WinGetLastError(vHabmain);
305 sError = wxPMErrorToStr(vError);
0fba44b4 306 wxLogError(_T("Can't set Gpi attributes for an AREABUNDLE. Error: %s\n"), sError.c_str());
26ac77db 307 }
6670f564
WS
308
309 return (bool)bOk;
26ac77db 310 }
6670f564 311 return false;
26ac77db 312} // end of wxPen::RealizeResource
cdf1e714
DW
313
314WXHANDLE wxPen::GetResourceHandle()
315{
26ac77db 316 if (!M_PENDATA)
cdf1e714
DW
317 return 0;
318 else
319 return (WXHANDLE)M_PENDATA->m_hPen;
26ac77db 320} // end of wxPen::GetResourceHandle
cdf1e714 321
6670f564 322bool wxPen::FreeResource( bool WXUNUSED(bForce) )
cdf1e714
DW
323{
324 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
325 {
cdf1e714 326 M_PENDATA->m_hPen = 0;
6670f564 327 return true;
cdf1e714 328 }
6670f564 329 else return false;
26ac77db 330} // end of wxPen::FreeResource
cdf1e714 331
e6ebb514
DW
332bool wxPen::IsFree() const
333{
26ac77db 334 return (M_PENDATA && M_PENDATA->m_hPen == 0);
e6ebb514
DW
335}
336
0e320a79
DW
337void wxPen::Unshare()
338{
cdf1e714
DW
339 // Don't change shared data
340 if (!m_refData)
0e320a79 341 {
cdf1e714
DW
342 m_refData = new wxPenRefData();
343 }
0e320a79
DW
344 else
345 {
cdf1e714
DW
346 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
347 UnRef();
348 m_refData = ref;
349 }
26ac77db 350} // end of wxPen::Unshare
0e320a79 351
46562151 352void wxPen::SetColour( const wxColour& rColour )
0e320a79
DW
353{
354 Unshare();
26ac77db 355 M_PENDATA->m_vColour = rColour;
0e320a79 356 RealizeResource();
26ac77db 357} // end of wxPen::SetColour
0e320a79 358
1a1498c0 359void wxPen::SetColour( unsigned char cRed, unsigned char cGreen, unsigned char cBlue)
0e320a79
DW
360{
361 Unshare();
26ac77db 362 M_PENDATA->m_vColour.Set(cRed, cGreen, cBlue);
0e320a79 363 RealizeResource();
26ac77db 364} // end of wxPen::SetColour
0e320a79 365
46562151 366void wxPen::SetPS( HPS hPS )
0e320a79
DW
367{
368 Unshare();
26ac77db
DW
369 if (M_PENDATA->m_hPen)
370 ::GpiDestroyPS(M_PENDATA->m_hPen);
371 M_PENDATA->m_hPen = hPS;
0e320a79 372 RealizeResource();
26ac77db 373} // end of WxWinGdi_CPen::SetPS
0e320a79 374
26ac77db
DW
375void wxPen::SetWidth(
376 int nWidth
377)
0e320a79
DW
378{
379 Unshare();
26ac77db 380 M_PENDATA->m_nWidth = nWidth;
0e320a79 381 RealizeResource();
26ac77db 382} // end of wxPen::SetWidth
0e320a79 383
26ac77db
DW
384void wxPen::SetStyle(
385 int nStyle
386)
0e320a79
DW
387{
388 Unshare();
26ac77db 389 M_PENDATA->m_nStyle = nStyle;
0e320a79 390 RealizeResource();
26ac77db 391} // end of wxPen::SetStyle
0e320a79 392
26ac77db
DW
393void wxPen::SetStipple(
394 const wxBitmap& rStipple
395)
0e320a79
DW
396{
397 Unshare();
26ac77db
DW
398 M_PENDATA->m_vStipple = rStipple;
399 M_PENDATA->m_nStyle = wxSTIPPLE;
0e320a79 400 RealizeResource();
26ac77db 401} // end of wxPen::SetStipple
0e320a79 402
6670f564
WS
403void wxPen::SetDashes( int WXUNUSED(nNbDashes),
404 const wxDash* WXUNUSED(pDash) )
26ac77db
DW
405{
406 //
407 // Does nothing under OS/2
408 //
409} // end of wxPen::SetDashes
410
411void wxPen::SetJoin(
412 int nJoin
413)
0e320a79
DW
414{
415 Unshare();
26ac77db 416 M_PENDATA->m_nJoin = nJoin;
0e320a79 417 RealizeResource();
26ac77db 418} // end of wxPen::SetJoin
0e320a79 419
26ac77db
DW
420void wxPen::SetCap(
421 int nCap
422)
0e320a79
DW
423{
424 Unshare();
26ac77db 425 M_PENDATA->m_nCap = nCap;
0e320a79 426 RealizeResource();
26ac77db 427} // end of wxPen::SetCap
0e320a79 428
26ac77db
DW
429int wx2os2PenStyle(
430 int nWxStyle
431)
cdf1e714 432{
26ac77db
DW
433 int nPMStyle = 0;
434
435 switch (nWxStyle)
fb46a9a6 436 {
26ac77db
DW
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