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