1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Vaclav Slavik 
   6 // Copyright:   (c) 2001 Vaclav Slavik 
   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
) 
 126     if (wxTheBrushList
) wxTheBrushList
->AddBrush(this); 
 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
; 
 135     if (wxTheBrushList
) wxTheBrushList
->AddBrush(this); 
 138 wxBrush::wxBrush(const wxBitmap 
&stippleBitmap
) 
 140     wxCHECK_RET( stippleBitmap
.Ok(), _T("invalid bitmap") ); 
 141     wxCHECK_RET( stippleBitmap
.GetWidth() == 8 && stippleBitmap
.GetHeight() == 8,  
 142                   _T("stipple bitmap must be 8x8") ); 
 144     m_refData 
= new wxBrushRefData(); 
 145     M_BRUSHDATA
->m_colour 
= *wxBLACK
; 
 147     M_BRUSHDATA
->m_stipple 
= stippleBitmap
; 
 148     wxBitmapToPixPattern(stippleBitmap
, &(M_BRUSHDATA
->m_pixPattern
),  
 149                                         &(M_BRUSHDATA
->m_maskPattern
)); 
 151     if (M_BRUSHDATA
->m_stipple
.GetMask()) 
 152                 M_BRUSHDATA
->m_style 
= wxSTIPPLE_MASK_OPAQUE
; 
 154                 M_BRUSHDATA
->m_style 
= wxSTIPPLE
; 
 156     if (wxTheBrushList
) wxTheBrushList
->AddBrush(this); 
 159 wxBrush::wxBrush(const wxBrush 
&brush
) 
 163     if (wxTheBrushList
) wxTheBrushList
->AddBrush(this); 
 168     if (wxTheBrushList
) wxTheBrushList
->RemoveBrush(this); 
 171 wxBrush
& wxBrush::operator = (const wxBrush
& brush
) 
 173     if (*this == brush
) return (*this); 
 178 bool wxBrush::operator == (const wxBrush
& brush
) const 
 180     return m_refData 
== brush
.m_refData
; 
 183 bool wxBrush::operator != (const wxBrush
& brush
) const 
 185     return m_refData 
!= brush
.m_refData
; 
 188 bool wxBrush::Ok() const 
 190     return ((m_refData
) && M_BRUSHDATA
->m_colour
.Ok()); 
 193 int wxBrush::GetStyle() const 
 195     if (m_refData 
== NULL
) 
 197         wxFAIL_MSG( wxT("invalid brush") ); 
 201     return M_BRUSHDATA
->m_style
; 
 204 wxColour 
&wxBrush::GetColour() const 
 206     if (m_refData 
== NULL
) 
 208         wxFAIL_MSG( wxT("invalid brush") ); 
 212     return M_BRUSHDATA
->m_colour
; 
 215 wxBitmap 
*wxBrush::GetStipple() const 
 217     if (m_refData 
== NULL
) 
 219         wxFAIL_MSG( wxT("invalid brush") ); 
 220         return &wxNullBitmap
; 
 223     return &M_BRUSHDATA
->m_stipple
; 
 226 void* wxBrush::GetMaskPattern() const 
 228     wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") ); 
 230     return (void*)&(M_BRUSHDATA
->m_maskPattern
); 
 233 void* wxBrush::GetPixPattern() const 
 235     wxCHECK_MSG( Ok(), NULL
, wxT("invalid brush") ); 
 237     return (void*)&(M_BRUSHDATA
->m_pixPattern
); 
 240 void wxBrush::SetColour(const wxColour
& col
) 
 243     M_BRUSHDATA
->m_colour 
= col
; 
 246 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
) 
 249     M_BRUSHDATA
->m_colour
.Set(r
, g
, b
); 
 252 void wxBrush::SetStyle( int style 
) 
 255     M_BRUSHDATA
->m_style 
= style
; 
 258 void wxBrush::SetStipple(const wxBitmap
& stipple
) 
 262     wxCHECK_RET( stipple
.Ok(), _T("invalid bitmap") ); 
 263     wxCHECK_RET( stipple
.GetWidth() == 8 && stipple
.GetHeight() == 8,  
 264                   _T("stipple bitmap must be 8x8") ); 
 266     M_BRUSHDATA
->m_stipple 
= stipple
; 
 267     wxBitmapToPixPattern(stipple
, &(M_BRUSHDATA
->m_pixPattern
), 
 268                                   &(M_BRUSHDATA
->m_maskPattern
)); 
 270     if (M_BRUSHDATA
->m_stipple
.GetMask()) 
 271         M_BRUSHDATA
->m_style 
= wxSTIPPLE_MASK_OPAQUE
; 
 273         M_BRUSHDATA
->m_style 
= wxSTIPPLE
; 
 276 void wxBrush::Unshare() 
 280         m_refData 
= new wxBrushRefData(); 
 284         wxBrushRefData
* ref 
= new wxBrushRefData(*(wxBrushRefData
*)m_refData
);