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