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 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 const double RAD2DEG
= 180.0 / M_PI
;
42 const short kEmulatedMode
= -1;
43 const short kUnsupportedMode
= -2;
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 static inline double dmin(double a
, double b
)
53 static inline double dmax(double a
, double b
)
58 static inline double DegToRad(double deg
)
60 return (deg
* M_PI
) / 180.0;
62 static inline double RadToDeg(double deg
)
64 return (deg
* 180.0) / M_PI
;
68 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
72 return wxPoint2DDouble(x
,y
);
75 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
77 MoveToPoint( p
.m_x
, p
.m_y
);
80 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
82 AddLineToPoint( p
.m_x
, p
.m_y
);
85 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
87 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
90 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
92 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
99 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
101 // calculate using degree elevation to a cubic bezier
105 wxPoint2DDouble start
= GetCurrentPoint() ;
106 wxPoint2DDouble
end(x
,y
);
107 wxPoint2DDouble
c(cx
,cy
);
108 c1
= (1/3.0) * start
+ (2/3.0) * c
;
109 c2
= (2/3.0) * c
+ (1/3.0) * end
;
110 AddCurveToPoint(c1
.m_x
,c1
.m_y
,c2
.m_x
,c2
.m_y
,x
,y
);
113 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
116 AddLineToPoint(x
,y
+h
);
117 AddLineToPoint(x
+w
,y
+h
);
118 AddLineToPoint(x
+w
,y
);
122 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
125 AddArc( x
,y
,r
,0,2*M_PI
,false);
129 // 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)
130 void wxGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
132 wxPoint2DDouble current
= GetCurrentPoint();
133 wxPoint2DDouble
p1(x1
,y1
);
134 wxPoint2DDouble
p2(x2
,y2
);
136 wxPoint2DDouble v1
= current
- p1
;
138 wxPoint2DDouble v2
= p2
- p1
;
141 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
144 alpha
= 360 + alpha
;
145 // TODO obtuse angles
147 alpha
= DegToRad(alpha
);
149 wxDouble dist
= r
/ sin(alpha
/2) * cos(alpha
/2) ;
150 // calculate tangential points
151 wxPoint2DDouble t1
= dist
*v1
+ p1
;
152 wxPoint2DDouble t2
= dist
*v2
+ p1
;
154 wxPoint2DDouble nv1
= v1
;
155 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
156 wxPoint2DDouble c
= t1
+ r
*nv1
;
158 wxDouble a1
= v1
.GetVectorAngle()+90 ;
159 wxDouble a2
= v2
.GetVectorAngle()-90 ;
162 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
166 //-----------------------------------------------------------------------------
167 // wxGraphicsContext Convenience Methods
168 //-----------------------------------------------------------------------------
170 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
172 FillPath( path
, fillStyle
);
176 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
180 DrawText( str
, 0, 0 );
185 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
187 wxGraphicsPath
* path
= CreatePath();
188 path
->MoveToPoint(x1
, y1
) ;
189 path
->AddLineToPoint( x2
, y2
);
194 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
196 wxGraphicsPath
* path
= CreatePath();
197 path
->AddRectangle( x
, y
, w
, h
);
202 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
204 wxGraphicsPath
* path
= CreatePath();
207 path
->AddCircle( x
+w
/2,y
+w
/2,w
/2);
213 Translate(x
+w
/2,y
+h
/2);
214 wxDouble factor
= ((wxDouble
) w
) / h
;
215 Scale( factor
, 1.0) ;
216 path
->AddCircle(0,0,h
/2);
223 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
225 wxGraphicsPath
* path
= CreatePath();
228 path
->AddRectangle( x
, y
, w
, h
);
236 double fw
= w
/ radius
;
237 double fh
= h
/ radius
;
239 path
->MoveToPoint(w
, h
/ 2);
240 path
->AddArcToPoint(w
, h
, w
/ 2, h
, radius
);
241 path
->AddArcToPoint(0, h
, 0, h
/ 2, radius
);
242 path
->AddArcToPoint(0, 0, w
/ 2, 0, radius
);
243 path
->AddArcToPoint(w
, 0, w
, h
/ 2, radius
);
244 path
->CloseSubpath();
251 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
254 wxGraphicsPath
* path
= CreatePath();
255 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
256 for ( int i
= 1 ; i
< n
; ++i
)
257 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
262 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
265 wxGraphicsPath
* path
= CreatePath();
266 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
267 for ( int i
= 1 ; i
< n
; ++i
)
268 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
269 DrawPath( path
, fillStyle
);
273 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
276 wxGraphicsPath
* path
= CreatePath();
277 for ( int i
= 0 ; i
< n
; ++i
)
279 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
) ;
280 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
286 IMPLEMENT_ABSTRACT_CLASS(wxGCDC
, wxObject
)
288 //-----------------------------------------------------------------------------
290 //-----------------------------------------------------------------------------
298 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
301 m_graphicContext
= wxGraphicsContext::Create(dc
);
303 if ( dc
.GetFont().Ok())
304 m_graphicContext
->SetFont(dc
.GetFont());
311 m_mm_to_pix_x
= mm2pt
;
312 m_mm_to_pix_y
= mm2pt
;
314 m_pen
= *wxBLACK_PEN
;
315 m_font
= *wxNORMAL_FONT
;
316 m_brush
= *wxWHITE_BRUSH
;
318 m_graphicContext
= NULL
;
324 delete m_graphicContext
;
327 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
329 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
330 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
332 wxCoord xx
= LogicalToDeviceX(x
);
333 wxCoord yy
= LogicalToDeviceY(y
);
334 wxCoord w
= bmp
.GetWidth();
335 wxCoord h
= bmp
.GetHeight();
336 wxCoord ww
= LogicalToDeviceXRel(w
);
337 wxCoord hh
= LogicalToDeviceYRel(h
);
339 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
342 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
344 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
345 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
347 wxCoord xx
= LogicalToDeviceX(x
);
348 wxCoord yy
= LogicalToDeviceY(y
);
349 wxCoord w
= icon
.GetWidth();
350 wxCoord h
= icon
.GetHeight();
351 wxCoord ww
= LogicalToDeviceXRel(w
);
352 wxCoord hh
= LogicalToDeviceYRel(h
);
354 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
357 void wxGCDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
361 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
363 wxCoord xx
, yy
, ww
, hh
;
364 xx
= LogicalToDeviceX(x
);
365 yy
= LogicalToDeviceY(y
);
366 ww
= LogicalToDeviceXRel(width
);
367 hh
= LogicalToDeviceYRel(height
);
369 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
370 CGRect clipRect
= CGRectMake( xx
, yy
, ww
, hh
);
371 CGContextClipToRect( cgContext
, clipRect
);
373 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
374 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
378 m_clipX1
= wxMax( m_clipX1
, xx
);
379 m_clipY1
= wxMax( m_clipY1
, yy
);
380 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
381 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
393 // TODO: as soon as we don't reset the context for each operation anymore
394 // we have to update the context as well
399 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
401 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
405 DestroyClippingRegion();
410 region
.GetBox( x
, y
, w
, h
);
411 wxCoord xx
, yy
, ww
, hh
;
412 xx
= LogicalToDeviceX(x
);
413 yy
= LogicalToDeviceY(y
);
414 ww
= LogicalToDeviceXRel(w
);
415 hh
= LogicalToDeviceYRel(h
);
417 // if we have a scaling that we cannot map onto native regions
418 // we must use the box
419 if ( ww
!= w
|| hh
!= h
)
421 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
426 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
);
427 if ( xx
!= x
|| yy
!= y
)
428 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
);
429 SectRgn( (RgnHandle
)m_macCurrentClipRgn
, (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
434 m_clipX1
= wxMax( m_clipX1
, xx
);
435 m_clipY1
= wxMax( m_clipY1
, yy
);
436 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
437 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
451 void wxGCDC::DestroyClippingRegion()
455 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
457 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
458 CGContextRestoreGState( cgContext
);
459 CGContextSaveGState( cgContext
);
461 m_graphicContext
->SetPen( m_pen
);
462 m_graphicContext
->SetBrush( m_brush
);
469 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
475 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
477 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
480 void wxGCDC::SetTextForeground( const wxColour
&col
)
482 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
484 if ( col
!= m_textForegroundColour
)
486 m_textForegroundColour
= col
;
487 m_graphicContext
->SetTextColor( col
);
491 void wxGCDC::SetTextBackground( const wxColour
&col
)
493 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
495 m_textBackgroundColour
= col
;
498 void wxGCDC::SetMapMode( int mode
)
503 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
507 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
511 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
515 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
520 SetLogicalScale( 1.0, 1.0 );
524 ComputeScaleAndOrigin();
527 void wxGCDC::SetUserScale( double x
, double y
)
529 // allow negative ? -> no
532 ComputeScaleAndOrigin();
535 void wxGCDC::SetLogicalScale( double x
, double y
)
540 ComputeScaleAndOrigin();
543 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
545 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
546 m_logicalOriginY
= y
* m_signY
;
547 ComputeScaleAndOrigin();
550 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
554 ComputeScaleAndOrigin();
557 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
559 m_signX
= (xLeftRight
? 1 : -1);
560 m_signY
= (yBottomUp
? -1 : 1);
561 ComputeScaleAndOrigin();
564 wxSize
wxGCDC::GetPPI() const
566 return wxSize(72, 72);
569 int wxGCDC::GetDepth() const
574 void wxGCDC::ComputeScaleAndOrigin()
576 // this is a bit artificial, but we need to force wxGCDC to think
577 // the pen has changed
578 wxPen
pen( GetPen() );
584 void wxGCDC::SetPalette( const wxPalette
& palette
)
587 void wxGCDC::SetBackgroundMode( int mode
)
589 m_backgroundMode
= mode
;
592 void wxGCDC::SetFont( const wxFont
&font
)
595 if ( m_graphicContext
)
596 m_graphicContext
->SetFont( font
);
599 void wxGCDC::SetPen( const wxPen
&pen
)
605 if ( m_graphicContext
)
607 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
609 m_graphicContext
->SetPen( m_pen
);
613 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
614 // eg when using a wxScrollWindow and scrolling around
615 int origX
= LogicalToDeviceX( 0 );
616 int origY
= LogicalToDeviceY( 0 );
617 m_graphicContext
->Translate( origX
, origY
);
618 m_graphicContext
->SetPen( m_pen
);
619 m_graphicContext
->Translate( -origX
, -origY
);
624 void wxGCDC::SetBrush( const wxBrush
&brush
)
626 if (m_brush
== brush
)
630 if ( m_graphicContext
)
632 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
634 m_graphicContext
->SetBrush( m_brush
);
638 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
639 // eg when using a wxScrollWindow and scrolling around
640 // TODO on MSW / GDIPlus this still occurs with hatched brushes
641 int origX
= LogicalToDeviceX(0);
642 int origY
= LogicalToDeviceY(0);
643 m_graphicContext
->Translate( origX
, origY
);
644 m_graphicContext
->SetBrush( m_brush
);
645 m_graphicContext
->Translate( -origX
, -origY
);
650 void wxGCDC::SetBackground( const wxBrush
&brush
)
652 if (m_backgroundBrush
== brush
)
655 m_backgroundBrush
= brush
;
656 if (!m_backgroundBrush
.Ok())
660 void wxGCDC::SetLogicalFunction( int function
)
662 if (m_logicalFunction
== function
)
665 m_logicalFunction
= function
;
666 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
668 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
669 if ( m_logicalFunction
== wxCOPY
)
670 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
671 else if ( m_logicalFunction
== wxINVERT
)
672 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
674 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
679 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
680 const wxColour
& col
, int style
);
682 bool wxGCDC::DoFloodFill(wxCoord x
, wxCoord y
,
683 const wxColour
& col
, int style
)
685 // return wxDoFloodFill(this, x, y, col, style);
689 bool wxGCDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
691 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
695 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
697 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
699 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
701 if ( m_logicalFunction
!= wxCOPY
)
705 wxCoord xx1
= LogicalToDeviceX(x1
);
706 wxCoord yy1
= LogicalToDeviceY(y1
);
707 wxCoord xx2
= LogicalToDeviceX(x2
);
708 wxCoord yy2
= LogicalToDeviceY(y2
);
710 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
712 CalcBoundingBox(x1
, y1
);
713 CalcBoundingBox(x2
, y2
);
716 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
718 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
720 if ( m_logicalFunction
!= wxCOPY
)
727 wxCoord xx
= LogicalToDeviceX(x
);
728 wxCoord yy
= LogicalToDeviceY(y
);
729 wxCoord xw
= LogicalToDeviceX(w
);
730 wxCoord x0
= LogicalToDeviceX(0);
731 wxCoord y0
= LogicalToDeviceY(0);
732 wxCoord yh
= LogicalToDeviceY(h
);
734 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
735 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
737 CalcBoundingBox(x0
, y0
);
738 CalcBoundingBox(x0
+xw
, y0
+yh
);
741 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
742 wxCoord x2
, wxCoord y2
,
743 wxCoord xc
, wxCoord yc
)
745 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
747 if ( m_logicalFunction
!= wxCOPY
)
750 wxCoord xx1
= LogicalToDeviceX(x1
);
751 wxCoord yy1
= LogicalToDeviceY(y1
);
752 wxCoord xx2
= LogicalToDeviceX(x2
);
753 wxCoord yy2
= LogicalToDeviceY(y2
);
754 wxCoord xxc
= LogicalToDeviceX(xc
);
755 wxCoord yyc
= LogicalToDeviceY(yc
);
757 double dx
= xx1
- xxc
;
758 double dy
= yy1
- yyc
;
759 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
760 wxCoord rad
= (wxCoord
)radius
;
762 if (xx1
== xx2
&& yy1
== yy2
)
767 else if (radius
== 0.0)
773 sa
= (xx1
- xxc
== 0) ?
774 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
775 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
776 ea
= (xx2
- xxc
== 0) ?
777 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
778 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
781 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
783 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
784 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
785 path
->MoveToPoint( xxc
, yyc
);
786 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
787 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
788 path
->AddLineToPoint( xxc
, yyc
);
789 m_graphicContext
->DrawPath(path
);
793 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
794 double sa
, double ea
)
796 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
798 if ( m_logicalFunction
!= wxCOPY
)
801 wxCoord xx
= LogicalToDeviceX(x
);
802 wxCoord yy
= LogicalToDeviceY(y
);
803 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
804 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
806 // handle -ve width and/or height
818 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
820 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
821 m_graphicContext
->PushState();
822 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
823 wxDouble factor
= ((wxDouble
) ww
) / hh
;
824 m_graphicContext
->Scale( factor
, 1.0) ;
825 if ( fill
&& (sa
!=ea
) )
826 path
->MoveToPoint(0,0);
827 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
828 // get clockwise angles
829 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
830 if ( fill
&& (sa
!=ea
) )
831 path
->AddLineToPoint(0,0);
832 m_graphicContext
->DrawPath( path
);
833 m_graphicContext
->PopState();
837 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
839 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
841 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
844 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
845 wxCoord xoffset
, wxCoord yoffset
)
847 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
849 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
851 if ( m_logicalFunction
!= wxCOPY
)
855 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
] ;
856 for( int i
= 0 ; i
< n
; ++i
)
858 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
859 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
862 m_graphicContext
->StrokeLines( n
, pointsD
) ;
867 void wxGCDC::DoDrawSpline(wxList
*points
)
869 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
871 if ( m_logicalFunction
!= wxCOPY
)
874 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
876 wxList::compatibility_iterator node
= points
->GetFirst();
877 if (node
== wxList::compatibility_iterator())
881 wxPoint
*p
= (wxPoint
*)node
->GetData();
886 node
= node
->GetNext();
887 p
= (wxPoint
*)node
->GetData();
891 wxCoord cx1
= ( x1
+ x2
) / 2;
892 wxCoord cy1
= ( y1
+ y2
) / 2;
894 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
895 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
898 while ((node
= node
->GetNext()) != NULL
)
901 while ((node
= node
->GetNext()))
905 p
= (wxPoint
*)node
->GetData();
910 wxCoord cx4
= (x1
+ x2
) / 2;
911 wxCoord cy4
= (y1
+ y2
) / 2;
913 path
->AddQuadCurveToPoint(
914 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
915 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
921 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
923 m_graphicContext
->StrokePath( path
);
928 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
929 wxCoord xoffset
, wxCoord yoffset
,
932 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
934 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
936 if ( m_logicalFunction
!= wxCOPY
)
939 bool closeIt
= false ;
940 if (points
[n
-1] != points
[0])
943 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)] ;
944 for( int i
= 0 ; i
< n
; ++i
)
946 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
947 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
950 pointsD
[n
] = pointsD
[0];
952 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
) ;
956 void wxGCDC::DoDrawPolyPolygon(int n
,
964 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
967 for ( int j
= 0 ; j
< n
; ++j
)
969 wxPoint start
= points
[i
];
970 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
)) ;
973 for ( int k
= 1 ; k
< l
; ++k
)
975 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
979 if ( start
!= points
[i
-1])
980 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
982 m_graphicContext
->DrawPath( path
, fillStyle
);
986 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
988 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
990 if ( m_logicalFunction
!= wxCOPY
)
993 wxCoord xx
= LogicalToDeviceX(x
);
994 wxCoord yy
= LogicalToDeviceY(y
);
995 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
996 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
998 // CMB: draw nothing if transformed w or h is 0
999 if (ww
== 0 || hh
== 0)
1002 // CMB: handle -ve width and/or height
1013 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1016 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1017 wxCoord width
, wxCoord height
,
1020 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1022 if ( m_logicalFunction
!= wxCOPY
)
1026 radius
= - radius
* ((width
< height
) ? width
: height
);
1027 wxCoord xx
= LogicalToDeviceX(x
);
1028 wxCoord yy
= LogicalToDeviceY(y
);
1029 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1030 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1032 // CMB: draw nothing if transformed w or h is 0
1033 if (ww
== 0 || hh
== 0)
1036 // CMB: handle -ve width and/or height
1048 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1051 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1053 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1055 if ( m_logicalFunction
!= wxCOPY
)
1058 wxCoord xx
= LogicalToDeviceX(x
);
1059 wxCoord yy
= LogicalToDeviceY(y
);
1060 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1061 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1063 // CMB: draw nothing if transformed w or h is 0
1064 if (ww
== 0 || hh
== 0)
1067 // CMB: handle -ve width and/or height
1079 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1082 bool wxGCDC::CanDrawBitmap() const
1087 bool wxGCDC::DoBlit(
1088 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1089 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1090 wxCoord xsrcMask
, wxCoord ysrcMask
)
1092 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1093 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1095 if ( logical_func
== wxNO_OP
)
1098 if (xsrcMask
== -1 && ysrcMask
== -1)
1104 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1105 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1106 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1107 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1109 wxCoord yydest
= LogicalToDeviceY(ydest
);
1110 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1111 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1112 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1114 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1115 if ( memdc
&& logical_func
== wxCOPY
)
1118 wxBitmap blit
= memdc
->GetSelectedObject();
1120 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1122 wxCoord bmpwidth
= blit
.GetWidth();
1123 wxCoord bmpheight
= blit
.GetHeight();
1125 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1127 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1128 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1129 if ( wwsrc
> 0 && hhsrc
> 0 )
1131 if ( xxsrc
>= 0 && yysrc
>= 0 )
1133 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1134 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1135 blit
= blit
.GetSubBitmap( subrect
);
1139 // in this case we'd probably have to adjust the different coordinates, but
1140 // we have to find out proper contract first
1141 blit
= wxNullBitmap
;
1146 blit
= wxNullBitmap
;
1152 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1159 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
1166 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1169 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1171 if ( str
.length() == 0 )
1173 if ( m_logicalFunction
!= wxCOPY
)
1176 int drawX
= LogicalToDeviceX(x
);
1177 int drawY
= LogicalToDeviceY(y
);
1179 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1182 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1184 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1186 if ( str
.length() == 0 )
1188 if ( m_logicalFunction
!= wxCOPY
)
1191 int drawX
= LogicalToDeviceX(x
);
1192 int drawY
= LogicalToDeviceY(y
);
1194 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1197 bool wxGCDC::CanGetTextExtent() const
1199 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1204 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1205 wxCoord
*descent
, wxCoord
*externalLeading
,
1206 wxFont
*theFont
) const
1208 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1210 wxFont formerFont
= m_font
;
1213 m_graphicContext
->SetFont( *theFont
);
1216 wxDouble h
, d
, e
, w
;
1218 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1221 *height
= DeviceToLogicalYRel( h
);
1223 *descent
=DeviceToLogicalYRel( d
);
1224 if ( externalLeading
)
1225 *externalLeading
= DeviceToLogicalYRel( e
);
1227 *width
= DeviceToLogicalXRel( w
);
1231 m_graphicContext
->SetFont( m_font
);
1235 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1237 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1239 widths
.Add(0,text
.Length());
1240 if ( text
.IsEmpty() )
1243 wxArrayDouble widthsD
;
1245 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1246 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1247 widths
[i
] = DeviceToLogicalXRel( widthsD
[i
] + 0.5 ) ;
1252 wxCoord
wxGCDC::GetCharWidth(void) const
1255 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1260 wxCoord
wxGCDC::GetCharHeight(void) const
1263 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1268 void wxGCDC::Clear(void)
1270 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1274 void wxGCDC::DoGetSize(int *width
, int *height
) const
1280 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1281 const wxColour
& initialColour
,
1282 const wxColour
& destColour
,
1283 wxDirection nDirection
)
1290 start
= rect
.GetRightBottom();
1292 end
= rect
.GetLeftBottom();
1295 start
= rect
.GetLeftBottom();
1296 end
= rect
.GetRightBottom();
1300 start
= rect
.GetLeftBottom();
1302 end
= rect
.GetLeftTop();
1305 start
= rect
.GetLeftTop();
1306 end
= rect
.GetLeftBottom();
1311 m_graphicContext
->SetLinearGradientBrush(
1312 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1313 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1315 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1316 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1317 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1318 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1320 if (ww
== 0 || hh
== 0)
1334 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1335 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1336 m_graphicContext
->SetPen(m_pen
);
1339 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1340 const wxColour
& initialColour
,
1341 const wxColour
& destColour
,
1342 const wxPoint
& circleCenter
)
1345 wxInt32 cx
= rect
.GetWidth() / 2;
1346 wxInt32 cy
= rect
.GetHeight() / 2;
1353 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1354 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1355 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1356 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1358 if (ww
== 0 || hh
== 0)
1372 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1373 m_graphicContext
->SetBrush( wxBrush( destColour
) ) ;
1374 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1376 m_graphicContext
->SetRadialGradientBrush(
1377 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1378 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1379 LogicalToDeviceXRel(nRadius
),
1380 initialColour
,destColour
);
1382 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1383 m_graphicContext
->SetPen(m_pen
);
1386 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1387 wxCoord width
, wxCoord height
)
1389 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);