1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/graphcmn.cpp
3 // Purpose: graphics context methods common to all platforms
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
27 #include "wx/graphics.h"
29 #if wxUSE_GRAPHICS_CONTEXT
31 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
35 return wxPoint2DDouble(x
,y
);
38 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
40 MoveToPoint( p
.m_x
, p
.m_y
);
43 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
45 AddLineToPoint( p
.m_x
, p
.m_y
);
48 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
50 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
53 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
55 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
62 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
64 // calculate using degree elevation to a cubic bezier
68 wxPoint2DDouble start
= GetCurrentPoint() ;
69 wxPoint2DDouble
end(x
,y
);
70 wxPoint2DDouble
c(cx
,cy
);
71 c1
= (1/3.0) * start
+ (2/3.0) * c
;
72 c2
= (2/3.0) * c
+ (1/3.0) * end
;
73 AddCurveToPoint(c1
.m_x
,c1
.m_y
,c2
.m_x
,c2
.m_y
,x
,y
);
76 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
79 AddLineToPoint(x
,y
+h
);
80 AddLineToPoint(x
+w
,y
+h
);
81 AddLineToPoint(x
+w
,y
);
85 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
88 AddArc( x
,y
,r
,0,2*M_PI
,false);
92 //-----------------------------------------------------------------------------
93 // wxGraphicsContext Convenience Methods
94 //-----------------------------------------------------------------------------
96 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
98 FillPath( path
, fillStyle
);
102 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
106 DrawText( str
, 0, 0 );
111 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
113 wxGraphicsPath
* path
= CreatePath();
114 path
->MoveToPoint(x1
, y1
) ;
115 path
->AddLineToPoint( x2
, y2
);
120 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
122 wxGraphicsPath
* path
= CreatePath();
123 path
->AddRectangle( x
, y
, w
, h
);
128 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
130 wxGraphicsPath
* path
= CreatePath();
133 path
->AddCircle( x
+w
/2,y
+w
/2,w
/2);
139 Translate(x
+w
/2,y
+h
/2);
140 wxDouble factor
= ((wxDouble
) w
) / h
;
141 Scale( factor
, 1.0) ;
142 path
->AddCircle(0,0,h
/2);
149 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
151 wxGraphicsPath
* path
= CreatePath();
154 path
->AddRectangle( x
, y
, w
, h
);
162 double fw
= w
/ radius
;
163 double fh
= h
/ radius
;
165 path
->MoveToPoint(w
, h
/ 2);
166 path
->AddArcToPoint(w
, h
, w
/ 2, h
, radius
);
167 path
->AddArcToPoint(0, h
, 0, h
/ 2, radius
);
168 path
->AddArcToPoint(0, 0, w
/ 2, 0, radius
);
169 path
->AddArcToPoint(w
, 0, w
, h
/ 2, radius
);
170 path
->CloseSubpath();
177 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
180 wxGraphicsPath
* path
= CreatePath();
181 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
182 for ( int i
= 1 ; i
< n
; ++i
)
183 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
188 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
191 wxGraphicsPath
* path
= CreatePath();
192 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
) ;
193 for ( int i
= 1 ; i
< n
; ++i
)
194 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
195 DrawPath( path
, fillStyle
);
199 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
202 wxGraphicsPath
* path
= CreatePath();
203 for ( int i
= 0 ; i
< n
; ++i
)
205 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
) ;
206 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
212 // 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)
213 void wxGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
218 IMPLEMENT_ABSTRACT_CLASS(wxGCDC
, wxObject
)
220 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
224 const double RAD2DEG
= 180.0 / M_PI
;
225 const short kEmulatedMode
= -1;
226 const short kUnsupportedMode
= -2;
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
232 static inline double dmin(double a
, double b
)
234 return a
< b
? a
: b
;
236 static inline double dmax(double a
, double b
)
238 return a
> b
? a
: b
;
241 static inline double DegToRad(double deg
)
243 return (deg
* M_PI
) / 180.0;
245 static inline double RadToDeg(double deg
)
247 return (deg
* 180.0) / M_PI
;
250 //-----------------------------------------------------------------------------
252 //-----------------------------------------------------------------------------
260 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
263 m_graphicContext
= wxGraphicsContext::Create(dc
);
265 m_graphicContext
->SetFont(dc
.GetFont());
272 m_mm_to_pix_x
= mm2pt
;
273 m_mm_to_pix_y
= mm2pt
;
275 m_pen
= *wxBLACK_PEN
;
276 m_font
= *wxNORMAL_FONT
;
277 m_brush
= *wxWHITE_BRUSH
;
279 m_graphicContext
= NULL
;
285 delete m_graphicContext
;
288 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
290 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
291 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
293 wxCoord xx
= LogicalToDeviceX(x
);
294 wxCoord yy
= LogicalToDeviceY(y
);
295 wxCoord w
= bmp
.GetWidth();
296 wxCoord h
= bmp
.GetHeight();
297 wxCoord ww
= LogicalToDeviceXRel(w
);
298 wxCoord hh
= LogicalToDeviceYRel(h
);
300 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
303 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
305 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
306 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
308 wxCoord xx
= LogicalToDeviceX(x
);
309 wxCoord yy
= LogicalToDeviceY(y
);
310 wxCoord w
= icon
.GetWidth();
311 wxCoord h
= icon
.GetHeight();
312 wxCoord ww
= LogicalToDeviceXRel(w
);
313 wxCoord hh
= LogicalToDeviceYRel(h
);
315 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
318 void wxGCDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
322 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
324 wxCoord xx
, yy
, ww
, hh
;
325 xx
= LogicalToDeviceX(x
);
326 yy
= LogicalToDeviceY(y
);
327 ww
= LogicalToDeviceXRel(width
);
328 hh
= LogicalToDeviceYRel(height
);
330 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
331 CGRect clipRect
= CGRectMake( xx
, yy
, ww
, hh
);
332 CGContextClipToRect( cgContext
, clipRect
);
334 // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh );
335 // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
339 m_clipX1
= wxMax( m_clipX1
, xx
);
340 m_clipY1
= wxMax( m_clipY1
, yy
);
341 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
342 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
354 // TODO: as soon as we don't reset the context for each operation anymore
355 // we have to update the context as well
360 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
362 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
366 DestroyClippingRegion();
371 region
.GetBox( x
, y
, w
, h
);
372 wxCoord xx
, yy
, ww
, hh
;
373 xx
= LogicalToDeviceX(x
);
374 yy
= LogicalToDeviceY(y
);
375 ww
= LogicalToDeviceXRel(w
);
376 hh
= LogicalToDeviceYRel(h
);
378 // if we have a scaling that we cannot map onto native regions
379 // we must use the box
380 if ( ww
!= w
|| hh
!= h
)
382 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
387 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
);
388 if ( xx
!= x
|| yy
!= y
)
389 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
);
390 SectRgn( (RgnHandle
)m_macCurrentClipRgn
, (RgnHandle
)m_macBoundaryClipRgn
, (RgnHandle
)m_macCurrentClipRgn
);
395 m_clipX1
= wxMax( m_clipX1
, xx
);
396 m_clipY1
= wxMax( m_clipY1
, yy
);
397 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
398 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
412 void wxGCDC::DestroyClippingRegion()
416 // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn );
418 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
419 CGContextRestoreGState( cgContext
);
420 CGContextSaveGState( cgContext
);
422 m_graphicContext
->SetPen( m_pen
);
423 m_graphicContext
->SetBrush( m_brush
);
430 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
436 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
438 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
441 void wxGCDC::SetTextForeground( const wxColour
&col
)
443 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
445 if ( col
!= m_textForegroundColour
)
447 m_textForegroundColour
= col
;
448 m_graphicContext
->SetTextColor( col
);
452 void wxGCDC::SetTextBackground( const wxColour
&col
)
454 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
456 m_textBackgroundColour
= col
;
459 void wxGCDC::SetMapMode( int mode
)
464 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
468 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
472 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
476 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
481 SetLogicalScale( 1.0, 1.0 );
485 ComputeScaleAndOrigin();
488 void wxGCDC::SetUserScale( double x
, double y
)
490 // allow negative ? -> no
493 ComputeScaleAndOrigin();
496 void wxGCDC::SetLogicalScale( double x
, double y
)
501 ComputeScaleAndOrigin();
504 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
506 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
507 m_logicalOriginY
= y
* m_signY
;
508 ComputeScaleAndOrigin();
511 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
515 ComputeScaleAndOrigin();
518 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
520 m_signX
= (xLeftRight
? 1 : -1);
521 m_signY
= (yBottomUp
? -1 : 1);
522 ComputeScaleAndOrigin();
525 wxSize
wxGCDC::GetPPI() const
527 return wxSize(72, 72);
530 int wxGCDC::GetDepth() const
535 void wxGCDC::ComputeScaleAndOrigin()
537 // this is a bit artificial, but we need to force wxGCDC to think
538 // the pen has changed
539 wxPen
pen( GetPen() );
545 void wxGCDC::SetPalette( const wxPalette
& palette
)
548 void wxGCDC::SetBackgroundMode( int mode
)
550 m_backgroundMode
= mode
;
553 void wxGCDC::SetFont( const wxFont
&font
)
556 if ( m_graphicContext
)
557 m_graphicContext
->SetFont( font
);
560 void wxGCDC::SetPen( const wxPen
&pen
)
566 if ( m_graphicContext
)
568 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
570 m_graphicContext
->SetPen( m_pen
);
574 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
575 // eg when using a wxScrollWindow and scrolling around
576 int origX
= LogicalToDeviceX( 0 );
577 int origY
= LogicalToDeviceY( 0 );
578 m_graphicContext
->Translate( origX
, origY
);
579 m_graphicContext
->SetPen( m_pen
);
580 m_graphicContext
->Translate( -origX
, -origY
);
585 void wxGCDC::SetBrush( const wxBrush
&brush
)
587 if (m_brush
== brush
)
591 if ( m_graphicContext
)
593 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
595 m_graphicContext
->SetBrush( m_brush
);
599 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
600 // eg when using a wxScrollWindow and scrolling around
601 // TODO on MSW / GDIPlus this still occurs with hatched brushes
602 int origX
= LogicalToDeviceX(0);
603 int origY
= LogicalToDeviceY(0);
604 m_graphicContext
->Translate( origX
, origY
);
605 m_graphicContext
->SetBrush( m_brush
);
606 m_graphicContext
->Translate( -origX
, -origY
);
611 void wxGCDC::SetBackground( const wxBrush
&brush
)
613 if (m_backgroundBrush
== brush
)
616 m_backgroundBrush
= brush
;
617 if (!m_backgroundBrush
.Ok())
621 void wxGCDC::SetLogicalFunction( int function
)
623 if (m_logicalFunction
== function
)
626 m_logicalFunction
= function
;
627 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
629 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
630 if ( m_logicalFunction
== wxCOPY
)
631 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
632 else if ( m_logicalFunction
== wxINVERT
)
633 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
635 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
640 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
641 const wxColour
& col
, int style
);
643 bool wxGCDC::DoFloodFill(wxCoord x
, wxCoord y
,
644 const wxColour
& col
, int style
)
646 // return wxDoFloodFill(this, x, y, col, style);
650 bool wxGCDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
652 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
656 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
658 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
660 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
662 if ( m_logicalFunction
!= wxCOPY
)
666 wxCoord xx1
= LogicalToDeviceX(x1
);
667 wxCoord yy1
= LogicalToDeviceY(y1
);
668 wxCoord xx2
= LogicalToDeviceX(x2
);
669 wxCoord yy2
= LogicalToDeviceY(y2
);
671 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
673 CalcBoundingBox(x1
, y1
);
674 CalcBoundingBox(x2
, y2
);
677 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
679 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
681 if ( m_logicalFunction
!= wxCOPY
)
688 wxCoord xx
= LogicalToDeviceX(x
);
689 wxCoord yy
= LogicalToDeviceY(y
);
690 wxCoord xw
= LogicalToDeviceX(w
);
691 wxCoord x0
= LogicalToDeviceX(0);
692 wxCoord y0
= LogicalToDeviceY(0);
693 wxCoord yh
= LogicalToDeviceY(h
);
695 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
696 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
698 CalcBoundingBox(x0
, y0
);
699 CalcBoundingBox(x0
+xw
, y0
+yh
);
702 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
703 wxCoord x2
, wxCoord y2
,
704 wxCoord xc
, wxCoord yc
)
706 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
708 if ( m_logicalFunction
!= wxCOPY
)
711 wxCoord xx1
= LogicalToDeviceX(x1
);
712 wxCoord yy1
= LogicalToDeviceY(y1
);
713 wxCoord xx2
= LogicalToDeviceX(x2
);
714 wxCoord yy2
= LogicalToDeviceY(y2
);
715 wxCoord xxc
= LogicalToDeviceX(xc
);
716 wxCoord yyc
= LogicalToDeviceY(yc
);
718 double dx
= xx1
- xxc
;
719 double dy
= yy1
- yyc
;
720 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
721 wxCoord rad
= (wxCoord
)radius
;
723 if (xx1
== xx2
&& yy1
== yy2
)
728 else if (radius
== 0.0)
734 sa
= (xx1
- xxc
== 0) ?
735 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
736 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
737 ea
= (xx2
- xxc
== 0) ?
738 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
739 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
742 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
744 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
745 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
746 path
->MoveToPoint( xxc
, yyc
);
747 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
748 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
749 path
->AddLineToPoint( xxc
, yyc
);
750 m_graphicContext
->DrawPath(path
);
754 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
755 double sa
, double ea
)
757 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
759 if ( m_logicalFunction
!= wxCOPY
)
762 wxCoord xx
= LogicalToDeviceX(x
);
763 wxCoord yy
= LogicalToDeviceY(y
);
764 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
765 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
767 // handle -ve width and/or height
779 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
781 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
782 m_graphicContext
->PushState();
783 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
784 wxDouble factor
= ((wxDouble
) ww
) / hh
;
785 m_graphicContext
->Scale( factor
, 1.0) ;
786 if ( fill
&& (sa
!=ea
) )
787 path
->MoveToPoint(0,0);
788 path
->AddArc( 0, 0, hh
/2 , DegToRad(sa
) , DegToRad(ea
), sa
> ea
);
789 if ( fill
&& (sa
!=ea
) )
790 path
->AddLineToPoint(0,0);
791 m_graphicContext
->DrawPath( path
);
792 m_graphicContext
->PopState();
796 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
798 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
800 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
803 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
804 wxCoord xoffset
, wxCoord yoffset
)
806 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
808 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
810 if ( m_logicalFunction
!= wxCOPY
)
814 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
] ;
815 for( int i
= 0 ; i
< n
; ++i
)
817 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
818 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
821 m_graphicContext
->StrokeLines( n
, pointsD
) ;
826 void wxGCDC::DoDrawSpline(wxList
*points
)
828 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
830 if ( m_logicalFunction
!= wxCOPY
)
833 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
835 wxList::compatibility_iterator node
= points
->GetFirst();
836 if (node
== wxList::compatibility_iterator())
840 wxPoint
*p
= (wxPoint
*)node
->GetData();
845 node
= node
->GetNext();
846 p
= (wxPoint
*)node
->GetData();
850 wxCoord cx1
= ( x1
+ x2
) / 2;
851 wxCoord cy1
= ( y1
+ y2
) / 2;
853 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
854 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
857 while ((node
= node
->GetNext()) != NULL
)
860 while ((node
= node
->GetNext()))
864 p
= (wxPoint
*)node
->GetData();
869 wxCoord cx4
= (x1
+ x2
) / 2;
870 wxCoord cy4
= (y1
+ y2
) / 2;
872 path
->AddQuadCurveToPoint(
873 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
874 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
880 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
882 m_graphicContext
->StrokePath( path
);
887 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
888 wxCoord xoffset
, wxCoord yoffset
,
891 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
893 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
895 if ( m_logicalFunction
!= wxCOPY
)
898 bool closeIt
= false ;
899 if (points
[n
-1] != points
[0])
902 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)] ;
903 for( int i
= 0 ; i
< n
; ++i
)
905 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
906 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
909 pointsD
[n
] = pointsD
[0];
911 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
) ;
915 void wxGCDC::DoDrawPolyPolygon(int n
,
923 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
926 for ( int j
= 0 ; j
< n
; ++j
)
928 wxPoint start
= points
[i
];
929 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
)) ;
932 for ( int k
= 1 ; k
< l
; ++k
)
934 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
938 if ( start
!= points
[i
-1])
939 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
941 m_graphicContext
->DrawPath( path
, fillStyle
);
945 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
947 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
949 if ( m_logicalFunction
!= wxCOPY
)
952 wxCoord xx
= LogicalToDeviceX(x
);
953 wxCoord yy
= LogicalToDeviceY(y
);
954 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
955 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
957 // CMB: draw nothing if transformed w or h is 0
958 if (ww
== 0 || hh
== 0)
961 // CMB: handle -ve width and/or height
972 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
975 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
976 wxCoord width
, wxCoord height
,
979 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
981 if ( m_logicalFunction
!= wxCOPY
)
985 radius
= - radius
* ((width
< height
) ? width
: height
);
986 wxCoord xx
= LogicalToDeviceX(x
);
987 wxCoord yy
= LogicalToDeviceY(y
);
988 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
989 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
991 // CMB: draw nothing if transformed w or h is 0
992 if (ww
== 0 || hh
== 0)
995 // CMB: handle -ve width and/or height
1007 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1010 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1012 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1014 if ( m_logicalFunction
!= wxCOPY
)
1017 wxCoord xx
= LogicalToDeviceX(x
);
1018 wxCoord yy
= LogicalToDeviceY(y
);
1019 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1020 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1022 // CMB: draw nothing if transformed w or h is 0
1023 if (ww
== 0 || hh
== 0)
1026 // CMB: handle -ve width and/or height
1038 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1041 bool wxGCDC::CanDrawBitmap() const
1046 bool wxGCDC::DoBlit(
1047 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1048 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1049 wxCoord xsrcMask
, wxCoord ysrcMask
)
1051 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1052 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1054 if ( logical_func
== wxNO_OP
)
1057 if (xsrcMask
== -1 && ysrcMask
== -1)
1063 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1064 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1065 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1066 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1068 wxCoord yydest
= LogicalToDeviceY(ydest
);
1069 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1070 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1071 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1073 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1074 if ( memdc
&& logical_func
== wxCOPY
)
1077 wxBitmap blit
= memdc
->GetSelectedObject();
1079 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1081 wxCoord bmpwidth
= blit
.GetWidth();
1082 wxCoord bmpheight
= blit
.GetHeight();
1084 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1086 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1087 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1088 if ( wwsrc
> 0 && hhsrc
> 0 )
1090 if ( xxsrc
>= 0 && yysrc
>= 0 )
1092 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1093 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1094 blit
= blit
.GetSubBitmap( subrect
);
1098 // in this case we'd probably have to adjust the different coordinates, but
1099 // we have to find out proper contract first
1100 blit
= wxNullBitmap
;
1105 blit
= wxNullBitmap
;
1111 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1118 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") );
1125 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1128 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1130 if ( str
.length() == 0 )
1132 if ( m_logicalFunction
!= wxCOPY
)
1135 int drawX
= LogicalToDeviceX(x
);
1136 int drawY
= LogicalToDeviceY(y
);
1138 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1141 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1143 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1145 if ( str
.length() == 0 )
1147 if ( m_logicalFunction
!= wxCOPY
)
1150 int drawX
= LogicalToDeviceX(x
);
1151 int drawY
= LogicalToDeviceY(y
);
1153 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1156 bool wxGCDC::CanGetTextExtent() const
1158 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1163 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1164 wxCoord
*descent
, wxCoord
*externalLeading
,
1165 wxFont
*theFont
) const
1167 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1169 wxFont formerFont
= m_font
;
1172 m_graphicContext
->SetFont( *theFont
);
1175 wxDouble h
, d
, e
, w
;
1177 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1180 *height
= DeviceToLogicalYRel( h
);
1182 *descent
=DeviceToLogicalYRel( d
);
1183 if ( externalLeading
)
1184 *externalLeading
= DeviceToLogicalYRel( e
);
1186 *width
= DeviceToLogicalXRel( w
);
1190 m_graphicContext
->SetFont( m_font
);
1194 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1196 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1198 widths
.Add(0,text
.Length());
1199 if ( text
.IsEmpty() )
1202 wxArrayDouble widthsD
;
1204 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1205 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1206 widths
[i
] = DeviceToLogicalXRel( widthsD
[i
] + 0.5 ) ;
1211 wxCoord
wxGCDC::GetCharWidth(void) const
1214 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1219 wxCoord
wxGCDC::GetCharHeight(void) const
1222 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1227 void wxGCDC::Clear(void)
1229 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1233 void wxGCDC::DoGetSize(int *width
, int *height
) const
1239 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1240 const wxColour
& initialColour
,
1241 const wxColour
& destColour
,
1242 wxDirection nDirection
)
1249 start
= rect
.GetRightBottom();
1251 end
= rect
.GetLeftBottom();
1254 start
= rect
.GetLeftBottom();
1255 end
= rect
.GetRightBottom();
1259 start
= rect
.GetLeftBottom();
1261 end
= rect
.GetLeftTop();
1264 start
= rect
.GetLeftTop();
1265 end
= rect
.GetLeftBottom();
1270 m_graphicContext
->SetLinearGradientBrush(
1271 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1272 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1274 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1275 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1276 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1277 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1279 if (ww
== 0 || hh
== 0)
1293 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1294 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1295 m_graphicContext
->SetPen(m_pen
);
1298 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1299 const wxColour
& initialColour
,
1300 const wxColour
& destColour
,
1301 const wxPoint
& circleCenter
)
1304 wxInt32 cx
= rect
.GetWidth() / 2;
1305 wxInt32 cy
= rect
.GetHeight() / 2;
1312 wxCoord xx
= LogicalToDeviceX(rect
.x
);
1313 wxCoord yy
= LogicalToDeviceY(rect
.y
);
1314 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1315 wxCoord hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1317 if (ww
== 0 || hh
== 0)
1331 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1332 m_graphicContext
->SetBrush( wxBrush( destColour
) ) ;
1333 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1335 m_graphicContext
->SetRadialGradientBrush(
1336 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1337 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1338 LogicalToDeviceXRel(nRadius
),
1339 initialColour
,destColour
);
1341 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1342 m_graphicContext
->SetPen(m_pen
);
1345 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1346 wxCoord width
, wxCoord height
)
1348 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);