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