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