COW cleanup patch (1583966):
[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/mgl/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.SelectObject(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.SelectObject(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 wxObjectRefData
74 {
75 public:
76 wxBrushRefData();
77 wxBrushRefData(const wxBrushRefData& data);
78
79 bool operator == (const wxBrushRefData& data) const
80 {
81 return (m_style == data.m_style &&
82 m_stipple.IsRefTo(&data.m_stipple) &&
83 m_colour == data.m_colour);
84 }
85
86 int m_style;
87 wxColour m_colour;
88 wxBitmap m_stipple;
89 pixpattern24_t m_pixPattern;
90 pattern_t m_maskPattern;
91 };
92
93 wxBrushRefData::wxBrushRefData()
94 {
95 m_style = 0;
96
97 int x, y, c;
98 for (y = 0; y < 8; y++)
99 for (x = 0; x < 8; x++)
100 for (c = 0; c < 3; c++)
101 m_pixPattern.p[y][x][c] = 0;
102 for (y = 0; y < 8; y++)
103 m_maskPattern.p[y] = 0;
104 }
105
106 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
107 {
108 m_style = data.m_style;
109 m_stipple = data.m_stipple;
110 m_colour = data.m_colour;
111
112 int x, y, c;
113 for (y = 0; y < 8; y++)
114 for (x = 0; x < 8; x++)
115 for (c = 0; c < 3; c++)
116 m_pixPattern.p[y][x][c] = data.m_pixPattern.p[y][x][c];
117 for (y = 0; y < 8; y++)
118 m_maskPattern.p[y] = data.m_maskPattern.p[y];
119 }
120
121 //-----------------------------------------------------------------------------
122
123 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
124
125 IMPLEMENT_DYNAMIC_CLASS(wxBrush,wxGDIObject)
126
127 wxBrush::wxBrush(const wxColour &colour, int style)
128 {
129 m_refData = new wxBrushRefData();
130 M_BRUSHDATA->m_style = style;
131 M_BRUSHDATA->m_colour = colour;
132 }
133
134 wxBrush::wxBrush(const wxBitmap &stippleBitmap)
135 {
136 wxCHECK_RET( stippleBitmap.Ok(), _T("invalid bitmap") );
137 wxCHECK_RET( stippleBitmap.GetWidth() == 8 && stippleBitmap.GetHeight() == 8,
138 _T("stipple bitmap must be 8x8") );
139
140 m_refData = new wxBrushRefData();
141 M_BRUSHDATA->m_colour = *wxBLACK;
142
143 M_BRUSHDATA->m_stipple = stippleBitmap;
144 wxBitmapToPixPattern(stippleBitmap, &(M_BRUSHDATA->m_pixPattern),
145 &(M_BRUSHDATA->m_maskPattern));
146
147 if (M_BRUSHDATA->m_stipple.GetMask())
148 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
149 else
150 M_BRUSHDATA->m_style = wxSTIPPLE;
151 }
152
153 bool wxBrush::operator == (const wxBrush& brush) const
154 {
155 if (m_refData == brush.m_refData) return true;
156
157 if (!m_refData || !brush.m_refData) return false;
158
159 return *(wxBrushRefData*)m_refData == *(wxBrushRefData*)brush.m_refData;
160 }
161
162 bool wxBrush::operator != (const wxBrush& brush) const
163 {
164 return m_refData != brush.m_refData;
165 }
166
167 bool wxBrush::IsOk() const
168 {
169 return ((m_refData) && M_BRUSHDATA->m_colour.Ok());
170 }
171
172 int wxBrush::GetStyle() const
173 {
174 if (m_refData == NULL)
175 {
176 wxFAIL_MSG( wxT("invalid brush") );
177 return 0;
178 }
179
180 return M_BRUSHDATA->m_style;
181 }
182
183 wxColour &wxBrush::GetColour() const
184 {
185 if (m_refData == NULL)
186 {
187 wxFAIL_MSG( wxT("invalid brush") );
188 return wxNullColour;
189 }
190
191 return M_BRUSHDATA->m_colour;
192 }
193
194 wxBitmap *wxBrush::GetStipple() const
195 {
196 if (m_refData == NULL)
197 {
198 wxFAIL_MSG( wxT("invalid brush") );
199 return &wxNullBitmap;
200 }
201
202 return &M_BRUSHDATA->m_stipple;
203 }
204
205 void* wxBrush::GetMaskPattern() const
206 {
207 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
208
209 return (void*)&(M_BRUSHDATA->m_maskPattern);
210 }
211
212 void* wxBrush::GetPixPattern() const
213 {
214 wxCHECK_MSG( Ok(), NULL, wxT("invalid brush") );
215
216 return (void*)&(M_BRUSHDATA->m_pixPattern);
217 }
218
219 void wxBrush::SetColour(const wxColour& col)
220 {
221 AllocExclusive();
222 M_BRUSHDATA->m_colour = col;
223 }
224
225 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
226 {
227 AllocExclusive();
228 M_BRUSHDATA->m_colour.Set(r, g, b);
229 }
230
231 void wxBrush::SetStyle( int style )
232 {
233 AllocExclusive();
234 M_BRUSHDATA->m_style = style;
235 }
236
237 void wxBrush::SetStipple(const wxBitmap& stipple)
238 {
239 AllocExclusive();
240
241 wxCHECK_RET( stipple.Ok(), _T("invalid bitmap") );
242 wxCHECK_RET( stipple.GetWidth() == 8 && stipple.GetHeight() == 8,
243 _T("stipple bitmap must be 8x8") );
244
245 M_BRUSHDATA->m_stipple = stipple;
246 wxBitmapToPixPattern(stipple, &(M_BRUSHDATA->m_pixPattern),
247 &(M_BRUSHDATA->m_maskPattern));
248
249 if (M_BRUSHDATA->m_stipple.GetMask())
250 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
251 else
252 M_BRUSHDATA->m_style = wxSTIPPLE;
253 }
254
255 wxObjectRefData *wxBrush::CreateRefData() const
256 {
257 return new wxBrushRefData;
258 }
259
260 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
261 {
262 return new wxBrushRefData(*(wxBrushRefData *)data);
263 }