1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/brush.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
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
)
156 #ifndef __WXMICROWIN__
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
;
163 #endif // __WXMICROWIN__
168 HBRUSH
wxBrushRefData::GetHBRUSH()
172 int hatchStyle
= TransllateHatchStyle(m_style
);
173 if ( hatchStyle
== -1 )
178 m_hBrush
= (HBRUSH
)::GetStockObject(NULL_BRUSH
);
182 m_hBrush
= ::CreatePatternBrush(GetHbitmapOf(m_stipple
));
185 case wxSTIPPLE_MASK_OPAQUE
:
186 m_hBrush
= ::CreatePatternBrush((HBITMAP
)m_stipple
.GetMask()
191 wxFAIL_MSG( _T("unknown brush style") );
195 m_hBrush
= ::CreateSolidBrush(m_colour
.GetPixel());
199 else // create a hatched brush
201 m_hBrush
= ::CreateHatchBrush(hatchStyle
, m_colour
.GetPixel());
206 wxLogLastError(_T("CreateXXXBrush()"));
213 // ============================================================================
214 // wxBrush implementation
215 // ============================================================================
217 // ----------------------------------------------------------------------------
218 // wxBrush ctors/dtor
219 // ----------------------------------------------------------------------------
225 wxBrush::wxBrush(const wxColour
& col
, int style
)
227 m_refData
= new wxBrushRefData(col
, style
);
230 wxBrush::wxBrush(const wxBitmap
& stipple
)
232 m_refData
= new wxBrushRefData(stipple
);
239 // ----------------------------------------------------------------------------
240 // wxBrush house keeping stuff
241 // ----------------------------------------------------------------------------
243 wxBrush
& wxBrush::operator=(const wxBrush
& brush
)
245 if ( *this != brush
)
253 bool wxBrush::operator==(const wxBrush
& brush
) const
255 const wxBrushRefData
*brushData
= (wxBrushRefData
*)brush
.m_refData
;
257 // an invalid brush is considered to be only equal to another invalid brush
258 return m_refData
? (brushData
&& *M_BRUSHDATA
== *brushData
) : !brushData
;
261 wxObjectRefData
*wxBrush::CreateRefData() const
263 return new wxBrushRefData
;
266 wxObjectRefData
*wxBrush::CloneRefData(const wxObjectRefData
*data
) const
268 return new wxBrushRefData(*(const wxBrushRefData
*)data
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 wxColour
wxBrush::GetColour() const
277 wxCHECK_MSG( Ok(), wxNullColour
, _T("invalid brush") );
279 return M_BRUSHDATA
->GetColour();
282 int wxBrush::GetStyle() const
284 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
286 return M_BRUSHDATA
->GetStyle();
289 wxBitmap
*wxBrush::GetStipple() const
291 wxCHECK_MSG( Ok(), NULL
, _T("invalid brush") );
293 return M_BRUSHDATA
->GetStipple();
296 WXHANDLE
wxBrush::GetResourceHandle() const
298 wxCHECK_MSG( Ok(), FALSE
, _T("invalid brush") );
300 return (WXHANDLE
)M_BRUSHDATA
->GetHBRUSH();
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 void wxBrush::SetColour(const wxColour
& col
)
311 M_BRUSHDATA
->SetColour(col
);
314 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
318 M_BRUSHDATA
->SetColour(wxColour(r
, g
, b
));
321 void wxBrush::SetStyle(int style
)
325 M_BRUSHDATA
->SetStyle(style
);
328 void wxBrush::SetStipple(const wxBitmap
& stipple
)
332 M_BRUSHDATA
->SetStipple(stipple
);