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
)
107 m_stipple(data
.m_stipple
),
108 m_colour(data
.m_colour
)
110 m_style
= data
.m_style
;
112 // we can't share HBRUSH, we'd need to create another one ourselves
116 wxBrushRefData::~wxBrushRefData()
121 // ----------------------------------------------------------------------------
122 // wxBrushRefData accesors
123 // ----------------------------------------------------------------------------
125 bool wxBrushRefData::operator==(const wxBrushRefData
& data
) const
127 // don't compare HBRUSHes
128 return m_style
== data
.m_style
&&
129 m_colour
== data
.m_colour
&&
130 m_stipple
== data
.m_stipple
;
133 void wxBrushRefData::DoSetStipple(const wxBitmap
& stipple
)
136 m_style
= stipple
.GetMask() ? wxSTIPPLE_MASK_OPAQUE
: wxSTIPPLE
;
139 // ----------------------------------------------------------------------------
140 // wxBrushRefData resource handling
141 // ----------------------------------------------------------------------------
143 void wxBrushRefData::Free()
147 ::DeleteObject(m_hBrush
);
153 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
155 static int TranslateHatchStyle(int style
)
159 case wxBDIAGONAL_HATCH
: return HS_BDIAGONAL
;
160 case wxCROSSDIAG_HATCH
: return HS_DIAGCROSS
;
161 case wxFDIAGONAL_HATCH
: return HS_FDIAGONAL
;
162 case wxCROSS_HATCH
: return HS_CROSS
;
163 case wxHORIZONTAL_HATCH
:return HS_HORIZONTAL
;
164 case wxVERTICAL_HATCH
: return HS_VERTICAL
;
169 #endif // !__WXMICROWIN__ && !__WXWINCE__
171 HBRUSH
wxBrushRefData::GetHBRUSH()
175 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
176 int hatchStyle
= TranslateHatchStyle(m_style
);
177 if ( hatchStyle
== -1 )
178 #endif // !__WXMICROWIN__ && !__WXWINCE__
183 m_hBrush
= (HBRUSH
)::GetStockObject(NULL_BRUSH
);
187 m_hBrush
= ::CreatePatternBrush(GetHbitmapOf(m_stipple
));
190 case wxSTIPPLE_MASK_OPAQUE
:
191 m_hBrush
= ::CreatePatternBrush((HBITMAP
)m_stipple
.GetMask()
196 wxFAIL_MSG( _T("unknown brush style") );
200 m_hBrush
= ::CreateSolidBrush(m_colour
.GetPixel());
205 else // create a hatched brush
207 m_hBrush
= ::CreateHatchBrush(hatchStyle
, m_colour
.GetPixel());
213 wxLogLastError(_T("CreateXXXBrush()"));
220 // ============================================================================
221 // wxBrush implementation
222 // ============================================================================
224 // ----------------------------------------------------------------------------
225 // wxBrush ctors/dtor
226 // ----------------------------------------------------------------------------
232 wxBrush::wxBrush(const wxColour
& col
, int style
)
234 m_refData
= new wxBrushRefData(col
, style
);
237 wxBrush::wxBrush(const wxBitmap
& stipple
)
239 m_refData
= new wxBrushRefData(stipple
);
246 // ----------------------------------------------------------------------------
247 // wxBrush house keeping stuff
248 // ----------------------------------------------------------------------------
250 wxBrush
& wxBrush::operator=(const wxBrush
& brush
)
252 if ( this != &brush
)
260 bool wxBrush::operator==(const wxBrush
& brush
) const
262 const wxBrushRefData
*brushData
= (wxBrushRefData
*)brush
.m_refData
;
264 // an invalid brush is considered to be only equal to another invalid brush
265 return m_refData
? (brushData
&& *M_BRUSHDATA
== *brushData
) : !brushData
;
268 wxObjectRefData
*wxBrush::CreateRefData() const
270 return new wxBrushRefData
;
273 wxObjectRefData
*wxBrush::CloneRefData(const wxObjectRefData
*data
) const
275 return new wxBrushRefData(*(const wxBrushRefData
*)data
);
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 wxColour
wxBrush::GetColour() const
284 wxCHECK_MSG( Ok(), wxNullColour
, _T("invalid brush") );
286 return M_BRUSHDATA
->GetColour();
289 int wxBrush::GetStyle() const
291 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
293 return M_BRUSHDATA
->GetStyle();
296 wxBitmap
*wxBrush::GetStipple() const
298 wxCHECK_MSG( Ok(), NULL
, _T("invalid brush") );
300 return M_BRUSHDATA
->GetStipple();
303 WXHANDLE
wxBrush::GetResourceHandle() const
305 wxCHECK_MSG( Ok(), FALSE
, _T("invalid brush") );
307 return (WXHANDLE
)M_BRUSHDATA
->GetHBRUSH();
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 void wxBrush::SetColour(const wxColour
& col
)
318 M_BRUSHDATA
->SetColour(col
);
321 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
325 M_BRUSHDATA
->SetColour(wxColour(r
, g
, b
));
328 void wxBrush::SetStyle(int style
)
332 M_BRUSHDATA
->SetStyle(style
);
335 void wxBrush::SetStipple(const wxBitmap
& stipple
)
339 M_BRUSHDATA
->SetStipple(stipple
);