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"
32 #include "wx/mac/private.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 static const double RAD2DEG
= 180.0 / M_PI
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static inline double DegToRad(double deg
)
46 return (deg
* M_PI
) / 180.0;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
55 wxGCDC::wxGCDC(const wxWindowDC
& dc
)
57 m_pimpl
= new wxGCDCImpl( this, dc
);
60 wxGCDC::wxGCDC( const wxMemoryDC
& dc
)
62 m_pimpl
= new wxGCDCImpl( this, dc
);
67 m_pimpl
= new wxGCDCImpl( this );
70 wxGraphicsContext
* wxGCDC::GetGraphicsContext()
72 if (!m_pimpl
) return NULL
;
73 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
74 return gc_impl
->GetGraphicsContext();
77 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
80 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
81 gc_impl
->SetGraphicsContext( ctx
);
84 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
86 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
92 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
94 delete m_graphicContext
;
95 m_graphicContext
= ctx
;
96 if ( m_graphicContext
)
98 m_matrixOriginal
= m_graphicContext
->GetTransform();
100 // apply the stored transformations to the passed in context
101 ComputeScaleAndOrigin();
102 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
103 m_graphicContext
->SetPen( m_pen
);
104 m_graphicContext
->SetBrush( m_brush
);
108 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
112 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
115 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
119 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
122 void wxGCDCImpl::Init()
126 m_mm_to_pix_x
= mm2pt
;
127 m_mm_to_pix_y
= mm2pt
;
129 m_pen
= *wxBLACK_PEN
;
130 m_font
= *wxNORMAL_FONT
;
131 m_brush
= *wxWHITE_BRUSH
;
133 m_graphicContext
= NULL
;
134 m_logicalFunctionSupported
= true;
138 wxGCDCImpl::~wxGCDCImpl()
140 delete m_graphicContext
;
143 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
145 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
146 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
148 if ( bmp
.GetDepth() == 1 )
150 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
151 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
152 m_graphicContext
->DrawRectangle( x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
153 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
154 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
155 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
156 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
159 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
162 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
164 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
165 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
167 wxCoord w
= icon
.GetWidth();
168 wxCoord h
= icon
.GetHeight();
170 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
173 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
178 void wxGCDCImpl::EndDoc()
182 void wxGCDCImpl::StartPage()
186 void wxGCDCImpl::EndPage()
190 void wxGCDCImpl::Flush()
193 CGContextFlush( (CGContextRef
) m_graphicContext
->GetNativeContext() );
197 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
199 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
201 m_graphicContext
->Clip( x
, y
, w
, h
);
204 m_clipX1
= wxMax( m_clipX1
, x
);
205 m_clipY1
= wxMax( m_clipY1
, y
);
206 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
207 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
220 void wxGCDCImpl::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
222 // region is in device coordinates
223 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
227 //DestroyClippingRegion();
231 wxRegion
logRegion( region
);
234 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
235 logRegion
.GetBox( x
, y
, w
, h
);
237 m_graphicContext
->Clip( logRegion
);
240 m_clipX1
= wxMax( m_clipX1
, x
);
241 m_clipY1
= wxMax( m_clipY1
, y
);
242 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
243 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
256 void wxGCDCImpl::DestroyClippingRegion()
258 m_graphicContext
->ResetClip();
259 // currently the clip eg of a window extends to the area between the scrollbars
260 // so we must explicitely make sure it only covers the area we want it to draw
262 GetOwner()->GetSize( &width
, &height
) ;
263 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
265 m_graphicContext
->SetPen( m_pen
);
266 m_graphicContext
->SetBrush( m_brush
);
271 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
275 GetOwner()->GetSize( &w
, &h
);
277 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
279 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
282 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
284 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
286 if ( col
!= m_textForegroundColour
)
288 m_textForegroundColour
= col
;
289 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
293 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
295 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
297 m_textBackgroundColour
= col
;
300 void wxGCDCImpl::SetMapMode( int mode
)
305 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
309 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
313 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
317 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
322 SetLogicalScale( 1.0, 1.0 );
326 ComputeScaleAndOrigin();
329 void wxGCDCImpl::SetUserScale( double x
, double y
)
331 // allow negative ? -> no
335 ComputeScaleAndOrigin();
338 void wxGCDCImpl::SetLogicalScale( double x
, double y
)
343 ComputeScaleAndOrigin();
346 void wxGCDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
348 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
349 m_logicalOriginY
= y
* m_signY
;
350 ComputeScaleAndOrigin();
353 void wxGCDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
357 ComputeScaleAndOrigin();
360 void wxGCDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
362 m_signX
= (xLeftRight
? 1 : -1);
363 m_signY
= (yBottomUp
? -1 : 1);
364 ComputeScaleAndOrigin();
367 wxSize
wxGCDCImpl::GetPPI() const
369 return wxSize(72, 72);
372 int wxGCDCImpl::GetDepth() const
377 void wxGCDCImpl::ComputeScaleAndOrigin()
379 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
380 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
382 if ( m_graphicContext
)
384 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
385 m_matrixCurrent
.Translate( m_deviceOriginX
, m_deviceOriginY
);
386 m_matrixCurrent
.Scale( m_scaleX
, m_scaleY
);
387 // the logical origin sets the origin to have new coordinates
388 m_matrixCurrent
.Translate( -m_logicalOriginX
, -m_logicalOriginY
);
390 m_graphicContext
->SetTransform( m_matrixOriginal
);
391 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
395 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
400 void wxGCDCImpl::SetBackgroundMode( int mode
)
402 m_backgroundMode
= mode
;
405 void wxGCDCImpl::SetFont( const wxFont
&font
)
408 if ( m_graphicContext
)
412 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
413 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
417 void wxGCDCImpl::SetPen( const wxPen
&pen
)
423 if ( m_graphicContext
)
425 m_graphicContext
->SetPen( m_pen
);
429 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
431 if (m_brush
== brush
)
435 if ( m_graphicContext
)
437 m_graphicContext
->SetBrush( m_brush
);
441 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
443 if (m_backgroundBrush
== brush
)
446 m_backgroundBrush
= brush
;
447 if (!m_backgroundBrush
.IsOk())
451 void wxGCDCImpl::SetLogicalFunction( int function
)
453 if (m_logicalFunction
== function
)
456 m_logicalFunction
= function
;
457 if ( m_graphicContext
->SetLogicalFunction( function
) )
458 m_logicalFunctionSupported
=true;
460 m_logicalFunctionSupported
=false;
463 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
464 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
469 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
471 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
475 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
477 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
479 if ( !m_logicalFunctionSupported
)
482 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
484 CalcBoundingBox(x1
, y1
);
485 CalcBoundingBox(x2
, y2
);
488 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
490 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
492 if ( !m_logicalFunctionSupported
)
497 GetOwner()->GetSize( &w
, &h
);
499 m_graphicContext
->StrokeLine(0,y
,w
,y
);
500 m_graphicContext
->StrokeLine(x
,0,x
,h
);
502 CalcBoundingBox(0, 0);
503 CalcBoundingBox(0+w
, 0+h
);
506 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
507 wxCoord x2
, wxCoord y2
,
508 wxCoord xc
, wxCoord yc
)
510 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
512 if ( !m_logicalFunctionSupported
)
517 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
518 wxCoord rad
= (wxCoord
)radius
;
520 if (x1
== x2
&& y1
== y2
)
525 else if (radius
== 0.0)
531 sa
= (x1
- xc
== 0) ?
532 (y1
- yc
< 0) ? 90.0 : -90.0 :
533 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
534 ea
= (x2
- xc
== 0) ?
535 (y2
- yc
< 0) ? 90.0 : -90.0 :
536 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
539 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
541 wxGraphicsPath path
= m_graphicContext
->CreatePath();
542 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
543 path
.MoveToPoint( xc
, yc
);
544 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
545 // get clockwise angles
546 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
547 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
548 path
.AddLineToPoint( xc
, yc
);
549 m_graphicContext
->DrawPath(path
);
552 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
553 double sa
, double ea
)
555 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
557 if ( !m_logicalFunctionSupported
)
560 m_graphicContext
->PushState();
561 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
562 wxDouble factor
= ((wxDouble
) w
) / h
;
563 m_graphicContext
->Scale( factor
, 1.0);
565 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
566 // get clockwise angles
567 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
569 wxGraphicsPath path
= m_graphicContext
->CreatePath();
570 path
.MoveToPoint( 0, 0 );
571 path
.AddLineToPoint( h
/ 2.0 * cos(DegToRad(sa
)) , h
/ 2.0 * sin(DegToRad(-sa
)) );
572 path
.AddLineToPoint( h
/ 2.0 * cos(DegToRad(ea
)) , h
/ 2.0 * sin(DegToRad(-ea
)) );
573 path
.AddLineToPoint( 0, 0 );
574 m_graphicContext
->FillPath( path
);
576 path
= m_graphicContext
->CreatePath();
577 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
578 m_graphicContext
->FillPath( path
);
579 m_graphicContext
->StrokePath( path
);
583 wxGraphicsPath path
= m_graphicContext
->CreatePath();
584 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
585 m_graphicContext
->DrawPath( path
);
588 m_graphicContext
->PopState();
591 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
593 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
595 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
598 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
599 wxCoord xoffset
, wxCoord yoffset
)
601 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
603 if ( !m_logicalFunctionSupported
)
606 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
607 for( int i
= 0; i
< n
; ++i
)
609 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
610 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
613 m_graphicContext
->StrokeLines( n
, pointsD
);
618 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
620 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
622 if ( !m_logicalFunctionSupported
)
625 wxGraphicsPath path
= m_graphicContext
->CreatePath();
627 wxPointList::compatibility_iterator node
= points
->GetFirst();
628 if (node
== wxPointList::compatibility_iterator())
632 wxPoint
*p
= node
->GetData();
637 node
= node
->GetNext();
642 wxCoord cx1
= ( x1
+ x2
) / 2;
643 wxCoord cy1
= ( y1
+ y2
) / 2;
645 path
.MoveToPoint( x1
, y1
);
646 path
.AddLineToPoint( cx1
, cy1
);
649 while ((node
= node
->GetNext()) != NULL
)
652 while ((node
= node
->GetNext()))
661 wxCoord cx4
= (x1
+ x2
) / 2;
662 wxCoord cy4
= (y1
+ y2
) / 2;
664 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
670 path
.AddLineToPoint( x2
, y2
);
672 m_graphicContext
->StrokePath( path
);
674 #endif // wxUSE_SPLINES
676 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
677 wxCoord xoffset
, wxCoord yoffset
,
680 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
682 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
684 if ( !m_logicalFunctionSupported
)
687 bool closeIt
= false;
688 if (points
[n
-1] != points
[0])
691 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
692 for( int i
= 0; i
< n
; ++i
)
694 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
695 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
698 pointsD
[n
] = pointsD
[0];
700 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
704 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
712 wxGraphicsPath path
= m_graphicContext
->CreatePath();
715 for ( int j
= 0; j
< n
; ++j
)
717 wxPoint start
= points
[i
];
718 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
721 for ( int k
= 1; k
< l
; ++k
)
723 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
727 if ( start
!= points
[i
-1])
728 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
730 m_graphicContext
->DrawPath( path
, fillStyle
);
733 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
735 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
737 if ( !m_logicalFunctionSupported
)
740 // CMB: draw nothing if transformed w or h is 0
741 if (w
== 0 || h
== 0)
744 if ( m_graphicContext
->ShouldOffset() )
746 // if we are offsetting the entire rectangle is moved 0.5, so the
747 // border line gets off by 1
751 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
754 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
755 wxCoord w
, wxCoord h
,
758 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
760 if ( !m_logicalFunctionSupported
)
764 radius
= - radius
* ((w
< h
) ? w
: h
);
766 // CMB: draw nothing if transformed w or h is 0
767 if (w
== 0 || h
== 0)
770 if ( m_graphicContext
->ShouldOffset() )
772 // if we are offsetting the entire rectangle is moved 0.5, so the
773 // border line gets off by 1
777 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
780 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
782 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
784 if ( !m_logicalFunctionSupported
)
787 if ( m_graphicContext
->ShouldOffset() )
789 // if we are offsetting the entire rectangle is moved 0.5, so the
790 // border line gets off by 1
794 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
797 bool wxGCDCImpl::CanDrawBitmap() const
802 bool wxGCDCImpl::DoBlit(
803 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
804 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
805 wxCoord xsrcMask
, wxCoord ysrcMask
)
807 return DoStretchBlit( xdest
, ydest
, width
, height
,
808 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
812 bool wxGCDCImpl::DoStretchBlit(
813 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
814 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
815 int logical_func
, bool WXUNUSED(useMask
),
816 wxCoord xsrcMask
, wxCoord ysrcMask
)
818 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
819 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
821 if ( logical_func
== wxNO_OP
)
823 else if ( !m_graphicContext
->SetLogicalFunction( logical_func
) )
826 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
830 if (xsrcMask
== -1 && ysrcMask
== -1)
836 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
837 source
->LogicalToDeviceY(ysrc
),
838 source
->LogicalToDeviceXRel(srcWidth
),
839 source
->LogicalToDeviceYRel(srcHeight
));
841 // if needed clip the subrect down to the size of the source DC
843 source
->GetSize(&sw
, &sh
);
844 sw
= source
->LogicalToDeviceXRel(sw
);
845 sh
= source
->LogicalToDeviceYRel(sh
);
846 if (subrect
.x
+ subrect
.width
> sw
)
847 subrect
.width
= sw
- subrect
.x
;
848 if (subrect
.y
+ subrect
.height
> sh
)
849 subrect
.height
= sh
- subrect
.y
;
851 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
855 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
856 dstWidth
, dstHeight
);
860 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
864 // reset logical function
865 m_graphicContext
->SetLogicalFunction( m_logicalFunction
);
870 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
873 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
875 if ( str
.length() == 0 )
877 if ( !m_logicalFunctionSupported
)
880 if ( m_backgroundMode
== wxTRANSPARENT
)
881 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
883 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
886 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
888 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
890 if ( str
.length() == 0 )
893 if ( !m_logicalFunctionSupported
)
896 if ( m_backgroundMode
== wxTRANSPARENT
)
897 m_graphicContext
->DrawText( str
, x
,y
);
899 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
902 bool wxGCDCImpl::CanGetTextExtent() const
904 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
909 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
910 wxCoord
*descent
, wxCoord
*externalLeading
,
911 const wxFont
*theFont
) const
913 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
917 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
920 wxDouble h
, d
, e
, w
;
922 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
925 *height
= (wxCoord
)(h
+0.5);
927 *descent
= (wxCoord
)(d
+0.5);
928 if ( externalLeading
)
929 *externalLeading
= (wxCoord
)(e
+0.5);
931 *width
= (wxCoord
)(w
+0.5);
935 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
939 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
941 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
943 widths
.Add(0,text
.Length());
944 if ( text
.IsEmpty() )
947 wxArrayDouble widthsD
;
949 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
950 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
951 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
956 wxCoord
wxGCDCImpl::GetCharWidth(void) const
959 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
964 wxCoord
wxGCDCImpl::GetCharHeight(void) const
967 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
972 void wxGCDCImpl::Clear(void)
974 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
975 // TODO better implementation / incorporate size info into wxGCDC or context
976 m_graphicContext
->SetBrush( m_backgroundBrush
);
977 wxPen p
= *wxTRANSPARENT_PEN
;
978 m_graphicContext
->SetPen( p
);
979 DoDrawRectangle( 0, 0, 32000 , 32000 );
980 m_graphicContext
->SetPen( m_pen
);
981 m_graphicContext
->SetBrush( m_brush
);
984 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
990 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
991 const wxColour
& initialColour
,
992 const wxColour
& destColour
,
993 wxDirection nDirection
)
1000 start
= rect
.GetRightBottom();
1002 end
= rect
.GetLeftBottom();
1005 start
= rect
.GetLeftBottom();
1006 end
= rect
.GetRightBottom();
1010 start
= rect
.GetLeftBottom();
1012 end
= rect
.GetLeftTop();
1015 start
= rect
.GetLeftTop();
1016 end
= rect
.GetLeftBottom();
1023 if (rect
.width
== 0 || rect
.height
== 0)
1026 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1027 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1028 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1029 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1030 m_graphicContext
->SetPen(m_pen
);
1033 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1034 const wxColour
& initialColour
,
1035 const wxColour
& destColour
,
1036 const wxPoint
& circleCenter
)
1039 wxInt32 cx
= rect
.GetWidth() / 2;
1040 wxInt32 cy
= rect
.GetHeight() / 2;
1047 // make sure the background is filled (todo move into specific platform implementation ?)
1048 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1049 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1050 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1052 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1053 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1054 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1055 nRadius
,initialColour
,destColour
));
1057 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1058 m_graphicContext
->SetPen(m_pen
);
1061 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1062 wxCoord width
, wxCoord height
)
1064 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1067 #endif // wxUSE_GRAPHICS_CONTEXT