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 double fw
= w
/ radius
;
241 double fh
= h
/ radius
;
243 path
->MoveToPoint(w
, h
/ 2);
244 path
->AddArcToPoint(w
, h
, w
/ 2, h
, radius
);
245 path
->AddArcToPoint(0, h
, 0, h
/ 2, radius
);
246 path
->AddArcToPoint(0, 0, w
/ 2, 0, radius
);
247 path
->AddArcToPoint(w
, 0, w
, h
/ 2, radius
);
248 path
->CloseSubpath();
255 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
258 wxGraphicsPath
* path
= CreatePath();
259 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
260 for ( int i
= 1 ; i
< n
; ++i
)
261 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
266 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
269 wxGraphicsPath
* path
= CreatePath();
270 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
271 for ( int i
= 1 ; i
< n
; ++i
)
272 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
273 DrawPath( path
, fillStyle
);
277 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
280 wxGraphicsPath
* path
= CreatePath();
281 for ( int i
= 0 ; i
< n
; ++i
)
283 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
) ;
284 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
290 IMPLEMENT_ABSTRACT_CLASS(wxGCDC
, wxObject
)
292 //-----------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------
302 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
305 m_graphicContext
= wxGraphicsContext::Create(dc
);
307 if ( dc
.GetFont().Ok())
308 m_graphicContext
->SetFont(dc
.GetFont());
315 m_mm_to_pix_x
= mm2pt
;
316 m_mm_to_pix_y
= mm2pt
;
318 m_pen
= *wxBLACK_PEN
;
319 m_font
= *wxNORMAL_FONT
;
320 m_brush
= *wxWHITE_BRUSH
;
322 m_graphicContext
= NULL
;
328 delete m_graphicContext
;
331 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
333 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
334 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
336 wxCoord xx
= LogicalToDeviceX(x
);
337 wxCoord yy
= LogicalToDeviceY(y
);
338 wxCoord w
= bmp
.GetWidth();
339 wxCoord h
= bmp
.GetHeight();
340 wxCoord ww
= LogicalToDeviceXRel(w
);
341 wxCoord hh
= LogicalToDeviceYRel(h
);
343 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
346 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
348 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
349 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
351 wxCoord xx
= LogicalToDeviceX(x
);
352 wxCoord yy
= LogicalToDeviceY(y
);
353 wxCoord w
= icon
.GetWidth();
354 wxCoord h
= icon
.GetHeight();
355 wxCoord ww
= LogicalToDeviceXRel(w
);
356 wxCoord hh
= LogicalToDeviceYRel(h
);
358 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
361 void wxGCDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
365 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
367 wxCoord xx
, yy
, ww
, hh
;
368 xx
= LogicalToDeviceX(x
);
369 yy
= LogicalToDeviceY(y
);
370 ww
= LogicalToDeviceXRel(width
);
371 hh
= LogicalToDeviceYRel(height
);
373 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
374 CGRect clipRect
= CGRectMake( xx
, yy
, ww
, hh
);
375 CGContextClipToRect( cgContext
, clipRect
);
377 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
378 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
382 m_clipX1
= wxMax( m_clipX1
, xx
);
383 m_clipY1
= wxMax( m_clipY1
, yy
);
384 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
385 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
397 // TODO: as soon as we don't reset the context for each operation anymore
398 // we have to update the context as well
403 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
405 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
409 DestroyClippingRegion();
414 region
.GetBox( x
, y
, w
, h
);
415 wxCoord xx
, yy
, ww
, hh
;
416 xx
= LogicalToDeviceX(x
);
417 yy
= LogicalToDeviceY(y
);
418 ww
= LogicalToDeviceXRel(w
);
419 hh
= LogicalToDeviceYRel(h
);
421 // if we have a scaling that we cannot map onto native regions
422 // we must use the box
423 if ( ww
!= w
|| hh
!= h
)
425 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
430 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
);
431 if ( xx
!= x
|| yy
!= y
)
432 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
);
433 SectRgn( (RgnHandle
)m_macCurrentClipRgn
, (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
438 m_clipX1
= wxMax( m_clipX1
, xx
);
439 m_clipY1
= wxMax( m_clipY1
, yy
);
440 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
441 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
455 void wxGCDC::DestroyClippingRegion()
459 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
461 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
462 CGContextRestoreGState( cgContext
);
463 CGContextSaveGState( cgContext
);
465 m_graphicContext
->SetPen( m_pen
);
466 m_graphicContext
->SetBrush( m_brush
);
473 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
479 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
481 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
484 void wxGCDC::SetTextForeground( const wxColour
&col
)
486 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
488 if ( col
!= m_textForegroundColour
)
490 m_textForegroundColour
= col
;
491 m_graphicContext
->SetTextColor( col
);
495 void wxGCDC::SetTextBackground( const wxColour
&col
)
497 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
499 m_textBackgroundColour
= col
;
502 void wxGCDC::SetMapMode( int mode
)
507 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
511 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
515 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
519 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
524 SetLogicalScale( 1.0, 1.0 );
528 ComputeScaleAndOrigin();
531 void wxGCDC::SetUserScale( double x
, double y
)
533 // allow negative ? -> no
536 ComputeScaleAndOrigin();
539 void wxGCDC::SetLogicalScale( double x
, double y
)
544 ComputeScaleAndOrigin();
547 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
549 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
550 m_logicalOriginY
= y
* m_signY
;
551 ComputeScaleAndOrigin();
554 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
558 ComputeScaleAndOrigin();
561 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
563 m_signX
= (xLeftRight
? 1 : -1);
564 m_signY
= (yBottomUp
? -1 : 1);
565 ComputeScaleAndOrigin();
568 wxSize
wxGCDC::GetPPI() const
570 return wxSize(72, 72);
573 int wxGCDC::GetDepth() const
578 void wxGCDC::ComputeScaleAndOrigin()
580 // this is a bit artificial, but we need to force wxGCDC to think
581 // the pen has changed
582 wxPen
pen( GetPen() );
588 void wxGCDC::SetPalette( const wxPalette
& palette
)
591 void wxGCDC::SetBackgroundMode( int mode
)
593 m_backgroundMode
= mode
;
596 void wxGCDC::SetFont( const wxFont
&font
)
599 if ( m_graphicContext
)
600 m_graphicContext
->SetFont( font
);
603 void wxGCDC::SetPen( const wxPen
&pen
)
609 if ( m_graphicContext
)
611 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
613 m_graphicContext
->SetPen( m_pen
);
617 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
618 // eg when using a wxScrollWindow and scrolling around
619 int origX
= LogicalToDeviceX( 0 );
620 int origY
= LogicalToDeviceY( 0 );
621 m_graphicContext
->Translate( origX
, origY
);
622 m_graphicContext
->SetPen( m_pen
);
623 m_graphicContext
->Translate( -origX
, -origY
);
628 void wxGCDC::SetBrush( const wxBrush
&brush
)
630 if (m_brush
== brush
)
634 if ( m_graphicContext
)
636 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
638 m_graphicContext
->SetBrush( m_brush
);
642 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
643 // eg when using a wxScrollWindow and scrolling around
644 // TODO on MSW / GDIPlus this still occurs with hatched brushes
645 int origX
= LogicalToDeviceX(0);
646 int origY
= LogicalToDeviceY(0);
647 m_graphicContext
->Translate( origX
, origY
);
648 m_graphicContext
->SetBrush( m_brush
);
649 m_graphicContext
->Translate( -origX
, -origY
);
654 void wxGCDC::SetBackground( const wxBrush
&brush
)
656 if (m_backgroundBrush
== brush
)
659 m_backgroundBrush
= brush
;
660 if (!m_backgroundBrush
.Ok())
664 void wxGCDC::SetLogicalFunction( int function
)
666 if (m_logicalFunction
== function
)
669 m_logicalFunction
= function
;
670 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
672 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
673 if ( m_logicalFunction
== wxCOPY
)
674 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
675 else if ( m_logicalFunction
== wxINVERT
)
676 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
678 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
683 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
684 const wxColour
& col
, int style
);
686 bool wxGCDC::DoFloodFill(wxCoord x
, wxCoord y
,
687 const wxColour
& col
, int style
)
689 // return wxDoFloodFill(this, x, y, col, style);
693 bool wxGCDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
695 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
699 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
701 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
703 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
705 if ( m_logicalFunction
!= wxCOPY
)
709 wxCoord xx1
= LogicalToDeviceX(x1
);
710 wxCoord yy1
= LogicalToDeviceY(y1
);
711 wxCoord xx2
= LogicalToDeviceX(x2
);
712 wxCoord yy2
= LogicalToDeviceY(y2
);
714 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
716 CalcBoundingBox(x1
, y1
);
717 CalcBoundingBox(x2
, y2
);
720 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
722 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
724 if ( m_logicalFunction
!= wxCOPY
)
731 wxCoord xx
= LogicalToDeviceX(x
);
732 wxCoord yy
= LogicalToDeviceY(y
);
733 wxCoord xw
= LogicalToDeviceX(w
);
734 wxCoord x0
= LogicalToDeviceX(0);
735 wxCoord y0
= LogicalToDeviceY(0);
736 wxCoord yh
= LogicalToDeviceY(h
);
738 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
739 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
741 CalcBoundingBox(x0
, y0
);
742 CalcBoundingBox(x0
+xw
, y0
+yh
);
745 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
746 wxCoord x2
, wxCoord y2
,
747 wxCoord xc
, wxCoord yc
)
749 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
751 if ( m_logicalFunction
!= wxCOPY
)
754 wxCoord xx1
= LogicalToDeviceX(x1
);
755 wxCoord yy1
= LogicalToDeviceY(y1
);
756 wxCoord xx2
= LogicalToDeviceX(x2
);
757 wxCoord yy2
= LogicalToDeviceY(y2
);
758 wxCoord xxc
= LogicalToDeviceX(xc
);
759 wxCoord yyc
= LogicalToDeviceY(yc
);
761 double dx
= xx1
- xxc
;
762 double dy
= yy1
- yyc
;
763 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
764 wxCoord rad
= (wxCoord
)radius
;
766 if (xx1
== xx2
&& yy1
== yy2
)
771 else if (radius
== 0.0)
777 sa
= (xx1
- xxc
== 0) ?
778 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
779 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
780 ea
= (xx2
- xxc
== 0) ?
781 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
782 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
785 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
787 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
788 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
789 path
->MoveToPoint( xxc
, yyc
);
790 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
791 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
792 path
->AddLineToPoint( xxc
, yyc
);
793 m_graphicContext
->DrawPath(path
);
797 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
798 double sa
, double ea
)
800 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
802 if ( m_logicalFunction
!= wxCOPY
)
805 wxCoord xx
= LogicalToDeviceX(x
);
806 wxCoord yy
= LogicalToDeviceY(y
);
807 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
808 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
810 // handle -ve width and/or height
822 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
824 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
825 m_graphicContext
->PushState();
826 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
827 wxDouble factor
= ((wxDouble
) ww
) / hh
;
828 m_graphicContext
->Scale( factor
, 1.0) ;
829 if ( fill
&& (sa
!=ea
) )
830 path
->MoveToPoint(0,0);
831 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
832 // get clockwise angles
833 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
834 if ( fill
&& (sa
!=ea
) )
835 path
->AddLineToPoint(0,0);
836 m_graphicContext
->DrawPath( path
);
837 m_graphicContext
->PopState();
841 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
843 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
845 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
848 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
849 wxCoord xoffset
, wxCoord yoffset
)
851 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
853 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
855 if ( m_logicalFunction
!= wxCOPY
)
859 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
] ;
860 for( int i
= 0 ; i
< n
; ++i
)
862 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
863 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
866 m_graphicContext
->StrokeLines( n
, pointsD
) ;
871 void wxGCDC::DoDrawSpline(wxList
*points
)
873 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
875 if ( m_logicalFunction
!= wxCOPY
)
878 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
880 wxList::compatibility_iterator node
= points
->GetFirst();
881 if (node
== wxList::compatibility_iterator())
885 wxPoint
*p
= (wxPoint
*)node
->GetData();
890 node
= node
->GetNext();
891 p
= (wxPoint
*)node
->GetData();
895 wxCoord cx1
= ( x1
+ x2
) / 2;
896 wxCoord cy1
= ( y1
+ y2
) / 2;
898 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
899 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
902 while ((node
= node
->GetNext()) != NULL
)
905 while ((node
= node
->GetNext()))
909 p
= (wxPoint
*)node
->GetData();
914 wxCoord cx4
= (x1
+ x2
) / 2;
915 wxCoord cy4
= (y1
+ y2
) / 2;
917 path
->AddQuadCurveToPoint(
918 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
919 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
925 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
927 m_graphicContext
->StrokePath( path
);
932 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
933 wxCoord xoffset
, wxCoord yoffset
,
936 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
938 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
940 if ( m_logicalFunction
!= wxCOPY
)
943 bool closeIt
= false ;
944 if (points
[n
-1] != points
[0])
947 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)] ;
948 for( int i
= 0 ; i
< n
; ++i
)
950 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
951 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
954 pointsD
[n
] = pointsD
[0];
956 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
) ;
960 void wxGCDC::DoDrawPolyPolygon(int n
,
968 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
971 for ( int j
= 0 ; j
< n
; ++j
)
973 wxPoint start
= points
[i
];
974 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
)) ;
977 for ( int k
= 1 ; k
< l
; ++k
)
979 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
983 if ( start
!= points
[i
-1])
984 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
986 m_graphicContext
->DrawPath( path
, fillStyle
);
990 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
992 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
994 if ( m_logicalFunction
!= wxCOPY
)
997 wxCoord xx
= LogicalToDeviceX(x
);
998 wxCoord yy
= LogicalToDeviceY(y
);
999 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1000 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1002 // CMB: draw nothing if transformed w or h is 0
1003 if (ww
== 0 || hh
== 0)
1006 // CMB: handle -ve width and/or height
1017 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1020 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1021 wxCoord width
, wxCoord height
,
1024 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1026 if ( m_logicalFunction
!= wxCOPY
)
1030 radius
= - radius
* ((width
< height
) ? width
: height
);
1031 wxCoord xx
= LogicalToDeviceX(x
);
1032 wxCoord yy
= LogicalToDeviceY(y
);
1033 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1034 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1036 // CMB: draw nothing if transformed w or h is 0
1037 if (ww
== 0 || hh
== 0)
1040 // CMB: handle -ve width and/or height
1052 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1055 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1057 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1059 if ( m_logicalFunction
!= wxCOPY
)
1062 wxCoord xx
= LogicalToDeviceX(x
);
1063 wxCoord yy
= LogicalToDeviceY(y
);
1064 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1065 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1067 // CMB: draw nothing if transformed w or h is 0
1068 if (ww
== 0 || hh
== 0)
1071 // CMB: handle -ve width and/or height
1083 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1086 bool wxGCDC::CanDrawBitmap() const
1091 bool wxGCDC::DoBlit(
1092 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1093 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1094 wxCoord xsrcMask
, wxCoord ysrcMask
)
1096 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1097 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1099 if ( logical_func
== wxNO_OP
)
1102 if (xsrcMask
== -1 && ysrcMask
== -1)
1108 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1109 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1110 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1111 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1113 wxCoord yydest
= LogicalToDeviceY(ydest
);
1114 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1115 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1116 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1118 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1119 if ( memdc
&& logical_func
== wxCOPY
)
1122 wxBitmap blit
= memdc
->GetSelectedObject();
1124 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1126 wxCoord bmpwidth
= blit
.GetWidth();
1127 wxCoord bmpheight
= blit
.GetHeight();
1129 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1131 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1132 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1133 if ( wwsrc
> 0 && hhsrc
> 0 )
1135 if ( xxsrc
>= 0 && yysrc
>= 0 )
1137 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1138 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1139 blit
= blit
.GetSubBitmap( subrect
);
1143 // in this case we'd probably have to adjust the different coordinates, but
1144 // we have to find out proper contract first
1145 blit
= wxNullBitmap
;
1150 blit
= wxNullBitmap
;
1156 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1163 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
1170 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1173 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1175 if ( str
.length() == 0 )
1177 if ( m_logicalFunction
!= wxCOPY
)
1180 int drawX
= LogicalToDeviceX(x
);
1181 int drawY
= LogicalToDeviceY(y
);
1183 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1186 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1188 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1190 if ( str
.length() == 0 )
1192 if ( m_logicalFunction
!= wxCOPY
)
1195 int drawX
= LogicalToDeviceX(x
);
1196 int drawY
= LogicalToDeviceY(y
);
1198 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1201 bool wxGCDC::CanGetTextExtent() const
1203 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1208 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1209 wxCoord
*descent
, wxCoord
*externalLeading
,
1210 wxFont
*theFont
) const
1212 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1214 wxFont formerFont
= m_font
;
1217 m_graphicContext
->SetFont( *theFont
);
1220 wxDouble h
, d
, e
, w
;
1222 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1225 *height
= DeviceToLogicalYRel( h
);
1227 *descent
=DeviceToLogicalYRel( d
);
1228 if ( externalLeading
)
1229 *externalLeading
= DeviceToLogicalYRel( e
);
1231 *width
= DeviceToLogicalXRel( w
);
1235 m_graphicContext
->SetFont( m_font
);
1239 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1241 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1243 widths
.Add(0,text
.Length());
1244 if ( text
.IsEmpty() )
1247 wxArrayDouble widthsD
;
1249 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1250 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1251 widths
[i
] = DeviceToLogicalXRel( widthsD
[i
] + 0.5 ) ;
1256 wxCoord
wxGCDC::GetCharWidth(void) const
1259 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1264 wxCoord
wxGCDC::GetCharHeight(void) const
1267 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1272 void wxGCDC::Clear(void)
1274 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1278 void wxGCDC::DoGetSize(int *width
, int *height
) const
1284 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1285 const wxColour
& initialColour
,
1286 const wxColour
& destColour
,
1287 wxDirection nDirection
)
1294 start
= rect
.GetRightBottom();
1296 end
= rect
.GetLeftBottom();
1299 start
= rect
.GetLeftBottom();
1300 end
= rect
.GetRightBottom();
1304 start
= rect
.GetLeftBottom();
1306 end
= rect
.GetLeftTop();
1309 start
= rect
.GetLeftTop();
1310 end
= rect
.GetLeftBottom();
1315 m_graphicContext
->SetLinearGradientBrush(
1316 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1317 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1319 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1320 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1321 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1322 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1324 if (ww
== 0 || hh
== 0)
1338 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1339 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1340 m_graphicContext
->SetPen(m_pen
);
1343 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1344 const wxColour
& initialColour
,
1345 const wxColour
& destColour
,
1346 const wxPoint
& circleCenter
)
1349 wxInt32 cx
= rect
.GetWidth() / 2;
1350 wxInt32 cy
= rect
.GetHeight() / 2;
1357 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1358 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1359 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1360 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1362 if (ww
== 0 || hh
== 0)
1376 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1377 m_graphicContext
->SetBrush( wxBrush( destColour
) ) ;
1378 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1380 m_graphicContext
->SetRadialGradientBrush(
1381 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1382 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1383 LogicalToDeviceXRel(nRadius
),
1384 initialColour
,destColour
);
1386 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1387 m_graphicContext
->SetPen(m_pen
);
1390 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1391 wxCoord width
, wxCoord height
)
1393 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);