introduce wxBrushStyle enum and replace 'int style' occurrences in wxBrush code with...
[wxWidgets.git] / src / mgl / brush.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/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 // 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"
19 #include "wx/dcmemory.h"
20
21
22 // ---------------------------------------------------------------------------
23 // helper functions
24 // ---------------------------------------------------------------------------
25
26 // This function converts wxBitmap into pixpattern24_t representation
27 // (used by wxBrush and wxPen)
28
29 void wxBitmapToPixPattern(const wxBitmap& bitmap,
30 pixpattern24_t *pix, pattern_t *mask)
31 {
32 wxMemoryDC mem;
33 MGLDevCtx *dc;
34 int x, y;
35
36 if ( pix != NULL )
37 {
38 mem.SelectObjectAsSource(bitmap);
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++)
44 dc->unpackColorFast(dc->getPixelFast(x, y),
45 pix->p[y][x][2],
46 pix->p[y][x][1],
47 pix->p[y][x][0]);
48 dc->endPixel();
49 }
50
51 if ( mask && bitmap.GetMask() )
52 {
53 mem.SelectObjectAsSource(bitmap.GetMask()->GetBitmap());
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 )
62 mask->p[y] = (uchar)(mask->p[y] | (1 << (7 - x)));
63 }
64 dc->endPixel();
65 }
66 }
67
68
69 //-----------------------------------------------------------------------------
70 // wxBrush
71 //-----------------------------------------------------------------------------
72
73 class wxBrushRefData : public wxGDIRefData
74 {
75 public:
76 wxBrushRefData();
77 wxBrushRefData(const wxBrushRefData& data);
78
79 virtual bool IsOk() const { return m_colour.IsOk(); }
80
81 bool operator==(const wxBrushRefData& data) const
82 {
83 return (m_style == data.m_style &&
84 m_stipple.IsSameAs(data.m_stipple) &&
85 m_colour == data.m_colour);
86 }
87
88 wxBrushStyle m_style;
89 wxColour m_colour;
90 wxBitmap m_stipple;
91 pixpattern24_t m_pixPattern;
92 pattern_t m_maskPattern;
93 };
94
95 wxBrushRefData::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
108 wxBrushRefData::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
127 IMPLEMENT_DYNAMIC_CLASS(wxBrush,wxGDIObject)
128
129 wxBrush::wxBrush(const wxColour &colour, wxBrushStyle style)
130 {
131 m_refData = new wxBrushRefData();
132 M_BRUSHDATA->m_style = style;
133 M_BRUSHDATA->m_colour = colour;
134 }
135
136 wxBrush::wxBrush(const wxBitmap &stippleBitmap)
137 {
138 wxCHECK_RET( stippleBitmap.Ok(), _T("invalid bitmap") );
139 wxCHECK_RET( stippleBitmap.GetWidth() == 8 && stippleBitmap.GetHeight() == 8,
140 _T("stipple bitmap must be 8x8") );
141
142 m_refData = new wxBrushRefData();
143 M_BRUSHDATA->m_colour = *wxBLACK;
144
145 M_BRUSHDATA->m_stipple = stippleBitmap;
146 wxBitmapToPixPattern(stippleBitmap, &(M_BRUSHDATA->m_pixPattern),
147 &(M_BRUSHDATA->m_maskPattern));
148
149 if (M_BRUSHDATA->m_stipple.GetMask())
150 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
151 else
152 M_BRUSHDATA->m_style = wxSTIPPLE;
153 }
154
155 bool wxBrush::operator == (const wxBrush& brush) const
156 {
157 if (m_refData == brush.m_refData) return true;
158
159 if (!m_refData || !brush.m_refData) return false;
160
161 return *(wxBrushRefData*)m_refData == *(wxBrushRefData*)brush.m_refData;
162 }
163
164 bool wxBrush::operator != (const wxBrush& brush) const
165 {
166 return m_refData != brush.m_refData;
167 }
168
169 wxBrushStyle wxBrush::GetStyle() const
170 {
171 if (m_refData == NULL)
172 {
173 wxFAIL_MSG( wxT("invalid brush") );
174 return 0;
175 }
176
177 return M_BRUSHDATA->m_style;
178 }
179
180 wxColour &wxBrush::GetColour() const
181 {
182 if (m_refData == NULL)
183 {
184 wxFAIL_MSG( wxT("invalid brush") );
185 return wxNullColour;
186 }
187
188 return M_BRUSHDATA->m_colour;
189 }
190
191 wxBitmap *wxBrush::GetStipple() const
192 {
193 if (m_refData == NULL)
194 {
195 wxFAIL_MSG( wxT("invalid brush") );
196 return &wxNullBitmap;
197 }
198
199 return &M_BRUSHDATA->m_stipple;
200 }
201
202 void* wxBrush::GetMaskPattern() const
203 {
204 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
205
206 return (void*)&(M_BRUSHDATA->m_maskPattern);
207 }
208
209 void* wxBrush::GetPixPattern() const
210 {
211 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
212
213 return (void*)&(M_BRUSHDATA->m_pixPattern);
214 }
215
216 void wxBrush::SetColour(const wxColour& col)
217 {
218 AllocExclusive();
219 M_BRUSHDATA->m_colour = col;
220 }
221
222 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
223 {
224 AllocExclusive();
225 M_BRUSHDATA->m_colour.Set(r, g, b);
226 }
227
228 void wxBrush::SetStyle( wxBrushStyle style )
229 {
230 AllocExclusive();
231 M_BRUSHDATA->m_style = style;
232 }
233
234 void wxBrush::SetStipple(const wxBitmap& stipple)
235 {
236 AllocExclusive();
237
238 wxCHECK_RET( stipple.Ok(), _T("invalid bitmap") );
239 wxCHECK_RET( stipple.GetWidth() == 8 && stipple.GetHeight() == 8,
240 _T("stipple bitmap must be 8x8") );
241
242 M_BRUSHDATA->m_stipple = stipple;
243 wxBitmapToPixPattern(stipple, &(M_BRUSHDATA->m_pixPattern),
244 &(M_BRUSHDATA->m_maskPattern));
245
246 if (M_BRUSHDATA->m_stipple.GetMask())
247 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
248 else
249 M_BRUSHDATA->m_style = wxSTIPPLE;
250 }
251
252 wxGDIRefData *wxBrush::CreateGDIRefData() const
253 {
254 return new wxBrushRefData;
255 }
256
257 wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
258 {
259 return new wxBrushRefData(*(wxBrushRefData *)data);
260 }