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