]>
Commit | Line | Data |
---|---|---|
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 | 29 | void 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 | 73 | class wxBrushRefData : public wxGDIRefData |
32b8ec41 VZ |
74 | { |
75 | public: | |
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 | ||
32b8ec41 VZ |
88 | int 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 | ||
32b8ec41 VZ |
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; | |
32b8ec41 VZ |
134 | } |
135 | ||
136 | wxBrush::wxBrush(const wxBitmap &stippleBitmap) | |
137 | { | |
138 | wxCHECK_RET( stippleBitmap.Ok(), _T("invalid bitmap") ); | |
46562151 | 139 | wxCHECK_RET( stippleBitmap.GetWidth() == 8 && stippleBitmap.GetHeight() == 8, |
32b8ec41 VZ |
140 | _T("stipple bitmap must be 8x8") ); |
141 | ||
142 | m_refData = new wxBrushRefData(); | |
143 | M_BRUSHDATA->m_colour = *wxBLACK; | |
46562151 | 144 | |
32b8ec41 | 145 | M_BRUSHDATA->m_stipple = stippleBitmap; |
46562151 | 146 | wxBitmapToPixPattern(stippleBitmap, &(M_BRUSHDATA->m_pixPattern), |
32b8ec41 VZ |
147 | &(M_BRUSHDATA->m_maskPattern)); |
148 | ||
149 | if (M_BRUSHDATA->m_stipple.GetMask()) | |
46562151 WS |
150 | M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE; |
151 | else | |
152 | M_BRUSHDATA->m_style = wxSTIPPLE; | |
32b8ec41 VZ |
153 | } |
154 | ||
32b8ec41 VZ |
155 | bool wxBrush::operator == (const wxBrush& brush) const |
156 | { | |
55ccdb93 VZ |
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; | |
32b8ec41 VZ |
162 | } |
163 | ||
164 | bool wxBrush::operator != (const wxBrush& brush) const | |
165 | { | |
166 | return m_refData != brush.m_refData; | |
167 | } | |
168 | ||
32b8ec41 VZ |
169 | int 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 | { | |
6d7ee9e8 | 218 | AllocExclusive(); |
32b8ec41 VZ |
219 | M_BRUSHDATA->m_colour = col; |
220 | } | |
221 | ||
1a1498c0 | 222 | void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b) |
32b8ec41 | 223 | { |
6d7ee9e8 | 224 | AllocExclusive(); |
32b8ec41 VZ |
225 | M_BRUSHDATA->m_colour.Set(r, g, b); |
226 | } | |
227 | ||
228 | void wxBrush::SetStyle( int style ) | |
229 | { | |
6d7ee9e8 | 230 | AllocExclusive(); |
32b8ec41 VZ |
231 | M_BRUSHDATA->m_style = style; |
232 | } | |
233 | ||
234 | void wxBrush::SetStipple(const wxBitmap& stipple) | |
235 | { | |
6d7ee9e8 | 236 | AllocExclusive(); |
32b8ec41 VZ |
237 | |
238 | wxCHECK_RET( stipple.Ok(), _T("invalid bitmap") ); | |
46562151 | 239 | wxCHECK_RET( stipple.GetWidth() == 8 && stipple.GetHeight() == 8, |
32b8ec41 VZ |
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 | ||
8f884a0d | 252 | wxGDIRefData *wxBrush::CreateGDIRefData() const |
32b8ec41 | 253 | { |
6d7ee9e8 VS |
254 | return new wxBrushRefData; |
255 | } | |
256 | ||
8f884a0d | 257 | wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const |
6d7ee9e8 VS |
258 | { |
259 | return new wxBrushRefData(*(wxBrushRefData *)data); | |
32b8ec41 | 260 | } |