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