1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/brush.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "brush.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/msw/private.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxBrushRefData
: public wxGDIRefData
47 wxBrushRefData(const wxColour
& colour
= wxNullColour
, int style
= wxSOLID
);
48 wxBrushRefData(const wxBitmap
& stipple
);
49 wxBrushRefData(const wxBrushRefData
& data
);
50 virtual ~wxBrushRefData();
52 bool operator==(const wxBrushRefData
& data
) const;
57 const wxColour
& GetColour() const { return m_colour
; }
58 int GetStyle() const { return m_style
; }
59 wxBitmap
*GetStipple() { return &m_stipple
; }
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
); }
66 void DoSetStipple(const wxBitmap
& stipple
);
73 // no assignment operator, the objects of this class are shared and never
74 // assigned after being created once
75 wxBrushRefData
& operator=(const wxBrushRefData
&);
78 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
80 // ============================================================================
81 // wxBrushRefData implementation
82 // ============================================================================
84 IMPLEMENT_DYNAMIC_CLASS(wxBrush
, wxGDIObject
)
86 // ----------------------------------------------------------------------------
87 // wxBrushRefData ctors/dtor
88 // ----------------------------------------------------------------------------
90 wxBrushRefData::wxBrushRefData(const wxColour
& colour
, int style
)
98 wxBrushRefData::wxBrushRefData(const wxBitmap
& stipple
)
100 DoSetStipple(stipple
);
105 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
)
106 : m_stipple(data
.m_stipple
),
107 m_colour(data
.m_colour
)
109 m_style
= data
.m_style
;
111 // we can't share HBRUSH, we'd need to create another one ourselves
115 wxBrushRefData::~wxBrushRefData()
120 // ----------------------------------------------------------------------------
121 // wxBrushRefData accesors
122 // ----------------------------------------------------------------------------
124 bool wxBrushRefData::operator==(const wxBrushRefData
& data
) const
126 // don't compare HBRUSHes
127 return m_style
== data
.m_style
&&
128 m_colour
== data
.m_colour
&&
129 m_stipple
== data
.m_stipple
;
132 void wxBrushRefData::DoSetStipple(const wxBitmap
& stipple
)
135 m_style
= stipple
.GetMask() ? wxSTIPPLE_MASK_OPAQUE
: wxSTIPPLE
;
138 // ----------------------------------------------------------------------------
139 // wxBrushRefData resource handling
140 // ----------------------------------------------------------------------------
142 void wxBrushRefData::Free()
146 ::DeleteObject(m_hBrush
);
152 static int TransllateHatchStyle(int style
)
154 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
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
;
165 #else // __WXMICROWIN__
170 HBRUSH
wxBrushRefData::GetHBRUSH()
175 int hatchStyle
= TransllateHatchStyle(m_style
);
176 if ( hatchStyle
== -1 )
182 m_hBrush
= (HBRUSH
)::GetStockObject(NULL_BRUSH
);
186 m_hBrush
= ::CreatePatternBrush(GetHbitmapOf(m_stipple
));
189 case wxSTIPPLE_MASK_OPAQUE
:
190 m_hBrush
= ::CreatePatternBrush((HBITMAP
)m_stipple
.GetMask()
195 wxFAIL_MSG( _T("unknown brush style") );
199 m_hBrush
= ::CreateSolidBrush(m_colour
.GetPixel());
204 else // create a hatched brush
206 m_hBrush
= ::CreateHatchBrush(hatchStyle
, m_colour
.GetPixel());
212 wxLogLastError(_T("CreateXXXBrush()"));
219 // ============================================================================
220 // wxBrush implementation
221 // ============================================================================
223 // ----------------------------------------------------------------------------
224 // wxBrush ctors/dtor
225 // ----------------------------------------------------------------------------
231 wxBrush::wxBrush(const wxColour
& col
, int style
)
233 m_refData
= new wxBrushRefData(col
, style
);
236 wxBrush::wxBrush(const wxBitmap
& stipple
)
238 m_refData
= new wxBrushRefData(stipple
);
245 // ----------------------------------------------------------------------------
246 // wxBrush house keeping stuff
247 // ----------------------------------------------------------------------------
249 wxBrush
& wxBrush::operator=(const wxBrush
& brush
)
251 if ( *this != brush
)
259 bool wxBrush::operator==(const wxBrush
& brush
) const
261 const wxBrushRefData
*brushData
= (wxBrushRefData
*)brush
.m_refData
;
263 // an invalid brush is considered to be only equal to another invalid brush
264 return m_refData
? (brushData
&& *M_BRUSHDATA
== *brushData
) : !brushData
;
267 wxObjectRefData
*wxBrush::CreateRefData() const
269 return new wxBrushRefData
;
272 wxObjectRefData
*wxBrush::CloneRefData(const wxObjectRefData
*data
) const
274 return new wxBrushRefData(*(const wxBrushRefData
*)data
);
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 wxColour
wxBrush::GetColour() const
283 wxCHECK_MSG( Ok(), wxNullColour
, _T("invalid brush") );
285 return M_BRUSHDATA
->GetColour();
288 int wxBrush::GetStyle() const
290 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
292 return M_BRUSHDATA
->GetStyle();
295 wxBitmap
*wxBrush::GetStipple() const
297 wxCHECK_MSG( Ok(), NULL
, _T("invalid brush") );
299 return M_BRUSHDATA
->GetStipple();
302 WXHANDLE
wxBrush::GetResourceHandle() const
304 wxCHECK_MSG( Ok(), FALSE
, _T("invalid brush") );
306 return (WXHANDLE
)M_BRUSHDATA
->GetHBRUSH();
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 void wxBrush::SetColour(const wxColour
& col
)
317 M_BRUSHDATA
->SetColour(col
);
320 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
324 M_BRUSHDATA
->SetColour(wxColour(r
, g
, b
));
327 void wxBrush::SetStyle(int style
)
331 M_BRUSHDATA
->SetStyle(style
);
334 void wxBrush::SetStipple(const wxBitmap
& stipple
)
338 M_BRUSHDATA
->SetStipple(stipple
);