]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/brush.mm
fixed memory leak introduced by dynamic wxMimeTypesManagerImpl creation (rev. 1.44...
[wxWidgets.git] / src / cocoa / brush.mm
CommitLineData
f162a338
DE
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
46562151 9// Licence: wxWidgets licence
f162a338
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673 12#include "wx/wxprec.h"
c64755ed
WS
13
14#include "wx/brush.h"
15
449c5673
DE
16#ifndef WX_PRECOMP
17 #include "wx/utils.h"
449c5673 18#endif //WX_PRECOMP
c64755ed 19
f162a338
DE
20#include "wx/colour.h"
21
22#import <AppKit/NSColor.h>
23
24class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
25{
26public:
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
46private:
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)
60IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
61
62wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
63{
64 m_cocoaNSColor = NULL;
6631a2a5 65 m_style = style;
f162a338
DE
66 m_colour = colour;
67}
68
69wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
70{
71 m_cocoaNSColor = NULL;
72 DoSetStipple(stipple);
73}
74
75wxBrushRefData::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
84wxBrushRefData::~wxBrushRefData()
85{
86 Free();
87}
88
89void wxBrushRefData::Free()
90{
91 [m_cocoaNSColor release];
92 m_cocoaNSColor = NULL;
93}
94
95bool 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
103void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
104{
105 m_stipple = stipple;
106 m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
107}
108
109WX_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
145wxBrush::wxBrush()
146{
f162a338
DE
147}
148
149wxBrush::~wxBrush()
150{
151}
152
153wxBrush::wxBrush(const wxColour& col, int style)
154{
155 m_refData = new wxBrushRefData(col, style);
156}
157
158wxBrush::wxBrush(const wxBitmap& stipple)
159{
160 m_refData = new wxBrushRefData(stipple);
161}
162
163wxObjectRefData *wxBrush::CreateRefData() const
164{
165 return new wxBrushRefData;
166}
167
168wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
169{
170 return new wxBrushRefData(*(wxBrushRefData *)data);
171}
172
173void wxBrush::SetColour(const wxColour& col)
174{
175 AllocExclusive();
176 M_BRUSHDATA->SetColour(col);
177}
178
1a1498c0 179void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
f162a338
DE
180{
181 AllocExclusive();
182 M_BRUSHDATA->SetColour(wxColour(r,g,b));
183}
184
185void wxBrush::SetStyle(int style)
186{
187 AllocExclusive();
188 M_BRUSHDATA->SetStyle(style);
189}
190
191void wxBrush::SetStipple(const wxBitmap& stipple)
192{
193 AllocExclusive();
194 M_BRUSHDATA->SetStipple(stipple);
195}
196
197wxColour wxBrush::GetColour() const
198{
199 wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
200 return M_BRUSHDATA->GetColour();
201}
202
203int wxBrush::GetStyle() const
204{
205 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
206 return M_BRUSHDATA->GetStyle();
207}
208
209wxBitmap *wxBrush::GetStipple() const
210{
211 wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
212 return M_BRUSHDATA->GetStipple();
213}
214
215WX_NSColor wxBrush::GetNSColor()
216{
1a94b3d8
DE
217 if(!m_refData)
218 return [NSColor clearColor];
f162a338
DE
219 return M_BRUSHDATA->GetNSColor();
220}