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