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
) )
69 wxDC( new wxGCDCImpl( this ) )
77 wxGraphicsContext
* wxGCDC::GetGraphicsContext()
79 if (!m_pimpl
) return NULL
;
80 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
81 return gc_impl
->GetGraphicsContext();
84 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
87 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
88 gc_impl
->SetGraphicsContext( ctx
);
91 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
93 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
99 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
101 delete m_graphicContext
;
102 m_graphicContext
= ctx
;
103 if ( m_graphicContext
)
105 m_matrixOriginal
= m_graphicContext
->GetTransform();
107 // apply the stored transformations to the passed in context
108 ComputeScaleAndOrigin();
109 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
110 m_graphicContext
->SetPen( m_pen
);
111 m_graphicContext
->SetBrush( m_brush
);
115 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
119 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
120 m_window
= dc
.GetWindow();
123 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
127 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
130 void wxGCDCImpl::Init()
134 m_mm_to_pix_x
= mm2pt
;
135 m_mm_to_pix_y
= mm2pt
;
137 m_pen
= *wxBLACK_PEN
;
138 m_font
= *wxNORMAL_FONT
;
139 m_brush
= *wxWHITE_BRUSH
;
141 m_graphicContext
= NULL
;
142 m_logicalFunctionSupported
= true;
146 wxGCDCImpl::~wxGCDCImpl()
148 delete m_graphicContext
;
151 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
153 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
154 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
156 if ( bmp
.GetDepth() == 1 )
158 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
159 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
160 m_graphicContext
->DrawRectangle( x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
161 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
162 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
163 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
164 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
167 m_graphicContext
->DrawBitmap( bmp
, x
, y
, bmp
.GetWidth() , bmp
.GetHeight() );
170 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
172 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
173 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
175 wxCoord w
= icon
.GetWidth();
176 wxCoord h
= icon
.GetHeight();
178 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
181 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
186 void wxGCDCImpl::EndDoc()
190 void wxGCDCImpl::StartPage()
194 void wxGCDCImpl::EndPage()
198 void wxGCDCImpl::Flush()
201 CGContextFlush( (CGContextRef
) m_graphicContext
->GetNativeContext() );
205 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
207 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
209 m_graphicContext
->Clip( x
, y
, w
, h
);
212 m_clipX1
= wxMax( m_clipX1
, x
);
213 m_clipY1
= wxMax( m_clipY1
, y
);
214 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
215 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
228 void wxGCDCImpl::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
230 // region is in device coordinates
231 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
235 //DestroyClippingRegion();
239 wxRegion
logRegion( region
);
242 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
243 logRegion
.GetBox( x
, y
, w
, h
);
245 m_graphicContext
->Clip( logRegion
);
248 m_clipX1
= wxMax( m_clipX1
, x
);
249 m_clipY1
= wxMax( m_clipY1
, y
);
250 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
251 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
264 void wxGCDCImpl::DestroyClippingRegion()
266 m_graphicContext
->ResetClip();
267 // currently the clip eg of a window extends to the area between the scrollbars
268 // so we must explicitely make sure it only covers the area we want it to draw
270 GetOwner()->GetSize( &width
, &height
) ;
271 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
273 m_graphicContext
->SetPen( m_pen
);
274 m_graphicContext
->SetBrush( m_brush
);
279 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
283 GetOwner()->GetSize( &w
, &h
);
285 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
287 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
290 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
292 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
294 if ( col
!= m_textForegroundColour
)
296 m_textForegroundColour
= col
;
297 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
301 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
303 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
305 m_textBackgroundColour
= col
;
308 void wxGCDCImpl::SetMapMode( int mode
)
313 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
317 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
321 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
325 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
330 SetLogicalScale( 1.0, 1.0 );
334 ComputeScaleAndOrigin();
337 wxSize
wxGCDCImpl::GetPPI() const
339 return wxSize(72, 72);
342 int wxGCDCImpl::GetDepth() const
347 void wxGCDCImpl::ComputeScaleAndOrigin()
349 wxDCImpl::ComputeScaleAndOrigin();
351 if ( m_graphicContext
)
353 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
354 m_matrixCurrent
.Translate( m_deviceOriginX
, m_deviceOriginY
);
355 m_matrixCurrent
.Scale( m_scaleX
, m_scaleY
);
356 // the logical origin sets the origin to have new coordinates
357 m_matrixCurrent
.Translate( -m_logicalOriginX
, -m_logicalOriginY
);
359 m_graphicContext
->SetTransform( m_matrixOriginal
);
360 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
364 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
369 void wxGCDCImpl::SetBackgroundMode( int mode
)
371 m_backgroundMode
= mode
;
374 void wxGCDCImpl::SetFont( const wxFont
&font
)
377 if ( m_graphicContext
)
381 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
382 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
386 void wxGCDCImpl::SetPen( const wxPen
&pen
)
392 if ( m_graphicContext
)
394 m_graphicContext
->SetPen( m_pen
);
398 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
400 if (m_brush
== brush
)
404 if ( m_graphicContext
)
406 m_graphicContext
->SetBrush( m_brush
);
410 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
412 if (m_backgroundBrush
== brush
)
415 m_backgroundBrush
= brush
;
416 if (!m_backgroundBrush
.IsOk())
420 void wxGCDCImpl::SetLogicalFunction( int function
)
422 if (m_logicalFunction
== function
)
425 m_logicalFunction
= function
;
426 if ( m_graphicContext
->SetLogicalFunction( function
) )
427 m_logicalFunctionSupported
=true;
429 m_logicalFunctionSupported
=false;
432 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
433 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
))
438 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
440 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
444 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
446 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
448 if ( !m_logicalFunctionSupported
)
451 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
453 CalcBoundingBox(x1
, y1
);
454 CalcBoundingBox(x2
, y2
);
457 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
459 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
461 if ( !m_logicalFunctionSupported
)
466 GetOwner()->GetSize( &w
, &h
);
468 m_graphicContext
->StrokeLine(0,y
,w
,y
);
469 m_graphicContext
->StrokeLine(x
,0,x
,h
);
471 CalcBoundingBox(0, 0);
472 CalcBoundingBox(0+w
, 0+h
);
475 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
476 wxCoord x2
, wxCoord y2
,
477 wxCoord xc
, wxCoord yc
)
479 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
481 if ( !m_logicalFunctionSupported
)
486 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
487 wxCoord rad
= (wxCoord
)radius
;
489 if (x1
== x2
&& y1
== y2
)
494 else if (radius
== 0.0)
500 sa
= (x1
- xc
== 0) ?
501 (y1
- yc
< 0) ? 90.0 : -90.0 :
502 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
503 ea
= (x2
- xc
== 0) ?
504 (y2
- yc
< 0) ? 90.0 : -90.0 :
505 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
508 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
510 wxGraphicsPath path
= m_graphicContext
->CreatePath();
511 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
512 path
.MoveToPoint( xc
, yc
);
513 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
514 // get clockwise angles
515 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
516 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
517 path
.AddLineToPoint( xc
, yc
);
518 m_graphicContext
->DrawPath(path
);
521 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
522 double sa
, double ea
)
524 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
526 if ( !m_logicalFunctionSupported
)
529 m_graphicContext
->PushState();
530 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
531 wxDouble factor
= ((wxDouble
) w
) / h
;
532 m_graphicContext
->Scale( factor
, 1.0);
534 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
535 // get clockwise angles
536 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
538 wxGraphicsPath path
= m_graphicContext
->CreatePath();
539 path
.MoveToPoint( 0, 0 );
540 path
.AddLineToPoint( h
/ 2.0 * cos(DegToRad(sa
)) , h
/ 2.0 * sin(DegToRad(-sa
)) );
541 path
.AddLineToPoint( h
/ 2.0 * cos(DegToRad(ea
)) , h
/ 2.0 * sin(DegToRad(-ea
)) );
542 path
.AddLineToPoint( 0, 0 );
543 m_graphicContext
->FillPath( path
);
545 path
= m_graphicContext
->CreatePath();
546 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
547 m_graphicContext
->FillPath( path
);
548 m_graphicContext
->StrokePath( path
);
552 wxGraphicsPath path
= m_graphicContext
->CreatePath();
553 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
554 m_graphicContext
->DrawPath( path
);
557 m_graphicContext
->PopState();
560 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
562 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
564 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
567 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
568 wxCoord xoffset
, wxCoord yoffset
)
570 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
572 if ( !m_logicalFunctionSupported
)
575 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
576 for( int i
= 0; i
< n
; ++i
)
578 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
579 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
582 m_graphicContext
->StrokeLines( n
, pointsD
);
587 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
589 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
591 if ( !m_logicalFunctionSupported
)
594 wxGraphicsPath path
= m_graphicContext
->CreatePath();
596 wxPointList::compatibility_iterator node
= points
->GetFirst();
597 if (node
== wxPointList::compatibility_iterator())
601 wxPoint
*p
= node
->GetData();
606 node
= node
->GetNext();
611 wxCoord cx1
= ( x1
+ x2
) / 2;
612 wxCoord cy1
= ( y1
+ y2
) / 2;
614 path
.MoveToPoint( x1
, y1
);
615 path
.AddLineToPoint( cx1
, cy1
);
618 while ((node
= node
->GetNext()) != NULL
)
621 while ((node
= node
->GetNext()))
630 wxCoord cx4
= (x1
+ x2
) / 2;
631 wxCoord cy4
= (y1
+ y2
) / 2;
633 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
639 path
.AddLineToPoint( x2
, y2
);
641 m_graphicContext
->StrokePath( path
);
643 #endif // wxUSE_SPLINES
645 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
646 wxCoord xoffset
, wxCoord yoffset
,
649 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
651 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
653 if ( !m_logicalFunctionSupported
)
656 bool closeIt
= false;
657 if (points
[n
-1] != points
[0])
660 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
661 for( int i
= 0; i
< n
; ++i
)
663 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
664 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
667 pointsD
[n
] = pointsD
[0];
669 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
673 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
681 wxGraphicsPath path
= m_graphicContext
->CreatePath();
684 for ( int j
= 0; j
< n
; ++j
)
686 wxPoint start
= points
[i
];
687 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
690 for ( int k
= 1; k
< l
; ++k
)
692 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
696 if ( start
!= points
[i
-1])
697 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
699 m_graphicContext
->DrawPath( path
, fillStyle
);
702 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
704 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
706 if ( !m_logicalFunctionSupported
)
709 // CMB: draw nothing if transformed w or h is 0
710 if (w
== 0 || h
== 0)
713 if ( m_graphicContext
->ShouldOffset() )
715 // if we are offsetting the entire rectangle is moved 0.5, so the
716 // border line gets off by 1
720 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
723 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
724 wxCoord w
, wxCoord h
,
727 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
729 if ( !m_logicalFunctionSupported
)
733 radius
= - radius
* ((w
< h
) ? w
: h
);
735 // CMB: draw nothing if transformed w or h is 0
736 if (w
== 0 || h
== 0)
739 if ( m_graphicContext
->ShouldOffset() )
741 // if we are offsetting the entire rectangle is moved 0.5, so the
742 // border line gets off by 1
746 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
749 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
751 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
753 if ( !m_logicalFunctionSupported
)
756 if ( m_graphicContext
->ShouldOffset() )
758 // if we are offsetting the entire rectangle is moved 0.5, so the
759 // border line gets off by 1
763 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
766 bool wxGCDCImpl::CanDrawBitmap() const
771 bool wxGCDCImpl::DoBlit(
772 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
773 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
774 wxCoord xsrcMask
, wxCoord ysrcMask
)
776 return DoStretchBlit( xdest
, ydest
, width
, height
,
777 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
781 bool wxGCDCImpl::DoStretchBlit(
782 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
783 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
784 int logical_func
, bool WXUNUSED(useMask
),
785 wxCoord xsrcMask
, wxCoord ysrcMask
)
787 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
788 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
790 if ( logical_func
== wxNO_OP
)
792 else if ( !m_graphicContext
->SetLogicalFunction( logical_func
) )
795 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
799 if (xsrcMask
== -1 && ysrcMask
== -1)
805 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
806 source
->LogicalToDeviceY(ysrc
),
807 source
->LogicalToDeviceXRel(srcWidth
),
808 source
->LogicalToDeviceYRel(srcHeight
));
810 // if needed clip the subrect down to the size of the source DC
812 source
->GetSize(&sw
, &sh
);
813 sw
= source
->LogicalToDeviceXRel(sw
);
814 sh
= source
->LogicalToDeviceYRel(sh
);
815 if (subrect
.x
+ subrect
.width
> sw
)
816 subrect
.width
= sw
- subrect
.x
;
817 if (subrect
.y
+ subrect
.height
> sh
)
818 subrect
.height
= sh
- subrect
.y
;
820 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
824 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
825 dstWidth
, dstHeight
);
829 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
833 // reset logical function
834 m_graphicContext
->SetLogicalFunction( m_logicalFunction
);
839 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
842 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
844 if ( str
.length() == 0 )
846 if ( !m_logicalFunctionSupported
)
849 if ( m_backgroundMode
== wxTRANSPARENT
)
850 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
852 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
855 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
857 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
859 if ( str
.length() == 0 )
862 if ( !m_logicalFunctionSupported
)
865 if ( m_backgroundMode
== wxTRANSPARENT
)
866 m_graphicContext
->DrawText( str
, x
,y
);
868 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
871 bool wxGCDCImpl::CanGetTextExtent() const
873 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
878 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
879 wxCoord
*descent
, wxCoord
*externalLeading
,
880 const wxFont
*theFont
) const
882 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
886 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
889 wxDouble h
, d
, e
, w
;
891 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
894 *height
= (wxCoord
)(h
+0.5);
896 *descent
= (wxCoord
)(d
+0.5);
897 if ( externalLeading
)
898 *externalLeading
= (wxCoord
)(e
+0.5);
900 *width
= (wxCoord
)(w
+0.5);
904 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
908 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
910 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
912 widths
.Add(0,text
.Length());
913 if ( text
.IsEmpty() )
916 wxArrayDouble widthsD
;
918 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
919 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
920 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
925 wxCoord
wxGCDCImpl::GetCharWidth(void) const
928 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
933 wxCoord
wxGCDCImpl::GetCharHeight(void) const
936 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
941 void wxGCDCImpl::Clear(void)
943 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
944 // TODO better implementation / incorporate size info into wxGCDC or context
945 m_graphicContext
->SetBrush( m_backgroundBrush
);
946 wxPen p
= *wxTRANSPARENT_PEN
;
947 m_graphicContext
->SetPen( p
);
948 DoDrawRectangle( 0, 0, 32000 , 32000 );
949 m_graphicContext
->SetPen( m_pen
);
950 m_graphicContext
->SetBrush( m_brush
);
953 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
955 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
957 m_graphicContext
->GetSize( &w
, &h
);
959 *height
= (int) (h
+0.5);
961 *width
= (int) (w
+0.5);
964 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
965 const wxColour
& initialColour
,
966 const wxColour
& destColour
,
967 wxDirection nDirection
)
974 start
= rect
.GetRightBottom();
976 end
= rect
.GetLeftBottom();
979 start
= rect
.GetLeftBottom();
980 end
= rect
.GetRightBottom();
984 start
= rect
.GetLeftBottom();
986 end
= rect
.GetLeftTop();
989 start
= rect
.GetLeftTop();
990 end
= rect
.GetLeftBottom();
997 if (rect
.width
== 0 || rect
.height
== 0)
1000 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1001 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1002 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1003 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1004 m_graphicContext
->SetPen(m_pen
);
1007 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1008 const wxColour
& initialColour
,
1009 const wxColour
& destColour
,
1010 const wxPoint
& circleCenter
)
1013 wxInt32 cx
= rect
.GetWidth() / 2;
1014 wxInt32 cy
= rect
.GetHeight() / 2;
1021 // make sure the background is filled (todo move into specific platform implementation ?)
1022 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1023 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1024 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1026 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1027 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1028 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1029 nRadius
,initialColour
,destColour
));
1031 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1032 m_graphicContext
->SetPen(m_pen
);
1035 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1036 wxCoord width
, wxCoord height
)
1038 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1041 #endif // wxUSE_GRAPHICS_CONTEXT