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 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
73 wxDC( new wxGCDCImpl( this, dc
) )
78 wxDC( new wxGCDCImpl( this ) )
86 wxGraphicsContext
* wxGCDC::GetGraphicsContext()
88 if (!m_pimpl
) return NULL
;
89 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
90 return gc_impl
->GetGraphicsContext();
93 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
96 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
97 gc_impl
->SetGraphicsContext( ctx
);
100 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
102 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
108 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
110 delete m_graphicContext
;
111 m_graphicContext
= ctx
;
112 if ( m_graphicContext
)
114 m_matrixOriginal
= m_graphicContext
->GetTransform();
116 // apply the stored transformations to the passed in context
117 ComputeScaleAndOrigin();
118 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
119 m_graphicContext
->SetPen( m_pen
);
120 m_graphicContext
->SetBrush( m_brush
);
124 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
128 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
129 m_window
= dc
.GetWindow();
132 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
136 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
139 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
143 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
146 void wxGCDCImpl::Init()
150 m_mm_to_pix_x
= mm2pt
;
151 m_mm_to_pix_y
= mm2pt
;
153 m_pen
= *wxBLACK_PEN
;
154 m_font
= *wxNORMAL_FONT
;
155 m_brush
= *wxWHITE_BRUSH
;
157 m_graphicContext
= NULL
;
158 m_logicalFunctionSupported
= true;
162 wxGCDCImpl::~wxGCDCImpl()
164 delete m_graphicContext
;
167 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
169 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
170 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
172 if ( bmp
.GetDepth() == 1 )
174 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
175 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
176 m_graphicContext
->DrawRectangle( x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
177 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
178 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
179 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
180 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
183 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
186 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
188 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
189 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
191 wxCoord w
= icon
.GetWidth();
192 wxCoord h
= icon
.GetHeight();
194 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
197 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
202 void wxGCDCImpl::EndDoc()
206 void wxGCDCImpl::StartPage()
210 void wxGCDCImpl::EndPage()
214 void wxGCDCImpl::Flush()
217 CGContextFlush( (CGContextRef
) m_graphicContext
->GetNativeContext() );
221 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
223 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
225 m_graphicContext
->Clip( x
, y
, w
, h
);
228 m_clipX1
= wxMax( m_clipX1
, x
);
229 m_clipY1
= wxMax( m_clipY1
, y
);
230 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
231 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
244 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
246 // region is in device coordinates
247 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
251 //DestroyClippingRegion();
255 wxRegion
logRegion( region
);
258 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
259 logRegion
.GetBox( x
, y
, w
, h
);
261 m_graphicContext
->Clip( logRegion
);
264 m_clipX1
= wxMax( m_clipX1
, x
);
265 m_clipY1
= wxMax( m_clipY1
, y
);
266 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
267 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
280 void wxGCDCImpl::DestroyClippingRegion()
282 m_graphicContext
->ResetClip();
283 // currently the clip eg of a window extends to the area between the scrollbars
284 // so we must explicitely make sure it only covers the area we want it to draw
286 GetOwner()->GetSize( &width
, &height
) ;
287 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
289 m_graphicContext
->SetPen( m_pen
);
290 m_graphicContext
->SetBrush( m_brush
);
295 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
299 GetOwner()->GetSize( &w
, &h
);
301 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
303 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
306 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
308 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
310 if ( col
!= m_textForegroundColour
)
312 m_textForegroundColour
= col
;
313 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
317 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
319 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
321 m_textBackgroundColour
= col
;
324 void wxGCDCImpl::SetMapMode( int mode
)
329 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
333 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
337 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
341 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
346 SetLogicalScale( 1.0, 1.0 );
350 ComputeScaleAndOrigin();
353 wxSize
wxGCDCImpl::GetPPI() const
355 return wxSize(72, 72);
358 int wxGCDCImpl::GetDepth() const
363 void wxGCDCImpl::ComputeScaleAndOrigin()
365 wxDCImpl::ComputeScaleAndOrigin();
367 if ( m_graphicContext
)
369 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
371 // the logical origin sets the origin to have new coordinates
372 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
373 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
375 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
377 m_graphicContext
->SetTransform( m_matrixOriginal
);
378 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
382 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
387 void wxGCDCImpl::SetBackgroundMode( int mode
)
389 m_backgroundMode
= mode
;
392 void wxGCDCImpl::SetFont( const wxFont
&font
)
395 if ( m_graphicContext
)
399 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
400 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
404 void wxGCDCImpl::SetPen( const wxPen
&pen
)
410 if ( m_graphicContext
)
412 m_graphicContext
->SetPen( m_pen
);
416 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
418 if (m_brush
== brush
)
422 if ( m_graphicContext
)
424 m_graphicContext
->SetBrush( m_brush
);
428 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
430 if (m_backgroundBrush
== brush
)
433 m_backgroundBrush
= brush
;
434 if (!m_backgroundBrush
.IsOk())
438 void wxGCDCImpl::SetLogicalFunction( int function
)
440 if (m_logicalFunction
== function
)
443 m_logicalFunction
= function
;
444 if ( m_graphicContext
->SetLogicalFunction( function
) )
445 m_logicalFunctionSupported
=true;
447 m_logicalFunctionSupported
=false;
450 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
451 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
456 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
458 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
462 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
464 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
466 if ( !m_logicalFunctionSupported
)
469 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
471 CalcBoundingBox(x1
, y1
);
472 CalcBoundingBox(x2
, y2
);
475 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
477 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
479 if ( !m_logicalFunctionSupported
)
484 GetOwner()->GetSize( &w
, &h
);
486 m_graphicContext
->StrokeLine(0,y
,w
,y
);
487 m_graphicContext
->StrokeLine(x
,0,x
,h
);
489 CalcBoundingBox(0, 0);
490 CalcBoundingBox(0+w
, 0+h
);
493 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
494 wxCoord x2
, wxCoord y2
,
495 wxCoord xc
, wxCoord yc
)
497 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
499 if ( !m_logicalFunctionSupported
)
504 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
505 wxCoord rad
= (wxCoord
)radius
;
507 if (x1
== x2
&& y1
== y2
)
512 else if (radius
== 0.0)
518 sa
= (x1
- xc
== 0) ?
519 (y1
- yc
< 0) ? 90.0 : -90.0 :
520 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
521 ea
= (x2
- xc
== 0) ?
522 (y2
- yc
< 0) ? 90.0 : -90.0 :
523 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
526 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
528 wxGraphicsPath path
= m_graphicContext
->CreatePath();
529 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
530 path
.MoveToPoint( xc
, yc
);
531 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
532 // get clockwise angles
533 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
534 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
535 path
.AddLineToPoint( xc
, yc
);
536 m_graphicContext
->DrawPath(path
);
539 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
540 double sa
, double ea
)
542 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
544 if ( !m_logicalFunctionSupported
)
547 m_graphicContext
->PushState();
548 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
549 wxDouble factor
= ((wxDouble
) w
) / h
;
550 m_graphicContext
->Scale( factor
, 1.0);
552 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
553 // get clockwise angles
554 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
556 wxGraphicsPath path
= m_graphicContext
->CreatePath();
557 path
.MoveToPoint( 0, 0 );
558 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
559 path
.AddLineToPoint( 0, 0 );
560 m_graphicContext
->FillPath( path
);
562 path
= m_graphicContext
->CreatePath();
563 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
564 m_graphicContext
->StrokePath( path
);
568 wxGraphicsPath path
= m_graphicContext
->CreatePath();
569 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
570 m_graphicContext
->DrawPath( path
);
573 m_graphicContext
->PopState();
576 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
578 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
580 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
583 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
584 wxCoord xoffset
, wxCoord yoffset
)
586 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
588 if ( !m_logicalFunctionSupported
)
591 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
592 for( int i
= 0; i
< n
; ++i
)
594 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
595 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
598 m_graphicContext
->StrokeLines( n
, pointsD
);
603 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
605 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
607 if ( !m_logicalFunctionSupported
)
610 wxGraphicsPath path
= m_graphicContext
->CreatePath();
612 wxPointList::compatibility_iterator node
= points
->GetFirst();
613 if (node
== wxPointList::compatibility_iterator())
617 wxPoint
*p
= node
->GetData();
622 node
= node
->GetNext();
627 wxCoord cx1
= ( x1
+ x2
) / 2;
628 wxCoord cy1
= ( y1
+ y2
) / 2;
630 path
.MoveToPoint( x1
, y1
);
631 path
.AddLineToPoint( cx1
, cy1
);
634 while ((node
= node
->GetNext()) != NULL
)
637 while ((node
= node
->GetNext()))
646 wxCoord cx4
= (x1
+ x2
) / 2;
647 wxCoord cy4
= (y1
+ y2
) / 2;
649 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
655 path
.AddLineToPoint( x2
, y2
);
657 m_graphicContext
->StrokePath( path
);
659 #endif // wxUSE_SPLINES
661 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
662 wxCoord xoffset
, wxCoord yoffset
,
665 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
667 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
669 if ( !m_logicalFunctionSupported
)
672 bool closeIt
= false;
673 if (points
[n
-1] != points
[0])
676 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
677 for( int i
= 0; i
< n
; ++i
)
679 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
680 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
683 pointsD
[n
] = pointsD
[0];
685 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
689 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
697 wxGraphicsPath path
= m_graphicContext
->CreatePath();
700 for ( int j
= 0; j
< n
; ++j
)
702 wxPoint start
= points
[i
];
703 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
706 for ( int k
= 1; k
< l
; ++k
)
708 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
712 if ( start
!= points
[i
-1])
713 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
715 m_graphicContext
->DrawPath( path
, fillStyle
);
718 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
720 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
722 if ( !m_logicalFunctionSupported
)
725 // CMB: draw nothing if transformed w or h is 0
726 if (w
== 0 || h
== 0)
729 if ( m_graphicContext
->ShouldOffset() )
731 // if we are offsetting the entire rectangle is moved 0.5, so the
732 // border line gets off by 1
736 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
739 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
740 wxCoord w
, wxCoord h
,
743 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
745 if ( !m_logicalFunctionSupported
)
749 radius
= - radius
* ((w
< h
) ? w
: h
);
751 // CMB: draw nothing if transformed w or h is 0
752 if (w
== 0 || h
== 0)
755 if ( m_graphicContext
->ShouldOffset() )
757 // if we are offsetting the entire rectangle is moved 0.5, so the
758 // border line gets off by 1
762 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
765 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
767 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
769 if ( !m_logicalFunctionSupported
)
772 if ( m_graphicContext
->ShouldOffset() )
774 // if we are offsetting the entire rectangle is moved 0.5, so the
775 // border line gets off by 1
779 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
782 bool wxGCDCImpl::CanDrawBitmap() const
787 bool wxGCDCImpl::DoBlit(
788 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
789 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
790 wxCoord xsrcMask
, wxCoord ysrcMask
)
792 return DoStretchBlit( xdest
, ydest
, width
, height
,
793 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
797 bool wxGCDCImpl::DoStretchBlit(
798 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
799 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
800 int logical_func
, bool WXUNUSED(useMask
),
801 wxCoord xsrcMask
, wxCoord ysrcMask
)
803 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
804 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
806 if ( logical_func
== wxNO_OP
)
808 else if ( !m_graphicContext
->SetLogicalFunction( logical_func
) )
811 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
815 if (xsrcMask
== -1 && ysrcMask
== -1)
821 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
822 source
->LogicalToDeviceY(ysrc
),
823 source
->LogicalToDeviceXRel(srcWidth
),
824 source
->LogicalToDeviceYRel(srcHeight
));
826 // if needed clip the subrect down to the size of the source DC
828 source
->GetSize(&sw
, &sh
);
829 sw
= source
->LogicalToDeviceXRel(sw
);
830 sh
= source
->LogicalToDeviceYRel(sh
);
831 if (subrect
.x
+ subrect
.width
> sw
)
832 subrect
.width
= sw
- subrect
.x
;
833 if (subrect
.y
+ subrect
.height
> sh
)
834 subrect
.height
= sh
- subrect
.y
;
836 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
840 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
841 dstWidth
, dstHeight
);
845 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
849 // reset logical function
850 m_graphicContext
->SetLogicalFunction( m_logicalFunction
);
855 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
858 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
860 if ( str
.length() == 0 )
862 if ( !m_logicalFunctionSupported
)
865 if ( m_backgroundMode
== wxTRANSPARENT
)
866 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
868 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
871 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
873 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
875 if ( str
.length() == 0 )
878 if ( !m_logicalFunctionSupported
)
881 if ( m_backgroundMode
== wxTRANSPARENT
)
882 m_graphicContext
->DrawText( str
, x
,y
);
884 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
887 bool wxGCDCImpl::CanGetTextExtent() const
889 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
894 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
895 wxCoord
*descent
, wxCoord
*externalLeading
,
896 const wxFont
*theFont
) const
898 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
902 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
905 wxDouble h
, d
, e
, w
;
907 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
910 *height
= (wxCoord
)(h
+0.5);
912 *descent
= (wxCoord
)(d
+0.5);
913 if ( externalLeading
)
914 *externalLeading
= (wxCoord
)(e
+0.5);
916 *width
= (wxCoord
)(w
+0.5);
920 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
924 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
926 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
928 widths
.Add(0,text
.Length());
929 if ( text
.IsEmpty() )
932 wxArrayDouble widthsD
;
934 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
935 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
936 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
941 wxCoord
wxGCDCImpl::GetCharWidth(void) const
944 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
949 wxCoord
wxGCDCImpl::GetCharHeight(void) const
952 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
957 void wxGCDCImpl::Clear(void)
959 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
960 // TODO better implementation / incorporate size info into wxGCDC or context
961 m_graphicContext
->SetBrush( m_backgroundBrush
);
962 wxPen p
= *wxTRANSPARENT_PEN
;
963 m_graphicContext
->SetPen( p
);
964 DoDrawRectangle( 0, 0, 32000 , 32000 );
965 m_graphicContext
->SetPen( m_pen
);
966 m_graphicContext
->SetBrush( m_brush
);
969 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
971 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
973 m_graphicContext
->GetSize( &w
, &h
);
975 *height
= (int) (h
+0.5);
977 *width
= (int) (w
+0.5);
980 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
981 const wxColour
& initialColour
,
982 const wxColour
& destColour
,
983 wxDirection nDirection
)
990 start
= rect
.GetRightBottom();
992 end
= rect
.GetLeftBottom();
995 start
= rect
.GetLeftBottom();
996 end
= rect
.GetRightBottom();
1000 start
= rect
.GetLeftBottom();
1002 end
= rect
.GetLeftTop();
1005 start
= rect
.GetLeftTop();
1006 end
= rect
.GetLeftBottom();
1013 if (rect
.width
== 0 || rect
.height
== 0)
1016 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1017 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1018 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1019 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1020 m_graphicContext
->SetPen(m_pen
);
1023 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1024 const wxColour
& initialColour
,
1025 const wxColour
& destColour
,
1026 const wxPoint
& circleCenter
)
1029 wxInt32 cx
= rect
.GetWidth() / 2;
1030 wxInt32 cy
= rect
.GetHeight() / 2;
1037 // make sure the background is filled (todo move into specific platform implementation ?)
1038 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1039 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1040 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1042 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1043 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1044 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1045 nRadius
,initialColour
,destColour
));
1047 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1048 m_graphicContext
->SetPen(m_pen
);
1051 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1052 wxCoord width
, wxCoord height
)
1054 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1057 #endif // wxUSE_GRAPHICS_CONTEXT