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 wxMacCGPath::wxMacCGPath()
137 m_path
= CGPathCreateMutable() ;
140 wxMacCGPath::~wxMacCGPath()
142 CGPathRelease( m_path
) ;
145 // Starts a new subpath at
146 void wxMacCGPath::MoveToPoint( wxCoord x1
, wxCoord y1
)
148 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
) ;
151 void wxMacCGPath::AddLineToPoint( wxCoord x1
, wxCoord y1
)
153 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
) ;
156 void wxMacCGPath::AddRectangle( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
158 CGRect cgRect
= { { x
, y
} , { w
, h
} } ;
159 CGPathAddRect( m_path
, NULL
, cgRect
) ;
162 void wxMacCGPath::AddCircle( wxCoord x
, wxCoord y
, wxCoord r
)
164 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true ) ;
167 // closes the current subpath
168 void wxMacCGPath::CloseSubpath()
170 CGPathCloseSubpath( m_path
) ;
173 CGPathRef
wxMacCGPath::GetPath() const
178 // we always stock two context states, one at entry, the other one after
179 // changing to HI Graphics orientation (this one is used for getting back clippings etc)
181 wxMacCGContext::wxMacCGContext( CGrafPtr port
)
187 wxMacCGContext::wxMacCGContext( CGContextRef cgcontext
)
190 m_cgContext
= cgcontext
;
191 CGContextSaveGState( m_cgContext
) ;
192 CGContextSaveGState( m_cgContext
) ;
195 wxMacCGContext::wxMacCGContext()
201 wxMacCGContext::~wxMacCGContext()
205 CGContextSynchronize( m_cgContext
) ;
206 CGContextRestoreGState( m_cgContext
) ;
207 CGContextRestoreGState( m_cgContext
) ;
210 CGContextRelease( m_cgContext
) ;
214 void wxMacCGContext::Clip( const wxRegion
®ion
)
216 // ClipCGContextToRegion ( m_cgContext, &bounds , (RgnHandle) dc->m_macCurrentClipRgn ) ;
219 void wxMacCGContext::StrokePath( const wxGraphicPath
*p
)
221 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
222 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
223 CGContextStrokePath( m_cgContext
) ;
226 void wxMacCGContext::DrawPath( const wxGraphicPath
*p
, int fillStyle
)
228 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
229 CGPathDrawingMode mode
= m_mode
;
230 if ( fillStyle
== wxODDEVEN_RULE
)
232 if ( mode
== kCGPathFill
)
233 mode
= kCGPathEOFill
;
234 else if ( mode
== kCGPathFillStroke
)
235 mode
= kCGPathEOFillStroke
;
237 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
238 CGContextDrawPath( m_cgContext
, mode
) ;
241 void wxMacCGContext::FillPath( const wxGraphicPath
*p
, const wxColor
&fillColor
, int fillStyle
)
243 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
244 CGContextSaveGState( m_cgContext
) ;
246 RGBColor col
= MAC_WXCOLORREF( fillColor
.GetPixel() ) ;
247 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
248 CGPathDrawingMode mode
= kCGPathFill
;
250 if ( fillStyle
== wxODDEVEN_RULE
)
251 mode
= kCGPathEOFill
;
253 CGContextBeginPath( m_cgContext
) ;
254 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
255 CGContextClosePath( m_cgContext
) ;
256 CGContextDrawPath( m_cgContext
, mode
) ;
258 CGContextRestoreGState( m_cgContext
) ;
261 wxGraphicPath
* wxMacCGContext::CreatePath()
263 // make sure that we now have a real cgref, before doing
264 // anything with paths
265 CGContextRef cg
= GetNativeContext() ;
267 return new wxMacCGPath() ;
270 // in case we only got a QDPort only create a cgref now
272 CGContextRef
wxMacCGContext::GetNativeContext()
274 if( m_cgContext
== NULL
)
277 GetPortBounds( (CGrafPtr
) m_qdPort
, &bounds
) ;
278 OSStatus status
= CreateCGContextForPort((CGrafPtr
) m_qdPort
, &m_cgContext
) ;
279 CGContextSaveGState( m_cgContext
) ;
281 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") ) ;
282 CGContextTranslateCTM( m_cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
283 CGContextScaleCTM( m_cgContext
, 1 , -1 ) ;
285 CGContextSaveGState( m_cgContext
) ;
287 SetBrush( m_brush
) ;
292 void wxMacCGContext::SetNativeContext( CGContextRef cg
)
294 // we allow either setting or clearing but not replacing
295 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
) ;
297 CGContextSaveGState( cg
) ;
303 // Experimental support for dashes and patterned brushes
304 // uncomment the following lines to enable it
306 // #define _NEW_GC_DASHES_
307 // #define _NEW_GC_SUPPORT_
309 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
310 #define kCGColorSpaceGenericRGB CFSTR("kCGColorSpaceGenericRGB")
313 void EstablishPatternColorSpace(
318 CGColorSpaceRef baseSpace
, patternSpace
;
328 patternSpace
= CGColorSpaceCreatePattern( NULL
);
331 CGContextSetFillColorSpace( ctxRef
, patternSpace
);
333 CGContextSetStrokeColorSpace( ctxRef
, patternSpace
);
337 baseSpace
= CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
);
338 patternSpace
= CGColorSpaceCreatePattern( baseSpace
);
341 CGContextSetFillColorSpace( ctxRef
, patternSpace
);
343 CGContextSetStrokeColorSpace( ctxRef
, patternSpace
);
346 // NB: the context owns these now, and this code is finished with them
347 if (patternSpace
!= NULL
)
348 CGColorSpaceRelease( patternSpace
);
349 if (baseSpace
!= NULL
)
350 CGColorSpaceRelease( baseSpace
);
353 void ImagePatternRender(
355 CGContextRef ctxRef
)
360 CGImageRef imageRef
= (CGImageRef
)info
;
361 if (imageRef
!= NULL
)
363 CGRect boundsR
= CGRectMake( 0.0, 0.0, (float)CGImageGetWidth( imageRef
), (float)CGImageGetHeight( imageRef
) );
364 CGContextDrawImage( ctxRef
, boundsR
, imageRef
);
368 void ImagePatternDispose(
371 CGImageRef imageRef
= (CGImageRef
)info
;
372 if (imageRef
!= NULL
)
373 CGImageRelease( imageRef
);
376 // specifies the struct version value and the callback functions for draw and release
377 static const CGPatternCallbacks sImagePatternCallback
= { 0, &ImagePatternRender
, &ImagePatternDispose
};
379 long CreatePatternFromBitmap(
380 CGPatternRef
*patternRef
,
381 const wxBitmap
*rasterInfo
,
386 long errorStatus
, widthV
, heightV
, depthV
;
388 if (patternRef
== NULL
)
395 if ((rasterInfo
== NULL
) || !rasterInfo
->Ok())
398 if (errorStatus
== 0)
400 // build a usable bounding CGRect from the wxBitmap's bounds wxRect
401 widthV
= rasterInfo
->GetWidth();
402 heightV
= rasterInfo
->GetHeight();
403 if ((widthV
<= 0) || (heightV
<= 0))
407 if (errorStatus
== 0)
409 depthV
= rasterInfo
->GetDepth();
410 // isColored = (depthV > 1);
412 // FIXME: this is often <= 0 - why???
414 // errorStatus = (-4);
417 if (errorStatus
== 0)
419 imageRef
= (CGImageRef
)(rasterInfo
->CGImageCreate());
420 if (imageRef
== NULL
)
424 if (errorStatus
== 0)
426 // FIXME: switch when this routine belongs to a DC class...
427 boundsR
= CGRectMake( 0.0, 0.0, (float)widthV
, (float)heightV
);
428 // boundsR = CGRectMake( 0.0, 0.0, (float)XLOG2DEVREL( widthV ), (float)XLOG2DEVREL( heightV ) );
430 *patternRef
= CGPatternCreate(
433 CGAffineTransformIdentity
,
436 kCGPatternTilingNoDistortion
,
438 &sImagePatternCallback
);
440 if (*patternRef
== (CGPatternRef
)NULL
)
447 long CreatePatternFromDashes(
448 CGPatternRef
*patternRef
,
449 const wxDash
*sourceDash
,
455 if (patternRef
== NULL
)
459 if ((sourceDash
== NULL
) || (count
<= 0))
462 wxBitmap
dashBits( (char*)sourceDash
, 8, count
, 1 );
463 errorStatus
= CreatePatternFromBitmap( patternRef
, &dashBits
, useMultibit
);
468 long CreatePatternFromBrush(
469 CGPatternRef
*patternRef
,
470 const wxBrush
&sourceBrush
,
475 if (patternRef
== NULL
)
479 errorStatus
= CreatePatternFromBitmap( patternRef
, sourceBrush
.GetStipple(), useMultibit
);
484 long CreatePatternFromPen(
485 CGPatternRef
*patternRef
,
486 const wxPen
&sourcePen
,
491 if (patternRef
== NULL
)
495 errorStatus
= CreatePatternFromBitmap( patternRef
, sourcePen
.GetStipple(), useMultibit
);
503 // FIXME: the NEW_GC_SUPPORT part this routine is unfinished and needs lots of work !!
505 void wxMacCGContext::SetPen( const wxPen
&pen
)
508 if ( m_cgContext
== NULL
)
510 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
511 bool stroke
= pen
.GetStyle() != wxTRANSPARENT
;
514 // we can benchmark performance, should go into a setting later
515 CGContextSetShouldAntialias( m_cgContext
, false ) ;
520 m_mode
= kCGPathFill
; // just a default
524 m_mode
= kCGPathFill
;
528 #if defined(_NEW_GC_SUPPORT_)
531 CGPatternRef patternRef
;
534 bool hasSetPattern
, useMultibit
;
536 hasSetPattern
= false;
538 result
= CreatePatternFromPen( &patternRef
, pen
, useMultibit
);
541 EstablishPatternColorSpace( m_cgContext
, useMultibit
, false );
544 CGContextSetStrokePattern( m_cgContext
, patternRef
, alphaArray
);
545 CGPatternRelease( patternRef
);
547 hasSetPattern
= true;
549 //wxLogDebug( wxT("CreatePatternFromPen succeeded!") );
552 // NB: the (-2) result is from wxPen instances that don't have a stipple wxBitmap
554 wxLogDebug( wxT("CreatePatternFromPen failed: result [%ld]"), result
);
561 col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() );
563 GetThemeBrushAsColor( pen
.MacGetTheme(), 32, true, &col
);
566 CGContextSetRGBStrokeColor(
567 m_cgContext
, (float) col
.red
/ 65536.0,
568 (float) col
.green
/ 65536.0, (float) col
.blue
/ 65536.0, 1.0 );
573 // original implementation
574 RGBColor col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() ) ;
575 CGContextSetRGBStrokeColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
579 switch( pen
.GetCap() )
582 cap
= kCGLineCapRound
;
584 case wxCAP_PROJECTING
:
585 cap
= kCGLineCapSquare
;
588 cap
= kCGLineCapButt
;
591 cap
= kCGLineCapButt
;
594 CGContextSetLineCap( m_cgContext
, cap
) ;
597 switch( pen
.GetJoin() )
600 join
= kCGLineJoinBevel
;
603 join
= kCGLineJoinMiter
;
606 join
= kCGLineJoinRound
;
609 join
= kCGLineJoinMiter
;
612 CGContextSetLineJoin( m_cgContext
, join
) ;
614 /* TODO * m_dc->m_scaleX */
615 float penWidth
= pen
.GetWidth();
618 CGContextSetLineWidth( m_cgContext
, penWidth
) ;
620 m_mode
= kCGPathStroke
;
623 #if defined(_NEW_GC_DASHES_)
624 const char *dashData
= NULL
;
625 char *userDashData
= NULL
;
628 const char dotted
[] = { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };
629 const char dashed
[] = { 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00 };
630 const char short_dashed
[] = { 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 };
631 const char dotted_dashed
[] = { 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x00 };
633 switch (pen
.GetStyle())
636 // default, undashed pen
641 count
= WXSIZEOF(dotted
);
645 count
= WXSIZEOF(dashed
);
648 dashData
= short_dashed
;
649 count
= WXSIZEOF(short_dashed
);
652 dashData
= dotted_dashed
;
653 count
= WXSIZEOF(dotted_dashed
);
656 count
= pen
.GetDashes( (wxDash
**)&userDashData
);
657 dashData
= userDashData
;
664 if ((dashData
!= NULL
) && (count
> 0))
666 CGPatternRef patternRef
;
672 result
= CreatePatternFromDashes( &patternRef
, (const wxDash
*)dashData
, count
, useMultibit
);
675 col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() );
676 CGContextSetRGBStrokeColor(
677 m_cgContext
, (float) col
.red
/ 65536.0,
678 (float) col
.green
/ 65536.0, (float) col
.blue
/ 65536.0, 1.0 );
680 EstablishPatternColorSpace( m_cgContext
, useMultibit
, false );
683 CGContextSetStrokePattern( m_cgContext
, patternRef
, alphaArray
);
684 CGPatternRelease( patternRef
);
688 wxLogDebug( wxT("CreatePatternFromDashes failed: result [%ld]"), result
);
691 const float *lengths
= NULL
;
692 float *userLengths
= NULL
;
694 const float dotted
[] = { 3 , 3 };
695 const float dashed
[] = { 19 , 9 };
696 const float short_dashed
[] = { 9 , 6 };
697 const float dotted_dashed
[] = { 9 , 6 , 3 , 3 };
699 switch( pen
.GetStyle() )
705 count
= WXSIZEOF(dotted
);
709 count
= WXSIZEOF(dashed
) ;
712 lengths
= short_dashed
;
713 count
= WXSIZEOF(short_dashed
) ;
716 lengths
= dotted_dashed
;
717 count
= WXSIZEOF(dotted_dashed
);
721 count
= pen
.GetDashes( &dashes
) ;
722 if ((dashes
!= NULL
) && (count
> 0))
724 userLengths
= new float[count
] ;
725 for( int i
= 0 ; i
< count
; ++i
)
727 userLengths
[i
] = (float)dashes
[i
] ;
728 if (userLengths
[i
] <= 0.0)
730 userLengths
[i
] = 1.0;
731 // wxLogDebug( wxT("wxMacCGContext::SetPen - bad dash length[%d] [%.2f]"), i, (float)dashes[i] );
735 lengths
= userLengths
;
741 if ((lengths
!= NULL
) && (count
> 0))
743 // we need to change the cap, otherwise everything overlaps
744 // and we get solid lines
745 CGContextSetLineDash( m_cgContext
, 0 , lengths
, count
) ;
746 CGContextSetLineCap( m_cgContext
, kCGLineCapButt
) ;
750 CGContextSetLineDash( m_cgContext
, 0 , NULL
, 0 ) ;
753 delete[] userLengths
;
756 if ( fill
&& stroke
)
758 m_mode
= kCGPathFillStroke
;
763 void wxMacCGContext::SetBrush( const wxBrush
&brush
)
766 if ( m_cgContext
== NULL
)
769 bool fill
= brush
.GetStyle() != wxTRANSPARENT
;
770 bool stroke
= m_pen
.GetStyle() != wxTRANSPARENT
;
773 // we can benchmark performance, should go into a setting later
774 CGContextSetShouldAntialias( m_cgContext
, false ) ;
780 m_mode
= kCGPathFill
; // just a default
784 #if defined(_NEW_GC_SUPPORT_)
787 CGPatternRef patternRef
;
790 bool hasSetPattern
, useMultibit
;
792 hasSetPattern
= false;
794 result
= CreatePatternFromBrush( &patternRef
, brush
, useMultibit
);
797 EstablishPatternColorSpace( m_cgContext
, useMultibit
, true );
800 CGContextSetFillPattern( m_cgContext
, patternRef
, alphaArray
);
801 CGPatternRelease( patternRef
);
803 hasSetPattern
= true;
805 //wxLogDebug( wxT("CreatePatternFromBrush succeeded!") );
808 // NB: the (-2) result is from wxBrush instances that don't have a stipple wxBitmap
810 wxLogDebug( wxT("CreatePatternFromBrush failed: result [%ld]"), result
);
817 col
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() );
819 GetThemeBrushAsColor( brush
.MacGetTheme(), 32, true, &col
);
822 CGContextSetRGBFillColor(
823 m_cgContext
, (float) col
.red
/ 65536.0,
824 (float) col
.green
/ 65536.0, (float) col
.blue
/ 65536.0, 1.0 );
829 // original implementation
830 RGBColor col
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() ) ;
831 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
834 m_mode
= kCGPathFill
;
838 m_mode
= kCGPathStroke
;
840 if ( fill
&& stroke
)
842 m_mode
= kCGPathFillStroke
;
847 void AddEllipticArcToPath(CGContextRef c
, CGPoint center
, float a
, float b
, float fromDegree
, float toDegree
)
849 CGContextSaveGState(c
);
850 CGContextTranslateCTM(c
, center
.x
, center
.y
);
851 CGContextScaleCTM(c
, a
, b
);
852 CGContextMoveToPoint(c
, 1, 0);
853 CGContextAddArc(c
, 0, 0, 1, DegToRad(fromDegree
), DegToRad(toDegree
), 0);
854 CGContextClosePath(c
);
855 CGContextRestoreGState(c
);
858 void AddRoundedRectToPath(CGContextRef c
, CGRect rect
, float ovalWidth
,
862 if (ovalWidth
== 0 || ovalHeight
== 0)
864 CGContextAddRect(c
, rect
);
867 CGContextSaveGState(c
);
868 CGContextTranslateCTM(c
, CGRectGetMinX(rect
), CGRectGetMinY(rect
));
869 CGContextScaleCTM(c
, ovalWidth
, ovalHeight
);
870 fw
= CGRectGetWidth(rect
) / ovalWidth
;
871 fh
= CGRectGetHeight(rect
) / ovalHeight
;
872 CGContextMoveToPoint(c
, fw
, fh
/2);
873 CGContextAddArcToPoint(c
, fw
, fh
, fw
/2, fh
, 1);
874 CGContextAddArcToPoint(c
, 0, fh
, 0, fh
/2, 1);
875 CGContextAddArcToPoint(c
, 0, 0, fw
/2, 0, 1);
876 CGContextAddArcToPoint(c
, fw
, 0, fw
, fh
/2, 1);
877 CGContextClosePath(c
);
878 CGContextRestoreGState(c
);
885 m_mm_to_pix_x
= mm2pt
;
886 m_mm_to_pix_y
= mm2pt
;
887 m_internalDeviceOriginX
= 0;
888 m_internalDeviceOriginY
= 0;
889 m_externalDeviceOriginX
= 0;
890 m_externalDeviceOriginY
= 0;
891 m_logicalScaleX
= 1.0;
892 m_logicalScaleY
= 1.0;
897 m_needComputeScaleX
= FALSE
;
898 m_needComputeScaleY
= FALSE
;
902 m_macLocalOrigin
.x
= m_macLocalOrigin
.y
= 0 ;
904 m_pen
= *wxBLACK_PEN
;
905 m_font
= *wxNORMAL_FONT
;
906 m_brush
= *wxWHITE_BRUSH
;
908 m_macATSUIStyle
= NULL
;
910 m_graphicContext
= NULL
;
915 if( m_macATSUIStyle
)
917 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
918 m_macATSUIStyle
= NULL
;
921 delete m_graphicContext
;
924 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
926 wxCHECK_RET( Ok(), wxT("invalid window dc") );
927 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
928 wxCoord xx
= XLOG2DEVMAC(x
);
929 wxCoord yy
= YLOG2DEVMAC(y
);
930 wxCoord w
= bmp
.GetWidth();
931 wxCoord h
= bmp
.GetHeight();
932 wxCoord ww
= XLOG2DEVREL(w
);
933 wxCoord hh
= YLOG2DEVREL(h
);
935 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
936 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() ) ;
937 HIRect r
= CGRectMake( xx
, yy
, ww
, hh
) ;
938 HIViewDrawCGImage( cg
, &r
, image
) ;
939 CGImageRelease( image
) ;
942 void wxDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
944 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
945 wxCHECK_RET(icon
.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
947 wxCoord xx
= XLOG2DEVMAC(x
);
948 wxCoord yy
= YLOG2DEVMAC(y
);
949 wxCoord w
= icon
.GetWidth();
950 wxCoord h
= icon
.GetHeight();
951 wxCoord ww
= XLOG2DEVREL(w
);
952 wxCoord hh
= YLOG2DEVREL(h
);
954 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
955 CGRect r
= CGRectMake( 00 , 00 , ww
, hh
) ;
956 CGContextSaveGState(cg
);
957 CGContextTranslateCTM(cg
, xx
, yy
+ hh
);
958 CGContextScaleCTM(cg
, 1, -1);
959 PlotIconRefInContext( cg
, &r
, kAlignNone
, kTransformNone
,
960 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) ) ;
961 CGContextRestoreGState( cg
) ;
964 void wxDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
966 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
967 wxCoord xx
, yy
, ww
, hh
;
970 ww
= XLOG2DEVREL(width
);
971 hh
= YLOG2DEVREL(height
);
973 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
974 CGRect clipRect
= CGRectMake( xx
,yy
, ww
, hh
) ;
975 CGContextClipToRect( cgContext
, clipRect
) ;
977 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
978 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
981 m_clipX1
= wxMax( m_clipX1
, xx
);
982 m_clipY1
= wxMax( m_clipY1
, yy
);
983 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
984 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
994 // TODO as soon as we don't reset the context for each operation anymore
995 // we have to update the context as well
998 void wxDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
1000 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
1003 DestroyClippingRegion();
1007 region
.GetBox( x
, y
, w
, h
);
1008 wxCoord xx
, yy
, ww
, hh
;
1009 xx
= XLOG2DEVMAC(x
);
1010 yy
= YLOG2DEVMAC(y
);
1011 ww
= XLOG2DEVREL(w
);
1012 hh
= YLOG2DEVREL(h
);
1013 // if we have a scaling that we cannot map onto native regions
1014 // we must use the box
1015 if ( ww
!= w
|| hh
!= h
)
1017 wxDC::DoSetClippingRegion( x
, y
, w
, h
);
1022 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
1023 if ( xx != x || yy != y )
1025 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
1027 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
1031 m_clipX1
= wxMax( m_clipX1
, xx
);
1032 m_clipY1
= wxMax( m_clipY1
, yy
);
1033 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
1034 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
1047 void wxDC::DestroyClippingRegion()
1049 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
1050 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1051 CGContextRestoreGState( cgContext
);
1052 CGContextSaveGState( cgContext
);
1053 m_graphicContext
->SetPen( m_pen
) ;
1054 m_graphicContext
->SetBrush( m_brush
) ;
1058 void wxDC::DoGetSizeMM( int* width
, int* height
) const
1063 *width
= long( double(w
) / (m_scaleX
*m_mm_to_pix_x
) );
1064 *height
= long( double(h
) / (m_scaleY
*m_mm_to_pix_y
) );
1067 void wxDC::SetTextForeground( const wxColour
&col
)
1069 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1070 if ( col
!= m_textForegroundColour
)
1072 m_textForegroundColour
= col
;
1077 void wxDC::SetTextBackground( const wxColour
&col
)
1079 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1080 m_textBackgroundColour
= col
;
1083 void wxDC::SetMapMode( int mode
)
1088 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
1091 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
1094 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
1097 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
1101 SetLogicalScale( 1.0, 1.0 );
1104 if (mode
!= wxMM_TEXT
)
1106 m_needComputeScaleX
= TRUE
;
1107 m_needComputeScaleY
= TRUE
;
1111 void wxDC::SetUserScale( double x
, double y
)
1113 // allow negative ? -> no
1116 ComputeScaleAndOrigin();
1119 void wxDC::SetLogicalScale( double x
, double y
)
1122 m_logicalScaleX
= x
;
1123 m_logicalScaleY
= y
;
1124 ComputeScaleAndOrigin();
1127 void wxDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
1129 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
1130 m_logicalOriginY
= y
* m_signY
;
1131 ComputeScaleAndOrigin();
1134 void wxDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
1136 m_externalDeviceOriginX
= x
;
1137 m_externalDeviceOriginY
= y
;
1138 ComputeScaleAndOrigin();
1141 void wxDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1143 m_signX
= (xLeftRight
? 1 : -1);
1144 m_signY
= (yBottomUp
? -1 : 1);
1145 ComputeScaleAndOrigin();
1148 wxSize
wxDC::GetPPI() const
1150 return wxSize(72, 72);
1153 int wxDC::GetDepth() const
1158 void wxDC::ComputeScaleAndOrigin()
1160 // CMB: copy scale to see if it changes
1161 double origScaleX
= m_scaleX
;
1162 double origScaleY
= m_scaleY
;
1163 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
1164 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
1165 m_deviceOriginX
= m_internalDeviceOriginX
+ m_externalDeviceOriginX
;
1166 m_deviceOriginY
= m_internalDeviceOriginY
+ m_externalDeviceOriginY
;
1167 // CMB: if scale has changed call SetPen to recalulate the line width
1168 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
1170 // this is a bit artificial, but we need to force wxDC to think
1171 // the pen has changed
1172 wxPen
pen(GetPen());
1178 void wxDC::SetPalette( const wxPalette
& palette
)
1182 void wxDC::SetBackgroundMode( int mode
)
1184 m_backgroundMode
= mode
;
1187 void wxDC::SetFont( const wxFont
&font
)
1193 void wxDC::SetPen( const wxPen
&pen
)
1198 if ( m_graphicContext
)
1200 m_graphicContext
->SetPen( m_pen
) ;
1204 void wxDC::SetBrush( const wxBrush
&brush
)
1206 if (m_brush
== brush
)
1209 if ( m_graphicContext
)
1211 m_graphicContext
->SetBrush( m_brush
) ;
1215 void wxDC::SetBackground( const wxBrush
&brush
)
1217 if (m_backgroundBrush
== brush
)
1219 m_backgroundBrush
= brush
;
1220 if (!m_backgroundBrush
.Ok())
1224 void wxDC::SetLogicalFunction( int function
)
1226 if (m_logicalFunction
== function
)
1228 m_logicalFunction
= function
;
1231 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
1232 const wxColour
& col
, int style
);
1234 bool wxDC::DoFloodFill(wxCoord x
, wxCoord y
,
1235 const wxColour
& col
, int style
)
1237 return wxDoFloodFill(this, x
, y
, col
, style
);
1240 bool wxDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
1242 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
1243 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
1244 wxMacPortSaver
helper((CGrafPtr
)m_macPort
) ;
1247 XLOG2DEVMAC(x
) + m_macLocalOriginInPort
.x
- m_macLocalOrigin
.x
,
1248 YLOG2DEVMAC(y
) + m_macLocalOriginInPort
.y
- m_macLocalOrigin
.y
, &colour
);
1249 // Convert from Mac colour to wx
1250 col
->Set( colour
.red
>> 8,
1256 void wxDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
1258 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1260 if ( m_logicalFunction
!= wxCOPY
)
1263 wxCoord xx1
= XLOG2DEVMAC(x1
) ;
1264 wxCoord yy1
= YLOG2DEVMAC(y1
) ;
1265 wxCoord xx2
= XLOG2DEVMAC(x2
) ;
1266 wxCoord yy2
= YLOG2DEVMAC(y2
) ;
1268 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1269 path
->MoveToPoint( xx1
, yy1
) ;
1270 path
->AddLineToPoint( xx2
, yy2
) ;
1271 path
->CloseSubpath() ;
1272 m_graphicContext
->StrokePath( path
) ;
1275 CalcBoundingBox(x1
, y1
);
1276 CalcBoundingBox(x2
, y2
);
1279 void wxDC::DoCrossHair( wxCoord x
, wxCoord y
)
1281 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
1283 if ( m_logicalFunction
!= wxCOPY
)
1289 wxCoord xx
= XLOG2DEVMAC(x
);
1290 wxCoord yy
= YLOG2DEVMAC(y
);
1292 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1293 path
->MoveToPoint( XLOG2DEVMAC(0), yy
) ;
1294 path
->AddLineToPoint( XLOG2DEVMAC(w
), yy
) ;
1295 path
->CloseSubpath() ;
1296 path
->MoveToPoint( xx
, YLOG2DEVMAC(0) ) ;
1297 path
->AddLineToPoint( xx
, YLOG2DEVMAC(h
) ) ;
1298 path
->CloseSubpath() ;
1299 m_graphicContext
->StrokePath( path
) ;
1302 CalcBoundingBox(x
, y
);
1303 CalcBoundingBox(x
+w
, y
+h
);
1306 void wxDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
1307 wxCoord x2
, wxCoord y2
,
1308 wxCoord xc
, wxCoord yc
)
1310 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
1312 if ( m_logicalFunction
!= wxCOPY
)
1315 wxCoord xx1
= XLOG2DEVMAC(x1
);
1316 wxCoord yy1
= YLOG2DEVMAC(y1
);
1317 wxCoord xx2
= XLOG2DEVMAC(x2
);
1318 wxCoord yy2
= YLOG2DEVMAC(y2
);
1319 wxCoord xxc
= XLOG2DEVMAC(xc
);
1320 wxCoord yyc
= YLOG2DEVMAC(yc
);
1321 double dx
= xx1
- xxc
;
1322 double dy
= yy1
- yyc
;
1323 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
1324 wxCoord rad
= (wxCoord
)radius
;
1326 if (xx1
== xx2
&& yy1
== yy2
)
1331 else if (radius
== 0.0)
1337 sa
= (xx1
- xxc
== 0) ?
1338 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
1339 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
1340 ea
= (xx2
- xxc
== 0) ?
1341 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
1342 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
1345 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
1346 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1347 CGContextRef ctx
= mctx
->GetNativeContext() ;
1348 CGContextSaveGState( ctx
) ;
1349 CGContextTranslateCTM( ctx
, xxc
, yyc
);
1350 CGContextScaleCTM( ctx
, 1 , -1 ) ;
1352 CGContextMoveToPoint( ctx
, 0 , 0 ) ;
1353 CGContextAddArc( ctx
, 0, 0 , rad
, DegToRad(sa
), DegToRad(ea
), 0);
1355 CGContextAddLineToPoint( ctx
, 0 , 0 ) ;
1356 CGContextRestoreGState( ctx
) ;
1357 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1360 void wxDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
1361 double sa
, double ea
)
1363 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
1365 if ( m_logicalFunction
!= wxCOPY
)
1368 wxCoord xx
= XLOG2DEVMAC(x
);
1369 wxCoord yy
= YLOG2DEVMAC(y
);
1370 wxCoord ww
= m_signX
* XLOG2DEVREL(w
);
1371 wxCoord hh
= m_signY
* YLOG2DEVREL(h
);
1372 // handle -ve width and/or height
1373 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
1374 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
1376 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
1378 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1379 CGContextRef ctx
= mctx
->GetNativeContext() ;
1381 CGContextSaveGState( ctx
) ;
1382 CGContextTranslateCTM( ctx
, xx
+ ww
/ 2, yy
+ hh
/ 2);
1383 CGContextScaleCTM( ctx
, 1 * ww
/ 2 , -1 * hh
/ 2 ) ;
1385 CGContextMoveToPoint( ctx
, 0 , 0 ) ;
1386 CGContextAddArc( ctx
, 0, 0, 1, DegToRad(sa
), DegToRad(ea
), 0);
1388 CGContextAddLineToPoint( ctx
, 0 , 0 ) ;
1389 CGContextRestoreGState( ctx
) ;
1390 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1393 void wxDC::DoDrawPoint( wxCoord x
, wxCoord y
)
1395 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1396 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 ) ;
1399 void wxDC::DoDrawLines(int n
, wxPoint points
[],
1400 wxCoord xoffset
, wxCoord yoffset
)
1402 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1404 if ( m_logicalFunction
!= wxCOPY
)
1407 wxCoord x1
, x2
, y1
, y2
;
1408 x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1409 y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1410 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1411 path
->MoveToPoint( x1
, y1
) ;
1412 for (int i
= 1; i
< n
; i
++)
1414 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1415 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1417 path
->AddLineToPoint( x2
, y2
) ;
1419 m_graphicContext
->StrokePath( path
) ;
1423 void wxDC::DoDrawPolygon(int n
, wxPoint points
[],
1424 wxCoord xoffset
, wxCoord yoffset
,
1427 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1428 wxCoord x1
, x2
, y1
, y2
;
1429 if ( n
== 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
1432 if ( m_logicalFunction
!= wxCOPY
)
1435 x2
= x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1436 y2
= y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1438 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1439 path
->MoveToPoint( x1
, y1
) ;
1440 for (int i
= 1; i
< n
; i
++)
1442 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1443 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1445 path
->AddLineToPoint( x2
, y2
) ;
1447 if ( x1
!= x2
|| y1
!= y2
)
1449 path
->AddLineToPoint( x1
,y1
) ;
1451 path
->CloseSubpath() ;
1452 m_graphicContext
->DrawPath( path
, fillStyle
) ;
1456 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1458 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1460 if ( m_logicalFunction
!= wxCOPY
)
1463 wxCoord xx
= XLOG2DEVMAC(x
);
1464 wxCoord yy
= YLOG2DEVMAC(y
);
1465 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1466 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1467 // CMB: draw nothing if transformed w or h is 0
1468 if (ww
== 0 || hh
== 0)
1470 // CMB: handle -ve width and/or height
1481 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1482 path
->AddRectangle(xx
,yy
, ww
, hh
) ;
1483 m_graphicContext
->DrawPath( path
) ;
1487 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1488 wxCoord width
, wxCoord height
,
1491 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1493 if ( m_logicalFunction
!= wxCOPY
)
1498 radius
= - radius
* ((width
< height
) ? width
: height
);
1499 wxCoord xx
= XLOG2DEVMAC(x
);
1500 wxCoord yy
= YLOG2DEVMAC(y
);
1501 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1502 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1503 // CMB: draw nothing if transformed w or h is 0
1504 if (ww
== 0 || hh
== 0)
1506 // CMB: handle -ve width and/or height
1517 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1518 CGContextRef ctx
= mctx
->GetNativeContext() ;
1519 AddRoundedRectToPath( ctx
, CGRectMake( xx
, yy
, ww
, hh
) , 16 ,16 ) ;
1520 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1523 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1525 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1527 if ( m_logicalFunction
!= wxCOPY
)
1530 wxCoord xx
= XLOG2DEVMAC(x
);
1531 wxCoord yy
= YLOG2DEVMAC(y
);
1532 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1533 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1534 // CMB: draw nothing if transformed w or h is 0
1535 if (ww
== 0 || hh
== 0)
1537 // CMB: handle -ve width and/or height
1549 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1550 CGContextRef ctx
= mctx
->GetNativeContext() ;
1551 CGContextSaveGState( ctx
) ;
1552 CGContextTranslateCTM( ctx
, xx
+ ww
/ 2, yy
+ hh
/ 2);
1553 CGContextScaleCTM( ctx
, ww
/ 2 , hh
/ 2 ) ;
1554 CGContextAddArc( ctx
, 0, 0, 1, 0 , 2*M_PI
, 0);
1555 CGContextRestoreGState( ctx
) ;
1556 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1559 bool wxDC::CanDrawBitmap(void) const
1564 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1565 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1566 wxCoord xsrcMask
, wxCoord ysrcMask
)
1568 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
1569 wxCHECK_MSG(source
->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
1570 if ( logical_func
== wxNO_OP
)
1572 if (xsrcMask
== -1 && ysrcMask
== -1)
1574 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1577 wxCoord yysrc
= source
->YLOG2DEVMAC(ysrc
) ;
1578 wxCoord xxsrc
= source
->XLOG2DEVMAC(xsrc
) ;
1579 wxCoord wwsrc
= source
->XLOG2DEVREL(width
) ;
1580 wxCoord hhsrc
= source
->YLOG2DEVREL(height
) ;
1582 wxCoord yydest
= YLOG2DEVMAC(ydest
) ;
1583 wxCoord xxdest
= XLOG2DEVMAC(xdest
) ;
1584 wxCoord wwdest
= XLOG2DEVREL(width
) ;
1585 wxCoord hhdest
= YLOG2DEVREL(height
) ;
1587 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
) ;
1588 if ( memdc
&& logical_func
== wxCOPY
)
1590 wxBitmap blit
= memdc
->GetSelectedObject() ;
1591 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") ) ;
1593 wxCoord bmpwidth
= blit
.GetWidth();
1594 wxCoord bmpheight
= blit
.GetHeight();
1596 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1598 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
) ;
1599 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
) ;
1600 if ( wwsrc
> 0 && hhsrc
> 0 )
1602 if ( xxsrc
>= 0 && yysrc
>= 0 )
1604 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
) ;
1605 blit
= blit
.GetSubBitmap( subrect
) ;
1609 // in this case we'd probably have to adjust the different coordinates, but
1610 // we have to find out proper contract first
1611 blit
= wxNullBitmap
;
1616 blit
= wxNullBitmap
;
1621 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1622 CGImageRef image
= (CGImageRef
)( blit
.CGImageCreate() ) ;
1623 HIRect r
= CGRectMake( xxdest
, yydest
, wwdest
, hhdest
) ;
1624 HIViewDrawCGImage( cg
, &r
, image
) ;
1625 CGImageRelease( image
) ;
1632 CGContextRef cg = (wxMacCGContext*)(source->GetGraphicContext())->GetNativeContext() ;
1633 void *data = CGBitmapContextGetData( cg ) ;
1635 return FALSE
; // wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ) ;
1640 void wxDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1643 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1645 if ( str
.Length() == 0 )
1648 if ( m_logicalFunction
!= wxCOPY
)
1651 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1653 OSStatus status
= noErr
;
1654 ATSUTextLayout atsuLayout
;
1655 UniCharCount chars
= str
.Length() ;
1656 UniChar
* ubuf
= NULL
;
1657 #if SIZEOF_WCHAR_T == 4
1658 wxMBConvUTF16 converter
;
1660 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1661 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1662 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1664 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1665 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1666 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1667 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1669 chars
= unicharlen
/ 2 ;
1672 ubuf
= (UniChar
*) str
.wc_str() ;
1674 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1675 chars
= wxWcslen( wchar
.data() ) ;
1676 ubuf
= (UniChar
*) wchar
.data() ;
1680 int drawX
= XLOG2DEVMAC(x
) ;
1681 int drawY
= YLOG2DEVMAC(y
) ;
1683 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1684 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1686 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1688 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true ) ;
1689 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1691 int iAngle
= int( angle
);
1692 if ( abs(iAngle
) > 0 )
1694 Fixed atsuAngle
= IntToFixed( iAngle
) ;
1695 ATSUAttributeTag atsuTags
[] =
1697 kATSULineRotationTag
,
1699 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1703 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1707 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1708 atsuTags
, atsuSizes
, atsuValues
) ;
1711 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1712 ATSUAttributeTag atsuTags
[] =
1716 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1718 sizeof( CGContextRef
) ,
1720 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1724 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1725 atsuTags
, atsuSizes
, atsuValues
) ;
1728 ATSUTextMeasurement textBefore
;
1729 ATSUTextMeasurement textAfter
;
1730 ATSUTextMeasurement ascent
;
1731 ATSUTextMeasurement descent
;
1733 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1734 &textBefore
, &textAfter
, &ascent
, &descent
);
1735 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1739 if ( m_backgroundMode
== wxSOLID
)
1741 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1745 path
->AddLineToPoint(
1746 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ,
1747 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ) ;
1748 path
->AddLineToPoint(
1749 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) + cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1750 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) - sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1751 path
->AddLineToPoint(
1752 (int) (drawX
+ cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1753 (int) (drawY
- sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1755 m_graphicContext
->FillPath( path
, m_textBackgroundColour
) ;
1759 drawX
+= (int)(sin(angle
/RAD2DEG
) * FixedToInt(ascent
));
1760 drawY
+= (int)(cos(angle
/RAD2DEG
) * FixedToInt(ascent
));
1762 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1763 IntToFixed(drawX
) , IntToFixed(drawY
) , &rect
);
1764 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1766 CGContextSaveGState(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext());
1767 CGContextTranslateCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), drawX
, drawY
);
1768 CGContextScaleCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), 1, -1);
1769 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1770 IntToFixed(0) , IntToFixed(0) );
1771 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1772 CGContextRestoreGState( ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ) ;
1774 CalcBoundingBox(XDEV2LOG(rect
.left
), YDEV2LOG(rect
.top
) );
1775 CalcBoundingBox(XDEV2LOG(rect
.right
), YDEV2LOG(rect
.bottom
) );
1777 ::ATSUDisposeTextLayout(atsuLayout
);
1778 #if SIZEOF_WCHAR_T == 4
1783 void wxDC::DoDrawText(const wxString
& strtext
, wxCoord x
, wxCoord y
)
1785 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1786 DoDrawRotatedText( strtext
, x
, y
, 0.0 ) ;
1789 bool wxDC::CanGetTextExtent() const
1791 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1795 void wxDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1796 wxCoord
*descent
, wxCoord
*externalLeading
,
1797 wxFont
*theFont
) const
1799 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1800 wxFont formerFont
= m_font
;
1803 // work around the constness
1804 *((wxFont
*)(&m_font
)) = *theFont
;
1808 if ( str
.Length() == 0 )
1811 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1813 OSStatus status
= noErr
;
1814 ATSUTextLayout atsuLayout
;
1815 UniCharCount chars
= str
.Length() ;
1816 UniChar
* ubuf
= NULL
;
1817 #if SIZEOF_WCHAR_T == 4
1818 wxMBConvUTF16 converter
;
1820 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1821 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1822 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1824 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1825 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1826 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1827 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1829 chars
= unicharlen
/ 2 ;
1832 ubuf
= (UniChar
*) str
.wc_str() ;
1834 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1835 chars
= wxWcslen( wchar
.data() ) ;
1836 ubuf
= (UniChar
*) wchar
.data() ;
1841 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1842 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1844 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1846 ATSUTextMeasurement textBefore
;
1847 ATSUTextMeasurement textAfter
;
1848 ATSUTextMeasurement textAscent
;
1849 ATSUTextMeasurement textDescent
;
1851 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1852 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1855 *height
= YDEV2LOGREL( FixedToInt(textAscent
+ textDescent
) ) ;
1857 *descent
=YDEV2LOGREL( FixedToInt(textDescent
) );
1858 if ( externalLeading
)
1859 *externalLeading
= 0 ;
1861 *width
= XDEV2LOGREL( FixedToInt(textAfter
- textBefore
) ) ;
1863 ::ATSUDisposeTextLayout(atsuLayout
);
1864 #if SIZEOF_WCHAR_T == 4
1869 // work around the constness
1870 *((wxFont
*)(&m_font
)) = formerFont
;
1876 bool wxDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1878 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1881 widths
.Add(0, text
.Length());
1883 if (text
.Length() == 0)
1886 ATSUTextLayout atsuLayout
;
1887 UniCharCount chars
= text
.Length() ;
1888 UniChar
* ubuf
= NULL
;
1889 #if SIZEOF_WCHAR_T == 4
1890 wxMBConvUTF16 converter
;
1892 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 ) ;
1893 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1894 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 ) ;
1896 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1897 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1898 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1899 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1901 chars
= unicharlen
/ 2 ;
1904 ubuf
= (UniChar
*) text
.wc_str() ;
1906 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1907 chars
= wxWcslen( wchar
.data() ) ;
1908 ubuf
= (UniChar
*) wchar
.data() ;
1913 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1914 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1916 for ( int pos
= 0; pos
< (int)chars
; pos
++ ) {
1917 unsigned long actualNumberOfBounds
= 0;
1918 ATSTrapezoid glyphBounds
;
1920 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1922 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1, kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1923 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1928 widths
[pos
] = XDEV2LOGREL(FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
));
1929 //unsigned char uch = s[i];
1932 ::ATSUDisposeTextLayout(atsuLayout
);
1936 wxCoord
wxDC::GetCharWidth(void) const
1939 DoGetTextExtent(wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
) ;
1943 wxCoord
wxDC::GetCharHeight(void) const
1946 DoGetTextExtent(wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
) ;
1950 void wxDC::Clear(void)
1952 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1954 if ( m_backgroundBrush
.Ok() && m_backgroundBrush
.GetStyle() != wxTRANSPARENT
)
1956 HIRect rect
= CGRectMake( -10000 , -10000 , 20000 , 20000 ) ;
1957 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1958 switch( m_backgroundBrush
.MacGetBrushKind() )
1960 case kwxMacBrushTheme
:
1962 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1963 if ( HIThemeSetFill
!= 0 )
1965 HIThemeSetFill( m_backgroundBrush
.MacGetTheme(), NULL
, cg
, kHIThemeOrientationNormal
);
1966 CGContextFillRect(cg
, rect
);
1973 GetThemeBrushAsColor( m_backgroundBrush
.MacGetTheme(), 32, true, &color
);
1974 CGContextSetRGBFillColor( cg
, (float) color
.red
/ 65536,
1975 (float) color
.green
/ 65536, (float) color
.blue
/ 65536, 1 );
1976 CGContextFillRect( cg
, rect
);
1979 // reset to normal value
1980 RGBColor col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
1981 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0, col
.green
/ 65536.0, col
.blue
/ 65536.0, 1.0 );
1984 case kwxMacBrushThemeBackground
:
1986 wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ;
1987 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1988 if ( UMAGetSystemVersion() >= 0x1030 )
1990 HIThemeBackgroundDrawInfo drawInfo
;
1991 drawInfo
.version
= 0 ;
1992 drawInfo
.state
= kThemeStateActive
;
1993 drawInfo
.kind
= m_backgroundBrush
.MacGetThemeBackground(NULL
) ;
1994 if ( drawInfo
.kind
== kThemeBackgroundMetal
)
1995 HIThemeDrawBackground( &rect
, &drawInfo
, cg
,
1996 kHIThemeOrientationNormal
) ;
1997 HIThemeApplyBackground( &rect
, &drawInfo
, cg
,
1998 kHIThemeOrientationNormal
) ;
2006 case kwxMacBrushColour
:
2008 RGBColor col
= MAC_WXCOLORREF( m_backgroundBrush
.GetColour().GetPixel()) ;
2009 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
2010 CGContextFillRect(cg
, rect
);
2012 // reset to normal value
2013 col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
2014 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
2021 void wxDC::MacInstallFont() const
2023 wxCHECK_RET(Ok(), wxT("Invalid DC"));
2025 if( m_macATSUIStyle
)
2027 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
2028 m_macATSUIStyle
= NULL
;
2033 OSStatus status
= noErr
;
2034 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) m_font
.MacGetATSUStyle() , (ATSUStyle
*) &m_macATSUIStyle
) ;
2035 wxASSERT_MSG( status
== noErr
, wxT("couldn't set create ATSU style") ) ;
2037 Fixed atsuSize
= IntToFixed( int(m_scaleY
* m_font
.MacGetFontSize()) ) ;
2038 RGBColor atsuColor
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel() ) ;
2039 ATSUAttributeTag atsuTags
[] =
2044 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
2047 sizeof( RGBColor
) ,
2049 // Boolean kTrue = true ;
2050 // Boolean kFalse = false ;
2052 // ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
2053 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
2058 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUIStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
) ,
2059 atsuTags
, atsuSizes
, atsuValues
);
2061 wxASSERT_MSG( status
== noErr
, wxT("couldn't Modify ATSU style") ) ;
2065 // ---------------------------------------------------------------------------
2066 // coordinates transformations
2067 // ---------------------------------------------------------------------------
2069 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
2071 return ((wxDC
*)this)->XDEV2LOG(x
);
2074 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
2076 return ((wxDC
*)this)->YDEV2LOG(y
);
2079 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
2081 return ((wxDC
*)this)->XDEV2LOGREL(x
);
2084 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
2086 return ((wxDC
*)this)->YDEV2LOGREL(y
);
2089 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
2091 return ((wxDC
*)this)->XLOG2DEV(x
);
2094 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
2096 return ((wxDC
*)this)->YLOG2DEV(y
);
2099 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
2101 return ((wxDC
*)this)->XLOG2DEVREL(x
);
2104 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
2106 return ((wxDC
*)this)->YLOG2DEVREL(y
);
2109 #endif // wxMAC_USE_CORE_GRAPHICS