1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/pen.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/bitmap.h"
17 #include "wx/colour.h"
19 #import <AppKit/NSColor.h>
21 // ========================================================================
23 // ========================================================================
24 class WXDLLEXPORT wxPenRefData: public wxGDIRefData
26 friend class WXDLLEXPORT wxPen;
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 float **pattern);
58 void FreeCocoaNSColor();
69 WX_NSColor m_cocoaNSColor;
72 // Predefined dash patterns
73 static const int scm_countDot;
74 static const float scm_patternDot[];
75 static const int scm_countLongDash;
76 static const float scm_patternLongDash[];
77 static const int scm_countShortDash;
78 static const float scm_patternShortDash[];
79 static const int scm_countDotDash;
80 static const float scm_patternDotDash[];
82 // Don't allow assignment
83 wxPenRefData& operator=(const wxPenRefData& data);
86 const int wxPenRefData::scm_countDot = 1;
87 const float wxPenRefData::scm_patternDot[] = {
90 const int wxPenRefData::scm_countLongDash = 1;
91 const float wxPenRefData::scm_patternLongDash[] = {
94 const int wxPenRefData::scm_countShortDash = 1;
95 const float wxPenRefData::scm_patternShortDash[] = {
98 const int wxPenRefData::scm_countDotDash = 4;
99 const float wxPenRefData::scm_patternDotDash[] = {
106 #define M_PENDATA ((wxPenRefData *)m_refData)
108 inline wxPenRefData::wxPenRefData(const wxColour& colour,
109 int width, int style, const wxBitmap& stipple)
113 m_join = wxJOIN_ROUND;
119 m_cocoaNSColor = nil;
123 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
125 m_width = data.m_width;
126 m_style = data.m_style;
127 m_join = data.m_join;
129 m_colour = data.m_colour;
130 m_nbDash = data.m_nbDash;
131 m_dash = data.m_dash;
132 m_stipple = data.m_stipple;
133 m_cocoaNSColor = [data.m_cocoaNSColor retain];
137 inline void wxPenRefData::FreeCocoaNSColor()
139 [m_cocoaNSColor release];
140 m_cocoaNSColor = nil;
143 inline void wxPenRefData::FreeCocoaDash()
149 inline WX_NSColor wxPenRefData::GetNSColor()
156 m_cocoaNSColor = [[NSColor clearColor] retain];
159 // wxBitmap isn't implemented yet
160 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
162 // The hatch brushes are going to be tricky
163 case wxBDIAGONAL_HATCH:
164 case wxCROSSDIAG_HATCH:
165 case wxFDIAGONAL_HATCH:
167 case wxHORIZONTAL_HATCH:
168 case wxVERTICAL_HATCH:
170 // Dot/dashed pens use solid colors
177 NSColor *colour_NSColor = m_colour.GetNSColor();
179 colour_NSColor = [NSColor clearColor];
180 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
184 return m_cocoaNSColor;
187 int wxPenRefData::GetCocoaLineDash(const float **pattern)
193 count = scm_countDot;
195 *pattern = scm_patternDot;
198 count = scm_countLongDash;
200 *pattern = scm_patternLongDash;
203 count = scm_countShortDash;
205 *pattern = scm_patternShortDash;
208 count = scm_countDotDash;
210 *pattern = scm_patternDotDash;
218 m_cocoaDash = new float[count];
219 for(int i=0; i<count; i++)
220 m_cocoaDash[i] = m_dash[i];
222 *pattern = m_cocoaDash;
227 case wxBDIAGONAL_HATCH:
228 case wxCROSSDIAG_HATCH:
229 case wxFDIAGONAL_HATCH:
231 case wxHORIZONTAL_HATCH:
232 case wxVERTICAL_HATCH:
242 // ========================================================================
244 // ========================================================================
245 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
255 // Should implement Create
256 wxPen::wxPen(const wxColour& colour, int width, int style)
258 m_refData = new wxPenRefData(colour,width,style);
261 wxPen::wxPen(const wxBitmap& stipple, int width)
263 m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
266 wxObjectRefData *wxPen::CreateRefData() const
268 return new wxPenRefData;
271 wxObjectRefData *wxPen::CloneRefData(const wxObjectRefData *data) const
273 return new wxPenRefData(*(wxPenRefData *)data);
276 void wxPen::SetWidth(int Width)
279 M_PENDATA->SetWidth(Width);
282 void wxPen::SetStyle(int Style)
285 M_PENDATA->SetStyle(Style);
288 void wxPen::SetJoin(int Join)
291 M_PENDATA->SetJoin(Join);
294 void wxPen::SetCap(int Cap)
297 M_PENDATA->SetCap(Cap);
300 void wxPen::SetColour(const wxColour& col)
303 M_PENDATA->SetColour(col);
306 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
309 M_PENDATA->SetColour(wxColour(r, g, b));
312 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
315 M_PENDATA->SetDashes(nb_dashes, Dash);
318 void wxPen::SetStipple(const wxBitmap& Stipple)
321 M_PENDATA->SetStipple(Stipple);
324 wxColour& wxPen::GetColour() const
326 return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour);
329 int wxPen::GetWidth() const
331 return (M_PENDATA ? M_PENDATA->m_width : 0);
334 int wxPen::GetStyle() const
336 return (M_PENDATA ? M_PENDATA->m_style : 0);
339 int wxPen::GetJoin() const
341 return (M_PENDATA ? M_PENDATA->m_join : 0);
344 int wxPen::GetCap() const
346 return (M_PENDATA ? M_PENDATA->m_cap : 0);
349 int wxPen::GetDashes(wxDash **ptr) const
351 *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
354 wxBitmap *wxPen::GetStipple() const
356 return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL);
359 WX_NSColor wxPen::GetNSColor()
361 return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
364 int wxPen::GetCocoaLineDash(const float **pattern)
367 return M_PENDATA->GetCocoaLineDash(pattern);