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"
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"
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 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
62 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
63 wxDC( new wxGCDCImpl( this, dc
) )
67 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
68 wxDC( new wxGCDCImpl( this, dc
) )
72 #if wxUSE_PRINTING_ARCHITECTURE
73 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
74 wxDC( new wxGCDCImpl( this, dc
) )
80 wxDC( new wxGCDCImpl( this ) )
88 wxGraphicsContext
* wxGCDC::GetGraphicsContext()
90 if (!m_pimpl
) return NULL
;
91 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
92 return gc_impl
->GetGraphicsContext();
95 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
98 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
99 gc_impl
->SetGraphicsContext( ctx
);
102 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
104 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
110 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
112 delete m_graphicContext
;
113 m_graphicContext
= ctx
;
114 if ( m_graphicContext
)
116 m_matrixOriginal
= m_graphicContext
->GetTransform();
118 // apply the stored transformations to the passed in context
119 ComputeScaleAndOrigin();
120 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
121 m_graphicContext
->SetPen( m_pen
);
122 m_graphicContext
->SetBrush( m_brush
);
126 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
130 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
131 m_window
= dc
.GetWindow();
134 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
138 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
141 #if wxUSE_PRINTING_ARCHITECTURE
142 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
146 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
150 void wxGCDCImpl::Init()
154 m_mm_to_pix_x
= mm2pt
;
155 m_mm_to_pix_y
= mm2pt
;
157 m_pen
= *wxBLACK_PEN
;
158 m_font
= *wxNORMAL_FONT
;
159 m_brush
= *wxWHITE_BRUSH
;
161 m_graphicContext
= NULL
;
162 m_logicalFunctionSupported
= true;
166 wxGCDCImpl::~wxGCDCImpl()
168 delete m_graphicContext
;
171 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
173 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
174 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
176 if ( bmp
.GetDepth() == 1 )
178 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
179 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
180 m_graphicContext
->DrawRectangle( x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
181 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
182 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
183 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
184 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
187 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
190 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
192 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
193 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
195 wxCoord w
= icon
.GetWidth();
196 wxCoord h
= icon
.GetHeight();
198 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
201 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
206 void wxGCDCImpl::EndDoc()
210 void wxGCDCImpl::StartPage()
214 void wxGCDCImpl::EndPage()
218 void wxGCDCImpl::Flush()
221 CGContextFlush( (CGContextRef
) m_graphicContext
->GetNativeContext() );
225 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
227 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
229 m_graphicContext
->Clip( x
, y
, w
, h
);
232 m_clipX1
= wxMax( m_clipX1
, x
);
233 m_clipY1
= wxMax( m_clipY1
, y
);
234 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
235 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
248 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
250 // region is in device coordinates
251 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
255 //DestroyClippingRegion();
259 wxRegion
logRegion( region
);
262 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
263 logRegion
.GetBox( x
, y
, w
, h
);
265 m_graphicContext
->Clip( logRegion
);
268 m_clipX1
= wxMax( m_clipX1
, x
);
269 m_clipY1
= wxMax( m_clipY1
, y
);
270 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
271 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
284 void wxGCDCImpl::DestroyClippingRegion()
286 m_graphicContext
->ResetClip();
287 // currently the clip eg of a window extends to the area between the scrollbars
288 // so we must explicitely make sure it only covers the area we want it to draw
290 GetOwner()->GetSize( &width
, &height
) ;
291 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
293 m_graphicContext
->SetPen( m_pen
);
294 m_graphicContext
->SetBrush( m_brush
);
299 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
303 GetOwner()->GetSize( &w
, &h
);
305 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
307 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
310 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
312 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
314 if ( col
!= m_textForegroundColour
)
316 m_textForegroundColour
= col
;
317 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
321 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
323 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
325 m_textBackgroundColour
= col
;
328 void wxGCDCImpl::SetMapMode( int mode
)
333 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
337 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
341 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
345 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
350 SetLogicalScale( 1.0, 1.0 );
354 ComputeScaleAndOrigin();
357 wxSize
wxGCDCImpl::GetPPI() const
359 return wxSize(72, 72);
362 int wxGCDCImpl::GetDepth() const
367 void wxGCDCImpl::ComputeScaleAndOrigin()
369 wxDCImpl::ComputeScaleAndOrigin();
371 if ( m_graphicContext
)
373 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
375 // the logical origin sets the origin to have new coordinates
376 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
377 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
379 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
381 m_graphicContext
->SetTransform( m_matrixOriginal
);
382 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
386 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
391 void wxGCDCImpl::SetBackgroundMode( int mode
)
393 m_backgroundMode
= mode
;
396 void wxGCDCImpl::SetFont( const wxFont
&font
)
399 if ( m_graphicContext
)
403 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
404 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
408 void wxGCDCImpl::SetPen( const wxPen
&pen
)
414 if ( m_graphicContext
)
416 m_graphicContext
->SetPen( m_pen
);
420 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
422 if (m_brush
== brush
)
426 if ( m_graphicContext
)
428 m_graphicContext
->SetBrush( m_brush
);
432 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
434 if (m_backgroundBrush
== brush
)
437 m_backgroundBrush
= brush
;
438 if (!m_backgroundBrush
.IsOk())
442 void wxGCDCImpl::SetLogicalFunction( int function
)
444 if (m_logicalFunction
== function
)
447 m_logicalFunction
= function
;
448 if ( m_graphicContext
->SetLogicalFunction( function
) )
449 m_logicalFunctionSupported
=true;
451 m_logicalFunctionSupported
=false;
454 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
455 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
460 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
462 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
466 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
468 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
470 if ( !m_logicalFunctionSupported
)
473 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
475 CalcBoundingBox(x1
, y1
);
476 CalcBoundingBox(x2
, y2
);
479 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
481 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
483 if ( !m_logicalFunctionSupported
)
488 GetOwner()->GetSize( &w
, &h
);
490 m_graphicContext
->StrokeLine(0,y
,w
,y
);
491 m_graphicContext
->StrokeLine(x
,0,x
,h
);
493 CalcBoundingBox(0, 0);
494 CalcBoundingBox(0+w
, 0+h
);
497 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
498 wxCoord x2
, wxCoord y2
,
499 wxCoord xc
, wxCoord yc
)
501 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
503 if ( !m_logicalFunctionSupported
)
508 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
509 wxCoord rad
= (wxCoord
)radius
;
511 if (x1
== x2
&& y1
== y2
)
516 else if (radius
== 0.0)
522 sa
= (x1
- xc
== 0) ?
523 (y1
- yc
< 0) ? 90.0 : -90.0 :
524 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
525 ea
= (x2
- xc
== 0) ?
526 (y2
- yc
< 0) ? 90.0 : -90.0 :
527 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
530 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
532 wxGraphicsPath path
= m_graphicContext
->CreatePath();
533 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
534 path
.MoveToPoint( xc
, yc
);
535 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
536 // get clockwise angles
537 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
538 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
539 path
.AddLineToPoint( xc
, yc
);
540 m_graphicContext
->DrawPath(path
);
543 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
544 double sa
, double ea
)
546 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
548 if ( !m_logicalFunctionSupported
)
551 m_graphicContext
->PushState();
552 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
553 wxDouble factor
= ((wxDouble
) w
) / h
;
554 m_graphicContext
->Scale( factor
, 1.0);
556 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
557 // get clockwise angles
558 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
560 wxGraphicsPath path
= m_graphicContext
->CreatePath();
561 path
.MoveToPoint( 0, 0 );
562 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
563 path
.AddLineToPoint( 0, 0 );
564 m_graphicContext
->FillPath( path
);
566 path
= m_graphicContext
->CreatePath();
567 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
568 m_graphicContext
->StrokePath( path
);
572 wxGraphicsPath path
= m_graphicContext
->CreatePath();
573 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
574 m_graphicContext
->DrawPath( path
);
577 m_graphicContext
->PopState();
580 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
582 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
584 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
587 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
588 wxCoord xoffset
, wxCoord yoffset
)
590 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
592 if ( !m_logicalFunctionSupported
)
595 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
596 for( int i
= 0; i
< n
; ++i
)
598 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
599 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
602 m_graphicContext
->StrokeLines( n
, pointsD
);
607 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
609 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
611 if ( !m_logicalFunctionSupported
)
614 wxGraphicsPath path
= m_graphicContext
->CreatePath();
616 wxPointList::compatibility_iterator node
= points
->GetFirst();
617 if (node
== wxPointList::compatibility_iterator())
621 wxPoint
*p
= node
->GetData();
626 node
= node
->GetNext();
631 wxCoord cx1
= ( x1
+ x2
) / 2;
632 wxCoord cy1
= ( y1
+ y2
) / 2;
634 path
.MoveToPoint( x1
, y1
);
635 path
.AddLineToPoint( cx1
, cy1
);
638 while ((node
= node
->GetNext()) != NULL
)
641 while ((node
= node
->GetNext()))
650 wxCoord cx4
= (x1
+ x2
) / 2;
651 wxCoord cy4
= (y1
+ y2
) / 2;
653 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
659 path
.AddLineToPoint( x2
, y2
);
661 m_graphicContext
->StrokePath( path
);
663 #endif // wxUSE_SPLINES
665 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
666 wxCoord xoffset
, wxCoord yoffset
,
669 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
671 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
673 if ( !m_logicalFunctionSupported
)
676 bool closeIt
= false;
677 if (points
[n
-1] != points
[0])
680 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
681 for( int i
= 0; i
< n
; ++i
)
683 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
684 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
687 pointsD
[n
] = pointsD
[0];
689 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
693 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
701 wxGraphicsPath path
= m_graphicContext
->CreatePath();
704 for ( int j
= 0; j
< n
; ++j
)
706 wxPoint start
= points
[i
];
707 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
710 for ( int k
= 1; k
< l
; ++k
)
712 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
716 if ( start
!= points
[i
-1])
717 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
719 m_graphicContext
->DrawPath( path
, fillStyle
);
722 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
724 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
726 if ( !m_logicalFunctionSupported
)
729 // CMB: draw nothing if transformed w or h is 0
730 if (w
== 0 || h
== 0)
733 if ( m_graphicContext
->ShouldOffset() )
735 // if we are offsetting the entire rectangle is moved 0.5, so the
736 // border line gets off by 1
740 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
743 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
744 wxCoord w
, wxCoord h
,
747 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
749 if ( !m_logicalFunctionSupported
)
753 radius
= - radius
* ((w
< h
) ? w
: h
);
755 // CMB: draw nothing if transformed w or h is 0
756 if (w
== 0 || h
== 0)
759 if ( m_graphicContext
->ShouldOffset() )
761 // if we are offsetting the entire rectangle is moved 0.5, so the
762 // border line gets off by 1
766 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
769 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
771 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
773 if ( !m_logicalFunctionSupported
)
776 if ( m_graphicContext
->ShouldOffset() )
778 // if we are offsetting the entire rectangle is moved 0.5, so the
779 // border line gets off by 1
783 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
786 bool wxGCDCImpl::CanDrawBitmap() const
791 bool wxGCDCImpl::DoBlit(
792 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
793 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
794 wxCoord xsrcMask
, wxCoord ysrcMask
)
796 return DoStretchBlit( xdest
, ydest
, width
, height
,
797 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
801 bool wxGCDCImpl::DoStretchBlit(
802 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
803 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
804 int logical_func
, bool WXUNUSED(useMask
),
805 wxCoord xsrcMask
, wxCoord ysrcMask
)
807 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
808 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
810 if ( logical_func
== wxNO_OP
)
812 else if ( !m_graphicContext
->SetLogicalFunction( logical_func
) )
815 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
819 if (xsrcMask
== -1 && ysrcMask
== -1)
825 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
826 source
->LogicalToDeviceY(ysrc
),
827 source
->LogicalToDeviceXRel(srcWidth
),
828 source
->LogicalToDeviceYRel(srcHeight
));
830 // if needed clip the subrect down to the size of the source DC
832 source
->GetSize(&sw
, &sh
);
833 sw
= source
->LogicalToDeviceXRel(sw
);
834 sh
= source
->LogicalToDeviceYRel(sh
);
835 if (subrect
.x
+ subrect
.width
> sw
)
836 subrect
.width
= sw
- subrect
.x
;
837 if (subrect
.y
+ subrect
.height
> sh
)
838 subrect
.height
= sh
- subrect
.y
;
840 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
844 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
845 dstWidth
, dstHeight
);
849 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
853 // reset logical function
854 m_graphicContext
->SetLogicalFunction( m_logicalFunction
);
859 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
862 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
864 if ( str
.length() == 0 )
866 if ( !m_logicalFunctionSupported
)
869 if ( m_backgroundMode
== wxTRANSPARENT
)
870 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
872 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
875 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
877 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
879 if ( str
.length() == 0 )
882 if ( !m_logicalFunctionSupported
)
885 if ( m_backgroundMode
== wxTRANSPARENT
)
886 m_graphicContext
->DrawText( str
, x
,y
);
888 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
891 bool wxGCDCImpl::CanGetTextExtent() const
893 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
898 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
899 wxCoord
*descent
, wxCoord
*externalLeading
,
900 const wxFont
*theFont
) const
902 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
906 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
909 wxDouble h
, d
, e
, w
;
911 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
914 *height
= (wxCoord
)(h
+0.5);
916 *descent
= (wxCoord
)(d
+0.5);
917 if ( externalLeading
)
918 *externalLeading
= (wxCoord
)(e
+0.5);
920 *width
= (wxCoord
)(w
+0.5);
924 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
928 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
930 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
932 widths
.Add(0,text
.Length());
933 if ( text
.IsEmpty() )
936 wxArrayDouble widthsD
;
938 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
939 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
940 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
945 wxCoord
wxGCDCImpl::GetCharWidth(void) const
948 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
953 wxCoord
wxGCDCImpl::GetCharHeight(void) const
956 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
961 void wxGCDCImpl::Clear(void)
963 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
964 // TODO better implementation / incorporate size info into wxGCDC or context
965 m_graphicContext
->SetBrush( m_backgroundBrush
);
966 wxPen p
= *wxTRANSPARENT_PEN
;
967 m_graphicContext
->SetPen( p
);
968 DoDrawRectangle( 0, 0, 32000 , 32000 );
969 m_graphicContext
->SetPen( m_pen
);
970 m_graphicContext
->SetBrush( m_brush
);
973 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
975 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
977 m_graphicContext
->GetSize( &w
, &h
);
979 *height
= (int) (h
+0.5);
981 *width
= (int) (w
+0.5);
984 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
985 const wxColour
& initialColour
,
986 const wxColour
& destColour
,
987 wxDirection nDirection
)
994 start
= rect
.GetRightBottom();
996 end
= rect
.GetLeftBottom();
999 start
= rect
.GetLeftBottom();
1000 end
= rect
.GetRightBottom();
1004 start
= rect
.GetLeftBottom();
1006 end
= rect
.GetLeftTop();
1009 start
= rect
.GetLeftTop();
1010 end
= rect
.GetLeftBottom();
1017 if (rect
.width
== 0 || rect
.height
== 0)
1020 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1021 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1022 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1023 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1024 m_graphicContext
->SetPen(m_pen
);
1027 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1028 const wxColour
& initialColour
,
1029 const wxColour
& destColour
,
1030 const wxPoint
& circleCenter
)
1033 wxInt32 cx
= rect
.GetWidth() / 2;
1034 wxInt32 cy
= rect
.GetHeight() / 2;
1041 // make sure the background is filled (todo move into specific platform implementation ?)
1042 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1043 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1044 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1046 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1047 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1048 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1049 nRadius
,initialColour
,destColour
));
1051 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1052 m_graphicContext
->SetPen(m_pen
);
1055 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1056 wxCoord width
, wxCoord height
)
1058 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1061 #endif // wxUSE_GRAPHICS_CONTEXT