1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/brush.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/bitmap.h"
36 #include "wx/msw/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxBrushRefData
: public wxGDIRefData
45 wxBrushRefData(const wxColour
& colour
= wxNullColour
, wxBrushStyle style
= wxBRUSHSTYLE_SOLID
);
46 wxBrushRefData(const wxBitmap
& stipple
);
47 wxBrushRefData(const wxBrushRefData
& data
);
48 virtual ~wxBrushRefData();
50 bool operator==(const wxBrushRefData
& data
) const;
55 const wxColour
& GetColour() const { return m_colour
; }
56 wxBrushStyle
GetStyle() const { return m_style
; }
57 wxBitmap
*GetStipple() { return &m_stipple
; }
59 void SetColour(const wxColour
& colour
) { Free(); m_colour
= colour
; }
60 void SetStyle(wxBrushStyle style
) { Free(); m_style
= style
; }
61 void SetStipple(const wxBitmap
& stipple
) { Free(); DoSetStipple(stipple
); }
64 void DoSetStipple(const wxBitmap
& stipple
);
71 // no assignment operator, the objects of this class are shared and never
72 // assigned after being created once
73 wxBrushRefData
& operator=(const wxBrushRefData
&);
76 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
78 // ============================================================================
79 // wxBrushRefData implementation
80 // ============================================================================
82 IMPLEMENT_DYNAMIC_CLASS(wxBrush
, wxGDIObject
)
84 // ----------------------------------------------------------------------------
85 // wxBrushRefData ctors/dtor
86 // ----------------------------------------------------------------------------
88 wxBrushRefData::wxBrushRefData(const wxColour
& colour
, wxBrushStyle style
)
96 wxBrushRefData::wxBrushRefData(const wxBitmap
& stipple
)
98 DoSetStipple(stipple
);
103 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
)
105 m_stipple(data
.m_stipple
),
106 m_colour(data
.m_colour
)
108 m_style
= data
.m_style
;
110 // we can't share HBRUSH, we'd need to create another one ourselves
114 wxBrushRefData::~wxBrushRefData()
119 // ----------------------------------------------------------------------------
120 // wxBrushRefData accesors
121 // ----------------------------------------------------------------------------
123 bool wxBrushRefData::operator==(const wxBrushRefData
& data
) const
125 // don't compare HBRUSHes
126 return m_style
== data
.m_style
&&
127 m_colour
== data
.m_colour
&&
128 m_stipple
.IsSameAs(data
.m_stipple
);
131 void wxBrushRefData::DoSetStipple(const wxBitmap
& stipple
)
134 m_style
= stipple
.GetMask() ? wxSTIPPLE_MASK_OPAQUE
: wxSTIPPLE
;
137 // ----------------------------------------------------------------------------
138 // wxBrushRefData resource handling
139 // ----------------------------------------------------------------------------
141 void wxBrushRefData::Free()
145 ::DeleteObject(m_hBrush
);
151 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
153 static int TranslateHatchStyle(int style
)
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
;
167 #endif // !__WXMICROWIN__ && !__WXWINCE__
169 HBRUSH
wxBrushRefData::GetHBRUSH()
173 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
174 int hatchStyle
= TranslateHatchStyle(m_style
);
175 if ( hatchStyle
== -1 )
176 #endif // !__WXMICROWIN__ && !__WXWINCE__
181 m_hBrush
= (HBRUSH
)::GetStockObject(NULL_BRUSH
);
185 m_hBrush
= ::CreatePatternBrush(GetHbitmapOf(m_stipple
));
188 case wxSTIPPLE_MASK_OPAQUE
:
189 m_hBrush
= ::CreatePatternBrush((HBITMAP
)m_stipple
.GetMask()
194 wxFAIL_MSG( _T("unknown brush style") );
198 m_hBrush
= ::CreateSolidBrush(m_colour
.GetPixel());
203 else // create a hatched brush
205 m_hBrush
= ::CreateHatchBrush(hatchStyle
, m_colour
.GetPixel());
211 wxLogLastError(_T("CreateXXXBrush()"));
218 // ============================================================================
219 // wxBrush implementation
220 // ============================================================================
222 // ----------------------------------------------------------------------------
223 // wxBrush ctors/dtor
224 // ----------------------------------------------------------------------------
230 wxBrush::wxBrush(const wxColour
& col
, wxBrushStyle style
)
232 m_refData
= new wxBrushRefData(col
, style
);
235 wxBrush::wxBrush(const wxBitmap
& stipple
)
237 m_refData
= new wxBrushRefData(stipple
);
244 // ----------------------------------------------------------------------------
245 // wxBrush house keeping stuff
246 // ----------------------------------------------------------------------------
248 bool wxBrush::operator==(const wxBrush
& brush
) const
250 const wxBrushRefData
*brushData
= (wxBrushRefData
*)brush
.m_refData
;
252 // an invalid brush is considered to be only equal to another invalid brush
253 return m_refData
? (brushData
&& *M_BRUSHDATA
== *brushData
) : !brushData
;
256 wxGDIRefData
*wxBrush::CreateGDIRefData() const
258 return new wxBrushRefData
;
261 wxGDIRefData
*wxBrush::CloneGDIRefData(const wxGDIRefData
*data
) const
263 return new wxBrushRefData(*(const wxBrushRefData
*)data
);
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 wxColour
wxBrush::GetColour() const
272 wxCHECK_MSG( Ok(), wxNullColour
, _T("invalid brush") );
274 return M_BRUSHDATA
->GetColour();
277 wxBrushStyle
wxBrush::GetStyle() const
279 wxCHECK_MSG( Ok(), wxBRUSHSTYLE_MAX
, _T("invalid brush") );
281 return M_BRUSHDATA
->GetStyle();
284 wxBitmap
*wxBrush::GetStipple() const
286 wxCHECK_MSG( Ok(), NULL
, _T("invalid brush") );
288 return M_BRUSHDATA
->GetStipple();
291 WXHANDLE
wxBrush::GetResourceHandle() const
293 wxCHECK_MSG( Ok(), FALSE
, _T("invalid brush") );
295 return (WXHANDLE
)M_BRUSHDATA
->GetHBRUSH();
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 void wxBrush::SetColour(const wxColour
& col
)
306 M_BRUSHDATA
->SetColour(col
);
309 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
313 M_BRUSHDATA
->SetColour(wxColour(r
, g
, b
));
316 void wxBrush::SetStyle(wxBrushStyle style
)
320 M_BRUSHDATA
->SetStyle(style
);
323 void wxBrush::SetStipple(const wxBitmap
& stipple
)
327 M_BRUSHDATA
->SetStipple(stipple
);