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 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/colour.h"
19 #import <AppKit/NSColor.h>
21 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
24 wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
25 wxBrushRefData(const wxBitmap& stipple);
26 wxBrushRefData(const wxBrushRefData& data);
27 virtual ~wxBrushRefData();
29 WX_NSColor GetNSColor();
32 bool operator==(const wxBrushRefData& data) const;
35 const wxColour& GetColour() const { return m_colour; }
36 int GetStyle() const { return m_style; }
37 wxBitmap *GetStipple() { return &m_stipple; }
39 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
40 void SetStyle(int style) { Free(); m_style = style; }
41 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
44 void DoSetStipple(const wxBitmap& stipple);
46 WX_NSColor m_cocoaNSColor;
51 // no assignment operator, the objects of this class are shared and never
52 // assigned after being created once
53 wxBrushRefData& operator=(const wxBrushRefData&);
56 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
57 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
59 wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
61 m_cocoaNSColor = NULL;
66 wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
68 m_cocoaNSColor = NULL;
69 DoSetStipple(stipple);
72 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
74 m_cocoaNSColor = data.m_cocoaNSColor;
75 [m_cocoaNSColor retain];
76 m_style = data.m_style;
77 m_stipple = data.m_stipple;
78 m_colour = data.m_colour;
81 wxBrushRefData::~wxBrushRefData()
86 void wxBrushRefData::Free()
88 [m_cocoaNSColor release];
89 m_cocoaNSColor = NULL;
92 bool wxBrushRefData::operator==(const wxBrushRefData& data) const
94 // don't compare our NSColor
95 return m_style == data.m_style &&
96 m_colour == data.m_colour &&
97 m_stipple == data.m_stipple;
100 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
103 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
106 WX_NSColor wxBrushRefData::GetNSColor()
113 m_cocoaNSColor = [[NSColor clearColor] retain];
116 // wxBitmap isn't implemented yet
117 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
119 case wxSTIPPLE_MASK_OPAQUE:
120 // This should be easy when wxBitmap works.
122 // The hatch brushes are going to be tricky
123 case wxBDIAGONAL_HATCH:
124 case wxCROSSDIAG_HATCH:
125 case wxFDIAGONAL_HATCH:
127 case wxHORIZONTAL_HATCH:
128 case wxVERTICAL_HATCH:
131 NSColor *colour_NSColor = m_colour.GetNSColor();
133 colour_NSColor = [NSColor clearColor];
134 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
138 return m_cocoaNSColor;
150 wxBrush::wxBrush(const wxColour& col, int style)
152 m_refData = new wxBrushRefData(col, style);
155 wxBrush::wxBrush(const wxBitmap& stipple)
157 m_refData = new wxBrushRefData(stipple);
160 wxObjectRefData *wxBrush::CreateRefData() const
162 return new wxBrushRefData;
165 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
167 return new wxBrushRefData(*(wxBrushRefData *)data);
170 void wxBrush::SetColour(const wxColour& col)
173 M_BRUSHDATA->SetColour(col);
176 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
179 M_BRUSHDATA->SetColour(wxColour(r,g,b));
182 void wxBrush::SetStyle(int style)
185 M_BRUSHDATA->SetStyle(style);
188 void wxBrush::SetStipple(const wxBitmap& stipple)
191 M_BRUSHDATA->SetStipple(stipple);
194 wxColour wxBrush::GetColour() const
196 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
197 return M_BRUSHDATA->GetColour();
200 int wxBrush::GetStyle() const
202 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
203 return M_BRUSHDATA->GetStyle();
206 wxBitmap *wxBrush::GetStipple() const
208 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
209 return M_BRUSHDATA->GetStipple();
212 WX_NSColor wxBrush::GetNSColor()
215 return [NSColor clearColor];
216 return M_BRUSHDATA->GetNSColor();