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