]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/graphics.cpp
5389a37e141a0f0addaa8494816ca829faa53e90
[wxWidgets.git] / src / osx / carbon / graphics.cpp
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[WXSIZEOF(atsuTags)] =
873 {
874 sizeof( Fixed ) ,
875 sizeof( RGBColor ) ,
876 };
877 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
878 {
879 &atsuSize ,
880 &atsuColor ,
881 };
882
883 status = ::ATSUSetAttributes(
884 m_macATSUIStyle, WXSIZEOF(atsuTags),
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 &region );
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, wxT("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 CGContextSaveGState( m_cgContext );
1629 CGContextConcatCTM( m_cgContext, m_windowTransform );
1630 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
1631 m_contextSynthesized = true;
1632 #if wxOSX_USE_COCOA_OR_CARBON
1633 if ( m_clipRgn.get() )
1634 {
1635 // the clip region is in device coordinates, so we convert this again to user coordinates
1636 wxCFRef<HIMutableShapeRef> hishape( HIShapeCreateMutableCopy( m_clipRgn ) );
1637 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero,m_windowTransform);
1638 HIShapeOffset( hishape, -transformedOrigin.x, -transformedOrigin.y );
1639 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1640 if ( HIShapeIsEmpty(hishape))
1641 {
1642 CGRect empty = CGRectMake( 0,0,0,0 );
1643 CGContextClipToRect( m_cgContext, empty );
1644 }
1645 else
1646 {
1647 HIShapeReplacePathInCGContext( hishape, m_cgContext );
1648 CGContextClip( m_cgContext );
1649 }
1650 }
1651 #endif
1652 CGContextSaveGState( m_cgContext );
1653
1654 #if 0 // turn on for debugging of clientdc
1655 static float color = 0.5 ;
1656 static int channel = 0 ;
1657 CGRect bounds = CGRectMake(-1000,-1000,2000,2000);
1658 CGContextSetRGBFillColor( m_cgContext, channel == 0 ? color : 0.5 ,
1659 channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 );
1660 CGContextFillRect( m_cgContext, bounds );
1661 color += 0.1 ;
1662 if ( color > 0.9 )
1663 {
1664 color = 0.5 ;
1665 channel++ ;
1666 if ( channel == 3 )
1667 channel = 0 ;
1668 }
1669 #endif
1670 }
1671 }
1672 return m_cgContext != NULL;
1673 }
1674
1675 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias)
1676 {
1677 if (EnsureIsValid()==false)
1678 return true;
1679
1680 if (m_antialias == antialias)
1681 return true;
1682
1683 m_antialias = antialias;
1684
1685 bool antialiasMode;
1686 switch (antialias)
1687 {
1688 case wxANTIALIAS_DEFAULT:
1689 antialiasMode = true;
1690 break;
1691 case wxANTIALIAS_NONE:
1692 antialiasMode = false;
1693 break;
1694 default:
1695 return false;
1696 }
1697 CGContextSetShouldAntialias(m_cgContext, antialiasMode);
1698 return true;
1699 }
1700
1701 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op)
1702 {
1703 if (EnsureIsValid()==false)
1704 return true;
1705
1706 if ( m_composition == op )
1707 return true;
1708
1709 m_composition = op;
1710
1711 if (m_composition == wxCOMPOSITION_DEST)
1712 return true;
1713
1714 #if wxOSX_USE_COCOA_OR_CARBON
1715 if ( UMAGetSystemVersion() < 0x1060 )
1716 {
1717 CGCompositeOperation cop = kCGCompositeOperationSourceOver;
1718 CGBlendMode mode = kCGBlendModeNormal;
1719 switch( op )
1720 {
1721 case wxCOMPOSITION_CLEAR:
1722 cop = kCGCompositeOperationClear;
1723 break;
1724 case wxCOMPOSITION_SOURCE:
1725 cop = kCGCompositeOperationCopy;
1726 break;
1727 case wxCOMPOSITION_OVER:
1728 mode = kCGBlendModeNormal;
1729 break;
1730 case wxCOMPOSITION_IN:
1731 cop = kCGCompositeOperationSourceIn;
1732 break;
1733 case wxCOMPOSITION_OUT:
1734 cop = kCGCompositeOperationSourceOut;
1735 break;
1736 case wxCOMPOSITION_ATOP:
1737 cop = kCGCompositeOperationSourceAtop;
1738 break;
1739 case wxCOMPOSITION_DEST_OVER:
1740 cop = kCGCompositeOperationDestinationOver;
1741 break;
1742 case wxCOMPOSITION_DEST_IN:
1743 cop = kCGCompositeOperationDestinationIn;
1744 break;
1745 case wxCOMPOSITION_DEST_OUT:
1746 cop = kCGCompositeOperationDestinationOut;
1747 break;
1748 case wxCOMPOSITION_DEST_ATOP:
1749 cop = kCGCompositeOperationDestinationAtop;
1750 break;
1751 case wxCOMPOSITION_XOR:
1752 cop = kCGCompositeOperationXOR;
1753 break;
1754 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1755 case wxCOMPOSITION_ADD:
1756 mode = kCGBlendModePlusLighter ;
1757 break;
1758 #endif
1759 default:
1760 return false;
1761 }
1762 if ( cop != kCGCompositeOperationSourceOver )
1763 CGContextSetCompositeOperation(m_cgContext, cop);
1764 else
1765 CGContextSetBlendMode(m_cgContext, mode);
1766 }
1767 #endif
1768 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1769 else
1770 {
1771 CGBlendMode mode = kCGBlendModeNormal;
1772 switch( op )
1773 {
1774 case wxCOMPOSITION_CLEAR:
1775 mode = kCGBlendModeClear;
1776 break;
1777 case wxCOMPOSITION_SOURCE:
1778 mode = kCGBlendModeCopy;
1779 break;
1780 case wxCOMPOSITION_OVER:
1781 mode = kCGBlendModeNormal;
1782 break;
1783 case wxCOMPOSITION_IN:
1784 mode = kCGBlendModeSourceIn;
1785 break;
1786 case wxCOMPOSITION_OUT:
1787 mode = kCGBlendModeSourceOut;
1788 break;
1789 case wxCOMPOSITION_ATOP:
1790 mode = kCGBlendModeSourceAtop;
1791 break;
1792 case wxCOMPOSITION_DEST_OVER:
1793 mode = kCGBlendModeDestinationOver;
1794 break;
1795 case wxCOMPOSITION_DEST_IN:
1796 mode = kCGBlendModeDestinationIn;
1797 break;
1798 case wxCOMPOSITION_DEST_OUT:
1799 mode = kCGBlendModeDestinationOut;
1800 break;
1801 case wxCOMPOSITION_DEST_ATOP:
1802 mode = kCGBlendModeDestinationAtop;
1803 break;
1804 case wxCOMPOSITION_XOR:
1805 mode = kCGBlendModeXOR;
1806 break;
1807
1808 case wxCOMPOSITION_ADD:
1809 mode = kCGBlendModePlusLighter ;
1810 break;
1811 default:
1812 return false;
1813 }
1814 CGContextSetBlendMode(m_cgContext, mode);
1815 }
1816 #endif
1817 return true;
1818 }
1819
1820 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity)
1821 {
1822 CGContextSaveGState(m_cgContext);
1823 CGContextSetAlpha(m_cgContext, (CGFloat) opacity);
1824 CGContextBeginTransparencyLayer(m_cgContext, 0);
1825 }
1826
1827 void wxMacCoreGraphicsContext::EndLayer()
1828 {
1829 CGContextEndTransparencyLayer(m_cgContext);
1830 CGContextRestoreGState(m_cgContext);
1831 }
1832
1833 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1834 {
1835 #if wxOSX_USE_COCOA_OR_CARBON
1836 if( m_cgContext )
1837 {
1838 wxCFRef<HIShapeRef> shape = wxCFRefFromGet(region.GetWXHRGN());
1839 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1840 if ( HIShapeIsEmpty(shape))
1841 {
1842 CGRect empty = CGRectMake( 0,0,0,0 );
1843 CGContextClipToRect( m_cgContext, empty );
1844 }
1845 else
1846 {
1847 HIShapeReplacePathInCGContext( shape, m_cgContext );
1848 CGContextClip( m_cgContext );
1849 }
1850 }
1851 else
1852 {
1853 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1854 // to regions we try at least to have correct translations
1855 HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( region.GetWXHRGN() );
1856
1857 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform );
1858 HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y );
1859 m_clipRgn.reset(mutableShape);
1860 }
1861 #else
1862 // allow usage as measuring context
1863 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1864 #endif
1865 }
1866
1867 // clips drawings to the rect
1868 void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1869 {
1870 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
1871 if ( m_cgContext )
1872 {
1873 CGContextClipToRect( m_cgContext, r );
1874 }
1875 else
1876 {
1877 #if wxOSX_USE_COCOA_OR_CARBON
1878 // the clipping itself must be stored as device coordinates, otherwise
1879 // we cannot apply it back correctly
1880 r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform );
1881 m_clipRgn.reset(HIShapeCreateWithRect(&r));
1882 #else
1883 // allow usage as measuring context
1884 // wxFAIL_MSG( "Needs a valid context for clipping" );
1885 #endif
1886 }
1887 }
1888
1889 // resets the clipping to original extent
1890 void wxMacCoreGraphicsContext::ResetClip()
1891 {
1892 if ( m_cgContext )
1893 {
1894 // there is no way for clearing the clip, we can only revert to the stored
1895 // state, but then we have to make sure everything else is NOT restored
1896 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
1897 CGContextRestoreGState( m_cgContext );
1898 CGContextSaveGState( m_cgContext );
1899 CGAffineTransform transformNew = CGContextGetCTM( m_cgContext );
1900 transformNew = CGAffineTransformInvert( transformNew ) ;
1901 CGContextConcatCTM( m_cgContext, transformNew);
1902 CGContextConcatCTM( m_cgContext, transform);
1903 }
1904 else
1905 {
1906 #if wxOSX_USE_COCOA_OR_CARBON
1907 m_clipRgn.reset();
1908 #else
1909 // allow usage as measuring context
1910 // wxFAIL_MSG( "Needs a valid context for clipping" );
1911 #endif
1912 }
1913 }
1914
1915 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
1916 {
1917 if ( m_pen.IsNull() )
1918 return ;
1919
1920 if (EnsureIsValid()==false)
1921 return;
1922
1923 if (m_composition == wxCOMPOSITION_DEST)
1924 return;
1925
1926 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1927
1928 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1929 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1930 CGContextStrokePath( m_cgContext );
1931 }
1932
1933 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
1934 {
1935 if (EnsureIsValid()==false)
1936 return;
1937
1938 if (m_composition == wxCOMPOSITION_DEST)
1939 return;
1940
1941 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1942 {
1943 // when using shading, we cannot draw pen and brush at the same time
1944 // revert to the base implementation of first filling and then stroking
1945 wxGraphicsContext::DrawPath( path, fillStyle );
1946 return;
1947 }
1948
1949 CGPathDrawingMode mode = kCGPathFill ;
1950 if ( m_brush.IsNull() )
1951 {
1952 if ( m_pen.IsNull() )
1953 return;
1954 else
1955 mode = kCGPathStroke;
1956 }
1957 else
1958 {
1959 if ( m_pen.IsNull() )
1960 {
1961 if ( fillStyle == wxODDEVEN_RULE )
1962 mode = kCGPathEOFill;
1963 else
1964 mode = kCGPathFill;
1965 }
1966 else
1967 {
1968 if ( fillStyle == wxODDEVEN_RULE )
1969 mode = kCGPathEOFillStroke;
1970 else
1971 mode = kCGPathFillStroke;
1972 }
1973 }
1974
1975 if ( !m_brush.IsNull() )
1976 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1977 if ( !m_pen.IsNull() )
1978 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1979
1980 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1981
1982 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1983 CGContextDrawPath( m_cgContext , mode );
1984 }
1985
1986 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
1987 {
1988 if ( m_brush.IsNull() )
1989 return;
1990
1991 if (EnsureIsValid()==false)
1992 return;
1993
1994 if (m_composition == wxCOMPOSITION_DEST)
1995 return;
1996
1997 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1998 {
1999 CGContextSaveGState( m_cgContext );
2000 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2001 CGContextClip( m_cgContext );
2002 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2003 CGContextRestoreGState( m_cgContext);
2004 }
2005 else
2006 {
2007 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2008 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2009 if ( fillStyle == wxODDEVEN_RULE )
2010 CGContextEOFillPath( m_cgContext );
2011 else
2012 CGContextFillPath( m_cgContext );
2013 }
2014 }
2015
2016 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
2017 {
2018 // we allow either setting or clearing but not replacing
2019 wxASSERT( m_cgContext == NULL || cg == NULL );
2020
2021 if ( m_cgContext )
2022 {
2023 CGContextRestoreGState( m_cgContext );
2024 CGContextRestoreGState( m_cgContext );
2025 if ( m_contextSynthesized )
2026 {
2027 // TODO: in case of performance problems, try issuing this not too
2028 // frequently (half of refresh rate)
2029 CGContextFlush(m_cgContext);
2030 #if wxOSX_USE_CARBON
2031 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
2032 #endif
2033 #if wxOSX_USE_COCOA
2034 wxOSXUnlockFocus(m_view);
2035 #endif
2036 }
2037 else
2038 CGContextRelease(m_cgContext);
2039 }
2040
2041 m_cgContext = cg;
2042
2043 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2044 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2045 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2046 // for this one operation.
2047
2048 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2049 // can be removed.
2050 if (m_cgContext)
2051 {
2052 CGContextRetain(m_cgContext);
2053 CGContextSaveGState( m_cgContext );
2054 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
2055 CGContextSaveGState( m_cgContext );
2056 m_contextSynthesized = false;
2057 }
2058 }
2059
2060 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
2061 {
2062 if ( m_cgContext )
2063 CGContextTranslateCTM( m_cgContext, (CGFloat) dx, (CGFloat) dy );
2064 else
2065 m_windowTransform = CGAffineTransformTranslate(m_windowTransform, (CGFloat) dx, (CGFloat) dy);
2066 }
2067
2068 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
2069 {
2070 if ( m_cgContext )
2071 CGContextScaleCTM( m_cgContext , (CGFloat) xScale , (CGFloat) yScale );
2072 else
2073 m_windowTransform = CGAffineTransformScale(m_windowTransform, (CGFloat) xScale, (CGFloat) yScale);
2074 }
2075
2076 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
2077 {
2078 if ( m_cgContext )
2079 CGContextRotateCTM( m_cgContext , (CGFloat) angle );
2080 else
2081 m_windowTransform = CGAffineTransformRotate(m_windowTransform, (CGFloat) angle);
2082 }
2083
2084 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2085 {
2086 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
2087 DrawBitmap(bitmap, x, y, w, h);
2088 }
2089
2090 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2091 {
2092 if (EnsureIsValid()==false)
2093 return;
2094
2095 if (m_composition == wxCOMPOSITION_DEST)
2096 return;
2097
2098 #ifdef __WXMAC__
2099 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2100 CGImageRef image = refdata->GetBitmap();
2101 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2102 if ( refdata->IsMonochrome() == 1 )
2103 {
2104 // is is a mask, the '1' in the mask tell where to draw the current brush
2105 if ( !m_brush.IsNull() )
2106 {
2107 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2108 {
2109 // TODO clip to mask
2110 /*
2111 CGContextSaveGState( m_cgContext );
2112 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2113 CGContextClip( m_cgContext );
2114 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2115 CGContextRestoreGState( m_cgContext);
2116 */
2117 }
2118 else
2119 {
2120 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2121 wxMacDrawCGImage( m_cgContext , &r , image );
2122 }
2123 }
2124 }
2125 else
2126 {
2127 wxMacDrawCGImage( m_cgContext , &r , image );
2128 }
2129 #endif
2130 }
2131
2132 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2133 {
2134 if (EnsureIsValid()==false)
2135 return;
2136
2137 if (m_composition == wxCOMPOSITION_DEST)
2138 return;
2139
2140 CGRect r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) w , (CGFloat) h );
2141 CGContextSaveGState( m_cgContext );
2142 CGContextTranslateCTM( m_cgContext,(CGFloat) x ,(CGFloat) (y + h) );
2143 CGContextScaleCTM( m_cgContext, 1, -1 );
2144 #if wxOSX_USE_COCOA_OR_CARBON
2145 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
2146 NULL , kPlotIconRefNormalFlags , icon.GetHICON() );
2147 #endif
2148 CGContextRestoreGState( m_cgContext );
2149 }
2150
2151 void wxMacCoreGraphicsContext::PushState()
2152 {
2153 if (EnsureIsValid()==false)
2154 return;
2155
2156 CGContextSaveGState( m_cgContext );
2157 }
2158
2159 void wxMacCoreGraphicsContext::PopState()
2160 {
2161 if (EnsureIsValid()==false)
2162 return;
2163
2164 CGContextRestoreGState( m_cgContext );
2165 }
2166
2167 void wxMacCoreGraphicsContext::DoDrawText( const wxString &str, wxDouble x, wxDouble y )
2168 {
2169 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2170
2171 if (EnsureIsValid()==false)
2172 return;
2173
2174 if (m_composition == wxCOMPOSITION_DEST)
2175 return;
2176
2177 #if wxOSX_USE_CORE_TEXT
2178 if ( UMAGetSystemVersion() >= 0x1050 )
2179 {
2180 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2181 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2182 CTFontRef font = fref->OSXGetCTFont();
2183 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2184 CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ;
2185 wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) );
2186 CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName };
2187 CFTypeRef values[] = { font, col, underlined };
2188 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2189 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2190 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2191 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2192
2193 y += CTFontGetAscent(font);
2194
2195 CGContextSaveGState(m_cgContext);
2196 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2197 CGContextScaleCTM(m_cgContext, 1, -1);
2198 CGContextSetTextPosition(m_cgContext, 0, 0);
2199 CTLineDraw( line, m_cgContext );
2200 CGContextRestoreGState(m_cgContext);
2201 CFRelease( col );
2202 return;
2203 }
2204 #endif
2205 #if wxOSX_USE_ATSU_TEXT
2206 {
2207 DrawText(str, x, y, 0.0);
2208 return;
2209 }
2210 #endif
2211 #if wxOSX_USE_IPHONE
2212 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2213
2214 CGContextSaveGState(m_cgContext);
2215
2216 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2217 CGContextSetTextDrawingMode (m_cgContext, kCGTextFill);
2218 CGContextSetFillColorWithColor( m_cgContext, col );
2219
2220 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2221 DrawTextInContext( m_cgContext, CGPointMake( x, y ), fref->GetUIFont() , text.AsNSString() );
2222
2223 CGContextRestoreGState(m_cgContext);
2224 CFRelease( col );
2225 #endif
2226 }
2227
2228 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString &str,
2229 wxDouble x, wxDouble y,
2230 wxDouble angle)
2231 {
2232 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2233
2234 if (EnsureIsValid()==false)
2235 return;
2236
2237 if (m_composition == wxCOMPOSITION_DEST)
2238 return;
2239
2240 #if wxOSX_USE_CORE_TEXT
2241 if ( UMAGetSystemVersion() >= 0x1050 )
2242 {
2243 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2244 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
2245 return;
2246 }
2247 #endif
2248 #if wxOSX_USE_ATSU_TEXT
2249 {
2250 OSStatus status = noErr;
2251 ATSUTextLayout atsuLayout;
2252 wxMacUniCharBuffer unibuf( str );
2253 UniCharCount chars = unibuf.GetChars();
2254
2255 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2256 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2257 &chars , &style , &atsuLayout );
2258
2259 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
2260
2261 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2262 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2263
2264 int iAngle = int( angle * RAD2DEG );
2265 if ( abs(iAngle) > 0 )
2266 {
2267 Fixed atsuAngle = IntToFixed( iAngle );
2268 ATSUAttributeTag atsuTags[] =
2269 {
2270 kATSULineRotationTag ,
2271 };
2272 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
2273 {
2274 sizeof( Fixed ) ,
2275 };
2276 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
2277 {
2278 &atsuAngle ,
2279 };
2280 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
2281 atsuTags, atsuSizes, atsuValues );
2282 }
2283
2284 {
2285 ATSUAttributeTag atsuTags[] =
2286 {
2287 kATSUCGContextTag ,
2288 };
2289 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
2290 {
2291 sizeof( CGContextRef ) ,
2292 };
2293 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
2294 {
2295 &m_cgContext ,
2296 };
2297 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
2298 atsuTags, atsuSizes, atsuValues );
2299 }
2300
2301 ATSUTextMeasurement textBefore, textAfter;
2302 ATSUTextMeasurement ascent, descent;
2303
2304 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2305 &textBefore , &textAfter, &ascent , &descent );
2306
2307 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2308
2309 Rect rect;
2310 x += (int)(sin(angle) * FixedToInt(ascent));
2311 y += (int)(cos(angle) * FixedToInt(ascent));
2312
2313 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2314 IntToFixed(x) , IntToFixed(y) , &rect );
2315 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2316
2317 CGContextSaveGState(m_cgContext);
2318 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2319 CGContextScaleCTM(m_cgContext, 1, -1);
2320 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2321 IntToFixed(0) , IntToFixed(0) );
2322
2323 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
2324
2325 CGContextRestoreGState(m_cgContext);
2326
2327 ::ATSUDisposeTextLayout(atsuLayout);
2328
2329 return;
2330 }
2331 #endif
2332 #if wxOSX_USE_IPHONE
2333 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2334 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
2335 #endif
2336 }
2337
2338 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
2339 wxDouble *descent, wxDouble *externalLeading ) const
2340 {
2341 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2342
2343 if ( width )
2344 *width = 0;
2345 if ( height )
2346 *height = 0;
2347 if ( descent )
2348 *descent = 0;
2349 if ( externalLeading )
2350 *externalLeading = 0;
2351
2352 if (str.empty())
2353 return;
2354
2355 #if wxOSX_USE_CORE_TEXT
2356 if ( UMAGetSystemVersion() >= 0x1050 )
2357 {
2358 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2359 CTFontRef font = fref->OSXGetCTFont();
2360
2361 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2362 CFStringRef keys[] = { kCTFontAttributeName };
2363 CFTypeRef values[] = { font };
2364 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2365 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2366 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2367 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2368
2369 double w;
2370 CGFloat a, d, l;
2371
2372 w = CTLineGetTypographicBounds(line, &a, &d, &l) ;
2373
2374 if ( height )
2375 *height = a+d+l;
2376 if ( descent )
2377 *descent = d;
2378 if ( externalLeading )
2379 *externalLeading = l;
2380 if ( width )
2381 *width = w;
2382 return;
2383 }
2384 #endif
2385 #if wxOSX_USE_ATSU_TEXT
2386 {
2387 OSStatus status = noErr;
2388
2389 ATSUTextLayout atsuLayout;
2390 wxMacUniCharBuffer unibuf( str );
2391 UniCharCount chars = unibuf.GetChars();
2392
2393 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2394 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2395 &chars , &style , &atsuLayout );
2396
2397 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2398
2399 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2400 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2401
2402 ATSUTextMeasurement textBefore, textAfter;
2403 ATSUTextMeasurement textAscent, textDescent;
2404
2405 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2406 &textBefore , &textAfter, &textAscent , &textDescent );
2407
2408 if ( height )
2409 *height = FixedToInt(textAscent + textDescent);
2410 if ( descent )
2411 *descent = FixedToInt(textDescent);
2412 if ( externalLeading )
2413 *externalLeading = 0;
2414 if ( width )
2415 *width = FixedToInt(textAfter - textBefore);
2416
2417 ::ATSUDisposeTextLayout(atsuLayout);
2418
2419 return;
2420 }
2421 #endif
2422 #if wxOSX_USE_IPHONE
2423 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2424
2425 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2426 CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() );
2427
2428 if ( height )
2429 *height = sz.height;
2430 /*
2431 if ( descent )
2432 *descent = FixedToInt(textDescent);
2433 if ( externalLeading )
2434 *externalLeading = 0;
2435 */
2436 if ( width )
2437 *width = sz.width;
2438 #endif
2439 }
2440
2441 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
2442 {
2443 widths.Empty();
2444 widths.Add(0, text.length());
2445
2446 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2447
2448 if (text.empty())
2449 return;
2450
2451 #if wxOSX_USE_CORE_TEXT
2452 {
2453 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2454 CTFontRef font = fref->OSXGetCTFont();
2455
2456 wxCFStringRef t(text, wxLocale::GetSystemEncoding() );
2457 CFStringRef keys[] = { kCTFontAttributeName };
2458 CFTypeRef values[] = { font };
2459 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2460 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2461 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) );
2462 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2463
2464 int chars = text.length();
2465 for ( int pos = 0; pos < (int)chars; pos ++ )
2466 {
2467 widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL )+0.5;
2468 }
2469
2470 return;
2471 }
2472 #endif
2473 #if wxOSX_USE_ATSU_TEXT
2474 {
2475 OSStatus status = noErr;
2476 ATSUTextLayout atsuLayout;
2477 wxMacUniCharBuffer unibuf( text );
2478 UniCharCount chars = unibuf.GetChars();
2479
2480 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2481 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2482 &chars , &style , &atsuLayout );
2483
2484 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2485
2486 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2487 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2488
2489 // new implementation from JS, keep old one just in case
2490 #if 0
2491 for ( int pos = 0; pos < (int)chars; pos ++ )
2492 {
2493 unsigned long actualNumberOfBounds = 0;
2494 ATSTrapezoid glyphBounds;
2495
2496 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2497 OSStatus result;
2498 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2499 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2500 if (result != noErr || actualNumberOfBounds != 1 )
2501 return;
2502
2503 widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
2504 //unsigned char uch = s[i];
2505 }
2506 #else
2507 ATSLayoutRecord *layoutRecords = NULL;
2508 ItemCount glyphCount = 0;
2509
2510 // Get the glyph extents
2511 OSStatus err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout,
2512 0,
2513 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2514 (void **)
2515 &layoutRecords,
2516 &glyphCount);
2517 wxASSERT(glyphCount == (text.length()+1));
2518
2519 if ( err == noErr && glyphCount == (text.length()+1))
2520 {
2521 for ( int pos = 1; pos < (int)glyphCount ; pos ++ )
2522 {
2523 widths[pos-1] = FixedToInt( layoutRecords[pos].realPos );
2524 }
2525 }
2526
2527 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL,
2528 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2529 (void **) &layoutRecords);
2530 #endif
2531 ::ATSUDisposeTextLayout(atsuLayout);
2532 }
2533 #endif
2534 #if wxOSX_USE_IPHONE
2535 // TODO core graphics text implementation here
2536 #endif
2537 }
2538
2539 void * wxMacCoreGraphicsContext::GetNativeContext()
2540 {
2541 return m_cgContext;
2542 }
2543
2544 // concatenates this transform with the current transform of this context
2545 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
2546 {
2547 if ( m_cgContext )
2548 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2549 else
2550 m_windowTransform = CGAffineTransformConcat(m_windowTransform, *(CGAffineTransform*) matrix.GetNativeMatrix());
2551 }
2552
2553 // sets the transform of this context
2554 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
2555 {
2556 if ( m_cgContext )
2557 {
2558 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2559 transform = CGAffineTransformInvert( transform ) ;
2560 CGContextConcatCTM( m_cgContext, transform);
2561 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2562 }
2563 else
2564 {
2565 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
2566 }
2567 }
2568
2569 // gets the matrix of this context
2570 wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
2571 {
2572 wxGraphicsMatrix m = CreateMatrix();
2573 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
2574 CGContextGetCTM( m_cgContext ));
2575 return m;
2576 }
2577
2578 //
2579 // Renderer
2580 //
2581
2582 //-----------------------------------------------------------------------------
2583 // wxMacCoreGraphicsRenderer declaration
2584 //-----------------------------------------------------------------------------
2585
2586 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
2587 {
2588 public :
2589 wxMacCoreGraphicsRenderer() {}
2590
2591 virtual ~wxMacCoreGraphicsRenderer() {}
2592
2593 // Context
2594
2595 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2596 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
2597 #if wxUSE_PRINTING_ARCHITECTURE
2598 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
2599 #endif
2600
2601 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2602
2603 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2604
2605 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2606
2607 virtual wxGraphicsContext * CreateMeasuringContext();
2608
2609 // Path
2610
2611 virtual wxGraphicsPath CreatePath();
2612
2613 // Matrix
2614
2615 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2616 wxDouble tx=0.0, wxDouble ty=0.0);
2617
2618
2619 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2620
2621 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2622
2623 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2624 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2625 const wxColour&c1, const wxColour&c2) ;
2626
2627 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2628 // with radius r and color cColor
2629 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2630 const wxColour &oColor, const wxColour &cColor) ;
2631
2632 // sets the font
2633 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2634
2635 // create a native bitmap representation
2636 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ;
2637
2638 // create a native bitmap representation
2639 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
2640 private :
2641 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
2642 } ;
2643
2644 //-----------------------------------------------------------------------------
2645 // wxMacCoreGraphicsRenderer implementation
2646 //-----------------------------------------------------------------------------
2647
2648 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
2649
2650 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
2651
2652 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2653 {
2654 return &gs_MacCoreGraphicsRenderer;
2655 }
2656
2657 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc )
2658 {
2659 const wxDCImpl* impl = dc.GetImpl();
2660 wxWindowDCImpl *win_impl = wxDynamicCast( impl, wxWindowDCImpl );
2661 if (win_impl)
2662 {
2663 int w, h;
2664 win_impl->GetSize( &w, &h );
2665 CGContextRef cgctx = 0;
2666
2667 wxASSERT_MSG(win_impl->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2668 if (win_impl->GetWindow())
2669 cgctx = (CGContextRef)(win_impl->GetWindow()->MacGetCGContextRef());
2670
2671 if (cgctx != 0)
2672 return new wxMacCoreGraphicsContext( this, cgctx, (wxDouble) w, (wxDouble) h );
2673 }
2674 return NULL;
2675 }
2676
2677 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC& dc )
2678 {
2679 #ifdef __WXMAC__
2680 const wxDCImpl* impl = dc.GetImpl();
2681 wxMemoryDCImpl *mem_impl = wxDynamicCast( impl, wxMemoryDCImpl );
2682 if (mem_impl)
2683 {
2684 int w, h;
2685 mem_impl->GetSize( &w, &h );
2686 return new wxMacCoreGraphicsContext( this,
2687 (CGContextRef)(mem_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2688 }
2689 #endif
2690 return NULL;
2691 }
2692
2693 #if wxUSE_PRINTING_ARCHITECTURE
2694 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC& dc )
2695 {
2696 #ifdef __WXMAC__
2697 const wxDCImpl* impl = dc.GetImpl();
2698 wxPrinterDCImpl *print_impl = wxDynamicCast( impl, wxPrinterDCImpl );
2699 if (print_impl)
2700 {
2701 int w, h;
2702 print_impl->GetSize( &w, &h );
2703 return new wxMacCoreGraphicsContext( this,
2704 (CGContextRef)(print_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2705 }
2706 #endif
2707 return NULL;
2708 }
2709 #endif
2710
2711 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
2712 {
2713 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
2714 }
2715
2716 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
2717 {
2718 #if wxOSX_USE_CARBON
2719 return new wxMacCoreGraphicsContext(this,(WindowRef)window);
2720 #else
2721 wxUnusedVar(window);
2722 return NULL;
2723 #endif
2724 }
2725
2726 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
2727 {
2728 return new wxMacCoreGraphicsContext(this, window );
2729 }
2730
2731 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2732 {
2733 return new wxMacCoreGraphicsContext(this);
2734 }
2735
2736 // Path
2737
2738 wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
2739 {
2740 wxGraphicsPath m;
2741 m.SetRefData( new wxMacCoreGraphicsPathData(this));
2742 return m;
2743 }
2744
2745
2746 // Matrix
2747
2748 wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2749 wxDouble tx, wxDouble ty)
2750 {
2751 wxGraphicsMatrix m;
2752 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
2753 data->Set( a,b,c,d,tx,ty ) ;
2754 m.SetRefData(data);
2755 return m;
2756 }
2757
2758 wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
2759 {
2760 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
2761 return wxNullGraphicsPen;
2762 else
2763 {
2764 wxGraphicsPen p;
2765 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
2766 return p;
2767 }
2768 }
2769
2770 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
2771 {
2772 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
2773 return wxNullGraphicsBrush;
2774 else
2775 {
2776 wxGraphicsBrush p;
2777 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
2778 return p;
2779 }
2780 }
2781
2782 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap& bmp )
2783 {
2784 if ( bmp.Ok() )
2785 {
2786 wxGraphicsBitmap p;
2787 #ifdef __WXMAC__
2788 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp.CreateCGImage(), bmp.GetDepth() == 1 ) );
2789 #endif
2790 return p;
2791 }
2792 else
2793 return wxNullGraphicsBitmap;
2794 }
2795
2796 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2797 {
2798 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2799 CGImageRef img = refdata->GetBitmap();
2800 if ( img )
2801 {
2802 wxGraphicsBitmap p;
2803 CGImageRef subimg = CGImageCreateWithImageInRect(img,CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ));
2804 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg, refdata->IsMonochrome() ) );
2805 return p;
2806 }
2807 else
2808 return wxNullGraphicsBitmap;
2809 }
2810
2811 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2812 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2813 const wxColour&c1, const wxColour&c2)
2814 {
2815 wxGraphicsBrush p;
2816 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2817 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
2818 p.SetRefData(d);
2819 return p;
2820 }
2821
2822 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2823 // with radius r and color cColor
2824 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2825 const wxColour &oColor, const wxColour &cColor)
2826 {
2827 wxGraphicsBrush p;
2828 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2829 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
2830 p.SetRefData(d);
2831 return p;
2832 }
2833
2834 // sets the font
2835 wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
2836 {
2837 if ( font.Ok() )
2838 {
2839 wxGraphicsFont p;
2840 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
2841 return p;
2842 }
2843 else
2844 return wxNullGraphicsFont;
2845 }
2846
2847 //
2848 // CoreGraphics Helper Methods
2849 //
2850
2851 // Data Providers and Consumers
2852
2853 size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
2854 {
2855 CFMutableDataRef data = (CFMutableDataRef) info;
2856 if ( data )
2857 {
2858 CFDataAppendBytes( data, (const UInt8*) bytes, count );
2859 }
2860 return count;
2861 }
2862
2863 void wxMacReleaseCFDataProviderCallback(void *info,
2864 const void *WXUNUSED(data),
2865 size_t WXUNUSED(count))
2866 {
2867 if ( info )
2868 CFRelease( (CFDataRef) info );
2869 }
2870
2871 void wxMacReleaseCFDataConsumerCallback( void *info )
2872 {
2873 if ( info )
2874 CFRelease( (CFDataRef) info );
2875 }
2876
2877 CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data )
2878 {
2879 if ( data == NULL )
2880 return NULL;
2881
2882 return CGDataProviderCreateWithCFData( data );
2883 }
2884
2885 CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data )
2886 {
2887 if ( data == NULL )
2888 return NULL;
2889
2890 return CGDataConsumerCreateWithCFData( data );
2891 }
2892
2893 void
2894 wxMacReleaseMemoryBufferProviderCallback(void *info,
2895 const void * WXUNUSED_UNLESS_DEBUG(data),
2896 size_t WXUNUSED(size))
2897 {
2898 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
2899
2900 wxASSERT( data == membuf->GetData() ) ;
2901
2902 delete membuf ;
2903 }
2904
2905 CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf )
2906 {
2907 wxMemoryBuffer* b = new wxMemoryBuffer( buf );
2908 if ( b->GetDataLen() == 0 )
2909 return NULL;
2910
2911 return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() ,
2912 wxMacReleaseMemoryBufferProviderCallback );
2913 }