1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "brush.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/mgl/private.h"
23 #include "wx/mgl/dcmemory.h"
26 // ---------------------------------------------------------------------------
28 // ---------------------------------------------------------------------------
30 // This function converts wxBitmap into pixpattern24_t representation
31 // (used by wxBrush and wxPen)
33 void wxBitmapToPixPattern(const wxBitmap
& bitmap
,
34 pixpattern24_t
*pix
, pattern_t
*mask
)
42 mem
.SelectObject(bitmap
);
44 wxCurrentDCSwitcher
curDC(dc
);
46 for (y
= 0; y
< 8; y
++)
47 for (x
= 0; x
< 8; x
++)
48 dc
->unpackColorFast(dc
->getPixelFast(x
, y
),
55 if ( mask
&& bitmap
.GetMask() )
57 mem
.SelectObject(*bitmap
.GetMask()->GetBitmap());
59 wxCurrentDCSwitcher
curDC(dc
);
61 for (y
= 0; y
< 8; y
++)
64 for (x
= 0; x
< 8; x
++)
65 if ( dc
->getPixelFast(x
, y
) != 0 )
66 mask
->p
[y
] |= 1 << (7 - x
);
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 class wxBrushRefData
: public wxObjectRefData
81 wxBrushRefData(const wxBrushRefData
& data
);
86 pixpattern24_t m_pixPattern
;
87 pattern_t m_maskPattern
;
90 wxBrushRefData::wxBrushRefData()
95 for (y
= 0; y
< 8; y
++)
96 for (x
= 0; x
< 8; x
++)
97 for (c
= 0; c
< 3; c
++)
98 m_pixPattern
.p
[y
][x
][c
] = 0;
99 for (y
= 0; y
< 8; y
++)
100 m_maskPattern
.p
[y
] = 0;
103 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
)
105 m_style
= data
.m_style
;
106 m_stipple
= data
.m_stipple
;
107 m_colour
= data
.m_colour
;
110 for (y
= 0; y
< 8; y
++)
111 for (x
= 0; x
< 8; x
++)
112 for (c
= 0; c
< 3; c
++)
113 m_pixPattern
.p
[y
][x
][c
] = data
.m_pixPattern
.p
[y
][x
][c
];
114 for (y
= 0; y
< 8; y
++)
115 m_maskPattern
.p
[y
] = data
.m_maskPattern
.p
[y
];
118 //-----------------------------------------------------------------------------
120 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
122 IMPLEMENT_DYNAMIC_CLASS(wxBrush
,wxGDIObject
)
124 wxBrush::wxBrush(const wxColour
&colour
, int style
)
126 m_refData
= new wxBrushRefData();
127 M_BRUSHDATA
->m_style
= style
;
128 M_BRUSHDATA
->m_colour
= colour
;
131 wxBrush::wxBrush(const wxBitmap
&stippleBitmap
)
133 wxCHECK_RET( stippleBitmap
.Ok(), _T("invalid bitmap") );
134 wxCHECK_RET( stippleBitmap
.GetWidth() == 8 && stippleBitmap
.GetHeight() == 8,
135 _T("stipple bitmap must be 8x8") );
137 m_refData
= new wxBrushRefData();
138 M_BRUSHDATA
->m_colour
= *wxBLACK
;
140 M_BRUSHDATA
->m_stipple
= stippleBitmap
;
141 wxBitmapToPixPattern(stippleBitmap
, &(M_BRUSHDATA
->m_pixPattern
),
142 &(M_BRUSHDATA
->m_maskPattern
));
144 if (M_BRUSHDATA
->m_stipple
.GetMask())
145 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
147 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
150 wxBrush::wxBrush(const wxBrush
&brush
)
155 wxBrush
& wxBrush::operator = (const wxBrush
& brush
)
157 if (*this == brush
) return (*this);
162 bool wxBrush::operator == (const wxBrush
& brush
) const
164 return m_refData
== brush
.m_refData
;
167 bool wxBrush::operator != (const wxBrush
& brush
) const
169 return m_refData
!= brush
.m_refData
;
172 bool wxBrush::Ok() const
174 return ((m_refData
) && M_BRUSHDATA
->m_colour
.Ok());
177 int wxBrush::GetStyle() const
179 if (m_refData
== NULL
)
181 wxFAIL_MSG( wxT("invalid brush") );
185 return M_BRUSHDATA
->m_style
;
188 wxColour
&wxBrush::GetColour() const
190 if (m_refData
== NULL
)
192 wxFAIL_MSG( wxT("invalid brush") );
196 return M_BRUSHDATA
->m_colour
;
199 wxBitmap
*wxBrush::GetStipple() const
201 if (m_refData
== NULL
)
203 wxFAIL_MSG( wxT("invalid brush") );
204 return &wxNullBitmap
;
207 return &M_BRUSHDATA
->m_stipple
;
210 void* wxBrush::GetMaskPattern() const
212 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
214 return (void*)&(M_BRUSHDATA
->m_maskPattern
);
217 void* wxBrush::GetPixPattern() const
219 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
221 return (void*)&(M_BRUSHDATA
->m_pixPattern
);
224 void wxBrush::SetColour(const wxColour
& col
)
227 M_BRUSHDATA
->m_colour
= col
;
230 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
233 M_BRUSHDATA
->m_colour
.Set(r
, g
, b
);
236 void wxBrush::SetStyle( int style
)
239 M_BRUSHDATA
->m_style
= style
;
242 void wxBrush::SetStipple(const wxBitmap
& stipple
)
246 wxCHECK_RET( stipple
.Ok(), _T("invalid bitmap") );
247 wxCHECK_RET( stipple
.GetWidth() == 8 && stipple
.GetHeight() == 8,
248 _T("stipple bitmap must be 8x8") );
250 M_BRUSHDATA
->m_stipple
= stipple
;
251 wxBitmapToPixPattern(stipple
, &(M_BRUSHDATA
->m_pixPattern
),
252 &(M_BRUSHDATA
->m_maskPattern
));
254 if (M_BRUSHDATA
->m_stipple
.GetMask())
255 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
257 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
260 wxObjectRefData
*wxBrush::CreateRefData() const
262 return new wxBrushRefData
;
265 wxObjectRefData
*wxBrush::CloneRefData(const wxObjectRefData
*data
) const
267 return new wxBrushRefData(*(wxBrushRefData
*)data
);