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();
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 x
, wxCoord y
, wxCoord width
, wxCoord height
)
344 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
346 wxCoord xx
, yy
, ww
, hh
;
347 xx
= LogicalToDeviceX(x
);
348 yy
= LogicalToDeviceY(y
);
349 ww
= LogicalToDeviceXRel(width
);
350 hh
= LogicalToDeviceYRel(height
);
352 m_graphicContext
->Clip( xx
, yy
, ww
, hh
);
355 m_clipX1
= wxMax( m_clipX1
, xx
);
356 m_clipY1
= wxMax( m_clipY1
, yy
);
357 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
358 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
371 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
373 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
377 DestroyClippingRegion();
382 region
.GetBox( x
, y
, w
, h
);
383 wxCoord xx
, yy
, ww
, hh
;
384 xx
= LogicalToDeviceX(x
);
385 yy
= LogicalToDeviceY(y
);
386 ww
= LogicalToDeviceXRel(w
);
387 hh
= LogicalToDeviceYRel(h
);
389 // if we have a scaling that we cannot map onto native regions
390 // we must use the box
391 if ( ww
!= w
|| hh
!= h
)
393 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
397 m_graphicContext
->Clip( region
);
400 m_clipX1
= wxMax( m_clipX1
, xx
);
401 m_clipY1
= wxMax( m_clipY1
, yy
);
402 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
403 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
417 void wxGCDC::DestroyClippingRegion()
419 m_graphicContext
->ResetClip();
420 m_graphicContext
->SetPen( m_pen
);
421 m_graphicContext
->SetBrush( m_brush
);
426 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
432 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
434 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
437 void wxGCDC::SetTextForeground( const wxColour
&col
)
439 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
441 if ( col
!= m_textForegroundColour
)
443 m_textForegroundColour
= col
;
444 m_graphicContext
->SetTextColor( col
);
448 void wxGCDC::SetTextBackground( const wxColour
&col
)
450 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
452 m_textBackgroundColour
= col
;
455 void wxGCDC::SetMapMode( int mode
)
460 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
464 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
468 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
472 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
477 SetLogicalScale( 1.0, 1.0 );
481 ComputeScaleAndOrigin();
484 void wxGCDC::SetUserScale( double x
, double y
)
486 // allow negative ? -> no
490 ComputeScaleAndOrigin();
493 void wxGCDC::SetLogicalScale( double x
, double y
)
498 ComputeScaleAndOrigin();
501 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
503 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
504 m_logicalOriginY
= y
* m_signY
;
505 ComputeScaleAndOrigin();
508 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
512 ComputeScaleAndOrigin();
515 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
517 m_signX
= (xLeftRight
? 1 : -1);
518 m_signY
= (yBottomUp
? -1 : 1);
519 ComputeScaleAndOrigin();
522 wxSize
wxGCDC::GetPPI() const
524 return wxSize(72, 72);
527 int wxGCDC::GetDepth() const
532 void wxGCDC::ComputeScaleAndOrigin()
534 // CMB: copy scale to see if it changes
535 double origScaleX
= m_scaleX
;
536 double origScaleY
= m_scaleY
;
537 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
538 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
539 m_deviceOriginX
= m_deviceOriginX
+ m_logicalOriginX
;
540 m_deviceOriginY
= m_deviceOriginY
+ m_logicalOriginY
;
542 // CMB: if scale has changed call SetPen to recalulate the line width
543 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
545 // this is a bit artificial, but we need to force wxDC to think
546 // the pen has changed
554 void wxGCDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
559 void wxGCDC::SetBackgroundMode( int mode
)
561 m_backgroundMode
= mode
;
564 void wxGCDC::SetFont( const wxFont
&font
)
567 if ( m_graphicContext
)
571 f
.SetPointSize( LogicalToDeviceYRel(font
.GetPointSize()));
572 m_graphicContext
->SetFont( f
);
576 void wxGCDC::SetPen( const wxPen
&pen
)
582 if ( m_graphicContext
)
584 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
586 m_graphicContext
->SetPen( m_pen
);
590 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
591 // eg when using a wxScrollWindow and scrolling around
592 int origX
= LogicalToDeviceX( 0 );
593 int origY
= LogicalToDeviceY( 0 );
594 m_graphicContext
->Translate( origX
, origY
);
595 m_graphicContext
->SetPen( m_pen
);
596 m_graphicContext
->Translate( -origX
, -origY
);
601 void wxGCDC::SetBrush( const wxBrush
&brush
)
603 if (m_brush
== brush
)
607 if ( m_graphicContext
)
609 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
611 m_graphicContext
->SetBrush( m_brush
);
615 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
616 // eg when using a wxScrollWindow and scrolling around
617 // TODO on MSW / GDIPlus this still occurs with hatched brushes
618 int origX
= LogicalToDeviceX(0);
619 int origY
= LogicalToDeviceY(0);
620 m_graphicContext
->Translate( origX
, origY
);
621 m_graphicContext
->SetBrush( m_brush
);
622 m_graphicContext
->Translate( -origX
, -origY
);
627 void wxGCDC::SetBackground( const wxBrush
&brush
)
629 if (m_backgroundBrush
== brush
)
632 m_backgroundBrush
= brush
;
633 if (!m_backgroundBrush
.Ok())
637 void wxGCDC::SetLogicalFunction( int function
)
639 if (m_logicalFunction
== function
)
642 m_logicalFunction
= function
;
643 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
645 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
646 if ( m_logicalFunction
== wxCOPY
)
647 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
648 else if ( m_logicalFunction
== wxINVERT
)
649 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
651 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
656 bool wxGCDC::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
657 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
662 bool wxGCDC::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
664 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
668 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
670 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
672 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
674 if ( m_logicalFunction
!= wxCOPY
)
678 wxCoord xx1
= LogicalToDeviceX(x1
);
679 wxCoord yy1
= LogicalToDeviceY(y1
);
680 wxCoord xx2
= LogicalToDeviceX(x2
);
681 wxCoord yy2
= LogicalToDeviceY(y2
);
683 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
685 CalcBoundingBox(x1
, y1
);
686 CalcBoundingBox(x2
, y2
);
689 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
691 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
693 if ( m_logicalFunction
!= wxCOPY
)
700 wxCoord xx
= LogicalToDeviceX(x
);
701 wxCoord yy
= LogicalToDeviceY(y
);
702 wxCoord xw
= LogicalToDeviceX(w
);
703 wxCoord x0
= LogicalToDeviceX(0);
704 wxCoord y0
= LogicalToDeviceY(0);
705 wxCoord yh
= LogicalToDeviceY(h
);
707 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
708 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
710 CalcBoundingBox(x0
, y0
);
711 CalcBoundingBox(x0
+xw
, y0
+yh
);
714 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
715 wxCoord x2
, wxCoord y2
,
716 wxCoord xc
, wxCoord yc
)
718 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
720 if ( m_logicalFunction
!= wxCOPY
)
723 wxCoord xx1
= LogicalToDeviceX(x1
);
724 wxCoord yy1
= LogicalToDeviceY(y1
);
725 wxCoord xx2
= LogicalToDeviceX(x2
);
726 wxCoord yy2
= LogicalToDeviceY(y2
);
727 wxCoord xxc
= LogicalToDeviceX(xc
);
728 wxCoord yyc
= LogicalToDeviceY(yc
);
730 double dx
= xx1
- xxc
;
731 double dy
= yy1
- yyc
;
732 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
733 wxCoord rad
= (wxCoord
)radius
;
735 if (xx1
== xx2
&& yy1
== yy2
)
740 else if (radius
== 0.0)
746 sa
= (xx1
- xxc
== 0) ?
747 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
748 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
749 ea
= (xx2
- xxc
== 0) ?
750 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
751 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
754 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
756 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
757 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
758 path
->MoveToPoint( xxc
, yyc
);
759 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
760 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
761 path
->AddLineToPoint( xxc
, yyc
);
762 m_graphicContext
->DrawPath(path
);
766 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
767 double sa
, double ea
)
769 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
771 if ( m_logicalFunction
!= wxCOPY
)
774 wxCoord xx
= LogicalToDeviceX(x
);
775 wxCoord yy
= LogicalToDeviceY(y
);
776 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
777 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
779 // handle -ve width and/or height
791 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
793 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
794 m_graphicContext
->PushState();
795 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
796 wxDouble factor
= ((wxDouble
) ww
) / hh
;
797 m_graphicContext
->Scale( factor
, 1.0);
798 if ( fill
&& (sa
!=ea
) )
799 path
->MoveToPoint(0,0);
800 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
801 // get clockwise angles
802 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
803 if ( fill
&& (sa
!=ea
) )
804 path
->AddLineToPoint(0,0);
805 m_graphicContext
->DrawPath( path
);
806 m_graphicContext
->PopState();
810 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
812 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
814 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
817 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
818 wxCoord xoffset
, wxCoord yoffset
)
820 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
822 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
824 if ( m_logicalFunction
!= wxCOPY
)
828 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
829 for( int i
= 0; i
< n
; ++i
)
831 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
832 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
835 m_graphicContext
->StrokeLines( n
, pointsD
);
840 void wxGCDC::DoDrawSpline(wxList
*points
)
842 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
844 if ( m_logicalFunction
!= wxCOPY
)
847 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
849 wxList::compatibility_iterator node
= points
->GetFirst();
850 if (node
== wxList::compatibility_iterator())
854 wxPoint
*p
= (wxPoint
*)node
->GetData();
859 node
= node
->GetNext();
860 p
= (wxPoint
*)node
->GetData();
864 wxCoord cx1
= ( x1
+ x2
) / 2;
865 wxCoord cy1
= ( y1
+ y2
) / 2;
867 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
868 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
871 while ((node
= node
->GetNext()) != NULL
)
874 while ((node
= node
->GetNext()))
878 p
= (wxPoint
*)node
->GetData();
883 wxCoord cx4
= (x1
+ x2
) / 2;
884 wxCoord cy4
= (y1
+ y2
) / 2;
886 path
->AddQuadCurveToPoint(
887 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
888 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
894 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
896 m_graphicContext
->StrokePath( path
);
899 #endif // wxUSE_SPLINES
901 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
902 wxCoord xoffset
, wxCoord yoffset
,
905 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
907 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
909 if ( m_logicalFunction
!= wxCOPY
)
912 bool closeIt
= false;
913 if (points
[n
-1] != points
[0])
916 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
917 for( int i
= 0; i
< n
; ++i
)
919 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
920 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
923 pointsD
[n
] = pointsD
[0];
925 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
929 void wxGCDC::DoDrawPolyPolygon(int n
,
937 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
940 for ( int j
= 0; j
< n
; ++j
)
942 wxPoint start
= points
[i
];
943 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
946 for ( int k
= 1; k
< l
; ++k
)
948 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
952 if ( start
!= points
[i
-1])
953 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
955 m_graphicContext
->DrawPath( path
, fillStyle
);
959 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
961 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
963 if ( m_logicalFunction
!= wxCOPY
)
966 wxCoord xx
= LogicalToDeviceX(x
);
967 wxCoord yy
= LogicalToDeviceY(y
);
968 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
969 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
971 // CMB: draw nothing if transformed w or h is 0
972 if (ww
== 0 || hh
== 0)
975 // CMB: handle -ve width and/or height
986 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
989 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
990 wxCoord width
, wxCoord height
,
993 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
995 if ( m_logicalFunction
!= wxCOPY
)
999 radius
= - radius
* ((width
< height
) ? width
: height
);
1000 wxCoord xx
= LogicalToDeviceX(x
);
1001 wxCoord yy
= LogicalToDeviceY(y
);
1002 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1003 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1005 // CMB: draw nothing if transformed w or h is 0
1006 if (ww
== 0 || hh
== 0)
1009 // CMB: handle -ve width and/or height
1021 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1024 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1026 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1028 if ( m_logicalFunction
!= wxCOPY
)
1031 wxDouble xx
= LogicalToDeviceX(x
);
1032 wxDouble yy
= LogicalToDeviceY(y
);
1033 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1034 wxDouble 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
->DrawEllipse(xx
,yy
,ww
,hh
);
1055 bool wxGCDC::CanDrawBitmap() const
1060 bool wxGCDC::DoBlit(
1061 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1062 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool WXUNUSED(useMask
),
1063 wxCoord xsrcMask
, wxCoord ysrcMask
)
1065 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1066 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1068 if ( logical_func
== wxNO_OP
)
1071 if (xsrcMask
== -1 && ysrcMask
== -1)
1077 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1078 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1079 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1080 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1082 wxCoord yydest
= LogicalToDeviceY(ydest
);
1083 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1084 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1085 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1087 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1088 if ( memdc
&& logical_func
== wxCOPY
)
1090 wxBitmap blit
= memdc
->GetSelectedBitmap();
1092 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1094 wxCoord bmpwidth
= blit
.GetWidth();
1095 wxCoord bmpheight
= blit
.GetHeight();
1097 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1099 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1100 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1101 if ( wwsrc
> 0 && hhsrc
> 0 )
1103 if ( xxsrc
>= 0 && yysrc
>= 0 )
1105 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1106 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1107 blit
= blit
.GetSubBitmap( subrect
);
1111 // in this case we'd probably have to adjust the different coordinates, but
1112 // we have to find out proper contract first
1113 blit
= wxNullBitmap
;
1118 blit
= wxNullBitmap
;
1124 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1129 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts, and only with wxCOPY logical operation.") );
1136 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1139 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1141 if ( str
.length() == 0 )
1143 if ( m_logicalFunction
!= wxCOPY
)
1146 int drawX
= LogicalToDeviceX(x
);
1147 int drawY
= LogicalToDeviceY(y
);
1149 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1152 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1154 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1156 if ( str
.length() == 0 )
1158 if ( m_logicalFunction
!= wxCOPY
)
1161 int drawX
= LogicalToDeviceX(x
);
1162 int drawY
= LogicalToDeviceY(y
);
1164 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1167 bool wxGCDC::CanGetTextExtent() const
1169 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1174 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1175 wxCoord
*descent
, wxCoord
*externalLeading
,
1176 wxFont
*theFont
) const
1178 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1182 m_graphicContext
->SetFont( *theFont
);
1185 wxDouble h
, d
, e
, w
;
1187 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1190 *height
= DeviceToLogicalYRel((wxCoord
)h
);
1192 *descent
= DeviceToLogicalYRel((wxCoord
)d
);
1193 if ( externalLeading
)
1194 *externalLeading
= DeviceToLogicalYRel((wxCoord
)e
);
1196 *width
= DeviceToLogicalXRel((wxCoord
)w
);
1200 m_graphicContext
->SetFont( m_font
);
1204 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1206 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1208 widths
.Add(0,text
.Length());
1209 if ( text
.IsEmpty() )
1212 wxArrayDouble widthsD
;
1214 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1215 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1216 widths
[i
] = DeviceToLogicalXRel((wxCoord
)(widthsD
[i
] + 0.5));
1221 wxCoord
wxGCDC::GetCharWidth(void) const
1224 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1229 wxCoord
wxGCDC::GetCharHeight(void) const
1232 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1237 void wxGCDC::Clear(void)
1239 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1240 // TODO better implementation / incorporate size info into wxGCDC or context
1241 m_graphicContext
->SetBrush( m_backgroundBrush
);
1242 wxPen p
= *wxTRANSPARENT_PEN
;
1243 m_graphicContext
->SetPen( p
);
1244 DoDrawRectangle( 0, 0, 32000 , 32000 );
1245 m_graphicContext
->SetPen( m_pen
);
1246 m_graphicContext
->SetBrush( m_brush
);
1249 void wxGCDC::DoGetSize(int *width
, int *height
) const
1255 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1256 const wxColour
& initialColour
,
1257 const wxColour
& destColour
,
1258 wxDirection nDirection
)
1265 start
= rect
.GetRightBottom();
1267 end
= rect
.GetLeftBottom();
1270 start
= rect
.GetLeftBottom();
1271 end
= rect
.GetRightBottom();
1275 start
= rect
.GetLeftBottom();
1277 end
= rect
.GetLeftTop();
1280 start
= rect
.GetLeftTop();
1281 end
= rect
.GetLeftBottom();
1288 m_graphicContext
->SetLinearGradientBrush(
1289 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1290 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1292 wxDouble xx
= LogicalToDeviceX(rect
.x
);
1293 wxDouble yy
= LogicalToDeviceY(rect
.y
);
1294 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1295 wxDouble hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1297 if (ww
== 0 || hh
== 0)
1311 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1312 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1313 m_graphicContext
->SetPen(m_pen
);
1316 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1317 const wxColour
& initialColour
,
1318 const wxColour
& destColour
,
1319 const wxPoint
& circleCenter
)
1322 wxInt32 cx
= rect
.GetWidth() / 2;
1323 wxInt32 cy
= rect
.GetHeight() / 2;
1330 wxDouble xx
= LogicalToDeviceX(rect
.x
);
1331 wxDouble yy
= LogicalToDeviceY(rect
.y
);
1332 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1333 wxDouble hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1335 if (ww
== 0 || hh
== 0)
1349 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1350 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1351 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1353 m_graphicContext
->SetRadialGradientBrush(
1354 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1355 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1356 LogicalToDeviceXRel(nRadius
),
1357 initialColour
,destColour
);
1359 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1360 m_graphicContext
->SetPen(m_pen
);
1363 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1364 wxCoord width
, wxCoord height
)
1366 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);
1369 #endif // wxUSE_GRAPHICS_CONTEXT