1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dc.h"
16 #include "wx/wxprec.h"
20 #if wxMAC_USE_CORE_GRAPHICS
23 #include "wx/mac/uma.h"
24 #include "wx/dcmemory.h"
25 #include "wx/dcprint.h"
26 #include "wx/region.h"
36 #include "wx/mac/private.h"
38 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
46 const double M_PI
= 3.14159265358979 ;
49 const double RAD2DEG
= 180.0 / M_PI
;
50 const short kEmulatedMode
= -1 ;
51 const short kUnsupportedMode
= -2 ;
53 extern TECObjectRef s_TECNativeCToUnicode
;
56 // The text ctrl implementation still needs that for the non hiview implementation
58 wxMacWindowClipper::wxMacWindowClipper( const wxWindow
* win
) :
59 wxMacPortSaver( (GrafPtr
) GetWindowPort((WindowRef
) win
->MacGetTopLevelWindowRef()) )
61 m_newPort
=(GrafPtr
) GetWindowPort((WindowRef
) win
->MacGetTopLevelWindowRef()) ;
62 m_formerClip
= NewRgn() ;
63 m_newClip
= NewRgn() ;
64 GetClip( m_formerClip
) ;
68 // guard against half constructed objects, this just leads to a empty clip
72 win
->MacWindowToRootWindow( &x
,&y
) ;
73 // get area including focus rect
74 CopyRgn( (RgnHandle
) ((wxWindow
*)win
)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip
) ;
75 if ( !EmptyRgn( m_newClip
) )
76 OffsetRgn( m_newClip
, x
, y
) ;
79 SetClip( m_newClip
) ;
83 wxMacWindowClipper::~wxMacWindowClipper()
85 SetPort( m_newPort
) ;
86 SetClip( m_formerClip
) ;
87 DisposeRgn( m_newClip
) ;
88 DisposeRgn( m_formerClip
) ;
91 wxMacWindowStateSaver::wxMacWindowStateSaver( const wxWindow
* win
) :
92 wxMacWindowClipper( win
)
94 // the port is already set at this point
95 m_newPort
=(GrafPtr
) GetWindowPort((WindowRef
) win
->MacGetTopLevelWindowRef()) ;
96 GetThemeDrawingState( &m_themeDrawingState
) ;
99 wxMacWindowStateSaver::~wxMacWindowStateSaver()
101 SetPort( m_newPort
) ;
102 SetThemeDrawingState( m_themeDrawingState
, true ) ;
105 // minimal implementation only used for appearance drawing < 10.3
107 wxMacPortSetter::wxMacPortSetter( const wxDC
* dc
) :
108 m_ph( (GrafPtr
) dc
->m_macPort
)
110 wxASSERT( dc
->Ok() ) ;
112 // dc->MacSetupPort(&m_ph) ;
114 wxMacPortSetter::~wxMacPortSetter()
116 // m_dc->MacCleanupPort(&m_ph) ;
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
124 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
125 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
127 //-----------------------------------------------------------------------------
128 // device context implementation
130 // more and more of the dc functionality should be implemented by calling
131 // the appropricate wxMacCGContext, but we will have to do that step by step
132 // also coordinate conversions should be moved to native matrix ops
133 //-----------------------------------------------------------------------------
135 // we always stock two context states, one at entry, to be able to preserve the
136 // state we were called with, the other one after changing to HI Graphics orientation
137 // (this one is used for getting back clippings etc)
139 wxMacCGPath::wxMacCGPath()
141 m_path
= CGPathCreateMutable() ;
144 wxMacCGPath::~wxMacCGPath()
146 CGPathRelease( m_path
) ;
149 // Starts a new subpath at
150 void wxMacCGPath::MoveToPoint( wxCoord x1
, wxCoord y1
)
152 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
) ;
155 void wxMacCGPath::AddLineToPoint( wxCoord x1
, wxCoord y1
)
157 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
) ;
160 void wxMacCGPath::AddRectangle( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
162 CGRect cgRect
= { { x
, y
} , { w
, h
} } ;
163 CGPathAddRect( m_path
, NULL
, cgRect
) ;
166 void wxMacCGPath::AddCircle( wxCoord x
, wxCoord y
, wxCoord r
)
168 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true ) ;
171 // closes the current subpath
172 void wxMacCGPath::CloseSubpath()
174 CGPathCloseSubpath( m_path
) ;
177 CGPathRef
wxMacCGPath::GetPath() const
182 wxMacCGContext::wxMacCGContext( CGrafPtr port
)
188 wxMacCGContext::wxMacCGContext( CGContextRef cgcontext
)
191 m_cgContext
= cgcontext
;
192 CGContextSaveGState( m_cgContext
) ;
193 CGContextSaveGState( m_cgContext
) ;
196 wxMacCGContext::wxMacCGContext()
202 wxMacCGContext::~wxMacCGContext()
206 CGContextSynchronize( m_cgContext
) ;
207 CGContextRestoreGState( m_cgContext
) ;
208 CGContextRestoreGState( m_cgContext
) ;
211 CGContextRelease( m_cgContext
) ;
215 void wxMacCGContext::Clip( const wxRegion
®ion
)
217 // ClipCGContextToRegion ( m_cgContext, &bounds , (RgnHandle) dc->m_macCurrentClipRgn ) ;
220 void wxMacCGContext::StrokePath( const wxGraphicPath
*p
)
222 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
223 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
224 CGContextStrokePath( m_cgContext
) ;
227 void wxMacCGContext::DrawPath( const wxGraphicPath
*p
, int fillStyle
)
229 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
230 CGPathDrawingMode mode
= m_mode
;
231 if ( fillStyle
== wxODDEVEN_RULE
)
233 if ( mode
== kCGPathFill
)
234 mode
= kCGPathEOFill
;
235 else if ( mode
== kCGPathFillStroke
)
236 mode
= kCGPathEOFillStroke
;
238 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
239 CGContextDrawPath( m_cgContext
, mode
) ;
242 void wxMacCGContext::FillPath( const wxGraphicPath
*p
, const wxColor
&fillColor
, int fillStyle
)
244 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
245 CGContextSaveGState( m_cgContext
) ;
247 RGBColor col
= MAC_WXCOLORREF( fillColor
.GetPixel() ) ;
248 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
249 CGPathDrawingMode mode
= kCGPathFill
;
251 if ( fillStyle
== wxODDEVEN_RULE
)
252 mode
= kCGPathEOFill
;
254 CGContextBeginPath( m_cgContext
) ;
255 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
256 CGContextClosePath( m_cgContext
) ;
257 CGContextDrawPath( m_cgContext
, mode
) ;
259 CGContextRestoreGState( m_cgContext
) ;
262 wxGraphicPath
* wxMacCGContext::CreatePath()
264 // make sure that we now have a real cgref, before doing
265 // anything with paths
266 CGContextRef cg
= GetNativeContext() ;
268 return new wxMacCGPath() ;
271 // in case we only got a QDPort only create a cgref now
273 CGContextRef
wxMacCGContext::GetNativeContext()
275 if( m_cgContext
== NULL
)
278 GetPortBounds( (CGrafPtr
) m_qdPort
, &bounds
) ;
279 OSStatus status
= CreateCGContextForPort((CGrafPtr
) m_qdPort
, &m_cgContext
) ;
280 CGContextSaveGState( m_cgContext
) ;
282 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") ) ;
283 CGContextTranslateCTM( m_cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
284 CGContextScaleCTM( m_cgContext
, 1 , -1 ) ;
286 CGContextSaveGState( m_cgContext
) ;
288 SetBrush( m_brush
) ;
293 void wxMacCGContext::SetNativeContext( CGContextRef cg
)
295 // we allow either setting or clearing but not replacing
296 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
) ;
298 CGContextSaveGState( cg
) ;
304 // Experimental support for dashes and patterned brushes
305 // uncomment the following lines to enable it
307 // #define _NEW_GC_DASHES_
308 // #define _NEW_GC_SUPPORT_
310 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
311 #define kCGColorSpaceGenericRGB CFSTR("kCGColorSpaceGenericRGB")
314 void EstablishPatternColorSpace(
319 CGColorSpaceRef baseSpace
, patternSpace
;
329 patternSpace
= CGColorSpaceCreatePattern( NULL
);
332 CGContextSetFillColorSpace( ctxRef
, patternSpace
);
334 CGContextSetStrokeColorSpace( ctxRef
, patternSpace
);
338 baseSpace
= CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
);
339 patternSpace
= CGColorSpaceCreatePattern( baseSpace
);
342 CGContextSetFillColorSpace( ctxRef
, patternSpace
);
344 CGContextSetStrokeColorSpace( ctxRef
, patternSpace
);
347 // NB: the context owns these now, and this code is finished with them
348 if (patternSpace
!= NULL
)
349 CGColorSpaceRelease( patternSpace
);
350 if (baseSpace
!= NULL
)
351 CGColorSpaceRelease( baseSpace
);
354 void ImagePatternRender(
356 CGContextRef ctxRef
)
361 CGImageRef imageRef
= (CGImageRef
)info
;
362 if (imageRef
!= NULL
)
364 CGRect boundsR
= CGRectMake( 0.0, 0.0, (float)CGImageGetWidth( imageRef
), (float)CGImageGetHeight( imageRef
) );
365 CGContextDrawImage( ctxRef
, boundsR
, imageRef
);
369 void ImagePatternDispose(
372 CGImageRef imageRef
= (CGImageRef
)info
;
373 if (imageRef
!= NULL
)
374 CGImageRelease( imageRef
);
377 // specifies the struct version value and the callback functions for draw and release
378 static const CGPatternCallbacks sImagePatternCallback
= { 0, &ImagePatternRender
, &ImagePatternDispose
};
380 long CreatePatternFromBitmap(
381 CGPatternRef
*patternRef
,
382 const wxBitmap
*rasterInfo
,
387 long errorStatus
, widthV
, heightV
, depthV
;
389 if (patternRef
== NULL
)
396 if ((rasterInfo
== NULL
) || !rasterInfo
->Ok())
399 if (errorStatus
== 0)
401 // build a usable bounding CGRect from the wxBitmap's bounds wxRect
402 widthV
= rasterInfo
->GetWidth();
403 heightV
= rasterInfo
->GetHeight();
404 if ((widthV
<= 0) || (heightV
<= 0))
408 if (errorStatus
== 0)
410 depthV
= rasterInfo
->GetDepth();
411 // isColored = (depthV > 1);
413 // FIXME: this is often <= 0 - why???
415 // errorStatus = (-4);
418 if (errorStatus
== 0)
420 imageRef
= (CGImageRef
)(rasterInfo
->CGImageCreate());
421 if (imageRef
== NULL
)
425 if (errorStatus
== 0)
427 // FIXME: switch when this routine belongs to a DC class...
428 boundsR
= CGRectMake( 0.0, 0.0, (float)widthV
, (float)heightV
);
429 // boundsR = CGRectMake( 0.0, 0.0, (float)XLOG2DEVREL( widthV ), (float)XLOG2DEVREL( heightV ) );
431 *patternRef
= CGPatternCreate(
434 CGAffineTransformIdentity
,
437 kCGPatternTilingNoDistortion
,
439 &sImagePatternCallback
);
441 if (*patternRef
== (CGPatternRef
)NULL
)
448 long CreatePatternFromDashes(
449 CGPatternRef
*patternRef
,
450 const wxDash
*sourceDash
,
456 if (patternRef
== NULL
)
460 if ((sourceDash
== NULL
) || (count
<= 0))
463 wxBitmap
dashBits( (char*)sourceDash
, 8, count
, 1 );
464 errorStatus
= CreatePatternFromBitmap( patternRef
, &dashBits
, useMultibit
);
469 long CreatePatternFromBrush(
470 CGPatternRef
*patternRef
,
471 const wxBrush
&sourceBrush
,
476 if (patternRef
== NULL
)
480 errorStatus
= CreatePatternFromBitmap( patternRef
, sourceBrush
.GetStipple(), useMultibit
);
485 long CreatePatternFromPen(
486 CGPatternRef
*patternRef
,
487 const wxPen
&sourcePen
,
492 if (patternRef
== NULL
)
496 errorStatus
= CreatePatternFromBitmap( patternRef
, sourcePen
.GetStipple(), useMultibit
);
504 // FIXME: the NEW_GC_SUPPORT part this routine is unfinished and needs lots of work !!
506 void wxMacCGContext::SetPen( const wxPen
&pen
)
509 if ( m_cgContext
== NULL
)
511 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
512 bool stroke
= pen
.GetStyle() != wxTRANSPARENT
;
515 // we can benchmark performance, should go into a setting later
516 CGContextSetShouldAntialias( m_cgContext
, false ) ;
521 m_mode
= kCGPathFill
; // just a default
525 m_mode
= kCGPathFill
;
529 #if defined(_NEW_GC_SUPPORT_)
532 CGPatternRef patternRef
;
535 bool hasSetPattern
, useMultibit
;
537 hasSetPattern
= false;
539 result
= CreatePatternFromPen( &patternRef
, pen
, useMultibit
);
542 EstablishPatternColorSpace( m_cgContext
, useMultibit
, false );
545 CGContextSetStrokePattern( m_cgContext
, patternRef
, alphaArray
);
546 CGPatternRelease( patternRef
);
548 hasSetPattern
= true;
550 //wxLogDebug( wxT("CreatePatternFromPen succeeded!") );
553 // NB: the (-2) result is from wxPen instances that don't have a stipple wxBitmap
555 wxLogDebug( wxT("CreatePatternFromPen failed: result [%ld]"), result
);
562 col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() );
564 GetThemeBrushAsColor( pen
.MacGetTheme(), 32, true, &col
);
567 CGContextSetRGBStrokeColor(
568 m_cgContext
, (float) col
.red
/ 65536.0,
569 (float) col
.green
/ 65536.0, (float) col
.blue
/ 65536.0, 1.0 );
574 // original implementation
575 RGBColor col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() ) ;
576 CGContextSetRGBStrokeColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
580 switch( pen
.GetCap() )
583 cap
= kCGLineCapRound
;
585 case wxCAP_PROJECTING
:
586 cap
= kCGLineCapSquare
;
589 cap
= kCGLineCapButt
;
592 cap
= kCGLineCapButt
;
595 CGContextSetLineCap( m_cgContext
, cap
) ;
598 switch( pen
.GetJoin() )
601 join
= kCGLineJoinBevel
;
604 join
= kCGLineJoinMiter
;
607 join
= kCGLineJoinRound
;
610 join
= kCGLineJoinMiter
;
613 CGContextSetLineJoin( m_cgContext
, join
) ;
615 /* TODO * m_dc->m_scaleX */
616 float penWidth
= pen
.GetWidth();
619 CGContextSetLineWidth( m_cgContext
, penWidth
) ;
621 m_mode
= kCGPathStroke
;
624 #if defined(_NEW_GC_DASHES_)
625 const char *dashData
= NULL
;
626 char *userDashData
= NULL
;
629 const char dotted
[] = { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };
630 const char dashed
[] = { 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00 };
631 const char short_dashed
[] = { 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 };
632 const char dotted_dashed
[] = { 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x00 };
634 switch (pen
.GetStyle())
637 // default, undashed pen
642 count
= WXSIZEOF(dotted
);
646 count
= WXSIZEOF(dashed
);
649 dashData
= short_dashed
;
650 count
= WXSIZEOF(short_dashed
);
653 dashData
= dotted_dashed
;
654 count
= WXSIZEOF(dotted_dashed
);
657 count
= pen
.GetDashes( (wxDash
**)&userDashData
);
658 dashData
= userDashData
;
665 if ((dashData
!= NULL
) && (count
> 0))
667 CGPatternRef patternRef
;
673 result
= CreatePatternFromDashes( &patternRef
, (const wxDash
*)dashData
, count
, useMultibit
);
676 col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() );
677 CGContextSetRGBStrokeColor(
678 m_cgContext
, (float) col
.red
/ 65536.0,
679 (float) col
.green
/ 65536.0, (float) col
.blue
/ 65536.0, 1.0 );
681 EstablishPatternColorSpace( m_cgContext
, useMultibit
, false );
684 CGContextSetStrokePattern( m_cgContext
, patternRef
, alphaArray
);
685 CGPatternRelease( patternRef
);
689 wxLogDebug( wxT("CreatePatternFromDashes failed: result [%ld]"), result
);
692 const float *lengths
= NULL
;
693 float *userLengths
= NULL
;
695 const float dotted
[] = { 3 , 3 };
696 const float dashed
[] = { 19 , 9 };
697 const float short_dashed
[] = { 9 , 6 };
698 const float dotted_dashed
[] = { 9 , 6 , 3 , 3 };
700 switch( pen
.GetStyle() )
706 count
= WXSIZEOF(dotted
);
710 count
= WXSIZEOF(dashed
) ;
713 lengths
= short_dashed
;
714 count
= WXSIZEOF(short_dashed
) ;
717 lengths
= dotted_dashed
;
718 count
= WXSIZEOF(dotted_dashed
);
722 count
= pen
.GetDashes( &dashes
) ;
723 if ((dashes
!= NULL
) && (count
> 0))
725 userLengths
= new float[count
] ;
726 for( int i
= 0 ; i
< count
; ++i
)
728 userLengths
[i
] = (float)dashes
[i
] ;
729 if (userLengths
[i
] <= 0.0)
731 userLengths
[i
] = 1.0;
732 // wxLogDebug( wxT("wxMacCGContext::SetPen - bad dash length[%d] [%.2f]"), i, (float)dashes[i] );
736 lengths
= userLengths
;
742 if ((lengths
!= NULL
) && (count
> 0))
744 // we need to change the cap, otherwise everything overlaps
745 // and we get solid lines
746 CGContextSetLineDash( m_cgContext
, 0 , lengths
, count
) ;
747 CGContextSetLineCap( m_cgContext
, kCGLineCapButt
) ;
751 CGContextSetLineDash( m_cgContext
, 0 , NULL
, 0 ) ;
754 delete[] userLengths
;
757 if ( fill
&& stroke
)
759 m_mode
= kCGPathFillStroke
;
764 void wxMacCGContext::SetBrush( const wxBrush
&brush
)
767 if ( m_cgContext
== NULL
)
770 bool fill
= brush
.GetStyle() != wxTRANSPARENT
;
771 bool stroke
= m_pen
.GetStyle() != wxTRANSPARENT
;
774 // we can benchmark performance, should go into a setting later
775 CGContextSetShouldAntialias( m_cgContext
, false ) ;
781 m_mode
= kCGPathFill
; // just a default
785 #if defined(_NEW_GC_SUPPORT_)
788 CGPatternRef patternRef
;
791 bool hasSetPattern
, useMultibit
;
793 hasSetPattern
= false;
795 result
= CreatePatternFromBrush( &patternRef
, brush
, useMultibit
);
798 EstablishPatternColorSpace( m_cgContext
, useMultibit
, true );
801 CGContextSetFillPattern( m_cgContext
, patternRef
, alphaArray
);
802 CGPatternRelease( patternRef
);
804 hasSetPattern
= true;
806 //wxLogDebug( wxT("CreatePatternFromBrush succeeded!") );
809 // NB: the (-2) result is from wxBrush instances that don't have a stipple wxBitmap
811 wxLogDebug( wxT("CreatePatternFromBrush failed: result [%ld]"), result
);
818 col
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() );
820 GetThemeBrushAsColor( brush
.MacGetTheme(), 32, true, &col
);
823 CGContextSetRGBFillColor(
824 m_cgContext
, (float) col
.red
/ 65536.0,
825 (float) col
.green
/ 65536.0, (float) col
.blue
/ 65536.0, 1.0 );
830 // original implementation
831 RGBColor col
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() ) ;
832 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
835 m_mode
= kCGPathFill
;
839 m_mode
= kCGPathStroke
;
841 if ( fill
&& stroke
)
843 m_mode
= kCGPathFillStroke
;
848 void AddEllipticArcToPath(CGContextRef c
, CGPoint center
, float a
, float b
, float fromDegree
, float toDegree
)
850 CGContextSaveGState(c
);
851 CGContextTranslateCTM(c
, center
.x
, center
.y
);
852 CGContextScaleCTM(c
, a
, b
);
853 CGContextMoveToPoint(c
, 1, 0);
854 CGContextAddArc(c
, 0, 0, 1, DegToRad(fromDegree
), DegToRad(toDegree
), 0);
855 CGContextClosePath(c
);
856 CGContextRestoreGState(c
);
859 void AddRoundedRectToPath(CGContextRef c
, CGRect rect
, float ovalWidth
,
863 if (ovalWidth
== 0 || ovalHeight
== 0)
865 CGContextAddRect(c
, rect
);
868 CGContextSaveGState(c
);
869 CGContextTranslateCTM(c
, CGRectGetMinX(rect
), CGRectGetMinY(rect
));
870 CGContextScaleCTM(c
, ovalWidth
, ovalHeight
);
871 fw
= CGRectGetWidth(rect
) / ovalWidth
;
872 fh
= CGRectGetHeight(rect
) / ovalHeight
;
873 CGContextMoveToPoint(c
, fw
, fh
/2);
874 CGContextAddArcToPoint(c
, fw
, fh
, fw
/2, fh
, 1);
875 CGContextAddArcToPoint(c
, 0, fh
, 0, fh
/2, 1);
876 CGContextAddArcToPoint(c
, 0, 0, fw
/2, 0, 1);
877 CGContextAddArcToPoint(c
, fw
, 0, fw
, fh
/2, 1);
878 CGContextClosePath(c
);
879 CGContextRestoreGState(c
);
886 m_mm_to_pix_x
= mm2pt
;
887 m_mm_to_pix_y
= mm2pt
;
888 m_internalDeviceOriginX
= 0;
889 m_internalDeviceOriginY
= 0;
890 m_externalDeviceOriginX
= 0;
891 m_externalDeviceOriginY
= 0;
892 m_logicalScaleX
= 1.0;
893 m_logicalScaleY
= 1.0;
898 m_needComputeScaleX
= FALSE
;
899 m_needComputeScaleY
= FALSE
;
903 m_macLocalOrigin
.x
= m_macLocalOrigin
.y
= 0 ;
905 m_pen
= *wxBLACK_PEN
;
906 m_font
= *wxNORMAL_FONT
;
907 m_brush
= *wxWHITE_BRUSH
;
909 m_macATSUIStyle
= NULL
;
911 m_graphicContext
= NULL
;
916 if( m_macATSUIStyle
)
918 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
919 m_macATSUIStyle
= NULL
;
922 delete m_graphicContext
;
925 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
927 wxCHECK_RET( Ok(), wxT("invalid window dc") );
928 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
929 wxCoord xx
= XLOG2DEVMAC(x
);
930 wxCoord yy
= YLOG2DEVMAC(y
);
931 wxCoord w
= bmp
.GetWidth();
932 wxCoord h
= bmp
.GetHeight();
933 wxCoord ww
= XLOG2DEVREL(w
);
934 wxCoord hh
= YLOG2DEVREL(h
);
936 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
937 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() ) ;
938 HIRect r
= CGRectMake( xx
, yy
, ww
, hh
) ;
939 HIViewDrawCGImage( cg
, &r
, image
) ;
940 CGImageRelease( image
) ;
943 void wxDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
945 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
946 wxCHECK_RET(icon
.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
948 wxCoord xx
= XLOG2DEVMAC(x
);
949 wxCoord yy
= YLOG2DEVMAC(y
);
950 wxCoord w
= icon
.GetWidth();
951 wxCoord h
= icon
.GetHeight();
952 wxCoord ww
= XLOG2DEVREL(w
);
953 wxCoord hh
= YLOG2DEVREL(h
);
955 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
956 CGRect r
= CGRectMake( 00 , 00 , ww
, hh
) ;
957 CGContextSaveGState(cg
);
958 CGContextTranslateCTM(cg
, xx
, yy
+ hh
);
959 CGContextScaleCTM(cg
, 1, -1);
960 PlotIconRefInContext( cg
, &r
, kAlignNone
, kTransformNone
,
961 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) ) ;
962 CGContextRestoreGState( cg
) ;
965 void wxDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
967 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
968 wxCoord xx
, yy
, ww
, hh
;
971 ww
= XLOG2DEVREL(width
);
972 hh
= YLOG2DEVREL(height
);
974 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
975 CGRect clipRect
= CGRectMake( xx
,yy
, ww
, hh
) ;
976 CGContextClipToRect( cgContext
, clipRect
) ;
978 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
979 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
982 m_clipX1
= wxMax( m_clipX1
, xx
);
983 m_clipY1
= wxMax( m_clipY1
, yy
);
984 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
985 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
995 // TODO as soon as we don't reset the context for each operation anymore
996 // we have to update the context as well
999 void wxDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
1001 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
1004 DestroyClippingRegion();
1008 region
.GetBox( x
, y
, w
, h
);
1009 wxCoord xx
, yy
, ww
, hh
;
1010 xx
= XLOG2DEVMAC(x
);
1011 yy
= YLOG2DEVMAC(y
);
1012 ww
= XLOG2DEVREL(w
);
1013 hh
= YLOG2DEVREL(h
);
1014 // if we have a scaling that we cannot map onto native regions
1015 // we must use the box
1016 if ( ww
!= w
|| hh
!= h
)
1018 wxDC::DoSetClippingRegion( x
, y
, w
, h
);
1023 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
1024 if ( xx != x || yy != y )
1026 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
1028 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
1032 m_clipX1
= wxMax( m_clipX1
, xx
);
1033 m_clipY1
= wxMax( m_clipY1
, yy
);
1034 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
1035 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
1048 void wxDC::DestroyClippingRegion()
1050 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
1051 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1052 CGContextRestoreGState( cgContext
);
1053 CGContextSaveGState( cgContext
);
1054 m_graphicContext
->SetPen( m_pen
) ;
1055 m_graphicContext
->SetBrush( m_brush
) ;
1059 void wxDC::DoGetSizeMM( int* width
, int* height
) const
1064 *width
= long( double(w
) / (m_scaleX
*m_mm_to_pix_x
) );
1065 *height
= long( double(h
) / (m_scaleY
*m_mm_to_pix_y
) );
1068 void wxDC::SetTextForeground( const wxColour
&col
)
1070 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1071 if ( col
!= m_textForegroundColour
)
1073 m_textForegroundColour
= col
;
1078 void wxDC::SetTextBackground( const wxColour
&col
)
1080 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1081 m_textBackgroundColour
= col
;
1084 void wxDC::SetMapMode( int mode
)
1089 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
1092 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
1095 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
1098 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
1102 SetLogicalScale( 1.0, 1.0 );
1105 if (mode
!= wxMM_TEXT
)
1107 m_needComputeScaleX
= TRUE
;
1108 m_needComputeScaleY
= TRUE
;
1112 void wxDC::SetUserScale( double x
, double y
)
1114 // allow negative ? -> no
1117 ComputeScaleAndOrigin();
1120 void wxDC::SetLogicalScale( double x
, double y
)
1123 m_logicalScaleX
= x
;
1124 m_logicalScaleY
= y
;
1125 ComputeScaleAndOrigin();
1128 void wxDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
1130 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
1131 m_logicalOriginY
= y
* m_signY
;
1132 ComputeScaleAndOrigin();
1135 void wxDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
1137 m_externalDeviceOriginX
= x
;
1138 m_externalDeviceOriginY
= y
;
1139 ComputeScaleAndOrigin();
1142 void wxDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1144 m_signX
= (xLeftRight
? 1 : -1);
1145 m_signY
= (yBottomUp
? -1 : 1);
1146 ComputeScaleAndOrigin();
1149 wxSize
wxDC::GetPPI() const
1151 return wxSize(72, 72);
1154 int wxDC::GetDepth() const
1159 void wxDC::ComputeScaleAndOrigin()
1161 // CMB: copy scale to see if it changes
1162 double origScaleX
= m_scaleX
;
1163 double origScaleY
= m_scaleY
;
1164 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
1165 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
1166 m_deviceOriginX
= m_internalDeviceOriginX
+ m_externalDeviceOriginX
;
1167 m_deviceOriginY
= m_internalDeviceOriginY
+ m_externalDeviceOriginY
;
1168 // CMB: if scale has changed call SetPen to recalulate the line width
1169 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
1171 // this is a bit artificial, but we need to force wxDC to think
1172 // the pen has changed
1173 wxPen
pen(GetPen());
1179 void wxDC::SetPalette( const wxPalette
& palette
)
1183 void wxDC::SetBackgroundMode( int mode
)
1185 m_backgroundMode
= mode
;
1188 void wxDC::SetFont( const wxFont
&font
)
1194 void wxDC::SetPen( const wxPen
&pen
)
1199 if ( m_graphicContext
)
1201 m_graphicContext
->SetPen( m_pen
) ;
1205 void wxDC::SetBrush( const wxBrush
&brush
)
1207 if (m_brush
== brush
)
1210 if ( m_graphicContext
)
1212 m_graphicContext
->SetBrush( m_brush
) ;
1216 void wxDC::SetBackground( const wxBrush
&brush
)
1218 if (m_backgroundBrush
== brush
)
1220 m_backgroundBrush
= brush
;
1221 if (!m_backgroundBrush
.Ok())
1225 void wxDC::SetLogicalFunction( int function
)
1227 if (m_logicalFunction
== function
)
1229 m_logicalFunction
= function
;
1232 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
1233 const wxColour
& col
, int style
);
1235 bool wxDC::DoFloodFill(wxCoord x
, wxCoord y
,
1236 const wxColour
& col
, int style
)
1238 return wxDoFloodFill(this, x
, y
, col
, style
);
1241 bool wxDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
1243 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
1244 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
1245 wxMacPortSaver
helper((CGrafPtr
)m_macPort
) ;
1248 XLOG2DEVMAC(x
) + m_macLocalOriginInPort
.x
- m_macLocalOrigin
.x
,
1249 YLOG2DEVMAC(y
) + m_macLocalOriginInPort
.y
- m_macLocalOrigin
.y
, &colour
);
1250 // Convert from Mac colour to wx
1251 col
->Set( colour
.red
>> 8,
1257 void wxDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
1259 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1261 if ( m_logicalFunction
!= wxCOPY
)
1264 wxCoord xx1
= XLOG2DEVMAC(x1
) ;
1265 wxCoord yy1
= YLOG2DEVMAC(y1
) ;
1266 wxCoord xx2
= XLOG2DEVMAC(x2
) ;
1267 wxCoord yy2
= YLOG2DEVMAC(y2
) ;
1269 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1270 path
->MoveToPoint( xx1
, yy1
) ;
1271 path
->AddLineToPoint( xx2
, yy2
) ;
1272 path
->CloseSubpath() ;
1273 m_graphicContext
->StrokePath( path
) ;
1276 CalcBoundingBox(x1
, y1
);
1277 CalcBoundingBox(x2
, y2
);
1280 void wxDC::DoCrossHair( wxCoord x
, wxCoord y
)
1282 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
1284 if ( m_logicalFunction
!= wxCOPY
)
1290 wxCoord xx
= XLOG2DEVMAC(x
);
1291 wxCoord yy
= YLOG2DEVMAC(y
);
1293 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1294 path
->MoveToPoint( XLOG2DEVMAC(0), yy
) ;
1295 path
->AddLineToPoint( XLOG2DEVMAC(w
), yy
) ;
1296 path
->CloseSubpath() ;
1297 path
->MoveToPoint( xx
, YLOG2DEVMAC(0) ) ;
1298 path
->AddLineToPoint( xx
, YLOG2DEVMAC(h
) ) ;
1299 path
->CloseSubpath() ;
1300 m_graphicContext
->StrokePath( path
) ;
1303 CalcBoundingBox(x
, y
);
1304 CalcBoundingBox(x
+w
, y
+h
);
1307 void wxDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
1308 wxCoord x2
, wxCoord y2
,
1309 wxCoord xc
, wxCoord yc
)
1311 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
1313 if ( m_logicalFunction
!= wxCOPY
)
1316 wxCoord xx1
= XLOG2DEVMAC(x1
);
1317 wxCoord yy1
= YLOG2DEVMAC(y1
);
1318 wxCoord xx2
= XLOG2DEVMAC(x2
);
1319 wxCoord yy2
= YLOG2DEVMAC(y2
);
1320 wxCoord xxc
= XLOG2DEVMAC(xc
);
1321 wxCoord yyc
= YLOG2DEVMAC(yc
);
1322 double dx
= xx1
- xxc
;
1323 double dy
= yy1
- yyc
;
1324 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
1325 wxCoord rad
= (wxCoord
)radius
;
1327 if (xx1
== xx2
&& yy1
== yy2
)
1332 else if (radius
== 0.0)
1338 sa
= (xx1
- xxc
== 0) ?
1339 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
1340 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
1341 ea
= (xx2
- xxc
== 0) ?
1342 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
1343 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
1346 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
1347 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1348 CGContextRef ctx
= mctx
->GetNativeContext() ;
1349 CGContextSaveGState( ctx
) ;
1350 CGContextTranslateCTM( ctx
, xxc
, yyc
);
1351 CGContextScaleCTM( ctx
, 1 , -1 ) ;
1353 CGContextMoveToPoint( ctx
, 0 , 0 ) ;
1354 CGContextAddArc( ctx
, 0, 0 , rad
, DegToRad(sa
), DegToRad(ea
), 0);
1356 CGContextAddLineToPoint( ctx
, 0 , 0 ) ;
1357 CGContextRestoreGState( ctx
) ;
1358 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1361 void wxDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
1362 double sa
, double ea
)
1364 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
1366 if ( m_logicalFunction
!= wxCOPY
)
1369 wxCoord xx
= XLOG2DEVMAC(x
);
1370 wxCoord yy
= YLOG2DEVMAC(y
);
1371 wxCoord ww
= m_signX
* XLOG2DEVREL(w
);
1372 wxCoord hh
= m_signY
* YLOG2DEVREL(h
);
1373 // handle -ve width and/or height
1374 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
1375 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
1377 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
1379 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1380 CGContextRef ctx
= mctx
->GetNativeContext() ;
1382 CGContextSaveGState( ctx
) ;
1383 CGContextTranslateCTM( ctx
, xx
+ ww
/ 2, yy
+ hh
/ 2);
1384 CGContextScaleCTM( ctx
, 1 * ww
/ 2 , -1 * hh
/ 2 ) ;
1386 CGContextMoveToPoint( ctx
, 0 , 0 ) ;
1387 CGContextAddArc( ctx
, 0, 0, 1, DegToRad(sa
), DegToRad(ea
), 0);
1389 CGContextAddLineToPoint( ctx
, 0 , 0 ) ;
1390 CGContextRestoreGState( ctx
) ;
1391 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1394 void wxDC::DoDrawPoint( wxCoord x
, wxCoord y
)
1396 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1397 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 ) ;
1400 void wxDC::DoDrawLines(int n
, wxPoint points
[],
1401 wxCoord xoffset
, wxCoord yoffset
)
1403 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1405 if ( m_logicalFunction
!= wxCOPY
)
1408 wxCoord x1
, x2
, y1
, y2
;
1409 x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1410 y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1411 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1412 path
->MoveToPoint( x1
, y1
) ;
1413 for (int i
= 1; i
< n
; i
++)
1415 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1416 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1418 path
->AddLineToPoint( x2
, y2
) ;
1420 m_graphicContext
->StrokePath( path
) ;
1424 void wxDC::DoDrawPolygon(int n
, wxPoint points
[],
1425 wxCoord xoffset
, wxCoord yoffset
,
1428 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1429 wxCoord x1
, x2
, y1
, y2
;
1430 if ( n
== 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
1433 if ( m_logicalFunction
!= wxCOPY
)
1436 x2
= x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1437 y2
= y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1439 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1440 path
->MoveToPoint( x1
, y1
) ;
1441 for (int i
= 1; i
< n
; i
++)
1443 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1444 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1446 path
->AddLineToPoint( x2
, y2
) ;
1448 if ( x1
!= x2
|| y1
!= y2
)
1450 path
->AddLineToPoint( x1
,y1
) ;
1452 path
->CloseSubpath() ;
1453 m_graphicContext
->DrawPath( path
, fillStyle
) ;
1457 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1459 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1461 if ( m_logicalFunction
!= wxCOPY
)
1464 wxCoord xx
= XLOG2DEVMAC(x
);
1465 wxCoord yy
= YLOG2DEVMAC(y
);
1466 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1467 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1468 // CMB: draw nothing if transformed w or h is 0
1469 if (ww
== 0 || hh
== 0)
1471 // CMB: handle -ve width and/or height
1482 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1483 path
->AddRectangle(xx
,yy
, ww
, hh
) ;
1484 m_graphicContext
->DrawPath( path
) ;
1488 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1489 wxCoord width
, wxCoord height
,
1492 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1494 if ( m_logicalFunction
!= wxCOPY
)
1499 radius
= - radius
* ((width
< height
) ? width
: height
);
1500 wxCoord xx
= XLOG2DEVMAC(x
);
1501 wxCoord yy
= YLOG2DEVMAC(y
);
1502 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1503 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1504 // CMB: draw nothing if transformed w or h is 0
1505 if (ww
== 0 || hh
== 0)
1507 // CMB: handle -ve width and/or height
1518 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1519 CGContextRef ctx
= mctx
->GetNativeContext() ;
1520 AddRoundedRectToPath( ctx
, CGRectMake( xx
, yy
, ww
, hh
) , 16 ,16 ) ;
1521 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1524 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1526 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1528 if ( m_logicalFunction
!= wxCOPY
)
1531 wxCoord xx
= XLOG2DEVMAC(x
);
1532 wxCoord yy
= YLOG2DEVMAC(y
);
1533 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1534 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1535 // CMB: draw nothing if transformed w or h is 0
1536 if (ww
== 0 || hh
== 0)
1538 // CMB: handle -ve width and/or height
1550 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1551 CGContextRef ctx
= mctx
->GetNativeContext() ;
1552 CGContextSaveGState( ctx
) ;
1553 CGContextTranslateCTM( ctx
, xx
+ ww
/ 2, yy
+ hh
/ 2);
1554 CGContextScaleCTM( ctx
, ww
/ 2 , hh
/ 2 ) ;
1555 CGContextAddArc( ctx
, 0, 0, 1, 0 , 2*M_PI
, 0);
1556 CGContextRestoreGState( ctx
) ;
1557 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1560 bool wxDC::CanDrawBitmap(void) const
1565 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1566 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1567 wxCoord xsrcMask
, wxCoord ysrcMask
)
1569 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
1570 wxCHECK_MSG(source
->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
1571 if ( logical_func
== wxNO_OP
)
1573 if (xsrcMask
== -1 && ysrcMask
== -1)
1575 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1578 wxCoord yysrc
= source
->YLOG2DEVMAC(ysrc
) ;
1579 wxCoord xxsrc
= source
->XLOG2DEVMAC(xsrc
) ;
1580 wxCoord wwsrc
= source
->XLOG2DEVREL(width
) ;
1581 wxCoord hhsrc
= source
->YLOG2DEVREL(height
) ;
1583 wxCoord yydest
= YLOG2DEVMAC(ydest
) ;
1584 wxCoord xxdest
= XLOG2DEVMAC(xdest
) ;
1585 wxCoord wwdest
= XLOG2DEVREL(width
) ;
1586 wxCoord hhdest
= YLOG2DEVREL(height
) ;
1588 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
) ;
1589 if ( memdc
&& logical_func
== wxCOPY
)
1591 wxBitmap blit
= memdc
->GetSelectedObject() ;
1592 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") ) ;
1594 wxCoord bmpwidth
= blit
.GetWidth();
1595 wxCoord bmpheight
= blit
.GetHeight();
1597 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1599 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
) ;
1600 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
) ;
1601 if ( wwsrc
> 0 && hhsrc
> 0 )
1603 if ( xxsrc
>= 0 && yysrc
>= 0 )
1605 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
) ;
1606 blit
= blit
.GetSubBitmap( subrect
) ;
1610 // in this case we'd probably have to adjust the different coordinates, but
1611 // we have to find out proper contract first
1612 blit
= wxNullBitmap
;
1617 blit
= wxNullBitmap
;
1622 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1623 CGImageRef image
= (CGImageRef
)( blit
.CGImageCreate() ) ;
1624 HIRect r
= CGRectMake( xxdest
, yydest
, wwdest
, hhdest
) ;
1625 HIViewDrawCGImage( cg
, &r
, image
) ;
1626 CGImageRelease( image
) ;
1633 CGContextRef cg = (wxMacCGContext*)(source->GetGraphicContext())->GetNativeContext() ;
1634 void *data = CGBitmapContextGetData( cg ) ;
1636 return FALSE
; // wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ) ;
1641 void wxDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1644 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1646 if ( str
.Length() == 0 )
1649 if ( m_logicalFunction
!= wxCOPY
)
1652 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1654 OSStatus status
= noErr
;
1655 ATSUTextLayout atsuLayout
;
1656 UniCharCount chars
= str
.Length() ;
1657 UniChar
* ubuf
= NULL
;
1658 #if SIZEOF_WCHAR_T == 4
1659 wxMBConvUTF16 converter
;
1661 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1662 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1663 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1665 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1666 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1667 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1668 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1670 chars
= unicharlen
/ 2 ;
1673 ubuf
= (UniChar
*) str
.wc_str() ;
1675 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1676 chars
= wxWcslen( wchar
.data() ) ;
1677 ubuf
= (UniChar
*) wchar
.data() ;
1681 int drawX
= XLOG2DEVMAC(x
) ;
1682 int drawY
= YLOG2DEVMAC(y
) ;
1684 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1685 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1687 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1689 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true ) ;
1690 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1692 int iAngle
= int( angle
);
1693 if ( abs(iAngle
) > 0 )
1695 Fixed atsuAngle
= IntToFixed( iAngle
) ;
1696 ATSUAttributeTag atsuTags
[] =
1698 kATSULineRotationTag
,
1700 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1704 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1708 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1709 atsuTags
, atsuSizes
, atsuValues
) ;
1712 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1713 ATSUAttributeTag atsuTags
[] =
1717 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1719 sizeof( CGContextRef
) ,
1721 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1725 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1726 atsuTags
, atsuSizes
, atsuValues
) ;
1729 ATSUTextMeasurement textBefore
;
1730 ATSUTextMeasurement textAfter
;
1731 ATSUTextMeasurement ascent
;
1732 ATSUTextMeasurement descent
;
1734 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1735 &textBefore
, &textAfter
, &ascent
, &descent
);
1736 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1740 if ( m_backgroundMode
== wxSOLID
)
1742 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1746 path
->AddLineToPoint(
1747 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ,
1748 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ) ;
1749 path
->AddLineToPoint(
1750 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) + cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1751 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) - sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1752 path
->AddLineToPoint(
1753 (int) (drawX
+ cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1754 (int) (drawY
- sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1756 m_graphicContext
->FillPath( path
, m_textBackgroundColour
) ;
1760 drawX
+= (int)(sin(angle
/RAD2DEG
) * FixedToInt(ascent
));
1761 drawY
+= (int)(cos(angle
/RAD2DEG
) * FixedToInt(ascent
));
1763 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1764 IntToFixed(drawX
) , IntToFixed(drawY
) , &rect
);
1765 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1767 CGContextSaveGState(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext());
1768 CGContextTranslateCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), drawX
, drawY
);
1769 CGContextScaleCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), 1, -1);
1770 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1771 IntToFixed(0) , IntToFixed(0) );
1772 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1773 CGContextRestoreGState( ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ) ;
1775 CalcBoundingBox(XDEV2LOG(rect
.left
), YDEV2LOG(rect
.top
) );
1776 CalcBoundingBox(XDEV2LOG(rect
.right
), YDEV2LOG(rect
.bottom
) );
1778 ::ATSUDisposeTextLayout(atsuLayout
);
1779 #if SIZEOF_WCHAR_T == 4
1784 void wxDC::DoDrawText(const wxString
& strtext
, wxCoord x
, wxCoord y
)
1786 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1787 DoDrawRotatedText( strtext
, x
, y
, 0.0 ) ;
1790 bool wxDC::CanGetTextExtent() const
1792 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1796 void wxDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1797 wxCoord
*descent
, wxCoord
*externalLeading
,
1798 wxFont
*theFont
) const
1800 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1801 wxFont formerFont
= m_font
;
1804 // work around the constness
1805 *((wxFont
*)(&m_font
)) = *theFont
;
1809 if ( str
.Length() == 0 )
1812 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1814 OSStatus status
= noErr
;
1815 ATSUTextLayout atsuLayout
;
1816 UniCharCount chars
= str
.Length() ;
1817 UniChar
* ubuf
= NULL
;
1818 #if SIZEOF_WCHAR_T == 4
1819 wxMBConvUTF16 converter
;
1821 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1822 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1823 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1825 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1826 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1827 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1828 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1830 chars
= unicharlen
/ 2 ;
1833 ubuf
= (UniChar
*) str
.wc_str() ;
1835 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1836 chars
= wxWcslen( wchar
.data() ) ;
1837 ubuf
= (UniChar
*) wchar
.data() ;
1842 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1843 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1845 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1847 ATSUTextMeasurement textBefore
;
1848 ATSUTextMeasurement textAfter
;
1849 ATSUTextMeasurement textAscent
;
1850 ATSUTextMeasurement textDescent
;
1852 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1853 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1856 *height
= YDEV2LOGREL( FixedToInt(textAscent
+ textDescent
) ) ;
1858 *descent
=YDEV2LOGREL( FixedToInt(textDescent
) );
1859 if ( externalLeading
)
1860 *externalLeading
= 0 ;
1862 *width
= XDEV2LOGREL( FixedToInt(textAfter
- textBefore
) ) ;
1864 ::ATSUDisposeTextLayout(atsuLayout
);
1865 #if SIZEOF_WCHAR_T == 4
1870 // work around the constness
1871 *((wxFont
*)(&m_font
)) = formerFont
;
1877 bool wxDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1879 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1882 widths
.Add(0, text
.Length());
1884 if (text
.Length() == 0)
1887 ATSUTextLayout atsuLayout
;
1888 UniCharCount chars
= text
.Length() ;
1889 UniChar
* ubuf
= NULL
;
1890 #if SIZEOF_WCHAR_T == 4
1891 wxMBConvUTF16 converter
;
1893 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 ) ;
1894 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1895 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 ) ;
1897 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1898 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1899 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1900 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1902 chars
= unicharlen
/ 2 ;
1905 ubuf
= (UniChar
*) text
.wc_str() ;
1907 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1908 chars
= wxWcslen( wchar
.data() ) ;
1909 ubuf
= (UniChar
*) wchar
.data() ;
1914 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1915 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1917 for ( int pos
= 0; pos
< (int)chars
; pos
++ ) {
1918 unsigned long actualNumberOfBounds
= 0;
1919 ATSTrapezoid glyphBounds
;
1921 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1923 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1, kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1924 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1929 widths
[pos
] = XDEV2LOGREL(FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
));
1930 //unsigned char uch = s[i];
1933 ::ATSUDisposeTextLayout(atsuLayout
);
1937 wxCoord
wxDC::GetCharWidth(void) const
1940 DoGetTextExtent(wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
) ;
1944 wxCoord
wxDC::GetCharHeight(void) const
1947 DoGetTextExtent(wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
) ;
1951 void wxDC::Clear(void)
1953 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1955 if ( m_backgroundBrush
.Ok() && m_backgroundBrush
.GetStyle() != wxTRANSPARENT
)
1957 HIRect rect
= CGRectMake( -10000 , -10000 , 20000 , 20000 ) ;
1958 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1959 switch( m_backgroundBrush
.MacGetBrushKind() )
1961 case kwxMacBrushTheme
:
1963 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1964 if ( HIThemeSetFill
!= 0 )
1966 HIThemeSetFill( m_backgroundBrush
.MacGetTheme(), NULL
, cg
, kHIThemeOrientationNormal
);
1967 CGContextFillRect(cg
, rect
);
1974 GetThemeBrushAsColor( m_backgroundBrush
.MacGetTheme(), 32, true, &color
);
1975 CGContextSetRGBFillColor( cg
, (float) color
.red
/ 65536,
1976 (float) color
.green
/ 65536, (float) color
.blue
/ 65536, 1 );
1977 CGContextFillRect( cg
, rect
);
1980 // reset to normal value
1981 RGBColor col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
1982 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0, col
.green
/ 65536.0, col
.blue
/ 65536.0, 1.0 );
1985 case kwxMacBrushThemeBackground
:
1987 wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ;
1988 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1989 if ( UMAGetSystemVersion() >= 0x1030 )
1991 HIThemeBackgroundDrawInfo drawInfo
;
1992 drawInfo
.version
= 0 ;
1993 drawInfo
.state
= kThemeStateActive
;
1994 drawInfo
.kind
= m_backgroundBrush
.MacGetThemeBackground(NULL
) ;
1995 if ( drawInfo
.kind
== kThemeBackgroundMetal
)
1996 HIThemeDrawBackground( &rect
, &drawInfo
, cg
,
1997 kHIThemeOrientationNormal
) ;
1998 HIThemeApplyBackground( &rect
, &drawInfo
, cg
,
1999 kHIThemeOrientationNormal
) ;
2007 case kwxMacBrushColour
:
2009 RGBColor col
= MAC_WXCOLORREF( m_backgroundBrush
.GetColour().GetPixel()) ;
2010 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
2011 CGContextFillRect(cg
, rect
);
2013 // reset to normal value
2014 col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
2015 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
2022 void wxDC::MacInstallFont() const
2024 wxCHECK_RET(Ok(), wxT("Invalid DC"));
2026 if( m_macATSUIStyle
)
2028 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
2029 m_macATSUIStyle
= NULL
;
2034 OSStatus status
= noErr
;
2035 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) m_font
.MacGetATSUStyle() , (ATSUStyle
*) &m_macATSUIStyle
) ;
2036 wxASSERT_MSG( status
== noErr
, wxT("couldn't set create ATSU style") ) ;
2038 Fixed atsuSize
= IntToFixed( int(m_scaleY
* m_font
.MacGetFontSize()) ) ;
2039 RGBColor atsuColor
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel() ) ;
2040 ATSUAttributeTag atsuTags
[] =
2045 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
2048 sizeof( RGBColor
) ,
2050 // Boolean kTrue = true ;
2051 // Boolean kFalse = false ;
2053 // ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
2054 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
2059 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUIStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
) ,
2060 atsuTags
, atsuSizes
, atsuValues
);
2062 wxASSERT_MSG( status
== noErr
, wxT("couldn't Modify ATSU style") ) ;
2066 // ---------------------------------------------------------------------------
2067 // coordinates transformations
2068 // ---------------------------------------------------------------------------
2070 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
2072 return ((wxDC
*)this)->XDEV2LOG(x
);
2075 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
2077 return ((wxDC
*)this)->YDEV2LOG(y
);
2080 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
2082 return ((wxDC
*)this)->XDEV2LOGREL(x
);
2085 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
2087 return ((wxDC
*)this)->YDEV2LOGREL(y
);
2090 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
2092 return ((wxDC
*)this)->XLOG2DEV(x
);
2095 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
2097 return ((wxDC
*)this)->YLOG2DEV(y
);
2100 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
2102 return ((wxDC
*)this)->XLOG2DEVREL(x
);
2105 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
2107 return ((wxDC
*)this)->YLOG2DEVREL(y
);
2110 #endif // wxMAC_USE_CORE_GRAPHICS