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 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath
, wxObject
)
53 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
57 return wxPoint2DDouble(x
,y
);
60 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
62 MoveToPoint( p
.m_x
, p
.m_y
);
65 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
67 AddLineToPoint( p
.m_x
, p
.m_y
);
70 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
72 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
75 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
77 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
84 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
86 // calculate using degree elevation to a cubic bezier
90 wxPoint2DDouble start
= GetCurrentPoint();
91 wxPoint2DDouble
end(x
,y
);
92 wxPoint2DDouble
c(cx
,cy
);
93 c1
= wxDouble(1/3.0) * start
+ wxDouble(2/3.0) * c
;
94 c2
= wxDouble(2/3.0) * c
+ wxDouble(1/3.0) * end
;
95 AddCurveToPoint(c1
.m_x
,c1
.m_y
,c2
.m_x
,c2
.m_y
,x
,y
);
98 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
101 AddLineToPoint(x
,y
+h
);
102 AddLineToPoint(x
+w
,y
+h
);
103 AddLineToPoint(x
+w
,y
);
107 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
110 AddArc( x
,y
,r
,0,2*M_PI
,false);
114 // 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)
115 void wxGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
117 wxPoint2DDouble current
= GetCurrentPoint();
118 wxPoint2DDouble
p1(x1
,y1
);
119 wxPoint2DDouble
p2(x2
,y2
);
121 wxPoint2DDouble v1
= current
- p1
;
123 wxPoint2DDouble v2
= p2
- p1
;
126 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
130 // TODO obtuse angles
132 alpha
= DegToRad(alpha
);
134 wxDouble dist
= r
/ sin(alpha
/2) * cos(alpha
/2);
135 // calculate tangential points
136 wxPoint2DDouble t1
= dist
*v1
+ p1
;
137 wxPoint2DDouble t2
= dist
*v2
+ p1
;
139 wxPoint2DDouble nv1
= v1
;
140 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
141 wxPoint2DDouble c
= t1
+ r
*nv1
;
143 wxDouble a1
= v1
.GetVectorAngle()+90;
144 wxDouble a2
= v2
.GetVectorAngle()-90;
147 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
151 //-----------------------------------------------------------------------------
152 // wxGraphicsContext Convenience Methods
153 //-----------------------------------------------------------------------------
155 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext
, wxObject
)
157 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
159 FillPath( path
, fillStyle
);
163 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
167 DrawText( str
, 0, 0 );
172 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
174 wxGraphicsPath
* path
= CreatePath();
175 path
->MoveToPoint(x1
, y1
);
176 path
->AddLineToPoint( x2
, y2
);
181 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
183 wxGraphicsPath
* path
= CreatePath();
184 path
->AddRectangle( x
, y
, w
, h
);
189 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
191 wxGraphicsPath
* path
= CreatePath();
194 path
->AddCircle( x
+w
/2,y
+w
/2,w
/2);
200 Translate(x
+w
/2,y
+h
/2);
201 wxDouble factor
= ((wxDouble
) w
) / h
;
202 Scale( factor
, 1.0);
203 path
->AddCircle(0,0,h
/2);
210 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
212 wxGraphicsPath
* path
= CreatePath();
215 path
->AddRectangle( x
, y
, w
, h
);
223 path
->MoveToPoint(w
, h
/ 2);
224 path
->AddArcToPoint(w
, h
, w
/ 2, h
, radius
);
225 path
->AddArcToPoint(0, h
, 0, h
/ 2, radius
);
226 path
->AddArcToPoint(0, 0, w
/ 2, 0, radius
);
227 path
->AddArcToPoint(w
, 0, w
, h
/ 2, radius
);
228 path
->CloseSubpath();
235 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
238 wxGraphicsPath
* path
= CreatePath();
239 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
);
240 for ( size_t i
= 1; i
< n
; ++i
)
241 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
246 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
249 wxGraphicsPath
* path
= CreatePath();
250 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
);
251 for ( size_t i
= 1; i
< n
; ++i
)
252 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
253 DrawPath( path
, fillStyle
);
257 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
260 wxGraphicsPath
* path
= CreatePath();
261 for ( size_t i
= 0; i
< n
; ++i
)
263 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
);
264 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
270 IMPLEMENT_ABSTRACT_CLASS(wxGCDC
, wxObject
)
272 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
281 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
283 delete m_graphicContext
;
284 m_graphicContext
= ctx
;
287 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
290 m_graphicContext
= wxGraphicsContext::Create(dc
);
292 if ( dc
.GetFont().Ok())
293 m_graphicContext
->SetFont(dc
.GetFont());
294 if ( dc
.GetPen().Ok())
295 m_graphicContext
->SetPen(dc
.GetPen());
296 if ( dc
.GetBrush().Ok())
297 m_graphicContext
->SetBrush(dc
.GetBrush());
298 m_graphicContext
->SetTextColor(dc
.GetTextForeground());
305 m_mm_to_pix_x
= mm2pt
;
306 m_mm_to_pix_y
= mm2pt
;
308 m_pen
= *wxBLACK_PEN
;
309 m_font
= *wxNORMAL_FONT
;
310 m_brush
= *wxWHITE_BRUSH
;
312 m_graphicContext
= NULL
;
318 delete m_graphicContext
;
321 void wxGCDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
323 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
324 wxCHECK_RET( bmp
.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
326 wxCoord xx
= LogicalToDeviceX(x
);
327 wxCoord yy
= LogicalToDeviceY(y
);
328 wxCoord w
= bmp
.GetWidth();
329 wxCoord h
= bmp
.GetHeight();
330 wxCoord ww
= LogicalToDeviceXRel(w
);
331 wxCoord hh
= LogicalToDeviceYRel(h
);
333 m_graphicContext
->DrawBitmap( bmp
, xx
, yy
, ww
, hh
);
336 void wxGCDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
338 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
339 wxCHECK_RET( icon
.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
341 wxCoord xx
= LogicalToDeviceX(x
);
342 wxCoord yy
= LogicalToDeviceY(y
);
343 wxCoord w
= icon
.GetWidth();
344 wxCoord h
= icon
.GetHeight();
345 wxCoord ww
= LogicalToDeviceXRel(w
);
346 wxCoord hh
= LogicalToDeviceYRel(h
);
348 m_graphicContext
->DrawIcon( icon
, xx
, yy
, ww
, hh
);
351 void wxGCDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
353 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
355 wxCoord xx
, yy
, ww
, hh
;
356 xx
= LogicalToDeviceX(x
);
357 yy
= LogicalToDeviceY(y
);
358 ww
= LogicalToDeviceXRel(width
);
359 hh
= LogicalToDeviceYRel(height
);
361 m_graphicContext
->Clip( xx
, yy
, ww
, hh
);
364 m_clipX1
= wxMax( m_clipX1
, xx
);
365 m_clipY1
= wxMax( m_clipY1
, yy
);
366 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
367 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
380 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
382 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
386 DestroyClippingRegion();
391 region
.GetBox( x
, y
, w
, h
);
392 wxCoord xx
, yy
, ww
, hh
;
393 xx
= LogicalToDeviceX(x
);
394 yy
= LogicalToDeviceY(y
);
395 ww
= LogicalToDeviceXRel(w
);
396 hh
= LogicalToDeviceYRel(h
);
398 // if we have a scaling that we cannot map onto native regions
399 // we must use the box
400 if ( ww
!= w
|| hh
!= h
)
402 wxGCDC::DoSetClippingRegion( x
, y
, w
, h
);
406 m_graphicContext
->Clip( region
);
409 m_clipX1
= wxMax( m_clipX1
, xx
);
410 m_clipY1
= wxMax( m_clipY1
, yy
);
411 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
) );
412 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
) );
426 void wxGCDC::DestroyClippingRegion()
428 m_graphicContext
->ResetClip();
429 m_graphicContext
->SetPen( m_pen
);
430 m_graphicContext
->SetBrush( m_brush
);
435 void wxGCDC::DoGetSizeMM( int* width
, int* height
) const
441 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
443 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
446 void wxGCDC::SetTextForeground( const wxColour
&col
)
448 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
450 if ( col
!= m_textForegroundColour
)
452 m_textForegroundColour
= col
;
453 m_graphicContext
->SetTextColor( col
);
457 void wxGCDC::SetTextBackground( const wxColour
&col
)
459 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
461 m_textBackgroundColour
= col
;
464 void wxGCDC::SetMapMode( int mode
)
469 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
473 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
477 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
481 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
486 SetLogicalScale( 1.0, 1.0 );
490 ComputeScaleAndOrigin();
493 void wxGCDC::SetUserScale( double x
, double y
)
495 // allow negative ? -> no
499 ComputeScaleAndOrigin();
502 void wxGCDC::SetLogicalScale( double x
, double y
)
507 ComputeScaleAndOrigin();
510 void wxGCDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
512 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
513 m_logicalOriginY
= y
* m_signY
;
514 ComputeScaleAndOrigin();
517 void wxGCDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
521 ComputeScaleAndOrigin();
524 void wxGCDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
526 m_signX
= (xLeftRight
? 1 : -1);
527 m_signY
= (yBottomUp
? -1 : 1);
528 ComputeScaleAndOrigin();
531 wxSize
wxGCDC::GetPPI() const
533 return wxSize(72, 72);
536 int wxGCDC::GetDepth() const
541 void wxGCDC::ComputeScaleAndOrigin()
543 // CMB: copy scale to see if it changes
544 double origScaleX
= m_scaleX
;
545 double origScaleY
= m_scaleY
;
546 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
547 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
548 m_deviceOriginX
= m_deviceOriginX
+ m_logicalOriginX
;
549 m_deviceOriginY
= m_deviceOriginY
+ m_logicalOriginY
;
551 // CMB: if scale has changed call SetPen to recalulate the line width
552 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
554 // this is a bit artificial, but we need to force wxDC to think
555 // the pen has changed
563 void wxGCDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
568 void wxGCDC::SetBackgroundMode( int mode
)
570 m_backgroundMode
= mode
;
573 void wxGCDC::SetFont( const wxFont
&font
)
576 if ( m_graphicContext
)
580 f
.SetPointSize( LogicalToDeviceYRel(font
.GetPointSize()));
581 m_graphicContext
->SetFont( f
);
585 void wxGCDC::SetPen( const wxPen
&pen
)
591 if ( m_graphicContext
)
593 if ( m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
)
595 m_graphicContext
->SetPen( m_pen
);
599 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
600 // eg when using a wxScrollWindow and scrolling around
601 int origX
= LogicalToDeviceX( 0 );
602 int origY
= LogicalToDeviceY( 0 );
603 m_graphicContext
->Translate( origX
, origY
);
604 m_graphicContext
->SetPen( m_pen
);
605 m_graphicContext
->Translate( -origX
, -origY
);
610 void wxGCDC::SetBrush( const wxBrush
&brush
)
612 if (m_brush
== brush
)
616 if ( m_graphicContext
)
618 if ( brush
.GetStyle() == wxSOLID
|| brush
.GetStyle() == wxTRANSPARENT
)
620 m_graphicContext
->SetBrush( m_brush
);
624 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
625 // eg when using a wxScrollWindow and scrolling around
626 // TODO on MSW / GDIPlus this still occurs with hatched brushes
627 int origX
= LogicalToDeviceX(0);
628 int origY
= LogicalToDeviceY(0);
629 m_graphicContext
->Translate( origX
, origY
);
630 m_graphicContext
->SetBrush( m_brush
);
631 m_graphicContext
->Translate( -origX
, -origY
);
636 void wxGCDC::SetBackground( const wxBrush
&brush
)
638 if (m_backgroundBrush
== brush
)
641 m_backgroundBrush
= brush
;
642 if (!m_backgroundBrush
.Ok())
646 void wxGCDC::SetLogicalFunction( int function
)
648 if (m_logicalFunction
== function
)
651 m_logicalFunction
= function
;
652 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
654 CGContextRef cgContext
= ((wxCairoContext
*)(m_graphicContext
))->GetNativeContext();
655 if ( m_logicalFunction
== wxCOPY
)
656 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
657 else if ( m_logicalFunction
== wxINVERT
)
658 CGContextSetBlendMode( cgContext
, kCGBlendModeExclusion
);
660 CGContextSetBlendMode( cgContext
, kCGBlendModeNormal
);
665 bool wxGCDC::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
666 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
671 bool wxGCDC::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
673 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
677 void wxGCDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
679 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
681 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
683 if ( m_logicalFunction
!= wxCOPY
)
687 wxCoord xx1
= LogicalToDeviceX(x1
);
688 wxCoord yy1
= LogicalToDeviceY(y1
);
689 wxCoord xx2
= LogicalToDeviceX(x2
);
690 wxCoord yy2
= LogicalToDeviceY(y2
);
692 m_graphicContext
->StrokeLine(xx1
,yy1
,xx2
,yy2
);
694 CalcBoundingBox(x1
, y1
);
695 CalcBoundingBox(x2
, y2
);
698 void wxGCDC::DoCrossHair( wxCoord x
, wxCoord y
)
700 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
702 if ( m_logicalFunction
!= wxCOPY
)
709 wxCoord xx
= LogicalToDeviceX(x
);
710 wxCoord yy
= LogicalToDeviceY(y
);
711 wxCoord xw
= LogicalToDeviceX(w
);
712 wxCoord x0
= LogicalToDeviceX(0);
713 wxCoord y0
= LogicalToDeviceY(0);
714 wxCoord yh
= LogicalToDeviceY(h
);
716 m_graphicContext
->StrokeLine(x0
,yy
,xw
,yy
);
717 m_graphicContext
->StrokeLine(xx
,y0
,xx
,yh
);
719 CalcBoundingBox(x0
, y0
);
720 CalcBoundingBox(x0
+xw
, y0
+yh
);
723 void wxGCDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
724 wxCoord x2
, wxCoord y2
,
725 wxCoord xc
, wxCoord yc
)
727 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
729 if ( m_logicalFunction
!= wxCOPY
)
732 wxCoord xx1
= LogicalToDeviceX(x1
);
733 wxCoord yy1
= LogicalToDeviceY(y1
);
734 wxCoord xx2
= LogicalToDeviceX(x2
);
735 wxCoord yy2
= LogicalToDeviceY(y2
);
736 wxCoord xxc
= LogicalToDeviceX(xc
);
737 wxCoord yyc
= LogicalToDeviceY(yc
);
739 double dx
= xx1
- xxc
;
740 double dy
= yy1
- yyc
;
741 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
742 wxCoord rad
= (wxCoord
)radius
;
744 if (xx1
== xx2
&& yy1
== yy2
)
749 else if (radius
== 0.0)
755 sa
= (xx1
- xxc
== 0) ?
756 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
757 -atan2(double(yy1
- yyc
), double(xx1
- xxc
)) * RAD2DEG
;
758 ea
= (xx2
- xxc
== 0) ?
759 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
760 -atan2(double(yy2
- yyc
), double(xx2
- xxc
)) * RAD2DEG
;
763 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
765 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
766 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
767 path
->MoveToPoint( xxc
, yyc
);
768 path
->AddArc( xxc
, yyc
, rad
, DegToRad(sa
) , DegToRad(ea
), false );
769 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
770 path
->AddLineToPoint( xxc
, yyc
);
771 m_graphicContext
->DrawPath(path
);
775 void wxGCDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
776 double sa
, double ea
)
778 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
780 if ( m_logicalFunction
!= wxCOPY
)
783 wxCoord xx
= LogicalToDeviceX(x
);
784 wxCoord yy
= LogicalToDeviceY(y
);
785 wxCoord ww
= m_signX
* LogicalToDeviceXRel(w
);
786 wxCoord hh
= m_signY
* LogicalToDeviceYRel(h
);
788 // handle -ve width and/or height
800 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
802 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
803 m_graphicContext
->PushState();
804 m_graphicContext
->Translate(xx
+ww
/2,yy
+hh
/2);
805 wxDouble factor
= ((wxDouble
) ww
) / hh
;
806 m_graphicContext
->Scale( factor
, 1.0);
807 if ( fill
&& (sa
!=ea
) )
808 path
->MoveToPoint(0,0);
809 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
810 // get clockwise angles
811 path
->AddArc( 0, 0, hh
/2 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
812 if ( fill
&& (sa
!=ea
) )
813 path
->AddLineToPoint(0,0);
814 m_graphicContext
->DrawPath( path
);
815 m_graphicContext
->PopState();
819 void wxGCDC::DoDrawPoint( wxCoord x
, wxCoord y
)
821 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
823 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
826 void wxGCDC::DoDrawLines(int n
, wxPoint points
[],
827 wxCoord xoffset
, wxCoord yoffset
)
829 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
831 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
833 if ( m_logicalFunction
!= wxCOPY
)
837 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
838 for( int i
= 0; i
< n
; ++i
)
840 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
841 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
844 m_graphicContext
->StrokeLines( n
, pointsD
);
849 void wxGCDC::DoDrawSpline(wxList
*points
)
851 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
853 if ( m_logicalFunction
!= wxCOPY
)
856 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
858 wxList::compatibility_iterator node
= points
->GetFirst();
859 if (node
== wxList::compatibility_iterator())
863 wxPoint
*p
= (wxPoint
*)node
->GetData();
868 node
= node
->GetNext();
869 p
= (wxPoint
*)node
->GetData();
873 wxCoord cx1
= ( x1
+ x2
) / 2;
874 wxCoord cy1
= ( y1
+ y2
) / 2;
876 path
->MoveToPoint( LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) );
877 path
->AddLineToPoint( LogicalToDeviceX( cx1
) , LogicalToDeviceY( cy1
) );
880 while ((node
= node
->GetNext()) != NULL
)
883 while ((node
= node
->GetNext()))
887 p
= (wxPoint
*)node
->GetData();
892 wxCoord cx4
= (x1
+ x2
) / 2;
893 wxCoord cy4
= (y1
+ y2
) / 2;
895 path
->AddQuadCurveToPoint(
896 LogicalToDeviceX( x1
) , LogicalToDeviceY( y1
) ,
897 LogicalToDeviceX( cx4
) , LogicalToDeviceY( cy4
) );
903 path
->AddLineToPoint( LogicalToDeviceX( x2
) , LogicalToDeviceY( y2
) );
905 m_graphicContext
->StrokePath( path
);
908 #endif // wxUSE_SPLINES
910 void wxGCDC::DoDrawPolygon( int n
, wxPoint points
[],
911 wxCoord xoffset
, wxCoord yoffset
,
914 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
916 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
918 if ( m_logicalFunction
!= wxCOPY
)
921 bool closeIt
= false;
922 if (points
[n
-1] != points
[0])
925 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
926 for( int i
= 0; i
< n
; ++i
)
928 pointsD
[i
].m_x
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
929 pointsD
[i
].m_y
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
932 pointsD
[n
] = pointsD
[0];
934 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
938 void wxGCDC::DoDrawPolyPolygon(int n
,
946 wxGraphicsPath
* path
= m_graphicContext
->CreatePath();
949 for ( int j
= 0; j
< n
; ++j
)
951 wxPoint start
= points
[i
];
952 path
->MoveToPoint(LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
955 for ( int k
= 1; k
< l
; ++k
)
957 path
->AddLineToPoint( LogicalToDeviceX(points
[i
].x
+ xoffset
), LogicalToDeviceY(points
[i
].y
+ yoffset
));
961 if ( start
!= points
[i
-1])
962 path
->AddLineToPoint( LogicalToDeviceX(start
.x
+ xoffset
), LogicalToDeviceY(start
.y
+ yoffset
));
964 m_graphicContext
->DrawPath( path
, fillStyle
);
968 void wxGCDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
970 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
972 if ( m_logicalFunction
!= wxCOPY
)
975 wxCoord xx
= LogicalToDeviceX(x
);
976 wxCoord yy
= LogicalToDeviceY(y
);
977 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
978 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
980 // CMB: draw nothing if transformed w or h is 0
981 if (ww
== 0 || hh
== 0)
984 // CMB: handle -ve width and/or height
995 if ( m_graphicContext
->ShouldOffset() )
997 // if we are offsetting the entire rectangle is moved 0.5, so the
998 // border line gets off by 1
1002 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1005 void wxGCDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1006 wxCoord width
, wxCoord height
,
1009 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1011 if ( m_logicalFunction
!= wxCOPY
)
1015 radius
= - radius
* ((width
< height
) ? width
: height
);
1016 wxCoord xx
= LogicalToDeviceX(x
);
1017 wxCoord yy
= LogicalToDeviceY(y
);
1018 wxCoord ww
= m_signX
* LogicalToDeviceXRel(width
);
1019 wxCoord hh
= m_signY
* LogicalToDeviceYRel(height
);
1021 // CMB: draw nothing if transformed w or h is 0
1022 if (ww
== 0 || hh
== 0)
1025 // CMB: handle -ve width and/or height
1037 m_graphicContext
->DrawRoundedRectangle( xx
,yy
,ww
,hh
,radius
);
1040 void wxGCDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1042 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1044 if ( m_logicalFunction
!= wxCOPY
)
1047 wxDouble xx
= LogicalToDeviceX(x
);
1048 wxDouble yy
= LogicalToDeviceY(y
);
1049 wxDouble ww
= m_signX
* LogicalToDeviceXRel(width
);
1050 wxDouble hh
= m_signY
* LogicalToDeviceYRel(height
);
1052 // CMB: draw nothing if transformed w or h is 0
1053 if (ww
== 0 || hh
== 0)
1056 // CMB: handle -ve width and/or height
1068 m_graphicContext
->DrawEllipse(xx
,yy
,ww
,hh
);
1071 bool wxGCDC::CanDrawBitmap() const
1076 bool wxGCDC::DoBlit(
1077 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1078 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool WXUNUSED(useMask
),
1079 wxCoord xsrcMask
, wxCoord ysrcMask
)
1081 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
1082 wxCHECK_MSG( source
->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
1084 if ( logical_func
== wxNO_OP
)
1087 if (xsrcMask
== -1 && ysrcMask
== -1)
1093 wxCoord yysrc
= source
-> LogicalToDeviceY(ysrc
);
1094 wxCoord xxsrc
= source
-> LogicalToDeviceX(xsrc
);
1095 wxCoord wwsrc
= source
-> LogicalToDeviceXRel(width
);
1096 wxCoord hhsrc
= source
-> LogicalToDeviceYRel(height
);
1098 wxCoord yydest
= LogicalToDeviceY(ydest
);
1099 wxCoord xxdest
= LogicalToDeviceX(xdest
);
1100 wxCoord wwdest
= LogicalToDeviceXRel(width
);
1101 wxCoord hhdest
= LogicalToDeviceYRel(height
);
1103 wxMemoryDC
* memdc
= wxDynamicCast(source
,wxMemoryDC
);
1104 if ( memdc
&& logical_func
== wxCOPY
)
1106 wxBitmap blit
= memdc
->GetSelectedBitmap();
1108 wxASSERT_MSG( blit
.Ok() , wxT("Invalid bitmap for blitting") );
1110 wxCoord bmpwidth
= blit
.GetWidth();
1111 wxCoord bmpheight
= blit
.GetHeight();
1113 if ( xxsrc
!= 0 || yysrc
!= 0 || bmpwidth
!= wwsrc
|| bmpheight
!= hhsrc
)
1115 wwsrc
= wxMin( wwsrc
, bmpwidth
- xxsrc
);
1116 hhsrc
= wxMin( hhsrc
, bmpheight
- yysrc
);
1117 if ( wwsrc
> 0 && hhsrc
> 0 )
1119 if ( xxsrc
>= 0 && yysrc
>= 0 )
1121 wxRect
subrect( xxsrc
, yysrc
, wwsrc
, hhsrc
);
1122 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
1123 blit
= blit
.GetSubBitmap( subrect
);
1127 // in this case we'd probably have to adjust the different coordinates, but
1128 // we have to find out proper contract first
1129 blit
= wxNullBitmap
;
1134 blit
= wxNullBitmap
;
1140 m_graphicContext
->DrawBitmap( blit
, xxdest
, yydest
, wwdest
, hhdest
);
1145 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts, and only with wxCOPY logical operation.") );
1152 void wxGCDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1155 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1157 if ( str
.length() == 0 )
1159 if ( m_logicalFunction
!= wxCOPY
)
1162 int drawX
= LogicalToDeviceX(x
);
1163 int drawY
= LogicalToDeviceY(y
);
1165 m_graphicContext
->DrawText( str
, drawX
,drawY
, DegToRad(angle
));
1168 void wxGCDC::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
1170 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1172 if ( str
.length() == 0 )
1174 if ( m_logicalFunction
!= wxCOPY
)
1177 int drawX
= LogicalToDeviceX(x
);
1178 int drawY
= LogicalToDeviceY(y
);
1180 m_graphicContext
->DrawText( str
, drawX
,drawY
);
1183 bool wxGCDC::CanGetTextExtent() const
1185 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1190 void wxGCDC::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1191 wxCoord
*descent
, wxCoord
*externalLeading
,
1192 wxFont
*theFont
) const
1194 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1198 m_graphicContext
->SetFont( *theFont
);
1201 wxDouble h
, d
, e
, w
;
1203 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1206 *height
= DeviceToLogicalYRel((wxCoord
)h
);
1208 *descent
= DeviceToLogicalYRel((wxCoord
)d
);
1209 if ( externalLeading
)
1210 *externalLeading
= DeviceToLogicalYRel((wxCoord
)e
);
1212 *width
= DeviceToLogicalXRel((wxCoord
)w
);
1216 m_graphicContext
->SetFont( m_font
);
1220 bool wxGCDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1222 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1224 widths
.Add(0,text
.Length());
1225 if ( text
.IsEmpty() )
1228 wxArrayDouble widthsD
;
1230 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1231 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1232 widths
[i
] = DeviceToLogicalXRel((wxCoord
)(widthsD
[i
] + 0.5));
1237 wxCoord
wxGCDC::GetCharWidth(void) const
1240 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1245 wxCoord
wxGCDC::GetCharHeight(void) const
1248 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1253 void wxGCDC::Clear(void)
1255 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1256 // TODO better implementation / incorporate size info into wxGCDC or context
1257 m_graphicContext
->SetBrush( m_backgroundBrush
);
1258 wxPen p
= *wxTRANSPARENT_PEN
;
1259 m_graphicContext
->SetPen( p
);
1260 DoDrawRectangle( 0, 0, 32000 , 32000 );
1261 m_graphicContext
->SetPen( m_pen
);
1262 m_graphicContext
->SetBrush( m_brush
);
1265 void wxGCDC::DoGetSize(int *width
, int *height
) const
1271 void wxGCDC::DoGradientFillLinear(const wxRect
& rect
,
1272 const wxColour
& initialColour
,
1273 const wxColour
& destColour
,
1274 wxDirection nDirection
)
1281 start
= rect
.GetRightBottom();
1283 end
= rect
.GetLeftBottom();
1286 start
= rect
.GetLeftBottom();
1287 end
= rect
.GetRightBottom();
1291 start
= rect
.GetLeftBottom();
1293 end
= rect
.GetLeftTop();
1296 start
= rect
.GetLeftTop();
1297 end
= rect
.GetLeftBottom();
1304 m_graphicContext
->SetLinearGradientBrush(
1305 LogicalToDeviceX(start
.x
),LogicalToDeviceY(start
.y
),
1306 LogicalToDeviceX(end
.x
),LogicalToDeviceY(end
.y
), initialColour
, destColour
);
1308 wxDouble xx
= LogicalToDeviceX(rect
.x
);
1309 wxDouble yy
= LogicalToDeviceY(rect
.y
);
1310 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1311 wxDouble hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1313 if (ww
== 0 || hh
== 0)
1327 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1328 m_graphicContext
->DrawRectangle(xx
,yy
,ww
,hh
);
1329 m_graphicContext
->SetPen(m_pen
);
1332 void wxGCDC::DoGradientFillConcentric(const wxRect
& rect
,
1333 const wxColour
& initialColour
,
1334 const wxColour
& destColour
,
1335 const wxPoint
& circleCenter
)
1338 wxInt32 cx
= rect
.GetWidth() / 2;
1339 wxInt32 cy
= rect
.GetHeight() / 2;
1346 wxDouble xx
= LogicalToDeviceX(rect
.x
);
1347 wxDouble yy
= LogicalToDeviceY(rect
.y
);
1348 wxDouble ww
= m_signX
* LogicalToDeviceXRel(rect
.width
);
1349 wxDouble hh
= m_signY
* LogicalToDeviceYRel(rect
.height
);
1351 if (ww
== 0 || hh
== 0)
1365 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1366 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1367 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1369 m_graphicContext
->SetRadialGradientBrush(
1370 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1371 xx
+LogicalToDeviceX(circleCenter
.x
),yy
+LogicalToDeviceY(circleCenter
.y
),
1372 LogicalToDeviceXRel(nRadius
),
1373 initialColour
,destColour
);
1375 m_graphicContext
->DrawRectangle( xx
,yy
,ww
,hh
);
1376 m_graphicContext
->SetPen(m_pen
);
1379 void wxGCDC::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1380 wxCoord width
, wxCoord height
)
1382 wxDCBase::DoDrawCheckMark(x
,y
,width
,height
);
1385 #endif // wxUSE_GRAPHICS_CONTEXT