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