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