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
) )
117 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
118 wxGCDC::wxGCDC(const wxEnhMetaFileDC
& dc
)
119 : wxDC(new wxGCDCImpl(this, dc
))
124 wxGCDC::wxGCDC(wxGraphicsContext
* context
) :
125 wxDC( new wxGCDCImpl( this ) )
127 SetGraphicsContext(context
);
131 wxDC( new wxGCDCImpl( this ) )
139 wxGraphicsContext
* wxGCDC::GetGraphicsContext() const
141 if (!m_pimpl
) return NULL
;
142 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
143 return gc_impl
->GetGraphicsContext();
146 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
148 if (!m_pimpl
) return;
149 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
150 gc_impl
->SetGraphicsContext( ctx
);
153 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
155 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
161 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
163 delete m_graphicContext
;
164 m_graphicContext
= ctx
;
165 if ( m_graphicContext
)
167 m_matrixOriginal
= m_graphicContext
->GetTransform();
169 // apply the stored transformations to the passed in context
170 ComputeScaleAndOrigin();
171 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
172 m_graphicContext
->SetPen( m_pen
);
173 m_graphicContext
->SetBrush( m_brush
);
177 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
181 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
182 m_window
= dc
.GetWindow();
185 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
189 wxGraphicsContext
* context
;
190 context
= wxGraphicsContext::Create(dc
);
191 SetGraphicsContext( context
);
194 #if wxUSE_PRINTING_ARCHITECTURE
195 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
199 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
203 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
204 wxGCDCImpl::wxGCDCImpl(wxDC
*owner
, const wxEnhMetaFileDC
& dc
)
208 SetGraphicsContext(wxGraphicsContext::Create(dc
));
212 void wxGCDCImpl::Init()
216 m_mm_to_pix_x
= mm2pt
;
217 m_mm_to_pix_y
= mm2pt
;
219 m_pen
= *wxBLACK_PEN
;
220 m_font
= *wxNORMAL_FONT
;
221 m_brush
= *wxWHITE_BRUSH
;
223 m_graphicContext
= wxGraphicsContext::Create();
224 m_logicalFunctionSupported
= true;
228 wxGCDCImpl::~wxGCDCImpl()
230 delete m_graphicContext
;
233 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
236 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
237 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
239 int w
= bmp
.GetWidth();
240 int h
= bmp
.GetHeight();
241 if ( bmp
.GetDepth() == 1 )
243 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
244 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
245 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
246 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
247 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
248 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
249 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
251 else // not a monochrome bitmap, handle it normally
253 // make a copy in case we need to remove its mask, if we don't modify
254 // it the copy is cheap as bitmaps are reference-counted
255 wxBitmap
bmpCopy(bmp
);
256 if ( !useMask
&& bmp
.GetMask() )
257 bmpCopy
.SetMask(NULL
);
259 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
263 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
265 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
266 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
268 wxCoord w
= icon
.GetWidth();
269 wxCoord h
= icon
.GetHeight();
271 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
274 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
279 void wxGCDCImpl::EndDoc()
283 void wxGCDCImpl::StartPage()
287 void wxGCDCImpl::EndPage()
291 void wxGCDCImpl::Flush()
293 m_graphicContext
->Flush();
296 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
298 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
300 m_graphicContext
->Clip( x
, y
, w
, h
);
303 m_clipX1
= wxMax( m_clipX1
, x
);
304 m_clipY1
= wxMax( m_clipY1
, y
);
305 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
306 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
319 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
321 // region is in device coordinates
322 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
326 //DestroyClippingRegion();
330 wxRegion
logRegion( region
);
333 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
334 logRegion
.GetBox( x
, y
, w
, h
);
336 m_graphicContext
->Clip( logRegion
);
339 m_clipX1
= wxMax( m_clipX1
, x
);
340 m_clipY1
= wxMax( m_clipY1
, y
);
341 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
342 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
355 void wxGCDCImpl::DestroyClippingRegion()
357 m_graphicContext
->ResetClip();
358 // currently the clip eg of a window extends to the area between the scrollbars
359 // so we must explicitly make sure it only covers the area we want it to draw
361 GetOwner()->GetSize( &width
, &height
) ;
362 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
364 m_graphicContext
->SetPen( m_pen
);
365 m_graphicContext
->SetBrush( m_brush
);
370 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
374 GetOwner()->GetSize( &w
, &h
);
376 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
378 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
381 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
383 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
385 // don't set m_textForegroundColour to an invalid colour as we'd crash
386 // later then (we use m_textForegroundColour.GetColor() without checking
390 m_textForegroundColour
= col
;
391 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
395 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
397 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
399 m_textBackgroundColour
= col
;
402 void wxGCDCImpl::SetMapMode( wxMappingMode mode
)
407 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
411 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
415 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
419 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
424 SetLogicalScale( 1.0, 1.0 );
428 ComputeScaleAndOrigin();
431 wxSize
wxGCDCImpl::GetPPI() const
433 return wxSize(72, 72);
436 int wxGCDCImpl::GetDepth() const
441 void wxGCDCImpl::ComputeScaleAndOrigin()
443 wxDCImpl::ComputeScaleAndOrigin();
445 if ( m_graphicContext
)
447 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
449 // the logical origin sets the origin to have new coordinates
450 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
451 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
453 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
455 m_graphicContext
->SetTransform( m_matrixOriginal
);
456 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
460 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
465 void wxGCDCImpl::SetBackgroundMode( int mode
)
467 m_backgroundMode
= mode
;
470 void wxGCDCImpl::SetFont( const wxFont
&font
)
473 if ( m_graphicContext
)
477 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
478 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
482 void wxGCDCImpl::SetPen( const wxPen
&pen
)
485 if ( m_graphicContext
)
487 m_graphicContext
->SetPen( m_pen
);
491 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
494 if ( m_graphicContext
)
496 m_graphicContext
->SetBrush( m_brush
);
500 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
502 m_backgroundBrush
= brush
;
503 if (!m_backgroundBrush
.IsOk())
507 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
509 m_logicalFunction
= function
;
511 wxCompositionMode mode
= TranslateRasterOp( function
);
512 m_logicalFunctionSupported
= mode
!= wxCOMPOSITION_INVALID
;
513 if (m_logicalFunctionSupported
)
514 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
516 if ( function
== wxXOR
)
517 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
519 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
522 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
523 const wxColour
& WXUNUSED(col
),
524 wxFloodFillStyle
WXUNUSED(style
))
529 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
531 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
535 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
537 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
539 if ( !m_logicalFunctionSupported
)
542 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
544 CalcBoundingBox(x1
, y1
);
545 CalcBoundingBox(x2
, y2
);
548 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
550 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
552 if ( !m_logicalFunctionSupported
)
557 GetOwner()->GetSize( &w
, &h
);
559 m_graphicContext
->StrokeLine(0,y
,w
,y
);
560 m_graphicContext
->StrokeLine(x
,0,x
,h
);
562 CalcBoundingBox(0, 0);
563 CalcBoundingBox(0+w
, 0+h
);
566 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
567 wxCoord x2
, wxCoord y2
,
568 wxCoord xc
, wxCoord yc
)
570 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
572 if ( !m_logicalFunctionSupported
)
577 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
578 wxCoord rad
= (wxCoord
)radius
;
580 if (x1
== x2
&& y1
== y2
)
585 else if (radius
== 0.0)
591 sa
= (x1
- xc
== 0) ?
592 (y1
- yc
< 0) ? 90.0 : -90.0 :
593 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
594 ea
= (x2
- xc
== 0) ?
595 (y2
- yc
< 0) ? 90.0 : -90.0 :
596 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
599 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
601 wxGraphicsPath path
= m_graphicContext
->CreatePath();
602 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
603 path
.MoveToPoint( xc
, yc
);
604 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
605 // get clockwise angles
606 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
607 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
608 path
.AddLineToPoint( xc
, yc
);
609 m_graphicContext
->DrawPath(path
);
612 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
613 double sa
, double ea
)
615 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
617 if ( !m_logicalFunctionSupported
)
620 m_graphicContext
->PushState();
621 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
622 wxDouble factor
= ((wxDouble
) w
) / h
;
623 m_graphicContext
->Scale( factor
, 1.0);
625 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
626 // get clockwise angles
627 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
629 wxGraphicsPath path
= m_graphicContext
->CreatePath();
630 path
.MoveToPoint( 0, 0 );
631 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
632 path
.AddLineToPoint( 0, 0 );
633 m_graphicContext
->FillPath( path
);
635 path
= m_graphicContext
->CreatePath();
636 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
637 m_graphicContext
->StrokePath( path
);
641 wxGraphicsPath path
= m_graphicContext
->CreatePath();
642 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
643 m_graphicContext
->DrawPath( path
);
646 m_graphicContext
->PopState();
649 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
651 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
653 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
656 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
657 wxCoord xoffset
, wxCoord yoffset
)
659 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
661 if ( !m_logicalFunctionSupported
)
664 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
665 for( int i
= 0; i
< n
; ++i
)
667 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
668 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
671 m_graphicContext
->StrokeLines( n
, pointsD
);
676 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
678 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
680 if ( !m_logicalFunctionSupported
)
683 wxGraphicsPath path
= m_graphicContext
->CreatePath();
685 wxPointList::compatibility_iterator node
= points
->GetFirst();
690 wxPoint
*p
= node
->GetData();
695 node
= node
->GetNext();
700 wxCoord cx1
= ( x1
+ x2
) / 2;
701 wxCoord cy1
= ( y1
+ y2
) / 2;
703 path
.MoveToPoint( x1
, y1
);
704 path
.AddLineToPoint( cx1
, cy1
);
705 #if !wxUSE_STD_CONTAINERS
707 while ((node
= node
->GetNext()) != NULL
)
710 while ((node
= node
->GetNext()))
711 #endif // !wxUSE_STD_CONTAINERS
719 wxCoord cx4
= (x1
+ x2
) / 2;
720 wxCoord cy4
= (y1
+ y2
) / 2;
722 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
728 path
.AddLineToPoint( x2
, y2
);
730 m_graphicContext
->StrokePath( path
);
732 #endif // wxUSE_SPLINES
734 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
735 wxCoord xoffset
, wxCoord yoffset
,
736 wxPolygonFillMode fillStyle
)
738 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
740 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
742 if ( !m_logicalFunctionSupported
)
745 bool closeIt
= false;
746 if (points
[n
-1] != points
[0])
749 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
750 for( int i
= 0; i
< n
; ++i
)
752 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
753 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
756 pointsD
[n
] = pointsD
[0];
758 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
762 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
767 wxPolygonFillMode fillStyle
)
770 wxGraphicsPath path
= m_graphicContext
->CreatePath();
773 for ( int j
= 0; j
< n
; ++j
)
775 wxPoint start
= points
[i
];
776 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
779 for ( int k
= 1; k
< l
; ++k
)
781 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
785 if ( start
!= points
[i
-1])
786 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
788 m_graphicContext
->DrawPath( path
, fillStyle
);
791 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
793 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
795 if ( !m_logicalFunctionSupported
)
798 // CMB: draw nothing if transformed w or h is 0
799 if (w
== 0 || h
== 0)
802 if ( m_graphicContext
->ShouldOffset() )
804 // if we are offsetting the entire rectangle is moved 0.5, so the
805 // border line gets off by 1
809 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
812 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
813 wxCoord w
, wxCoord h
,
816 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
818 if ( !m_logicalFunctionSupported
)
822 radius
= - radius
* ((w
< h
) ? w
: h
);
824 // CMB: draw nothing if transformed w or h is 0
825 if (w
== 0 || h
== 0)
828 if ( m_graphicContext
->ShouldOffset() )
830 // if we are offsetting the entire rectangle is moved 0.5, so the
831 // border line gets off by 1
835 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
838 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
840 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
842 if ( !m_logicalFunctionSupported
)
845 if ( m_graphicContext
->ShouldOffset() )
847 // if we are offsetting the entire rectangle is moved 0.5, so the
848 // border line gets off by 1
852 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
855 bool wxGCDCImpl::CanDrawBitmap() const
860 bool wxGCDCImpl::DoBlit(
861 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
862 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
863 wxRasterOperationMode logical_func
, bool useMask
,
864 wxCoord xsrcMask
, wxCoord ysrcMask
)
866 return DoStretchBlit( xdest
, ydest
, width
, height
,
867 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
871 bool wxGCDCImpl::DoStretchBlit(
872 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
873 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
874 wxRasterOperationMode logical_func
, bool useMask
,
875 wxCoord xsrcMask
, wxCoord ysrcMask
)
877 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
878 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
880 if ( logical_func
== wxNO_OP
)
883 wxCompositionMode mode
= TranslateRasterOp(logical_func
);
884 if ( mode
== wxCOMPOSITION_INVALID
)
886 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
890 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
891 source
->LogicalToDeviceY(ysrc
),
892 source
->LogicalToDeviceXRel(srcWidth
),
893 source
->LogicalToDeviceYRel(srcHeight
));
894 const wxRect subrectOrig
= subrect
;
895 // clip the subrect down to the size of the source DC
897 source
->GetSize(&clip
.width
, &clip
.height
);
898 subrect
.Intersect(clip
);
899 if (subrect
.width
== 0)
904 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
905 if (m_graphicContext
->SetCompositionMode(mode
))
907 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
908 if (mode
== wxCOMPOSITION_XOR
)
910 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
913 if (xsrcMask
== -1 && ysrcMask
== -1)
919 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
923 if ( !useMask
&& blit
.GetMask() )
929 double h
= dstHeight
;
930 // adjust dest rect if source rect is clipped
931 if (subrect
.width
!= subrectOrig
.width
|| subrect
.height
!= subrectOrig
.height
)
933 x
+= (subrect
.x
- subrectOrig
.x
) / double(subrectOrig
.width
) * dstWidth
;
934 y
+= (subrect
.y
- subrectOrig
.y
) / double(subrectOrig
.height
) * dstHeight
;
935 w
*= double(subrect
.width
) / subrectOrig
.width
;
936 h
*= double(subrect
.height
) / subrectOrig
.height
;
938 m_graphicContext
->DrawBitmap(blit
, x
, y
, w
, h
);
942 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
946 if (mode
== wxCOMPOSITION_XOR
)
948 m_graphicContext
->SetAntialiasMode(formerAa
);
952 m_graphicContext
->SetCompositionMode(formerMode
);
957 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
960 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
964 if ( !m_logicalFunctionSupported
)
967 if ( m_backgroundMode
== wxTRANSPARENT
)
968 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
970 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
973 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
975 // For compatibility with other ports (notably wxGTK) and because it's
976 // genuinely useful, we allow passing multiline strings to DrawText().
977 // However there is no native OSX function to draw them directly so we
978 // instead reuse the generic DrawLabel() method to render them. Of course,
979 // DrawLabel() itself will call back to us but with single line strings
980 // only so there won't be any infinite recursion here.
981 if ( str
.find('\n') != wxString::npos
)
983 GetOwner()->DrawLabel(str
, wxRect(x
, y
, 0, 0));
987 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
992 if ( !m_logicalFunctionSupported
)
995 if ( m_backgroundMode
== wxTRANSPARENT
)
996 m_graphicContext
->DrawText( str
, x
,y
);
998 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
1001 bool wxGCDCImpl::CanGetTextExtent() const
1003 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1008 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
1009 wxCoord
*descent
, wxCoord
*externalLeading
,
1010 const wxFont
*theFont
) const
1012 wxCHECK_RET( m_graphicContext
, wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1016 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
1019 wxDouble h
, d
, e
, w
;
1021 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
1024 *height
= (wxCoord
)(h
+0.5);
1026 *descent
= (wxCoord
)(d
+0.5);
1027 if ( externalLeading
)
1028 *externalLeading
= (wxCoord
)(e
+0.5);
1030 *width
= (wxCoord
)(w
+0.5);
1034 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
1038 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1040 wxCHECK_MSG( m_graphicContext
, false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1042 widths
.Add(0,text
.Length());
1043 if ( text
.IsEmpty() )
1046 wxArrayDouble widthsD
;
1048 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1049 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1050 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1055 wxCoord
wxGCDCImpl::GetCharWidth(void) const
1058 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1063 wxCoord
wxGCDCImpl::GetCharHeight(void) const
1066 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1071 void wxGCDCImpl::Clear(void)
1073 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1074 // TODO better implementation / incorporate size info into wxGCDC or context
1075 m_graphicContext
->SetBrush( m_backgroundBrush
);
1076 wxPen p
= *wxTRANSPARENT_PEN
;
1077 m_graphicContext
->SetPen( p
);
1078 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
1079 m_graphicContext
->SetCompositionMode(wxCOMPOSITION_SOURCE
);
1080 DoDrawRectangle( 0, 0, 32000 , 32000 );
1081 m_graphicContext
->SetCompositionMode(formerMode
);
1082 m_graphicContext
->SetPen( m_pen
);
1083 m_graphicContext
->SetBrush( m_brush
);
1086 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
1088 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
1090 m_graphicContext
->GetSize( &w
, &h
);
1092 *height
= (int) (h
+0.5);
1094 *width
= (int) (w
+0.5);
1097 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1098 const wxColour
& initialColour
,
1099 const wxColour
& destColour
,
1100 wxDirection nDirection
)
1107 start
= rect
.GetRightBottom();
1109 end
= rect
.GetLeftBottom();
1112 start
= rect
.GetLeftBottom();
1113 end
= rect
.GetRightBottom();
1117 start
= rect
.GetLeftBottom();
1119 end
= rect
.GetLeftTop();
1122 start
= rect
.GetLeftTop();
1123 end
= rect
.GetLeftBottom();
1130 if (rect
.width
== 0 || rect
.height
== 0)
1133 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1134 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1135 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1136 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1137 m_graphicContext
->SetPen(m_pen
);
1138 m_graphicContext
->SetBrush(m_brush
);
1141 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1142 const wxColour
& initialColour
,
1143 const wxColour
& destColour
,
1144 const wxPoint
& circleCenter
)
1147 wxInt32 cx
= rect
.GetWidth() / 2;
1148 wxInt32 cy
= rect
.GetHeight() / 2;
1155 // make sure the background is filled (todo move into specific platform implementation ?)
1156 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1157 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1158 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1160 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1161 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1162 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1163 nRadius
,initialColour
,destColour
));
1165 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1166 m_graphicContext
->SetPen(m_pen
);
1167 m_graphicContext
->SetBrush(m_brush
);
1170 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1171 wxCoord width
, wxCoord height
)
1173 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1177 wxRect
wxGCDCImpl::MSWApplyGDIPlusTransform(const wxRect
& r
) const
1179 wxGraphicsContext
* const gc
= GetGraphicsContext();
1180 wxCHECK_MSG( gc
, r
, wxT("Invalid wxGCDC") );
1184 gc
->GetTransform().TransformPoint(&x
, &y
);
1193 #endif // wxUSE_GRAPHICS_CONTEXT