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