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"
20 #include "wx/colour.h"
22 #import <AppKit/NSColor.h>
24 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
27 wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
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 int GetStyle() const { return m_style; }
40 wxBitmap *GetStipple() { return &m_stipple; }
42 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
43 void SetStyle(int 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, int 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 == data.m_stipple;
103 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
106 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
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, int style)
155 m_refData = new wxBrushRefData(col, style);
158 wxBrush::wxBrush(const wxBitmap& stipple)
160 m_refData = new wxBrushRefData(stipple);
163 wxObjectRefData *wxBrush::CreateRefData() const
165 return new wxBrushRefData;
168 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
170 return new wxBrushRefData(*(wxBrushRefData *)data);
173 void wxBrush::SetColour(const wxColour& col)
176 M_BRUSHDATA->SetColour(col);
179 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
182 M_BRUSHDATA->SetColour(wxColour(r,g,b));
185 void wxBrush::SetStyle(int style)
188 M_BRUSHDATA->SetStyle(style);
191 void wxBrush::SetStipple(const wxBitmap& stipple)
194 M_BRUSHDATA->SetStipple(stipple);
197 wxColour wxBrush::GetColour() const
199 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
200 return M_BRUSHDATA->GetColour();
203 int wxBrush::GetStyle() const
205 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
206 return M_BRUSHDATA->GetStyle();
209 wxBitmap *wxBrush::GetStipple() const
211 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
212 return M_BRUSHDATA->GetStipple();
215 WX_NSColor wxBrush::GetNSColor()
218 return [NSColor clearColor];
219 return M_BRUSHDATA->GetNSColor();