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"
20 #include "wx/cocoa/ObjcRef.h"
22 #import <AppKit/NSColor.h>
24 // ========================================================================
26 // ========================================================================
27 class WXDLLEXPORT wxPenRefData: public wxGDIRefData
30 wxPenRefData(const wxColour& colour = wxNullColour,
31 int width = 1, int style = wxSOLID,
32 const wxBitmap& stipple = wxNullBitmap);
33 wxPenRefData(const wxPenRefData& data);
34 ~wxPenRefData() { FreeCocoaNSColor(); FreeCocoaDash(); }
36 void SetWidth(int Width) { m_width = Width; }
37 void SetStyle(int Style)
42 void SetJoin(int Join) { m_join = Join; }
43 void SetCap(int Cap) { m_cap = Cap; }
44 void SetColour(const wxColour& col) { FreeCocoaNSColor(); m_colour = col; }
45 void SetDashes(int nb_dashes, const wxDash *Dash)
49 m_dash = (wxDash *)Dash;
51 void SetStipple(const wxBitmap& Stipple)
57 WX_NSColor GetNSColor();
58 int GetCocoaLineDash(const CGFloat **pattern);
60 void FreeCocoaNSColor();
71 WX_NSColor m_cocoaNSColor;
74 // Predefined dash patterns
75 static const int scm_countDot;
76 static const CGFloat scm_patternDot[];
77 static const int scm_countLongDash;
78 static const CGFloat scm_patternLongDash[];
79 static const int scm_countShortDash;
80 static const CGFloat scm_patternShortDash[];
81 static const int scm_countDotDash;
82 static const CGFloat scm_patternDotDash[];
84 friend class WXDLLIMPEXP_FWD_CORE wxPen;
87 // Don't allow assignment
88 wxPenRefData& operator=(const wxPenRefData& data);
91 const int wxPenRefData::scm_countDot = 1;
92 const CGFloat wxPenRefData::scm_patternDot[] = {
95 const int wxPenRefData::scm_countLongDash = 1;
96 const CGFloat wxPenRefData::scm_patternLongDash[] = {
99 const int wxPenRefData::scm_countShortDash = 1;
100 const CGFloat wxPenRefData::scm_patternShortDash[] = {
103 const int wxPenRefData::scm_countDotDash = 4;
104 const CGFloat wxPenRefData::scm_patternDotDash[] = {
111 #define M_PENDATA ((wxPenRefData *)m_refData)
113 inline wxPenRefData::wxPenRefData(const wxColour& colour,
114 int width, int style, const wxBitmap& stipple)
118 m_join = wxJOIN_ROUND;
124 m_cocoaNSColor = nil;
128 inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
130 m_width = data.m_width;
131 m_style = data.m_style;
132 m_join = data.m_join;
134 m_colour = data.m_colour;
135 m_nbDash = data.m_nbDash;
136 m_dash = data.m_dash;
137 m_stipple = data.m_stipple;
138 m_cocoaNSColor = wxGCSafeRetain(data.m_cocoaNSColor);
142 inline void wxPenRefData::FreeCocoaNSColor()
144 wxGCSafeRelease(m_cocoaNSColor);
145 m_cocoaNSColor = nil;
148 inline void wxPenRefData::FreeCocoaDash()
154 inline WX_NSColor wxPenRefData::GetNSColor()
161 m_cocoaNSColor = wxGCSafeRetain([NSColor clearColor]);
164 // wxBitmap isn't implemented yet
165 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
167 // The hatch brushes are going to be tricky
168 case wxBDIAGONAL_HATCH:
169 case wxCROSSDIAG_HATCH:
170 case wxFDIAGONAL_HATCH:
172 case wxHORIZONTAL_HATCH:
173 case wxVERTICAL_HATCH:
175 // Dot/dashed pens use solid colors
182 NSColor *colour_NSColor = m_colour.GetNSColor();
184 colour_NSColor = [NSColor clearColor];
185 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
186 [wxGCSafeRetain(m_cocoaNSColor) release]; // retain in GC. no change in RR.
190 return m_cocoaNSColor;
193 int wxPenRefData::GetCocoaLineDash(const CGFloat **pattern)
199 count = scm_countDot;
201 *pattern = scm_patternDot;
204 count = scm_countLongDash;
206 *pattern = scm_patternLongDash;
209 count = scm_countShortDash;
211 *pattern = scm_patternShortDash;
214 count = scm_countDotDash;
216 *pattern = scm_patternDotDash;
224 m_cocoaDash = new CGFloat[count];
225 for(int i=0; i<count; i++)
226 m_cocoaDash[i] = m_dash[i];
228 *pattern = m_cocoaDash;
233 case wxBDIAGONAL_HATCH:
234 case wxCROSSDIAG_HATCH:
235 case wxFDIAGONAL_HATCH:
237 case wxHORIZONTAL_HATCH:
238 case wxVERTICAL_HATCH:
248 // ========================================================================
250 // ========================================================================
251 IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
261 // Should implement Create
262 wxPen::wxPen(const wxColour& colour, int width, int style)
264 m_refData = new wxPenRefData(colour,width,style);
267 wxPen::wxPen(const wxBitmap& stipple, int width)
269 m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
272 wxGDIRefData *wxPen::CreateGDIRefData() const
274 return new wxPenRefData;
277 wxGDIRefData *wxPen::CloneGDIRefData(const wxGDIRefData *data) const
279 return new wxPenRefData(*(wxPenRefData *)data);
282 void wxPen::SetWidth(int Width)
285 M_PENDATA->SetWidth(Width);
288 void wxPen::SetStyle(int Style)
291 M_PENDATA->SetStyle(Style);
294 void wxPen::SetJoin(int Join)
297 M_PENDATA->SetJoin(Join);
300 void wxPen::SetCap(int Cap)
303 M_PENDATA->SetCap(Cap);
306 void wxPen::SetColour(const wxColour& col)
309 M_PENDATA->SetColour(col);
312 void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
315 M_PENDATA->SetColour(wxColour(r, g, b));
318 void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
321 M_PENDATA->SetDashes(nb_dashes, Dash);
324 void wxPen::SetStipple(const wxBitmap& Stipple)
327 M_PENDATA->SetStipple(Stipple);
330 wxColour wxPen::GetColour() const
332 return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour);
335 int wxPen::GetWidth() const
337 return (M_PENDATA ? M_PENDATA->m_width : 0);
340 int wxPen::GetStyle() const
342 return (M_PENDATA ? M_PENDATA->m_style : 0);
345 int wxPen::GetJoin() const
347 return (M_PENDATA ? M_PENDATA->m_join : 0);
350 int wxPen::GetCap() const
352 return (M_PENDATA ? M_PENDATA->m_cap : 0);
355 int wxPen::GetDashes(wxDash **ptr) const
357 *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
360 wxBitmap *wxPen::GetStipple() const
362 return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL);
365 WX_NSColor wxPen::GetNSColor()
367 return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
370 int wxPen::GetCocoaLineDash(const CGFloat **pattern)
373 return M_PENDATA->GetCocoaLineDash(pattern);