]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/graphics.cpp
fixed unused var warning
[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 #if wxUSE_GRAPHICS_CONTEXT && wxMAC_USE_CORE_GRAPHICS
17
18 #ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/app.h"
21 #include "wx/dcmemory.h"
22 #include "wx/dcprint.h"
23 #include "wx/region.h"
24 #include "wx/image.h"
25 #endif
26
27 #include "wx/mac/uma.h"
28
29
30 #ifdef __MSL__
31 #if __MSL__ >= 0x6000
32 #include "math.h"
33 // in case our functions were defined outside std, we make it known all the same
34 namespace std { }
35 using namespace std;
36 #endif
37 #endif
38
39 #include "wx/mac/private.h"
40
41 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
42 typedef float CGFloat;
43 #endif
44
45 //
46 // Graphics Path
47 //
48
49 class WXDLLEXPORT wxMacCoreGraphicsPath : public wxGraphicsPath
50 {
51 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsPath)
52 public :
53 wxMacCoreGraphicsPath();
54 ~wxMacCoreGraphicsPath();
55
56 // begins a new subpath at (x,y)
57 virtual void MoveToPoint( wxDouble x, wxDouble y );
58
59 // adds a straight line from the current point to (x,y)
60 virtual void AddLineToPoint( wxDouble x, wxDouble y );
61
62 // adds a cubic Bezier curve from the current point, using two control points and an end point
63 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
64
65 // closes the current sub-path
66 virtual void CloseSubpath();
67
68 // gets the last point of the current path, (0,0) if not yet set
69 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y);
70
71 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
72 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
73
74 //
75 // These are convenience functions which - if not available natively will be assembled
76 // using the primitives from above
77 //
78
79 // adds a quadratic Bezier curve from the current point, using a control point and an end point
80 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
81
82 // appends a rectangle as a new closed subpath
83 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
84
85 // appends an ellipsis as a new closed subpath fitting the passed rectangle
86 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
87
88 // 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)
89 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
90
91 CGPathRef GetPath() const;
92 private :
93 CGMutablePathRef m_path;
94 };
95
96 wxMacCoreGraphicsPath::wxMacCoreGraphicsPath()
97 {
98 m_path = CGPathCreateMutable();
99 }
100
101 wxMacCoreGraphicsPath::~wxMacCoreGraphicsPath()
102 {
103 CGPathRelease( m_path );
104 }
105
106 // opens (starts) a new subpath
107 void wxMacCoreGraphicsPath::MoveToPoint( wxDouble x1 , wxDouble y1 )
108 {
109 CGPathMoveToPoint( m_path , NULL , x1 , y1 );
110 }
111
112 void wxMacCoreGraphicsPath::AddLineToPoint( wxDouble x1 , wxDouble y1 )
113 {
114 CGPathAddLineToPoint( m_path , NULL , x1 , y1 );
115 }
116
117 void wxMacCoreGraphicsPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
118 {
119 CGPathAddCurveToPoint( m_path , NULL , cx1 , cy1 , cx2, cy2, x , y );
120 }
121
122 void wxMacCoreGraphicsPath::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
123 {
124 CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x , y );
125 }
126
127 void wxMacCoreGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
128 {
129 CGRect cgRect = { { x , y } , { w , h } };
130 CGPathAddRect( m_path , NULL , cgRect );
131 }
132
133 void wxMacCoreGraphicsPath::AddCircle( wxDouble x, wxDouble y , wxDouble r )
134 {
135 CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true );
136 }
137
138 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
139 void wxMacCoreGraphicsPath::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
140 {
141 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
142 CGPathAddArc( m_path, NULL , x, y, r, startAngle, endAngle, !clockwise);
143 }
144
145 void wxMacCoreGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
146 {
147 CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
148 }
149
150 // closes the current subpath
151 void wxMacCoreGraphicsPath::CloseSubpath()
152 {
153 CGPathCloseSubpath( m_path );
154 }
155
156 CGPathRef wxMacCoreGraphicsPath::GetPath() const
157 {
158 return m_path;
159 }
160
161 // gets the last point of the current path, (0,0) if not yet set
162 void wxMacCoreGraphicsPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
163 {
164 CGPoint p = CGPathGetCurrentPoint( m_path );
165 x = p.x;
166 y = p.y;
167 }
168
169 //
170 // Graphics Context
171 //
172
173 class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
174 {
175 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext)
176
177 public:
178 wxMacCoreGraphicsContext( CGrafPtr port );
179
180 wxMacCoreGraphicsContext( CGContextRef cgcontext );
181
182 wxMacCoreGraphicsContext();
183
184 ~wxMacCoreGraphicsContext();
185
186
187 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
188 virtual wxGraphicsPath * CreatePath();
189
190 // push the current state of the context, ie the transformation matrix on a stack
191 virtual void PushState();
192
193 // pops a stored state from the stack
194 virtual void PopState();
195
196 // clips drawings to the region
197 virtual void Clip( const wxRegion &region );
198
199 //
200 // transformation
201 //
202
203 // translate
204 virtual void Translate( wxDouble dx , wxDouble dy );
205
206 // scale
207 virtual void Scale( wxDouble xScale , wxDouble yScale );
208
209 // rotate (radians)
210 virtual void Rotate( wxDouble angle );
211
212 //
213 // setting the paint
214 //
215
216 // sets the pan
217 virtual void SetPen( const wxPen &pen );
218
219 // sets the brush for filling
220 virtual void SetBrush( const wxBrush &brush );
221
222 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
223 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
224 const wxColour&c1, const wxColour&c2);
225
226 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
227 // with radius r and color cColor
228 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
229 const wxColour &oColor, const wxColour &cColor);
230
231 // sets the font
232 virtual void SetFont( const wxFont &font );
233
234 // sets the text color
235 virtual void SetTextColor( const wxColour &col );
236
237 // strokes along a path with the current pen
238 virtual void StrokePath( const wxGraphicsPath *path );
239
240 // fills a path with the current brush
241 virtual void FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
242
243 // draws a path by first filling and then stroking
244 virtual void DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
245
246 //
247 // text
248 //
249
250 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y );
251
252 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
253
254 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
255 wxDouble *descent, wxDouble *externalLeading ) const;
256
257 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
258
259 //
260 // image support
261 //
262
263 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
264
265 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
266
267 CGContextRef GetNativeContext();
268 void SetNativeContext( CGContextRef cg );
269 CGPathDrawingMode GetDrawingMode() const { return m_mode; }
270
271 private:
272 CGContextRef m_cgContext;
273 CGrafPtr m_qdPort;
274 CGPathDrawingMode m_mode;
275 ATSUStyle m_macATSUIStyle;
276 wxPen m_pen;
277 wxBrush m_brush;
278 wxColor m_textForegroundColor;
279 };
280
281 //-----------------------------------------------------------------------------
282 // constants
283 //-----------------------------------------------------------------------------
284
285 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
286 #ifndef M_PI
287 const double M_PI = 3.14159265358979;
288 #endif
289 #endif
290
291 const double RAD2DEG = 180.0 / M_PI;
292 const short kEmulatedMode = -1;
293 const short kUnsupportedMode = -2;
294
295 extern TECObjectRef s_TECNativeCToUnicode;
296
297 //-----------------------------------------------------------------------------
298 // Local functions
299 //-----------------------------------------------------------------------------
300
301 static inline double dmin(double a, double b) { return a < b ? a : b; }
302 static inline double dmax(double a, double b) { return a > b ? a : b; }
303 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
304
305 //-----------------------------------------------------------------------------
306 // device context implementation
307 //
308 // more and more of the dc functionality should be implemented by calling
309 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
310 // also coordinate conversions should be moved to native matrix ops
311 //-----------------------------------------------------------------------------
312
313 // we always stock two context states, one at entry, to be able to preserve the
314 // state we were called with, the other one after changing to HI Graphics orientation
315 // (this one is used for getting back clippings etc)
316
317 //-----------------------------------------------------------------------------
318 // wxGraphicsPath implementation
319 //-----------------------------------------------------------------------------
320
321 //-----------------------------------------------------------------------------
322 // wxGraphicsContext implementation
323 //-----------------------------------------------------------------------------
324
325 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( CGrafPtr port )
326 {
327 m_qdPort = port;
328 m_cgContext = NULL;
329 m_mode = kCGPathFill;
330 m_macATSUIStyle = NULL;
331 }
332
333 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( CGContextRef cgcontext )
334 {
335 m_qdPort = NULL;
336 m_cgContext = cgcontext;
337 m_mode = kCGPathFill;
338 m_macATSUIStyle = NULL;
339 CGContextSaveGState( m_cgContext );
340 CGContextSaveGState( m_cgContext );
341 }
342
343 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext()
344 {
345 m_qdPort = NULL;
346 m_cgContext = NULL;
347 m_mode = kCGPathFill;
348 m_macATSUIStyle = NULL;
349 }
350
351 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
352 {
353 if ( m_cgContext )
354 {
355 CGContextSynchronize( m_cgContext );
356 CGContextRestoreGState( m_cgContext );
357 CGContextRestoreGState( m_cgContext );
358 }
359
360 if ( m_qdPort )
361 CGContextRelease( m_cgContext );
362 }
363
364 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
365 {
366 // ClipCGContextToRegion ( m_cgContext, &bounds , (RgnHandle) dc->m_macCurrentClipRgn );
367 }
368
369 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath *p )
370 {
371 const wxMacCoreGraphicsPath* path = dynamic_cast< const wxMacCoreGraphicsPath*>( p );
372 CGContextAddPath( m_cgContext , path->GetPath() );
373 CGContextStrokePath( m_cgContext );
374 }
375
376 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath *p , int fillStyle )
377 {
378 const wxMacCoreGraphicsPath* path = dynamic_cast< const wxMacCoreGraphicsPath*>( p );
379 CGPathDrawingMode mode = m_mode;
380
381 if ( fillStyle == wxODDEVEN_RULE )
382 {
383 if ( mode == kCGPathFill )
384 mode = kCGPathEOFill;
385 else if ( mode == kCGPathFillStroke )
386 mode = kCGPathEOFillStroke;
387 }
388
389 CGContextAddPath( m_cgContext , path->GetPath() );
390 CGContextDrawPath( m_cgContext , mode );
391 }
392
393 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath *p , int fillStyle )
394 {
395 const wxMacCoreGraphicsPath* path = dynamic_cast< const wxMacCoreGraphicsPath*>( p );
396
397 CGContextAddPath( m_cgContext , path->GetPath() );
398 if ( fillStyle == wxODDEVEN_RULE )
399 CGContextEOFillPath( m_cgContext );
400 else
401 CGContextFillPath( m_cgContext );
402 }
403
404 wxGraphicsPath* wxMacCoreGraphicsContext::CreatePath()
405 {
406 // make sure that we now have a real cgref, before doing
407 // anything with paths
408 CGContextRef cg = GetNativeContext();
409 cg = NULL;
410
411 return new wxMacCoreGraphicsPath();
412 }
413
414 CGContextRef wxMacCoreGraphicsContext::GetNativeContext()
415 {
416 return m_cgContext;
417 }
418
419 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
420 {
421 // we allow either setting or clearing but not replacing
422 wxASSERT( m_cgContext == NULL || cg == NULL );
423
424 if ( cg )
425 CGContextSaveGState( cg );
426 m_cgContext = cg;
427 }
428
429 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
430 {
431 CGContextTranslateCTM( m_cgContext, dx, dy );
432 }
433
434 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
435 {
436 CGContextScaleCTM( m_cgContext , xScale , yScale );
437 }
438
439 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
440 {
441 CGContextRotateCTM( m_cgContext , angle );
442 }
443
444 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
445 {
446 CGImageRef image = (CGImageRef)( bmp.CGImageCreate() );
447 HIRect r = CGRectMake( x , y , w , h );
448 HIViewDrawCGImage( m_cgContext , &r , image );
449 CGImageRelease( image );
450 }
451
452 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
453 {
454 CGRect r = CGRectMake( 00 , 00 , w , h );
455 CGContextSaveGState( m_cgContext );
456 CGContextTranslateCTM( m_cgContext, x , y + h );
457 CGContextScaleCTM( m_cgContext, 1, -1 );
458 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
459 NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) );
460 CGContextRestoreGState( m_cgContext );
461 }
462
463 void wxMacCoreGraphicsContext::PushState()
464 {
465 CGContextSaveGState( m_cgContext );
466 }
467
468 void wxMacCoreGraphicsContext::PopState()
469 {
470 CGContextRestoreGState( m_cgContext );
471 }
472
473 void wxMacCoreGraphicsContext::SetTextColor( const wxColour &col )
474 {
475 m_textForegroundColor = col;
476 }
477
478 #pragma mark -
479 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
480
481 // CGPattern wrapper class: always allocate on heap, never call destructor
482
483 class wxMacCoreGraphicsPattern
484 {
485 public :
486 wxMacCoreGraphicsPattern() {}
487
488 // is guaranteed to be called only with a non-Null CGContextRef
489 virtual void Render( CGContextRef ctxRef ) = 0;
490
491 operator CGPatternRef() const { return m_patternRef; }
492
493 protected :
494 virtual ~wxMacCoreGraphicsPattern()
495 {
496 // as this is called only when the m_patternRef is been released;
497 // don't release it again
498 }
499
500 static void _Render( void *info, CGContextRef ctxRef )
501 {
502 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
503 if ( self && ctxRef )
504 self->Render( ctxRef );
505 }
506
507 static void _Dispose( void *info )
508 {
509 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
510 delete self;
511 }
512
513 CGPatternRef m_patternRef;
514
515 static const CGPatternCallbacks ms_Callbacks;
516 };
517
518 const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose };
519
520 class ImagePattern : public wxMacCoreGraphicsPattern
521 {
522 public :
523 ImagePattern( const wxBitmap* bmp , CGAffineTransform transform )
524 {
525 wxASSERT( bmp && bmp->Ok() );
526
527 Init( (CGImageRef) bmp->CGImageCreate() , transform );
528 }
529
530 // ImagePattern takes ownership of CGImageRef passed in
531 ImagePattern( CGImageRef image , CGAffineTransform transform )
532 {
533 if ( image )
534 CFRetain( image );
535
536 Init( image , transform );
537 }
538
539 virtual void Render( CGContextRef ctxRef )
540 {
541 if (m_image != NULL)
542 HIViewDrawCGImage( ctxRef, &m_imageBounds, m_image );
543 }
544
545 protected :
546 void Init( CGImageRef image, CGAffineTransform transform )
547 {
548 m_image = image;
549 if ( m_image )
550 {
551 m_imageBounds = CGRectMake( 0.0, 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) );
552 m_patternRef = CGPatternCreate(
553 this , m_imageBounds, transform ,
554 m_imageBounds.size.width, m_imageBounds.size.height,
555 kCGPatternTilingNoDistortion, true , &wxMacCoreGraphicsPattern::ms_Callbacks );
556 }
557 }
558
559 virtual ~ImagePattern()
560 {
561 if ( m_image )
562 CGImageRelease( m_image );
563 }
564
565 CGImageRef m_image;
566 CGRect m_imageBounds;
567 };
568
569 class HatchPattern : public wxMacCoreGraphicsPattern
570 {
571 public :
572 HatchPattern( int hatchstyle, CGAffineTransform transform )
573 {
574 m_hatch = hatchstyle;
575 m_imageBounds = CGRectMake( 0.0, 0.0, 8.0 , 8.0 );
576 m_patternRef = CGPatternCreate(
577 this , m_imageBounds, transform ,
578 m_imageBounds.size.width, m_imageBounds.size.height,
579 kCGPatternTilingNoDistortion, false , &wxMacCoreGraphicsPattern::ms_Callbacks );
580 }
581
582 void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
583 {
584 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
585 if ( UMAGetSystemVersion() >= 0x1040 )
586 {
587 CGContextStrokeLineSegments( ctxRef , pts , count );
588 }
589 else
590 #endif
591 {
592 CGContextBeginPath( ctxRef );
593 for (size_t i = 0; i < count; i += 2)
594 {
595 CGContextMoveToPoint(ctxRef, pts[i].x, pts[i].y);
596 CGContextAddLineToPoint(ctxRef, pts[i+1].x, pts[i+1].y);
597 }
598 CGContextStrokePath(ctxRef);
599 }
600 }
601
602 virtual void Render( CGContextRef ctxRef )
603 {
604 switch ( m_hatch )
605 {
606 case wxBDIAGONAL_HATCH :
607 {
608 CGPoint pts[] =
609 {
610 { 8.0 , 0.0 } , { 0.0 , 8.0 }
611 };
612 StrokeLineSegments( ctxRef , pts , 2 );
613 }
614 break;
615
616 case wxCROSSDIAG_HATCH :
617 {
618 CGPoint pts[] =
619 {
620 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
621 { 8.0 , 0.0 } , { 0.0 , 8.0 }
622 };
623 StrokeLineSegments( ctxRef , pts , 4 );
624 }
625 break;
626
627 case wxFDIAGONAL_HATCH :
628 {
629 CGPoint pts[] =
630 {
631 { 0.0 , 0.0 } , { 8.0 , 8.0 }
632 };
633 StrokeLineSegments( ctxRef , pts , 2 );
634 }
635 break;
636
637 case wxCROSS_HATCH :
638 {
639 CGPoint pts[] =
640 {
641 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
642 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
643 };
644 StrokeLineSegments( ctxRef , pts , 4 );
645 }
646 break;
647
648 case wxHORIZONTAL_HATCH :
649 {
650 CGPoint pts[] =
651 {
652 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
653 };
654 StrokeLineSegments( ctxRef , pts , 2 );
655 }
656 break;
657
658 case wxVERTICAL_HATCH :
659 {
660 CGPoint pts[] =
661 {
662 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
663 };
664 StrokeLineSegments( ctxRef , pts , 2 );
665 }
666 break;
667
668 default:
669 break;
670 }
671 }
672
673 protected :
674 virtual ~HatchPattern() {}
675
676 CGRect m_imageBounds;
677 int m_hatch;
678 };
679
680 #pragma mark -
681
682 void wxMacCoreGraphicsContext::SetPen( const wxPen &pen )
683 {
684 m_pen = pen;
685 if ( m_cgContext == NULL )
686 return;
687
688 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
689 bool stroke = pen.GetStyle() != wxTRANSPARENT;
690
691 #if 0
692 // we can benchmark performance; should go into a setting eventually
693 CGContextSetShouldAntialias( m_cgContext , false );
694 #endif
695
696 if ( fill | stroke )
697 {
698 // set up brushes
699 m_mode = kCGPathFill; // just a default
700
701 if ( stroke )
702 {
703 CGContextSetRGBStrokeColor( m_cgContext , pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 ,
704 pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 );
705
706 // TODO: * m_dc->m_scaleX
707 CGFloat penWidth = pen.GetWidth();
708 if (penWidth <= 0.0)
709 penWidth = 0.1;
710 CGContextSetLineWidth( m_cgContext , penWidth );
711
712 CGLineCap cap;
713 switch ( pen.GetCap() )
714 {
715 case wxCAP_ROUND :
716 cap = kCGLineCapRound;
717 break;
718
719 case wxCAP_PROJECTING :
720 cap = kCGLineCapSquare;
721 break;
722
723 case wxCAP_BUTT :
724 cap = kCGLineCapButt;
725 break;
726
727 default :
728 cap = kCGLineCapButt;
729 break;
730 }
731
732 CGLineJoin join;
733 switch ( pen.GetJoin() )
734 {
735 case wxJOIN_BEVEL :
736 join = kCGLineJoinBevel;
737 break;
738
739 case wxJOIN_MITER :
740 join = kCGLineJoinMiter;
741 break;
742
743 case wxJOIN_ROUND :
744 join = kCGLineJoinRound;
745 break;
746
747 default :
748 join = kCGLineJoinMiter;
749 break;
750 }
751
752 m_mode = kCGPathStroke;
753 int count = 0;
754
755 const CGFloat *lengths = NULL;
756 CGFloat *userLengths = NULL;
757
758 const CGFloat dashUnit = penWidth < 1.0 ? 1.0 : penWidth;
759
760 const CGFloat dotted[] = { dashUnit , dashUnit + 2.0 };
761 const CGFloat short_dashed[] = { 9.0 , 6.0 };
762 const CGFloat dashed[] = { 19.0 , 9.0 };
763 const CGFloat dotted_dashed[] = { 9.0 , 6.0 , 3.0 , 3.0 };
764
765 switch ( pen.GetStyle() )
766 {
767 case wxSOLID :
768 break;
769
770 case wxDOT :
771 lengths = dotted;
772 count = WXSIZEOF(dotted);
773 break;
774
775 case wxLONG_DASH :
776 lengths = dashed;
777 count = WXSIZEOF(dashed);
778 break;
779
780 case wxSHORT_DASH :
781 lengths = short_dashed;
782 count = WXSIZEOF(short_dashed);
783 break;
784
785 case wxDOT_DASH :
786 lengths = dotted_dashed;
787 count = WXSIZEOF(dotted_dashed);
788 break;
789
790 case wxUSER_DASH :
791 wxDash *dashes;
792 count = pen.GetDashes( &dashes );
793 if ((dashes != NULL) && (count > 0))
794 {
795 userLengths = new CGFloat[count];
796 for ( int i = 0; i < count; ++i )
797 {
798 userLengths[i] = dashes[i] * dashUnit;
799
800 if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 )
801 userLengths[i] = dashUnit + 2.0;
802 else if ( i % 2 == 0 && userLengths[i] < dashUnit )
803 userLengths[i] = dashUnit;
804 }
805 }
806 lengths = userLengths;
807 break;
808
809 case wxSTIPPLE :
810 {
811 CGFloat alphaArray[1] = { 1.0 };
812 wxBitmap* bmp = pen.GetStipple();
813 if ( bmp && bmp->Ok() )
814 {
815 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) );
816 CGContextSetStrokeColorSpace( m_cgContext , patternSpace );
817 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
818 CGContextSetStrokePattern( m_cgContext, pattern , alphaArray );
819 }
820 }
821 break;
822
823 default :
824 {
825 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
826 CGContextSetStrokeColorSpace( m_cgContext , patternSpace );
827 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( pen.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
828
829 CGFloat colorArray[4] = { pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 ,
830 pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 };
831
832 CGContextSetStrokePattern( m_cgContext, pattern , colorArray );
833 }
834 break;
835 }
836
837 if ((lengths != NULL) && (count > 0))
838 {
839 CGContextSetLineDash( m_cgContext , 0 , lengths , count );
840 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
841 cap = kCGLineCapButt;
842 }
843 else
844 {
845 CGContextSetLineDash( m_cgContext , 0 , NULL , 0 );
846 }
847
848 CGContextSetLineCap( m_cgContext , cap );
849 CGContextSetLineJoin( m_cgContext , join );
850
851 delete[] userLengths;
852 }
853
854 if ( fill && stroke )
855 m_mode = kCGPathFillStroke;
856 }
857 }
858
859 void wxMacCoreGraphicsContext::SetBrush( const wxBrush &brush )
860 {
861 m_brush = brush;
862 if ( m_cgContext == NULL )
863 return;
864
865 bool fill = brush.GetStyle() != wxTRANSPARENT;
866 bool stroke = m_pen.GetStyle() != wxTRANSPARENT;
867
868 #if 0
869 // we can benchmark performance, should go into a setting later
870 CGContextSetShouldAntialias( m_cgContext , false );
871 #endif
872
873 if ( fill | stroke )
874 {
875 // setup brushes
876 m_mode = kCGPathFill; // just a default
877
878 if ( fill )
879 {
880 if ( brush.GetStyle() == wxSOLID )
881 {
882 CGContextSetRGBFillColor( m_cgContext , brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
883 brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 );
884 }
885 else if ( brush.IsHatch() )
886 {
887 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
888 CGContextSetFillColorSpace( m_cgContext , patternSpace );
889 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( brush.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
890
891 CGFloat colorArray[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
892 brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 };
893
894 CGContextSetFillPattern( m_cgContext, pattern , colorArray );
895 }
896 else
897 {
898 // now brush is a bitmap
899 CGFloat alphaArray[1] = { 1.0 };
900 wxBitmap* bmp = brush.GetStipple();
901 if ( bmp && bmp->Ok() )
902 {
903 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) );
904 CGContextSetFillColorSpace( m_cgContext , patternSpace );
905 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
906 CGContextSetFillPattern( m_cgContext, pattern , alphaArray );
907 }
908 }
909
910 m_mode = kCGPathFill;
911 }
912
913 if ( fill && stroke )
914 m_mode = kCGPathFillStroke;
915 else if ( stroke )
916 m_mode = kCGPathStroke;
917 }
918 }
919
920 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
921 void wxMacCoreGraphicsContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
922 const wxColour&c1, const wxColour&c2)
923 {
924 }
925
926 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
927 // with radius r and color cColor
928 void wxMacCoreGraphicsContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
929 const wxColour &oColor, const wxColour &cColor)
930 {
931 }
932
933
934 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
935 {
936 DrawText(str, x, y, 0.0);
937 }
938
939 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
940 {
941 OSStatus status = noErr;
942 ATSUTextLayout atsuLayout;
943 UniCharCount chars = str.length();
944 UniChar* ubuf = NULL;
945
946 #if SIZEOF_WCHAR_T == 4
947 wxMBConvUTF16 converter;
948 #if wxUSE_UNICODE
949 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
950 ubuf = (UniChar*) malloc( unicharlen + 2 );
951 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
952 #else
953 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
954 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
955 ubuf = (UniChar*) malloc( unicharlen + 2 );
956 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
957 #endif
958 chars = unicharlen / 2;
959 #else
960 #if wxUSE_UNICODE
961 ubuf = (UniChar*) str.wc_str();
962 #else
963 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
964 chars = wxWcslen( wchar.data() );
965 ubuf = (UniChar*) wchar.data();
966 #endif
967 #endif
968
969 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
970 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout );
971
972 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
973
974 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
975 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
976
977 int iAngle = int( angle * RAD2DEG );
978 if ( abs(iAngle) > 0 )
979 {
980 Fixed atsuAngle = IntToFixed( iAngle );
981 ATSUAttributeTag atsuTags[] =
982 {
983 kATSULineRotationTag ,
984 };
985 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
986 {
987 sizeof( Fixed ) ,
988 };
989 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
990 {
991 &atsuAngle ,
992 };
993 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
994 atsuTags, atsuSizes, atsuValues );
995 }
996
997 {
998 ATSUAttributeTag atsuTags[] =
999 {
1000 kATSUCGContextTag ,
1001 };
1002 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1003 {
1004 sizeof( CGContextRef ) ,
1005 };
1006 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1007 {
1008 &m_cgContext ,
1009 };
1010 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1011 atsuTags, atsuSizes, atsuValues );
1012 }
1013
1014 ATSUTextMeasurement textBefore, textAfter;
1015 ATSUTextMeasurement ascent, descent;
1016
1017 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1018 &textBefore , &textAfter, &ascent , &descent );
1019
1020 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1021
1022 Rect rect;
1023 /*
1024 // TODO
1025 if ( m_backgroundMode == wxSOLID )
1026 {
1027 wxGraphicsPath* path = m_graphicContext->CreatePath();
1028 path->MoveToPoint( drawX , drawY );
1029 path->AddLineToPoint(
1030 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1031 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) );
1032 path->AddLineToPoint(
1033 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1034 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1035 path->AddLineToPoint(
1036 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1037 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1038
1039 m_graphicContext->FillPath( path , m_textBackgroundColour );
1040 delete path;
1041 }
1042 */
1043 x += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent));
1044 y += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent));
1045
1046 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1047 IntToFixed(x) , IntToFixed(y) , &rect );
1048 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1049
1050 CGContextSaveGState(m_cgContext);
1051 CGContextTranslateCTM(m_cgContext, x, y);
1052 CGContextScaleCTM(m_cgContext, 1, -1);
1053 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1054 IntToFixed(0) , IntToFixed(0) );
1055
1056 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
1057
1058 CGContextRestoreGState(m_cgContext);
1059
1060 ::ATSUDisposeTextLayout(atsuLayout);
1061
1062 #if SIZEOF_WCHAR_T == 4
1063 free( ubuf );
1064 #endif
1065 }
1066
1067 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1068 wxDouble *descent, wxDouble *externalLeading ) const
1069 {
1070 wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1071
1072 OSStatus status = noErr;
1073
1074 ATSUTextLayout atsuLayout;
1075 UniCharCount chars = str.length();
1076 UniChar* ubuf = NULL;
1077
1078 #if SIZEOF_WCHAR_T == 4
1079 wxMBConvUTF16 converter;
1080 #if wxUSE_UNICODE
1081 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
1082 ubuf = (UniChar*) malloc( unicharlen + 2 );
1083 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
1084 #else
1085 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1086 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1087 ubuf = (UniChar*) malloc( unicharlen + 2 );
1088 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1089 #endif
1090 chars = unicharlen / 2;
1091 #else
1092 #if wxUSE_UNICODE
1093 ubuf = (UniChar*) str.wc_str();
1094 #else
1095 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1096 chars = wxWcslen( wchar.data() );
1097 ubuf = (UniChar*) wchar.data();
1098 #endif
1099 #endif
1100
1101 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1102 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout );
1103
1104 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
1105
1106 ATSUTextMeasurement textBefore, textAfter;
1107 ATSUTextMeasurement textAscent, textDescent;
1108
1109 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1110 &textBefore , &textAfter, &textAscent , &textDescent );
1111
1112 if ( height )
1113 *height = FixedToInt(textAscent + textDescent);
1114 if ( descent )
1115 *descent = FixedToInt(textDescent);
1116 if ( externalLeading )
1117 *externalLeading = 0;
1118 if ( width )
1119 *width = FixedToInt(textAfter - textBefore);
1120
1121 ::ATSUDisposeTextLayout(atsuLayout);
1122 }
1123
1124 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1125 {
1126 widths.Empty();
1127 widths.Add(0, text.length());
1128
1129 if (text.empty())
1130 return;
1131
1132 ATSUTextLayout atsuLayout;
1133 UniCharCount chars = text.length();
1134 UniChar* ubuf = NULL;
1135
1136 #if SIZEOF_WCHAR_T == 4
1137 wxMBConvUTF16 converter;
1138 #if wxUSE_UNICODE
1139 size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 );
1140 ubuf = (UniChar*) malloc( unicharlen + 2 );
1141 converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 );
1142 #else
1143 const wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1144 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1145 ubuf = (UniChar*) malloc( unicharlen + 2 );
1146 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1147 #endif
1148 chars = unicharlen / 2;
1149 #else
1150 #if wxUSE_UNICODE
1151 ubuf = (UniChar*) text.wc_str();
1152 #else
1153 wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1154 chars = wxWcslen( wchar.data() );
1155 ubuf = (UniChar*) wchar.data();
1156 #endif
1157 #endif
1158
1159 OSStatus status;
1160 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1161 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout );
1162
1163 for ( int pos = 0; pos < (int)chars; pos ++ )
1164 {
1165 unsigned long actualNumberOfBounds = 0;
1166 ATSTrapezoid glyphBounds;
1167
1168 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1169 OSStatus result;
1170 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
1171 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
1172 if (result != noErr || actualNumberOfBounds != 1 )
1173 return;
1174
1175 widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
1176 //unsigned char uch = s[i];
1177 }
1178
1179 ::ATSUDisposeTextLayout(atsuLayout);
1180 }
1181
1182 void wxMacCoreGraphicsContext::SetFont( const wxFont &font )
1183 {
1184 if ( m_macATSUIStyle )
1185 {
1186 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
1187 m_macATSUIStyle = NULL;
1188 }
1189
1190 if ( font.Ok() )
1191 {
1192 OSStatus status;
1193
1194 status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle );
1195
1196 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );
1197
1198 // we need the scale here ...
1199
1200 Fixed atsuSize = IntToFixed( int( /*m_scaleY*/ 1 * font.MacGetFontSize()) );
1201 RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColor.GetPixel() );
1202 ATSUAttributeTag atsuTags[] =
1203 {
1204 kATSUSizeTag ,
1205 kATSUColorTag ,
1206 };
1207 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1208 {
1209 sizeof( Fixed ) ,
1210 sizeof( RGBColor ) ,
1211 };
1212 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1213 {
1214 &atsuSize ,
1215 &atsuColor ,
1216 };
1217
1218 status = ::ATSUSetAttributes(
1219 (ATSUStyle)m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
1220 atsuTags, atsuSizes, atsuValues);
1221
1222 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
1223 }
1224 }
1225
1226 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC &dc )
1227 {
1228 return new wxMacCoreGraphicsContext((CGContextRef)dc.GetWindow()->MacGetCGContextRef() );
1229 }
1230
1231 #endif // wxMAC_USE_CORE_GRAPHICS