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
27 friend class WXDLLEXPORT wxPen;
29 wxPenRefData(const wxColour& colour = wxNullColour,
30 int width = 1, int style = wxSOLID,
31 const wxBitmap& stipple = wxNullBitmap);
32 wxPenRefData(const wxPenRefData& data);
33 ~wxPenRefData() { FreeCocoaNSColor(); FreeCocoaDash(); }
35 void SetWidth(int Width) { m_width = Width; }
36 void SetStyle(int Style)
41 void SetJoin(int Join) { m_join = Join; }
42 void SetCap(int Cap) { m_cap = Cap; }
43 void SetColour(const wxColour& col) { FreeCocoaNSColor(); m_colour = col; }
44 void SetDashes(int nb_dashes, const wxDash *Dash)
48 m_dash = (wxDash *)Dash;
50 void SetStipple(const wxBitmap& Stipple)
56 WX_NSColor GetNSColor();
57 int GetCocoaLineDash(const CGFloat **pattern);
59 void FreeCocoaNSColor();
70 WX_NSColor m_cocoaNSColor;
73 // Predefined dash patterns
74 static const int scm_countDot;
75 static const CGFloat scm_patternDot[];
76 static const int scm_countLongDash;
77 static const CGFloat scm_patternLongDash[];
78 static const int scm_countShortDash;
79 static const CGFloat scm_patternShortDash[];
80 static const int scm_countDotDash;
81 static const CGFloat scm_patternDotDash[];
83 // Don't allow assignment
84 wxPenRefData& operator=(const wxPenRefData& data);
87 const int wxPenRefData::scm_countDot = 1;
88 const CGFloat wxPenRefData::scm_patternDot[] = {
91 const int wxPenRefData::scm_countLongDash = 1;
92 const CGFloat wxPenRefData::scm_patternLongDash[] = {
95 const int wxPenRefData::scm_countShortDash = 1;
96 const CGFloat wxPenRefData::scm_patternShortDash[] = {
99 const int wxPenRefData::scm_countDotDash = 4;
100 const CGFloat wxPenRefData::scm_patternDotDash[] = {
107 #define M_PENDATA ((wxPenRefData *)m_refData)
109 inline wxPenRefData::wxPenRefData(const wxColour& colour,
110 int width, int style, const wxBitmap& stipple)
114 m_join = wxJOIN_ROUND;
120 m_cocoaNSColor = nil;
124 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
126 m_width = data.m_width;
127 m_style = data.m_style;
128 m_join = data.m_join;
130 m_colour = data.m_colour;
131 m_nbDash = data.m_nbDash;
132 m_dash = data.m_dash;
133 m_stipple = data.m_stipple;
134 m_cocoaNSColor = [data.m_cocoaNSColor retain];
138 inline void wxPenRefData::FreeCocoaNSColor()
140 [m_cocoaNSColor release];
141 m_cocoaNSColor = nil;
144 inline void wxPenRefData::FreeCocoaDash()
150 inline WX_NSColor wxPenRefData::GetNSColor()
157 m_cocoaNSColor = [[NSColor clearColor] retain];
160 // wxBitmap isn't implemented yet
161 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
163 // The hatch brushes are going to be tricky
164 case wxBDIAGONAL_HATCH:
165 case wxCROSSDIAG_HATCH:
166 case wxFDIAGONAL_HATCH:
168 case wxHORIZONTAL_HATCH:
169 case wxVERTICAL_HATCH:
171 // Dot/dashed pens use solid colors
178 NSColor *colour_NSColor = m_colour.GetNSColor();
180 colour_NSColor = [NSColor clearColor];
181 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
185 return m_cocoaNSColor;
188 int wxPenRefData::GetCocoaLineDash(const CGFloat **pattern)
194 count = scm_countDot;
196 *pattern = scm_patternDot;
199 count = scm_countLongDash;
201 *pattern = scm_patternLongDash;
204 count = scm_countShortDash;
206 *pattern = scm_patternShortDash;
209 count = scm_countDotDash;
211 *pattern = scm_patternDotDash;
219 m_cocoaDash = new CGFloat[count];
220 for(int i=0; i<count; i++)
221 m_cocoaDash[i] = m_dash[i];
223 *pattern = m_cocoaDash;
228 case wxBDIAGONAL_HATCH:
229 case wxCROSSDIAG_HATCH:
230 case wxFDIAGONAL_HATCH:
232 case wxHORIZONTAL_HATCH:
233 case wxVERTICAL_HATCH:
243 // ========================================================================
245 // ========================================================================
246 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
256 // Should implement Create
257 wxPen::wxPen(const wxColour& colour, int width, int style)
259 m_refData = new wxPenRefData(colour,width,style);
262 wxPen::wxPen(const wxBitmap& stipple, int width)
264 m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
267 wxObjectRefData *wxPen::CreateRefData() const
269 return new wxPenRefData;
272 wxObjectRefData *wxPen::CloneRefData(const wxObjectRefData *data) const
274 return new wxPenRefData(*(wxPenRefData *)data);
277 void wxPen::SetWidth(int Width)
280 M_PENDATA->SetWidth(Width);
283 void wxPen::SetStyle(int Style)
286 M_PENDATA->SetStyle(Style);
289 void wxPen::SetJoin(int Join)
292 M_PENDATA->SetJoin(Join);
295 void wxPen::SetCap(int Cap)
298 M_PENDATA->SetCap(Cap);
301 void wxPen::SetColour(const wxColour& col)
304 M_PENDATA->SetColour(col);
307 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
310 M_PENDATA->SetColour(wxColour(r, g, b));
313 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
316 M_PENDATA->SetDashes(nb_dashes, Dash);
319 void wxPen::SetStipple(const wxBitmap& Stipple)
322 M_PENDATA->SetStipple(Stipple);
325 wxColour& wxPen::GetColour() const
327 return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour);
330 int wxPen::GetWidth() const
332 return (M_PENDATA ? M_PENDATA->m_width : 0);
335 int wxPen::GetStyle() const
337 return (M_PENDATA ? M_PENDATA->m_style : 0);
340 int wxPen::GetJoin() const
342 return (M_PENDATA ? M_PENDATA->m_join : 0);
345 int wxPen::GetCap() const
347 return (M_PENDATA ? M_PENDATA->m_cap : 0);
350 int wxPen::GetDashes(wxDash **ptr) const
352 *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
355 wxBitmap *wxPen::GetStipple() const
357 return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL);
360 WX_NSColor wxPen::GetNSColor()
362 return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
365 int wxPen::GetCocoaLineDash(const CGFloat **pattern)
368 return M_PENDATA->GetCocoaLineDash(pattern);