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