1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/brush.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Copyright:   (c) 1998 Robert Roebling
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 #include "wx/brush.h"
13 
14 #ifndef WX_PRECOMP
15     #include "wx/bitmap.h"
16     #include "wx/colour.h"
17 #endif
18 
19 //-----------------------------------------------------------------------------
20 // wxBrush
21 //-----------------------------------------------------------------------------
22 
23 class wxBrushRefData: public wxGDIRefData
24 {
25 public:
wxBrushRefData(const wxColour & colour=wxNullColour,wxBrushStyle style=wxBRUSHSTYLE_SOLID)26     wxBrushRefData(const wxColour& colour = wxNullColour, wxBrushStyle style = wxBRUSHSTYLE_SOLID)
27         : m_colour(colour)
28     {
29         m_style = style;
30     }
31 
wxBrushRefData(const wxBrushRefData & data)32     wxBrushRefData( const wxBrushRefData& data )
33         : wxGDIRefData()
34         , m_colour(data.m_colour)
35         , m_stipple(data.m_stipple)
36     {
37         m_style = data.m_style;
38     }
39 
operator ==(const wxBrushRefData & data) const40     bool operator == (const wxBrushRefData& data) const
41     {
42         return (m_style == data.m_style &&
43                 m_stipple.IsSameAs(data.m_stipple) &&
44                 m_colour == data.m_colour);
45     }
46 
47     wxBrushStyle m_style;
48     wxColour     m_colour;
49     wxBitmap     m_stipple;
50 };
51 
52 //-----------------------------------------------------------------------------
53 
54 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
55 
56 wxIMPLEMENT_DYNAMIC_CLASS(wxBrush,wxGDIObject);
57 
wxBrush(const wxColour & colour,wxBrushStyle style)58 wxBrush::wxBrush( const wxColour &colour, wxBrushStyle style )
59 {
60     m_refData = new wxBrushRefData(colour, style);
61 }
62 
wxBrush(const wxColour & col,int style)63 wxBrush::wxBrush(const wxColour& col, int style)
64 {
65     m_refData = new wxBrushRefData(col, (wxBrushStyle)style);
66 }
67 
wxBrush(const wxBitmap & stippleBitmap)68 wxBrush::wxBrush( const wxBitmap &stippleBitmap )
69 {
70     wxBrushStyle style = wxBRUSHSTYLE_STIPPLE;
71     if (stippleBitmap.GetMask())
72         style = wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE;
73 
74     m_refData = new wxBrushRefData(*wxBLACK, style);
75     M_BRUSHDATA->m_stipple = stippleBitmap;
76 }
77 
~wxBrush()78 wxBrush::~wxBrush()
79 {
80     // m_refData unrefed in ~wxObject
81 }
82 
CreateGDIRefData() const83 wxGDIRefData *wxBrush::CreateGDIRefData() const
84 {
85     return new wxBrushRefData;
86 }
87 
CloneGDIRefData(const wxGDIRefData * data) const88 wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
89 {
90     return new wxBrushRefData(*static_cast<const wxBrushRefData*>(data));
91 }
92 
operator ==(const wxBrush & brush) const93 bool wxBrush::operator==(const wxBrush& brush) const
94 {
95     if (m_refData == brush.m_refData) return true;
96 
97     if (!m_refData || !brush.m_refData) return false;
98 
99     return ( *(wxBrushRefData*)m_refData == *(wxBrushRefData*)brush.m_refData );
100 }
101 
GetStyle() const102 wxBrushStyle wxBrush::GetStyle() const
103 {
104     wxCHECK_MSG( IsOk(), wxBRUSHSTYLE_INVALID, wxT("invalid brush") );
105 
106     return M_BRUSHDATA->m_style;
107 }
108 
GetColour() const109 wxColour wxBrush::GetColour() const
110 {
111     wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid brush") );
112 
113     return M_BRUSHDATA->m_colour;
114 }
115 
GetStipple() const116 wxBitmap *wxBrush::GetStipple() const
117 {
118     wxCHECK_MSG( IsOk(), NULL, wxT("invalid brush") );
119 
120     return &M_BRUSHDATA->m_stipple;
121 }
122 
SetColour(const wxColour & col)123 void wxBrush::SetColour( const wxColour& col )
124 {
125     AllocExclusive();
126 
127     M_BRUSHDATA->m_colour = col;
128 }
129 
SetColour(unsigned char r,unsigned char g,unsigned char b)130 void wxBrush::SetColour( unsigned char r, unsigned char g, unsigned char b )
131 {
132     AllocExclusive();
133 
134     M_BRUSHDATA->m_colour.Set( r, g, b );
135 }
136 
SetStyle(wxBrushStyle style)137 void wxBrush::SetStyle( wxBrushStyle style )
138 {
139     AllocExclusive();
140 
141     M_BRUSHDATA->m_style = style;
142 }
143 
SetStipple(const wxBitmap & stipple)144 void wxBrush::SetStipple( const wxBitmap& stipple )
145 {
146     AllocExclusive();
147 
148     M_BRUSHDATA->m_stipple = stipple;
149     if (M_BRUSHDATA->m_stipple.GetMask())
150     {
151         M_BRUSHDATA->m_style = wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE;
152     }
153     else
154     {
155         M_BRUSHDATA->m_style = wxBRUSHSTYLE_STIPPLE;
156     }
157 }
158