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