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