]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/brush.mm
wxBrushRefData constructor: m_style = style instead of wxSOLID
[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/setup.h"
13 #include "wx/utils.h"
14 #include "wx/brush.h"
15 #include "wx/colour.h"
16
17 #import <AppKit/NSColor.h>
18
19 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
20 {
21 public:
22 wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
23 wxBrushRefData(const wxBitmap& stipple);
24 wxBrushRefData(const wxBrushRefData& data);
25 virtual ~wxBrushRefData();
26
27 WX_NSColor GetNSColor();
28 void Free();
29
30 bool operator==(const wxBrushRefData& data) const;
31
32 // accessors
33 const wxColour& GetColour() const { return m_colour; }
34 int GetStyle() const { return m_style; }
35 wxBitmap *GetStipple() { return &m_stipple; }
36
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); }
40
41 private:
42 void DoSetStipple(const wxBitmap& stipple);
43
44 WX_NSColor m_cocoaNSColor;
45 int m_style;
46 wxBitmap m_stipple;
47 wxColour m_colour;
48
49 // no assignment operator, the objects of this class are shared and never
50 // assigned after being created once
51 wxBrushRefData& operator=(const wxBrushRefData&);
52 };
53
54 #define M_BRUSHDATA ((wxBrushRefData *)m_refData)
55 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
56
57 wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
58 {
59 m_cocoaNSColor = NULL;
60 m_style = style;
61 m_colour = colour;
62 }
63
64 wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
65 {
66 m_cocoaNSColor = NULL;
67 DoSetStipple(stipple);
68 }
69
70 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
71 {
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;
77 }
78
79 wxBrushRefData::~wxBrushRefData()
80 {
81 Free();
82 }
83
84 void wxBrushRefData::Free()
85 {
86 [m_cocoaNSColor release];
87 m_cocoaNSColor = NULL;
88 }
89
90 bool wxBrushRefData::operator==(const wxBrushRefData& data) const
91 {
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;
96 }
97
98 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
99 {
100 m_stipple = stipple;
101 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
102 }
103
104 WX_NSColor wxBrushRefData::GetNSColor()
105 {
106 if(!m_cocoaNSColor)
107 {
108 switch( m_style )
109 {
110 case wxTRANSPARENT:
111 m_cocoaNSColor = [[NSColor clearColor] retain];
112 break;
113 case wxSTIPPLE:
114 // wxBitmap isn't implemented yet
115 // m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
116 // break;
117 case wxSTIPPLE_MASK_OPAQUE:
118 // This should be easy when wxBitmap works.
119 // break;
120 // The hatch brushes are going to be tricky
121 case wxBDIAGONAL_HATCH:
122 case wxCROSSDIAG_HATCH:
123 case wxFDIAGONAL_HATCH:
124 case wxCROSS_HATCH:
125 case wxHORIZONTAL_HATCH:
126 case wxVERTICAL_HATCH:
127 default:
128 case wxSOLID:
129 NSColor *colour_NSColor = m_colour.GetNSColor();
130 if(!colour_NSColor)
131 colour_NSColor = [NSColor clearColor];
132 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
133 break;
134 }
135 }
136 return m_cocoaNSColor;
137 }
138
139 // Brushes
140 wxBrush::wxBrush()
141 {
142 m_refData = new wxBrushRefData;
143 }
144
145 wxBrush::~wxBrush()
146 {
147 }
148
149 wxBrush::wxBrush(const wxColour& col, int style)
150 {
151 m_refData = new wxBrushRefData(col, style);
152 }
153
154 wxBrush::wxBrush(const wxBitmap& stipple)
155 {
156 m_refData = new wxBrushRefData(stipple);
157 }
158
159 wxObjectRefData *wxBrush::CreateRefData() const
160 {
161 return new wxBrushRefData;
162 }
163
164 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
165 {
166 return new wxBrushRefData(*(wxBrushRefData *)data);
167 }
168
169 void wxBrush::SetColour(const wxColour& col)
170 {
171 AllocExclusive();
172 M_BRUSHDATA->SetColour(col);
173 }
174
175 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
176 {
177 AllocExclusive();
178 M_BRUSHDATA->SetColour(wxColour(r,g,b));
179 }
180
181 void wxBrush::SetStyle(int style)
182 {
183 AllocExclusive();
184 M_BRUSHDATA->SetStyle(style);
185 }
186
187 void wxBrush::SetStipple(const wxBitmap& stipple)
188 {
189 AllocExclusive();
190 M_BRUSHDATA->SetStipple(stipple);
191 }
192
193 wxColour wxBrush::GetColour() const
194 {
195 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
196 return M_BRUSHDATA->GetColour();
197 }
198
199 int wxBrush::GetStyle() const
200 {
201 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
202 return M_BRUSHDATA->GetStyle();
203 }
204
205 wxBitmap *wxBrush::GetStipple() const
206 {
207 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
208 return M_BRUSHDATA->GetStipple();
209 }
210
211 WX_NSColor wxBrush::GetNSColor()
212 {
213 wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
214 return M_BRUSHDATA->GetNSColor();
215 }
216