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"
34 #include "wx/msw/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class WXDLLEXPORT wxBrushRefData
: public wxGDIRefData
43 wxBrushRefData(const wxColour
& colour
= wxNullColour
, int style
= wxSOLID
);
44 wxBrushRefData(const wxBitmap
& stipple
);
45 wxBrushRefData(const wxBrushRefData
& data
);
46 virtual ~wxBrushRefData();
48 bool operator==(const wxBrushRefData
& data
) const;
53 const wxColour
& GetColour() const { return m_colour
; }
54 int GetStyle() const { return m_style
; }
55 wxBitmap
*GetStipple() { return &m_stipple
; }
57 void SetColour(const wxColour
& colour
) { Free(); m_colour
= colour
; }
58 void SetStyle(int style
) { Free(); m_style
= style
; }
59 void SetStipple(const wxBitmap
& stipple
) { Free(); DoSetStipple(stipple
); }
62 void DoSetStipple(const wxBitmap
& stipple
);
69 // no assignment operator, the objects of this class are shared and never
70 // assigned after being created once
71 wxBrushRefData
& operator=(const wxBrushRefData
&);
74 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
76 // ============================================================================
77 // wxBrushRefData implementation
78 // ============================================================================
80 IMPLEMENT_DYNAMIC_CLASS(wxBrush
, wxGDIObject
)
82 // ----------------------------------------------------------------------------
83 // wxBrushRefData ctors/dtor
84 // ----------------------------------------------------------------------------
86 wxBrushRefData::wxBrushRefData(const wxColour
& colour
, int style
)
94 wxBrushRefData::wxBrushRefData(const wxBitmap
& stipple
)
96 DoSetStipple(stipple
);
101 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
)
103 m_stipple(data
.m_stipple
),
104 m_colour(data
.m_colour
)
106 m_style
= data
.m_style
;
108 // we can't share HBRUSH, we'd need to create another one ourselves
112 wxBrushRefData::~wxBrushRefData()
117 // ----------------------------------------------------------------------------
118 // wxBrushRefData accesors
119 // ----------------------------------------------------------------------------
121 bool wxBrushRefData::operator==(const wxBrushRefData
& data
) const
123 // don't compare HBRUSHes
124 return m_style
== data
.m_style
&&
125 m_colour
== data
.m_colour
&&
126 m_stipple
== data
.m_stipple
;
129 void wxBrushRefData::DoSetStipple(const wxBitmap
& stipple
)
132 m_style
= stipple
.GetMask() ? wxSTIPPLE_MASK_OPAQUE
: wxSTIPPLE
;
135 // ----------------------------------------------------------------------------
136 // wxBrushRefData resource handling
137 // ----------------------------------------------------------------------------
139 void wxBrushRefData::Free()
143 ::DeleteObject(m_hBrush
);
149 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
151 static int TranslateHatchStyle(int style
)
155 case wxBDIAGONAL_HATCH
: return HS_BDIAGONAL
;
156 case wxCROSSDIAG_HATCH
: return HS_DIAGCROSS
;
157 case wxFDIAGONAL_HATCH
: return HS_FDIAGONAL
;
158 case wxCROSS_HATCH
: return HS_CROSS
;
159 case wxHORIZONTAL_HATCH
:return HS_HORIZONTAL
;
160 case wxVERTICAL_HATCH
: return HS_VERTICAL
;
165 #endif // !__WXMICROWIN__ && !__WXWINCE__
167 HBRUSH
wxBrushRefData::GetHBRUSH()
171 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
172 int hatchStyle
= TranslateHatchStyle(m_style
);
173 if ( hatchStyle
== -1 )
174 #endif // !__WXMICROWIN__ && !__WXWINCE__
179 m_hBrush
= (HBRUSH
)::GetStockObject(NULL_BRUSH
);
183 m_hBrush
= ::CreatePatternBrush(GetHbitmapOf(m_stipple
));
186 case wxSTIPPLE_MASK_OPAQUE
:
187 m_hBrush
= ::CreatePatternBrush((HBITMAP
)m_stipple
.GetMask()
192 wxFAIL_MSG( _T("unknown brush style") );
196 m_hBrush
= ::CreateSolidBrush(m_colour
.GetPixel());
201 else // create a hatched brush
203 m_hBrush
= ::CreateHatchBrush(hatchStyle
, m_colour
.GetPixel());
209 wxLogLastError(_T("CreateXXXBrush()"));
216 // ============================================================================
217 // wxBrush implementation
218 // ============================================================================
220 // ----------------------------------------------------------------------------
221 // wxBrush ctors/dtor
222 // ----------------------------------------------------------------------------
228 wxBrush::wxBrush(const wxColour
& col
, int style
)
230 m_refData
= new wxBrushRefData(col
, style
);
233 wxBrush::wxBrush(const wxBitmap
& stipple
)
235 m_refData
= new wxBrushRefData(stipple
);
242 // ----------------------------------------------------------------------------
243 // wxBrush house keeping stuff
244 // ----------------------------------------------------------------------------
246 bool wxBrush::operator==(const wxBrush
& brush
) const
248 const wxBrushRefData
*brushData
= (wxBrushRefData
*)brush
.m_refData
;
250 // an invalid brush is considered to be only equal to another invalid brush
251 return m_refData
? (brushData
&& *M_BRUSHDATA
== *brushData
) : !brushData
;
254 wxObjectRefData
*wxBrush::CreateRefData() const
256 return new wxBrushRefData
;
259 wxObjectRefData
*wxBrush::CloneRefData(const wxObjectRefData
*data
) const
261 return new wxBrushRefData(*(const wxBrushRefData
*)data
);
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 wxColour
wxBrush::GetColour() const
270 wxCHECK_MSG( Ok(), wxNullColour
, _T("invalid brush") );
272 return M_BRUSHDATA
->GetColour();
275 int wxBrush::GetStyle() const
277 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
279 return M_BRUSHDATA
->GetStyle();
282 wxBitmap
*wxBrush::GetStipple() const
284 wxCHECK_MSG( Ok(), NULL
, _T("invalid brush") );
286 return M_BRUSHDATA
->GetStipple();
289 WXHANDLE
wxBrush::GetResourceHandle() const
291 wxCHECK_MSG( Ok(), FALSE
, _T("invalid brush") );
293 return (WXHANDLE
)M_BRUSHDATA
->GetHBRUSH();
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 void wxBrush::SetColour(const wxColour
& col
)
304 M_BRUSHDATA
->SetColour(col
);
307 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
311 M_BRUSHDATA
->SetColour(wxColour(r
, g
, b
));
314 void wxBrush::SetStyle(int style
)
318 M_BRUSHDATA
->SetStyle(style
);
321 void wxBrush::SetStipple(const wxBitmap
& stipple
)
325 M_BRUSHDATA
->SetStipple(stipple
);