1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/brush.mm
4 // Author: David Elliott <dfe@cox.net>
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/colour.h"
21 #import <AppKit/NSColor.h>
23 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
26 wxBrushRefData(const wxColour& colour = wxNullColour,
27 wxBrushStyle style = wxBRUSHSTYLE_SOLID);
28 wxBrushRefData(const wxBitmap& stipple);
29 wxBrushRefData(const wxBrushRefData& data);
30 virtual ~wxBrushRefData();
32 WX_NSColor GetNSColor();
35 bool operator==(const wxBrushRefData& data) const;
38 const wxColour& GetColour() const { return m_colour; }
39 wxBrushStyle GetStyle() const { return m_style; }
40 wxBitmap *GetStipple() { return &m_stipple; }
42 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
43 void SetStyle(wxBrushStyle style) { Free(); m_style = style; }
44 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
47 void DoSetStipple(const wxBitmap& stipple);
49 WX_NSColor m_cocoaNSColor;
54 // no assignment operator, the objects of this class are shared and never
55 // assigned after being created once
56 wxBrushRefData& operator=(const wxBrushRefData&);
59 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
60 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
62 wxBrushRefData::wxBrushRefData(const wxColour& colour, wxBrushStyle style)
64 m_cocoaNSColor = NULL;
69 wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
71 m_cocoaNSColor = NULL;
72 DoSetStipple(stipple);
75 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
77 m_cocoaNSColor = data.m_cocoaNSColor;
78 [m_cocoaNSColor retain];
79 m_style = data.m_style;
80 m_stipple = data.m_stipple;
81 m_colour = data.m_colour;
84 wxBrushRefData::~wxBrushRefData()
89 void wxBrushRefData::Free()
91 [m_cocoaNSColor release];
92 m_cocoaNSColor = NULL;
95 bool wxBrushRefData::operator==(const wxBrushRefData& data) const
97 // don't compare our NSColor
98 return m_style == data.m_style &&
99 m_colour == data.m_colour &&
100 m_stipple.IsSameAs(data.m_stipple);
103 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
106 m_style = stipple.GetMask() ? wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
107 : wxBRUSHSTYLE_STIPPLE;
110 WX_NSColor wxBrushRefData::GetNSColor()
117 m_cocoaNSColor = [[NSColor clearColor] retain];
120 // wxBitmap isn't implemented yet
121 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
123 case wxSTIPPLE_MASK_OPAQUE:
124 // This should be easy when wxBitmap works.
126 // The hatch brushes are going to be tricky
127 case wxBDIAGONAL_HATCH:
128 case wxCROSSDIAG_HATCH:
129 case wxFDIAGONAL_HATCH:
131 case wxHORIZONTAL_HATCH:
132 case wxVERTICAL_HATCH:
135 NSColor *colour_NSColor = m_colour.GetNSColor();
137 colour_NSColor = [NSColor clearColor];
138 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
142 return m_cocoaNSColor;
154 wxBrush::wxBrush(const wxColour& col, wxBrushStyle style)
156 m_refData = new wxBrushRefData(col, style);
159 wxBrush::wxBrush(const wxColour& col, int style)
161 m_refData = new wxBrushRefData(col, (wxBrushStyle)style);
164 wxBrush::wxBrush(const wxBitmap& stipple)
166 m_refData = new wxBrushRefData(stipple);
169 wxGDIRefData *wxBrush::CreateGDIRefData() const
171 return new wxBrushRefData;
174 wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
176 return new wxBrushRefData(*(wxBrushRefData *)data);
179 void wxBrush::SetColour(const wxColour& col)
182 M_BRUSHDATA->SetColour(col);
185 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
188 M_BRUSHDATA->SetColour(wxColour(r,g,b));
191 void wxBrush::SetStyle(wxBrushStyle style)
194 M_BRUSHDATA->SetStyle(style);
197 void wxBrush::SetStipple(const wxBitmap& stipple)
200 M_BRUSHDATA->SetStipple(stipple);
203 wxColour wxBrush::GetColour() const
205 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
206 return M_BRUSHDATA->GetColour();
209 wxBrushStyle wxBrush::GetStyle() const
211 wxCHECK_MSG( Ok(), wxBRUSHSTYLE_INVALID, _T("invalid brush") );
212 return M_BRUSHDATA->GetStyle();
215 wxBitmap *wxBrush::GetStipple() const
217 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
218 return M_BRUSHDATA->GetStipple();
221 WX_NSColor wxBrush::GetNSColor()
224 return [NSColor clearColor];
225 return M_BRUSHDATA->GetNSColor();