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