]> git.saurik.com Git - wxWidgets.git/blame - src/msw/brush.cpp
preserve type when loaded image is rescaled, #11543
[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
cafbad8f
VZ
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
cafbad8f 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
c64755ed
WS
27#include "wx/brush.h"
28
2bda0e17 29#ifndef WX_PRECOMP
cafbad8f
VZ
30 #include "wx/list.h"
31 #include "wx/utils.h"
32 #include "wx/app.h"
ed7ec76d 33 #include "wx/bitmap.h"
cafbad8f 34#endif // WX_PRECOMP
2bda0e17
KB
35
36#include "wx/msw/private.h"
37
cafbad8f
VZ
38// ----------------------------------------------------------------------------
39// private classes
40// ----------------------------------------------------------------------------
41
42class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
43{
44public:
3e6858cd 45 wxBrushRefData(const wxColour& colour = wxNullColour, wxBrushStyle style = wxBRUSHSTYLE_SOLID);
cafbad8f
VZ
46 wxBrushRefData(const wxBitmap& stipple);
47 wxBrushRefData(const wxBrushRefData& data);
48 virtual ~wxBrushRefData();
49
50 bool operator==(const wxBrushRefData& data) const;
51
52 HBRUSH GetHBRUSH();
53 void Free();
54
55 const wxColour& GetColour() const { return m_colour; }
3e6858cd 56 wxBrushStyle GetStyle() const { return m_style; }
cafbad8f
VZ
57 wxBitmap *GetStipple() { return &m_stipple; }
58
59 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
3e6858cd 60 void SetStyle(wxBrushStyle style) { Free(); m_style = style; }
cafbad8f
VZ
61 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
62
63private:
64 void DoSetStipple(const wxBitmap& stipple);
65
3e6858cd 66 wxBrushStyle m_style;
cafbad8f
VZ
67 wxBitmap m_stipple;
68 wxColour m_colour;
69 HBRUSH m_hBrush;
70
71 // no assignment operator, the objects of this class are shared and never
72 // assigned after being created once
73 wxBrushRefData& operator=(const wxBrushRefData&);
74};
75
76#define M_BRUSHDATA ((wxBrushRefData *)m_refData)
77
78// ============================================================================
79// wxBrushRefData implementation
80// ============================================================================
2bda0e17 81
2bda0e17 82IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
2bda0e17 83
cafbad8f
VZ
84// ----------------------------------------------------------------------------
85// wxBrushRefData ctors/dtor
86// ----------------------------------------------------------------------------
87
3e6858cd 88wxBrushRefData::wxBrushRefData(const wxColour& colour, wxBrushStyle style)
cafbad8f 89 : m_colour(colour)
2bda0e17 90{
cafbad8f
VZ
91 m_style = style;
92
93 m_hBrush = NULL;
b823f5a1
JS
94}
95
cafbad8f 96wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
b823f5a1 97{
cafbad8f
VZ
98 DoSetStipple(stipple);
99
100 m_hBrush = NULL;
2bda0e17
KB
101}
102
cafbad8f 103wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
04a18b0d
WS
104 : wxGDIRefData(),
105 m_stipple(data.m_stipple),
cafbad8f 106 m_colour(data.m_colour)
2bda0e17 107{
cafbad8f
VZ
108 m_style = data.m_style;
109
110 // we can't share HBRUSH, we'd need to create another one ourselves
111 m_hBrush = NULL;
2bda0e17
KB
112}
113
cafbad8f 114wxBrushRefData::~wxBrushRefData()
2bda0e17 115{
cafbad8f 116 Free();
2bda0e17
KB
117}
118
cafbad8f
VZ
119// ----------------------------------------------------------------------------
120// wxBrushRefData accesors
121// ----------------------------------------------------------------------------
122
123bool wxBrushRefData::operator==(const wxBrushRefData& data) const
2bda0e17 124{
cafbad8f
VZ
125 // don't compare HBRUSHes
126 return m_style == data.m_style &&
127 m_colour == data.m_colour &&
a3ab1c18 128 m_stipple.IsSameAs(data.m_stipple);
2bda0e17
KB
129}
130
cafbad8f 131void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
2bda0e17 132{
cafbad8f 133 m_stipple = stipple;
cb129171
FM
134 m_style = stipple.GetMask() ? wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
135 : wxBRUSHSTYLE_STIPPLE;
cafbad8f 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 {
cb129171
FM
158 case wxBRUSHSTYLE_BDIAGONAL_HATCH: return HS_BDIAGONAL;
159 case wxBRUSHSTYLE_CROSSDIAG_HATCH: return HS_DIAGCROSS;
160 case wxBRUSHSTYLE_FDIAGONAL_HATCH: return HS_FDIAGONAL;
161 case wxBRUSHSTYLE_CROSS_HATCH: return HS_CROSS;
162 case wxBRUSHSTYLE_HORIZONTAL_HATCH:return HS_HORIZONTAL;
163 case wxBRUSHSTYLE_VERTICAL_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 {
cb129171 181 case wxBRUSHSTYLE_TRANSPARENT:
cafbad8f
VZ
182 m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
183 break;
184
cb129171 185 case wxBRUSHSTYLE_STIPPLE:
cafbad8f
VZ
186 m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple));
187 break;
188
cb129171 189 case wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE:
cafbad8f
VZ
190 m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask()
191 ->GetMaskBitmap());
192 break;
193
194 default:
9a83f860 195 wxFAIL_MSG( wxT("unknown brush style") );
cafbad8f
VZ
196 // fall through
197
cb129171 198 case wxBRUSHSTYLE_SOLID:
cafbad8f
VZ
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 {
9a83f860 212 wxLogLastError(wxT("CreateXXXBrush()"));
cafbad8f
VZ
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
3e6858cd 231wxBrush::wxBrush(const wxColour& col, wxBrushStyle style)
cafbad8f
VZ
232{
233 m_refData = new wxBrushRefData(col, style);
2bda0e17
KB
234}
235
ac3688c0
FM
236#if FUTURE_WXWIN_COMPATIBILITY_3_0
237wxBrush::wxBrush(const wxColour& col, int style)
238{
239 m_refData = new wxBrushRefData(col, (wxBrushStyle)style);
240}
241#endif
242
cafbad8f 243wxBrush::wxBrush(const wxBitmap& stipple)
2bda0e17 244{
cafbad8f
VZ
245 m_refData = new wxBrushRefData(stipple);
246}
2bda0e17 247
cafbad8f
VZ
248wxBrush::~wxBrush()
249{
250}
2bda0e17 251
cafbad8f
VZ
252// ----------------------------------------------------------------------------
253// wxBrush house keeping stuff
254// ----------------------------------------------------------------------------
255
cafbad8f 256bool wxBrush::operator==(const wxBrush& brush) const
2bda0e17 257{
cafbad8f
VZ
258 const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData;
259
260 // an invalid brush is considered to be only equal to another invalid brush
261 return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData;
2bda0e17
KB
262}
263
8f884a0d 264wxGDIRefData *wxBrush::CreateGDIRefData() const
2bda0e17 265{
cafbad8f 266 return new wxBrushRefData;
2bda0e17
KB
267}
268
8f884a0d 269wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
2bda0e17 270{
cafbad8f 271 return new wxBrushRefData(*(const wxBrushRefData *)data);
2bda0e17 272}
2bda0e17 273
cafbad8f
VZ
274// ----------------------------------------------------------------------------
275// wxBrush accessors
276// ----------------------------------------------------------------------------
2bda0e17 277
cafbad8f 278wxColour wxBrush::GetColour() const
2bda0e17 279{
9a83f860 280 wxCHECK_MSG( Ok(), wxNullColour, wxT("invalid brush") );
2bda0e17 281
cafbad8f
VZ
282 return M_BRUSHDATA->GetColour();
283}
284
3e6858cd 285wxBrushStyle wxBrush::GetStyle() const
cafbad8f 286{
9a83f860 287 wxCHECK_MSG( Ok(), wxBRUSHSTYLE_INVALID, wxT("invalid brush") );
2bda0e17 288
cafbad8f 289 return M_BRUSHDATA->GetStyle();
2bda0e17
KB
290}
291
cafbad8f 292wxBitmap *wxBrush::GetStipple() const
2bda0e17 293{
9a83f860 294 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
cafbad8f
VZ
295
296 return M_BRUSHDATA->GetStipple();
297}
2bda0e17 298
2b5f62a0 299WXHANDLE wxBrush::GetResourceHandle() const
cafbad8f 300{
9a83f860 301 wxCHECK_MSG( Ok(), FALSE, wxT("invalid brush") );
2bda0e17 302
2b5f62a0 303 return (WXHANDLE)M_BRUSHDATA->GetHBRUSH();
2bda0e17
KB
304}
305
cafbad8f
VZ
306// ----------------------------------------------------------------------------
307// wxBrush setters
308// ----------------------------------------------------------------------------
309
310void wxBrush::SetColour(const wxColour& col)
2bda0e17 311{
cafbad8f 312 AllocExclusive();
2bda0e17 313
cafbad8f
VZ
314 M_BRUSHDATA->SetColour(col);
315}
2bda0e17 316
1a1498c0 317void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
cafbad8f
VZ
318{
319 AllocExclusive();
320
321 M_BRUSHDATA->SetColour(wxColour(r, g, b));
2bda0e17
KB
322}
323
3e6858cd 324void wxBrush::SetStyle(wxBrushStyle style)
2bda0e17 325{
cafbad8f
VZ
326 AllocExclusive();
327
328 M_BRUSHDATA->SetStyle(style);
329}
2bda0e17 330
cafbad8f
VZ
331void wxBrush::SetStipple(const wxBitmap& stipple)
332{
333 AllocExclusive();
2bda0e17 334
cafbad8f 335 M_BRUSHDATA->SetStipple(stipple);
2bda0e17 336}