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