1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/pen.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/bitmap.h"
15 #include "wx/colour.h"
17 #import <AppKit/NSColor.h>
19 // ========================================================================
21 // ========================================================================
22 class WXDLLEXPORT wxPenRefData: public wxGDIRefData
24 friend class WXDLLEXPORT wxPen;
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(); }
32 void SetWidth(int Width) { m_width = Width; }
33 void SetStyle(int Style)
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)
45 m_dash = (wxDash *)Dash;
47 void SetStipple(const wxBitmap& Stipple)
53 WX_NSColor GetNSColor();
54 int GetCocoaLineDash(const float **pattern);
56 void FreeCocoaNSColor();
67 WX_NSColor m_cocoaNSColor;
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[];
80 // Don't allow assignment
81 wxPenRefData& operator=(const wxPenRefData& data);
84 const int wxPenRefData::scm_countDot = 1;
85 const float wxPenRefData::scm_patternDot[] = {
88 const int wxPenRefData::scm_countLongDash = 1;
89 const float wxPenRefData::scm_patternLongDash[] = {
92 const int wxPenRefData::scm_countShortDash = 1;
93 const float wxPenRefData::scm_patternShortDash[] = {
96 const int wxPenRefData::scm_countDotDash = 4;
97 const float wxPenRefData::scm_patternDotDash[] = {
104 #define M_PENDATA ((wxPenRefData *)m_refData)
106 inline wxPenRefData::wxPenRefData(const wxColour& colour,
107 int width, int style, const wxBitmap& stipple)
111 m_join = wxJOIN_ROUND;
117 m_cocoaNSColor = nil;
121 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
123 m_width = data.m_width;
124 m_style = data.m_style;
125 m_join = data.m_join;
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];
135 inline void wxPenRefData::FreeCocoaNSColor()
137 [m_cocoaNSColor release];
138 m_cocoaNSColor = nil;
141 inline void wxPenRefData::FreeCocoaDash()
147 inline WX_NSColor wxPenRefData::GetNSColor()
154 m_cocoaNSColor = [[NSColor clearColor] retain];
157 // wxBitmap isn't implemented yet
158 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
160 // The hatch brushes are going to be tricky
161 case wxBDIAGONAL_HATCH:
162 case wxCROSSDIAG_HATCH:
163 case wxFDIAGONAL_HATCH:
165 case wxHORIZONTAL_HATCH:
166 case wxVERTICAL_HATCH:
168 // Dot/dashed pens use solid colors
175 NSColor *colour_NSColor = m_colour.GetNSColor();
177 colour_NSColor = [NSColor clearColor];
178 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
182 return m_cocoaNSColor;
185 int wxPenRefData::GetCocoaLineDash(const float **pattern)
191 count = scm_countDot;
193 *pattern = scm_patternDot;
196 count = scm_countLongDash;
198 *pattern = scm_patternLongDash;
201 count = scm_countShortDash;
203 *pattern = scm_patternShortDash;
206 count = scm_countDotDash;
208 *pattern = scm_patternDotDash;
216 m_cocoaDash = new float[count];
217 for(int i=0; i<count; i++)
218 m_cocoaDash[i] = m_dash[i];
220 *pattern = m_cocoaDash;
225 case wxBDIAGONAL_HATCH:
226 case wxCROSSDIAG_HATCH:
227 case wxFDIAGONAL_HATCH:
229 case wxHORIZONTAL_HATCH:
230 case wxVERTICAL_HATCH:
240 // ========================================================================
242 // ========================================================================
243 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
253 // Should implement Create
254 wxPen::wxPen(const wxColour& colour, int width, int style)
256 m_refData = new wxPenRefData(colour,width,style);
259 wxPen::wxPen(const wxBitmap& stipple, int width)
261 m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
264 wxObjectRefData *wxPen::CreateRefData() const
266 return new wxPenRefData;
269 wxObjectRefData *wxPen::CloneRefData(const wxObjectRefData *data) const
271 return new wxPenRefData(*(wxPenRefData *)data);
274 void wxPen::SetWidth(int Width)
277 M_PENDATA->SetWidth(Width);
280 void wxPen::SetStyle(int Style)
283 M_PENDATA->SetStyle(Style);
286 void wxPen::SetJoin(int Join)
289 M_PENDATA->SetJoin(Join);
292 void wxPen::SetCap(int Cap)
295 M_PENDATA->SetCap(Cap);
298 void wxPen::SetColour(const wxColour& col)
301 M_PENDATA->SetColour(col);
304 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
307 M_PENDATA->SetColour(wxColour(r, g, b));
310 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
313 M_PENDATA->SetDashes(nb_dashes, Dash);
316 void wxPen::SetStipple(const wxBitmap& Stipple)
319 M_PENDATA->SetStipple(Stipple);
322 wxColour& wxPen::GetColour() const
324 return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour);
327 int wxPen::GetWidth() const
329 return (M_PENDATA ? M_PENDATA->m_width : 0);
332 int wxPen::GetStyle() const
334 return (M_PENDATA ? M_PENDATA->m_style : 0);
337 int wxPen::GetJoin() const
339 return (M_PENDATA ? M_PENDATA->m_join : 0);
342 int wxPen::GetCap() const
344 return (M_PENDATA ? M_PENDATA->m_cap : 0);
347 int wxPen::GetDashes(wxDash **ptr) const
349 *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
352 wxBitmap *wxPen::GetStipple() const
354 return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL);
357 WX_NSColor wxPen::GetNSColor()
359 return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
362 int wxPen::GetCocoaLineDash(const float **pattern)
365 return M_PENDATA->GetCocoaLineDash(pattern);