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