1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/pen.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/pen.h"
13 
14 #ifndef WX_PRECOMP
15     #include "wx/colour.h"
16 #endif
17 
18 #include <gdk/gdk.h>
19 
20 //-----------------------------------------------------------------------------
21 // wxPen
22 //-----------------------------------------------------------------------------
23 
24 class wxPenRefData: public wxGDIRefData
25 {
26 public:
wxPenRefData()27     wxPenRefData()
28     {
29         m_width = 1;
30         m_style = wxPENSTYLE_SOLID;
31         m_joinStyle = wxJOIN_ROUND;
32         m_capStyle = wxCAP_ROUND;
33         m_dash = NULL;
34         m_countDashes = 0;
35     }
36 
wxPenRefData(const wxPenRefData & data)37     wxPenRefData( const wxPenRefData& data )
38         : wxGDIRefData()
39         , m_colour(data.m_colour)
40     {
41         m_style = data.m_style;
42         m_width = data.m_width;
43         m_joinStyle = data.m_joinStyle;
44         m_capStyle = data.m_capStyle;
45         m_countDashes = data.m_countDashes;
46         m_dash = data.m_dash;
47     }
48 
wxPenRefData(const wxPenInfo & info)49     wxPenRefData( const wxPenInfo& info )
50         : m_colour(info.GetColour())
51     {
52         m_width = info.GetWidth();
53         m_style = info.GetStyle();
54         m_joinStyle = info.GetJoin();
55         m_capStyle = info.GetCap();
56         m_countDashes = info.GetDashes(const_cast<wxDash**>(&m_dash));
57     }
58 
operator ==(const wxPenRefData & data) const59     bool operator == (const wxPenRefData& data) const
60     {
61         if ( m_countDashes != data.m_countDashes )
62             return false;
63 
64         if ( m_dash )
65         {
66             if ( !data.m_dash ||
67                  memcmp(m_dash, data.m_dash, m_countDashes*sizeof(wxDash)) )
68             {
69                 return false;
70             }
71         }
72         else if ( data.m_dash )
73         {
74             return false;
75         }
76 
77 
78         return m_style == data.m_style &&
79                m_width == data.m_width &&
80                m_joinStyle == data.m_joinStyle &&
81                m_capStyle == data.m_capStyle &&
82                m_colour == data.m_colour;
83     }
84 
85     int        m_width;
86     wxPenStyle m_style;
87     wxPenJoin  m_joinStyle;
88     wxPenCap   m_capStyle;
89     wxColour   m_colour;
90     int        m_countDashes;
91     const wxDash* m_dash;
92 };
93 
94 //-----------------------------------------------------------------------------
95 
96 #define M_PENDATA ((wxPenRefData *)m_refData)
97 
98 wxIMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject);
99 
wxPen(const wxColour & colour,int width,wxPenStyle style)100 wxPen::wxPen( const wxColour &colour, int width, wxPenStyle style )
101 {
102     m_refData = new wxPenRefData(wxPenInfo(colour, width).Style(style));
103 }
104 
wxPen(const wxColour & colour,int width,int style)105 wxPen::wxPen(const wxColour& colour, int width, int style)
106 {
107     m_refData = new wxPenRefData
108                     (
109                         wxPenInfo(colour, width).Style((wxPenStyle)style)
110                     );
111 }
112 
wxPen(const wxPenInfo & info)113 wxPen::wxPen(const wxPenInfo& info)
114 {
115     m_refData = new wxPenRefData(info);
116 }
117 
~wxPen()118 wxPen::~wxPen()
119 {
120     // m_refData unrefed in ~wxObject
121 }
122 
CreateGDIRefData() const123 wxGDIRefData *wxPen::CreateGDIRefData() const
124 {
125     return new wxPenRefData;
126 }
127 
CloneGDIRefData(const wxGDIRefData * data) const128 wxGDIRefData *wxPen::CloneGDIRefData(const wxGDIRefData *data) const
129 {
130     return new wxPenRefData(*static_cast<const wxPenRefData*>(data));
131 }
132 
operator ==(const wxPen & pen) const133 bool wxPen::operator == ( const wxPen& pen ) const
134 {
135     if (m_refData == pen.m_refData) return true;
136 
137     if (!m_refData || !pen.m_refData) return false;
138 
139     return ( *(wxPenRefData*)m_refData == *(wxPenRefData*)pen.m_refData );
140 }
141 
SetColour(const wxColour & colour)142 void wxPen::SetColour( const wxColour &colour )
143 {
144     AllocExclusive();
145 
146     M_PENDATA->m_colour = colour;
147 }
148 
SetDashes(int number_of_dashes,const wxDash * dash)149 void wxPen::SetDashes( int number_of_dashes, const wxDash *dash )
150 {
151     AllocExclusive();
152 
153     M_PENDATA->m_countDashes = number_of_dashes;
154     M_PENDATA->m_dash = dash;
155 }
156 
SetColour(unsigned char red,unsigned char green,unsigned char blue)157 void wxPen::SetColour( unsigned char red, unsigned char green, unsigned char blue )
158 {
159     AllocExclusive();
160 
161     M_PENDATA->m_colour.Set( red, green, blue );
162 }
163 
SetCap(wxPenCap capStyle)164 void wxPen::SetCap( wxPenCap capStyle )
165 {
166     AllocExclusive();
167 
168     M_PENDATA->m_capStyle = capStyle;
169 }
170 
SetJoin(wxPenJoin joinStyle)171 void wxPen::SetJoin( wxPenJoin joinStyle )
172 {
173     AllocExclusive();
174 
175     M_PENDATA->m_joinStyle = joinStyle;
176 }
177 
SetStyle(wxPenStyle style)178 void wxPen::SetStyle( wxPenStyle style )
179 {
180     AllocExclusive();
181 
182     M_PENDATA->m_style = style;
183 }
184 
SetWidth(int width)185 void wxPen::SetWidth( int width )
186 {
187     AllocExclusive();
188 
189     M_PENDATA->m_width = width;
190 }
191 
GetDashes(wxDash ** ptr) const192 int wxPen::GetDashes( wxDash **ptr ) const
193 {
194     wxCHECK_MSG( IsOk(), -1, wxT("invalid pen") );
195 
196     *ptr = const_cast<wxDash*>(M_PENDATA->m_dash);
197      return M_PENDATA->m_countDashes;
198 }
199 
GetDashCount() const200 int wxPen::GetDashCount() const
201 {
202     wxCHECK_MSG( IsOk(), -1, wxT("invalid pen") );
203 
204     return (M_PENDATA->m_countDashes);
205 }
206 
GetDash() const207 wxDash* wxPen::GetDash() const
208 {
209     wxCHECK_MSG( IsOk(), NULL, wxT("invalid pen") );
210 
211     return const_cast<wxDash*>(M_PENDATA->m_dash);
212 }
213 
GetCap() const214 wxPenCap wxPen::GetCap() const
215 {
216     wxCHECK_MSG( IsOk(), wxCAP_INVALID, wxT("invalid pen") );
217 
218     return M_PENDATA->m_capStyle;
219 }
220 
GetJoin() const221 wxPenJoin wxPen::GetJoin() const
222 {
223     wxCHECK_MSG( IsOk(), wxJOIN_INVALID, wxT("invalid pen") );
224 
225     return M_PENDATA->m_joinStyle;
226 }
227 
GetStyle() const228 wxPenStyle wxPen::GetStyle() const
229 {
230     wxCHECK_MSG( IsOk(), wxPENSTYLE_INVALID, wxT("invalid pen") );
231 
232     return M_PENDATA->m_style;
233 }
234 
GetWidth() const235 int wxPen::GetWidth() const
236 {
237     wxCHECK_MSG( IsOk(), -1, wxT("invalid pen") );
238 
239     return M_PENDATA->m_width;
240 }
241 
GetColour() const242 wxColour wxPen::GetColour() const
243 {
244     wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid pen") );
245 
246     return M_PENDATA->m_colour;
247 }
248 
249 // stippled pens are not supported by wxGTK
SetStipple(const wxBitmap & WXUNUSED (stipple))250 void wxPen::SetStipple(const wxBitmap& WXUNUSED(stipple))
251 {
252     wxFAIL_MSG( "stippled pens not supported" );
253 }
254 
GetStipple() const255 wxBitmap *wxPen::GetStipple() const
256 {
257     return NULL;
258 }
259 
260