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