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