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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if defined(__BORLANDC__)
19 #if wxUSE_GRAPHICS_CONTEXT
21 #include "wx/graphics.h"
25 #include "wx/bitmap.h"
26 #include "wx/dcmemory.h"
27 #include "wx/region.h"
30 #if !defined(wxMAC_USE_CORE_GRAPHICS_BLEND_MODES)
31 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 static const double RAD2DEG
= 180.0 / M_PI
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static inline double DegToRad(double deg
)
46 return (deg
* M_PI
) / 180.0;
49 //-----------------------------------------------------------------------------
51 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
55 return wxPoint2DDouble(x
,y
);
58 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
60 MoveToPoint( p
.m_x
, p
.m_y
);
63 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
65 AddLineToPoint( p
.m_x
, p
.m_y
);
68 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
70 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
73 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
75 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
82 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
84 // calculate using degree elevation to a cubic bezier
88 wxPoint2DDouble start
= GetCurrentPoint() ;
89 wxPoint2DDouble
end(x
,y
);
90 wxPoint2DDouble
c(cx
,cy
);
91 c1
= wxDouble(1/3.0) * start
+ wxDouble(2/3.0) * c
;
92 c2
= wxDouble(2/3.0) * c
+ wxDouble(1/3.0) * end
;
93 AddCurveToPoint(c1
.m_x
,c1
.m_y
,c2
.m_x
,c2
.m_y
,x
,y
);
96 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
99 AddLineToPoint(x
,y
+h
);
100 AddLineToPoint(x
+w
,y
+h
);
101 AddLineToPoint(x
+w
,y
);
105 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
108 AddArc( x
,y
,r
,0,2*M_PI
,false);
112 // 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)
113 void wxGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
115 wxPoint2DDouble current
= GetCurrentPoint();
116 wxPoint2DDouble
p1(x1
,y1
);
117 wxPoint2DDouble
p2(x2
,y2
);
119 wxPoint2DDouble v1
= current
- p1
;
121 wxPoint2DDouble v2
= p2
- p1
;
124 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
127 alpha
= 360 + alpha
;
128 // TODO obtuse angles
130 alpha
= DegToRad(alpha
);
132 wxDouble dist
= r
/ sin(alpha
/2) * cos(alpha
/2) ;
133 // calculate tangential points
134 wxPoint2DDouble t1
= dist
*v1
+ p1
;
135 wxPoint2DDouble t2
= dist
*v2
+ p1
;
137 wxPoint2DDouble nv1
= v1
;
138 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
139 wxPoint2DDouble c
= t1
+ r
*nv1
;
141 wxDouble a1
= v1
.GetVectorAngle()+90 ;
142 wxDouble a2
= v2
.GetVectorAngle()-90 ;
145 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
149 //-----------------------------------------------------------------------------
150 // wxGraphicsContext Convenience Methods
151 //-----------------------------------------------------------------------------
153 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
155 FillPath( path
, fillStyle
);
159 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
163 DrawText( str
, 0, 0 );
168 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
170 wxGraphicsPath
* path
= CreatePath();
171 path
->MoveToPoint(x1
, y1
) ;
172 path
->AddLineToPoint( x2
, y2
);
177 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
179 wxGraphicsPath
* path
= CreatePath();
180 path
->AddRectangle( x
, y
, w
, h
);
185 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
187 wxGraphicsPath
* path
= CreatePath();
190 path
->AddCircle( x
+w
/2,y
+w
/2,w
/2);
196 Translate(x
+w
/2,y
+h
/2);
197 wxDouble factor
= ((wxDouble
) w
) / h
;
198 Scale( factor
, 1.0) ;
199 path
->AddCircle(0,0,h
/2);
206 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
208 wxGraphicsPath
* path
= CreatePath();
211 path
->AddRectangle( x
, y
, w
, h
);
219 path
->MoveToPoint(w
, h
/ 2);
220 path
->AddArcToPoint(w
, h
, w
/ 2, h
, radius
);
221 path
->AddArcToPoint(0, h
, 0, h
/ 2, radius
);
222 path
->AddArcToPoint(0, 0, w
/ 2, 0, radius
);
223 path
->AddArcToPoint(w
, 0, w
, h
/ 2, radius
);
224 path
->CloseSubpath();
231 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
234 wxGraphicsPath
* path
= CreatePath();
235 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
236 for ( size_t i
= 1 ; i
< n
; ++i
)
237 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
242 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
245 wxGraphicsPath
* path
= CreatePath();
246 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
247 for ( size_t i
= 1 ; i
< n
; ++i
)
248 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
249 DrawPath( path
, fillStyle
);
253 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
256 wxGraphicsPath
* path
= CreatePath();
257 for ( size_t i
= 0 ; i
< n
; ++i
)
259 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
) ;
260 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
266 IMPLEMENT_ABSTRACT_CLASS(wxGCDC
, wxObject
)
268 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
278 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
281 m_graphicContext
= wxGraphicsContext::Create(dc
);
283 if ( dc
.GetFont().Ok())
284 m_graphicContext
->SetFont(dc
.GetFont());
285 if ( dc
.GetPen().Ok())
286 m_graphicContext
->SetPen(dc
.GetPen());
287 if ( dc
.GetBrush().Ok())
288 m_graphicContext
->SetBrush(dc
.GetBrush());
289 m_graphicContext
->SetTextColor(dc
.GetTextForeground());
296 m_mm_to_pix_x
= mm2pt
;
297 m_mm_to_pix_y
= mm2pt
;
299 m_pen
= *wxBLACK_PEN
;
300 m_font
= *wxNORMAL_FONT
;
301 m_brush
= *wxWHITE_BRUSH
;
303 m_graphicContext
= NULL
;
309 delete m_graphicContext
;
312 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
314 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
315 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
317 wxCoord xx
= LogicalToDeviceX(x
);
318 wxCoord yy
= LogicalToDeviceY(y
);
319 wxCoord w
= bmp
.GetWidth();
320 wxCoord h
= bmp
.GetHeight();
321 wxCoord ww
= LogicalToDeviceXRel(w
);
322 wxCoord hh
= LogicalToDeviceYRel(h
);
324 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
327 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
329 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
330 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
332 wxCoord xx
= LogicalToDeviceX(x
);
333 wxCoord yy
= LogicalToDeviceY(y
);
334 wxCoord w
= icon
.GetWidth();
335 wxCoord h
= icon
.GetHeight();
336 wxCoord ww
= LogicalToDeviceXRel(w
);
337 wxCoord hh
= LogicalToDeviceYRel(h
);
339 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
342 void wxGCDC::DoSetClippingRegion( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxCoord
WXUNUSED(width
), wxCoord
WXUNUSED(height
) )
346 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
348 wxCoord xx
, yy
, ww
, hh
;
349 xx
= LogicalToDeviceX(x
);
350 yy
= LogicalToDeviceY(y
);
351 ww
= LogicalToDeviceXRel(width
);
352 hh
= LogicalToDeviceYRel(height
);
354 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
355 CGRect clipRect
= CGRectMake( xx
, yy
, ww
, hh
);
356 CGContextClipToRect( cgContext
, clipRect
);
358 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
359 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
363 m_clipX1
= wxMax( m_clipX1
, xx
);
364 m_clipY1
= wxMax( m_clipY1
, yy
);
365 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
366 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
378 // TODO: as soon as we don't reset the context for each operation anymore
379 // we have to update the context as well
384 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
386 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
390 DestroyClippingRegion();
395 region
.GetBox( x
, y
, w
, h
);
396 wxCoord xx
, yy
, ww
, hh
;
397 xx
= LogicalToDeviceX(x
);
398 yy
= LogicalToDeviceY(y
);
399 ww
= LogicalToDeviceXRel(w
);
400 hh
= LogicalToDeviceYRel(h
);
402 // if we have a scaling that we cannot map onto native regions
403 // we must use the box
404 if ( ww
!= w
|| hh
!= h
)
406 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
411 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
);
412 if ( xx
!= x
|| yy
!= y
)
413 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
);
414 SectRgn( (RgnHandle
)m_macCurrentClipRgn
, (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
419 m_clipX1
= wxMax( m_clipX1
, xx
);
420 m_clipY1
= wxMax( m_clipY1
, yy
);
421 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
422 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
436 void wxGCDC::DestroyClippingRegion()
440 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
442 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
443 CGContextRestoreGState( cgContext
);
444 CGContextSaveGState( cgContext
);
446 m_graphicContext
->SetPen( m_pen
);
447 m_graphicContext
->SetBrush( m_brush
);
454 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
460 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
462 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
465 void wxGCDC::SetTextForeground( const wxColour
&col
)
467 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
469 if ( col
!= m_textForegroundColour
)
471 m_textForegroundColour
= col
;
472 m_graphicContext
->SetTextColor( col
);
476 void wxGCDC::SetTextBackground( const wxColour
&col
)
478 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
480 m_textBackgroundColour
= col
;
483 void wxGCDC::SetMapMode( int mode
)
488 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
492 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
496 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
500 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
505 SetLogicalScale( 1.0, 1.0 );
509 ComputeScaleAndOrigin();
512 void wxGCDC::SetUserScale( double x
, double y
)
514 // allow negative ? -> no
517 ComputeScaleAndOrigin();
520 void wxGCDC::SetLogicalScale( double x
, double y
)
525 ComputeScaleAndOrigin();
528 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
530 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
531 m_logicalOriginY
= y
* m_signY
;
532 ComputeScaleAndOrigin();
535 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
539 ComputeScaleAndOrigin();
542 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
544 m_signX
= (xLeftRight
? 1 : -1);
545 m_signY
= (yBottomUp
? -1 : 1);
546 ComputeScaleAndOrigin();
549 wxSize
wxGCDC::GetPPI() const
551 return wxSize(72, 72);
554 int wxGCDC::GetDepth() const
559 void wxGCDC::ComputeScaleAndOrigin()
561 // this is a bit artificial, but we need to force wxGCDC to think
562 // the pen has changed
563 wxPen
pen( GetPen() );
569 void wxGCDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
574 void wxGCDC::SetBackgroundMode( int mode
)
576 m_backgroundMode
= mode
;
579 void wxGCDC::SetFont( const wxFont
&font
)
582 if ( m_graphicContext
)
583 m_graphicContext
->SetFont( font
);
586 void wxGCDC::SetPen( const wxPen
&pen
)
592 if ( m_graphicContext
)
594 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
596 m_graphicContext
->SetPen( m_pen
);
600 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
601 // eg when using a wxScrollWindow and scrolling around
602 int origX
= LogicalToDeviceX( 0 );
603 int origY
= LogicalToDeviceY( 0 );
604 m_graphicContext
->Translate( origX
, origY
);
605 m_graphicContext
->SetPen( m_pen
);
606 m_graphicContext
->Translate( -origX
, -origY
);
611 void wxGCDC::SetBrush( const wxBrush
&brush
)
613 if (m_brush
== brush
)
617 if ( m_graphicContext
)
619 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
621 m_graphicContext
->SetBrush( m_brush
);
625 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
626 // eg when using a wxScrollWindow and scrolling around
627 // TODO on MSW / GDIPlus this still occurs with hatched brushes
628 int origX
= LogicalToDeviceX(0);
629 int origY
= LogicalToDeviceY(0);
630 m_graphicContext
->Translate( origX
, origY
);
631 m_graphicContext
->SetBrush( m_brush
);
632 m_graphicContext
->Translate( -origX
, -origY
);
637 void wxGCDC::SetBackground( const wxBrush
&brush
)
639 if (m_backgroundBrush
== brush
)
642 m_backgroundBrush
= brush
;
643 if (!m_backgroundBrush
.Ok())
647 void wxGCDC::SetLogicalFunction( int function
)
649 if (m_logicalFunction
== function
)
652 m_logicalFunction
= function
;
653 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
655 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
656 if ( m_logicalFunction
== wxCOPY
)
657 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
658 else if ( m_logicalFunction
== wxINVERT
)
659 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
661 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
666 bool wxGCDC::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
667 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
672 bool wxGCDC::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
674 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
678 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
680 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
682 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
684 if ( m_logicalFunction
!= wxCOPY
)
688 wxCoord xx1
= LogicalToDeviceX(x1
);
689 wxCoord yy1
= LogicalToDeviceY(y1
);
690 wxCoord xx2
= LogicalToDeviceX(x2
);
691 wxCoord yy2
= LogicalToDeviceY(y2
);
693 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
695 CalcBoundingBox(x1
, y1
);
696 CalcBoundingBox(x2
, y2
);
699 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
701 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
703 if ( m_logicalFunction
!= wxCOPY
)
710 wxCoord xx
= LogicalToDeviceX(x
);
711 wxCoord yy
= LogicalToDeviceY(y
);
712 wxCoord xw
= LogicalToDeviceX(w
);
713 wxCoord x0
= LogicalToDeviceX(0);
714 wxCoord y0
= LogicalToDeviceY(0);
715 wxCoord yh
= LogicalToDeviceY(h
);
717 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
718 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
720 CalcBoundingBox(x0
, y0
);
721 CalcBoundingBox(x0
+xw
, y0
+yh
);
724 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
725 wxCoord x2
, wxCoord y2
,
726 wxCoord xc
, wxCoord yc
)
728 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
730 if ( m_logicalFunction
!= wxCOPY
)
733 wxCoord xx1
= LogicalToDeviceX(x1
);
734 wxCoord yy1
= LogicalToDeviceY(y1
);
735 wxCoord xx2
= LogicalToDeviceX(x2
);
736 wxCoord yy2
= LogicalToDeviceY(y2
);
737 wxCoord xxc
= LogicalToDeviceX(xc
);
738 wxCoord yyc
= LogicalToDeviceY(yc
);
740 double dx
= xx1
- xxc
;
741 double dy
= yy1
- yyc
;
742 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
743 wxCoord rad
= (wxCoord
)radius
;
745 if (xx1
== xx2
&& yy1
== yy2
)
750 else if (radius
== 0.0)
756 sa
= (xx1
- xxc
== 0) ?
757 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
758 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
759 ea
= (xx2
- xxc
== 0) ?
760 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
761 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
764 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
766 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
767 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
768 path
->MoveToPoint( xxc
, yyc
);
769 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
770 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
771 path
->AddLineToPoint( xxc
, yyc
);
772 m_graphicContext
->DrawPath(path
);
776 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
777 double sa
, double ea
)
779 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
781 if ( m_logicalFunction
!= wxCOPY
)
784 wxCoord xx
= LogicalToDeviceX(x
);
785 wxCoord yy
= LogicalToDeviceY(y
);
786 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
787 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
789 // handle -ve width and/or height
801 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
803 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
804 m_graphicContext
->PushState();
805 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
806 wxDouble factor
= ((wxDouble
) ww
) / hh
;
807 m_graphicContext
->Scale( factor
, 1.0) ;
808 if ( fill
&& (sa
!=ea
) )
809 path
->MoveToPoint(0,0);
810 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
811 // get clockwise angles
812 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
813 if ( fill
&& (sa
!=ea
) )
814 path
->AddLineToPoint(0,0);
815 m_graphicContext
->DrawPath( path
);
816 m_graphicContext
->PopState();
820 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
822 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
824 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
827 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
828 wxCoord xoffset
, wxCoord yoffset
)
830 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
832 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
834 if ( m_logicalFunction
!= wxCOPY
)
838 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
] ;
839 for( int i
= 0 ; i
< n
; ++i
)
841 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
842 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
845 m_graphicContext
->StrokeLines( n
, pointsD
) ;
850 void wxGCDC::DoDrawSpline(wxList
*points
)
852 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
854 if ( m_logicalFunction
!= wxCOPY
)
857 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
859 wxList::compatibility_iterator node
= points
->GetFirst();
860 if (node
== wxList::compatibility_iterator())
864 wxPoint
*p
= (wxPoint
*)node
->GetData();
869 node
= node
->GetNext();
870 p
= (wxPoint
*)node
->GetData();
874 wxCoord cx1
= ( x1
+ x2
) / 2;
875 wxCoord cy1
= ( y1
+ y2
) / 2;
877 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
878 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
881 while ((node
= node
->GetNext()) != NULL
)
884 while ((node
= node
->GetNext()))
888 p
= (wxPoint
*)node
->GetData();
893 wxCoord cx4
= (x1
+ x2
) / 2;
894 wxCoord cy4
= (y1
+ y2
) / 2;
896 path
->AddQuadCurveToPoint(
897 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
898 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
904 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
906 m_graphicContext
->StrokePath( path
);
909 #endif // wxUSE_SPLINES
911 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
912 wxCoord xoffset
, wxCoord yoffset
,
915 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
917 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
919 if ( m_logicalFunction
!= wxCOPY
)
922 bool closeIt
= false ;
923 if (points
[n
-1] != points
[0])
926 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)] ;
927 for( int i
= 0 ; i
< n
; ++i
)
929 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
930 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
933 pointsD
[n
] = pointsD
[0];
935 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
) ;
939 void wxGCDC::DoDrawPolyPolygon(int n
,
947 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
950 for ( int j
= 0 ; j
< n
; ++j
)
952 wxPoint start
= points
[i
];
953 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
)) ;
956 for ( int k
= 1 ; k
< l
; ++k
)
958 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
962 if ( start
!= points
[i
-1])
963 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
965 m_graphicContext
->DrawPath( path
, fillStyle
);
969 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
971 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
973 if ( m_logicalFunction
!= wxCOPY
)
976 wxCoord xx
= LogicalToDeviceX(x
);
977 wxCoord yy
= LogicalToDeviceY(y
);
978 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
979 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
981 // CMB: draw nothing if transformed w or h is 0
982 if (ww
== 0 || hh
== 0)
985 // CMB: handle -ve width and/or height
996 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
999 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1000 wxCoord width
, wxCoord height
,
1003 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1005 if ( m_logicalFunction
!= wxCOPY
)
1009 radius
= - radius
* ((width
< height
) ? width
: height
);
1010 wxCoord xx
= LogicalToDeviceX(x
);
1011 wxCoord yy
= LogicalToDeviceY(y
);
1012 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1013 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1015 // CMB: draw nothing if transformed w or h is 0
1016 if (ww
== 0 || hh
== 0)
1019 // CMB: handle -ve width and/or height
1031 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1034 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1036 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1038 if ( m_logicalFunction
!= wxCOPY
)
1041 wxCoord xx
= LogicalToDeviceX(x
);
1042 wxCoord yy
= LogicalToDeviceY(y
);
1043 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1044 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1046 // CMB: draw nothing if transformed w or h is 0
1047 if (ww
== 0 || hh
== 0)
1050 // CMB: handle -ve width and/or height
1062 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1065 bool wxGCDC::CanDrawBitmap() const
1070 bool wxGCDC::DoBlit(
1071 wxCoord
WXUNUSED(xdest
), wxCoord
WXUNUSED(ydest
), wxCoord
WXUNUSED(width
), wxCoord
WXUNUSED(height
),
1072 wxDC
*source
, wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
), int logical_func
, bool WXUNUSED(useMask
),
1073 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
) )
1075 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1076 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1078 if ( logical_func
== wxNO_OP
)
1082 if (xsrcMask
== -1 && ysrcMask
== -1)
1088 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1089 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1090 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1091 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1093 wxCoord yydest
= LogicalToDeviceY(ydest
);
1094 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1095 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1096 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1098 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1099 if ( memdc
&& logical_func
== wxCOPY
)
1101 wxBitmap blit
= memdc
->GetSelectedObject();
1103 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1105 wxCoord bmpwidth
= blit
.GetWidth();
1106 wxCoord bmpheight
= blit
.GetHeight();
1108 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1110 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1111 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1112 if ( wwsrc
> 0 && hhsrc
> 0 )
1114 if ( xxsrc
>= 0 && yysrc
>= 0 )
1116 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1117 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1118 blit
= blit
.GetSubBitmap( subrect
);
1122 // in this case we'd probably have to adjust the different coordinates, but
1123 // we have to find out proper contract first
1124 blit
= wxNullBitmap
;
1129 blit
= wxNullBitmap
;
1135 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1141 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
1149 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1152 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1154 if ( str
.length() == 0 )
1156 if ( m_logicalFunction
!= wxCOPY
)
1159 int drawX
= LogicalToDeviceX(x
);
1160 int drawY
= LogicalToDeviceY(y
);
1162 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1165 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1167 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1169 if ( str
.length() == 0 )
1171 if ( m_logicalFunction
!= wxCOPY
)
1174 int drawX
= LogicalToDeviceX(x
);
1175 int drawY
= LogicalToDeviceY(y
);
1177 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1180 bool wxGCDC::CanGetTextExtent() const
1182 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1187 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1188 wxCoord
*descent
, wxCoord
*externalLeading
,
1189 wxFont
*theFont
) const
1191 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1195 m_graphicContext
->SetFont( *theFont
);
1198 wxDouble h
, d
, e
, w
;
1200 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1203 *height
= DeviceToLogicalYRel( h
);
1205 *descent
=DeviceToLogicalYRel( d
);
1206 if ( externalLeading
)
1207 *externalLeading
= DeviceToLogicalYRel( e
);
1209 *width
= DeviceToLogicalXRel( w
);
1213 m_graphicContext
->SetFont( m_font
);
1217 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1219 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1221 widths
.Add(0,text
.Length());
1222 if ( text
.IsEmpty() )
1225 wxArrayDouble widthsD
;
1227 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1228 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1229 widths
[i
] = DeviceToLogicalXRel( widthsD
[i
] + 0.5 ) ;
1234 wxCoord
wxGCDC::GetCharWidth(void) const
1237 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1242 wxCoord
wxGCDC::GetCharHeight(void) const
1245 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1250 void wxGCDC::Clear(void)
1252 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1256 void wxGCDC::DoGetSize(int *width
, int *height
) const
1262 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1263 const wxColour
& initialColour
,
1264 const wxColour
& destColour
,
1265 wxDirection nDirection
)
1272 start
= rect
.GetRightBottom();
1274 end
= rect
.GetLeftBottom();
1277 start
= rect
.GetLeftBottom();
1278 end
= rect
.GetRightBottom();
1282 start
= rect
.GetLeftBottom();
1284 end
= rect
.GetLeftTop();
1287 start
= rect
.GetLeftTop();
1288 end
= rect
.GetLeftBottom();
1293 m_graphicContext
->SetLinearGradientBrush(
1294 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1295 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1297 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1298 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1299 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1300 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1302 if (ww
== 0 || hh
== 0)
1316 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1317 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1318 m_graphicContext
->SetPen(m_pen
);
1321 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1322 const wxColour
& initialColour
,
1323 const wxColour
& destColour
,
1324 const wxPoint
& circleCenter
)
1327 wxInt32 cx
= rect
.GetWidth() / 2;
1328 wxInt32 cy
= rect
.GetHeight() / 2;
1335 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1336 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1337 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1338 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1340 if (ww
== 0 || hh
== 0)
1354 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1355 m_graphicContext
->SetBrush( wxBrush( destColour
) ) ;
1356 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1358 m_graphicContext
->SetRadialGradientBrush(
1359 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1360 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1361 LogicalToDeviceXRel(nRadius
),
1362 initialColour
,destColour
);
1364 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1365 m_graphicContext
->SetPen(m_pen
);
1368 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1369 wxCoord width
, wxCoord height
)
1371 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);
1374 #endif // wxUSE_GRAPHICS_CONTEXT