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/mgl/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
.SelectObject(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
.SelectObject(*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
] |= 1 << (7 - x
);
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 class wxBrushRefData
: public wxObjectRefData
77 wxBrushRefData(const wxBrushRefData
& data
);
82 pixpattern24_t m_pixPattern
;
83 pattern_t m_maskPattern
;
86 wxBrushRefData::wxBrushRefData()
91 for (y
= 0; y
< 8; y
++)
92 for (x
= 0; x
< 8; x
++)
93 for (c
= 0; c
< 3; c
++)
94 m_pixPattern
.p
[y
][x
][c
] = 0;
95 for (y
= 0; y
< 8; y
++)
96 m_maskPattern
.p
[y
] = 0;
99 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
)
101 m_style
= data
.m_style
;
102 m_stipple
= data
.m_stipple
;
103 m_colour
= data
.m_colour
;
106 for (y
= 0; y
< 8; y
++)
107 for (x
= 0; x
< 8; x
++)
108 for (c
= 0; c
< 3; c
++)
109 m_pixPattern
.p
[y
][x
][c
] = data
.m_pixPattern
.p
[y
][x
][c
];
110 for (y
= 0; y
< 8; y
++)
111 m_maskPattern
.p
[y
] = data
.m_maskPattern
.p
[y
];
114 //-----------------------------------------------------------------------------
116 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
118 IMPLEMENT_DYNAMIC_CLASS(wxBrush
,wxGDIObject
)
120 wxBrush::wxBrush(const wxColour
&colour
, int style
)
122 m_refData
= new wxBrushRefData();
123 M_BRUSHDATA
->m_style
= style
;
124 M_BRUSHDATA
->m_colour
= colour
;
127 wxBrush::wxBrush(const wxBitmap
&stippleBitmap
)
129 wxCHECK_RET( stippleBitmap
.Ok(), _T("invalid bitmap") );
130 wxCHECK_RET( stippleBitmap
.GetWidth() == 8 && stippleBitmap
.GetHeight() == 8,
131 _T("stipple bitmap must be 8x8") );
133 m_refData
= new wxBrushRefData();
134 M_BRUSHDATA
->m_colour
= *wxBLACK
;
136 M_BRUSHDATA
->m_stipple
= stippleBitmap
;
137 wxBitmapToPixPattern(stippleBitmap
, &(M_BRUSHDATA
->m_pixPattern
),
138 &(M_BRUSHDATA
->m_maskPattern
));
140 if (M_BRUSHDATA
->m_stipple
.GetMask())
141 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
143 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
146 wxBrush::wxBrush(const wxBrush
&brush
)
151 wxBrush
& wxBrush::operator = (const wxBrush
& brush
)
153 if (*this == brush
) return (*this);
158 bool wxBrush::operator == (const wxBrush
& brush
) const
160 return m_refData
== brush
.m_refData
;
163 bool wxBrush::operator != (const wxBrush
& brush
) const
165 return m_refData
!= brush
.m_refData
;
168 bool wxBrush::Ok() const
170 return ((m_refData
) && M_BRUSHDATA
->m_colour
.Ok());
173 int wxBrush::GetStyle() const
175 if (m_refData
== NULL
)
177 wxFAIL_MSG( wxT("invalid brush") );
181 return M_BRUSHDATA
->m_style
;
184 wxColour
&wxBrush::GetColour() const
186 if (m_refData
== NULL
)
188 wxFAIL_MSG( wxT("invalid brush") );
192 return M_BRUSHDATA
->m_colour
;
195 wxBitmap
*wxBrush::GetStipple() const
197 if (m_refData
== NULL
)
199 wxFAIL_MSG( wxT("invalid brush") );
200 return &wxNullBitmap
;
203 return &M_BRUSHDATA
->m_stipple
;
206 void* wxBrush::GetMaskPattern() const
208 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
210 return (void*)&(M_BRUSHDATA
->m_maskPattern
);
213 void* wxBrush::GetPixPattern() const
215 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
217 return (void*)&(M_BRUSHDATA
->m_pixPattern
);
220 void wxBrush::SetColour(const wxColour
& col
)
223 M_BRUSHDATA
->m_colour
= col
;
226 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
229 M_BRUSHDATA
->m_colour
.Set(r
, g
, b
);
232 void wxBrush::SetStyle( int style
)
235 M_BRUSHDATA
->m_style
= style
;
238 void wxBrush::SetStipple(const wxBitmap
& stipple
)
242 wxCHECK_RET( stipple
.Ok(), _T("invalid bitmap") );
243 wxCHECK_RET( stipple
.GetWidth() == 8 && stipple
.GetHeight() == 8,
244 _T("stipple bitmap must be 8x8") );
246 M_BRUSHDATA
->m_stipple
= stipple
;
247 wxBitmapToPixPattern(stipple
, &(M_BRUSHDATA
->m_pixPattern
),
248 &(M_BRUSHDATA
->m_maskPattern
));
250 if (M_BRUSHDATA
->m_stipple
.GetMask())
251 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
253 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
256 wxObjectRefData
*wxBrush::CreateRefData() const
258 return new wxBrushRefData
;
261 wxObjectRefData
*wxBrush::CloneRefData(const wxObjectRefData
*data
) const
263 return new wxBrushRefData(*(wxBrushRefData
*)data
);