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"
29 #if wxUSE_GRAPHICS_CONTEXT
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 const double RAD2DEG
= 180.0 / M_PI
;
36 const short kEmulatedMode
= -1;
37 const short kUnsupportedMode
= -2;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 static inline double dmin(double a
, double b
)
47 static inline double dmax(double a
, double b
)
52 static inline double DegToRad(double deg
)
54 return (deg
* M_PI
) / 180.0;
56 static inline double RadToDeg(double deg
)
58 return (deg
* 180.0) / M_PI
;
62 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
66 return wxPoint2DDouble(x
,y
);
69 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
71 MoveToPoint( p
.m_x
, p
.m_y
);
74 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
76 AddLineToPoint( p
.m_x
, p
.m_y
);
79 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
81 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
84 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
86 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
93 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
95 // calculate using degree elevation to a cubic bezier
99 wxPoint2DDouble start
= GetCurrentPoint() ;
100 wxPoint2DDouble
end(x
,y
);
101 wxPoint2DDouble
c(cx
,cy
);
102 c1
= (1/3.0) * start
+ (2/3.0) * c
;
103 c2
= (2/3.0) * c
+ (1/3.0) * end
;
104 AddCurveToPoint(c1
.m_x
,c1
.m_y
,c2
.m_x
,c2
.m_y
,x
,y
);
107 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
110 AddLineToPoint(x
,y
+h
);
111 AddLineToPoint(x
+w
,y
+h
);
112 AddLineToPoint(x
+w
,y
);
116 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
119 AddArc( x
,y
,r
,0,2*M_PI
,false);
123 // 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)
124 void wxGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
126 wxPoint2DDouble current
= GetCurrentPoint();
127 wxPoint2DDouble
p1(x1
,y1
);
128 wxPoint2DDouble
p2(x2
,y2
);
130 wxPoint2DDouble v1
= current
- p1
;
132 wxPoint2DDouble v2
= p2
- p1
;
135 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
138 alpha
= 360 + alpha
;
139 // TODO obtuse angles
141 alpha
= DegToRad(alpha
);
143 wxDouble dist
= r
/ sin(alpha
/2) * cos(alpha
/2) ;
144 // calculate tangential points
145 wxPoint2DDouble t1
= dist
*v1
+ p1
;
146 wxPoint2DDouble t2
= dist
*v2
+ p1
;
148 wxPoint2DDouble nv1
= v1
;
149 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
150 wxPoint2DDouble c
= t1
+ r
*nv1
;
152 wxDouble a1
= v1
.GetVectorAngle()+90 ;
153 wxDouble a2
= v2
.GetVectorAngle()-90 ;
156 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
160 //-----------------------------------------------------------------------------
161 // wxGraphicsContext Convenience Methods
162 //-----------------------------------------------------------------------------
164 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
166 FillPath( path
, fillStyle
);
170 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
174 DrawText( str
, 0, 0 );
179 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
181 wxGraphicsPath
* path
= CreatePath();
182 path
->MoveToPoint(x1
, y1
) ;
183 path
->AddLineToPoint( x2
, y2
);
188 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
190 wxGraphicsPath
* path
= CreatePath();
191 path
->AddRectangle( x
, y
, w
, h
);
196 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
198 wxGraphicsPath
* path
= CreatePath();
201 path
->AddCircle( x
+w
/2,y
+w
/2,w
/2);
207 Translate(x
+w
/2,y
+h
/2);
208 wxDouble factor
= ((wxDouble
) w
) / h
;
209 Scale( factor
, 1.0) ;
210 path
->AddCircle(0,0,h
/2);
217 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
219 wxGraphicsPath
* path
= CreatePath();
222 path
->AddRectangle( x
, y
, w
, h
);
230 double fw
= w
/ radius
;
231 double fh
= h
/ radius
;
233 path
->MoveToPoint(w
, h
/ 2);
234 path
->AddArcToPoint(w
, h
, w
/ 2, h
, radius
);
235 path
->AddArcToPoint(0, h
, 0, h
/ 2, radius
);
236 path
->AddArcToPoint(0, 0, w
/ 2, 0, radius
);
237 path
->AddArcToPoint(w
, 0, w
, h
/ 2, radius
);
238 path
->CloseSubpath();
245 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
248 wxGraphicsPath
* path
= CreatePath();
249 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
250 for ( int i
= 1 ; i
< n
; ++i
)
251 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
256 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
259 wxGraphicsPath
* path
= CreatePath();
260 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
261 for ( int i
= 1 ; i
< n
; ++i
)
262 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
263 DrawPath( path
, fillStyle
);
267 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
270 wxGraphicsPath
* path
= CreatePath();
271 for ( int i
= 0 ; i
< n
; ++i
)
273 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
) ;
274 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
280 IMPLEMENT_ABSTRACT_CLASS(wxGCDC
, wxObject
)
282 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------
292 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
295 m_graphicContext
= wxGraphicsContext::Create(dc
);
297 if ( dc
.GetFont().Ok())
298 m_graphicContext
->SetFont(dc
.GetFont());
305 m_mm_to_pix_x
= mm2pt
;
306 m_mm_to_pix_y
= mm2pt
;
308 m_pen
= *wxBLACK_PEN
;
309 m_font
= *wxNORMAL_FONT
;
310 m_brush
= *wxWHITE_BRUSH
;
312 m_graphicContext
= NULL
;
318 delete m_graphicContext
;
321 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
323 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
324 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
326 wxCoord xx
= LogicalToDeviceX(x
);
327 wxCoord yy
= LogicalToDeviceY(y
);
328 wxCoord w
= bmp
.GetWidth();
329 wxCoord h
= bmp
.GetHeight();
330 wxCoord ww
= LogicalToDeviceXRel(w
);
331 wxCoord hh
= LogicalToDeviceYRel(h
);
333 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
336 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
338 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
339 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
341 wxCoord xx
= LogicalToDeviceX(x
);
342 wxCoord yy
= LogicalToDeviceY(y
);
343 wxCoord w
= icon
.GetWidth();
344 wxCoord h
= icon
.GetHeight();
345 wxCoord ww
= LogicalToDeviceXRel(w
);
346 wxCoord hh
= LogicalToDeviceYRel(h
);
348 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
351 void wxGCDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
355 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
357 wxCoord xx
, yy
, ww
, hh
;
358 xx
= LogicalToDeviceX(x
);
359 yy
= LogicalToDeviceY(y
);
360 ww
= LogicalToDeviceXRel(width
);
361 hh
= LogicalToDeviceYRel(height
);
363 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
364 CGRect clipRect
= CGRectMake( xx
, yy
, ww
, hh
);
365 CGContextClipToRect( cgContext
, clipRect
);
367 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
368 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
372 m_clipX1
= wxMax( m_clipX1
, xx
);
373 m_clipY1
= wxMax( m_clipY1
, yy
);
374 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
375 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
387 // TODO: as soon as we don't reset the context for each operation anymore
388 // we have to update the context as well
393 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
395 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
399 DestroyClippingRegion();
404 region
.GetBox( x
, y
, w
, h
);
405 wxCoord xx
, yy
, ww
, hh
;
406 xx
= LogicalToDeviceX(x
);
407 yy
= LogicalToDeviceY(y
);
408 ww
= LogicalToDeviceXRel(w
);
409 hh
= LogicalToDeviceYRel(h
);
411 // if we have a scaling that we cannot map onto native regions
412 // we must use the box
413 if ( ww
!= w
|| hh
!= h
)
415 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
420 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
);
421 if ( xx
!= x
|| yy
!= y
)
422 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
);
423 SectRgn( (RgnHandle
)m_macCurrentClipRgn
, (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
428 m_clipX1
= wxMax( m_clipX1
, xx
);
429 m_clipY1
= wxMax( m_clipY1
, yy
);
430 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
431 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
445 void wxGCDC::DestroyClippingRegion()
449 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
451 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
452 CGContextRestoreGState( cgContext
);
453 CGContextSaveGState( cgContext
);
455 m_graphicContext
->SetPen( m_pen
);
456 m_graphicContext
->SetBrush( m_brush
);
463 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
469 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
471 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
474 void wxGCDC::SetTextForeground( const wxColour
&col
)
476 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
478 if ( col
!= m_textForegroundColour
)
480 m_textForegroundColour
= col
;
481 m_graphicContext
->SetTextColor( col
);
485 void wxGCDC::SetTextBackground( const wxColour
&col
)
487 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
489 m_textBackgroundColour
= col
;
492 void wxGCDC::SetMapMode( int mode
)
497 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
501 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
505 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
509 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
514 SetLogicalScale( 1.0, 1.0 );
518 ComputeScaleAndOrigin();
521 void wxGCDC::SetUserScale( double x
, double y
)
523 // allow negative ? -> no
526 ComputeScaleAndOrigin();
529 void wxGCDC::SetLogicalScale( double x
, double y
)
534 ComputeScaleAndOrigin();
537 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
539 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
540 m_logicalOriginY
= y
* m_signY
;
541 ComputeScaleAndOrigin();
544 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
548 ComputeScaleAndOrigin();
551 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
553 m_signX
= (xLeftRight
? 1 : -1);
554 m_signY
= (yBottomUp
? -1 : 1);
555 ComputeScaleAndOrigin();
558 wxSize
wxGCDC::GetPPI() const
560 return wxSize(72, 72);
563 int wxGCDC::GetDepth() const
568 void wxGCDC::ComputeScaleAndOrigin()
570 // this is a bit artificial, but we need to force wxGCDC to think
571 // the pen has changed
572 wxPen
pen( GetPen() );
578 void wxGCDC::SetPalette( const wxPalette
& palette
)
581 void wxGCDC::SetBackgroundMode( int mode
)
583 m_backgroundMode
= mode
;
586 void wxGCDC::SetFont( const wxFont
&font
)
589 if ( m_graphicContext
)
590 m_graphicContext
->SetFont( font
);
593 void wxGCDC::SetPen( const wxPen
&pen
)
599 if ( m_graphicContext
)
601 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
603 m_graphicContext
->SetPen( m_pen
);
607 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
608 // eg when using a wxScrollWindow and scrolling around
609 int origX
= LogicalToDeviceX( 0 );
610 int origY
= LogicalToDeviceY( 0 );
611 m_graphicContext
->Translate( origX
, origY
);
612 m_graphicContext
->SetPen( m_pen
);
613 m_graphicContext
->Translate( -origX
, -origY
);
618 void wxGCDC::SetBrush( const wxBrush
&brush
)
620 if (m_brush
== brush
)
624 if ( m_graphicContext
)
626 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
628 m_graphicContext
->SetBrush( m_brush
);
632 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
633 // eg when using a wxScrollWindow and scrolling around
634 // TODO on MSW / GDIPlus this still occurs with hatched brushes
635 int origX
= LogicalToDeviceX(0);
636 int origY
= LogicalToDeviceY(0);
637 m_graphicContext
->Translate( origX
, origY
);
638 m_graphicContext
->SetBrush( m_brush
);
639 m_graphicContext
->Translate( -origX
, -origY
);
644 void wxGCDC::SetBackground( const wxBrush
&brush
)
646 if (m_backgroundBrush
== brush
)
649 m_backgroundBrush
= brush
;
650 if (!m_backgroundBrush
.Ok())
654 void wxGCDC::SetLogicalFunction( int function
)
656 if (m_logicalFunction
== function
)
659 m_logicalFunction
= function
;
660 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
662 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
663 if ( m_logicalFunction
== wxCOPY
)
664 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
665 else if ( m_logicalFunction
== wxINVERT
)
666 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
668 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
673 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
674 const wxColour
& col
, int style
);
676 bool wxGCDC::DoFloodFill(wxCoord x
, wxCoord y
,
677 const wxColour
& col
, int style
)
679 // return wxDoFloodFill(this, x, y, col, style);
683 bool wxGCDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
685 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
689 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
691 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
693 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
695 if ( m_logicalFunction
!= wxCOPY
)
699 wxCoord xx1
= LogicalToDeviceX(x1
);
700 wxCoord yy1
= LogicalToDeviceY(y1
);
701 wxCoord xx2
= LogicalToDeviceX(x2
);
702 wxCoord yy2
= LogicalToDeviceY(y2
);
704 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
706 CalcBoundingBox(x1
, y1
);
707 CalcBoundingBox(x2
, y2
);
710 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
712 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
714 if ( m_logicalFunction
!= wxCOPY
)
721 wxCoord xx
= LogicalToDeviceX(x
);
722 wxCoord yy
= LogicalToDeviceY(y
);
723 wxCoord xw
= LogicalToDeviceX(w
);
724 wxCoord x0
= LogicalToDeviceX(0);
725 wxCoord y0
= LogicalToDeviceY(0);
726 wxCoord yh
= LogicalToDeviceY(h
);
728 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
729 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
731 CalcBoundingBox(x0
, y0
);
732 CalcBoundingBox(x0
+xw
, y0
+yh
);
735 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
736 wxCoord x2
, wxCoord y2
,
737 wxCoord xc
, wxCoord yc
)
739 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
741 if ( m_logicalFunction
!= wxCOPY
)
744 wxCoord xx1
= LogicalToDeviceX(x1
);
745 wxCoord yy1
= LogicalToDeviceY(y1
);
746 wxCoord xx2
= LogicalToDeviceX(x2
);
747 wxCoord yy2
= LogicalToDeviceY(y2
);
748 wxCoord xxc
= LogicalToDeviceX(xc
);
749 wxCoord yyc
= LogicalToDeviceY(yc
);
751 double dx
= xx1
- xxc
;
752 double dy
= yy1
- yyc
;
753 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
754 wxCoord rad
= (wxCoord
)radius
;
756 if (xx1
== xx2
&& yy1
== yy2
)
761 else if (radius
== 0.0)
767 sa
= (xx1
- xxc
== 0) ?
768 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
769 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
770 ea
= (xx2
- xxc
== 0) ?
771 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
772 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
775 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
777 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
778 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
779 path
->MoveToPoint( xxc
, yyc
);
780 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
781 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
782 path
->AddLineToPoint( xxc
, yyc
);
783 m_graphicContext
->DrawPath(path
);
787 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
788 double sa
, double ea
)
790 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
792 if ( m_logicalFunction
!= wxCOPY
)
795 wxCoord xx
= LogicalToDeviceX(x
);
796 wxCoord yy
= LogicalToDeviceY(y
);
797 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
798 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
800 // handle -ve width and/or height
812 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
814 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
815 m_graphicContext
->PushState();
816 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
817 wxDouble factor
= ((wxDouble
) ww
) / hh
;
818 m_graphicContext
->Scale( factor
, 1.0) ;
819 if ( fill
&& (sa
!=ea
) )
820 path
->MoveToPoint(0,0);
821 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
822 // get clockwise angles
823 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
824 if ( fill
&& (sa
!=ea
) )
825 path
->AddLineToPoint(0,0);
826 m_graphicContext
->DrawPath( path
);
827 m_graphicContext
->PopState();
831 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
833 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
835 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
838 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
839 wxCoord xoffset
, wxCoord yoffset
)
841 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
843 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
845 if ( m_logicalFunction
!= wxCOPY
)
849 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
] ;
850 for( int i
= 0 ; i
< n
; ++i
)
852 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
853 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
856 m_graphicContext
->StrokeLines( n
, pointsD
) ;
861 void wxGCDC::DoDrawSpline(wxList
*points
)
863 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
865 if ( m_logicalFunction
!= wxCOPY
)
868 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
870 wxList::compatibility_iterator node
= points
->GetFirst();
871 if (node
== wxList::compatibility_iterator())
875 wxPoint
*p
= (wxPoint
*)node
->GetData();
880 node
= node
->GetNext();
881 p
= (wxPoint
*)node
->GetData();
885 wxCoord cx1
= ( x1
+ x2
) / 2;
886 wxCoord cy1
= ( y1
+ y2
) / 2;
888 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
889 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
892 while ((node
= node
->GetNext()) != NULL
)
895 while ((node
= node
->GetNext()))
899 p
= (wxPoint
*)node
->GetData();
904 wxCoord cx4
= (x1
+ x2
) / 2;
905 wxCoord cy4
= (y1
+ y2
) / 2;
907 path
->AddQuadCurveToPoint(
908 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
909 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
915 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
917 m_graphicContext
->StrokePath( path
);
922 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
923 wxCoord xoffset
, wxCoord yoffset
,
926 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
928 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
930 if ( m_logicalFunction
!= wxCOPY
)
933 bool closeIt
= false ;
934 if (points
[n
-1] != points
[0])
937 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)] ;
938 for( int i
= 0 ; i
< n
; ++i
)
940 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
941 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
944 pointsD
[n
] = pointsD
[0];
946 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
) ;
950 void wxGCDC::DoDrawPolyPolygon(int n
,
958 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
961 for ( int j
= 0 ; j
< n
; ++j
)
963 wxPoint start
= points
[i
];
964 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
)) ;
967 for ( int k
= 1 ; k
< l
; ++k
)
969 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
973 if ( start
!= points
[i
-1])
974 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
976 m_graphicContext
->DrawPath( path
, fillStyle
);
980 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
982 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
984 if ( m_logicalFunction
!= wxCOPY
)
987 wxCoord xx
= LogicalToDeviceX(x
);
988 wxCoord yy
= LogicalToDeviceY(y
);
989 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
990 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
992 // CMB: draw nothing if transformed w or h is 0
993 if (ww
== 0 || hh
== 0)
996 // CMB: handle -ve width and/or height
1007 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1010 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1011 wxCoord width
, wxCoord height
,
1014 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1016 if ( m_logicalFunction
!= wxCOPY
)
1020 radius
= - radius
* ((width
< height
) ? width
: height
);
1021 wxCoord xx
= LogicalToDeviceX(x
);
1022 wxCoord yy
= LogicalToDeviceY(y
);
1023 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1024 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1026 // CMB: draw nothing if transformed w or h is 0
1027 if (ww
== 0 || hh
== 0)
1030 // CMB: handle -ve width and/or height
1042 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1045 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1047 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1049 if ( m_logicalFunction
!= wxCOPY
)
1052 wxCoord xx
= LogicalToDeviceX(x
);
1053 wxCoord yy
= LogicalToDeviceY(y
);
1054 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1055 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1057 // CMB: draw nothing if transformed w or h is 0
1058 if (ww
== 0 || hh
== 0)
1061 // CMB: handle -ve width and/or height
1073 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1076 bool wxGCDC::CanDrawBitmap() const
1081 bool wxGCDC::DoBlit(
1082 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1083 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1084 wxCoord xsrcMask
, wxCoord ysrcMask
)
1086 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1087 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1089 if ( logical_func
== wxNO_OP
)
1092 if (xsrcMask
== -1 && ysrcMask
== -1)
1098 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1099 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1100 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1101 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1103 wxCoord yydest
= LogicalToDeviceY(ydest
);
1104 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1105 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1106 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1108 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1109 if ( memdc
&& logical_func
== wxCOPY
)
1112 wxBitmap blit
= memdc
->GetSelectedObject();
1114 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1116 wxCoord bmpwidth
= blit
.GetWidth();
1117 wxCoord bmpheight
= blit
.GetHeight();
1119 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1121 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1122 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1123 if ( wwsrc
> 0 && hhsrc
> 0 )
1125 if ( xxsrc
>= 0 && yysrc
>= 0 )
1127 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1128 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1129 blit
= blit
.GetSubBitmap( subrect
);
1133 // in this case we'd probably have to adjust the different coordinates, but
1134 // we have to find out proper contract first
1135 blit
= wxNullBitmap
;
1140 blit
= wxNullBitmap
;
1146 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1153 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
1160 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1163 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1165 if ( str
.length() == 0 )
1167 if ( m_logicalFunction
!= wxCOPY
)
1170 int drawX
= LogicalToDeviceX(x
);
1171 int drawY
= LogicalToDeviceY(y
);
1173 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1176 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1178 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1180 if ( str
.length() == 0 )
1182 if ( m_logicalFunction
!= wxCOPY
)
1185 int drawX
= LogicalToDeviceX(x
);
1186 int drawY
= LogicalToDeviceY(y
);
1188 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1191 bool wxGCDC::CanGetTextExtent() const
1193 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1198 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1199 wxCoord
*descent
, wxCoord
*externalLeading
,
1200 wxFont
*theFont
) const
1202 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1204 wxFont formerFont
= m_font
;
1207 m_graphicContext
->SetFont( *theFont
);
1210 wxDouble h
, d
, e
, w
;
1212 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1215 *height
= DeviceToLogicalYRel( h
);
1217 *descent
=DeviceToLogicalYRel( d
);
1218 if ( externalLeading
)
1219 *externalLeading
= DeviceToLogicalYRel( e
);
1221 *width
= DeviceToLogicalXRel( w
);
1225 m_graphicContext
->SetFont( m_font
);
1229 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1231 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1233 widths
.Add(0,text
.Length());
1234 if ( text
.IsEmpty() )
1237 wxArrayDouble widthsD
;
1239 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1240 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1241 widths
[i
] = DeviceToLogicalXRel( widthsD
[i
] + 0.5 ) ;
1246 wxCoord
wxGCDC::GetCharWidth(void) const
1249 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1254 wxCoord
wxGCDC::GetCharHeight(void) const
1257 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1262 void wxGCDC::Clear(void)
1264 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1268 void wxGCDC::DoGetSize(int *width
, int *height
) const
1274 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1275 const wxColour
& initialColour
,
1276 const wxColour
& destColour
,
1277 wxDirection nDirection
)
1284 start
= rect
.GetRightBottom();
1286 end
= rect
.GetLeftBottom();
1289 start
= rect
.GetLeftBottom();
1290 end
= rect
.GetRightBottom();
1294 start
= rect
.GetLeftBottom();
1296 end
= rect
.GetLeftTop();
1299 start
= rect
.GetLeftTop();
1300 end
= rect
.GetLeftBottom();
1305 m_graphicContext
->SetLinearGradientBrush(
1306 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1307 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1309 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1310 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1311 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1312 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1314 if (ww
== 0 || hh
== 0)
1328 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1329 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1330 m_graphicContext
->SetPen(m_pen
);
1333 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1334 const wxColour
& initialColour
,
1335 const wxColour
& destColour
,
1336 const wxPoint
& circleCenter
)
1339 wxInt32 cx
= rect
.GetWidth() / 2;
1340 wxInt32 cy
= rect
.GetHeight() / 2;
1347 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1348 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1349 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1350 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1352 if (ww
== 0 || hh
== 0)
1366 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1367 m_graphicContext
->SetBrush( wxBrush( destColour
) ) ;
1368 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1370 m_graphicContext
->SetRadialGradientBrush(
1371 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1372 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1373 LogicalToDeviceXRel(nRadius
),
1374 initialColour
,destColour
);
1376 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1377 m_graphicContext
->SetPen(m_pen
);
1380 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1381 wxCoord width
, wxCoord height
)
1383 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);