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"
37 #include <ATSUnicode.h>
38 #include <TextCommon.h>
39 #include <TextEncodingConverter.h>
41 #include <CGContext.h>
43 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
51 const double M_PI
= 3.14159265358979 ;
54 const double RAD2DEG
= 180.0 / M_PI
;
55 const short kEmulatedMode
= -1 ;
56 const short kUnsupportedMode
= -2 ;
58 extern TECObjectRef s_TECNativeCToUnicode
;
61 // The text ctrl implementation still needs that for the non hiview implementation
63 wxMacWindowClipper::wxMacWindowClipper( const wxWindow
* win
) :
64 wxMacPortSaver( (GrafPtr
) GetWindowPort((WindowRef
) win
->MacGetTopLevelWindowRef()) )
66 m_newPort
=(GrafPtr
) GetWindowPort((WindowRef
) win
->MacGetTopLevelWindowRef()) ;
67 m_formerClip
= NewRgn() ;
68 m_newClip
= NewRgn() ;
69 GetClip( m_formerClip
) ;
73 // guard against half constructed objects, this just leads to a empty clip
77 win
->MacWindowToRootWindow( &x
,&y
) ;
78 // get area including focus rect
79 CopyRgn( (RgnHandle
) ((wxWindow
*)win
)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip
) ;
80 if ( !EmptyRgn( m_newClip
) )
81 OffsetRgn( m_newClip
, x
, y
) ;
84 SetClip( m_newClip
) ;
88 wxMacWindowClipper::~wxMacWindowClipper()
90 SetPort( m_newPort
) ;
91 SetClip( m_formerClip
) ;
92 DisposeRgn( m_newClip
) ;
93 DisposeRgn( m_formerClip
) ;
96 wxMacWindowStateSaver::wxMacWindowStateSaver( const wxWindow
* win
) :
97 wxMacWindowClipper( win
)
99 // the port is already set at this point
100 m_newPort
=(GrafPtr
) GetWindowPort((WindowRef
) win
->MacGetTopLevelWindowRef()) ;
101 GetThemeDrawingState( &m_themeDrawingState
) ;
104 wxMacWindowStateSaver::~wxMacWindowStateSaver()
106 SetPort( m_newPort
) ;
107 SetThemeDrawingState( m_themeDrawingState
, true ) ;
110 // minimal implementation only used for appearance drawing < 10.3
112 wxMacPortSetter::wxMacPortSetter( const wxDC
* dc
) :
113 m_ph( (GrafPtr
) dc
->m_macPort
)
115 wxASSERT( dc
->Ok() ) ;
117 // dc->MacSetupPort(&m_ph) ;
119 wxMacPortSetter::~wxMacPortSetter()
121 // m_dc->MacCleanupPort(&m_ph) ;
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
129 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
130 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
132 //-----------------------------------------------------------------------------
133 // device context implementation
135 // more and more of the dc functionality should be implemented by calling
136 // the appropricate wxMacCGContext, but we will have to do that step by step
137 // also coordinate conversions should be moved to native matrix ops
138 //-----------------------------------------------------------------------------
140 wxMacCGPath::wxMacCGPath()
142 m_path
= CGPathCreateMutable() ;
145 wxMacCGPath::~wxMacCGPath()
147 CGPathRelease( m_path
) ;
150 // Starts a new subpath at
151 void wxMacCGPath::MoveToPoint( wxCoord x1
, wxCoord y1
)
153 CGPathMoveToPoint( m_path
, NULL
, x1
, y1
) ;
156 void wxMacCGPath::AddLineToPoint( wxCoord x1
, wxCoord y1
)
158 CGPathAddLineToPoint( m_path
, NULL
, x1
, y1
) ;
161 void wxMacCGPath::AddRectangle( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
163 CGRect cgRect
= { { x
, y
} , { w
, h
} } ;
164 CGPathAddRect( m_path
, NULL
, cgRect
) ;
167 void wxMacCGPath::AddCircle( wxCoord x
, wxCoord y
, wxCoord r
)
169 CGPathAddArc( m_path
, NULL
, x
, y
, r
, 0.0 , 2 * M_PI
, true ) ;
172 // closes the current subpath
173 void wxMacCGPath::CloseSubpath()
175 CGPathCloseSubpath( m_path
) ;
178 CGPathRef
wxMacCGPath::GetPath() const
183 // we always stock two context states, one at entry, the other one after
184 // changing to HI Graphics orientation (this one is used for getting back clippings etc)
186 wxMacCGContext::wxMacCGContext( CGrafPtr port
)
192 wxMacCGContext::wxMacCGContext( CGContextRef cgcontext
)
195 m_cgContext
= cgcontext
;
196 CGContextSaveGState( m_cgContext
) ;
197 CGContextSaveGState( m_cgContext
) ;
200 wxMacCGContext::wxMacCGContext()
206 wxMacCGContext::~wxMacCGContext()
210 CGContextSynchronize( m_cgContext
) ;
211 CGContextRestoreGState( m_cgContext
) ;
212 CGContextRestoreGState( m_cgContext
) ;
215 CGContextRelease( m_cgContext
) ;
219 void wxMacCGContext::Clip( const wxRegion
®ion
)
221 // ClipCGContextToRegion ( m_cgContext, &bounds , (RgnHandle) dc->m_macCurrentClipRgn ) ;
224 void wxMacCGContext::StrokePath( const wxGraphicPath
*p
)
226 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
227 CGContextBeginPath( m_cgContext
) ;
228 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
229 CGContextClosePath( m_cgContext
) ;
230 CGContextStrokePath( m_cgContext
) ;
233 void wxMacCGContext::DrawPath( const wxGraphicPath
*p
, int fillStyle
)
235 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
236 CGPathDrawingMode mode
= m_mode
;
237 if ( fillStyle
== wxODDEVEN_RULE
)
239 if ( mode
== kCGPathFill
)
240 mode
= kCGPathEOFill
;
241 else if ( mode
== kCGPathFillStroke
)
242 mode
= kCGPathEOFillStroke
;
244 CGContextBeginPath( m_cgContext
) ;
245 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
246 CGContextClosePath( m_cgContext
) ;
247 CGContextDrawPath( m_cgContext
, mode
) ;
250 void wxMacCGContext::FillPath( const wxGraphicPath
*p
, const wxColor
&fillColor
, int fillStyle
)
252 const wxMacCGPath
* path
= dynamic_cast< const wxMacCGPath
*>( p
) ;
253 CGContextSaveGState( m_cgContext
) ;
255 RGBColor col
= MAC_WXCOLORREF( fillColor
.GetPixel() ) ;
256 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
257 CGPathDrawingMode mode
= kCGPathFill
;
259 if ( fillStyle
== wxODDEVEN_RULE
)
260 mode
= kCGPathEOFill
;
262 CGContextBeginPath( m_cgContext
) ;
263 CGContextAddPath( m_cgContext
, path
->GetPath() ) ;
264 CGContextClosePath( m_cgContext
) ;
265 CGContextDrawPath( m_cgContext
, mode
) ;
267 CGContextRestoreGState( m_cgContext
) ;
270 wxGraphicPath
* wxMacCGContext::CreatePath()
272 // make sure that we now have a real cgref, before doing
273 // anything with paths
274 CGContextRef cg
= GetNativeContext() ;
276 return new wxMacCGPath() ;
279 // in case we only got a QDPort only create a cgref now
281 CGContextRef
wxMacCGContext::GetNativeContext()
283 if( m_cgContext
== NULL
)
286 GetPortBounds( (CGrafPtr
) m_qdPort
, &bounds
) ;
287 OSStatus status
= CreateCGContextForPort((CGrafPtr
) m_qdPort
, &m_cgContext
) ;
288 CGContextSaveGState( m_cgContext
) ;
290 wxASSERT_MSG( status
== noErr
, wxT("Cannot nest wxDCs on the same window") ) ;
291 CGContextTranslateCTM( m_cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
292 CGContextScaleCTM( m_cgContext
, 1 , -1 ) ;
294 CGContextSaveGState( m_cgContext
) ;
296 SetBrush( m_brush
) ;
301 void wxMacCGContext::SetNativeContext( CGContextRef cg
)
303 wxASSERT( m_cgContext
== NULL
) ;
305 CGContextSaveGState( m_cgContext
) ;
308 void wxMacCGContext::SetPen( const wxPen
&pen
)
311 if ( m_cgContext
== NULL
)
313 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
314 bool stroke
= pen
.GetStyle() != wxTRANSPARENT
;
317 // we can benchmark performance, should go into a setting later
318 CGContextSetShouldAntialias( m_cgContext
, false ) ;
323 m_mode
= kCGPathFill
; // just a default
327 m_mode
= kCGPathFill
;
331 RGBColor col
= MAC_WXCOLORREF( pen
.GetColour().GetPixel() ) ;
332 CGContextSetRGBStrokeColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
335 switch( pen
.GetCap() )
338 cap
= kCGLineCapRound
;
340 case wxCAP_PROJECTING
:
341 cap
= kCGLineCapSquare
;
344 cap
= kCGLineCapButt
;
347 cap
= kCGLineCapButt
;
350 CGContextSetLineCap( m_cgContext
, cap
) ;
353 switch( pen
.GetJoin() )
356 join
= kCGLineJoinBevel
;
359 join
= kCGLineJoinMiter
;
362 join
= kCGLineJoinRound
;
365 join
= kCGLineJoinMiter
;
368 CGContextSetLineJoin( m_cgContext
, join
) ;
370 CGContextSetLineWidth( m_cgContext
, pen
.GetWidth() == 0 ? 0.1 : pen
.GetWidth() /* TODO * m_dc->m_scaleX */ ) ;
372 m_mode
= kCGPathStroke
;
374 const float *lengths
= NULL
;
375 float *userLengths
= NULL
;
377 const float dotted
[] = { 3 , 3 };
378 const float dashed
[] = { 19 , 9 };
379 const float short_dashed
[] = { 9 , 6 };
380 const float dotted_dashed
[] = { 9 , 6 , 3 , 3 };
382 switch( pen
.GetStyle() )
388 count
= WXSIZEOF(dotted
);
392 count
= WXSIZEOF(dashed
) ;
395 lengths
= short_dashed
;
396 count
= WXSIZEOF(short_dashed
) ;
399 lengths
= dotted_dashed
;
400 count
= WXSIZEOF(dotted_dashed
);
404 count
= pen
.GetDashes( &dashes
) ;
407 userLengths
= new float[count
] ;
408 for( int i
= 0 ; i
< count
; ++i
)
409 userLengths
[i
] = dashes
[i
] ;
411 lengths
= userLengths
;
417 CGContextSetLineDash( m_cgContext
, 0 , lengths
, count
) ;
418 delete[] userLengths
;
419 // we need to change the cap, otherwise everything overlaps
420 // and we get solid lines
422 CGContextSetLineCap( m_cgContext
, kCGLineCapButt
) ;
424 if ( fill
&& stroke
)
426 m_mode
= kCGPathFillStroke
;
431 void wxMacCGContext::SetBrush( const wxBrush
&brush
)
434 if ( m_cgContext
== NULL
)
437 bool fill
= brush
.GetStyle() != wxTRANSPARENT
;
438 bool stroke
= m_pen
.GetStyle() != wxTRANSPARENT
;
441 // we can benchmark performance, should go into a setting later
442 CGContextSetShouldAntialias( m_cgContext
, false ) ;
448 m_mode
= kCGPathFill
; // just a default
452 RGBColor col
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() ) ;
453 CGContextSetRGBFillColor( m_cgContext
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
454 m_mode
= kCGPathFill
;
458 m_mode
= kCGPathStroke
;
460 if ( fill
&& stroke
)
462 m_mode
= kCGPathFillStroke
;
468 void AddEllipticArcToPath(CGContextRef c
, CGPoint center
, float a
, float b
, float fromDegree
, float toDegree
)
470 CGContextSaveGState(c
);
471 CGContextTranslateCTM(c
, center
.x
, center
.y
);
472 CGContextScaleCTM(c
, a
, b
);
473 CGContextMoveToPoint(c
, 1, 0);
474 CGContextAddArc(c
, 0, 0, 1, DegToRad(fromDegree
), DegToRad(toDegree
), 0);
475 CGContextClosePath(c
);
476 CGContextRestoreGState(c
);
479 void AddRoundedRectToPath(CGContextRef c
, CGRect rect
, float ovalWidth
,
483 if (ovalWidth
== 0 || ovalHeight
== 0)
485 CGContextAddRect(c
, rect
);
488 CGContextSaveGState(c
);
489 CGContextTranslateCTM(c
, CGRectGetMinX(rect
), CGRectGetMinY(rect
));
490 CGContextScaleCTM(c
, ovalWidth
, ovalHeight
);
491 fw
= CGRectGetWidth(rect
) / ovalWidth
;
492 fh
= CGRectGetHeight(rect
) / ovalHeight
;
493 CGContextMoveToPoint(c
, fw
, fh
/2);
494 CGContextAddArcToPoint(c
, fw
, fh
, fw
/2, fh
, 1);
495 CGContextAddArcToPoint(c
, 0, fh
, 0, fh
/2, 1);
496 CGContextAddArcToPoint(c
, 0, 0, fw
/2, 0, 1);
497 CGContextAddArcToPoint(c
, fw
, 0, fw
, fh
/2, 1);
498 CGContextClosePath(c
);
499 CGContextRestoreGState(c
);
506 m_mm_to_pix_x
= mm2pt
;
507 m_mm_to_pix_y
= mm2pt
;
508 m_internalDeviceOriginX
= 0;
509 m_internalDeviceOriginY
= 0;
510 m_externalDeviceOriginX
= 0;
511 m_externalDeviceOriginY
= 0;
512 m_logicalScaleX
= 1.0;
513 m_logicalScaleY
= 1.0;
518 m_needComputeScaleX
= FALSE
;
519 m_needComputeScaleY
= FALSE
;
523 m_macLocalOrigin
.x
= m_macLocalOrigin
.y
= 0 ;
525 m_pen
= *wxBLACK_PEN
;
526 m_font
= *wxNORMAL_FONT
;
527 m_brush
= *wxWHITE_BRUSH
;
529 m_macATSUIStyle
= NULL
;
531 m_graphicContext
= NULL
;
536 if( m_macATSUIStyle
)
538 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
539 m_macATSUIStyle
= NULL
;
542 delete m_graphicContext
;
545 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
547 wxCHECK_RET( Ok(), wxT("invalid window dc") );
548 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
549 wxCoord xx
= XLOG2DEVMAC(x
);
550 wxCoord yy
= YLOG2DEVMAC(y
);
551 wxCoord w
= bmp
.GetWidth();
552 wxCoord h
= bmp
.GetHeight();
553 wxCoord ww
= XLOG2DEVREL(w
);
554 wxCoord hh
= YLOG2DEVREL(h
);
556 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
557 CGImageRef image
= (CGImageRef
)( bmp
.CGImageCreate() ) ;
558 HIRect r
= CGRectMake( xx
, yy
, ww
, hh
) ;
559 HIViewDrawCGImage( cg
, &r
, image
) ;
560 CGImageRelease( image
) ;
563 void wxDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
565 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
566 wxCHECK_RET(icon
.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
568 wxCoord xx
= XLOG2DEVMAC(x
);
569 wxCoord yy
= YLOG2DEVMAC(y
);
570 wxCoord w
= icon
.GetWidth();
571 wxCoord h
= icon
.GetHeight();
572 wxCoord ww
= XLOG2DEVREL(w
);
573 wxCoord hh
= YLOG2DEVREL(h
);
575 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
576 CGRect r
= CGRectMake( 00 , 00 , ww
, hh
) ;
577 CGContextSaveGState(cg
);
578 CGContextTranslateCTM(cg
, xx
, yy
+ hh
);
579 CGContextScaleCTM(cg
, 1, -1);
580 PlotIconRefInContext( cg
, &r
, kAlignNone
, kTransformNone
,
581 NULL
, kPlotIconRefNormalFlags
, MAC_WXHICON( icon
.GetHICON() ) ) ;
582 CGContextRestoreGState( cg
) ;
585 void wxDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
587 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
588 wxCoord xx
, yy
, ww
, hh
;
591 ww
= XLOG2DEVREL(width
);
592 hh
= YLOG2DEVREL(height
);
594 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
595 CGRect clipRect
= CGRectMake( xx
,yy
, ww
, hh
) ;
596 CGContextClipToRect( cgContext
, clipRect
) ;
598 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
599 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
602 m_clipX1
= wxMax( m_clipX1
, xx
);
603 m_clipY1
= wxMax( m_clipY1
, yy
);
604 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
605 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
615 // TODO as soon as we don't reset the context for each operation anymore
616 // we have to update the context as well
619 void wxDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
621 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
624 DestroyClippingRegion();
628 region
.GetBox( x
, y
, w
, h
);
629 wxCoord xx
, yy
, ww
, hh
;
634 // if we have a scaling that we cannot map onto native regions
635 // we must use the box
636 if ( ww
!= w
|| hh
!= h
)
638 wxDC::DoSetClippingRegion( x
, y
, w
, h
);
643 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
644 if ( xx != x || yy != y )
646 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
648 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
652 m_clipX1
= wxMax( m_clipX1
, xx
);
653 m_clipY1
= wxMax( m_clipY1
, yy
);
654 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
655 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
668 void wxDC::DestroyClippingRegion()
670 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
671 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
672 CGContextRestoreGState( cgContext
);
673 CGContextSaveGState( cgContext
);
675 SetBrush( m_brush
) ;
679 void wxDC::DoGetSizeMM( int* width
, int* height
) const
684 *width
= long( double(w
) / (m_scaleX
*m_mm_to_pix_x
) );
685 *height
= long( double(h
) / (m_scaleY
*m_mm_to_pix_y
) );
688 void wxDC::SetTextForeground( const wxColour
&col
)
690 wxCHECK_RET(Ok(), wxT("Invalid DC"));
691 if ( col
!= m_textForegroundColour
)
693 m_textForegroundColour
= col
;
698 void wxDC::SetTextBackground( const wxColour
&col
)
700 wxCHECK_RET(Ok(), wxT("Invalid DC"));
701 m_textBackgroundColour
= col
;
704 void wxDC::SetMapMode( int mode
)
709 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
712 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
715 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
718 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
722 SetLogicalScale( 1.0, 1.0 );
725 if (mode
!= wxMM_TEXT
)
727 m_needComputeScaleX
= TRUE
;
728 m_needComputeScaleY
= TRUE
;
732 void wxDC::SetUserScale( double x
, double y
)
734 // allow negative ? -> no
737 ComputeScaleAndOrigin();
740 void wxDC::SetLogicalScale( double x
, double y
)
745 ComputeScaleAndOrigin();
748 void wxDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
750 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
751 m_logicalOriginY
= y
* m_signY
;
752 ComputeScaleAndOrigin();
755 void wxDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
757 m_externalDeviceOriginX
= x
;
758 m_externalDeviceOriginY
= y
;
759 ComputeScaleAndOrigin();
762 void wxDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
764 m_signX
= (xLeftRight
? 1 : -1);
765 m_signY
= (yBottomUp
? -1 : 1);
766 ComputeScaleAndOrigin();
769 wxSize
wxDC::GetPPI() const
771 return wxSize(72, 72);
774 int wxDC::GetDepth() const
779 void wxDC::ComputeScaleAndOrigin()
781 // CMB: copy scale to see if it changes
782 double origScaleX
= m_scaleX
;
783 double origScaleY
= m_scaleY
;
784 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
785 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
786 m_deviceOriginX
= m_internalDeviceOriginX
+ m_externalDeviceOriginX
;
787 m_deviceOriginY
= m_internalDeviceOriginY
+ m_externalDeviceOriginY
;
788 // CMB: if scale has changed call SetPen to recalulate the line width
789 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
791 // this is a bit artificial, but we need to force wxDC to think
792 // the pen has changed
799 void wxDC::SetPalette( const wxPalette
& palette
)
803 void wxDC::SetBackgroundMode( int mode
)
805 m_backgroundMode
= mode
;
808 void wxDC::SetFont( const wxFont
&font
)
814 void wxDC::SetPen( const wxPen
&pen
)
819 if ( m_graphicContext
)
821 m_graphicContext
->SetPen( m_pen
) ;
825 void wxDC::SetBrush( const wxBrush
&brush
)
827 if (m_brush
== brush
)
830 if ( m_graphicContext
)
832 m_graphicContext
->SetBrush( m_brush
) ;
836 void wxDC::SetBackground( const wxBrush
&brush
)
838 if (m_backgroundBrush
== brush
)
840 m_backgroundBrush
= brush
;
841 if (!m_backgroundBrush
.Ok())
845 void wxDC::SetLogicalFunction( int function
)
847 if (m_logicalFunction
== function
)
849 m_logicalFunction
= function
;
852 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
853 const wxColour
& col
, int style
);
855 bool wxDC::DoFloodFill(wxCoord x
, wxCoord y
,
856 const wxColour
& col
, int style
)
858 return wxDoFloodFill(this, x
, y
, col
, style
);
861 bool wxDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
863 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
864 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
865 wxMacPortSaver
helper((CGrafPtr
)m_macPort
) ;
868 XLOG2DEVMAC(x
) + m_macLocalOriginInPort
.x
- m_macLocalOrigin
.x
,
869 YLOG2DEVMAC(y
) + m_macLocalOriginInPort
.y
- m_macLocalOrigin
.y
, &colour
);
870 // Convert from Mac colour to wx
871 col
->Set( colour
.red
>> 8,
877 void wxDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
879 wxCHECK_RET(Ok(), wxT("Invalid DC"));
881 if ( m_logicalFunction
!= wxCOPY
)
884 wxCoord xx1
= XLOG2DEVMAC(x1
) ;
885 wxCoord yy1
= YLOG2DEVMAC(y1
) ;
886 wxCoord xx2
= XLOG2DEVMAC(x2
) ;
887 wxCoord yy2
= YLOG2DEVMAC(y2
) ;
889 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
890 path
->MoveToPoint( xx1
, yy1
) ;
891 path
->AddLineToPoint( xx2
, yy2
) ;
892 path
->CloseSubpath() ;
893 m_graphicContext
->StrokePath( path
) ;
896 CalcBoundingBox(x1
, y1
);
897 CalcBoundingBox(x2
, y2
);
900 void wxDC::DoCrossHair( wxCoord x
, wxCoord y
)
902 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
904 if ( m_logicalFunction
!= wxCOPY
)
910 wxCoord xx
= XLOG2DEVMAC(x
);
911 wxCoord yy
= YLOG2DEVMAC(y
);
913 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
914 path
->MoveToPoint( XLOG2DEVMAC(0), yy
) ;
915 path
->AddLineToPoint( XLOG2DEVMAC(w
), yy
) ;
916 path
->CloseSubpath() ;
917 path
->MoveToPoint( xx
, YLOG2DEVMAC(0) ) ;
918 path
->AddLineToPoint( xx
, YLOG2DEVMAC(h
) ) ;
919 path
->CloseSubpath() ;
920 m_graphicContext
->StrokePath( path
) ;
923 CalcBoundingBox(x
, y
);
924 CalcBoundingBox(x
+w
, y
+h
);
928 * To draw arcs properly the angles need to be converted from the WX style:
929 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
930 * a normal axis on paper).
933 * Angles start on the +ve y axis and go clockwise.
936 static double wxConvertWXangleToMACangle(double angle
)
938 double newAngle
= 90 - angle
;
944 void wxDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
945 wxCoord x2
, wxCoord y2
,
946 wxCoord xc
, wxCoord yc
)
948 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
950 if ( m_logicalFunction
!= wxCOPY
)
953 wxCoord xx1
= XLOG2DEVMAC(x1
);
954 wxCoord yy1
= YLOG2DEVMAC(y1
);
955 wxCoord xx2
= XLOG2DEVMAC(x2
);
956 wxCoord yy2
= YLOG2DEVMAC(y2
);
957 wxCoord xxc
= XLOG2DEVMAC(xc
);
958 wxCoord yyc
= YLOG2DEVMAC(yc
);
959 double dx
= xx1
- xxc
;
960 double dy
= yy1
- yyc
;
961 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
962 wxCoord rad
= (wxCoord
)radius
;
963 double radius1
, radius2
;
964 if (xx1
== xx2
&& yy1
== yy2
)
969 else if (radius
== 0.0)
971 radius1
= radius2
= 0.0;
975 radius1
= (xx1
- xxc
== 0) ?
976 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
977 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
978 radius2
= (xx2
- xxc
== 0) ?
979 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
980 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
982 wxCoord alpha2
= wxCoord(radius2
- radius1
);
983 wxCoord alpha1
= wxCoord(wxConvertWXangleToMACangle(radius1
));
984 if( (xx1
> xx2
) || (yy1
> yy2
) ) {
987 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
988 CGContextRef ctx
= mctx
->GetNativeContext() ;
989 AddEllipticArcToPath( ctx
, CGPointMake( xxc
, yyc
) , rad
, rad
, alpha1
, alpha2
) ;
990 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
993 void wxDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
994 double sa
, double ea
)
996 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
998 if ( m_logicalFunction
!= wxCOPY
)
1001 double angle
= sa
- ea
; // Order important Mac in opposite direction to wx
1002 // we have to make sure that the filling is always counter-clockwise
1005 wxCoord xx
= XLOG2DEVMAC(x
);
1006 wxCoord yy
= YLOG2DEVMAC(y
);
1007 wxCoord ww
= m_signX
* XLOG2DEVREL(w
);
1008 wxCoord hh
= m_signY
* YLOG2DEVREL(h
);
1009 // handle -ve width and/or height
1010 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
1011 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
1012 sa
= wxConvertWXangleToMACangle(sa
);
1013 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1014 CGContextRef ctx
= mctx
->GetNativeContext() ;
1015 AddEllipticArcToPath( ctx
, CGPointMake( xx
+ ww
/ 2 , yy
+ hh
/ 2 ) , ww
/ 2 , hh
/ 2 , sa
, angle
) ;
1016 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1019 void wxDC::DoDrawPoint( wxCoord x
, wxCoord y
)
1021 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1022 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 ) ;
1025 void wxDC::DoDrawLines(int n
, wxPoint points
[],
1026 wxCoord xoffset
, wxCoord yoffset
)
1028 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1030 if ( m_logicalFunction
!= wxCOPY
)
1033 wxCoord x1
, x2
, y1
, y2
;
1034 x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1035 y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1036 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1037 path
->MoveToPoint( x1
, y1
) ;
1038 for (int i
= 1; i
< n
; i
++)
1040 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1041 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1043 path
->AddLineToPoint( x2
, y2
) ;
1045 m_graphicContext
->StrokePath( path
) ;
1049 void wxDC::DoDrawPolygon(int n
, wxPoint points
[],
1050 wxCoord xoffset
, wxCoord yoffset
,
1053 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1054 wxCoord x1
, x2
, y1
, y2
;
1055 if ( n
== 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
1058 if ( m_logicalFunction
!= wxCOPY
)
1061 x2
= x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
1062 y2
= y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
1064 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1065 path
->MoveToPoint( x1
, y1
) ;
1066 for (int i
= 1; i
< n
; i
++)
1068 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
1069 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
1071 path
->AddLineToPoint( x2
, y2
) ;
1073 if ( x1
!= x2
|| y1
!= y2
)
1075 path
->AddLineToPoint( x1
,y1
) ;
1077 path
->CloseSubpath() ;
1078 m_graphicContext
->DrawPath( path
, fillStyle
) ;
1082 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1084 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1086 if ( m_logicalFunction
!= wxCOPY
)
1089 wxCoord xx
= XLOG2DEVMAC(x
);
1090 wxCoord yy
= YLOG2DEVMAC(y
);
1091 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1092 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1093 // CMB: draw nothing if transformed w or h is 0
1094 if (ww
== 0 || hh
== 0)
1096 // CMB: handle -ve width and/or height
1107 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1108 path
->AddRectangle(xx
,yy
, ww
, hh
) ;
1109 m_graphicContext
->DrawPath( path
) ;
1113 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1114 wxCoord width
, wxCoord height
,
1117 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1119 if ( m_logicalFunction
!= wxCOPY
)
1124 radius
= - radius
* ((width
< height
) ? width
: height
);
1125 wxCoord xx
= XLOG2DEVMAC(x
);
1126 wxCoord yy
= YLOG2DEVMAC(y
);
1127 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1128 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1129 // CMB: draw nothing if transformed w or h is 0
1130 if (ww
== 0 || hh
== 0)
1132 // CMB: handle -ve width and/or height
1143 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1144 CGContextRef ctx
= mctx
->GetNativeContext() ;
1145 AddRoundedRectToPath( ctx
, CGRectMake( xx
, yy
, ww
, hh
) , 16 ,16 ) ;
1146 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1149 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1151 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1153 if ( m_logicalFunction
!= wxCOPY
)
1156 wxCoord xx
= XLOG2DEVMAC(x
);
1157 wxCoord yy
= YLOG2DEVMAC(y
);
1158 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1159 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1160 // CMB: draw nothing if transformed w or h is 0
1161 if (ww
== 0 || hh
== 0)
1163 // CMB: handle -ve width and/or height
1175 wxMacCGContext
* mctx
= ((wxMacCGContext
*) m_graphicContext
) ;
1176 CGContextRef ctx
= mctx
->GetNativeContext() ;
1177 if ( width
== height
)
1179 CGContextBeginPath(ctx
);
1180 CGContextAddArc(ctx
,
1187 CGContextClosePath(ctx
);
1189 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1193 AddEllipticArcToPath( ctx
, CGPointMake( xx
+ ww
/ 2 , yy
+ hh
/ 2 ) , ww
/ 2 , hh
/ 2 , 0 , 360) ;
1194 CGContextDrawPath( ctx
, mctx
->GetDrawingMode() ) ;
1198 bool wxDC::CanDrawBitmap(void) const
1203 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1204 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1205 wxCoord xsrcMask
, wxCoord ysrcMask
)
1207 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
1208 wxCHECK_MSG(source
->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
1209 if ( logical_func
== wxNO_OP
)
1211 if (xsrcMask
== -1 && ysrcMask
== -1)
1213 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1216 wxCoord yysrc
= source
->YLOG2DEVMAC(ysrc
) ;
1217 wxCoord xxsrc
= source
->XLOG2DEVMAC(xsrc
) ;
1218 wxCoord wwsrc
= source
->XLOG2DEVREL(width
) ;
1219 wxCoord hhsrc
= source
->YLOG2DEVREL(height
) ;
1221 wxCoord yydest
= YLOG2DEVMAC(ydest
) ;
1222 wxCoord xxdest
= XLOG2DEVMAC(xdest
) ;
1223 wxCoord wwdest
= XLOG2DEVREL(width
) ;
1224 wxCoord hhdest
= YLOG2DEVREL(height
) ;
1226 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
) ;
1227 if ( memdc
&& logical_func
== wxCOPY
)
1229 wxBitmap blit
= memdc
->GetSelectedObject() ;
1230 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") ) ;
1232 wxCoord bmpwidth
= blit
.GetWidth();
1233 wxCoord bmpheight
= blit
.GetHeight();
1235 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1237 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
) ;
1238 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
) ;
1239 if ( wwsrc
> 0 && hhsrc
> 0 )
1241 if ( xxsrc
>= 0 && yysrc
>= 0 )
1243 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
) ;
1244 blit
= blit
.GetSubBitmap( subrect
) ;
1248 // in this case we'd probably have to adjust the different coordinates, but
1249 // we have to find out proper contract first
1250 blit
= wxNullBitmap
;
1255 blit
= wxNullBitmap
;
1260 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1261 CGImageRef image
= (CGImageRef
)( blit
.CGImageCreate() ) ;
1262 HIRect r
= CGRectMake( xxdest
, yydest
, wwdest
, hhdest
) ;
1263 HIViewDrawCGImage( cg
, &r
, image
) ;
1264 CGImageRelease( image
) ;
1271 CGContextRef cg = (wxMacCGContext*)(source->GetGraphicContext())->GetNativeContext() ;
1272 void *data = CGBitmapContextGetData( cg ) ;
1274 return FALSE
; // wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ) ;
1279 void wxDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1282 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1284 if ( str
.Length() == 0 )
1287 if ( m_logicalFunction
!= wxCOPY
)
1290 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1292 OSStatus status
= noErr
;
1293 ATSUTextLayout atsuLayout
;
1294 UniCharCount chars
= str
.Length() ;
1295 UniChar
* ubuf
= NULL
;
1296 #if SIZEOF_WCHAR_T == 4
1297 wxMBConvUTF16 converter
;
1299 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1300 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1301 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1303 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1304 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1305 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1306 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1308 chars
= unicharlen
/ 2 ;
1311 ubuf
= (UniChar
*) str
.wc_str() ;
1313 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1314 chars
= wxWcslen( wchar
.data() ) ;
1315 ubuf
= (UniChar
*) wchar
.data() ;
1319 int drawX
= XLOG2DEVMAC(x
) ;
1320 int drawY
= YLOG2DEVMAC(y
) ;
1322 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1323 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1325 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the rotated text") );
1327 status
= ::ATSUSetTransientFontMatching( atsuLayout
, true ) ;
1328 wxASSERT_MSG( status
== noErr
, wxT("couldn't setup transient font matching") );
1330 int iAngle
= int( angle
);
1331 if ( abs(iAngle
) > 0 )
1333 Fixed atsuAngle
= IntToFixed( iAngle
) ;
1334 ATSUAttributeTag atsuTags
[] =
1336 kATSULineRotationTag
,
1338 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1342 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1346 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1347 atsuTags
, atsuSizes
, atsuValues
) ;
1350 CGContextRef cgContext
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1351 ATSUAttributeTag atsuTags
[] =
1355 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1357 sizeof( CGContextRef
) ,
1359 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1363 status
= ::ATSUSetLayoutControls(atsuLayout
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1364 atsuTags
, atsuSizes
, atsuValues
) ;
1367 ATSUTextMeasurement textBefore
;
1368 ATSUTextMeasurement textAfter
;
1369 ATSUTextMeasurement ascent
;
1370 ATSUTextMeasurement descent
;
1372 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1373 &textBefore
, &textAfter
, &ascent
, &descent
);
1374 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1378 if ( m_backgroundMode
== wxSOLID
)
1380 wxGraphicPath
* path
= m_graphicContext
->CreatePath() ;
1384 path
->AddLineToPoint(
1385 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ,
1386 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
)) ) ;
1387 path
->AddLineToPoint(
1388 (int) (drawX
+ sin(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) + cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1389 (int) (drawY
+ cos(angle
/RAD2DEG
) * FixedToInt(ascent
+ descent
) - sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1390 path
->AddLineToPoint(
1391 (int) (drawX
+ cos(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ,
1392 (int) (drawY
- sin(angle
/RAD2DEG
) * FixedToInt(textAfter
)) ) ;
1394 m_graphicContext
->FillPath( path
, m_textBackgroundColour
) ;
1398 drawX
+= (int)(sin(angle
/RAD2DEG
) * FixedToInt(ascent
));
1399 drawY
+= (int)(cos(angle
/RAD2DEG
) * FixedToInt(ascent
));
1401 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1402 IntToFixed(drawX
) , IntToFixed(drawY
) , &rect
);
1403 wxASSERT_MSG( status
== noErr
, wxT("couldn't measure the rotated text") );
1405 CGContextSaveGState(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext());
1406 CGContextTranslateCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), drawX
, drawY
);
1407 CGContextScaleCTM(((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext(), 1, -1);
1408 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1409 IntToFixed(0) , IntToFixed(0) );
1410 wxASSERT_MSG( status
== noErr
, wxT("couldn't draw the rotated text") );
1411 CGContextRestoreGState( ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ) ;
1413 CalcBoundingBox(XDEV2LOG(rect
.left
), YDEV2LOG(rect
.top
) );
1414 CalcBoundingBox(XDEV2LOG(rect
.right
), YDEV2LOG(rect
.bottom
) );
1416 ::ATSUDisposeTextLayout(atsuLayout
);
1417 #if SIZEOF_WCHAR_T == 4
1422 void wxDC::DoDrawText(const wxString
& strtext
, wxCoord x
, wxCoord y
)
1424 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1425 DoDrawRotatedText( strtext
, x
, y
, 0.0 ) ;
1428 bool wxDC::CanGetTextExtent() const
1430 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1434 void wxDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1435 wxCoord
*descent
, wxCoord
*externalLeading
,
1436 wxFont
*theFont
) const
1438 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1439 wxFont formerFont
= m_font
;
1442 // work around the constness
1443 *((wxFont
*)(&m_font
)) = *theFont
;
1447 if ( str
.Length() == 0 )
1450 wxCHECK_RET( m_macATSUIStyle
!= NULL
, wxT("No valid font set") ) ;
1452 OSStatus status
= noErr
;
1453 ATSUTextLayout atsuLayout
;
1454 UniCharCount chars
= str
.Length() ;
1455 UniChar
* ubuf
= NULL
;
1456 #if SIZEOF_WCHAR_T == 4
1457 wxMBConvUTF16 converter
;
1459 size_t unicharlen
= converter
.WC2MB( NULL
, str
.wc_str() , 0 ) ;
1460 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1461 converter
.WC2MB( (char*) ubuf
, str
.wc_str(), unicharlen
+ 2 ) ;
1463 const wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1464 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1465 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1466 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1468 chars
= unicharlen
/ 2 ;
1471 ubuf
= (UniChar
*) str
.wc_str() ;
1473 wxWCharBuffer wchar
= str
.wc_str( wxConvLocal
) ;
1474 chars
= wxWcslen( wchar
.data() ) ;
1475 ubuf
= (UniChar
*) wchar
.data() ;
1480 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1481 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1483 wxASSERT_MSG( status
== noErr
, wxT("couldn't create the layout of the text") );
1485 ATSUTextMeasurement textBefore
;
1486 ATSUTextMeasurement textAfter
;
1487 ATSUTextMeasurement textAscent
;
1488 ATSUTextMeasurement textDescent
;
1490 status
= ::ATSUGetUnjustifiedBounds( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1491 &textBefore
, &textAfter
, &textAscent
, &textDescent
);
1494 *height
= YDEV2LOGREL( FixedToInt(textAscent
+ textDescent
) ) ;
1496 *descent
=YDEV2LOGREL( FixedToInt(textDescent
) );
1497 if ( externalLeading
)
1498 *externalLeading
= 0 ;
1500 *width
= XDEV2LOGREL( FixedToInt(textAfter
- textBefore
) ) ;
1502 ::ATSUDisposeTextLayout(atsuLayout
);
1503 #if SIZEOF_WCHAR_T == 4
1508 // work around the constness
1509 *((wxFont
*)(&m_font
)) = formerFont
;
1515 bool wxDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1517 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1520 widths
.Add(0, text
.Length());
1522 if (text
.Length() == 0)
1525 ATSUTextLayout atsuLayout
;
1526 UniCharCount chars
= text
.Length() ;
1527 UniChar
* ubuf
= NULL
;
1528 #if SIZEOF_WCHAR_T == 4
1529 wxMBConvUTF16 converter
;
1531 size_t unicharlen
= converter
.WC2MB( NULL
, text
.wc_str() , 0 ) ;
1532 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1533 converter
.WC2MB( (char*) ubuf
, text
.wc_str(), unicharlen
+ 2 ) ;
1535 const wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1536 size_t unicharlen
= converter
.WC2MB( NULL
, wchar
.data() , 0 ) ;
1537 ubuf
= (UniChar
*) malloc( unicharlen
+ 2 ) ;
1538 converter
.WC2MB( (char*) ubuf
, wchar
.data() , unicharlen
+ 2 ) ;
1540 chars
= unicharlen
/ 2 ;
1543 ubuf
= (UniChar
*) text
.wc_str() ;
1545 wxWCharBuffer wchar
= text
.wc_str( wxConvLocal
) ;
1546 chars
= wxWcslen( wchar
.data() ) ;
1547 ubuf
= (UniChar
*) wchar
.data() ;
1552 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) ubuf
, 0 , chars
, chars
, 1 ,
1553 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1555 for ( int pos
= 0; pos
< chars
; pos
++ ) {
1556 unsigned long actualNumberOfBounds
= 0;
1557 ATSTrapezoid glyphBounds
;
1559 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1561 result
= ATSUGetGlyphBounds( atsuLayout
, 0, 0, kATSUFromTextBeginning
, pos
+ 1, kATSUseDeviceOrigins
, 1, &glyphBounds
, &actualNumberOfBounds
);
1562 if (result
!= noErr
|| actualNumberOfBounds
!= 1 )
1567 widths
[pos
] = XDEV2LOGREL(FixedToInt( glyphBounds
.upperRight
.x
- glyphBounds
.upperLeft
.x
));
1568 //unsigned char uch = s[i];
1571 ::ATSUDisposeTextLayout(atsuLayout
);
1575 wxCoord
wxDC::GetCharWidth(void) const
1578 DoGetTextExtent(wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
) ;
1582 wxCoord
wxDC::GetCharHeight(void) const
1585 DoGetTextExtent(wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
) ;
1589 void wxDC::Clear(void)
1591 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1593 if ( m_backgroundBrush
.Ok() && m_backgroundBrush
.GetStyle() != wxTRANSPARENT
)
1595 HIRect rect
= CGRectMake( -10000 , -10000 , 20000 , 20000 ) ;
1596 CGContextRef cg
= ((wxMacCGContext
*)(m_graphicContext
))->GetNativeContext() ;
1597 switch( m_backgroundBrush
.MacGetBrushKind() )
1599 case kwxMacBrushTheme
:
1601 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1602 if ( HIThemeSetFill
!= 0 )
1604 HIThemeSetFill( m_backgroundBrush
.MacGetTheme() , NULL
, cg
, kHIThemeOrientationNormal
) ;
1605 CGContextFillRect(cg
, rect
);
1612 GetThemeBrushAsColor( m_backgroundBrush
.MacGetTheme() , 32, true, &color
);
1613 CGContextSetRGBFillColor( cg
, (float) color
.red
/ 65536,
1614 (float) color
.green
/ 65536, (float) color
.blue
/ 65536, 1 );
1615 CGContextFillRect( cg
, rect
);
1617 // reset to normal value
1618 RGBColor col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
1619 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
1622 case kwxMacBrushThemeBackground
:
1624 wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ;
1625 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1626 if ( UMAGetSystemVersion() >= 0x1030 )
1628 HIThemeBackgroundDrawInfo drawInfo
;
1629 drawInfo
.version
= 0 ;
1630 drawInfo
.state
= kThemeStateActive
;
1631 drawInfo
.kind
= m_backgroundBrush
.MacGetThemeBackground(NULL
) ;
1632 if ( drawInfo
.kind
== kThemeBackgroundMetal
)
1633 HIThemeDrawBackground( &rect
, &drawInfo
, cg
,
1634 kHIThemeOrientationNormal
) ;
1635 HIThemeApplyBackground( &rect
, &drawInfo
, cg
,
1636 kHIThemeOrientationNormal
) ;
1644 case kwxMacBrushColour
:
1646 RGBColor col
= MAC_WXCOLORREF( m_backgroundBrush
.GetColour().GetPixel()) ;
1647 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
1648 CGContextFillRect(cg
, rect
);
1650 // reset to normal value
1651 col
= MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
1652 CGContextSetRGBFillColor( cg
, col
.red
/ 65536.0 , col
.green
/ 65536.0 , col
.blue
/ 65536.0 , 1.0 ) ;
1659 void wxDC::MacInstallFont() const
1661 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1663 if( m_macATSUIStyle
)
1665 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
1666 m_macATSUIStyle
= NULL
;
1671 OSStatus status
= noErr
;
1672 status
= ATSUCreateAndCopyStyle( (ATSUStyle
) m_font
.MacGetATSUStyle() , (ATSUStyle
*) &m_macATSUIStyle
) ;
1673 wxASSERT_MSG( status
== noErr
, wxT("couldn't set create ATSU style") ) ;
1675 Fixed atsuSize
= IntToFixed( int(m_scaleY
* m_font
.MacGetFontSize()) ) ;
1676 RGBColor atsuColor
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel() ) ;
1677 ATSUAttributeTag atsuTags
[] =
1682 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1685 sizeof( RGBColor
) ,
1687 // Boolean kTrue = true ;
1688 // Boolean kFalse = false ;
1690 // ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
1691 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1696 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUIStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
) ,
1697 atsuTags
, atsuSizes
, atsuValues
);
1699 wxASSERT_MSG( status
== noErr
, wxT("couldn't Modify ATSU style") ) ;
1703 // ---------------------------------------------------------------------------
1704 // coordinates transformations
1705 // ---------------------------------------------------------------------------
1707 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
1709 return ((wxDC
*)this)->XDEV2LOG(x
);
1712 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
1714 return ((wxDC
*)this)->YDEV2LOG(y
);
1717 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
1719 return ((wxDC
*)this)->XDEV2LOGREL(x
);
1722 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
1724 return ((wxDC
*)this)->YDEV2LOGREL(y
);
1727 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
1729 return ((wxDC
*)this)->XLOG2DEV(x
);
1732 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
1734 return ((wxDC
*)this)->YLOG2DEV(y
);
1737 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
1739 return ((wxDC
*)this)->XLOG2DEVREL(x
);
1742 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
1744 return ((wxDC
*)this)->YLOG2DEVREL(y
);
1747 #endif // wxMAC_USE_CORE_GRAPHICS