]> git.saurik.com Git - wxWidgets.git/blob - src/msw/pen.cpp
build fixes after previous commit
[wxWidgets.git] / src / msw / pen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/pen.cpp
3 // Purpose: wxPen
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin: refactored wxPen code to wxPenRefData
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/pen.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/bitmap.h"
31 #include "wx/utils.h"
32 #endif
33
34 #include "wx/msw/private.h"
35
36 #define M_PENDATA ((wxPenRefData*)m_refData)
37
38 // Win32 has ExtCreatePen() but WinCE doesn't
39 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
40 #define wxHAVE_EXT_CREATE_PEN
41 #endif
42
43 // ----------------------------------------------------------------------------
44 // wxPenRefData: contains information about an HPEN and its handle
45 // ----------------------------------------------------------------------------
46
47 class WXDLLEXPORT wxPenRefData : public wxGDIRefData
48 {
49 public:
50 // ctors and dtor
51 // --------------
52
53 wxPenRefData();
54 wxPenRefData(const wxPenRefData& data);
55 wxPenRefData(const wxColour& col, int width, int style);
56 wxPenRefData(const wxBitmap& stipple, int width);
57 virtual ~wxPenRefData();
58
59 bool operator==(const wxPenRefData& data) const
60 {
61 // we intentionally don't compare m_hPen fields here
62 return m_style == data.m_style &&
63 m_width == data.m_width &&
64 m_join == data.m_join &&
65 m_cap == data.m_cap &&
66 m_colour == data.m_colour &&
67 (m_style != wxSTIPPLE || m_stipple.IsSameAs(data.m_stipple)) &&
68 (m_style != wxUSER_DASH ||
69 (m_nbDash == data.m_nbDash &&
70 memcmp(m_dash, data.m_dash, m_nbDash*sizeof(wxDash)) == 0));
71 }
72
73
74 // accessors and setters
75 // ---------------------
76
77 wxColour& GetColour() const { return wx_const_cast(wxColour&, m_colour); }
78 int GetWidth() const { return m_width; }
79 int GetStyle() const { return m_style; }
80 int GetJoin() const { return m_join; }
81 int GetCap() const { return m_cap; }
82 wxDash* GetDash() const { return m_dash; }
83 int GetDashCount() const { return m_nbDash; }
84 wxBitmap* GetStipple() const { return wx_const_cast(wxBitmap *, &m_stipple); }
85
86 void SetColour(const wxColour& col) { Free(); m_colour = col; }
87 void SetWidth(int width) { Free(); m_width = width; }
88 void SetStyle(int style) { Free(); m_style = style; }
89 void SetStipple(const wxBitmap& stipple)
90 {
91 Free();
92
93 m_style = wxSTIPPLE;
94 m_stipple = stipple;
95 }
96
97 void SetDashes(int nb_dashes, const wxDash *dash)
98 {
99 Free();
100
101 m_nbDash = nb_dashes;
102 m_dash = wx_const_cast(wxDash *, dash);
103 }
104
105 void SetJoin(int join) { Free(); m_join = join; }
106 void SetCap(int cap) { Free(); m_cap = cap; }
107
108
109 // HPEN management
110 // ---------------
111
112 // create the HPEN if we don't have it yet
113 bool Alloc();
114
115 // get the HPEN creating it on demand
116 WXHPEN GetHPEN() const;
117
118 // return true if we have a valid HPEN
119 bool HasHPEN() const { return m_hPen != 0; }
120
121 // return true if we had a valid handle before, false otherwise
122 bool Free();
123
124 private:
125 // initialize the fields which have reasonable default values
126 //
127 // doesn't initialize m_width and m_style which must be initialize in ctor
128 void Init()
129 {
130 m_join = wxJOIN_ROUND;
131 m_cap = wxCAP_ROUND;
132 m_nbDash = 0;
133 m_dash = NULL;
134 m_hPen = 0;
135 }
136
137 int m_width;
138 int m_style;
139 int m_join;
140 int m_cap;
141 wxBitmap m_stipple;
142 int m_nbDash;
143 wxDash * m_dash;
144 wxColour m_colour;
145 HPEN m_hPen;
146
147 DECLARE_NO_ASSIGN_CLASS(wxPenRefData)
148 };
149
150 // ============================================================================
151 // implementation
152 // ============================================================================
153
154 // ----------------------------------------------------------------------------
155 // wxPenRefData ctors/dtor
156 // ----------------------------------------------------------------------------
157
158 wxPenRefData::wxPenRefData()
159 {
160 Init();
161
162 m_style = wxSOLID;
163 m_width = 1;
164 }
165
166 wxPenRefData::wxPenRefData(const wxPenRefData& data)
167 :wxGDIRefData()
168 {
169 m_style = data.m_style;
170 m_width = data.m_width;
171 m_join = data.m_join;
172 m_cap = data.m_cap;
173 m_nbDash = data.m_nbDash;
174 m_dash = data.m_dash;
175 m_colour = data.m_colour;
176 m_hPen = 0;
177 }
178
179 wxPenRefData::wxPenRefData(const wxColour& col, int width, int style)
180 {
181 Init();
182
183 m_style = style;
184 m_width = width;
185
186 m_colour = col;
187 }
188
189 wxPenRefData::wxPenRefData(const wxBitmap& stipple, int width)
190 {
191 Init();
192
193 m_style = wxSTIPPLE;
194 m_width = width;
195
196 m_stipple = stipple;
197 }
198
199 wxPenRefData::~wxPenRefData()
200 {
201 if ( m_hPen )
202 ::DeleteObject(m_hPen);
203 }
204
205 // ----------------------------------------------------------------------------
206 // wxPenRefData HPEN management
207 // ----------------------------------------------------------------------------
208
209 #ifdef wxHAVE_EXT_CREATE_PEN
210
211 static int ConvertPenStyle(int style)
212 {
213 switch ( style )
214 {
215 case wxDOT:
216 return PS_DOT;
217
218 case wxDOT_DASH:
219 return PS_DASHDOT;
220
221 case wxSHORT_DASH:
222 case wxLONG_DASH:
223 return PS_DASH;
224
225 case wxTRANSPARENT:
226 return PS_NULL;
227
228 case wxUSER_DASH:
229 return PS_USERSTYLE;
230
231 default:
232 wxFAIL_MSG( _T("unknown pen style") );
233 // fall through
234
235 case wxSTIPPLE:
236 case wxBDIAGONAL_HATCH:
237 case wxCROSSDIAG_HATCH:
238 case wxFDIAGONAL_HATCH:
239 case wxCROSS_HATCH:
240 case wxHORIZONTAL_HATCH:
241 case wxVERTICAL_HATCH:
242 case wxSOLID:
243 return PS_SOLID;
244 }
245 }
246
247 static int ConvertJoinStyle(int join)
248 {
249 switch( join )
250 {
251 case wxJOIN_BEVEL:
252 return PS_JOIN_BEVEL;
253
254 case wxJOIN_MITER:
255 return PS_JOIN_MITER;
256
257 default:
258 wxFAIL_MSG( _T("unknown pen join style") );
259 // fall through
260
261 case wxJOIN_ROUND:
262 return PS_JOIN_ROUND;
263 }
264 }
265
266 static int ConvertCapStyle(int cap)
267 {
268 switch ( cap )
269 {
270 case wxCAP_PROJECTING:
271 return PS_ENDCAP_SQUARE;
272
273 case wxCAP_BUTT:
274 return PS_ENDCAP_FLAT;
275
276 default:
277 wxFAIL_MSG( _T("unknown pen cap style") );
278 // fall through
279
280 case wxCAP_ROUND:
281 return PS_ENDCAP_ROUND;
282 }
283 }
284
285 #endif // wxHAVE_EXT_CREATE_PEN
286
287 bool wxPenRefData::Alloc()
288 {
289 if ( m_hPen )
290 return false;
291
292 if ( m_style == wxTRANSPARENT )
293 {
294 m_hPen = (HPEN)::GetStockObject(NULL_PEN);
295 return true;
296 }
297
298 const COLORREF col = m_colour.GetPixel();
299
300 #ifdef wxHAVE_EXT_CREATE_PEN
301 // Only NT can display dashed or dotted lines with width > 1
302 static const int os = wxGetOsVersion();
303 if ( os != wxOS_WINDOWS_NT &&
304 (m_style == wxDOT ||
305 m_style == wxLONG_DASH ||
306 m_style == wxSHORT_DASH ||
307 m_style == wxDOT_DASH ||
308 m_style == wxUSER_DASH) &&
309 m_width > 1 )
310 {
311 m_width = 1;
312 }
313
314 // check if it's a standard kind of pen which can be created with just
315 // CreatePen()
316 if ( m_join == wxJOIN_ROUND &&
317 m_cap == wxCAP_ROUND &&
318 m_style != wxUSER_DASH &&
319 m_style != wxSTIPPLE &&
320 (m_width <= 1 || m_style == wxSOLID) )
321 #endif // !wxHAVE_EXT_CREATE_PEN
322 {
323 m_hPen = ::CreatePen(ConvertPenStyle(m_style), m_width, col);
324 }
325 #ifdef wxHAVE_EXT_CREATE_PEN
326 else // need to use ExtCreatePen()
327 {
328 DWORD styleMSW = PS_GEOMETRIC |
329 ConvertPenStyle(m_style) |
330 ConvertJoinStyle(m_join) |
331 ConvertCapStyle(m_cap);
332
333 LOGBRUSH lb;
334 switch( m_style )
335 {
336 case wxSTIPPLE:
337 lb.lbStyle = BS_PATTERN;
338 lb.lbHatch = (LONG)m_stipple.GetHBITMAP();
339 break;
340
341 case wxBDIAGONAL_HATCH:
342 lb.lbStyle = BS_HATCHED;
343 lb.lbHatch = HS_BDIAGONAL;
344 break;
345
346 case wxCROSSDIAG_HATCH:
347 lb.lbStyle = BS_HATCHED;
348 lb.lbHatch = HS_DIAGCROSS;
349 break;
350
351 case wxFDIAGONAL_HATCH:
352 lb.lbStyle = BS_HATCHED;
353 lb.lbHatch = HS_FDIAGONAL;
354 break;
355
356 case wxCROSS_HATCH:
357 lb.lbStyle = BS_HATCHED;
358 lb.lbHatch = HS_CROSS;
359 break;
360
361 case wxHORIZONTAL_HATCH:
362 lb.lbStyle = BS_HATCHED;
363 lb.lbHatch = HS_HORIZONTAL;
364 break;
365
366 case wxVERTICAL_HATCH:
367 lb.lbStyle = BS_HATCHED;
368 lb.lbHatch = HS_VERTICAL;
369 break;
370
371 default:
372 lb.lbStyle = BS_SOLID;
373 #ifdef __WXDEBUG__
374 // this should be unnecessary (it's unused) but suppresses the
375 // Purify messages about uninitialized memory read
376 lb.lbHatch = 0;
377 #endif
378 break;
379 }
380
381 lb.lbColor = col;
382
383 DWORD *dash;
384 if ( m_style == wxUSER_DASH && m_nbDash && m_dash )
385 {
386 dash = new DWORD[m_nbDash];
387 int rw = m_width > 1 ? m_width : 1;
388 for ( int i = 0; i < m_nbDash; i++ )
389 dash[i] = m_dash[i] * rw;
390 }
391 else
392 {
393 dash = NULL;
394 }
395
396 m_hPen = ::ExtCreatePen(styleMSW, m_width, &lb, m_nbDash, (LPDWORD)dash);
397
398 delete [] dash;
399 }
400 #endif // wxHAVE_EXT_CREATE_PEN
401
402 return m_hPen != 0;
403 }
404
405 bool wxPenRefData::Free()
406 {
407 if ( !m_hPen )
408 return false;
409
410 ::DeleteObject(m_hPen);
411 m_hPen = 0;
412
413 return true;
414 }
415
416 WXHPEN wxPenRefData::GetHPEN() const
417 {
418 if ( !m_hPen )
419 wx_const_cast(wxPenRefData *, this)->Alloc();
420
421 return (WXHPEN)m_hPen;
422 }
423
424 // ----------------------------------------------------------------------------
425 // wxPen
426 // ----------------------------------------------------------------------------
427
428 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
429
430 wxPen::wxPen(const wxColour& col, int width, int style)
431 {
432 m_refData = new wxPenRefData(col, width, style);
433 }
434
435 wxPen::wxPen(const wxBitmap& stipple, int width)
436 {
437 m_refData = new wxPenRefData(stipple, width);
438 }
439
440 bool wxPen::operator==(const wxPen& pen) const
441 {
442 const wxPenRefData *
443 penData = wx_static_cast(const wxPenRefData *, pen.m_refData);
444
445 // an invalid pen is only equal to another invalid pen
446 return m_refData ? penData && *M_PENDATA == *penData : !penData;
447 }
448
449 bool wxPen::RealizeResource()
450 {
451 return M_PENDATA && M_PENDATA->Alloc();
452 }
453
454 WXHANDLE wxPen::GetResourceHandle() const
455 {
456 return M_PENDATA ? M_PENDATA->GetHPEN() : 0;
457 }
458
459 bool wxPen::FreeResource(bool WXUNUSED(force))
460 {
461 return M_PENDATA && M_PENDATA->Free();
462 }
463
464 bool wxPen::IsFree() const
465 {
466 return M_PENDATA && !M_PENDATA->HasHPEN();
467 }
468
469 wxObjectRefData* wxPen::CreateRefData() const
470 {
471 return new wxPenRefData;
472 }
473
474 wxObjectRefData* wxPen::CloneRefData(const wxObjectRefData* data) const
475 {
476 return new wxPenRefData(*wx_static_cast(const wxPenRefData*, data));
477 }
478
479 void wxPen::SetColour(const wxColour& col)
480 {
481 AllocExclusive();
482
483 M_PENDATA->SetColour(col);
484 }
485
486 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
487 {
488 SetColour(wxColour(r, g, b));
489 }
490
491 void wxPen::SetWidth(int width)
492 {
493 AllocExclusive();
494
495 M_PENDATA->SetWidth(width);
496 }
497
498 void wxPen::SetStyle(int style)
499 {
500 AllocExclusive();
501
502 M_PENDATA->SetStyle(style);
503 }
504
505 void wxPen::SetStipple(const wxBitmap& stipple)
506 {
507 AllocExclusive();
508
509 M_PENDATA->SetStipple(stipple);
510 }
511
512 void wxPen::SetDashes(int nb_dashes, const wxDash *dash)
513 {
514 AllocExclusive();
515
516 M_PENDATA->SetDashes(nb_dashes, dash);
517 }
518
519 void wxPen::SetJoin(int join)
520 {
521 AllocExclusive();
522
523 M_PENDATA->SetJoin(join);
524 }
525
526 void wxPen::SetCap(int cap)
527 {
528 AllocExclusive();
529
530 M_PENDATA->SetCap(cap);
531 }
532
533 wxColour& wxPen::GetColour() const
534 {
535 return m_refData ? M_PENDATA->GetColour() : wxNullColour;
536 }
537
538 int wxPen::GetWidth() const
539 {
540 return m_refData ? M_PENDATA->GetWidth() : 0;
541 }
542
543 int wxPen::GetStyle() const
544 {
545 return m_refData ? M_PENDATA->GetStyle() : 0;
546 }
547
548 int wxPen::GetJoin() const
549 {
550 return m_refData ? M_PENDATA->GetJoin() : 0;
551 }
552
553 int wxPen::GetCap() const
554 {
555 return m_refData ? M_PENDATA->GetCap() : 0;
556 }
557
558 int wxPen::GetDashes(wxDash** ptr) const
559 {
560 if ( !m_refData )
561 {
562 *ptr = NULL;
563 return 0;
564 }
565
566 *ptr = M_PENDATA->GetDash();
567 return M_PENDATA->GetDashCount();
568 }
569
570 wxDash* wxPen::GetDash() const
571 {
572 return m_refData ? M_PENDATA->GetDash() : NULL;
573 }
574
575 int wxPen::GetDashCount() const
576 {
577 return m_refData ? M_PENDATA->GetDashCount() : 0;
578 }
579
580 wxBitmap* wxPen::GetStipple() const
581 {
582 return m_refData ? M_PENDATA->GetStipple() : NULL;
583 }