1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/brush.cpp 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  35 #include "wx/msw/private.h" 
  37 // ---------------------------------------------------------------------------- 
  39 // ---------------------------------------------------------------------------- 
  41 class WXDLLEXPORT wxBrushRefData
: public wxGDIRefData
 
  44     wxBrushRefData(const wxColour
& colour 
= wxNullColour
, int style 
= wxSOLID
); 
  45     wxBrushRefData(const wxBitmap
& stipple
); 
  46     wxBrushRefData(const wxBrushRefData
& data
); 
  47     virtual ~wxBrushRefData(); 
  49     bool operator==(const wxBrushRefData
& data
) const; 
  54     const wxColour
& GetColour() const { return m_colour
; } 
  55     int GetStyle() const { return m_style
; } 
  56     wxBitmap 
*GetStipple() { return &m_stipple
; } 
  58     void SetColour(const wxColour
& colour
) { Free(); m_colour 
= colour
; } 
  59     void SetStyle(int style
) { Free(); m_style 
= style
; } 
  60     void SetStipple(const wxBitmap
& stipple
) { Free(); DoSetStipple(stipple
); } 
  63     void DoSetStipple(const wxBitmap
& stipple
); 
  70     // no assignment operator, the objects of this class are shared and never 
  71     // assigned after being created once 
  72     wxBrushRefData
& operator=(const wxBrushRefData
&); 
  75 #define M_BRUSHDATA ((wxBrushRefData *)m_refData) 
  77 // ============================================================================ 
  78 // wxBrushRefData implementation 
  79 // ============================================================================ 
  81 IMPLEMENT_DYNAMIC_CLASS(wxBrush
, wxGDIObject
) 
  83 // ---------------------------------------------------------------------------- 
  84 // wxBrushRefData ctors/dtor 
  85 // ---------------------------------------------------------------------------- 
  87 wxBrushRefData::wxBrushRefData(const wxColour
& colour
, int style
) 
  95 wxBrushRefData::wxBrushRefData(const wxBitmap
& stipple
) 
  97     DoSetStipple(stipple
); 
 102 wxBrushRefData::wxBrushRefData(const wxBrushRefData
& data
) 
 104                 m_stipple(data
.m_stipple
), 
 105                 m_colour(data
.m_colour
) 
 107     m_style 
= data
.m_style
; 
 109     // we can't share HBRUSH, we'd need to create another one ourselves 
 113 wxBrushRefData::~wxBrushRefData() 
 118 // ---------------------------------------------------------------------------- 
 119 // wxBrushRefData accesors 
 120 // ---------------------------------------------------------------------------- 
 122 bool wxBrushRefData::operator==(const wxBrushRefData
& data
) const 
 124     // don't compare HBRUSHes 
 125     return m_style 
== data
.m_style 
&& 
 126            m_colour 
== data
.m_colour 
&& 
 127            m_stipple 
== data
.m_stipple
; 
 130 void wxBrushRefData::DoSetStipple(const wxBitmap
& stipple
) 
 133     m_style 
= stipple
.GetMask() ? wxSTIPPLE_MASK_OPAQUE 
: wxSTIPPLE
; 
 136 // ---------------------------------------------------------------------------- 
 137 // wxBrushRefData resource handling 
 138 // ---------------------------------------------------------------------------- 
 140 void wxBrushRefData::Free() 
 144         ::DeleteObject(m_hBrush
); 
 150 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) 
 152 static int TranslateHatchStyle(int style
) 
 156         case wxBDIAGONAL_HATCH
: return HS_BDIAGONAL
; 
 157         case wxCROSSDIAG_HATCH
: return HS_DIAGCROSS
; 
 158         case wxFDIAGONAL_HATCH
: return HS_FDIAGONAL
; 
 159         case wxCROSS_HATCH
:     return HS_CROSS
; 
 160         case wxHORIZONTAL_HATCH
:return HS_HORIZONTAL
; 
 161         case wxVERTICAL_HATCH
:  return HS_VERTICAL
; 
 166 #endif // !__WXMICROWIN__ && !__WXWINCE__ 
 168 HBRUSH 
