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