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