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