]> git.saurik.com Git - wxWidgets.git/blob - src/msw/pen.cpp
Fix some bugs
[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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
34 static int wx2msPenStyle(int wx_style);
35
36 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
37
38 wxPenRefData::wxPenRefData()
39 {
40 m_style = wxSOLID;
41 m_width = 1;
42 m_join = wxJOIN_ROUND ;
43 m_cap = wxCAP_ROUND ;
44 m_nbDash = 0 ;
45 m_dash = (wxDash*)NULL;
46 m_hPen = 0;
47 }
48
49 wxPenRefData::wxPenRefData(const wxPenRefData& data)
50 {
51 m_style = data.m_style;
52 m_width = data.m_width;
53 m_join = data.m_join;
54 m_cap = data.m_cap;
55 m_nbDash = data.m_nbDash;
56 m_dash = data.m_dash;
57 m_colour = data.m_colour;
58 m_hPen = 0;
59 }
60
61 wxPenRefData::~wxPenRefData()
62 {
63 if ( m_hPen )
64 ::DeleteObject((HPEN) m_hPen);
65 }
66
67 // Pens
68
69 wxPen::wxPen()
70 {
71 }
72
73 wxPen::~wxPen()
74 {
75 }
76
77 // Should implement Create
78 wxPen::wxPen(const wxColour& col, int Width, int Style)
79 {
80 m_refData = new wxPenRefData;
81
82 M_PENDATA->m_colour = col;
83 // M_PENDATA->m_stipple = NULL;
84 M_PENDATA->m_width = Width;
85 M_PENDATA->m_style = Style;
86 M_PENDATA->m_join = wxJOIN_ROUND ;
87 M_PENDATA->m_cap = wxCAP_ROUND ;
88 M_PENDATA->m_nbDash = 0 ;
89 M_PENDATA->m_dash = (wxDash*)NULL;
90 M_PENDATA->m_hPen = 0 ;
91
92 RealizeResource();
93 }
94
95 wxPen::wxPen(const wxBitmap& stipple, int Width)
96 {
97 m_refData = new wxPenRefData;
98
99 // M_PENDATA->m_colour = col;
100 M_PENDATA->m_stipple = stipple;
101 M_PENDATA->m_width = Width;
102 M_PENDATA->m_style = wxSTIPPLE;
103 M_PENDATA->m_join = wxJOIN_ROUND ;
104 M_PENDATA->m_cap = wxCAP_ROUND ;
105 M_PENDATA->m_nbDash = 0 ;
106 M_PENDATA->m_dash = (wxDash*)NULL;
107 M_PENDATA->m_hPen = 0 ;
108
109 RealizeResource();
110 }
111
112 bool wxPen::RealizeResource()
113 {
114 if ( !M_PENDATA || M_PENDATA->m_hPen )
115 return false;
116
117 if (M_PENDATA->m_style==wxTRANSPARENT)
118 {
119 M_PENDATA->m_hPen = (WXHPEN) ::GetStockObject(NULL_PEN);
120 return true;
121 }
122
123 static const int os = wxGetOsVersion();
124 COLORREF ms_colour = M_PENDATA->m_colour.GetPixel();
125
126 // Join style, Cap style, Pen Stippling
127 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
128 // Only NT can display dashed or dotted lines with width > 1
129 if ( os != wxWINDOWS_NT &&
130 (M_PENDATA->m_style & (wxDOT |
131 wxLONG_DASH |
132 wxSHORT_DASH |
133 wxDOT_DASH |
134 wxUSER_DASH)) &&
135 M_PENDATA->m_width > 1 )
136 {
137 M_PENDATA->m_width = 1;
138 }
139
140 if (M_PENDATA->m_join==wxJOIN_ROUND &&
141 M_PENDATA->m_cap==wxCAP_ROUND &&
142 M_PENDATA->m_style!=wxUSER_DASH &&
143 M_PENDATA->m_style!=wxSTIPPLE &&
144 M_PENDATA->m_width <= 1)
145 {
146 M_PENDATA->m_hPen =
147 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
148 M_PENDATA->m_width,
149 ms_colour );
150 }
151 else
152 {
153 DWORD ms_style = PS_GEOMETRIC | wx2msPenStyle(M_PENDATA->m_style);
154
155 switch(M_PENDATA->m_join)
156 {
157 case wxJOIN_BEVEL: ms_style |= PS_JOIN_BEVEL; break;
158 case wxJOIN_MITER: ms_style |= PS_JOIN_MITER; break;
159 default:
160 case wxJOIN_ROUND: ms_style |= PS_JOIN_ROUND; break;
161 }
162
163 switch(M_PENDATA->m_cap)
164 {
165 case wxCAP_PROJECTING: ms_style |= PS_ENDCAP_SQUARE; break;
166 case wxCAP_BUTT: ms_style |= PS_ENDCAP_FLAT; break;
167 default:
168 case wxCAP_ROUND: ms_style |= PS_ENDCAP_ROUND; break;
169 }
170
171 LOGBRUSH logb;
172
173 switch(M_PENDATA->m_style)
174 {
175 case wxSTIPPLE:
176 logb.lbStyle = BS_PATTERN ;
177 if (M_PENDATA->m_stipple.Ok())
178 logb.lbHatch = (LONG)M_PENDATA->m_stipple.GetHBITMAP();
179 else
180 logb.lbHatch = (LONG)0;
181 break;
182 case wxBDIAGONAL_HATCH:
183 logb.lbStyle = BS_HATCHED;
184 logb.lbHatch = HS_BDIAGONAL;
185 break;
186 case wxCROSSDIAG_HATCH:
187 logb.lbStyle = BS_HATCHED;
188 logb.lbHatch = HS_DIAGCROSS;
189 break;
190 case wxFDIAGONAL_HATCH:
191 logb.lbStyle = BS_HATCHED;
192 logb.lbHatch = HS_FDIAGONAL;
193 break;
194 case wxCROSS_HATCH:
195 logb.lbStyle = BS_HATCHED;
196 logb.lbHatch = HS_CROSS;
197 break;
198 case wxHORIZONTAL_HATCH:
199 logb.lbStyle = BS_HATCHED;
200 logb.lbHatch = HS_HORIZONTAL;
201 break;
202 case wxVERTICAL_HATCH:
203 logb.lbStyle = BS_HATCHED;
204 logb.lbHatch = HS_VERTICAL;
205 break;
206 default:
207 logb.lbStyle = BS_SOLID;
208 #ifdef __WXDEBUG__
209 // this should be unnecessary (it's unused) but suppresses the Purify
210 // messages about uninitialized memory read
211 logb.lbHatch = 0;
212 #endif
213 break;
214 }
215
216 logb.lbColor = ms_colour;
217
218 wxMSWDash *real_dash;
219 if (M_PENDATA->m_style==wxUSER_DASH && M_PENDATA->m_nbDash && M_PENDATA->m_dash)
220 {
221 real_dash = new wxMSWDash[M_PENDATA->m_nbDash];
222 int rw = M_PENDATA->m_width > 1 ? M_PENDATA->m_width : 1;
223 for ( int i = 0; i < M_PENDATA->m_nbDash; i++ )
224 real_dash[i] = M_PENDATA->m_dash[i] * rw;
225 }
226 else
227 {
228 real_dash = (wxMSWDash*)NULL;
229 }
230
231 // Win32s doesn't have ExtCreatePen function...
232 if (os==wxWINDOWS_NT || os==wxWIN95)
233 {
234 M_PENDATA->m_hPen =
235 (WXHPEN) ExtCreatePen( ms_style,
236 M_PENDATA->m_width,
237 &logb,
238 M_PENDATA->m_style == wxUSER_DASH
239 ? M_PENDATA->m_nbDash
240 : 0,
241 (LPDWORD)real_dash );
242 }
243 else
244 {
245 M_PENDATA->m_hPen =
246 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
247 M_PENDATA->m_width,
248 ms_colour );
249 }
250
251 delete [] real_dash;
252 }
253 #else // WinCE
254 M_PENDATA->m_hPen =
255 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
256 M_PENDATA->m_width,
257 ms_colour );
258 #endif // !WinCE/WinCE
259
260 return true;
261 }
262
263 WXHANDLE wxPen::GetResourceHandle() const
264 {
265 if ( !M_PENDATA )
266 return 0;
267 else
268 return (WXHANDLE)M_PENDATA->m_hPen;
269 }
270
271 bool wxPen::FreeResource(bool WXUNUSED(force))
272 {
273 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
274 {
275 DeleteObject((HPEN) M_PENDATA->m_hPen);
276 M_PENDATA->m_hPen = 0;
277 return true;
278 }
279 else return false;
280 }
281
282 bool wxPen::IsFree() const
283 {
284 return (M_PENDATA && M_PENDATA->m_hPen == 0);
285 }
286
287 void wxPen::Unshare()
288 {
289 // Don't change shared data
290 if (!m_refData)
291 {
292 m_refData = new wxPenRefData();
293 }
294 else
295 {
296 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
297 UnRef();
298 m_refData = ref;
299 }
300 }
301
302 void wxPen::SetColour(const wxColour& col)
303 {
304 Unshare();
305
306 M_PENDATA->m_colour = col;
307
308 RealizeResource();
309 }
310
311 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
312 {
313 Unshare();
314
315 M_PENDATA->m_colour.Set(r, g, b);
316
317 RealizeResource();
318 }
319
320 void wxPen::SetWidth(int Width)
321 {
322 Unshare();
323
324 M_PENDATA->m_width = Width;
325
326 RealizeResource();
327 }
328
329 void wxPen::SetStyle(int Style)
330 {
331 Unshare();
332
333 M_PENDATA->m_style = Style;
334
335 RealizeResource();
336 }
337
338 void wxPen::SetStipple(const wxBitmap& Stipple)
339 {
340 Unshare();
341
342 M_PENDATA->m_stipple = Stipple;
343 M_PENDATA->m_style = wxSTIPPLE;
344
345 RealizeResource();
346 }
347
348 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
349 {
350 Unshare();
351
352 M_PENDATA->m_nbDash = nb_dashes;
353 M_PENDATA->m_dash = (wxDash *)Dash;
354
355 RealizeResource();
356 }
357
358 void wxPen::SetJoin(int Join)
359 {
360 Unshare();
361
362 M_PENDATA->m_join = Join;
363
364 RealizeResource();
365 }
366
367 void wxPen::SetCap(int Cap)
368 {
369 Unshare();
370
371 M_PENDATA->m_cap = Cap;
372
373 RealizeResource();
374 }
375
376 int wx2msPenStyle(int wx_style)
377 {
378 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
379 switch (wx_style)
380 {
381 case wxDOT:
382 return PS_DOT;
383
384 case wxDOT_DASH:
385 return PS_DASHDOT;
386
387 case wxSHORT_DASH:
388 case wxLONG_DASH:
389 return PS_DASH;
390
391 case wxTRANSPARENT:
392 return PS_NULL;
393
394 case wxUSER_DASH:
395 // if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
396 return PS_USERSTYLE;
397 }
398 #else
399 wxUnusedVar(wx_style);
400 #endif
401 return PS_SOLID;
402 }
403