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