1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/brush.cpp
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
18 #include "wx/mgl/private.h"
19 #include "wx/dcmemory.h"
22 // ---------------------------------------------------------------------------
24 // ---------------------------------------------------------------------------
26 // This function converts wxBitmap into pixpattern24_t representation
27 // (used by wxBrush and wxPen)
29 void wxBitmapToPixPattern(const wxBitmap
& bitmap
,
30 pixpattern24_t
*pix
, pattern_t
*mask
)
38 mem
.SelectObjectAsSource(bitmap
);
40 wxCurrentDCSwitcher
curDC(dc
);
42 for (y
= 0; y
< 8; y
++)
43 for (x
= 0; x
< 8; x
++)
44 dc
->unpackColorFast(dc
->getPixelFast(x
, y
),
51 if ( mask
&& bitmap
.GetMask() )
53 mem
.SelectObjectAsSource(bitmap
.GetMask()->GetBitmap());
55 wxCurrentDCSwitcher
curDC(dc
);
57 for (y
= 0; y
< 8; y
++)
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
)));
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 class wxBrushRefData
: public wxGDIRefData
77 wxBrushRefData(const wxBrushRefData
& data
);
79 virtual bool IsOk() const { return m_colour
.IsOk(); }
81 bool operator==(const wxBrushRefData
& data
) const
83 return (m_style
== data
.m_style
&&
84 m_stipple
.IsSameAs(data
.m_stipple
) &&
85 m_colour
== data
.m_colour
);
91 pixpattern24_t m_pixPattern
;
92 pattern_t m_maskPattern
;
95 wxBrushRefData::wxBrushRefData()
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;
108 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
)
110 m_style
= data
.m_style
;
111 m_stipple
= data
.m_stipple
;
112 m_colour
= data
.m_colour
;
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
];
123 //-----------------------------------------------------------------------------
125 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
127 IMPLEMENT_DYNAMIC_CLASS(wxBrush
,wxGDIObject
)
129 wxBrush::wxBrush(const wxColour
&colour
, int style
)
131 m_refData
= new wxBrushRefData();
132 M_BRUSHDATA
->m_style
= style
;
133 M_BRUSHDATA
->m_colour
= colour
;
136 wxBrush::wxBrush(const wxBitmap
&stippleBitmap
)
138 wxCHECK_RET( stippleBitmap
.Ok(), _T("invalid bitmap") );
139 wxCHECK_RET( stippleBitmap
.GetWidth() == 8 && stippleBitmap
.GetHeight() == 8,
140 _T("stipple bitmap must be 8x8") );
142 m_refData
= new wxBrushRefData();
143 M_BRUSHDATA
->m_colour
= *wxBLACK
;
145 M_BRUSHDATA
->m_stipple
= stippleBitmap
;
146 wxBitmapToPixPattern(stippleBitmap
, &(M_BRUSHDATA
->m_pixPattern
),
147 &(M_BRUSHDATA
->m_maskPattern
));
149 if (M_BRUSHDATA
->m_stipple
.GetMask())
150 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
152 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
155 bool wxBrush::operator == (const wxBrush
& brush
) const
157 if (m_refData
== brush
.m_refData
) return true;
159 if (!m_refData
|| !brush
.m_refData
) return false;
161 return *(wxBrushRefData
*)m_refData
== *(wxBrushRefData
*)brush
.m_refData
;
164 bool wxBrush::operator != (const wxBrush
& brush
) const
166 return m_refData
!= brush
.m_refData
;
169 int wxBrush::GetStyle() const
171 if (m_refData
== NULL
)
173 wxFAIL_MSG( wxT("invalid brush") );
177 return M_BRUSHDATA
->m_style
;
180 wxColour
&wxBrush::GetColour() const
182 if (m_refData
== NULL
)
184 wxFAIL_MSG( wxT("invalid brush") );
188 return M_BRUSHDATA
->m_colour
;
191 wxBitmap
*wxBrush::GetStipple() const
193 if (m_refData
== NULL
)
195 wxFAIL_MSG( wxT("invalid brush") );
196 return &wxNullBitmap
;
199 return &M_BRUSHDATA
->m_stipple
;
202 void* wxBrush::GetMaskPattern() const
204 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
206 return (void*)&(M_BRUSHDATA
->m_maskPattern
);
209 void* wxBrush::GetPixPattern() const
211 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
213 return (void*)&(M_BRUSHDATA
->m_pixPattern
);
216 void wxBrush::SetColour(const wxColour
& col
)
219 M_BRUSHDATA
->m_colour
= col
;
222 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
225 M_BRUSHDATA
->m_colour
.Set(r
, g
, b
);
228 void wxBrush::SetStyle( int style
)
231 M_BRUSHDATA
->m_style
= style
;
234 void wxBrush::SetStipple(const wxBitmap
& stipple
)
238 wxCHECK_RET( stipple
.Ok(), _T("invalid bitmap") );
239 wxCHECK_RET( stipple
.GetWidth() == 8 && stipple
.GetHeight() == 8,
240 _T("stipple bitmap must be 8x8") );
242 M_BRUSHDATA
->m_stipple
= stipple
;
243 wxBitmapToPixPattern(stipple
, &(M_BRUSHDATA
->m_pixPattern
),
244 &(M_BRUSHDATA
->m_maskPattern
));
246 if (M_BRUSHDATA
->m_stipple
.GetMask())
247 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
249 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
252 wxGDIRefData
*wxBrush::CreateGDIRefData() const
254 return new wxBrushRefData
;
257 wxGDIRefData
*wxBrush::CloneGDIRefData(const wxGDIRefData
*data
) const
259 return new wxBrushRefData(*(wxBrushRefData
*)data
);