]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/graphics.cpp
Document lack of wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK under OS X.
[wxWidgets.git] / src / osx / carbon / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/graphics.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/private.h"
39 #include "wx/osx/dcprint.h"
40 #include "wx/osx/dcclient.h"
41 #include "wx/osx/dcmemory.h"
42 #include "wx/osx/private.h"
43 #else
44 #include "CoreServices/CoreServices.h"
45 #include "ApplicationServices/ApplicationServices.h"
46 #include "wx/osx/core/cfstring.h"
47 #include "wx/cocoa/dcclient.h"
48 #endif
49
50 #ifdef __WXCOCOA__
51
52 CGColorSpaceRef wxMacGetGenericRGBColorSpace()
53 {
54 static wxCFRef<CGColorSpaceRef> genericRGBColorSpace;
55
56 if (genericRGBColorSpace == NULL)
57 {
58 genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) );
59 }
60
61 return genericRGBColorSpace;
62 }
63
64 int UMAGetSystemVersion()
65 {
66 return 0x1050;
67 }
68
69
70 #define wxOSX_USE_CORE_TEXT 1
71
72 #endif
73
74 #if wxOSX_USE_COCOA_OR_IPHONE
75 extern CGContextRef wxOSXGetContextFromCurrentContext() ;
76 #if wxOSX_USE_COCOA
77 extern bool wxOSXLockFocus( WXWidget view) ;
78 extern void wxOSXUnlockFocus( WXWidget view) ;
79 #endif
80 #endif
81
82 #if 1 // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
83
84 // TODO test whether this private API also works under 10.3
85
86 // copying values from NSCompositingModes (see also webkit and cairo sources)
87
88 typedef enum CGCompositeOperation {
89 kCGCompositeOperationClear = 0,
90 kCGCompositeOperationCopy = 1,
91 kCGCompositeOperationSourceOver = 2,
92 kCGCompositeOperationSourceIn = 3,
93 kCGCompositeOperationSourceOut = 4,
94 kCGCompositeOperationSourceAtop = 5,
95 kCGCompositeOperationDestinationOver = 6,
96 kCGCompositeOperationDestinationIn = 7,
97 kCGCompositeOperationDestinationOut = 8,
98 kCGCompositeOperationDestinationAtop = 9,
99 kCGCompositeOperationXOR = 10,
100 kCGCompositeOperationPlusDarker = 11,
101 // NS only, unsupported by CG : Highlight
102 kCGCompositeOperationPlusLighter = 12
103 } CGCompositeOperation ;
104
105 extern "C"
106 {
107 CG_EXTERN void CGContextSetCompositeOperation (CGContextRef context, int operation);
108 } ;
109
110 #endif
111
112 //-----------------------------------------------------------------------------
113 // constants
114 //-----------------------------------------------------------------------------
115
116 #ifndef M_PI
117 const double M_PI = 3.14159265358979;
118 #endif
119
120 static const double RAD2DEG = 180.0 / M_PI;
121
122 //
123 // Pen, Brushes and Fonts
124 //
125
126 #pragma mark -
127 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
128
129 OSStatus wxMacDrawCGImage(
130 CGContextRef inContext,
131 const CGRect * inBounds,
132 CGImageRef inImage)
133 {
134 #if wxOSX_USE_CARBON
135 return HIViewDrawCGImage( inContext, inBounds, inImage );
136 #else
137 CGContextSaveGState(inContext);
138 CGContextTranslateCTM(inContext, inBounds->origin.x, inBounds->origin.y + inBounds->size.height);
139 CGRect r = *inBounds;
140 r.origin.x = r.origin.y = 0;
141 CGContextScaleCTM(inContext, 1, -1);
142 CGContextDrawImage(inContext, r, inImage );
143 CGContextRestoreGState(inContext);
144 return noErr;
145 #endif
146 }
147
148 CGColorRef wxMacCreateCGColor( const wxColour& col )
149 {
150 CGColorRef retval = col.CreateCGColor();
151
152 wxASSERT(retval != NULL);
153 return retval;
154 }
155
156 #if wxOSX_USE_CORE_TEXT
157
158 CTFontRef wxMacCreateCTFont( const wxFont& font )
159 {
160 #ifdef __WXMAC__
161 return wxCFRetain((CTFontRef) font.OSXGetCTFont());
162 #else
163 return CTFontCreateWithName( wxCFStringRef( font.GetFaceName(), wxLocale::GetSystemEncoding() ) , font.GetPointSize() , NULL );
164 #endif
165 }
166
167 #endif
168
169 // CGPattern wrapper class: always allocate on heap, never call destructor
170
171 class wxMacCoreGraphicsPattern
172 {
173 public :
174 wxMacCoreGraphicsPattern() {}
175
176 // is guaranteed to be called only with a non-Null CGContextRef
177 virtual void Render( CGContextRef ctxRef ) = 0;
178
179 operator CGPatternRef() const { return m_patternRef; }
180
181 protected :
182 virtual ~wxMacCoreGraphicsPattern()
183 {
184 // as this is called only when the m_patternRef is been released;
185 // don't release it again
186 }
187
188 static void _Render( void *info, CGContextRef ctxRef )
189 {
190 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
191 if ( self && ctxRef )
192 self->Render( ctxRef );
193 }
194
195 static void _Dispose( void *info )
196 {
197 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
198 delete self;
199 }
200
201 CGPatternRef m_patternRef;
202
203 static const CGPatternCallbacks ms_Callbacks;
204 };
205
206 const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose };
207
208 class ImagePattern : public wxMacCoreGraphicsPattern
209 {
210 public :
211 ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform )
212 {
213 wxASSERT( bmp && bmp->IsOk() );
214 #ifdef __WXMAC__
215 Init( (CGImageRef) bmp->CreateCGImage() , transform );
216 #endif
217 }
218
219 // ImagePattern takes ownership of CGImageRef passed in
220 ImagePattern( CGImageRef image , const CGAffineTransform& transform )
221 {
222 if ( image )
223 CFRetain( image );
224
225 Init( image , transform );
226 }
227
228 virtual void Render( CGContextRef ctxRef )
229 {
230 if (m_image != NULL)
231 wxMacDrawCGImage( ctxRef, &m_imageBounds, m_image );
232 }
233
234 protected :
235 void Init( CGImageRef image, const CGAffineTransform& transform )
236 {
237 m_image = image;
238 if ( m_image )
239 {
240 m_imageBounds = CGRectMake( (CGFloat) 0.0, (CGFloat) 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) );
241 m_patternRef = CGPatternCreate(
242 this , m_imageBounds, transform ,
243 m_imageBounds.size.width, m_imageBounds.size.height,
244 kCGPatternTilingNoDistortion, true , &wxMacCoreGraphicsPattern::ms_Callbacks );
245 }
246 }
247
248 virtual ~ImagePattern()
249 {
250 if ( m_image )
251 CGImageRelease( m_image );
252 }
253
254 CGImageRef m_image;
255 CGRect m_imageBounds;
256 };
257
258 class HatchPattern : public wxMacCoreGraphicsPattern
259 {
260 public :
261 HatchPattern( int hatchstyle, const CGAffineTransform& transform )
262 {
263 m_hatch = hatchstyle;
264 m_imageBounds = CGRectMake( (CGFloat) 0.0, (CGFloat) 0.0, (CGFloat) 8.0 , (CGFloat) 8.0 );
265 m_patternRef = CGPatternCreate(
266 this , m_imageBounds, transform ,
267 m_imageBounds.size.width, m_imageBounds.size.height,
268 kCGPatternTilingNoDistortion, false , &wxMacCoreGraphicsPattern::ms_Callbacks );
269 }
270
271 void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
272 {
273 CGContextStrokeLineSegments( ctxRef , pts , count );
274 }
275
276 virtual void Render( CGContextRef ctxRef )
277 {
278 switch ( m_hatch )
279 {
280 case wxBDIAGONAL_HATCH :
281 {
282 CGPoint pts[] =
283 {
284 { (CGFloat) 8.0 , (CGFloat) 0.0 } , { (CGFloat) 0.0 , (CGFloat) 8.0 }
285 };
286 StrokeLineSegments( ctxRef , pts , 2 );
287 }
288 break;
289
290 case wxCROSSDIAG_HATCH :
291 {
292 CGPoint pts[] =
293 {
294 { (CGFloat) 0.0 , (CGFloat) 0.0 } , { (CGFloat) 8.0 , (CGFloat) 8.0 } ,
295 { (CGFloat) 8.0 , (CGFloat) 0.0 } , { (CGFloat) 0.0 , (CGFloat) 8.0 }
296 };
297 StrokeLineSegments( ctxRef , pts , 4 );
298 }
299 break;
300
301 case wxFDIAGONAL_HATCH :
302 {
303 CGPoint pts[] =
304 {
305 { (CGFloat) 0.0 , (CGFloat) 0.0 } , { (CGFloat) 8.0 , (CGFloat) 8.0 }
306 };
307 StrokeLineSegments( ctxRef , pts , 2 );
308 }
309 break;
310
311 case wxCROSS_HATCH :
312 {
313 CGPoint pts[] =
314 {
315 { (CGFloat) 0.0 , (CGFloat) 4.0 } , { (CGFloat) 8.0 , (CGFloat) 4.0 } ,
316 { (CGFloat) 4.0 , (CGFloat) 0.0 } , { (CGFloat) 4.0 , (CGFloat) 8.0 } ,
317 };
318 StrokeLineSegments( ctxRef , pts , 4 );
319 }
320 break;
321
322 case wxHORIZONTAL_HATCH :
323 {
324 CGPoint pts[] =
325 {
326 { (CGFloat) 0.0 , (CGFloat) 4.0 } , { (CGFloat) 8.0 , (CGFloat) 4.0 } ,
327 };
328 StrokeLineSegments( ctxRef , pts , 2 );
329 }
330 break;
331
332 case wxVERTICAL_HATCH :
333 {
334 CGPoint pts[] =
335 {
336 { (CGFloat) 4.0 , (CGFloat) 0.0 } , { (CGFloat) 4.0 , (CGFloat) 8.0 } ,
337 };
338 StrokeLineSegments( ctxRef , pts , 2 );
339 }
340 break;
341
342 default:
343 break;
344 }
345 }
346
347 protected :
348 virtual ~HatchPattern() {}
349
350 CGRect m_imageBounds;
351 int m_hatch;
352 };
353
354 class wxMacCoreGraphicsPenData : public wxGraphicsObjectRefData
355 {
356 public:
357 wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
358 ~wxMacCoreGraphicsPenData();
359
360 void Init();
361 virtual void Apply( wxGraphicsContext* context );
362 virtual wxDouble GetWidth() { return m_width; }
363
364 protected :
365 CGLineCap m_cap;
366 wxCFRef<CGColorRef> m_color;
367 wxCFRef<CGColorSpaceRef> m_colorSpace;
368
369 CGLineJoin m_join;
370 CGFloat m_width;
371
372 int m_count;
373 const CGFloat *m_lengths;
374 CGFloat *m_userLengths;
375
376
377 bool m_isPattern;
378 wxCFRef<CGPatternRef> m_pattern;
379 CGFloat* m_patternColorComponents;
380 };
381
382 wxMacCoreGraphicsPenData::wxMacCoreGraphicsPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) :
383 wxGraphicsObjectRefData( renderer )
384 {
385 Init();
386
387 m_color.reset( wxMacCreateCGColor( pen.GetColour() ) ) ;
388
389 // TODO: * m_dc->m_scaleX
390 m_width = pen.GetWidth();
391 if (m_width <= 0.0)
392 m_width = (CGFloat) 0.1;
393
394 switch ( pen.GetCap() )
395 {
396 case wxCAP_ROUND :
397 m_cap = kCGLineCapRound;
398 break;
399
400 case wxCAP_PROJECTING :
401 m_cap = kCGLineCapSquare;
402 break;
403
404 case wxCAP_BUTT :
405 m_cap = kCGLineCapButt;
406 break;
407
408 default :
409 m_cap = kCGLineCapButt;
410 break;
411 }
412
413 switch ( pen.GetJoin() )
414 {
415 case wxJOIN_BEVEL :
416 m_join = kCGLineJoinBevel;
417 break;
418
419 case wxJOIN_MITER :
420 m_join = kCGLineJoinMiter;
421 break;
422
423 case wxJOIN_ROUND :
424 m_join = kCGLineJoinRound;
425 break;
426
427 default :
428 m_join = kCGLineJoinMiter;
429 break;
430 }
431
432 const CGFloat dashUnit = m_width < 1.0 ? (CGFloat) 1.0 : m_width;
433
434 const CGFloat dotted[] = { (CGFloat) dashUnit , (CGFloat) (dashUnit + 2.0) };
435 static const CGFloat short_dashed[] = { (CGFloat) 9.0 , (CGFloat) 6.0 };
436 static const CGFloat dashed[] = { (CGFloat) 19.0 , (CGFloat) 9.0 };
437 static const CGFloat dotted_dashed[] = { (CGFloat) 9.0 , (CGFloat) 6.0 , (CGFloat) 3.0 , (CGFloat) 3.0 };
438
439 switch ( pen.GetStyle() )
440 {
441 case wxPENSTYLE_SOLID:
442 break;
443
444 case wxPENSTYLE_DOT:
445 m_count = WXSIZEOF(dotted);
446 m_userLengths = new CGFloat[ m_count ] ;
447 memcpy( m_userLengths, dotted, sizeof(dotted) );
448 m_lengths = m_userLengths;
449 break;
450
451 case wxPENSTYLE_LONG_DASH:
452 m_count = WXSIZEOF(dashed);
453 m_lengths = dashed;
454 break;
455
456 case wxPENSTYLE_SHORT_DASH:
457 m_count = WXSIZEOF(short_dashed);
458 m_lengths = short_dashed;
459 break;
460
461 case wxPENSTYLE_DOT_DASH:
462 m_count = WXSIZEOF(dotted_dashed);
463 m_lengths = dotted_dashed;
464 break;
465
466 case wxPENSTYLE_USER_DASH:
467 wxDash *dashes;
468 m_count = pen.GetDashes( &dashes );
469 if ((dashes != NULL) && (m_count > 0))
470 {
471 m_userLengths = new CGFloat[m_count];
472 for ( int i = 0; i < m_count; ++i )
473 {
474 m_userLengths[i] = dashes[i] * dashUnit;
475
476 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
477 m_userLengths[i] = (CGFloat) (dashUnit + 2.0);
478 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
479 m_userLengths[i] = dashUnit;
480 }
481 }
482 m_lengths = m_userLengths;
483 break;
484
485 case wxPENSTYLE_STIPPLE:
486 {
487 wxBitmap* bmp = pen.GetStipple();
488 if ( bmp && bmp->IsOk() )
489 {
490 m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) );
491 m_pattern.reset( (CGPatternRef) *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );
492 m_patternColorComponents = new CGFloat[1] ;
493 m_patternColorComponents[0] = (CGFloat) 1.0;
494 m_isPattern = true;
495 }
496 }
497 break;
498
499 default :
500 {
501 m_isPattern = true;
502 m_colorSpace.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
503 m_pattern.reset( (CGPatternRef) *( new HatchPattern( pen.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
504 m_patternColorComponents = new CGFloat[4] ;
505 m_patternColorComponents[0] = (CGFloat) (pen.GetColour().Red() / 255.0);
506 m_patternColorComponents[1] = (CGFloat) (pen.GetColour().Green() / 255.0);
507 m_patternColorComponents[2] = (CGFloat) (pen.GetColour().Blue() / 255.0);
508 m_patternColorComponents[3] = (CGFloat) (pen.GetColour().Alpha() / 255.0);
509 }
510 break;
511 }
512 if ((m_lengths != NULL) && (m_count > 0))
513 {
514 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
515 m_cap = kCGLineCapButt;
516 }
517 }
518
519 wxMacCoreGraphicsPenData::~wxMacCoreGraphicsPenData()
520 {
521 delete[] m_userLengths;
522 delete[] m_patternColorComponents;
523 }
524
525 void wxMacCoreGraphicsPenData::Init()
526 {
527 m_lengths = NULL;
528 m_userLengths = NULL;
529 m_width = 0;
530 m_count = 0;
531 m_patternColorComponents = NULL;
532 m_isPattern = false;
533 }
534
535 void wxMacCoreGraphicsPenData::Apply( wxGraphicsContext* context )
536 {
537 CGContextRef cg = (CGContextRef) context->GetNativeContext();
538 CGContextSetLineWidth( cg , m_width );
539 CGContextSetLineJoin( cg , m_join );
540
541 CGContextSetLineDash( cg , 0 , m_lengths , m_count );
542 CGContextSetLineCap( cg , m_cap );
543
544 if ( m_isPattern )
545 {
546 CGAffineTransform matrix = CGContextGetCTM( cg );
547 CGContextSetPatternPhase( cg, CGSizeMake(matrix.tx, matrix.ty) );
548 CGContextSetStrokeColorSpace( cg , m_colorSpace );
549 CGContextSetStrokePattern( cg, m_pattern , m_patternColorComponents );
550 }
551 else
552 {
553 CGContextSetStrokeColorWithColor( cg , m_color );
554 }
555 }
556
557 //
558 // Brush
559 //
560
561 // make sure we all use one class for all conversions from wx to native colour
562
563 class wxMacCoreGraphicsColour
564 {
565 public:
566 wxMacCoreGraphicsColour();
567 wxMacCoreGraphicsColour(const wxBrush &brush);
568 ~wxMacCoreGraphicsColour();
569
570 void Apply( CGContextRef cgContext );
571 protected:
572 void Init();
573 wxCFRef<CGColorRef> m_color;
574 wxCFRef<CGColorSpaceRef> m_colorSpace;
575
576 bool m_isPattern;
577 wxCFRef<CGPatternRef> m_pattern;
578 CGFloat* m_patternColorComponents;
579 } ;
580
581 wxMacCoreGraphicsColour::~wxMacCoreGraphicsColour()
582 {
583 delete[] m_patternColorComponents;
584 }
585
586 void wxMacCoreGraphicsColour::Init()
587 {
588 m_isPattern = false;
589 m_patternColorComponents = NULL;
590 }
591
592 void wxMacCoreGraphicsColour::Apply( CGContextRef cgContext )
593 {
594 if ( m_isPattern )
595 {
596 CGAffineTransform matrix = CGContextGetCTM( cgContext );
597 CGContextSetPatternPhase( cgContext, CGSizeMake(matrix.tx, matrix.ty) );
598 CGContextSetFillColorSpace( cgContext , m_colorSpace );
599 CGContextSetFillPattern( cgContext, m_pattern , m_patternColorComponents );
600 }
601 else
602 {
603 CGContextSetFillColorWithColor( cgContext, m_color );
604 }
605 }
606
607 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour()
608 {
609 Init();
610 }
611
612 wxMacCoreGraphicsColour::wxMacCoreGraphicsColour( const wxBrush &brush )
613 {
614 Init();
615 if ( brush.GetStyle() == wxSOLID )
616 {
617 m_color.reset( wxMacCreateCGColor( brush.GetColour() ));
618 }
619 else if ( brush.IsHatch() )
620 {
621 m_isPattern = true;
622 m_colorSpace.reset( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
623 m_pattern.reset( (CGPatternRef) *( new HatchPattern( brush.GetStyle() , CGAffineTransformMakeScale( 1,-1 ) ) ) );
624
625 m_patternColorComponents = new CGFloat[4] ;
626 m_patternColorComponents[0] = (CGFloat) (brush.GetColour().Red() / 255.0);
627 m_patternColorComponents[1] = (CGFloat) (brush.GetColour().Green() / 255.0);
628 m_patternColorComponents[2] = (CGFloat) (brush.GetColour().Blue() / 255.0);
629 m_patternColorComponents[3] = (CGFloat) (brush.GetColour().Alpha() / 255.0);
630 }
631 else
632 {
633 // now brush is a bitmap
634 wxBitmap* bmp = brush.GetStipple();
635 if ( bmp && bmp->IsOk() )
636 {
637 m_isPattern = true;
638 m_patternColorComponents = new CGFloat[1] ;
639 m_patternColorComponents[0] = (CGFloat) 1.0;
640 m_colorSpace.reset( CGColorSpaceCreatePattern( NULL ) );
641 m_pattern.reset( (CGPatternRef) *( new ImagePattern( bmp , CGAffineTransformMakeScale( 1,-1 ) ) ) );
642 }
643 }
644 }
645
646 class wxMacCoreGraphicsBrushData : public wxGraphicsObjectRefData
647 {
648 public:
649 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer );
650 wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
651 ~wxMacCoreGraphicsBrushData ();
652
653 virtual void Apply( wxGraphicsContext* context );
654 void CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
655 wxDouble x2, wxDouble y2,
656 const wxGraphicsGradientStops& stops);
657 void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
658 wxDouble xc, wxDouble yc, wxDouble radius,
659 const wxGraphicsGradientStops& stops);
660
661 virtual bool IsShading() { return m_isShading; }
662 CGShadingRef GetShading() { return m_shading; }
663 protected:
664 CGFunctionRef CreateGradientFunction(const wxGraphicsGradientStops& stops);
665
666 static void CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out);
667 virtual void Init();
668
669 wxMacCoreGraphicsColour m_cgColor;
670
671 bool m_isShading;
672 CGFunctionRef m_gradientFunction;
673 CGShadingRef m_shading;
674
675 // information about a single gradient component
676 struct GradientComponent
677 {
678 CGFloat pos;
679 CGFloat red;
680 CGFloat green;
681 CGFloat blue;
682 CGFloat alpha;
683 };
684
685 // and information about all of them
686 struct GradientComponents
687 {
688 GradientComponents()
689 {
690 count = 0;
691 comps = NULL;
692 }
693
694 void Init(unsigned count_)
695 {
696 count = count_;
697 comps = new GradientComponent[count];
698 }
699
700 ~GradientComponents()
701 {
702 delete [] comps;
703 }
704
705 unsigned count;
706 GradientComponent *comps;
707 };
708
709 GradientComponents m_gradientComponents;
710 };
711
712 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxGraphicsObjectRefData( renderer )
713 {
714 Init();
715 }
716
717 void
718 wxMacCoreGraphicsBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
719 wxDouble x2, wxDouble y2,
720 const wxGraphicsGradientStops& stops)
721 {
722 m_gradientFunction = CreateGradientFunction(stops);
723 m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) x1, (CGFloat) y1),
724 CGPointMake((CGFloat) x2,(CGFloat) y2), m_gradientFunction, true, true ) ;
725 m_isShading = true ;
726 }
727
728 void
729 wxMacCoreGraphicsBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
730 wxDouble xc, wxDouble yc,
731 wxDouble radius,
732 const wxGraphicsGradientStops& stops)
733 {
734 m_gradientFunction = CreateGradientFunction(stops);
735 m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) xo,(CGFloat) yo), 0,
736 CGPointMake((CGFloat) xc,(CGFloat) yc), (CGFloat) radius, m_gradientFunction, true, true ) ;
737 m_isShading = true ;
738 }
739
740 wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxGraphicsObjectRefData( renderer ),
741 m_cgColor( brush )
742 {
743 Init();
744
745 }
746
747 wxMacCoreGraphicsBrushData::~wxMacCoreGraphicsBrushData()
748 {
749 if ( m_shading )
750 CGShadingRelease(m_shading);
751
752 if( m_gradientFunction )
753 CGFunctionRelease(m_gradientFunction);
754 }
755
756 void wxMacCoreGraphicsBrushData::Init()
757 {
758 m_gradientFunction = NULL;
759 m_shading = NULL;
760 m_isShading = false;
761 }
762
763 void wxMacCoreGraphicsBrushData::Apply( wxGraphicsContext* context )
764 {
765 CGContextRef cg = (CGContextRef) context->GetNativeContext();
766
767 if ( m_isShading )
768 {
769 // nothing to set as shades are processed by clipping using the path and filling
770 }
771 else
772 {
773 m_cgColor.Apply( cg );
774 }
775 }
776
777 void wxMacCoreGraphicsBrushData::CalculateShadingValues (void *info, const CGFloat *in, CGFloat *out)
778 {
779 const GradientComponents& stops = *(GradientComponents*) info ;
780
781 CGFloat f = *in;
782 if (f <= 0.0)
783 {
784 // Start
785 out[0] = stops.comps[0].red;
786 out[1] = stops.comps[0].green;
787 out[2] = stops.comps[0].blue;
788 out[3] = stops.comps[0].alpha;
789 }
790 else if (f >= 1.0)
791 {
792 // end
793 out[0] = stops.comps[stops.count - 1].red;
794 out[1] = stops.comps[stops.count - 1].green;
795 out[2] = stops.comps[stops.count - 1].blue;
796 out[3] = stops.comps[stops.count - 1].alpha;
797 }
798 else
799 {
800 // Find first component with position greater than f
801 unsigned i;
802 for ( i = 0; i < stops.count; i++ )
803 {
804 if (stops.comps[i].pos > f)
805 break;
806 }
807
808 // Interpolated between stops
809 CGFloat diff = (f - stops.comps[i-1].pos);
810 CGFloat range = (stops.comps[i].pos - stops.comps[i-1].pos);
811 CGFloat fact = diff / range;
812
813 out[0] = stops.comps[i - 1].red + (stops.comps[i].red - stops.comps[i - 1].red) * fact;
814 out[1] = stops.comps[i - 1].green + (stops.comps[i].green - stops.comps[i - 1].green) * fact;
815 out[2] = stops.comps[i - 1].blue + (stops.comps[i].blue - stops.comps[i - 1].blue) * fact;
816 out[3] = stops.comps[i - 1].alpha + (stops.comps[i].alpha - stops.comps[i - 1].alpha) * fact;
817 }
818 }
819
820 CGFunctionRef
821 wxMacCoreGraphicsBrushData::CreateGradientFunction(const wxGraphicsGradientStops& stops)
822 {
823
824 static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, NULL };
825 static const CGFloat input_value_range [2] = { 0, 1 };
826 static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
827
828 m_gradientComponents.Init(stops.GetCount());
829 for ( unsigned i = 0; i < m_gradientComponents.count; i++ )
830 {
831 const wxGraphicsGradientStop stop = stops.Item(i);
832
833 m_gradientComponents.comps[i].pos = stop.GetPosition();
834
835 const wxColour col = stop.GetColour();
836 m_gradientComponents.comps[i].red = (CGFloat) (col.Red() / 255.0);
837 m_gradientComponents.comps[i].green = (CGFloat) (col.Green() / 255.0);
838 m_gradientComponents.comps[i].blue = (CGFloat) (col.Blue() / 255.0);
839 m_gradientComponents.comps[i].alpha = (CGFloat) (col.Alpha() / 255.0);
840 }
841
842 return CGFunctionCreate ( &m_gradientComponents, 1,
843 input_value_range,
844 4,
845 output_value_ranges,
846 &callbacks);
847 }
848
849 //
850 // Font
851 //
852
853 #if wxOSX_USE_IPHONE
854
855 extern UIFont* CreateUIFont( const wxFont& font );
856 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text );
857 extern CGSize MeasureTextInContext( UIFont *font, NSString* text );
858
859 #endif
860
861 class wxMacCoreGraphicsFontData : public wxGraphicsObjectRefData
862 {
863 public:
864 wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
865 ~wxMacCoreGraphicsFontData();
866
867 #if wxOSX_USE_ATSU_TEXT
868 virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; }
869 #endif
870 #if wxOSX_USE_CORE_TEXT
871 CTFontRef OSXGetCTFont() const { return m_ctFont ; }
872 #endif
873 wxColour GetColour() const { return m_colour ; }
874
875 bool GetUnderlined() const { return m_underlined ; }
876 #if wxOSX_USE_IPHONE
877 UIFont* GetUIFont() const { return m_uiFont; }
878 #endif
879 private :
880 wxColour m_colour;
881 bool m_underlined;
882 #if wxOSX_USE_ATSU_TEXT
883 ATSUStyle m_macATSUIStyle;
884 #endif
885 #if wxOSX_USE_CORE_TEXT
886 wxCFRef< CTFontRef > m_ctFont;
887 #endif
888 #if wxOSX_USE_IPHONE
889 UIFont* m_uiFont;
890 #endif
891 };
892
893 wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer )
894 {
895 m_colour = col;
896 m_underlined = font.GetUnderlined();
897
898 #if wxOSX_USE_CORE_TEXT
899 m_ctFont.reset( wxMacCreateCTFont( font ) );
900 #endif
901 #if wxOSX_USE_IPHONE
902 m_uiFont = CreateUIFont(font);
903 wxMacCocoaRetain( m_uiFont );
904 #endif
905 #if wxOSX_USE_ATSU_TEXT
906 OSStatus status = noErr;
907 m_macATSUIStyle = NULL;
908
909 status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle );
910
911 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );
912
913 // we need the scale here ...
914
915 Fixed atsuSize = IntToFixed( int( 1 * font.GetPointSize()) );
916 RGBColor atsuColor ;
917 col.GetRGBColor( &atsuColor );
918 ATSUAttributeTag atsuTags[] =
919 {
920 kATSUSizeTag ,
921 kATSUColorTag ,
922 };
923 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
924 {
925 sizeof( Fixed ) ,
926 sizeof( RGBColor ) ,
927 };
928 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
929 {
930 &atsuSize ,
931 &atsuColor ,
932 };
933
934 status = ::ATSUSetAttributes(
935 m_macATSUIStyle, WXSIZEOF(atsuTags),
936 atsuTags, atsuSizes, atsuValues);
937
938 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
939 #endif
940 }
941
942 wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
943 {
944 #if wxOSX_USE_CORE_TEXT
945 #endif
946 #if wxOSX_USE_ATSU_TEXT
947 if ( m_macATSUIStyle )
948 {
949 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
950 m_macATSUIStyle = NULL;
951 }
952 #endif
953 #if wxOSX_USE_IPHONE
954 wxMacCocoaRelease( m_uiFont );
955 #endif
956 }
957
958 class wxMacCoreGraphicsBitmapData : public wxGraphicsBitmapData
959 {
960 public:
961 wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome );
962 ~wxMacCoreGraphicsBitmapData();
963
964 virtual CGImageRef GetBitmap() { return m_bitmap; }
965 virtual void* GetNativeBitmap() const { return m_bitmap; }
966 bool IsMonochrome() { return m_monochrome; }
967
968 #if wxUSE_IMAGE
969 wxImage ConvertToImage() const
970 {
971 return wxBitmap(m_bitmap).ConvertToImage();
972 }
973 #endif // wxUSE_IMAGE
974
975 private :
976 CGImageRef m_bitmap;
977 bool m_monochrome;
978 };
979
980 wxMacCoreGraphicsBitmapData::wxMacCoreGraphicsBitmapData( wxGraphicsRenderer* renderer, CGImageRef bitmap, bool monochrome ) : wxGraphicsBitmapData( renderer ),
981 m_bitmap(bitmap), m_monochrome(monochrome)
982 {
983 }
984
985 wxMacCoreGraphicsBitmapData::~wxMacCoreGraphicsBitmapData()
986 {
987 CGImageRelease( m_bitmap );
988 }
989
990
991 //
992 // Graphics Matrix
993 //
994
995 //-----------------------------------------------------------------------------
996 // wxMacCoreGraphicsMatrix declaration
997 //-----------------------------------------------------------------------------
998
999 class WXDLLIMPEXP_CORE wxMacCoreGraphicsMatrixData : public wxGraphicsMatrixData
1000 {
1001 public :
1002 wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) ;
1003
1004 virtual ~wxMacCoreGraphicsMatrixData() ;
1005
1006 virtual wxGraphicsObjectRefData *Clone() const ;
1007
1008 // concatenates the matrix
1009 virtual void Concat( const wxGraphicsMatrixData *t );
1010
1011 // sets the matrix to the respective values
1012 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1013 wxDouble tx=0.0, wxDouble ty=0.0);
1014
1015 // gets the component valuess of the matrix
1016 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
1017 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
1018
1019 // makes this the inverse matrix
1020 virtual void Invert();
1021
1022 // returns true if the elements of the transformation matrix are equal ?
1023 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
1024
1025 // return true if this is the identity matrix
1026 virtual bool IsIdentity() const;
1027
1028 //
1029 // transformation
1030 //
1031
1032 // add the translation to this matrix
1033 virtual void Translate( wxDouble dx , wxDouble dy );
1034
1035 // add the scale to this matrix
1036 virtual void Scale( wxDouble xScale , wxDouble yScale );
1037
1038 // add the rotation to this matrix (radians)
1039 virtual void Rotate( wxDouble angle );
1040
1041 //
1042 // apply the transforms
1043 //
1044
1045 // applies that matrix to the point
1046 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
1047
1048 // applies the matrix except for translations
1049 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
1050
1051 // returns the native representation
1052 virtual void * GetNativeMatrix() const;
1053
1054 private :
1055 CGAffineTransform m_matrix;
1056 } ;
1057
1058 //-----------------------------------------------------------------------------
1059 // wxMacCoreGraphicsMatrix implementation
1060 //-----------------------------------------------------------------------------
1061
1062 wxMacCoreGraphicsMatrixData::wxMacCoreGraphicsMatrixData(wxGraphicsRenderer* renderer) : wxGraphicsMatrixData(renderer)
1063 {
1064 }
1065
1066 wxMacCoreGraphicsMatrixData::~wxMacCoreGraphicsMatrixData()
1067 {
1068 }
1069
1070 wxGraphicsObjectRefData *wxMacCoreGraphicsMatrixData::Clone() const
1071 {
1072 wxMacCoreGraphicsMatrixData* m = new wxMacCoreGraphicsMatrixData(GetRenderer()) ;
1073 m->m_matrix = m_matrix ;
1074 return m;
1075 }
1076
1077 // concatenates the matrix
1078 void wxMacCoreGraphicsMatrixData::Concat( const wxGraphicsMatrixData *t )
1079 {
1080 m_matrix = CGAffineTransformConcat(*((CGAffineTransform*) t->GetNativeMatrix()), m_matrix );
1081 }
1082
1083 // sets the matrix to the respective values
1084 void wxMacCoreGraphicsMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1085 wxDouble tx, wxDouble ty)
1086 {
1087 m_matrix = CGAffineTransformMake((CGFloat) a,(CGFloat) b,(CGFloat) c,(CGFloat) d,(CGFloat) tx,(CGFloat) ty);
1088 }
1089
1090 // gets the component valuess of the matrix
1091 void wxMacCoreGraphicsMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1092 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1093 {
1094 if (a) *a = m_matrix.a;
1095 if (b) *b = m_matrix.b;
1096 if (c) *c = m_matrix.c;
1097 if (d) *d = m_matrix.d;
1098 if (tx) *tx= m_matrix.tx;
1099 if (ty) *ty= m_matrix.ty;
1100 }
1101
1102 // makes this the inverse matrix
1103 void wxMacCoreGraphicsMatrixData::Invert()
1104 {
1105 m_matrix = CGAffineTransformInvert( m_matrix );
1106 }
1107
1108 // returns true if the elements of the transformation matrix are equal ?
1109 bool wxMacCoreGraphicsMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
1110 {
1111 return CGAffineTransformEqualToTransform(m_matrix, *((CGAffineTransform*) t->GetNativeMatrix()));
1112 }
1113
1114 // return true if this is the identity matrix
1115 bool wxMacCoreGraphicsMatrixData::IsIdentity() const
1116 {
1117 return ( m_matrix.a == 1 && m_matrix.d == 1 &&
1118 m_matrix.b == 0 && m_matrix.d == 0 && m_matrix.tx == 0 && m_matrix.ty == 0);
1119 }
1120
1121 //
1122 // transformation
1123 //
1124
1125 // add the translation to this matrix
1126 void wxMacCoreGraphicsMatrixData::Translate( wxDouble dx , wxDouble dy )
1127 {
1128 m_matrix = CGAffineTransformTranslate( m_matrix, (CGFloat) dx, (CGFloat) dy);
1129 }
1130
1131 // add the scale to this matrix
1132 void wxMacCoreGraphicsMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1133 {
1134 m_matrix = CGAffineTransformScale( m_matrix, (CGFloat) xScale, (CGFloat) yScale);
1135 }
1136
1137 // add the rotation to this matrix (radians)
1138 void wxMacCoreGraphicsMatrixData::Rotate( wxDouble angle )
1139 {
1140 m_matrix = CGAffineTransformRotate( m_matrix, (CGFloat) angle);
1141 }
1142
1143 //
1144 // apply the transforms
1145 //
1146
1147 // applies that matrix to the point
1148 void wxMacCoreGraphicsMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1149 {
1150 CGPoint pt = CGPointApplyAffineTransform( CGPointMake((CGFloat) *x,(CGFloat) *y), m_matrix);
1151
1152 *x = pt.x;
1153 *y = pt.y;
1154 }
1155
1156 // applies the matrix except for translations
1157 void wxMacCoreGraphicsMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1158 {
1159 CGSize sz = CGSizeApplyAffineTransform( CGSizeMake((CGFloat) *dx,(CGFloat) *dy) , m_matrix );
1160 *dx = sz.width;
1161 *dy = sz.height;
1162 }
1163
1164 // returns the native representation
1165 void * wxMacCoreGraphicsMatrixData::GetNativeMatrix() const
1166 {
1167 return (void*) &m_matrix;
1168 }
1169
1170 //
1171 // Graphics Path
1172 //
1173
1174 //-----------------------------------------------------------------------------
1175 // wxMacCoreGraphicsPath declaration
1176 //-----------------------------------------------------------------------------
1177
1178 class WXDLLEXPORT wxMacCoreGraphicsPathData : public wxGraphicsPathData
1179 {
1180 public :
1181 wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path = NULL);
1182
1183 ~wxMacCoreGraphicsPathData();
1184
1185 virtual wxGraphicsObjectRefData *Clone() const;
1186
1187 // begins a new subpath at (x,y)
1188 virtual void MoveToPoint( wxDouble x, wxDouble y );
1189
1190 // adds a straight line from the current point to (x,y)
1191 virtual void AddLineToPoint( wxDouble x, wxDouble y );
1192
1193 // adds a cubic Bezier curve from the current point, using two control points and an end point
1194 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
1195
1196 // closes the current sub-path
1197 virtual void CloseSubpath();
1198
1199 // gets the last point of the current path, (0,0) if not yet set
1200 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
1201
1202 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1203 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
1204
1205 //
1206 // These are convenience functions which - if not available natively will be assembled
1207 // using the primitives from above
1208 //
1209
1210 // adds a quadratic Bezier curve from the current point, using a control point and an end point
1211 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
1212
1213 // appends a rectangle as a new closed subpath
1214 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1215
1216 // appends a circle as a new closed subpath
1217 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
1218
1219 // appends an ellipsis as a new closed subpath fitting the passed rectangle
1220 virtual void AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
1221
1222 // 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)
1223 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
1224
1225 // adds another path
1226 virtual void AddPath( const wxGraphicsPathData* path );
1227
1228 // returns the native path
1229 virtual void * GetNativePath() const { return m_path; }
1230
1231 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
1232 virtual void UnGetNativePath(void *WXUNUSED(p)) const {}
1233
1234 // transforms each point of this path by the matrix
1235 virtual void Transform( const wxGraphicsMatrixData* matrix );
1236
1237 // gets the bounding box enclosing all points (possibly including control points)
1238 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
1239
1240 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE) const;
1241 private :
1242 CGMutablePathRef m_path;
1243 };
1244
1245 //-----------------------------------------------------------------------------
1246 // wxMacCoreGraphicsPath implementation
1247 //-----------------------------------------------------------------------------
1248
1249 wxMacCoreGraphicsPathData::wxMacCoreGraphicsPathData( wxGraphicsRenderer* renderer, CGMutablePathRef path) : wxGraphicsPathData(renderer)
1250 {
1251 if ( path )
1252 m_path = path;
1253 else
1254 m_path = CGPathCreateMutable();
1255 }
1256
1257 wxMacCoreGraphicsPathData::~wxMacCoreGraphicsPathData()
1258 {
1259 CGPathRelease( m_path );
1260 }
1261
1262 wxGraphicsObjectRefData* wxMacCoreGraphicsPathData::Clone() const
1263 {
1264 wxMacCoreGraphicsPathData* clone = new wxMacCoreGraphicsPathData(GetRenderer(),CGPathCreateMutableCopy(m_path));
1265 return clone ;
1266 }
1267
1268
1269 // opens (starts) a new subpath
1270 void wxMacCoreGraphicsPathData::MoveToPoint( wxDouble x1 , wxDouble y1 )
1271 {
1272 CGPathMoveToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1273 }
1274
1275 void wxMacCoreGraphicsPathData::AddLineToPoint( wxDouble x1 , wxDouble y1 )
1276 {
1277 CGPathAddLineToPoint( m_path , NULL , (CGFloat) x1 , (CGFloat) y1 );
1278 }
1279
1280 void wxMacCoreGraphicsPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
1281 {
1282 CGPathAddCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) cx2, (CGFloat) cy2, (CGFloat) x , (CGFloat) y );
1283 }
1284
1285 void wxMacCoreGraphicsPathData::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
1286 {
1287 CGPathAddQuadCurveToPoint( m_path , NULL , (CGFloat) cx1 , (CGFloat) cy1 , (CGFloat) x , (CGFloat) y );
1288 }
1289
1290 void wxMacCoreGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1291 {
1292 CGRect cgRect = { { (CGFloat) x , (CGFloat) y } , { (CGFloat) w , (CGFloat) h } };
1293 CGPathAddRect( m_path , NULL , cgRect );
1294 }
1295
1296 void wxMacCoreGraphicsPathData::AddCircle( wxDouble x, wxDouble y , wxDouble r )
1297 {
1298 CGPathAddEllipseInRect( m_path, NULL, CGRectMake(x-r,y-r,2*r,2*r));
1299 }
1300
1301 void wxMacCoreGraphicsPathData::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1302 {
1303 CGPathAddEllipseInRect( m_path, NULL, CGRectMake(x,y,w,h));
1304 }
1305
1306 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
1307 void wxMacCoreGraphicsPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
1308 {
1309 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
1310 CGPathAddArc( m_path, NULL , (CGFloat) x, (CGFloat) y, (CGFloat) r, (CGFloat) startAngle, (CGFloat) endAngle, !clockwise);
1311 }
1312
1313 void wxMacCoreGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
1314 {
1315 CGPathAddArcToPoint( m_path, NULL , (CGFloat) x1, (CGFloat) y1, (CGFloat) x2, (CGFloat) y2, (CGFloat) r);
1316 }
1317
1318 void wxMacCoreGraphicsPathData::AddPath( const wxGraphicsPathData* path )
1319 {
1320 CGPathAddPath( m_path , NULL, (CGPathRef) path->GetNativePath() );
1321 }
1322
1323 // closes the current subpath
1324 void wxMacCoreGraphicsPathData::CloseSubpath()
1325 {
1326 CGPathCloseSubpath( m_path );
1327 }
1328
1329 // gets the last point of the current path, (0,0) if not yet set
1330 void wxMacCoreGraphicsPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
1331 {
1332 CGPoint p = CGPathGetCurrentPoint( m_path );
1333 *x = p.x;
1334 *y = p.y;
1335 }
1336
1337 // transforms each point of this path by the matrix
1338 void wxMacCoreGraphicsPathData::Transform( const wxGraphicsMatrixData* matrix )
1339 {
1340 CGMutablePathRef p = CGPathCreateMutable() ;
1341 CGPathAddPath( p, (CGAffineTransform*) matrix->GetNativeMatrix() , m_path );
1342 CGPathRelease( m_path );
1343 m_path = p;
1344 }
1345
1346 // gets the bounding box enclosing all points (possibly including control points)
1347 void wxMacCoreGraphicsPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
1348 {
1349 CGRect bounds = CGPathGetBoundingBox( m_path ) ;
1350 *x = bounds.origin.x;
1351 *y = bounds.origin.y;
1352 *w = bounds.size.width;
1353 *h = bounds.size.height;
1354 }
1355
1356 bool wxMacCoreGraphicsPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle) const
1357 {
1358 return CGPathContainsPoint( m_path, NULL, CGPointMake((CGFloat) x,(CGFloat) y), fillStyle == wxODDEVEN_RULE );
1359 }
1360
1361 //
1362 // Graphics Context
1363 //
1364
1365 //-----------------------------------------------------------------------------
1366 // wxMacCoreGraphicsContext declaration
1367 //-----------------------------------------------------------------------------
1368
1369 class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
1370 {
1371 public:
1372 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width = 0, wxDouble height = 0 );
1373
1374 #if wxOSX_USE_CARBON
1375 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window );
1376 #endif
1377
1378 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window );
1379
1380 wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer);
1381
1382 ~wxMacCoreGraphicsContext();
1383
1384 void Init();
1385
1386 virtual void StartPage( wxDouble width, wxDouble height );
1387
1388 virtual void EndPage();
1389
1390 virtual void Flush();
1391
1392 // push the current state of the context, ie the transformation matrix on a stack
1393 virtual void PushState();
1394
1395 // pops a stored state from the stack
1396 virtual void PopState();
1397
1398 // clips drawings to the region
1399 virtual void Clip( const wxRegion &region );
1400
1401 // clips drawings to the rect
1402 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1403
1404 // resets the clipping to original extent
1405 virtual void ResetClip();
1406
1407 virtual void * GetNativeContext();
1408
1409 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
1410
1411 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation);
1412
1413 virtual bool SetCompositionMode(wxCompositionMode op);
1414
1415 virtual void BeginLayer(wxDouble opacity);
1416
1417 virtual void EndLayer();
1418
1419 //
1420 // transformation
1421 //
1422
1423 // translate
1424 virtual void Translate( wxDouble dx , wxDouble dy );
1425
1426 // scale
1427 virtual void Scale( wxDouble xScale , wxDouble yScale );
1428
1429 // rotate (radians)
1430 virtual void Rotate( wxDouble angle );
1431
1432 // concatenates this transform with the current transform of this context
1433 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
1434
1435 // sets the transform of this context
1436 virtual void SetTransform( const wxGraphicsMatrix& matrix );
1437
1438 // gets the matrix of this context
1439 virtual wxGraphicsMatrix GetTransform() const;
1440 //
1441 // setting the paint
1442 //
1443
1444 // strokes along a path with the current pen
1445 virtual void StrokePath( const wxGraphicsPath &path );
1446
1447 // fills a path with the current brush
1448 virtual void FillPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
1449
1450 // draws a path by first filling and then stroking
1451 virtual void DrawPath( const wxGraphicsPath &path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
1452
1453 virtual bool ShouldOffset() const
1454 {
1455 if ( !m_enableOffset )
1456 return false;
1457
1458 int penwidth = 0 ;
1459 if ( !m_pen.IsNull() )
1460 {
1461 penwidth = (int)((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->GetWidth();
1462 if ( penwidth == 0 )
1463 penwidth = 1;
1464 }
1465 return ( penwidth % 2 ) == 1;
1466 }
1467 //
1468 // text
1469 //
1470
1471 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
1472 wxDouble *descent, wxDouble *externalLeading ) const;
1473
1474 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
1475
1476 //
1477 // image support
1478 //
1479
1480 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1481
1482 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1483
1484 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1485
1486 // fast convenience methods
1487
1488
1489 virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1490
1491 void SetNativeContext( CGContextRef cg );
1492
1493 wxDECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext);
1494
1495 private:
1496 bool EnsureIsValid();
1497 void CheckInvariants() const;
1498
1499 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
1500 virtual void DoDrawRotatedText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
1501
1502 CGContextRef m_cgContext;
1503 #if wxOSX_USE_CARBON
1504 WindowRef m_windowRef;
1505 #else
1506 WXWidget m_view;
1507 #endif
1508 bool m_contextSynthesized;
1509 CGAffineTransform m_windowTransform;
1510 bool m_invisible;
1511
1512 #if wxOSX_USE_COCOA_OR_CARBON
1513 wxCFRef<HIShapeRef> m_clipRgn;
1514 #endif
1515 };
1516
1517 //-----------------------------------------------------------------------------
1518 // device context implementation
1519 //
1520 // more and more of the dc functionality should be implemented by calling
1521 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1522 // also coordinate conversions should be moved to native matrix ops
1523 //-----------------------------------------------------------------------------
1524
1525 // we always stock two context states, one at entry, to be able to preserve the
1526 // state we were called with, the other one after changing to HI Graphics orientation
1527 // (this one is used for getting back clippings etc)
1528
1529 //-----------------------------------------------------------------------------
1530 // wxMacCoreGraphicsContext implementation
1531 //-----------------------------------------------------------------------------
1532
1533 class wxQuartzOffsetHelper
1534 {
1535 public :
1536 wxQuartzOffsetHelper( CGContextRef cg , bool offset )
1537 {
1538 m_cg = cg;
1539 m_offset = offset;
1540 if ( m_offset )
1541 {
1542 m_userOffset = CGContextConvertSizeToUserSpace( m_cg, CGSizeMake( 0.5 , 0.5 ) );
1543 CGContextTranslateCTM( m_cg, m_userOffset.width , m_userOffset.height );
1544 }
1545 else
1546 {
1547 m_userOffset = CGSizeMake(0.0, 0.0);
1548 }
1549
1550 }
1551 ~wxQuartzOffsetHelper( )
1552 {
1553 if ( m_offset )
1554 CGContextTranslateCTM( m_cg, -m_userOffset.width , -m_userOffset.height );
1555 }
1556 public :
1557 CGSize m_userOffset;
1558 CGContextRef m_cg;
1559 bool m_offset;
1560 } ;
1561
1562 void wxMacCoreGraphicsContext::Init()
1563 {
1564 m_cgContext = NULL;
1565 m_contextSynthesized = false;
1566 m_width = 0;
1567 m_height = 0;
1568 #if wxOSX_USE_CARBON
1569 m_windowRef = NULL;
1570 #endif
1571 #if wxOSX_USE_COCOA_OR_IPHONE
1572 m_view = NULL;
1573 #endif
1574 m_invisible = false;
1575 m_antialias = wxANTIALIAS_DEFAULT;
1576 m_interpolation = wxINTERPOLATION_DEFAULT;
1577 }
1578
1579 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width, wxDouble height ) : wxGraphicsContext(renderer)
1580 {
1581 Init();
1582 SetNativeContext(cgcontext);
1583 m_width = width;
1584 m_height = height;
1585 }
1586
1587 #if wxOSX_USE_CARBON
1588 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer)
1589 {
1590 Init();
1591 m_windowRef = window;
1592 m_enableOffset = true;
1593 }
1594 #endif
1595
1596 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer)
1597 {
1598 Init();
1599
1600 m_enableOffset = true;
1601 wxSize sz = window->GetSize();
1602 m_width = sz.x;
1603 m_height = sz.y;
1604
1605 #if wxOSX_USE_COCOA_OR_IPHONE
1606 m_view = window->GetHandle();
1607
1608 #if wxOSX_USE_COCOA
1609 if ( ! window->GetPeer()->IsFlipped() )
1610 {
1611 m_windowTransform = CGAffineTransformMakeTranslation( 0 , m_height );
1612 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1613 }
1614 else
1615 #endif
1616 {
1617 m_windowTransform = CGAffineTransformIdentity;
1618 }
1619 #else
1620 int originX , originY;
1621 originX = originY = 0;
1622 Rect bounds = { 0,0,0,0 };
1623 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
1624 window->MacWindowToRootWindow( &originX , &originY );
1625 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
1626 m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top );
1627 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1628 m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ;
1629 #endif
1630 }
1631
1632 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer)
1633 {
1634 Init();
1635 }
1636
1637 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1638 {
1639 SetNativeContext(NULL);
1640 }
1641
1642
1643 void wxMacCoreGraphicsContext::CheckInvariants() const
1644 {
1645 // check invariants here for debugging ...
1646 }
1647
1648
1649
1650 void wxMacCoreGraphicsContext::StartPage( wxDouble width, wxDouble height )
1651 {
1652 CGRect r;
1653 if ( width != 0 && height != 0)
1654 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) width , (CGFloat) height );
1655 else
1656 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) m_width , (CGFloat) m_height );
1657
1658 CGContextBeginPage(m_cgContext, &r );
1659 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1660 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1661 }
1662
1663 void wxMacCoreGraphicsContext::EndPage()
1664 {
1665 CGContextEndPage(m_cgContext);
1666 }
1667
1668 void wxMacCoreGraphicsContext::Flush()
1669 {
1670 CGContextFlush(m_cgContext);
1671 }
1672
1673 bool wxMacCoreGraphicsContext::EnsureIsValid()
1674 {
1675 CheckInvariants();
1676
1677 if ( !m_cgContext )
1678 {
1679 if (m_invisible)
1680 return false;
1681
1682 #if wxOSX_USE_COCOA
1683 if ( wxOSXLockFocus(m_view) )
1684 {
1685 m_cgContext = wxOSXGetContextFromCurrentContext();
1686 wxASSERT_MSG( m_cgContext != NULL, wxT("Unable to retrieve drawing context from View"));
1687 }
1688 else
1689 {
1690 m_invisible = true;
1691 }
1692 #endif
1693 #if wxOSX_USE_IPHONE
1694 m_cgContext = wxOSXGetContextFromCurrentContext();
1695 if ( m_cgContext == NULL )
1696 {
1697 m_invisible = true;
1698 }
1699 #endif
1700 #if wxOSX_USE_CARBON
1701 OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
1702 if ( status != noErr )
1703 {
1704 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1705 }
1706 #endif
1707 if ( m_cgContext )
1708 {
1709 CGContextSaveGState( m_cgContext );
1710 #if wxOSX_USE_COCOA_OR_CARBON
1711 if ( m_clipRgn.get() )
1712 {
1713 wxCFRef<HIMutableShapeRef> hishape( HIShapeCreateMutableCopy( m_clipRgn ) );
1714 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1715 if ( HIShapeIsEmpty(hishape))
1716 {
1717 CGRect empty = CGRectMake( 0,0,0,0 );
1718 CGContextClipToRect( m_cgContext, empty );
1719 }
1720 else
1721 {
1722 HIShapeReplacePathInCGContext( hishape, m_cgContext );
1723 CGContextClip( m_cgContext );
1724 }
1725 }
1726 #endif
1727 CGContextConcatCTM( m_cgContext, m_windowTransform );
1728 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
1729 m_contextSynthesized = true;
1730 CGContextSaveGState( m_cgContext );
1731
1732 #if 0 // turn on for debugging of clientdc
1733 static float color = 0.5 ;
1734 static int channel = 0 ;
1735 CGRect bounds = CGRectMake(-1000,-1000,2000,2000);
1736 CGContextSetRGBFillColor( m_cgContext, channel == 0 ? color : 0.5 ,
1737 channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 );
1738 CGContextFillRect( m_cgContext, bounds );
1739 color += 0.1 ;
1740 if ( color > 0.9 )
1741 {
1742 color = 0.5 ;
1743 channel++ ;
1744 if ( channel == 3 )
1745 channel = 0 ;
1746 }
1747 #endif
1748 }
1749 }
1750 CheckInvariants();
1751
1752 return m_cgContext != NULL;
1753 }
1754
1755 bool wxMacCoreGraphicsContext::SetAntialiasMode(wxAntialiasMode antialias)
1756 {
1757 if (!EnsureIsValid())
1758 return true;
1759
1760 if (m_antialias == antialias)
1761 return true;
1762
1763 m_antialias = antialias;
1764
1765 bool antialiasMode;
1766 switch (antialias)
1767 {
1768 case wxANTIALIAS_DEFAULT:
1769 antialiasMode = true;
1770 break;
1771 case wxANTIALIAS_NONE:
1772 antialiasMode = false;
1773 break;
1774 default:
1775 return false;
1776 }
1777 CGContextSetShouldAntialias(m_cgContext, antialiasMode);
1778 CheckInvariants();
1779 return true;
1780 }
1781
1782 bool wxMacCoreGraphicsContext::SetInterpolationQuality(wxInterpolationQuality interpolation)
1783 {
1784 if (!EnsureIsValid())
1785 return true;
1786
1787 if (m_interpolation == interpolation)
1788 return true;
1789
1790 m_interpolation = interpolation;
1791 CGInterpolationQuality quality;
1792
1793 switch (interpolation)
1794 {
1795 case wxINTERPOLATION_DEFAULT:
1796 quality = kCGInterpolationDefault;
1797 break;
1798 case wxINTERPOLATION_NONE:
1799 quality = kCGInterpolationNone;
1800 break;
1801 case wxINTERPOLATION_FAST:
1802 quality = kCGInterpolationLow;
1803 break;
1804 case wxINTERPOLATION_GOOD:
1805 #if wxOSX_USE_COCOA_OR_CARBON
1806 quality = UMAGetSystemVersion() < 0x1060 ? kCGInterpolationHigh : (CGInterpolationQuality) 4 /*kCGInterpolationMedium only on 10.6*/;
1807 #else
1808 quality = kCGInterpolationMedium;
1809 #endif
1810 break;
1811 case wxINTERPOLATION_BEST:
1812 quality = kCGInterpolationHigh;
1813 break;
1814 default:
1815 return false;
1816 }
1817 CGContextSetInterpolationQuality(m_cgContext, quality);
1818 CheckInvariants();
1819 return true;
1820 }
1821
1822 bool wxMacCoreGraphicsContext::SetCompositionMode(wxCompositionMode op)
1823 {
1824 if (!EnsureIsValid())
1825 return true;
1826
1827 if ( m_composition == op )
1828 return true;
1829
1830 m_composition = op;
1831
1832 if (m_composition == wxCOMPOSITION_DEST)
1833 return true;
1834
1835 // TODO REMOVE if we don't need it because of bugs in 10.5
1836 #if 0
1837 {
1838 CGCompositeOperation cop = kCGCompositeOperationSourceOver;
1839 CGBlendMode mode = kCGBlendModeNormal;
1840 switch( op )
1841 {
1842 case wxCOMPOSITION_CLEAR:
1843 cop = kCGCompositeOperationClear;
1844 break;
1845 case wxCOMPOSITION_SOURCE:
1846 cop = kCGCompositeOperationCopy;
1847 break;
1848 case wxCOMPOSITION_OVER:
1849 mode = kCGBlendModeNormal;
1850 break;
1851 case wxCOMPOSITION_IN:
1852 cop = kCGCompositeOperationSourceIn;
1853 break;
1854 case wxCOMPOSITION_OUT:
1855 cop = kCGCompositeOperationSourceOut;
1856 break;
1857 case wxCOMPOSITION_ATOP:
1858 cop = kCGCompositeOperationSourceAtop;
1859 break;
1860 case wxCOMPOSITION_DEST_OVER:
1861 cop = kCGCompositeOperationDestinationOver;
1862 break;
1863 case wxCOMPOSITION_DEST_IN:
1864 cop = kCGCompositeOperationDestinationIn;
1865 break;
1866 case wxCOMPOSITION_DEST_OUT:
1867 cop = kCGCompositeOperationDestinationOut;
1868 break;
1869 case wxCOMPOSITION_DEST_ATOP:
1870 cop = kCGCompositeOperationDestinationAtop;
1871 break;
1872 case wxCOMPOSITION_XOR:
1873 cop = kCGCompositeOperationXOR;
1874 break;
1875 case wxCOMPOSITION_ADD:
1876 mode = kCGBlendModePlusLighter ;
1877 break;
1878 default:
1879 return false;
1880 }
1881 if ( cop != kCGCompositeOperationSourceOver )
1882 CGContextSetCompositeOperation(m_cgContext, cop);
1883 else
1884 CGContextSetBlendMode(m_cgContext, mode);
1885 }
1886 #endif
1887 {
1888 CGBlendMode mode = kCGBlendModeNormal;
1889 switch( op )
1890 {
1891 case wxCOMPOSITION_CLEAR:
1892 mode = kCGBlendModeClear;
1893 break;
1894 case wxCOMPOSITION_SOURCE:
1895 mode = kCGBlendModeCopy;
1896 break;
1897 case wxCOMPOSITION_OVER:
1898 mode = kCGBlendModeNormal;
1899 break;
1900 case wxCOMPOSITION_IN:
1901 mode = kCGBlendModeSourceIn;
1902 break;
1903 case wxCOMPOSITION_OUT:
1904 mode = kCGBlendModeSourceOut;
1905 break;
1906 case wxCOMPOSITION_ATOP:
1907 mode = kCGBlendModeSourceAtop;
1908 break;
1909 case wxCOMPOSITION_DEST_OVER:
1910 mode = kCGBlendModeDestinationOver;
1911 break;
1912 case wxCOMPOSITION_DEST_IN:
1913 mode = kCGBlendModeDestinationIn;
1914 break;
1915 case wxCOMPOSITION_DEST_OUT:
1916 mode = kCGBlendModeDestinationOut;
1917 break;
1918 case wxCOMPOSITION_DEST_ATOP:
1919 mode = kCGBlendModeDestinationAtop;
1920 break;
1921 case wxCOMPOSITION_XOR:
1922 mode = kCGBlendModeExclusion; // Not kCGBlendModeXOR!
1923 break;
1924
1925 case wxCOMPOSITION_ADD:
1926 mode = kCGBlendModePlusLighter ;
1927 break;
1928 default:
1929 return false;
1930 }
1931 CGContextSetBlendMode(m_cgContext, mode);
1932 }
1933
1934 CheckInvariants();
1935 return true;
1936 }
1937
1938 void wxMacCoreGraphicsContext::BeginLayer(wxDouble opacity)
1939 {
1940 CheckInvariants();
1941 CGContextSaveGState(m_cgContext);
1942 CGContextSetAlpha(m_cgContext, (CGFloat) opacity);
1943 CGContextBeginTransparencyLayer(m_cgContext, 0);
1944 CheckInvariants();
1945 }
1946
1947 void wxMacCoreGraphicsContext::EndLayer()
1948 {
1949 CheckInvariants();
1950 CGContextEndTransparencyLayer(m_cgContext);
1951 CGContextRestoreGState(m_cgContext);
1952 CheckInvariants();
1953 }
1954
1955 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1956 {
1957 CheckInvariants();
1958 #if wxOSX_USE_COCOA_OR_CARBON
1959 if( m_cgContext )
1960 {
1961 wxCFRef<HIShapeRef> shape = wxCFRefFromGet(region.GetWXHRGN());
1962 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1963 if ( HIShapeIsEmpty(shape))
1964 {
1965 CGRect empty = CGRectMake( 0,0,0,0 );
1966 CGContextClipToRect( m_cgContext, empty );
1967 }
1968 else
1969 {
1970 HIShapeReplacePathInCGContext( shape, m_cgContext );
1971 CGContextClip( m_cgContext );
1972 }
1973 }
1974 else
1975 {
1976 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1977 // to regions we try at least to have correct translations
1978 HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( region.GetWXHRGN() );
1979
1980 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform );
1981 HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y );
1982 m_clipRgn.reset(mutableShape);
1983 }
1984 #else
1985 // allow usage as measuring context
1986 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1987 #endif
1988 CheckInvariants();
1989 }
1990
1991 // clips drawings to the rect
1992 void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1993 {
1994 CheckInvariants();
1995 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
1996 if ( m_cgContext )
1997 {
1998 CGContextClipToRect( m_cgContext, r );
1999 }
2000 else
2001 {
2002 #if wxOSX_USE_COCOA_OR_CARBON
2003 // the clipping itself must be stored as device coordinates, otherwise
2004 // we cannot apply it back correctly
2005 r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform );
2006 r.size= CGSizeApplyAffineTransform(r.size, m_windowTransform);
2007 m_clipRgn.reset(HIShapeCreateWithRect(&r));
2008 #else
2009 // allow usage as measuring context
2010 // wxFAIL_MSG( "Needs a valid context for clipping" );
2011 #endif
2012 }
2013 CheckInvariants();
2014 }
2015
2016 // resets the clipping to original extent
2017 void wxMacCoreGraphicsContext::ResetClip()
2018 {
2019 if ( m_cgContext )
2020 {
2021 // there is no way for clearing the clip, we can only revert to the stored
2022 // state, but then we have to make sure everything else is NOT restored
2023 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2024 CGContextRestoreGState( m_cgContext );
2025 CGContextSaveGState( m_cgContext );
2026 CGAffineTransform transformNew = CGContextGetCTM( m_cgContext );
2027 transformNew = CGAffineTransformInvert( transformNew ) ;
2028 CGContextConcatCTM( m_cgContext, transformNew);
2029 CGContextConcatCTM( m_cgContext, transform);
2030 }
2031 else
2032 {
2033 #if wxOSX_USE_COCOA_OR_CARBON
2034 m_clipRgn.reset();
2035 #else
2036 // allow usage as measuring context
2037 // wxFAIL_MSG( "Needs a valid context for clipping" );
2038 #endif
2039 }
2040 CheckInvariants();
2041 }
2042
2043 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
2044 {
2045 if ( m_pen.IsNull() )
2046 return ;
2047
2048 if (!EnsureIsValid())
2049 return;
2050
2051 if (m_composition == wxCOMPOSITION_DEST)
2052 return;
2053
2054 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2055
2056 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2057 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2058 CGContextStrokePath( m_cgContext );
2059
2060 CheckInvariants();
2061 }
2062
2063 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
2064 {
2065 if (!EnsureIsValid())
2066 return;
2067
2068 if (m_composition == wxCOMPOSITION_DEST)
2069 return;
2070
2071 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2072 {
2073 // when using shading, we cannot draw pen and brush at the same time
2074 // revert to the base implementation of first filling and then stroking
2075 wxGraphicsContext::DrawPath( path, fillStyle );
2076 return;
2077 }
2078
2079 CGPathDrawingMode mode = kCGPathFill ;
2080 if ( m_brush.IsNull() )
2081 {
2082 if ( m_pen.IsNull() )
2083 return;
2084 else
2085 mode = kCGPathStroke;
2086 }
2087 else
2088 {
2089 if ( m_pen.IsNull() )
2090 {
2091 if ( fillStyle == wxODDEVEN_RULE )
2092 mode = kCGPathEOFill;
2093 else
2094 mode = kCGPathFill;
2095 }
2096 else
2097 {
2098 if ( fillStyle == wxODDEVEN_RULE )
2099 mode = kCGPathEOFillStroke;
2100 else
2101 mode = kCGPathFillStroke;
2102 }
2103 }
2104
2105 if ( !m_brush.IsNull() )
2106 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2107 if ( !m_pen.IsNull() )
2108 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2109
2110 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2111
2112 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2113 CGContextDrawPath( m_cgContext , mode );
2114
2115 CheckInvariants();
2116 }
2117
2118 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , wxPolygonFillMode fillStyle )
2119 {
2120 if ( m_brush.IsNull() )
2121 return;
2122
2123 if (!EnsureIsValid())
2124 return;
2125
2126 if (m_composition == wxCOMPOSITION_DEST)
2127 return;
2128
2129 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2130 {
2131 CGContextSaveGState( m_cgContext );
2132 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2133 CGContextClip( m_cgContext );
2134 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2135 CGContextRestoreGState( m_cgContext);
2136 }
2137 else
2138 {
2139 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2140 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2141 if ( fillStyle == wxODDEVEN_RULE )
2142 CGContextEOFillPath( m_cgContext );
2143 else
2144 CGContextFillPath( m_cgContext );
2145 }
2146
2147 CheckInvariants();
2148 }
2149
2150 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
2151 {
2152 // we allow either setting or clearing but not replacing
2153 wxASSERT( m_cgContext == NULL || cg == NULL );
2154
2155 if ( m_cgContext )
2156 {
2157 CheckInvariants();
2158 CGContextRestoreGState( m_cgContext );
2159 CGContextRestoreGState( m_cgContext );
2160 if ( m_contextSynthesized )
2161 {
2162 #if wxOSX_USE_CARBON
2163 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
2164 #endif
2165 #if wxOSX_USE_COCOA
2166 wxOSXUnlockFocus(m_view);
2167 #endif
2168 }
2169 else
2170 CGContextRelease(m_cgContext);
2171 }
2172
2173 m_cgContext = cg;
2174
2175 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
2176 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
2177 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
2178 // for this one operation.
2179
2180 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
2181 // can be removed.
2182 if (m_cgContext)
2183 {
2184 CGContextRetain(m_cgContext);
2185 CGContextSaveGState( m_cgContext );
2186 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
2187 CGContextSaveGState( m_cgContext );
2188 m_contextSynthesized = false;
2189 }
2190 }
2191
2192 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
2193 {
2194 if ( m_cgContext )
2195 CGContextTranslateCTM( m_cgContext, (CGFloat) dx, (CGFloat) dy );
2196 else
2197 m_windowTransform = CGAffineTransformTranslate(m_windowTransform, (CGFloat) dx, (CGFloat) dy);
2198 }
2199
2200 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
2201 {
2202 if ( m_cgContext )
2203 CGContextScaleCTM( m_cgContext , (CGFloat) xScale , (CGFloat) yScale );
2204 else
2205 m_windowTransform = CGAffineTransformScale(m_windowTransform, (CGFloat) xScale, (CGFloat) yScale);
2206 }
2207
2208 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
2209 {
2210 if ( m_cgContext )
2211 CGContextRotateCTM( m_cgContext , (CGFloat) angle );
2212 else
2213 m_windowTransform = CGAffineTransformRotate(m_windowTransform, (CGFloat) angle);
2214 }
2215
2216 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2217 {
2218 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
2219 DrawBitmap(bitmap, x, y, w, h);
2220 }
2221
2222 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2223 {
2224 if (!EnsureIsValid())
2225 return;
2226
2227 if (m_composition == wxCOMPOSITION_DEST)
2228 return;
2229
2230 #ifdef __WXMAC__
2231 wxMacCoreGraphicsBitmapData* refdata = static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2232 CGImageRef image = refdata->GetBitmap();
2233 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2234 if ( refdata->IsMonochrome() == 1 )
2235 {
2236 // is a mask, the '1' in the mask tell where to draw the current brush
2237 if ( !m_brush.IsNull() )
2238 {
2239 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2240 {
2241 // TODO clip to mask
2242 /*
2243 CGContextSaveGState( m_cgContext );
2244 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
2245 CGContextClip( m_cgContext );
2246 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
2247 CGContextRestoreGState( m_cgContext);
2248 */
2249 }
2250 else
2251 {
2252 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2253 wxMacDrawCGImage( m_cgContext , &r , image );
2254 }
2255 }
2256 }
2257 else
2258 {
2259 wxMacDrawCGImage( m_cgContext , &r , image );
2260 }
2261 #endif
2262
2263 CheckInvariants();
2264 }
2265
2266 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2267 {
2268 if (!EnsureIsValid())
2269 return;
2270
2271 if (m_composition == wxCOMPOSITION_DEST)
2272 return;
2273
2274 CGContextSaveGState( m_cgContext );
2275 CGContextTranslateCTM( m_cgContext,(CGFloat) x ,(CGFloat) (y + h) );
2276 CGContextScaleCTM( m_cgContext, 1, -1 );
2277 #if wxOSX_USE_COCOA_OR_CARBON
2278 CGRect r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) w , (CGFloat) h );
2279 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
2280 NULL , kPlotIconRefNormalFlags , icon.GetHICON() );
2281 #endif
2282 CGContextRestoreGState( m_cgContext );
2283
2284 CheckInvariants();
2285 }
2286
2287 void wxMacCoreGraphicsContext::PushState()
2288 {
2289 if (!EnsureIsValid())
2290 return;
2291
2292 CGContextSaveGState( m_cgContext );
2293 }
2294
2295 void wxMacCoreGraphicsContext::PopState()
2296 {
2297 if (!EnsureIsValid())
2298 return;
2299
2300 CGContextRestoreGState( m_cgContext );
2301 }
2302
2303 void wxMacCoreGraphicsContext::DoDrawText( const wxString &str, wxDouble x, wxDouble y )
2304 {
2305 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2306
2307 if (!EnsureIsValid())
2308 return;
2309
2310 if (m_composition == wxCOMPOSITION_DEST)
2311 return;
2312
2313 #if wxOSX_USE_CORE_TEXT
2314 {
2315 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2316 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2317 CTFontRef font = fref->OSXGetCTFont();
2318 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2319 #if 0
2320 // right now there's no way to get continuous underlines, only words, so we emulate it
2321 CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ;
2322 wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) );
2323 CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName };
2324 CFTypeRef values[] = { font, col, underlined };
2325 #else
2326 CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName };
2327 CFTypeRef values[] = { font, col };
2328 #endif
2329 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2330 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2331 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2332 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2333
2334 y += CTFontGetAscent(font);
2335
2336 CGContextSaveGState(m_cgContext);
2337 CGAffineTransform textMatrix = CGContextGetTextMatrix(m_cgContext);
2338
2339 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2340 CGContextScaleCTM(m_cgContext, 1, -1);
2341 CGContextSetTextMatrix(m_cgContext, CGAffineTransformIdentity);
2342
2343 CTLineDraw( line, m_cgContext );
2344
2345 if ( fref->GetUnderlined() ) {
2346 //AKT: draw horizontal line 1 pixel thick and with 1 pixel gap under baseline
2347 CGFloat width = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
2348
2349 CGPoint points[] = { {0.0, -2.0}, {width, -2.0} };
2350
2351 CGContextSetStrokeColorWithColor(m_cgContext, col);
2352 CGContextSetShouldAntialias(m_cgContext, false);
2353 CGContextSetLineWidth(m_cgContext, 1.0);
2354 CGContextStrokeLineSegments(m_cgContext, points, 2);
2355 }
2356
2357 CGContextRestoreGState(m_cgContext);
2358 CGContextSetTextMatrix(m_cgContext, textMatrix);
2359 CGColorRelease( col );
2360 CheckInvariants();
2361 return;
2362 }
2363 #endif
2364 #if wxOSX_USE_ATSU_TEXT
2365 {
2366 DrawText(str, x, y, 0.0);
2367 return;
2368 }
2369 #endif
2370 #if wxOSX_USE_IPHONE
2371 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2372
2373 CGContextSaveGState(m_cgContext);
2374
2375 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
2376 CGContextSetTextDrawingMode (m_cgContext, kCGTextFill);
2377 CGContextSetFillColorWithColor( m_cgContext, col );
2378
2379 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2380 DrawTextInContext( m_cgContext, CGPointMake( x, y ), fref->GetUIFont() , text.AsNSString() );
2381
2382 CGContextRestoreGState(m_cgContext);
2383 CFRelease( col );
2384 #endif
2385
2386 CheckInvariants();
2387 }
2388
2389 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString &str,
2390 wxDouble x, wxDouble y,
2391 wxDouble angle)
2392 {
2393 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2394
2395 if (!EnsureIsValid())
2396 return;
2397
2398 if (m_composition == wxCOMPOSITION_DEST)
2399 return;
2400
2401 #if wxOSX_USE_CORE_TEXT
2402 {
2403 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2404 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
2405 return;
2406 }
2407 #endif
2408 #if wxOSX_USE_ATSU_TEXT
2409 {
2410 OSStatus status = noErr;
2411 ATSUTextLayout atsuLayout;
2412 wxMacUniCharBuffer unibuf( str );
2413 UniCharCount chars = unibuf.GetChars();
2414
2415 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2416 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2417 &chars , &style , &atsuLayout );
2418
2419 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
2420
2421 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2422 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2423
2424 int iAngle = int( angle * RAD2DEG );
2425 if ( abs(iAngle) > 0 )
2426 {
2427 Fixed atsuAngle = IntToFixed( iAngle );
2428 ATSUAttributeTag atsuTags[] =
2429 {
2430 kATSULineRotationTag ,
2431 };
2432 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
2433 {
2434 sizeof( Fixed ) ,
2435 };
2436 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
2437 {
2438 &atsuAngle ,
2439 };
2440 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
2441 atsuTags, atsuSizes, atsuValues );
2442 }
2443
2444 {
2445 ATSUAttributeTag atsuTags[] =
2446 {
2447 kATSUCGContextTag ,
2448 };
2449 ByteCount atsuSizes[WXSIZEOF(atsuTags)] =
2450 {
2451 sizeof( CGContextRef ) ,
2452 };
2453 ATSUAttributeValuePtr atsuValues[WXSIZEOF(atsuTags)] =
2454 {
2455 &m_cgContext ,
2456 };
2457 status = ::ATSUSetLayoutControls(atsuLayout , WXSIZEOF(atsuTags),
2458 atsuTags, atsuSizes, atsuValues );
2459 }
2460
2461 ATSUTextMeasurement textBefore, textAfter;
2462 ATSUTextMeasurement ascent, descent;
2463
2464 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2465 &textBefore , &textAfter, &ascent , &descent );
2466
2467 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2468
2469 Rect rect;
2470 x += (int)(sin(angle) * FixedToFloat(ascent));
2471 y += (int)(cos(angle) * FixedToFloat(ascent));
2472
2473 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2474 IntToFixed(x) , IntToFixed(y) , &rect );
2475 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2476
2477 CGContextSaveGState(m_cgContext);
2478 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2479 CGContextScaleCTM(m_cgContext, 1, -1);
2480 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2481 IntToFixed(0) , IntToFixed(0) );
2482
2483 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
2484
2485 CGContextRestoreGState(m_cgContext);
2486
2487 ::ATSUDisposeTextLayout(atsuLayout);
2488 CheckInvariants();
2489
2490 return;
2491 }
2492 #endif
2493 #if wxOSX_USE_IPHONE
2494 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2495 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
2496 #endif
2497
2498 CheckInvariants();
2499 }
2500
2501 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
2502 wxDouble *descent, wxDouble *externalLeading ) const
2503 {
2504 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2505
2506 if ( width )
2507 *width = 0;
2508 if ( height )
2509 *height = 0;
2510 if ( descent )
2511 *descent = 0;
2512 if ( externalLeading )
2513 *externalLeading = 0;
2514
2515 if (str.empty())
2516 return;
2517
2518 #if wxOSX_USE_CORE_TEXT
2519 {
2520 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2521 CTFontRef font = fref->OSXGetCTFont();
2522
2523 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2524 CFStringRef keys[] = { kCTFontAttributeName };
2525 CFTypeRef values[] = { font };
2526 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2527 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2528 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2529 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2530
2531 CGFloat a, d, l, w;
2532 w = CTLineGetTypographicBounds(line, &a, &d, &l);
2533
2534 if ( height )
2535 *height = a+d+l;
2536 if ( descent )
2537 *descent = d;
2538 if ( externalLeading )
2539 *externalLeading = l;
2540 if ( width )
2541 *width = w;
2542 return;
2543 }
2544 #endif
2545 #if wxOSX_USE_ATSU_TEXT
2546 {
2547 OSStatus status = noErr;
2548
2549 ATSUTextLayout atsuLayout;
2550 wxMacUniCharBuffer unibuf( str );
2551 UniCharCount chars = unibuf.GetChars();
2552
2553 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2554 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2555 &chars , &style , &atsuLayout );
2556
2557 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2558
2559 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2560 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2561
2562 ATSUTextMeasurement textBefore, textAfter;
2563 ATSUTextMeasurement textAscent, textDescent;
2564
2565 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2566 &textBefore , &textAfter, &textAscent , &textDescent );
2567
2568 if ( height )
2569 *height = FixedToFloat(textAscent + textDescent);
2570 if ( descent )
2571 *descent = FixedToFloat(textDescent);
2572 if ( externalLeading )
2573 *externalLeading = 0;
2574 if ( width )
2575 *width = FixedToFloat(textAfter - textBefore);
2576
2577 ::ATSUDisposeTextLayout(atsuLayout);
2578
2579 return;
2580 }
2581 #endif
2582 #if wxOSX_USE_IPHONE
2583 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2584
2585 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2586 CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() );
2587
2588 if ( height )
2589 *height = sz.height;
2590 /*
2591 if ( descent )
2592 *descent = FixedToFloat(textDescent);
2593 if ( externalLeading )
2594 *externalLeading = 0;
2595 */
2596 if ( width )
2597 *width = sz.width;
2598 #endif
2599
2600 CheckInvariants();
2601 }
2602
2603 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
2604 {
2605 widths.Empty();
2606 widths.Add(0, text.length());
2607
2608 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2609
2610 if (text.empty())
2611 return;
2612
2613 #if wxOSX_USE_CORE_TEXT
2614 {
2615 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2616 CTFontRef font = fref->OSXGetCTFont();
2617
2618 wxCFStringRef t(text, wxLocale::GetSystemEncoding() );
2619 CFStringRef keys[] = { kCTFontAttributeName };
2620 CFTypeRef values[] = { font };
2621 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2622 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2623 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) );
2624 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2625
2626 int chars = text.length();
2627 for ( int pos = 0; pos < (int)chars; pos ++ )
2628 {
2629 widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL );
2630 }
2631
2632 return;
2633 }
2634 #endif
2635 #if wxOSX_USE_ATSU_TEXT
2636 {
2637 OSStatus status = noErr;
2638 ATSUTextLayout atsuLayout;
2639 wxMacUniCharBuffer unibuf( text );
2640 UniCharCount chars = unibuf.GetChars();
2641
2642 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2643 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2644 &chars , &style , &atsuLayout );
2645
2646 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2647
2648 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2649 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2650
2651 // new implementation from JS, keep old one just in case
2652 #if 0
2653 for ( int pos = 0; pos < (int)chars; pos ++ )
2654 {
2655 unsigned long actualNumberOfBounds = 0;
2656 ATSTrapezoid glyphBounds;
2657
2658 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2659 OSStatus result;
2660 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2661 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2662 if (result != noErr || actualNumberOfBounds != 1 )
2663 return;
2664
2665 widths[pos] = FixedToFloat( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
2666 //unsigned char uch = s[i];
2667 }
2668 #else
2669 ATSLayoutRecord *layoutRecords = NULL;
2670 ItemCount glyphCount = 0;
2671
2672 // Get the glyph extents
2673 OSStatus err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout,
2674 0,
2675 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2676 (void **)
2677 &layoutRecords,
2678 &glyphCount);
2679 wxASSERT(glyphCount == (text.length()+1));
2680
2681 if ( err == noErr && glyphCount == (text.length()+1))
2682 {
2683 for ( int pos = 1; pos < (int)glyphCount ; pos ++ )
2684 {
2685 widths[pos-1] = FixedToFloat( layoutRecords[pos].realPos );
2686 }
2687 }
2688
2689 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL,
2690 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2691 (void **) &layoutRecords);
2692 #endif
2693 ::ATSUDisposeTextLayout(atsuLayout);
2694 }
2695 #endif
2696 #if wxOSX_USE_IPHONE
2697 // TODO core graphics text implementation here
2698 #endif
2699
2700 CheckInvariants();
2701 }
2702
2703 void * wxMacCoreGraphicsContext::GetNativeContext()
2704 {
2705 return m_cgContext;
2706 }
2707
2708
2709 void wxMacCoreGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2710 {
2711 if (!EnsureIsValid())
2712 return;
2713
2714 if (m_composition == wxCOMPOSITION_DEST)
2715 return;
2716
2717 // when using shading, we have to go back to drawing paths
2718 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
2719 {
2720 wxGraphicsContext::DrawRectangle( x,y,w,h );
2721 return;
2722 }
2723
2724 CGRect rect = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
2725 if ( !m_brush.IsNull() )
2726 {
2727 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
2728 CGContextFillRect(m_cgContext, rect);
2729 }
2730
2731 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
2732 if ( !m_pen.IsNull() )
2733 {
2734 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
2735 CGContextStrokeRect(m_cgContext, rect);
2736 }
2737 }
2738
2739 // concatenates this transform with the current transform of this context
2740 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
2741 {
2742 if ( m_cgContext )
2743 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2744 else
2745 m_windowTransform = CGAffineTransformConcat(*(CGAffineTransform*) matrix.GetNativeMatrix(), m_windowTransform);
2746 }
2747
2748 // sets the transform of this context
2749 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
2750 {
2751 CheckInvariants();
2752 if ( m_cgContext )
2753 {
2754 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2755 transform = CGAffineTransformInvert( transform ) ;
2756 CGContextConcatCTM( m_cgContext, transform);
2757 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2758 }
2759 else
2760 {
2761 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
2762 }
2763 CheckInvariants();
2764 }
2765
2766 // gets the matrix of this context
2767 wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
2768 {
2769 wxGraphicsMatrix m = CreateMatrix();
2770 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
2771 CGContextGetCTM( m_cgContext ));
2772 return m;
2773 }
2774
2775
2776 #if wxUSE_IMAGE
2777
2778 // ----------------------------------------------------------------------------
2779 // wxMacCoreGraphicsImageContext
2780 // ----------------------------------------------------------------------------
2781
2782 // This is a GC that can be used to draw on wxImage. In this implementation we
2783 // simply draw on a wxBitmap using wxMemoryDC and then convert it to wxImage in
2784 // the end so it's not especially interesting and exists mainly for
2785 // compatibility with the other platforms.
2786 class wxMacCoreGraphicsImageContext : public wxMacCoreGraphicsContext
2787 {
2788 public:
2789 wxMacCoreGraphicsImageContext(wxGraphicsRenderer* renderer,
2790 wxImage& image) :
2791 wxMacCoreGraphicsContext(renderer),
2792 m_image(image),
2793 m_bitmap(image),
2794 m_memDC(m_bitmap)
2795 {
2796 SetNativeContext
2797 (
2798 (CGContextRef)(m_memDC.GetGraphicsContext()->GetNativeContext())
2799 );
2800 m_width = image.GetWidth();
2801 m_height = image.GetHeight();
2802 }
2803
2804 virtual ~wxMacCoreGraphicsImageContext()
2805 {
2806 m_memDC.SelectObject(wxNullBitmap);
2807 m_image = m_bitmap.ConvertToImage();
2808 }
2809
2810 private:
2811 wxImage& m_image;
2812 wxBitmap m_bitmap;
2813 wxMemoryDC m_memDC;
2814 };
2815
2816 #endif // wxUSE_IMAGE
2817
2818 //
2819 // Renderer
2820 //
2821
2822 //-----------------------------------------------------------------------------
2823 // wxMacCoreGraphicsRenderer declaration
2824 //-----------------------------------------------------------------------------
2825
2826 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
2827 {
2828 public :
2829 wxMacCoreGraphicsRenderer() {}
2830
2831 virtual ~wxMacCoreGraphicsRenderer() {}
2832
2833 // Context
2834
2835 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2836 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
2837 #if wxUSE_PRINTING_ARCHITECTURE
2838 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
2839 #endif
2840
2841 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2842
2843 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2844
2845 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2846
2847 #if wxUSE_IMAGE
2848 virtual wxGraphicsContext * CreateContextFromImage(wxImage& image);
2849 #endif // wxUSE_IMAGE
2850
2851 virtual wxGraphicsContext * CreateMeasuringContext();
2852
2853 // Path
2854
2855 virtual wxGraphicsPath CreatePath();
2856
2857 // Matrix
2858
2859 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2860 wxDouble tx=0.0, wxDouble ty=0.0);
2861
2862
2863 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2864
2865 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2866
2867 virtual wxGraphicsBrush
2868 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2869 wxDouble x2, wxDouble y2,
2870 const wxGraphicsGradientStops& stops);
2871
2872 virtual wxGraphicsBrush
2873 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2874 wxDouble xc, wxDouble yc,
2875 wxDouble radius,
2876 const wxGraphicsGradientStops& stops);
2877
2878 // sets the font
2879 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2880 virtual wxGraphicsFont CreateFont(double sizeInPixels,
2881 const wxString& facename,
2882 int flags = wxFONTFLAG_DEFAULT,
2883 const wxColour& col = *wxBLACK);
2884
2885 // create a native bitmap representation
2886 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ;
2887
2888 #if wxUSE_IMAGE
2889 virtual wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image);
2890 virtual wxImage CreateImageFromBitmap(const wxGraphicsBitmap& bmp);
2891 #endif // wxUSE_IMAGE
2892
2893 // create a graphics bitmap from a native bitmap
2894 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
2895
2896 // create a native bitmap representation
2897 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
2898 private :
2899 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
2900 } ;
2901
2902 //-----------------------------------------------------------------------------
2903 // wxMacCoreGraphicsRenderer implementation
2904 //-----------------------------------------------------------------------------
2905
2906 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
2907
2908 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
2909
2910 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2911 {
2912 return &gs_MacCoreGraphicsRenderer;
2913 }
2914
2915 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc )
2916 {
2917 const wxDCImpl* impl = dc.GetImpl();
2918 wxWindowDCImpl *win_impl = wxDynamicCast( impl, wxWindowDCImpl );
2919 if (win_impl)
2920 {
2921 int w, h;
2922 win_impl->GetSize( &w, &h );
2923 CGContextRef cgctx = 0;
2924
2925 wxASSERT_MSG(win_impl->GetWindow(), "Invalid wxWindow in wxMacCoreGraphicsRenderer::CreateContext");
2926 if (win_impl->GetWindow())
2927 cgctx = (CGContextRef)(win_impl->GetWindow()->MacGetCGContextRef());
2928
2929 // having a cgctx being NULL is fine (will be created on demand)
2930 // this is the case for all wxWindowDCs except wxPaintDC
2931 wxMacCoreGraphicsContext *context =
2932 new wxMacCoreGraphicsContext( this, cgctx, (wxDouble) w, (wxDouble) h );
2933 context->EnableOffset(true);
2934 return context;
2935 }
2936 return NULL;
2937 }
2938
2939 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC& dc )
2940 {
2941 #ifdef __WXMAC__
2942 const wxDCImpl* impl = dc.GetImpl();
2943 wxMemoryDCImpl *mem_impl = wxDynamicCast( impl, wxMemoryDCImpl );
2944 if (mem_impl)
2945 {
2946 int w, h;
2947 mem_impl->GetSize( &w, &h );
2948 wxMacCoreGraphicsContext* context = new wxMacCoreGraphicsContext( this,
2949 (CGContextRef)(mem_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2950 context->EnableOffset(true);
2951 return context;
2952 }
2953 #endif
2954 return NULL;
2955 }
2956
2957 #if wxUSE_PRINTING_ARCHITECTURE
2958 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC& dc )
2959 {
2960 #ifdef __WXMAC__
2961 const wxDCImpl* impl = dc.GetImpl();
2962 wxPrinterDCImpl *print_impl = wxDynamicCast( impl, wxPrinterDCImpl );
2963 if (print_impl)
2964 {
2965 int w, h;
2966 print_impl->GetSize( &w, &h );
2967 return new wxMacCoreGraphicsContext( this,
2968 (CGContextRef)(print_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2969 }
2970 #endif
2971 return NULL;
2972 }
2973 #endif
2974
2975 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
2976 {
2977 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
2978 }
2979
2980 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
2981 {
2982 #if wxOSX_USE_CARBON
2983 wxMacCoreGraphicsContext* context = new wxMacCoreGraphicsContext(this,(WindowRef)window);
2984 context->EnableOffset(true);
2985 return context;
2986 #else
2987 wxUnusedVar(window);
2988 return NULL;
2989 #endif
2990 }
2991
2992 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
2993 {
2994 return new wxMacCoreGraphicsContext(this, window );
2995 }
2996
2997 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2998 {
2999 return new wxMacCoreGraphicsContext(this);
3000 }
3001
3002 #if wxUSE_IMAGE
3003
3004 wxGraphicsContext*
3005 wxMacCoreGraphicsRenderer::CreateContextFromImage(wxImage& image)
3006 {
3007 return new wxMacCoreGraphicsImageContext(this, image);
3008 }
3009
3010 #endif // wxUSE_IMAGE
3011
3012 // Path
3013
3014 wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
3015 {
3016 wxGraphicsPath m;
3017 m.SetRefData( new wxMacCoreGraphicsPathData(this));
3018 return m;
3019 }
3020
3021
3022 // Matrix
3023
3024 wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
3025 wxDouble tx, wxDouble ty)
3026 {
3027 wxGraphicsMatrix m;
3028 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
3029 data->Set( a,b,c,d,tx,ty ) ;
3030 m.SetRefData(data);
3031 return m;
3032 }
3033
3034 wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
3035 {
3036 if ( !pen.IsOk() || pen.GetStyle() == wxTRANSPARENT )
3037 return wxNullGraphicsPen;
3038 else
3039 {
3040 wxGraphicsPen p;
3041 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
3042 return p;
3043 }
3044 }
3045
3046 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
3047 {
3048 if ( !brush.IsOk() || brush.GetStyle() == wxTRANSPARENT )
3049 return wxNullGraphicsBrush;
3050 else
3051 {
3052 wxGraphicsBrush p;
3053 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
3054 return p;
3055 }
3056 }
3057
3058 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap& bmp )
3059 {
3060 if ( bmp.IsOk() )
3061 {
3062 wxGraphicsBitmap p;
3063 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp.CreateCGImage(), bmp.GetDepth() == 1 ) );
3064 return p;
3065 }
3066 else
3067 return wxNullGraphicsBitmap;
3068 }
3069
3070 #if wxUSE_IMAGE
3071
3072 wxGraphicsBitmap
3073 wxMacCoreGraphicsRenderer::CreateBitmapFromImage(const wxImage& image)
3074 {
3075 // We don't have any direct way to convert wxImage to CGImage so pass by
3076 // wxBitmap. This makes this function pretty useless in this implementation
3077 // but it allows to have the same API as with Cairo backend where we can
3078 // convert wxImage to a Cairo surface directly, bypassing wxBitmap.
3079 return CreateBitmap(wxBitmap(image));
3080 }
3081
3082 wxImage wxMacCoreGraphicsRenderer::CreateImageFromBitmap(const wxGraphicsBitmap& bmp)
3083 {
3084 wxMacCoreGraphicsBitmapData* const
3085 data = static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
3086
3087 return data ? data->ConvertToImage() : wxNullImage;
3088 }
3089
3090 #endif // wxUSE_IMAGE
3091
3092 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
3093 {
3094 if ( bitmap != NULL )
3095 {
3096 wxGraphicsBitmap p;
3097 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , (CGImageRef) bitmap, false ));
3098 return p;
3099 }
3100 else
3101 return wxNullGraphicsBitmap;
3102 }
3103
3104 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
3105 {
3106 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
3107 CGImageRef img = refdata->GetBitmap();
3108 if ( img )
3109 {
3110 wxGraphicsBitmap p;
3111 CGImageRef subimg = CGImageCreateWithImageInRect(img,CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ));
3112 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg, refdata->IsMonochrome() ) );
3113 return p;
3114 }
3115 else
3116 return wxNullGraphicsBitmap;
3117 }
3118
3119 wxGraphicsBrush
3120 wxMacCoreGraphicsRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
3121 wxDouble x2, wxDouble y2,
3122 const wxGraphicsGradientStops& stops)
3123 {
3124 wxGraphicsBrush p;
3125 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
3126 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
3127 p.SetRefData(d);
3128 return p;
3129 }
3130
3131 wxGraphicsBrush
3132 wxMacCoreGraphicsRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
3133 wxDouble xc, wxDouble yc,
3134 wxDouble radius,
3135 const wxGraphicsGradientStops& stops)
3136 {
3137 wxGraphicsBrush p;
3138 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
3139 d->CreateRadialGradientBrush(xo, yo, xc, yc, radius, stops);
3140 p.SetRefData(d);
3141 return p;
3142 }
3143
3144 wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
3145 {
3146 if ( font.IsOk() )
3147 {
3148 wxGraphicsFont p;
3149 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
3150 return p;
3151 }
3152 else
3153 return wxNullGraphicsFont;
3154 }
3155
3156 wxGraphicsFont
3157 wxMacCoreGraphicsRenderer::CreateFont(double sizeInPixels,
3158 const wxString& facename,
3159 int flags,
3160 const wxColour& col)
3161 {
3162 // This implementation is not ideal as we don't support fractional font
3163 // sizes right now, but it's the simplest one.
3164 //
3165 // Notice that under Mac we always use 72 DPI so the font size in pixels is
3166 // the same as the font size in points and we can pass it directly to wxFont
3167 // ctor.
3168 wxFont font(wxRound(sizeInPixels),
3169 wxFONTFAMILY_DEFAULT,
3170 flags & wxFONTFLAG_ITALIC ? wxFONTSTYLE_ITALIC
3171 : wxFONTSTYLE_NORMAL,
3172 flags & wxFONTFLAG_BOLD ? wxFONTWEIGHT_BOLD
3173 : wxFONTWEIGHT_NORMAL,
3174 (flags & wxFONTFLAG_UNDERLINED) != 0,
3175 facename);
3176
3177 wxGraphicsFont f;
3178 f.SetRefData(new wxMacCoreGraphicsFontData(this, font, col));
3179 return f;
3180 }
3181
3182 //
3183 // CoreGraphics Helper Methods
3184 //
3185
3186 // Data Providers and Consumers
3187
3188 size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
3189 {
3190 CFMutableDataRef data = (CFMutableDataRef) info;
3191 if ( data )
3192 {
3193 CFDataAppendBytes( data, (const UInt8*) bytes, count );
3194 }
3195 return count;
3196 }
3197
3198 void wxMacReleaseCFDataProviderCallback(void *info,
3199 const void *WXUNUSED(data),
3200 size_t WXUNUSED(count))
3201 {
3202 if ( info )
3203 CFRelease( (CFDataRef) info );
3204 }
3205
3206 void wxMacReleaseCFDataConsumerCallback( void *info )
3207 {
3208 if ( info )
3209 CFRelease( (CFDataRef) info );
3210 }
3211
3212 CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data )
3213 {
3214 if ( data == NULL )
3215 return NULL;
3216
3217 return CGDataProviderCreateWithCFData( data );
3218 }
3219
3220 CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data )
3221 {
3222 if ( data == NULL )
3223 return NULL;
3224
3225 return CGDataConsumerCreateWithCFData( data );
3226 }
3227
3228 void
3229 wxMacReleaseMemoryBufferProviderCallback(void *info,
3230 const void * WXUNUSED_UNLESS_DEBUG(data),
3231 size_t WXUNUSED(size))
3232 {
3233 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
3234
3235 wxASSERT( data == membuf->GetData() ) ;
3236
3237 delete membuf ;
3238 }
3239
3240 CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf )
3241 {
3242 wxMemoryBuffer* b = new wxMemoryBuffer( buf );
3243 if ( b->GetDataLen() == 0 )
3244 return NULL;
3245
3246 return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() ,
3247 wxMacReleaseMemoryBufferProviderCallback );
3248 }