]> git.saurik.com Git - wxWidgets.git/blame - src/msw/pen.cpp
Misc fixes
[wxWidgets.git] / src / msw / pen.cpp
CommitLineData
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$
8// Copyright: (c) Julian Smart and Markus Holzem
bbd41262 9// Licence: wxWindows license
2bda0e17
KB
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
2bda0e17 35IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
2bda0e17 36
e4a81a2e 37wxPenRefData::wxPenRefData()
2bda0e17 38{
2bda0e17
KB
39 m_style = wxSOLID;
40 m_width = 1;
41 m_join = wxJOIN_ROUND ;
42 m_cap = wxCAP_ROUND ;
43 m_nbDash = 0 ;
236a9de3 44 m_dash = (wxMSWDash*)NULL;
2bda0e17
KB
45 m_hPen = 0;
46}
47
b823f5a1
JS
48wxPenRefData::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
e4a81a2e 60wxPenRefData::~wxPenRefData()
2bda0e17 61{
bbd41262
VZ
62 if ( m_hPen )
63 ::DeleteObject((HPEN) m_hPen);
2bda0e17
KB
64}
65
66// Pens
67
e4a81a2e 68wxPen::wxPen()
2bda0e17 69{
c45a644e 70 if (wxThePenList)
2bda0e17
KB
71 wxThePenList->AddPen(this);
72}
73
74wxPen::~wxPen()
75{
76 if (wxThePenList)
77 wxThePenList->RemovePen(this);
78}
79
80// Should implement Create
debe6624 81wxPen::wxPen(const wxColour& col, int Width, int Style)
2bda0e17
KB
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 ;
236a9de3 92 M_PENDATA->m_dash = (wxMSWDash*)NULL;
2bda0e17
KB
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 }
bbd41262 117#endif
2bda0e17
KB
118 RealizeResource();
119
120 if ( wxThePenList )
121 wxThePenList->AddPen(this);
122}
123
debe6624 124wxPen::wxPen(const wxBitmap& stipple, int Width)
2bda0e17 125{
c45a644e 126 m_refData = new wxPenRefData;
2bda0e17
KB
127
128// M_PENDATA->m_colour = col;
c45a644e
RR
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 ;
236a9de3 135 M_PENDATA->m_dash = (wxMSWDash*)NULL;
c45a644e 136 M_PENDATA->m_hPen = 0 ;
2bda0e17 137
c45a644e 138 RealizeResource();
2bda0e17 139
c45a644e
RR
140 if (wxThePenList)
141 wxThePenList->AddPen(this);
2bda0e17
KB
142}
143
e4a81a2e 144bool wxPen::RealizeResource()
2bda0e17 145{
c45a644e
RR
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!!]
2bda0e17 160#ifdef __WIN32__
c45a644e
RR
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 &&
bbd41262 165 M_PENDATA->m_width <= 1)
c45a644e 166 {
bbd41262
VZ
167 M_PENDATA->m_hPen =
168 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
169 M_PENDATA->m_width,
170 ms_colour );
c45a644e
RR
171 }
172 else
173 {
bbd41262 174 DWORD ms_style = PS_GEOMETRIC | wx2msPenStyle(M_PENDATA->m_style);
c45a644e 175
bbd41262 176 switch(M_PENDATA->m_join)
c45a644e
RR
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
bbd41262 192 LOGBRUSH logb;
c45a644e 193
bbd41262 194 switch(M_PENDATA->m_style)
c45a644e
RR
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:
bbd41262
VZ
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:
c45a644e 228 logb.lbStyle = BS_SOLID;
61ba49f2 229#ifdef __WXDEBUG__
bbd41262 230 // this should be unnecessary (it's unused) but suppresses the Purigy
c45a644e 231 // messages about uninitialized memory read
bbd41262 232 logb.lbHatch = 0;
61ba49f2 233#endif
bbd41262
VZ
234 break;
235 }
c45a644e 236
bbd41262 237 logb.lbColor = ms_colour;
c45a644e 238
bbd41262 239 wxMSWDash *real_dash;
c45a644e 240 if (M_PENDATA->m_style==wxUSER_DASH && M_PENDATA->m_nbDash && M_PENDATA->m_dash)
bbd41262
VZ
241 {
242 real_dash = new wxMSWDash[M_PENDATA->m_nbDash];
c45a644e
RR
243 int i;
244 for (i=0; i<M_PENDATA->m_nbDash; i++)
236a9de3 245 real_dash[i] = M_PENDATA->m_dash[i] * M_PENDATA->m_width;
c45a644e 246 }
bbd41262 247 else
c45a644e 248 {
bbd41262 249 real_dash = (wxMSWDash*)NULL;
c45a644e
RR
250 }
251
252 // Win32s doesn't have ExtCreatePen function...
bbd41262 253 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
c45a644e 254 {
bbd41262
VZ
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 );
c45a644e
RR
270 }
271
272 if (real_dash)
273 delete [] real_dash;
274 }
2bda0e17 275#else
c45a644e 276 M_PENDATA->m_hPen =
bbd41262
VZ
277 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
278 M_PENDATA->m_width,
279 ms_colour );
2bda0e17 280#endif
b2aef89b 281#ifdef WXDEBUG_CREATE
c45a644e
RR
282 if (M_PENDATA->m_hPen==0)
283 wxError("Cannot create pen","Internal error") ;
2bda0e17 284#endif
c45a644e
RR
285 return TRUE;
286 }
287 return FALSE;
2bda0e17
KB
288}
289
e4a81a2e 290WXHANDLE wxPen::GetResourceHandle()
2bda0e17 291{
bbd41262
VZ
292 if ( !M_PENDATA )
293 return 0;
294 else
295 return (WXHANDLE)M_PENDATA->m_hPen;
2bda0e17
KB
296}
297
298bool wxPen::FreeResource(bool 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
e4a81a2e 309bool wxPen::IsFree() const
2bda0e17 310{
b823f5a1 311 return (M_PENDATA && M_PENDATA->m_hPen == 0);
2bda0e17 312}
2bda0e17 313
b823f5a1 314void wxPen::Unshare()
2bda0e17 315{
bbd41262
VZ
316 // Don't change shared data
317 if (!m_refData)
b823f5a1 318 {
bbd41262
VZ
319 m_refData = new wxPenRefData();
320 }
b823f5a1
JS
321 else
322 {
bbd41262
VZ
323 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
324 UnRef();
325 m_refData = ref;
326 }
2bda0e17
KB
327}
328
329void wxPen::SetColour(const wxColour& col)
330{
b823f5a1 331 Unshare();
2bda0e17 332
b823f5a1 333 M_PENDATA->m_colour = col;
bbd41262 334
2bda0e17
KB
335 RealizeResource();
336}
337
e4a81a2e 338void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
2bda0e17 339{
b823f5a1 340 Unshare();
2bda0e17 341
b823f5a1 342 M_PENDATA->m_colour.Set(r, g, b);
bbd41262 343
2bda0e17
KB
344 RealizeResource();
345}
346
debe6624 347void wxPen::SetWidth(int Width)
2bda0e17 348{
b823f5a1 349 Unshare();
2bda0e17 350
b823f5a1 351 M_PENDATA->m_width = Width;
2bda0e17 352
2bda0e17
KB
353 RealizeResource();
354}
355
debe6624 356void wxPen::SetStyle(int Style)
2bda0e17 357{
b823f5a1 358 Unshare();
2bda0e17 359
b823f5a1 360 M_PENDATA->m_style = Style;
2bda0e17 361
2bda0e17
KB
362 RealizeResource();
363}
364
365void wxPen::SetStipple(const wxBitmap& Stipple)
366{
b823f5a1 367 Unshare();
2bda0e17 368
b823f5a1
JS
369 M_PENDATA->m_stipple = Stipple;
370 M_PENDATA->m_style = wxSTIPPLE;
bbd41262 371
2bda0e17
KB
372 RealizeResource();
373}
374
debe6624 375void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
2bda0e17 376{
b823f5a1 377 Unshare();
2bda0e17 378
b823f5a1 379 M_PENDATA->m_nbDash = nb_dashes;
236a9de3 380 M_PENDATA->m_dash = (wxMSWDash *)Dash;
bbd41262 381
2bda0e17
KB
382 RealizeResource();
383}
384
debe6624 385void wxPen::SetJoin(int Join)
2bda0e17 386{
b823f5a1 387 Unshare();
2bda0e17 388
b823f5a1 389 M_PENDATA->m_join = Join;
2bda0e17 390
2bda0e17
KB
391 RealizeResource();
392}
393
debe6624 394void wxPen::SetCap(int Cap)
2bda0e17 395{
b823f5a1 396 Unshare();
2bda0e17 397
b823f5a1 398 M_PENDATA->m_cap = Cap;
2bda0e17 399
2bda0e17
KB
400 RealizeResource();
401}
402
403int wx2msPenStyle(int wx_style)
404{
c45a644e
RR
405 int cstyle;
406 switch (wx_style)
bbd41262 407 {
c45a644e
RR
408 case wxDOT:
409 cstyle = PS_DOT;
bbd41262 410 break;
c45a644e
RR
411
412 case wxDOT_DASH:
bbd41262
VZ
413 cstyle = PS_DASHDOT;
414 break;
c45a644e
RR
415
416 case wxSHORT_DASH:
417 case wxLONG_DASH:
418 cstyle = PS_DASH;
bbd41262 419 break;
c45a644e
RR
420
421 case wxTRANSPARENT:
422 cstyle = PS_NULL;
bbd41262 423 break;
c45a644e
RR
424
425 case wxUSER_DASH:
2bda0e17 426#ifdef __WIN32__
c45a644e 427 // Win32s doesn't have PS_USERSTYLE
bbd41262
VZ
428 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
429 cstyle = PS_USERSTYLE;
430 else
c45a644e 431 cstyle = PS_DOT; // We must make a choice... This is mine!
2bda0e17 432#else
c45a644e 433 cstyle = PS_DASH;
2bda0e17 434#endif
c45a644e
RR
435 break;
436 case wxSOLID:
437 default:
438 cstyle = PS_SOLID;
439 break;
440 }
441 return cstyle;
2bda0e17
KB
442}
443