]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/pen.mm
Halfway reasonable implementation of wxFont for wxCocoa.
[wxWidgets.git] / src / cocoa / pen.mm
CommitLineData
8e517013
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/cocoa/pen.mm
3// Purpose: wxPen
4// Author: David Elliott
5// Modified by:
6// Created: 2003/08/02
7// RCS-ID: $Id$
8// Copyright: (c) 2003 David Elliott
46562151 9// Licence: wxWidgets licence
8e517013
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673 12#include "wx/wxprec.h"
0bca0373 13
449c5673
DE
14#ifndef WX_PRECOMP
15 #include "wx/pen.h"
0bca0373 16 #include "wx/bitmap.h"
7cf41a5d 17 #include "wx/colour.h"
449c5673 18#endif //WX_PRECOMP
0bca0373 19
8e517013
DE
20#import <AppKit/NSColor.h>
21
22// ========================================================================
23// wxPenRefData
24// ========================================================================
25class WXDLLEXPORT wxPenRefData: public wxGDIRefData
26{
8e517013
DE
27public:
28 wxPenRefData(const wxColour& colour = wxNullColour,
29 int width = 1, int style = wxSOLID,
30 const wxBitmap& stipple = wxNullBitmap);
31 wxPenRefData(const wxPenRefData& data);
1f631557 32 ~wxPenRefData() { FreeCocoaNSColor(); FreeCocoaDash(); }
8e517013
DE
33
34 void SetWidth(int Width) { m_width = Width; }
1f631557
DE
35 void SetStyle(int Style)
36 { FreeCocoaNSColor();
37 FreeCocoaDash();
38 m_style = Style;
39 }
8e517013
DE
40 void SetJoin(int Join) { m_join = Join; }
41 void SetCap(int Cap) { m_cap = Cap; }
1f631557 42 void SetColour(const wxColour& col) { FreeCocoaNSColor(); m_colour = col; }
8e517013
DE
43 void SetDashes(int nb_dashes, const wxDash *Dash)
44 {
1f631557 45 FreeCocoaDash();
8e517013
DE
46 m_nbDash = nb_dashes;
47 m_dash = (wxDash *)Dash;
48 }
49 void SetStipple(const wxBitmap& Stipple)
50 {
1f631557 51 FreeCocoaNSColor();
8e517013
DE
52 m_stipple = Stipple;
53 m_style = wxSTIPPLE;
54 }
55 WX_NSColor GetNSColor();
4799f3ba 56 int GetCocoaLineDash(const CGFloat **pattern);
8e517013 57protected:
1f631557
DE
58 void FreeCocoaNSColor();
59 void FreeCocoaDash();
8e517013
DE
60
61 int m_width;
62 int m_style;
63 int m_join;
64 int m_cap;
65 wxColour m_colour;
66 int m_nbDash;
67 wxDash *m_dash;
68 wxBitmap m_stipple;
69 WX_NSColor m_cocoaNSColor;
4799f3ba 70 CGFloat *m_cocoaDash;
1f631557
DE
71
72 // Predefined dash patterns
73 static const int scm_countDot;
4799f3ba 74 static const CGFloat scm_patternDot[];
1f631557 75 static const int scm_countLongDash;
4799f3ba 76 static const CGFloat scm_patternLongDash[];
1f631557 77 static const int scm_countShortDash;
4799f3ba 78 static const CGFloat scm_patternShortDash[];
1f631557 79 static const int scm_countDotDash;
4799f3ba 80 static const CGFloat scm_patternDotDash[];
f55c979a
VZ
81
82 friend class WXDLLIMPEXP_FWD_CORE wxPen;
83
8e517013
DE
84private:
85 // Don't allow assignment
86 wxPenRefData& operator=(const wxPenRefData& data);
87};
88
1f631557 89const int wxPenRefData::scm_countDot = 1;
4799f3ba 90const CGFloat wxPenRefData::scm_patternDot[] = {
1f631557
DE
91 1.0
92};
93const int wxPenRefData::scm_countLongDash = 1;
4799f3ba 94const CGFloat wxPenRefData::scm_patternLongDash[] = {
1f631557
DE
95 10.0
96};
97const int wxPenRefData::scm_countShortDash = 1;
4799f3ba 98const CGFloat wxPenRefData::scm_patternShortDash[] = {
1f631557
DE
99 5.0
100};
101const int wxPenRefData::scm_countDotDash = 4;
4799f3ba 102const CGFloat wxPenRefData::scm_patternDotDash[] = {
1f631557
DE
103 1.0
104, 1.0
105, 5.0
106, 1.0
107};
108
8e517013
DE
109#define M_PENDATA ((wxPenRefData *)m_refData)
110
111inline wxPenRefData::wxPenRefData(const wxColour& colour,
112 int width, int style, const wxBitmap& stipple)
113{
114 m_width = width;
115 m_style = style;
116 m_join = wxJOIN_ROUND;
117 m_cap = wxCAP_ROUND;
118 m_colour = colour;
119 m_nbDash = 0;
120 m_dash = 0;
121 m_stipple = stipple;
122 m_cocoaNSColor = nil;
1f631557 123 m_cocoaDash = NULL;
8e517013
DE
124}
125
126inline wxPenRefData::wxPenRefData(const wxPenRefData& data)
127{
128 m_width = data.m_width;
129 m_style = data.m_style;
130 m_join = data.m_join;
131 m_cap = data.m_cap;
132 m_colour = data.m_colour;
133 m_nbDash = data.m_nbDash;
134 m_dash = data.m_dash;
135 m_stipple = data.m_stipple;
136 m_cocoaNSColor = [data.m_cocoaNSColor retain];
568cdf9c 137 m_cocoaDash = NULL;
8e517013
DE
138}
139
1f631557 140inline void wxPenRefData::FreeCocoaNSColor()
8e517013
DE
141{
142 [m_cocoaNSColor release];
143 m_cocoaNSColor = nil;
144}
145
1f631557
DE
146inline void wxPenRefData::FreeCocoaDash()
147{
148 delete m_cocoaDash;
149 m_cocoaDash = NULL;
150}
151
8e517013
DE
152inline WX_NSColor wxPenRefData::GetNSColor()
153{
154 if(!m_cocoaNSColor)
155 {
156 switch( m_style )
157 {
158 case wxTRANSPARENT:
159 m_cocoaNSColor = [[NSColor clearColor] retain];
160 break;
161 case wxSTIPPLE:
162// wxBitmap isn't implemented yet
163// m_cocoaNSColor = [[NSColor colorWithPatternImage: m_stipple.GetNSImage()] retain];
164// break;
165 // The hatch brushes are going to be tricky
166 case wxBDIAGONAL_HATCH:
167 case wxCROSSDIAG_HATCH:
168 case wxFDIAGONAL_HATCH:
169 case wxCROSS_HATCH:
170 case wxHORIZONTAL_HATCH:
171 case wxVERTICAL_HATCH:
172 default:
173 // Dot/dashed pens use solid colors
174 case wxDOT:
175 case wxLONG_DASH:
176 case wxSHORT_DASH:
177 case wxDOT_DASH:
178 case wxUSER_DASH:
179 case wxSOLID:
180 NSColor *colour_NSColor = m_colour.GetNSColor();
181 if(!colour_NSColor)
182 colour_NSColor = [NSColor clearColor];
183 m_cocoaNSColor = [colour_NSColor copyWithZone:nil];
184 break;
185 }
186 }
187 return m_cocoaNSColor;
188}
189
4799f3ba 190int wxPenRefData::GetCocoaLineDash(const CGFloat **pattern)
1f631557
DE
191{
192 int count;
193 switch( m_style )
194 {
195 case wxDOT:
196 count = scm_countDot;
197 if(pattern)
198 *pattern = scm_patternDot;
199 break;
200 case wxLONG_DASH:
201 count = scm_countLongDash;
202 if(pattern)
203 *pattern = scm_patternLongDash;
204 break;
205 case wxSHORT_DASH:
206 count = scm_countShortDash;
207 if(pattern)
208 *pattern = scm_patternShortDash;
209 break;
210 case wxDOT_DASH:
211 count = scm_countDotDash;
212 if(pattern)
213 *pattern = scm_patternDotDash;
214 break;
215 case wxUSER_DASH:
216 count = m_nbDash;
217 if(pattern)
218 {
219 if(!m_cocoaDash)
220 {
4799f3ba 221 m_cocoaDash = new CGFloat[count];
1f631557
DE
222 for(int i=0; i<count; i++)
223 m_cocoaDash[i] = m_dash[i];
224 }
225 *pattern = m_cocoaDash;
226 }
227 break;
228 case wxTRANSPARENT:
229 case wxSTIPPLE:
230 case wxBDIAGONAL_HATCH:
231 case wxCROSSDIAG_HATCH:
232 case wxFDIAGONAL_HATCH:
233 case wxCROSS_HATCH:
234 case wxHORIZONTAL_HATCH:
235 case wxVERTICAL_HATCH:
236 case wxSOLID:
237 default:
238 count = 0;
239 if(pattern)
240 *pattern = NULL;
241 }
242 return count;
243}
244
8e517013
DE
245// ========================================================================
246// wxPen
247// ========================================================================
248IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
249
250wxPen::wxPen()
251{
252}
253
254wxPen::~wxPen()
255{
256}
257
258// Should implement Create
259wxPen::wxPen(const wxColour& colour, int width, int style)
260{
261 m_refData = new wxPenRefData(colour,width,style);
262}
263
264wxPen::wxPen(const wxBitmap& stipple, int width)
265{
266 m_refData = new wxPenRefData(wxNullColour,width,wxSTIPPLE,stipple);
267}
268
269wxObjectRefData *wxPen::CreateRefData() const
270{
271 return new wxPenRefData;
272}
273
274wxObjectRefData *wxPen::CloneRefData(const wxObjectRefData *data) const
275{
276 return new wxPenRefData(*(wxPenRefData *)data);
277}
278
279void wxPen::SetWidth(int Width)
280{
281 AllocExclusive();
282 M_PENDATA->SetWidth(Width);
283}
284
285void wxPen::SetStyle(int Style)
286{
287 AllocExclusive();
288 M_PENDATA->SetStyle(Style);
289}
290
291void wxPen::SetJoin(int Join)
292{
293 AllocExclusive();
294 M_PENDATA->SetJoin(Join);
295}
296
297void wxPen::SetCap(int Cap)
298{
299 AllocExclusive();
300 M_PENDATA->SetCap(Cap);
301}
302
303void wxPen::SetColour(const wxColour& col)
304{
305 AllocExclusive();
306 M_PENDATA->SetColour(col);
307}
308
1a1498c0 309void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
8e517013
DE
310{
311 AllocExclusive();
312 M_PENDATA->SetColour(wxColour(r, g, b));
313}
314
315void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
316{
317 AllocExclusive();
318 M_PENDATA->SetDashes(nb_dashes, Dash);
319}
320
321void wxPen::SetStipple(const wxBitmap& Stipple)
322{
323 AllocExclusive();
324 M_PENDATA->SetStipple(Stipple);
325}
326
327wxColour& wxPen::GetColour() const
328{
46562151 329 return (M_PENDATA ? M_PENDATA->m_colour : wxNullColour);
8e517013
DE
330}
331
332int wxPen::GetWidth() const
333{
46562151 334 return (M_PENDATA ? M_PENDATA->m_width : 0);
8e517013
DE
335}
336
337int wxPen::GetStyle() const
338{
46562151 339 return (M_PENDATA ? M_PENDATA->m_style : 0);
8e517013
DE
340}
341
342int wxPen::GetJoin() const
343{
46562151 344 return (M_PENDATA ? M_PENDATA->m_join : 0);
8e517013
DE
345}
346
347int wxPen::GetCap() const
348{
46562151 349 return (M_PENDATA ? M_PENDATA->m_cap : 0);
8e517013
DE
350}
351
352int wxPen::GetDashes(wxDash **ptr) const
353{
354 *ptr = (M_PENDATA ? M_PENDATA->m_dash : (wxDash*) NULL); return (M_PENDATA ? M_PENDATA->m_nbDash : 0);
355}
356
357wxBitmap *wxPen::GetStipple() const
358{
46562151 359 return (M_PENDATA ? (& M_PENDATA->m_stipple) : (wxBitmap*) NULL);
8e517013
DE
360}
361
362WX_NSColor wxPen::GetNSColor()
363{
364 return (M_PENDATA ? M_PENDATA->GetNSColor() : nil);
365}
366
4799f3ba 367int wxPen::GetCocoaLineDash(const CGFloat **pattern)
1f631557
DE
368{
369 if(M_PENDATA)
370 return M_PENDATA->GetCocoaLineDash(pattern);
371 if(pattern)
372 *pattern = NULL;
373 return 0;
374}