]> git.saurik.com Git - wxWidgets.git/blame - src/msw/brush.cpp
Clean up memory if have to exit early
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
cafbad8f 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
cafbad8f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
2bda0e17
KB
16#ifdef __GNUG__
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
VZ
105wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
106 : m_stipple(data.m_stipple),
107 m_colour(data.m_colour)
2bda0e17 108{
cafbad8f
VZ
109 m_style = data.m_style;
110
111 // we can't share HBRUSH, we'd need to create another one ourselves
112 m_hBrush = NULL;
2bda0e17
KB
113}
114
cafbad8f 115wxBrushRefData::~wxBrushRefData()
2bda0e17 116{
cafbad8f 117 Free();
2bda0e17
KB
118}
119
cafbad8f
VZ
120// ----------------------------------------------------------------------------
121// wxBrushRefData accesors
122// ----------------------------------------------------------------------------
123
124bool wxBrushRefData::operator==(const wxBrushRefData& data) const
2bda0e17 125{
cafbad8f
VZ
126 // don't compare HBRUSHes
127 return m_style == data.m_style &&
128 m_colour == data.m_colour &&
129 m_stipple == data.m_stipple;
2bda0e17
KB
130}
131
cafbad8f 132void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
2bda0e17 133{
cafbad8f
VZ
134 m_stipple = stipple;
135 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
136}
2bda0e17 137
cafbad8f
VZ
138// ----------------------------------------------------------------------------
139// wxBrushRefData resource handling
140// ----------------------------------------------------------------------------
2bda0e17 141
cafbad8f
VZ
142void wxBrushRefData::Free()
143{
144 if ( m_hBrush )
145 {
146 ::DeleteObject(m_hBrush);
2bda0e17 147
cafbad8f
VZ
148 m_hBrush = NULL;
149 }
2bda0e17
KB
150}
151
cafbad8f
VZ
152static int TransllateHatchStyle(int style)
153{
154 switch ( style )
155 {
156#ifndef __WXMICROWIN__
157 case wxBDIAGONAL_HATCH: return HS_BDIAGONAL;
158 case wxCROSSDIAG_HATCH: return HS_DIAGCROSS;
159 case wxFDIAGONAL_HATCH: return HS_FDIAGONAL;
160 case wxCROSS_HATCH: return HS_CROSS;
161 case wxHORIZONTAL_HATCH:return HS_HORIZONTAL;
162 case wxVERTICAL_HATCH: return HS_VERTICAL;
163#endif // __WXMICROWIN__
164 default: return -1;
165 }
166}
167
168HBRUSH wxBrushRefData::GetHBRUSH()
2bda0e17 169{
cafbad8f
VZ
170 if ( !m_hBrush )
171 {
172 int hatchStyle = TransllateHatchStyle(m_style);
173 if ( hatchStyle == -1 )
174 {
175 switch ( m_style )
176 {
177 case wxTRANSPARENT:
178 m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
179 break;
180
181 case wxSTIPPLE:
182 m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple));
183 break;
184
185 case wxSTIPPLE_MASK_OPAQUE:
186 m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask()
187 ->GetMaskBitmap());
188 break;
189
190 default:
191 wxFAIL_MSG( _T("unknown brush style") );
192 // fall through
193
194 case wxSOLID:
195 m_hBrush = ::CreateSolidBrush(m_colour.GetPixel());
196 break;
197 }
198 }
199 else // create a hatched brush
200 {
201 m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel());
202 }
203
204 if ( !m_hBrush )
205 {
206 wxLogLastError(_T("CreateXXXBrush()"));
207 }
208 }
209
210 return m_hBrush;
211}
2bda0e17 212
cafbad8f
VZ
213// ============================================================================
214// wxBrush implementation
215// ============================================================================
de2d2cdc 216
cafbad8f
VZ
217// ----------------------------------------------------------------------------
218// wxBrush ctors/dtor
219// ----------------------------------------------------------------------------
2bda0e17 220
cafbad8f
VZ
221wxBrush::wxBrush()
222{
223}
2bda0e17 224
cafbad8f
VZ
225wxBrush::wxBrush(const wxColour& col, int style)
226{
227 m_refData = new wxBrushRefData(col, style);
2bda0e17
KB
228}
229
cafbad8f 230wxBrush::wxBrush(const wxBitmap& stipple)
2bda0e17 231{
cafbad8f
VZ
232 m_refData = new wxBrushRefData(stipple);
233}
2bda0e17 234
cafbad8f
VZ
235wxBrush::~wxBrush()
236{
237}
2bda0e17 238
cafbad8f
VZ
239// ----------------------------------------------------------------------------
240// wxBrush house keeping stuff
241// ----------------------------------------------------------------------------
242
243wxBrush& wxBrush::operator=(const wxBrush& brush)
244{
245 if ( *this != brush )
2bda0e17 246 {
cafbad8f 247 Ref(brush);
2bda0e17 248 }
2bda0e17 249
cafbad8f 250 return *this;
2bda0e17
KB
251}
252
cafbad8f 253bool wxBrush::operator==(const wxBrush& brush) const
2bda0e17 254{
cafbad8f
VZ
255 const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData;
256
257 // an invalid brush is considered to be only equal to another invalid brush
258 return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData;
2bda0e17
KB
259}
260
cafbad8f 261wxObjectRefData *wxBrush::CreateRefData() const
2bda0e17 262{
cafbad8f 263 return new wxBrushRefData;
2bda0e17
KB
264}
265
cafbad8f 266wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
2bda0e17 267{
cafbad8f 268 return new wxBrushRefData(*(const wxBrushRefData *)data);
2bda0e17 269}
2bda0e17 270
cafbad8f
VZ
271// ----------------------------------------------------------------------------
272// wxBrush accessors
273// ----------------------------------------------------------------------------
2bda0e17 274
cafbad8f 275wxColour wxBrush::GetColour() const
2bda0e17 276{
cafbad8f 277 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
2bda0e17 278
cafbad8f
VZ
279 return M_BRUSHDATA->GetColour();
280}
281
282int wxBrush::GetStyle() const
283{
284 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
2bda0e17 285
cafbad8f 286 return M_BRUSHDATA->GetStyle();
2bda0e17
KB
287}
288
cafbad8f 289wxBitmap *wxBrush::GetStipple() const
2bda0e17 290{
cafbad8f
VZ
291 wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
292
293 return M_BRUSHDATA->GetStipple();
294}
2bda0e17 295
2b5f62a0 296WXHANDLE wxBrush::GetResourceHandle() const
cafbad8f
VZ
297{
298 wxCHECK_MSG( Ok(), FALSE, _T("invalid brush") );
2bda0e17 299
2b5f62a0 300 return (WXHANDLE)M_BRUSHDATA->GetHBRUSH();
2bda0e17
KB
301}
302
cafbad8f
VZ
303// ----------------------------------------------------------------------------
304// wxBrush setters
305// ----------------------------------------------------------------------------
306
307void wxBrush::SetColour(const wxColour& col)
2bda0e17 308{
cafbad8f 309 AllocExclusive();
2bda0e17 310
cafbad8f
VZ
311 M_BRUSHDATA->SetColour(col);
312}
2bda0e17 313
cafbad8f
VZ
314void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
315{
316 AllocExclusive();
317
318 M_BRUSHDATA->SetColour(wxColour(r, g, b));
2bda0e17
KB
319}
320
cafbad8f 321void wxBrush::SetStyle(int style)
2bda0e17 322{
cafbad8f
VZ
323 AllocExclusive();
324
325 M_BRUSHDATA->SetStyle(style);
326}
2bda0e17 327
cafbad8f
VZ
328void wxBrush::SetStipple(const wxBitmap& stipple)
329{
330 AllocExclusive();
2bda0e17 331
cafbad8f 332 M_BRUSHDATA->SetStipple(stipple);
2bda0e17
KB
333}
334
335