]> git.saurik.com Git - wxWidgets.git/blame - src/msw/brush.cpp
added InitCommandEventWithItems() and call it from wxChoice and wxComboBox code to...
[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
cafbad8f 246bool wxBrush::operator==(const wxBrush& brush) const
2bda0e17 247{
cafbad8f
VZ
248 const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData;
249
250 // an invalid brush is considered to be only equal to another invalid brush
251 return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData;
2bda0e17
KB
252}
253
cafbad8f 254wxObjectRefData *wxBrush::CreateRefData() const
2bda0e17 255{
cafbad8f 256 return new wxBrushRefData;
2bda0e17
KB
257}
258
cafbad8f 259wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
2bda0e17 260{
cafbad8f 261 return new wxBrushRefData(*(const wxBrushRefData *)data);
2bda0e17 262}
2bda0e17 263
cafbad8f
VZ
264// ----------------------------------------------------------------------------
265// wxBrush accessors
266// ----------------------------------------------------------------------------
2bda0e17 267
cafbad8f 268wxColour wxBrush::GetColour() const
2bda0e17 269{
cafbad8f 270 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
2bda0e17 271
cafbad8f
VZ
272 return M_BRUSHDATA->GetColour();
273}
274
275int wxBrush::GetStyle() const
276{
277 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
2bda0e17 278
cafbad8f 279 return M_BRUSHDATA->GetStyle();
2bda0e17
KB
280}
281
cafbad8f 282wxBitmap *wxBrush::GetStipple() const
2bda0e17 283{
cafbad8f
VZ
284 wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
285
286 return M_BRUSHDATA->GetStipple();
287}
2bda0e17 288
2b5f62a0 289WXHANDLE wxBrush::GetResourceHandle() const
cafbad8f
VZ
290{
291 wxCHECK_MSG( Ok(), FALSE, _T("invalid brush") );
2bda0e17 292
2b5f62a0 293 return (WXHANDLE)M_BRUSHDATA->GetHBRUSH();
2bda0e17
KB
294}
295
cafbad8f
VZ
296// ----------------------------------------------------------------------------
297// wxBrush setters
298// ----------------------------------------------------------------------------
299
300void wxBrush::SetColour(const wxColour& col)
2bda0e17 301{
cafbad8f 302 AllocExclusive();
2bda0e17 303
cafbad8f
VZ
304 M_BRUSHDATA->SetColour(col);
305}
2bda0e17 306
1a1498c0 307void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
cafbad8f
VZ
308{
309 AllocExclusive();
310
311 M_BRUSHDATA->SetColour(wxColour(r, g, b));
2bda0e17
KB
312}
313
cafbad8f 314void wxBrush::SetStyle(int style)
2bda0e17 315{
cafbad8f
VZ
316 AllocExclusive();
317
318 M_BRUSHDATA->SetStyle(style);
319}
2bda0e17 320
cafbad8f
VZ
321void wxBrush::SetStipple(const wxBitmap& stipple)
322{
323 AllocExclusive();
2bda0e17 324
cafbad8f 325 M_BRUSHDATA->SetStipple(stipple);
2bda0e17 326}