1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/brush.mm
4 // Author: David Elliott <dfe@cox.net>
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/colour.h"
17 #import <AppKit/NSColor.h>
19 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
22 wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
23 wxBrushRefData(const wxBitmap& stipple);
24 wxBrushRefData(const wxBrushRefData& data);
25 virtual ~wxBrushRefData();
27 WX_NSColor GetNSColor();
30 bool operator==(const wxBrushRefData& data) const;
33 const wxColour& GetColour() const { return m_colour; }
34 int GetStyle() const { return m_style; }
35 wxBitmap *GetStipple() { return &m_stipple; }
37 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
38 void SetStyle(int style) { Free(); m_style = style; }
39 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
42 void DoSetStipple(const wxBitmap& stipple);
44 WX_NSColor m_cocoaNSColor;
49 // no assignment operator, the objects of this class are shared and never
50 // assigned after being created once
51 wxBrushRefData& operator=(const wxBrushRefData&);
54 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
55 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
57 wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
59 m_cocoaNSColor = NULL;
64 wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
66 m_cocoaNSColor = NULL;
67 DoSetStipple(stipple);
70 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
72 m_cocoaNSColor = data.m_cocoaNSColor;
73 [m_cocoaNSColor retain];
74 m_style = data.m_style;
75 m_stipple = data.m_stipple;
76 m_colour = data.m_colour;
79 wxBrushRefData::~wxBrushRefData()
84 void wxBrushRefData::Free()
86 [m_cocoaNSColor release];
87 m_cocoaNSColor = NULL;
90 bool wxBrushRefData::operator==(const wxBrushRefData& data) const
92 // don't compare our NSColor
93 return m_style == data.m_style &&
94 m_colour == data.m_colour &&
95 m_stipple == data.m_stipple;
98 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
101 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
104 WX_NSColor wxBrushRefData::GetNSColor()
111 m_cocoaNSColor = [[NSColor clearColor] retain];
114 // wxBitmap isn't implemented yet
115 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
117 case wxSTIPPLE_MASK_OPAQUE:
118 // This should be easy when wxBitmap works.
120 // The hatch brushes are going to be tricky
121 case wxBDIAGONAL_HATCH:
122 case wxCROSSDIAG_HATCH:
123 case wxFDIAGONAL_HATCH:
125 case wxHORIZONTAL_HATCH:
126 case wxVERTICAL_HATCH:
129 NSColor *colour_NSColor = m_colour.GetNSColor();
131 colour_NSColor = [NSColor clearColor];
132 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
136 return m_cocoaNSColor;
148 wxBrush::wxBrush(const wxColour& col, int style)
150 m_refData = new wxBrushRefData(col, style);
153 wxBrush::wxBrush(const wxBitmap& stipple)
155 m_refData = new wxBrushRefData(stipple);
158 wxObjectRefData *wxBrush::CreateRefData() const
160 return new wxBrushRefData;
163 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
165 return new wxBrushRefData(*(wxBrushRefData *)data);
168 void wxBrush::SetColour(const wxColour& col)
171 M_BRUSHDATA->SetColour(col);
174 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
177 M_BRUSHDATA->SetColour(wxColour(r,g,b));
180 void wxBrush::SetStyle(int style)
183 M_BRUSHDATA->SetStyle(style);
186 void wxBrush::SetStipple(const wxBitmap& stipple)
189 M_BRUSHDATA->SetStipple(stipple);
192 wxColour wxBrush::GetColour() const
194 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
195 return M_BRUSHDATA->GetColour();
198 int wxBrush::GetStyle() const
200 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
201 return M_BRUSHDATA->GetStyle();
204 wxBitmap *wxBrush::GetStipple() const
206 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
207 return M_BRUSHDATA->GetStipple();
210 WX_NSColor wxBrush::GetNSColor()
213 return [NSColor clearColor];
214 return M_BRUSHDATA->GetNSColor();