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