]> git.saurik.com Git - wxWidgets.git/blame - src/msw/brush.cpp
fixed wxImage->wxBitmap conversion for images with alpha channel
[wxWidgets.git] / src / msw / brush.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
cafbad8f 2// Name: src/msw/brush.cpp
2bda0e17
KB
3// Purpose: wxBrush
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
cafbad8f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2bda0e17
KB
17#pragma implementation "brush.h"
18#endif
19
cafbad8f
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
2bda0e17
KB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
cafbad8f 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
cafbad8f
VZ
32 #include "wx/list.h"
33 #include "wx/utils.h"
34 #include "wx/app.h"
35 #include "wx/brush.h"
36#endif // WX_PRECOMP
2bda0e17
KB
37
38#include "wx/msw/private.h"
39
cafbad8f
VZ
40// ----------------------------------------------------------------------------
41// private classes
42// ----------------------------------------------------------------------------
43
44class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
45{
46public:
47 wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
48 wxBrushRefData(const wxBitmap& stipple);
49 wxBrushRefData(const wxBrushRefData& data);
50 virtual ~wxBrushRefData();
51
52 bool operator==(const wxBrushRefData& data) const;
53
54 HBRUSH GetHBRUSH();
55 void Free();
56
57 const wxColour& GetColour() const { return m_colour; }
58 int GetStyle() const { return m_style; }
59 wxBitmap *GetStipple() { return &m_stipple; }
60
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); }
64
65private:
66 void DoSetStipple(const wxBitmap& stipple);
67
68 int m_style;
69 wxBitmap m_stipple;
70 wxColour m_colour;
71 HBRUSH m_hBrush;
72
73 // no assignment operator, the objects of this class are shared and never
74 // assigned after being created once
75 wxBrushRefData& operator=(const wxBrushRefData&);
76};
77
78#define M_BRUSHDATA ((wxBrushRefData *)m_refData)
79
80// ============================================================================
81// wxBrushRefData implementation
82// ============================================================================
2bda0e17 83
2bda0e17 84IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
2bda0e17 85
cafbad8f
VZ
86// ----------------------------------------------------------------------------
87// wxBrushRefData ctors/dtor
88// ----------------------------------------------------------------------------
89
90wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
91 : m_colour(colour)
2bda0e17 92{
cafbad8f
VZ
93 m_style = style;
94
95 m_hBrush = NULL;
b823f5a1
JS
96}
97
cafbad8f 98wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
b823f5a1 99{
cafbad8f
VZ
100 DoSetStipple(stipple);
101
102 m_hBrush = NULL;
2bda0e17
KB
103}
104
cafbad8f
VZ
105wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
106 : m_stipple(data.m_stipple),
107 m_colour(data.m_colour)
2bda0e17 108{
cafbad8f
VZ
109 m_style = data.m_style;
110
111 // we can't share HBRUSH, we'd need to create another one ourselves
112 m_hBrush = NULL;
2bda0e17
KB
113}
114
cafbad8f 115wxBrushRefData::~wxBrushRefData()
2bda0e17 116{
cafbad8f 117 Free();
2bda0e17
KB
118}
119
cafbad8f
VZ
120// ----------------------------------------------------------------------------
121// wxBrushRefData accesors
122// ----------------------------------------------------------------------------
123
124bool wxBrushRefData::operator==(const wxBrushRefData& data) const
2bda0e17 125{
cafbad8f
VZ
126 // don't compare HBRUSHes
127 return m_style == data.m_style &&
128 m_colour == data.m_colour &&
129 m_stipple == data.m_stipple;
2bda0e17
KB
130}
131
cafbad8f 132void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
2bda0e17 133{
cafbad8f
VZ
134 m_stipple = stipple;
135 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
136}
2bda0e17 137
cafbad8f
VZ
138// ----------------------------------------------------------------------------
139// wxBrushRefData resource handling
140// ----------------------------------------------------------------------------
2bda0e17 141
cafbad8f
VZ
142void wxBrushRefData::Free()
143{
144 if ( m_hBrush )
145 {
146 ::DeleteObject(m_hBrush);
2bda0e17 147
cafbad8f
VZ
148 m_hBrush = NULL;
149 }
2bda0e17
KB
150}
151
4676948b 152#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
35f51293
VZ
153
154static int TranslateHatchStyle(int style)
155{
cafbad8f
VZ
156 switch ( style )
157 {
cafbad8f
VZ
158 case wxBDIAGONAL_HATCH: return HS_BDIAGONAL;
159 case wxCROSSDIAG_HATCH: return HS_DIAGCROSS;
160 case wxFDIAGONAL_HATCH: return HS_FDIAGONAL;
161 case wxCROSS_HATCH: return HS_CROSS;
162 case wxHORIZONTAL_HATCH:return HS_HORIZONTAL;
163 case wxVERTICAL_HATCH: return HS_VERTICAL;
cafbad8f
VZ
164 default: return -1;
165 }
166}
167
35f51293
VZ
168#endif // !__WXMICROWIN__ && !__WXWINCE__
169
cafbad8f 170HBRUSH wxBrushRefData::GetHBRUSH()
2bda0e17 171{
cafbad8f
VZ
172 if ( !m_hBrush )
173 {
35f51293
VZ
174#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
175 int hatchStyle = TranslateHatchStyle(m_style);
cafbad8f 176 if ( hatchStyle == -1 )
35f51293 177#endif // !__WXMICROWIN__ && !__WXWINCE__
cafbad8f
VZ
178 {
179 switch ( m_style )
180 {
181 case wxTRANSPARENT:
182 m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
183 break;
184
185 case wxSTIPPLE:
186 m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple));
187 break;
188
189 case wxSTIPPLE_MASK_OPAQUE:
190 m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask()
191 ->GetMaskBitmap());
192 break;
193
194 default:
195 wxFAIL_MSG( _T("unknown brush style") );
196 // fall through
197
198 case wxSOLID:
199 m_hBrush = ::CreateSolidBrush(m_colour.GetPixel());
200 break;
201 }
202 }
4676948b 203#ifndef __WXWINCE__
cafbad8f
VZ
204 else // create a hatched brush
205 {
206 m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel());
207 }
4676948b 208#endif
cafbad8f
VZ
209
210 if ( !m_hBrush )
211 {
212 wxLogLastError(_T("CreateXXXBrush()"));
213 }
214 }
215
216 return m_hBrush;
217}
2bda0e17 218
cafbad8f
VZ
219// ============================================================================
220// wxBrush implementation
221// ============================================================================
de2d2cdc 222
cafbad8f
VZ
223// ----------------------------------------------------------------------------
224// wxBrush ctors/dtor
225// ----------------------------------------------------------------------------
2bda0e17 226
cafbad8f
VZ
227wxBrush::wxBrush()
228{
229}
2bda0e17 230
cafbad8f
VZ
231wxBrush::wxBrush(const wxColour& col, int style)
232{
233 m_refData = new wxBrushRefData(col, style);
2bda0e17
KB
234}
235
cafbad8f 236wxBrush::wxBrush(const wxBitmap& stipple)
2bda0e17 237{
cafbad8f
VZ
238 m_refData = new wxBrushRefData(stipple);
239}
2bda0e17 240
cafbad8f
VZ
241wxBrush::~wxBrush()
242{
243}
2bda0e17 244
cafbad8f
VZ
245// ----------------------------------------------------------------------------
246// wxBrush house keeping stuff
247// ----------------------------------------------------------------------------
248
249wxBrush& wxBrush::operator=(const wxBrush& brush)
250{
6c6bf502 251 if ( this != &brush )
2bda0e17 252 {
cafbad8f 253 Ref(brush);
2bda0e17 254 }
2bda0e17 255
cafbad8f 256 return *this;
2bda0e17
KB
257}
258
cafbad8f 259bool wxBrush::operator==(const wxBrush& brush) const
2bda0e17 260{
cafbad8f
VZ
261 const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData;
262
263 // an invalid brush is considered to be only equal to another invalid brush
264 return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData;
2bda0e17
KB
265}
266
cafbad8f 267wxObjectRefData *wxBrush::CreateRefData() const
2bda0e17 268{
cafbad8f 269 return new wxBrushRefData;
2bda0e17
KB
270}
271
cafbad8f 272wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
2bda0e17 273{
cafbad8f 274 return new wxBrushRefData(*(const wxBrushRefData *)data);
2bda0e17 275}
2bda0e17 276
cafbad8f
VZ
277// ----------------------------------------------------------------------------
278// wxBrush accessors
279// ----------------------------------------------------------------------------
2bda0e17 280
cafbad8f 281wxColour wxBrush::GetColour() const
2bda0e17 282{
cafbad8f 283 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
2bda0e17 284
cafbad8f
VZ
285 return M_BRUSHDATA->GetColour();
286}
287
288int wxBrush::GetStyle() const
289{
290 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
2bda0e17 291
cafbad8f 292 return M_BRUSHDATA->GetStyle();
2bda0e17
KB
293}
294
cafbad8f 295wxBitmap *wxBrush::GetStipple() const
2bda0e17 296{
cafbad8f
VZ
297 wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
298
299 return M_BRUSHDATA->GetStipple();
300}
2bda0e17 301
2b5f62a0 302WXHANDLE wxBrush::GetResourceHandle() const
cafbad8f
VZ
303{
304 wxCHECK_MSG( Ok(), FALSE, _T("invalid brush") );
2bda0e17 305
2b5f62a0 306 return (WXHANDLE)M_BRUSHDATA->GetHBRUSH();
2bda0e17
KB
307}
308
cafbad8f
VZ
309// ----------------------------------------------------------------------------
310// wxBrush setters
311// ----------------------------------------------------------------------------
312
313void wxBrush::SetColour(const wxColour& col)
2bda0e17 314{
cafbad8f 315 AllocExclusive();
2bda0e17 316
cafbad8f
VZ
317 M_BRUSHDATA->SetColour(col);
318}
2bda0e17 319
cafbad8f
VZ
320void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
321{
322 AllocExclusive();
323
324 M_BRUSHDATA->SetColour(wxColour(r, g, b));
2bda0e17
KB
325}
326
cafbad8f 327void wxBrush::SetStyle(int style)
2bda0e17 328{
cafbad8f
VZ
329 AllocExclusive();
330
331 M_BRUSHDATA->SetStyle(style);
332}
2bda0e17 333
cafbad8f
VZ
334void wxBrush::SetStipple(const wxBitmap& stipple)
335{
336 AllocExclusive();
2bda0e17 337
cafbad8f 338 M_BRUSHDATA->SetStipple(stipple);
2bda0e17
KB
339}
340
341