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