1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/graphcmn.cpp
3 // Purpose: graphics context methods common to all platforms
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
27 #include "wx/graphics.h"
31 #include "wx/bitmap.h"
32 #include "wx/dcmemory.h"
35 #if wxUSE_GRAPHICS_CONTEXT
37 #if !defined(wxMAC_USE_CORE_GRAPHICS_BLEND_MODES)
38 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 const double RAD2DEG
= 180.0 / M_PI
;
46 const short kEmulatedMode
= -1;
47 const short kUnsupportedMode
= -2;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 static inline double dmin(double a
, double b
)
57 static inline double dmax(double a
, double b
)
62 static inline double DegToRad(double deg
)
64 return (deg
* M_PI
) / 180.0;
66 static inline double RadToDeg(double deg
)
68 return (deg
* 180.0) / M_PI
;
72 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
76 return wxPoint2DDouble(x
,y
);
79 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
81 MoveToPoint( p
.m_x
, p
.m_y
);
84 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
86 AddLineToPoint( p
.m_x
, p
.m_y
);
89 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
91 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
94 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
96 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
103 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
105 // calculate using degree elevation to a cubic bezier
109 wxPoint2DDouble start
= GetCurrentPoint() ;
110 wxPoint2DDouble
end(x
,y
);
111 wxPoint2DDouble
c(cx
,cy
);
112 c1
= wxDouble(1/3.0) * start
+ wxDouble(2/3.0) * c
;
113 c2
= wxDouble(2/3.0) * c
+ wxDouble(1/3.0) * end
;
114 AddCurveToPoint(c1
.m_x
,c1
.m_y
,c2
.m_x
,c2
.m_y
,x
,y
);
117 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
120 AddLineToPoint(x
,y
+h
);
121 AddLineToPoint(x
+w
,y
+h
);
122 AddLineToPoint(x
+w
,y
);
126 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
129 AddArc( x
,y
,r
,0,2*M_PI
,false);
133 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
134 void wxGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
136 wxPoint2DDouble current
= GetCurrentPoint();
137 wxPoint2DDouble
p1(x1
,y1
);
138 wxPoint2DDouble
p2(x2
,y2
);
140 wxPoint2DDouble v1
= current
- p1
;
142 wxPoint2DDouble v2
= p2
- p1
;
145 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
148 alpha
= 360 + alpha
;
149 // TODO obtuse angles
151 alpha
= DegToRad(alpha
);
153 wxDouble dist
= r
/ sin(alpha
/2) * cos(alpha
/2) ;
154 // calculate tangential points
155 wxPoint2DDouble t1
= dist
*v1
+ p1
;
156 wxPoint2DDouble t2
= dist
*v2
+ p1
;
158 wxPoint2DDouble nv1
= v1
;
159 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
160 wxPoint2DDouble c
= t1
+ r
*nv1
;
162 wxDouble a1
= v1
.GetVectorAngle()+90 ;
163 wxDouble a2
= v2
.GetVectorAngle()-90 ;
166 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
170 //-----------------------------------------------------------------------------
171 // wxGraphicsContext Convenience Methods
172 //-----------------------------------------------------------------------------
174 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
176 FillPath( path
, fillStyle
);
180 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
184 DrawText( str
, 0, 0 );
189 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
191 wxGraphicsPath
* path
= CreatePath();
192 path
->MoveToPoint(x1
, y1
) ;
193 path
->AddLineToPoint( x2
, y2
);
198 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
200 wxGraphicsPath
* path
= CreatePath();
201 path
->AddRectangle( x
, y
, w
, h
);
206 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
208 wxGraphicsPath
* path
= CreatePath();
211 path
->AddCircle( x
+w
/2,y
+w
/2,w
/2);
217 Translate(x
+w
/2,y
+h
/2);
218 wxDouble factor
= ((wxDouble
) w
) / h
;
219 Scale( factor
, 1.0) ;
220 path
->AddCircle(0,0,h
/2);
227 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
229 wxGraphicsPath
* path
= CreatePath();
232 path
->AddRectangle( x
, y
, w
, h
);
240 path
->MoveToPoint(w
, h
/ 2);
241 path
->AddArcToPoint(w
, h
, w
/ 2, h
, radius
);
242 path
->AddArcToPoint(0, h
, 0, h
/ 2, radius
);
243 path
->AddArcToPoint(0, 0, w
/ 2, 0, radius
);
244 path
->AddArcToPoint(w
, 0, w
, h
/ 2, radius
);
245 path
->CloseSubpath();
252 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
255 wxGraphicsPath
* path
= CreatePath();
256 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
257 for ( size_t i
= 1 ; i
< n
; ++i
)
258 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
263 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
266 wxGraphicsPath
* path
= CreatePath();
267 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
268 for ( size_t i
= 1 ; i
< n
; ++i
)
269 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
270 DrawPath( path
, fillStyle
);
274 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
277 wxGraphicsPath
* path
= CreatePath();
278 for ( size_t i
= 0 ; i
< n
; ++i
)
280 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
) ;
281 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
287 IMPLEMENT_ABSTRACT_CLASS(wxGCDC
, wxObject
)
289 //-----------------------------------------------------------------------------
291 //-----------------------------------------------------------------------------
299 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
302 m_graphicContext
= wxGraphicsContext::Create(dc
);
304 if ( dc
.GetFont().Ok())
305 m_graphicContext
->SetFont(dc
.GetFont());
306 if ( dc
.GetPen().Ok())
307 m_graphicContext
->SetPen(dc
.GetPen());
308 if ( dc
.GetBrush().Ok())
309 m_graphicContext
->SetBrush(dc
.GetBrush());
310 m_graphicContext
->SetTextColor(dc
.GetTextForeground());
317 m_mm_to_pix_x
= mm2pt
;
318 m_mm_to_pix_y
= mm2pt
;
320 m_pen
= *wxBLACK_PEN
;
321 m_font
= *wxNORMAL_FONT
;
322 m_brush
= *wxWHITE_BRUSH
;
324 m_graphicContext
= NULL
;
330 delete m_graphicContext
;
333 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
335 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
336 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
338 wxCoord xx
= LogicalToDeviceX(x
);
339 wxCoord yy
= LogicalToDeviceY(y
);
340 wxCoord w
= bmp
.GetWidth();
341 wxCoord h
= bmp
.GetHeight();
342 wxCoord ww
= LogicalToDeviceXRel(w
);
343 wxCoord hh
= LogicalToDeviceYRel(h
);
345 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
348 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
350 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
351 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
353 wxCoord xx
= LogicalToDeviceX(x
);
354 wxCoord yy
= LogicalToDeviceY(y
);
355 wxCoord w
= icon
.GetWidth();
356 wxCoord h
= icon
.GetHeight();
357 wxCoord ww
= LogicalToDeviceXRel(w
);
358 wxCoord hh
= LogicalToDeviceYRel(h
);
360 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
363 void wxGCDC::DoSetClippingRegion( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxCoord
WXUNUSED(width
), wxCoord
WXUNUSED(height
) )
367 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
369 wxCoord xx
, yy
, ww
, hh
;
370 xx
= LogicalToDeviceX(x
);
371 yy
= LogicalToDeviceY(y
);
372 ww
= LogicalToDeviceXRel(width
);
373 hh
= LogicalToDeviceYRel(height
);
375 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
376 CGRect clipRect
= CGRectMake( xx
, yy
, ww
, hh
);
377 CGContextClipToRect( cgContext
, clipRect
);
379 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
380 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
384 m_clipX1
= wxMax( m_clipX1
, xx
);
385 m_clipY1
= wxMax( m_clipY1
, yy
);
386 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
387 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
399 // TODO: as soon as we don't reset the context for each operation anymore
400 // we have to update the context as well
405 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
407 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
411 DestroyClippingRegion();
416 region
.GetBox( x
, y
, w
, h
);
417 wxCoord xx
, yy
, ww
, hh
;
418 xx
= LogicalToDeviceX(x
);
419 yy
= LogicalToDeviceY(y
);
420 ww
= LogicalToDeviceXRel(w
);
421 hh
= LogicalToDeviceYRel(h
);
423 // if we have a scaling that we cannot map onto native regions
424 // we must use the box
425 if ( ww
!= w
|| hh
!= h
)
427 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
432 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
);
433 if ( xx
!= x
|| yy
!= y
)
434 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
);
435 SectRgn( (RgnHandle
)m_macCurrentClipRgn
, (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
440 m_clipX1
= wxMax( m_clipX1
, xx
);
441 m_clipY1
= wxMax( m_clipY1
, yy
);
442 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
443 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
457 void wxGCDC::DestroyClippingRegion()
461 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
463 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
464 CGContextRestoreGState( cgContext
);
465 CGContextSaveGState( cgContext
);
467 m_graphicContext
->SetPen( m_pen
);
468 m_graphicContext
->SetBrush( m_brush
);
475 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
481 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
483 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
486 void wxGCDC::SetTextForeground( const wxColour
&col
)
488 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
490 if ( col
!= m_textForegroundColour
)
492 m_textForegroundColour
= col
;
493 m_graphicContext
->SetTextColor( col
);
497 void wxGCDC::SetTextBackground( const wxColour
&col
)
499 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
501 m_textBackgroundColour
= col
;
504 void wxGCDC::SetMapMode( int mode
)
509 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
513 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
517 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
521 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
526 SetLogicalScale( 1.0, 1.0 );
530 ComputeScaleAndOrigin();
533 void wxGCDC::SetUserScale( double x
, double y
)
535 // allow negative ? -> no
538 ComputeScaleAndOrigin();
541 void wxGCDC::SetLogicalScale( double x
, double y
)
546 ComputeScaleAndOrigin();
549 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
551 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
552 m_logicalOriginY
= y
* m_signY
;
553 ComputeScaleAndOrigin();
556 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
560 ComputeScaleAndOrigin();
563 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
565 m_signX
= (xLeftRight
? 1 : -1);
566 m_signY
= (yBottomUp
? -1 : 1);
567 ComputeScaleAndOrigin();
570 wxSize
wxGCDC::GetPPI() const
572 return wxSize(72, 72);
575 int wxGCDC::GetDepth() const
580 void wxGCDC::ComputeScaleAndOrigin()
582 // this is a bit artificial, but we need to force wxGCDC to think
583 // the pen has changed
584 wxPen
pen( GetPen() );
590 void wxGCDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
595 void wxGCDC::SetBackgroundMode( int mode
)
597 m_backgroundMode
= mode
;
600 void wxGCDC::SetFont( const wxFont
&font
)
603 if ( m_graphicContext
)
604 m_graphicContext
->SetFont( font
);
607 void wxGCDC::SetPen( const wxPen
&pen
)
613 if ( m_graphicContext
)
615 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
617 m_graphicContext
->SetPen( m_pen
);
621 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
622 // eg when using a wxScrollWindow and scrolling around
623 int origX
= LogicalToDeviceX( 0 );
624 int origY
= LogicalToDeviceY( 0 );
625 m_graphicContext
->Translate( origX
, origY
);
626 m_graphicContext
->SetPen( m_pen
);
627 m_graphicContext
->Translate( -origX
, -origY
);
632 void wxGCDC::SetBrush( const wxBrush
&brush
)
634 if (m_brush
== brush
)
638 if ( m_graphicContext
)
640 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
642 m_graphicContext
->SetBrush( m_brush
);
646 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
647 // eg when using a wxScrollWindow and scrolling around
648 // TODO on MSW / GDIPlus this still occurs with hatched brushes
649 int origX
= LogicalToDeviceX(0);
650 int origY
= LogicalToDeviceY(0);
651 m_graphicContext
->Translate( origX
, origY
);
652 m_graphicContext
->SetBrush( m_brush
);
653 m_graphicContext
->Translate( -origX
, -origY
);
658 void wxGCDC::SetBackground( const wxBrush
&brush
)
660 if (m_backgroundBrush
== brush
)
663 m_backgroundBrush
= brush
;
664 if (!m_backgroundBrush
.Ok())
668 void wxGCDC::SetLogicalFunction( int function
)
670 if (m_logicalFunction
== function
)
673 m_logicalFunction
= function
;
674 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
676 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
677 if ( m_logicalFunction
== wxCOPY
)
678 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
679 else if ( m_logicalFunction
== wxINVERT
)
680 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
682 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
687 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
688 const wxColour
& col
, int style
);
690 bool wxGCDC::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
691 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
696 bool wxGCDC::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
698 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
702 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
704 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
706 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
708 if ( m_logicalFunction
!= wxCOPY
)
712 wxCoord xx1
= LogicalToDeviceX(x1
);
713 wxCoord yy1
= LogicalToDeviceY(y1
);
714 wxCoord xx2
= LogicalToDeviceX(x2
);
715 wxCoord yy2
= LogicalToDeviceY(y2
);
717 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
719 CalcBoundingBox(x1
, y1
);
720 CalcBoundingBox(x2
, y2
);
723 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
725 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
727 if ( m_logicalFunction
!= wxCOPY
)
734 wxCoord xx
= LogicalToDeviceX(x
);
735 wxCoord yy
= LogicalToDeviceY(y
);
736 wxCoord xw
= LogicalToDeviceX(w
);
737 wxCoord x0
= LogicalToDeviceX(0);
738 wxCoord y0
= LogicalToDeviceY(0);
739 wxCoord yh
= LogicalToDeviceY(h
);
741 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
742 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
744 CalcBoundingBox(x0
, y0
);
745 CalcBoundingBox(x0
+xw
, y0
+yh
);
748 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
749 wxCoord x2
, wxCoord y2
,
750 wxCoord xc
, wxCoord yc
)
752 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
754 if ( m_logicalFunction
!= wxCOPY
)
757 wxCoord xx1
= LogicalToDeviceX(x1
);
758 wxCoord yy1
= LogicalToDeviceY(y1
);
759 wxCoord xx2
= LogicalToDeviceX(x2
);
760 wxCoord yy2
= LogicalToDeviceY(y2
);
761 wxCoord xxc
= LogicalToDeviceX(xc
);
762 wxCoord yyc
= LogicalToDeviceY(yc
);
764 double dx
= xx1
- xxc
;
765 double dy
= yy1
- yyc
;
766 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
767 wxCoord rad
= (wxCoord
)radius
;
769 if (xx1
== xx2
&& yy1
== yy2
)
774 else if (radius
== 0.0)
780 sa
= (xx1
- xxc
== 0) ?
781 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
782 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
783 ea
= (xx2
- xxc
== 0) ?
784 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
785 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
788 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
790 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
791 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
792 path
->MoveToPoint( xxc
, yyc
);
793 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
794 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
795 path
->AddLineToPoint( xxc
, yyc
);
796 m_graphicContext
->DrawPath(path
);
800 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
801 double sa
, double ea
)
803 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
805 if ( m_logicalFunction
!= wxCOPY
)
808 wxCoord xx
= LogicalToDeviceX(x
);
809 wxCoord yy
= LogicalToDeviceY(y
);
810 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
811 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
813 // handle -ve width and/or height
825 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
827 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
828 m_graphicContext
->PushState();
829 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
830 wxDouble factor
= ((wxDouble
) ww
) / hh
;
831 m_graphicContext
->Scale( factor
, 1.0) ;
832 if ( fill
&& (sa
!=ea
) )
833 path
->MoveToPoint(0,0);
834 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
835 // get clockwise angles
836 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
837 if ( fill
&& (sa
!=ea
) )
838 path
->AddLineToPoint(0,0);
839 m_graphicContext
->DrawPath( path
);
840 m_graphicContext
->PopState();
844 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
846 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
848 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
851 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
852 wxCoord xoffset
, wxCoord yoffset
)
854 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
856 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
858 if ( m_logicalFunction
!= wxCOPY
)
862 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
] ;
863 for( int i
= 0 ; i
< n
; ++i
)
865 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
866 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
869 m_graphicContext
->StrokeLines( n
, pointsD
) ;
874 void wxGCDC::DoDrawSpline(wxList
*points
)
876 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
878 if ( m_logicalFunction
!= wxCOPY
)
881 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
883 wxList::compatibility_iterator node
= points
->GetFirst();
884 if (node
== wxList::compatibility_iterator())
888 wxPoint
*p
= (wxPoint
*)node
->GetData();
893 node
= node
->GetNext();
894 p
= (wxPoint
*)node
->GetData();
898 wxCoord cx1
= ( x1
+ x2
) / 2;
899 wxCoord cy1
= ( y1
+ y2
) / 2;
901 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
902 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
905 while ((node
= node
->GetNext()) != NULL
)
908 while ((node
= node
->GetNext()))
912 p
= (wxPoint
*)node
->GetData();
917 wxCoord cx4
= (x1
+ x2
) / 2;
918 wxCoord cy4
= (y1
+ y2
) / 2;
920 path
->AddQuadCurveToPoint(
921 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
922 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
928 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
930 m_graphicContext
->StrokePath( path
);
935 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
936 wxCoord xoffset
, wxCoord yoffset
,
939 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
941 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
943 if ( m_logicalFunction
!= wxCOPY
)
946 bool closeIt
= false ;
947 if (points
[n
-1] != points
[0])
950 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)] ;
951 for( int i
= 0 ; i
< n
; ++i
)
953 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
954 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
957 pointsD
[n
] = pointsD
[0];
959 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
) ;
963 void wxGCDC::DoDrawPolyPolygon(int n
,
971 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
974 for ( int j
= 0 ; j
< n
; ++j
)
976 wxPoint start
= points
[i
];
977 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
)) ;
980 for ( int k
= 1 ; k
< l
; ++k
)
982 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
986 if ( start
!= points
[i
-1])
987 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
989 m_graphicContext
->DrawPath( path
, fillStyle
);
993 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
995 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
997 if ( m_logicalFunction
!= wxCOPY
)
1000 wxCoord xx
= LogicalToDeviceX(x
);
1001 wxCoord yy
= LogicalToDeviceY(y
);
1002 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1003 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1005 // CMB: draw nothing if transformed w or h is 0
1006 if (ww
== 0 || hh
== 0)
1009 // CMB: handle -ve width and/or height
1020 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1023 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1024 wxCoord width
, wxCoord height
,
1027 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1029 if ( m_logicalFunction
!= wxCOPY
)
1033 radius
= - radius
* ((width
< height
) ? width
: height
);
1034 wxCoord xx
= LogicalToDeviceX(x
);
1035 wxCoord yy
= LogicalToDeviceY(y
);
1036 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1037 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1039 // CMB: draw nothing if transformed w or h is 0
1040 if (ww
== 0 || hh
== 0)
1043 // CMB: handle -ve width and/or height
1055 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1058 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1060 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1062 if ( m_logicalFunction
!= wxCOPY
)
1065 wxCoord xx
= LogicalToDeviceX(x
);
1066 wxCoord yy
= LogicalToDeviceY(y
);
1067 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1068 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1070 // CMB: draw nothing if transformed w or h is 0
1071 if (ww
== 0 || hh
== 0)
1074 // CMB: handle -ve width and/or height
1086 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1089 bool wxGCDC::CanDrawBitmap() const
1094 bool wxGCDC::DoBlit(
1095 wxCoord
WXUNUSED(xdest
), wxCoord
WXUNUSED(ydest
), wxCoord
WXUNUSED(width
), wxCoord
WXUNUSED(height
),
1096 wxDC
*source
, wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
), int logical_func
, bool WXUNUSED(useMask
),
1097 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
) )
1099 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1100 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1102 if ( logical_func
== wxNO_OP
)
1106 if (xsrcMask
== -1 && ysrcMask
== -1)
1112 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1113 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1114 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1115 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1117 wxCoord yydest
= LogicalToDeviceY(ydest
);
1118 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1119 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1120 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1122 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1123 if ( memdc
&& logical_func
== wxCOPY
)
1125 wxBitmap blit
= memdc
->GetSelectedObject();
1127 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1129 wxCoord bmpwidth
= blit
.GetWidth();
1130 wxCoord bmpheight
= blit
.GetHeight();
1132 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1134 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1135 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1136 if ( wwsrc
> 0 && hhsrc
> 0 )
1138 if ( xxsrc
>= 0 && yysrc
>= 0 )
1140 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1141 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1142 blit
= blit
.GetSubBitmap( subrect
);
1146 // in this case we'd probably have to adjust the different coordinates, but
1147 // we have to find out proper contract first
1148 blit
= wxNullBitmap
;
1153 blit
= wxNullBitmap
;
1159 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1165 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
1173 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1176 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1178 if ( str
.length() == 0 )
1180 if ( m_logicalFunction
!= wxCOPY
)
1183 int drawX
= LogicalToDeviceX(x
);
1184 int drawY
= LogicalToDeviceY(y
);
1186 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1189 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1191 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1193 if ( str
.length() == 0 )
1195 if ( m_logicalFunction
!= wxCOPY
)
1198 int drawX
= LogicalToDeviceX(x
);
1199 int drawY
= LogicalToDeviceY(y
);
1201 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1204 bool wxGCDC::CanGetTextExtent() const
1206 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1211 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1212 wxCoord
*descent
, wxCoord
*externalLeading
,
1213 wxFont
*theFont
) const
1215 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1217 wxFont formerFont
= m_font
;
1220 m_graphicContext
->SetFont( *theFont
);
1223 wxDouble h
, d
, e
, w
;
1225 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1228 *height
= DeviceToLogicalYRel( h
);
1230 *descent
=DeviceToLogicalYRel( d
);
1231 if ( externalLeading
)
1232 *externalLeading
= DeviceToLogicalYRel( e
);
1234 *width
= DeviceToLogicalXRel( w
);
1238 m_graphicContext
->SetFont( m_font
);
1242 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1244 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1246 widths
.Add(0,text
.Length());
1247 if ( text
.IsEmpty() )
1250 wxArrayDouble widthsD
;
1252 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1253 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1254 widths
[i
] = DeviceToLogicalXRel( widthsD
[i
] + 0.5 ) ;
1259 wxCoord
wxGCDC::GetCharWidth(void) const
1262 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1267 wxCoord
wxGCDC::GetCharHeight(void) const
1270 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1275 void wxGCDC::Clear(void)
1277 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1281 void wxGCDC::DoGetSize(int *width
, int *height
) const
1287 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1288 const wxColour
& initialColour
,
1289 const wxColour
& destColour
,
1290 wxDirection nDirection
)
1297 start
= rect
.GetRightBottom();
1299 end
= rect
.GetLeftBottom();
1302 start
= rect
.GetLeftBottom();
1303 end
= rect
.GetRightBottom();
1307 start
= rect
.GetLeftBottom();
1309 end
= rect
.GetLeftTop();
1312 start
= rect
.GetLeftTop();
1313 end
= rect
.GetLeftBottom();
1318 m_graphicContext
->SetLinearGradientBrush(
1319 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1320 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1322 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1323 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1324 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1325 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1327 if (ww
== 0 || hh
== 0)
1341 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1342 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1343 m_graphicContext
->SetPen(m_pen
);
1346 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1347 const wxColour
& initialColour
,
1348 const wxColour
& destColour
,
1349 const wxPoint
& circleCenter
)
1352 wxInt32 cx
= rect
.GetWidth() / 2;
1353 wxInt32 cy
= rect
.GetHeight() / 2;
1360 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1361 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1362 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1363 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1365 if (ww
== 0 || hh
== 0)
1379 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1380 m_graphicContext
->SetBrush( wxBrush( destColour
) ) ;
1381 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1383 m_graphicContext
->SetRadialGradientBrush(
1384 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1385 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1386 LogicalToDeviceXRel(nRadius
),
1387 initialColour
,destColour
);
1389 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1390 m_graphicContext
->SetPen(m_pen
);
1393 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1394 wxCoord width
, wxCoord height
)
1396 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);