Include wx/brush.h according to precompiled headers of wx/wx.h (with other minor...
[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 #endif //WX_PRECOMP
19
20 #include "wx/colour.h"
21
22 #import <AppKit/NSColor.h>
23
24 class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
25 {
26 public:
27     wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
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     int 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(int 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     int           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, int 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 == data.m_stipple;
101 }
102
103 void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
104 {
105     m_stipple = stipple;
106     m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
107 }
108
109 WX_NSColor wxBrushRefData::GetNSColor()
110 {
111     if(!m_cocoaNSColor)
112     {
113         switch( m_style )
114         {
115         case wxTRANSPARENT:
116             m_cocoaNSColor = [[NSColor clearColor] retain];
117             break;
118         case wxSTIPPLE:
119 //  wxBitmap isn't implemented yet
120 //            m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
121 //            break;
122         case wxSTIPPLE_MASK_OPAQUE:
123             // This should be easy when wxBitmap works.
124 //            break;
125         // The hatch brushes are going to be tricky
126         case wxBDIAGONAL_HATCH:
127         case wxCROSSDIAG_HATCH:
128         case wxFDIAGONAL_HATCH:
129         case wxCROSS_HATCH:
130         case wxHORIZONTAL_HATCH:
131         case wxVERTICAL_HATCH:
132         default:
133         case wxSOLID:
134             NSColor *colour_NSColor = m_colour.GetNSColor();
135             if(!colour_NSColor)
136                 colour_NSColor = [NSColor clearColor];
137             m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
138             break;
139         }
140     }
141     return m_cocoaNSColor;
142 }
143
144 // Brushes
145 wxBrush::wxBrush()
146 {
147 }
148
149 wxBrush::~wxBrush()
150 {
151 }
152
153 wxBrush::wxBrush(const wxColour& col, int style)
154 {
155     m_refData = new wxBrushRefData(col, style);
156 }
157
158 wxBrush::wxBrush(const wxBitmap& stipple)
159 {
160     m_refData = new wxBrushRefData(stipple);
161 }
162
163 wxObjectRefData *wxBrush::CreateRefData() const
164 {
165     return new wxBrushRefData;
166 }
167
168 wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
169 {
170     return new wxBrushRefData(*(wxBrushRefData *)data);
171 }
172
173 void wxBrush::SetColour(const wxColour& col)
174 {
175     AllocExclusive();
176     M_BRUSHDATA->SetColour(col);
177 }
178
179 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
180 {
181     AllocExclusive();
182     M_BRUSHDATA->SetColour(wxColour(r,g,b));
183 }
184
185 void wxBrush::SetStyle(int style)
186 {
187     AllocExclusive();
188     M_BRUSHDATA->SetStyle(style);
189 }
190
191 void wxBrush::SetStipple(const wxBitmap& stipple)
192 {
193     AllocExclusive();
194     M_BRUSHDATA->SetStipple(stipple);
195 }
196
197 wxColour wxBrush::GetColour() const
198 {
199     wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
200     return M_BRUSHDATA->GetColour();
201 }
202
203 int wxBrush::GetStyle() const
204 {
205     wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
206     return M_BRUSHDATA->GetStyle();
207 }
208
209 wxBitmap *wxBrush::GetStipple() const
210 {
211     wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
212     return M_BRUSHDATA->GetStipple();
213 }
214
215 WX_NSColor wxBrush::GetNSColor()
216 {
217     if(!m_refData)
218         return [NSColor clearColor];
219     return M_BRUSHDATA->GetNSColor();
220 }