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