]> git.saurik.com Git - wxWidgets.git/blob - src/os2/pen.cpp
This is how wxPlotWindow would look like with the
[wxWidgets.git] / src / os2 / pen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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/setup.h"
18 #include "wx/list.h"
19 #include "wx/utils.h"
20 #include "wx/app.h"
21 #include "wx/pen.h"
22 #endif
23
24 #include "wx/os2/private.h"
25 #include "assert.h"
26
27 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
28
29 wxPenRefData::wxPenRefData()
30 {
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 )
41 {
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
49
50 wxPenRefData::~wxPenRefData()
51 {
52 } // end of wxPenRefData::~wxPenRefData
53
54 //
55 // Pens
56 //
57 wxPen::wxPen()
58 {
59 if ( wxThePenList )
60 wxThePenList->AddPen(this);
61 } // end of wxPen::wxPen
62
63 wxPen::~wxPen()
64 {
65 if (wxThePenList)
66 wxThePenList->RemovePen(this);
67 } // end of wxPen::wxPen
68
69 // Should implement Create
70 wxPen::wxPen(
71 const wxColour& rColour
72 , int nWidth
73 , int nStyle
74 )
75 {
76 m_refData = new wxPenRefData;
77
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
85 RealizeResource();
86
87 if ( wxThePenList )
88 wxThePenList->AddPen(this);
89 } // end of wxPen::wxPen
90
91 wxPen::wxPen(
92 const wxBitmap& rStipple
93 , int nWidth
94 )
95 {
96 m_refData = new wxPenRefData;
97
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;
104
105 RealizeResource();
106
107 if ( wxThePenList )
108 wxThePenList->AddPen(this);
109 } // end of wxPen::wxPen
110
111 bool wxPen::RealizeResource()
112 {
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
217 switch(M_PENDATA->m_nStyle)
218 {
219 case wxSTIPPLE:
220 ::GpiSetBitmapId( M_PENDATA->m_hPen
221 ,(USHORT)M_PENDATA->m_vStipple.GetHBITMAP()
222 ,(USHORT)M_PENDATA->m_vStipple.GetId()
223 );
224 ::GpiSetPatternSet( M_PENDATA->m_hPen
225 ,(USHORT)M_PENDATA->m_vStipple.GetId()
226 );
227 break;
228
229 case wxBDIAGONAL_HATCH:
230 m_vAreaBundle.usSymbol = PATSYM_DIAG3;
231 m_vAreaBundle.usSet = LCID_DEFAULT;
232 break;
233
234 case wxCROSSDIAG_HATCH:
235 m_vAreaBundle.usSymbol = PATSYM_DIAGHATCH;
236 m_vAreaBundle.usSet = LCID_DEFAULT;
237 break;
238
239 case wxFDIAGONAL_HATCH:
240 m_vAreaBundle.usSymbol = PATSYM_DIAG1;
241 m_vAreaBundle.usSet = LCID_DEFAULT;
242 break;
243
244 case wxCROSS_HATCH:
245 m_vAreaBundle.usSymbol = PATSYM_HATCH;
246 m_vAreaBundle.usSet = LCID_DEFAULT;
247 break;
248
249 case wxHORIZONTAL_HATCH:
250 m_vAreaBundle.usSymbol = PATSYM_HORIZ;
251 m_vAreaBundle.usSet = LCID_DEFAULT;
252 break;
253
254 case wxVERTICAL_HATCH:
255 m_vAreaBundle.usSymbol = PATSYM_VERT;
256 m_vAreaBundle.usSet = LCID_DEFAULT;
257 break;
258
259 default:
260 m_vAreaBundle.usSymbol = PATSYM_SOLID;
261 m_vAreaBundle.usSet = LCID_DEFAULT;
262 break;
263 }
264
265 m_vAreaBundle.lColor = vPmColour;
266 m_vAreaBundle.lBackColor = RGB_WHITE;
267 m_vAreaBundle.usMixMode = FM_OVERPAINT;
268 m_vAreaBundle.usBackMixMode = BM_OVERPAINT;
269
270 bOk = ::GpiSetAttrs( M_PENDATA->m_hPen
271 ,PRIM_AREA
272 ,ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE |
273 ABB_SET | ABB_SYMBOL
274 ,ABB_REF_POINT
275 ,&m_vAreaBundle
276 );
277 if (!bOk)
278 {
279 vError = ::WinGetLastError(vHabmain);
280 sError = wxPMErrorToStr(vError);
281 wxLogError("Can't set Gpi attributes for an AREABUNDLE. Error: %s\n", sError);
282 }
283 return bOk;
284 }
285 return FALSE;
286 } // end of wxPen::RealizeResource
287
288 WXHANDLE wxPen::GetResourceHandle()
289 {
290 if (!M_PENDATA)
291 return 0;
292 else
293 return (WXHANDLE)M_PENDATA->m_hPen;
294 } // end of wxPen::GetResourceHandle
295
296 bool wxPen::FreeResource(
297 bool bForce
298 )
299 {
300 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
301 {
302 M_PENDATA->m_hPen = 0;
303 return TRUE;
304 }
305 else return FALSE;
306 } // end of wxPen::FreeResource
307
308 bool wxPen::IsFree() const
309 {
310 return (M_PENDATA && M_PENDATA->m_hPen == 0);
311 }
312
313 void wxPen::Unshare()
314 {
315 // Don't change shared data
316 if (!m_refData)
317 {
318 m_refData = new wxPenRefData();
319 }
320 else
321 {
322 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
323 UnRef();
324 m_refData = ref;
325 }
326 } // end of wxPen::Unshare
327
328 void wxPen::SetColour(
329 const wxColour& rColour
330 )
331 {
332 Unshare();
333 M_PENDATA->m_vColour = rColour;
334 RealizeResource();
335 } // end of wxPen::SetColour
336
337 void wxPen::SetColour(
338 unsigned char cRed
339 , unsigned char cGreen
340 , unsigned char cBlue
341 )
342 {
343 Unshare();
344 M_PENDATA->m_vColour.Set(cRed, cGreen, cBlue);
345 RealizeResource();
346 } // end of wxPen::SetColour
347
348 void wxPen::SetPS(
349 HPS hPS
350 )
351 {
352 Unshare();
353 if (M_PENDATA->m_hPen)
354 ::GpiDestroyPS(M_PENDATA->m_hPen);
355 M_PENDATA->m_hPen = hPS;
356 RealizeResource();
357 } // end of WxWinGdi_CPen::SetPS
358
359 void wxPen::SetWidth(
360 int nWidth
361 )
362 {
363 Unshare();
364 M_PENDATA->m_nWidth = nWidth;
365 RealizeResource();
366 } // end of wxPen::SetWidth
367
368 void wxPen::SetStyle(
369 int nStyle
370 )
371 {
372 Unshare();
373 M_PENDATA->m_nStyle = nStyle;
374 RealizeResource();
375 } // end of wxPen::SetStyle
376
377 void wxPen::SetStipple(
378 const wxBitmap& rStipple
379 )
380 {
381 Unshare();
382 M_PENDATA->m_vStipple = rStipple;
383 M_PENDATA->m_nStyle = wxSTIPPLE;
384 RealizeResource();
385 } // end of wxPen::SetStipple
386
387 void wxPen::SetDashes(
388 int nNbDashes
389 , const wxDash* pDash
390 )
391 {
392 //
393 // Does nothing under OS/2
394 //
395 } // end of wxPen::SetDashes
396
397 void wxPen::SetJoin(
398 int nJoin
399 )
400 {
401 Unshare();
402 M_PENDATA->m_nJoin = nJoin;
403 RealizeResource();
404 } // end of wxPen::SetJoin
405
406 void wxPen::SetCap(
407 int nCap
408 )
409 {
410 Unshare();
411 M_PENDATA->m_nCap = nCap;
412 RealizeResource();
413 } // end of wxPen::SetCap
414
415 int wx2os2PenStyle(
416 int nWxStyle
417 )
418 {
419 int nPMStyle = 0;
420
421 switch (nWxStyle)
422 {
423 case wxDOT:
424 nPMStyle = LINETYPE_DOT;
425 break;
426
427 case wxDOT_DASH:
428 nPMStyle = LINETYPE_DASHDOT;
429 break;
430
431 case wxSHORT_DASH:
432 nPMStyle = LINETYPE_SHORTDASH;
433 break;
434
435 case wxLONG_DASH:
436 nPMStyle = LINETYPE_LONGDASH;
437 break;
438
439 case wxTRANSPARENT:
440 nPMStyle = LINETYPE_INVISIBLE;
441 break;
442
443 case wxUSER_DASH:
444 nPMStyle = LINETYPE_DASHDOUBLEDOT; // We must make a choice... This is mine!
445 break;
446
447 case wxSOLID:
448 default:
449 nPMStyle = LINETYPE_SOLID;
450 break;
451 }
452 return nPMStyle;
453 } // end of wx2os2PenStyle
454
455