wxPen implementation (no dash support yet)
[wxWidgets.git] / src / cocoa / pen.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/pen.mm
3 // Purpose:     wxPen
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/08/02
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/setup.h"
13 #include "wx/pen.h"
14 #include "wx/bitmap.h"
15 #include "wx/colour.h"
16
17 #import <AppKit/NSColor.h>
18
19 // ========================================================================
20 // wxPenRefData
21 // ========================================================================
22 class WXDLLEXPORT wxPenRefData: public wxGDIRefData
23 {
24     friend class WXDLLEXPORT wxPen;
25 public:
26     wxPenRefData(const wxColour& colour = wxNullColour,
27         int width = 1, int style = wxSOLID,
28         const wxBitmap& stipple = wxNullBitmap);
29     wxPenRefData(const wxPenRefData& data);
30     ~wxPenRefData() { Free(); }
31
32     void SetWidth(int Width) { m_width = Width; }
33     void SetStyle(int Style) { m_style = Style; }
34     void SetJoin(int Join) { m_join = Join; }
35     void SetCap(int Cap) { m_cap = Cap; }
36     void SetColour(const wxColour& col) { Free(); m_colour = col; }
37     void SetDashes(int nb_dashes, const wxDash *Dash)
38     {
39         m_nbDash = nb_dashes;
40         m_dash = (wxDash *)Dash;
41     }
42     void SetStipple(const wxBitmap& Stipple)
43     {
44         Free();
45         m_stipple = Stipple;
46         m_style = wxSTIPPLE;
47     }
48     WX_NSColor GetNSColor();
49 protected:
50     void Free();
51
52     int             m_width;
53     int             m_style;
54     int             m_join;
55     int             m_cap;
56     wxColour        m_colour;
57     int             m_nbDash;
58     wxDash         *m_dash;
59     wxBitmap        m_stipple;
60     WX_NSColor      m_cocoaNSColor;
61 private:
62     // Don't allow assignment
63     wxPenRefData& operator=(const wxPenRefData& data);
64 };
65
66 #define M_PENDATA ((wxPenRefData *)m_refData)
67
68 inline wxPenRefData::wxPenRefData(const wxColour& colour,
69         int width, int style, const wxBitmap& stipple)
70 {
71     m_width = width;
72     m_style = style;
73     m_join = wxJOIN_ROUND;
74     m_cap = wxCAP_ROUND;
75     m_colour = colour;
76     m_nbDash = 0;
77     m_dash = 0;
78     m_stipple = stipple;
79     m_cocoaNSColor = nil;
80 }
81
82 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
83 {
84     m_width = data.m_width;
85     m_style = data.m_style;
86     m_join = data.m_join;
87     m_cap = data.m_cap;
88     m_colour = data.m_colour;
89     m_nbDash = data.m_nbDash;
90     m_dash = data.m_dash;
91     m_stipple = data.m_stipple;
92     m_cocoaNSColor = [data.m_cocoaNSColor retain];
93 }
94
95 inline void wxPenRefData::Free()
96 {
97     [m_cocoaNSColor release];
98     m_cocoaNSColor = nil;
99 }
100
101 inline WX_NSColor wxPenRefData::GetNSColor()
102 {
103     if(!m_cocoaNSColor)
104     {
105         switch( m_style )
106         {
107         case wxTRANSPARENT:
108             m_cocoaNSColor = [[NSColor clearColor] retain];
109             break;
110         case wxSTIPPLE:
111 //  wxBitmap isn't implemented yet
112 //            m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
113 //            break;
114         // The hatch brushes are going to be tricky
115         case wxBDIAGONAL_HATCH:
116         case wxCROSSDIAG_HATCH:
117         case wxFDIAGONAL_HATCH:
118         case wxCROSS_HATCH:
119         case wxHORIZONTAL_HATCH:
120         case wxVERTICAL_HATCH:
121         default:
122         // Dot/dashed pens use solid colors
123         case wxDOT:
124         case wxLONG_DASH:
125         case wxSHORT_DASH:
126         case wxDOT_DASH:
127         case wxUSER_DASH:
128         case wxSOLID:
129             NSColor *colour_NSColor = m_colour.GetNSColor();
130             if(!colour_NSColor)
131                 colour_NSColor = [NSColor clearColor];
132             m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
133             break;
134         }
135     }
136     return m_cocoaNSColor;
137 }
138
139 // ========================================================================
140 // wxPen
141 // ========================================================================
142 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
143
144 wxPen::wxPen()
145 {
146 }
147
148 wxPen::~wxPen()
149 {
150 }
151
152 // Should implement Create
153 wxPen::wxPen(const wxColour& colour, int width, int style)
154 {
155     m_refData = new wxPenRefData(colour,width,style);
156 }
157
158 wxPen::wxPen(const wxBitmap& stipple, int width)
159 {
160     m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
161 }
162
163 wxObjectRefData *wxPen::CreateRefData() const
164 {
165     return new wxPenRefData;
166 }
167
168 wxObjectRefData *wxPen::CloneRefData(const wxObjectRefData *data) const
169 {
170     return new wxPenRefData(*(wxPenRefData *)data);
171 }
172
173 void wxPen::SetWidth(int Width)
174 {
175     AllocExclusive();
176     M_PENDATA->SetWidth(Width);
177 }
178
179 void wxPen::SetStyle(int Style)
180 {
181     AllocExclusive();
182     M_PENDATA->SetStyle(Style);
183 }
184
185 void wxPen::SetJoin(int Join)
186 {
187     AllocExclusive();
188     M_PENDATA->SetJoin(Join);
189 }
190
191 void wxPen::SetCap(int Cap)
192 {
193     AllocExclusive();
194     M_PENDATA->SetCap(Cap);
195 }
196
197 void wxPen::SetColour(const wxColour& col)
198 {
199     AllocExclusive();
200     M_PENDATA->SetColour(col);
201 }
202
203 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
204 {
205     AllocExclusive();
206     M_PENDATA->SetColour(wxColour(r, g, b));
207 }
208
209 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
210 {
211     AllocExclusive();
212     M_PENDATA->SetDashes(nb_dashes, Dash);
213 }
214
215 void wxPen::SetStipple(const wxBitmap& Stipple)
216 {
217     AllocExclusive();
218     M_PENDATA->SetStipple(Stipple);
219 }
220
221 wxColour& wxPen::GetColour() const
222 {
223     return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour); 
224 }
225
226 int wxPen::GetWidth() const
227 {
228     return (M_PENDATA ? M_PENDATA->m_width : 0); 
229 }
230
231 int wxPen::GetStyle() const
232 {
233     return (M_PENDATA ? M_PENDATA->m_style : 0); 
234 }
235
236 int wxPen::GetJoin() const
237 {
238     return (M_PENDATA ? M_PENDATA->m_join : 0); 
239 }
240
241 int wxPen::GetCap() const
242 {
243     return (M_PENDATA ? M_PENDATA->m_cap : 0); 
244 }
245
246 int wxPen::GetDashes(wxDash **ptr) const
247 {
248     *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
249 }
250
251 wxBitmap *wxPen::GetStipple() const
252 {
253     return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL); 
254 }
255
256 WX_NSColor wxPen::GetNSColor()
257 {
258     return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
259 }
260