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