wxNullBrush should not be Ok(), and GetNSColor() should return clearColor.
[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 }
143
144 wxBrush::~wxBrush()
145 {
146 }
147
148 wxBrush::wxBrush(const wxColour& col, int style)
149 {
150     m_refData = new wxBrushRefData(col, style);
151 }
152
153 wxBrush::wxBrush(const wxBitmap& stipple)
154 {
155     m_refData = new wxBrushRefData(stipple);
156 }
157
158 wxObjectRefData *wxBrush::CreateRefData() const
159 {
160     return new wxBrushRefData;
161 }
162
163 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
164 {
165     return new wxBrushRefData(*(wxBrushRefData *)data);
166 }
167
168 void wxBrush::SetColour(const wxColour& col)
169 {
170     AllocExclusive();
171     M_BRUSHDATA->SetColour(col);
172 }
173
174 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
175 {
176     AllocExclusive();
177     M_BRUSHDATA->SetColour(wxColour(r,g,b));
178 }
179
180 void wxBrush::SetStyle(int style)
181 {
182     AllocExclusive();
183     M_BRUSHDATA->SetStyle(style);
184 }
185
186 void wxBrush::SetStipple(const wxBitmap& stipple)
187 {
188     AllocExclusive();
189     M_BRUSHDATA->SetStipple(stipple);
190 }
191
192 wxColour wxBrush::GetColour() const
193 {
194     wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
195     return M_BRUSHDATA->GetColour();
196 }
197
198 int wxBrush::GetStyle() const
199 {
200     wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
201     return M_BRUSHDATA->GetStyle();
202 }
203
204 wxBitmap *wxBrush::GetStipple() const
205 {
206     wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
207     return M_BRUSHDATA->GetStipple();
208 }
209
210 WX_NSColor wxBrush::GetNSColor()
211 {
212     if(!m_refData)
213         return [NSColor clearColor];
214     return M_BRUSHDATA->GetNSColor();
215 }
216