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