]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/brush.mm
in DoSetSize, only call GetPosition if necessary
[wxWidgets.git] / src / cocoa / brush.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/brush.mm
3 // Purpose: wxBrush
4 // Author: David Elliott <dfe@cox.net>
5 // Modified by:
6 // Created: 2003/07/03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/brush.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/utils.h"
18 #include "wx/colour.h"
19 #endif //WX_PRECOMP
20
21 #import <AppKit/NSColor.h>
22
23 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
24 {
25 public:
26 wxBrushRefData(const wxColour& colour = wxNullColour,
27 wxBrushStyle style = wxBRUSHSTYLE_SOLID);
28 wxBrushRefData(const wxBitmap& stipple);
29 wxBrushRefData(const wxBrushRefData& data);
30 virtual ~wxBrushRefData();
31
32 WX_NSColor GetNSColor();
33 void Free();
34
35 bool operator==(const wxBrushRefData& data) const;
36
37 // accessors
38 const wxColour& GetColour() const { return m_colour; }
39 wxBrushStyle GetStyle() const { return m_style; }
40 wxBitmap *GetStipple() { return &m_stipple; }
41
42 void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
43 void SetStyle(wxBrushStyle style) { Free(); m_style = style; }
44 void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
45
46 private:
47 void DoSetStipple(const wxBitmap& stipple);
48
49 WX_NSColor m_cocoaNSColor;
50 wxBrushStyle m_style;
51 wxBitmap m_stipple;
52 wxColour m_colour;
53
54 // no assignment operator, the objects of this class are shared and never
55 // assigned after being created once
56 wxBrushRefData& operator=(const wxBrushRefData&);
57 };
58
59 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
60 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
61
62 wxBrushRefData::wxBrushRefData(const wxColour& colour, wxBrushStyle style)
63 {
64 m_cocoaNSColor = NULL;
65 m_style = style;
66 m_colour = colour;
67 }
68
69 wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
70 {
71 m_cocoaNSColor = NULL;
72 DoSetStipple(stipple);
73 }
74
75 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
76 {
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;
82 }
83
84 wxBrushRefData::~wxBrushRefData()
85 {
86 Free();
87 }
88
89 void wxBrushRefData::Free()
90 {
91 [m_cocoaNSColor release];
92 m_cocoaNSColor = NULL;
93 }
94
95 bool wxBrushRefData::operator==(const wxBrushRefData& data) const
96 {
97 // don't compare our NSColor
98 return m_style == data.m_style &&
99 m_colour == data.m_colour &&
100 m_stipple.IsSameAs(data.m_stipple);
101 }
102
103 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
104 {
105 m_stipple = stipple;
106 m_style = stipple.GetMask() ? wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
107 : wxBRUSHSTYLE_STIPPLE;
108 }
109
110 WX_NSColor wxBrushRefData::GetNSColor()
111 {
112 if(!m_cocoaNSColor)
113 {
114 switch( m_style )
115 {
116 case wxTRANSPARENT:
117 m_cocoaNSColor = [[NSColor clearColor] retain];
118 break;
119 case wxSTIPPLE:
120 // wxBitmap isn't implemented yet
121 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
122 // break;
123 case wxSTIPPLE_MASK_OPAQUE:
124 // This should be easy when wxBitmap works.
125 // break;
126 // The hatch brushes are going to be tricky
127 case wxBDIAGONAL_HATCH:
128 case wxCROSSDIAG_HATCH:
129 case wxFDIAGONAL_HATCH:
130 case wxCROSS_HATCH:
131 case wxHORIZONTAL_HATCH:
132 case wxVERTICAL_HATCH:
133 default:
134 case wxSOLID:
135 NSColor *colour_NSColor = m_colour.GetNSColor();
136 if(!colour_NSColor)
137 colour_NSColor = [NSColor clearColor];
138 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
139 break;
140 }
141 }
142 return m_cocoaNSColor;
143 }
144
145 // Brushes
146 wxBrush::wxBrush()
147 {
148 }
149
150 wxBrush::~wxBrush()
151 {
152 }
153
154 wxBrush::wxBrush(const wxColour& col, wxBrushStyle style)
155 {
156 m_refData = new wxBrushRefData(col, style);
157 }
158
159 wxBrush::wxBrush(const wxColour& col, int style)
160 {
161 m_refData = new wxBrushRefData(col, (wxBrushStyle)style);
162 }
163
164 wxBrush::wxBrush(const wxBitmap& stipple)
165 {
166 m_refData = new wxBrushRefData(stipple);
167 }
168
169 wxGDIRefData *wxBrush::CreateGDIRefData() const
170 {
171 return new wxBrushRefData;
172 }
173
174 wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const
175 {
176 return new wxBrushRefData(*(wxBrushRefData *)data);
177 }
178
179 void wxBrush::SetColour(const wxColour& col)
180 {
181 AllocExclusive();
182 M_BRUSHDATA->SetColour(col);
183 }
184
185 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
186 {
187 AllocExclusive();
188 M_BRUSHDATA->SetColour(wxColour(r,g,b));
189 }
190
191 void wxBrush::SetStyle(wxBrushStyle style)
192 {
193 AllocExclusive();
194 M_BRUSHDATA->SetStyle(style);
195 }
196
197 void wxBrush::SetStipple(const wxBitmap& stipple)
198 {
199 AllocExclusive();
200 M_BRUSHDATA->SetStipple(stipple);
201 }
202
203 wxColour wxBrush::GetColour() const
204 {
205 wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid brush") );
206 return M_BRUSHDATA->GetColour();
207 }
208
209 wxBrushStyle wxBrush::GetStyle() const
210 {
211 wxCHECK_MSG( IsOk(), wxBRUSHSTYLE_INVALID, wxT("invalid brush") );
212 return M_BRUSHDATA->GetStyle();
213 }
214
215 wxBitmap *wxBrush::GetStipple() const
216 {
217 wxCHECK_MSG( IsOk(), 0, wxT("invalid brush") );
218 return M_BRUSHDATA->GetStipple();
219 }
220
221 WX_NSColor wxBrush::GetNSColor()
222 {
223 if(!m_refData)
224 return [NSColor clearColor];
225 return M_BRUSHDATA->GetNSColor();
226 }