added support for gcc precompiled headers
[wxWidgets.git] / src / mgl / brush.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: brush.cpp
3 // Purpose:
4 // Author: Vaclav Slavik
5 // Id: $Id$
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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(const wxColour &colour, int style)
125 {
126 m_refData = new wxBrushRefData();
127 M_BRUSHDATA->m_style = style;
128 M_BRUSHDATA->m_colour = colour;
129 }
130
131 wxBrush::wxBrush(const wxBitmap &stippleBitmap)
132 {
133 wxCHECK_RET( stippleBitmap.Ok(), _T("invalid bitmap") );
134 wxCHECK_RET( stippleBitmap.GetWidth() == 8 && stippleBitmap.GetHeight() == 8,
135 _T("stipple bitmap must be 8x8") );
136
137 m_refData = new wxBrushRefData();
138 M_BRUSHDATA->m_colour = *wxBLACK;
139
140 M_BRUSHDATA->m_stipple = stippleBitmap;
141 wxBitmapToPixPattern(stippleBitmap, &(M_BRUSHDATA->m_pixPattern),
142 &(M_BRUSHDATA->m_maskPattern));
143
144 if (M_BRUSHDATA->m_stipple.GetMask())
145 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
146 else
147 M_BRUSHDATA->m_style = wxSTIPPLE;
148 }
149
150 wxBrush::wxBrush(const wxBrush &brush)
151 {
152 Ref(brush);
153 }
154
155 wxBrush& wxBrush::operator = (const wxBrush& brush)
156 {
157 if (*this == brush) return (*this);
158 Ref(brush);
159 return *this;
160 }
161
162 bool wxBrush::operator == (const wxBrush& brush) const
163 {
164 return m_refData == brush.m_refData;
165 }
166
167 bool wxBrush::operator != (const wxBrush& brush) const
168 {
169 return m_refData != brush.m_refData;
170 }
171
172 bool wxBrush::Ok() const
173 {
174 return ((m_refData) && M_BRUSHDATA->m_colour.Ok());
175 }
176
177 int wxBrush::GetStyle() const
178 {
179 if (m_refData == NULL)
180 {
181 wxFAIL_MSG( wxT("invalid brush") );
182 return 0;
183 }
184
185 return M_BRUSHDATA->m_style;
186 }
187
188 wxColour &wxBrush::GetColour() const
189 {
190 if (m_refData == NULL)
191 {
192 wxFAIL_MSG( wxT("invalid brush") );
193 return wxNullColour;
194 }
195
196 return M_BRUSHDATA->m_colour;
197 }
198
199 wxBitmap *wxBrush::GetStipple() const
200 {
201 if (m_refData == NULL)
202 {
203 wxFAIL_MSG( wxT("invalid brush") );
204 return &wxNullBitmap;
205 }
206
207 return &M_BRUSHDATA->m_stipple;
208 }
209
210 void* wxBrush::GetMaskPattern() const
211 {
212 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
213
214 return (void*)&(M_BRUSHDATA->m_maskPattern);
215 }
216
217 void* wxBrush::GetPixPattern() const
218 {
219 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
220
221 return (void*)&(M_BRUSHDATA->m_pixPattern);
222 }
223
224 void wxBrush::SetColour(const wxColour& col)
225 {
226 AllocExclusive();
227 M_BRUSHDATA->m_colour = col;
228 }
229
230 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
231 {
232 AllocExclusive();
233 M_BRUSHDATA->m_colour.Set(r, g, b);
234 }
235
236 void wxBrush::SetStyle( int style )
237 {
238 AllocExclusive();
239 M_BRUSHDATA->m_style = style;
240 }
241
242 void wxBrush::SetStipple(const wxBitmap& stipple)
243 {
244 AllocExclusive();
245
246 wxCHECK_RET( stipple.Ok(), _T("invalid bitmap") );
247 wxCHECK_RET( stipple.GetWidth() == 8 && stipple.GetHeight() == 8,
248 _T("stipple bitmap must be 8x8") );
249
250 M_BRUSHDATA->m_stipple = stipple;
251 wxBitmapToPixPattern(stipple, &(M_BRUSHDATA->m_pixPattern),
252 &(M_BRUSHDATA->m_maskPattern));
253
254 if (M_BRUSHDATA->m_stipple.GetMask())
255 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
256 else
257 M_BRUSHDATA->m_style = wxSTIPPLE;
258 }
259
260 wxObjectRefData *wxBrush::CreateRefData() const
261 {
262 return new wxBrushRefData;
263 }
264
265 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
266 {
267 return new wxBrushRefData(*(wxBrushRefData *)data);
268 }
269