]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/osx/carbon/dccg.cpp | |
3 | // Purpose: wxDC class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/graphics.h" | |
15 | #include "wx/private/graphics.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/dcclient.h" | |
19 | #include "wx/dcmemory.h" | |
20 | #include "wx/dcprint.h" | |
21 | #include "wx/log.h" | |
22 | #include "wx/region.h" | |
23 | #include "wx/image.h" | |
24 | #include "wx/icon.h" | |
25 | #endif | |
26 | ||
27 | ||
28 | #ifdef __MSL__ | |
29 | #if __MSL__ >= 0x6000 | |
30 | #include "math.h" | |
31 | // in case our functions were defined outside std, we make it known all the same | |
32 | namespace std { } | |
33 | using namespace std; | |
34 | #endif | |
35 | #endif | |
36 | ||
37 | #ifdef __WXMAC__ | |
38 | #include "wx/osx/private.h" | |
39 | #include "wx/osx/dcprint.h" | |
40 | #include "wx/osx/dcclient.h" | |
41 | #include "wx/osx/dcmemory.h" | |
42 | #include "wx/osx/private.h" | |
43 | #else | |
44 | #include "CoreServices/CoreServices.h" | |
45 | #include "ApplicationServices/ApplicationServices.h" | |
46 | #include "wx/osx/core/cfstring.h" | |
47 | #include "wx/cocoa/dcclient.h" | |
48 | #endif | |
49 | ||
50 | #ifdef __WXCOCOA__ | |
51 | ||
52 | CGColorSpaceRef wxMacGetGenericRGBColorSpace() | |
53 | { | |
54 | static wxCFRef<CGColorSpaceRef> genericRGBColorSpace; | |
55 | ||
56 | if (genericRGBColorSpace == NULL) | |
57 | { | |
58 | genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) ); | |
59 | } | |
60 | ||
61 | return genericRGBColorSpace; | |
62 | } | |
63 | ||
64 | int UMAGetSystemVersion() | |
65 | { | |
66 | return 0x1050; | |
67 | } | |
68 | ||
69 | ||
70 | #define wxOSX_USE_CORE_TEXT 1 | |
71 | ||
72 | #endif | |
73 | ||
74 | #if wxOSX_USE_COCOA_OR_IPHONE | |
75 | extern CGContextRef wxOSXGetContextFromCurrentContext() ; | |
76 | #if wxOSX_USE_COCOA | |
77 | extern bool wxOSXLockFocus( WXWidget view) ; | |
78 | extern void wxOSXUnlockFocus( WXWidget view) ; | |
79 | #endif | |
80 | #endif | |
81 | ||
82 | #if 1 // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
83 | ||
84 | // TODO test whether this private API also works under 10.3 | |
85 | ||
86 | // copying values from NSCompositingModes (see also webkit and cairo sources) | |
87 | ||
88 | typedef enum CGCompositeOperation { | |
89 | kCGCompositeOperationClear = 0, | |
90 | kCGCompositeOperationCopy = 1, | |
91 | kCGCompositeOperationSourceOver = 2, | |
92 | kCGCompositeOperationSourceIn = 3, | |
93 | kCGCompositeOperationSourceOut = 4, | |
94 | kCGCompositeOperationSourceAtop = 5, | |
95 | kCGCompositeOperationDestinationOver = 6, | |
96 | kCGCompositeOperationDestinationIn = 7, | |
97 | kCGCompositeOperationDestinationOut = 8, | |
98 | kCGCompositeOperationDestinationAtop = 9, | |
99 | kCGCompositeOperationXOR = 10, | |
100 | kCGCompositeOperationPlusDarker = 11, | |
101 | // NS only, unsupported by CG : Highlight | |
102 | kCGCompositeOperationPlusLighter = 12 | |
103 | } CGCompositeOperation ; | |
104 | ||
105 | extern "C" | |
106 | { | |
107 | CG_EXTERN void CGContextSetCompositeOperation (CGContextRef context, int operation); | |
108 | } ; | |
109 | ||
110 | #endif | |
111 | ||
112 | //----------------------------------------------------------------------------- | |
113 | // constants | |
114 | //----------------------------------------------------------------------------- | |
115 | ||
116 | #if !defined( __DARWIN__ ) || defined(__MWERKS__) | |
117 | #ifndef M_PI | |
118 | const double M_PI = 3.14159265358979; | |
119 | #endif | |
120 | #endif | |
121 | ||
122 | static const double RAD2DEG = 180.0 / M_PI; | |
123 | ||
124 | // | |
125 | // Pen, Brushes and Fonts | |
126 | // | |
127 | ||
128 | #pragma mark - | |
129 | #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes | |
130 | ||
131 | OSStatus wxMacDrawCGImage( | |
132 | CGContextRef inContext, | |
133 | const CGRect * inBounds, | |
134 | CGImageRef inImage) | |
135 | { | |
136 | #if wxOSX_USE_CARBON | |
137 | return HIViewDrawCGImage( inContext, inBounds, inImage ); | |
138 | #else | |
139 | CGContextSaveGState(inContext); | |
140 | CGContextTranslateCTM(inContext, inBounds->origin.x, inBounds->origin.y + inBounds->size.height); | |
141 | CGRect r = *inBounds; | |
142 | r.origin.x = r.origin.y = 0; | |
143 | CGContextScaleCTM(inContext, 1, -1); | |
144 | CGContextDrawImage(inContext, r, inImage ); | |
145 | CGContextRestoreGState(inContext); | |
146 | return noErr; | |
147 | #endif | |
148 | } | |
149 | ||
150 | CGColorRef wxMacCreateCGColor( const wxColour& col ) | |
151 | { | |
152 | CGColorRef retval = 0; | |
153 | #ifdef __WXMAC__ | |
154 | retval = col.CreateCGColor(); | |
155 | #else | |
156 | // TODO add conversion NSColor - CGColorRef (obj-c) | |
157 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 | |
158 | if ( CGColorCreateGenericRGB ) | |
159 | retval = CGColorCreateGenericRGB( col.Red() / 255.0 , col.Green() / 255.0, col.Blue() / 255.0, col.Alpha() / 255.0 ); | |
160 | else | |
161 | #endif | |
162 | { | |
163 | CGFloat components[4] = { col.Red() / 255.0, col.Green() / 255.0, col.Blue() / 255.0, col.Alpha() / 255.0 } ; | |
164 | retval = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ; | |
165 | } | |
166 | ||
167 | #endif | |
168 | return retval; | |
169 | } | |
170 | ||
171 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && wxOSX_USE_CORE_TEXT | |
172 | ||
173 | CTFontRef wxMacCreateCTFont( const wxFont& font ) | |
174 | { | |
175 | #ifdef __WXMAC__ | |
176 | return wxCFRetain((CTFontRef) font.OSXGetCTFont()); | |
177 | #else | |
178 | return CTFontCreateWithName( wxCFStringRef( font.GetFaceName(), wxLocale::GetSystemEncoding() ) , font.GetPointSize() , NULL ); | |
179 | #endif | |
180 | } | |
181 | ||
182 | #endif | |
183 | ||
184 | // CGPattern wrapper class: always allocate on heap, never call destructor | |
185 | ||
186 | class wxMacCoreGraphicsPattern | |
187 | { | |
188 | public : | |
189 | wxMacCoreGraphicsPattern() {} | |
190 | ||
191 | // is guaranteed to be called only with a non-Null CGContextRef | |
192 | virtual void Render( CGContextRef ctxRef ) = 0; | |
193 | ||
194 | operator CGPatternRef() const { return m_patternRef; } | |
195 | ||
196 | protected : | |
197 | virtual ~wxMacCoreGraphicsPattern() | |
198 | { | |
199 | // as this is called only when the m_patternRef is been released; | |
200 | // don't release it again | |
201 | } | |
202 | ||
203 | static void _Render( void *info, CGContextRef ctxRef ) | |
204 | { | |
205 | wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info; | |
206 | if ( self && ctxRef ) | |
207 | self->Render( ctxRef ); | |
208 | } | |
209 | ||
210 | static void _Dispose( void *info ) | |
211 | { | |
212 | wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info; | |
213 | delete self; | |
214 | } | |
215 | ||
216 | CGPatternRef m_patternRef; | |
217 | ||
218 | static const CGPatternCallbacks ms_Callbacks; | |
219 | }; | |
220 | ||
221 | const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose }; | |
222 | ||
223 | class ImagePattern : public wxMacCoreGraphicsPattern | |
224 | { | |
225 | public : | |
226 | ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform ) | |
227 | { | |
228 | wxASSERT( bmp && bmp->Ok() ); | |
229 | #ifdef __WXMAC__ | |
230 | Init( (CGImageRef) bmp->CreateCGImage() , transform ); | |
231 | #endif | |
232 | } | |
233 | ||
234 | // ImagePattern takes ownership of CGImageRef passed in | |
235 | ImagePattern( CGImageRef image , const CGAffineTransform& transform ) | |
236 | { | |
237 | if ( image ) | |
238 | CFRetain( image ); | |
239 | ||
240 | Init( image , transform ); | |
241 | } | |
242 | ||
243 | virtual void Render( CGContextRef ctxRef ) | |
244 | { | |
245 | if (m_image != NULL) | |
246 | wxMacDrawCGImage( ctxRef, &m_imageBounds, m_image ); | |
247 | } | |
248 | ||
249 | protected : | |
250 | void Init( CGImageRef image, const CGAffineTransform& transform ) | |
251 | { | |
252 | m_image = image; | |
253 | if ( m_image ) | |
254 | { | |
255 | m_imageBounds = CGRectMake( (CGFloat) 0.0, (CGFloat) 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) ); | |
256 | m_patternRef = CGPatternCreate( | |
257 | this , m_imageBounds, transform , | |
258 | m_imageBounds.size.width, m_imageBounds.size.height, | |
259 | kCGPatternTilingNoDistortion, true , &wxMacCoreGraphicsPattern::ms_Callbacks ); | |
260 | } | |
261 | } | |
262 | ||
263 | virtual ~ImagePattern() | |
264 | { | |
265 | if ( m_image ) | |
266 | CGImageRelease( m_image ); | |
267 | } | |
268 | ||
269 | CGImageRef m_image; | |
270 | CGRect m_imageBounds; | |
271 | }; | |
272 | ||
273 | class HatchPattern : public wxMacCoreGraphicsPattern | |
274 | { | |
275 | public : | |
276 | HatchPattern( int hatchstyle, const CGAffineTransform& transform ) | |
277 | { | |
278 | m_hatch = hatchstyle; | |
279 | m_imageBounds = CGRectMake( (CGFloat) 0.0, (CGFloat) 0.0, (CGFloat) 8.0 , (CGFloat) 8.0 ); | |
280 | m_patternRef = CGPatternCreate( | |
281 | this , m_imageBounds, transform , | |
282 | m_imageBounds.size.width, m_imageBounds.size.height, | |
283 | kCGPatternTilingNoDistortion, false , &wxMacCoreGraphicsPattern::ms_Callbacks ); | |
284 | } | |
285 | ||
286 | void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count ) | |
287 | { | |
288 | CGContextStrokeLineSegments( ctxRef , pts , count ); | |
289 | } | |
290 | ||
291 | virtual void Render( CGContextRef ctxRef ) | |
292 | { | |
293 | switch ( m_hatch ) | |
294 | { | |
295 | case wxBDIAGONAL_HATCH : | |
296 | { | |
297 | CGPoint pts[] = | |
298 | { | |
299 | { (CGFloat) 8.0 , (CGFloat) 0.0 } , { (CGFloat) 0.0 , (CGFloat) 8.0 } | |
300 | }; | |
301 | StrokeLineSegments( ctxRef , pts , 2 ); | |
302 | } | |
303 | break; | |
304 | ||
305 | case wxCROSSDIAG_HATCH : | |
306 | { | |
307 | CGPoint pts[] = | |
308 | { | |
309 | { (CGFloat) 0.0 , (CGFloat) 0.0 } , { (CGFloat) 8.0 , (CGFloat) 8.0 } , | |
310 | { (CGFloat) 8.0 , (CGFloat) 0.0 } , { (CGFloat) 0.0 , (CGFloat) 8.0 } | |
311 | }; | |
312 | StrokeLineSegments( ctxRef , pts , 4 ); | |
313 | } | |
314 | break; | |
315 | ||
316 | case wxFDIAGONAL_HATCH : | |
317 | { | |
318 | CGPoint pts[] = | |
319 | { | |
320 | { (CGFloat) 0.0 , (CGFloat) 0.0 } , { (CGFloat) 8.0 , (CGFloat) 8.0 } | |
321 | }; | |
322 | StrokeLineSegments( ctxRef , pts , 2 ); | |
323 | } | |
324 | break; | |
325 | ||
326 | case wxCROSS_HATCH : | |
327 | { | |
328 | CGPoint pts[] = | |
329 | { | |
330 | { (CGFloat) 0.0 , (CGFloat) 4.0 } , { (CGFloat) 8.0 , (CGFloat) 4.0 } , | |
331 | { (CGFloat) 4.0 , (CGFloat) 0.0 } , { (CGFloat) 4.0 , (CGFloat) 8.0 } , | |
332 | }; | |
333 | StrokeLineSegments( ctxRef , pts , 4 ); | |
334 | } | |
335 | break; | |
336 | ||
337 | case wxHORIZONTAL_HATCH : | |
338 | { | |
339 | CGPoint pts[] = | |
340 | { | |
341 | { (CGFloat) 0.0 , (CGFloat) 4.0 } , { (CGFloat) 8.0 , (CGFloat) 4.0 } , | |
342 | }; | |
343 | StrokeLineSegments( ctxRef , pts , 2 ); | |
344 | } | |
345 | break; | |
346 | ||
347 | case wxVERTICAL_HATCH : | |
348 | { | |
349 | CGPoint pts[] = | |
350 | { | |
351 | { (CGFloat) 4.0 , (CGFloat) 0.0 } , { (CGFloat) 4.0 , (CGFloat) 8.0 } , | |
352 | }; | |
353 | StrokeLineSegments( ctxRef , pts , 2 ); | |
354 | } | |
355 | break; | |
356 | ||
357 | default: | |
358 | break; | |
359 | } | |
360 | } | |
361 | ||
362 | protected : | |
363 | virtual ~HatchPattern() {} | |
364 | ||
365 | CGRect m_imageBounds; | |
366 | int m_hatch; | |
367 | }; | |
368 | ||
369 | class wxMacCoreGraphicsPenData : public wxGraphicsObjectRefData | |
370 | { | |
371 | public: | |
372 | wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); | |
373 | ~wxMacCoreGraphicsPenData(); | |
374 | ||
375 | void Init(); | |
376 | virtual void Apply( wxGraphicsContext* context ); | |
377 | virtual wxDouble GetWidth() { return m_width; } | |
378 | ||
379 | protected : | |
380 | CGLineCap m_cap; | |
381 | wxCFRef<CGColorRef> m_color; | |
382 | wxCFRef<CGColorSpaceRef> m_colorSpace; | |
383 | ||
384 | CGLineJoin m_join; | |
385 | CGFloat m_width; | |
386 | ||
387 | int m_count; | |
388 | const CGFloat *m_lengths; | |
389 | CGFloat *m_userLengths; | |
390 | ||
391 | ||
392 | bool m_isPattern; | |
393 | wxCFRef<CGPatternRef> m_pattern; | |
394 | CGFloat* m_patternColorComponents; | |
395 | }; | |
396 | ||
397 | wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) : | |
398 | wxGraphicsObjectRefData( renderer ) | |
399 | { | |
400 | Init(); | |
401 | ||
402 | m_color.reset( wxMacCreateCGColor( pen.GetColour() ) ) ; | |
403 | ||
404 | // TODO: * m_dc->m_scaleX | |
405 | m_width = pen.GetWidth(); | |
406 | if (m_width <= 0.0) | |
407 | m_width = (CGFloat) 0.1; | |
408 | ||
409 | switch ( pen.GetCap() ) | |
410 | { | |
411 | case wxCAP_ROUND : | |
412 | m_cap = kCGLineCapRound; | |
413 | break; | |
414 | ||
415 | case wxCAP_PROJECTING : | |
416 | m_cap = kCGLineCapSquare; | |
417 | break; | |
418 | ||
419 | case wxCAP_BUTT : | |
420 | m_cap = kCGLineCapButt; | |
421 | break; | |
422 | ||
423 | default : | |
424 | m_cap = kCGLineCapButt; | |
425 | break; | |
426 | } | |
427 | ||
428 | switch ( pen.GetJoin() ) | |
429 | { | |
430 | case wxJOIN_BEVEL : | |
431 | m_join = kCGLineJoinBevel; | |
432 | break; | |
433 | ||
434 | case wxJOIN_MITER : | |
435 | m_join = kCGLineJoinMiter; | |
436 | break; | |
437 | ||
438 | case wxJOIN_ROUND : | |
439 | m_join = kCGLineJoinRound; | |
440 | break; | |
441 | ||
442 | default : | |
443 | m_join = kCGLineJoinMiter; | |
444 | break; | |
445 | } | |
446 | ||
447 | const CGFloat dashUnit = m_width < 1.0 ? (CGFloat) 1.0 : m_width; | |
448 | ||
449 | const CGFloat dotted[] = { (CGFloat) dashUnit , (CGFloat) (dashUnit + 2.0) }; | |
450 | static const CGFloat short_dashed[] = { (CGFloat) 9.0 , (CGFloat) 6.0 }; | |
451 | static const CGFloat dashed[] = { (CGFloat) 19.0 , (CGFloat) 9.0 }; | |
452 | static const CGFloat dotted_dashed[] = { (CGFloat) 9.0 , (CGFloat) 6.0 , (CGFloat) 3.0 , (CGFloat) 3.0 }; | |
453 | ||
454 | switch ( pen.GetStyle() ) | |
455 | { | |
456 | case wxSOLID : | |
457 | break; | |
458 | ||
459 | case wxDOT : | |
460 | m_count = WXSIZEOF(dotted); | |
461 | m_userLengths = new CGFloat[ m_count ] ; | |
462 | memcpy( m_userLengths, dotted, sizeof(dotted) ); | |
463 | m_lengths = m_userLengths; | |
464 | break; | |
465 | ||
466 | case wxLONG_DASH : | |
467 | m_count = WXSIZEOF(dashed); | |
468 | m_lengths = dashed; | |
469 | break; | |
470 | ||
471 | case wxSHORT_DASH : | |
472 | m_count = WXSIZEOF(short_dashed); | |
473 | m_lengths = short_dashed; | |
474 | break; | |
475 | ||
476 | case wxDOT_DASH : | |
477 | m_count = WXSIZEOF(dotted_dashed); | |
478 | m_lengths = dotted_dashed; | |
479 | break; | |
480 | ||
481 | case wxUSER_DASH : | |
482 | wxDash *dashes; | |
483 | m_count = pen.GetDashes( &dashes ); | |
484 | if ((dashes != NULL) && (m_count > 0)) | |
485 | { | |
486 | m_userLengths = new CGFloat[m_count]; | |
487 | for ( int i = 0; i < m_count; ++i ) | |
488 | { | |
489 | m_userLengths[i] = dashes[i] * dashUnit; | |
490 | ||
491 | if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 ) | |
492 | m_userLengths[i] = (CGFloat) (dashUnit + 2.0); | |
493 | else if ( i % 2 == 0 && m_userLengths[i] < dashUnit ) | |
494 | m_userLengths[i] = dashUnit; | |
495 | } | |
496 | } | |
497 | m_lengths = m_userLengths; | |
498 | break; | |
499 | ||
500 | case wxSTIPPLE : | |
501 | { | |
502 | wxBitmap* bmp = pen.GetStipple(); | |
503 | if ( bmp && bmp->Ok() ) | |
504 | { | |
505 | m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) ); | |
506 | m_pattern.reset( (CGPatternRef) *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) ); | |
507 | m_patternColorComponents = new CGFloat[1] ; | |
508 | m_patternColorComponents[0] = (CGFloat) 1.0; | |
509 | m_isPattern = true; | |
510 | } | |
511 | } | |
512 | break; | |
513 | ||
514 | default : | |
515 | { | |
516 | m_isPattern = true; | |
517 | m_colorSpace.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ); | |
518 | m_pattern.reset( (CGPatternRef) *( new HatchPattern( pen.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) ); | |
519 | m_patternColorComponents = new CGFloat[4] ; | |
520 | m_patternColorComponents[0] = (CGFloat) (pen.GetColour().Red() / 255.0); | |
521 | m_patternColorComponents[1] = (CGFloat) (pen.GetColour().Green() / 255.0); | |
522 | m_patternColorComponents[2] = (CGFloat) (pen.GetColour().Blue() / 255.0); | |
523 | m_patternColorComponents[3] = (CGFloat) (pen.GetColour().Alpha() / 255.0); | |
524 | } | |
525 | break; | |
526 | } | |
527 | if ((m_lengths != NULL) && (m_count > 0)) | |
528 | { | |
529 | // force the line cap, otherwise we get artifacts (overlaps) and just solid lines | |
530 | m_cap = kCGLineCapButt; | |
531 | } | |
532 | } | |
533 | ||
534 | wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData() | |
535 | { | |
536 | delete[] m_userLengths; | |
537 | delete[] m_patternColorComponents; | |
538 | } | |
539 | ||
540 | void wxMacCoreGraphicsPenData::Init() | |
541 | { | |
542 | m_lengths = NULL; | |
543 | m_userLengths = NULL; | |
544 | m_width = 0; | |
545 | m_count = 0; | |
546 | m_patternColorComponents = NULL; | |
547 | m_isPattern = false; | |
548 | } | |
549 | ||
550 | void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext* context ) | |
551 | { | |
552 | CGContextRef cg = (CGContextRef) context->GetNativeContext(); | |
553 | CGContextSetLineWidth( cg , m_width ); | |
554 | CGContextSetLineJoin( cg , m_join ); | |
555 | ||
556 | CGContextSetLineDash( cg , 0 , m_lengths , m_count ); | |
557 | CGContextSetLineCap( cg , m_cap ); | |
558 | ||
559 | if ( m_isPattern ) | |
560 | { | |
561 | CGAffineTransform matrix = CGContextGetCTM( cg ); | |
562 | CGContextSetPatternPhase( cg, CGSizeMake(matrix.tx, matrix.ty) ); | |
563 | CGContextSetStrokeColorSpace( cg , m_colorSpace ); | |
564 | CGContextSetStrokePattern( cg, m_pattern , m_patternColorComponents ); | |
565 | } | |
566 | else | |
567 | { | |
568 | CGContextSetStrokeColorWithColor( cg , m_color ); | |
569 | } | |
570 | } | |
571 | ||
572 | // | |
573 | // Brush | |
574 | // | |
575 | ||
576 | static const char *gs_stripedback_xpm[] = { | |
577 | /* columns rows colors chars-per-pixel */ | |
578 | "4 4 2 1", | |
579 | ". c #F0F0F0", | |
580 | "X c #ECECEC", | |
581 | /* pixels */ | |
582 | "....", | |
583 | "....", | |
584 | "XXXX", | |
585 | "XXXX" | |
586 | }; | |
587 | ||
588 | wxBitmap gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm ), -1 ) ; | |
589 | ||
590 | // make sure we all use one class for all conversions from wx to native colour | |
591 | ||
592 | class wxMacCoreGraphicsColour | |
593 | { | |
594 | public: | |
595 | wxMacCoreGraphicsColour(); | |
596 | wxMacCoreGraphicsColour(const wxBrush &brush); | |
597 | ~wxMacCoreGraphicsColour(); | |
598 | ||
599 | void Apply( CGContextRef cgContext ); | |
600 | protected: | |
601 | void Init(); | |
602 | wxCFRef<CGColorRef> m_color; | |
603 | wxCFRef<CGColorSpaceRef> m_colorSpace; | |
604 | ||
605 | bool m_isPattern; | |
606 | wxCFRef<CGPatternRef> m_pattern; | |
607 | CGFloat* m_patternColorComponents; | |
608 | } ; | |
609 | ||
610 | wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour() | |
611 | { | |
612 | delete[] m_patternColorComponents; | |
613 | } | |
614 | ||
615 | void wxMacCoreGraphicsColour::Init() | |
616 | { | |
617 | m_isPattern = false; | |
618 | m_patternColorComponents = NULL; | |
619 | } | |
620 | ||
621 | void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext ) | |
622 | { | |
623 | if ( m_isPattern ) | |
624 | { | |
625 | CGAffineTransform matrix = CGContextGetCTM( cgContext ); | |
626 | CGContextSetPatternPhase( cgContext, CGSizeMake(matrix.tx, matrix.ty) ); | |
627 | CGContextSetFillColorSpace( cgContext , m_colorSpace ); | |
628 | CGContextSetFillPattern( cgContext, m_pattern , m_patternColorComponents ); | |
629 | } | |
630 | else | |
631 | { | |
632 | CGContextSetFillColorWithColor( cgContext, m_color ); | |
633 | } | |
634 | } | |
635 | ||
636 | wxMacCoreGraphicsColour::wxMacCoreGraphicsColour() | |
637 | { | |
638 | Init(); | |
639 | } | |
640 | ||
641 | wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush &brush ) | |
642 | { | |
643 | Init(); | |
644 | if ( brush.GetStyle() == wxSOLID ) | |
645 | { | |
646 | m_color.reset( wxMacCreateCGColor( brush.GetColour() )); | |
647 | } | |
648 | else if ( brush.IsHatch() ) | |
649 | { | |
650 | m_isPattern = true; | |
651 | m_colorSpace.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ); | |
652 | m_pattern.reset( (CGPatternRef) *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) ); | |
653 | ||
654 | m_patternColorComponents = new CGFloat[4] ; | |
655 | m_patternColorComponents[0] = (CGFloat) (brush.GetColour().Red() / 255.0); | |
656 | m_patternColorComponents[1] = (CGFloat) (brush.GetColour().Green() / 255.0); | |
657 | m_patternColorComponents[2] = (CGFloat) (brush.GetColour().Blue() / 255.0); | |
658 | m_patternColorComponents[3] = (CGFloat) (brush.GetColour().Alpha() / 255.0); | |
659 | } | |
660 | else | |
661 | { | |
662 | // now brush is a bitmap | |
663 | wxBitmap* bmp = brush.GetStipple(); | |
664 | if ( bmp && bmp->Ok() ) | |
665 | { | |
666 | m_isPattern = true; | |
667 | m_patternColorComponents = new CGFloat[1] ; | |
668 | m_patternColorComponents[0] = (CGFloat) 1.0; | |
669 | m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) ); | |
670 | m_pattern.reset( (CGPatternRef) *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) ); | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | class wxMacCoreGraphicsBrushData : public wxGraphicsObjectRefData | |
676 | { | |
677 | public: | |
678 | wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer ); | |
679 | wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush ); | |
680 | ~wxMacCoreGraphicsBrushData (); | |
681 | ||
682 | virtual void Apply( wxGraphicsContext* context ); | |
683 | void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, | |
684 | const wxColour&c1, const wxColour&c2 ); | |
685 | void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
686 | const wxColour &oColor, const wxColour &cColor ); | |
687 | ||
688 | virtual bool IsShading() { return m_isShading; } | |
689 | CGShadingRef GetShading() { return m_shading; } | |
690 | protected: | |
691 | CGFunctionRef CreateGradientFunction( const wxColour& c1, const wxColour& c2 ); | |
692 | static void CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out); | |
693 | virtual void Init(); | |
694 | ||
695 | wxMacCoreGraphicsColour m_cgColor; | |
696 | ||
697 | bool m_isShading; | |
698 | CGFunctionRef m_gradientFunction; | |
699 | CGShadingRef m_shading; | |
700 | CGFloat *m_gradientComponents; | |
701 | }; | |
702 | ||
703 | wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxGraphicsObjectRefData( renderer ) | |
704 | { | |
705 | Init(); | |
706 | } | |
707 | ||
708 | void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, | |
709 | const wxColour&c1, const wxColour&c2 ) | |
710 | { | |
711 | m_gradientFunction = CreateGradientFunction( c1, c2 ); | |
712 | m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) x1, (CGFloat) y1), | |
713 | CGPointMake((CGFloat) x2,(CGFloat) y2), m_gradientFunction, true, true ) ; | |
714 | m_isShading = true ; | |
715 | } | |
716 | ||
717 | void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
718 | const wxColour &oColor, const wxColour &cColor ) | |
719 | { | |
720 | m_gradientFunction = CreateGradientFunction( oColor, cColor ); | |
721 | m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) xo,(CGFloat) yo), 0, | |
722 | CGPointMake((CGFloat) xc,(CGFloat) yc), (CGFloat) radius, m_gradientFunction, true, true ) ; | |
723 | m_isShading = true ; | |
724 | } | |
725 | ||
726 | wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxGraphicsObjectRefData( renderer ), | |
727 | m_cgColor( brush ) | |
728 | { | |
729 | Init(); | |
730 | ||
731 | } | |
732 | ||
733 | wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData() | |
734 | { | |
735 | if ( m_shading ) | |
736 | CGShadingRelease(m_shading); | |
737 | ||
738 | if( m_gradientFunction ) | |
739 | CGFunctionRelease(m_gradientFunction); | |
740 | ||
741 | delete[] m_gradientComponents; | |
742 | } | |
743 | ||
744 | void wxMacCoreGraphicsBrushData::Init() | |
745 | { | |
746 | m_gradientFunction = NULL; | |
747 | m_shading = NULL; | |
748 | m_gradientComponents = NULL; | |
749 | m_isShading = false; | |
750 | } | |
751 | ||
752 | void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context ) | |
753 | { | |
754 | CGContextRef cg = (CGContextRef) context->GetNativeContext(); | |
755 | ||
756 | if ( m_isShading ) | |
757 | { | |
758 | // nothing to set as shades are processed by clipping using the path and filling | |
759 | } | |
760 | else | |
761 | { | |
762 | m_cgColor.Apply( cg ); | |
763 | } | |
764 | } | |
765 | ||
766 | void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out) | |
767 | { | |
768 | CGFloat* colors = (CGFloat*) info ; | |
769 | CGFloat f = *in; | |
770 | for( int i = 0 ; i < 4 ; ++i ) | |
771 | { | |
772 | out[i] = colors[i] + ( colors[4+i] - colors[i] ) * f; | |
773 | } | |
774 | } | |
775 | ||
776 | CGFunctionRef wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour& c1, const wxColour& c2 ) | |
777 | { | |
778 | static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, NULL }; | |
779 | static const CGFloat input_value_range [2] = { 0, 1 }; | |
780 | static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 }; | |
781 | m_gradientComponents = new CGFloat[8] ; | |
782 | m_gradientComponents[0] = (CGFloat) (c1.Red() / 255.0); | |
783 | m_gradientComponents[1] = (CGFloat) (c1.Green() / 255.0); | |
784 | m_gradientComponents[2] = (CGFloat) (c1.Blue() / 255.0); | |
785 | m_gradientComponents[3] = (CGFloat) (c1.Alpha() / 255.0); | |
786 | m_gradientComponents[4] = (CGFloat) (c2.Red() / 255.0); | |
787 | m_gradientComponents[5] = (CGFloat) (c2.Green() / 255.0); | |
788 | m_gradientComponents[6] = (CGFloat) (c2.Blue() / 255.0); | |
789 | m_gradientComponents[7] = (CGFloat) (c2.Alpha() / 255.0); | |
790 | ||
791 | return CGFunctionCreate ( m_gradientComponents, 1, | |
792 | input_value_range, | |
793 | 4, | |
794 | output_value_ranges, | |
795 | &callbacks); | |
796 | } | |
797 | ||
798 | // | |
799 | // Font | |
800 | // | |
801 | ||
802 | #if wxOSX_USE_IPHONE | |
803 | ||
804 | extern UIFont* CreateUIFont( const wxFont& font ); | |
805 | extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text ); | |
806 | extern CGSize MeasureTextInContext( UIFont *font, NSString* text ); | |
807 | ||
808 | #endif | |
809 | ||
810 | class wxMacCoreGraphicsFontData : public wxGraphicsObjectRefData | |
811 | { | |
812 | public: | |
813 | wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col ); | |
814 | ~wxMacCoreGraphicsFontData(); | |
815 | ||
816 | #if wxOSX_USE_ATSU_TEXT | |
817 | virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; } | |
818 | #endif | |
819 | #if wxOSX_USE_CORE_TEXT | |
820 | CTFontRef OSXGetCTFont() const { return m_ctFont ; } | |
821 | #endif | |
822 | wxColour GetColour() const { return m_colour ; } | |
823 | ||
824 | bool GetUnderlined() const { return m_underlined ; } | |
825 | #if wxOSX_USE_IPHONE | |
826 | UIFont* GetUIFont() const { return m_uiFont; } | |
827 | #endif | |
828 | private : | |
829 | wxColour m_colour; | |
830 | bool m_underlined; | |
831 | #if wxOSX_USE_ATSU_TEXT | |
832 | ATSUStyle m_macATSUIStyle; | |
833 | #endif | |
834 | #if wxOSX_USE_CORE_TEXT | |
835 | wxCFRef< CTFontRef > m_ctFont; | |
836 | #endif | |
837 | #if wxOSX_USE_IPHONE | |
838 | UIFont* m_uiFont; | |
839 | #endif | |
840 | }; | |
841 | ||
842 | wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer ) | |
843 | { | |
844 | m_colour = col; | |
845 | m_underlined = font.GetUnderlined(); | |
846 | ||
847 | #if wxOSX_USE_CORE_TEXT | |
848 | m_ctFont.reset( wxMacCreateCTFont( font ) ); | |
849 | #endif | |
850 | #if wxOSX_USE_IPHONE | |
851 | m_uiFont = CreateUIFont(font); | |
852 | wxMacCocoaRetain( m_uiFont ); | |
853 | #endif | |
854 | #if wxOSX_USE_ATSU_TEXT | |
855 | OSStatus status = noErr; | |
856 | m_macATSUIStyle = NULL; | |
857 | ||
858 | status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle ); | |
859 | ||
860 | wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") ); | |
861 | ||
862 | // we need the scale here ... | |
863 | ||
864 | Fixed atsuSize = IntToFixed( int( 1 * font.GetPointSize()) ); | |
865 | RGBColor atsuColor ; | |
866 | col.GetRGBColor( &atsuColor ); | |
867 | ATSUAttributeTag atsuTags[] = | |
868 | { | |
869 | kATSUSizeTag , | |
870 | kATSUColorTag , | |
871 | }; | |
872 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
873 | { | |
874 | sizeof( Fixed ) , | |
875 | sizeof( RGBColor ) , | |
876 | }; | |
877 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
878 | { | |
879 | &atsuSize , | |
880 | &atsuColor , | |
881 | }; | |
882 | ||
883 | status = ::ATSUSetAttributes( | |
884 | m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) , | |
885 | atsuTags, atsuSizes, atsuValues); | |
886 | ||
887 | wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ); | |
888 | #endif | |
889 | } | |
890 | ||
891 | wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData() | |
892 | { | |
893 | #if wxOSX_USE_CORE_TEXT | |
894 | #endif | |
895 | #if wxOSX_USE_ATSU_TEXT | |
896 | if ( m_macATSUIStyle ) | |
897 | { | |
898 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle); | |
899 | m_macATSUIStyle = NULL; | |
900 | } | |
901 | #endif | |
902 | #if wxOSX_USE_IPHONE | |
903 | wxMacCocoaRelease( m_uiFont ); | |
904 | #endif | |
905 | } | |
906 | ||
907 | class wxMacCoreGraphicsBitmapData : public wxGraphicsObjectRefData | |
908 | { | |
909 | public: | |
910 | wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome ); | |
911 | ~wxMacCoreGraphicsBitmapData(); | |
912 | ||
913 | virtual CGImageRef GetBitmap() { return m_bitmap; } | |
914 | bool IsMonochrome() { return m_monochrome; } | |
915 | private : | |
916 | CGImageRef m_bitmap; | |
917 | bool m_monochrome; | |
918 | }; | |
919 | ||
920 | wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome ) : wxGraphicsObjectRefData( renderer ), | |
921 | m_bitmap(bitmap), m_monochrome(monochrome) | |
922 | { | |
923 | } | |
924 | ||
925 | wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData() | |
926 | { | |
927 | CGImageRelease( m_bitmap ); | |
928 | } | |
929 | ||
930 | // | |
931 | // Graphics Matrix | |
932 | // | |
933 | ||
934 | //----------------------------------------------------------------------------- | |
935 | // wxMacCoreGraphicsMatrix declaration | |
936 | //----------------------------------------------------------------------------- | |
937 | ||
938 | class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData | |
939 | { | |
940 | public : | |
941 | wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ; | |
942 | ||
943 | virtual ~wxMacCoreGraphicsMatrixData() ; | |
944 | ||
945 | virtual wxGraphicsObjectRefData *Clone() const ; | |
946 | ||
947 | // concatenates the matrix | |
948 | virtual void Concat( const wxGraphicsMatrixData *t ); | |
949 | ||
950 | // sets the matrix to the respective values | |
951 | virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, | |
952 | wxDouble tx=0.0, wxDouble ty=0.0); | |
953 | ||
954 | // gets the component valuess of the matrix | |
955 | virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL, | |
956 | wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const; | |
957 | ||
958 | // makes this the inverse matrix | |
959 | virtual void Invert(); | |
960 | ||
961 | // returns true if the elements of the transformation matrix are equal ? | |
962 | virtual bool IsEqual( const wxGraphicsMatrixData* t) const ; | |
963 | ||
964 | // return true if this is the identity matrix | |
965 | virtual bool IsIdentity() const; | |
966 | ||
967 | // | |
968 | // transformation | |
969 | // | |
970 | ||
971 | // add the translation to this matrix | |
972 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
973 | ||
974 | // add the scale to this matrix | |
975 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
976 | ||
977 | // add the rotation to this matrix (radians) | |
978 | virtual void Rotate( wxDouble angle ); | |
979 | ||
980 | // | |
981 | // apply the transforms | |
982 | // | |
983 | ||
984 | // applies that matrix to the point | |
985 | virtual void TransformPoint( wxDouble *x, wxDouble *y ) const; | |
986 | ||
987 | // applies the matrix except for translations | |
988 | virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const; | |
989 | ||
990 | // returns the native representation | |
991 | virtual void * GetNativeMatrix() const; | |
992 | ||
993 | private : | |
994 | CGAffineTransform m_matrix; | |
995 | } ; | |
996 | ||
997 | //----------------------------------------------------------------------------- | |
998 | // wxMacCoreGraphicsMatrix implementation | |
999 | //----------------------------------------------------------------------------- | |
1000 | ||
1001 | wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer) | |
1002 | { | |
1003 | } | |
1004 | ||
1005 | wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData() | |
1006 | { | |
1007 | } | |
1008 | ||
1009 | wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const | |
1010 | { | |
1011 | wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ; | |
1012 | m->m_matrix = m_matrix ; | |
1013 | return m; | |
1014 | } | |
1015 | ||
1016 | // concatenates the matrix | |
1017 | void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t ) | |
1018 | { | |
1019 | m_matrix = CGAffineTransformConcat(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()) ); | |
1020 | } | |
1021 | ||
1022 | // sets the matrix to the respective values | |
1023 | void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d, | |
1024 | wxDouble tx, wxDouble ty) | |
1025 | { | |
1026 | m_matrix = CGAffineTransformMake((CGFloat) a,(CGFloat) b,(CGFloat) c,(CGFloat) d,(CGFloat) tx,(CGFloat) ty); | |
1027 | } | |
1028 | ||
1029 | // gets the component valuess of the matrix | |
1030 | void wxMacCoreGraphicsMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c, | |
1031 | wxDouble* d, wxDouble* tx, wxDouble* ty) const | |
1032 | { | |
1033 | if (a) *a = m_matrix.a; | |
1034 | if (b) *b = m_matrix.b; | |
1035 | if (c) *c = m_matrix.c; | |
1036 | if (d) *d = m_matrix.d; | |
1037 | if (tx) *tx= m_matrix.tx; | |
1038 | if (ty) *ty= m_matrix.ty; | |
1039 | } | |
1040 | ||
1041 | // makes this the inverse matrix | |
1042 | void wxMacCoreGraphicsMatrixData::Invert() | |
1043 | { | |
1044 | m_matrix = CGAffineTransformInvert( m_matrix ); | |
1045 | } | |
1046 | ||
1047 | // returns true if the elements of the transformation matrix are equal ? | |
1048 | bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const | |
1049 | { | |
1050 | return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix())); | |
1051 | } | |
1052 | ||
1053 | // return true if this is the identity matrix | |
1054 | bool wxMacCoreGraphicsMatrixData::IsIdentity() const | |
1055 | { | |
1056 | return ( m_matrix.a == 1 && m_matrix.d == 1 && | |
1057 | m_matrix.b == 0 && m_matrix.d == 0 && m_matrix.tx == 0 && m_matrix.ty == 0); | |
1058 | } | |
1059 | ||
1060 | // | |
1061 | // transformation | |
1062 | // | |
1063 | ||
1064 | // add the translation to this matrix | |
1065 | void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx , wxDouble dy ) | |
1066 | { | |
1067 | m_matrix = CGAffineTransformTranslate( m_matrix, (CGFloat) dx, (CGFloat) dy); | |
1068 | } | |
1069 | ||
1070 | // add the scale to this matrix | |
1071 | void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale , wxDouble yScale ) | |
1072 | { | |
1073 | m_matrix = CGAffineTransformScale( m_matrix, (CGFloat) xScale, (CGFloat) yScale); | |
1074 | } | |
1075 | ||
1076 | // add the rotation to this matrix (radians) | |
1077 | void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle ) | |
1078 | { | |
1079 | m_matrix = CGAffineTransformRotate( m_matrix, (CGFloat) angle); | |
1080 | } | |
1081 | ||
1082 | // | |
1083 | // apply the transforms | |
1084 | // | |
1085 | ||
1086 | // applies that matrix to the point | |
1087 | void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const | |
1088 | { | |
1089 | CGPoint pt = CGPointApplyAffineTransform( CGPointMake((CGFloat) *x,(CGFloat) *y), m_matrix); | |
1090 | ||
1091 | *x = pt.x; | |
1092 | *y = pt.y; | |
1093 | } | |
1094 | ||
1095 | // applies the matrix except for translations | |
1096 | void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const | |
1097 | { | |
1098 | CGSize sz = CGSizeApplyAffineTransform( CGSizeMake((CGFloat) *dx,(CGFloat) *dy) , m_matrix ); | |
1099 | *dx = sz.width; | |
1100 | *dy = sz.height; | |
1101 | } | |
1102 | ||
1103 | // returns the native representation | |
1104 | void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const | |
1105 | { | |
1106 | return (void*) &m_matrix; | |
1107 | } | |
1108 | ||
1109 | // | |
1110 | // Graphics Path | |
1111 | // | |
1112 | ||
1113 | //----------------------------------------------------------------------------- | |
1114 | // wxMacCoreGraphicsPath declaration | |
1115 | //----------------------------------------------------------------------------- | |
1116 | ||
1117 | class WXDLLEXPORT wxMacCoreGraphicsPathData : public wxGraphicsPathData | |
1118 | { | |
1119 | public : | |
1120 | wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path = NULL); | |
1121 | ||
1122 | ~wxMacCoreGraphicsPathData(); | |
1123 | ||
1124 | virtual wxGraphicsObjectRefData *Clone() const; | |
1125 | ||
1126 | // begins a new subpath at (x,y) | |
1127 | virtual void MoveToPoint( wxDouble x, wxDouble y ); | |
1128 | ||
1129 | // adds a straight line from the current point to (x,y) | |
1130 | virtual void AddLineToPoint( wxDouble x, wxDouble y ); | |
1131 | ||
1132 | // adds a cubic Bezier curve from the current point, using two control points and an end point | |
1133 | virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ); | |
1134 | ||
1135 | // closes the current sub-path | |
1136 | virtual void CloseSubpath(); | |
1137 | ||
1138 | // gets the last point of the current path, (0,0) if not yet set | |
1139 | virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const; | |
1140 | ||
1141 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
1142 | virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ); | |
1143 | ||
1144 | // | |
1145 | // These are convenience functions which - if not available natively will be assembled | |
1146 | // using the primitives from above | |
1147 | // | |
1148 | ||
1149 | // adds a quadratic Bezier curve from the current point, using a control point and an end point | |
1150 | virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y ); | |
1151 | ||
1152 | // appends a rectangle as a new closed subpath | |
1153 | virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
1154 | ||
1155 | // appends an ellipsis as a new closed subpath fitting the passed rectangle | |
1156 | virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r ); | |
1157 | ||
1158 | // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1) | |
1159 | virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ); | |
1160 | ||
1161 | // adds another path | |
1162 | virtual void AddPath( const wxGraphicsPathData* path ); | |
1163 | ||
1164 | // returns the native path | |
1165 | virtual void * GetNativePath() const { return m_path; } | |
1166 | ||
1167 | // give the native path returned by GetNativePath() back (there might be some deallocations necessary) | |
1168 | virtual void UnGetNativePath(void *WXUNUSED(p)) const {} | |
1169 | ||
1170 | // transforms each point of this path by the matrix | |
1171 | virtual void Transform( const wxGraphicsMatrixData* matrix ); | |
1172 | ||
1173 | // gets the bounding box enclosing all points (possibly including control points) | |
1174 | virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *y) const; | |
1175 | ||
1176 | virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE) const; | |
1177 | private : | |
1178 | CGMutablePathRef m_path; | |
1179 | }; | |
1180 | ||
1181 | //----------------------------------------------------------------------------- | |
1182 | // wxMacCoreGraphicsPath implementation | |
1183 | //----------------------------------------------------------------------------- | |
1184 | ||
1185 | wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path) : wxGraphicsPathData(renderer) | |
1186 | { | |
1187 | if ( path ) | |
1188 | m_path = path; | |
1189 | else | |
1190 | m_path = CGPathCreateMutable(); | |
1191 | } | |
1192 | ||
1193 | wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData() | |
1194 | { | |
1195 | CGPathRelease( m_path ); | |
1196 | } | |
1197 | ||
1198 | wxGraphicsObjectRefData* wxMacCoreGraphicsPathData::Clone() const | |
1199 | { | |
1200 | wxMacCoreGraphicsPathData* clone = new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path)); | |
1201 | return clone ; | |
1202 | } | |
1203 | ||
1204 | ||
1205 | // opens (starts) a new subpath | |
1206 | void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1 , wxDouble y1 ) | |
1207 | { | |
1208 | CGPathMoveToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 ); | |
1209 | } | |
1210 | ||
1211 | void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1 , wxDouble y1 ) | |
1212 | { | |
1213 | CGPathAddLineToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 ); | |
1214 | } | |
1215 | ||
1216 | void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) | |
1217 | { | |
1218 | CGPathAddCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) cx2, (CGFloat) cy2, (CGFloat) x , (CGFloat) y ); | |
1219 | } | |
1220 | ||
1221 | void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y ) | |
1222 | { | |
1223 | CGPathAddQuadCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) x , (CGFloat) y ); | |
1224 | } | |
1225 | ||
1226 | void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1227 | { | |
1228 | CGRect cgRect = { { (CGFloat) x , (CGFloat) y } , { (CGFloat) w , (CGFloat) h } }; | |
1229 | CGPathAddRect( m_path , NULL , cgRect ); | |
1230 | } | |
1231 | ||
1232 | void wxMacCoreGraphicsPathData::AddCircle( wxDouble x, wxDouble y , wxDouble r ) | |
1233 | { | |
1234 | CGPathAddArc( m_path , NULL , (CGFloat) x , (CGFloat) y , (CGFloat) r , (CGFloat) 0.0 , (CGFloat) (2 * M_PI) , true ); | |
1235 | } | |
1236 | ||
1237 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
1238 | void wxMacCoreGraphicsPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) | |
1239 | { | |
1240 | // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup | |
1241 | CGPathAddArc( m_path, NULL , (CGFloat) x, (CGFloat) y, (CGFloat) r, (CGFloat) startAngle, (CGFloat) endAngle, !clockwise); | |
1242 | } | |
1243 | ||
1244 | void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) | |
1245 | { | |
1246 | CGPathAddArcToPoint( m_path, NULL , (CGFloat) x1, (CGFloat) y1, (CGFloat) x2, (CGFloat) y2, (CGFloat) r); | |
1247 | } | |
1248 | ||
1249 | void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData* path ) | |
1250 | { | |
1251 | CGPathAddPath( m_path , NULL, (CGPathRef) path->GetNativePath() ); | |
1252 | } | |
1253 | ||
1254 | // closes the current subpath | |
1255 | void wxMacCoreGraphicsPathData::CloseSubpath() | |
1256 | { | |
1257 | CGPathCloseSubpath( m_path ); | |
1258 | } | |
1259 | ||
1260 | // gets the last point of the current path, (0,0) if not yet set | |
1261 | void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const | |
1262 | { | |
1263 | CGPoint p = CGPathGetCurrentPoint( m_path ); | |
1264 | *x = p.x; | |
1265 | *y = p.y; | |
1266 | } | |
1267 | ||
1268 | // transforms each point of this path by the matrix | |
1269 | void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData* matrix ) | |
1270 | { | |
1271 | CGMutablePathRef p = CGPathCreateMutable() ; | |
1272 | CGPathAddPath( p, (CGAffineTransform*) matrix->GetNativeMatrix() , m_path ); | |
1273 | CGPathRelease( m_path ); | |
1274 | m_path = p; | |
1275 | } | |
1276 | ||
1277 | // gets the bounding box enclosing all points (possibly including control points) | |
1278 | void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const | |
1279 | { | |
1280 | CGRect bounds = CGPathGetBoundingBox( m_path ) ; | |
1281 | *x = bounds.origin.x; | |
1282 | *y = bounds.origin.y; | |
1283 | *w = bounds.size.width; | |
1284 | *h = bounds.size.height; | |
1285 | } | |
1286 | ||
1287 | bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle) const | |
1288 | { | |
1289 | return CGPathContainsPoint( m_path, NULL, CGPointMake((CGFloat) x,(CGFloat) y), fillStyle == wxODDEVEN_RULE ); | |
1290 | } | |
1291 | ||
1292 | // | |
1293 | // Graphics Context | |
1294 | // | |
1295 | ||
1296 | //----------------------------------------------------------------------------- | |
1297 | // wxMacCoreGraphicsContext declaration | |
1298 | //----------------------------------------------------------------------------- | |
1299 | ||
1300 | class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext | |
1301 | { | |
1302 | public: | |
1303 | wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width = 0, wxDouble height = 0 ); | |
1304 | ||
1305 | #if wxOSX_USE_CARBON | |
1306 | wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ); | |
1307 | #endif | |
1308 | ||
1309 | wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ); | |
1310 | ||
1311 | wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer); | |
1312 | ||
1313 | wxMacCoreGraphicsContext(); | |
1314 | ||
1315 | ~wxMacCoreGraphicsContext(); | |
1316 | ||
1317 | void Init(); | |
1318 | ||
1319 | // returns the size of the graphics context in device coordinates | |
1320 | virtual void GetSize( wxDouble* width, wxDouble* height); | |
1321 | ||
1322 | virtual void StartPage( wxDouble width, wxDouble height ); | |
1323 | ||
1324 | virtual void EndPage(); | |
1325 | ||
1326 | virtual void Flush(); | |
1327 | ||
1328 | // push the current state of the context, ie the transformation matrix on a stack | |
1329 | virtual void PushState(); | |
1330 | ||
1331 | // pops a stored state from the stack | |
1332 | virtual void PopState(); | |
1333 | ||
1334 | // clips drawings to the region | |
1335 | virtual void Clip( const wxRegion ®ion ); | |
1336 | ||
1337 | // clips drawings to the rect | |
1338 | virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
1339 | ||
1340 | // resets the clipping to original extent | |
1341 | virtual void ResetClip(); | |
1342 | ||
1343 | virtual void * GetNativeContext(); | |
1344 | ||
1345 | virtual bool SetAntialiasMode(wxAntialiasMode antialias); | |
1346 | ||
1347 | virtual bool SetCompositionMode(wxCompositionMode op); | |
1348 | ||
1349 | virtual void BeginLayer(wxDouble opacity); | |
1350 | ||
1351 | virtual void EndLayer(); | |
1352 | ||
1353 | // | |
1354 | // transformation | |
1355 | // | |
1356 | ||
1357 | // translate | |
1358 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
1359 | ||
1360 | // scale | |
1361 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
1362 | ||
1363 | // rotate (radians) | |
1364 | virtual void Rotate( wxDouble angle ); | |
1365 | ||
1366 | // concatenates this transform with the current transform of this context | |
1367 | virtual void ConcatTransform( const wxGraphicsMatrix& matrix ); | |
1368 | ||
1369 | // sets the transform of this context | |
1370 | virtual void SetTransform( const wxGraphicsMatrix& matrix ); | |
1371 | ||
1372 | // gets the matrix of this context | |
1373 | virtual wxGraphicsMatrix GetTransform() const; | |
1374 | // | |
1375 | // setting the paint | |
1376 | // | |
1377 | ||
1378 | // strokes along a path with the current pen | |
1379 | virtual void StrokePath( const wxGraphicsPath &path ); | |
1380 | ||
1381 | // fills a path with the current brush | |
1382 | virtual void FillPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE ); | |
1383 | ||
1384 | // draws a path by first filling and then stroking | |
1385 | virtual void DrawPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE ); | |
1386 | ||
1387 | virtual bool ShouldOffset() const | |
1388 | { | |
1389 | int penwidth = 0 ; | |
1390 | if ( !m_pen.IsNull() ) | |
1391 | { | |
1392 | penwidth = (int)((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->GetWidth(); | |
1393 | if ( penwidth == 0 ) | |
1394 | penwidth = 1; | |
1395 | } | |
1396 | return ( penwidth % 2 ) == 1; | |
1397 | } | |
1398 | // | |
1399 | // text | |
1400 | // | |
1401 | ||
1402 | virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height, | |
1403 | wxDouble *descent, wxDouble *externalLeading ) const; | |
1404 | ||
1405 | virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const; | |
1406 | ||
1407 | // | |
1408 | // image support | |
1409 | // | |
1410 | ||
1411 | virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
1412 | ||
1413 | virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
1414 | ||
1415 | virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
1416 | ||
1417 | void SetNativeContext( CGContextRef cg ); | |
1418 | ||
1419 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext) | |
1420 | ||
1421 | private: | |
1422 | bool EnsureIsValid(); | |
1423 | ||
1424 | virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y ); | |
1425 | virtual void DoDrawRotatedText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle ); | |
1426 | ||
1427 | CGContextRef m_cgContext; | |
1428 | #if wxOSX_USE_CARBON | |
1429 | WindowRef m_windowRef; | |
1430 | #else | |
1431 | WXWidget m_view; | |
1432 | #endif | |
1433 | bool m_contextSynthesized; | |
1434 | CGAffineTransform m_windowTransform; | |
1435 | wxDouble m_width; | |
1436 | wxDouble m_height; | |
1437 | bool m_invisible; | |
1438 | ||
1439 | #if wxOSX_USE_COCOA_OR_CARBON | |
1440 | wxCFRef<HIShapeRef> m_clipRgn; | |
1441 | #endif | |
1442 | }; | |
1443 | ||
1444 | //----------------------------------------------------------------------------- | |
1445 | // device context implementation | |
1446 | // | |
1447 | // more and more of the dc functionality should be implemented by calling | |
1448 | // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step | |
1449 | // also coordinate conversions should be moved to native matrix ops | |
1450 | //----------------------------------------------------------------------------- | |
1451 | ||
1452 | // we always stock two context states, one at entry, to be able to preserve the | |
1453 | // state we were called with, the other one after changing to HI Graphics orientation | |
1454 | // (this one is used for getting back clippings etc) | |
1455 | ||
1456 | //----------------------------------------------------------------------------- | |
1457 | // wxMacCoreGraphicsContext implementation | |
1458 | //----------------------------------------------------------------------------- | |
1459 | ||
1460 | IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext) | |
1461 | ||
1462 | class wxQuartzOffsetHelper | |
1463 | { | |
1464 | public : | |
1465 | wxQuartzOffsetHelper( CGContextRef cg , bool offset ) | |
1466 | { | |
1467 | m_cg = cg; | |
1468 | m_offset = offset; | |
1469 | if ( m_offset ) | |
1470 | CGContextTranslateCTM( m_cg, (CGFloat) 0.5, (CGFloat) 0.5 ); | |
1471 | } | |
1472 | ~wxQuartzOffsetHelper( ) | |
1473 | { | |
1474 | if ( m_offset ) | |
1475 | CGContextTranslateCTM( m_cg, (CGFloat) -0.5, (CGFloat) -0.5 ); | |
1476 | } | |
1477 | public : | |
1478 | CGContextRef m_cg; | |
1479 | bool m_offset; | |
1480 | } ; | |
1481 | ||
1482 | void wxMacCoreGraphicsContext::Init() | |
1483 | { | |
1484 | m_cgContext = NULL; | |
1485 | m_contextSynthesized = false; | |
1486 | m_width = 0; | |
1487 | m_height = 0; | |
1488 | #if wxOSX_USE_CARBON | |
1489 | m_windowRef = NULL; | |
1490 | #endif | |
1491 | #if wxOSX_USE_COCOA_OR_IPHONE | |
1492 | m_view = NULL; | |
1493 | #endif | |
1494 | m_invisible = false; | |
1495 | } | |
1496 | ||
1497 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width, wxDouble height ) : wxGraphicsContext(renderer) | |
1498 | { | |
1499 | Init(); | |
1500 | SetNativeContext(cgcontext); | |
1501 | m_width = width; | |
1502 | m_height = height; | |
1503 | } | |
1504 | ||
1505 | #if wxOSX_USE_CARBON | |
1506 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer) | |
1507 | { | |
1508 | Init(); | |
1509 | m_windowRef = window; | |
1510 | } | |
1511 | #endif | |
1512 | ||
1513 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer) | |
1514 | { | |
1515 | Init(); | |
1516 | ||
1517 | wxSize sz = window->GetSize(); | |
1518 | m_width = sz.x; | |
1519 | m_height = sz.y; | |
1520 | ||
1521 | #if wxOSX_USE_COCOA_OR_IPHONE | |
1522 | m_view = window->GetHandle(); | |
1523 | ||
1524 | #if wxOSX_USE_COCOA | |
1525 | if ( ! window->GetPeer()->IsFlipped() ) | |
1526 | { | |
1527 | m_windowTransform = CGAffineTransformMakeTranslation( 0 , m_height ); | |
1528 | m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 ); | |
1529 | } | |
1530 | else | |
1531 | #endif | |
1532 | { | |
1533 | m_windowTransform = CGAffineTransformIdentity; | |
1534 | } | |
1535 | #else | |
1536 | int originX , originY; | |
1537 | originX = originY = 0; | |
1538 | Rect bounds = { 0,0,0,0 }; | |
1539 | m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef(); | |
1540 | window->MacWindowToRootWindow( &originX , &originY ); | |
1541 | GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds ); | |
1542 | m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top ); | |
1543 | m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 ); | |
1544 | m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ; | |
1545 | #endif | |
1546 | } | |
1547 | ||
1548 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer) | |
1549 | { | |
1550 | Init(); | |
1551 | } | |
1552 | ||
1553 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL) | |
1554 | { | |
1555 | Init(); | |
1556 | wxLogDebug(wxT("Illegal Constructor called")); | |
1557 | } | |
1558 | ||
1559 | wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext() | |
1560 | { | |
1561 | SetNativeContext(NULL); | |
1562 | } | |
1563 | ||
1564 | void wxMacCoreGraphicsContext::GetSize( wxDouble* width, wxDouble* height) | |
1565 | { | |
1566 | *width = m_width; | |
1567 | *height = m_height; | |
1568 | } | |
1569 | ||
1570 | ||
1571 | void wxMacCoreGraphicsContext::StartPage( wxDouble width, wxDouble height ) | |
1572 | { | |
1573 | CGRect r; | |
1574 | if ( width != 0 && height != 0) | |
1575 | r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) width , (CGFloat) height ); | |
1576 | else | |
1577 | r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) m_width , (CGFloat) m_height ); | |
1578 | ||
1579 | CGContextBeginPage(m_cgContext, &r ); | |
1580 | // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height ); | |
1581 | // CGContextScaleCTM( m_cgContext , 1 , -1 ); | |
1582 | } | |
1583 | ||
1584 | void wxMacCoreGraphicsContext::EndPage() | |
1585 | { | |
1586 | CGContextEndPage(m_cgContext); | |
1587 | } | |
1588 | ||
1589 | void wxMacCoreGraphicsContext::Flush() | |
1590 | { | |
1591 | CGContextFlush(m_cgContext); | |
1592 | } | |
1593 | ||
1594 | bool wxMacCoreGraphicsContext::EnsureIsValid() | |
1595 | { | |
1596 | if ( !m_cgContext ) | |
1597 | { | |
1598 | if (m_invisible) | |
1599 | return false; | |
1600 | ||
1601 | #if wxOSX_USE_COCOA | |
1602 | if ( wxOSXLockFocus(m_view) ) | |
1603 | { | |
1604 | m_cgContext = wxOSXGetContextFromCurrentContext(); | |
1605 | wxASSERT_MSG( m_cgContext != NULL, _T("Unable to retrieve drawing context from View")); | |
1606 | } | |
1607 | else | |
1608 | { | |
1609 | m_invisible = true; | |
1610 | } | |
1611 | #endif | |
1612 | #if wxOSX_USE_IPHONE | |
1613 | m_cgContext = wxOSXGetContextFromCurrentContext(); | |
1614 | if ( m_cgContext == NULL ) | |
1615 | { | |
1616 | m_invisible = true; | |
1617 | } | |
1618 | #endif | |
1619 | #if wxOSX_USE_CARBON | |
1620 | OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext ); | |
1621 | if ( status != noErr ) | |
1622 | { | |
1623 | wxFAIL_MSG("Cannot nest wxDCs on the same window"); | |
1624 | } | |
1625 | #endif | |
1626 | if ( m_cgContext ) | |
1627 | { | |
1628 | CGContextConcatCTM( m_cgContext, m_windowTransform ); | |
1629 | CGContextSaveGState( m_cgContext ); | |
1630 | m_contextSynthesized = true; | |
1631 | #if wxOSX_USE_COCOA_OR_CARBON | |
1632 | if ( m_clipRgn.get() ) | |
1633 | { | |
1634 | // the clip region is in device coordinates, so we convert this again to user coordinates | |
1635 | wxCFRef<HIMutableShapeRef> hishape( HIShapeCreateMutableCopy( m_clipRgn ) ); | |
1636 | CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero,m_windowTransform); | |
1637 | HIShapeOffset( hishape, -transformedOrigin.x, -transformedOrigin.y ); | |
1638 | // if the shape is empty, HIShapeReplacePathInCGContext doesn't work | |
1639 | if ( HIShapeIsEmpty(hishape)) | |
1640 | { | |
1641 | CGRect empty = CGRectMake( 0,0,0,0 ); | |
1642 | CGContextClipToRect( m_cgContext, empty ); | |
1643 | } | |
1644 | else | |
1645 | { | |
1646 | HIShapeReplacePathInCGContext( hishape, m_cgContext ); | |
1647 | CGContextClip( m_cgContext ); | |
1648 | } | |
1649 | } | |
1650 | #endif | |
1651 | CGContextSaveGState( m_cgContext ); | |
1652 | ||
1653 | #if 0 // turn on for debugging of clientdc | |
1654 | static float color = 0.5 ; | |
1655 | static int channel = 0 ; | |
1656 | CGRect bounds = CGRectMake(-1000,-1000,2000,2000); | |
1657 | CGContextSetRGBFillColor( m_cgContext, channel == 0 ? color : 0.5 , | |
1658 | channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 ); | |
1659 | CGContextFillRect( m_cgContext, bounds ); | |
1660 | color += 0.1 ; | |
1661 | if ( color > 0.9 ) | |
1662 | { | |
1663 | color = 0.5 ; | |
1664 | channel++ ; | |
1665 | if ( channel == 3 ) | |
1666 | channel = 0 ; | |
1667 | } | |
1668 | #endif | |
1669 | } | |
1670 | } | |
1671 | return m_cgContext != NULL; | |
1672 | } | |
1673 | ||
1674 | bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias) | |
1675 | { | |
1676 | if (EnsureIsValid()==false) | |
1677 | return true; | |
1678 | ||
1679 | if (m_antialias == antialias) | |
1680 | return true; | |
1681 | ||
1682 | m_antialias = antialias; | |
1683 | ||
1684 | bool antialiasMode; | |
1685 | switch (antialias) | |
1686 | { | |
1687 | case wxANTIALIAS_DEFAULT: | |
1688 | antialiasMode = true; | |
1689 | break; | |
1690 | case wxANTIALIAS_NONE: | |
1691 | antialiasMode = false; | |
1692 | break; | |
1693 | default: | |
1694 | return false; | |
1695 | } | |
1696 | CGContextSetShouldAntialias(m_cgContext, antialiasMode); | |
1697 | return true; | |
1698 | } | |
1699 | ||
1700 | bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op) | |
1701 | { | |
1702 | if (EnsureIsValid()==false) | |
1703 | return true; | |
1704 | ||
1705 | if ( m_composition == op ) | |
1706 | return true; | |
1707 | ||
1708 | m_composition = op; | |
1709 | ||
1710 | if (m_composition == wxCOMPOSITION_DEST) | |
1711 | return true; | |
1712 | ||
1713 | #if wxOSX_USE_COCOA_OR_CARBON | |
1714 | if ( UMAGetSystemVersion() < 0x1060 ) | |
1715 | { | |
1716 | CGCompositeOperation cop = kCGCompositeOperationSourceOver; | |
1717 | CGBlendMode mode = kCGBlendModeNormal; | |
1718 | switch( op ) | |
1719 | { | |
1720 | case wxCOMPOSITION_CLEAR: | |
1721 | cop = kCGCompositeOperationClear; | |
1722 | break; | |
1723 | case wxCOMPOSITION_SOURCE: | |
1724 | cop = kCGCompositeOperationCopy; | |
1725 | break; | |
1726 | case wxCOMPOSITION_OVER: | |
1727 | mode = kCGBlendModeNormal; | |
1728 | break; | |
1729 | case wxCOMPOSITION_IN: | |
1730 | cop = kCGCompositeOperationSourceIn; | |
1731 | break; | |
1732 | case wxCOMPOSITION_OUT: | |
1733 | cop = kCGCompositeOperationSourceOut; | |
1734 | break; | |
1735 | case wxCOMPOSITION_ATOP: | |
1736 | cop = kCGCompositeOperationSourceAtop; | |
1737 | break; | |
1738 | case wxCOMPOSITION_DEST_OVER: | |
1739 | cop = kCGCompositeOperationDestinationOver; | |
1740 | break; | |
1741 | case wxCOMPOSITION_DEST_IN: | |
1742 | cop = kCGCompositeOperationDestinationIn; | |
1743 | break; | |
1744 | case wxCOMPOSITION_DEST_OUT: | |
1745 | cop = kCGCompositeOperationDestinationOut; | |
1746 | break; | |
1747 | case wxCOMPOSITION_DEST_ATOP: | |
1748 | cop = kCGCompositeOperationDestinationAtop; | |
1749 | break; | |
1750 | case wxCOMPOSITION_XOR: | |
1751 | cop = kCGCompositeOperationXOR; | |
1752 | break; | |
1753 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 | |
1754 | case wxCOMPOSITION_ADD: | |
1755 | mode = kCGBlendModePlusLighter ; | |
1756 | break; | |
1757 | #endif | |
1758 | default: | |
1759 | return false; | |
1760 | } | |
1761 | if ( cop != kCGCompositeOperationSourceOver ) | |
1762 | CGContextSetCompositeOperation(m_cgContext, cop); | |
1763 | else | |
1764 | CGContextSetBlendMode(m_cgContext, mode); | |
1765 | } | |
1766 | #endif | |
1767 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 | |
1768 | else | |
1769 | { | |
1770 | CGBlendMode mode = kCGBlendModeNormal; | |
1771 | switch( op ) | |
1772 | { | |
1773 | case wxCOMPOSITION_CLEAR: | |
1774 | mode = kCGBlendModeClear; | |
1775 | break; | |
1776 | case wxCOMPOSITION_SOURCE: | |
1777 | mode = kCGBlendModeCopy; | |
1778 | break; | |
1779 | case wxCOMPOSITION_OVER: | |
1780 | mode = kCGBlendModeNormal; | |
1781 | break; | |
1782 | case wxCOMPOSITION_IN: | |
1783 | mode = kCGBlendModeSourceIn; | |
1784 | break; | |
1785 | case wxCOMPOSITION_OUT: | |
1786 | mode = kCGBlendModeSourceOut; | |
1787 | break; | |
1788 | case wxCOMPOSITION_ATOP: | |
1789 | mode = kCGBlendModeSourceAtop; | |
1790 | break; | |
1791 | case wxCOMPOSITION_DEST_OVER: | |
1792 | mode = kCGBlendModeDestinationOver; | |
1793 | break; | |
1794 | case wxCOMPOSITION_DEST_IN: | |
1795 | mode = kCGBlendModeDestinationIn; | |
1796 | break; | |
1797 | case wxCOMPOSITION_DEST_OUT: | |
1798 | mode = kCGBlendModeDestinationOut; | |
1799 | break; | |
1800 | case wxCOMPOSITION_DEST_ATOP: | |
1801 | mode = kCGBlendModeDestinationAtop; | |
1802 | break; | |
1803 | case wxCOMPOSITION_XOR: | |
1804 | mode = kCGBlendModeXOR; | |
1805 | break; | |
1806 | ||
1807 | case wxCOMPOSITION_ADD: | |
1808 | mode = kCGBlendModePlusLighter ; | |
1809 | break; | |
1810 | default: | |
1811 | return false; | |
1812 | } | |
1813 | CGContextSetBlendMode(m_cgContext, mode); | |
1814 | } | |
1815 | #endif | |
1816 | return true; | |
1817 | } | |
1818 | ||
1819 | void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity) | |
1820 | { | |
1821 | CGContextSaveGState(m_cgContext); | |
1822 | CGContextSetAlpha(m_cgContext, (CGFloat) opacity); | |
1823 | CGContextBeginTransparencyLayer(m_cgContext, 0); | |
1824 | } | |
1825 | ||
1826 | void wxMacCoreGraphicsContext::EndLayer() | |
1827 | { | |
1828 | CGContextEndTransparencyLayer(m_cgContext); | |
1829 | CGContextRestoreGState(m_cgContext); | |
1830 | } | |
1831 | ||
1832 | void wxMacCoreGraphicsContext::Clip( const wxRegion ®ion ) | |
1833 | { | |
1834 | #if wxOSX_USE_COCOA_OR_CARBON | |
1835 | if( m_cgContext ) | |
1836 | { | |
1837 | wxCFRef<HIShapeRef> shape = wxCFRefFromGet(region.GetWXHRGN()); | |
1838 | // if the shape is empty, HIShapeReplacePathInCGContext doesn't work | |
1839 | if ( HIShapeIsEmpty(shape)) | |
1840 | { | |
1841 | CGRect empty = CGRectMake( 0,0,0,0 ); | |
1842 | CGContextClipToRect( m_cgContext, empty ); | |
1843 | } | |
1844 | else | |
1845 | { | |
1846 | HIShapeReplacePathInCGContext( shape, m_cgContext ); | |
1847 | CGContextClip( m_cgContext ); | |
1848 | } | |
1849 | } | |
1850 | else | |
1851 | { | |
1852 | // this offsetting to device coords is not really correct, but since we cannot apply affine transforms | |
1853 | // to regions we try at least to have correct translations | |
1854 | HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( region.GetWXHRGN() ); | |
1855 | ||
1856 | CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform ); | |
1857 | HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y ); | |
1858 | m_clipRgn.reset(mutableShape); | |
1859 | } | |
1860 | #else | |
1861 | // allow usage as measuring context | |
1862 | // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" ); | |
1863 | #endif | |
1864 | } | |
1865 | ||
1866 | // clips drawings to the rect | |
1867 | void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1868 | { | |
1869 | CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ); | |
1870 | if ( m_cgContext ) | |
1871 | { | |
1872 | CGContextClipToRect( m_cgContext, r ); | |
1873 | } | |
1874 | else | |
1875 | { | |
1876 | #if wxOSX_USE_COCOA_OR_CARBON | |
1877 | // the clipping itself must be stored as device coordinates, otherwise | |
1878 | // we cannot apply it back correctly | |
1879 | r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform ); | |
1880 | m_clipRgn.reset(HIShapeCreateWithRect(&r)); | |
1881 | #else | |
1882 | // allow usage as measuring context | |
1883 | // wxFAIL_MSG( "Needs a valid context for clipping" ); | |
1884 | #endif | |
1885 | } | |
1886 | } | |
1887 | ||
1888 | // resets the clipping to original extent | |
1889 | void wxMacCoreGraphicsContext::ResetClip() | |
1890 | { | |
1891 | if ( m_cgContext ) | |
1892 | { | |
1893 | // there is no way for clearing the clip, we can only revert to the stored | |
1894 | // state, but then we have to make sure everything else is NOT restored | |
1895 | CGAffineTransform transform = CGContextGetCTM( m_cgContext ); | |
1896 | CGContextRestoreGState( m_cgContext ); | |
1897 | CGContextSaveGState( m_cgContext ); | |
1898 | CGAffineTransform transformNew = CGContextGetCTM( m_cgContext ); | |
1899 | transformNew = CGAffineTransformInvert( transformNew ) ; | |
1900 | CGContextConcatCTM( m_cgContext, transformNew); | |
1901 | CGContextConcatCTM( m_cgContext, transform); | |
1902 | } | |
1903 | else | |
1904 | { | |
1905 | #if wxOSX_USE_COCOA_OR_CARBON | |
1906 | m_clipRgn.reset(); | |
1907 | #else | |
1908 | // allow usage as measuring context | |
1909 | // wxFAIL_MSG( "Needs a valid context for clipping" ); | |
1910 | #endif | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path ) | |
1915 | { | |
1916 | if ( m_pen.IsNull() ) | |
1917 | return ; | |
1918 | ||
1919 | if (EnsureIsValid()==false) | |
1920 | return; | |
1921 | ||
1922 | if (m_composition == wxCOMPOSITION_DEST) | |
1923 | return; | |
1924 | ||
1925 | wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() ); | |
1926 | ||
1927 | ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this); | |
1928 | CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() ); | |
1929 | CGContextStrokePath( m_cgContext ); | |
1930 | } | |
1931 | ||
1932 | void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle ) | |
1933 | { | |
1934 | if (EnsureIsValid()==false) | |
1935 | return; | |
1936 | ||
1937 | if (m_composition == wxCOMPOSITION_DEST) | |
1938 | return; | |
1939 | ||
1940 | if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() ) | |
1941 | { | |
1942 | // when using shading, we cannot draw pen and brush at the same time | |
1943 | // revert to the base implementation of first filling and then stroking | |
1944 | wxGraphicsContext::DrawPath( path, fillStyle ); | |
1945 | return; | |
1946 | } | |
1947 | ||
1948 | CGPathDrawingMode mode = kCGPathFill ; | |
1949 | if ( m_brush.IsNull() ) | |
1950 | { | |
1951 | if ( m_pen.IsNull() ) | |
1952 | return; | |
1953 | else | |
1954 | mode = kCGPathStroke; | |
1955 | } | |
1956 | else | |
1957 | { | |
1958 | if ( m_pen.IsNull() ) | |
1959 | { | |
1960 | if ( fillStyle == wxODDEVEN_RULE ) | |
1961 | mode = kCGPathEOFill; | |
1962 | else | |
1963 | mode = kCGPathFill; | |
1964 | } | |
1965 | else | |
1966 | { | |
1967 | if ( fillStyle == wxODDEVEN_RULE ) | |
1968 | mode = kCGPathEOFillStroke; | |
1969 | else | |
1970 | mode = kCGPathFillStroke; | |
1971 | } | |
1972 | } | |
1973 | ||
1974 | if ( !m_brush.IsNull() ) | |
1975 | ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this); | |
1976 | if ( !m_pen.IsNull() ) | |
1977 | ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this); | |
1978 | ||
1979 | wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() ); | |
1980 | ||
1981 | CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() ); | |
1982 | CGContextDrawPath( m_cgContext , mode ); | |
1983 | } | |
1984 | ||
1985 | void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle ) | |
1986 | { | |
1987 | if ( m_brush.IsNull() ) | |
1988 | return; | |
1989 | ||
1990 | if (EnsureIsValid()==false) | |
1991 | return; | |
1992 | ||
1993 | if (m_composition == wxCOMPOSITION_DEST) | |
1994 | return; | |
1995 | ||
1996 | if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() ) | |
1997 | { | |
1998 | CGContextSaveGState( m_cgContext ); | |
1999 | CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() ); | |
2000 | CGContextClip( m_cgContext ); | |
2001 | CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() ); | |
2002 | CGContextRestoreGState( m_cgContext); | |
2003 | } | |
2004 | else | |
2005 | { | |
2006 | ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this); | |
2007 | CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() ); | |
2008 | if ( fillStyle == wxODDEVEN_RULE ) | |
2009 | CGContextEOFillPath( m_cgContext ); | |
2010 | else | |
2011 | CGContextFillPath( m_cgContext ); | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg ) | |
2016 | { | |
2017 | // we allow either setting or clearing but not replacing | |
2018 | wxASSERT( m_cgContext == NULL || cg == NULL ); | |
2019 | ||
2020 | if ( m_cgContext ) | |
2021 | { | |
2022 | CGContextRestoreGState( m_cgContext ); | |
2023 | CGContextRestoreGState( m_cgContext ); | |
2024 | if ( m_contextSynthesized ) | |
2025 | { | |
2026 | // TODO: in case of performance problems, try issuing this not too | |
2027 | // frequently (half of refresh rate) | |
2028 | CGContextFlush(m_cgContext); | |
2029 | #if wxOSX_USE_CARBON | |
2030 | QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext); | |
2031 | #endif | |
2032 | #if wxOSX_USE_COCOA | |
2033 | wxOSXUnlockFocus(m_view); | |
2034 | #endif | |
2035 | } | |
2036 | else | |
2037 | CGContextRelease(m_cgContext); | |
2038 | } | |
2039 | ||
2040 | m_cgContext = cg; | |
2041 | ||
2042 | // FIXME: This check is needed because currently we need to use a DC/GraphicsContext | |
2043 | // in order to get font properties, like wxFont::GetPixelSize, but since we don't have | |
2044 | // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef | |
2045 | // for this one operation. | |
2046 | ||
2047 | // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check | |
2048 | // can be removed. | |
2049 | if (m_cgContext) | |
2050 | { | |
2051 | CGContextRetain(m_cgContext); | |
2052 | CGContextSaveGState( m_cgContext ); | |
2053 | CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity ); | |
2054 | CGContextSaveGState( m_cgContext ); | |
2055 | m_contextSynthesized = false; | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy ) | |
2060 | { | |
2061 | if ( m_cgContext ) | |
2062 | CGContextTranslateCTM( m_cgContext, (CGFloat) dx, (CGFloat) dy ); | |
2063 | else | |
2064 | m_windowTransform = CGAffineTransformTranslate(m_windowTransform, (CGFloat) dx, (CGFloat) dy); | |
2065 | } | |
2066 | ||
2067 | void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale ) | |
2068 | { | |
2069 | if ( m_cgContext ) | |
2070 | CGContextScaleCTM( m_cgContext , (CGFloat) xScale , (CGFloat) yScale ); | |
2071 | else | |
2072 | m_windowTransform = CGAffineTransformScale(m_windowTransform, (CGFloat) xScale, (CGFloat) yScale); | |
2073 | } | |
2074 | ||
2075 | void wxMacCoreGraphicsContext::Rotate( wxDouble angle ) | |
2076 | { | |
2077 | if ( m_cgContext ) | |
2078 | CGContextRotateCTM( m_cgContext , (CGFloat) angle ); | |
2079 | else | |
2080 | m_windowTransform = CGAffineTransformRotate(m_windowTransform, (CGFloat) angle); | |
2081 | } | |
2082 | ||
2083 | void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
2084 | { | |
2085 | wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp); | |
2086 | DrawBitmap(bitmap, x, y, w, h); | |
2087 | } | |
2088 | ||
2089 | void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
2090 | { | |
2091 | if (EnsureIsValid()==false) | |
2092 | return; | |
2093 | ||
2094 | if (m_composition == wxCOMPOSITION_DEST) | |
2095 | return; | |
2096 | ||
2097 | #ifdef __WXMAC__ | |
2098 | wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData()); | |
2099 | CGImageRef image = refdata->GetBitmap(); | |
2100 | CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ); | |
2101 | if ( refdata->IsMonochrome() == 1 ) | |
2102 | { | |
2103 | // is is a mask, the '1' in the mask tell where to draw the current brush | |
2104 | if ( !m_brush.IsNull() ) | |
2105 | { | |
2106 | if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() ) | |
2107 | { | |
2108 | // TODO clip to mask | |
2109 | /* | |
2110 | CGContextSaveGState( m_cgContext ); | |
2111 | CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() ); | |
2112 | CGContextClip( m_cgContext ); | |
2113 | CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() ); | |
2114 | CGContextRestoreGState( m_cgContext); | |
2115 | */ | |
2116 | } | |
2117 | else | |
2118 | { | |
2119 | ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this); | |
2120 | wxMacDrawCGImage( m_cgContext , &r , image ); | |
2121 | } | |
2122 | } | |
2123 | } | |
2124 | else | |
2125 | { | |
2126 | wxMacDrawCGImage( m_cgContext , &r , image ); | |
2127 | } | |
2128 | #endif | |
2129 | } | |
2130 | ||
2131 | void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
2132 | { | |
2133 | if (EnsureIsValid()==false) | |
2134 | return; | |
2135 | ||
2136 | if (m_composition == wxCOMPOSITION_DEST) | |
2137 | return; | |
2138 | ||
2139 | CGRect r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) w , (CGFloat) h ); | |
2140 | CGContextSaveGState( m_cgContext ); | |
2141 | CGContextTranslateCTM( m_cgContext,(CGFloat) x ,(CGFloat) (y + h) ); | |
2142 | CGContextScaleCTM( m_cgContext, 1, -1 ); | |
2143 | #if wxOSX_USE_COCOA_OR_CARBON | |
2144 | PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone , | |
2145 | NULL , kPlotIconRefNormalFlags , icon.GetHICON() ); | |
2146 | #endif | |
2147 | CGContextRestoreGState( m_cgContext ); | |
2148 | } | |
2149 | ||
2150 | void wxMacCoreGraphicsContext::PushState() | |
2151 | { | |
2152 | if (EnsureIsValid()==false) | |
2153 | return; | |
2154 | ||
2155 | CGContextSaveGState( m_cgContext ); | |
2156 | } | |
2157 | ||
2158 | void wxMacCoreGraphicsContext::PopState() | |
2159 | { | |
2160 | if (EnsureIsValid()==false) | |
2161 | return; | |
2162 | ||
2163 | CGContextRestoreGState( m_cgContext ); | |
2164 | } | |
2165 | ||
2166 | void wxMacCoreGraphicsContext::DoDrawText( const wxString &str, wxDouble x, wxDouble y ) | |
2167 | { | |
2168 | wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") ); | |
2169 | ||
2170 | if (EnsureIsValid()==false) | |
2171 | return; | |
2172 | ||
2173 | if (m_composition == wxCOMPOSITION_DEST) | |
2174 | return; | |
2175 | ||
2176 | #if wxOSX_USE_CORE_TEXT | |
2177 | if ( UMAGetSystemVersion() >= 0x1050 ) | |
2178 | { | |
2179 | wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData(); | |
2180 | wxCFStringRef text(str, wxLocale::GetSystemEncoding() ); | |
2181 | CTFontRef font = fref->OSXGetCTFont(); | |
2182 | CGColorRef col = wxMacCreateCGColor( fref->GetColour() ); | |
2183 | CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ; | |
2184 | wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) ); | |
2185 | CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName }; | |
2186 | CFTypeRef values[] = { font, col, underlined }; | |
2187 | wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values, | |
2188 | WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) ); | |
2189 | wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) ); | |
2190 | wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) ); | |
2191 | ||
2192 | y += CTFontGetAscent(font); | |
2193 | ||
2194 | CGContextSaveGState(m_cgContext); | |
2195 | CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y); | |
2196 | CGContextScaleCTM(m_cgContext, 1, -1); | |
2197 | CGContextSetTextPosition(m_cgContext, 0, 0); | |
2198 | CTLineDraw( line, m_cgContext ); | |
2199 | CGContextRestoreGState(m_cgContext); | |
2200 | CFRelease( col ); | |
2201 | return; | |
2202 | } | |
2203 | #endif | |
2204 | #if wxOSX_USE_ATSU_TEXT | |
2205 | { | |
2206 | DrawText(str, x, y, 0.0); | |
2207 | return; | |
2208 | } | |
2209 | #endif | |
2210 | #if wxOSX_USE_IPHONE | |
2211 | wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData(); | |
2212 | ||
2213 | CGContextSaveGState(m_cgContext); | |
2214 | ||
2215 | CGColorRef col = wxMacCreateCGColor( fref->GetColour() ); | |
2216 | CGContextSetTextDrawingMode (m_cgContext, kCGTextFill); | |
2217 | CGContextSetFillColorWithColor( m_cgContext, col ); | |
2218 | ||
2219 | wxCFStringRef text(str, wxLocale::GetSystemEncoding() ); | |
2220 | DrawTextInContext( m_cgContext, CGPointMake( x, y ), fref->GetUIFont() , text.AsNSString() ); | |
2221 | ||
2222 | CGContextRestoreGState(m_cgContext); | |
2223 | CFRelease( col ); | |
2224 | #endif | |
2225 | } | |
2226 | ||
2227 | void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString &str, | |
2228 | wxDouble x, wxDouble y, | |
2229 | wxDouble angle) | |
2230 | { | |
2231 | wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") ); | |
2232 | ||
2233 | if (EnsureIsValid()==false) | |
2234 | return; | |
2235 | ||
2236 | if (m_composition == wxCOMPOSITION_DEST) | |
2237 | return; | |
2238 | ||
2239 | #if wxOSX_USE_CORE_TEXT | |
2240 | if ( UMAGetSystemVersion() >= 0x1050 ) | |
2241 | { | |
2242 | // default implementation takes care of rotation and calls non rotated DrawText afterwards | |
2243 | wxGraphicsContext::DoDrawRotatedText( str, x, y, angle ); | |
2244 | return; | |
2245 | } | |
2246 | #endif | |
2247 | #if wxOSX_USE_ATSU_TEXT | |
2248 | { | |
2249 | OSStatus status = noErr; | |
2250 | ATSUTextLayout atsuLayout; | |
2251 | wxMacUniCharBuffer unibuf( str ); | |
2252 | UniCharCount chars = unibuf.GetChars(); | |
2253 | ||
2254 | ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle()); | |
2255 | status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 , | |
2256 | &chars , &style , &atsuLayout ); | |
2257 | ||
2258 | wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") ); | |
2259 | ||
2260 | status = ::ATSUSetTransientFontMatching( atsuLayout , true ); | |
2261 | wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") ); | |
2262 | ||
2263 | int iAngle = int( angle * RAD2DEG ); | |
2264 | if ( abs(iAngle) > 0 ) | |
2265 | { | |
2266 | Fixed atsuAngle = IntToFixed( iAngle ); | |
2267 | ATSUAttributeTag atsuTags[] = | |
2268 | { | |
2269 | kATSULineRotationTag , | |
2270 | }; | |
2271 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
2272 | { | |
2273 | sizeof( Fixed ) , | |
2274 | }; | |
2275 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
2276 | { | |
2277 | &atsuAngle , | |
2278 | }; | |
2279 | status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag), | |
2280 | atsuTags, atsuSizes, atsuValues ); | |
2281 | } | |
2282 | ||
2283 | { | |
2284 | ATSUAttributeTag atsuTags[] = | |
2285 | { | |
2286 | kATSUCGContextTag , | |
2287 | }; | |
2288 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
2289 | { | |
2290 | sizeof( CGContextRef ) , | |
2291 | }; | |
2292 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
2293 | { | |
2294 | &m_cgContext , | |
2295 | }; | |
2296 | status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag), | |
2297 | atsuTags, atsuSizes, atsuValues ); | |
2298 | } | |
2299 | ||
2300 | ATSUTextMeasurement textBefore, textAfter; | |
2301 | ATSUTextMeasurement ascent, descent; | |
2302 | ||
2303 | status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
2304 | &textBefore , &textAfter, &ascent , &descent ); | |
2305 | ||
2306 | wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") ); | |
2307 | ||
2308 | Rect rect; | |
2309 | x += (int)(sin(angle) * FixedToInt(ascent)); | |
2310 | y += (int)(cos(angle) * FixedToInt(ascent)); | |
2311 | ||
2312 | status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
2313 | IntToFixed(x) , IntToFixed(y) , &rect ); | |
2314 | wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") ); | |
2315 | ||
2316 | CGContextSaveGState(m_cgContext); | |
2317 | CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y); | |
2318 | CGContextScaleCTM(m_cgContext, 1, -1); | |
2319 | status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
2320 | IntToFixed(0) , IntToFixed(0) ); | |
2321 | ||
2322 | wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") ); | |
2323 | ||
2324 | CGContextRestoreGState(m_cgContext); | |
2325 | ||
2326 | ::ATSUDisposeTextLayout(atsuLayout); | |
2327 | ||
2328 | return; | |
2329 | } | |
2330 | #endif | |
2331 | #if wxOSX_USE_IPHONE | |
2332 | // default implementation takes care of rotation and calls non rotated DrawText afterwards | |
2333 | wxGraphicsContext::DoDrawRotatedText( str, x, y, angle ); | |
2334 | #endif | |
2335 | } | |
2336 | ||
2337 | void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
2338 | wxDouble *descent, wxDouble *externalLeading ) const | |
2339 | { | |
2340 | wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") ); | |
2341 | ||
2342 | if ( width ) | |
2343 | *width = 0; | |
2344 | if ( height ) | |
2345 | *height = 0; | |
2346 | if ( descent ) | |
2347 | *descent = 0; | |
2348 | if ( externalLeading ) | |
2349 | *externalLeading = 0; | |
2350 | ||
2351 | if (str.empty()) | |
2352 | return; | |
2353 | ||
2354 | #if wxOSX_USE_CORE_TEXT | |
2355 | if ( UMAGetSystemVersion() >= 0x1050 ) | |
2356 | { | |
2357 | wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData(); | |
2358 | CTFontRef font = fref->OSXGetCTFont(); | |
2359 | ||
2360 | wxCFStringRef text(str, wxLocale::GetSystemEncoding() ); | |
2361 | CFStringRef keys[] = { kCTFontAttributeName }; | |
2362 | CFTypeRef values[] = { font }; | |
2363 | wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values, | |
2364 | WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) ); | |
2365 | wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) ); | |
2366 | wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) ); | |
2367 | ||
2368 | double w; | |
2369 | CGFloat a, d, l; | |
2370 | ||
2371 | w = CTLineGetTypographicBounds(line, &a, &d, &l) ; | |
2372 | ||
2373 | if ( height ) | |
2374 | *height = a+d+l; | |
2375 | if ( descent ) | |
2376 | *descent = d; | |
2377 | if ( externalLeading ) | |
2378 | *externalLeading = l; | |
2379 | if ( width ) | |
2380 | *width = w; | |
2381 | return; | |
2382 | } | |
2383 | #endif | |
2384 | #if wxOSX_USE_ATSU_TEXT | |
2385 | { | |
2386 | OSStatus status = noErr; | |
2387 | ||
2388 | ATSUTextLayout atsuLayout; | |
2389 | wxMacUniCharBuffer unibuf( str ); | |
2390 | UniCharCount chars = unibuf.GetChars(); | |
2391 | ||
2392 | ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle()); | |
2393 | status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 , | |
2394 | &chars , &style , &atsuLayout ); | |
2395 | ||
2396 | wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") ); | |
2397 | ||
2398 | status = ::ATSUSetTransientFontMatching( atsuLayout , true ); | |
2399 | wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") ); | |
2400 | ||
2401 | ATSUTextMeasurement textBefore, textAfter; | |
2402 | ATSUTextMeasurement textAscent, textDescent; | |
2403 | ||
2404 | status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
2405 | &textBefore , &textAfter, &textAscent , &textDescent ); | |
2406 | ||
2407 | if ( height ) | |
2408 | *height = FixedToInt(textAscent + textDescent); | |
2409 | if ( descent ) | |
2410 | *descent = FixedToInt(textDescent); | |
2411 | if ( externalLeading ) | |
2412 | *externalLeading = 0; | |
2413 | if ( width ) | |
2414 | *width = FixedToInt(textAfter - textBefore); | |
2415 | ||
2416 | ::ATSUDisposeTextLayout(atsuLayout); | |
2417 | ||
2418 | return; | |
2419 | } | |
2420 | #endif | |
2421 | #if wxOSX_USE_IPHONE | |
2422 | wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData(); | |
2423 | ||
2424 | wxCFStringRef text(str, wxLocale::GetSystemEncoding() ); | |
2425 | CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() ); | |
2426 | ||
2427 | if ( height ) | |
2428 | *height = sz.height; | |
2429 | /* | |
2430 | if ( descent ) | |
2431 | *descent = FixedToInt(textDescent); | |
2432 | if ( externalLeading ) | |
2433 | *externalLeading = 0; | |
2434 | */ | |
2435 | if ( width ) | |
2436 | *width = sz.width; | |
2437 | #endif | |
2438 | } | |
2439 | ||
2440 | void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const | |
2441 | { | |
2442 | widths.Empty(); | |
2443 | widths.Add(0, text.length()); | |
2444 | ||
2445 | wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") ); | |
2446 | ||
2447 | if (text.empty()) | |
2448 | return; | |
2449 | ||
2450 | #if wxOSX_USE_CORE_TEXT | |
2451 | { | |
2452 | wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData(); | |
2453 | CTFontRef font = fref->OSXGetCTFont(); | |
2454 | ||
2455 | wxCFStringRef t(text, wxLocale::GetSystemEncoding() ); | |
2456 | CFStringRef keys[] = { kCTFontAttributeName }; | |
2457 | CFTypeRef values[] = { font }; | |
2458 | wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values, | |
2459 | WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) ); | |
2460 | wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) ); | |
2461 | wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) ); | |
2462 | ||
2463 | int chars = text.length(); | |
2464 | for ( int pos = 0; pos < (int)chars; pos ++ ) | |
2465 | { | |
2466 | widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL )+0.5; | |
2467 | } | |
2468 | ||
2469 | return; | |
2470 | } | |
2471 | #endif | |
2472 | #if wxOSX_USE_ATSU_TEXT | |
2473 | { | |
2474 | OSStatus status = noErr; | |
2475 | ATSUTextLayout atsuLayout; | |
2476 | wxMacUniCharBuffer unibuf( text ); | |
2477 | UniCharCount chars = unibuf.GetChars(); | |
2478 | ||
2479 | ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle()); | |
2480 | status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 , | |
2481 | &chars , &style , &atsuLayout ); | |
2482 | ||
2483 | wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") ); | |
2484 | ||
2485 | status = ::ATSUSetTransientFontMatching( atsuLayout , true ); | |
2486 | wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") ); | |
2487 | ||
2488 | // new implementation from JS, keep old one just in case | |
2489 | #if 0 | |
2490 | for ( int pos = 0; pos < (int)chars; pos ++ ) | |
2491 | { | |
2492 | unsigned long actualNumberOfBounds = 0; | |
2493 | ATSTrapezoid glyphBounds; | |
2494 | ||
2495 | // We get a single bound, since the text should only require one. If it requires more, there is an issue | |
2496 | OSStatus result; | |
2497 | result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1, | |
2498 | kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds ); | |
2499 | if (result != noErr || actualNumberOfBounds != 1 ) | |
2500 | return; | |
2501 | ||
2502 | widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x ); | |
2503 | //unsigned char uch = s[i]; | |
2504 | } | |
2505 | #else | |
2506 | ATSLayoutRecord *layoutRecords = NULL; | |
2507 | ItemCount glyphCount = 0; | |
2508 | ||
2509 | // Get the glyph extents | |
2510 | OSStatus err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout, | |
2511 | 0, | |
2512 | kATSUDirectDataLayoutRecordATSLayoutRecordCurrent, | |
2513 | (void **) | |
2514 | &layoutRecords, | |
2515 | &glyphCount); | |
2516 | wxASSERT(glyphCount == (text.length()+1)); | |
2517 | ||
2518 | if ( err == noErr && glyphCount == (text.length()+1)) | |
2519 | { | |
2520 | for ( int pos = 1; pos < (int)glyphCount ; pos ++ ) | |
2521 | { | |
2522 | widths[pos-1] = FixedToInt( layoutRecords[pos].realPos ); | |
2523 | } | |
2524 | } | |
2525 | ||
2526 | ::ATSUDirectReleaseLayoutDataArrayPtr(NULL, | |
2527 | kATSUDirectDataLayoutRecordATSLayoutRecordCurrent, | |
2528 | (void **) &layoutRecords); | |
2529 | #endif | |
2530 | ::ATSUDisposeTextLayout(atsuLayout); | |
2531 | } | |
2532 | #endif | |
2533 | #if wxOSX_USE_IPHONE | |
2534 | // TODO core graphics text implementation here | |
2535 | #endif | |
2536 | } | |
2537 | ||
2538 | void * wxMacCoreGraphicsContext::GetNativeContext() | |
2539 | { | |
2540 | return m_cgContext; | |
2541 | } | |
2542 | ||
2543 | // concatenates this transform with the current transform of this context | |
2544 | void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix ) | |
2545 | { | |
2546 | if ( m_cgContext ) | |
2547 | CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix()); | |
2548 | else | |
2549 | m_windowTransform = CGAffineTransformConcat(m_windowTransform, *(CGAffineTransform*) matrix.GetNativeMatrix()); | |
2550 | } | |
2551 | ||
2552 | // sets the transform of this context | |
2553 | void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix ) | |
2554 | { | |
2555 | if ( m_cgContext ) | |
2556 | { | |
2557 | CGAffineTransform transform = CGContextGetCTM( m_cgContext ); | |
2558 | transform = CGAffineTransformInvert( transform ) ; | |
2559 | CGContextConcatCTM( m_cgContext, transform); | |
2560 | CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix()); | |
2561 | } | |
2562 | else | |
2563 | { | |
2564 | m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix(); | |
2565 | } | |
2566 | } | |
2567 | ||
2568 | // gets the matrix of this context | |
2569 | wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const | |
2570 | { | |
2571 | wxGraphicsMatrix m = CreateMatrix(); | |
2572 | *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform : | |
2573 | CGContextGetCTM( m_cgContext )); | |
2574 | return m; | |
2575 | } | |
2576 | ||
2577 | // | |
2578 | // Renderer | |
2579 | // | |
2580 | ||
2581 | //----------------------------------------------------------------------------- | |
2582 | // wxMacCoreGraphicsRenderer declaration | |
2583 | //----------------------------------------------------------------------------- | |
2584 | ||
2585 | class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer | |
2586 | { | |
2587 | public : | |
2588 | wxMacCoreGraphicsRenderer() {} | |
2589 | ||
2590 | virtual ~wxMacCoreGraphicsRenderer() {} | |
2591 | ||
2592 | // Context | |
2593 | ||
2594 | virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc); | |
2595 | virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc); | |
2596 | #if wxUSE_PRINTING_ARCHITECTURE | |
2597 | virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc); | |
2598 | #endif | |
2599 | ||
2600 | virtual wxGraphicsContext * CreateContextFromNativeContext( void * context ); | |
2601 | ||
2602 | virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ); | |
2603 | ||
2604 | virtual wxGraphicsContext * CreateContext( wxWindow* window ); | |
2605 | ||
2606 | virtual wxGraphicsContext * CreateMeasuringContext(); | |
2607 | ||
2608 | // Path | |
2609 | ||
2610 | virtual wxGraphicsPath CreatePath(); | |
2611 | ||
2612 | // Matrix | |
2613 | ||
2614 | virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, | |
2615 | wxDouble tx=0.0, wxDouble ty=0.0); | |
2616 | ||
2617 | ||
2618 | virtual wxGraphicsPen CreatePen(const wxPen& pen) ; | |
2619 | ||
2620 | virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ; | |
2621 | ||
2622 | // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2 | |
2623 | virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, | |
2624 | const wxColour&c1, const wxColour&c2) ; | |
2625 | ||
2626 | // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) | |
2627 | // with radius r and color cColor | |
2628 | virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
2629 | const wxColour &oColor, const wxColour &cColor) ; | |
2630 | ||
2631 | // sets the font | |
2632 | virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ; | |
2633 | ||
2634 | // create a native bitmap representation | |
2635 | virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ; | |
2636 | ||
2637 | // create a native bitmap representation | |
2638 | virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ; | |
2639 | private : | |
2640 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer) | |
2641 | } ; | |
2642 | ||
2643 | //----------------------------------------------------------------------------- | |
2644 | // wxMacCoreGraphicsRenderer implementation | |
2645 | //----------------------------------------------------------------------------- | |
2646 | ||
2647 | IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer) | |
2648 | ||
2649 | static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer; | |
2650 | ||
2651 | wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer() | |
2652 | { | |
2653 | return &gs_MacCoreGraphicsRenderer; | |
2654 | } | |
2655 | ||
2656 | wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc ) | |
2657 | { | |
2658 | const wxDCImpl* impl = dc.GetImpl(); | |
2659 | wxWindowDCImpl *win_impl = wxDynamicCast( impl, wxWindowDCImpl ); | |
2660 | if (win_impl) | |
2661 | { | |
2662 | int w, h; | |
2663 | win_impl->GetSize( &w, &h ); | |
2664 | CGContextRef cgctx = 0; | |
2665 | ||
2666 | wxASSERT_MSG(win_impl->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext"); | |
2667 | if (win_impl->GetWindow()) | |
2668 | cgctx = (CGContextRef)(win_impl->GetWindow()->MacGetCGContextRef()); | |
2669 | ||
2670 | if (cgctx != 0) | |
2671 | return new wxMacCoreGraphicsContext( this, cgctx, (wxDouble) w, (wxDouble) h ); | |
2672 | } | |
2673 | return NULL; | |
2674 | } | |
2675 | ||
2676 | wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC& dc ) | |
2677 | { | |
2678 | #ifdef __WXMAC__ | |
2679 | const wxDCImpl* impl = dc.GetImpl(); | |
2680 | wxMemoryDCImpl *mem_impl = wxDynamicCast( impl, wxMemoryDCImpl ); | |
2681 | if (mem_impl) | |
2682 | { | |
2683 | int w, h; | |
2684 | mem_impl->GetSize( &w, &h ); | |
2685 | return new wxMacCoreGraphicsContext( this, | |
2686 | (CGContextRef)(mem_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h ); | |
2687 | } | |
2688 | #endif | |
2689 | return NULL; | |
2690 | } | |
2691 | ||
2692 | #if wxUSE_PRINTING_ARCHITECTURE | |
2693 | wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC& dc ) | |
2694 | { | |
2695 | #ifdef __WXMAC__ | |
2696 | const wxDCImpl* impl = dc.GetImpl(); | |
2697 | wxPrinterDCImpl *print_impl = wxDynamicCast( impl, wxPrinterDCImpl ); | |
2698 | if (print_impl) | |
2699 | { | |
2700 | int w, h; | |
2701 | print_impl->GetSize( &w, &h ); | |
2702 | return new wxMacCoreGraphicsContext( this, | |
2703 | (CGContextRef)(print_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h ); | |
2704 | } | |
2705 | #endif | |
2706 | return NULL; | |
2707 | } | |
2708 | #endif | |
2709 | ||
2710 | wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context ) | |
2711 | { | |
2712 | return new wxMacCoreGraphicsContext(this,(CGContextRef)context); | |
2713 | } | |
2714 | ||
2715 | wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window ) | |
2716 | { | |
2717 | #if wxOSX_USE_CARBON | |
2718 | return new wxMacCoreGraphicsContext(this,(WindowRef)window); | |
2719 | #else | |
2720 | wxUnusedVar(window); | |
2721 | return NULL; | |
2722 | #endif | |
2723 | } | |
2724 | ||
2725 | wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window ) | |
2726 | { | |
2727 | return new wxMacCoreGraphicsContext(this, window ); | |
2728 | } | |
2729 | ||
2730 | wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext() | |
2731 | { | |
2732 | return new wxMacCoreGraphicsContext(this); | |
2733 | } | |
2734 | ||
2735 | // Path | |
2736 | ||
2737 | wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath() | |
2738 | { | |
2739 | wxGraphicsPath m; | |
2740 | m.SetRefData( new wxMacCoreGraphicsPathData(this)); | |
2741 | return m; | |
2742 | } | |
2743 | ||
2744 | ||
2745 | // Matrix | |
2746 | ||
2747 | wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d, | |
2748 | wxDouble tx, wxDouble ty) | |
2749 | { | |
2750 | wxGraphicsMatrix m; | |
2751 | wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this ); | |
2752 | data->Set( a,b,c,d,tx,ty ) ; | |
2753 | m.SetRefData(data); | |
2754 | return m; | |
2755 | } | |
2756 | ||
2757 | wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen) | |
2758 | { | |
2759 | if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT ) | |
2760 | return wxNullGraphicsPen; | |
2761 | else | |
2762 | { | |
2763 | wxGraphicsPen p; | |
2764 | p.SetRefData(new wxMacCoreGraphicsPenData( this, pen )); | |
2765 | return p; | |
2766 | } | |
2767 | } | |
2768 | ||
2769 | wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush ) | |
2770 | { | |
2771 | if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT ) | |
2772 | return wxNullGraphicsBrush; | |
2773 | else | |
2774 | { | |
2775 | wxGraphicsBrush p; | |
2776 | p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush )); | |
2777 | return p; | |
2778 | } | |
2779 | } | |
2780 | ||
2781 | wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap& bmp ) | |
2782 | { | |
2783 | if ( bmp.Ok() ) | |
2784 | { | |
2785 | wxGraphicsBitmap p; | |
2786 | #ifdef __WXMAC__ | |
2787 | p.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp.CreateCGImage(), bmp.GetDepth() == 1 ) ); | |
2788 | #endif | |
2789 | return p; | |
2790 | } | |
2791 | else | |
2792 | return wxNullGraphicsBitmap; | |
2793 | } | |
2794 | ||
2795 | wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
2796 | { | |
2797 | wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData()); | |
2798 | CGImageRef img = refdata->GetBitmap(); | |
2799 | if ( img ) | |
2800 | { | |
2801 | wxGraphicsBitmap p; | |
2802 | CGImageRef subimg = CGImageCreateWithImageInRect(img,CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h )); | |
2803 | p.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg, refdata->IsMonochrome() ) ); | |
2804 | return p; | |
2805 | } | |
2806 | else | |
2807 | return wxNullGraphicsBitmap; | |
2808 | } | |
2809 | ||
2810 | // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2 | |
2811 | wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, | |
2812 | const wxColour&c1, const wxColour&c2) | |
2813 | { | |
2814 | wxGraphicsBrush p; | |
2815 | wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this ); | |
2816 | d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2); | |
2817 | p.SetRefData(d); | |
2818 | return p; | |
2819 | } | |
2820 | ||
2821 | // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) | |
2822 | // with radius r and color cColor | |
2823 | wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
2824 | const wxColour &oColor, const wxColour &cColor) | |
2825 | { | |
2826 | wxGraphicsBrush p; | |
2827 | wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this ); | |
2828 | d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor); | |
2829 | p.SetRefData(d); | |
2830 | return p; | |
2831 | } | |
2832 | ||
2833 | // sets the font | |
2834 | wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col ) | |
2835 | { | |
2836 | if ( font.Ok() ) | |
2837 | { | |
2838 | wxGraphicsFont p; | |
2839 | p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col )); | |
2840 | return p; | |
2841 | } | |
2842 | else | |
2843 | return wxNullGraphicsFont; | |
2844 | } | |
2845 | ||
2846 | // | |
2847 | // CoreGraphics Helper Methods | |
2848 | // | |
2849 | ||
2850 | // Data Providers and Consumers | |
2851 | ||
2852 | size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count ) | |
2853 | { | |
2854 | CFMutableDataRef data = (CFMutableDataRef) info; | |
2855 | if ( data ) | |
2856 | { | |
2857 | CFDataAppendBytes( data, (const UInt8*) bytes, count ); | |
2858 | } | |
2859 | return count; | |
2860 | } | |
2861 | ||
2862 | void wxMacReleaseCFDataProviderCallback(void *info, | |
2863 | const void *WXUNUSED(data), | |
2864 | size_t WXUNUSED(count)) | |
2865 | { | |
2866 | if ( info ) | |
2867 | CFRelease( (CFDataRef) info ); | |
2868 | } | |
2869 | ||
2870 | void wxMacReleaseCFDataConsumerCallback( void *info ) | |
2871 | { | |
2872 | if ( info ) | |
2873 | CFRelease( (CFDataRef) info ); | |
2874 | } | |
2875 | ||
2876 | CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data ) | |
2877 | { | |
2878 | if ( data == NULL ) | |
2879 | return NULL; | |
2880 | ||
2881 | return CGDataProviderCreateWithCFData( data ); | |
2882 | } | |
2883 | ||
2884 | CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data ) | |
2885 | { | |
2886 | if ( data == NULL ) | |
2887 | return NULL; | |
2888 | ||
2889 | return CGDataConsumerCreateWithCFData( data ); | |
2890 | } | |
2891 | ||
2892 | void | |
2893 | wxMacReleaseMemoryBufferProviderCallback(void *info, | |
2894 | const void * WXUNUSED_UNLESS_DEBUG(data), | |
2895 | size_t WXUNUSED(size)) | |
2896 | { | |
2897 | wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ; | |
2898 | ||
2899 | wxASSERT( data == membuf->GetData() ) ; | |
2900 | ||
2901 | delete membuf ; | |
2902 | } | |
2903 | ||
2904 | CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf ) | |
2905 | { | |
2906 | wxMemoryBuffer* b = new wxMemoryBuffer( buf ); | |
2907 | if ( b->GetDataLen() == 0 ) | |
2908 | return NULL; | |
2909 | ||
2910 | return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() , | |
2911 | wxMacReleaseMemoryBufferProviderCallback ); | |
2912 | } |