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