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