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/graphics.h"
22 #include "wx/dcgraph.h"
26 #include "wx/bitmap.h"
27 #include "wx/dcmemory.h"
28 #include "wx/region.h"
31 #include "wx/dcclient.h"
33 #ifdef __WXOSX_OR_COCOA__
34 #ifdef __WXOSX_IPHONE__
35 #include <CoreGraphics/CoreGraphics.h>
37 #include <ApplicationServices/ApplicationServices.h>
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static const double RAD2DEG
= 180.0 / M_PI
;
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 static inline double DegToRad(double deg
)
53 return (deg
* M_PI
) / 180.0;
56 static wxCompositionMode
TranslateRasterOp(wxRasterOperationMode function
)
61 // since we are supporting alpha, _OVER is closer to the intention than _SOURCE
62 // since the latter would overwrite even when alpha is not set to opaque
63 return wxCOMPOSITION_OVER
;
65 case wxOR
: // src OR dst
66 return wxCOMPOSITION_ADD
;
69 return wxCOMPOSITION_DEST
; // ignore the source
72 return wxCOMPOSITION_CLEAR
;// clear dst
74 case wxXOR
: // src XOR dst
75 return wxCOMPOSITION_XOR
;
77 case wxAND
: // src AND dst
78 case wxAND_INVERT
: // (NOT src) AND dst
79 case wxAND_REVERSE
:// src AND (NOT dst)
80 case wxEQUIV
: // (NOT src) XOR dst
81 case wxINVERT
: // NOT dst
82 case wxNAND
: // (NOT src) OR (NOT dst)
83 case wxNOR
: // (NOT src) AND (NOT dst)
84 case wxOR_INVERT
: // (NOT src) OR dst
85 case wxOR_REVERSE
: // src OR (NOT dst)
87 case wxSRC_INVERT
: // NOT src
91 return wxCOMPOSITION_INVALID
;
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
98 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
100 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
101 wxDC( new wxGCDCImpl( this, dc
) )
105 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
106 wxDC( new wxGCDCImpl( this, dc
) )
110 #if wxUSE_PRINTING_ARCHITECTURE
111 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
112 wxDC( new wxGCDCImpl( this, dc
) )
118 wxDC( new wxGCDCImpl( this ) )
126 wxGraphicsContext
* wxGCDC::GetGraphicsContext() const
128 if (!m_pimpl
) return NULL
;
129 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
130 return gc_impl
->GetGraphicsContext();
133 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
135 if (!m_pimpl
) return;
136 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
137 gc_impl
->SetGraphicsContext( ctx
);
140 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
142 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
148 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
150 delete m_graphicContext
;
151 m_graphicContext
= ctx
;
152 if ( m_graphicContext
)
154 m_matrixOriginal
= m_graphicContext
->GetTransform();
156 // apply the stored transformations to the passed in context
157 ComputeScaleAndOrigin();
158 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
159 m_graphicContext
->SetPen( m_pen
);
160 m_graphicContext
->SetBrush( m_brush
);
164 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
168 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
169 m_window
= dc
.GetWindow();
172 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
176 wxGraphicsContext
* context
;
178 wxGraphicsRenderer
* renderer
= wxGraphicsRenderer::GetCairoRenderer();
179 context
= renderer
->CreateContext(dc
);
181 context
= wxGraphicsContext::Create(dc
);
184 SetGraphicsContext( context
);
187 #if wxUSE_PRINTING_ARCHITECTURE
188 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
192 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
196 void wxGCDCImpl::Init()
200 m_mm_to_pix_x
= mm2pt
;
201 m_mm_to_pix_y
= mm2pt
;
203 m_pen
= *wxBLACK_PEN
;
204 m_font
= *wxNORMAL_FONT
;
205 m_brush
= *wxWHITE_BRUSH
;
207 m_graphicContext
= wxGraphicsContext::Create();
208 m_logicalFunctionSupported
= true;
212 wxGCDCImpl::~wxGCDCImpl()
214 delete m_graphicContext
;
217 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
220 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
221 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
223 int w
= bmp
.GetWidth();
224 int h
= bmp
.GetHeight();
225 if ( bmp
.GetDepth() == 1 )
227 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
228 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
229 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
230 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
231 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
232 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
233 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
235 else // not a monochrome bitmap, handle it normally
237 // make a copy in case we need to remove its mask, if we don't modify
238 // it the copy is cheap as bitmaps are reference-counted
239 wxBitmap
bmpCopy(bmp
);
240 if ( !useMask
&& bmp
.GetMask() )
241 bmpCopy
.SetMask(NULL
);
243 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
247 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
249 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
250 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
252 wxCoord w
= icon
.GetWidth();
253 wxCoord h
= icon
.GetHeight();
255 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
258 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
263 void wxGCDCImpl::EndDoc()
267 void wxGCDCImpl::StartPage()
271 void wxGCDCImpl::EndPage()
275 void wxGCDCImpl::Flush()
277 m_graphicContext
->Flush();
280 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
282 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
284 m_graphicContext
->Clip( x
, y
, w
, h
);
287 m_clipX1
= wxMax( m_clipX1
, x
);
288 m_clipY1
= wxMax( m_clipY1
, y
);
289 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
290 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
303 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
305 // region is in device coordinates
306 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
310 //DestroyClippingRegion();
314 wxRegion
logRegion( region
);
317 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
318 logRegion
.GetBox( x
, y
, w
, h
);
320 m_graphicContext
->Clip( logRegion
);
323 m_clipX1
= wxMax( m_clipX1
, x
);
324 m_clipY1
= wxMax( m_clipY1
, y
);
325 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
326 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
339 void wxGCDCImpl::DestroyClippingRegion()
341 m_graphicContext
->ResetClip();
342 // currently the clip eg of a window extends to the area between the scrollbars
343 // so we must explicitly make sure it only covers the area we want it to draw
345 GetOwner()->GetSize( &width
, &height
) ;
346 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
348 m_graphicContext
->SetPen( m_pen
);
349 m_graphicContext
->SetBrush( m_brush
);
354 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
358 GetOwner()->GetSize( &w
, &h
);
360 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
362 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
365 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
367 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
369 if ( col
!= m_textForegroundColour
)
371 m_textForegroundColour
= col
;
372 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
376 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
378 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
380 m_textBackgroundColour
= col
;
383 void wxGCDCImpl::SetMapMode( wxMappingMode mode
)
388 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
392 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
396 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
400 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
405 SetLogicalScale( 1.0, 1.0 );
409 ComputeScaleAndOrigin();
412 wxSize
wxGCDCImpl::GetPPI() const
414 return wxSize(72, 72);
417 int wxGCDCImpl::GetDepth() const
422 void wxGCDCImpl::ComputeScaleAndOrigin()
424 wxDCImpl::ComputeScaleAndOrigin();
426 if ( m_graphicContext
)
428 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
430 // the logical origin sets the origin to have new coordinates
431 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
432 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
434 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
436 m_graphicContext
->SetTransform( m_matrixOriginal
);
437 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
441 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
446 void wxGCDCImpl::SetBackgroundMode( int mode
)
448 m_backgroundMode
= mode
;
451 void wxGCDCImpl::SetFont( const wxFont
&font
)
454 if ( m_graphicContext
)
458 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
459 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
463 void wxGCDCImpl::SetPen( const wxPen
&pen
)
469 if ( m_graphicContext
)
471 m_graphicContext
->SetPen( m_pen
);
475 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
477 if (m_brush
== brush
)
481 if ( m_graphicContext
)
483 m_graphicContext
->SetBrush( m_brush
);
487 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
489 if (m_backgroundBrush
== brush
)
492 m_backgroundBrush
= brush
;
493 if (!m_backgroundBrush
.IsOk())
497 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
499 if (m_logicalFunction
== function
)
502 m_logicalFunction
= function
;
504 wxCompositionMode mode
= TranslateRasterOp( function
);
505 m_logicalFunctionSupported
= mode
!= wxCOMPOSITION_INVALID
;
506 if (m_logicalFunctionSupported
)
507 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
509 if ( function
== wxXOR
)
510 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
512 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
515 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
516 const wxColour
& WXUNUSED(col
),
517 wxFloodFillStyle
WXUNUSED(style
))
522 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
524 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
528 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
530 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
532 if ( !m_logicalFunctionSupported
)
535 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
537 CalcBoundingBox(x1
, y1
);
538 CalcBoundingBox(x2
, y2
);
541 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
543 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
545 if ( !m_logicalFunctionSupported
)
550 GetOwner()->GetSize( &w
, &h
);
552 m_graphicContext
->StrokeLine(0,y
,w
,y
);
553 m_graphicContext
->StrokeLine(x
,0,x
,h
);
555 CalcBoundingBox(0, 0);
556 CalcBoundingBox(0+w
, 0+h
);
559 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
560 wxCoord x2
, wxCoord y2
,
561 wxCoord xc
, wxCoord yc
)
563 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
565 if ( !m_logicalFunctionSupported
)
570 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
571 wxCoord rad
= (wxCoord
)radius
;
573 if (x1
== x2
&& y1
== y2
)
578 else if (radius
== 0.0)
584 sa
= (x1
- xc
== 0) ?
585 (y1
- yc
< 0) ? 90.0 : -90.0 :
586 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
587 ea
= (x2
- xc
== 0) ?
588 (y2
- yc
< 0) ? 90.0 : -90.0 :
589 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
592 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
594 wxGraphicsPath path
= m_graphicContext
->CreatePath();
595 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
596 path
.MoveToPoint( xc
, yc
);
597 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
598 // get clockwise angles
599 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
600 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
601 path
.AddLineToPoint( xc
, yc
);
602 m_graphicContext
->DrawPath(path
);
605 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
606 double sa
, double ea
)
608 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
610 if ( !m_logicalFunctionSupported
)
613 m_graphicContext
->PushState();
614 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
615 wxDouble factor
= ((wxDouble
) w
) / h
;
616 m_graphicContext
->Scale( factor
, 1.0);
618 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
619 // get clockwise angles
620 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
622 wxGraphicsPath path
= m_graphicContext
->CreatePath();
623 path
.MoveToPoint( 0, 0 );
624 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
625 path
.AddLineToPoint( 0, 0 );
626 m_graphicContext
->FillPath( path
);
628 path
= m_graphicContext
->CreatePath();
629 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
630 m_graphicContext
->StrokePath( path
);
634 wxGraphicsPath path
= m_graphicContext
->CreatePath();
635 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
636 m_graphicContext
->DrawPath( path
);
639 m_graphicContext
->PopState();
642 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
644 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
646 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
649 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
650 wxCoord xoffset
, wxCoord yoffset
)
652 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
654 if ( !m_logicalFunctionSupported
)
657 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
658 for( int i
= 0; i
< n
; ++i
)
660 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
661 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
664 m_graphicContext
->StrokeLines( n
, pointsD
);
669 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
671 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
673 if ( !m_logicalFunctionSupported
)
676 wxGraphicsPath path
= m_graphicContext
->CreatePath();
678 wxPointList::compatibility_iterator node
= points
->GetFirst();
683 wxPoint
*p
= node
->GetData();
688 node
= node
->GetNext();
693 wxCoord cx1
= ( x1
+ x2
) / 2;
694 wxCoord cy1
= ( y1
+ y2
) / 2;
696 path
.MoveToPoint( x1
, y1
);
697 path
.AddLineToPoint( cx1
, cy1
);
698 #if !wxUSE_STD_CONTAINERS
700 while ((node
= node
->GetNext()) != NULL
)
703 while ((node
= node
->GetNext()))
704 #endif // !wxUSE_STD_CONTAINERS
712 wxCoord cx4
= (x1
+ x2
) / 2;
713 wxCoord cy4
= (y1
+ y2
) / 2;
715 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
721 path
.AddLineToPoint( x2
, y2
);
723 m_graphicContext
->StrokePath( path
);
725 #endif // wxUSE_SPLINES
727 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
728 wxCoord xoffset
, wxCoord yoffset
,
729 wxPolygonFillMode fillStyle
)
731 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
733 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
735 if ( !m_logicalFunctionSupported
)
738 bool closeIt
= false;
739 if (points
[n
-1] != points
[0])
742 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
743 for( int i
= 0; i
< n
; ++i
)
745 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
746 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
749 pointsD
[n
] = pointsD
[0];
751 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
755 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
760 wxPolygonFillMode fillStyle
)
763 wxGraphicsPath path
= m_graphicContext
->CreatePath();
766 for ( int j
= 0; j
< n
; ++j
)
768 wxPoint start
= points
[i
];
769 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
772 for ( int k
= 1; k
< l
; ++k
)
774 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
778 if ( start
!= points
[i
-1])
779 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
781 m_graphicContext
->DrawPath( path
, fillStyle
);
784 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
786 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
788 if ( !m_logicalFunctionSupported
)
791 // CMB: draw nothing if transformed w or h is 0
792 if (w
== 0 || h
== 0)
795 if ( m_graphicContext
->ShouldOffset() )
797 // if we are offsetting the entire rectangle is moved 0.5, so the
798 // border line gets off by 1
802 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
805 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
806 wxCoord w
, wxCoord h
,
809 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
811 if ( !m_logicalFunctionSupported
)
815 radius
= - radius
* ((w
< h
) ? w
: h
);
817 // CMB: draw nothing if transformed w or h is 0
818 if (w
== 0 || h
== 0)
821 if ( m_graphicContext
->ShouldOffset() )
823 // if we are offsetting the entire rectangle is moved 0.5, so the
824 // border line gets off by 1
828 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
831 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
833 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
835 if ( !m_logicalFunctionSupported
)
838 if ( m_graphicContext
->ShouldOffset() )
840 // if we are offsetting the entire rectangle is moved 0.5, so the
841 // border line gets off by 1
845 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
848 bool wxGCDCImpl::CanDrawBitmap() const
853 bool wxGCDCImpl::DoBlit(
854 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
855 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
856 wxRasterOperationMode logical_func
, bool useMask
,
857 wxCoord xsrcMask
, wxCoord ysrcMask
)
859 return DoStretchBlit( xdest
, ydest
, width
, height
,
860 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
864 bool wxGCDCImpl::DoStretchBlit(
865 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
866 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
867 wxRasterOperationMode logical_func
, bool useMask
,
868 wxCoord xsrcMask
, wxCoord ysrcMask
)
870 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
871 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
873 if ( logical_func
== wxNO_OP
)
876 wxCompositionMode mode
= TranslateRasterOp(logical_func
);
877 if ( mode
== wxCOMPOSITION_INVALID
)
879 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
885 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
886 if (m_graphicContext
->SetCompositionMode(mode
))
888 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
889 if (mode
== wxCOMPOSITION_XOR
)
891 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
894 if (xsrcMask
== -1 && ysrcMask
== -1)
900 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
901 source
->LogicalToDeviceY(ysrc
),
902 source
->LogicalToDeviceXRel(srcWidth
),
903 source
->LogicalToDeviceYRel(srcHeight
));
905 // if needed clip the subrect down to the size of the source DC
907 source
->GetSize(&sw
, &sh
);
908 sw
= source
->LogicalToDeviceXRel(sw
);
909 sh
= source
->LogicalToDeviceYRel(sh
);
910 if (subrect
.x
+ subrect
.width
> sw
)
911 subrect
.width
= sw
- subrect
.x
;
912 if (subrect
.y
+ subrect
.height
> sh
)
913 subrect
.height
= sh
- subrect
.y
;
915 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
919 if ( !useMask
&& blit
.GetMask() )
922 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
923 dstWidth
, dstHeight
);
927 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
931 if (mode
== wxCOMPOSITION_XOR
)
933 m_graphicContext
->SetAntialiasMode(formerAa
);
937 m_graphicContext
->SetCompositionMode(formerMode
);
942 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
945 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
949 if ( !m_logicalFunctionSupported
)
952 if ( m_backgroundMode
== wxTRANSPARENT
)
953 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
955 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
958 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
960 // For compatibility with other ports (notably wxGTK) and because it's
961 // genuinely useful, we allow passing multiline strings to DrawText().
962 // However there is no native OSX function to draw them directly so we
963 // instead reuse the generic DrawLabel() method to render them. Of course,
964 // DrawLabel() itself will call back to us but with single line strings
965 // only so there won't be any infinite recursion here.
966 if ( str
.find('\n') != wxString::npos
)
968 GetOwner()->DrawLabel(str
, wxRect(x
, y
, 0, 0));
972 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
977 if ( !m_logicalFunctionSupported
)
980 if ( m_backgroundMode
== wxTRANSPARENT
)
981 m_graphicContext
->DrawText( str
, x
,y
);
983 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
986 bool wxGCDCImpl::CanGetTextExtent() const
988 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
993 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
994 wxCoord
*descent
, wxCoord
*externalLeading
,
995 const wxFont
*theFont
) const
997 wxCHECK_RET( m_graphicContext
, wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1001 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
1004 wxDouble h
, d
, e
, w
;
1006 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1009 *height
= (wxCoord
)(h
+0.5);
1011 *descent
= (wxCoord
)(d
+0.5);
1012 if ( externalLeading
)
1013 *externalLeading
= (wxCoord
)(e
+0.5);
1015 *width
= (wxCoord
)(w
+0.5);
1019 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
1023 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1025 wxCHECK_MSG( m_graphicContext
, false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1027 widths
.Add(0,text
.Length());
1028 if ( text
.IsEmpty() )
1031 wxArrayDouble widthsD
;
1033 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1034 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1035 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1040 wxCoord
wxGCDCImpl::GetCharWidth(void) const
1043 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1048 wxCoord
wxGCDCImpl::GetCharHeight(void) const
1051 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1056 void wxGCDCImpl::Clear(void)
1058 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1059 // TODO better implementation / incorporate size info into wxGCDC or context
1060 m_graphicContext
->SetBrush( m_backgroundBrush
);
1061 wxPen p
= *wxTRANSPARENT_PEN
;
1062 m_graphicContext
->SetPen( p
);
1063 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
1064 m_graphicContext
->SetCompositionMode(wxCOMPOSITION_SOURCE
);
1065 DoDrawRectangle( 0, 0, 32000 , 32000 );
1066 m_graphicContext
->SetCompositionMode(formerMode
);
1067 m_graphicContext
->SetPen( m_pen
);
1068 m_graphicContext
->SetBrush( m_brush
);
1071 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
1073 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
1075 m_graphicContext
->GetSize( &w
, &h
);
1077 *height
= (int) (h
+0.5);
1079 *width
= (int) (w
+0.5);
1082 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1083 const wxColour
& initialColour
,
1084 const wxColour
& destColour
,
1085 wxDirection nDirection
)
1092 start
= rect
.GetRightBottom();
1094 end
= rect
.GetLeftBottom();
1097 start
= rect
.GetLeftBottom();
1098 end
= rect
.GetRightBottom();
1102 start
= rect
.GetLeftBottom();
1104 end
= rect
.GetLeftTop();
1107 start
= rect
.GetLeftTop();
1108 end
= rect
.GetLeftBottom();
1115 if (rect
.width
== 0 || rect
.height
== 0)
1118 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1119 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1120 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1121 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1122 m_graphicContext
->SetPen(m_pen
);
1125 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1126 const wxColour
& initialColour
,
1127 const wxColour
& destColour
,
1128 const wxPoint
& circleCenter
)
1131 wxInt32 cx
= rect
.GetWidth() / 2;
1132 wxInt32 cy
= rect
.GetHeight() / 2;
1139 // make sure the background is filled (todo move into specific platform implementation ?)
1140 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1141 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1142 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1144 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1145 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1146 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1147 nRadius
,initialColour
,destColour
));
1149 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1150 m_graphicContext
->SetPen(m_pen
);
1153 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1154 wxCoord width
, wxCoord height
)
1156 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1160 wxRect
wxGCDCImpl::MSWApplyGDIPlusTransform(const wxRect
& r
) const
1162 wxGraphicsContext
* const gc
= GetGraphicsContext();
1163 wxCHECK_MSG( gc
, r
, wxT("Invalid wxGCDC") );
1167 gc
->GetTransform().TransformPoint(&x
, &y
);
1176 #endif // wxUSE_GRAPHICS_CONTEXT