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