Added dash support
[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() { FreeCocoaNSColor(); FreeCocoaDash(); }
31
32     void SetWidth(int Width) { m_width = Width; }
33     void SetStyle(int Style)
34     {   FreeCocoaNSColor();
35         FreeCocoaDash();
36         m_style = Style;
37     }
38     void SetJoin(int Join) { m_join = Join; }
39     void SetCap(int Cap) { m_cap = Cap; }
40     void SetColour(const wxColour& col) { FreeCocoaNSColor(); m_colour = col; }
41     void SetDashes(int nb_dashes, const wxDash *Dash)
42     {
43         FreeCocoaDash();
44         m_nbDash = nb_dashes;
45         m_dash = (wxDash *)Dash;
46     }
47     void SetStipple(const wxBitmap& Stipple)
48     {
49         FreeCocoaNSColor();
50         m_stipple = Stipple;
51         m_style = wxSTIPPLE;
52     }
53     WX_NSColor GetNSColor();
54     int GetCocoaLineDash(const float **pattern);
55 protected:
56     void FreeCocoaNSColor();
57     void FreeCocoaDash();
58
59     int             m_width;
60     int             m_style;
61     int             m_join;
62     int             m_cap;
63     wxColour        m_colour;
64     int             m_nbDash;
65     wxDash         *m_dash;
66     wxBitmap        m_stipple;
67     WX_NSColor      m_cocoaNSColor;
68     float          *m_cocoaDash;
69
70     // Predefined dash patterns
71     static const int scm_countDot;
72     static const float scm_patternDot[];
73     static const int scm_countLongDash;
74     static const float scm_patternLongDash[];
75     static const int scm_countShortDash;
76     static const float scm_patternShortDash[];
77     static const int scm_countDotDash;
78     static const float scm_patternDotDash[];
79 private:
80     // Don't allow assignment
81     wxPenRefData& operator=(const wxPenRefData& data);
82 };
83
84 const int wxPenRefData::scm_countDot = 1;
85 const float wxPenRefData::scm_patternDot[] = {
86     1.0
87 };
88 const int wxPenRefData::scm_countLongDash = 1;
89 const float wxPenRefData::scm_patternLongDash[] = {
90     10.0
91 };
92 const int wxPenRefData::scm_countShortDash = 1;
93 const float wxPenRefData::scm_patternShortDash[] = {
94     5.0
95 };
96 const int wxPenRefData::scm_countDotDash = 4;
97 const float wxPenRefData::scm_patternDotDash[] = {
98     1.0
99 ,   1.0
100 ,   5.0
101 ,   1.0
102 };
103
104 #define M_PENDATA ((wxPenRefData *)m_refData)
105
106 inline wxPenRefData::wxPenRefData(const wxColour& colour,
107         int width, int style, const wxBitmap& stipple)
108 {
109     m_width = width;
110     m_style = style;
111     m_join = wxJOIN_ROUND;
112     m_cap = wxCAP_ROUND;
113     m_colour = colour;
114     m_nbDash = 0;
115     m_dash = 0;
116     m_stipple = stipple;
117     m_cocoaNSColor = nil;
118     m_cocoaDash = NULL;
119 }
120
121 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
122 {
123     m_width = data.m_width;
124     m_style = data.m_style;
125     m_join = data.m_join;
126     m_cap = data.m_cap;
127     m_colour = data.m_colour;
128     m_nbDash = data.m_nbDash;
129     m_dash = data.m_dash;
130     m_stipple = data.m_stipple;
131     m_cocoaNSColor = [data.m_cocoaNSColor retain];
132 }
133
134 inline void wxPenRefData::FreeCocoaNSColor()
135 {
136     [m_cocoaNSColor release];
137     m_cocoaNSColor = nil;
138 }
139
140 inline void wxPenRefData::FreeCocoaDash()
141 {
142     delete m_cocoaDash;
143     m_cocoaDash = NULL;
144 }
145
146 inline WX_NSColor wxPenRefData::GetNSColor()
147 {
148     if(!m_cocoaNSColor)
149     {
150         switch( m_style )
151         {
152         case wxTRANSPARENT:
153             m_cocoaNSColor = [[NSColor clearColor] retain];
154             break;
155         case wxSTIPPLE:
156 //  wxBitmap isn't implemented yet
157 //            m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
158 //            break;
159         // The hatch brushes are going to be tricky
160         case wxBDIAGONAL_HATCH:
161         case wxCROSSDIAG_HATCH:
162         case wxFDIAGONAL_HATCH:
163         case wxCROSS_HATCH:
164         case wxHORIZONTAL_HATCH:
165         case wxVERTICAL_HATCH:
166         default:
167         // Dot/dashed pens use solid colors
168         case wxDOT:
169         case wxLONG_DASH:
170         case wxSHORT_DASH:
171         case wxDOT_DASH:
172         case wxUSER_DASH:
173         case wxSOLID:
174             NSColor *colour_NSColor = m_colour.GetNSColor();
175             if(!colour_NSColor)
176                 colour_NSColor = [NSColor clearColor];
177             m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
178             break;
179         }
180     }
181     return m_cocoaNSColor;
182 }
183
184 int wxPenRefData::GetCocoaLineDash(const float **pattern)
185 {
186     int count;
187     switch( m_style )
188     {
189     case wxDOT:
190         count = scm_countDot;
191         if(pattern)
192             *pattern = scm_patternDot;
193         break;
194     case wxLONG_DASH:
195         count = scm_countLongDash;
196         if(pattern)
197             *pattern = scm_patternLongDash;
198         break;
199     case wxSHORT_DASH:
200         count = scm_countShortDash;
201         if(pattern)
202             *pattern = scm_patternShortDash;
203         break;
204     case wxDOT_DASH:
205         count = scm_countDotDash;
206         if(pattern)
207             *pattern = scm_patternDotDash;
208         break;
209     case wxUSER_DASH:
210         count = m_nbDash;
211         if(pattern)
212         {
213             if(!m_cocoaDash)
214             {
215                 m_cocoaDash = new float[count];
216                 for(int i=0; i<count; i++)
217                     m_cocoaDash[i] = m_dash[i];
218             }
219             *pattern = m_cocoaDash;
220         }
221         break;
222     case wxTRANSPARENT:
223     case wxSTIPPLE:
224     case wxBDIAGONAL_HATCH:
225     case wxCROSSDIAG_HATCH:
226     case wxFDIAGONAL_HATCH:
227     case wxCROSS_HATCH:
228     case wxHORIZONTAL_HATCH:
229     case wxVERTICAL_HATCH:
230     case wxSOLID:
231     default:
232         count = 0;
233         if(pattern)
234             *pattern = NULL;
235     }
236     return count;
237 }
238
239 // ========================================================================
240 // wxPen
241 // ========================================================================
242 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
243
244 wxPen::wxPen()
245 {
246 }
247
248 wxPen::~wxPen()
249 {
250 }
251
252 // Should implement Create
253 wxPen::wxPen(const wxColour& colour, int width, int style)
254 {
255     m_refData = new wxPenRefData(colour,width,style);
256 }
257
258 wxPen::wxPen(const wxBitmap& stipple, int width)
259 {
260     m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
261 }
262
263 wxObjectRefData *wxPen::CreateRefData() const
264 {
265     return new wxPenRefData;
266 }
267
268 wxObjectRefData *wxPen::CloneRefData(const wxObjectRefData *data) const
269 {
270     return new wxPenRefData(*(wxPenRefData *)data);
271 }
272
273 void wxPen::SetWidth(int Width)
274 {
275     AllocExclusive();
276     M_PENDATA->SetWidth(Width);
277 }
278
279 void wxPen::SetStyle(int Style)
280 {
281     AllocExclusive();
282     M_PENDATA->SetStyle(Style);
283 }
284
285 void wxPen::SetJoin(int Join)
286 {
287     AllocExclusive();
288     M_PENDATA->SetJoin(Join);
289 }
290
291 void wxPen::SetCap(int Cap)
292 {
293     AllocExclusive();
294     M_PENDATA->SetCap(Cap);
295 }
296
297 void wxPen::SetColour(const wxColour& col)
298 {
299     AllocExclusive();
300     M_PENDATA->SetColour(col);
301 }
302
303 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
304 {
305     AllocExclusive();
306     M_PENDATA->SetColour(wxColour(r, g, b));
307 }
308
309 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
310 {
311     AllocExclusive();
312     M_PENDATA->SetDashes(nb_dashes, Dash);
313 }
314
315 void wxPen::SetStipple(const wxBitmap& Stipple)
316 {
317     AllocExclusive();
318     M_PENDATA->SetStipple(Stipple);
319 }
320
321 wxColour& wxPen::GetColour() const
322 {
323     return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour); 
324 }
325
326 int wxPen::GetWidth() const
327 {
328     return (M_PENDATA ? M_PENDATA->m_width : 0); 
329 }
330
331 int wxPen::GetStyle() const
332 {
333     return (M_PENDATA ? M_PENDATA->m_style : 0); 
334 }
335
336 int wxPen::GetJoin() const
337 {
338     return (M_PENDATA ? M_PENDATA->m_join : 0); 
339 }
340
341 int wxPen::GetCap() const
342 {
343     return (M_PENDATA ? M_PENDATA->m_cap : 0); 
344 }
345
346 int wxPen::GetDashes(wxDash **ptr) const
347 {
348     *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
349 }
350
351 wxBitmap *wxPen::GetStipple() const
352 {
353     return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL); 
354 }
355
356 WX_NSColor wxPen::GetNSColor()
357 {
358     return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
359 }
360
361 int wxPen::GetCocoaLineDash(const float **pattern)
362 {
363     if(M_PENDATA)
364         return M_PENDATA->GetCocoaLineDash(pattern);
365     if(pattern)
366         *pattern = NULL;
367     return 0;
368 }
369