1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/pen.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/bitmap.h"
17 #include "wx/colour.h"
20 #import <AppKit/NSColor.h>
22 // ========================================================================
24 // ========================================================================
25 class WXDLLEXPORT wxPenRefData: public wxGDIRefData
28 wxPenRefData(const wxColour& colour = wxNullColour,
29 int width = 1, int style = wxSOLID,
30 const wxBitmap& stipple = wxNullBitmap);
31 wxPenRefData(const wxPenRefData& data);
32 ~wxPenRefData() { FreeCocoaNSColor(); FreeCocoaDash(); }
34 void SetWidth(int Width) { m_width = Width; }
35 void SetStyle(int Style)
40 void SetJoin(int Join) { m_join = Join; }
41 void SetCap(int Cap) { m_cap = Cap; }
42 void SetColour(const wxColour& col) { FreeCocoaNSColor(); m_colour = col; }
43 void SetDashes(int nb_dashes, const wxDash *Dash)
47 m_dash = (wxDash *)Dash;
49 void SetStipple(const wxBitmap& Stipple)
55 WX_NSColor GetNSColor();
56 int GetCocoaLineDash(const CGFloat **pattern);
58 void FreeCocoaNSColor();
69 WX_NSColor m_cocoaNSColor;
72 // Predefined dash patterns
73 static const int scm_countDot;
74 static const CGFloat scm_patternDot[];
75 static const int scm_countLongDash;
76 static const CGFloat scm_patternLongDash[];
77 static const int scm_countShortDash;
78 static const CGFloat scm_patternShortDash[];
79 static const int scm_countDotDash;
80 static const CGFloat scm_patternDotDash[];
82 friend class WXDLLIMPEXP_FWD_CORE wxPen;
85 // Don't allow assignment
86 wxPenRefData& operator=(const wxPenRefData& data);
89 const int wxPenRefData::scm_countDot = 1;
90 const CGFloat wxPenRefData::scm_patternDot[] = {
93 const int wxPenRefData::scm_countLongDash = 1;
94 const CGFloat wxPenRefData::scm_patternLongDash[] = {
97 const int wxPenRefData::scm_countShortDash = 1;
98 const CGFloat wxPenRefData::scm_patternShortDash[] = {
101 const int wxPenRefData::scm_countDotDash = 4;
102 const CGFloat wxPenRefData::scm_patternDotDash[] = {
109 #define M_PENDATA ((wxPenRefData *)m_refData)
111 inline wxPenRefData::wxPenRefData(const wxColour& colour,
112 int width, int style, const wxBitmap& stipple)
116 m_join = wxJOIN_ROUND;
122 m_cocoaNSColor = nil;
126 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
128 m_width = data.m_width;
129 m_style = data.m_style;
130 m_join = data.m_join;
132 m_colour = data.m_colour;
133 m_nbDash = data.m_nbDash;
134 m_dash = data.m_dash;
135 m_stipple = data.m_stipple;
136 m_cocoaNSColor = [data.m_cocoaNSColor retain];
140 inline void wxPenRefData::FreeCocoaNSColor()
142 [m_cocoaNSColor release];
143 m_cocoaNSColor = nil;
146 inline void wxPenRefData::FreeCocoaDash()
152 inline WX_NSColor wxPenRefData::GetNSColor()
159 m_cocoaNSColor = [[NSColor clearColor] retain];
162 // wxBitmap isn't implemented yet
163 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
165 // The hatch brushes are going to be tricky
166 case wxBDIAGONAL_HATCH:
167 case wxCROSSDIAG_HATCH:
168 case wxFDIAGONAL_HATCH:
170 case wxHORIZONTAL_HATCH:
171 case wxVERTICAL_HATCH:
173 // Dot/dashed pens use solid colors
180 NSColor *colour_NSColor = m_colour.GetNSColor();
182 colour_NSColor = [NSColor clearColor];
183 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
187 return m_cocoaNSColor;
190 int wxPenRefData::GetCocoaLineDash(const CGFloat **pattern)
196 count = scm_countDot;
198 *pattern = scm_patternDot;
201 count = scm_countLongDash;
203 *pattern = scm_patternLongDash;
206 count = scm_countShortDash;
208 *pattern = scm_patternShortDash;
211 count = scm_countDotDash;
213 *pattern = scm_patternDotDash;
221 m_cocoaDash = new CGFloat[count];
222 for(int i=0; i<count; i++)
223 m_cocoaDash[i] = m_dash[i];
225 *pattern = m_cocoaDash;
230 case wxBDIAGONAL_HATCH:
231 case wxCROSSDIAG_HATCH:
232 case wxFDIAGONAL_HATCH:
234 case wxHORIZONTAL_HATCH:
235 case wxVERTICAL_HATCH:
245 // ========================================================================
247 // ========================================================================
248 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
258 // Should implement Create
259 wxPen::wxPen(const wxColour& colour, int width, int style)
261 m_refData = new wxPenRefData(colour,width,style);
264 wxPen::wxPen(const wxBitmap& stipple, int width)
266 m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
269 wxGDIRefData *wxPen::CreateGDIRefData() const
271 return new wxPenRefData;
274 wxGDIRefData *wxPen::CloneGDIRefData(const wxGDIRefData *data) const
276 return new wxPenRefData(*(wxPenRefData *)data);
279 void wxPen::SetWidth(int Width)
282 M_PENDATA->SetWidth(Width);
285 void wxPen::SetStyle(int Style)
288 M_PENDATA->SetStyle(Style);
291 void wxPen::SetJoin(int Join)
294 M_PENDATA->SetJoin(Join);
297 void wxPen::SetCap(int Cap)
300 M_PENDATA->SetCap(Cap);
303 void wxPen::SetColour(const wxColour& col)
306 M_PENDATA->SetColour(col);
309 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
312 M_PENDATA->SetColour(wxColour(r, g, b));
315 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
318 M_PENDATA->SetDashes(nb_dashes, Dash);
321 void wxPen::SetStipple(const wxBitmap& Stipple)
324 M_PENDATA->SetStipple(Stipple);
327 wxColour& wxPen::GetColour() const
329 return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour);
332 int wxPen::GetWidth() const
334 return (M_PENDATA ? M_PENDATA->m_width : 0);
337 int wxPen::GetStyle() const
339 return (M_PENDATA ? M_PENDATA->m_style : 0);
342 int wxPen::GetJoin() const
344 return (M_PENDATA ? M_PENDATA->m_join : 0);
347 int wxPen::GetCap() const
349 return (M_PENDATA ? M_PENDATA->m_cap : 0);
352 int wxPen::GetDashes(wxDash **ptr) const
354 *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
357 wxBitmap *wxPen::GetStipple() const
359 return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL);
362 WX_NSColor wxPen::GetNSColor()
364 return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
367 int wxPen::GetCocoaLineDash(const CGFloat **pattern)
370 return M_PENDATA->GetCocoaLineDash(pattern);