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 #include "ApplicationServices/ApplicationServices.h"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 static const double RAD2DEG
= 180.0 / M_PI
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 static inline double DegToRad(double deg
)
49 return (deg
* M_PI
) / 180.0;
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
58 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
59 wxDC( new wxGCDCImpl( this, dc
) )
63 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
64 wxDC( new wxGCDCImpl( this, dc
) )
68 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
69 wxDC( new wxGCDCImpl( this, dc
) )
74 wxDC( new wxGCDCImpl( this ) )
82 wxGraphicsContext
* wxGCDC::GetGraphicsContext()
84 if (!m_pimpl
) return NULL
;
85 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
86 return gc_impl
->GetGraphicsContext();
89 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
92 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
93 gc_impl
->SetGraphicsContext( ctx
);
96 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
98 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
104 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
106 delete m_graphicContext
;
107 m_graphicContext
= ctx
;
108 if ( m_graphicContext
)
110 m_matrixOriginal
= m_graphicContext
->GetTransform();
112 // apply the stored transformations to the passed in context
113 ComputeScaleAndOrigin();
114 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
115 m_graphicContext
->SetPen( m_pen
);
116 m_graphicContext
->SetBrush( m_brush
);
120 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
124 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
125 m_window
= dc
.GetWindow();
128 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
132 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
135 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
139 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
142 void wxGCDCImpl::Init()
146 m_mm_to_pix_x
= mm2pt
;
147 m_mm_to_pix_y
= mm2pt
;
149 m_pen
= *wxBLACK_PEN
;
150 m_font
= *wxNORMAL_FONT
;
151 m_brush
= *wxWHITE_BRUSH
;
153 m_graphicContext
= NULL
;
154 m_logicalFunctionSupported
= true;
158 wxGCDCImpl::~wxGCDCImpl()
160 delete m_graphicContext
;
163 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
165 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
166 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
168 if ( bmp
.GetDepth() == 1 )
170 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
171 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
172 m_graphicContext
->DrawRectangle( x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
173 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
174 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
175 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
176 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
179 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
182 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
184 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
185 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
187 wxCoord w
= icon
.GetWidth();
188 wxCoord h
= icon
.GetHeight();
190 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
193 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
198 void wxGCDCImpl::EndDoc()
202 void wxGCDCImpl::StartPage()
206 void wxGCDCImpl::EndPage()
210 void wxGCDCImpl::Flush()
213 CGContextFlush( (CGContextRef
) m_graphicContext
->GetNativeContext() );
217 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
219 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
221 m_graphicContext
->Clip( x
, y
, w
, h
);
224 m_clipX1
= wxMax( m_clipX1
, x
);
225 m_clipY1
= wxMax( m_clipY1
, y
);
226 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
227 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
240 void wxGCDCImpl::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
242 // region is in device coordinates
243 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
247 //DestroyClippingRegion();
251 wxRegion
logRegion( region
);
254 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
255 logRegion
.GetBox( x
, y
, w
, h
);
257 m_graphicContext
->Clip( logRegion
);
260 m_clipX1
= wxMax( m_clipX1
, x
);
261 m_clipY1
= wxMax( m_clipY1
, y
);
262 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
263 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
276 void wxGCDCImpl::DestroyClippingRegion()
278 m_graphicContext
->ResetClip();
279 // currently the clip eg of a window extends to the area between the scrollbars
280 // so we must explicitely make sure it only covers the area we want it to draw
282 GetOwner()->GetSize( &width
, &height
) ;
283 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
285 m_graphicContext
->SetPen( m_pen
);
286 m_graphicContext
->SetBrush( m_brush
);
291 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
295 GetOwner()->GetSize( &w
, &h
);
297 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
299 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
302 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
304 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
306 if ( col
!= m_textForegroundColour
)
308 m_textForegroundColour
= col
;
309 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
313 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
315 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
317 m_textBackgroundColour
= col
;
320 void wxGCDCImpl::SetMapMode( int mode
)
325 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
329 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
333 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
337 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
342 SetLogicalScale( 1.0, 1.0 );
346 ComputeScaleAndOrigin();
349 wxSize
wxGCDCImpl::GetPPI() const
351 return wxSize(72, 72);
354 int wxGCDCImpl::GetDepth() const
359 void wxGCDCImpl::ComputeScaleAndOrigin()
361 wxDCImpl::ComputeScaleAndOrigin();
363 if ( m_graphicContext
)
365 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
367 // the logical origin sets the origin to have new coordinates
368 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
369 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
371 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
373 m_graphicContext
->SetTransform( m_matrixOriginal
);
374 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
378 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
383 void wxGCDCImpl::SetBackgroundMode( int mode
)
385 m_backgroundMode
= mode
;
388 void wxGCDCImpl::SetFont( const wxFont
&font
)
391 if ( m_graphicContext
)
395 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
396 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
400 void wxGCDCImpl::SetPen( const wxPen
&pen
)
406 if ( m_graphicContext
)
408 m_graphicContext
->SetPen( m_pen
);
412 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
414 if (m_brush
== brush
)
418 if ( m_graphicContext
)
420 m_graphicContext
->SetBrush( m_brush
);
424 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
426 if (m_backgroundBrush
== brush
)
429 m_backgroundBrush
= brush
;
430 if (!m_backgroundBrush
.IsOk())
434 void wxGCDCImpl::SetLogicalFunction( int function
)
436 if (m_logicalFunction
== function
)
439 m_logicalFunction
= function
;
440 if ( m_graphicContext
->SetLogicalFunction( function
) )
441 m_logicalFunctionSupported
=true;
443 m_logicalFunctionSupported
=false;
446 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
447 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
452 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
454 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
458 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
460 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
462 if ( !m_logicalFunctionSupported
)
465 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
467 CalcBoundingBox(x1
, y1
);
468 CalcBoundingBox(x2
, y2
);
471 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
473 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
475 if ( !m_logicalFunctionSupported
)
480 GetOwner()->GetSize( &w
, &h
);
482 m_graphicContext
->StrokeLine(0,y
,w
,y
);
483 m_graphicContext
->StrokeLine(x
,0,x
,h
);
485 CalcBoundingBox(0, 0);
486 CalcBoundingBox(0+w
, 0+h
);
489 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
490 wxCoord x2
, wxCoord y2
,
491 wxCoord xc
, wxCoord yc
)
493 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
495 if ( !m_logicalFunctionSupported
)
500 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
501 wxCoord rad
= (wxCoord
)radius
;
503 if (x1
== x2
&& y1
== y2
)
508 else if (radius
== 0.0)
514 sa
= (x1
- xc
== 0) ?
515 (y1
- yc
< 0) ? 90.0 : -90.0 :
516 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
517 ea
= (x2
- xc
== 0) ?
518 (y2
- yc
< 0) ? 90.0 : -90.0 :
519 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
522 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
524 wxGraphicsPath path
= m_graphicContext
->CreatePath();
525 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
526 path
.MoveToPoint( xc
, yc
);
527 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
528 // get clockwise angles
529 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
530 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
531 path
.AddLineToPoint( xc
, yc
);
532 m_graphicContext
->DrawPath(path
);
535 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
536 double sa
, double ea
)
538 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
540 if ( !m_logicalFunctionSupported
)
543 m_graphicContext
->PushState();
544 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
545 wxDouble factor
= ((wxDouble
) w
) / h
;
546 m_graphicContext
->Scale( factor
, 1.0);
548 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
549 // get clockwise angles
550 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
552 wxGraphicsPath path
= m_graphicContext
->CreatePath();
553 path
.MoveToPoint( 0, 0 );
554 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
555 path
.AddLineToPoint( 0, 0 );
556 m_graphicContext
->FillPath( path
);
558 path
= m_graphicContext
->CreatePath();
559 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
560 m_graphicContext
->StrokePath( path
);
564 wxGraphicsPath path
= m_graphicContext
->CreatePath();
565 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
566 m_graphicContext
->DrawPath( path
);
569 m_graphicContext
->PopState();
572 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
574 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
576 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
579 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
580 wxCoord xoffset
, wxCoord yoffset
)
582 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
584 if ( !m_logicalFunctionSupported
)
587 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
588 for( int i
= 0; i
< n
; ++i
)
590 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
591 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
594 m_graphicContext
->StrokeLines( n
, pointsD
);
599 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
601 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
603 if ( !m_logicalFunctionSupported
)
606 wxGraphicsPath path
= m_graphicContext
->CreatePath();
608 wxPointList::compatibility_iterator node
= points
->GetFirst();
609 if (node
== wxPointList::compatibility_iterator())
613 wxPoint
*p
= node
->GetData();
618 node
= node
->GetNext();
623 wxCoord cx1
= ( x1
+ x2
) / 2;
624 wxCoord cy1
= ( y1
+ y2
) / 2;
626 path
.MoveToPoint( x1
, y1
);
627 path
.AddLineToPoint( cx1
, cy1
);
630 while ((node
= node
->GetNext()) != NULL
)
633 while ((node
= node
->GetNext()))
642 wxCoord cx4
= (x1
+ x2
) / 2;
643 wxCoord cy4
= (y1
+ y2
) / 2;
645 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
651 path
.AddLineToPoint( x2
, y2
);
653 m_graphicContext
->StrokePath( path
);
655 #endif // wxUSE_SPLINES
657 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
658 wxCoord xoffset
, wxCoord yoffset
,
661 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
663 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
665 if ( !m_logicalFunctionSupported
)
668 bool closeIt
= false;
669 if (points
[n
-1] != points
[0])
672 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
673 for( int i
= 0; i
< n
; ++i
)
675 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
676 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
679 pointsD
[n
] = pointsD
[0];
681 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
685 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
693 wxGraphicsPath path
= m_graphicContext
->CreatePath();
696 for ( int j
= 0; j
< n
; ++j
)
698 wxPoint start
= points
[i
];
699 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
702 for ( int k
= 1; k
< l
; ++k
)
704 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
708 if ( start
!= points
[i
-1])
709 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
711 m_graphicContext
->DrawPath( path
, fillStyle
);
714 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
716 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
718 if ( !m_logicalFunctionSupported
)
721 // CMB: draw nothing if transformed w or h is 0
722 if (w
== 0 || h
== 0)
725 if ( m_graphicContext
->ShouldOffset() )
727 // if we are offsetting the entire rectangle is moved 0.5, so the
728 // border line gets off by 1
732 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
735 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
736 wxCoord w
, wxCoord h
,
739 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
741 if ( !m_logicalFunctionSupported
)
745 radius
= - radius
* ((w
< h
) ? w
: h
);
747 // CMB: draw nothing if transformed w or h is 0
748 if (w
== 0 || h
== 0)
751 if ( m_graphicContext
->ShouldOffset() )
753 // if we are offsetting the entire rectangle is moved 0.5, so the
754 // border line gets off by 1
758 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
761 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
763 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
765 if ( !m_logicalFunctionSupported
)
768 if ( m_graphicContext
->ShouldOffset() )
770 // if we are offsetting the entire rectangle is moved 0.5, so the
771 // border line gets off by 1
775 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
778 bool wxGCDCImpl::CanDrawBitmap() const
783 bool wxGCDCImpl::DoBlit(
784 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
785 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
786 wxCoord xsrcMask
, wxCoord ysrcMask
)
788 return DoStretchBlit( xdest
, ydest
, width
, height
,
789 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
793 bool wxGCDCImpl::DoStretchBlit(
794 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
795 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
796 int logical_func
, bool WXUNUSED(useMask
),
797 wxCoord xsrcMask
, wxCoord ysrcMask
)
799 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
800 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
802 if ( logical_func
== wxNO_OP
)
804 else if ( !m_graphicContext
->SetLogicalFunction( logical_func
) )
807 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
811 if (xsrcMask
== -1 && ysrcMask
== -1)
817 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
818 source
->LogicalToDeviceY(ysrc
),
819 source
->LogicalToDeviceXRel(srcWidth
),
820 source
->LogicalToDeviceYRel(srcHeight
));
822 // if needed clip the subrect down to the size of the source DC
824 source
->GetSize(&sw
, &sh
);
825 sw
= source
->LogicalToDeviceXRel(sw
);
826 sh
= source
->LogicalToDeviceYRel(sh
);
827 if (subrect
.x
+ subrect
.width
> sw
)
828 subrect
.width
= sw
- subrect
.x
;
829 if (subrect
.y
+ subrect
.height
> sh
)
830 subrect
.height
= sh
- subrect
.y
;
832 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
836 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
837 dstWidth
, dstHeight
);
841 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
845 // reset logical function
846 m_graphicContext
->SetLogicalFunction( m_logicalFunction
);
851 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
854 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
856 if ( str
.length() == 0 )
858 if ( !m_logicalFunctionSupported
)
861 if ( m_backgroundMode
== wxTRANSPARENT
)
862 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
864 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
867 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
869 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
871 if ( str
.length() == 0 )
874 if ( !m_logicalFunctionSupported
)
877 if ( m_backgroundMode
== wxTRANSPARENT
)
878 m_graphicContext
->DrawText( str
, x
,y
);
880 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
883 bool wxGCDCImpl::CanGetTextExtent() const
885 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
890 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
891 wxCoord
*descent
, wxCoord
*externalLeading
,
892 const wxFont
*theFont
) const
894 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
898 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
901 wxDouble h
, d
, e
, w
;
903 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
906 *height
= (wxCoord
)(h
+0.5);
908 *descent
= (wxCoord
)(d
+0.5);
909 if ( externalLeading
)
910 *externalLeading
= (wxCoord
)(e
+0.5);
912 *width
= (wxCoord
)(w
+0.5);
916 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
920 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
922 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
924 widths
.Add(0,text
.Length());
925 if ( text
.IsEmpty() )
928 wxArrayDouble widthsD
;
930 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
931 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
932 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
937 wxCoord
wxGCDCImpl::GetCharWidth(void) const
940 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
945 wxCoord
wxGCDCImpl::GetCharHeight(void) const
948 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
953 void wxGCDCImpl::Clear(void)
955 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
956 // TODO better implementation / incorporate size info into wxGCDC or context
957 m_graphicContext
->SetBrush( m_backgroundBrush
);
958 wxPen p
= *wxTRANSPARENT_PEN
;
959 m_graphicContext
->SetPen( p
);
960 DoDrawRectangle( 0, 0, 32000 , 32000 );
961 m_graphicContext
->SetPen( m_pen
);
962 m_graphicContext
->SetBrush( m_brush
);
965 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
967 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
969 m_graphicContext
->GetSize( &w
, &h
);
971 *height
= (int) (h
+0.5);
973 *width
= (int) (w
+0.5);
976 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
977 const wxColour
& initialColour
,
978 const wxColour
& destColour
,
979 wxDirection nDirection
)
986 start
= rect
.GetRightBottom();
988 end
= rect
.GetLeftBottom();
991 start
= rect
.GetLeftBottom();
992 end
= rect
.GetRightBottom();
996 start
= rect
.GetLeftBottom();
998 end
= rect
.GetLeftTop();
1001 start
= rect
.GetLeftTop();
1002 end
= rect
.GetLeftBottom();
1009 if (rect
.width
== 0 || rect
.height
== 0)
1012 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1013 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1014 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1015 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1016 m_graphicContext
->SetPen(m_pen
);
1019 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1020 const wxColour
& initialColour
,
1021 const wxColour
& destColour
,
1022 const wxPoint
& circleCenter
)
1025 wxInt32 cx
= rect
.GetWidth() / 2;
1026 wxInt32 cy
= rect
.GetHeight() / 2;
1033 // make sure the background is filled (todo move into specific platform implementation ?)
1034 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1035 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1036 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1038 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1039 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1040 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1041 nRadius
,initialColour
,destColour
));
1043 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1044 m_graphicContext
->SetPen(m_pen
);
1047 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1048 wxCoord width
, wxCoord height
)
1050 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1053 #endif // wxUSE_GRAPHICS_CONTEXT