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());
309 if ( dc
.GetPen().Ok())
310 m_graphicContext
->SetPen(dc
.GetPen());
311 if ( dc
.GetBrush().Ok())
312 m_graphicContext
->SetBrush(dc
.GetBrush());
313 m_graphicContext
->SetTextColor(dc
.GetTextForeground());
320 m_mm_to_pix_x
= mm2pt
;
321 m_mm_to_pix_y
= mm2pt
;
323 m_pen
= *wxBLACK_PEN
;
324 m_font
= *wxNORMAL_FONT
;
325 m_brush
= *wxWHITE_BRUSH
;
327 m_graphicContext
= NULL
;
333 delete m_graphicContext
;
336 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
338 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
339 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
341 wxCoord xx
= LogicalToDeviceX(x
);
342 wxCoord yy
= LogicalToDeviceY(y
);
343 wxCoord w
= bmp
.GetWidth();
344 wxCoord h
= bmp
.GetHeight();
345 wxCoord ww
= LogicalToDeviceXRel(w
);
346 wxCoord hh
= LogicalToDeviceYRel(h
);
348 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
351 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
353 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
354 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
356 wxCoord xx
= LogicalToDeviceX(x
);
357 wxCoord yy
= LogicalToDeviceY(y
);
358 wxCoord w
= icon
.GetWidth();
359 wxCoord h
= icon
.GetHeight();
360 wxCoord ww
= LogicalToDeviceXRel(w
);
361 wxCoord hh
= LogicalToDeviceYRel(h
);
363 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
366 void wxGCDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
370 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
372 wxCoord xx
, yy
, ww
, hh
;
373 xx
= LogicalToDeviceX(x
);
374 yy
= LogicalToDeviceY(y
);
375 ww
= LogicalToDeviceXRel(width
);
376 hh
= LogicalToDeviceYRel(height
);
378 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
379 CGRect clipRect
= CGRectMake( xx
, yy
, ww
, hh
);
380 CGContextClipToRect( cgContext
, clipRect
);
382 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
383 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
387 m_clipX1
= wxMax( m_clipX1
, xx
);
388 m_clipY1
= wxMax( m_clipY1
, yy
);
389 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
390 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
402 // TODO: as soon as we don't reset the context for each operation anymore
403 // we have to update the context as well
408 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
410 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
414 DestroyClippingRegion();
419 region
.GetBox( x
, y
, w
, h
);
420 wxCoord xx
, yy
, ww
, hh
;
421 xx
= LogicalToDeviceX(x
);
422 yy
= LogicalToDeviceY(y
);
423 ww
= LogicalToDeviceXRel(w
);
424 hh
= LogicalToDeviceYRel(h
);
426 // if we have a scaling that we cannot map onto native regions
427 // we must use the box
428 if ( ww
!= w
|| hh
!= h
)
430 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
435 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
);
436 if ( xx
!= x
|| yy
!= y
)
437 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
);
438 SectRgn( (RgnHandle
)m_macCurrentClipRgn
, (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
443 m_clipX1
= wxMax( m_clipX1
, xx
);
444 m_clipY1
= wxMax( m_clipY1
, yy
);
445 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
446 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
460 void wxGCDC::DestroyClippingRegion()
464 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
466 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
467 CGContextRestoreGState( cgContext
);
468 CGContextSaveGState( cgContext
);
470 m_graphicContext
->SetPen( m_pen
);
471 m_graphicContext
->SetBrush( m_brush
);
478 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
484 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
486 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
489 void wxGCDC::SetTextForeground( const wxColour
&col
)
491 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
493 if ( col
!= m_textForegroundColour
)
495 m_textForegroundColour
= col
;
496 m_graphicContext
->SetTextColor( col
);
500 void wxGCDC::SetTextBackground( const wxColour
&col
)
502 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
504 m_textBackgroundColour
= col
;
507 void wxGCDC::SetMapMode( int mode
)
512 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
516 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
520 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
524 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
529 SetLogicalScale( 1.0, 1.0 );
533 ComputeScaleAndOrigin();
536 void wxGCDC::SetUserScale( double x
, double y
)
538 // allow negative ? -> no
541 ComputeScaleAndOrigin();
544 void wxGCDC::SetLogicalScale( double x
, double y
)
549 ComputeScaleAndOrigin();
552 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
554 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
555 m_logicalOriginY
= y
* m_signY
;
556 ComputeScaleAndOrigin();
559 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
563 ComputeScaleAndOrigin();
566 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
568 m_signX
= (xLeftRight
? 1 : -1);
569 m_signY
= (yBottomUp
? -1 : 1);
570 ComputeScaleAndOrigin();
573 wxSize
wxGCDC::GetPPI() const
575 return wxSize(72, 72);
578 int wxGCDC::GetDepth() const
583 void wxGCDC::ComputeScaleAndOrigin()
585 // this is a bit artificial, but we need to force wxGCDC to think
586 // the pen has changed
587 wxPen
pen( GetPen() );
593 void wxGCDC::SetPalette( const wxPalette
& palette
)
596 void wxGCDC::SetBackgroundMode( int mode
)
598 m_backgroundMode
= mode
;
601 void wxGCDC::SetFont( const wxFont
&font
)
604 if ( m_graphicContext
)
605 m_graphicContext
->SetFont( font
);
608 void wxGCDC::SetPen( const wxPen
&pen
)
614 if ( m_graphicContext
)
616 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
618 m_graphicContext
->SetPen( m_pen
);
622 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
623 // eg when using a wxScrollWindow and scrolling around
624 int origX
= LogicalToDeviceX( 0 );
625 int origY
= LogicalToDeviceY( 0 );
626 m_graphicContext
->Translate( origX
, origY
);
627 m_graphicContext
->SetPen( m_pen
);
628 m_graphicContext
->Translate( -origX
, -origY
);
633 void wxGCDC::SetBrush( const wxBrush
&brush
)
635 if (m_brush
== brush
)
639 if ( m_graphicContext
)
641 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
643 m_graphicContext
->SetBrush( m_brush
);
647 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
648 // eg when using a wxScrollWindow and scrolling around
649 // TODO on MSW / GDIPlus this still occurs with hatched brushes
650 int origX
= LogicalToDeviceX(0);
651 int origY
= LogicalToDeviceY(0);
652 m_graphicContext
->Translate( origX
, origY
);
653 m_graphicContext
->SetBrush( m_brush
);
654 m_graphicContext
->Translate( -origX
, -origY
);
659 void wxGCDC::SetBackground( const wxBrush
&brush
)
661 if (m_backgroundBrush
== brush
)
664 m_backgroundBrush
= brush
;
665 if (!m_backgroundBrush
.Ok())
669 void wxGCDC::SetLogicalFunction( int function
)
671 if (m_logicalFunction
== function
)
674 m_logicalFunction
= function
;
675 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
677 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
678 if ( m_logicalFunction
== wxCOPY
)
679 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
680 else if ( m_logicalFunction
== wxINVERT
)
681 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
683 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
688 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
689 const wxColour
& col
, int style
);
691 bool wxGCDC::DoFloodFill(wxCoord x
, wxCoord y
,
692 const wxColour
& col
, int style
)
694 // return wxDoFloodFill(this, x, y, col, style);
698 bool wxGCDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
700 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
704 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
706 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
708 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
710 if ( m_logicalFunction
!= wxCOPY
)
714 wxCoord xx1
= LogicalToDeviceX(x1
);
715 wxCoord yy1
= LogicalToDeviceY(y1
);
716 wxCoord xx2
= LogicalToDeviceX(x2
);
717 wxCoord yy2
= LogicalToDeviceY(y2
);
719 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
721 CalcBoundingBox(x1
, y1
);
722 CalcBoundingBox(x2
, y2
);
725 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
727 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
729 if ( m_logicalFunction
!= wxCOPY
)
736 wxCoord xx
= LogicalToDeviceX(x
);
737 wxCoord yy
= LogicalToDeviceY(y
);
738 wxCoord xw
= LogicalToDeviceX(w
);
739 wxCoord x0
= LogicalToDeviceX(0);
740 wxCoord y0
= LogicalToDeviceY(0);
741 wxCoord yh
= LogicalToDeviceY(h
);
743 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
744 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
746 CalcBoundingBox(x0
, y0
);
747 CalcBoundingBox(x0
+xw
, y0
+yh
);
750 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
751 wxCoord x2
, wxCoord y2
,
752 wxCoord xc
, wxCoord yc
)
754 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
756 if ( m_logicalFunction
!= wxCOPY
)
759 wxCoord xx1
= LogicalToDeviceX(x1
);
760 wxCoord yy1
= LogicalToDeviceY(y1
);
761 wxCoord xx2
= LogicalToDeviceX(x2
);
762 wxCoord yy2
= LogicalToDeviceY(y2
);
763 wxCoord xxc
= LogicalToDeviceX(xc
);
764 wxCoord yyc
= LogicalToDeviceY(yc
);
766 double dx
= xx1
- xxc
;
767 double dy
= yy1
- yyc
;
768 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
769 wxCoord rad
= (wxCoord
)radius
;
771 if (xx1
== xx2
&& yy1
== yy2
)
776 else if (radius
== 0.0)
782 sa
= (xx1
- xxc
== 0) ?
783 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
784 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
785 ea
= (xx2
- xxc
== 0) ?
786 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
787 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
790 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
792 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
793 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
794 path
->MoveToPoint( xxc
, yyc
);
795 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
796 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
797 path
->AddLineToPoint( xxc
, yyc
);
798 m_graphicContext
->DrawPath(path
);
802 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
803 double sa
, double ea
)
805 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
807 if ( m_logicalFunction
!= wxCOPY
)
810 wxCoord xx
= LogicalToDeviceX(x
);
811 wxCoord yy
= LogicalToDeviceY(y
);
812 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
813 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
815 // handle -ve width and/or height
827 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
829 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
830 m_graphicContext
->PushState();
831 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
832 wxDouble factor
= ((wxDouble
) ww
) / hh
;
833 m_graphicContext
->Scale( factor
, 1.0) ;
834 if ( fill
&& (sa
!=ea
) )
835 path
->MoveToPoint(0,0);
836 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
837 // get clockwise angles
838 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
839 if ( fill
&& (sa
!=ea
) )
840 path
->AddLineToPoint(0,0);
841 m_graphicContext
->DrawPath( path
);
842 m_graphicContext
->PopState();
846 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
848 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
850 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
853 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
854 wxCoord xoffset
, wxCoord yoffset
)
856 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
858 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
860 if ( m_logicalFunction
!= wxCOPY
)
864 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
] ;
865 for( int i
= 0 ; i
< n
; ++i
)
867 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
868 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
871 m_graphicContext
->StrokeLines( n
, pointsD
) ;
876 void wxGCDC::DoDrawSpline(wxList
*points
)
878 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
880 if ( m_logicalFunction
!= wxCOPY
)
883 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
885 wxList::compatibility_iterator node
= points
->GetFirst();
886 if (node
== wxList::compatibility_iterator())
890 wxPoint
*p
= (wxPoint
*)node
->GetData();
895 node
= node
->GetNext();
896 p
= (wxPoint
*)node
->GetData();
900 wxCoord cx1
= ( x1
+ x2
) / 2;
901 wxCoord cy1
= ( y1
+ y2
) / 2;
903 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
904 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
907 while ((node
= node
->GetNext()) != NULL
)
910 while ((node
= node
->GetNext()))
914 p
= (wxPoint
*)node
->GetData();
919 wxCoord cx4
= (x1
+ x2
) / 2;
920 wxCoord cy4
= (y1
+ y2
) / 2;
922 path
->AddQuadCurveToPoint(
923 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
924 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
930 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
932 m_graphicContext
->StrokePath( path
);
937 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
938 wxCoord xoffset
, wxCoord yoffset
,
941 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
943 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
945 if ( m_logicalFunction
!= wxCOPY
)
948 bool closeIt
= false ;
949 if (points
[n
-1] != points
[0])
952 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)] ;
953 for( int i
= 0 ; i
< n
; ++i
)
955 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
956 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
959 pointsD
[n
] = pointsD
[0];
961 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
) ;
965 void wxGCDC::DoDrawPolyPolygon(int n
,
973 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
976 for ( int j
= 0 ; j
< n
; ++j
)
978 wxPoint start
= points
[i
];
979 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
)) ;
982 for ( int k
= 1 ; k
< l
; ++k
)
984 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
988 if ( start
!= points
[i
-1])
989 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
991 m_graphicContext
->DrawPath( path
, fillStyle
);
995 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
997 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
999 if ( m_logicalFunction
!= wxCOPY
)
1002 wxCoord xx
= LogicalToDeviceX(x
);
1003 wxCoord yy
= LogicalToDeviceY(y
);
1004 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1005 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1007 // CMB: draw nothing if transformed w or h is 0
1008 if (ww
== 0 || hh
== 0)
1011 // CMB: handle -ve width and/or height
1022 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1025 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1026 wxCoord width
, wxCoord height
,
1029 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1031 if ( m_logicalFunction
!= wxCOPY
)
1035 radius
= - radius
* ((width
< height
) ? width
: height
);
1036 wxCoord xx
= LogicalToDeviceX(x
);
1037 wxCoord yy
= LogicalToDeviceY(y
);
1038 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1039 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1041 // CMB: draw nothing if transformed w or h is 0
1042 if (ww
== 0 || hh
== 0)
1045 // CMB: handle -ve width and/or height
1057 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1060 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1062 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1064 if ( m_logicalFunction
!= wxCOPY
)
1067 wxCoord xx
= LogicalToDeviceX(x
);
1068 wxCoord yy
= LogicalToDeviceY(y
);
1069 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1070 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1072 // CMB: draw nothing if transformed w or h is 0
1073 if (ww
== 0 || hh
== 0)
1076 // CMB: handle -ve width and/or height
1088 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1091 bool wxGCDC::CanDrawBitmap() const
1096 bool wxGCDC::DoBlit(
1097 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1098 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1099 wxCoord xsrcMask
, wxCoord ysrcMask
)
1101 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1102 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1104 if ( logical_func
== wxNO_OP
)
1107 if (xsrcMask
== -1 && ysrcMask
== -1)
1113 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1114 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1115 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1116 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1118 wxCoord yydest
= LogicalToDeviceY(ydest
);
1119 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1120 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1121 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1123 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1124 if ( memdc
&& logical_func
== wxCOPY
)
1127 wxBitmap blit
= memdc
->GetSelectedObject();
1129 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1131 wxCoord bmpwidth
= blit
.GetWidth();
1132 wxCoord bmpheight
= blit
.GetHeight();
1134 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1136 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1137 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1138 if ( wwsrc
> 0 && hhsrc
> 0 )
1140 if ( xxsrc
>= 0 && yysrc
>= 0 )
1142 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1143 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1144 blit
= blit
.GetSubBitmap( subrect
);
1148 // in this case we'd probably have to adjust the different coordinates, but
1149 // we have to find out proper contract first
1150 blit
= wxNullBitmap
;
1155 blit
= wxNullBitmap
;
1161 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1168 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
1175 void wxGCDC::DoDrawRotatedText(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
, DegToRad(angle
));
1191 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1193 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1195 if ( str
.length() == 0 )
1197 if ( m_logicalFunction
!= wxCOPY
)
1200 int drawX
= LogicalToDeviceX(x
);
1201 int drawY
= LogicalToDeviceY(y
);
1203 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1206 bool wxGCDC::CanGetTextExtent() const
1208 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1213 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1214 wxCoord
*descent
, wxCoord
*externalLeading
,
1215 wxFont
*theFont
) const
1217 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1219 wxFont formerFont
= m_font
;
1222 m_graphicContext
->SetFont( *theFont
);
1225 wxDouble h
, d
, e
, w
;
1227 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1230 *height
= DeviceToLogicalYRel( h
);
1232 *descent
=DeviceToLogicalYRel( d
);
1233 if ( externalLeading
)
1234 *externalLeading
= DeviceToLogicalYRel( e
);
1236 *width
= DeviceToLogicalXRel( w
);
1240 m_graphicContext
->SetFont( m_font
);
1244 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1246 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1248 widths
.Add(0,text
.Length());
1249 if ( text
.IsEmpty() )
1252 wxArrayDouble widthsD
;
1254 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1255 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1256 widths
[i
] = DeviceToLogicalXRel( widthsD
[i
] + 0.5 ) ;
1261 wxCoord
wxGCDC::GetCharWidth(void) const
1264 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1269 wxCoord
wxGCDC::GetCharHeight(void) const
1272 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1277 void wxGCDC::Clear(void)
1279 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1283 void wxGCDC::DoGetSize(int *width
, int *height
) const
1289 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1290 const wxColour
& initialColour
,
1291 const wxColour
& destColour
,
1292 wxDirection nDirection
)
1299 start
= rect
.GetRightBottom();
1301 end
= rect
.GetLeftBottom();
1304 start
= rect
.GetLeftBottom();
1305 end
= rect
.GetRightBottom();
1309 start
= rect
.GetLeftBottom();
1311 end
= rect
.GetLeftTop();
1314 start
= rect
.GetLeftTop();
1315 end
= rect
.GetLeftBottom();
1320 m_graphicContext
->SetLinearGradientBrush(
1321 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1322 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1324 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1325 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1326 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1327 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1329 if (ww
== 0 || hh
== 0)
1343 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1344 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1345 m_graphicContext
->SetPen(m_pen
);
1348 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1349 const wxColour
& initialColour
,
1350 const wxColour
& destColour
,
1351 const wxPoint
& circleCenter
)
1354 wxInt32 cx
= rect
.GetWidth() / 2;
1355 wxInt32 cy
= rect
.GetHeight() / 2;
1362 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1363 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1364 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1365 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1367 if (ww
== 0 || hh
== 0)
1381 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1382 m_graphicContext
->SetBrush( wxBrush( destColour
) ) ;
1383 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1385 m_graphicContext
->SetRadialGradientBrush(
1386 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1387 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1388 LogicalToDeviceXRel(nRadius
),
1389 initialColour
,destColour
);
1391 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1392 m_graphicContext
->SetPen(m_pen
);
1395 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1396 wxCoord width
, wxCoord height
)
1398 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);