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