1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/pen.mm
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/bitmap.h"
16 #include "wx/colour.h"
19 #include "wx/cocoa/ObjcRef.h"
21 #import <AppKit/NSColor.h>
23 // ========================================================================
25 // ========================================================================
26 class WXDLLEXPORT wxPenRefData: public wxGDIRefData
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 friend class WXDLLIMPEXP_FWD_CORE wxPen;
86 // Don't allow assignment
87 wxPenRefData& operator=(const wxPenRefData& data);
90 const int wxPenRefData::scm_countDot = 1;
91 const CGFloat wxPenRefData::scm_patternDot[] = {
94 const int wxPenRefData::scm_countLongDash = 1;
95 const CGFloat wxPenRefData::scm_patternLongDash[] = {
98 const int wxPenRefData::scm_countShortDash = 1;
99 const CGFloat wxPenRefData::scm_patternShortDash[] = {
102 const int wxPenRefData::scm_countDotDash = 4;
103 const CGFloat wxPenRefData::scm_patternDotDash[] = {
110 #define M_PENDATA ((wxPenRefData *)m_refData)
112 inline wxPenRefData::wxPenRefData(const wxColour& colour,
113 int width, int style, const wxBitmap& stipple)
117 m_join = wxJOIN_ROUND;
123 m_cocoaNSColor = nil;
127 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
129 m_width = data.m_width;
130 m_style = data.m_style;
131 m_join = data.m_join;
133 m_colour = data.m_colour;
134 m_nbDash = data.m_nbDash;
135 m_dash = data.m_dash;
136 m_stipple = data.m_stipple;
137 m_cocoaNSColor = wxGCSafeRetain(data.m_cocoaNSColor);
141 inline void wxPenRefData::FreeCocoaNSColor()
143 wxGCSafeRelease(m_cocoaNSColor);
144 m_cocoaNSColor = nil;
147 inline void wxPenRefData::FreeCocoaDash()
153 inline WX_NSColor wxPenRefData::GetNSColor()
160 m_cocoaNSColor = wxGCSafeRetain([NSColor clearColor]);
163 // wxBitmap isn't implemented yet
164 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
166 // The hatch brushes are going to be tricky
167 case wxBDIAGONAL_HATCH:
168 case wxCROSSDIAG_HATCH:
169 case wxFDIAGONAL_HATCH:
171 case wxHORIZONTAL_HATCH:
172 case wxVERTICAL_HATCH:
174 // Dot/dashed pens use solid colors
181 NSColor *colour_NSColor = m_colour.GetNSColor();
183 colour_NSColor = [NSColor clearColor];
184 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
185 [wxGCSafeRetain(m_cocoaNSColor) release]; // retain in GC. no change in RR.
189 return m_cocoaNSColor;
192 int wxPenRefData::GetCocoaLineDash(const CGFloat **pattern)
198 count = scm_countDot;
200 *pattern = scm_patternDot;
203 count = scm_countLongDash;
205 *pattern = scm_patternLongDash;
208 count = scm_countShortDash;
210 *pattern = scm_patternShortDash;
213 count = scm_countDotDash;
215 *pattern = scm_patternDotDash;
223 m_cocoaDash = new CGFloat[count];
224 for(int i=0; i<count; i++)
225 m_cocoaDash[i] = m_dash[i];
227 *pattern = m_cocoaDash;
232 case wxBDIAGONAL_HATCH:
233 case wxCROSSDIAG_HATCH:
234 case wxFDIAGONAL_HATCH:
236 case wxHORIZONTAL_HATCH:
237 case wxVERTICAL_HATCH:
247 // ========================================================================
249 // ========================================================================
250 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
260 // Should implement Create
261 wxPen::wxPen(const wxColour& colour, int width, int style)
263 m_refData = new wxPenRefData(colour,width,style);
266 wxPen::wxPen(const wxBitmap& stipple, int width)
268 m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
271 wxGDIRefData *wxPen::CreateGDIRefData() const
273 return new wxPenRefData;
276 wxGDIRefData *wxPen::CloneGDIRefData(const wxGDIRefData *data) const
278 return new wxPenRefData(*(wxPenRefData *)data);
281 void wxPen::SetWidth(int Width)
284 M_PENDATA->SetWidth(Width);
287 void wxPen::SetStyle(int Style)
290 M_PENDATA->SetStyle(Style);
293 void wxPen::SetJoin(int Join)
296 M_PENDATA->SetJoin(Join);
299 void wxPen::SetCap(int Cap)
302 M_PENDATA->SetCap(Cap);
305 void wxPen::SetColour(const wxColour& col)
308 M_PENDATA->SetColour(col);
311 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
314 M_PENDATA->SetColour(wxColour(r, g, b));
317 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
320 M_PENDATA->SetDashes(nb_dashes, Dash);
323 void wxPen::SetStipple(const wxBitmap& Stipple)
326 M_PENDATA->SetStipple(Stipple);
329 wxColour wxPen::GetColour() const
331 return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour);
334 int wxPen::GetWidth() const
336 return (M_PENDATA ? M_PENDATA->m_width : 0);
339 int wxPen::GetStyle() const
341 return (M_PENDATA ? M_PENDATA->m_style : 0);
344 int wxPen::GetJoin() const
346 return (M_PENDATA ? M_PENDATA->m_join : 0);
349 int wxPen::GetCap() const
351 return (M_PENDATA ? M_PENDATA->m_cap : 0);
354 int wxPen::GetDashes(wxDash **ptr) const
356 *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
359 wxBitmap *wxPen::GetStipple() const
361 return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL);
364 WX_NSColor wxPen::GetNSColor()
366 return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
369 int wxPen::GetCocoaLineDash(const CGFloat **pattern)
372 return M_PENDATA->GetCocoaLineDash(pattern);