]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dccg.cpp
Include wx/memory.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / carbon / dccg.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/dc.h"
15
16 #if 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 #endif
24
25 #include "wx/mac/uma.h"
26 #include "wx/region.h"
27 #include "wx/image.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 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
42
43 #ifndef wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
44 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
45 #endif
46
47 //-----------------------------------------------------------------------------
48 // constants
49 //-----------------------------------------------------------------------------
50
51 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
52 #ifndef M_PI
53 const double M_PI = 3.14159265358979 ;
54 #endif
55 #endif
56
57 const double RAD2DEG = 180.0 / M_PI;
58 const short kEmulatedMode = -1 ;
59 const short kUnsupportedMode = -2 ;
60
61 extern TECObjectRef s_TECNativeCToUnicode ;
62
63
64 // TODO: update
65 // The textctrl implementation still needs that (needs what?) for the non-HIView implementation
66 //
67 wxMacWindowClipper::wxMacWindowClipper( const wxWindow* win ) :
68 wxMacPortSaver( (GrafPtr) GetWindowPort( (WindowRef) win->MacGetTopLevelWindowRef() ) )
69 {
70 m_newPort = (GrafPtr) GetWindowPort( (WindowRef) win->MacGetTopLevelWindowRef() ) ;
71 m_formerClip = NewRgn() ;
72 m_newClip = NewRgn() ;
73 GetClip( m_formerClip ) ;
74
75 if ( win )
76 {
77 // guard against half constructed objects, this just leads to a empty clip
78 if ( win->GetPeer() )
79 {
80 int x = 0 , y = 0;
81 win->MacWindowToRootWindow( &x, &y ) ;
82
83 // get area including focus rect
84 CopyRgn( (RgnHandle) ((wxWindow*)win)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip ) ;
85 if ( !EmptyRgn( m_newClip ) )
86 OffsetRgn( m_newClip , x , y ) ;
87 }
88
89 SetClip( m_newClip ) ;
90 }
91 }
92
93 wxMacWindowClipper::~wxMacWindowClipper()
94 {
95 SetPort( m_newPort ) ;
96 SetClip( m_formerClip ) ;
97 DisposeRgn( m_newClip ) ;
98 DisposeRgn( m_formerClip ) ;
99 }
100
101 wxMacWindowStateSaver::wxMacWindowStateSaver( const wxWindow* win ) :
102 wxMacWindowClipper( win )
103 {
104 // the port is already set at this point
105 m_newPort = (GrafPtr) GetWindowPort( (WindowRef) win->MacGetTopLevelWindowRef() ) ;
106 GetThemeDrawingState( &m_themeDrawingState ) ;
107 }
108
109 wxMacWindowStateSaver::~wxMacWindowStateSaver()
110 {
111 SetPort( m_newPort ) ;
112 SetThemeDrawingState( m_themeDrawingState , true ) ;
113 }
114
115 // minimal implementation only used for appearance drawing < 10.3
116
117 wxMacPortSetter::wxMacPortSetter( const wxDC* dc ) :
118 m_ph( (GrafPtr) dc->m_macPort )
119 {
120 wxASSERT( dc->Ok() ) ;
121 m_dc = dc ;
122
123 // dc->MacSetupPort(&m_ph) ;
124 }
125
126 wxMacPortSetter::~wxMacPortSetter()
127 {
128 // m_dc->MacCleanupPort(&m_ph) ;
129 }
130
131 //-----------------------------------------------------------------------------
132 // Local functions
133 //-----------------------------------------------------------------------------
134
135 static inline double dmin(double a, double b) { return a < b ? a : b; }
136 static inline double dmax(double a, double b) { return a > b ? a : b; }
137 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
138
139 //-----------------------------------------------------------------------------
140 // device context implementation
141 //
142 // more and more of the dc functionality should be implemented by calling
143 // the appropricate wxMacCGContext, but we will have to do that step by step
144 // also coordinate conversions should be moved to native matrix ops
145 //-----------------------------------------------------------------------------
146
147 // we always stock two context states, one at entry, to be able to preserve the
148 // state we were called with, the other one after changing to HI Graphics orientation
149 // (this one is used for getting back clippings etc)
150
151 wxMacCGPath::wxMacCGPath()
152 {
153 m_path = CGPathCreateMutable() ;
154 }
155
156 wxMacCGPath::~wxMacCGPath()
157 {
158 CGPathRelease( m_path ) ;
159 }
160
161 // opens (starts) a new subpath
162 void wxMacCGPath::MoveToPoint( wxCoord x1 , wxCoord y1 )
163 {
164 CGPathMoveToPoint( m_path , NULL , x1 , y1 ) ;
165 }
166
167 void wxMacCGPath::AddLineToPoint( wxCoord x1 , wxCoord y1 )
168 {
169 CGPathAddLineToPoint( m_path , NULL , x1 , y1 ) ;
170 }
171
172 void wxMacCGPath::AddQuadCurveToPoint( wxCoord cx1, wxCoord cy1, wxCoord x1, wxCoord y1 )
173 {
174 CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x1 , y1 );
175 }
176
177 void wxMacCGPath::AddRectangle( wxCoord x, wxCoord y, wxCoord w, wxCoord h )
178 {
179 CGRect cgRect = { { x , y } , { w , h } } ;
180 CGPathAddRect( m_path , NULL , cgRect ) ;
181 }
182
183 void wxMacCGPath::AddCircle( wxCoord x, wxCoord y , wxCoord r )
184 {
185 CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true ) ;
186 }
187
188 // closes the current subpath
189 void wxMacCGPath::CloseSubpath()
190 {
191 CGPathCloseSubpath( m_path ) ;
192 }
193
194 CGPathRef wxMacCGPath::GetPath() const
195 {
196 return m_path ;
197 }
198
199 wxMacCGContext::wxMacCGContext( CGrafPtr port )
200 {
201 m_qdPort = port ;
202 m_cgContext = NULL ;
203 }
204
205 wxMacCGContext::wxMacCGContext( CGContextRef cgcontext )
206 {
207 m_qdPort = NULL ;
208 m_cgContext = cgcontext ;
209 CGContextSaveGState( m_cgContext ) ;
210 CGContextSaveGState( m_cgContext ) ;
211 }
212
213 wxMacCGContext::wxMacCGContext()
214 {
215 m_qdPort = NULL ;
216 m_cgContext = NULL ;
217 }
218
219 wxMacCGContext::~wxMacCGContext()
220 {
221 if ( m_cgContext )
222 {
223 CGContextSynchronize( m_cgContext ) ;
224 CGContextRestoreGState( m_cgContext ) ;
225 CGContextRestoreGState( m_cgContext ) ;
226 }
227
228 if ( m_qdPort )
229 CGContextRelease( m_cgContext ) ;
230 }
231
232
233 void wxMacCGContext::Clip( const wxRegion &region )
234 {
235 // ClipCGContextToRegion ( m_cgContext, &bounds , (RgnHandle) dc->m_macCurrentClipRgn ) ;
236 }
237
238 void wxMacCGContext::StrokePath( const wxGraphicPath *p )
239 {
240 const wxMacCGPath* path = dynamic_cast< const wxMacCGPath*>( p ) ;
241 CGContextAddPath( m_cgContext , path->GetPath() ) ;
242 CGContextStrokePath( m_cgContext ) ;
243 }
244
245 void wxMacCGContext::DrawPath( const wxGraphicPath *p , int fillStyle )
246 {
247 const wxMacCGPath* path = dynamic_cast< const wxMacCGPath*>( p ) ;
248 CGPathDrawingMode mode = m_mode ;
249
250 if ( fillStyle == wxODDEVEN_RULE )
251 {
252 if ( mode == kCGPathFill )
253 mode = kCGPathEOFill ;
254 else if ( mode == kCGPathFillStroke )
255 mode = kCGPathEOFillStroke ;
256 }
257
258 CGContextAddPath( m_cgContext , path->GetPath() ) ;
259 CGContextDrawPath( m_cgContext , mode ) ;
260 }
261
262 void wxMacCGContext::FillPath( const wxGraphicPath *p , const wxColor &fillColor , int fillStyle )
263 {
264 const wxMacCGPath* path = dynamic_cast< const wxMacCGPath*>( p ) ;
265 CGContextSaveGState( m_cgContext ) ;
266
267 RGBColor col = MAC_WXCOLORREF( fillColor.GetPixel() ) ;
268 CGContextSetRGBFillColor( m_cgContext , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
269 CGPathDrawingMode mode = kCGPathFill ;
270
271 if ( fillStyle == wxODDEVEN_RULE )
272 mode = kCGPathEOFill ;
273
274 CGContextBeginPath( m_cgContext ) ;
275 CGContextAddPath( m_cgContext , path->GetPath() ) ;
276 CGContextClosePath( m_cgContext ) ;
277 CGContextDrawPath( m_cgContext , mode ) ;
278
279 CGContextRestoreGState( m_cgContext ) ;
280 }
281
282 wxGraphicPath* wxMacCGContext::CreatePath()
283 {
284 // make sure that we now have a real cgref, before doing
285 // anything with paths
286 CGContextRef cg = GetNativeContext() ;
287 cg = NULL ;
288
289 return new wxMacCGPath() ;
290 }
291
292 // in case we only got a QDPort only create a cgref now
293
294 CGContextRef wxMacCGContext::GetNativeContext()
295 {
296 if ( m_cgContext == NULL )
297 {
298 Rect bounds ;
299 GetPortBounds( (CGrafPtr) m_qdPort , &bounds ) ;
300 OSStatus status = CreateCGContextForPort((CGrafPtr) m_qdPort , &m_cgContext) ;
301 CGContextSaveGState( m_cgContext ) ;
302
303 wxASSERT_MSG( status == noErr , wxT("Cannot nest wxDCs on the same window") ) ;
304
305 CGContextTranslateCTM( m_cgContext , 0 , bounds.bottom - bounds.top ) ;
306 CGContextScaleCTM( m_cgContext , 1 , -1 ) ;
307
308 CGContextSaveGState( m_cgContext ) ;
309 SetPen( m_pen ) ;
310 SetBrush( m_brush ) ;
311 }
312
313 return m_cgContext ;
314 }
315
316 void wxMacCGContext::SetNativeContext( CGContextRef cg )
317 {
318 // we allow either setting or clearing but not replacing
319 wxASSERT( m_cgContext == NULL || cg == NULL ) ;
320
321 if ( cg )
322 CGContextSaveGState( cg ) ;
323 m_cgContext = cg ;
324 }
325
326 #pragma mark -
327 #pragma mark wxMacCGPattern, ImagePattern, HatchPattern classes
328
329 // CGPattern wrapper class: always allocate on heap, never call destructor
330
331 class wxMacCGPattern
332 {
333 public :
334 wxMacCGPattern() {}
335
336 // is guaranteed to be called only with a non-Null CGContextRef
337 virtual void Render( CGContextRef ctxRef ) = 0 ;
338
339 operator CGPatternRef() const { return m_patternRef ; }
340
341 protected :
342 virtual ~wxMacCGPattern()
343 {
344 // as this is called only when the m_patternRef is been released;
345 // don't release it again
346 }
347
348 static void _Render( void *info, CGContextRef ctxRef )
349 {
350 wxMacCGPattern* self = (wxMacCGPattern*) info ;
351 if ( self && ctxRef )
352 self->Render( ctxRef ) ;
353 }
354
355 static void _Dispose( void *info )
356 {
357 wxMacCGPattern* self = (wxMacCGPattern*) info ;
358 delete self ;
359 }
360
361 CGPatternRef m_patternRef ;
362
363 static const CGPatternCallbacks ms_Callbacks ;
364 } ;
365
366 const CGPatternCallbacks wxMacCGPattern::ms_Callbacks = { 0, &wxMacCGPattern::_Render, &wxMacCGPattern::_Dispose };
367
368 class ImagePattern : public wxMacCGPattern
369 {
370 public :
371 ImagePattern( const wxBitmap* bmp , CGAffineTransform transform )
372 {
373 wxASSERT( bmp && bmp->Ok() ) ;
374
375 Init( (CGImageRef) bmp->CGImageCreate() , transform ) ;
376 }
377
378 // ImagePattern takes ownership of CGImageRef passed in
379 ImagePattern( CGImageRef image , CGAffineTransform transform )
380 {
381 if ( image )
382 CFRetain( image ) ;
383
384 Init( image , transform ) ;
385 }
386
387 virtual void Render( CGContextRef ctxRef )
388 {
389 if (m_image != NULL)
390 HIViewDrawCGImage( ctxRef, &m_imageBounds, m_image );
391 }
392
393 protected :
394 void Init( CGImageRef image, CGAffineTransform transform )
395 {
396 m_image = image ;
397 if ( m_image )
398 {
399 m_imageBounds = CGRectMake( 0.0, 0.0, (float)CGImageGetWidth( m_image ), (float)CGImageGetHeight( m_image ) ) ;
400 m_patternRef = CGPatternCreate(
401 this , m_imageBounds, transform ,
402 m_imageBounds.size.width, m_imageBounds.size.height,
403 kCGPatternTilingNoDistortion, true , &wxMacCGPattern::ms_Callbacks ) ;
404 }
405 }
406
407 ~ImagePattern()
408 {
409 if ( m_image )
410 CGImageRelease( m_image ) ;
411 }
412
413 CGImageRef m_image ;
414 CGRect m_imageBounds ;
415 } ;
416
417 class HatchPattern : public wxMacCGPattern
418 {
419 public :
420 HatchPattern( int hatchstyle, CGAffineTransform transform )
421 {
422 m_hatch = hatchstyle ;
423 m_imageBounds = CGRectMake( 0.0, 0.0, 8.0 , 8.0 ) ;
424 m_patternRef = CGPatternCreate(
425 this , m_imageBounds, transform ,
426 m_imageBounds.size.width, m_imageBounds.size.height,
427 kCGPatternTilingNoDistortion, false , &wxMacCGPattern::ms_Callbacks ) ;
428 }
429
430 void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
431 {
432 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
433 if ( UMAGetSystemVersion() >= 0x1040 )
434 {
435 CGContextStrokeLineSegments( ctxRef , pts , count ) ;
436 }
437 else
438 #endif
439 {
440 CGContextBeginPath( ctxRef );
441 for (size_t i = 0; i < count; i += 2)
442 {
443 CGContextMoveToPoint(ctxRef, pts[i].x, pts[i].y);
444 CGContextAddLineToPoint(ctxRef, pts[i+1].x, pts[i+1].y);
445 }
446 CGContextStrokePath(ctxRef);
447 }
448 }
449
450 virtual void Render( CGContextRef ctxRef )
451 {
452 switch ( m_hatch )
453 {
454 case wxBDIAGONAL_HATCH :
455 {
456 CGPoint pts[] =
457 {
458 { 8.0 , 0.0 } , { 0.0 , 8.0 }
459 };
460 StrokeLineSegments( ctxRef , pts , 2 ) ;
461 }
462 break ;
463
464 case wxCROSSDIAG_HATCH :
465 {
466 CGPoint pts[] =
467 {
468 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
469 { 8.0 , 0.0 } , { 0.0 , 8.0 }
470 };
471 StrokeLineSegments( ctxRef , pts , 4 ) ;
472 }
473 break ;
474
475 case wxFDIAGONAL_HATCH :
476 {
477 CGPoint pts[] =
478 {
479 { 0.0 , 0.0 } , { 8.0 , 8.0 }
480 };
481 StrokeLineSegments( ctxRef , pts , 2 ) ;
482 }
483 break ;
484
485 case wxCROSS_HATCH :
486 {
487 CGPoint pts[] =
488 {
489 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
490 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
491 };
492 StrokeLineSegments( ctxRef , pts , 4 ) ;
493 }
494 break ;
495
496 case wxHORIZONTAL_HATCH :
497 {
498 CGPoint pts[] =
499 {
500 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
501 };
502 StrokeLineSegments( ctxRef , pts , 2 ) ;
503 }
504 break ;
505
506 case wxVERTICAL_HATCH :
507 {
508 CGPoint pts[] =
509 {
510 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
511 };
512 StrokeLineSegments( ctxRef , pts , 2 ) ;
513 }
514 break ;
515
516 default:
517 break;
518 }
519 }
520
521 protected :
522 ~HatchPattern() {}
523
524 CGRect m_imageBounds ;
525 int m_hatch ;
526 };
527
528 #pragma mark -
529
530 void wxMacCGContext::SetPen( const wxPen &pen )
531 {
532 m_pen = pen ;
533 if ( m_cgContext == NULL )
534 return ;
535
536 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
537 bool stroke = pen.GetStyle() != wxTRANSPARENT ;
538
539 #if 0
540 // we can benchmark performance; should go into a setting eventually
541 CGContextSetShouldAntialias( m_cgContext , false ) ;
542 #endif
543
544 if ( fill | stroke )
545 {
546 // set up brushes
547 m_mode = kCGPathFill ; // just a default
548
549 if ( stroke )
550 {
551 RGBColor col = MAC_WXCOLORREF( pen.GetColour().GetPixel() ) ;
552 CGContextSetRGBStrokeColor( m_cgContext , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
553
554 // TODO: * m_dc->m_scaleX
555 float penWidth = pen.GetWidth();
556 if (penWidth <= 0.0)
557 penWidth = 0.1;
558 CGContextSetLineWidth( m_cgContext , penWidth ) ;
559
560 CGLineCap cap ;
561 switch ( pen.GetCap() )
562 {
563 case wxCAP_ROUND :
564 cap = kCGLineCapRound ;
565 break ;
566
567 case wxCAP_PROJECTING :
568 cap = kCGLineCapSquare ;
569 break ;
570
571 case wxCAP_BUTT :
572 cap = kCGLineCapButt ;
573 break ;
574
575 default :
576 cap = kCGLineCapButt ;
577 break ;
578 }
579
580 CGLineJoin join ;
581 switch ( pen.GetJoin() )
582 {
583 case wxJOIN_BEVEL :
584 join = kCGLineJoinBevel ;
585 break ;
586
587 case wxJOIN_MITER :
588 join = kCGLineJoinMiter ;
589 break ;
590
591 case wxJOIN_ROUND :
592 join = kCGLineJoinRound ;
593 break ;
594
595 default :
596 join = kCGLineJoinMiter ;
597 break;
598 }
599
600 m_mode = kCGPathStroke ;
601 int count = 0 ;
602
603 const float *lengths = NULL ;
604 float *userLengths = NULL ;
605
606 const float dashUnit = penWidth < 1.0 ? 1.0 : penWidth;
607
608 const float dotted[] = { dashUnit , dashUnit + 2.0 };
609 const float short_dashed[] = { 9.0 , 6.0 };
610 const float dashed[] = { 19.0 , 9.0 };
611 const float dotted_dashed[] = { 9.0 , 6.0 , 3.0 , 3.0 };
612
613 switch ( pen.GetStyle() )
614 {
615 case wxSOLID :
616 break ;
617
618 case wxDOT :
619 lengths = dotted ;
620 count = WXSIZEOF(dotted);
621 break ;
622
623 case wxLONG_DASH :
624 lengths = dashed ;
625 count = WXSIZEOF(dashed) ;
626 break ;
627
628 case wxSHORT_DASH :
629 lengths = short_dashed ;
630 count = WXSIZEOF(short_dashed) ;
631 break ;
632
633 case wxDOT_DASH :
634 lengths = dotted_dashed ;
635 count = WXSIZEOF(dotted_dashed);
636 break ;
637
638 case wxUSER_DASH :
639 wxDash *dashes ;
640 count = pen.GetDashes( &dashes ) ;
641 if ((dashes != NULL) && (count > 0))
642 {
643 userLengths = new float[count] ;
644 for ( int i = 0 ; i < count ; ++i )
645 {
646 userLengths[i] = dashes[i] * dashUnit ;
647
648 if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 )
649 userLengths[i] = dashUnit + 2.0 ;
650 else if ( i % 2 == 0 && userLengths[i] < dashUnit )
651 userLengths[i] = dashUnit ;
652 }
653 }
654 lengths = userLengths ;
655 break ;
656
657 case wxSTIPPLE :
658 {
659 float alphaArray[1] = { 1.0 } ;
660 wxBitmap* bmp = pen.GetStipple() ;
661 if ( bmp && bmp->Ok() )
662 {
663 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ) ;
664 CGContextSetStrokeColorSpace( m_cgContext , patternSpace ) ;
665 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
666 CGContextSetStrokePattern( m_cgContext, pattern , alphaArray ) ;
667 }
668 }
669 break ;
670
671 default :
672 {
673 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ) ;
674 CGContextSetStrokeColorSpace( m_cgContext , patternSpace ) ;
675 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( pen.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
676
677 RGBColor col = MAC_WXCOLORREF( pen.GetColour().GetPixel() ) ;
678 float colorArray[4] = { col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 } ;
679
680 CGContextSetStrokePattern( m_cgContext, pattern , colorArray ) ;
681 }
682 break ;
683 }
684
685 if ((lengths != NULL) && (count > 0))
686 {
687 CGContextSetLineDash( m_cgContext , 0 , lengths , count ) ;
688 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
689 cap = kCGLineCapButt ;
690 }
691 else
692 {
693 CGContextSetLineDash( m_cgContext , 0 , NULL , 0 ) ;
694 }
695
696 CGContextSetLineCap( m_cgContext , cap ) ;
697 CGContextSetLineJoin( m_cgContext , join ) ;
698
699 delete[] userLengths ;
700 }
701
702 if ( fill && stroke )
703 m_mode = kCGPathFillStroke ;
704 }
705 }
706
707 void wxMacCGContext::SetBrush( const wxBrush &brush )
708 {
709 m_brush = brush ;
710 if ( m_cgContext == NULL )
711 return ;
712
713 bool fill = brush.GetStyle() != wxTRANSPARENT ;
714 bool stroke = m_pen.GetStyle() != wxTRANSPARENT ;
715
716 #if 0
717 // we can benchmark performance, should go into a setting later
718 CGContextSetShouldAntialias( m_cgContext , false ) ;
719 #endif
720
721 if ( fill | stroke )
722 {
723 // setup brushes
724 m_mode = kCGPathFill ; // just a default
725
726 if ( fill )
727 {
728 if ( brush.GetStyle() == wxSOLID )
729 {
730 RGBColor col = MAC_WXCOLORREF( brush.GetColour().GetPixel() ) ;
731 CGContextSetRGBFillColor( m_cgContext , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
732 }
733 else if ( brush.IsHatch() )
734 {
735 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ) ;
736 CGContextSetFillColorSpace( m_cgContext , patternSpace ) ;
737 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( brush.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
738
739 RGBColor col = MAC_WXCOLORREF( brush.GetColour().GetPixel() ) ;
740 float colorArray[4] = { col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 } ;
741
742 CGContextSetFillPattern( m_cgContext, pattern , colorArray ) ;
743 }
744 else
745 {
746 // now brush is a bitmap
747 float alphaArray[1] = { 1.0 } ;
748 wxBitmap* bmp = brush.GetStipple() ;
749 if ( bmp && bmp->Ok() )
750 {
751 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ) ;
752 CGContextSetFillColorSpace( m_cgContext , patternSpace ) ;
753 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
754 CGContextSetFillPattern( m_cgContext, pattern , alphaArray ) ;
755 }
756 }
757
758 m_mode = kCGPathFill ;
759 }
760
761 if ( fill && stroke )
762 m_mode = kCGPathFillStroke ;
763 else if ( stroke )
764 m_mode = kCGPathStroke ;
765 }
766 }
767
768 void AddEllipticArcToPath(CGContextRef c, CGPoint center, float a, float b, float fromDegree , float toDegree )
769 {
770 CGContextSaveGState(c);
771 CGContextTranslateCTM(c, center.x, center.y);
772 CGContextScaleCTM(c, a, b);
773 CGContextMoveToPoint(c, 1, 0);
774 CGContextAddArc(c, 0, 0, 1, DegToRad(fromDegree), DegToRad(toDegree), 0);
775 CGContextClosePath(c);
776 CGContextRestoreGState(c);
777 }
778
779 void AddRoundedRectToPath(CGContextRef c, CGRect rect, float ovalWidth,
780 float ovalHeight)
781 {
782 float fw, fh;
783 if (ovalWidth == 0 || ovalHeight == 0)
784 {
785 CGContextAddRect(c, rect);
786 return;
787 }
788
789 CGContextSaveGState(c);
790 CGContextTranslateCTM(c, CGRectGetMinX(rect), CGRectGetMinY(rect));
791 CGContextScaleCTM(c, ovalWidth, ovalHeight);
792
793 fw = CGRectGetWidth(rect) / ovalWidth;
794 fh = CGRectGetHeight(rect) / ovalHeight;
795
796 CGContextMoveToPoint(c, fw, fh / 2);
797 CGContextAddArcToPoint(c, fw, fh, fw / 2, fh, 1);
798 CGContextAddArcToPoint(c, 0, fh, 0, fh / 2, 1);
799 CGContextAddArcToPoint(c, 0, 0, fw / 2, 0, 1);
800 CGContextAddArcToPoint(c, fw, 0, fw, fh / 2, 1);
801 CGContextClosePath(c);
802 CGContextRestoreGState(c);
803 }
804
805 #pragma mark -
806
807 wxDC::wxDC()
808 {
809 m_ok = false ;
810 m_colour = true;
811 m_mm_to_pix_x = mm2pt;
812 m_mm_to_pix_y = mm2pt;
813
814 m_externalDeviceOriginX = 0;
815 m_externalDeviceOriginY = 0;
816 m_logicalScaleX = 1.0;
817 m_logicalScaleY = 1.0;
818 m_userScaleX = 1.0;
819 m_userScaleY = 1.0;
820 m_scaleX = 1.0;
821 m_scaleY = 1.0;
822 m_needComputeScaleX =
823 m_needComputeScaleY = false;
824
825 m_macPort = 0 ;
826 m_macLocalOrigin.x =
827 m_macLocalOrigin.y = 0 ;
828
829 m_pen = *wxBLACK_PEN;
830 m_font = *wxNORMAL_FONT;
831 m_brush = *wxWHITE_BRUSH;
832
833 m_macATSUIStyle = NULL ;
834 m_graphicContext = NULL ;
835 }
836
837 wxDC::~wxDC()
838 {
839 if ( m_macATSUIStyle )
840 {
841 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
842 m_macATSUIStyle = NULL ;
843 }
844
845 delete m_graphicContext ;
846 }
847
848 void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
849 {
850 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid DC") );
851 wxCHECK_RET( bmp.Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid bitmap") );
852
853 wxCoord xx = XLOG2DEVMAC(x);
854 wxCoord yy = YLOG2DEVMAC(y);
855 wxCoord w = bmp.GetWidth();
856 wxCoord h = bmp.GetHeight();
857 wxCoord ww = XLOG2DEVREL(w);
858 wxCoord hh = YLOG2DEVREL(h);
859
860 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
861 CGImageRef image = (CGImageRef)( bmp.CGImageCreate() ) ;
862 HIRect r = CGRectMake( xx , yy , ww , hh ) ;
863 HIViewDrawCGImage( cg , &r , image ) ;
864 CGImageRelease( image ) ;
865 }
866
867 void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
868 {
869 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid DC") );
870 wxCHECK_RET( icon.Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid icon") );
871
872 wxCoord xx = XLOG2DEVMAC(x);
873 wxCoord yy = YLOG2DEVMAC(y);
874 wxCoord w = icon.GetWidth();
875 wxCoord h = icon.GetHeight();
876 wxCoord ww = XLOG2DEVREL(w);
877 wxCoord hh = YLOG2DEVREL(h);
878
879 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
880 CGRect r = CGRectMake( 00 , 00 , ww , hh ) ;
881 CGContextSaveGState( cg );
882 CGContextTranslateCTM( cg, xx , yy + hh );
883 CGContextScaleCTM( cg, 1, -1 );
884 PlotIconRefInContext( cg , &r , kAlignNone , kTransformNone ,
885 NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) ) ;
886 CGContextRestoreGState( cg ) ;
887 }
888
889 void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
890 {
891 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoSetClippingRegion - invalid DC") );
892
893 wxCoord xx, yy, ww, hh;
894 xx = XLOG2DEVMAC(x);
895 yy = YLOG2DEVMAC(y);
896 ww = XLOG2DEVREL(width);
897 hh = YLOG2DEVREL(height);
898
899 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
900 CGRect clipRect = CGRectMake( xx , yy , ww, hh ) ;
901 CGContextClipToRect( cgContext , clipRect ) ;
902
903 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
904 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
905
906 if ( m_clipping )
907 {
908 m_clipX1 = wxMax( m_clipX1, xx );
909 m_clipY1 = wxMax( m_clipY1, yy );
910 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
911 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
912 }
913 else
914 {
915 m_clipping = true;
916
917 m_clipX1 = xx;
918 m_clipY1 = yy;
919 m_clipX2 = xx + ww;
920 m_clipY2 = yy + hh;
921 }
922
923 // TODO: as soon as we don't reset the context for each operation anymore
924 // we have to update the context as well
925 }
926
927 void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
928 {
929 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
930
931 if (region.Empty())
932 {
933 DestroyClippingRegion();
934 return;
935 }
936
937 wxCoord x, y, w, h;
938 region.GetBox( x, y, w, h );
939 wxCoord xx, yy, ww, hh;
940 xx = XLOG2DEVMAC(x);
941 yy = YLOG2DEVMAC(y);
942 ww = XLOG2DEVREL(w);
943 hh = YLOG2DEVREL(h);
944
945 // if we have a scaling that we cannot map onto native regions
946 // we must use the box
947 if ( ww != w || hh != h )
948 {
949 wxDC::DoSetClippingRegion( x, y, w, h );
950 }
951 else
952 {
953 #if 0
954 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
955 if ( xx != x || yy != y )
956 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
957 SectRgn( (RgnHandle)m_macCurrentClipRgn , (RgnHandle)m_macBoundaryClipRgn , (RgnHandle)m_macCurrentClipRgn ) ;
958 #endif
959
960 if ( m_clipping )
961 {
962 m_clipX1 = wxMax( m_clipX1, xx );
963 m_clipY1 = wxMax( m_clipY1, yy );
964 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
965 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
966 }
967 else
968 {
969 m_clipping = true;
970
971 m_clipX1 = xx;
972 m_clipY1 = yy;
973 m_clipX2 = xx + ww;
974 m_clipY2 = yy + hh;
975 }
976 }
977 }
978
979 void wxDC::DestroyClippingRegion()
980 {
981 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
982
983 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
984 CGContextRestoreGState( cgContext );
985 CGContextSaveGState( cgContext );
986
987 m_graphicContext->SetPen( m_pen ) ;
988 m_graphicContext->SetBrush( m_brush ) ;
989
990 m_clipping = false;
991 }
992
993 void wxDC::DoGetSizeMM( int* width, int* height ) const
994 {
995 int w = 0, h = 0;
996
997 GetSize( &w, &h );
998 if (width)
999 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
1000 if (height)
1001 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
1002 }
1003
1004 void wxDC::SetTextForeground( const wxColour &col )
1005 {
1006 wxCHECK_RET( Ok(), wxT("wxDC(cg)::SetTextForeground - invalid DC") );
1007
1008 if ( col != m_textForegroundColour )
1009 {
1010 m_textForegroundColour = col;
1011 MacInstallFont() ;
1012 }
1013 }
1014
1015 void wxDC::SetTextBackground( const wxColour &col )
1016 {
1017 wxCHECK_RET( Ok(), wxT("wxDC(cg)::SetTextBackground - invalid DC") );
1018
1019 m_textBackgroundColour = col;
1020 }
1021
1022 void wxDC::SetMapMode( int mode )
1023 {
1024 switch (mode)
1025 {
1026 case wxMM_TWIPS:
1027 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
1028 break;
1029
1030 case wxMM_POINTS:
1031 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
1032 break;
1033
1034 case wxMM_METRIC:
1035 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
1036 break;
1037
1038 case wxMM_LOMETRIC:
1039 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
1040 break;
1041
1042 case wxMM_TEXT:
1043 default:
1044 SetLogicalScale( 1.0, 1.0 );
1045 break;
1046 }
1047
1048 if (mode != wxMM_TEXT)
1049 {
1050 m_needComputeScaleX =
1051 m_needComputeScaleY = true;
1052 }
1053 }
1054
1055 void wxDC::SetUserScale( double x, double y )
1056 {
1057 // allow negative ? -> no
1058 m_userScaleX = x;
1059 m_userScaleY = y;
1060 ComputeScaleAndOrigin();
1061 }
1062
1063 void wxDC::SetLogicalScale( double x, double y )
1064 {
1065 // allow negative ?
1066 m_logicalScaleX = x;
1067 m_logicalScaleY = y;
1068 ComputeScaleAndOrigin();
1069 }
1070
1071 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
1072 {
1073 m_logicalOriginX = x * m_signX; // is this still correct ?
1074 m_logicalOriginY = y * m_signY;
1075 ComputeScaleAndOrigin();
1076 }
1077
1078 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1079 {
1080 m_externalDeviceOriginX = x;
1081 m_externalDeviceOriginY = y;
1082 ComputeScaleAndOrigin();
1083 }
1084
1085 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1086 {
1087 m_signX = (xLeftRight ? 1 : -1);
1088 m_signY = (yBottomUp ? -1 : 1);
1089 ComputeScaleAndOrigin();
1090 }
1091
1092 wxSize wxDC::GetPPI() const
1093 {
1094 return wxSize(72, 72);
1095 }
1096
1097 int wxDC::GetDepth() const
1098 {
1099 return 32;
1100 }
1101
1102 void wxDC::ComputeScaleAndOrigin()
1103 {
1104 // CMB: copy scale to see if it changes
1105 double origScaleX = m_scaleX;
1106 double origScaleY = m_scaleY;
1107 m_scaleX = m_logicalScaleX * m_userScaleX;
1108 m_scaleY = m_logicalScaleY * m_userScaleY;
1109 m_deviceOriginX = m_externalDeviceOriginX;
1110 m_deviceOriginY = m_externalDeviceOriginY;
1111
1112 // CMB: if scale has changed call SetPen to recalulate the line width
1113 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
1114 {
1115 // this is a bit artificial, but we need to force wxDC to think
1116 // the pen has changed
1117 wxPen pen( GetPen() );
1118
1119 m_pen = wxNullPen;
1120 SetPen( pen );
1121 }
1122 }
1123
1124 void wxDC::SetPalette( const wxPalette& palette )
1125 {
1126 }
1127
1128 void wxDC::SetBackgroundMode( int mode )
1129 {
1130 m_backgroundMode = mode ;
1131 }
1132
1133 void wxDC::SetFont( const wxFont &font )
1134 {
1135 m_font = font;
1136 MacInstallFont() ;
1137 }
1138
1139 void wxDC::SetPen( const wxPen &pen )
1140 {
1141 if ( m_pen == pen )
1142 return ;
1143
1144 m_pen = pen;
1145 if ( m_graphicContext )
1146 {
1147 if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT )
1148 {
1149 m_graphicContext->SetPen( m_pen ) ;
1150 }
1151 else
1152 {
1153 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
1154 // eg when using a wxScrollWindow and scrolling around
1155 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1156 int origX = XLOG2DEVMAC( 0 ) ;
1157 int origY = YLOG2DEVMAC( 0 ) ;
1158 CGContextTranslateCTM( cgContext, origX, origY );
1159 m_graphicContext->SetPen( m_pen ) ;
1160 CGContextTranslateCTM( cgContext, -origX, -origY );
1161 }
1162 }
1163 }
1164
1165 void wxDC::SetBrush( const wxBrush &brush )
1166 {
1167 if (m_brush == brush)
1168 return;
1169
1170 m_brush = brush;
1171 if ( m_graphicContext )
1172 {
1173 if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT )
1174 {
1175 m_graphicContext->SetBrush( m_brush ) ;
1176 }
1177 else
1178 {
1179 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
1180 // eg when using a wxScrollWindow and scrolling around
1181 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1182 int origX = XLOG2DEVMAC(0) ;
1183 int origY = YLOG2DEVMAC(0) ;
1184 CGContextTranslateCTM( cgContext, origX, origY );
1185 m_graphicContext->SetBrush( m_brush ) ;
1186 CGContextTranslateCTM( cgContext, -origX, -origY );
1187 }
1188 }
1189 }
1190
1191 void wxDC::SetBackground( const wxBrush &brush )
1192 {
1193 if (m_backgroundBrush == brush)
1194 return;
1195
1196 m_backgroundBrush = brush;
1197 if (!m_backgroundBrush.Ok())
1198 return;
1199 }
1200
1201 void wxDC::SetLogicalFunction( int function )
1202 {
1203 if (m_logicalFunction == function)
1204 return;
1205
1206 m_logicalFunction = function ;
1207 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1208 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1209 if ( m_logicalFunction == wxCOPY )
1210 CGContextSetBlendMode( cgContext, kCGBlendModeNormal ) ;
1211 else if ( m_logicalFunction == wxINVERT )
1212 CGContextSetBlendMode( cgContext, kCGBlendModeExclusion ) ;
1213 else
1214 CGContextSetBlendMode( cgContext, kCGBlendModeNormal ) ;
1215 #endif
1216 }
1217
1218 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
1219 const wxColour & col, int style);
1220
1221 bool wxDC::DoFloodFill(wxCoord x, wxCoord y,
1222 const wxColour& col, int style)
1223 {
1224 return wxDoFloodFill(this, x, y, col, style);
1225 }
1226
1227 bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
1228 {
1229 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPixel - invalid DC") );
1230
1231 wxMacPortSaver helper((CGrafPtr)m_macPort) ;
1232 RGBColor colour;
1233
1234 // NB: GetCPixel is a deprecated QD call, and a slow one at that
1235 GetCPixel(
1236 XLOG2DEVMAC(x) + m_macLocalOriginInPort.x - m_macLocalOrigin.x,
1237 YLOG2DEVMAC(y) + m_macLocalOriginInPort.y - m_macLocalOrigin.y, &colour );
1238
1239 // convert from Mac colour to wx
1240 col->Set( colour.red >> 8, colour.green >> 8, colour.blue >> 8 );
1241
1242 return true ;
1243 }
1244
1245 void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
1246 {
1247 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLine - invalid DC") );
1248
1249 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1250 if ( m_logicalFunction != wxCOPY )
1251 return ;
1252 #endif
1253
1254 wxCoord xx1 = XLOG2DEVMAC(x1) ;
1255 wxCoord yy1 = YLOG2DEVMAC(y1) ;
1256 wxCoord xx2 = XLOG2DEVMAC(x2) ;
1257 wxCoord yy2 = YLOG2DEVMAC(y2) ;
1258
1259 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1260 path->MoveToPoint( xx1, yy1 ) ;
1261 path->AddLineToPoint( xx2 , yy2 ) ;
1262 path->CloseSubpath() ;
1263 m_graphicContext->StrokePath( path ) ;
1264 delete path ;
1265
1266 CalcBoundingBox(x1, y1);
1267 CalcBoundingBox(x2, y2);
1268 }
1269
1270 void wxDC::DoCrossHair( wxCoord x, wxCoord y )
1271 {
1272 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoCrossHair - invalid DC") );
1273
1274 if ( m_logicalFunction != wxCOPY )
1275 return ;
1276
1277 int w = 0, h = 0;
1278
1279 GetSize( &w, &h );
1280 wxCoord xx = XLOG2DEVMAC(x);
1281 wxCoord yy = YLOG2DEVMAC(y);
1282
1283 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1284 path->MoveToPoint( XLOG2DEVMAC(0), yy ) ;
1285 path->AddLineToPoint( XLOG2DEVMAC(w), yy ) ;
1286 path->CloseSubpath() ;
1287 path->MoveToPoint( xx, YLOG2DEVMAC(0) ) ;
1288 path->AddLineToPoint( xx, YLOG2DEVMAC(h) ) ;
1289 path->CloseSubpath() ;
1290 m_graphicContext->StrokePath( path ) ;
1291 delete path ;
1292
1293 CalcBoundingBox(x, y);
1294 CalcBoundingBox(x + w, y + h);
1295 }
1296
1297 void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
1298 wxCoord x2, wxCoord y2,
1299 wxCoord xc, wxCoord yc )
1300 {
1301 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawArc - invalid DC") );
1302
1303 if ( m_logicalFunction != wxCOPY )
1304 return ;
1305
1306 wxCoord xx1 = XLOG2DEVMAC(x1);
1307 wxCoord yy1 = YLOG2DEVMAC(y1);
1308 wxCoord xx2 = XLOG2DEVMAC(x2);
1309 wxCoord yy2 = YLOG2DEVMAC(y2);
1310 wxCoord xxc = XLOG2DEVMAC(xc);
1311 wxCoord yyc = YLOG2DEVMAC(yc);
1312
1313 double dx = xx1 - xxc;
1314 double dy = yy1 - yyc;
1315 double radius = sqrt((double)(dx * dx + dy * dy));
1316 wxCoord rad = (wxCoord)radius;
1317 double sa, ea;
1318 if (xx1 == xx2 && yy1 == yy2)
1319 {
1320 sa = 0.0;
1321 ea = 360.0;
1322 }
1323 else if (radius == 0.0)
1324 {
1325 sa = ea = 0.0;
1326 }
1327 else
1328 {
1329 sa = (xx1 - xxc == 0) ?
1330 (yy1 - yyc < 0) ? 90.0 : -90.0 :
1331 -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG;
1332 ea = (xx2 - xxc == 0) ?
1333 (yy2 - yyc < 0) ? 90.0 : -90.0 :
1334 -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG;
1335 }
1336
1337 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
1338 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1339 CGContextRef ctx = mctx->GetNativeContext() ;
1340 CGContextSaveGState( ctx ) ;
1341 CGContextTranslateCTM( ctx, xxc , yyc );
1342 CGContextScaleCTM( ctx , 1 , -1 ) ;
1343 if ( fill )
1344 CGContextMoveToPoint( ctx , 0 , 0 ) ;
1345 CGContextAddArc( ctx, 0, 0 , rad , DegToRad(sa), DegToRad(ea), 0 );
1346 if ( fill )
1347 CGContextAddLineToPoint( ctx , 0 , 0 ) ;
1348 CGContextRestoreGState( ctx ) ;
1349 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
1350 }
1351
1352 void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
1353 double sa, double ea )
1354 {
1355 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipticArc - invalid DC") );
1356
1357 if ( m_logicalFunction != wxCOPY )
1358 return ;
1359
1360 wxCoord xx = XLOG2DEVMAC(x);
1361 wxCoord yy = YLOG2DEVMAC(y);
1362 wxCoord ww = m_signX * XLOG2DEVREL(w);
1363 wxCoord hh = m_signY * YLOG2DEVREL(h);
1364
1365 // handle -ve width and/or height
1366 if (ww < 0)
1367 {
1368 ww = -ww;
1369 xx = xx - ww;
1370 }
1371 if (hh < 0)
1372 {
1373 hh = -hh;
1374 yy = yy - hh;
1375 }
1376
1377 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
1378
1379 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1380 CGContextRef ctx = mctx->GetNativeContext() ;
1381
1382 CGContextSaveGState( ctx ) ;
1383 CGContextTranslateCTM( ctx, xx + ww / 2, yy + hh / 2 );
1384 CGContextScaleCTM( ctx , 1 * ww / 2 , -1 * hh / 2 ) ;
1385 if ( fill )
1386 CGContextMoveToPoint( ctx , 0 , 0 ) ;
1387 CGContextAddArc( ctx, 0, 0, 1, DegToRad( sa ), DegToRad( ea ), 0 );
1388 if ( fill )
1389 CGContextAddLineToPoint( ctx , 0 , 0 ) ;
1390 CGContextRestoreGState( ctx ) ;
1391 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
1392 }
1393
1394 void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
1395 {
1396 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPoint - invalid DC") );
1397
1398 DoDrawLine( x , y , x + 1 , y + 1 ) ;
1399 }
1400
1401 void wxDC::DoDrawLines(int n, wxPoint points[],
1402 wxCoord xoffset, wxCoord yoffset)
1403 {
1404 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLines - invalid DC") );
1405
1406 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1407 if ( m_logicalFunction != wxCOPY )
1408 return ;
1409 #endif
1410
1411 wxCoord x1, x2 , y1 , y2 ;
1412 x1 = XLOG2DEVMAC(points[0].x + xoffset);
1413 y1 = YLOG2DEVMAC(points[0].y + yoffset);
1414 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1415 path->MoveToPoint( x1 , y1 ) ;
1416 for (int i = 1; i < n; i++)
1417 {
1418 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1419 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1420
1421 path->AddLineToPoint( x2 , y2 ) ;
1422 }
1423
1424 m_graphicContext->StrokePath( path ) ;
1425 delete path ;
1426 }
1427
1428 #if wxUSE_SPLINES
1429 void wxDC::DoDrawSpline(wxList *points)
1430 {
1431 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawSpline - invalid DC") );
1432
1433 if ( m_logicalFunction != wxCOPY )
1434 return ;
1435
1436 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1437
1438 wxList::compatibility_iterator node = points->GetFirst();
1439 if (node == wxList::compatibility_iterator())
1440 // empty list
1441 return;
1442
1443 wxPoint *p = (wxPoint *)node->GetData();
1444
1445 wxCoord x1 = p->x;
1446 wxCoord y1 = p->y;
1447
1448 node = node->GetNext();
1449 p = (wxPoint *)node->GetData();
1450
1451 wxCoord x2 = p->x;
1452 wxCoord y2 = p->y;
1453 wxCoord cx1 = ( x1 + x2 ) / 2;
1454 wxCoord cy1 = ( y1 + y2 ) / 2;
1455
1456 path->MoveToPoint( XLOG2DEVMAC( x1 ) , XLOG2DEVMAC( y1 ) ) ;
1457 path->AddLineToPoint( XLOG2DEVMAC( cx1 ) , XLOG2DEVMAC( cy1 ) ) ;
1458
1459 #if !wxUSE_STL
1460 while ((node = node->GetNext()) != NULL)
1461 #else
1462 while ((node = node->GetNext()))
1463 #endif // !wxUSE_STL
1464 {
1465 p = (wxPoint *)node->GetData();
1466 x1 = x2;
1467 y1 = y2;
1468 x2 = p->x;
1469 y2 = p->y;
1470 wxCoord cx4 = (x1 + x2) / 2;
1471 wxCoord cy4 = (y1 + y2) / 2;
1472
1473 path->AddQuadCurveToPoint(
1474 XLOG2DEVMAC( x1 ) , XLOG2DEVMAC( y1 ) ,
1475 XLOG2DEVMAC( cx4 ) , XLOG2DEVMAC( cy4 ) ) ;
1476
1477 cx1 = cx4;
1478 cy1 = cy4;
1479 }
1480
1481 path->AddLineToPoint( XLOG2DEVMAC( x2 ) , XLOG2DEVMAC( y2 ) ) ;
1482
1483 m_graphicContext->StrokePath( path ) ;
1484 delete path ;
1485 }
1486 #endif
1487
1488 void wxDC::DoDrawPolygon( int n, wxPoint points[],
1489 wxCoord xoffset, wxCoord yoffset,
1490 int fillStyle )
1491 {
1492 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPolygon - invalid DC") );
1493
1494 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
1495 return ;
1496 if ( m_logicalFunction != wxCOPY )
1497 return ;
1498
1499 wxCoord x1, x2 , y1 , y2 ;
1500 x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset);
1501 y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset);
1502
1503 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1504 path->MoveToPoint( x1 , y1 ) ;
1505 for (int i = 1; i < n; i++)
1506 {
1507 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1508 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1509
1510 path->AddLineToPoint( x2 , y2 ) ;
1511 }
1512
1513 if ( x1 != x2 || y1 != y2 )
1514 path->AddLineToPoint( x1, y1 ) ;
1515
1516 path->CloseSubpath() ;
1517 m_graphicContext->DrawPath( path , fillStyle ) ;
1518
1519 delete path ;
1520 }
1521
1522 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1523 {
1524 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRectangle - invalid DC") );
1525
1526 if ( m_logicalFunction != wxCOPY )
1527 return ;
1528
1529 wxCoord xx = XLOG2DEVMAC(x);
1530 wxCoord yy = YLOG2DEVMAC(y);
1531 wxCoord ww = m_signX * XLOG2DEVREL(width);
1532 wxCoord hh = m_signY * YLOG2DEVREL(height);
1533
1534 // CMB: draw nothing if transformed w or h is 0
1535 if (ww == 0 || hh == 0)
1536 return;
1537
1538 // CMB: handle -ve width and/or height
1539 if (ww < 0)
1540 {
1541 ww = -ww;
1542 xx = xx - ww;
1543 }
1544 if (hh < 0)
1545 {
1546 hh = -hh;
1547 yy = yy - hh;
1548 }
1549
1550 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1551 path->AddRectangle( xx , yy , ww , hh ) ;
1552 m_graphicContext->DrawPath( path ) ;
1553 delete path ;
1554 }
1555
1556 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
1557 wxCoord width, wxCoord height,
1558 double radius)
1559 {
1560 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1561
1562 if ( m_logicalFunction != wxCOPY )
1563 return ;
1564
1565 if (radius < 0.0)
1566 radius = - radius * ((width < height) ? width : height);
1567 wxCoord xx = XLOG2DEVMAC(x);
1568 wxCoord yy = YLOG2DEVMAC(y);
1569 wxCoord ww = m_signX * XLOG2DEVREL(width);
1570 wxCoord hh = m_signY * YLOG2DEVREL(height);
1571
1572 // CMB: draw nothing if transformed w or h is 0
1573 if (ww == 0 || hh == 0)
1574 return;
1575
1576 // CMB: handle -ve width and/or height
1577 if (ww < 0)
1578 {
1579 ww = -ww;
1580 xx = xx - ww;
1581 }
1582 if (hh < 0)
1583 {
1584 hh = -hh;
1585 yy = yy - hh;
1586 }
1587
1588 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1589 CGContextRef ctx = mctx->GetNativeContext() ;
1590 AddRoundedRectToPath( ctx , CGRectMake( xx , yy , ww , hh ) , radius , radius ) ;
1591 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
1592 }
1593
1594 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1595 {
1596 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipse - invalid DC") );
1597
1598 if ( m_logicalFunction != wxCOPY )
1599 return ;
1600
1601 wxCoord xx = XLOG2DEVMAC(x);
1602 wxCoord yy = YLOG2DEVMAC(y);
1603 wxCoord ww = m_signX * XLOG2DEVREL(width);
1604 wxCoord hh = m_signY * YLOG2DEVREL(height);
1605
1606 // CMB: draw nothing if transformed w or h is 0
1607 if (ww == 0 || hh == 0)
1608 return;
1609
1610 // CMB: handle -ve width and/or height
1611 if (ww < 0)
1612 {
1613 ww = -ww;
1614 xx = xx - ww;
1615 }
1616 if (hh < 0)
1617 {
1618 hh = -hh;
1619 yy = yy - hh;
1620 }
1621
1622 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1623 CGContextRef ctx = mctx->GetNativeContext() ;
1624 CGContextSaveGState( ctx ) ;
1625 CGContextTranslateCTM( ctx, xx + ww / 2, yy + hh / 2 );
1626 CGContextScaleCTM( ctx , ww / 2 , hh / 2 ) ;
1627 CGContextAddArc( ctx, 0, 0, 1, 0 , 2 * M_PI , 0 );
1628 CGContextRestoreGState( ctx ) ;
1629 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
1630 }
1631
1632 bool wxDC::CanDrawBitmap() const
1633 {
1634 return true ;
1635 }
1636
1637 bool wxDC::DoBlit(
1638 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
1639 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
1640 wxCoord xsrcMask, wxCoord ysrcMask )
1641 {
1642 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoBlit - invalid DC") );
1643 wxCHECK_MSG( source->Ok(), false, wxT("wxDC(cg)::DoBlit - invalid source DC") );
1644
1645 if ( logical_func == wxNO_OP )
1646 return true ;
1647
1648 if (xsrcMask == -1 && ysrcMask == -1)
1649 {
1650 xsrcMask = xsrc;
1651 ysrcMask = ysrc;
1652 }
1653
1654 wxCoord yysrc = source->YLOG2DEVMAC(ysrc) ;
1655 wxCoord xxsrc = source->XLOG2DEVMAC(xsrc) ;
1656 wxCoord wwsrc = source->XLOG2DEVREL(width) ;
1657 wxCoord hhsrc = source->YLOG2DEVREL(height) ;
1658
1659 wxCoord yydest = YLOG2DEVMAC(ydest) ;
1660 wxCoord xxdest = XLOG2DEVMAC(xdest) ;
1661 wxCoord wwdest = XLOG2DEVREL(width) ;
1662 wxCoord hhdest = YLOG2DEVREL(height) ;
1663
1664 wxMemoryDC* memdc = dynamic_cast<wxMemoryDC*>(source) ;
1665 if ( memdc && logical_func == wxCOPY )
1666 {
1667 wxBitmap blit = memdc->GetSelectedObject() ;
1668
1669 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") ) ;
1670
1671 wxCoord bmpwidth = blit.GetWidth();
1672 wxCoord bmpheight = blit.GetHeight();
1673
1674 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
1675 {
1676 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc ) ;
1677 hhsrc = wxMin( hhsrc , bmpheight - yysrc ) ;
1678 if ( wwsrc > 0 && hhsrc > 0 )
1679 {
1680 if ( xxsrc >= 0 && yysrc >= 0 )
1681 {
1682 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc ) ;
1683 blit = blit.GetSubBitmap( subrect ) ;
1684 }
1685 else
1686 {
1687 // in this case we'd probably have to adjust the different coordinates, but
1688 // we have to find out proper contract first
1689 blit = wxNullBitmap ;
1690 }
1691 }
1692 else
1693 {
1694 blit = wxNullBitmap ;
1695 }
1696 }
1697
1698 if ( blit.Ok() )
1699 {
1700 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1701 CGImageRef image = (CGImageRef)( blit.CGImageCreate() ) ;
1702 HIRect r = CGRectMake( xxdest , yydest , wwdest , hhdest ) ;
1703 HIViewDrawCGImage( cg , &r , image ) ;
1704 CGImageRelease( image ) ;
1705 }
1706 }
1707 else
1708 {
1709 #if 0
1710 CGContextRef cg = (wxMacCGContext*)(source->GetGraphicContext())->GetNativeContext() ;
1711 void *data = CGBitmapContextGetData( cg ) ;
1712 #endif
1713
1714 return false ; // wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ) ;
1715 }
1716
1717 return true;
1718 }
1719
1720 void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
1721 double angle)
1722 {
1723 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRotatedText - invalid DC") );
1724 wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoDrawRotatedText - no valid font set") );
1725
1726 if ( str.length() == 0 )
1727 return ;
1728 if ( m_logicalFunction != wxCOPY )
1729 return ;
1730
1731 OSStatus status = noErr ;
1732 ATSUTextLayout atsuLayout ;
1733 UniCharCount chars = str.length() ;
1734 UniChar* ubuf = NULL ;
1735
1736 #if SIZEOF_WCHAR_T == 4
1737 wxMBConvUTF16 converter ;
1738 #if wxUSE_UNICODE
1739 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1740 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1741 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
1742 #else
1743 const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1744 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
1745 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1746 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
1747 #endif
1748 chars = unicharlen / 2 ;
1749 #else
1750 #if wxUSE_UNICODE
1751 ubuf = (UniChar*) str.wc_str() ;
1752 #else
1753 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1754 chars = wxWcslen( wchar.data() ) ;
1755 ubuf = (UniChar*) wchar.data() ;
1756 #endif
1757 #endif
1758
1759 int drawX = XLOG2DEVMAC(x) ;
1760 int drawY = YLOG2DEVMAC(y) ;
1761
1762 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1763 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1764
1765 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
1766
1767 status = ::ATSUSetTransientFontMatching( atsuLayout , true ) ;
1768 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
1769
1770 int iAngle = int( angle );
1771 if ( abs(iAngle) > 0 )
1772 {
1773 Fixed atsuAngle = IntToFixed( iAngle ) ;
1774 ATSUAttributeTag atsuTags[] =
1775 {
1776 kATSULineRotationTag ,
1777 } ;
1778 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1779 {
1780 sizeof( Fixed ) ,
1781 } ;
1782 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1783 {
1784 &atsuAngle ,
1785 } ;
1786 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1787 atsuTags, atsuSizes, atsuValues ) ;
1788 }
1789
1790 {
1791 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1792 ATSUAttributeTag atsuTags[] =
1793 {
1794 kATSUCGContextTag ,
1795 } ;
1796 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1797 {
1798 sizeof( CGContextRef ) ,
1799 } ;
1800 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1801 {
1802 &cgContext ,
1803 } ;
1804 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1805 atsuTags, atsuSizes, atsuValues ) ;
1806 }
1807
1808 ATSUTextMeasurement textBefore, textAfter ;
1809 ATSUTextMeasurement ascent, descent ;
1810
1811 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1812 &textBefore , &textAfter, &ascent , &descent );
1813
1814 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1815
1816 Rect rect ;
1817
1818 if ( m_backgroundMode == wxSOLID )
1819 {
1820 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1821 path->MoveToPoint( drawX , drawY ) ;
1822 path->AddLineToPoint(
1823 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1824 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) ) ;
1825 path->AddLineToPoint(
1826 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1827 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ;
1828 path->AddLineToPoint(
1829 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1830 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ;
1831
1832 m_graphicContext->FillPath( path , m_textBackgroundColour ) ;
1833 delete path ;
1834 }
1835
1836 drawX += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent));
1837 drawY += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent));
1838
1839 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1840 IntToFixed(drawX) , IntToFixed(drawY) , &rect );
1841 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1842
1843 CGContextSaveGState(((wxMacCGContext*)(m_graphicContext))->GetNativeContext());
1844 CGContextTranslateCTM(((wxMacCGContext*)(m_graphicContext))->GetNativeContext(), drawX, drawY);
1845 CGContextScaleCTM(((wxMacCGContext*)(m_graphicContext))->GetNativeContext(), 1, -1);
1846 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1847 IntToFixed(0) , IntToFixed(0) );
1848
1849 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
1850
1851 CGContextRestoreGState( ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ) ;
1852
1853 CalcBoundingBox(XDEV2LOG(rect.left), YDEV2LOG(rect.top) );
1854 CalcBoundingBox(XDEV2LOG(rect.right), YDEV2LOG(rect.bottom) );
1855
1856 ::ATSUDisposeTextLayout(atsuLayout);
1857
1858 #if SIZEOF_WCHAR_T == 4
1859 free( ubuf ) ;
1860 #endif
1861 }
1862
1863 void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
1864 {
1865 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawText - invalid DC") );
1866
1867 DoDrawRotatedText( strtext , x , y , 0.0 ) ;
1868 }
1869
1870 bool wxDC::CanGetTextExtent() const
1871 {
1872 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::CanGetTextExtent - invalid DC") );
1873
1874 return true ;
1875 }
1876
1877 void wxDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
1878 wxCoord *descent, wxCoord *externalLeading ,
1879 wxFont *theFont ) const
1880 {
1881 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoGetTextExtent - invalid DC") );
1882
1883 wxFont formerFont = m_font ;
1884 if ( theFont )
1885 {
1886 // work around the const-ness
1887 *((wxFont*)(&m_font)) = *theFont ;
1888 MacInstallFont() ;
1889 }
1890
1891 if ( str.empty() )
1892 return ;
1893
1894 wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") ) ;
1895
1896 OSStatus status = noErr ;
1897 ATSUTextLayout atsuLayout ;
1898 UniCharCount chars = str.length() ;
1899 UniChar* ubuf = NULL ;
1900
1901 #if SIZEOF_WCHAR_T == 4
1902 wxMBConvUTF16 converter ;
1903 #if wxUSE_UNICODE
1904 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1905 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1906 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
1907 #else
1908 const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1909 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
1910 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1911 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
1912 #endif
1913 chars = unicharlen / 2 ;
1914 #else
1915 #if wxUSE_UNICODE
1916 ubuf = (UniChar*) str.wc_str() ;
1917 #else
1918 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1919 chars = wxWcslen( wchar.data() ) ;
1920 ubuf = (UniChar*) wchar.data() ;
1921 #endif
1922 #endif
1923
1924 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1925 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1926
1927 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
1928
1929 ATSUTextMeasurement textBefore, textAfter ;
1930 ATSUTextMeasurement textAscent, textDescent ;
1931
1932 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1933 &textBefore , &textAfter, &textAscent , &textDescent );
1934
1935 if ( height )
1936 *height = YDEV2LOGREL( FixedToInt(textAscent + textDescent) ) ;
1937 if ( descent )
1938 *descent =YDEV2LOGREL( FixedToInt(textDescent) );
1939 if ( externalLeading )
1940 *externalLeading = 0 ;
1941 if ( width )
1942 *width = XDEV2LOGREL( FixedToInt(textAfter - textBefore) ) ;
1943
1944 ::ATSUDisposeTextLayout(atsuLayout);
1945
1946 #if SIZEOF_WCHAR_T == 4
1947 free( ubuf ) ;
1948 #endif
1949
1950 if ( theFont )
1951 {
1952 // work around the constness
1953 *((wxFont*)(&m_font)) = formerFont ;
1954 MacInstallFont() ;
1955 }
1956 }
1957
1958 bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
1959 {
1960 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPartialTextExtents - invalid DC") );
1961
1962 widths.Empty();
1963 widths.Add(0, text.length());
1964
1965 if (text.empty())
1966 return false;
1967
1968 ATSUTextLayout atsuLayout ;
1969 UniCharCount chars = text.length() ;
1970 UniChar* ubuf = NULL ;
1971
1972 #if SIZEOF_WCHAR_T == 4
1973 wxMBConvUTF16 converter ;
1974 #if wxUSE_UNICODE
1975 size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 ) ;
1976 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1977 converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 ) ;
1978 #else
1979 const wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ;
1980 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
1981 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1982 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
1983 #endif
1984 chars = unicharlen / 2 ;
1985 #else
1986 #if wxUSE_UNICODE
1987 ubuf = (UniChar*) text.wc_str() ;
1988 #else
1989 wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ;
1990 chars = wxWcslen( wchar.data() ) ;
1991 ubuf = (UniChar*) wchar.data() ;
1992 #endif
1993 #endif
1994
1995 OSStatus status;
1996 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1997 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1998
1999 for ( int pos = 0; pos < (int)chars; pos ++ )
2000 {
2001 unsigned long actualNumberOfBounds = 0;
2002 ATSTrapezoid glyphBounds;
2003
2004 // We get a single bound, since the text should only require one. If it requires more, there is an issue
2005 OSStatus result;
2006 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2007 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2008 if (result != noErr || actualNumberOfBounds != 1 )
2009 return false;
2010
2011 widths[pos] = XDEV2LOGREL(FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x ));
2012 //unsigned char uch = s[i];
2013 }
2014
2015 ::ATSUDisposeTextLayout(atsuLayout);
2016 return true;
2017 }
2018
2019 wxCoord wxDC::GetCharWidth(void) const
2020 {
2021 wxCoord width ;
2022 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL ) ;
2023
2024 return width ;
2025 }
2026
2027 wxCoord wxDC::GetCharHeight(void) const
2028 {
2029 wxCoord height ;
2030 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL ) ;
2031
2032 return height ;
2033 }
2034
2035 void wxDC::Clear(void)
2036 {
2037 wxCHECK_RET( Ok(), wxT("wxDC(cg)::Clear - invalid DC") );
2038
2039 if (m_backgroundBrush.Ok() && m_backgroundBrush.GetStyle() != wxTRANSPARENT)
2040 {
2041 HIRect rect = CGRectMake( -10000 , -10000 , 20000 , 20000 ) ;
2042 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
2043 switch ( m_backgroundBrush.MacGetBrushKind() )
2044 {
2045 case kwxMacBrushTheme :
2046 {
2047 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
2048 if ( HIThemeSetFill != 0 )
2049 {
2050 HIThemeSetFill( m_backgroundBrush.MacGetTheme(), NULL, cg, kHIThemeOrientationNormal );
2051 CGContextFillRect(cg, rect);
2052
2053 }
2054 else
2055 #endif
2056 {
2057 RGBColor color;
2058 GetThemeBrushAsColor( m_backgroundBrush.MacGetTheme(), 32, true, &color );
2059 CGContextSetRGBFillColor( cg, (float) color.red / 65536,
2060 (float) color.green / 65536, (float) color.blue / 65536, 1 );
2061 CGContextFillRect( cg, rect );
2062 }
2063
2064 // reset to normal value
2065 RGBColor col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
2066 CGContextSetRGBFillColor( cg, col.red / 65536.0, col.green / 65536.0, col.blue / 65536.0, 1.0 );
2067 }
2068 break ;
2069
2070 case kwxMacBrushThemeBackground :
2071 {
2072 wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ;
2073
2074 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2075 if ( UMAGetSystemVersion() >= 0x1030 )
2076 {
2077 HIThemeBackgroundDrawInfo drawInfo ;
2078 drawInfo.version = 0 ;
2079 drawInfo.state = kThemeStateActive ;
2080 drawInfo.kind = m_backgroundBrush.MacGetThemeBackground( NULL ) ;
2081 if ( drawInfo.kind == kThemeBackgroundMetal )
2082 {
2083 HIThemeDrawBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ;
2084 HIThemeApplyBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ;
2085 }
2086 }
2087 #endif
2088 }
2089 break ;
2090
2091 case kwxMacBrushColour :
2092 {
2093 // FIXME: doesn't correctly render stippled brushes !!
2094 // FIXME: should this be replaced by ::SetBrush() ??
2095
2096 RGBColor col = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) ;
2097 CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
2098 CGContextFillRect(cg, rect);
2099
2100 // reset to normal value
2101 col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
2102 CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
2103 }
2104 break ;
2105
2106 default :
2107 wxFAIL_MSG( wxT("unknown brush kind") ) ;
2108 break ;
2109 }
2110 }
2111 }
2112
2113 void wxDC::MacInstallFont() const
2114 {
2115 wxCHECK_RET( Ok(), wxT("wxDC(cg)::MacInstallFont - invalid DC") );
2116
2117 if ( m_macATSUIStyle )
2118 {
2119 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
2120 m_macATSUIStyle = NULL ;
2121 }
2122
2123 if ( m_font.Ok() )
2124 {
2125 OSStatus status ;
2126
2127 status = ATSUCreateAndCopyStyle( (ATSUStyle) m_font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle ) ;
2128
2129 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") ) ;
2130
2131 Fixed atsuSize = IntToFixed( int(m_scaleY * m_font.MacGetFontSize()) ) ;
2132 RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel() ) ;
2133 ATSUAttributeTag atsuTags[] =
2134 {
2135 kATSUSizeTag ,
2136 kATSUColorTag ,
2137 } ;
2138 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
2139 {
2140 sizeof( Fixed ) ,
2141 sizeof( RGBColor ) ,
2142 } ;
2143 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
2144 {
2145 &atsuSize ,
2146 &atsuColor ,
2147 } ;
2148
2149 status = ::ATSUSetAttributes(
2150 (ATSUStyle)m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
2151 atsuTags, atsuSizes, atsuValues);
2152
2153 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ) ;
2154 }
2155 }
2156
2157 #pragma mark -
2158
2159 // ---------------------------------------------------------------------------
2160 // coordinates transformations
2161 // ---------------------------------------------------------------------------
2162
2163 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2164 {
2165 return ((wxDC *)this)->XDEV2LOG(x);
2166 }
2167
2168 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2169 {
2170 return ((wxDC *)this)->YDEV2LOG(y);
2171 }
2172
2173 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2174 {
2175 return ((wxDC *)this)->XDEV2LOGREL(x);
2176 }
2177
2178 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2179 {
2180 return ((wxDC *)this)->YDEV2LOGREL(y);
2181 }
2182
2183 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2184 {
2185 return ((wxDC *)this)->XLOG2DEV(x);
2186 }
2187
2188 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2189 {
2190 return ((wxDC *)this)->YLOG2DEV(y);
2191 }
2192
2193 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2194 {
2195 return ((wxDC *)this)->XLOG2DEVREL(x);
2196 }
2197
2198 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2199 {
2200 return ((wxDC *)this)->YLOG2DEVREL(y);
2201 }
2202
2203 #endif // wxMAC_USE_CORE_GRAPHICS