]> git.saurik.com Git - wxWidgets.git/blame - src/msw/pen.cpp
fixed dll import specification of wxGetHInstance()
[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$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 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
a6c81161 34static int wx2msPenStyle(int wx_style);
ef59847c 35
2bda0e17 36IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
2bda0e17 37
e4a81a2e 38wxPenRefData::wxPenRefData()
2bda0e17 39{
2bda0e17
KB
40 m_style = wxSOLID;
41 m_width = 1;
42 m_join = wxJOIN_ROUND ;
43 m_cap = wxCAP_ROUND ;
44 m_nbDash = 0 ;
edd97174 45 m_dash = (wxDash*)NULL;
2bda0e17
KB
46 m_hPen = 0;
47}
48
b823f5a1
JS
49wxPenRefData::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
e4a81a2e 61wxPenRefData::~wxPenRefData()
2bda0e17 62{
8b0975a3
VZ
63 if ( m_hPen )
64 ::DeleteObject((HPEN) m_hPen);
2bda0e17
KB
65}
66
67// Pens
68
e4a81a2e 69wxPen::wxPen()
2bda0e17 70{
2bda0e17
KB
71}
72
73wxPen::~wxPen()
74{
2bda0e17
KB
75}
76
77// Should implement Create
debe6624 78wxPen::wxPen(const wxColour& col, int Width, int Style)
2bda0e17
KB
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 ;
edd97174 89 M_PENDATA->m_dash = (wxDash*)NULL;
2bda0e17
KB
90 M_PENDATA->m_hPen = 0 ;
91
2bda0e17 92 RealizeResource();
2bda0e17
KB
93}
94
debe6624 95wxPen::wxPen(const wxBitmap& stipple, int Width)
2bda0e17 96{
c45a644e 97 m_refData = new wxPenRefData;
2bda0e17
KB
98
99// M_PENDATA->m_colour = col;
c45a644e
RR
100 M_PENDATA->m_stipple = stipple;
101 M_PENDATA->m_width = Width;
102 M_PENDATA->m_style = wxSTIPPLE;
103 M_PENDATA->m_join = wxJOIN_ROUND ;
104 M_PENDATA->m_cap = wxCAP_ROUND ;
105 M_PENDATA->m_nbDash = 0 ;
edd97174 106 M_PENDATA->m_dash = (wxDash*)NULL;
c45a644e 107 M_PENDATA->m_hPen = 0 ;
2bda0e17 108
c45a644e 109 RealizeResource();
2bda0e17
KB
110}
111
e4a81a2e 112bool wxPen::RealizeResource()
2bda0e17 113{
8b0975a3
VZ
114 if ( !M_PENDATA || M_PENDATA->m_hPen )
115 return false;
116
117 if (M_PENDATA->m_style==wxTRANSPARENT)
c45a644e 118 {
8b0975a3
VZ
119 M_PENDATA->m_hPen = (WXHPEN) ::GetStockObject(NULL_PEN);
120 return true;
121 }
122
123 static const int os = wxGetOsVersion();
124 COLORREF ms_colour = M_PENDATA->m_colour.GetPixel();
125
126 // Join style, Cap style, Pen Stippling
127#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
128 // Only NT can display dashed or dotted lines with width > 1
129 if ( os != wxWINDOWS_NT &&
130 (M_PENDATA->m_style & (wxDOT |
131 wxLONG_DASH |
132 wxSHORT_DASH |
133 wxDOT_DASH |
134 wxUSER_DASH)) &&
135 M_PENDATA->m_width > 1 )
136 {
137 M_PENDATA->m_width = 1;
138 }
139
140 if (M_PENDATA->m_join==wxJOIN_ROUND &&
141 M_PENDATA->m_cap==wxCAP_ROUND &&
142 M_PENDATA->m_style!=wxUSER_DASH &&
143 M_PENDATA->m_style!=wxSTIPPLE &&
144 M_PENDATA->m_width <= 1)
145 {
146 M_PENDATA->m_hPen =
147 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
148 M_PENDATA->m_width,
149 ms_colour );
150 }
151 else
152 {
153 DWORD ms_style = PS_GEOMETRIC | wx2msPenStyle(M_PENDATA->m_style);
154
155 switch(M_PENDATA->m_join)
c45a644e 156 {
8b0975a3
VZ
157 case wxJOIN_BEVEL: ms_style |= PS_JOIN_BEVEL; break;
158 case wxJOIN_MITER: ms_style |= PS_JOIN_MITER; break;
159 default:
160 case wxJOIN_ROUND: ms_style |= PS_JOIN_ROUND; break;
c45a644e
RR
161 }
162
8b0975a3 163 switch(M_PENDATA->m_cap)
c45a644e 164 {
8b0975a3
VZ
165 case wxCAP_PROJECTING: ms_style |= PS_ENDCAP_SQUARE; break;
166 case wxCAP_BUTT: ms_style |= PS_ENDCAP_FLAT; break;
167 default:
168 case wxCAP_ROUND: ms_style |= PS_ENDCAP_ROUND; break;
c45a644e 169 }
8b0975a3
VZ
170
171 LOGBRUSH logb;
172
173 switch(M_PENDATA->m_style)
c45a644e 174 {
8b0975a3
VZ
175 case wxSTIPPLE:
176 logb.lbStyle = BS_PATTERN ;
177 if (M_PENDATA->m_stipple.Ok())
178 logb.lbHatch = (LONG)M_PENDATA->m_stipple.GetHBITMAP();
179 else
180 logb.lbHatch = (LONG)0;
181 break;
182 case wxBDIAGONAL_HATCH:
183 logb.lbStyle = BS_HATCHED;
184 logb.lbHatch = HS_BDIAGONAL;
185 break;
186 case wxCROSSDIAG_HATCH:
187 logb.lbStyle = BS_HATCHED;
188 logb.lbHatch = HS_DIAGCROSS;
189 break;
190 case wxFDIAGONAL_HATCH:
191 logb.lbStyle = BS_HATCHED;
192 logb.lbHatch = HS_FDIAGONAL;
193 break;
194 case wxCROSS_HATCH:
195 logb.lbStyle = BS_HATCHED;
196 logb.lbHatch = HS_CROSS;
197 break;
198 case wxHORIZONTAL_HATCH:
199 logb.lbStyle = BS_HATCHED;
200 logb.lbHatch = HS_HORIZONTAL;
201 break;
202 case wxVERTICAL_HATCH:
203 logb.lbStyle = BS_HATCHED;
204 logb.lbHatch = HS_VERTICAL;
205 break;
206 default:
207 logb.lbStyle = BS_SOLID;
61ba49f2 208#ifdef __WXDEBUG__
8b0975a3
VZ
209 // this should be unnecessary (it's unused) but suppresses the Purify
210 // messages about uninitialized memory read
211 logb.lbHatch = 0;
61ba49f2 212#endif
8b0975a3 213 break;
c45a644e 214 }
8b0975a3
VZ
215
216 logb.lbColor = ms_colour;
217
218 wxMSWDash *real_dash;
219 if (M_PENDATA->m_style==wxUSER_DASH && M_PENDATA->m_nbDash && M_PENDATA->m_dash)
220 {
221 real_dash = new wxMSWDash[M_PENDATA->m_nbDash];
222 int rw = M_PENDATA->m_width > 1 ? M_PENDATA->m_width : 1;
223 for ( int i = 0; i < M_PENDATA->m_nbDash; i++ )
224 real_dash[i] = M_PENDATA->m_dash[i] * rw;
225 }
226 else
227 {
228 real_dash = (wxMSWDash*)NULL;
229 }
230
231 // Win32s doesn't have ExtCreatePen function...
232 if (os==wxWINDOWS_NT || os==wxWIN95)
233 {
234 M_PENDATA->m_hPen =
235 (WXHPEN) ExtCreatePen( ms_style,
236 M_PENDATA->m_width,
237 &logb,
238 M_PENDATA->m_style == wxUSER_DASH
239 ? M_PENDATA->m_nbDash
240 : 0,
241 (LPDWORD)real_dash );
242 }
243 else
244 {
245 M_PENDATA->m_hPen =
246 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
247 M_PENDATA->m_width,
248 ms_colour );
249 }
250
251 delete [] real_dash;
252 }
253#else // WinCE
254 M_PENDATA->m_hPen =
255 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
256 M_PENDATA->m_width,
257 ms_colour );
258#endif // !WinCE/WinCE
259
260 return true;
2bda0e17
KB
261}
262
2b5f62a0 263WXHANDLE wxPen::GetResourceHandle() const
2bda0e17 264{
bbd41262
VZ
265 if ( !M_PENDATA )
266 return 0;
267 else
268 return (WXHANDLE)M_PENDATA->m_hPen;
2bda0e17
KB
269}
270
33ac7e6f 271bool wxPen::FreeResource(bool WXUNUSED(force))
2bda0e17
KB
272{
273 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
274 {
275 DeleteObject((HPEN) M_PENDATA->m_hPen);
276 M_PENDATA->m_hPen = 0;
078cf5cb 277 return true;
2bda0e17 278 }
078cf5cb 279 else return false;
2bda0e17
KB
280}
281
e4a81a2e 282bool wxPen::IsFree() const
2bda0e17 283{
b823f5a1 284 return (M_PENDATA && M_PENDATA->m_hPen == 0);
2bda0e17 285}
2bda0e17 286
b823f5a1 287void wxPen::Unshare()
2bda0e17 288{
bbd41262
VZ
289 // Don't change shared data
290 if (!m_refData)
b823f5a1 291 {
bbd41262
VZ
292 m_refData = new wxPenRefData();
293 }
b823f5a1
JS
294 else
295 {
bbd41262
VZ
296 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
297 UnRef();
298 m_refData = ref;
299 }
2bda0e17
KB
300}
301
302void wxPen::SetColour(const wxColour& col)
303{
b823f5a1 304 Unshare();
2bda0e17 305
b823f5a1 306 M_PENDATA->m_colour = col;
bbd41262 307
2bda0e17
KB
308 RealizeResource();
309}
310
e4a81a2e 311void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
2bda0e17 312{
b823f5a1 313 Unshare();
2bda0e17 314
b823f5a1 315 M_PENDATA->m_colour.Set(r, g, b);
bbd41262 316
2bda0e17
KB
317 RealizeResource();
318}
319
debe6624 320void wxPen::SetWidth(int Width)
2bda0e17 321{
b823f5a1 322 Unshare();
2bda0e17 323
b823f5a1 324 M_PENDATA->m_width = Width;
2bda0e17 325
2bda0e17
KB
326 RealizeResource();
327}
328
debe6624 329void wxPen::SetStyle(int Style)
2bda0e17 330{
b823f5a1 331 Unshare();
2bda0e17 332
b823f5a1 333 M_PENDATA->m_style = Style;
2bda0e17 334
2bda0e17
KB
335 RealizeResource();
336}
337
338void wxPen::SetStipple(const wxBitmap& Stipple)
339{
b823f5a1 340 Unshare();
2bda0e17 341
b823f5a1
JS
342 M_PENDATA->m_stipple = Stipple;
343 M_PENDATA->m_style = wxSTIPPLE;
bbd41262 344
2bda0e17
KB
345 RealizeResource();
346}
347
debe6624 348void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
2bda0e17 349{
b823f5a1 350 Unshare();
2bda0e17 351
b823f5a1 352 M_PENDATA->m_nbDash = nb_dashes;
edd97174 353 M_PENDATA->m_dash = (wxDash *)Dash;
bbd41262 354
2bda0e17
KB
355 RealizeResource();
356}
357
debe6624 358void wxPen::SetJoin(int Join)
2bda0e17 359{
b823f5a1 360 Unshare();
2bda0e17 361
b823f5a1 362 M_PENDATA->m_join = Join;
2bda0e17 363
2bda0e17
KB
364 RealizeResource();
365}
366
debe6624 367void wxPen::SetCap(int Cap)
2bda0e17 368{
b823f5a1 369 Unshare();
2bda0e17 370
b823f5a1 371 M_PENDATA->m_cap = Cap;
2bda0e17 372
2bda0e17
KB
373 RealizeResource();
374}
375
376int wx2msPenStyle(int wx_style)
377{
07505825 378#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
c45a644e 379 switch (wx_style)
bbd41262 380 {
07505825
WS
381 case wxDOT:
382 return PS_DOT;
c45a644e 383
07505825
WS
384 case wxDOT_DASH:
385 return PS_DASHDOT;
386
387 case wxSHORT_DASH:
388 case wxLONG_DASH:
389 return PS_DASH;
390
391 case wxTRANSPARENT:
392 return PS_NULL;
393
394 case wxUSER_DASH:
395 // if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
396 return PS_USERSTYLE;
397 }
2bda0e17 398#else
07505825 399 wxUnusedVar(wx_style);
2bda0e17 400#endif
07505825 401 return PS_SOLID;
2bda0e17
KB
402}
403