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 CGContextBeginPath( m_cgContext
) ;
223 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
224 CGContextClosePath( m_cgContext
) ;
225 CGContextStrokePath( m_cgContext
) ;
228 void wxMacCGContext::DrawPath( const wxGraphicPath
*p
, int fillStyle
)
230 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
231 CGPathDrawingMode mode
= m_mode
;
232 if ( fillStyle
== wxODDEVEN_RULE
)
234 if ( mode
== kCGPathFill
)
235 mode
= kCGPathEOFill
;
236 else if ( mode
== kCGPathFillStroke
)
237 mode
= kCGPathEOFillStroke
;
239 CGContextBeginPath( m_cgContext
) ;
240 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
241 CGContextClosePath( m_cgContext
) ;
242 CGContextDrawPath( m_cgContext
, mode
) ;
245 void wxMacCGContext::FillPath( const wxGraphicPath
*p
, const wxColor
&fillColor
, int fillStyle
)
247 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
248 CGContextSaveGState( m_cgContext
) ;
250 RGBColor col
= MAC_WXCOLORREF( fillColor
.GetPixel() ) ;
251 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
252 CGPathDrawingMode mode
= kCGPathFill
;
254 if ( fillStyle
== wxODDEVEN_RULE
)
255 mode
= kCGPathEOFill
;
257 CGContextBeginPath( m_cgContext
) ;
258 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
259 CGContextClosePath( m_cgContext
) ;
260 CGContextDrawPath( m_cgContext
, mode
) ;
262 CGContextRestoreGState( m_cgContext
) ;
265 wxGraphicPath
* wxMacCGContext::CreatePath()
267 // make sure that we now have a real cgref, before doing
268 // anything with paths
269 CGContextRef cg
= GetNativeContext() ;
271 return new wxMacCGPath() ;
274 // in case we only got a QDPort only create a cgref now
276 CGContextRef
wxMacCGContext::GetNativeContext()
278 if( m_cgContext
== NULL
)
281 GetPortBounds( (CGrafPtr
) m_qdPort
, &bounds
) ;
282 OSStatus status
= CreateCGContextForPort((CGrafPtr
) m_qdPort
, &m_cgContext
) ;
283 CGContextSaveGState( m_cgContext
) ;
285 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") ) ;
286 CGContextTranslateCTM( m_cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
287 CGContextScaleCTM( m_cgContext
, 1 , -1 ) ;
289 CGContextSaveGState( m_cgContext
) ;
291 SetBrush( m_brush
) ;
296 void wxMacCGContext::SetNativeContext( CGContextRef cg
)
298 // we allow either setting or clearing but not replacing
299 wxASSERT( m_cgContext
== NULL
|| cg
== NULL
) ;
301 CGContextSaveGState( cg
) ;
305 void wxMacCGContext::SetPen( const wxPen
&pen
)
308 if ( m_cgContext
== NULL
)
310 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
311 bool stroke
= pen
.GetStyle() != wxTRANSPARENT
;
314 // we can benchmark performance, should go into a setting later
315 CGContextSetShouldAntialias( m_cgContext
, false ) ;
320 m_mode
= kCGPathFill
; // just a default
324 m_mode
= kCGPathFill
;
328 RGBColor col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() ) ;
329 CGContextSetRGBStrokeColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
332 switch( pen
.GetCap() )
335 cap
= kCGLineCapRound
;
337 case wxCAP_PROJECTING
:
338 cap
= kCGLineCapSquare
;
341 cap
= kCGLineCapButt
;
344 cap
= kCGLineCapButt
;
347 CGContextSetLineCap( m_cgContext
, cap
) ;
350 switch( pen
.GetJoin() )
353 join
= kCGLineJoinBevel
;
356 join
= kCGLineJoinMiter
;
359 join
= kCGLineJoinRound
;
362 join
= kCGLineJoinMiter
;
365 CGContextSetLineJoin( m_cgContext
, join
) ;
367 CGContextSetLineWidth( m_cgContext
, pen
.GetWidth() == 0 ? 0.1 : pen
.GetWidth() /* TODO * m_dc->m_scaleX */ ) ;
369 m_mode
= kCGPathStroke
;
371 const float *lengths
= NULL
;
372 float *userLengths
= NULL
;
374 const float dotted
[] = { 3 , 3 };
375 const float dashed
[] = { 19 , 9 };
376 const float short_dashed
[] = { 9 , 6 };
377 const float dotted_dashed
[] = { 9 , 6 , 3 , 3 };
379 switch( pen
.GetStyle() )
385 count
= WXSIZEOF(dotted
);
389 count
= WXSIZEOF(dashed
) ;
392 lengths
= short_dashed
;
393 count
= WXSIZEOF(short_dashed
) ;
396 lengths
= dotted_dashed
;
397 count
= WXSIZEOF(dotted_dashed
);
401 count
= pen
.GetDashes( &dashes
) ;
404 userLengths
= new float[count
] ;
405 for( int i
= 0 ; i
< count
; ++i
)
406 userLengths
[i
] = dashes
[i
] ;
408 lengths
= userLengths
;
414 CGContextSetLineDash( m_cgContext
, 0 , lengths
, count
) ;
415 delete[] userLengths
;
416 // we need to change the cap, otherwise everything overlaps
417 // and we get solid lines
419 CGContextSetLineCap( m_cgContext
, kCGLineCapButt
) ;
421 if ( fill
&& stroke
)
423 m_mode
= kCGPathFillStroke
;
428 void wxMacCGContext::SetBrush( const wxBrush
&brush
)
431 if ( m_cgContext
== NULL
)
434 bool fill
= brush
.GetStyle() != wxTRANSPARENT
;
435 bool stroke
= m_pen
.GetStyle() != wxTRANSPARENT
;
438 // we can benchmark performance, should go into a setting later
439 CGContextSetShouldAntialias( m_cgContext
, false ) ;
445 m_mode
= kCGPathFill
; // just a default
449 RGBColor col
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() ) ;
450 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
451 m_mode
= kCGPathFill
;
455 m_mode
= kCGPathStroke
;
457 if ( fill
&& stroke
)
459 m_mode
= kCGPathFillStroke
;
465 void AddEllipticArcToPath(CGContextRef c
, CGPoint center
, float a
, float b
, float fromDegree
, float toDegree
)
467 CGContextSaveGState(c
);
468 CGContextTranslateCTM(c
, center
.x
, center
.y
);
469 CGContextScaleCTM(c
, a
, b
);
470 CGContextMoveToPoint(c
, 1, 0);
471 CGContextAddArc(c
, 0, 0, 1, DegToRad(fromDegree
), DegToRad(toDegree
), 0);
472 CGContextClosePath(c
);
473 CGContextRestoreGState(c
);
476 void AddRoundedRectToPath(CGContextRef c
, CGRect rect
, float ovalWidth
,
480 if (ovalWidth
== 0 || ovalHeight
== 0)
482 CGContextAddRect(c
, rect
);
485 CGContextSaveGState(c
);
486 CGContextTranslateCTM(c
, CGRectGetMinX(rect
), CGRectGetMinY(rect
));
487 CGContextScaleCTM(c
, ovalWidth
, ovalHeight
);
488 fw
= CGRectGetWidth(rect
) / ovalWidth
;
489 fh
= CGRectGetHeight(rect
) / ovalHeight
;
490 CGContextMoveToPoint(c
, fw
, fh
/2);
491 CGContextAddArcToPoint(c
, fw
, fh
, fw
/2, fh
, 1);
492 CGContextAddArcToPoint(c
, 0, fh
, 0, fh
/2, 1);
493 CGContextAddArcToPoint(c
, 0, 0, fw
/2, 0, 1);
494 CGContextAddArcToPoint(c
, fw
, 0, fw
, fh
/2, 1);
495 CGContextClosePath(c
);
496 CGContextRestoreGState(c
);
503 m_mm_to_pix_x
= mm2pt
;
504 m_mm_to_pix_y
= mm2pt
;
505 m_internalDeviceOriginX
= 0;
506 m_internalDeviceOriginY
= 0;
507 m_externalDeviceOriginX
= 0;
508 m_externalDeviceOriginY
= 0;
509 m_logicalScaleX
= 1.0;
510 m_logicalScaleY
= 1.0;
515 m_needComputeScaleX
= FALSE
;
516 m_needComputeScaleY
= FALSE
;
520 m_macLocalOrigin
.x
= m_macLocalOrigin
.y
= 0 ;
522 m_pen
= *wxBLACK_PEN
;
523 m_font
= *wxNORMAL_FONT
;
524 m_brush
= *wxWHITE_BRUSH
;
526 m_macATSUIStyle
= NULL
;
528 m_graphicContext
= NULL
;
533 if( m_macATSUIStyle
)
535 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
536 m_macATSUIStyle
= NULL
;
539 delete m_graphicContext
;
542 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
544 wxCHECK_RET( Ok(), wxT("invalid window dc") );
545 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
546 wxCoord xx
= XLOG2DEVMAC(x
);
547 wxCoord yy
= YLOG2DEVMAC(y
);
548 wxCoord w
= bmp
.GetWidth();
549 wxCoord h
= bmp
.GetHeight();
550 wxCoord ww
= XLOG2DEVREL(w
);
551 wxCoord hh
= YLOG2DEVREL(h
);
553 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
554 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() ) ;
555 HIRect r
= CGRectMake( xx
, yy
, ww
, hh
) ;
556 HIViewDrawCGImage( cg
, &r
, image
) ;
557 CGImageRelease( image
) ;
560 void wxDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
562 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
563 wxCHECK_RET(icon
.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
565 wxCoord xx
= XLOG2DEVMAC(x
);
566 wxCoord yy
= YLOG2DEVMAC(y
);
567 wxCoord w
= icon
.GetWidth();
568 wxCoord h
= icon
.GetHeight();
569 wxCoord ww
= XLOG2DEVREL(w
);
570 wxCoord hh
= YLOG2DEVREL(h
);
572 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
573 CGRect r
= CGRectMake( 00 , 00 , ww
, hh
) ;
574 CGContextSaveGState(cg
);
575 CGContextTranslateCTM(cg
, xx
, yy
+ hh
);
576 CGContextScaleCTM(cg
, 1, -1);
577 PlotIconRefInContext( cg
, &r
, kAlignNone
, kTransformNone
,
578 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) ) ;
579 CGContextRestoreGState( cg
) ;
582 void wxDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
584 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
585 wxCoord xx
, yy
, ww
, hh
;
588 ww
= XLOG2DEVREL(width
);
589 hh
= YLOG2DEVREL(height
);
591 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
592 CGRect clipRect
= CGRectMake( xx
,yy
, ww
, hh
) ;
593 CGContextClipToRect( cgContext
, clipRect
) ;
595 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
596 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
599 m_clipX1
= wxMax( m_clipX1
, xx
);
600 m_clipY1
= wxMax( m_clipY1
, yy
);
601 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
602 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
612 // TODO as soon as we don't reset the context for each operation anymore
613 // we have to update the context as well
616 void wxDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
618 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
621 DestroyClippingRegion();
625 region
.GetBox( x
, y
, w
, h
);
626 wxCoord xx
, yy
, ww
, hh
;
631 // if we have a scaling that we cannot map onto native regions
632 // we must use the box
633 if ( ww
!= w
|| hh
!= h
)
635 wxDC::DoSetClippingRegion( x
, y
, w
, h
);
640 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
641 if ( xx != x || yy != y )
643 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
645 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
649 m_clipX1
= wxMax( m_clipX1
, xx
);
650 m_clipY1
= wxMax( m_clipY1
, yy
);
651 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
652 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
665 void wxDC::DestroyClippingRegion()
667 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
668 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
669 CGContextRestoreGState( cgContext
);
670 CGContextSaveGState( cgContext
);
671 m_graphicContext
->SetPen( m_pen
) ;
672 m_graphicContext
->SetBrush( m_brush
) ;
676 void wxDC::DoGetSizeMM( int* width
, int* height
) const
681 *width
= long( double(w
) / (m_scaleX
*m_mm_to_pix_x
) );
682 *height
= long( double(h
) / (m_scaleY
*m_mm_to_pix_y
) );
685 void wxDC::SetTextForeground( const wxColour
&col
)
687 wxCHECK_RET(Ok(), wxT("Invalid DC"));
688 if ( col
!= m_textForegroundColour
)
690 m_textForegroundColour
= col
;
695 void wxDC::SetTextBackground( const wxColour
&col
)
697 wxCHECK_RET(Ok(), wxT("Invalid DC"));
698 m_textBackgroundColour
= col
;
701 void wxDC::SetMapMode( int mode
)
706 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
709 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
712 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
715 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
719 SetLogicalScale( 1.0, 1.0 );
722 if (mode
!= wxMM_TEXT
)
724 m_needComputeScaleX
= TRUE
;
725 m_needComputeScaleY
= TRUE
;
729 void wxDC::SetUserScale( double x
, double y
)
731 // allow negative ? -> no
734 ComputeScaleAndOrigin();
737 void wxDC::SetLogicalScale( double x
, double y
)
742 ComputeScaleAndOrigin();
745 void wxDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
747 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
748 m_logicalOriginY
= y
* m_signY
;
749 ComputeScaleAndOrigin();
752 void wxDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
754 m_externalDeviceOriginX
= x
;
755 m_externalDeviceOriginY
= y
;
756 ComputeScaleAndOrigin();
759 void wxDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
761 m_signX
= (xLeftRight
? 1 : -1);
762 m_signY
= (yBottomUp
? -1 : 1);
763 ComputeScaleAndOrigin();
766 wxSize
wxDC::GetPPI() const
768 return wxSize(72, 72);
771 int wxDC::GetDepth() const
776 void wxDC::ComputeScaleAndOrigin()
778 // CMB: copy scale to see if it changes
779 double origScaleX
= m_scaleX
;
780 double origScaleY
= m_scaleY
;
781 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
782 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
783 m_deviceOriginX
= m_internalDeviceOriginX
+ m_externalDeviceOriginX
;
784 m_deviceOriginY
= m_internalDeviceOriginY
+ m_externalDeviceOriginY
;
785 // CMB: if scale has changed call SetPen to recalulate the line width
786 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
788 // this is a bit artificial, but we need to force wxDC to think
789 // the pen has changed
796 void wxDC::SetPalette( const wxPalette
& palette
)
800 void wxDC::SetBackgroundMode( int mode
)
802 m_backgroundMode
= mode
;
805 void wxDC::SetFont( const wxFont
&font
)
811 void wxDC::SetPen( const wxPen
&pen
)
816 if ( m_graphicContext
)
818 m_graphicContext
->SetPen( m_pen
) ;
822 void wxDC::SetBrush( const wxBrush
&brush
)
824 if (m_brush
== brush
)
827 if ( m_graphicContext
)
829 m_graphicContext
->SetBrush( m_brush
) ;
833 void wxDC::SetBackground( const wxBrush
&brush
)
835 if (m_backgroundBrush
== brush
)
837 m_backgroundBrush
= brush
;
838 if (!m_backgroundBrush
.Ok())
842 void wxDC::SetLogicalFunction( int function
)
844 if (m_logicalFunction
== function
)
846 m_logicalFunction
= function
;
849 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
850 const wxColour
& col
, int style
);
852 bool wxDC::DoFloodFill(wxCoord x
, wxCoord y
,
853 const wxColour
& col
, int style
)
855 return wxDoFloodFill(this, x
, y
, col
, style
);
858 bool wxDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
860 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
861 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
862 wxMacPortSaver
helper((CGrafPtr
)m_macPort
) ;
865 XLOG2DEVMAC(x
) + m_macLocalOriginInPort
.x
- m_macLocalOrigin
.x
,
866 YLOG2DEVMAC(y
) + m_macLocalOriginInPort
.y
- m_macLocalOrigin
.y
, &colour
);
867 // Convert from Mac colour to wx
868 col
->Set( colour
.red
>> 8,
874 void wxDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
876 wxCHECK_RET(Ok(), wxT("Invalid DC"));
878 if ( m_logicalFunction
!= wxCOPY
)
881 wxCoord xx1
= XLOG2DEVMAC(x1
) ;
882 wxCoord yy1
= YLOG2DEVMAC(y1
) ;
883 wxCoord xx2
= XLOG2DEVMAC(x2
) ;
884 wxCoord yy2
= YLOG2DEVMAC(y2
) ;
886 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
887 path
->MoveToPoint( xx1
, yy1
) ;
888 path
->AddLineToPoint( xx2
, yy2
) ;
889 path
->CloseSubpath() ;
890 m_graphicContext
->StrokePath( path
) ;
893 CalcBoundingBox(x1
, y1
);
894 CalcBoundingBox(x2
, y2
);
897 void wxDC::DoCrossHair( wxCoord x
, wxCoord y
)
899 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
901 if ( m_logicalFunction
!= wxCOPY
)
907 wxCoord xx
= XLOG2DEVMAC(x
);
908 wxCoord yy
= YLOG2DEVMAC(y
);
910 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
911 path
->MoveToPoint( XLOG2DEVMAC(0), yy
) ;
912 path
->AddLineToPoint( XLOG2DEVMAC(w
), yy
) ;
913 path
->CloseSubpath() ;
914 path
->MoveToPoint( xx
, YLOG2DEVMAC(0) ) ;
915 path
->AddLineToPoint( xx
, YLOG2DEVMAC(h
) ) ;
916 path
->CloseSubpath() ;
917 m_graphicContext
->StrokePath( path
) ;
920 CalcBoundingBox(x
, y
);
921 CalcBoundingBox(x
+w
, y
+h
);
925 * To draw arcs properly the angles need to be converted from the WX style:
926 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
927 * a normal axis on paper).
930 * Angles start on the +ve y axis and go clockwise.
933 static double wxConvertWXangleToMACangle(double angle
)
935 double newAngle
= 90 - angle
;
941 void wxDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
942 wxCoord x2
, wxCoord y2
,
943 wxCoord xc
, wxCoord yc
)
945 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
947 if ( m_logicalFunction
!= wxCOPY
)
950 wxCoord xx1
= XLOG2DEVMAC(x1
);
951 wxCoord yy1
= YLOG2DEVMAC(y1
);
952 wxCoord xx2
= XLOG2DEVMAC(x2
);
953 wxCoord yy2
= YLOG2DEVMAC(y2
);
954 wxCoord xxc
= XLOG2DEVMAC(xc
);
955 wxCoord yyc
= YLOG2DEVMAC(yc
);
956 double dx
= xx1
- xxc
;
957 double dy
= yy1
- yyc
;
958 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
959 wxCoord rad
= (wxCoord
)radius
;
960 double radius1
, radius2
;
961 if (xx1
== xx2
&& yy1
== yy2
)
966 else if (radius
== 0.0)
968 radius1
= radius2
= 0.0;
972 radius1
= (xx1
- xxc
== 0) ?
973 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
974 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
975 radius2
= (xx2
- xxc
== 0) ?
976 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
977 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
979 wxCoord alpha2
= wxCoord(radius2
- radius1
);
980 wxCoord alpha1
= wxCoord(wxConvertWXangleToMACangle(radius1
));
981 if( (xx1
> xx2
) || (yy1
> yy2
) ) {
984 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
985 CGContextRef ctx
= mctx
->GetNativeContext() ;
986 AddEllipticArcToPath( ctx
, CGPointMake( xxc
, yyc
) , rad
, rad
, alpha1
, alpha2
) ;
987 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
990 void wxDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
991 double sa
, double ea
)
993 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
995 if ( m_logicalFunction
!= wxCOPY
)
998 double angle
= sa
- ea
; // Order important Mac in opposite direction to wx
999 // we have to make sure that the filling is always counter-clockwise
1002 wxCoord xx
= XLOG2DEVMAC(x
);
1003 wxCoord yy
= YLOG2DEVMAC(y
);
1004 wxCoord ww
= m_signX
* XLOG2DEVREL(w
);
1005 wxCoord hh
= m_signY
* YLOG2DEVREL(h
);
1006 // handle -ve width and/or height
1007 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
1008 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
1009 sa
= wxConvertWXangleToMACangle(sa
);
1010 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1011 CGContextRef ctx
= mctx
->GetNativeContext() ;
1012 AddEllipticArcToPath( ctx
, CGPointMake( xx
+ ww
/ 2 , yy
+ hh
/ 2 ) , ww
/ 2 , hh
/ 2 , sa
, angle
) ;
1013 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1016 void wxDC::DoDrawPoint( wxCoord x
, wxCoord y
)
1018 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1019 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 ) ;
1022 void wxDC::DoDrawLines(int n
, wxPoint points
[],
1023 wxCoord xoffset
, wxCoord yoffset
)
1025 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1027 if ( m_logicalFunction
!= wxCOPY
)
1030 wxCoord x1
, x2
, y1
, y2
;
1031 x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1032 y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1033 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1034 path
->MoveToPoint( x1
, y1
) ;
1035 for (int i
= 1; i
< n
; i
++)
1037 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1038 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1040 path
->AddLineToPoint( x2
, y2
) ;
1042 m_graphicContext
->StrokePath( path
) ;
1046 void wxDC::DoDrawPolygon(int n
, wxPoint points
[],
1047 wxCoord xoffset
, wxCoord yoffset
,
1050 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1051 wxCoord x1
, x2
, y1
, y2
;
1052 if ( n
== 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
1055 if ( m_logicalFunction
!= wxCOPY
)
1058 x2
= x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1059 y2
= y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1061 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1062 path
->MoveToPoint( x1
, y1
) ;
1063 for (int i
= 1; i
< n
; i
++)
1065 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1066 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1068 path
->AddLineToPoint( x2
, y2
) ;
1070 if ( x1
!= x2
|| y1
!= y2
)
1072 path
->AddLineToPoint( x1
,y1
) ;
1074 path
->CloseSubpath() ;
1075 m_graphicContext
->DrawPath( path
, fillStyle
) ;
1079 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1081 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1083 if ( m_logicalFunction
!= wxCOPY
)
1086 wxCoord xx
= XLOG2DEVMAC(x
);
1087 wxCoord yy
= YLOG2DEVMAC(y
);
1088 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1089 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1090 // CMB: draw nothing if transformed w or h is 0
1091 if (ww
== 0 || hh
== 0)
1093 // CMB: handle -ve width and/or height
1104 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1105 path
->AddRectangle(xx
,yy
, ww
, hh
) ;
1106 m_graphicContext
->DrawPath( path
) ;
1110 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1111 wxCoord width
, wxCoord height
,
1114 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1116 if ( m_logicalFunction
!= wxCOPY
)
1121 radius
= - radius
* ((width
< height
) ? width
: height
);
1122 wxCoord xx
= XLOG2DEVMAC(x
);
1123 wxCoord yy
= YLOG2DEVMAC(y
);
1124 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1125 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1126 // CMB: draw nothing if transformed w or h is 0
1127 if (ww
== 0 || hh
== 0)
1129 // CMB: handle -ve width and/or height
1140 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1141 CGContextRef ctx
= mctx
->GetNativeContext() ;
1142 AddRoundedRectToPath( ctx
, CGRectMake( xx
, yy
, ww
, hh
) , 16 ,16 ) ;
1143 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1146 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1148 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1150 if ( m_logicalFunction
!= wxCOPY
)
1153 wxCoord xx
= XLOG2DEVMAC(x
);
1154 wxCoord yy
= YLOG2DEVMAC(y
);
1155 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1156 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1157 // CMB: draw nothing if transformed w or h is 0
1158 if (ww
== 0 || hh
== 0)
1160 // CMB: handle -ve width and/or height
1172 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1173 CGContextRef ctx
= mctx
->GetNativeContext() ;
1174 if ( width
== height
)
1176 CGContextBeginPath(ctx
);
1177 CGContextAddArc(ctx
,
1184 CGContextClosePath(ctx
);
1186 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1190 AddEllipticArcToPath( ctx
, CGPointMake( xx
+ ww
/ 2 , yy
+ hh
/ 2 ) , ww
/ 2 , hh
/ 2 , 0 , 360) ;
1191 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1195 bool wxDC::CanDrawBitmap(void) const
1200 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1201 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1202 wxCoord xsrcMask
, wxCoord ysrcMask
)
1204 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
1205 wxCHECK_MSG(source
->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
1206 if ( logical_func
== wxNO_OP
)
1208 if (xsrcMask
== -1 && ysrcMask
== -1)
1210 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1213 wxCoord yysrc
= source
->YLOG2DEVMAC(ysrc
) ;
1214 wxCoord xxsrc
= source
->XLOG2DEVMAC(xsrc
) ;
1215 wxCoord wwsrc
= source
->XLOG2DEVREL(width
) ;
1216 wxCoord hhsrc
= source
->YLOG2DEVREL(height
) ;
1218 wxCoord yydest
= YLOG2DEVMAC(ydest
) ;
1219 wxCoord xxdest
= XLOG2DEVMAC(xdest
) ;
1220 wxCoord wwdest
= XLOG2DEVREL(width
) ;
1221 wxCoord hhdest
= YLOG2DEVREL(height
) ;
1223 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
) ;
1224 if ( memdc
&& logical_func
== wxCOPY
)
1226 wxBitmap blit
= memdc
->GetSelectedObject() ;
1227 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") ) ;
1229 wxCoord bmpwidth
= blit
.GetWidth();
1230 wxCoord bmpheight
= blit
.GetHeight();
1232 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1234 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
) ;
1235 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
) ;
1236 if ( wwsrc
> 0 && hhsrc
> 0 )
1238 if ( xxsrc
>= 0 && yysrc
>= 0 )
1240 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
) ;
1241 blit
= blit
.GetSubBitmap( subrect
) ;
1245 // in this case we'd probably have to adjust the different coordinates, but
1246 // we have to find out proper contract first
1247 blit
= wxNullBitmap
;
1252 blit
= wxNullBitmap
;
1257 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1258 CGImageRef image
= (CGImageRef
)( blit
.CGImageCreate() ) ;
1259 HIRect r
= CGRectMake( xxdest
, yydest
, wwdest
, hhdest
) ;
1260 HIViewDrawCGImage( cg
, &r
, image
) ;
1261 CGImageRelease( image
) ;
1268 CGContextRef cg = (wxMacCGContext*)(source->GetGraphicContext())->GetNativeContext() ;
1269 void *data = CGBitmapContextGetData( cg ) ;
1271 return FALSE
; // wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ) ;
1276 void wxDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1279 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1281 if ( str
.Length() == 0 )
1284 if ( m_logicalFunction
!= wxCOPY
)
1287 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1289 OSStatus status
= noErr
;
1290 ATSUTextLayout atsuLayout
;
1291 UniCharCount chars
= str
.Length() ;
1292 UniChar
* ubuf
= NULL
;
1293 #if SIZEOF_WCHAR_T == 4
1294 wxMBConvUTF16 converter
;
1296 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1297 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1298 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1300 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1301 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1302 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1303 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1305 chars
= unicharlen
/ 2 ;
1308 ubuf
= (UniChar
*) str
.wc_str() ;
1310 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1311 chars
= wxWcslen( wchar
.data() ) ;
1312 ubuf
= (UniChar
*) wchar
.data() ;
1316 int drawX
= XLOG2DEVMAC(x
) ;
1317 int drawY
= YLOG2DEVMAC(y
) ;
1319 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1320 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1322 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1324 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true ) ;
1325 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1327 int iAngle
= int( angle
);
1328 if ( abs(iAngle
) > 0 )
1330 Fixed atsuAngle
= IntToFixed( iAngle
) ;
1331 ATSUAttributeTag atsuTags
[] =
1333 kATSULineRotationTag
,
1335 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1339 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1343 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1344 atsuTags
, atsuSizes
, atsuValues
) ;
1347 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1348 ATSUAttributeTag atsuTags
[] =
1352 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1354 sizeof( CGContextRef
) ,
1356 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1360 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1361 atsuTags
, atsuSizes
, atsuValues
) ;
1364 ATSUTextMeasurement textBefore
;
1365 ATSUTextMeasurement textAfter
;
1366 ATSUTextMeasurement ascent
;
1367 ATSUTextMeasurement descent
;
1369 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1370 &textBefore
, &textAfter
, &ascent
, &descent
);
1371 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1375 if ( m_backgroundMode
== wxSOLID
)
1377 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1381 path
->AddLineToPoint(
1382 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ,
1383 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ) ;
1384 path
->AddLineToPoint(
1385 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) + cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1386 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) - sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1387 path
->AddLineToPoint(
1388 (int) (drawX
+ cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1389 (int) (drawY
- sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1391 m_graphicContext
->FillPath( path
, m_textBackgroundColour
) ;
1395 drawX
+= (int)(sin(angle
/RAD2DEG
) * FixedToInt(ascent
));
1396 drawY
+= (int)(cos(angle
/RAD2DEG
) * FixedToInt(ascent
));
1398 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1399 IntToFixed(drawX
) , IntToFixed(drawY
) , &rect
);
1400 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1402 CGContextSaveGState(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext());
1403 CGContextTranslateCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), drawX
, drawY
);
1404 CGContextScaleCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), 1, -1);
1405 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1406 IntToFixed(0) , IntToFixed(0) );
1407 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1408 CGContextRestoreGState( ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ) ;
1410 CalcBoundingBox(XDEV2LOG(rect
.left
), YDEV2LOG(rect
.top
) );
1411 CalcBoundingBox(XDEV2LOG(rect
.right
), YDEV2LOG(rect
.bottom
) );
1413 ::ATSUDisposeTextLayout(atsuLayout
);
1414 #if SIZEOF_WCHAR_T == 4
1419 void wxDC::DoDrawText(const wxString
& strtext
, wxCoord x
, wxCoord y
)
1421 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1422 DoDrawRotatedText( strtext
, x
, y
, 0.0 ) ;
1425 bool wxDC::CanGetTextExtent() const
1427 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1431 void wxDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1432 wxCoord
*descent
, wxCoord
*externalLeading
,
1433 wxFont
*theFont
) const
1435 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1436 wxFont formerFont
= m_font
;
1439 // work around the constness
1440 *((wxFont
*)(&m_font
)) = *theFont
;
1444 if ( str
.Length() == 0 )
1447 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1449 OSStatus status
= noErr
;
1450 ATSUTextLayout atsuLayout
;
1451 UniCharCount chars
= str
.Length() ;
1452 UniChar
* ubuf
= NULL
;
1453 #if SIZEOF_WCHAR_T == 4
1454 wxMBConvUTF16 converter
;
1456 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1457 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1458 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1460 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1461 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1462 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1463 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1465 chars
= unicharlen
/ 2 ;
1468 ubuf
= (UniChar
*) str
.wc_str() ;
1470 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1471 chars
= wxWcslen( wchar
.data() ) ;
1472 ubuf
= (UniChar
*) wchar
.data() ;
1477 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1478 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1480 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1482 ATSUTextMeasurement textBefore
;
1483 ATSUTextMeasurement textAfter
;
1484 ATSUTextMeasurement textAscent
;
1485 ATSUTextMeasurement textDescent
;
1487 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1488 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1491 *height
= YDEV2LOGREL( FixedToInt(textAscent
+ textDescent
) ) ;
1493 *descent
=YDEV2LOGREL( FixedToInt(textDescent
) );
1494 if ( externalLeading
)
1495 *externalLeading
= 0 ;
1497 *width
= XDEV2LOGREL( FixedToInt(textAfter
- textBefore
) ) ;
1499 ::ATSUDisposeTextLayout(atsuLayout
);
1500 #if SIZEOF_WCHAR_T == 4
1505 // work around the constness
1506 *((wxFont
*)(&m_font
)) = formerFont
;
1512 bool wxDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1514 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1517 widths
.Add(0, text
.Length());
1519 if (text
.Length() == 0)
1522 ATSUTextLayout atsuLayout
;
1523 UniCharCount chars
= text
.Length() ;
1524 UniChar
* ubuf
= NULL
;
1525 #if SIZEOF_WCHAR_T == 4
1526 wxMBConvUTF16 converter
;
1528 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 ) ;
1529 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1530 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 ) ;
1532 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1533 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1534 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1535 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1537 chars
= unicharlen
/ 2 ;
1540 ubuf
= (UniChar
*) text
.wc_str() ;
1542 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1543 chars
= wxWcslen( wchar
.data() ) ;
1544 ubuf
= (UniChar
*) wchar
.data() ;
1549 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1550 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1552 for ( int pos
= 0; pos
< chars
; pos
++ ) {
1553 unsigned long actualNumberOfBounds
= 0;
1554 ATSTrapezoid glyphBounds
;
1556 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1558 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1, kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1559 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1564 widths
[pos
] = XDEV2LOGREL(FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
));
1565 //unsigned char uch = s[i];
1568 ::ATSUDisposeTextLayout(atsuLayout
);
1572 wxCoord
wxDC::GetCharWidth(void) const
1575 DoGetTextExtent(wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
) ;
1579 wxCoord
wxDC::GetCharHeight(void) const
1582 DoGetTextExtent(wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
) ;
1586 void wxDC::Clear(void)
1588 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1590 if ( m_backgroundBrush
.Ok() && m_backgroundBrush
.GetStyle() != wxTRANSPARENT
)
1592 HIRect rect
= CGRectMake( -10000 , -10000 , 20000 , 20000 ) ;
1593 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1594 switch( m_backgroundBrush
.MacGetBrushKind() )
1596 case kwxMacBrushTheme
:
1598 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1599 if ( HIThemeSetFill
!= 0 )
1601 HIThemeSetFill( m_backgroundBrush
.MacGetTheme() , NULL
, cg
, kHIThemeOrientationNormal
) ;
1602 CGContextFillRect(cg
, rect
);
1609 GetThemeBrushAsColor( m_backgroundBrush
.MacGetTheme() , 32, true, &color
);
1610 CGContextSetRGBFillColor( cg
, (float) color
.red
/ 65536,
1611 (float) color
.green
/ 65536, (float) color
.blue
/ 65536, 1 );
1612 CGContextFillRect( cg
, rect
);
1614 // reset to normal value
1615 RGBColor col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
1616 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
1619 case kwxMacBrushThemeBackground
:
1621 wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ;
1622 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1623 if ( UMAGetSystemVersion() >= 0x1030 )
1625 HIThemeBackgroundDrawInfo drawInfo
;
1626 drawInfo
.version
= 0 ;
1627 drawInfo
.state
= kThemeStateActive
;
1628 drawInfo
.kind
= m_backgroundBrush
.MacGetThemeBackground(NULL
) ;
1629 if ( drawInfo
.kind
== kThemeBackgroundMetal
)
1630 HIThemeDrawBackground( &rect
, &drawInfo
, cg
,
1631 kHIThemeOrientationNormal
) ;
1632 HIThemeApplyBackground( &rect
, &drawInfo
, cg
,
1633 kHIThemeOrientationNormal
) ;
1641 case kwxMacBrushColour
:
1643 RGBColor col
= MAC_WXCOLORREF( m_backgroundBrush
.GetColour().GetPixel()) ;
1644 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
1645 CGContextFillRect(cg
, rect
);
1647 // reset to normal value
1648 col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
1649 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
1656 void wxDC::MacInstallFont() const
1658 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1660 if( m_macATSUIStyle
)
1662 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
1663 m_macATSUIStyle
= NULL
;
1668 OSStatus status
= noErr
;
1669 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) m_font
.MacGetATSUStyle() , (ATSUStyle
*) &m_macATSUIStyle
) ;
1670 wxASSERT_MSG( status
== noErr
, wxT("couldn't set create ATSU style") ) ;
1672 Fixed atsuSize
= IntToFixed( int(m_scaleY
* m_font
.MacGetFontSize()) ) ;
1673 RGBColor atsuColor
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel() ) ;
1674 ATSUAttributeTag atsuTags
[] =
1679 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1682 sizeof( RGBColor
) ,
1684 // Boolean kTrue = true ;
1685 // Boolean kFalse = false ;
1687 // ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
1688 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1693 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUIStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
) ,
1694 atsuTags
, atsuSizes
, atsuValues
);
1696 wxASSERT_MSG( status
== noErr
, wxT("couldn't Modify ATSU style") ) ;
1700 // ---------------------------------------------------------------------------
1701 // coordinates transformations
1702 // ---------------------------------------------------------------------------
1704 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
1706 return ((wxDC
*)this)->XDEV2LOG(x
);
1709 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
1711 return ((wxDC
*)this)->YDEV2LOG(y
);
1714 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
1716 return ((wxDC
*)this)->XDEV2LOGREL(x
);
1719 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
1721 return ((wxDC
*)this)->YDEV2LOGREL(y
);
1724 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
1726 return ((wxDC
*)this)->XLOG2DEV(x
);
1729 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
1731 return ((wxDC
*)this)->YLOG2DEV(y
);
1734 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
1736 return ((wxDC
*)this)->XLOG2DEVREL(x
);
1739 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
1741 return ((wxDC
*)this)->YLOG2DEVREL(y
);
1744 #endif // wxMAC_USE_CORE_GRAPHICS