]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/graphics.cpp
make GetPaperRect() and GetResolution() const functions
[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 GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
1368 wxDouble *descent, wxDouble *externalLeading ) const;
1369
1370 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
1371
1372 //
1373 // image support
1374 //
1375
1376 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1377
1378 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1379
1380 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1381
1382 void SetNativeContext( CGContextRef cg );
1383
1384 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsContext)
1385
1386 private:
1387 void EnsureIsValid();
1388
1389 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
1390 virtual void DoDrawRotatedText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
1391
1392 CGContextRef m_cgContext;
1393 #if wxOSX_USE_CARBON
1394 WindowRef m_windowRef;
1395 #endif
1396 bool m_releaseContext;
1397 CGAffineTransform m_windowTransform;
1398 wxDouble m_width;
1399 wxDouble m_height;
1400
1401 #if wxOSX_USE_CARBON
1402 wxCFRef<HIShapeRef> m_clipRgn;
1403 #endif
1404 };
1405
1406 //-----------------------------------------------------------------------------
1407 // device context implementation
1408 //
1409 // more and more of the dc functionality should be implemented by calling
1410 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
1411 // also coordinate conversions should be moved to native matrix ops
1412 //-----------------------------------------------------------------------------
1413
1414 // we always stock two context states, one at entry, to be able to preserve the
1415 // state we were called with, the other one after changing to HI Graphics orientation
1416 // (this one is used for getting back clippings etc)
1417
1418 //-----------------------------------------------------------------------------
1419 // wxMacCoreGraphicsContext implementation
1420 //-----------------------------------------------------------------------------
1421
1422 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext)
1423
1424 class wxQuartzOffsetHelper
1425 {
1426 public :
1427 wxQuartzOffsetHelper( CGContextRef cg , bool offset )
1428 {
1429 m_cg = cg;
1430 m_offset = offset;
1431 if ( m_offset )
1432 CGContextTranslateCTM( m_cg, (CGFloat) 0.5, (CGFloat) 0.5 );
1433 }
1434 ~wxQuartzOffsetHelper( )
1435 {
1436 if ( m_offset )
1437 CGContextTranslateCTM( m_cg, (CGFloat) -0.5, (CGFloat) -0.5 );
1438 }
1439 public :
1440 CGContextRef m_cg;
1441 bool m_offset;
1442 } ;
1443
1444 void wxMacCoreGraphicsContext::Init()
1445 {
1446 m_cgContext = NULL;
1447 m_releaseContext = false;
1448 #if wxOSX_USE_CARBON
1449 m_windowRef = NULL;
1450 #endif
1451 m_width = 0;
1452 m_height = 0;
1453 }
1454
1455 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, CGContextRef cgcontext, wxDouble width, wxDouble height ) : wxGraphicsContext(renderer)
1456 {
1457 Init();
1458 SetNativeContext(cgcontext);
1459 m_width = width;
1460 m_height = height;
1461 }
1462
1463 #if wxOSX_USE_CARBON
1464 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, WindowRef window ): wxGraphicsContext(renderer)
1465 {
1466 Init();
1467 m_windowRef = window;
1468 }
1469 #endif
1470
1471 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxGraphicsRenderer* renderer, wxWindow* window ): wxGraphicsContext(renderer)
1472 {
1473 Init();
1474
1475 int originX , originY;
1476 originX = originY = 0;
1477
1478 Rect bounds = { 0,0,0,0 };
1479 #if defined(__WXCOCOA__) || !wxOSX_USE_CARBON
1480 #else
1481 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
1482 window->MacWindowToRootWindow( &originX , &originY );
1483 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
1484 #endif
1485 m_windowTransform = CGAffineTransformMakeTranslation( 0 , bounds.bottom - bounds.top );
1486 m_windowTransform = CGAffineTransformScale( m_windowTransform , 1 , -1 );
1487 m_windowTransform = CGAffineTransformTranslate( m_windowTransform, originX, originY ) ;
1488 }
1489
1490 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsContext(renderer)
1491 {
1492 Init();
1493 }
1494
1495 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() : wxGraphicsContext(NULL)
1496 {
1497 Init();
1498 wxLogDebug(wxT("Illegal Constructor called"));
1499 }
1500
1501 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
1502 {
1503 SetNativeContext(NULL);
1504 }
1505
1506 void wxMacCoreGraphicsContext::GetSize( wxDouble* width, wxDouble* height)
1507 {
1508 *width = m_width;
1509 *height = m_height;
1510 }
1511
1512
1513 void wxMacCoreGraphicsContext::StartPage( wxDouble width, wxDouble height )
1514 {
1515 CGRect r;
1516 if ( width != 0 && height != 0)
1517 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) width , (CGFloat) height );
1518 else
1519 r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) m_width , (CGFloat) m_height );
1520
1521 CGContextBeginPage(m_cgContext, &r );
1522 // CGContextTranslateCTM( m_cgContext , 0 , height == 0 ? m_height : height );
1523 // CGContextScaleCTM( m_cgContext , 1 , -1 );
1524 }
1525
1526 void wxMacCoreGraphicsContext::EndPage()
1527 {
1528 CGContextEndPage(m_cgContext);
1529 }
1530
1531 void wxMacCoreGraphicsContext::Flush()
1532 {
1533 CGContextFlush(m_cgContext);
1534 }
1535
1536 void wxMacCoreGraphicsContext::EnsureIsValid()
1537 {
1538 if ( !m_cgContext )
1539 {
1540 #if defined(__WXCOCOA__) || ! wxOSX_USE_CARBON
1541 wxFAIL_MSG("Cannot create wxDCs lazily");
1542 #else
1543 OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
1544 if ( status != noErr )
1545 {
1546 wxFAIL_MSG("Cannot nest wxDCs on the same window");
1547 }
1548
1549 CGContextConcatCTM( m_cgContext, m_windowTransform );
1550 CGContextSaveGState( m_cgContext );
1551 m_releaseContext = true;
1552 if ( m_clipRgn.get() )
1553 {
1554 // the clip region is in device coordinates, so we convert this again to user coordinates
1555 wxCFRef<HIMutableShapeRef> hishape( HIShapeCreateMutableCopy( m_clipRgn ) );
1556 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero,m_windowTransform);
1557 HIShapeOffset( hishape, -transformedOrigin.x, -transformedOrigin.y );
1558 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1559 if ( HIShapeIsEmpty(hishape))
1560 {
1561 CGRect empty = CGRectMake( 0,0,0,0 );
1562 CGContextClipToRect( m_cgContext, empty );
1563 }
1564 else
1565 {
1566 HIShapeReplacePathInCGContext( hishape, m_cgContext );
1567 CGContextClip( m_cgContext );
1568 }
1569 }
1570 CGContextSaveGState( m_cgContext );
1571 #endif
1572 }
1573 }
1574
1575 // TODO test whether the private CGContextSetCompositeOperation works under 10.3 (using NSCompositingModes)
1576
1577 bool wxMacCoreGraphicsContext::SetLogicalFunction( int function )
1578 {
1579 if (m_logicalFunction == function)
1580 return true;
1581
1582 EnsureIsValid();
1583
1584 bool retval = false;
1585 bool shouldAntiAlias = true;
1586 CGBlendMode mode = kCGBlendModeNormal;
1587
1588 #if defined(__WXMAC__) && ( wxOSX_USE_IPHONE || ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 ) )
1589 #if wxOSX_USE_IPHONE
1590 if ( 1 )
1591 #else
1592 if ( UMAGetSystemVersion() >= 0x1050 )
1593 #endif
1594 {
1595 retval = true;
1596 switch ( function )
1597 {
1598 // TODO find best corresponding porter duff modes
1599 case wxCOPY :
1600 mode = kCGBlendModeCopy;
1601 break;
1602 case wxCLEAR :
1603 mode = kCGBlendModeClear;
1604 break;
1605 case wxXOR :
1606 mode = kCGBlendModeXOR;
1607 shouldAntiAlias = false;
1608 break;
1609 default :
1610 retval = false;
1611 break;
1612 }
1613 }
1614 else
1615 #endif
1616 {
1617 if ( function == wxCOPY )
1618 {
1619 retval = true;
1620 }
1621 else if ( function == wxINVERT || function == wxXOR )
1622 {
1623 // change color to white
1624 mode = kCGBlendModeExclusion;
1625 shouldAntiAlias = false;
1626 retval = true;
1627 }
1628 }
1629
1630 if (retval)
1631 {
1632 m_logicalFunction = function;
1633 CGContextSetBlendMode( m_cgContext, mode );
1634 CGContextSetShouldAntialias(m_cgContext, shouldAntiAlias);
1635 }
1636 return retval ;
1637 }
1638
1639 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
1640 {
1641 #if wxOSX_USE_CARBON
1642 if( m_cgContext )
1643 {
1644 wxCFRef<HIShapeRef> shape = wxCFRefFromGet(region.GetWXHRGN());
1645 // if the shape is empty, HIShapeReplacePathInCGContext doesn't work
1646 if ( HIShapeIsEmpty(shape))
1647 {
1648 CGRect empty = CGRectMake( 0,0,0,0 );
1649 CGContextClipToRect( m_cgContext, empty );
1650 }
1651 else
1652 {
1653 HIShapeReplacePathInCGContext( shape, m_cgContext );
1654 CGContextClip( m_cgContext );
1655 }
1656 }
1657 else
1658 {
1659 // this offsetting to device coords is not really correct, but since we cannot apply affine transforms
1660 // to regions we try at least to have correct translations
1661 HIMutableShapeRef mutableShape = HIShapeCreateMutableCopy( region.GetWXHRGN() );
1662
1663 CGPoint transformedOrigin = CGPointApplyAffineTransform( CGPointZero, m_windowTransform );
1664 HIShapeOffset( mutableShape, transformedOrigin.x, transformedOrigin.y );
1665 m_clipRgn.reset(mutableShape);
1666 }
1667 #else
1668 // allow usage as measuring context
1669 // wxASSERT_MSG( m_cgContext != NULL, "Needs a valid context for clipping" );
1670 #endif
1671 }
1672
1673 // clips drawings to the rect
1674 void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1675 {
1676 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
1677 if ( m_cgContext )
1678 {
1679 CGContextClipToRect( m_cgContext, r );
1680 }
1681 else
1682 {
1683 #if wxOSX_USE_CARBON
1684 // the clipping itself must be stored as device coordinates, otherwise
1685 // we cannot apply it back correctly
1686 r.origin= CGPointApplyAffineTransform( r.origin, m_windowTransform );
1687 m_clipRgn.reset(HIShapeCreateWithRect(&r));
1688 #else
1689 // allow usage as measuring context
1690 // wxFAIL_MSG( "Needs a valid context for clipping" );
1691 #endif
1692 }
1693 }
1694
1695 // resets the clipping to original extent
1696 void wxMacCoreGraphicsContext::ResetClip()
1697 {
1698 if ( m_cgContext )
1699 {
1700 // there is no way for clearing the clip, we can only revert to the stored
1701 // state, but then we have to make sure everything else is NOT restored
1702 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
1703 CGContextRestoreGState( m_cgContext );
1704 CGContextSaveGState( m_cgContext );
1705 CGAffineTransform transformNew = CGContextGetCTM( m_cgContext );
1706 transformNew = CGAffineTransformInvert( transformNew ) ;
1707 CGContextConcatCTM( m_cgContext, transformNew);
1708 CGContextConcatCTM( m_cgContext, transform);
1709 }
1710 else
1711 {
1712 #if wxOSX_USE_CARBON
1713 m_clipRgn.reset();
1714 #else
1715 // allow usage as measuring context
1716 // wxFAIL_MSG( "Needs a valid context for clipping" );
1717 #endif
1718 }
1719 }
1720
1721 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path )
1722 {
1723 if ( m_pen.IsNull() )
1724 return ;
1725
1726 EnsureIsValid();
1727
1728 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1729
1730 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1731 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1732 CGContextStrokePath( m_cgContext );
1733 }
1734
1735 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath &path , int fillStyle )
1736 {
1737 if ( !m_brush.IsNull() && ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1738 {
1739 // when using shading, we cannot draw pen and brush at the same time
1740 // revert to the base implementation of first filling and then stroking
1741 wxGraphicsContext::DrawPath( path, fillStyle );
1742 return;
1743 }
1744
1745 CGPathDrawingMode mode = kCGPathFill ;
1746 if ( m_brush.IsNull() )
1747 {
1748 if ( m_pen.IsNull() )
1749 return;
1750 else
1751 mode = kCGPathStroke;
1752 }
1753 else
1754 {
1755 if ( m_pen.IsNull() )
1756 {
1757 if ( fillStyle == wxODDEVEN_RULE )
1758 mode = kCGPathEOFill;
1759 else
1760 mode = kCGPathFill;
1761 }
1762 else
1763 {
1764 if ( fillStyle == wxODDEVEN_RULE )
1765 mode = kCGPathEOFillStroke;
1766 else
1767 mode = kCGPathFillStroke;
1768 }
1769 }
1770
1771 EnsureIsValid();
1772
1773 if ( !m_brush.IsNull() )
1774 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1775 if ( !m_pen.IsNull() )
1776 ((wxMacCoreGraphicsPenData*)m_pen.GetRefData())->Apply(this);
1777
1778 wxQuartzOffsetHelper helper( m_cgContext , ShouldOffset() );
1779
1780 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1781 CGContextDrawPath( m_cgContext , mode );
1782 }
1783
1784 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath &path , int fillStyle )
1785 {
1786 if ( m_brush.IsNull() )
1787 return;
1788
1789 EnsureIsValid();
1790
1791 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1792 {
1793 CGContextSaveGState( m_cgContext );
1794 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1795 CGContextClip( m_cgContext );
1796 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1797 CGContextRestoreGState( m_cgContext);
1798 }
1799 else
1800 {
1801 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1802 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1803 if ( fillStyle == wxODDEVEN_RULE )
1804 CGContextEOFillPath( m_cgContext );
1805 else
1806 CGContextFillPath( m_cgContext );
1807 }
1808 }
1809
1810 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
1811 {
1812 // we allow either setting or clearing but not replacing
1813 wxASSERT( m_cgContext == NULL || cg == NULL );
1814
1815 if ( m_cgContext )
1816 {
1817 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
1818 CGContextRestoreGState( m_cgContext );
1819 CGContextRestoreGState( m_cgContext );
1820 if ( m_releaseContext )
1821 {
1822 #if wxOSX_USE_CARBON
1823 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
1824 #endif
1825 }
1826 else
1827 CGContextRelease(m_cgContext);
1828 }
1829
1830
1831 m_cgContext = cg;
1832
1833 // FIXME: This check is needed because currently we need to use a DC/GraphicsContext
1834 // in order to get font properties, like wxFont::GetPixelSize, but since we don't have
1835 // a native window attached to use, I create a wxGraphicsContext with a NULL CGContextRef
1836 // for this one operation.
1837
1838 // When wxFont::GetPixelSize on Mac no longer needs a graphics context, this check
1839 // can be removed.
1840 if (m_cgContext)
1841 {
1842 CGContextRetain(m_cgContext);
1843 CGContextSaveGState( m_cgContext );
1844 CGContextSetTextMatrix( m_cgContext, CGAffineTransformIdentity );
1845 CGContextSaveGState( m_cgContext );
1846 m_releaseContext = false;
1847 }
1848 }
1849
1850 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
1851 {
1852 if ( m_cgContext )
1853 CGContextTranslateCTM( m_cgContext, (CGFloat) dx, (CGFloat) dy );
1854 else
1855 m_windowTransform = CGAffineTransformTranslate(m_windowTransform, (CGFloat) dx, (CGFloat) dy);
1856 }
1857
1858 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
1859 {
1860 if ( m_cgContext )
1861 CGContextScaleCTM( m_cgContext , (CGFloat) xScale , (CGFloat) yScale );
1862 else
1863 m_windowTransform = CGAffineTransformScale(m_windowTransform, (CGFloat) xScale, (CGFloat) yScale);
1864 }
1865
1866 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
1867 {
1868 if ( m_cgContext )
1869 CGContextRotateCTM( m_cgContext , (CGFloat) angle );
1870 else
1871 m_windowTransform = CGAffineTransformRotate(m_windowTransform, (CGFloat) angle);
1872 }
1873
1874 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1875 {
1876 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1877 DrawBitmap(bitmap, x, y, w, h);
1878 }
1879
1880 void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1881 {
1882 EnsureIsValid();
1883 #ifdef __WXMAC__
1884 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
1885 CGImageRef image = refdata->GetBitmap();
1886 CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h );
1887 if ( refdata->IsMonochrome() == 1 )
1888 {
1889 // is is a mask, the '1' in the mask tell where to draw the current brush
1890 if ( !m_brush.IsNull() )
1891 {
1892 if ( ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->IsShading() )
1893 {
1894 // TODO clip to mask
1895 /*
1896 CGContextSaveGState( m_cgContext );
1897 CGContextAddPath( m_cgContext , (CGPathRef) path.GetNativePath() );
1898 CGContextClip( m_cgContext );
1899 CGContextDrawShading( m_cgContext, ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->GetShading() );
1900 CGContextRestoreGState( m_cgContext);
1901 */
1902 }
1903 else
1904 {
1905 ((wxMacCoreGraphicsBrushData*)m_brush.GetRefData())->Apply(this);
1906 wxMacDrawCGImage( m_cgContext , &r , image );
1907 }
1908 }
1909 }
1910 else
1911 {
1912 wxMacDrawCGImage( m_cgContext , &r , image );
1913 }
1914 #endif
1915 }
1916
1917 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1918 {
1919 EnsureIsValid();
1920
1921 CGRect r = CGRectMake( (CGFloat) 0.0 , (CGFloat) 0.0 , (CGFloat) w , (CGFloat) h );
1922 CGContextSaveGState( m_cgContext );
1923 CGContextTranslateCTM( m_cgContext,(CGFloat) x ,(CGFloat) (y + h) );
1924 CGContextScaleCTM( m_cgContext, 1, -1 );
1925 #if wxOSX_USE_CARBON
1926 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
1927 NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) );
1928 #endif
1929 CGContextRestoreGState( m_cgContext );
1930 }
1931
1932 void wxMacCoreGraphicsContext::PushState()
1933 {
1934 EnsureIsValid();
1935
1936 CGContextSaveGState( m_cgContext );
1937 }
1938
1939 void wxMacCoreGraphicsContext::PopState()
1940 {
1941 EnsureIsValid();
1942
1943 CGContextRestoreGState( m_cgContext );
1944 }
1945
1946 void wxMacCoreGraphicsContext::DoDrawText( const wxString &str, wxDouble x, wxDouble y )
1947 {
1948 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
1949
1950 EnsureIsValid();
1951 #if wxOSX_USE_CORE_TEXT
1952 if ( UMAGetSystemVersion() >= 0x1050 )
1953 {
1954 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
1955 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
1956 CTFontRef font = fref->GetCTFont();
1957 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
1958 CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ;
1959 wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) );
1960 CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName };
1961 CFTypeRef values[] = { font, col, underlined };
1962 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
1963 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
1964 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
1965 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
1966
1967 y += CTFontGetAscent(font);
1968
1969 CGContextSaveGState(m_cgContext);
1970 CGContextTranslateCTM(m_cgContext, x, y);
1971 CGContextScaleCTM(m_cgContext, 1, -1);
1972 CGContextSetTextPosition(m_cgContext, 0, 0);
1973 CTLineDraw( line, m_cgContext );
1974 CGContextRestoreGState(m_cgContext);
1975 CFRelease( col );
1976 return;
1977 }
1978 #endif
1979 #if wxOSX_USE_ATSU_TEXT
1980 {
1981 DrawText(str, x, y, 0.0);
1982 return;
1983 }
1984 #endif
1985 #if wxOSX_USE_IPHONE
1986 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
1987
1988 CGContextSaveGState(m_cgContext);
1989
1990 CGColorRef col = wxMacCreateCGColor( fref->GetColour() );
1991 CGContextSetTextDrawingMode (m_cgContext, kCGTextFill);
1992 CGContextSetFillColorWithColor( m_cgContext, col );
1993
1994 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
1995 DrawTextInContext( m_cgContext, CGPointMake( x, y ), fref->GetUIFont() , text.AsNSString() );
1996
1997 CGContextRestoreGState(m_cgContext);
1998 CFRelease( col );
1999 #endif
2000 }
2001
2002 void wxMacCoreGraphicsContext::DoDrawRotatedText(const wxString &str,
2003 wxDouble x, wxDouble y,
2004 wxDouble angle)
2005 {
2006 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2007
2008 EnsureIsValid();
2009 #if wxOSX_USE_CORE_TEXT
2010 if ( UMAGetSystemVersion() >= 0x1050 )
2011 {
2012 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2013 wxGraphicsContext::DrawText( str, x, y, angle );
2014 return;
2015 }
2016 #endif
2017 #if wxOSX_USE_ATSU_TEXT
2018 {
2019 OSStatus status = noErr;
2020 ATSUTextLayout atsuLayout;
2021 wxMacUniCharBuffer unibuf( str );
2022 UniCharCount chars = unibuf.GetChars();
2023
2024 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2025 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2026 &chars , &style , &atsuLayout );
2027
2028 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
2029
2030 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2031 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2032
2033 int iAngle = int( angle * RAD2DEG );
2034 if ( abs(iAngle) > 0 )
2035 {
2036 Fixed atsuAngle = IntToFixed( iAngle );
2037 ATSUAttributeTag atsuTags[] =
2038 {
2039 kATSULineRotationTag ,
2040 };
2041 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
2042 {
2043 sizeof( Fixed ) ,
2044 };
2045 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
2046 {
2047 &atsuAngle ,
2048 };
2049 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
2050 atsuTags, atsuSizes, atsuValues );
2051 }
2052
2053 {
2054 ATSUAttributeTag atsuTags[] =
2055 {
2056 kATSUCGContextTag ,
2057 };
2058 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
2059 {
2060 sizeof( CGContextRef ) ,
2061 };
2062 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
2063 {
2064 &m_cgContext ,
2065 };
2066 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
2067 atsuTags, atsuSizes, atsuValues );
2068 }
2069
2070 ATSUTextMeasurement textBefore, textAfter;
2071 ATSUTextMeasurement ascent, descent;
2072
2073 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2074 &textBefore , &textAfter, &ascent , &descent );
2075
2076 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2077
2078 Rect rect;
2079 x += (int)(sin(angle) * FixedToInt(ascent));
2080 y += (int)(cos(angle) * FixedToInt(ascent));
2081
2082 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2083 IntToFixed(x) , IntToFixed(y) , &rect );
2084 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
2085
2086 CGContextSaveGState(m_cgContext);
2087 CGContextTranslateCTM(m_cgContext, (CGFloat) x, (CGFloat) y);
2088 CGContextScaleCTM(m_cgContext, 1, -1);
2089 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2090 IntToFixed(0) , IntToFixed(0) );
2091
2092 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
2093
2094 CGContextRestoreGState(m_cgContext);
2095
2096 ::ATSUDisposeTextLayout(atsuLayout);
2097
2098 return;
2099 }
2100 #endif
2101 #if wxOSX_USE_IPHONE
2102 // default implementation takes care of rotation and calls non rotated DrawText afterwards
2103 wxGraphicsContext::DoDrawRotatedText( str, x, y, angle );
2104 #endif
2105 }
2106
2107 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
2108 wxDouble *descent, wxDouble *externalLeading ) const
2109 {
2110 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::GetTextExtent - no valid font set") );
2111
2112 if ( width )
2113 *width = 0;
2114 if ( height )
2115 *height = 0;
2116 if ( descent )
2117 *descent = 0;
2118 if ( externalLeading )
2119 *externalLeading = 0;
2120
2121 if (str.empty())
2122 return;
2123
2124 #if wxOSX_USE_CORE_TEXT
2125 if ( UMAGetSystemVersion() >= 0x1050 )
2126 {
2127 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2128 CTFontRef font = fref->GetCTFont();
2129
2130 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2131 CFStringRef keys[] = { kCTFontAttributeName };
2132 CFTypeRef values[] = { font };
2133 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2134 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2135 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
2136 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2137
2138 CGFloat w, a, d, l;
2139
2140 w = CTLineGetTypographicBounds(line, &a, &d, &l) ;
2141
2142 if ( height )
2143 *height = a+d+l;
2144 if ( descent )
2145 *descent = d;
2146 if ( externalLeading )
2147 *externalLeading = l;
2148 if ( width )
2149 *width = w;
2150 return;
2151 }
2152 #endif
2153 #if wxOSX_USE_ATSU_TEXT
2154 {
2155 OSStatus status = noErr;
2156
2157 ATSUTextLayout atsuLayout;
2158 wxMacUniCharBuffer unibuf( str );
2159 UniCharCount chars = unibuf.GetChars();
2160
2161 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2162 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2163 &chars , &style , &atsuLayout );
2164
2165 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2166
2167 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2168 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2169
2170 ATSUTextMeasurement textBefore, textAfter;
2171 ATSUTextMeasurement textAscent, textDescent;
2172
2173 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
2174 &textBefore , &textAfter, &textAscent , &textDescent );
2175
2176 if ( height )
2177 *height = FixedToInt(textAscent + textDescent);
2178 if ( descent )
2179 *descent = FixedToInt(textDescent);
2180 if ( externalLeading )
2181 *externalLeading = 0;
2182 if ( width )
2183 *width = FixedToInt(textAfter - textBefore);
2184
2185 ::ATSUDisposeTextLayout(atsuLayout);
2186
2187 return;
2188 }
2189 #endif
2190 #if wxOSX_USE_IPHONE
2191 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2192
2193 wxCFStringRef text(str, wxLocale::GetSystemEncoding() );
2194 CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() );
2195
2196 if ( height )
2197 *height = sz.height;
2198 /*
2199 if ( descent )
2200 *descent = FixedToInt(textDescent);
2201 if ( externalLeading )
2202 *externalLeading = 0;
2203 */
2204 if ( width )
2205 *width = sz.width;
2206 #endif
2207 }
2208
2209 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
2210 {
2211 widths.Empty();
2212 widths.Add(0, text.length());
2213
2214 wxCHECK_RET( !m_font.IsNull(), wxT("wxMacCoreGraphicsContext::DrawText - no valid font set") );
2215
2216 if (text.empty())
2217 return;
2218
2219 #if wxOSX_USE_CORE_TEXT
2220 {
2221 wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
2222 CTFontRef font = fref->GetCTFont();
2223
2224 wxCFStringRef t(text, wxLocale::GetSystemEncoding() );
2225 CFStringRef keys[] = { kCTFontAttributeName };
2226 CFTypeRef values[] = { font };
2227 wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
2228 WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
2229 wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) );
2230 wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
2231
2232 int chars = text.length();
2233 for ( int pos = 0; pos < (int)chars; pos ++ )
2234 {
2235 widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL )+0.5;
2236 }
2237
2238 return;
2239 }
2240 #endif
2241 #if wxOSX_USE_ATSU_TEXT
2242 {
2243 OSStatus status = noErr;
2244 ATSUTextLayout atsuLayout;
2245 wxMacUniCharBuffer unibuf( text );
2246 UniCharCount chars = unibuf.GetChars();
2247
2248 ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle());
2249 status = ::ATSUCreateTextLayoutWithTextPtr( unibuf.GetBuffer() , 0 , chars , chars , 1 ,
2250 &chars , &style , &atsuLayout );
2251
2252 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
2253
2254 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
2255 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
2256
2257 // new implementation from JS, keep old one just in case
2258 #if 0
2259 for ( int pos = 0; pos < (int)chars; pos ++ )
2260 {
2261 unsigned long actualNumberOfBounds = 0;
2262 ATSTrapezoid glyphBounds;
2263
2264 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2265 OSStatus result;
2266 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2267 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2268 if (result != noErr || actualNumberOfBounds != 1 )
2269 return;
2270
2271 widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
2272 //unsigned char uch = s[i];
2273 }
2274 #else
2275 ATSLayoutRecord *layoutRecords = NULL;
2276 ItemCount glyphCount = 0;
2277
2278 // Get the glyph extents
2279 OSStatus err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(atsuLayout,
2280 0,
2281 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2282 (void **)
2283 &layoutRecords,
2284 &glyphCount);
2285 wxASSERT(glyphCount == (text.length()+1));
2286
2287 if ( err == noErr && glyphCount == (text.length()+1))
2288 {
2289 for ( int pos = 1; pos < (int)glyphCount ; pos ++ )
2290 {
2291 widths[pos-1] = FixedToInt( layoutRecords[pos].realPos );
2292 }
2293 }
2294
2295 ::ATSUDirectReleaseLayoutDataArrayPtr(NULL,
2296 kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
2297 (void **) &layoutRecords);
2298 #endif
2299 ::ATSUDisposeTextLayout(atsuLayout);
2300 }
2301 #endif
2302 #if wxOSX_USE_IPHONE
2303 // TODO core graphics text implementation here
2304 #endif
2305 }
2306
2307 void * wxMacCoreGraphicsContext::GetNativeContext()
2308 {
2309 return m_cgContext;
2310 }
2311
2312 // concatenates this transform with the current transform of this context
2313 void wxMacCoreGraphicsContext::ConcatTransform( const wxGraphicsMatrix& matrix )
2314 {
2315 if ( m_cgContext )
2316 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2317 else
2318 m_windowTransform = CGAffineTransformConcat(m_windowTransform, *(CGAffineTransform*) matrix.GetNativeMatrix());
2319 }
2320
2321 // sets the transform of this context
2322 void wxMacCoreGraphicsContext::SetTransform( const wxGraphicsMatrix& matrix )
2323 {
2324 if ( m_cgContext )
2325 {
2326 CGAffineTransform transform = CGContextGetCTM( m_cgContext );
2327 transform = CGAffineTransformInvert( transform ) ;
2328 CGContextConcatCTM( m_cgContext, transform);
2329 CGContextConcatCTM( m_cgContext, *(CGAffineTransform*) matrix.GetNativeMatrix());
2330 }
2331 else
2332 {
2333 m_windowTransform = *(CGAffineTransform*) matrix.GetNativeMatrix();
2334 }
2335 }
2336
2337 // gets the matrix of this context
2338 wxGraphicsMatrix wxMacCoreGraphicsContext::GetTransform() const
2339 {
2340 wxGraphicsMatrix m = CreateMatrix();
2341 *((CGAffineTransform*) m.GetNativeMatrix()) = ( m_cgContext == NULL ? m_windowTransform :
2342 CGContextGetCTM( m_cgContext ));
2343 return m;
2344 }
2345
2346 //
2347 // Renderer
2348 //
2349
2350 //-----------------------------------------------------------------------------
2351 // wxMacCoreGraphicsRenderer declaration
2352 //-----------------------------------------------------------------------------
2353
2354 class WXDLLIMPEXP_CORE wxMacCoreGraphicsRenderer : public wxGraphicsRenderer
2355 {
2356 public :
2357 wxMacCoreGraphicsRenderer() {}
2358
2359 virtual ~wxMacCoreGraphicsRenderer() {}
2360
2361 // Context
2362
2363 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2364 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
2365 #if wxUSE_PRINTING_ARCHITECTURE
2366 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
2367 #endif
2368
2369 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2370
2371 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2372
2373 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2374
2375 virtual wxGraphicsContext * CreateMeasuringContext();
2376
2377 // Path
2378
2379 virtual wxGraphicsPath CreatePath();
2380
2381 // Matrix
2382
2383 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2384 wxDouble tx=0.0, wxDouble ty=0.0);
2385
2386
2387 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2388
2389 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2390
2391 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2392 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2393 const wxColour&c1, const wxColour&c2) ;
2394
2395 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2396 // with radius r and color cColor
2397 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2398 const wxColour &oColor, const wxColour &cColor) ;
2399
2400 // sets the font
2401 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2402
2403 // create a native bitmap representation
2404 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ;
2405
2406 // create a native bitmap representation
2407 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
2408 private :
2409 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMacCoreGraphicsRenderer)
2410 } ;
2411
2412 //-----------------------------------------------------------------------------
2413 // wxMacCoreGraphicsRenderer implementation
2414 //-----------------------------------------------------------------------------
2415
2416 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsRenderer,wxGraphicsRenderer)
2417
2418 static wxMacCoreGraphicsRenderer gs_MacCoreGraphicsRenderer;
2419
2420 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2421 {
2422 return &gs_MacCoreGraphicsRenderer;
2423 }
2424
2425 #if defined( __WXCOCOA__ ) || wxOSX_USE_COCOA
2426 extern CGContextRef wxMacGetContextFromCurrentNSContext() ;
2427 #endif
2428
2429 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxWindowDC& dc )
2430 {
2431 const wxDCImpl* impl = dc.GetImpl();
2432 wxWindowDCImpl *win_impl = wxDynamicCast( impl, wxWindowDCImpl );
2433 if (win_impl)
2434 {
2435 int w, h;
2436 win_impl->GetSize( &w, &h );
2437 CGContextRef cgctx = 0;
2438 #ifdef __WXMAC__
2439 cgctx = (CGContextRef)(win_impl->GetWindow()->MacGetCGContextRef());
2440 #else
2441 cgctx = wxMacGetContextFromCurrentNSContext() ;
2442 #endif
2443 return new wxMacCoreGraphicsContext( this, cgctx, (wxDouble) w, (wxDouble) h );
2444 }
2445 return NULL;
2446 }
2447
2448 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxMemoryDC& dc )
2449 {
2450 #ifdef __WXMAC__
2451 const wxDCImpl* impl = dc.GetImpl();
2452 wxMemoryDCImpl *mem_impl = wxDynamicCast( impl, wxMemoryDCImpl );
2453 if (mem_impl)
2454 {
2455 int w, h;
2456 mem_impl->GetSize( &w, &h );
2457 return new wxMacCoreGraphicsContext( this,
2458 (CGContextRef)(mem_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2459 }
2460 #endif
2461 return NULL;
2462 }
2463
2464 #if wxUSE_PRINTING_ARCHITECTURE
2465 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( const wxPrinterDC& dc )
2466 {
2467 #ifdef __WXMAC__
2468 const wxDCImpl* impl = dc.GetImpl();
2469 wxPrinterDCImpl *print_impl = wxDynamicCast( impl, wxPrinterDCImpl );
2470 if (print_impl)
2471 {
2472 int w, h;
2473 print_impl->GetSize( &w, &h );
2474 return new wxMacCoreGraphicsContext( this,
2475 (CGContextRef)(print_impl->GetGraphicsContext()->GetNativeContext()), (wxDouble) w, (wxDouble) h );
2476 }
2477 #endif
2478 return NULL;
2479 }
2480 #endif
2481
2482 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeContext( void * context )
2483 {
2484 return new wxMacCoreGraphicsContext(this,(CGContextRef)context);
2485 }
2486
2487 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContextFromNativeWindow( void * window )
2488 {
2489 #if wxOSX_USE_CARBON
2490 return new wxMacCoreGraphicsContext(this,(WindowRef)window);
2491 #else
2492 return NULL;
2493 #endif
2494 }
2495
2496 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateContext( wxWindow* window )
2497 {
2498 return new wxMacCoreGraphicsContext(this, window );
2499 }
2500
2501 wxGraphicsContext * wxMacCoreGraphicsRenderer::CreateMeasuringContext()
2502 {
2503 return new wxMacCoreGraphicsContext(this);
2504 }
2505
2506 // Path
2507
2508 wxGraphicsPath wxMacCoreGraphicsRenderer::CreatePath()
2509 {
2510 wxGraphicsPath m;
2511 m.SetRefData( new wxMacCoreGraphicsPathData(this));
2512 return m;
2513 }
2514
2515
2516 // Matrix
2517
2518 wxGraphicsMatrix wxMacCoreGraphicsRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2519 wxDouble tx, wxDouble ty)
2520 {
2521 wxGraphicsMatrix m;
2522 wxMacCoreGraphicsMatrixData* data = new wxMacCoreGraphicsMatrixData( this );
2523 data->Set( a,b,c,d,tx,ty ) ;
2524 m.SetRefData(data);
2525 return m;
2526 }
2527
2528 wxGraphicsPen wxMacCoreGraphicsRenderer::CreatePen(const wxPen& pen)
2529 {
2530 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
2531 return wxNullGraphicsPen;
2532 else
2533 {
2534 wxGraphicsPen p;
2535 p.SetRefData(new wxMacCoreGraphicsPenData( this, pen ));
2536 return p;
2537 }
2538 }
2539
2540 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateBrush(const wxBrush& brush )
2541 {
2542 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
2543 return wxNullGraphicsBrush;
2544 else
2545 {
2546 wxGraphicsBrush p;
2547 p.SetRefData(new wxMacCoreGraphicsBrushData( this, brush ));
2548 return p;
2549 }
2550 }
2551
2552 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateBitmap( const wxBitmap& bmp )
2553 {
2554 if ( bmp.Ok() )
2555 {
2556 wxGraphicsBitmap p;
2557 #ifdef __WXMAC__
2558 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , bmp.CreateCGImage(), bmp.GetDepth() == 1 ) );
2559 #endif
2560 return p;
2561 }
2562 else
2563 return wxNullGraphicsBitmap;
2564 }
2565
2566 wxGraphicsBitmap wxMacCoreGraphicsRenderer::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2567 {
2568 wxMacCoreGraphicsBitmapData* refdata =static_cast<wxMacCoreGraphicsBitmapData*>(bmp.GetRefData());
2569 CGImageRef img = refdata->GetBitmap();
2570 if ( img )
2571 {
2572 wxGraphicsBitmap p;
2573 CGImageRef subimg = CGImageCreateWithImageInRect(img,CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ));
2574 p.SetRefData(new wxMacCoreGraphicsBitmapData( this , subimg, refdata->IsMonochrome() ) );
2575 return p;
2576 }
2577 else
2578 return wxNullGraphicsBitmap;
2579 }
2580
2581 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2582 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
2583 const wxColour&c1, const wxColour&c2)
2584 {
2585 wxGraphicsBrush p;
2586 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2587 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
2588 p.SetRefData(d);
2589 return p;
2590 }
2591
2592 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
2593 // with radius r and color cColor
2594 wxGraphicsBrush wxMacCoreGraphicsRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
2595 const wxColour &oColor, const wxColour &cColor)
2596 {
2597 wxGraphicsBrush p;
2598 wxMacCoreGraphicsBrushData* d = new wxMacCoreGraphicsBrushData( this );
2599 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
2600 p.SetRefData(d);
2601 return p;
2602 }
2603
2604 // sets the font
2605 wxGraphicsFont wxMacCoreGraphicsRenderer::CreateFont( const wxFont &font , const wxColour &col )
2606 {
2607 if ( font.Ok() )
2608 {
2609 wxGraphicsFont p;
2610 p.SetRefData(new wxMacCoreGraphicsFontData( this , font, col ));
2611 return p;
2612 }
2613 else
2614 return wxNullGraphicsFont;
2615 }
2616
2617 //
2618 // CoreGraphics Helper Methods
2619 //
2620
2621 // Data Providers and Consumers
2622
2623 size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
2624 {
2625 CFMutableDataRef data = (CFMutableDataRef) info;
2626 if ( data )
2627 {
2628 CFDataAppendBytes( data, (const UInt8*) bytes, count );
2629 }
2630 return count;
2631 }
2632
2633 void wxMacReleaseCFDataProviderCallback(void *info,
2634 const void *WXUNUSED(data),
2635 size_t WXUNUSED(count))
2636 {
2637 if ( info )
2638 CFRelease( (CFDataRef) info );
2639 }
2640
2641 void wxMacReleaseCFDataConsumerCallback( void *info )
2642 {
2643 if ( info )
2644 CFRelease( (CFDataRef) info );
2645 }
2646
2647 CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data )
2648 {
2649 if ( data == NULL )
2650 return NULL;
2651
2652 return CGDataProviderCreateWithCFData( data );
2653 }
2654
2655 CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data )
2656 {
2657 if ( data == NULL )
2658 return NULL;
2659
2660 return CGDataConsumerCreateWithCFData( data );
2661 }
2662
2663 void
2664 wxMacReleaseMemoryBufferProviderCallback(void *info,
2665 const void * WXUNUSED_UNLESS_DEBUG(data),
2666 size_t WXUNUSED(size))
2667 {
2668 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
2669
2670 wxASSERT( data == membuf->GetData() ) ;
2671
2672 delete membuf ;
2673 }
2674
2675 CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf )
2676 {
2677 wxMemoryBuffer* b = new wxMemoryBuffer( buf );
2678 if ( b->GetDataLen() == 0 )
2679 return NULL;
2680
2681 return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() ,
2682 wxMacReleaseMemoryBufferProviderCallback );
2683 }