]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/brush.cpp
Forgot header with OSX prefix
[wxWidgets.git] / src / mgl / brush.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
46562151 2// Name: src/mgl/brush.cpp
32b8ec41
VZ
3// Purpose:
4// Author: Vaclav Slavik
5// Id: $Id$
c41c20a5 6// Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
46562151 7// Licence: wxWindows licence
32b8ec41
VZ
8/////////////////////////////////////////////////////////////////////////////
9
32b8ec41
VZ
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
16
17#include "wx/brush.h"
18#include "wx/mgl/private.h"
51b7f946 19#include "wx/dcmemory.h"
32b8ec41
VZ
20
21
22// ---------------------------------------------------------------------------
23// helper functions
24// ---------------------------------------------------------------------------
25
26// This function converts wxBitmap into pixpattern24_t representation
27// (used by wxBrush and wxPen)
28
46562151 29void wxBitmapToPixPattern(const wxBitmap& bitmap,
32b8ec41
VZ
30 pixpattern24_t *pix, pattern_t *mask)
31{
32 wxMemoryDC mem;
33 MGLDevCtx *dc;
34 int x, y;
46562151 35
32b8ec41
VZ
36 if ( pix != NULL )
37 {
cf6824d9 38 mem.SelectObjectAsSource(bitmap);
32b8ec41
VZ
39 dc = mem.GetMGLDC();
40 wxCurrentDCSwitcher curDC(dc);
41 dc->beginPixel();
42 for (y = 0; y < 8; y++)
43 for (x = 0; x < 8; x++)
46562151 44 dc->unpackColorFast(dc->getPixelFast(x, y),
32b8ec41 45 pix->p[y][x][2],
46562151 46 pix->p[y][x][1],
32b8ec41
VZ
47 pix->p[y][x][0]);
48 dc->endPixel();
49 }
50
51 if ( mask && bitmap.GetMask() )
52 {
cf6824d9 53 mem.SelectObjectAsSource(bitmap.GetMask()->GetBitmap());
32b8ec41
VZ
54 dc = mem.GetMGLDC();
55 wxCurrentDCSwitcher curDC(dc);
56 dc->beginPixel();
57 for (y = 0; y < 8; y++)
58 {
59 mask->p[y] = 0;
60 for (x = 0; x < 8; x++)
61 if ( dc->getPixelFast(x, y) != 0 )
127eab18 62 mask->p[y] = (uchar)(mask->p[y] | (1 << (7 - x)));
32b8ec41
VZ
63 }
64 dc->endPixel();
65 }
66}
67
68
69//-----------------------------------------------------------------------------
70// wxBrush
71//-----------------------------------------------------------------------------
72
8f884a0d 73class wxBrushRefData : public wxGDIRefData
32b8ec41
VZ
74{
75public:
76 wxBrushRefData();
77 wxBrushRefData(const wxBrushRefData& data);
78
8f884a0d
VZ
79 virtual bool IsOk() const { return m_colour.IsOk(); }
80
81 bool operator==(const wxBrushRefData& data) const
55ccdb93
VZ
82 {
83 return (m_style == data.m_style &&
a3ab1c18 84 m_stipple.IsSameAs(data.m_stipple) &&
55ccdb93
VZ
85 m_colour == data.m_colour);
86 }
87
3e6858cd 88 wxBrushStyle m_style;
32b8ec41
VZ
89 wxColour m_colour;
90 wxBitmap m_stipple;
91 pixpattern24_t m_pixPattern;
92 pattern_t m_maskPattern;
93};
94
95wxBrushRefData::wxBrushRefData()
96{
97 m_style = 0;
98
99 int x, y, c;
100 for (y = 0; y < 8; y++)
101 for (x = 0; x < 8; x++)
102 for (c = 0; c < 3; c++)
103 m_pixPattern.p[y][x][c] = 0;
104 for (y = 0; y < 8; y++)
105 m_maskPattern.p[y] = 0;
106}
107
108wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
109{
110 m_style = data.m_style;
111 m_stipple = data.m_stipple;
112 m_colour = data.m_colour;
113
114 int x, y, c;
115 for (y = 0; y < 8; y++)
116 for (x = 0; x < 8; x++)
117 for (c = 0; c < 3; c++)
118 m_pixPattern.p[y][x][c] = data.m_pixPattern.p[y][x][c];
119 for (y = 0; y < 8; y++)
120 m_maskPattern.p[y] = data.m_maskPattern.p[y];
121}
122
123//-----------------------------------------------------------------------------
124
125#define M_BRUSHDATA ((wxBrushRefData *)m_refData)
126
127IMPLEMENT_DYNAMIC_CLASS(wxBrush,wxGDIObject)
128
3e6858cd 129wxBrush::wxBrush(const wxColour &colour, wxBrushStyle style)
32b8ec41
VZ
130{
131 m_refData = new wxBrushRefData();
132 M_BRUSHDATA->m_style = style;
133 M_BRUSHDATA->m_colour = colour;
32b8ec41
VZ
134}
135
ac3688c0
FM
136#if FUTURE_WXWIN_COMPATIBILITY_3_0
137wxBrush::wxBrush(const wxColour& col, int style)
138{
139 m_refData = new wxBrushRefData;
140 M_BRUSHDATA->m_style = (wxBrushStyle)style;
141 M_BRUSHDATA->m_colour = colour;
142}
143#endif
144
32b8ec41
VZ
145wxBrush::wxBrush(const wxBitmap &stippleBitmap)
146{
147 wxCHECK_RET( stippleBitmap.Ok(), _T("invalid bitmap") );
46562151 148 wxCHECK_RET( stippleBitmap.GetWidth() == 8 && stippleBitmap.GetHeight() == 8,
32b8ec41
VZ
149 _T("stipple bitmap must be 8x8") );
150
151 m_refData = new wxBrushRefData();
152 M_BRUSHDATA->m_colour = *wxBLACK;
46562151 153
32b8ec41 154 M_BRUSHDATA->m_stipple = stippleBitmap;
46562151 155 wxBitmapToPixPattern(stippleBitmap, &(M_BRUSHDATA->m_pixPattern),
32b8ec41
VZ
156 &(M_BRUSHDATA->m_maskPattern));
157
158 if (M_BRUSHDATA->m_stipple.GetMask())
46562151
WS
159 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
160 else
161 M_BRUSHDATA->m_style = wxSTIPPLE;
32b8ec41
VZ
162}
163
32b8ec41
VZ
164bool wxBrush::operator == (const wxBrush& brush) const
165{
55ccdb93
VZ
166 if (m_refData == brush.m_refData) return true;
167
168 if (!m_refData || !brush.m_refData) return false;
169
170 return *(wxBrushRefData*)m_refData == *(wxBrushRefData*)brush.m_refData;
32b8ec41
VZ
171}
172
173bool wxBrush::operator != (const wxBrush& brush) const
174{
175 return m_refData != brush.m_refData;
176}
177
3e6858cd 178wxBrushStyle wxBrush::GetStyle() const
32b8ec41 179{
ac3688c0 180 wxCHECK_MSG( Ok(), wxBRUSHSTYLE_INVALID, _T("invalid brush") );
32b8ec41
VZ
181
182 return M_BRUSHDATA->m_style;
183}
184
231b9591 185wxColour wxBrush::GetColour() const
32b8ec41 186{
ac3688c0 187 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
32b8ec41
VZ
188
189 return M_BRUSHDATA->m_colour;
190}
191
192wxBitmap *wxBrush::GetStipple() const
193{
ac3688c0 194 wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
32b8ec41
VZ
195
196 return &M_BRUSHDATA->m_stipple;
197}
198
199void* wxBrush::GetMaskPattern() const
200{
201 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
202
203 return (void*)&(M_BRUSHDATA->m_maskPattern);
204}
205
206void* wxBrush::GetPixPattern() const
207{
208 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
209
210 return (void*)&(M_BRUSHDATA->m_pixPattern);
211}
212
213void wxBrush::SetColour(const wxColour& col)
214{
6d7ee9e8 215 AllocExclusive();
32b8ec41
VZ
216 M_BRUSHDATA->m_colour = col;
217}
218
1a1498c0 219void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
32b8ec41 220{
6d7ee9e8 221 AllocExclusive();
32b8ec41
VZ
222 M_BRUSHDATA->m_colour.Set(r, g, b);
223}
224
3e6858cd 225void wxBrush::SetStyle( wxBrushStyle style )
32b8ec41 226{
6d7ee9e8 227 AllocExclusive();
32b8ec41
VZ
228 M_BRUSHDATA->m_style = style;
229}
230
231void wxBrush::SetStipple(const wxBitmap& stipple)
232{
6d7ee9e8 233 AllocExclusive();
32b8ec41
VZ
234
235 wxCHECK_RET( stipple.Ok(), _T("invalid bitmap") );
46562151 236 wxCHECK_RET( stipple.GetWidth() == 8 && stipple.GetHeight() == 8,
32b8ec41
VZ
237 _T("stipple bitmap must be 8x8") );
238
239 M_BRUSHDATA->m_stipple = stipple;
240 wxBitmapToPixPattern(stipple, &(M_BRUSHDATA->m_pixPattern),
241 &(M_BRUSHDATA->m_maskPattern));
242
243 if (M_BRUSHDATA->m_stipple.GetMask())
244 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
245 else
246 M_BRUSHDATA->m_style = wxSTIPPLE;
247}
248
8f884a0d 249wxGDIRefData *wxBrush::CreateGDIRefData() const
32b8ec41 250{
6d7ee9e8
VS
251 return new wxBrushRefData;
252}
253
8f884a0d 254wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
6d7ee9e8
VS
255{
256 return new wxBrushRefData(*(wxBrushRefData *)data);
32b8ec41 257}