]> git.saurik.com Git - wxWidgets.git/blame - src/msw/brush.cpp
wxMessageBox off the main thread lost result code.
[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
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
cafbad8f
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
cafbad8f
VZ
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
cafbad8f 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
c64755ed
WS
26#include "wx/brush.h"
27
2bda0e17 28#ifndef WX_PRECOMP
cafbad8f
VZ
29 #include "wx/list.h"
30 #include "wx/utils.h"
31 #include "wx/app.h"
ed7ec76d 32 #include "wx/bitmap.h"
cafbad8f 33#endif // WX_PRECOMP
2bda0e17
KB
34
35#include "wx/msw/private.h"
36
cafbad8f
VZ
37// ----------------------------------------------------------------------------
38// private classes
39// ----------------------------------------------------------------------------
40
41class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
42{
43public:
3e6858cd 44 wxBrushRefData(const wxColour& colour = wxNullColour, wxBrushStyle style = wxBRUSHSTYLE_SOLID);
cafbad8f
VZ
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; }
3e6858cd 55 wxBrushStyle GetStyle() const { return m_style; }
cafbad8f
VZ
56 wxBitmap *GetStipple() { return &m_stipple; }
57
58 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
3e6858cd 59 void SetStyle(wxBrushStyle style) { Free(); m_style = style; }
cafbad8f
VZ
60 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
61
62private:
63 void DoSetStipple(const wxBitmap& stipple);
64
3e6858cd 65 wxBrushStyle m_style;
cafbad8f
VZ
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// ============================================================================
2bda0e17 80
2bda0e17 81IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
2bda0e17 82
cafbad8f
VZ
83// ----------------------------------------------------------------------------
84// wxBrushRefData ctors/dtor
85// ----------------------------------------------------------------------------
86
3e6858cd 87wxBrushRefData::wxBrushRefData(const wxColour& colour, wxBrushStyle style)
cafbad8f 88 : m_colour(colour)
2bda0e17 89{
cafbad8f
VZ
90 m_style = style;
91
92 m_hBrush = NULL;
b823f5a1
JS
93}
94
cafbad8f 95wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
b823f5a1 96{
cafbad8f
VZ
97 DoSetStipple(stipple);
98
99 m_hBrush = NULL;
2bda0e17
KB
100}
101
cafbad8f 102wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
04a18b0d
WS
103 : wxGDIRefData(),
104 m_stipple(data.m_stipple),
cafbad8f 105 m_colour(data.m_colour)
2bda0e17 106{
cafbad8f
VZ
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;
2bda0e17
KB
111}
112
cafbad8f 113wxBrushRefData::~wxBrushRefData()
2bda0e17 114{
cafbad8f 115 Free();
2bda0e17
KB
116}
117
cafbad8f
VZ
118// ----------------------------------------------------------------------------
119// wxBrushRefData accesors
120// ----------------------------------------------------------------------------
121
122bool wxBrushRefData::operator==(const wxBrushRefData& data) const
2bda0e17 123{
cafbad8f
VZ
124 // don't compare HBRUSHes
125 return m_style == data.m_style &&
126 m_colour == data.m_colour &&
a3ab1c18 127 m_stipple.IsSameAs(data.m_stipple);
2bda0e17
KB
128}
129
cafbad8f 130void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
2bda0e17 131{
cafbad8f 132 m_stipple = stipple;
cb129171
FM
133 m_style = stipple.GetMask() ? wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
134 : wxBRUSHSTYLE_STIPPLE;
cafbad8f 135}
2bda0e17 136
cafbad8f
VZ
137// ----------------------------------------------------------------------------
138// wxBrushRefData resource handling
139// ----------------------------------------------------------------------------
2bda0e17 140
cafbad8f
VZ
141void wxBrushRefData::Free()
142{
143 if ( m_hBrush )
144 {
145 ::DeleteObject(m_hBrush);
2bda0e17 146
cafbad8f
VZ
147 m_hBrush = NULL;
148 }
2bda0e17
KB
149}
150
4676948b 151#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
35f51293
VZ
152
153static int TranslateHatchStyle(int style)
154{
cafbad8f
VZ
155 switch ( style )
156 {
cb129171
FM
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;
cafbad8f
VZ
163 default: return -1;
164 }
165}
166
35f51293
VZ
167#endif // !__WXMICROWIN__ && !__WXWINCE__
168
cafbad8f 169HBRUSH wxBrushRefData::GetHBRUSH()
2bda0e17 170{
cafbad8f
VZ
171 if ( !m_hBrush )
172 {
35f51293
VZ
173#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
174 int hatchStyle = TranslateHatchStyle(m_style);
cafbad8f 175 if ( hatchStyle == -1 )
35f51293 176#endif // !__WXMICROWIN__ && !__WXWINCE__
cafbad8f
VZ
177 {
178 switch ( m_style )
179 {
cb129171 180 case wxBRUSHSTYLE_TRANSPARENT:
cafbad8f
VZ
181 m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
182 break;
183
cb129171 184 case wxBRUSHSTYLE_STIPPLE:
cafbad8f
VZ
185 m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple));
186 break;
187
cb129171 188 case wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE:
cafbad8f
VZ
189 m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask()
190 ->GetMaskBitmap());
191 break;
192
193 default:
9a83f860 194 wxFAIL_MSG( wxT("unknown brush style") );
cafbad8f
VZ
195 // fall through
196
cb129171 197 case wxBRUSHSTYLE_SOLID:
cafbad8f
VZ
198 m_hBrush = ::CreateSolidBrush(m_colour.GetPixel());
199 break;
200 }
201 }
4676948b 202#ifndef __WXWINCE__
cafbad8f
VZ
203 else // create a hatched brush
204 {
205 m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel());
206 }
4676948b 207#endif
cafbad8f
VZ
208
209 if ( !m_hBrush )
210 {
9a83f860 211 wxLogLastError(wxT("CreateXXXBrush()"));
cafbad8f
VZ
212 }
213 }
214
215 return m_hBrush;
216}
2bda0e17 217
cafbad8f
VZ
218// ============================================================================
219// wxBrush implementation
220// ============================================================================
de2d2cdc 221
cafbad8f
VZ
222// ----------------------------------------------------------------------------
223// wxBrush ctors/dtor
224// ----------------------------------------------------------------------------
2bda0e17 225
cafbad8f
VZ
226wxBrush::wxBrush()
227{
228}
2bda0e17 229
3e6858cd 230wxBrush::wxBrush(const wxColour& col, wxBrushStyle style)
cafbad8f
VZ
231{
232 m_refData = new wxBrushRefData(col, style);
2bda0e17
KB
233}
234
ac3688c0
FM
235#if FUTURE_WXWIN_COMPATIBILITY_3_0
236wxBrush::wxBrush(const wxColour& col, int style)
237{
238 m_refData = new wxBrushRefData(col, (wxBrushStyle)style);
239}
240#endif
241
cafbad8f 242wxBrush::wxBrush(const wxBitmap& stipple)
2bda0e17 243{
cafbad8f
VZ
244 m_refData = new wxBrushRefData(stipple);
245}
2bda0e17 246
cafbad8f
VZ
247wxBrush::~wxBrush()
248{
249}
2bda0e17 250
cafbad8f
VZ
251// ----------------------------------------------------------------------------
252// wxBrush house keeping stuff
253// ----------------------------------------------------------------------------
254
cafbad8f 255bool wxBrush::operator==(const wxBrush& brush) const
2bda0e17 256{
cafbad8f
VZ
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;
2bda0e17
KB
261}
262
8f884a0d 263wxGDIRefData *wxBrush::CreateGDIRefData() const
2bda0e17 264{
cafbad8f 265 return new wxBrushRefData;
2bda0e17
KB
266}
267
8f884a0d 268wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
2bda0e17 269{
cafbad8f 270 return new wxBrushRefData(*(const wxBrushRefData *)data);
2bda0e17 271}
2bda0e17 272
cafbad8f
VZ
273// ----------------------------------------------------------------------------
274// wxBrush accessors
275// ----------------------------------------------------------------------------
2bda0e17 276
cafbad8f 277wxColour wxBrush::GetColour() const
2bda0e17 278{
a1b806b9 279 wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid brush") );
2bda0e17 280
cafbad8f
VZ
281 return M_BRUSHDATA->GetColour();
282}
283
3e6858cd 284wxBrushStyle wxBrush::GetStyle() const
cafbad8f 285{
a1b806b9 286 wxCHECK_MSG( IsOk(), wxBRUSHSTYLE_INVALID, wxT("invalid brush") );
2bda0e17 287
cafbad8f 288 return M_BRUSHDATA->GetStyle();
2bda0e17
KB
289}
290
cafbad8f 291wxBitmap *wxBrush::GetStipple() const
2bda0e17 292{
a1b806b9 293 wxCHECK_MSG( IsOk(), NULL, wxT("invalid brush") );
cafbad8f
VZ
294
295 return M_BRUSHDATA->GetStipple();
296}
2bda0e17 297
2b5f62a0 298WXHANDLE wxBrush::GetResourceHandle() const
cafbad8f 299{
a1b806b9 300 wxCHECK_MSG( IsOk(), FALSE, wxT("invalid brush") );
2bda0e17 301
2b5f62a0 302 return (WXHANDLE)M_BRUSHDATA->GetHBRUSH();
2bda0e17
KB
303}
304
cafbad8f
VZ
305// ----------------------------------------------------------------------------
306// wxBrush setters
307// ----------------------------------------------------------------------------
308
309void wxBrush::SetColour(const wxColour& col)
2bda0e17 310{
cafbad8f 311 AllocExclusive();
2bda0e17 312
cafbad8f
VZ
313 M_BRUSHDATA->SetColour(col);
314}
2bda0e17 315
1a1498c0 316void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
cafbad8f
VZ
317{
318 AllocExclusive();
319
320 M_BRUSHDATA->SetColour(wxColour(r, g, b));
2bda0e17
KB
321}
322
3e6858cd 323void wxBrush::SetStyle(wxBrushStyle style)
2bda0e17 324{
cafbad8f
VZ
325 AllocExclusive();
326
327 M_BRUSHDATA->SetStyle(style);
328}
2bda0e17 329
cafbad8f
VZ
330void wxBrush::SetStipple(const wxBitmap& stipple)
331{
332 AllocExclusive();
2bda0e17 333
cafbad8f 334 M_BRUSHDATA->SetStipple(stipple);
2bda0e17 335}