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 //-----------------------------------------------------------------------------
277 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
279 delete m_graphicContext
;
280 m_graphicContext
= ctx
;
283 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
286 m_graphicContext
= wxGraphicsContext::Create(dc
);
288 if ( dc
.GetFont().Ok())
289 m_graphicContext
->SetFont(dc
.GetFont());
290 if ( dc
.GetPen().Ok())
291 m_graphicContext
->SetPen(dc
.GetPen());
292 if ( dc
.GetBrush().Ok())
293 m_graphicContext
->SetBrush(dc
.GetBrush());
294 m_graphicContext
->SetTextColor(dc
.GetTextForeground());
301 m_mm_to_pix_x
= mm2pt
;
302 m_mm_to_pix_y
= mm2pt
;
304 m_pen
= *wxBLACK_PEN
;
305 m_font
= *wxNORMAL_FONT
;
306 m_brush
= *wxWHITE_BRUSH
;
308 m_graphicContext
= NULL
;
314 delete m_graphicContext
;
317 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
319 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
320 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
322 wxCoord xx
= LogicalToDeviceX(x
);
323 wxCoord yy
= LogicalToDeviceY(y
);
324 wxCoord w
= bmp
.GetWidth();
325 wxCoord h
= bmp
.GetHeight();
326 wxCoord ww
= LogicalToDeviceXRel(w
);
327 wxCoord hh
= LogicalToDeviceYRel(h
);
329 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
332 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
334 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
335 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
337 wxCoord xx
= LogicalToDeviceX(x
);
338 wxCoord yy
= LogicalToDeviceY(y
);
339 wxCoord w
= icon
.GetWidth();
340 wxCoord h
= icon
.GetHeight();
341 wxCoord ww
= LogicalToDeviceXRel(w
);
342 wxCoord hh
= LogicalToDeviceYRel(h
);
344 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
347 void wxGCDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
349 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
351 wxCoord xx
, yy
, ww
, hh
;
352 xx
= LogicalToDeviceX(x
);
353 yy
= LogicalToDeviceY(y
);
354 ww
= LogicalToDeviceXRel(width
);
355 hh
= LogicalToDeviceYRel(height
);
357 m_graphicContext
->Clip( xx
, yy
, ww
, hh
);
360 m_clipX1
= wxMax( m_clipX1
, xx
);
361 m_clipY1
= wxMax( m_clipY1
, yy
);
362 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
363 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
376 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
378 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
382 DestroyClippingRegion();
387 region
.GetBox( x
, y
, w
, h
);
388 wxCoord xx
, yy
, ww
, hh
;
389 xx
= LogicalToDeviceX(x
);
390 yy
= LogicalToDeviceY(y
);
391 ww
= LogicalToDeviceXRel(w
);
392 hh
= LogicalToDeviceYRel(h
);
394 // if we have a scaling that we cannot map onto native regions
395 // we must use the box
396 if ( ww
!= w
|| hh
!= h
)
398 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
402 m_graphicContext
->Clip( region
);
405 m_clipX1
= wxMax( m_clipX1
, xx
);
406 m_clipY1
= wxMax( m_clipY1
, yy
);
407 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
408 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
422 void wxGCDC::DestroyClippingRegion()
424 m_graphicContext
->ResetClip();
425 m_graphicContext
->SetPen( m_pen
);
426 m_graphicContext
->SetBrush( m_brush
);
431 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
437 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
439 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
442 void wxGCDC::SetTextForeground( const wxColour
&col
)
444 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
446 if ( col
!= m_textForegroundColour
)
448 m_textForegroundColour
= col
;
449 m_graphicContext
->SetTextColor( col
);
453 void wxGCDC::SetTextBackground( const wxColour
&col
)
455 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
457 m_textBackgroundColour
= col
;
460 void wxGCDC::SetMapMode( int mode
)
465 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
469 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
473 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
477 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
482 SetLogicalScale( 1.0, 1.0 );
486 ComputeScaleAndOrigin();
489 void wxGCDC::SetUserScale( double x
, double y
)
491 // allow negative ? -> no
495 ComputeScaleAndOrigin();
498 void wxGCDC::SetLogicalScale( double x
, double y
)
503 ComputeScaleAndOrigin();
506 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
508 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
509 m_logicalOriginY
= y
* m_signY
;
510 ComputeScaleAndOrigin();
513 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
517 ComputeScaleAndOrigin();
520 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
522 m_signX
= (xLeftRight
? 1 : -1);
523 m_signY
= (yBottomUp
? -1 : 1);
524 ComputeScaleAndOrigin();
527 wxSize
wxGCDC::GetPPI() const
529 return wxSize(72, 72);
532 int wxGCDC::GetDepth() const
537 void wxGCDC::ComputeScaleAndOrigin()
539 // CMB: copy scale to see if it changes
540 double origScaleX
= m_scaleX
;
541 double origScaleY
= m_scaleY
;
542 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
543 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
544 m_deviceOriginX
= m_deviceOriginX
+ m_logicalOriginX
;
545 m_deviceOriginY
= m_deviceOriginY
+ m_logicalOriginY
;
547 // CMB: if scale has changed call SetPen to recalulate the line width
548 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
550 // this is a bit artificial, but we need to force wxDC to think
551 // the pen has changed
559 void wxGCDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
564 void wxGCDC::SetBackgroundMode( int mode
)
566 m_backgroundMode
= mode
;
569 void wxGCDC::SetFont( const wxFont
&font
)
572 if ( m_graphicContext
)
576 f
.SetPointSize( LogicalToDeviceYRel(font
.GetPointSize()));
577 m_graphicContext
->SetFont( f
);
581 void wxGCDC::SetPen( const wxPen
&pen
)
587 if ( m_graphicContext
)
589 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
591 m_graphicContext
->SetPen( m_pen
);
595 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
596 // eg when using a wxScrollWindow and scrolling around
597 int origX
= LogicalToDeviceX( 0 );
598 int origY
= LogicalToDeviceY( 0 );
599 m_graphicContext
->Translate( origX
, origY
);
600 m_graphicContext
->SetPen( m_pen
);
601 m_graphicContext
->Translate( -origX
, -origY
);
606 void wxGCDC::SetBrush( const wxBrush
&brush
)
608 if (m_brush
== brush
)
612 if ( m_graphicContext
)
614 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
616 m_graphicContext
->SetBrush( m_brush
);
620 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
621 // eg when using a wxScrollWindow and scrolling around
622 // TODO on MSW / GDIPlus this still occurs with hatched brushes
623 int origX
= LogicalToDeviceX(0);
624 int origY
= LogicalToDeviceY(0);
625 m_graphicContext
->Translate( origX
, origY
);
626 m_graphicContext
->SetBrush( m_brush
);
627 m_graphicContext
->Translate( -origX
, -origY
);
632 void wxGCDC::SetBackground( const wxBrush
&brush
)
634 if (m_backgroundBrush
== brush
)
637 m_backgroundBrush
= brush
;
638 if (!m_backgroundBrush
.Ok())
642 void wxGCDC::SetLogicalFunction( int function
)
644 if (m_logicalFunction
== function
)
647 m_logicalFunction
= function
;
648 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
650 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
651 if ( m_logicalFunction
== wxCOPY
)
652 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
653 else if ( m_logicalFunction
== wxINVERT
)
654 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
656 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
661 bool wxGCDC::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
662 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
667 bool wxGCDC::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
669 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
673 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
675 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
677 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
679 if ( m_logicalFunction
!= wxCOPY
)
683 wxCoord xx1
= LogicalToDeviceX(x1
);
684 wxCoord yy1
= LogicalToDeviceY(y1
);
685 wxCoord xx2
= LogicalToDeviceX(x2
);
686 wxCoord yy2
= LogicalToDeviceY(y2
);
688 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
690 CalcBoundingBox(x1
, y1
);
691 CalcBoundingBox(x2
, y2
);
694 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
696 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
698 if ( m_logicalFunction
!= wxCOPY
)
705 wxCoord xx
= LogicalToDeviceX(x
);
706 wxCoord yy
= LogicalToDeviceY(y
);
707 wxCoord xw
= LogicalToDeviceX(w
);
708 wxCoord x0
= LogicalToDeviceX(0);
709 wxCoord y0
= LogicalToDeviceY(0);
710 wxCoord yh
= LogicalToDeviceY(h
);
712 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
713 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
715 CalcBoundingBox(x0
, y0
);
716 CalcBoundingBox(x0
+xw
, y0
+yh
);
719 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
720 wxCoord x2
, wxCoord y2
,
721 wxCoord xc
, wxCoord yc
)
723 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
725 if ( m_logicalFunction
!= wxCOPY
)
728 wxCoord xx1
= LogicalToDeviceX(x1
);
729 wxCoord yy1
= LogicalToDeviceY(y1
);
730 wxCoord xx2
= LogicalToDeviceX(x2
);
731 wxCoord yy2
= LogicalToDeviceY(y2
);
732 wxCoord xxc
= LogicalToDeviceX(xc
);
733 wxCoord yyc
= LogicalToDeviceY(yc
);
735 double dx
= xx1
- xxc
;
736 double dy
= yy1
- yyc
;
737 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
738 wxCoord rad
= (wxCoord
)radius
;
740 if (xx1
== xx2
&& yy1
== yy2
)
745 else if (radius
== 0.0)
751 sa
= (xx1
- xxc
== 0) ?
752 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
753 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
754 ea
= (xx2
- xxc
== 0) ?
755 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
756 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
759 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
761 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
762 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
763 path
->MoveToPoint( xxc
, yyc
);
764 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
765 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
766 path
->AddLineToPoint( xxc
, yyc
);
767 m_graphicContext
->DrawPath(path
);
771 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
772 double sa
, double ea
)
774 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
776 if ( m_logicalFunction
!= wxCOPY
)
779 wxCoord xx
= LogicalToDeviceX(x
);
780 wxCoord yy
= LogicalToDeviceY(y
);
781 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
782 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
784 // handle -ve width and/or height
796 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
798 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
799 m_graphicContext
->PushState();
800 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
801 wxDouble factor
= ((wxDouble
) ww
) / hh
;
802 m_graphicContext
->Scale( factor
, 1.0);
803 if ( fill
&& (sa
!=ea
) )
804 path
->MoveToPoint(0,0);
805 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
806 // get clockwise angles
807 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
808 if ( fill
&& (sa
!=ea
) )
809 path
->AddLineToPoint(0,0);
810 m_graphicContext
->DrawPath( path
);
811 m_graphicContext
->PopState();
815 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
817 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
819 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
822 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
823 wxCoord xoffset
, wxCoord yoffset
)
825 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
827 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
829 if ( m_logicalFunction
!= wxCOPY
)
833 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
834 for( int i
= 0; i
< n
; ++i
)
836 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
837 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
840 m_graphicContext
->StrokeLines( n
, pointsD
);
845 void wxGCDC::DoDrawSpline(wxList
*points
)
847 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
849 if ( m_logicalFunction
!= wxCOPY
)
852 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
854 wxList::compatibility_iterator node
= points
->GetFirst();
855 if (node
== wxList::compatibility_iterator())
859 wxPoint
*p
= (wxPoint
*)node
->GetData();
864 node
= node
->GetNext();
865 p
= (wxPoint
*)node
->GetData();
869 wxCoord cx1
= ( x1
+ x2
) / 2;
870 wxCoord cy1
= ( y1
+ y2
) / 2;
872 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
873 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
876 while ((node
= node
->GetNext()) != NULL
)
879 while ((node
= node
->GetNext()))
883 p
= (wxPoint
*)node
->GetData();
888 wxCoord cx4
= (x1
+ x2
) / 2;
889 wxCoord cy4
= (y1
+ y2
) / 2;
891 path
->AddQuadCurveToPoint(
892 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
893 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
899 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
901 m_graphicContext
->StrokePath( path
);
904 #endif // wxUSE_SPLINES
906 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
907 wxCoord xoffset
, wxCoord yoffset
,
910 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
912 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
914 if ( m_logicalFunction
!= wxCOPY
)
917 bool closeIt
= false;
918 if (points
[n
-1] != points
[0])
921 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
922 for( int i
= 0; i
< n
; ++i
)
924 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
925 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
928 pointsD
[n
] = pointsD
[0];
930 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
934 void wxGCDC::DoDrawPolyPolygon(int n
,
942 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
945 for ( int j
= 0; j
< n
; ++j
)
947 wxPoint start
= points
[i
];
948 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
951 for ( int k
= 1; k
< l
; ++k
)
953 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
957 if ( start
!= points
[i
-1])
958 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
960 m_graphicContext
->DrawPath( path
, fillStyle
);
964 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
966 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
968 if ( m_logicalFunction
!= wxCOPY
)
971 wxCoord xx
= LogicalToDeviceX(x
);
972 wxCoord yy
= LogicalToDeviceY(y
);
973 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
974 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
976 // CMB: draw nothing if transformed w or h is 0
977 if (ww
== 0 || hh
== 0)
980 // CMB: handle -ve width and/or height
991 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
994 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
995 wxCoord width
, wxCoord height
,
998 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1000 if ( m_logicalFunction
!= wxCOPY
)
1004 radius
= - radius
* ((width
< height
) ? width
: height
);
1005 wxCoord xx
= LogicalToDeviceX(x
);
1006 wxCoord yy
= LogicalToDeviceY(y
);
1007 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1008 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1010 // CMB: draw nothing if transformed w or h is 0
1011 if (ww
== 0 || hh
== 0)
1014 // CMB: handle -ve width and/or height
1026 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1029 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1031 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1033 if ( m_logicalFunction
!= wxCOPY
)
1036 wxDouble xx
= LogicalToDeviceX(x
);
1037 wxDouble yy
= LogicalToDeviceY(y
);
1038 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1039 wxDouble 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
->DrawEllipse(xx
,yy
,ww
,hh
);
1060 bool wxGCDC::CanDrawBitmap() const
1065 bool wxGCDC::DoBlit(
1066 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1067 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool WXUNUSED(useMask
),
1068 wxCoord xsrcMask
, wxCoord ysrcMask
)
1070 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1071 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1073 if ( logical_func
== wxNO_OP
)
1076 if (xsrcMask
== -1 && ysrcMask
== -1)
1082 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1083 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1084 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1085 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1087 wxCoord yydest
= LogicalToDeviceY(ydest
);
1088 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1089 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1090 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1092 wxMemoryDC
* memdc
= dynamic_cast<wxMemoryDC
*>(source
);
1093 if ( memdc
&& logical_func
== wxCOPY
)
1095 wxBitmap blit
= memdc
->GetSelectedBitmap();
1097 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1099 wxCoord bmpwidth
= blit
.GetWidth();
1100 wxCoord bmpheight
= blit
.GetHeight();
1102 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1104 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1105 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1106 if ( wwsrc
> 0 && hhsrc
> 0 )
1108 if ( xxsrc
>= 0 && yysrc
>= 0 )
1110 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1111 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1112 blit
= blit
.GetSubBitmap( subrect
);
1116 // in this case we'd probably have to adjust the different coordinates, but
1117 // we have to find out proper contract first
1118 blit
= wxNullBitmap
;
1123 blit
= wxNullBitmap
;
1129 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1134 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts, and only with wxCOPY logical operation.") );
1141 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1144 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1146 if ( str
.length() == 0 )
1148 if ( m_logicalFunction
!= wxCOPY
)
1151 int drawX
= LogicalToDeviceX(x
);
1152 int drawY
= LogicalToDeviceY(y
);
1154 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1157 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1159 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1161 if ( str
.length() == 0 )
1163 if ( m_logicalFunction
!= wxCOPY
)
1166 int drawX
= LogicalToDeviceX(x
);
1167 int drawY
= LogicalToDeviceY(y
);
1169 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1172 bool wxGCDC::CanGetTextExtent() const
1174 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1179 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1180 wxCoord
*descent
, wxCoord
*externalLeading
,
1181 wxFont
*theFont
) const
1183 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1187 m_graphicContext
->SetFont( *theFont
);
1190 wxDouble h
, d
, e
, w
;
1192 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1195 *height
= DeviceToLogicalYRel((wxCoord
)h
);
1197 *descent
= DeviceToLogicalYRel((wxCoord
)d
);
1198 if ( externalLeading
)
1199 *externalLeading
= DeviceToLogicalYRel((wxCoord
)e
);
1201 *width
= DeviceToLogicalXRel((wxCoord
)w
);
1205 m_graphicContext
->SetFont( m_font
);
1209 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1211 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1213 widths
.Add(0,text
.Length());
1214 if ( text
.IsEmpty() )
1217 wxArrayDouble widthsD
;
1219 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1220 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1221 widths
[i
] = DeviceToLogicalXRel((wxCoord
)(widthsD
[i
] + 0.5));
1226 wxCoord
wxGCDC::GetCharWidth(void) const
1229 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1234 wxCoord
wxGCDC::GetCharHeight(void) const
1237 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1242 void wxGCDC::Clear(void)
1244 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1245 // TODO better implementation / incorporate size info into wxGCDC or context
1246 m_graphicContext
->SetBrush( m_backgroundBrush
);
1247 wxPen p
= *wxTRANSPARENT_PEN
;
1248 m_graphicContext
->SetPen( p
);
1249 DoDrawRectangle( 0, 0, 32000 , 32000 );
1250 m_graphicContext
->SetPen( m_pen
);
1251 m_graphicContext
->SetBrush( m_brush
);
1254 void wxGCDC::DoGetSize(int *width
, int *height
) const
1260 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1261 const wxColour
& initialColour
,
1262 const wxColour
& destColour
,
1263 wxDirection nDirection
)
1270 start
= rect
.GetRightBottom();
1272 end
= rect
.GetLeftBottom();
1275 start
= rect
.GetLeftBottom();
1276 end
= rect
.GetRightBottom();
1280 start
= rect
.GetLeftBottom();
1282 end
= rect
.GetLeftTop();
1285 start
= rect
.GetLeftTop();
1286 end
= rect
.GetLeftBottom();
1293 m_graphicContext
->SetLinearGradientBrush(
1294 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1295 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1297 wxDouble xx
= LogicalToDeviceX(rect
.x
);
1298 wxDouble yy
= LogicalToDeviceY(rect
.y
);
1299 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1300 wxDouble 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 wxDouble xx
= LogicalToDeviceX(rect
.x
);
1336 wxDouble yy
= LogicalToDeviceY(rect
.y
);
1337 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1338 wxDouble 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