1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/brush.mm
4 // Author: David Elliott <dfe@cox.net>
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #include "wx/colour.h"
20 #import <AppKit/NSColor.h>
22 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
25 wxBrushRefData(const wxColour& colour = wxNullColour,
26 wxBrushStyle style = wxBRUSHSTYLE_SOLID);
27 wxBrushRefData(const wxBitmap& stipple);
28 wxBrushRefData(const wxBrushRefData& data);
29 virtual ~wxBrushRefData();
31 WX_NSColor GetNSColor();
34 bool operator==(const wxBrushRefData& data) const;
37 const wxColour& GetColour() const { return m_colour; }
38 wxBrushStyle GetStyle() const { return m_style; }
39 wxBitmap *GetStipple() { return &m_stipple; }
41 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
42 void SetStyle(wxBrushStyle style) { Free(); m_style = style; }
43 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
46 void DoSetStipple(const wxBitmap& stipple);
48 WX_NSColor m_cocoaNSColor;
53 // no assignment operator, the objects of this class are shared and never
54 // assigned after being created once
55 wxBrushRefData& operator=(const wxBrushRefData&);
58 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
59 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
61 wxBrushRefData::wxBrushRefData(const wxColour& colour, wxBrushStyle style)
63 m_cocoaNSColor = NULL;
68 wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
70 m_cocoaNSColor = NULL;
71 DoSetStipple(stipple);
74 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
76 m_cocoaNSColor = data.m_cocoaNSColor;
77 [m_cocoaNSColor retain];
78 m_style = data.m_style;
79 m_stipple = data.m_stipple;
80 m_colour = data.m_colour;
83 wxBrushRefData::~wxBrushRefData()
88 void wxBrushRefData::Free()
90 [m_cocoaNSColor release];
91 m_cocoaNSColor = NULL;
94 bool wxBrushRefData::operator==(const wxBrushRefData& data) const
96 // don't compare our NSColor
97 return m_style == data.m_style &&
98 m_colour == data.m_colour &&
99 m_stipple.IsSameAs(data.m_stipple);
102 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
105 m_style = stipple.GetMask() ? wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
106 : wxBRUSHSTYLE_STIPPLE;
109 WX_NSColor wxBrushRefData::GetNSColor()
116 m_cocoaNSColor = [[NSColor clearColor] retain];
119 // wxBitmap isn't implemented yet
120 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
122 case wxSTIPPLE_MASK_OPAQUE:
123 // This should be easy when wxBitmap works.
125 // The hatch brushes are going to be tricky
126 case wxBDIAGONAL_HATCH:
127 case wxCROSSDIAG_HATCH:
128 case wxFDIAGONAL_HATCH:
130 case wxHORIZONTAL_HATCH:
131 case wxVERTICAL_HATCH:
134 NSColor *colour_NSColor = m_colour.GetNSColor();
136 colour_NSColor = [NSColor clearColor];
137 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
141 return m_cocoaNSColor;
153 wxBrush::wxBrush(const wxColour& col, wxBrushStyle style)
155 m_refData = new wxBrushRefData(col, style);
158 wxBrush::wxBrush(const wxColour& col, int style)
160 m_refData = new wxBrushRefData(col, (wxBrushStyle)style);
163 wxBrush::wxBrush(const wxBitmap& stipple)
165 m_refData = new wxBrushRefData(stipple);
168 wxGDIRefData *wxBrush::CreateGDIRefData() const
170 return new wxBrushRefData;
173 wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
175 return new wxBrushRefData(*(wxBrushRefData *)data);
178 void wxBrush::SetColour(const wxColour& col)
181 M_BRUSHDATA->SetColour(col);
184 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
187 M_BRUSHDATA->SetColour(wxColour(r,g,b));
190 void wxBrush::SetStyle(wxBrushStyle style)
193 M_BRUSHDATA->SetStyle(style);
196 void wxBrush::SetStipple(const wxBitmap& stipple)
199 M_BRUSHDATA->SetStipple(stipple);
202 wxColour wxBrush::GetColour() const
204 wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid brush") );
205 return M_BRUSHDATA->GetColour();
208 wxBrushStyle wxBrush::GetStyle() const
210 wxCHECK_MSG( IsOk(), wxBRUSHSTYLE_INVALID, wxT("invalid brush") );
211 return M_BRUSHDATA->GetStyle();
214 wxBitmap *wxBrush::GetStipple() const
216 wxCHECK_MSG( IsOk(), 0, wxT("invalid brush") );
217 return M_BRUSHDATA->GetStipple();
220 WX_NSColor wxBrush::GetNSColor()
223 return [NSColor clearColor];
224 return M_BRUSHDATA->GetNSColor();