]> git.saurik.com Git - wxWidgets.git/blob - src/msw/pen.cpp
fixed GetWeekOfYear() for first/last week in some cases (patch 908793)
[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 #ifndef __WIN32__
93 // In Windows, only a pen of width = 1 can be dotted or dashed!
94 if ((Style == wxDOT) || (Style == wxLONG_DASH) ||
95 (Style == wxSHORT_DASH) || (Style == wxDOT_DASH) ||
96 (Style == wxUSER_DASH))
97 M_PENDATA->m_width = 1;
98 #else
99 /***
100 DWORD vers = GetVersion() ;
101 WORD high = HIWORD(vers) ; // high bit=0 for NT, 1 for Win32s
102 // Win32s doesn't support wide dashed pens
103
104 if ((high&0x8000)!=0)
105 ***/
106 if (wxGetOsVersion()==wxWIN32S)
107 {
108 // In Windows, only a pen of width = 1 can be dotted or dashed!
109 if ((Style == wxDOT) || (Style == wxLONG_DASH) ||
110 (Style == wxSHORT_DASH) || (Style == wxDOT_DASH) ||
111 (Style == wxUSER_DASH))
112 M_PENDATA->m_width = 1;
113 }
114 #endif
115 RealizeResource();
116
117 }
118
119 wxPen::wxPen(const wxBitmap& stipple, int Width)
120 {
121 m_refData = new wxPenRefData;
122
123 // M_PENDATA->m_colour = col;
124 M_PENDATA->m_stipple = stipple;
125 M_PENDATA->m_width = Width;
126 M_PENDATA->m_style = wxSTIPPLE;
127 M_PENDATA->m_join = wxJOIN_ROUND ;
128 M_PENDATA->m_cap = wxCAP_ROUND ;
129 M_PENDATA->m_nbDash = 0 ;
130 M_PENDATA->m_dash = (wxDash*)NULL;
131 M_PENDATA->m_hPen = 0 ;
132
133 RealizeResource();
134
135 }
136
137 bool wxPen::RealizeResource()
138 {
139 if (M_PENDATA && (M_PENDATA->m_hPen == 0))
140 {
141 if (M_PENDATA->m_style==wxTRANSPARENT)
142 {
143 M_PENDATA->m_hPen = (WXHPEN) ::GetStockObject(NULL_PEN);
144 return true;
145 }
146
147 COLORREF 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__) && !defined(__WXWINCE__)
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 Purify
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 int rw = M_PENDATA->m_width > 1 ? M_PENDATA->m_width : 1;
236 for ( int i = 0; i < M_PENDATA->m_nbDash; i++ )
237 real_dash[i] = M_PENDATA->m_dash[i] * rw;
238 }
239 else
240 {
241 real_dash = (wxMSWDash*)NULL;
242 }
243
244 // Win32s doesn't have ExtCreatePen function...
245 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
246 {
247 M_PENDATA->m_hPen =
248 (WXHPEN) ExtCreatePen( ms_style,
249 M_PENDATA->m_width,
250 &logb,
251 M_PENDATA->m_style == wxUSER_DASH
252 ? M_PENDATA->m_nbDash
253 : 0,
254 (LPDWORD)real_dash );
255 }
256 else
257 {
258 M_PENDATA->m_hPen =
259 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
260 M_PENDATA->m_width,
261 ms_colour );
262 }
263
264 if (real_dash)
265 delete [] real_dash;
266 }
267 #else
268 M_PENDATA->m_hPen =
269 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
270 M_PENDATA->m_width,
271 ms_colour );
272 #endif
273 #ifdef WXDEBUG_CREATE
274 if (M_PENDATA->m_hPen==0)
275 wxError("Cannot create pen","Internal error") ;
276 #endif
277 return true;
278 }
279 return false;
280 }
281
282 WXHANDLE wxPen::GetResourceHandle() const
283 {
284 if ( !M_PENDATA )
285 return 0;
286 else
287 return (WXHANDLE)M_PENDATA->m_hPen;
288 }
289
290 bool wxPen::FreeResource(bool WXUNUSED(force))
291 {
292 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
293 {
294 DeleteObject((HPEN) M_PENDATA->m_hPen);
295 M_PENDATA->m_hPen = 0;
296 return true;
297 }
298 else return false;
299 }
300
301 bool wxPen::IsFree() const
302 {
303 return (M_PENDATA && M_PENDATA->m_hPen == 0);
304 }
305
306 void wxPen::Unshare()
307 {
308 // Don't change shared data
309 if (!m_refData)
310 {
311 m_refData = new wxPenRefData();
312 }
313 else
314 {
315 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
316 UnRef();
317 m_refData = ref;
318 }
319 }
320
321 void wxPen::SetColour(const wxColour& col)
322 {
323 Unshare();
324
325 M_PENDATA->m_colour = col;
326
327 RealizeResource();
328 }
329
330 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
331 {
332 Unshare();
333
334 M_PENDATA->m_colour.Set(r, g, b);
335
336 RealizeResource();
337 }
338
339 void wxPen::SetWidth(int Width)
340 {
341 Unshare();
342
343 M_PENDATA->m_width = Width;
344
345 RealizeResource();
346 }
347
348 void wxPen::SetStyle(int Style)
349 {
350 Unshare();
351
352 M_PENDATA->m_style = Style;
353
354 RealizeResource();
355 }
356
357 void wxPen::SetStipple(const wxBitmap& Stipple)
358 {
359 Unshare();
360
361 M_PENDATA->m_stipple = Stipple;
362 M_PENDATA->m_style = wxSTIPPLE;
363
364 RealizeResource();
365 }
366
367 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
368 {
369 Unshare();
370
371 M_PENDATA->m_nbDash = nb_dashes;
372 M_PENDATA->m_dash = (wxDash *)Dash;
373
374 RealizeResource();
375 }
376
377 void wxPen::SetJoin(int Join)
378 {
379 Unshare();
380
381 M_PENDATA->m_join = Join;
382
383 RealizeResource();
384 }
385
386 void wxPen::SetCap(int Cap)
387 {
388 Unshare();
389
390 M_PENDATA->m_cap = Cap;
391
392 RealizeResource();
393 }
394
395 int wx2msPenStyle(int wx_style)
396 {
397 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
398 switch (wx_style)
399 {
400 case wxDOT:
401 return PS_DOT;
402
403 case wxDOT_DASH:
404 return PS_DASHDOT;
405
406 case wxSHORT_DASH:
407 case wxLONG_DASH:
408 return PS_DASH;
409
410 case wxTRANSPARENT:
411 return PS_NULL;
412
413 case wxUSER_DASH:
414 // if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
415 return PS_USERSTYLE;
416 }
417 #else
418 wxUnusedVar(wx_style);
419 #endif
420 return PS_SOLID;
421 }
422