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