]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/graphics.cpp
cleanup
[wxWidgets.git] / src / mac / carbon / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dccg.cpp
3 // Purpose: wxDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_GRAPHICS_CONTEXT && wxMAC_USE_CORE_GRAPHICS
15
16 #include "wx/graphics.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/dcclient.h"
20 #include "wx/dcmemory.h"
21 #include "wx/log.h"
22 #include "wx/region.h"
23 #include "wx/image.h"
24 #endif
25
26 #include "wx/mac/uma.h"
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 #include "wx/mac/private.h"
38
39 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
40 typedef float CGFloat;
41 #endif
42 #ifndef wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
43 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
44 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 1
45 #else
46 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
47 #endif
48 #endif
49
50 //-----------------------------------------------------------------------------
51 // constants
52 //-----------------------------------------------------------------------------
53
54 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
55 #ifndef M_PI
56 const double M_PI = 3.14159265358979;
57 #endif
58 #endif
59
60 static const double RAD2DEG = 180.0 / M_PI;
61
62 //
63 // Pen, Brushes and Fonts
64 //
65
66 #pragma mark -
67 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
68
69 // CGPattern wrapper class: always allocate on heap, never call destructor
70
71 class wxMacCoreGraphicsPattern
72 {
73 public :
74 wxMacCoreGraphicsPattern() {}
75
76 // is guaranteed to be called only with a non-Null CGContextRef
77 virtual void Render( CGContextRef ctxRef ) = 0;
78
79 operator CGPatternRef() const { return m_patternRef; }
80
81 protected :
82 virtual ~wxMacCoreGraphicsPattern()
83 {
84 // as this is called only when the m_patternRef is been released;
85 // don't release it again
86 }
87
88 static void _Render( void *info, CGContextRef ctxRef )
89 {
90 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
91 if ( self && ctxRef )
92 self->Render( ctxRef );
93 }
94
95 static void _Dispose( void *info )
96 {
97 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
98 delete self;
99 }
100
101 CGPatternRef m_patternRef;
102
103 static const CGPatternCallbacks ms_Callbacks;
104 };
105
106 const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose };
107
108 class ImagePattern : public wxMacCoreGraphicsPattern
109 {
110 public :
111 ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform )
112 {
113 wxASSERT( bmp && bmp->Ok() );
114
115 Init( (CGImageRef) bmp->CGImageCreate() , transform );
116 }
117
118 // ImagePattern takes ownership of CGImageRef passed in
119 ImagePattern( CGImageRef image , const CGAffineTransform& transform )
120 {
121 if ( image )
122 CFRetain( image );
123
124 Init( image , transform );
125 }
126
127 virtual void Render( CGContextRef ctxRef )
128 {
129 if (m_image != NULL)
130 HIViewDrawCGImage( ctxRef, &m_imageBounds, m_image );
131 }
132
133 protected :
134 void Init( CGImageRef image, const CGAffineTransform& transform )
135 {
136 m_image = image;
137 if ( m_image )
138 {
139 m_imageBounds = CGRectMake( 0.0, 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) );
140 m_patternRef = CGPatternCreate(
141 this , m_imageBounds, transform ,
142 m_imageBounds.size.width, m_imageBounds.size.height,
143 kCGPatternTilingNoDistortion, true , &wxMacCoreGraphicsPattern::ms_Callbacks );
144 }
145 }
146
147 virtual ~ImagePattern()
148 {
149 if ( m_image )
150 CGImageRelease( m_image );
151 }
152
153 CGImageRef m_image;
154 CGRect m_imageBounds;
155 };
156
157 class HatchPattern : public wxMacCoreGraphicsPattern
158 {
159 public :
160 HatchPattern( int hatchstyle, const CGAffineTransform& transform )
161 {
162 m_hatch = hatchstyle;
163 m_imageBounds = CGRectMake( 0.0, 0.0, 8.0 , 8.0 );
164 m_patternRef = CGPatternCreate(
165 this , m_imageBounds, transform ,
166 m_imageBounds.size.width, m_imageBounds.size.height,
167 kCGPatternTilingNoDistortion, false , &wxMacCoreGraphicsPattern::ms_Callbacks );
168 }
169
170 void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
171 {
172 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
173 if ( CGContextStrokeLineSegments!=NULL )
174 {
175 CGContextStrokeLineSegments( ctxRef , pts , count );
176 }
177 else
178 #endif
179 {
180 CGContextBeginPath( ctxRef );
181 for (size_t i = 0; i < count; i += 2)
182 {
183 CGContextMoveToPoint(ctxRef, pts[i].x, pts[i].y);
184 CGContextAddLineToPoint(ctxRef, pts[i+1].x, pts[i+1].y);
185 }
186 CGContextStrokePath(ctxRef);
187 }
188 }
189
190 virtual void Render( CGContextRef ctxRef )
191 {
192 switch ( m_hatch )
193 {
194 case wxBDIAGONAL_HATCH :
195 {
196 CGPoint pts[] =
197 {
198 { 8.0 , 0.0 } , { 0.0 , 8.0 }
199 };
200 StrokeLineSegments( ctxRef , pts , 2 );
201 }
202 break;
203
204 case wxCROSSDIAG_HATCH :
205 {
206 CGPoint pts[] =
207 {
208 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
209 { 8.0 , 0.0 } , { 0.0 , 8.0 }
210 };
211 StrokeLineSegments( ctxRef , pts , 4 );
212 }
213 break;
214
215 case wxFDIAGONAL_HATCH :
216 {
217 CGPoint pts[] =
218 {
219 { 0.0 , 0.0 } , { 8.0 , 8.0 }
220 };
221 StrokeLineSegments( ctxRef , pts , 2 );
222 }
223 break;
224
225 case wxCROSS_HATCH :
226 {
227 CGPoint pts[] =
228 {
229 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
230 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
231 };
232 StrokeLineSegments( ctxRef , pts , 4 );
233 }
234 break;
235
236 case wxHORIZONTAL_HATCH :
237 {
238 CGPoint pts[] =
239 {
240 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
241 };
242 StrokeLineSegments( ctxRef , pts , 2 );
243 }
244 break;
245
246 case wxVERTICAL_HATCH :
247 {
248 CGPoint pts[] =
249 {
250 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
251 };
252 StrokeLineSegments( ctxRef , pts , 2 );
253 }
254 break;
255
256 default:
257 break;
258 }
259 }
260
261 protected :
262 virtual ~HatchPattern() {}
263
264 CGRect m_imageBounds;
265 int m_hatch;
266 };
267
268 class wxMacCoreGraphicsPenData : public wxGraphicsObjectRefData
269 {
270 public:
271 wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
272 ~wxMacCoreGraphicsPenData();
273
274 void Init();
275 virtual void Apply( wxGraphicsContext* context );
276 virtual wxDouble GetWidth() { return m_width; }
277
278 protected :
279 CGLineCap m_cap;
280 wxMacCFRefHolder<CGColorRef> m_color;
281 wxMacCFRefHolder<CGColorSpaceRef> m_colorSpace;
282
283 CGLineJoin m_join;
284 CGFloat m_width;
285
286 int m_count;
287 const CGFloat *m_lengths;
288 CGFloat *m_userLengths;
289
290
291 bool m_isPattern;
292 wxMacCFRefHolder<CGPatternRef> m_pattern;
293 CGFloat* m_patternColorComponents;
294 };
295
296 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) :
297 wxGraphicsObjectRefData( renderer )
298 {
299 Init();
300
301 CGFloat components[4] = { pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 ,
302 pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 } ;
303 m_color.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ) ;
304
305 // TODO: * m_dc->m_scaleX
306 m_width = pen.GetWidth();
307 if (m_width <= 0.0)
308 m_width = 0.1;
309
310 switch ( pen.GetCap() )
311 {
312 case wxCAP_ROUND :
313 m_cap = kCGLineCapRound;
314 break;
315
316 case wxCAP_PROJECTING :
317 m_cap = kCGLineCapSquare;
318 break;
319
320 case wxCAP_BUTT :
321 m_cap = kCGLineCapButt;
322 break;
323
324 default :
325 m_cap = kCGLineCapButt;
326 break;
327 }
328
329 switch ( pen.GetJoin() )
330 {
331 case wxJOIN_BEVEL :
332 m_join = kCGLineJoinBevel;
333 break;
334
335 case wxJOIN_MITER :
336 m_join = kCGLineJoinMiter;
337 break;
338
339 case wxJOIN_ROUND :
340 m_join = kCGLineJoinRound;
341 break;
342
343 default :
344 m_join = kCGLineJoinMiter;
345 break;
346 }
347
348 const CGFloat dashUnit = m_width < 1.0 ? 1.0 : m_width;
349
350 const CGFloat dotted[] = { dashUnit , dashUnit + 2.0 };
351 static const CGFloat short_dashed[] = { 9.0 , 6.0 };
352 static const CGFloat dashed[] = { 19.0 , 9.0 };
353 static const CGFloat dotted_dashed[] = { 9.0 , 6.0 , 3.0 , 3.0 };
354
355 switch ( pen.GetStyle() )
356 {
357 case wxSOLID :
358 break;
359
360 case wxDOT :
361 m_count = WXSIZEOF(dotted);
362 m_userLengths = new CGFloat[ m_count ] ;
363 memcpy( m_userLengths, dotted, sizeof(dotted) );
364 m_lengths = m_userLengths;
365 break;
366
367 case wxLONG_DASH :
368 m_count = WXSIZEOF(dashed);
369 m_lengths = dashed;
370 break;
371
372 case wxSHORT_DASH :
373 m_count = WXSIZEOF(short_dashed);
374 m_lengths = short_dashed;
375 break;
376
377 case wxDOT_DASH :
378 m_count = WXSIZEOF(dotted_dashed);
379 m_lengths = dotted_dashed;
380 break;
381
382 case wxUSER_DASH :
383 wxDash *dashes;
384 m_count = pen.GetDashes( &dashes );
385 if ((dashes != NULL) && (m_count > 0))
386 {
387 m_userLengths = new CGFloat[m_count];
388 for ( int i = 0; i < m_count; ++i )
389 {
390 m_userLengths[i] = dashes[i] * dashUnit;
391
392 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
393 m_userLengths[i] = dashUnit + 2.0;
394 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
395 m_userLengths[i] = dashUnit;
396 }
397 }
398 m_lengths = m_userLengths;
399 break;
400
401 case wxSTIPPLE :
402 {
403 wxBitmap* bmp = pen.GetStipple();
404 if ( bmp && bmp->Ok() )
405 {
406 m_colorSpace.Set( CGColorSpaceCreatePattern( NULL ) );
407 m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );
408 m_patternColorComponents = new CGFloat[1] ;
409 m_patternColorComponents[0] = 1.0;
410 m_isPattern = true;
411 }
412 }
413 break;
414
415 default :
416 {
417 m_isPattern = true;
418 m_colorSpace.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
419 m_pattern.Set( *( new HatchPattern( pen.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
420 m_patternColorComponents = new CGFloat[4] ;
421 m_patternColorComponents[0] = pen.GetColour().Red() / 255.0;
422 m_patternColorComponents[1] = pen.GetColour().Green() / 255.0;
423 m_patternColorComponents[2] = pen.GetColour().Blue() / 255.0;
424 m_patternColorComponents[3] = pen.GetColour().Alpha() / 255.0;
425 }
426 break;
427 }
428 if ((m_lengths != NULL) && (m_count > 0))
429 {
430 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
431 m_cap = kCGLineCapButt;
432 }
433 }
434
435 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
436 {
437 delete[] m_userLengths;
438 delete[] m_patternColorComponents;
439 }
440
441 void wxMacCoreGraphicsPenData::Init()
442 {
443 m_lengths = NULL;
444 m_userLengths = NULL;
445 m_width = 0;
446 m_count = 0;
447 m_patternColorComponents = NULL;
448 m_isPattern = false;
449 }
450
451 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext* context )
452 {
453 CGContextRef cg = (CGContextRef) context->GetNativeContext();
454 CGContextSetLineWidth( cg , m_width );
455 CGContextSetLineJoin( cg , m_join );
456
457 CGContextSetLineDash( cg , 0 , m_lengths , m_count );
458 CGContextSetLineCap( cg , m_cap );
459
460 if ( m_isPattern )
461 {
462 CGAffineTransform matrix = CGContextGetCTM( cg );
463 CGContextSetPatternPhase( cg, CGSizeMake(matrix.tx, matrix.ty) );
464 CGContextSetStrokeColorSpace( cg , m_colorSpace );
465 CGContextSetStrokePattern( cg, m_pattern , m_patternColorComponents );
466 }
467 else
468 {
469 if ( context->GetLogicalFunction() == wxINVERT || context->GetLogicalFunction() == wxXOR )
470 {
471 CGContextSetRGBStrokeColor( cg , 1.0, 1.0 , 1.0, 1.0 );
472 }
473 else
474 CGContextSetStrokeColorWithColor( cg , m_color );
475 }
476 }
477
478 //
479 // Brush
480 //
481
482 static const char *gs_stripedback_xpm[] = {
483 /* columns rows colors chars-per-pixel */
484 "4 4 2 1",
485 ". c #F0F0F0",
486 "X c #ECECEC",
487 /* pixels */
488 "....",
489 "....",
490 "XXXX",
491 "XXXX"
492 };
493
494 wxBitmap gs_stripedback_bmp( wxImage( (const char* const* ) gs_stripedback_xpm ), -1 ) ;
495
496 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
497 {
498 delete[] m_patternColorComponents;
499 }
500
501 void wxMacCoreGraphicsColour::Init()
502 {
503 m_isPattern = false;
504 m_patternColorComponents = NULL;
505 }
506
507 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext )
508 {
509 if ( m_isPattern )
510 {
511 CGAffineTransform matrix = CGContextGetCTM( cgContext );
512 CGContextSetPatternPhase( cgContext, CGSizeMake(matrix.tx, matrix.ty) );
513 CGContextSetFillColorSpace( cgContext , m_colorSpace );
514 CGContextSetFillPattern( cgContext, m_pattern , m_patternColorComponents );
515 }
516 else
517 {
518 CGContextSetFillColorWithColor( cgContext, m_color );
519 }
520 }
521
522 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
523 {
524 Init();
525 }
526
527 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush &brush )
528 {
529 Init();
530 if ( brush.GetStyle() == wxSOLID )
531 {
532 if ( brush.MacGetBrushKind() == kwxMacBrushTheme )
533 {
534 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
535 if ( UMAGetSystemVersion() >= 0x1040 )
536 {
537 CGColorRef color ;
538 HIThemeBrushCreateCGColor( brush.MacGetTheme(), &color );
539 m_color.Set( color ) ;
540 }
541 else
542 #endif
543 {
544 if( brush.MacGetTheme() == kThemeBrushDialogBackgroundActive )
545 {
546 // striped background is a pattern, we have to emulate it
547
548 m_isPattern = true;
549 m_patternColorComponents = new CGFloat[1] ;
550 m_patternColorComponents[0] = 1.0;
551 m_colorSpace.Set( CGColorSpaceCreatePattern( NULL ) );
552 m_pattern.Set( *( new ImagePattern( &gs_stripedback_bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );
553 }
554 else
555 {
556 // as close as we can get, unfortunately < 10.4 things get difficult
557 RGBColor color;
558 GetThemeBrushAsColor( brush.MacGetTheme(), 32, true, &color );
559 CGFloat components[4] = { (CGFloat) color.red / 65536,
560 (CGFloat) color.green / 65536, (CGFloat) color.blue / 65536, 1 } ;
561 m_color.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ) ;
562 }
563 }
564 }
565 else
566 {
567 CGFloat components[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
568 brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 } ;
569 m_color.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ) ;
570 }
571 }
572 else if ( brush.IsHatch() )
573 {
574 m_isPattern = true;
575 m_colorSpace.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
576 m_pattern.Set( *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
577
578 m_patternColorComponents = new CGFloat[4] ;
579 m_patternColorComponents[0] = brush.GetColour().Red() / 255.0;
580 m_patternColorComponents[1] = brush.GetColour().Green() / 255.0;
581 m_patternColorComponents[2] = brush.GetColour().Blue() / 255.0;
582 m_patternColorComponents[3] = brush.GetColour().Alpha() / 255.0;
583 }
584 else
585 {
586 // now brush is a bitmap
587 wxBitmap* bmp = brush.GetStipple();
588 if ( bmp && bmp->Ok() )
589 {
590 m_isPattern = true;
591 m_patternColorComponents = new CGFloat[1] ;
592 m_patternColorComponents[0] = 1.0;
593 m_colorSpace.Set( CGColorSpaceCreatePattern( NULL ) );
594 m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );
595 }
596 }
597 }
598
599 class wxMacCoreGraphicsBrushData : public wxGraphicsObjectRefData
600 {
601 public:
602 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer );
603 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
604 ~wxMacCoreGraphicsBrushData ();
605
606 virtual void Apply( wxGraphicsContext* context );
607 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
608 const wxColour&c1, const wxColour&c2 );
609 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
610 const wxColour &oColor, const wxColour &cColor );
611
612 virtual bool IsShading() { return m_isShading; }
613 CGShadingRef GetShading() { return m_shading; }
614 protected:
615 CGFunctionRef CreateGradientFunction( const wxColour& c1, const wxColour& c2 );
616 static void CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out);
617 virtual void Init();
618
619 wxMacCoreGraphicsColour m_cgColor;
620
621 bool m_isShading;
622 CGFunctionRef m_gradientFunction;
623 CGShadingRef m_shading;
624 CGFloat *m_gradientComponents;
625 };
626
627 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxGraphicsObjectRefData( renderer )
628 {
629 Init();
630 }
631
632 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
633 const wxColour&c1, const wxColour&c2 )
634 {
635 m_gradientFunction = CreateGradientFunction( c1, c2 );
636 m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1,y1), CGPointMake(x2,y2), m_gradientFunction, true, true ) ;
637 m_isShading = true ;
638 }
639
640 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
641 const wxColour &oColor, const wxColour &cColor )
642 {
643 m_gradientFunction = CreateGradientFunction( oColor, cColor );
644 m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo,yo), 0, CGPointMake(xc,yc), radius, m_gradientFunction, true, true ) ;
645 m_isShading = true ;
646 }
647
648 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxGraphicsObjectRefData( renderer ),
649 m_cgColor( brush )
650 {
651 Init();
652
653 }
654
655 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
656 {
657 if ( m_shading )
658 CGShadingRelease(m_shading);
659
660 if( m_gradientFunction )
661 CGFunctionRelease(m_gradientFunction);
662
663 delete[] m_gradientComponents;
664 }
665
666 void wxMacCoreGraphicsBrushData::Init()
667 {
668 m_gradientFunction = NULL;
669 m_shading = NULL;
670 m_gradientComponents = NULL;
671 m_isShading = false;
672 }
673
674 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context )
675 {
676 CGContextRef cg = (CGContextRef) context->GetNativeContext();
677
678 if ( m_isShading )
679 {
680 // nothing to set as shades are processed by clipping using the path and filling
681 }
682 else
683 {
684 m_cgColor.Apply( cg );
685 }
686 }
687
688 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out)
689 {
690 CGFloat* colors = (CGFloat*) info ;
691 CGFloat f = *in;
692 for( int i = 0 ; i < 4 ; ++i )
693 {
694 out[i] = colors[i] + ( colors[4+i] - colors[i] ) * f;
695 }
696 }
697
698 CGFunctionRef wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour& c1, const wxColour& c2 )
699 {
700 static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, NULL };
701 static const CGFloat input_value_range [2] = { 0, 1 };
702 static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
703 m_gradientComponents = new CGFloat[8] ;
704 m_gradientComponents[0] = c1.Red() / 255.0;
705 m_gradientComponents[1] = c1.Green() / 255.0;
706 m_gradientComponents[2] = c1.Blue() / 255.0;
707 m_gradientComponents[3] = c1.Alpha() / 255.0;
708 m_gradientComponents[4] = c2.Red() / 255.0;
709 m_gradientComponents[5] = c2.Green() / 255.0;
710 m_gradientComponents[6] = c2.Blue() / 255.0;
711 m_gradientComponents[7] = c2.Alpha() / 255.0;
712
713 return CGFunctionCreate ( m_gradientComponents, 1,
714 input_value_range,
715 4,
716 output_value_ranges,
717 &callbacks);
718 }
719
720 //
721 // Font
722 //
723
724 class wxMacCoreGraphicsFontData : public wxGraphicsObjectRefData
725 {
726 public:
727 wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
728 ~wxMacCoreGraphicsFontData();
729
730 virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; }
731 private :
732 ATSUStyle m_macATSUIStyle;
733 };
734
735 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer )
736 {
737 m_macATSUIStyle = NULL;
738
739 OSStatus status;
740
741 status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle );
742
743 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );
744
745 // we need the scale here ...
746
747 Fixed atsuSize = IntToFixed( int( 1 * font.MacGetFontSize()) );
748 RGBColor atsuColor = MAC_WXCOLORREF( col.GetPixel() );
749 ATSUAttributeTag atsuTags[] =
750 {
751 kATSUSizeTag ,
752 kATSUColorTag ,
753 };
754 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
755 {
756 sizeof( Fixed ) ,
757 sizeof( RGBColor ) ,
758 };
759 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
760 {
761 &atsuSize ,
762 &atsuColor ,
763 };
764
765 status = ::ATSUSetAttributes(
766 m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
767 atsuTags, atsuSizes, atsuValues);
768
769 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
770 }
771
772 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
773 {
774 if ( m_macATSUIStyle )
775 {
776 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
777 m_macATSUIStyle = NULL;
778 }
779 }
780
781 //
782 // Graphics Matrix
783 //
784
785 //-----------------------------------------------------------------------------
786 // wxMacCoreGraphicsMatrix declaration
787 //-----------------------------------------------------------------------------
788
789 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData
790 {
791 public :
792 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ;
793
794 virtual ~wxMacCoreGraphicsMatrixData() ;
795
796 virtual wxGraphicsObjectRefData *Clone() const ;
797
798 // concatenates the matrix
799 virtual void Concat( const wxGraphicsMatrixData *t );
800
801 // sets the matrix to the respective values
802 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
803 wxDouble tx=0.0, wxDouble ty=0.0);
804
805 // gets the component valuess of the matrix
806 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
807 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
808
809 // makes this the inverse matrix
810 virtual void Invert();
811
812 // returns true if the elements of the transformation matrix are equal ?
813 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
814
815 // return true if this is the identity matrix
816 virtual bool IsIdentity() const;
817
818 //
819 // transformation
820 //
821
822 // add the translation to this matrix
823 virtual void Translate( wxDouble dx , wxDouble dy );
824
825 // add the scale to this matrix
826 virtual void Scale( wxDouble xScale , wxDouble yScale );
827
828 // add the rotation to this matrix (radians)
829 virtual void Rotate( wxDouble angle );
830
831 //
832 // apply the transforms
833 //
834
835 // applies that matrix to the point
836 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
837
838 // applies the matrix except for translations
839 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
840
841 // returns the native representation
842 virtual void * GetNativeMatrix() const;
843
844 private :
845 CGAffineTransform m_matrix;
846 } ;
847
848 //-----------------------------------------------------------------------------
849 // wxMacCoreGraphicsMatrix implementation
850 //-----------------------------------------------------------------------------
851
852 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer)
853 {
854 }
855
856 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
857 {
858 }
859
860 wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const
861 {
862 wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
863 m->m_matrix = m_matrix ;
864 return m;
865 }
866
867 // concatenates the matrix
868 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t )
869 {
870 m_matrix = CGAffineTransformConcat(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()) );
871 }
872
873 // sets the matrix to the respective values
874 void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
875 wxDouble tx, wxDouble ty)
876 {
877 m_matrix = CGAffineTransformMake(a,b,c,d,tx,ty);
878 }
879
880 // gets the component valuess of the matrix
881 void wxMacCoreGraphicsMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
882 wxDouble* d, wxDouble* tx, wxDouble* ty) const
883 {
884 if (a) *a = m_matrix.a;
885 if (b) *b = m_matrix.b;
886 if (c) *c = m_matrix.c;
887 if (d) *d = m_matrix.d;
888 if (tx) *tx= m_matrix.tx;
889 if (ty) *ty= m_matrix.ty;
890 }
891
892 // makes this the inverse matrix
893 void wxMacCoreGraphicsMatrixData::Invert()
894 {
895 m_matrix = CGAffineTransformInvert( m_matrix );
896 }
897
898 // returns true if the elements of the transformation matrix are equal ?
899 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
900 {
901 const CGAffineTransform* tm = (CGAffineTransform*) t->GetNativeMatrix();
902 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
903 if ( CGAffineTransformEqualToTransform!=NULL )
904 {
905 return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()));
906 }
907 else
908 #endif
909 {
910 return (
911 m_matrix.a == tm->a &&
912 m_matrix.b == tm->b &&
913 m_matrix.c == tm->c &&
914 m_matrix.d == tm->d &&
915 m_matrix.tx == tm->tx &&
916 m_matrix.ty == tm->ty ) ;
917 }
918 }
919
920 // return true if this is the identity matrix
921 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
922 {
923 return ( m_matrix.a == 1 && m_matrix.d == 1 &&
924 m_matrix.b == 0 && m_matrix.d == 0 && m_matrix.tx == 0 && m_matrix.ty == 0);
925 }
926
927 //
928 // transformation
929 //
930
931 // add the translation to this matrix
932 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx , wxDouble dy )
933 {
934 m_matrix = CGAffineTransformTranslate( m_matrix, dx, dy);
935 }
936
937 // add the scale to this matrix
938 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale , wxDouble yScale )
939 {
940 m_matrix = CGAffineTransformScale( m_matrix, xScale, yScale);
941 }
942
943 // add the rotation to this matrix (radians)
944 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle )
945 {
946 m_matrix = CGAffineTransformRotate( m_matrix, angle);
947 }
948
949 //
950 // apply the transforms
951 //
952
953 // applies that matrix to the point
954 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
955 {
956 CGPoint pt = CGPointApplyAffineTransform( CGPointMake(*x,*y), m_matrix);
957
958 *x = pt.x;
959 *y = pt.y;
960 }
961
962 // applies the matrix except for translations
963 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
964 {
965 CGSize sz = CGSizeApplyAffineTransform( CGSizeMake(*dx,*dy) , m_matrix );
966 *dx = sz.width;
967 *dy = sz.height;
968 }
969
970 // returns the native representation
971 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
972 {
973 return (void*) &m_matrix;
974 }
975
976 //
977 // Graphics Path
978 //
979
980 //-----------------------------------------------------------------------------
981 // wxMacCoreGraphicsPath declaration
982 //-----------------------------------------------------------------------------
983
984 class WXDLLEXPORT wxMacCoreGraphicsPathData : public wxGraphicsPathData
985 {
986 public :
987 wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path = NULL);
988
989 ~wxMacCoreGraphicsPathData();
990
991 virtual wxGraphicsObjectRefData *Clone() const;
992
993 // begins a new subpath at (x,y)
994 virtual void MoveToPoint( wxDouble x, wxDouble y );
995
996 // adds a straight line from the current point to (x,y)
997 virtual void AddLineToPoint( wxDouble x, wxDouble y );
998
999 // adds a cubic Bezier curve from the current point, using two control points and an end point
1000 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
1001
1002 // closes the current sub-path
1003 virtual void CloseSubpath();
1004
1005 // gets the last point of the current path, (0,0) if not yet set
1006 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
1007
1008 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1009 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
1010
1011 //
1012 // These are convenience functions which - if not available natively will be assembled
1013 // using the primitives from above
1014 //
1015
1016 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1017 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
1018
1019 // appends a rectangle as a new closed subpath
1020 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1021
1022 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1023 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
1024
1025 // 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)
1026 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
1027
1028 // adds another path
1029 virtual void AddPath( const wxGraphicsPathData* path );
1030
1031 // returns the native path
1032 virtual void * GetNativePath() const { return m_path; }
1033
1034 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1035 virtual void UnGetNativePath(void *WXUNUSED(p)) const {}
1036
1037 // transforms each point of this path by the matrix
1038 virtual void Transform( const wxGraphicsMatrixData* matrix );
1039
1040 // gets the bounding box enclosing all points (possibly including control points)
1041 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *y) const;
1042
1043 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxODDEVEN_RULE) const;
1044 private :
1045 CGMutablePathRef m_path;
1046 };
1047
1048 //-----------------------------------------------------------------------------
1049 // wxMacCoreGraphicsPath implementation
1050 //-----------------------------------------------------------------------------
1051
1052 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path) : wxGraphicsPathData(renderer)
1053 {
1054 if ( path )
1055 m_path = path;
1056 else
1057 m_path = CGPathCreateMutable();
1058 }
1059
1060 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1061 {
1062 CGPathRelease( m_path );
1063 }
1064
1065 wxGraphicsObjectRefData* wxMacCoreGraphicsPathData::Clone() const
1066 {
1067 wxMacCoreGraphicsPathData* clone = new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path));
1068 return clone ;
1069 }
1070
1071
1072 // opens (starts) a new subpath
1073 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1 , wxDouble y1 )
1074 {
1075 CGPathMoveToPoint( m_path , NULL , x1 , y1 );
1076 }
1077
1078 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1 , wxDouble y1 )
1079 {
1080 CGPathAddLineToPoint( m_path , NULL , x1 , y1 );
1081 }
1082
1083 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
1084 {
1085 CGPathAddCurveToPoint( m_path , NULL , cx1 , cy1 , cx2, cy2, x , y );
1086 }
1087
1088 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
1089 {
1090 CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x , y );
1091 }
1092
1093 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1094 {
1095 CGRect cgRect = { { x , y } , { w , h } };
1096 CGPathAddRect( m_path , NULL , cgRect );
1097 }
1098
1099 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x, wxDouble y , wxDouble r )
1100 {
1101 CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true );
1102 }
1103
1104 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1105 void wxMacCoreGraphicsPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
1106 {
1107 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1108 CGPathAddArc( m_path, NULL , x, y, r, startAngle, endAngle, !clockwise);
1109 }
1110
1111 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
1112 {
1113 CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
1114 }
1115
1116 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData* path )
1117 {
1118 CGPathAddPath( m_path , NULL, (CGPathRef) path->GetNativePath() );
1119 }
1120
1121 // closes the current subpath
1122 void wxMacCoreGraphicsPathData::CloseSubpath()
1123 {
1124 CGPathCloseSubpath( m_path );
1125 }
1126
1127 // gets the last point of the current path, (0,0) if not yet set
1128 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
1129 {
1130 CGPoint p = CGPathGetCurrentPoint( m_path );
1131 *x = p.x;
1132 *y = p.y;
1133 }
1134
1135 // transforms each point of this path by the matrix
1136 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData* matrix )
1137 {
1138 CGMutablePathRef p = CGPathCreateMutable() ;
1139 CGPathAddPath( p, (CGAffineTransform*) matrix->GetNativeMatrix() , m_path );
1140 CGPathRelease( m_path );
1141 m_path = p;
1142 }
1143
1144 // gets the bounding box enclosing all points (possibly including control points)
1145 void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
1146 {
1147 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1148 *x = bounds.origin.x;
1149 *y = bounds.origin.y;
1150 *w = bounds.size.width;
1151 *h = bounds.size.height;
1152 }
1153
1154 bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, int fillStyle) const
1155 {
1156 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1157 if ( CGPathContainsPoint!=NULL )
1158 {
1159 return CGPathContainsPoint( m_path, NULL, CGPointMake(x,y), fillStyle == wxODDEVEN_RULE );
1160 }
1161 else
1162 #endif
1163 {
1164 // TODO : implementation for 10.3
1165 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1166 return CGRectContainsPoint( bounds, CGPointMake(x,y) ) == 1;
1167 }
1168 }
1169
1170 //
1171 // Graphics Context
1172 //
1173
1174 //-----------------------------------------------------------------------------
1175 // wxMacCoreGraphicsContext declaration
1176 //-----------------------------------------------------------------------------
1177
1178 class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
1179 {
1180 public:
1181 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width = 0, wxDouble height = 0 );
1182
1183 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window );
1184
1185 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window );
1186
1187 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer);
1188
1189 wxMacCoreGraphicsContext();
1190
1191 ~wxMacCoreGraphicsContext();
1192
1193 void Init();
1194
1195 // returns the size of the graphics context in device coordinates
1196 virtual void GetSize( wxDouble* width, wxDouble* height);
1197
1198 virtual void StartPage( wxDouble width, wxDouble height );
1199
1200 virtual void EndPage();
1201
1202 virtual void Flush();
1203
1204 // push the current state of the context, ie the transformation matrix on a stack
1205 virtual void PushState();
1206
1207 // pops a stored state from the stack
1208 virtual void PopState();
1209
1210 // clips drawings to the region
1211 virtual void Clip( const wxRegion &region );
1212
1213 // clips drawings to the rect
1214 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1215
1216 // resets the clipping to original extent
1217 virtual void ResetClip();
1218
1219 virtual void * GetNativeContext();
1220
1221 bool SetLogicalFunction( int function );
1222 //
1223 // transformation
1224 //
1225
1226 // translate
1227 virtual void Translate( wxDouble dx , wxDouble dy );
1228
1229 // scale
1230 virtual void Scale( wxDouble xScale , wxDouble yScale );
1231
1232 // rotate (radians)
1233 virtual void Rotate( wxDouble angle );
1234
1235 // concatenates this transform with the current transform of this context
1236 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
1237
1238 // sets the transform of this context
1239 virtual void SetTransform( const wxGraphicsMatrix& matrix );
1240
1241 // gets the matrix of this context
1242 virtual wxGraphicsMatrix GetTransform() const;
1243 //
1244 // setting the paint
1245 //
1246
1247 // strokes along a path with the current pen
1248 virtual void StrokePath( const wxGraphicsPath &path );
1249
1250 // fills a path with the current brush
1251 virtual void FillPath( const wxGraphicsPath &path, int fillStyle = wxODDEVEN_RULE );
1252
1253 // draws a path by first filling and then stroking
1254 virtual void DrawPath( const wxGraphicsPath &path, int fillStyle = wxODDEVEN_RULE );
1255
1256 virtual bool ShouldOffset() const
1257 {
1258 int penwidth = 0 ;
1259 if ( !m_pen.IsNull() )
1260 {
1261 penwidth = (int)((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->GetWidth();
1262 if ( penwidth == 0 )
1263 penwidth = 1;
1264 }
1265 return ( penwidth % 2 ) == 1;
1266 }
1267 //
1268 // text
1269 //
1270
1271 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y );
1272
1273 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
1274
1275 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
1276 wxDouble *descent, wxDouble *externalLeading ) const;
1277
1278 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
1279
1280 //
1281 // image support
1282 //
1283
1284 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1285
1286 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1287
1288 void SetNativeContext( CGContextRef cg );
1289
1290 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext)
1291 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext)
1292
1293 private:
1294 void EnsureIsValid();
1295
1296 CGContextRef m_cgContext;
1297 WindowRef m_windowRef;
1298 bool m_releaseContext;
1299 CGAffineTransform m_windowTransform;
1300 wxDouble m_width;
1301 wxDouble m_height;
1302
1303 wxMacCFRefHolder<HIShapeRef> m_clipRgn;
1304 };
1305
1306 //-----------------------------------------------------------------------------
1307 // device context implementation
1308 //
1309 // more and more of the dc functionality should be implemented by calling
1310 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1311 // also coordinate conversions should be moved to native matrix ops
1312 //-----------------------------------------------------------------------------
1313
1314 // we always stock two context states, one at entry, to be able to preserve the
1315 // state we were called with, the other one after changing to HI Graphics orientation
1316 // (this one is used for getting back clippings etc)
1317
1318 //-----------------------------------------------------------------------------
1319 // wxMacCoreGraphicsContext implementation
1320 //-----------------------------------------------------------------------------
1321
1322 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext)
1323
1324 class wxQuartzOffsetHelper
1325 {
1326 public :
1327 wxQuartzOffsetHelper( CGContextRef cg , bool offset )
1328 {
1329 m_cg = cg;
1330 m_offset = offset;
1331 if ( m_offset )
1332 CGContextTranslateCTM( m_cg, 0.5, 0.5 );
1333 }
1334 ~wxQuartzOffsetHelper( )
1335 {
1336 if ( m_offset )
1337 CGContextTranslateCTM( m_cg, -0.5, -0.5 );
1338 }
1339 public :
1340 CGContextRef m_cg;
1341 bool m_offset;
1342 } ;
1343
1344 void wxMacCoreGraphicsContext::Init()
1345 {
1346 m_cgContext = NULL;
1347 m_releaseContext = false;
1348 m_windowRef = NULL;
1349 m_width = 0;
1350 m_height = 0;
1351
1352 HIRect r = CGRectMake(0,0,0,0);
1353 m_clipRgn.Set(HIShapeCreateWithRect(&r));
1354 }
1355
1356 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width, wxDouble height ) : wxGraphicsContext(renderer)
1357 {
1358 Init();
1359 SetNativeContext(cgcontext);
1360 m_width = width;
1361 m_height = height;
1362 }
1363
1364 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer)
1365 {
1366 Init();
1367 m_windowRef = window;
1368 }
1369
1370 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer)
1371 {
1372 Init();
1373 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
1374 int originX , originY;
1375 originX = originY = 0;
1376 window->MacWindowToRootWindow( &originX , &originY );
1377 Rect bounds;
1378 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
1379
1380 m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top );
1381 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1382 m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ;
1383 }
1384
1385 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer)
1386 {
1387 Init();
1388 }
1389
1390 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL)
1391 {
1392 Init();
1393 wxLogDebug(wxT("Illegal Constructor called"));
1394 }
1395
1396 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1397 {
1398 SetNativeContext(NULL);
1399 }
1400
1401 void wxMacCoreGraphicsContext::GetSize( wxDouble* width, wxDouble* height)
1402 {
1403 *width = m_width;
1404 *height = m_height;
1405 }
1406
1407
1408 void wxMacCoreGraphicsContext::StartPage( wxDouble width, wxDouble height )
1409 {
1410 CGRect r;
1411 if ( width != 0 && height != 0)
1412 r = CGRectMake( 0 , 0 , width , height );
1413 else
1414 r = CGRectMake( 0 , 0 , m_width , m_height );
1415
1416 CGContextBeginPage(m_cgContext, &r );
1417 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1418 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1419 }
1420
1421 void wxMacCoreGraphicsContext::EndPage()
1422 {
1423 CGContextEndPage(m_cgContext);
1424 }
1425
1426 void wxMacCoreGraphicsContext::Flush()
1427 {
1428 CGContextFlush(m_cgContext);
1429 }
1430
1431 void wxMacCoreGraphicsContext::EnsureIsValid()
1432 {
1433 if ( !m_cgContext )
1434 {
1435 OSStatus status =
1436 #ifndef __LP64__
1437 QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
1438 #else
1439 paramErr;
1440 #endif
1441 wxASSERT_MSG( status == noErr , wxT("Cannot nest wxDCs on the same window") );
1442
1443 CGContextConcatCTM( m_cgContext, m_windowTransform );
1444 CGContextSaveGState( m_cgContext );
1445 m_releaseContext = true;
1446 if ( !HIShapeIsEmpty(m_clipRgn) )
1447 {
1448 // the clip region is in device coordinates, so we convert this again to user coordinates
1449 wxMacCFRefHolder<HIMutableShapeRef> hishape ;
1450 hishape.Set( HIShapeCreateMutableCopy( m_clipRgn ) );
1451 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero,m_windowTransform);
1452 HIShapeOffset( hishape, -transformedOrigin.x, -transformedOrigin.y );
1453 HIShapeReplacePathInCGContext( hishape, m_cgContext );
1454 CGContextClip( m_cgContext );
1455 }
1456 CGContextSaveGState( m_cgContext );
1457 }
1458 }
1459
1460 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1461
1462 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function )
1463 {
1464 if (m_logicalFunction == function)
1465 return true;
1466
1467 EnsureIsValid();
1468
1469 bool retval = false;
1470
1471 if ( function == wxCOPY )
1472 {
1473 retval = true;
1474 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1475 if ( CGContextSetBlendMode != NULL )
1476 {
1477 CGContextSetBlendMode( m_cgContext, kCGBlendModeNormal );
1478 CGContextSetShouldAntialias( m_cgContext, true );
1479 }
1480 #endif
1481 }
1482 else if ( function == wxINVERT || function == wxXOR )
1483 {
1484 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1485 if ( CGContextSetBlendMode != NULL )
1486 {
1487 // change color to white
1488 CGContextSetBlendMode( m_cgContext, kCGBlendModeExclusion );
1489 CGContextSetShouldAntialias( m_cgContext, false );
1490 retval = true;
1491 }
1492 #endif
1493 }
1494
1495 if (retval)
1496 m_logicalFunction = function;
1497 return retval ;
1498 }
1499
1500 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1501 {
1502 if( m_cgContext )
1503 {
1504 HIShapeRef shape = HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() );
1505 HIShapeReplacePathInCGContext( shape, m_cgContext );
1506 CGContextClip( m_cgContext );
1507 CFRelease( shape );
1508 }
1509 else
1510 {
1511 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1512 // to regions we try at least to have correct translations
1513 wxMacCFRefHolder<HIShapeRef> hishape ;
1514 hishape.Set( HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() ));
1515 HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( hishape );
1516
1517 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform );
1518 HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y );
1519 m_clipRgn.Set(mutableShape);
1520 }
1521 }
1522
1523 // clips drawings to the rect
1524 void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1525 {
1526 HIRect r = CGRectMake( x , y , w , h );
1527 if ( m_cgContext )
1528 {
1529 CGContextClipToRect( m_cgContext, r );
1530 }
1531 else
1532 {
1533 // the clipping itself must be stored as device coordinates, otherwise
1534 // we cannot apply it back correctly
1535 r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform );
1536 m_clipRgn.Set(HIShapeCreateWithRect(&r));
1537 }
1538 }
1539
1540 // resets the clipping to original extent
1541 void wxMacCoreGraphicsContext::ResetClip()
1542 {
1543 if ( m_cgContext )
1544 {
1545 // there is no way for clearing the clip, we can only revert to the stored
1546 // state, but then we have to make sure everything else is NOT restored
1547 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
1548 CGContextRestoreGState( m_cgContext );
1549 CGContextSaveGState( m_cgContext );
1550 CGAffineTransform transformNew = CGContextGetCTM( m_cgContext );
1551 transformNew = CGAffineTransformInvert( transformNew ) ;
1552 CGContextConcatCTM( m_cgContext, transformNew);
1553 CGContextConcatCTM( m_cgContext, transform);
1554 }
1555 else
1556 {
1557 HIRect r = CGRectMake(0,0,0,0);
1558 m_clipRgn.Set(HIShapeCreateWithRect(&r));
1559 }
1560 }
1561
1562 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
1563 {
1564 if ( m_pen.IsNull() )
1565 return ;
1566
1567 EnsureIsValid();
1568
1569 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1570
1571 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1572 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1573 CGContextStrokePath( m_cgContext );
1574 }
1575
1576 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , int fillStyle )
1577 {
1578 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1579 {
1580 // when using shading, we cannot draw pen and brush at the same time
1581 // revert to the base implementation of first filling and then stroking
1582 wxGraphicsContext::DrawPath( path, fillStyle );
1583 return;
1584 }
1585
1586 CGPathDrawingMode mode = kCGPathFill ;
1587 if ( m_brush.IsNull() )
1588 {
1589 if ( m_pen.IsNull() )
1590 return;
1591 else
1592 mode = kCGPathStroke;
1593 }
1594 else
1595 {
1596 if ( m_pen.IsNull() )
1597 {
1598 if ( fillStyle == wxODDEVEN_RULE )
1599 mode = kCGPathEOFill;
1600 else
1601 mode = kCGPathFill;
1602 }
1603 else
1604 {
1605 if ( fillStyle == wxODDEVEN_RULE )
1606 mode = kCGPathEOFillStroke;
1607 else
1608 mode = kCGPathFillStroke;
1609 }
1610 }
1611
1612 EnsureIsValid();
1613
1614 if ( !m_brush.IsNull() )
1615 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1616 if ( !m_pen.IsNull() )
1617 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1618
1619 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1620
1621 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1622 CGContextDrawPath( m_cgContext , mode );
1623 }
1624
1625 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , int fillStyle )
1626 {
1627 if ( m_brush.IsNull() )
1628 return;
1629
1630 EnsureIsValid();
1631
1632 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1633 {
1634 CGContextSaveGState( m_cgContext );
1635 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1636 CGContextClip( m_cgContext );
1637 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1638 CGContextRestoreGState( m_cgContext);
1639 }
1640 else
1641 {
1642 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1643 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1644 if ( fillStyle == wxODDEVEN_RULE )
1645 CGContextEOFillPath( m_cgContext );
1646 else
1647 CGContextFillPath( m_cgContext );
1648 }
1649 }
1650
1651 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
1652 {
1653 // we allow either setting or clearing but not replacing
1654 wxASSERT( m_cgContext == NULL || cg == NULL );
1655
1656 if ( m_cgContext )
1657 {
1658 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1659 CGContextRestoreGState( m_cgContext );
1660 CGContextRestoreGState( m_cgContext );
1661 if ( m_releaseContext )
1662 {
1663 #ifndef __LP64__
1664 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
1665 #endif
1666 }
1667 else
1668 CGContextRelease(m_cgContext);
1669 }
1670
1671
1672 m_cgContext = cg;
1673
1674 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1675 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1676 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1677 // for this one operation.
1678
1679 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1680 // can be removed.
1681 if (m_cgContext)
1682 {
1683 CGContextRetain(m_cgContext);
1684 CGContextSaveGState( m_cgContext );
1685 CGContextSaveGState( m_cgContext );
1686 m_releaseContext = false;
1687 }
1688 }
1689
1690 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
1691 {
1692 if ( m_cgContext )
1693 CGContextTranslateCTM( m_cgContext, dx, dy );
1694 else
1695 m_windowTransform = CGAffineTransformTranslate(m_windowTransform,dx,dy);
1696 }
1697
1698 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
1699 {
1700 if ( m_cgContext )
1701 CGContextScaleCTM( m_cgContext , xScale , yScale );
1702 else
1703 m_windowTransform = CGAffineTransformScale(m_windowTransform,xScale,yScale);
1704 }
1705
1706 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
1707 {
1708 if ( m_cgContext )
1709 CGContextRotateCTM( m_cgContext , angle );
1710 else
1711 m_windowTransform = CGAffineTransformRotate(m_windowTransform,angle);
1712 }
1713
1714 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1715 {
1716 EnsureIsValid();
1717
1718 CGImageRef image = (CGImageRef)( bmp.CGImageCreate() );
1719 HIRect r = CGRectMake( x , y , w , h );
1720 if ( bmp.GetDepth() == 1 )
1721 {
1722 // is is a mask, the '1' in the mask tell where to draw the current brush
1723 if ( !m_brush.IsNull() )
1724 {
1725 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1726 {
1727 // TODO clip to mask
1728 /*
1729 CGContextSaveGState( m_cgContext );
1730 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1731 CGContextClip( m_cgContext );
1732 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1733 CGContextRestoreGState( m_cgContext);
1734 */
1735 }
1736 else
1737 {
1738 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1739 HIViewDrawCGImage( m_cgContext , &r , image );
1740 }
1741 }
1742 }
1743 else
1744 {
1745 HIViewDrawCGImage( m_cgContext , &r , image );
1746 }
1747 CGImageRelease( image );
1748 }
1749
1750 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1751 {
1752 EnsureIsValid();
1753
1754 CGRect r = CGRectMake( 00 , 00 , w , h );
1755 CGContextSaveGState( m_cgContext );
1756 CGContextTranslateCTM( m_cgContext, x , y + h );
1757 CGContextScaleCTM( m_cgContext, 1, -1 );
1758 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
1759 NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) );
1760 CGContextRestoreGState( m_cgContext );
1761 }
1762
1763 void wxMacCoreGraphicsContext::PushState()
1764 {
1765 EnsureIsValid();
1766
1767 CGContextSaveGState( m_cgContext );
1768 }
1769
1770 void wxMacCoreGraphicsContext::PopState()
1771 {
1772 EnsureIsValid();
1773
1774 CGContextRestoreGState( m_cgContext );
1775 }
1776
1777 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1778 {
1779 DrawText(str, x, y, 0.0);
1780 }
1781
1782 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
1783 {
1784 if ( m_font.IsNull() )
1785 return;
1786
1787 EnsureIsValid();
1788
1789 OSStatus status = noErr;
1790 ATSUTextLayout atsuLayout;
1791 UniCharCount chars = str.length();
1792 UniChar* ubuf = NULL;
1793
1794 #if SIZEOF_WCHAR_T == 4
1795 wxMBConvUTF16 converter;
1796 #if wxUSE_UNICODE
1797 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
1798 ubuf = (UniChar*) malloc( unicharlen + 2 );
1799 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
1800 #else
1801 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1802 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1803 ubuf = (UniChar*) malloc( unicharlen + 2 );
1804 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1805 #endif
1806 chars = unicharlen / 2;
1807 #else
1808 #if wxUSE_UNICODE
1809 ubuf = (UniChar*) str.wc_str();
1810 #else
1811 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1812 chars = wxWcslen( wchar.data() );
1813 ubuf = (UniChar*) wchar.data();
1814 #endif
1815 #endif
1816
1817 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
1818 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1819 &chars , &style , &atsuLayout );
1820
1821 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
1822
1823 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
1824 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
1825
1826 int iAngle = int( angle * RAD2DEG );
1827 if ( abs(iAngle) > 0 )
1828 {
1829 Fixed atsuAngle = IntToFixed( iAngle );
1830 ATSUAttributeTag atsuTags[] =
1831 {
1832 kATSULineRotationTag ,
1833 };
1834 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1835 {
1836 sizeof( Fixed ) ,
1837 };
1838 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1839 {
1840 &atsuAngle ,
1841 };
1842 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1843 atsuTags, atsuSizes, atsuValues );
1844 }
1845
1846 {
1847 ATSUAttributeTag atsuTags[] =
1848 {
1849 kATSUCGContextTag ,
1850 };
1851 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1852 {
1853 sizeof( CGContextRef ) ,
1854 };
1855 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1856 {
1857 &m_cgContext ,
1858 };
1859 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1860 atsuTags, atsuSizes, atsuValues );
1861 }
1862
1863 ATSUTextMeasurement textBefore, textAfter;
1864 ATSUTextMeasurement ascent, descent;
1865
1866 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1867 &textBefore , &textAfter, &ascent , &descent );
1868
1869 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1870
1871 Rect rect;
1872 x += (int)(sin(angle) * FixedToInt(ascent));
1873 y += (int)(cos(angle) * FixedToInt(ascent));
1874
1875 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1876 IntToFixed(x) , IntToFixed(y) , &rect );
1877 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1878
1879 CGContextSaveGState(m_cgContext);
1880 CGContextTranslateCTM(m_cgContext, x, y);
1881 CGContextScaleCTM(m_cgContext, 1, -1);
1882 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1883 IntToFixed(0) , IntToFixed(0) );
1884
1885 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
1886
1887 CGContextRestoreGState(m_cgContext);
1888
1889 ::ATSUDisposeTextLayout(atsuLayout);
1890
1891 #if SIZEOF_WCHAR_T == 4
1892 free( ubuf );
1893 #endif
1894 }
1895
1896 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1897 wxDouble *descent, wxDouble *externalLeading ) const
1898 {
1899 wxCHECK_RET( !m_font.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1900
1901 if ( width )
1902 *width = 0;
1903 if ( height )
1904 *height = 0;
1905 if ( descent )
1906 *descent = 0;
1907 if ( externalLeading )
1908 *externalLeading = 0;
1909
1910 if (str.empty())
1911 return;
1912
1913 OSStatus status = noErr;
1914
1915 ATSUTextLayout atsuLayout;
1916 UniCharCount chars = str.length();
1917 UniChar* ubuf = NULL;
1918
1919 #if SIZEOF_WCHAR_T == 4
1920 wxMBConvUTF16 converter;
1921 #if wxUSE_UNICODE
1922 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
1923 ubuf = (UniChar*) malloc( unicharlen + 2 );
1924 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
1925 #else
1926 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1927 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1928 ubuf = (UniChar*) malloc( unicharlen + 2 );
1929 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1930 #endif
1931 chars = unicharlen / 2;
1932 #else
1933 #if wxUSE_UNICODE
1934 ubuf = (UniChar*) str.wc_str();
1935 #else
1936 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1937 chars = wxWcslen( wchar.data() );
1938 ubuf = (UniChar*) wchar.data();
1939 #endif
1940 #endif
1941
1942 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
1943 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1944 &chars , &style , &atsuLayout );
1945
1946 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
1947
1948 ATSUTextMeasurement textBefore, textAfter;
1949 ATSUTextMeasurement textAscent, textDescent;
1950
1951 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1952 &textBefore , &textAfter, &textAscent , &textDescent );
1953
1954 if ( height )
1955 *height = FixedToInt(textAscent + textDescent);
1956 if ( descent )
1957 *descent = FixedToInt(textDescent);
1958 if ( externalLeading )
1959 *externalLeading = 0;
1960 if ( width )
1961 *width = FixedToInt(textAfter - textBefore);
1962
1963 ::ATSUDisposeTextLayout(atsuLayout);
1964 #if SIZEOF_WCHAR_T == 4
1965 free( ubuf ) ;
1966 #endif
1967 }
1968
1969 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1970 {
1971 widths.Empty();
1972 widths.Add(0, text.length());
1973
1974 if (text.empty())
1975 return;
1976
1977 ATSUTextLayout atsuLayout;
1978 UniCharCount chars = text.length();
1979 UniChar* ubuf = NULL;
1980
1981 #if SIZEOF_WCHAR_T == 4
1982 wxMBConvUTF16 converter;
1983 #if wxUSE_UNICODE
1984 size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 );
1985 ubuf = (UniChar*) malloc( unicharlen + 2 );
1986 converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 );
1987 #else
1988 const wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1989 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1990 ubuf = (UniChar*) malloc( unicharlen + 2 );
1991 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1992 #endif
1993 chars = unicharlen / 2;
1994 #else
1995 #if wxUSE_UNICODE
1996 ubuf = (UniChar*) text.wc_str();
1997 #else
1998 wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1999 chars = wxWcslen( wchar.data() );
2000 ubuf = (UniChar*) wchar.data();
2001 #endif
2002 #endif
2003
2004 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2005 ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
2006 &chars , &style , &atsuLayout );
2007
2008 for ( int pos = 0; pos < (int)chars; pos ++ )
2009 {
2010 unsigned long actualNumberOfBounds = 0;
2011 ATSTrapezoid glyphBounds;
2012
2013 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2014 OSStatus result;
2015 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2016 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2017 if (result != noErr || actualNumberOfBounds != 1 )
2018 return;
2019
2020 widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
2021 //unsigned char uch = s[i];
2022 }
2023
2024 ::ATSUDisposeTextLayout(atsuLayout);
2025 #if SIZEOF_WCHAR_T == 4
2026 free( ubuf ) ;
2027 #endif
2028 }
2029
2030 void * wxMacCoreGraphicsContext::GetNativeContext()
2031 {
2032 return m_cgContext;
2033 }
2034
2035 // concatenates this transform with the current transform of this context
2036 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
2037 {
2038 if ( m_cgContext )
2039 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2040 else
2041 m_windowTransform = CGAffineTransformConcat(m_windowTransform, *(CGAffineTransform*) matrix.GetNativeMatrix());
2042 }
2043
2044 // sets the transform of this context
2045 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
2046 {
2047 if ( m_cgContext )
2048 {
2049 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2050 transform = CGAffineTransformInvert( transform ) ;
2051 CGContextConcatCTM( m_cgContext, transform);
2052 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2053 }
2054 else
2055 {
2056 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
2057 }
2058 }
2059
2060 // gets the matrix of this context
2061 wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
2062 {
2063 wxGraphicsMatrix m = CreateMatrix();
2064 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
2065 CGContextGetCTM( m_cgContext ));
2066 return m;
2067 }
2068
2069 //
2070 // Renderer
2071 //
2072
2073 //-----------------------------------------------------------------------------
2074 // wxMacCoreGraphicsRenderer declaration
2075 //-----------------------------------------------------------------------------
2076
2077 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
2078 {
2079 public :
2080 wxMacCoreGraphicsRenderer() {}
2081
2082 virtual ~wxMacCoreGraphicsRenderer() {}
2083
2084 // Context
2085
2086 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2087
2088 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2089
2090 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2091
2092 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2093
2094 virtual wxGraphicsContext * CreateMeasuringContext();
2095
2096 // Path
2097
2098 virtual wxGraphicsPath CreatePath();
2099
2100 // Matrix
2101
2102 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2103 wxDouble tx=0.0, wxDouble ty=0.0);
2104
2105
2106 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2107
2108 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2109
2110 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2111 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2112 const wxColour&c1, const wxColour&c2) ;
2113
2114 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2115 // with radius r and color cColor
2116 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2117 const wxColour &oColor, const wxColour &cColor) ;
2118
2119 // sets the font
2120 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2121
2122 private :
2123 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
2124 } ;
2125
2126 //-----------------------------------------------------------------------------
2127 // wxMacCoreGraphicsRenderer implementation
2128 //-----------------------------------------------------------------------------
2129
2130 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
2131
2132 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
2133
2134 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2135 {
2136 return &gs_MacCoreGraphicsRenderer;
2137 }
2138
2139 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc)
2140 {
2141 wxMemoryDC* mdc = wxDynamicCast(&dc, wxMemoryDC);
2142 if ( mdc )
2143 {
2144 return new wxMacCoreGraphicsContext(this,
2145 (CGContextRef)mdc->GetGraphicsContext()->GetNativeContext());
2146 }
2147 else
2148 {
2149 return new wxMacCoreGraphicsContext(this,(CGContextRef)dc.GetWindow()->MacGetCGContextRef() );
2150 }
2151 }
2152
2153 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
2154 {
2155 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
2156 }
2157
2158
2159 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
2160 {
2161 return new wxMacCoreGraphicsContext(this,(WindowRef)window);
2162 }
2163
2164 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
2165 {
2166 return new wxMacCoreGraphicsContext(this, window );
2167 }
2168
2169 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2170 {
2171 return new wxMacCoreGraphicsContext(this);
2172 }
2173
2174 // Path
2175
2176 wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
2177 {
2178 wxGraphicsPath m;
2179 m.SetRefData( new wxMacCoreGraphicsPathData(this));
2180 return m;
2181 }
2182
2183
2184 // Matrix
2185
2186 wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2187 wxDouble tx, wxDouble ty)
2188 {
2189 wxGraphicsMatrix m;
2190 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
2191 data->Set( a,b,c,d,tx,ty ) ;
2192 m.SetRefData(data);
2193 return m;
2194 }
2195
2196 wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
2197 {
2198 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
2199 return wxNullGraphicsPen;
2200 else
2201 {
2202 wxGraphicsPen p;
2203 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
2204 return p;
2205 }
2206 }
2207
2208 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
2209 {
2210 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
2211 return wxNullGraphicsBrush;
2212 else
2213 {
2214 wxGraphicsBrush p;
2215 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
2216 return p;
2217 }
2218 }
2219
2220 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2221 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2222 const wxColour&c1, const wxColour&c2)
2223 {
2224 wxGraphicsBrush p;
2225 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2226 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
2227 p.SetRefData(d);
2228 return p;
2229 }
2230
2231 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2232 // with radius r and color cColor
2233 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2234 const wxColour &oColor, const wxColour &cColor)
2235 {
2236 wxGraphicsBrush p;
2237 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2238 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
2239 p.SetRefData(d);
2240 return p;
2241 }
2242
2243 // sets the font
2244 wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
2245 {
2246 if ( font.Ok() )
2247 {
2248 wxGraphicsFont p;
2249 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
2250 return p;
2251 }
2252 else
2253 return wxNullGraphicsFont;
2254 }
2255
2256
2257
2258 #endif // wxMAC_USE_CORE_GRAPHICS