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