]> git.saurik.com Git - wxWidgets.git/blame - src/msw/pen.cpp
Mutiple updates from SciTech for wxWindows including the following:
[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 ;
edd97174 44 m_dash = (wxDash*)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 ;
edd97174 92 M_PENDATA->m_dash = (wxDash*)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 ;
edd97174 135 M_PENDATA->m_dash = (wxDash*)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!!]
04ef50df 160#if defined(__WIN32__) && !defined(__WXMICROWIN__)
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];
d275c7eb 243 for ( int i = 0; i < M_PENDATA->m_nbDash; i++ )
236a9de3 244 real_dash[i] = M_PENDATA->m_dash[i] * M_PENDATA->m_width;
c45a644e 245 }
bbd41262 246 else
c45a644e 247 {
bbd41262 248 real_dash = (wxMSWDash*)NULL;
c45a644e
RR
249 }
250
251 // Win32s doesn't have ExtCreatePen function...
bbd41262 252 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
c45a644e 253 {
bbd41262
VZ
254 M_PENDATA->m_hPen =
255 (WXHPEN) ExtCreatePen( ms_style,
256 M_PENDATA->m_width,
257 &logb,
258 M_PENDATA->m_style == wxUSER_DASH
259 ? M_PENDATA->m_nbDash
260 : 0,
261 (LPDWORD)real_dash );
262 }
263 else
264 {
265 M_PENDATA->m_hPen =
266 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
267 M_PENDATA->m_width,
268 ms_colour );
c45a644e
RR
269 }
270
271 if (real_dash)
272 delete [] real_dash;
273 }
2bda0e17 274#else
c45a644e 275 M_PENDATA->m_hPen =
bbd41262
VZ
276 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
277 M_PENDATA->m_width,
278 ms_colour );
2bda0e17 279#endif
b2aef89b 280#ifdef WXDEBUG_CREATE
c45a644e
RR
281 if (M_PENDATA->m_hPen==0)
282 wxError("Cannot create pen","Internal error") ;
2bda0e17 283#endif
c45a644e
RR
284 return TRUE;
285 }
286 return FALSE;
2bda0e17
KB
287}
288
e4a81a2e 289WXHANDLE wxPen::GetResourceHandle()
2bda0e17 290{
bbd41262
VZ
291 if ( !M_PENDATA )
292 return 0;
293 else
294 return (WXHANDLE)M_PENDATA->m_hPen;
2bda0e17
KB
295}
296
33ac7e6f 297bool wxPen::FreeResource(bool WXUNUSED(force))
2bda0e17
KB
298{
299 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
300 {
301 DeleteObject((HPEN) M_PENDATA->m_hPen);
302 M_PENDATA->m_hPen = 0;
303 return TRUE;
304 }
305 else return FALSE;
306}
307
e4a81a2e 308bool wxPen::IsFree() const
2bda0e17 309{
b823f5a1 310 return (M_PENDATA && M_PENDATA->m_hPen == 0);
2bda0e17 311}
2bda0e17 312
b823f5a1 313void wxPen::Unshare()
2bda0e17 314{
bbd41262
VZ
315 // Don't change shared data
316 if (!m_refData)
b823f5a1 317 {
bbd41262
VZ
318 m_refData = new wxPenRefData();
319 }
b823f5a1
JS
320 else
321 {
bbd41262
VZ
322 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
323 UnRef();
324 m_refData = ref;
325 }
2bda0e17
KB
326}
327
328void wxPen::SetColour(const wxColour& col)
329{
b823f5a1 330 Unshare();
2bda0e17 331
b823f5a1 332 M_PENDATA->m_colour = col;
bbd41262 333
2bda0e17
KB
334 RealizeResource();
335}
336
e4a81a2e 337void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
2bda0e17 338{
b823f5a1 339 Unshare();
2bda0e17 340
b823f5a1 341 M_PENDATA->m_colour.Set(r, g, b);
bbd41262 342
2bda0e17
KB
343 RealizeResource();
344}
345
debe6624 346void wxPen::SetWidth(int Width)
2bda0e17 347{
b823f5a1 348 Unshare();
2bda0e17 349
b823f5a1 350 M_PENDATA->m_width = Width;
2bda0e17 351
2bda0e17
KB
352 RealizeResource();
353}
354
debe6624 355void wxPen::SetStyle(int Style)
2bda0e17 356{
b823f5a1 357 Unshare();
2bda0e17 358
b823f5a1 359 M_PENDATA->m_style = Style;
2bda0e17 360
2bda0e17
KB
361 RealizeResource();
362}
363
364void wxPen::SetStipple(const wxBitmap& Stipple)
365{
b823f5a1 366 Unshare();
2bda0e17 367
b823f5a1
JS
368 M_PENDATA->m_stipple = Stipple;
369 M_PENDATA->m_style = wxSTIPPLE;
bbd41262 370
2bda0e17
KB
371 RealizeResource();
372}
373
debe6624 374void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
2bda0e17 375{
b823f5a1 376 Unshare();
2bda0e17 377
b823f5a1 378 M_PENDATA->m_nbDash = nb_dashes;
edd97174 379 M_PENDATA->m_dash = (wxDash *)Dash;
bbd41262 380
2bda0e17
KB
381 RealizeResource();
382}
383
debe6624 384void wxPen::SetJoin(int Join)
2bda0e17 385{
b823f5a1 386 Unshare();
2bda0e17 387
b823f5a1 388 M_PENDATA->m_join = Join;
2bda0e17 389
2bda0e17
KB
390 RealizeResource();
391}
392
debe6624 393void wxPen::SetCap(int Cap)
2bda0e17 394{
b823f5a1 395 Unshare();
2bda0e17 396
b823f5a1 397 M_PENDATA->m_cap = Cap;
2bda0e17 398
2bda0e17
KB
399 RealizeResource();
400}
401
402int wx2msPenStyle(int wx_style)
403{
c45a644e
RR
404 int cstyle;
405 switch (wx_style)
bbd41262 406 {
04ef50df 407#if !defined(__WXMICROWIN__)
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;
04ef50df 424#endif
c45a644e
RR
425
426 case wxUSER_DASH:
04ef50df 427#if !defined(__WXMICROWIN__)
2bda0e17 428#ifdef __WIN32__
c45a644e 429 // Win32s doesn't have PS_USERSTYLE
bbd41262
VZ
430 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
431 cstyle = PS_USERSTYLE;
432 else
c45a644e 433 cstyle = PS_DOT; // We must make a choice... This is mine!
2bda0e17 434#else
c45a644e 435 cstyle = PS_DASH;
04ef50df 436#endif
2bda0e17 437#endif
c45a644e
RR
438 break;
439 case wxSOLID:
440 default:
441 cstyle = PS_SOLID;
442 break;
443 }
444 return cstyle;
2bda0e17
KB
445}
446