]> git.saurik.com Git - wxWidgets.git/blob - src/msw/brush.cpp
preliminary EXTENDED_RTTI
[wxWidgets.git] / src / msw / brush.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/brush.cpp
3 // Purpose: wxBrush
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "brush.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/list.h"
33 #include "wx/utils.h"
34 #include "wx/app.h"
35 #include "wx/brush.h"
36 #endif // WX_PRECOMP
37
38 #include "wx/msw/private.h"
39
40 // ----------------------------------------------------------------------------
41 // private classes
42 // ----------------------------------------------------------------------------
43
44 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
45 {
46 public:
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
65 private:
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 // ============================================================================
83
84 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
85
86 // ----------------------------------------------------------------------------
87 // wxBrushRefData ctors/dtor
88 // ----------------------------------------------------------------------------
89
90 wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
91 : m_colour(colour)
92 {
93 m_style = style;
94
95 m_hBrush = NULL;
96 }
97
98 wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
99 {
100 DoSetStipple(stipple);
101
102 m_hBrush = NULL;
103 }
104
105 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
106 : m_stipple(data.m_stipple),
107 m_colour(data.m_colour)
108 {
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;
113 }
114
115 wxBrushRefData::~wxBrushRefData()
116 {
117 Free();
118 }
119
120 // ----------------------------------------------------------------------------
121 // wxBrushRefData accesors
122 // ----------------------------------------------------------------------------
123
124 bool wxBrushRefData::operator==(const wxBrushRefData& data) const
125 {
126 // don't compare HBRUSHes
127 return m_style == data.m_style &&
128 m_colour == data.m_colour &&
129 m_stipple == data.m_stipple;
130 }
131
132 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
133 {
134 m_stipple = stipple;
135 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
136 }
137
138 // ----------------------------------------------------------------------------
139 // wxBrushRefData resource handling
140 // ----------------------------------------------------------------------------
141
142 void wxBrushRefData::Free()
143 {
144 if ( m_hBrush )
145 {
146 ::DeleteObject(m_hBrush);
147
148 m_hBrush = NULL;
149 }
150 }
151
152 static int TransllateHatchStyle(int style)
153 {
154 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
155 switch ( style )
156 {
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 default: return -1;
164 }
165 #else // __WXMICROWIN__
166 return -1;
167 #endif
168 }
169
170 HBRUSH wxBrushRefData::GetHBRUSH()
171 {
172 if ( !m_hBrush )
173 {
174 #ifndef __WXWINCE__
175 int hatchStyle = TransllateHatchStyle(m_style);
176 if ( hatchStyle == -1 )
177 #endif
178 {
179 switch ( m_style )
180 {
181 case wxTRANSPARENT:
182 m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
183 break;
184
185 case wxSTIPPLE:
186 m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple));
187 break;
188
189 case wxSTIPPLE_MASK_OPAQUE:
190 m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask()
191 ->GetMaskBitmap());
192 break;
193
194 default:
195 wxFAIL_MSG( _T("unknown brush style") );
196 // fall through
197
198 case wxSOLID:
199 m_hBrush = ::CreateSolidBrush(m_colour.GetPixel());
200 break;
201 }
202 }
203 #ifndef __WXWINCE__
204 else // create a hatched brush
205 {
206 m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel());
207 }
208 #endif
209
210 if ( !m_hBrush )
211 {
212 wxLogLastError(_T("CreateXXXBrush()"));
213 }
214 }
215
216 return m_hBrush;
217 }
218
219 // ============================================================================
220 // wxBrush implementation
221 // ============================================================================
222
223 // ----------------------------------------------------------------------------
224 // wxBrush ctors/dtor
225 // ----------------------------------------------------------------------------
226
227 wxBrush::wxBrush()
228 {
229 }
230
231 wxBrush::wxBrush(const wxColour& col, int style)
232 {
233 m_refData = new wxBrushRefData(col, style);
234 }
235
236 wxBrush::wxBrush(const wxBitmap& stipple)
237 {
238 m_refData = new wxBrushRefData(stipple);
239 }
240
241 wxBrush::~wxBrush()
242 {
243 }
244
245 // ----------------------------------------------------------------------------
246 // wxBrush house keeping stuff
247 // ----------------------------------------------------------------------------
248
249 wxBrush& wxBrush::operator=(const wxBrush& brush)
250 {
251 if ( *this != brush )
252 {
253 Ref(brush);
254 }
255
256 return *this;
257 }
258
259 bool wxBrush::operator==(const wxBrush& brush) const
260 {
261 const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData;
262
263 // an invalid brush is considered to be only equal to another invalid brush
264 return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData;
265 }
266
267 wxObjectRefData *wxBrush::CreateRefData() const
268 {
269 return new wxBrushRefData;
270 }
271
272 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
273 {
274 return new wxBrushRefData(*(const wxBrushRefData *)data);
275 }
276
277 // ----------------------------------------------------------------------------
278 // wxBrush accessors
279 // ----------------------------------------------------------------------------
280
281 wxColour wxBrush::GetColour() const
282 {
283 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
284
285 return M_BRUSHDATA->GetColour();
286 }
287
288 int wxBrush::GetStyle() const
289 {
290 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
291
292 return M_BRUSHDATA->GetStyle();
293 }
294
295 wxBitmap *wxBrush::GetStipple() const
296 {
297 wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
298
299 return M_BRUSHDATA->GetStipple();
300 }
301
302 WXHANDLE wxBrush::GetResourceHandle() const
303 {
304 wxCHECK_MSG( Ok(), FALSE, _T("invalid brush") );
305
306 return (WXHANDLE)M_BRUSHDATA->GetHBRUSH();
307 }
308
309 // ----------------------------------------------------------------------------
310 // wxBrush setters
311 // ----------------------------------------------------------------------------
312
313 void wxBrush::SetColour(const wxColour& col)
314 {
315 AllocExclusive();
316
317 M_BRUSHDATA->SetColour(col);
318 }
319
320 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
321 {
322 AllocExclusive();
323
324 M_BRUSHDATA->SetColour(wxColour(r, g, b));
325 }
326
327 void wxBrush::SetStyle(int style)
328 {
329 AllocExclusive();
330
331 M_BRUSHDATA->SetStyle(style);
332 }
333
334 void wxBrush::SetStipple(const wxBitmap& stipple)
335 {
336 AllocExclusive();
337
338 M_BRUSHDATA->SetStipple(stipple);
339 }
340
341