1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dcgraph.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/dcgraph.h"
25 #include "wx/dcclient.h"
26 #include "wx/dcmemory.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 static const double RAD2DEG
= 180.0 / M_PI
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 static inline double DegToRad(double deg
)
41 return (deg
* M_PI
) / 180.0;
44 static wxCompositionMode
TranslateRasterOp(wxRasterOperationMode function
)
49 // since we are supporting alpha, _OVER is closer to the intention than _SOURCE
50 // since the latter would overwrite even when alpha is not set to opaque
51 return wxCOMPOSITION_OVER
;
53 case wxOR
: // src OR dst
54 return wxCOMPOSITION_ADD
;
57 return wxCOMPOSITION_DEST
; // ignore the source
60 return wxCOMPOSITION_CLEAR
;// clear dst
62 case wxXOR
: // src XOR dst
63 return wxCOMPOSITION_XOR
;
65 case wxAND
: // src AND dst
66 case wxAND_INVERT
: // (NOT src) AND dst
67 case wxAND_REVERSE
:// src AND (NOT dst)
68 case wxEQUIV
: // (NOT src) XOR dst
69 case wxINVERT
: // NOT dst
70 case wxNAND
: // (NOT src) OR (NOT dst)
71 case wxNOR
: // (NOT src) AND (NOT dst)
72 case wxOR_INVERT
: // (NOT src) OR dst
73 case wxOR_REVERSE
: // src OR (NOT dst)
75 case wxSRC_INVERT
: // NOT src
79 return wxCOMPOSITION_INVALID
;
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
88 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
89 wxDC( new wxGCDCImpl( this, dc
) )
93 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
94 wxDC( new wxGCDCImpl( this, dc
) )
98 #if wxUSE_PRINTING_ARCHITECTURE
99 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
100 wxDC( new wxGCDCImpl( this, dc
) )
105 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
106 wxGCDC::wxGCDC(const wxEnhMetaFileDC
& dc
)
107 : wxDC(new wxGCDCImpl(this, dc
))
112 wxGCDC::wxGCDC(wxGraphicsContext
* context
) :
113 wxDC( new wxGCDCImpl( this ) )
115 SetGraphicsContext(context
);
119 wxDC( new wxGCDCImpl( this ) )
127 wxGraphicsContext
* wxGCDC::GetGraphicsContext() const
129 if (!m_pimpl
) return NULL
;
130 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
131 return gc_impl
->GetGraphicsContext();
134 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
136 if (!m_pimpl
) return;
137 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
138 gc_impl
->SetGraphicsContext( ctx
);
141 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
143 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
146 Init(wxGraphicsContext::Create());
149 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
151 delete m_graphicContext
;
152 m_graphicContext
= ctx
;
153 if ( m_graphicContext
)
155 m_matrixOriginal
= m_graphicContext
->GetTransform();
157 // apply the stored transformations to the passed in context
158 ComputeScaleAndOrigin();
159 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
160 m_graphicContext
->SetPen( m_pen
);
161 m_graphicContext
->SetBrush( m_brush
);
165 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
168 Init(wxGraphicsContext::Create(dc
));
169 m_window
= dc
.GetWindow();
172 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
175 Init(wxGraphicsContext::Create(dc
));
178 #if wxUSE_PRINTING_ARCHITECTURE
179 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
182 Init(wxGraphicsContext::Create(dc
));
186 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
187 wxGCDCImpl::wxGCDCImpl(wxDC
*owner
, const wxEnhMetaFileDC
& dc
)
190 Init(wxGraphicsContext::Create(dc
));
194 wxGCDCImpl::wxGCDCImpl(wxDC
* owner
, int)
197 // derived class will set a context
201 void wxGCDCImpl::Init(wxGraphicsContext
* ctx
)
205 m_mm_to_pix_x
= mm2pt
;
206 m_mm_to_pix_y
= mm2pt
;
208 m_pen
= *wxBLACK_PEN
;
209 m_font
= *wxNORMAL_FONT
;
210 m_brush
= *wxWHITE_BRUSH
;
212 m_graphicContext
= NULL
;
214 SetGraphicsContext(ctx
);
216 m_logicalFunctionSupported
= true;
219 wxGCDCImpl::~wxGCDCImpl()
221 delete m_graphicContext
;
224 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
227 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
228 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
230 int w
= bmp
.GetWidth();
231 int h
= bmp
.GetHeight();
232 if ( bmp
.GetDepth() == 1 )
234 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
235 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
236 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
237 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
238 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
239 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
240 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
242 else // not a monochrome bitmap, handle it normally
244 // make a copy in case we need to remove its mask, if we don't modify
245 // it the copy is cheap as bitmaps are reference-counted
246 wxBitmap
bmpCopy(bmp
);
247 if ( !useMask
&& bmp
.GetMask() )
248 bmpCopy
.SetMask(NULL
);
250 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
254 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
256 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
257 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
259 wxCoord w
= icon
.GetWidth();
260 wxCoord h
= icon
.GetHeight();
262 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
265 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
270 void wxGCDCImpl::EndDoc()
274 void wxGCDCImpl::StartPage()
278 void wxGCDCImpl::EndPage()
282 void wxGCDCImpl::Flush()
284 m_graphicContext
->Flush();
287 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
289 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
291 m_graphicContext
->Clip( x
, y
, w
, h
);
293 wxDCImpl::DoSetClippingRegion(x
, y
, w
, h
);
296 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
298 // region is in device coordinates
299 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
303 //DestroyClippingRegion();
307 wxRegion
logRegion( region
);
310 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
311 logRegion
.GetBox( x
, y
, w
, h
);
313 m_graphicContext
->Clip( logRegion
);
316 m_clipX1
= wxMax( m_clipX1
, x
);
317 m_clipY1
= wxMax( m_clipY1
, y
);
318 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
319 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
332 void wxGCDCImpl::DestroyClippingRegion()
334 m_graphicContext
->ResetClip();
335 // currently the clip eg of a window extends to the area between the scrollbars
336 // so we must explicitly make sure it only covers the area we want it to draw
338 GetOwner()->GetSize( &width
, &height
) ;
339 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
341 m_graphicContext
->SetPen( m_pen
);
342 m_graphicContext
->SetBrush( m_brush
);
347 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
351 GetOwner()->GetSize( &w
, &h
);
353 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
355 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
358 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
360 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
362 // don't set m_textForegroundColour to an invalid colour as we'd crash
363 // later then (we use m_textForegroundColour.GetColor() without checking
367 m_textForegroundColour
= col
;
368 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
372 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
374 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
376 m_textBackgroundColour
= col
;
379 wxSize
wxGCDCImpl::GetPPI() const
381 return wxSize(72, 72);
384 int wxGCDCImpl::GetDepth() const
389 void wxGCDCImpl::ComputeScaleAndOrigin()
391 wxDCImpl::ComputeScaleAndOrigin();
393 if ( m_graphicContext
)
395 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
397 // the logical origin sets the origin to have new coordinates
398 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
399 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
401 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
403 m_graphicContext
->SetTransform( m_matrixOriginal
);
404 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
408 void* wxGCDCImpl::GetHandle() const
411 wxGraphicsContext
* gc
= GetGraphicsContext();
413 cgctx
= gc
->GetNativeContext();
418 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
423 void wxGCDCImpl::SetBackgroundMode( int mode
)
425 m_backgroundMode
= mode
;
428 void wxGCDCImpl::SetFont( const wxFont
&font
)
431 if ( m_graphicContext
)
433 m_graphicContext
->SetFont(font
, m_textForegroundColour
);
437 void wxGCDCImpl::SetPen( const wxPen
&pen
)
440 if ( m_graphicContext
)
442 m_graphicContext
->SetPen( m_pen
);
446 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
449 if ( m_graphicContext
)
451 m_graphicContext
->SetBrush( m_brush
);
455 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
457 m_backgroundBrush
= brush
;
458 if (!m_backgroundBrush
.IsOk())
462 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
464 m_logicalFunction
= function
;
466 wxCompositionMode mode
= TranslateRasterOp( function
);
467 m_logicalFunctionSupported
= mode
!= wxCOMPOSITION_INVALID
;
468 if (m_logicalFunctionSupported
)
469 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
471 if ( function
== wxXOR
)
472 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
474 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
477 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
478 const wxColour
& WXUNUSED(col
),
479 wxFloodFillStyle
WXUNUSED(style
))
484 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
486 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
490 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
492 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
494 if ( !m_logicalFunctionSupported
)
497 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
499 CalcBoundingBox(x1
, y1
);
500 CalcBoundingBox(x2
, y2
);
503 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
505 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
507 if ( !m_logicalFunctionSupported
)
512 GetOwner()->GetSize( &w
, &h
);
514 m_graphicContext
->StrokeLine(0,y
,w
,y
);
515 m_graphicContext
->StrokeLine(x
,0,x
,h
);
517 CalcBoundingBox(0, 0);
518 CalcBoundingBox(0+w
, 0+h
);
521 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
522 wxCoord x2
, wxCoord y2
,
523 wxCoord xc
, wxCoord yc
)
525 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
527 if ( !m_logicalFunctionSupported
)
532 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
533 wxCoord rad
= (wxCoord
)radius
;
535 if (x1
== x2
&& y1
== y2
)
540 else if (radius
== 0.0)
546 sa
= (x1
- xc
== 0) ?
547 (y1
- yc
< 0) ? 90.0 : -90.0 :
548 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
549 ea
= (x2
- xc
== 0) ?
550 (y2
- yc
< 0) ? 90.0 : -90.0 :
551 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
554 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
556 wxGraphicsPath path
= m_graphicContext
->CreatePath();
557 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
558 path
.MoveToPoint( xc
, yc
);
559 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
560 // get clockwise angles
561 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
562 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
563 path
.AddLineToPoint( xc
, yc
);
564 m_graphicContext
->DrawPath(path
);
567 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
568 double sa
, double ea
)
570 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
572 if ( !m_logicalFunctionSupported
)
575 m_graphicContext
->PushState();
576 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
577 wxDouble factor
= ((wxDouble
) w
) / h
;
578 m_graphicContext
->Scale( factor
, 1.0);
580 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
581 // get clockwise angles
582 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
584 wxGraphicsPath path
= m_graphicContext
->CreatePath();
585 path
.MoveToPoint( 0, 0 );
586 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
587 path
.AddLineToPoint( 0, 0 );
588 m_graphicContext
->FillPath( path
);
590 path
= m_graphicContext
->CreatePath();
591 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
592 m_graphicContext
->StrokePath( path
);
596 wxGraphicsPath path
= m_graphicContext
->CreatePath();
597 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
598 m_graphicContext
->DrawPath( path
);
601 m_graphicContext
->PopState();
604 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
606 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
608 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
611 void wxGCDCImpl::DoDrawLines(int n
, const wxPoint points
[],
612 wxCoord xoffset
, wxCoord yoffset
)
614 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
616 if ( !m_logicalFunctionSupported
)
619 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
620 for( int i
= 0; i
< n
; ++i
)
622 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
623 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
626 m_graphicContext
->StrokeLines( n
, pointsD
);
631 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
633 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
635 if ( !m_logicalFunctionSupported
)
638 wxGraphicsPath path
= m_graphicContext
->CreatePath();
640 wxPointList::compatibility_iterator node
= points
->GetFirst();
645 const wxPoint
*p
= node
->GetData();
650 node
= node
->GetNext();
655 wxCoord cx1
= ( x1
+ x2
) / 2;
656 wxCoord cy1
= ( y1
+ y2
) / 2;
658 path
.MoveToPoint( x1
, y1
);
659 path
.AddLineToPoint( cx1
, cy1
);
660 #if !wxUSE_STD_CONTAINERS
662 while ((node
= node
->GetNext()) != NULL
)
665 while ((node
= node
->GetNext()))
666 #endif // !wxUSE_STD_CONTAINERS
674 wxCoord cx4
= (x1
+ x2
) / 2;
675 wxCoord cy4
= (y1
+ y2
) / 2;
677 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
683 path
.AddLineToPoint( x2
, y2
);
685 m_graphicContext
->StrokePath( path
);
687 #endif // wxUSE_SPLINES
689 void wxGCDCImpl::DoDrawPolygon( int n
, const wxPoint points
[],
690 wxCoord xoffset
, wxCoord yoffset
,
691 wxPolygonFillMode fillStyle
)
693 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
695 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
697 if ( !m_logicalFunctionSupported
)
700 bool closeIt
= false;
701 if (points
[n
-1] != points
[0])
704 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
705 for( int i
= 0; i
< n
; ++i
)
707 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
708 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
711 pointsD
[n
] = pointsD
[0];
713 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
717 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
719 const wxPoint points
[],
722 wxPolygonFillMode fillStyle
)
725 wxGraphicsPath path
= m_graphicContext
->CreatePath();
728 for ( int j
= 0; j
< n
; ++j
)
730 wxPoint start
= points
[i
];
731 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
734 for ( int k
= 1; k
< l
; ++k
)
736 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
740 if ( start
!= points
[i
-1])
741 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
743 m_graphicContext
->DrawPath( path
, fillStyle
);
746 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
748 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
750 if ( !m_logicalFunctionSupported
)
753 // CMB: draw nothing if transformed w or h is 0
754 if (w
== 0 || h
== 0)
757 if ( m_graphicContext
->ShouldOffset() )
759 // if we are offsetting the entire rectangle is moved 0.5, so the
760 // border line gets off by 1
764 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
767 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
768 wxCoord w
, wxCoord h
,
771 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
773 if ( !m_logicalFunctionSupported
)
777 radius
= - radius
* ((w
< h
) ? w
: h
);
779 // CMB: draw nothing if transformed w or h is 0
780 if (w
== 0 || h
== 0)
783 if ( m_graphicContext
->ShouldOffset() )
785 // if we are offsetting the entire rectangle is moved 0.5, so the
786 // border line gets off by 1
790 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
793 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
795 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
797 if ( !m_logicalFunctionSupported
)
800 if ( m_graphicContext
->ShouldOffset() )
802 // if we are offsetting the entire rectangle is moved 0.5, so the
803 // border line gets off by 1
807 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
810 bool wxGCDCImpl::CanDrawBitmap() const
815 bool wxGCDCImpl::DoBlit(
816 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
817 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
818 wxRasterOperationMode logical_func
, bool useMask
,
819 wxCoord xsrcMask
, wxCoord ysrcMask
)
821 return DoStretchBlit( xdest
, ydest
, width
, height
,
822 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
826 bool wxGCDCImpl::DoStretchBlit(
827 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
828 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
829 wxRasterOperationMode logical_func
, bool useMask
,
830 wxCoord xsrcMask
, wxCoord ysrcMask
)
832 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
833 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
835 if ( logical_func
== wxNO_OP
)
838 wxCompositionMode mode
= TranslateRasterOp(logical_func
);
839 if ( mode
== wxCOMPOSITION_INVALID
)
841 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
845 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
846 source
->LogicalToDeviceY(ysrc
),
847 source
->LogicalToDeviceXRel(srcWidth
),
848 source
->LogicalToDeviceYRel(srcHeight
));
849 const wxRect subrectOrig
= subrect
;
850 // clip the subrect down to the size of the source DC
852 source
->GetSize(&clip
.width
, &clip
.height
);
853 subrect
.Intersect(clip
);
854 if (subrect
.width
== 0)
859 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
860 if (m_graphicContext
->SetCompositionMode(mode
))
862 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
863 if (mode
== wxCOMPOSITION_XOR
)
865 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
868 if (xsrcMask
== -1 && ysrcMask
== -1)
874 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
878 if ( !useMask
&& blit
.GetMask() )
884 double h
= dstHeight
;
885 // adjust dest rect if source rect is clipped
886 if (subrect
.width
!= subrectOrig
.width
|| subrect
.height
!= subrectOrig
.height
)
888 x
+= (subrect
.x
- subrectOrig
.x
) / double(subrectOrig
.width
) * dstWidth
;
889 y
+= (subrect
.y
- subrectOrig
.y
) / double(subrectOrig
.height
) * dstHeight
;
890 w
*= double(subrect
.width
) / subrectOrig
.width
;
891 h
*= double(subrect
.height
) / subrectOrig
.height
;
893 m_graphicContext
->DrawBitmap(blit
, x
, y
, w
, h
);
897 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
901 if (mode
== wxCOMPOSITION_XOR
)
903 m_graphicContext
->SetAntialiasMode(formerAa
);
907 m_graphicContext
->SetCompositionMode(formerMode
);
912 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
915 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
919 if ( !m_logicalFunctionSupported
)
922 if ( m_backgroundMode
== wxTRANSPARENT
)
923 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
925 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
928 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
930 // For compatibility with other ports (notably wxGTK) and because it's
931 // genuinely useful, we allow passing multiline strings to DrawText().
932 // However there is no native OSX function to draw them directly so we
933 // instead reuse the generic DrawLabel() method to render them. Of course,
934 // DrawLabel() itself will call back to us but with single line strings
935 // only so there won't be any infinite recursion here.
936 if ( str
.find('\n') != wxString::npos
)
938 GetOwner()->DrawLabel(str
, wxRect(x
, y
, 0, 0));
942 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
947 if ( !m_logicalFunctionSupported
)
950 if ( m_backgroundMode
== wxTRANSPARENT
)
951 m_graphicContext
->DrawText( str
, x
,y
);
953 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
956 bool wxGCDCImpl::CanGetTextExtent() const
958 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
963 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
964 wxCoord
*descent
, wxCoord
*externalLeading
,
965 const wxFont
*theFont
) const
967 wxCHECK_RET( m_graphicContext
, wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
971 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
974 wxDouble h
, d
, e
, w
;
976 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
979 *height
= (wxCoord
)(h
+0.5);
981 *descent
= (wxCoord
)(d
+0.5);
982 if ( externalLeading
)
983 *externalLeading
= (wxCoord
)(e
+0.5);
985 *width
= (wxCoord
)(w
+0.5);
989 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
993 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
995 wxCHECK_MSG( m_graphicContext
, false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
997 widths
.Add(0,text
.Length());
998 if ( text
.IsEmpty() )
1001 wxArrayDouble widthsD
;
1003 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1004 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1005 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1010 wxCoord
wxGCDCImpl::GetCharWidth(void) const
1013 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1018 wxCoord
wxGCDCImpl::GetCharHeight(void) const
1021 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1026 void wxGCDCImpl::Clear(void)
1028 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1029 // TODO better implementation / incorporate size info into wxGCDC or context
1030 m_graphicContext
->SetBrush( m_backgroundBrush
);
1031 wxPen p
= *wxTRANSPARENT_PEN
;
1032 m_graphicContext
->SetPen( p
);
1033 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
1034 m_graphicContext
->SetCompositionMode(wxCOMPOSITION_SOURCE
);
1035 // maximum positive coordinate Cairo can handle is 2^23 - 1
1037 DeviceToLogicalX(0), DeviceToLogicalY(0),
1038 DeviceToLogicalXRel(0x007fffff), DeviceToLogicalYRel(0x007fffff));
1039 m_graphicContext
->SetCompositionMode(formerMode
);
1040 m_graphicContext
->SetPen( m_pen
);
1041 m_graphicContext
->SetBrush( m_brush
);
1044 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
1046 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
1048 m_graphicContext
->GetSize( &w
, &h
);
1050 *height
= (int) (h
+0.5);
1052 *width
= (int) (w
+0.5);
1055 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1056 const wxColour
& initialColour
,
1057 const wxColour
& destColour
,
1058 wxDirection nDirection
)
1065 start
= rect
.GetRightBottom();
1067 end
= rect
.GetLeftBottom();
1070 start
= rect
.GetLeftBottom();
1071 end
= rect
.GetRightBottom();
1075 start
= rect
.GetLeftBottom();
1077 end
= rect
.GetLeftTop();
1080 start
= rect
.GetLeftTop();
1081 end
= rect
.GetLeftBottom();
1088 if (rect
.width
== 0 || rect
.height
== 0)
1091 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1092 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1093 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1094 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1095 m_graphicContext
->SetPen(m_pen
);
1096 m_graphicContext
->SetBrush(m_brush
);
1099 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1100 const wxColour
& initialColour
,
1101 const wxColour
& destColour
,
1102 const wxPoint
& circleCenter
)
1105 wxInt32 cx
= rect
.GetWidth() / 2;
1106 wxInt32 cy
= rect
.GetHeight() / 2;
1113 // make sure the background is filled (todo move into specific platform implementation ?)
1114 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1115 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1116 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1118 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1119 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1120 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1121 nRadius
,initialColour
,destColour
));
1123 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1124 m_graphicContext
->SetPen(m_pen
);
1125 m_graphicContext
->SetBrush(m_brush
);
1128 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1129 wxCoord width
, wxCoord height
)
1131 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1135 wxRect
wxGCDCImpl::MSWApplyGDIPlusTransform(const wxRect
& r
) const
1137 wxGraphicsContext
* const gc
= GetGraphicsContext();
1138 wxCHECK_MSG( gc
, r
, wxT("Invalid wxGCDC") );
1142 gc
->GetTransform().TransformPoint(&x
, &y
);
1151 #endif // wxUSE_GRAPHICS_CONTEXT