wxBrushRefData::GetHBRUSH() 
 172 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) 
 173         int hatchStyle 
= TranslateHatchStyle(m_style
); 
 174         if ( hatchStyle 
== -1 ) 
 175 #endif // !__WXMICROWIN__ && !__WXWINCE__ 
 180                     m_hBrush 
= (HBRUSH
)::GetStockObject(NULL_BRUSH
); 
 184                     m_hBrush 
= ::CreatePatternBrush(GetHbitmapOf(m_stipple
)); 
 187                 case wxSTIPPLE_MASK_OPAQUE
: 
 188                     m_hBrush 
= ::CreatePatternBrush((HBITMAP
)m_stipple
.GetMask() 
 193                     wxFAIL_MSG( _T("unknown brush style") ); 
 197                     m_hBrush 
= ::CreateSolidBrush(m_colour
.GetPixel()); 
 202         else // create a hatched brush 
 204             m_hBrush 
= ::CreateHatchBrush(hatchStyle
, m_colour
.GetPixel()); 
 210             wxLogLastError(_T("CreateXXXBrush()")); 
 217 // ============================================================================ 
 218 // wxBrush implementation 
 219 // ============================================================================ 
 221 // ---------------------------------------------------------------------------- 
 222 // wxBrush ctors/dtor 
 223 // ---------------------------------------------------------------------------- 
 229 wxBrush::wxBrush(const wxColour
& col
, int style
) 
 231     m_refData 
= new wxBrushRefData(col
, style
); 
 234 wxBrush::wxBrush(const wxBitmap
& stipple
) 
 236     m_refData 
= new wxBrushRefData(stipple
); 
 243 // ---------------------------------------------------------------------------- 
 244 // wxBrush house keeping stuff 
 245 // ---------------------------------------------------------------------------- 
 247 bool wxBrush::operator==(const wxBrush
& brush
) const 
 249     const wxBrushRefData 
*brushData 
= (wxBrushRefData 
*)brush
.m_refData
; 
 251     // an invalid brush is considered to be only equal to another invalid brush 
 252     return m_refData 
? (brushData 
&& *M_BRUSHDATA 
== *brushData
) : !brushData
; 
 255 wxObjectRefData 
*wxBrush::CreateRefData() const 
 257     return new wxBrushRefData
; 
 260 wxObjectRefData 
*wxBrush::CloneRefData(const wxObjectRefData 
*data
) const 
 262     return new wxBrushRefData(*(const wxBrushRefData 
*)data
); 
 265 // ---------------------------------------------------------------------------- 
 267 // ---------------------------------------------------------------------------- 
 269 wxColour 
wxBrush::GetColour() const 
 271     wxCHECK_MSG( Ok(), wxNullColour
, _T("invalid brush") ); 
 273     return M_BRUSHDATA
->GetColour(); 
 276 int wxBrush::GetStyle() const 
 278     wxCHECK_MSG( Ok(), 0, _T("invalid brush") ); 
 280     return M_BRUSHDATA
->GetStyle(); 
 283 wxBitmap 
*wxBrush::GetStipple() const 
 285     wxCHECK_MSG( Ok(), NULL
, _T("invalid brush") ); 
 287     return M_BRUSHDATA
->GetStipple(); 
 290 WXHANDLE 
wxBrush::GetResourceHandle() const 
 292     wxCHECK_MSG( Ok(), FALSE
, _T("invalid brush") ); 
 294     return (WXHANDLE
)M_BRUSHDATA
->GetHBRUSH(); 
 297 // ---------------------------------------------------------------------------- 
 299 // ---------------------------------------------------------------------------- 
 301 void wxBrush::SetColour(const wxColour
& col
) 
 305     M_BRUSHDATA
->SetColour(col
); 
 308 void wxBrush::SetColour(unsigned char r
, unsigned char g
, unsigned char b
) 
 312     M_BRUSHDATA
->SetColour(wxColour(r
, g
, b
)); 
 315 void wxBrush::SetStyle(int style
) 
 319     M_BRUSHDATA
->SetStyle(style
); 
 322 void wxBrush::SetStipple(const wxBitmap
& stipple
) 
 326     M_BRUSHDATA
->SetStipple(stipple
);