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