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