]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/graphics.cpp
666d1322ec4bdef10c331eb263e052d7ce14e1cf
[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 , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
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() , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
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 CGContextSetStrokeColorSpace( cg , m_colorSpace );
454 CGContextSetStrokePattern( cg, m_pattern , m_patternColorComponents );
455 }
456 else
457 {
458 CGContextSetStrokeColorWithColor( cg , m_color );
459 }
460 }
461
462 //
463 // Brush
464 //
465
466 class wxMacCoreGraphicsBrushData : public wxGraphicsObjectRefData
467 {
468 public:
469 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer );
470 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
471 ~wxMacCoreGraphicsBrushData ();
472
473 virtual void Apply( wxGraphicsContext* context );
474 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
475 const wxColour&c1, const wxColour&c2 );
476 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
477 const wxColour &oColor, const wxColour &cColor );
478
479 virtual bool IsShading() { return m_isShading; }
480 CGShadingRef GetShading() { return m_shading; }
481 protected:
482 CGFunctionRef CreateGradientFunction( const wxColour& c1, const wxColour& c2 );
483 static void CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out);
484 virtual void Init();
485
486 wxMacCFRefHolder<CGColorRef> m_color;
487 wxMacCFRefHolder<CGColorSpaceRef> m_colorSpace;
488
489 bool m_isPattern;
490 wxMacCFRefHolder<CGPatternRef> m_pattern;
491 CGFloat* m_patternColorComponents;
492
493 bool m_isShading;
494 CGFunctionRef m_gradientFunction;
495 CGShadingRef m_shading;
496 CGFloat *m_gradientComponents;
497 };
498
499 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxGraphicsObjectRefData( renderer )
500 {
501 Init();
502 }
503
504 void wxMacCoreGraphicsBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
505 const wxColour&c1, const wxColour&c2 )
506 {
507 m_gradientFunction = CreateGradientFunction( c1, c2 );
508 m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake(x1,y1), CGPointMake(x2,y2), m_gradientFunction, true, true ) ;
509 m_isShading = true ;
510 }
511
512 void wxMacCoreGraphicsBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
513 const wxColour &oColor, const wxColour &cColor )
514 {
515 m_gradientFunction = CreateGradientFunction( oColor, cColor );
516 m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake(xo,yo), 0, CGPointMake(xc,yc), radius, m_gradientFunction, true, true ) ;
517 m_isShading = true ;
518 }
519
520 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxGraphicsObjectRefData( renderer )
521 {
522 Init();
523
524 if ( brush.GetStyle() == wxSOLID )
525 {
526 float components[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
527 brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 } ;
528 m_color.Set( CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ) ;
529 }
530 else if ( brush.IsHatch() )
531 {
532 m_isPattern = true;
533 m_colorSpace.Set( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
534 m_pattern.Set( *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
535
536 m_patternColorComponents = new CGFloat[4] ;
537 m_patternColorComponents[0] = brush.GetColour().Red() / 255.0;
538 m_patternColorComponents[1] = brush.GetColour().Green() / 255.0;
539 m_patternColorComponents[2] = brush.GetColour().Blue() / 255.0;
540 m_patternColorComponents[3] = brush.GetColour().Alpha() / 255.0;
541 }
542 else
543 {
544 // now brush is a bitmap
545 wxBitmap* bmp = brush.GetStipple();
546 if ( bmp && bmp->Ok() )
547 {
548 m_isPattern = true;
549 m_patternColorComponents = new CGFloat[1] ;
550 m_patternColorComponents[0] = 1.0;
551 m_colorSpace.Set( CGColorSpaceCreatePattern( NULL ) );
552 m_pattern.Set( *( new ImagePattern( bmp , CGAffineTransformMakeTranslation( 0,0 ) ) ) );
553 }
554 }
555 }
556
557 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
558 {
559 if ( m_shading )
560 CGShadingRelease(m_shading);
561
562 if( m_gradientFunction )
563 CGFunctionRelease(m_gradientFunction);
564
565 delete[] m_gradientComponents;
566 delete[] m_patternColorComponents;
567 }
568
569 void wxMacCoreGraphicsBrushData::Init()
570 {
571 m_patternColorComponents = NULL;
572 m_gradientFunction = NULL;
573 m_shading = NULL;
574 m_isPattern = false;
575 m_gradientComponents = NULL;
576 m_isShading = false;
577 }
578
579 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context )
580 {
581 CGContextRef cg = (CGContextRef) context->GetNativeContext();
582
583 if ( m_isShading )
584 {
585 }
586 else
587 {
588 if ( m_isPattern )
589 {
590 CGContextSetFillColorSpace( cg , m_colorSpace );
591 CGContextSetFillPattern( cg, m_pattern , m_patternColorComponents );
592 }
593 else
594 {
595 CGContextSetFillColorWithColor( cg, m_color );
596 }
597 }
598 }
599
600 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out)
601 {
602 CGFloat* colors = (CGFloat*) info ;
603 CGFloat f = *in;
604 for( int i = 0 ; i < 4 ; ++i )
605 {
606 out[i] = colors[i] + ( colors[4+i] - colors[i] ) * f;
607 }
608 }
609
610 CGFunctionRef wxMacCoreGraphicsBrushData::CreateGradientFunction( const wxColour& c1, const wxColour& c2 )
611 {
612 static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, NULL };
613 static const CGFloat input_value_range [2] = { 0, 1 };
614 static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
615 m_gradientComponents = new CGFloat[8] ;
616 m_gradientComponents[0] = c1.Red() / 255.0;
617 m_gradientComponents[1] = c1.Green() / 255.0;
618 m_gradientComponents[2] = c1.Blue() / 255.0;
619 m_gradientComponents[3] = c1.Alpha() / 255.0;
620 m_gradientComponents[4] = c2.Red() / 255.0;
621 m_gradientComponents[5] = c2.Green() / 255.0;
622 m_gradientComponents[6] = c2.Blue() / 255.0;
623 m_gradientComponents[7] = c2.Alpha() / 255.0;
624
625 return CGFunctionCreate ( m_gradientComponents, 1,
626 input_value_range,
627 4,
628 output_value_ranges,
629 &callbacks);
630 }
631
632 //
633 // Font
634 //
635
636 class wxMacCoreGraphicsFontData : public wxGraphicsObjectRefData
637 {
638 public:
639 wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
640 ~wxMacCoreGraphicsFontData();
641
642 virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; }
643 private :
644 ATSUStyle m_macATSUIStyle;
645 };
646
647 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer )
648 {
649 m_macATSUIStyle = NULL;
650
651 OSStatus status;
652
653 status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle );
654
655 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );
656
657 // we need the scale here ...
658
659 Fixed atsuSize = IntToFixed( int( 1 * font.MacGetFontSize()) );
660 RGBColor atsuColor = MAC_WXCOLORREF( col.GetPixel() );
661 ATSUAttributeTag atsuTags[] =
662 {
663 kATSUSizeTag ,
664 kATSUColorTag ,
665 };
666 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
667 {
668 sizeof( Fixed ) ,
669 sizeof( RGBColor ) ,
670 };
671 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
672 {
673 &atsuSize ,
674 &atsuColor ,
675 };
676
677 status = ::ATSUSetAttributes(
678 m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
679 atsuTags, atsuSizes, atsuValues);
680
681 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
682 }
683
684 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
685 {
686 if ( m_macATSUIStyle )
687 {
688 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
689 m_macATSUIStyle = NULL;
690 }
691 }
692
693 //
694 // Graphics Matrix
695 //
696
697 //-----------------------------------------------------------------------------
698 // wxMacCoreGraphicsMatrix declaration
699 //-----------------------------------------------------------------------------
700
701 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData
702 {
703 public :
704 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ;
705
706 virtual ~wxMacCoreGraphicsMatrixData() ;
707
708 virtual wxGraphicsObjectRefData *Clone() const ;
709
710 // concatenates the matrix
711 virtual void Concat( const wxGraphicsMatrixData *t );
712
713 // sets the matrix to the respective values
714 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
715 wxDouble tx=0.0, wxDouble ty=0.0);
716
717 // makes this the inverse matrix
718 virtual void Invert();
719
720 // returns true if the elements of the transformation matrix are equal ?
721 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
722
723 // return true if this is the identity matrix
724 virtual bool IsIdentity() const;
725
726 //
727 // transformation
728 //
729
730 // add the translation to this matrix
731 virtual void Translate( wxDouble dx , wxDouble dy );
732
733 // add the scale to this matrix
734 virtual void Scale( wxDouble xScale , wxDouble yScale );
735
736 // add the rotation to this matrix (radians)
737 virtual void Rotate( wxDouble angle );
738
739 //
740 // apply the transforms
741 //
742
743 // applies that matrix to the point
744 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
745
746 // applies the matrix except for translations
747 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
748
749 // returns the native representation
750 virtual void * GetNativeMatrix() const;
751
752 private :
753 CGAffineTransform m_matrix;
754 } ;
755
756 //-----------------------------------------------------------------------------
757 // wxMacCoreGraphicsMatrix implementation
758 //-----------------------------------------------------------------------------
759
760 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer)
761 {
762 }
763
764 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
765 {
766 }
767
768 wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const
769 {
770 wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
771 m->m_matrix = m_matrix ;
772 return m;
773 }
774
775 // concatenates the matrix
776 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t )
777 {
778 m_matrix = CGAffineTransformConcat(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()) );
779 }
780
781 // sets the matrix to the respective values
782 void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
783 wxDouble tx, wxDouble ty)
784 {
785 m_matrix = CGAffineTransformMake(a,b,c,d,tx,ty);
786 }
787
788 // makes this the inverse matrix
789 void wxMacCoreGraphicsMatrixData::Invert()
790 {
791 m_matrix = CGAffineTransformInvert( m_matrix );
792 }
793
794 // returns true if the elements of the transformation matrix are equal ?
795 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
796 {
797 const CGAffineTransform* tm = (CGAffineTransform*) t->GetNativeMatrix();
798 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
799 if ( CGAffineTransformEqualToTransform )
800 {
801 return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()));
802 }
803 else
804 #endif
805 {
806 return (
807 m_matrix.a == tm->a &&
808 m_matrix.b == tm->b &&
809 m_matrix.c == tm->c &&
810 m_matrix.d == tm->d &&
811 m_matrix.tx == tm->tx &&
812 m_matrix.ty == tm->ty ) ;
813 }
814 }
815
816 // return true if this is the identity matrix
817 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
818 {
819 return ( m_matrix.a == 1 && m_matrix.d == 1 &&
820 m_matrix.b == 0 && m_matrix.d == 0 && m_matrix.tx == 0 && m_matrix.ty == 0);
821 }
822
823 //
824 // transformation
825 //
826
827 // add the translation to this matrix
828 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx , wxDouble dy )
829 {
830 m_matrix = CGAffineTransformTranslate( m_matrix, dx, dy);
831 }
832
833 // add the scale to this matrix
834 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale , wxDouble yScale )
835 {
836 m_matrix = CGAffineTransformScale( m_matrix, xScale, yScale);
837 }
838
839 // add the rotation to this matrix (radians)
840 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle )
841 {
842 m_matrix = CGAffineTransformRotate( m_matrix, angle);
843 }
844
845 //
846 // apply the transforms
847 //
848
849 // applies that matrix to the point
850 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
851 {
852 CGPoint pt = CGPointApplyAffineTransform( CGPointMake(*x,*y), m_matrix);
853
854 *x = pt.x;
855 *y = pt.y;
856 }
857
858 // applies the matrix except for translations
859 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
860 {
861 CGSize sz = CGSizeApplyAffineTransform( CGSizeMake(*dx,*dy) , m_matrix );
862 *dx = sz.width;
863 *dy = sz.height;
864 }
865
866 // returns the native representation
867 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
868 {
869 return (void*) &m_matrix;
870 }
871
872 //
873 // Graphics Path
874 //
875
876 //-----------------------------------------------------------------------------
877 // wxMacCoreGraphicsPath declaration
878 //-----------------------------------------------------------------------------
879
880 class WXDLLEXPORT wxMacCoreGraphicsPathData : public wxGraphicsPathData
881 {
882 public :
883 wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path = NULL);
884
885 ~wxMacCoreGraphicsPathData();
886
887 virtual wxGraphicsObjectRefData *Clone() const;
888
889 // begins a new subpath at (x,y)
890 virtual void MoveToPoint( wxDouble x, wxDouble y );
891
892 // adds a straight line from the current point to (x,y)
893 virtual void AddLineToPoint( wxDouble x, wxDouble y );
894
895 // adds a cubic Bezier curve from the current point, using two control points and an end point
896 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
897
898 // closes the current sub-path
899 virtual void CloseSubpath();
900
901 // gets the last point of the current path, (0,0) if not yet set
902 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
903
904 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
905 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
906
907 //
908 // These are convenience functions which - if not available natively will be assembled
909 // using the primitives from above
910 //
911
912 // adds a quadratic Bezier curve from the current point, using a control point and an end point
913 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
914
915 // appends a rectangle as a new closed subpath
916 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
917
918 // appends an ellipsis as a new closed subpath fitting the passed rectangle
919 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
920
921 // 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)
922 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
923
924 // adds another path
925 virtual void AddPath( const wxGraphicsPathData* path );
926
927 // returns the native path
928 virtual void * GetNativePath() const { return m_path; }
929
930 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
931 virtual void UnGetNativePath(void *p) const {}
932
933 // transforms each point of this path by the matrix
934 virtual void Transform( const wxGraphicsMatrixData* matrix );
935
936 // gets the bounding box enclosing all points (possibly including control points)
937 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *y) const;
938
939 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxODDEVEN_RULE) const;
940 private :
941 CGMutablePathRef m_path;
942 };
943
944 //-----------------------------------------------------------------------------
945 // wxMacCoreGraphicsPath implementation
946 //-----------------------------------------------------------------------------
947
948 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path) : wxGraphicsPathData(renderer)
949 {
950 if ( path )
951 m_path = path;
952 else
953 m_path = CGPathCreateMutable();
954 }
955
956 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
957 {
958 CGPathRelease( m_path );
959 }
960
961 wxGraphicsObjectRefData* wxMacCoreGraphicsPathData::Clone() const
962 {
963 wxMacCoreGraphicsPathData* clone = new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path));
964 return clone ;
965 }
966
967
968 // opens (starts) a new subpath
969 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1 , wxDouble y1 )
970 {
971 CGPathMoveToPoint( m_path , NULL , x1 , y1 );
972 }
973
974 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1 , wxDouble y1 )
975 {
976 CGPathAddLineToPoint( m_path , NULL , x1 , y1 );
977 }
978
979 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
980 {
981 CGPathAddCurveToPoint( m_path , NULL , cx1 , cy1 , cx2, cy2, x , y );
982 }
983
984 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
985 {
986 CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x , y );
987 }
988
989 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
990 {
991 CGRect cgRect = { { x , y } , { w , h } };
992 CGPathAddRect( m_path , NULL , cgRect );
993 }
994
995 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x, wxDouble y , wxDouble r )
996 {
997 CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true );
998 }
999
1000 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1001 void wxMacCoreGraphicsPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
1002 {
1003 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1004 CGPathAddArc( m_path, NULL , x, y, r, startAngle, endAngle, !clockwise);
1005 }
1006
1007 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
1008 {
1009 CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
1010 }
1011
1012 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData* path )
1013 {
1014 CGPathAddPath( m_path , NULL, (CGPathRef) path->GetNativePath() );
1015 }
1016
1017 // closes the current subpath
1018 void wxMacCoreGraphicsPathData::CloseSubpath()
1019 {
1020 CGPathCloseSubpath( m_path );
1021 }
1022
1023 // gets the last point of the current path, (0,0) if not yet set
1024 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
1025 {
1026 CGPoint p = CGPathGetCurrentPoint( m_path );
1027 *x = p.x;
1028 *y = p.y;
1029 }
1030
1031 // transforms each point of this path by the matrix
1032 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData* matrix )
1033 {
1034 CGMutablePathRef p = CGPathCreateMutable() ;
1035 CGPathAddPath( p, (CGAffineTransform*) matrix->GetNativeMatrix() , m_path );
1036 CGPathRelease( m_path );
1037 m_path = p;
1038 }
1039
1040 // gets the bounding box enclosing all points (possibly including control points)
1041 void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
1042 {
1043 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1044 *x = bounds.origin.x;
1045 *y = bounds.origin.y;
1046 *w = bounds.size.width;
1047 *h = bounds.size.height;
1048 }
1049
1050 bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, int fillStyle) const
1051 {
1052 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1053 if ( CGPathContainsPoint )
1054 {
1055 return CGPathContainsPoint( m_path, NULL, CGPointMake(x,y), fillStyle == wxODDEVEN_RULE );
1056 }
1057 else
1058 #endif
1059 {
1060 // TODO : implementation for 10.3
1061 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1062 return CGRectContainsPoint( bounds, CGPointMake(x,y) ) == 1;
1063 }
1064 }
1065
1066 //
1067 // Graphics Context
1068 //
1069
1070 //-----------------------------------------------------------------------------
1071 // wxMacCoreGraphicsContext declaration
1072 //-----------------------------------------------------------------------------
1073
1074 class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
1075 {
1076 public:
1077 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext );
1078
1079 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window );
1080
1081 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window );
1082
1083 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer);
1084
1085 wxMacCoreGraphicsContext();
1086
1087 ~wxMacCoreGraphicsContext();
1088
1089 void Init();
1090
1091 // push the current state of the context, ie the transformation matrix on a stack
1092 virtual void PushState();
1093
1094 // pops a stored state from the stack
1095 virtual void PopState();
1096
1097 // clips drawings to the region
1098 virtual void Clip( const wxRegion &region );
1099
1100 // clips drawings to the rect
1101 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1102
1103 // resets the clipping to original extent
1104 virtual void ResetClip();
1105
1106 virtual void * GetNativeContext();
1107
1108 //
1109 // transformation
1110 //
1111
1112 // translate
1113 virtual void Translate( wxDouble dx , wxDouble dy );
1114
1115 // scale
1116 virtual void Scale( wxDouble xScale , wxDouble yScale );
1117
1118 // rotate (radians)
1119 virtual void Rotate( wxDouble angle );
1120
1121 // concatenates this transform with the current transform of this context
1122 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
1123
1124 // sets the transform of this context
1125 virtual void SetTransform( const wxGraphicsMatrix& matrix );
1126
1127 // gets the matrix of this context
1128 virtual wxGraphicsMatrix GetTransform() const;
1129 //
1130 // setting the paint
1131 //
1132
1133 // strokes along a path with the current pen
1134 virtual void StrokePath( const wxGraphicsPath &path );
1135
1136 // fills a path with the current brush
1137 virtual void FillPath( const wxGraphicsPath &path, int fillStyle = wxODDEVEN_RULE );
1138
1139 // draws a path by first filling and then stroking
1140 virtual void DrawPath( const wxGraphicsPath &path, int fillStyle = wxODDEVEN_RULE );
1141
1142 virtual bool ShouldOffset() const
1143 {
1144 int penwidth = 0 ;
1145 if ( !m_pen.IsNull() )
1146 {
1147 penwidth = (int)((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->GetWidth();
1148 if ( penwidth == 0 )
1149 penwidth = 1;
1150 }
1151 return ( penwidth % 2 ) == 1;
1152 }
1153 //
1154 // text
1155 //
1156
1157 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y );
1158
1159 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
1160
1161 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
1162 wxDouble *descent, wxDouble *externalLeading ) const;
1163
1164 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
1165
1166 //
1167 // image support
1168 //
1169
1170 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1171
1172 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1173
1174 void SetNativeContext( CGContextRef cg );
1175
1176 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext)
1177 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext)
1178
1179 private:
1180 void EnsureIsValid();
1181
1182 CGContextRef m_cgContext;
1183 WindowRef m_windowRef;
1184 bool m_releaseContext;
1185 CGAffineTransform m_windowTransform;
1186
1187 wxMacCFRefHolder<HIShapeRef> m_clipRgn;
1188 };
1189
1190 //-----------------------------------------------------------------------------
1191 // device context implementation
1192 //
1193 // more and more of the dc functionality should be implemented by calling
1194 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1195 // also coordinate conversions should be moved to native matrix ops
1196 //-----------------------------------------------------------------------------
1197
1198 // we always stock two context states, one at entry, to be able to preserve the
1199 // state we were called with, the other one after changing to HI Graphics orientation
1200 // (this one is used for getting back clippings etc)
1201
1202 //-----------------------------------------------------------------------------
1203 // wxMacCoreGraphicsContext implementation
1204 //-----------------------------------------------------------------------------
1205
1206 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext)
1207
1208 void wxMacCoreGraphicsContext::Init()
1209 {
1210 m_cgContext = NULL;
1211 m_releaseContext = false;
1212 m_windowRef = NULL;
1213
1214 HIRect r = CGRectMake(0,0,0,0);
1215 m_clipRgn.Set(HIShapeCreateWithRect(&r));
1216 }
1217
1218 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext ) : wxGraphicsContext(renderer)
1219 {
1220 Init();
1221 SetNativeContext(cgcontext);
1222 }
1223
1224 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer)
1225 {
1226 Init();
1227 m_windowRef = window;
1228 }
1229
1230 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer)
1231 {
1232 Init();
1233 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
1234 int originX , originY;
1235 originX = originY = 0;
1236 window->MacWindowToRootWindow( &originX , &originY );
1237 Rect bounds;
1238 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
1239
1240 m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top );
1241 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1242 m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ;
1243 }
1244
1245 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer)
1246 {
1247 Init();
1248 }
1249
1250 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL)
1251 {
1252 Init();
1253 wxLogDebug(wxT("Illegal Constructor called"));
1254 }
1255
1256 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1257 {
1258 SetNativeContext(NULL);
1259 }
1260
1261 void wxMacCoreGraphicsContext::EnsureIsValid()
1262 {
1263 if ( !m_cgContext )
1264 {
1265 OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
1266 wxASSERT_MSG( status == noErr , wxT("Cannot nest wxDCs on the same window") );
1267
1268 CGContextConcatCTM( m_cgContext, m_windowTransform );
1269 CGContextSaveGState( m_cgContext );
1270 m_releaseContext = true;
1271 if ( !HIShapeIsEmpty(m_clipRgn) )
1272 {
1273 HIShapeReplacePathInCGContext( m_clipRgn, m_cgContext );
1274 CGContextClip( m_cgContext );
1275 }
1276 CGContextSaveGState( m_cgContext );
1277 }
1278 }
1279
1280
1281 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1282 {
1283 if( m_cgContext )
1284 {
1285 HIShapeRef shape = HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() );
1286 HIShapeReplacePathInCGContext( shape, m_cgContext );
1287 CGContextClip( m_cgContext );
1288 CFRelease( shape );
1289 }
1290 else
1291 {
1292 m_clipRgn.Set(HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() ));
1293 }
1294 }
1295
1296 // clips drawings to the rect
1297 void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1298 {
1299 HIRect r = CGRectMake( x , y , w , h );
1300 if ( m_cgContext )
1301 {
1302 CGContextClipToRect( m_cgContext, r );
1303 }
1304 else
1305 {
1306 m_clipRgn.Set(HIShapeCreateWithRect(&r));
1307 }
1308 }
1309
1310 // resets the clipping to original extent
1311 void wxMacCoreGraphicsContext::ResetClip()
1312 {
1313 if ( m_cgContext )
1314 {
1315 CGContextRestoreGState( m_cgContext );
1316 CGContextSaveGState( m_cgContext );
1317 }
1318 else
1319 {
1320 HIRect r = CGRectMake(0,0,0,0);
1321 m_clipRgn.Set(HIShapeCreateWithRect(&r));
1322 }
1323 }
1324
1325 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
1326 {
1327 if ( m_pen.IsNull() )
1328 return ;
1329
1330 EnsureIsValid();
1331
1332 bool offset = ShouldOffset();
1333 if ( offset )
1334 CGContextTranslateCTM( m_cgContext, 0.5, 0.5 );
1335
1336 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1337 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1338 CGContextStrokePath( m_cgContext );
1339
1340 if ( offset )
1341 CGContextTranslateCTM( m_cgContext, -0.5, -0.5 );
1342 }
1343
1344 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , int fillStyle )
1345 {
1346 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1347 {
1348 // when using shading, we cannot draw pen and brush at the same time
1349 // revert to the base implementation of first filling and then stroking
1350 wxGraphicsContext::DrawPath( path, fillStyle );
1351 return;
1352 }
1353
1354 CGPathDrawingMode mode = kCGPathFill ;
1355 if ( m_brush.IsNull() )
1356 {
1357 if ( m_pen.IsNull() )
1358 return;
1359 else
1360 mode = kCGPathStroke;
1361 }
1362 else
1363 {
1364 if ( m_pen.IsNull() )
1365 {
1366 if ( fillStyle == wxODDEVEN_RULE )
1367 mode = kCGPathEOFill;
1368 else
1369 mode = kCGPathFill;
1370 }
1371 else
1372 {
1373 if ( fillStyle == wxODDEVEN_RULE )
1374 mode = kCGPathEOFillStroke;
1375 else
1376 mode = kCGPathFillStroke;
1377 }
1378 }
1379
1380 EnsureIsValid();
1381
1382 if ( !m_brush.IsNull() )
1383 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1384 if ( !m_pen.IsNull() )
1385 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1386
1387 bool offset = ShouldOffset();
1388
1389 if ( offset )
1390 CGContextTranslateCTM( m_cgContext, 0.5, 0.5 );
1391
1392 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1393 CGContextDrawPath( m_cgContext , mode );
1394
1395 if ( offset )
1396 CGContextTranslateCTM( m_cgContext, -0.5, -0.5 );
1397 }
1398
1399 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , int fillStyle )
1400 {
1401 if ( m_brush.IsNull() )
1402 return;
1403
1404 EnsureIsValid();
1405
1406 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1407 {
1408 CGContextSaveGState( m_cgContext );
1409 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1410 CGContextClip( m_cgContext );
1411 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1412 CGContextRestoreGState( m_cgContext);
1413 }
1414 else
1415 {
1416 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1417 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1418 if ( fillStyle == wxODDEVEN_RULE )
1419 CGContextEOFillPath( m_cgContext );
1420 else
1421 CGContextFillPath( m_cgContext );
1422 }
1423 }
1424
1425 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
1426 {
1427 // we allow either setting or clearing but not replacing
1428 wxASSERT( m_cgContext == NULL || cg == NULL );
1429
1430 if ( m_cgContext )
1431 {
1432 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1433 CGContextRestoreGState( m_cgContext );
1434 CGContextRestoreGState( m_cgContext );
1435 if ( m_releaseContext )
1436 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
1437 else
1438 CGContextRelease(m_cgContext);
1439 }
1440
1441
1442 m_cgContext = cg;
1443
1444 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1445 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1446 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1447 // for this one operation.
1448
1449 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1450 // can be removed.
1451 if (m_cgContext)
1452 {
1453 CGContextRetain(m_cgContext);
1454 CGContextSaveGState( m_cgContext );
1455 CGContextSaveGState( m_cgContext );
1456 m_releaseContext = false;
1457 }
1458 }
1459
1460 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
1461 {
1462 if ( m_cgContext )
1463 CGContextTranslateCTM( m_cgContext, dx, dy );
1464 else
1465 m_windowTransform = CGAffineTransformTranslate(m_windowTransform,dx,dy);
1466 }
1467
1468 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
1469 {
1470 if ( m_cgContext )
1471 CGContextScaleCTM( m_cgContext , xScale , yScale );
1472 else
1473 m_windowTransform = CGAffineTransformScale(m_windowTransform,xScale,yScale);
1474 }
1475
1476 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
1477 {
1478 if ( m_cgContext )
1479 CGContextRotateCTM( m_cgContext , angle );
1480 else
1481 m_windowTransform = CGAffineTransformRotate(m_windowTransform,angle);
1482 }
1483
1484 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1485 {
1486 EnsureIsValid();
1487
1488 CGImageRef image = (CGImageRef)( bmp.CGImageCreate() );
1489 HIRect r = CGRectMake( x , y , w , h );
1490 HIViewDrawCGImage( m_cgContext , &r , image );
1491 CGImageRelease( image );
1492 }
1493
1494 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1495 {
1496 EnsureIsValid();
1497
1498 CGRect r = CGRectMake( 00 , 00 , w , h );
1499 CGContextSaveGState( m_cgContext );
1500 CGContextTranslateCTM( m_cgContext, x , y + h );
1501 CGContextScaleCTM( m_cgContext, 1, -1 );
1502 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
1503 NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) );
1504 CGContextRestoreGState( m_cgContext );
1505 }
1506
1507 void wxMacCoreGraphicsContext::PushState()
1508 {
1509 EnsureIsValid();
1510
1511 CGContextSaveGState( m_cgContext );
1512 }
1513
1514 void wxMacCoreGraphicsContext::PopState()
1515 {
1516 EnsureIsValid();
1517
1518 CGContextRestoreGState( m_cgContext );
1519 }
1520
1521 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1522 {
1523 DrawText(str, x, y, 0.0);
1524 }
1525
1526 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
1527 {
1528 if ( m_font.IsNull() )
1529 return;
1530
1531 EnsureIsValid();
1532
1533 OSStatus status = noErr;
1534 ATSUTextLayout atsuLayout;
1535 UniCharCount chars = str.length();
1536 UniChar* ubuf = NULL;
1537
1538 #if SIZEOF_WCHAR_T == 4
1539 wxMBConvUTF16 converter;
1540 #if wxUSE_UNICODE
1541 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
1542 ubuf = (UniChar*) malloc( unicharlen + 2 );
1543 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
1544 #else
1545 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1546 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1547 ubuf = (UniChar*) malloc( unicharlen + 2 );
1548 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1549 #endif
1550 chars = unicharlen / 2;
1551 #else
1552 #if wxUSE_UNICODE
1553 ubuf = (UniChar*) str.wc_str();
1554 #else
1555 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1556 chars = wxWcslen( wchar.data() );
1557 ubuf = (UniChar*) wchar.data();
1558 #endif
1559 #endif
1560
1561 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
1562 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1563 &chars , &style , &atsuLayout );
1564
1565 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
1566
1567 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
1568 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
1569
1570 int iAngle = int( angle * RAD2DEG );
1571 if ( abs(iAngle) > 0 )
1572 {
1573 Fixed atsuAngle = IntToFixed( iAngle );
1574 ATSUAttributeTag atsuTags[] =
1575 {
1576 kATSULineRotationTag ,
1577 };
1578 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1579 {
1580 sizeof( Fixed ) ,
1581 };
1582 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1583 {
1584 &atsuAngle ,
1585 };
1586 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1587 atsuTags, atsuSizes, atsuValues );
1588 }
1589
1590 {
1591 ATSUAttributeTag atsuTags[] =
1592 {
1593 kATSUCGContextTag ,
1594 };
1595 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1596 {
1597 sizeof( CGContextRef ) ,
1598 };
1599 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1600 {
1601 &m_cgContext ,
1602 };
1603 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1604 atsuTags, atsuSizes, atsuValues );
1605 }
1606
1607 ATSUTextMeasurement textBefore, textAfter;
1608 ATSUTextMeasurement ascent, descent;
1609
1610 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1611 &textBefore , &textAfter, &ascent , &descent );
1612
1613 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1614
1615 Rect rect;
1616 /*
1617 // TODO
1618 if ( m_backgroundMode == wxSOLID )
1619 {
1620 wxGraphicsPath* path = m_graphicContext->CreatePath();
1621 path->MoveToPoint( drawX , drawY );
1622 path->AddLineToPoint(
1623 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1624 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) );
1625 path->AddLineToPoint(
1626 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1627 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1628 path->AddLineToPoint(
1629 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1630 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1631
1632 m_graphicContext->FillPath( path , m_textBackgroundColour );
1633 delete path;
1634 }
1635 */
1636 x += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent));
1637 y += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent));
1638
1639 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1640 IntToFixed(x) , IntToFixed(y) , &rect );
1641 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1642
1643 CGContextSaveGState(m_cgContext);
1644 CGContextTranslateCTM(m_cgContext, x, y);
1645 CGContextScaleCTM(m_cgContext, 1, -1);
1646 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1647 IntToFixed(0) , IntToFixed(0) );
1648
1649 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
1650
1651 CGContextRestoreGState(m_cgContext);
1652
1653 ::ATSUDisposeTextLayout(atsuLayout);
1654
1655 #if SIZEOF_WCHAR_T == 4
1656 free( ubuf );
1657 #endif
1658 }
1659
1660 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1661 wxDouble *descent, wxDouble *externalLeading ) const
1662 {
1663 wxCHECK_RET( !m_font.IsNull(), wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1664
1665 OSStatus status = noErr;
1666
1667 ATSUTextLayout atsuLayout;
1668 UniCharCount chars = str.length();
1669 UniChar* ubuf = NULL;
1670
1671 #if SIZEOF_WCHAR_T == 4
1672 wxMBConvUTF16 converter;
1673 #if wxUSE_UNICODE
1674 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
1675 ubuf = (UniChar*) malloc( unicharlen + 2 );
1676 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
1677 #else
1678 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1679 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1680 ubuf = (UniChar*) malloc( unicharlen + 2 );
1681 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1682 #endif
1683 chars = unicharlen / 2;
1684 #else
1685 #if wxUSE_UNICODE
1686 ubuf = (UniChar*) str.wc_str();
1687 #else
1688 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1689 chars = wxWcslen( wchar.data() );
1690 ubuf = (UniChar*) wchar.data();
1691 #endif
1692 #endif
1693
1694 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
1695 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1696 &chars , &style , &atsuLayout );
1697
1698 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
1699
1700 ATSUTextMeasurement textBefore, textAfter;
1701 ATSUTextMeasurement textAscent, textDescent;
1702
1703 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1704 &textBefore , &textAfter, &textAscent , &textDescent );
1705
1706 if ( height )
1707 *height = FixedToInt(textAscent + textDescent);
1708 if ( descent )
1709 *descent = FixedToInt(textDescent);
1710 if ( externalLeading )
1711 *externalLeading = 0;
1712 if ( width )
1713 *width = FixedToInt(textAfter - textBefore);
1714
1715 ::ATSUDisposeTextLayout(atsuLayout);
1716 }
1717
1718 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1719 {
1720 widths.Empty();
1721 widths.Add(0, text.length());
1722
1723 if (text.empty())
1724 return;
1725
1726 ATSUTextLayout atsuLayout;
1727 UniCharCount chars = text.length();
1728 UniChar* ubuf = NULL;
1729
1730 #if SIZEOF_WCHAR_T == 4
1731 wxMBConvUTF16 converter;
1732 #if wxUSE_UNICODE
1733 size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 );
1734 ubuf = (UniChar*) malloc( unicharlen + 2 );
1735 converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 );
1736 #else
1737 const wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1738 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1739 ubuf = (UniChar*) malloc( unicharlen + 2 );
1740 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1741 #endif
1742 chars = unicharlen / 2;
1743 #else
1744 #if wxUSE_UNICODE
1745 ubuf = (UniChar*) text.wc_str();
1746 #else
1747 wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1748 chars = wxWcslen( wchar.data() );
1749 ubuf = (UniChar*) wchar.data();
1750 #endif
1751 #endif
1752
1753 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
1754 ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1755 &chars , &style , &atsuLayout );
1756
1757 for ( int pos = 0; pos < (int)chars; pos ++ )
1758 {
1759 unsigned long actualNumberOfBounds = 0;
1760 ATSTrapezoid glyphBounds;
1761
1762 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1763 OSStatus result;
1764 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
1765 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
1766 if (result != noErr || actualNumberOfBounds != 1 )
1767 return;
1768
1769 widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
1770 //unsigned char uch = s[i];
1771 }
1772
1773 ::ATSUDisposeTextLayout(atsuLayout);
1774 }
1775
1776 void * wxMacCoreGraphicsContext::GetNativeContext()
1777 {
1778 return m_cgContext;
1779 }
1780
1781 // concatenates this transform with the current transform of this context
1782 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1783 {
1784 if ( m_cgContext )
1785 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
1786 else
1787 m_windowTransform = CGAffineTransformConcat(m_windowTransform, *(CGAffineTransform*) matrix.GetNativeMatrix());
1788 }
1789
1790 // sets the transform of this context
1791 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
1792 {
1793 if ( m_cgContext )
1794 {
1795 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
1796 transform = CGAffineTransformInvert( transform ) ;
1797 CGContextConcatCTM( m_cgContext, transform);
1798 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
1799 }
1800 else
1801 {
1802 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
1803 }
1804 }
1805
1806 // gets the matrix of this context
1807 wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
1808 {
1809 wxGraphicsMatrix m = CreateMatrix();
1810 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
1811 CGContextGetCTM( m_cgContext ));
1812 return m;
1813 }
1814
1815 //
1816 // Renderer
1817 //
1818
1819 //-----------------------------------------------------------------------------
1820 // wxMacCoreGraphicsRenderer declaration
1821 //-----------------------------------------------------------------------------
1822
1823 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
1824 {
1825 public :
1826 wxMacCoreGraphicsRenderer() {}
1827
1828 virtual ~wxMacCoreGraphicsRenderer() {}
1829
1830 // Context
1831
1832 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1833
1834 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1835
1836 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1837
1838 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1839
1840 // Path
1841
1842 virtual wxGraphicsPath CreatePath();
1843
1844 // Matrix
1845
1846 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1847 wxDouble tx=0.0, wxDouble ty=0.0);
1848
1849
1850 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1851
1852 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1853
1854 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1855 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1856 const wxColour&c1, const wxColour&c2) ;
1857
1858 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1859 // with radius r and color cColor
1860 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1861 const wxColour &oColor, const wxColour &cColor) ;
1862
1863 // sets the font
1864 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1865
1866 private :
1867 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
1868 } ;
1869
1870 //-----------------------------------------------------------------------------
1871 // wxMacCoreGraphicsRenderer implementation
1872 //-----------------------------------------------------------------------------
1873
1874 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
1875
1876 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
1877
1878 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1879 {
1880 return &gs_MacCoreGraphicsRenderer;
1881 }
1882
1883 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc)
1884 {
1885 return new wxMacCoreGraphicsContext(this,(CGContextRef)dc.GetWindow()->MacGetCGContextRef() );
1886 }
1887
1888 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
1889 {
1890 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
1891 }
1892
1893
1894 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
1895 {
1896 return new wxMacCoreGraphicsContext(this,(WindowRef)window);
1897 }
1898
1899 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
1900 {
1901 return new wxMacCoreGraphicsContext(this, window );
1902 }
1903
1904 // Path
1905
1906 wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
1907 {
1908 wxGraphicsPath m;
1909 m.SetRefData( new wxMacCoreGraphicsPathData(this));
1910 return m;
1911 }
1912
1913
1914 // Matrix
1915
1916 wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1917 wxDouble tx, wxDouble ty)
1918 {
1919 wxGraphicsMatrix m;
1920 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
1921 data->Set( a,b,c,d,tx,ty ) ;
1922 m.SetRefData(data);
1923 return m;
1924 }
1925
1926 wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
1927 {
1928 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1929 return wxNullGraphicsPen;
1930 else
1931 {
1932 wxGraphicsPen p;
1933 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
1934 return p;
1935 }
1936 }
1937
1938 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
1939 {
1940 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1941 return wxNullGraphicsBrush;
1942 else
1943 {
1944 wxGraphicsBrush p;
1945 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
1946 return p;
1947 }
1948 }
1949
1950 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1951 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1952 const wxColour&c1, const wxColour&c2)
1953 {
1954 wxGraphicsBrush p;
1955 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
1956 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1957 p.SetRefData(d);
1958 return p;
1959 }
1960
1961 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1962 // with radius r and color cColor
1963 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1964 const wxColour &oColor, const wxColour &cColor)
1965 {
1966 wxGraphicsBrush p;
1967 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
1968 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1969 p.SetRefData(d);
1970 return p;
1971 }
1972
1973 // sets the font
1974 wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
1975 {
1976 if ( font.Ok() )
1977 {
1978 wxGraphicsFont p;
1979 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
1980 return p;
1981 }
1982 else
1983 return wxNullGraphicsFont;
1984 }
1985
1986
1987
1988 #endif // wxMAC_USE_CORE_GRAPHICS