]> git.saurik.com Git - wxWidgets.git/blame - src/msw/brush.cpp
don't crash when trying to dump struct members which are NULL pointers
[wxWidgets.git] / src / msw / brush.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
cafbad8f 2// Name: src/msw/brush.cpp
2bda0e17
KB
3// Purpose: wxBrush
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
cafbad8f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2bda0e17
KB
17#pragma implementation "brush.h"
18#endif
19
cafbad8f
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
2bda0e17
KB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
cafbad8f 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
cafbad8f
VZ
32 #include "wx/list.h"
33 #include "wx/utils.h"
34 #include "wx/app.h"
35 #include "wx/brush.h"
36#endif // WX_PRECOMP
2bda0e17
KB
37
38#include "wx/msw/private.h"
39
cafbad8f
VZ
40// ----------------------------------------------------------------------------
41// private classes
42// ----------------------------------------------------------------------------
43
44class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
45{
46public:
47 wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
48 wxBrushRefData(const wxBitmap& stipple);
49 wxBrushRefData(const wxBrushRefData& data);
50 virtual ~wxBrushRefData();
51
52 bool operator==(const wxBrushRefData& data) const;
53
54 HBRUSH GetHBRUSH();
55 void Free();
56
57 const wxColour& GetColour() const { return m_colour; }
58 int GetStyle() const { return m_style; }
59 wxBitmap *GetStipple() { return &m_stipple; }
60
61 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
62 void SetStyle(int style) { Free(); m_style = style; }
63 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
64
65private:
66 void DoSetStipple(const wxBitmap& stipple);
67
68 int m_style;
69 wxBitmap m_stipple;
70 wxColour m_colour;
71 HBRUSH m_hBrush;
72
73 // no assignment operator, the objects of this class are shared and never
74 // assigned after being created once
75 wxBrushRefData& operator=(const wxBrushRefData&);
76};
77
78#define M_BRUSHDATA ((wxBrushRefData *)m_refData)
79
80// ============================================================================
81// wxBrushRefData implementation
82// ============================================================================
2bda0e17 83
2bda0e17 84IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
2bda0e17 85
cafbad8f
VZ
86// ----------------------------------------------------------------------------
87// wxBrushRefData ctors/dtor
88// ----------------------------------------------------------------------------
89
90wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
91 : m_colour(colour)
2bda0e17 92{
cafbad8f
VZ
93 m_style = style;
94
95 m_hBrush = NULL;
b823f5a1
JS
96}
97
cafbad8f 98wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
b823f5a1 99{
cafbad8f
VZ
100 DoSetStipple(stipple);
101
102 m_hBrush = NULL;
2bda0e17
KB
103}
104
cafbad8f 105wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
04a18b0d
WS
106 : wxGDIRefData(),
107 m_stipple(data.m_stipple),
cafbad8f 108 m_colour(data.m_colour)
2bda0e17 109{
cafbad8f
VZ
110 m_style = data.m_style;
111
112 // we can't share HBRUSH, we'd need to create another one ourselves
113 m_hBrush = NULL;
2bda0e17
KB
114}
115
cafbad8f 116wxBrushRefData::~wxBrushRefData()
2bda0e17 117{
cafbad8f 118 Free();
2bda0e17
KB
119}
120
cafbad8f
VZ
121// ----------------------------------------------------------------------------
122// wxBrushRefData accesors
123// ----------------------------------------------------------------------------
124
125bool wxBrushRefData::operator==(const wxBrushRefData& data) const
2bda0e17 126{
cafbad8f
VZ
127 // don't compare HBRUSHes
128 return m_style == data.m_style &&
129 m_colour == data.m_colour &&
130 m_stipple == data.m_stipple;
2bda0e17
KB
131}
132
cafbad8f 133void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
2bda0e17 134{
cafbad8f
VZ
135 m_stipple = stipple;
136 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
137}
2bda0e17 138
cafbad8f
VZ
139// ----------------------------------------------------------------------------
140// wxBrushRefData resource handling
141// ----------------------------------------------------------------------------
2bda0e17 142
cafbad8f
VZ
143void wxBrushRefData::Free()
144{
145 if ( m_hBrush )
146 {
147 ::DeleteObject(m_hBrush);
2bda0e17 148
cafbad8f
VZ
149 m_hBrush = NULL;
150 }
2bda0e17
KB
151}
152
4676948b 153#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
35f51293
VZ
154
155static int TranslateHatchStyle(int style)
156{
cafbad8f
VZ
157 switch ( style )
158 {
cafbad8f
VZ
159 case wxBDIAGONAL_HATCH: return HS_BDIAGONAL;
160 case wxCROSSDIAG_HATCH: return HS_DIAGCROSS;
161 case wxFDIAGONAL_HATCH: return HS_FDIAGONAL;
162 case wxCROSS_HATCH: return HS_CROSS;
163 case wxHORIZONTAL_HATCH:return HS_HORIZONTAL;
164 case wxVERTICAL_HATCH: return HS_VERTICAL;
cafbad8f
VZ
165 default: return -1;
166 }
167}
168
35f51293
VZ
169#endif // !__WXMICROWIN__ && !__WXWINCE__
170
cafbad8f 171HBRUSH wxBrushRefData::GetHBRUSH()
2bda0e17 172{
cafbad8f
VZ
173 if ( !m_hBrush )
174 {
35f51293
VZ
175#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
176 int hatchStyle = TranslateHatchStyle(m_style);
cafbad8f 177 if ( hatchStyle == -1 )
35f51293 178#endif // !__WXMICROWIN__ && !__WXWINCE__
cafbad8f
VZ
179 {
180 switch ( m_style )
181 {
182 case wxTRANSPARENT:
183 m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
184 break;
185
186 case wxSTIPPLE:
187 m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple));
188 break;
189
190 case wxSTIPPLE_MASK_OPAQUE:
191 m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask()
192 ->GetMaskBitmap());
193 break;
194
195 default:
196 wxFAIL_MSG( _T("unknown brush style") );
197 // fall through
198
199 case wxSOLID:
200 m_hBrush = ::CreateSolidBrush(m_colour.GetPixel());
201 break;
202 }
203 }
4676948b 204#ifndef __WXWINCE__
cafbad8f
VZ
205 else // create a hatched brush
206 {
207 m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel());
208 }
4676948b 209#endif
cafbad8f
VZ
210
211 if ( !m_hBrush )
212 {
213 wxLogLastError(_T("CreateXXXBrush()"));
214 }
215 }
216
217 return m_hBrush;
218}
2bda0e17 219
cafbad8f
VZ
220// ============================================================================
221// wxBrush implementation
222// ============================================================================
de2d2cdc 223
cafbad8f
VZ
224// ----------------------------------------------------------------------------
225// wxBrush ctors/dtor
226// ----------------------------------------------------------------------------
2bda0e17 227
cafbad8f
VZ
228wxBrush::wxBrush()
229{
230}
2bda0e17 231
cafbad8f
VZ
232wxBrush::wxBrush(const wxColour& col, int style)
233{
234 m_refData = new wxBrushRefData(col, style);
2bda0e17
KB
235}
236
cafbad8f 237wxBrush::wxBrush(const wxBitmap& stipple)
2bda0e17 238{
cafbad8f
VZ
239 m_refData = new wxBrushRefData(stipple);
240}
2bda0e17 241
cafbad8f
VZ
242wxBrush::~wxBrush()
243{
244}
2bda0e17 245
cafbad8f
VZ
246// ----------------------------------------------------------------------------
247// wxBrush house keeping stuff
248// ----------------------------------------------------------------------------
249
250wxBrush& wxBrush::operator=(const wxBrush& brush)
251{
6c6bf502 252 if ( this != &brush )
2bda0e17 253 {
cafbad8f 254 Ref(brush);
2bda0e17 255 }
2bda0e17 256
cafbad8f 257 return *this;
2bda0e17
KB
258}
259
cafbad8f 260bool wxBrush::operator==(const wxBrush& brush) const
2bda0e17 261{
cafbad8f
VZ
262 const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData;
263
264 // an invalid brush is considered to be only equal to another invalid brush
265 return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData;
2bda0e17
KB
266}
267
cafbad8f 268wxObjectRefData *wxBrush::CreateRefData() const
2bda0e17 269{
cafbad8f 270 return new wxBrushRefData;
2bda0e17
KB
271}
272
cafbad8f 273wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
2bda0e17 274{
cafbad8f 275 return new wxBrushRefData(*(const wxBrushRefData *)data);
2bda0e17 276}
2bda0e17 277
cafbad8f
VZ
278// ----------------------------------------------------------------------------
279// wxBrush accessors
280// ----------------------------------------------------------------------------
2bda0e17 281
cafbad8f 282wxColour wxBrush::GetColour() const
2bda0e17 283{
cafbad8f 284 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
2bda0e17 285
cafbad8f
VZ
286 return M_BRUSHDATA->GetColour();
287}
288
289int wxBrush::GetStyle() const
290{
291 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
2bda0e17 292
cafbad8f 293 return M_BRUSHDATA->GetStyle();
2bda0e17
KB
294}
295
cafbad8f 296wxBitmap *wxBrush::GetStipple() const
2bda0e17 297{
cafbad8f
VZ
298 wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
299
300 return M_BRUSHDATA->GetStipple();
301}
2bda0e17 302
2b5f62a0 303WXHANDLE wxBrush::GetResourceHandle() const
cafbad8f
VZ
304{
305 wxCHECK_MSG( Ok(), FALSE, _T("invalid brush") );
2bda0e17 306
2b5f62a0 307 return (WXHANDLE)M_BRUSHDATA->GetHBRUSH();
2bda0e17
KB
308}
309
cafbad8f
VZ
310// ----------------------------------------------------------------------------
311// wxBrush setters
312// ----------------------------------------------------------------------------
313
314void wxBrush::SetColour(const wxColour& col)
2bda0e17 315{
cafbad8f 316 AllocExclusive();
2bda0e17 317
cafbad8f
VZ
318 M_BRUSHDATA->SetColour(col);
319}
2bda0e17 320
cafbad8f
VZ
321void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
322{
323 AllocExclusive();
324
325 M_BRUSHDATA->SetColour(wxColour(r, g, b));
2bda0e17
KB
326}
327
cafbad8f 328void wxBrush::SetStyle(int style)
2bda0e17 329{
cafbad8f
VZ
330 AllocExclusive();
331
332 M_BRUSHDATA->SetStyle(style);
333}
2bda0e17 334
cafbad8f
VZ
335void wxBrush::SetStipple(const wxBitmap& stipple)
336{
337 AllocExclusive();
2bda0e17 338
cafbad8f 339 M_BRUSHDATA->SetStipple(stipple);
2bda0e17
KB
340}
341
342