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
, wxBrushStyle style
)
131 m_refData
= new wxBrushRefData();
132 M_BRUSHDATA
->m_style
= style
;
133 M_BRUSHDATA
->m_colour
= colour
;
136 #if FUTURE_WXWIN_COMPATIBILITY_3_0
137 wxBrush::wxBrush(const wxColour
& col
, int style
)
139 m_refData
= new wxBrushRefData
;
140 M_BRUSHDATA
->m_style
= (wxBrushStyle
)style
;
141 M_BRUSHDATA
->m_colour
= colour
;
145 wxBrush::wxBrush(const wxBitmap
&stippleBitmap
)
147 wxCHECK_RET( stippleBitmap
.IsOk(), wxT("invalid bitmap") );
148 wxCHECK_RET( stippleBitmap
.GetWidth() == 8 && stippleBitmap
.GetHeight() == 8,
149 wxT("stipple bitmap must be 8x8") );
151 m_refData
= new wxBrushRefData();
152 M_BRUSHDATA
->m_colour
= *wxBLACK
;
154 M_BRUSHDATA
->m_stipple
= stippleBitmap
;
155 wxBitmapToPixPattern(stippleBitmap
, &(M_BRUSHDATA
->m_pixPattern
),
156 &(M_BRUSHDATA
->m_maskPattern
));
158 if (M_BRUSHDATA
->m_stipple
.GetMask())
159 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
161 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
164 bool wxBrush::operator == (const wxBrush
& brush
) const
166 if (m_refData
== brush
.m_refData
) return true;
168 if (!m_refData
|| !brush
.m_refData
) return false;
170 return *(wxBrushRefData
*)m_refData
== *(wxBrushRefData
*)brush
.m_refData
;
173 bool wxBrush::operator != (const wxBrush
& brush
) const
175 return m_refData
!= brush
.m_refData
;
178 wxBrushStyle
wxBrush::GetStyle() const
180 wxCHECK_MSG( IsOk(), wxBRUSHSTYLE_INVALID
, wxT("invalid brush") );
182 return M_BRUSHDATA
->m_style
;
185 wxColour
wxBrush::GetColour() const
187 wxCHECK_MSG( IsOk(), wxNullColour
, wxT("invalid brush") );
189 return M_BRUSHDATA
->m_colour
;
192 wxBitmap
*wxBrush::GetStipple() const
194 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid brush") );
196 return &M_BRUSHDATA
->m_stipple
;
199 void* wxBrush::GetMaskPattern() const
201 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid brush") );
203 return (void*)&(M_BRUSHDATA
->m_maskPattern
);
206 void* wxBrush::GetPixPattern() const
208 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid brush") );
210 return (void*)&(M_BRUSHDATA
->m_pixPattern
);
213 void wxBrush::SetColour(const wxColour
& col
)
216 M_BRUSHDATA
->m_colour
= col
;
219 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
)
222 M_BRUSHDATA
->m_colour
.Set(r
, g
, b
);
225 void wxBrush::SetStyle( wxBrushStyle style
)
228 M_BRUSHDATA
->m_style
= style
;
231 void wxBrush::SetStipple(const wxBitmap
& stipple
)
235 wxCHECK_RET( stipple
.IsOk(), wxT("invalid bitmap") );
236 wxCHECK_RET( stipple
.GetWidth() == 8 && stipple
.GetHeight() == 8,
237 wxT("stipple bitmap must be 8x8") );
239 M_BRUSHDATA
->m_stipple
= stipple
;
240 wxBitmapToPixPattern(stipple
, &(M_BRUSHDATA
->m_pixPattern
),
241 &(M_BRUSHDATA
->m_maskPattern
));
243 if (M_BRUSHDATA
->m_stipple
.GetMask())
244 M_BRUSHDATA
->m_style
= wxSTIPPLE_MASK_OPAQUE
;
246 M_BRUSHDATA
->m_style
= wxSTIPPLE
;
249 wxGDIRefData
*wxBrush::CreateGDIRefData() const
251 return new wxBrushRefData
;
254 wxGDIRefData
*wxBrush::CloneGDIRefData(const wxGDIRefData
*data
) const
256 return new wxBrushRefData(*(wxBrushRefData
*)data
);