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