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