don't use wxTheXXXList in wxXXX ctor/dtor, only objects explicitly created
[wxWidgets.git] / src / msw / pen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/pen.cpp
3 // Purpose: wxPen
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "pen.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/list.h"
27 #include "wx/utils.h"
28 #include "wx/app.h"
29 #include "wx/pen.h"
30 #endif
31
32 #include "wx/msw/private.h"
33 #include "assert.h"
34
35 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
36
37 wxPenRefData::wxPenRefData()
38 {
39 m_style = wxSOLID;
40 m_width = 1;
41 m_join = wxJOIN_ROUND ;
42 m_cap = wxCAP_ROUND ;
43 m_nbDash = 0 ;
44 m_dash = (wxDash*)NULL;
45 m_hPen = 0;
46 }
47
48 wxPenRefData::wxPenRefData(const wxPenRefData& data)
49 {
50 m_style = data.m_style;
51 m_width = data.m_width;
52 m_join = data.m_join;
53 m_cap = data.m_cap;
54 m_nbDash = data.m_nbDash;
55 m_dash = data.m_dash;
56 m_colour = data.m_colour;
57 m_hPen = 0;
58 }
59
60 wxPenRefData::~wxPenRefData()
61 {
62 if ( m_hPen )
63 ::DeleteObject((HPEN) m_hPen);
64 }
65
66 // Pens
67
68 wxPen::wxPen()
69 {
70 }
71
72 wxPen::~wxPen()
73 {
74 }
75
76 // Should implement Create
77 wxPen::wxPen(const wxColour& col, int Width, int Style)
78 {
79 m_refData = new wxPenRefData;
80
81 M_PENDATA->m_colour = col;
82 // M_PENDATA->m_stipple = NULL;
83 M_PENDATA->m_width = Width;
84 M_PENDATA->m_style = Style;
85 M_PENDATA->m_join = wxJOIN_ROUND ;
86 M_PENDATA->m_cap = wxCAP_ROUND ;
87 M_PENDATA->m_nbDash = 0 ;
88 M_PENDATA->m_dash = (wxDash*)NULL;
89 M_PENDATA->m_hPen = 0 ;
90
91 #ifndef __WIN32__
92 // In Windows, only a pen of width = 1 can be dotted or dashed!
93 if ((Style == wxDOT) || (Style == wxLONG_DASH) ||
94 (Style == wxSHORT_DASH) || (Style == wxDOT_DASH) ||
95 (Style == wxUSER_DASH))
96 M_PENDATA->m_width = 1;
97 #else
98 /***
99 DWORD vers = GetVersion() ;
100 WORD high = HIWORD(vers) ; // high bit=0 for NT, 1 for Win32s
101 // Win32s doesn't support wide dashed pens
102
103 if ((high&0x8000)!=0)
104 ***/
105 if (wxGetOsVersion()==wxWIN32S)
106 {
107 // In Windows, only a pen of width = 1 can be dotted or dashed!
108 if ((Style == wxDOT) || (Style == wxLONG_DASH) ||
109 (Style == wxSHORT_DASH) || (Style == wxDOT_DASH) ||
110 (Style == wxUSER_DASH))
111 M_PENDATA->m_width = 1;
112 }
113 #endif
114 RealizeResource();
115
116 }
117
118 wxPen::wxPen(const wxBitmap& stipple, int Width)
119 {
120 m_refData = new wxPenRefData;
121
122 // M_PENDATA->m_colour = col;
123 M_PENDATA->m_stipple = stipple;
124 M_PENDATA->m_width = Width;
125 M_PENDATA->m_style = wxSTIPPLE;
126 M_PENDATA->m_join = wxJOIN_ROUND ;
127 M_PENDATA->m_cap = wxCAP_ROUND ;
128 M_PENDATA->m_nbDash = 0 ;
129 M_PENDATA->m_dash = (wxDash*)NULL;
130 M_PENDATA->m_hPen = 0 ;
131
132 RealizeResource();
133
134 }
135
136 bool wxPen::RealizeResource()
137 {
138 if (M_PENDATA && (M_PENDATA->m_hPen == 0))
139 {
140 if (M_PENDATA->m_style==wxTRANSPARENT)
141 {
142 M_PENDATA->m_hPen = (WXHPEN) ::GetStockObject(NULL_PEN);
143 return TRUE;
144 }
145
146 COLORREF ms_colour = 0;
147 ms_colour = M_PENDATA->m_colour.GetPixel();
148
149 // Join style, Cap style, Pen Stippling only on Win32.
150 // Currently no time to find equivalent on Win3.1, sorry
151 // [if such equiv exist!!]
152 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
153 if (M_PENDATA->m_join==wxJOIN_ROUND &&
154 M_PENDATA->m_cap==wxCAP_ROUND &&
155 M_PENDATA->m_style!=wxUSER_DASH &&
156 M_PENDATA->m_style!=wxSTIPPLE &&
157 M_PENDATA->m_width <= 1)
158 {
159 M_PENDATA->m_hPen =
160 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
161 M_PENDATA->m_width,
162 ms_colour );
163 }
164 else
165 {
166 DWORD ms_style = PS_GEOMETRIC | wx2msPenStyle(M_PENDATA->m_style);
167
168 switch(M_PENDATA->m_join)
169 {
170 case wxJOIN_BEVEL: ms_style |= PS_JOIN_BEVEL; break;
171 case wxJOIN_MITER: ms_style |= PS_JOIN_MITER; break;
172 default:
173 case wxJOIN_ROUND: ms_style |= PS_JOIN_ROUND; break;
174 }
175
176 switch(M_PENDATA->m_cap)
177 {
178 case wxCAP_PROJECTING: ms_style |= PS_ENDCAP_SQUARE; break;
179 case wxCAP_BUTT: ms_style |= PS_ENDCAP_FLAT; break;
180 default:
181 case wxCAP_ROUND: ms_style |= PS_ENDCAP_ROUND; break;
182 }
183
184 LOGBRUSH logb;
185
186 switch(M_PENDATA->m_style)
187 {
188 case wxSTIPPLE:
189 logb.lbStyle = BS_PATTERN ;
190 if (M_PENDATA->m_stipple.Ok())
191 logb.lbHatch = (LONG)M_PENDATA->m_stipple.GetHBITMAP();
192 else
193 logb.lbHatch = (LONG)0;
194 break;
195 case wxBDIAGONAL_HATCH:
196 logb.lbStyle = BS_HATCHED;
197 logb.lbHatch = HS_BDIAGONAL;
198 break;
199 case wxCROSSDIAG_HATCH:
200 logb.lbStyle = BS_HATCHED;
201 logb.lbHatch = HS_DIAGCROSS;
202 break;
203 case wxFDIAGONAL_HATCH:
204 logb.lbStyle = BS_HATCHED;
205 logb.lbHatch = HS_FDIAGONAL;
206 break;
207 case wxCROSS_HATCH:
208 logb.lbStyle = BS_HATCHED;
209 logb.lbHatch = HS_CROSS;
210 break;
211 case wxHORIZONTAL_HATCH:
212 logb.lbStyle = BS_HATCHED;
213 logb.lbHatch = HS_HORIZONTAL;
214 break;
215 case wxVERTICAL_HATCH:
216 logb.lbStyle = BS_HATCHED;
217 logb.lbHatch = HS_VERTICAL;
218 break;
219 default:
220 logb.lbStyle = BS_SOLID;
221 #ifdef __WXDEBUG__
222 // this should be unnecessary (it's unused) but suppresses the Purigy
223 // messages about uninitialized memory read
224 logb.lbHatch = 0;
225 #endif
226 break;
227 }
228
229 logb.lbColor = ms_colour;
230
231 wxMSWDash *real_dash;
232 if (M_PENDATA->m_style==wxUSER_DASH && M_PENDATA->m_nbDash && M_PENDATA->m_dash)
233 {
234 real_dash = new wxMSWDash[M_PENDATA->m_nbDash];
235 for ( int i = 0; i < M_PENDATA->m_nbDash; i++ )
236 real_dash[i] = M_PENDATA->m_dash[i] * M_PENDATA->m_width;
237 }
238 else
239 {
240 real_dash = (wxMSWDash*)NULL;
241 }
242
243 // Win32s doesn't have ExtCreatePen function...
244 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
245 {
246 M_PENDATA->m_hPen =
247 (WXHPEN) ExtCreatePen( ms_style,
248 M_PENDATA->m_width,
249 &logb,
250 M_PENDATA->m_style == wxUSER_DASH
251 ? M_PENDATA->m_nbDash
252 : 0,
253 (LPDWORD)real_dash );
254 }
255 else
256 {
257 M_PENDATA->m_hPen =
258 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
259 M_PENDATA->m_width,
260 ms_colour );
261 }
262
263 if (real_dash)
264 delete [] real_dash;
265 }
266 #else
267 M_PENDATA->m_hPen =
268 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
269 M_PENDATA->m_width,
270 ms_colour );
271 #endif
272 #ifdef WXDEBUG_CREATE
273 if (M_PENDATA->m_hPen==0)
274 wxError("Cannot create pen","Internal error") ;
275 #endif
276 return TRUE;
277 }
278 return FALSE;
279 }
280
281 WXHANDLE wxPen::GetResourceHandle()
282 {
283 if ( !M_PENDATA )
284 return 0;
285 else
286 return (WXHANDLE)M_PENDATA->m_hPen;
287 }
288
289 bool wxPen::FreeResource(bool WXUNUSED(force))
290 {
291 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
292 {
293 DeleteObject((HPEN) M_PENDATA->m_hPen);
294 M_PENDATA->m_hPen = 0;
295 return TRUE;
296 }
297 else return FALSE;
298 }
299
300 bool wxPen::IsFree() const
301 {
302 return (M_PENDATA && M_PENDATA->m_hPen == 0);
303 }
304
305 void wxPen::Unshare()
306 {
307 // Don't change shared data
308 if (!m_refData)
309 {
310 m_refData = new wxPenRefData();
311 }
312 else
313 {
314 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
315 UnRef();
316 m_refData = ref;
317 }
318 }
319
320 void wxPen::SetColour(const wxColour& col)
321 {
322 Unshare();
323
324 M_PENDATA->m_colour = col;
325
326 RealizeResource();
327 }
328
329 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
330 {
331 Unshare();
332
333 M_PENDATA->m_colour.Set(r, g, b);
334
335 RealizeResource();
336 }
337
338 void wxPen::SetWidth(int Width)
339 {
340 Unshare();
341
342 M_PENDATA->m_width = Width;
343
344 RealizeResource();
345 }
346
347 void wxPen::SetStyle(int Style)
348 {
349 Unshare();
350
351 M_PENDATA->m_style = Style;
352
353 RealizeResource();
354 }
355
356 void wxPen::SetStipple(const wxBitmap& Stipple)
357 {
358 Unshare();
359
360 M_PENDATA->m_stipple = Stipple;
361 M_PENDATA->m_style = wxSTIPPLE;
362
363 RealizeResource();
364 }
365
366 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
367 {
368 Unshare();
369
370 M_PENDATA->m_nbDash = nb_dashes;
371 M_PENDATA->m_dash = (wxDash *)Dash;
372
373 RealizeResource();
374 }
375
376 void wxPen::SetJoin(int Join)
377 {
378 Unshare();
379
380 M_PENDATA->m_join = Join;
381
382 RealizeResource();
383 }
384
385 void wxPen::SetCap(int Cap)
386 {
387 Unshare();
388
389 M_PENDATA->m_cap = Cap;
390
391 RealizeResource();
392 }
393
394 int wx2msPenStyle(int wx_style)
395 {
396 int cstyle;
397 switch (wx_style)
398 {
399 #if !defined(__WXMICROWIN__)
400 case wxDOT:
401 cstyle = PS_DOT;
402 break;
403
404 case wxDOT_DASH:
405 cstyle = PS_DASHDOT;
406 break;
407
408 case wxSHORT_DASH:
409 case wxLONG_DASH:
410 cstyle = PS_DASH;
411 break;
412
413 case wxTRANSPARENT:
414 cstyle = PS_NULL;
415 break;
416 #endif
417
418 case wxUSER_DASH:
419 #if !defined(__WXMICROWIN__)
420 #ifdef __WIN32__
421 // Win32s doesn't have PS_USERSTYLE
422 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
423 cstyle = PS_USERSTYLE;
424 else
425 cstyle = PS_DOT; // We must make a choice... This is mine!
426 #else
427 cstyle = PS_DASH;
428 #endif
429 #endif
430 break;
431 case wxSOLID:
432 default:
433 cstyle = PS_SOLID;
434 break;
435 }
436 return cstyle;
437 }
438