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