]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/graphics.cpp
support for raw ctrl shortcut
[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; }
981private :
982 CGImageRef m_bitmap;
983 bool m_monochrome;
984};
985
986wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome ) : wxGraphicsObjectRefData( renderer ),
987 m_bitmap(bitmap), m_monochrome(monochrome)
988{
989}
990
991wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
992{
993 CGImageRelease( m_bitmap );
994}
995
996//
997// Graphics Matrix
998//
999
1000//-----------------------------------------------------------------------------
1001// wxMacCoreGraphicsMatrix declaration
1002//-----------------------------------------------------------------------------
1003
1004class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData
1005{
1006public :
1007 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ;
1008
1009 virtual ~wxMacCoreGraphicsMatrixData() ;
1010
1011 virtual wxGraphicsObjectRefData *Clone() const ;
1012
1013 // concatenates the matrix
1014 virtual void Concat( const wxGraphicsMatrixData *t );
1015
1016 // sets the matrix to the respective values
1017 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1018 wxDouble tx=0.0, wxDouble ty=0.0);
1019
1020 // gets the component valuess of the matrix
1021 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
1022 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
1023
1024 // makes this the inverse matrix
1025 virtual void Invert();
1026
1027 // returns true if the elements of the transformation matrix are equal ?
1028 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
1029
1030 // return true if this is the identity matrix
1031 virtual bool IsIdentity() const;
1032
1033 //
1034 // transformation
1035 //
1036
1037 // add the translation to this matrix
1038 virtual void Translate( wxDouble dx , wxDouble dy );
1039
1040 // add the scale to this matrix
1041 virtual void Scale( wxDouble xScale , wxDouble yScale );
1042
1043 // add the rotation to this matrix (radians)
1044 virtual void Rotate( wxDouble angle );
1045
1046 //
1047 // apply the transforms
1048 //
1049
1050 // applies that matrix to the point
1051 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
1052
1053 // applies the matrix except for translations
1054 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
1055
1056 // returns the native representation
1057 virtual void * GetNativeMatrix() const;
1058
1059private :
1060 CGAffineTransform m_matrix;
1061} ;
1062
1063//-----------------------------------------------------------------------------
1064// wxMacCoreGraphicsMatrix implementation
1065//-----------------------------------------------------------------------------
1066
1067wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer)
1068{
1069}
1070
1071wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1072{
1073}
1074
1075wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const
1076{
1077 wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1078 m->m_matrix = m_matrix ;
1079 return m;
1080}
1081
1082// concatenates the matrix
1083void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t )
1084{
362439b6 1085 m_matrix = CGAffineTransformConcat(*((CGAffineTransform*) t->GetNativeMatrix()), m_matrix );
489468fe
SC
1086}
1087
1088// sets the matrix to the respective values
1089void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1090 wxDouble tx, wxDouble ty)
1091{
1092 m_matrix = CGAffineTransformMake((CGFloat) a,(CGFloat) b,(CGFloat) c,(CGFloat) d,(CGFloat) tx,(CGFloat) ty);
1093}
1094
1095// gets the component valuess of the matrix
1096void wxMacCoreGraphicsMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1097 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1098{
1099 if (a) *a = m_matrix.a;
1100 if (b) *b = m_matrix.b;
1101 if (c) *c = m_matrix.c;
1102 if (d) *d = m_matrix.d;
1103 if (tx) *tx= m_matrix.tx;
1104 if (ty) *ty= m_matrix.ty;
1105}
1106
1107// makes this the inverse matrix
1108void wxMacCoreGraphicsMatrixData::Invert()
1109{
1110 m_matrix = CGAffineTransformInvert( m_matrix );
1111}
1112
1113// returns true if the elements of the transformation matrix are equal ?
1114bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
1115{
1116 return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()));
1117}
1118
1119// return true if this is the identity matrix
1120bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1121{
1122 return ( m_matrix.a == 1 && m_matrix.d == 1 &&
1123 m_matrix.b == 0 && m_matrix.d == 0 && m_matrix.tx == 0 && m_matrix.ty == 0);
1124}
1125
1126//
1127// transformation
1128//
1129
1130// add the translation to this matrix
1131void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx , wxDouble dy )
1132{
1133 m_matrix = CGAffineTransformTranslate( m_matrix, (CGFloat) dx, (CGFloat) dy);
1134}
1135
1136// add the scale to this matrix
1137void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1138{
1139 m_matrix = CGAffineTransformScale( m_matrix, (CGFloat) xScale, (CGFloat) yScale);
1140}
1141
1142// add the rotation to this matrix (radians)
1143void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle )
1144{
1145 m_matrix = CGAffineTransformRotate( m_matrix, (CGFloat) angle);
1146}
1147
1148//
1149// apply the transforms
1150//
1151
1152// applies that matrix to the point
1153void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1154{
1155 CGPoint pt = CGPointApplyAffineTransform( CGPointMake((CGFloat) *x,(CGFloat) *y), m_matrix);
1156
1157 *x = pt.x;
1158 *y = pt.y;
1159}
1160
1161// applies the matrix except for translations
1162void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1163{
1164 CGSize sz = CGSizeApplyAffineTransform( CGSizeMake((CGFloat) *dx,(CGFloat) *dy) , m_matrix );
1165 *dx = sz.width;
1166 *dy = sz.height;
1167}
1168
1169// returns the native representation
1170void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1171{
1172 return (void*) &m_matrix;
1173}
1174
1175//
1176// Graphics Path
1177//
1178
1179//-----------------------------------------------------------------------------
1180// wxMacCoreGraphicsPath declaration
1181//-----------------------------------------------------------------------------
1182
1183class WXDLLEXPORT wxMacCoreGraphicsPathData : public wxGraphicsPathData
1184{
1185public :
1186 wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path = NULL);
1187
1188 ~wxMacCoreGraphicsPathData();
1189
1190 virtual wxGraphicsObjectRefData *Clone() const;
1191
1192 // begins a new subpath at (x,y)
1193 virtual void MoveToPoint( wxDouble x, wxDouble y );
1194
1195 // adds a straight line from the current point to (x,y)
1196 virtual void AddLineToPoint( wxDouble x, wxDouble y );
1197
1198 // adds a cubic Bezier curve from the current point, using two control points and an end point
1199 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
1200
1201 // closes the current sub-path
1202 virtual void CloseSubpath();
1203
1204 // gets the last point of the current path, (0,0) if not yet set
1205 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
1206
1207 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1208 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
1209
1210 //
1211 // These are convenience functions which - if not available natively will be assembled
1212 // using the primitives from above
1213 //
1214
1215 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1216 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
1217
1218 // appends a rectangle as a new closed subpath
1219 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1220
4ee4c7b9 1221 // appends a circle as a new closed subpath
489468fe
SC
1222 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
1223
9f43f269
SC
1224 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1225 virtual void AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
1226
489468fe
SC
1227 // 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)
1228 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
1229
1230 // adds another path
1231 virtual void AddPath( const wxGraphicsPathData* path );
1232
1233 // returns the native path
1234 virtual void * GetNativePath() const { return m_path; }
1235
1236 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1237 virtual void UnGetNativePath(void *WXUNUSED(p)) const {}
1238
1239 // transforms each point of this path by the matrix
1240 virtual void Transform( const wxGraphicsMatrixData* matrix );
1241
1242 // gets the bounding box enclosing all points (possibly including control points)
df04f800 1243 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
489468fe 1244
94a007ec 1245 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE) const;
489468fe
SC
1246private :
1247 CGMutablePathRef m_path;
1248};
1249
1250//-----------------------------------------------------------------------------
1251// wxMacCoreGraphicsPath implementation
1252//-----------------------------------------------------------------------------
1253
1254wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path) : wxGraphicsPathData(renderer)
1255{
1256 if ( path )
1257 m_path = path;
1258 else
1259 m_path = CGPathCreateMutable();
1260}
1261
1262wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1263{
1264 CGPathRelease( m_path );
1265}
1266
1267wxGraphicsObjectRefData* wxMacCoreGraphicsPathData::Clone() const
1268{
1269 wxMacCoreGraphicsPathData* clone = new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path));
1270 return clone ;
1271}
1272
1273
1274// opens (starts) a new subpath
1275void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1 , wxDouble y1 )
1276{
1277 CGPathMoveToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1278}
1279
1280void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1 , wxDouble y1 )
1281{
1282 CGPathAddLineToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1283}
1284
1285void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
1286{
1287 CGPathAddCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) cx2, (CGFloat) cy2, (CGFloat) x , (CGFloat) y );
1288}
1289
1290void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
1291{
1292 CGPathAddQuadCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) x , (CGFloat) y );
1293}
1294
1295void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1296{
1297 CGRect cgRect = { { (CGFloat) x , (CGFloat) y } , { (CGFloat) w , (CGFloat) h } };
1298 CGPathAddRect( m_path , NULL , cgRect );
1299}
1300
1301void wxMacCoreGraphicsPathData::AddCircle( wxDouble x, wxDouble y , wxDouble r )
1302{
9f43f269
SC
1303 CGPathAddEllipseInRect( m_path, NULL, CGRectMake(x-r,y-r,2*r,2*r));
1304}
1305
1306void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1307{
1308 CGPathAddEllipseInRect( m_path, NULL, CGRectMake(x,y,w,h));
489468fe
SC
1309}
1310
1311// adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1312void wxMacCoreGraphicsPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
1313{
1314 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1315 CGPathAddArc( m_path, NULL , (CGFloat) x, (CGFloat) y, (CGFloat) r, (CGFloat) startAngle, (CGFloat) endAngle, !clockwise);
1316}
1317
1318void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
1319{
1320 CGPathAddArcToPoint( m_path, NULL , (CGFloat) x1, (CGFloat) y1, (CGFloat) x2, (CGFloat) y2, (CGFloat) r);
1321}
1322
1323void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData* path )
1324{
1325 CGPathAddPath( m_path , NULL, (CGPathRef) path->GetNativePath() );
1326}
1327
1328// closes the current subpath
1329void wxMacCoreGraphicsPathData::CloseSubpath()
1330{
1331 CGPathCloseSubpath( m_path );
1332}
1333
1334// gets the last point of the current path, (0,0) if not yet set
1335void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
1336{
1337 CGPoint p = CGPathGetCurrentPoint( m_path );
1338 *x = p.x;
1339 *y = p.y;
1340}
1341
1342// transforms each point of this path by the matrix
1343void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData* matrix )
1344{
1345 CGMutablePathRef p = CGPathCreateMutable() ;
1346 CGPathAddPath( p, (CGAffineTransform*) matrix->GetNativeMatrix() , m_path );
1347 CGPathRelease( m_path );
1348 m_path = p;
1349}
1350
1351// gets the bounding box enclosing all points (possibly including control points)
1352void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
1353{
1354 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1355 *x = bounds.origin.x;
1356 *y = bounds.origin.y;
1357 *w = bounds.size.width;
1358 *h = bounds.size.height;
1359}
1360
33ef0bdb 1361bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle) const
489468fe
SC
1362{
1363 return CGPathContainsPoint( m_path, NULL, CGPointMake((CGFloat) x,(CGFloat) y), fillStyle == wxODDEVEN_RULE );
1364}
1365
1366//
1367// Graphics Context
1368//
1369
1370//-----------------------------------------------------------------------------
1371// wxMacCoreGraphicsContext declaration
1372//-----------------------------------------------------------------------------
1373
1374class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
1375{
1376public:
1377 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width = 0, wxDouble height = 0 );
1378
b2680ced 1379#if wxOSX_USE_CARBON
489468fe 1380 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window );
b2680ced 1381#endif
489468fe
SC
1382
1383 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window );
1384
1385 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer);
1386
1387 wxMacCoreGraphicsContext();
1388
1389 ~wxMacCoreGraphicsContext();
1390
1391 void Init();
1392
489468fe
SC
1393 virtual void StartPage( wxDouble width, wxDouble height );
1394
1395 virtual void EndPage();
1396
1397 virtual void Flush();
1398
1399 // push the current state of the context, ie the transformation matrix on a stack
1400 virtual void PushState();
1401
1402 // pops a stored state from the stack
1403 virtual void PopState();
1404
1405 // clips drawings to the region
1406 virtual void Clip( const wxRegion &region );
1407
1408 // clips drawings to the rect
1409 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1410
1411 // resets the clipping to original extent
1412 virtual void ResetClip();
1413
1414 virtual void * GetNativeContext();
1415
bf02a7f9
SC
1416 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
1417
133cb28b
SC
1418 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation);
1419
bf02a7f9
SC
1420 virtual bool SetCompositionMode(wxCompositionMode op);
1421
1422 virtual void BeginLayer(wxDouble opacity);
1423
1424 virtual void EndLayer();
03647350 1425
489468fe
SC
1426 //
1427 // transformation
1428 //
1429
1430 // translate
1431 virtual void Translate( wxDouble dx , wxDouble dy );
1432
1433 // scale
1434 virtual void Scale( wxDouble xScale , wxDouble yScale );
1435
1436 // rotate (radians)
1437 virtual void Rotate( wxDouble angle );
1438
1439 // concatenates this transform with the current transform of this context
1440 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
1441
1442 // sets the transform of this context
1443 virtual void SetTransform( const wxGraphicsMatrix& matrix );
1444
1445 // gets the matrix of this context
1446 virtual wxGraphicsMatrix GetTransform() const;
1447 //
1448 // setting the paint
1449 //
1450
1451 // strokes along a path with the current pen
1452 virtual void StrokePath( const wxGraphicsPath &path );
1453
1454 // fills a path with the current brush
33ef0bdb 1455 virtual void FillPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
489468fe
SC
1456
1457 // draws a path by first filling and then stroking
33ef0bdb 1458 virtual void DrawPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
489468fe
SC
1459
1460 virtual bool ShouldOffset() const
1461 {
ad967c5b
SC
1462 if ( !m_enableOffset )
1463 return false;
1464
489468fe
SC
1465 int penwidth = 0 ;
1466 if ( !m_pen.IsNull() )
1467 {
1468 penwidth = (int)((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->GetWidth();
1469 if ( penwidth == 0 )
1470 penwidth = 1;
1471 }
1472 return ( penwidth % 2 ) == 1;
1473 }
1474 //
1475 // text
1476 //
1477
489468fe
SC
1478 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
1479 wxDouble *descent, wxDouble *externalLeading ) const;
1480
1481 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
1482
1483 //
1484 // image support
1485 //
1486
1487 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1488
1489 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1490
1491 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
2bc4cc1e
SC
1492
1493 // fast convenience methods
1494
1495
1496 virtual void DrawRectangleX( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
489468fe
SC
1497
1498 void SetNativeContext( CGContextRef cg );
1499
b2680ced 1500 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext)
489468fe
SC
1501
1502private:
f28b6f06 1503 bool EnsureIsValid();
489468fe 1504
0b7dce54
VZ
1505 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
1506 virtual void DoDrawRotatedText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
1507
489468fe 1508 CGContextRef m_cgContext;
b2680ced 1509#if wxOSX_USE_CARBON
489468fe 1510 WindowRef m_windowRef;
15fc716c
SC
1511#else
1512 WXWidget m_view;
b2680ced 1513#endif
15fc716c 1514 bool m_contextSynthesized;
489468fe 1515 CGAffineTransform m_windowTransform;
f28b6f06 1516 bool m_invisible;
489468fe 1517
15fc716c 1518#if wxOSX_USE_COCOA_OR_CARBON
489468fe 1519 wxCFRef<HIShapeRef> m_clipRgn;
b2680ced 1520#endif
489468fe
SC
1521};
1522
1523//-----------------------------------------------------------------------------
1524// device context implementation
1525//
1526// more and more of the dc functionality should be implemented by calling
1527// the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1528// also coordinate conversions should be moved to native matrix ops
1529//-----------------------------------------------------------------------------
1530
1531// we always stock two context states, one at entry, to be able to preserve the
1532// state we were called with, the other one after changing to HI Graphics orientation
1533// (this one is used for getting back clippings etc)
1534
1535//-----------------------------------------------------------------------------
1536// wxMacCoreGraphicsContext implementation
1537//-----------------------------------------------------------------------------
1538
1539IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext)
1540
1541class wxQuartzOffsetHelper
1542{
1543public :
1544 wxQuartzOffsetHelper( CGContextRef cg , bool offset )
1545 {
1546 m_cg = cg;
1547 m_offset = offset;
1548 if ( m_offset )
e812e279
SC
1549 {
1550 m_userOffset = CGContextConvertSizeToUserSpace( m_cg, CGSizeMake( 0.5 , 0.5 ) );
1551 CGContextTranslateCTM( m_cg, m_userOffset.width , m_userOffset.height );
1552 }
ce00f59b 1553 else
24e67779
SC
1554 {
1555 m_userOffset = CGSizeMake(0.0, 0.0);
1556 }
1557
489468fe
SC
1558 }
1559 ~wxQuartzOffsetHelper( )
1560 {
1561 if ( m_offset )
e812e279 1562 CGContextTranslateCTM( m_cg, -m_userOffset.width , -m_userOffset.height );
489468fe
SC
1563 }
1564public :
e812e279 1565 CGSize m_userOffset;
489468fe
SC
1566 CGContextRef m_cg;
1567 bool m_offset;
1568} ;
1569
1570void wxMacCoreGraphicsContext::Init()
1571{
1572 m_cgContext = NULL;
15fc716c
SC
1573 m_contextSynthesized = false;
1574 m_width = 0;
1575 m_height = 0;
b2680ced 1576#if wxOSX_USE_CARBON
489468fe 1577 m_windowRef = NULL;
b2680ced 1578#endif
15fc716c
SC
1579#if wxOSX_USE_COCOA_OR_IPHONE
1580 m_view = NULL;
1581#endif
f28b6f06 1582 m_invisible = false;
57d3e266
SC
1583 m_antialias = wxANTIALIAS_DEFAULT;
1584 m_interpolation = wxINTERPOLATION_DEFAULT;
489468fe
SC
1585}
1586
1587wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width, wxDouble height ) : wxGraphicsContext(renderer)
1588{
1589 Init();
1590 SetNativeContext(cgcontext);
1591 m_width = width;
1592 m_height = height;
1593}
1594
b2680ced 1595#if wxOSX_USE_CARBON
489468fe
SC
1596wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer)
1597{
1598 Init();
1599 m_windowRef = window;
ad967c5b 1600 m_enableOffset = true;
489468fe 1601}
b2680ced 1602#endif
489468fe
SC
1603
1604wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer)
1605{
1606 Init();
1607
ad967c5b 1608 m_enableOffset = true;
15fc716c
SC
1609 wxSize sz = window->GetSize();
1610 m_width = sz.x;
1611 m_height = sz.y;
1612
1613#if wxOSX_USE_COCOA_OR_IPHONE
1614 m_view = window->GetHandle();
1615
b4ff8b3e
SC
1616#if wxOSX_USE_COCOA
1617 if ( ! window->GetPeer()->IsFlipped() )
15fc716c
SC
1618 {
1619 m_windowTransform = CGAffineTransformMakeTranslation( 0 , m_height );
1620 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1621 }
1622 else
b4ff8b3e 1623#endif
15fc716c
SC
1624 {
1625 m_windowTransform = CGAffineTransformIdentity;
1626 }
1627#else
489468fe
SC
1628 int originX , originY;
1629 originX = originY = 0;
489468fe 1630 Rect bounds = { 0,0,0,0 };
489468fe
SC
1631 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
1632 window->MacWindowToRootWindow( &originX , &originY );
1633 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
489468fe
SC
1634 m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top );
1635 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1636 m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ;
15fc716c 1637#endif
489468fe
SC
1638}
1639
1640wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer)
1641{
1642 Init();
1643}
1644
1645wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL)
1646{
1647 Init();
1648 wxLogDebug(wxT("Illegal Constructor called"));
1649}
1650
1651wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1652{
1653 SetNativeContext(NULL);
1654}
1655
489468fe
SC
1656
1657void wxMacCoreGraphicsContext::StartPage( wxDouble width, wxDouble height )
1658{
1659 CGRect r;
1660 if ( width != 0 && height != 0)
1661 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) width , (CGFloat) height );
1662 else
1663 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) m_width , (CGFloat) m_height );
1664
1665 CGContextBeginPage(m_cgContext, &r );
1666// CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1667// CGContextScaleCTM( m_cgContext , 1 , -1 );
1668}
1669
1670void wxMacCoreGraphicsContext::EndPage()
1671{
1672 CGContextEndPage(m_cgContext);
1673}
1674
1675void wxMacCoreGraphicsContext::Flush()
1676{
1677 CGContextFlush(m_cgContext);
1678}
1679
f28b6f06 1680bool wxMacCoreGraphicsContext::EnsureIsValid()
489468fe
SC
1681{
1682 if ( !m_cgContext )
1683 {
f28b6f06
SC
1684 if (m_invisible)
1685 return false;
03647350 1686
b4ff8b3e 1687#if wxOSX_USE_COCOA
f28b6f06
SC
1688 if ( wxOSXLockFocus(m_view) )
1689 {
b4ff8b3e 1690 m_cgContext = wxOSXGetContextFromCurrentContext();
9a83f860 1691 wxASSERT_MSG( m_cgContext != NULL, wxT("Unable to retrieve drawing context from View"));
f28b6f06
SC
1692 }
1693 else
1694 {
1695 m_invisible = true;
1696 }
15fc716c 1697#endif
b4ff8b3e
SC
1698#if wxOSX_USE_IPHONE
1699 m_cgContext = wxOSXGetContextFromCurrentContext();
1700 if ( m_cgContext == NULL )
1701 {
1702 m_invisible = true;
1703 }
1704#endif
15fc716c 1705#if wxOSX_USE_CARBON
b2680ced 1706 OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
489468fe
SC
1707 if ( status != noErr )
1708 {
1709 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1710 }
15fc716c 1711#endif
f28b6f06 1712 if ( m_cgContext )
489468fe 1713 {
f28b6f06 1714 CGContextSaveGState( m_cgContext );
b4ff8b3e 1715#if wxOSX_USE_COCOA_OR_CARBON
f28b6f06 1716 if ( m_clipRgn.get() )
489468fe 1717 {
f28b6f06 1718 wxCFRef<HIMutableShapeRef> hishape( HIShapeCreateMutableCopy( m_clipRgn ) );
f28b6f06
SC
1719 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1720 if ( HIShapeIsEmpty(hishape))
1721 {
1722 CGRect empty = CGRectMake( 0,0,0,0 );
1723 CGContextClipToRect( m_cgContext, empty );
1724 }
1725 else
1726 {
1727 HIShapeReplacePathInCGContext( hishape, m_cgContext );
1728 CGContextClip( m_cgContext );
1729 }
489468fe 1730 }
b4ff8b3e 1731#endif
73be1281
SC
1732 CGContextConcatCTM( m_cgContext, m_windowTransform );
1733 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
1734 m_contextSynthesized = true;
f28b6f06 1735 CGContextSaveGState( m_cgContext );
03647350 1736
f28b6f06
SC
1737#if 0 // turn on for debugging of clientdc
1738 static float color = 0.5 ;
1739 static int channel = 0 ;
1740 CGRect bounds = CGRectMake(-1000,-1000,2000,2000);
1741 CGContextSetRGBFillColor( m_cgContext, channel == 0 ? color : 0.5 ,
1742 channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 );
1743 CGContextFillRect( m_cgContext, bounds );
1744 color += 0.1 ;
1745 if ( color > 0.9 )
489468fe 1746 {
f28b6f06
SC
1747 color = 0.5 ;
1748 channel++ ;
1749 if ( channel == 3 )
1750 channel = 0 ;
489468fe 1751 }
b2680ced 1752#endif
f28b6f06 1753 }
489468fe 1754 }
f28b6f06 1755 return m_cgContext != NULL;
489468fe
SC
1756}
1757
bf02a7f9 1758bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias)
489468fe 1759{
ab451f97 1760 if (!EnsureIsValid())
bf02a7f9
SC
1761 return true;
1762
1763 if (m_antialias == antialias)
489468fe 1764 return true;
03647350 1765
bf02a7f9 1766 m_antialias = antialias;
03647350 1767
bf02a7f9
SC
1768 bool antialiasMode;
1769 switch (antialias)
1770 {
1771 case wxANTIALIAS_DEFAULT:
1772 antialiasMode = true;
1773 break;
1774 case wxANTIALIAS_NONE:
1775 antialiasMode = false;
1776 break;
1777 default:
1778 return false;
1779 }
1780 CGContextSetShouldAntialias(m_cgContext, antialiasMode);
1781 return true;
1782}
489468fe 1783
57d3e266 1784bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation)
133cb28b 1785{
57d3e266
SC
1786 if (!EnsureIsValid())
1787 return true;
1788
1789 if (m_interpolation == interpolation)
1790 return true;
1791
1792 m_interpolation = interpolation;
1793 CGInterpolationQuality quality;
1794
1795 switch (interpolation)
1796 {
1797 case wxINTERPOLATION_DEFAULT:
1798 quality = kCGInterpolationDefault;
1799 break;
1800 case wxINTERPOLATION_NONE:
1801 quality = kCGInterpolationNone;
1802 break;
1803 case wxINTERPOLATION_FAST:
1804 quality = kCGInterpolationLow;
1805 break;
1806 case wxINTERPOLATION_GOOD:
2ebfa9c0 1807#if wxOSX_USE_COCOA_OR_CARBON
57d3e266 1808 quality = UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh : (CGInterpolationQuality) 4 /*kCGInterpolationMedium only on 10.6*/;
2ebfa9c0
SC
1809#else
1810 quality = kCGInterpolationMedium;
1811#endif
57d3e266
SC
1812 break;
1813 case wxINTERPOLATION_BEST:
1814 quality = kCGInterpolationHigh;
1815 break;
1816 default:
1817 return false;
1818 }
1819 CGContextSetInterpolationQuality(m_cgContext, quality);
1820 return true;
133cb28b
SC
1821}
1822
bf02a7f9
SC
1823bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op)
1824{
ab451f97 1825 if (!EnsureIsValid())
f28b6f06 1826 return true;
489468fe 1827
bf02a7f9
SC
1828 if ( m_composition == op )
1829 return true;
03647350 1830
bf02a7f9 1831 m_composition = op;
03647350 1832
bf02a7f9
SC
1833 if (m_composition == wxCOMPOSITION_DEST)
1834 return true;
03647350 1835
bf02a7f9 1836#if wxOSX_USE_COCOA_OR_CARBON
bf02a7f9 1837 if ( UMAGetSystemVersion() < 0x1060 )
489468fe 1838 {
bf02a7f9
SC
1839 CGCompositeOperation cop = kCGCompositeOperationSourceOver;
1840 CGBlendMode mode = kCGBlendModeNormal;
1841 switch( op )
489468fe 1842 {
bf02a7f9 1843 case wxCOMPOSITION_CLEAR:
03647350 1844 cop = kCGCompositeOperationClear;
bf02a7f9
SC
1845 break;
1846 case wxCOMPOSITION_SOURCE:
03647350 1847 cop = kCGCompositeOperationCopy;
bf02a7f9
SC
1848 break;
1849 case wxCOMPOSITION_OVER:
03647350 1850 mode = kCGBlendModeNormal;
bf02a7f9
SC
1851 break;
1852 case wxCOMPOSITION_IN:
03647350 1853 cop = kCGCompositeOperationSourceIn;
bf02a7f9
SC
1854 break;
1855 case wxCOMPOSITION_OUT:
03647350 1856 cop = kCGCompositeOperationSourceOut;
bf02a7f9
SC
1857 break;
1858 case wxCOMPOSITION_ATOP:
03647350 1859 cop = kCGCompositeOperationSourceAtop;
bf02a7f9
SC
1860 break;
1861 case wxCOMPOSITION_DEST_OVER:
03647350 1862 cop = kCGCompositeOperationDestinationOver;
bf02a7f9
SC
1863 break;
1864 case wxCOMPOSITION_DEST_IN:
03647350 1865 cop = kCGCompositeOperationDestinationIn;
bf02a7f9
SC
1866 break;
1867 case wxCOMPOSITION_DEST_OUT:
03647350 1868 cop = kCGCompositeOperationDestinationOut;
bf02a7f9
SC
1869 break;
1870 case wxCOMPOSITION_DEST_ATOP:
03647350 1871 cop = kCGCompositeOperationDestinationAtop;
bf02a7f9
SC
1872 break;
1873 case wxCOMPOSITION_XOR:
03647350 1874 cop = kCGCompositeOperationXOR;
bf02a7f9 1875 break;
c0b5b33b 1876#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
bf02a7f9
SC
1877 case wxCOMPOSITION_ADD:
1878 mode = kCGBlendModePlusLighter ;
1879 break;
c0b5b33b 1880#endif
bf02a7f9
SC
1881 default:
1882 return false;
489468fe 1883 }
bf02a7f9
SC
1884 if ( cop != kCGCompositeOperationSourceOver )
1885 CGContextSetCompositeOperation(m_cgContext, cop);
1886 else
1887 CGContextSetBlendMode(m_cgContext, mode);
489468fe 1888 }
489468fe 1889#endif
f72e03b2
KO
1890#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1891 else
489468fe 1892 {
bf02a7f9
SC
1893 CGBlendMode mode = kCGBlendModeNormal;
1894 switch( op )
489468fe 1895 {
bf02a7f9 1896 case wxCOMPOSITION_CLEAR:
03647350 1897 mode = kCGBlendModeClear;
bf02a7f9
SC
1898 break;
1899 case wxCOMPOSITION_SOURCE:
03647350 1900 mode = kCGBlendModeCopy;
bf02a7f9
SC
1901 break;
1902 case wxCOMPOSITION_OVER:
03647350 1903 mode = kCGBlendModeNormal;
bf02a7f9
SC
1904 break;
1905 case wxCOMPOSITION_IN:
03647350 1906 mode = kCGBlendModeSourceIn;
bf02a7f9
SC
1907 break;
1908 case wxCOMPOSITION_OUT:
03647350 1909 mode = kCGBlendModeSourceOut;
bf02a7f9
SC
1910 break;
1911 case wxCOMPOSITION_ATOP:
03647350 1912 mode = kCGBlendModeSourceAtop;
bf02a7f9
SC
1913 break;
1914 case wxCOMPOSITION_DEST_OVER:
03647350 1915 mode = kCGBlendModeDestinationOver;
bf02a7f9
SC
1916 break;
1917 case wxCOMPOSITION_DEST_IN:
03647350 1918 mode = kCGBlendModeDestinationIn;
bf02a7f9
SC
1919 break;
1920 case wxCOMPOSITION_DEST_OUT:
03647350 1921 mode = kCGBlendModeDestinationOut;
bf02a7f9
SC
1922 break;
1923 case wxCOMPOSITION_DEST_ATOP:
03647350 1924 mode = kCGBlendModeDestinationAtop;
bf02a7f9
SC
1925 break;
1926 case wxCOMPOSITION_XOR:
03647350 1927 mode = kCGBlendModeXOR;
bf02a7f9 1928 break;
03647350 1929
bf02a7f9
SC
1930 case wxCOMPOSITION_ADD:
1931 mode = kCGBlendModePlusLighter ;
1932 break;
1933 default:
1934 return false;
489468fe 1935 }
bf02a7f9 1936 CGContextSetBlendMode(m_cgContext, mode);
489468fe 1937 }
f72e03b2 1938#endif
bf02a7f9
SC
1939 return true;
1940}
489468fe 1941
bf02a7f9
SC
1942void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity)
1943{
1944 CGContextSaveGState(m_cgContext);
de0d2095 1945 CGContextSetAlpha(m_cgContext, (CGFloat) opacity);
bf02a7f9
SC
1946 CGContextBeginTransparencyLayer(m_cgContext, 0);
1947}
1948
1949void wxMacCoreGraphicsContext::EndLayer()
1950{
1951 CGContextEndTransparencyLayer(m_cgContext);
1952 CGContextRestoreGState(m_cgContext);
489468fe
SC
1953}
1954
1955void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1956{
15fc716c 1957#if wxOSX_USE_COCOA_OR_CARBON
489468fe
SC
1958 if( m_cgContext )
1959 {
1960 wxCFRef<HIShapeRef> shape = wxCFRefFromGet(region.GetWXHRGN());
1961 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1962 if ( HIShapeIsEmpty(shape))
1963 {
1964 CGRect empty = CGRectMake( 0,0,0,0 );
1965 CGContextClipToRect( m_cgContext, empty );
1966 }
1967 else
1968 {
1969 HIShapeReplacePathInCGContext( shape, m_cgContext );
1970 CGContextClip( m_cgContext );
1971 }
1972 }
1973 else
1974 {
1975 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1976 // to regions we try at least to have correct translations
1977 HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( region.GetWXHRGN() );
1978
1979 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform );
1980 HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y );
1981 m_clipRgn.reset(mutableShape);
1982 }
b2680ced
SC
1983#else
1984 // allow usage as measuring context
1985 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
489468fe
SC
1986#endif
1987}
1988
1989// clips drawings to the rect
1990void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1991{
1992 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
1993 if ( m_cgContext )
1994 {
1995 CGContextClipToRect( m_cgContext, r );
1996 }
1997 else
1998 {
15fc716c 1999#if wxOSX_USE_COCOA_OR_CARBON
489468fe
SC
2000 // the clipping itself must be stored as device coordinates, otherwise
2001 // we cannot apply it back correctly
2002 r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform );
73be1281 2003 r.size= CGSizeApplyAffineTransform(r.size, m_windowTransform);
489468fe 2004 m_clipRgn.reset(HIShapeCreateWithRect(&r));
b2680ced
SC
2005#else
2006 // allow usage as measuring context
2007 // wxFAIL_MSG( "Needs a valid context for clipping" );
2008#endif
489468fe
SC
2009 }
2010}
2011
2012 // resets the clipping to original extent
2013void wxMacCoreGraphicsContext::ResetClip()
2014{
2015 if ( m_cgContext )
2016 {
2017 // there is no way for clearing the clip, we can only revert to the stored
2018 // state, but then we have to make sure everything else is NOT restored
2019 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2020 CGContextRestoreGState( m_cgContext );
2021 CGContextSaveGState( m_cgContext );
2022 CGAffineTransform transformNew = CGContextGetCTM( m_cgContext );
2023 transformNew = CGAffineTransformInvert( transformNew ) ;
2024 CGContextConcatCTM( m_cgContext, transformNew);
2025 CGContextConcatCTM( m_cgContext, transform);
2026 }
2027 else
2028 {
15fc716c 2029#if wxOSX_USE_COCOA_OR_CARBON
489468fe 2030 m_clipRgn.reset();
b2680ced
SC
2031#else
2032 // allow usage as measuring context
2033 // wxFAIL_MSG( "Needs a valid context for clipping" );
2034#endif
489468fe
SC
2035 }
2036}
2037
2038void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
2039{
2040 if ( m_pen.IsNull() )
2041 return ;
2042
ab451f97 2043 if (!EnsureIsValid())
f28b6f06 2044 return;
03647350 2045
bf02a7f9
SC
2046 if (m_composition == wxCOMPOSITION_DEST)
2047 return;
2048
489468fe
SC
2049 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2050
2051 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2052 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2053 CGContextStrokePath( m_cgContext );
2054}
2055
33ef0bdb 2056void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
489468fe 2057{
ab451f97 2058 if (!EnsureIsValid())
f28b6f06 2059 return;
03647350 2060
bf02a7f9
SC
2061 if (m_composition == wxCOMPOSITION_DEST)
2062 return;
2063
489468fe
SC
2064 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2065 {
2066 // when using shading, we cannot draw pen and brush at the same time
2067 // revert to the base implementation of first filling and then stroking
2068 wxGraphicsContext::DrawPath( path, fillStyle );
2069 return;
2070 }
2071
2072 CGPathDrawingMode mode = kCGPathFill ;
2073 if ( m_brush.IsNull() )
2074 {
2075 if ( m_pen.IsNull() )
2076 return;
2077 else
2078 mode = kCGPathStroke;
2079 }
2080 else
2081 {
2082 if ( m_pen.IsNull() )
2083 {
2084 if ( fillStyle == wxODDEVEN_RULE )
2085 mode = kCGPathEOFill;
2086 else
2087 mode = kCGPathFill;
2088 }
2089 else
2090 {
2091 if ( fillStyle == wxODDEVEN_RULE )
2092 mode = kCGPathEOFillStroke;
2093 else
2094 mode = kCGPathFillStroke;
2095 }
2096 }
2097
489468fe
SC
2098 if ( !m_brush.IsNull() )
2099 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2100 if ( !m_pen.IsNull() )
2101 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2102
2103 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2104
2105 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2106 CGContextDrawPath( m_cgContext , mode );
2107}
2108
33ef0bdb 2109void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
489468fe
SC
2110{
2111 if ( m_brush.IsNull() )
2112 return;
2113
ab451f97 2114 if (!EnsureIsValid())
f28b6f06 2115 return;
03647350 2116
bf02a7f9
SC
2117 if (m_composition == wxCOMPOSITION_DEST)
2118 return;
2119
489468fe
SC
2120 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2121 {
2122 CGContextSaveGState( m_cgContext );
2123 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2124 CGContextClip( m_cgContext );
2125 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2126 CGContextRestoreGState( m_cgContext);
2127 }
2128 else
2129 {
2130 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2131 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2132 if ( fillStyle == wxODDEVEN_RULE )
2133 CGContextEOFillPath( m_cgContext );
2134 else
2135 CGContextFillPath( m_cgContext );
2136 }
2137}
2138
2139void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
2140{
2141 // we allow either setting or clearing but not replacing
2142 wxASSERT( m_cgContext == NULL || cg == NULL );
2143
2144 if ( m_cgContext )
2145 {
489468fe
SC
2146 CGContextRestoreGState( m_cgContext );
2147 CGContextRestoreGState( m_cgContext );
15fc716c 2148 if ( m_contextSynthesized )
489468fe 2149 {
b2680ced 2150#if wxOSX_USE_CARBON
489468fe 2151 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
15fc716c 2152#endif
b4ff8b3e 2153#if wxOSX_USE_COCOA
15fc716c 2154 wxOSXUnlockFocus(m_view);
489468fe
SC
2155#endif
2156 }
2157 else
2158 CGContextRelease(m_cgContext);
2159 }
2160
489468fe
SC
2161 m_cgContext = cg;
2162
2163 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2164 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2165 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2166 // for this one operation.
2167
2168 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2169 // can be removed.
2170 if (m_cgContext)
2171 {
2172 CGContextRetain(m_cgContext);
2173 CGContextSaveGState( m_cgContext );
2174 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
2175 CGContextSaveGState( m_cgContext );
15fc716c 2176 m_contextSynthesized = false;
489468fe
SC
2177 }
2178}
2179
2180void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
2181{
2182 if ( m_cgContext )
2183 CGContextTranslateCTM( m_cgContext, (CGFloat) dx, (CGFloat) dy );
2184 else
2185 m_windowTransform = CGAffineTransformTranslate(m_windowTransform, (CGFloat) dx, (CGFloat) dy);
2186}
2187
2188void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
2189{
2190 if ( m_cgContext )
2191 CGContextScaleCTM( m_cgContext , (CGFloat) xScale , (CGFloat) yScale );
2192 else
2193 m_windowTransform = CGAffineTransformScale(m_windowTransform, (CGFloat) xScale, (CGFloat) yScale);
2194}
2195
2196void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
2197{
2198 if ( m_cgContext )
2199 CGContextRotateCTM( m_cgContext , (CGFloat) angle );
2200 else
2201 m_windowTransform = CGAffineTransformRotate(m_windowTransform, (CGFloat) angle);
2202}
2203
2204void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2205{
2206 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
2207 DrawBitmap(bitmap, x, y, w, h);
2208}
2209
2210void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2211{
ab451f97 2212 if (!EnsureIsValid())
f28b6f06 2213 return;
bf02a7f9
SC
2214
2215 if (m_composition == wxCOMPOSITION_DEST)
2216 return;
03647350 2217
489468fe
SC
2218#ifdef __WXMAC__
2219 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2220 CGImageRef image = refdata->GetBitmap();
2221 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2222 if ( refdata->IsMonochrome() == 1 )
2223 {
d13b34d3 2224 // is a mask, the '1' in the mask tell where to draw the current brush
489468fe
SC
2225 if ( !m_brush.IsNull() )
2226 {
2227 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2228 {
2229 // TODO clip to mask
2230 /*
2231 CGContextSaveGState( m_cgContext );
2232 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2233 CGContextClip( m_cgContext );
2234 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2235 CGContextRestoreGState( m_cgContext);
2236 */
2237 }
2238 else
2239 {
2240 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2241 wxMacDrawCGImage( m_cgContext , &r , image );
2242 }
2243 }
2244 }
2245 else
2246 {
2247 wxMacDrawCGImage( m_cgContext , &r , image );
2248 }
2249#endif
2250}
2251
2252void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2253{
ab451f97 2254 if (!EnsureIsValid())
f28b6f06 2255 return;
03647350 2256
bf02a7f9
SC
2257 if (m_composition == wxCOMPOSITION_DEST)
2258 return;
2259
489468fe
SC
2260 CGRect r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) w , (CGFloat) h );
2261 CGContextSaveGState( m_cgContext );
2262 CGContextTranslateCTM( m_cgContext,(CGFloat) x ,(CGFloat) (y + h) );
2263 CGContextScaleCTM( m_cgContext, 1, -1 );
f28b6f06 2264#if wxOSX_USE_COCOA_OR_CARBON
489468fe 2265 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
f28b6f06 2266 NULL , kPlotIconRefNormalFlags , icon.GetHICON() );
489468fe
SC
2267#endif
2268 CGContextRestoreGState( m_cgContext );
2269}
2270
2271void wxMacCoreGraphicsContext::PushState()
2272{
ab451f97 2273 if (!EnsureIsValid())
f28b6f06 2274 return;
03647350 2275
489468fe
SC
2276 CGContextSaveGState( m_cgContext );
2277}
2278
2279void wxMacCoreGraphicsContext::PopState()
2280{
ab451f97 2281 if (!EnsureIsValid())
f28b6f06 2282 return;
03647350 2283
489468fe
SC
2284 CGContextRestoreGState( m_cgContext );
2285}
2286
0b7dce54 2287void wxMacCoreGraphicsContext::DoDrawText( const wxString &str, wxDouble x, wxDouble y )
489468fe 2288{
1011fbeb 2289 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
489468fe 2290
ab451f97 2291 if (!EnsureIsValid())
f28b6f06 2292 return;
03647350 2293
bf02a7f9
SC
2294 if (m_composition == wxCOMPOSITION_DEST)
2295 return;
2296
292e5e1f 2297#if wxOSX_USE_CORE_TEXT
489468fe
SC
2298 if ( UMAGetSystemVersion() >= 0x1050 )
2299 {
2300 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2301 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
aa6208d9 2302 CTFontRef font = fref->OSXGetCTFont();
489468fe
SC
2303 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2304 CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ;
2305 wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) );
2306 CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName };
2307 CFTypeRef values[] = { font, col, underlined };
2308 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2309 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2310 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2311 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2312
2313 y += CTFontGetAscent(font);
2314
2315 CGContextSaveGState(m_cgContext);
de0d2095 2316 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
489468fe
SC
2317 CGContextScaleCTM(m_cgContext, 1, -1);
2318 CGContextSetTextPosition(m_cgContext, 0, 0);
2319 CTLineDraw( line, m_cgContext );
2320 CGContextRestoreGState(m_cgContext);
2321 CFRelease( col );
2322 return;
2323 }
2324#endif
292e5e1f 2325#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2326 {
2327 DrawText(str, x, y, 0.0);
2328 return;
2329 }
2330#endif
292e5e1f 2331#if wxOSX_USE_IPHONE
b2680ced
SC
2332 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2333
2334 CGContextSaveGState(m_cgContext);
2335
2336 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
0b7dce54 2337 CGContextSetTextDrawingMode (m_cgContext, kCGTextFill);
b2680ced
SC
2338 CGContextSetFillColorWithColor( m_cgContext, col );
2339
2340 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2341 DrawTextInContext( m_cgContext, CGPointMake( x, y ), fref->GetUIFont() , text.AsNSString() );
2342
2343 CGContextRestoreGState(m_cgContext);
2344 CFRelease( col );
489468fe
SC
2345#endif
2346}
2347
0b7dce54
VZ
2348void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString &str,
2349 wxDouble x, wxDouble y,
2350 wxDouble angle)
489468fe 2351{
1011fbeb 2352 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
489468fe 2353
ab451f97 2354 if (!EnsureIsValid())
f28b6f06 2355 return;
03647350 2356
bf02a7f9
SC
2357 if (m_composition == wxCOMPOSITION_DEST)
2358 return;
2359
292e5e1f 2360#if wxOSX_USE_CORE_TEXT
489468fe
SC
2361 if ( UMAGetSystemVersion() >= 0x1050 )
2362 {
2363 // default implementation takes care of rotation and calls non rotated DrawText afterwards
02fd8b9b 2364 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
489468fe
SC
2365 return;
2366 }
2367#endif
292e5e1f 2368#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2369 {
2370 OSStatus status = noErr;
2371 ATSUTextLayout atsuLayout;
2372 wxMacUniCharBuffer unibuf( str );
2373 UniCharCount chars = unibuf.GetChars();
2374
2375 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2376 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2377 &chars , &style , &atsuLayout );
2378
2379 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
2380
2381 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2382 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2383
2384 int iAngle = int( angle * RAD2DEG );
2385 if ( abs(iAngle) > 0 )
2386 {
2387 Fixed atsuAngle = IntToFixed( iAngle );
2388 ATSUAttributeTag atsuTags[] =
2389 {
2390 kATSULineRotationTag ,
2391 };
9611b797 2392 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
489468fe
SC
2393 {
2394 sizeof( Fixed ) ,
2395 };
9611b797 2396 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
489468fe
SC
2397 {
2398 &atsuAngle ,
2399 };
9611b797 2400 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
489468fe
SC
2401 atsuTags, atsuSizes, atsuValues );
2402 }
2403
2404 {
2405 ATSUAttributeTag atsuTags[] =
2406 {
2407 kATSUCGContextTag ,
2408 };
9611b797 2409 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
489468fe
SC
2410 {
2411 sizeof( CGContextRef ) ,
2412 };
9611b797 2413 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
489468fe
SC
2414 {
2415 &m_cgContext ,
2416 };
9611b797 2417 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
489468fe
SC
2418 atsuTags, atsuSizes, atsuValues );
2419 }
2420
2421 ATSUTextMeasurement textBefore, textAfter;
2422 ATSUTextMeasurement ascent, descent;
2423
2424 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2425 &textBefore , &textAfter, &ascent , &descent );
2426
2427 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2428
2429 Rect rect;
7dbda71e
SC
2430 x += (int)(sin(angle) * FixedToFloat(ascent));
2431 y += (int)(cos(angle) * FixedToFloat(ascent));
489468fe
SC
2432
2433 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2434 IntToFixed(x) , IntToFixed(y) , &rect );
2435 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2436
2437 CGContextSaveGState(m_cgContext);
2438 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2439 CGContextScaleCTM(m_cgContext, 1, -1);
2440 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2441 IntToFixed(0) , IntToFixed(0) );
2442
2443 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
2444
2445 CGContextRestoreGState(m_cgContext);
2446
2447 ::ATSUDisposeTextLayout(atsuLayout);
2448
2449 return;
2450 }
2451#endif
292e5e1f 2452#if wxOSX_USE_IPHONE
489468fe 2453 // default implementation takes care of rotation and calls non rotated DrawText afterwards
0b7dce54 2454 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
489468fe
SC
2455#endif
2456}
2457
2458void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
2459 wxDouble *descent, wxDouble *externalLeading ) const
2460{
1011fbeb 2461 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
489468fe
SC
2462
2463 if ( width )
2464 *width = 0;
2465 if ( height )
2466 *height = 0;
2467 if ( descent )
2468 *descent = 0;
2469 if ( externalLeading )
2470 *externalLeading = 0;
2471
2472 if (str.empty())
2473 return;
2474
292e5e1f 2475#if wxOSX_USE_CORE_TEXT
489468fe
SC
2476 if ( UMAGetSystemVersion() >= 0x1050 )
2477 {
2478 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
aa6208d9 2479 CTFontRef font = fref->OSXGetCTFont();
489468fe
SC
2480
2481 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2482 CFStringRef keys[] = { kCTFontAttributeName };
2483 CFTypeRef values[] = { font };
2484 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2485 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2486 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2487 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2488
7dbda71e
SC
2489 CGFloat a, d, l, w;
2490 w = CTLineGetTypographicBounds(line, &a, &d, &l);
489468fe
SC
2491
2492 if ( height )
2493 *height = a+d+l;
2494 if ( descent )
2495 *descent = d;
2496 if ( externalLeading )
2497 *externalLeading = l;
2498 if ( width )
2499 *width = w;
2500 return;
2501 }
2502#endif
292e5e1f 2503#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2504 {
2505 OSStatus status = noErr;
2506
2507 ATSUTextLayout atsuLayout;
2508 wxMacUniCharBuffer unibuf( str );
2509 UniCharCount chars = unibuf.GetChars();
2510
2511 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2512 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2513 &chars , &style , &atsuLayout );
2514
2515 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2516
2517 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2518 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2519
2520 ATSUTextMeasurement textBefore, textAfter;
2521 ATSUTextMeasurement textAscent, textDescent;
2522
2523 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2524 &textBefore , &textAfter, &textAscent , &textDescent );
2525
2526 if ( height )
7dbda71e 2527 *height = FixedToFloat(textAscent + textDescent);
489468fe 2528 if ( descent )
7dbda71e 2529 *descent = FixedToFloat(textDescent);
489468fe
SC
2530 if ( externalLeading )
2531 *externalLeading = 0;
2532 if ( width )
7dbda71e 2533 *width = FixedToFloat(textAfter - textBefore);
489468fe
SC
2534
2535 ::ATSUDisposeTextLayout(atsuLayout);
2536
2537 return;
2538 }
2539#endif
292e5e1f 2540#if wxOSX_USE_IPHONE
b2680ced
SC
2541 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2542
2543 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2544 CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() );
0b7dce54 2545
b2680ced
SC
2546 if ( height )
2547 *height = sz.height;
2548 /*
2549 if ( descent )
7dbda71e 2550 *descent = FixedToFloat(textDescent);
b2680ced
SC
2551 if ( externalLeading )
2552 *externalLeading = 0;
2553 */
2554 if ( width )
2555 *width = sz.width;
489468fe
SC
2556#endif
2557}
2558
2559void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
2560{
2561 widths.Empty();
2562 widths.Add(0, text.length());
2563
1011fbeb
SC
2564 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2565
489468fe
SC
2566 if (text.empty())
2567 return;
2568
292e5e1f 2569#if wxOSX_USE_CORE_TEXT
489468fe
SC
2570 {
2571 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
aa6208d9 2572 CTFontRef font = fref->OSXGetCTFont();
489468fe
SC
2573
2574 wxCFStringRef t(text, wxLocale::GetSystemEncoding() );
2575 CFStringRef keys[] = { kCTFontAttributeName };
2576 CFTypeRef values[] = { font };
2577 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2578 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2579 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) );
2580 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2581
2582 int chars = text.length();
2583 for ( int pos = 0; pos < (int)chars; pos ++ )
2584 {
0738b901 2585 widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL );
489468fe
SC
2586 }
2587
2588 return;
2589 }
2590#endif
292e5e1f 2591#if wxOSX_USE_ATSU_TEXT
489468fe
SC
2592 {
2593 OSStatus status = noErr;
2594 ATSUTextLayout atsuLayout;
2595 wxMacUniCharBuffer unibuf( text );
2596 UniCharCount chars = unibuf.GetChars();
2597
2598 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2599 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2600 &chars , &style , &atsuLayout );
2601
2602 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2603
2604 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2605 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2606
2607// new implementation from JS, keep old one just in case
2608#if 0
2609 for ( int pos = 0; pos < (int)chars; pos ++ )
2610 {
2611 unsigned long actualNumberOfBounds = 0;
2612 ATSTrapezoid glyphBounds;
2613
2614 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2615 OSStatus result;
2616 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2617 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2618 if (result != noErr || actualNumberOfBounds != 1 )
2619 return;
2620
7dbda71e 2621 widths[pos] = FixedToFloat( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
489468fe
SC
2622 //unsigned char uch = s[i];
2623 }
2624#else
2625 ATSLayoutRecord *layoutRecords = NULL;
2626 ItemCount glyphCount = 0;
0b7dce54 2627
489468fe
SC
2628 // Get the glyph extents
2629 OSStatus err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout,
2630 0,
2631 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2632 (void **)
2633 &layoutRecords,
2634 &glyphCount);
2635 wxASSERT(glyphCount == (text.length()+1));
0b7dce54 2636
489468fe
SC
2637 if ( err == noErr && glyphCount == (text.length()+1))
2638 {
2639 for ( int pos = 1; pos < (int)glyphCount ; pos ++ )
2640 {
7dbda71e 2641 widths[pos-1] = FixedToFloat( layoutRecords[pos].realPos );
489468fe
SC
2642 }
2643 }
0b7dce54 2644
489468fe
SC
2645 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL,
2646 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2647 (void **) &layoutRecords);
2648#endif
2649 ::ATSUDisposeTextLayout(atsuLayout);
2650 }
2651#endif
292e5e1f 2652#if wxOSX_USE_IPHONE
489468fe
SC
2653 // TODO core graphics text implementation here
2654#endif
2655}
2656
2657void * wxMacCoreGraphicsContext::GetNativeContext()
2658{
2659 return m_cgContext;
2660}
2661
2bc4cc1e
SC
2662
2663void wxMacCoreGraphicsContext::DrawRectangleX( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2664{
2665 if (m_composition == wxCOMPOSITION_DEST)
2666 return;
2667
2668 CGRect rect = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2669 if ( !m_brush.IsNull() )
2670 {
2671 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2672 CGContextFillRect(m_cgContext, rect);
2673 }
2674
2675 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2676 if ( !m_pen.IsNull() )
2677 {
2678 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2679 CGContextStrokeRect(m_cgContext, rect);
2680 }
2681}
2682
489468fe
SC
2683// concatenates this transform with the current transform of this context
2684void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
2685{
2686 if ( m_cgContext )
2687 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2688 else
362439b6 2689 m_windowTransform = CGAffineTransformConcat(*(CGAffineTransform*) matrix.GetNativeMatrix(), m_windowTransform);
489468fe
SC
2690}
2691
2692// sets the transform of this context
2693void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
2694{
2695 if ( m_cgContext )
2696 {
2697 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2698 transform = CGAffineTransformInvert( transform ) ;
2699 CGContextConcatCTM( m_cgContext, transform);
2700 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2701 }
2702 else
2703 {
2704 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
2705 }
2706}
2707
2708// gets the matrix of this context
2709wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
2710{
2711 wxGraphicsMatrix m = CreateMatrix();
2712 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
2713 CGContextGetCTM( m_cgContext ));
2714 return m;
2715}
2716
2717//
2718// Renderer
2719//
2720
2721//-----------------------------------------------------------------------------
2722// wxMacCoreGraphicsRenderer declaration
2723//-----------------------------------------------------------------------------
2724
2725class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
2726{
2727public :
2728 wxMacCoreGraphicsRenderer() {}
2729
2730 virtual ~wxMacCoreGraphicsRenderer() {}
2731
2732 // Context
2733
2734 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2735 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
9535b479 2736#if wxUSE_PRINTING_ARCHITECTURE
489468fe 2737 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
9535b479 2738#endif
489468fe
SC
2739
2740 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2741
2742 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2743
2744 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2745
2746 virtual wxGraphicsContext * CreateMeasuringContext();
2747
2748 // Path
2749
2750 virtual wxGraphicsPath CreatePath();
2751
2752 // Matrix
2753
2754 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2755 wxDouble tx=0.0, wxDouble ty=0.0);
2756
2757
2758 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2759
2760 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2761
4ee4c7b9
VZ
2762 virtual wxGraphicsBrush
2763 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2764 wxDouble x2, wxDouble y2,
2765 const wxGraphicsGradientStops& stops);
489468fe 2766
4ee4c7b9
VZ
2767 virtual wxGraphicsBrush
2768 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2769 wxDouble xc, wxDouble yc,
2770 wxDouble radius,
2771 const wxGraphicsGradientStops& stops);
489468fe
SC
2772
2773 // sets the font
2774 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2775
2776 // create a native bitmap representation
2777 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ;
2778
2986eb86
SC
2779 // create a graphics bitmap from a native bitmap
2780 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
2781
489468fe
SC
2782 // create a native bitmap representation
2783 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
2784private :
2785 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
2786} ;
2787
2788//-----------------------------------------------------------------------------
2789// wxMacCoreGraphicsRenderer implementation
2790//-----------------------------------------------------------------------------
2791
2792IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
2793
2794static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
2795
2796wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2797{
2798 return &gs_MacCoreGraphicsRenderer;
2799}
2800
489468fe
SC
2801wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc )
2802{
2803 const wxDCImpl* impl = dc.GetImpl();
2804 wxWindowDCImpl *win_impl = wxDynamicCast( impl, wxWindowDCImpl );
2805 if (win_impl)
2806 {
2807 int w, h;
2808 win_impl->GetSize( &w, &h );
2809 CGContextRef cgctx = 0;
15fc716c 2810
0600cf3a
KO
2811 wxASSERT_MSG(win_impl->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2812 if (win_impl->GetWindow())
2813 cgctx = (CGContextRef)(win_impl->GetWindow()->MacGetCGContextRef());
15fc716c 2814
10c08696
SC
2815 // having a cgctx being NULL is fine (will be created on demand)
2816 // this is the case for all wxWindowDCs except wxPaintDC
ad967c5b
SC
2817 wxMacCoreGraphicsContext *context =
2818 new wxMacCoreGraphicsContext( this, cgctx, (wxDouble) w, (wxDouble) h );
2819 context->EnableOffset(true);
2820 return context;
489468fe
SC
2821 }
2822 return NULL;
2823}
2824
2825wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC& dc )
2826{
2827#ifdef __WXMAC__
2828 const wxDCImpl* impl = dc.GetImpl();
2829 wxMemoryDCImpl *mem_impl = wxDynamicCast( impl, wxMemoryDCImpl );
2830 if (mem_impl)
2831 {
2832 int w, h;
2833 mem_impl->GetSize( &w, &h );
ad967c5b 2834 wxMacCoreGraphicsContext* context = new wxMacCoreGraphicsContext( this,
489468fe 2835 (CGContextRef)(mem_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
ad967c5b
SC
2836 context->EnableOffset(true);
2837 return context;
489468fe
SC
2838 }
2839#endif
2840 return NULL;
2841}
2842
9535b479 2843#if wxUSE_PRINTING_ARCHITECTURE
489468fe
SC
2844wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC& dc )
2845{
2846#ifdef __WXMAC__
2847 const wxDCImpl* impl = dc.GetImpl();
2848 wxPrinterDCImpl *print_impl = wxDynamicCast( impl, wxPrinterDCImpl );
2849 if (print_impl)
2850 {
2851 int w, h;
2852 print_impl->GetSize( &w, &h );
2853 return new wxMacCoreGraphicsContext( this,
2854 (CGContextRef)(print_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2855 }
2856#endif
2857 return NULL;
2858}
9535b479 2859#endif
489468fe
SC
2860
2861wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
2862{
2863 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
2864}
2865
489468fe
SC
2866wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
2867{
b2680ced 2868#if wxOSX_USE_CARBON
ad967c5b
SC
2869 wxMacCoreGraphicsContext* context = new wxMacCoreGraphicsContext(this,(WindowRef)window);
2870 context->EnableOffset(true);
2871 return context;
b2680ced 2872#else
de0d2095 2873 wxUnusedVar(window);
b2680ced
SC
2874 return NULL;
2875#endif
489468fe
SC
2876}
2877
2878wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
2879{
2880 return new wxMacCoreGraphicsContext(this, window );
2881}
2882
2883wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2884{
2885 return new wxMacCoreGraphicsContext(this);
2886}
2887
2888// Path
2889
2890wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
2891{
2892 wxGraphicsPath m;
2893 m.SetRefData( new wxMacCoreGraphicsPathData(this));
2894 return m;
2895}
2896
2897
2898// Matrix
2899
2900wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2901 wxDouble tx, wxDouble ty)
2902{
2903 wxGraphicsMatrix m;
2904 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
2905 data->Set( a,b,c,d,tx,ty ) ;
2906 m.SetRefData(data);
2907 return m;
2908}
2909
2910wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
2911{
a1b806b9 2912 if ( !pen.IsOk() || pen.GetStyle() == wxTRANSPARENT )
489468fe
SC
2913 return wxNullGraphicsPen;
2914 else
2915 {
2916 wxGraphicsPen p;
2917 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
2918 return p;
2919 }
2920}
2921
2922wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
2923{
a1b806b9 2924 if ( !brush.IsOk() || brush.GetStyle() == wxTRANSPARENT )
489468fe
SC
2925 return wxNullGraphicsBrush;
2926 else
2927 {
2928 wxGraphicsBrush p;
2929 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
2930 return p;
2931 }
2932}
2933
2934wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap& bmp )
2935{
a1b806b9 2936 if ( bmp.IsOk() )
489468fe
SC
2937 {
2938 wxGraphicsBitmap p;
489468fe 2939 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp.CreateCGImage(), bmp.GetDepth() == 1 ) );
2986eb86
SC
2940 return p;
2941 }
2942 else
2943 return wxNullGraphicsBitmap;
2944}
2945
2946wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
2947{
2948 if ( bitmap != NULL )
2949 {
2950 wxGraphicsBitmap p;
2951 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef) bitmap, false ));
489468fe
SC
2952 return p;
2953 }
2954 else
2955 return wxNullGraphicsBitmap;
2956}
2957
2958wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2959{
2960 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2961 CGImageRef img = refdata->GetBitmap();
2962 if ( img )
2963 {
2964 wxGraphicsBitmap p;
2965 CGImageRef subimg = CGImageCreateWithImageInRect(img,CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ));
2966 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg, refdata->IsMonochrome() ) );
2967 return p;
2968 }
2969 else
2970 return wxNullGraphicsBitmap;
2971}
2972
4ee4c7b9
VZ
2973wxGraphicsBrush
2974wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2975 wxDouble x2, wxDouble y2,
2976 const wxGraphicsGradientStops& stops)
489468fe
SC
2977{
2978 wxGraphicsBrush p;
2979 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
4ee4c7b9 2980 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
489468fe
SC
2981 p.SetRefData(d);
2982 return p;
2983}
2984
4ee4c7b9
VZ
2985wxGraphicsBrush
2986wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2987 wxDouble xc, wxDouble yc,
2988 wxDouble radius,
2989 const wxGraphicsGradientStops& stops)
489468fe
SC
2990{
2991 wxGraphicsBrush p;
2992 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
4ee4c7b9 2993 d->CreateRadialGradientBrush(xo, yo, xc, yc, radius, stops);
489468fe
SC
2994 p.SetRefData(d);
2995 return p;
2996}
2997
2998// sets the font
2999wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
3000{
a1b806b9 3001 if ( font.IsOk() )
489468fe
SC
3002 {
3003 wxGraphicsFont p;
3004 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
3005 return p;
3006 }
3007 else
3008 return wxNullGraphicsFont;
3009}
3010
3011//
3012// CoreGraphics Helper Methods
3013//
3014
3015// Data Providers and Consumers
3016
3017size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
3018{
3019 CFMutableDataRef data = (CFMutableDataRef) info;
3020 if ( data )
3021 {
3022 CFDataAppendBytes( data, (const UInt8*) bytes, count );
3023 }
3024 return count;
3025}
3026
3027void wxMacReleaseCFDataProviderCallback(void *info,
3028 const void *WXUNUSED(data),
3029 size_t WXUNUSED(count))
3030{
3031 if ( info )
3032 CFRelease( (CFDataRef) info );
3033}
3034
3035void wxMacReleaseCFDataConsumerCallback( void *info )
3036{
3037 if ( info )
3038 CFRelease( (CFDataRef) info );
3039}
3040
3041CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data )
3042{
3043 if ( data == NULL )
3044 return NULL;
3045
3046 return CGDataProviderCreateWithCFData( data );
3047}
3048
3049CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data )
3050{
3051 if ( data == NULL )
3052 return NULL;
3053
3054 return CGDataConsumerCreateWithCFData( data );
3055}
3056
3057void
3058wxMacReleaseMemoryBufferProviderCallback(void *info,
3059 const void * WXUNUSED_UNLESS_DEBUG(data),
3060 size_t WXUNUSED(size))
3061{
3062 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
3063
3064 wxASSERT( data == membuf->GetData() ) ;
3065
3066 delete membuf ;
3067}
3068
3069CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf )
3070{
3071 wxMemoryBuffer* b = new wxMemoryBuffer( buf );
3072 if ( b->GetDataLen() == 0 )
3073 return NULL;
3074
3075 return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() ,
3076 wxMacReleaseMemoryBufferProviderCallback );
3077}