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