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, int style = wxSOLID);
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 int GetStyle() const { return m_style; }
39 wxBitmap *GetStipple() { return &m_stipple; }
41 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
42 void SetStyle(int 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, int 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 == data.m_stipple;
102 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
105 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
108 WX_NSColor wxBrushRefData::GetNSColor()
115 m_cocoaNSColor = [[NSColor clearColor] retain];
118 // wxBitmap isn't implemented yet
119 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
121 case wxSTIPPLE_MASK_OPAQUE:
122 // This should be easy when wxBitmap works.
124 // The hatch brushes are going to be tricky
125 case wxBDIAGONAL_HATCH:
126 case wxCROSSDIAG_HATCH:
127 case wxFDIAGONAL_HATCH:
129 case wxHORIZONTAL_HATCH:
130 case wxVERTICAL_HATCH:
133 NSColor *colour_NSColor = m_colour.GetNSColor();
135 colour_NSColor = [NSColor clearColor];
136 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
140 return m_cocoaNSColor;
152 wxBrush::wxBrush(const wxColour& col, int style)
154 m_refData = new wxBrushRefData(col, style);
157 wxBrush::wxBrush(const wxBitmap& stipple)
159 m_refData = new wxBrushRefData(stipple);
162 wxObjectRefData *wxBrush::CreateRefData() const
164 return new wxBrushRefData;
167 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
169 return new wxBrushRefData(*(wxBrushRefData *)data);
172 void wxBrush::SetColour(const wxColour& col)
175 M_BRUSHDATA->SetColour(col);
178 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
181 M_BRUSHDATA->SetColour(wxColour(r,g,b));
184 void wxBrush::SetStyle(int style)
187 M_BRUSHDATA->SetStyle(style);
190 void wxBrush::SetStipple(const wxBitmap& stipple)
193 M_BRUSHDATA->SetStipple(stipple);
196 wxColour wxBrush::GetColour() const
198 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
199 return M_BRUSHDATA->GetColour();
202 int wxBrush::GetStyle() const
204 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
205 return M_BRUSHDATA->GetStyle();
208 wxBitmap *wxBrush::GetStipple() const
210 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
211 return M_BRUSHDATA->GetStipple();
214 WX_NSColor wxBrush::GetNSColor()
217 return [NSColor clearColor];
218 return M_BRUSHDATA->GetNSColor();