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