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
] = (uchar
)(mask
->p
[y
] | (1 << (7 - x
)));
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 class wxBrushRefData
: public wxObjectRefData
77 wxBrushRefData(const wxBrushRefData
& data
);
79 bool operator == (const wxBrushRefData
& data
) const
81 return (m_style
== data
.m_style
&&
82 m_stipple
.IsSameAs(data
.m_stipple
) &&
83 m_colour
== data
.m_colour
);
89 pixpattern24_t m_pixPattern
;
90 pattern_t m_maskPattern
;
93 wxBrushRefData::wxBrushRefData()
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;
106 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
)
108 m_style
= data
.m_style
;
109 m_stipple
= data
.m_stipple
;
110 m_colour
= data
.m_colour
;
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
];
121 //-----------------------------------------------------------------------------
123 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
125 IMPLEMENT_DYNAMIC_CLASS(wxBrush
,wxGDIObject
)
127 wxBrush::wxBrush(const wxColour
&colour
, int style
)
129 m_refData
= new wxBrushRefData();
130 M_BRUSHDATA
->m_style
= style
;
131 M_BRUSHDATA
->m_colour
= colour
;
134 wxBrush::wxBrush(const wxBitmap
&stippleBitmap
)
136 wxCHECK_RET( stippleBitmap
.Ok(), _T("invalid bitmap") );
137 wxCHECK_RET( stippleBitmap
.GetWidth() == 8 && stippleBitmap
.GetHeight() == 8,
138 _T("stipple bitmap must be 8x8") );
140 m_refData
= new wxBrushRefData();
141 M_BRUSHDATA
->m_colour
= *wxBLACK
;
143 M_BRUSHDATA
->m_stipple
= stippleBitmap
;
144 wxBitmapToPixPattern(stippleBitmap
, &(M_BRUSHDATA
->m_pixPattern
),
145 &(M_BRUSHDATA
->m_maskPattern
));
147 if (M_BRUSHDATA
->m_stipple
.GetMask())
148 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
150 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
153 bool wxBrush::operator == (const wxBrush
& brush
) const
155 if (m_refData
== brush
.m_refData
) return true;
157 if (!m_refData
|| !brush
.m_refData
) return false;
159 return *(wxBrushRefData
*)m_refData
== *(wxBrushRefData
*)brush
.m_refData
;
162 bool wxBrush::operator != (const wxBrush
& brush
) const
164 return m_refData
!= brush
.m_refData
;
167 bool wxBrush::IsOk() const
169 return ((m_refData
) && M_BRUSHDATA
->m_colour
.Ok());
172 int wxBrush::GetStyle() const
174 if (m_refData
== NULL
)
176 wxFAIL_MSG( wxT("invalid brush") );
180 return M_BRUSHDATA
->m_style
;
183 wxColour
&wxBrush::GetColour() const
185 if (m_refData
== NULL
)
187 wxFAIL_MSG( wxT("invalid brush") );
191 return M_BRUSHDATA
->m_colour
;
194 wxBitmap
*wxBrush::GetStipple() const
196 if (m_refData
== NULL
)
198 wxFAIL_MSG( wxT("invalid brush") );
199 return &wxNullBitmap
;
202 return &M_BRUSHDATA
->m_stipple
;
205 void* wxBrush::GetMaskPattern() const
207 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
209 return (void*)&(M_BRUSHDATA
->m_maskPattern
);
212 void* wxBrush::GetPixPattern() const
214 wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") );
216 return (void*)&(M_BRUSHDATA
->m_pixPattern
);
219 void wxBrush::SetColour(const wxColour
& col
)
222 M_BRUSHDATA
->m_colour
= col
;
225 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
228 M_BRUSHDATA
->m_colour
.Set(r
, g
, b
);
231 void wxBrush::SetStyle( int style
)
234 M_BRUSHDATA
->m_style
= style
;
237 void wxBrush::SetStipple(const wxBitmap
& stipple
)
241 wxCHECK_RET( stipple
.Ok(), _T("invalid bitmap") );
242 wxCHECK_RET( stipple
.GetWidth() == 8 && stipple
.GetHeight() == 8,
243 _T("stipple bitmap must be 8x8") );
245 M_BRUSHDATA
->m_stipple
= stipple
;
246 wxBitmapToPixPattern(stipple
, &(M_BRUSHDATA
->m_pixPattern
),
247 &(M_BRUSHDATA
->m_maskPattern
));
249 if (M_BRUSHDATA
->m_stipple
.GetMask())
250 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
252 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
255 wxObjectRefData
*wxBrush::CreateRefData() const
257 return new wxBrushRefData
;
260 wxObjectRefData
*wxBrush::CloneRefData(const wxObjectRefData
*data
) const
262 return new wxBrushRefData(*(wxBrushRefData
*)data
);