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