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"
33 #ifdef __WXOSX_OR_COCOA__
34 #ifdef __WXOSX_IPHONE__
35 #include <CoreGraphics/CoreGraphics.h>
37 #include <ApplicationServices/ApplicationServices.h>
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static const double RAD2DEG
= 180.0 / M_PI
;
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 static inline double DegToRad(double deg
)
53 return (deg
* M_PI
) / 180.0;
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
62 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
63 wxDC( new wxGCDCImpl( this, dc
) )
67 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
68 wxDC( new wxGCDCImpl( this, dc
) )
72 #if wxUSE_PRINTING_ARCHITECTURE
73 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
74 wxDC( new wxGCDCImpl( this, dc
) )
80 wxDC( new wxGCDCImpl( this ) )
88 wxGraphicsContext
* wxGCDC::GetGraphicsContext()
90 if (!m_pimpl
) return NULL
;
91 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
92 return gc_impl
->GetGraphicsContext();
95 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
98 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
99 gc_impl
->SetGraphicsContext( ctx
);
102 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
104 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
110 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
112 delete m_graphicContext
;
113 m_graphicContext
= ctx
;
114 if ( m_graphicContext
)
116 m_matrixOriginal
= m_graphicContext
->GetTransform();
118 // apply the stored transformations to the passed in context
119 ComputeScaleAndOrigin();
120 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
121 m_graphicContext
->SetPen( m_pen
);
122 m_graphicContext
->SetBrush( m_brush
);
126 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
130 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
131 m_window
= dc
.GetWindow();
134 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
138 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
141 #if wxUSE_PRINTING_ARCHITECTURE
142 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
146 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
150 void wxGCDCImpl::Init()
154 m_mm_to_pix_x
= mm2pt
;
155 m_mm_to_pix_y
= mm2pt
;
157 m_pen
= *wxBLACK_PEN
;
158 m_font
= *wxNORMAL_FONT
;
159 m_brush
= *wxWHITE_BRUSH
;
161 m_graphicContext
= NULL
;
162 m_logicalFunctionSupported
= true;
166 wxGCDCImpl::~wxGCDCImpl()
168 delete m_graphicContext
;
171 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
174 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
175 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
177 int w
= bmp
.GetWidth();
178 int h
= bmp
.GetHeight();
179 if ( bmp
.GetDepth() == 1 )
181 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
182 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
183 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
184 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
185 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
186 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
187 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
189 else // not a monochrome bitmap, handle it normally
191 // make a copy in case we need to remove its mask, if we don't modify
192 // it the copy is cheap as bitmaps are reference-counted
193 wxBitmap
bmpCopy(bmp
);
194 if ( !useMask
&& bmp
.GetMask() )
195 bmpCopy
.SetMask(NULL
);
197 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
201 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
203 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
204 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
206 wxCoord w
= icon
.GetWidth();
207 wxCoord h
= icon
.GetHeight();
209 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
212 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
217 void wxGCDCImpl::EndDoc()
221 void wxGCDCImpl::StartPage()
225 void wxGCDCImpl::EndPage()
229 void wxGCDCImpl::Flush()
231 #ifdef __WXOSX_OR_COCOA__
232 CGContextFlush( (CGContextRef
) m_graphicContext
->GetNativeContext() );
236 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
238 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
240 m_graphicContext
->Clip( x
, y
, w
, h
);
243 m_clipX1
= wxMax( m_clipX1
, x
);
244 m_clipY1
= wxMax( m_clipY1
, y
);
245 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
246 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
259 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
261 // region is in device coordinates
262 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
266 //DestroyClippingRegion();
270 wxRegion
logRegion( region
);
273 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
274 logRegion
.GetBox( x
, y
, w
, h
);
276 m_graphicContext
->Clip( logRegion
);
279 m_clipX1
= wxMax( m_clipX1
, x
);
280 m_clipY1
= wxMax( m_clipY1
, y
);
281 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
282 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
295 void wxGCDCImpl::DestroyClippingRegion()
297 m_graphicContext
->ResetClip();
298 // currently the clip eg of a window extends to the area between the scrollbars
299 // so we must explicitely make sure it only covers the area we want it to draw
301 GetOwner()->GetSize( &width
, &height
) ;
302 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
304 m_graphicContext
->SetPen( m_pen
);
305 m_graphicContext
->SetBrush( m_brush
);
310 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
314 GetOwner()->GetSize( &w
, &h
);
316 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
318 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
321 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
323 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
325 if ( col
!= m_textForegroundColour
)
327 m_textForegroundColour
= col
;
328 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
332 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
334 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
336 m_textBackgroundColour
= col
;
339 void wxGCDCImpl::SetMapMode( wxMappingMode mode
)
344 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
348 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
352 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
356 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
361 SetLogicalScale( 1.0, 1.0 );
365 ComputeScaleAndOrigin();
368 wxSize
wxGCDCImpl::GetPPI() const
370 return wxSize(72, 72);
373 int wxGCDCImpl::GetDepth() const
378 void wxGCDCImpl::ComputeScaleAndOrigin()
380 wxDCImpl::ComputeScaleAndOrigin();
382 if ( m_graphicContext
)
384 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
386 // the logical origin sets the origin to have new coordinates
387 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
388 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
390 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
392 m_graphicContext
->SetTransform( m_matrixOriginal
);
393 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
397 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
402 void wxGCDCImpl::SetBackgroundMode( int mode
)
404 m_backgroundMode
= mode
;
407 void wxGCDCImpl::SetFont( const wxFont
&font
)
410 if ( m_graphicContext
)
414 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
415 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
419 void wxGCDCImpl::SetPen( const wxPen
&pen
)
425 if ( m_graphicContext
)
427 m_graphicContext
->SetPen( m_pen
);
431 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
433 if (m_brush
== brush
)
437 if ( m_graphicContext
)
439 m_graphicContext
->SetBrush( m_brush
);
443 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
445 if (m_backgroundBrush
== brush
)
448 m_backgroundBrush
= brush
;
449 if (!m_backgroundBrush
.IsOk())
453 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
455 if (m_logicalFunction
== function
)
458 m_logicalFunction
= function
;
459 if ( m_graphicContext
->SetLogicalFunction( function
) )
460 m_logicalFunctionSupported
=true;
462 m_logicalFunctionSupported
=false;
465 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
466 const wxColour
& WXUNUSED(col
),
467 wxFloodFillStyle
WXUNUSED(style
))
472 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
474 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
478 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
480 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
482 if ( !m_logicalFunctionSupported
)
485 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
487 CalcBoundingBox(x1
, y1
);
488 CalcBoundingBox(x2
, y2
);
491 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
493 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
495 if ( !m_logicalFunctionSupported
)
500 GetOwner()->GetSize( &w
, &h
);
502 m_graphicContext
->StrokeLine(0,y
,w
,y
);
503 m_graphicContext
->StrokeLine(x
,0,x
,h
);
505 CalcBoundingBox(0, 0);
506 CalcBoundingBox(0+w
, 0+h
);
509 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
510 wxCoord x2
, wxCoord y2
,
511 wxCoord xc
, wxCoord yc
)
513 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
515 if ( !m_logicalFunctionSupported
)
520 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
521 wxCoord rad
= (wxCoord
)radius
;
523 if (x1
== x2
&& y1
== y2
)
528 else if (radius
== 0.0)
534 sa
= (x1
- xc
== 0) ?
535 (y1
- yc
< 0) ? 90.0 : -90.0 :
536 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
537 ea
= (x2
- xc
== 0) ?
538 (y2
- yc
< 0) ? 90.0 : -90.0 :
539 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
542 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
544 wxGraphicsPath path
= m_graphicContext
->CreatePath();
545 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
546 path
.MoveToPoint( xc
, yc
);
547 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
548 // get clockwise angles
549 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
550 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
551 path
.AddLineToPoint( xc
, yc
);
552 m_graphicContext
->DrawPath(path
);
555 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
556 double sa
, double ea
)
558 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
560 if ( !m_logicalFunctionSupported
)
563 m_graphicContext
->PushState();
564 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
565 wxDouble factor
= ((wxDouble
) w
) / h
;
566 m_graphicContext
->Scale( factor
, 1.0);
568 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
569 // get clockwise angles
570 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
572 wxGraphicsPath path
= m_graphicContext
->CreatePath();
573 path
.MoveToPoint( 0, 0 );
574 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
575 path
.AddLineToPoint( 0, 0 );
576 m_graphicContext
->FillPath( path
);
578 path
= m_graphicContext
->CreatePath();
579 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
580 m_graphicContext
->StrokePath( path
);
584 wxGraphicsPath path
= m_graphicContext
->CreatePath();
585 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
586 m_graphicContext
->DrawPath( path
);
589 m_graphicContext
->PopState();
592 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
594 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
596 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
599 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
600 wxCoord xoffset
, wxCoord yoffset
)
602 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
604 if ( !m_logicalFunctionSupported
)
607 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
608 for( int i
= 0; i
< n
; ++i
)
610 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
611 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
614 m_graphicContext
->StrokeLines( n
, pointsD
);
619 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
621 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
623 if ( !m_logicalFunctionSupported
)
626 wxGraphicsPath path
= m_graphicContext
->CreatePath();
628 wxPointList::compatibility_iterator node
= points
->GetFirst();
629 if (node
== wxPointList::compatibility_iterator())
633 wxPoint
*p
= node
->GetData();
638 node
= node
->GetNext();
643 wxCoord cx1
= ( x1
+ x2
) / 2;
644 wxCoord cy1
= ( y1
+ y2
) / 2;
646 path
.MoveToPoint( x1
, y1
);
647 path
.AddLineToPoint( cx1
, cy1
);
650 while ((node
= node
->GetNext()) != NULL
)
653 while ((node
= node
->GetNext()))
662 wxCoord cx4
= (x1
+ x2
) / 2;
663 wxCoord cy4
= (y1
+ y2
) / 2;
665 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
671 path
.AddLineToPoint( x2
, y2
);
673 m_graphicContext
->StrokePath( path
);
675 #endif // wxUSE_SPLINES
677 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
678 wxCoord xoffset
, wxCoord yoffset
,
679 wxPolygonFillMode fillStyle
)
681 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
683 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
685 if ( !m_logicalFunctionSupported
)
688 bool closeIt
= false;
689 if (points
[n
-1] != points
[0])
692 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
693 for( int i
= 0; i
< n
; ++i
)
695 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
696 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
699 pointsD
[n
] = pointsD
[0];
701 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
705 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
710 wxPolygonFillMode fillStyle
)
713 wxGraphicsPath path
= m_graphicContext
->CreatePath();
716 for ( int j
= 0; j
< n
; ++j
)
718 wxPoint start
= points
[i
];
719 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
722 for ( int k
= 1; k
< l
; ++k
)
724 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
728 if ( start
!= points
[i
-1])
729 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
731 m_graphicContext
->DrawPath( path
, fillStyle
);
734 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
736 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
738 if ( !m_logicalFunctionSupported
)
741 // CMB: draw nothing if transformed w or h is 0
742 if (w
== 0 || h
== 0)
745 if ( m_graphicContext
->ShouldOffset() )
747 // if we are offsetting the entire rectangle is moved 0.5, so the
748 // border line gets off by 1
752 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
755 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
756 wxCoord w
, wxCoord h
,
759 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
761 if ( !m_logicalFunctionSupported
)
765 radius
= - radius
* ((w
< h
) ? w
: h
);
767 // CMB: draw nothing if transformed w or h is 0
768 if (w
== 0 || h
== 0)
771 if ( m_graphicContext
->ShouldOffset() )
773 // if we are offsetting the entire rectangle is moved 0.5, so the
774 // border line gets off by 1
778 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
781 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
783 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
785 if ( !m_logicalFunctionSupported
)
788 if ( m_graphicContext
->ShouldOffset() )
790 // if we are offsetting the entire rectangle is moved 0.5, so the
791 // border line gets off by 1
795 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
798 bool wxGCDCImpl::CanDrawBitmap() const
803 bool wxGCDCImpl::DoBlit(
804 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
805 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
806 wxRasterOperationMode logical_func
, bool useMask
,
807 wxCoord xsrcMask
, wxCoord ysrcMask
)
809 return DoStretchBlit( xdest
, ydest
, width
, height
,
810 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
814 bool wxGCDCImpl::DoStretchBlit(
815 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
816 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
817 wxRasterOperationMode logical_func
, bool useMask
,
818 wxCoord xsrcMask
, wxCoord ysrcMask
)
820 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
821 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
823 if ( logical_func
== wxNO_OP
)
825 else if ( !m_graphicContext
->SetLogicalFunction( logical_func
) )
827 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
831 if (xsrcMask
== -1 && ysrcMask
== -1)
837 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
838 source
->LogicalToDeviceY(ysrc
),
839 source
->LogicalToDeviceXRel(srcWidth
),
840 source
->LogicalToDeviceYRel(srcHeight
));
842 // if needed clip the subrect down to the size of the source DC
844 source
->GetSize(&sw
, &sh
);
845 sw
= source
->LogicalToDeviceXRel(sw
);
846 sh
= source
->LogicalToDeviceYRel(sh
);
847 if (subrect
.x
+ subrect
.width
> sw
)
848 subrect
.width
= sw
- subrect
.x
;
849 if (subrect
.y
+ subrect
.height
> sh
)
850 subrect
.height
= sh
- subrect
.y
;
852 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
856 if ( !useMask
&& blit
.GetMask() )
859 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
860 dstWidth
, dstHeight
);
864 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
868 // reset logical function
869 m_graphicContext
->SetLogicalFunction( m_logicalFunction
);
874 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
877 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
879 if ( str
.length() == 0 )
881 if ( !m_logicalFunctionSupported
)
884 if ( m_backgroundMode
== wxTRANSPARENT
)
885 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
887 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
890 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
892 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
894 if ( str
.length() == 0 )
897 if ( !m_logicalFunctionSupported
)
900 if ( m_backgroundMode
== wxTRANSPARENT
)
901 m_graphicContext
->DrawText( str
, x
,y
);
903 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
906 bool wxGCDCImpl::CanGetTextExtent() const
908 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
913 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
914 wxCoord
*descent
, wxCoord
*externalLeading
,
915 const wxFont
*theFont
) const
917 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
921 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
924 wxDouble h
, d
, e
, w
;
926 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
929 *height
= (wxCoord
)(h
+0.5);
931 *descent
= (wxCoord
)(d
+0.5);
932 if ( externalLeading
)
933 *externalLeading
= (wxCoord
)(e
+0.5);
935 *width
= (wxCoord
)(w
+0.5);
939 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
943 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
945 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
947 widths
.Add(0,text
.Length());
948 if ( text
.IsEmpty() )
951 wxArrayDouble widthsD
;
953 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
954 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
955 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
960 wxCoord
wxGCDCImpl::GetCharWidth(void) const
963 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
968 wxCoord
wxGCDCImpl::GetCharHeight(void) const
971 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
976 void wxGCDCImpl::Clear(void)
978 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
979 // TODO better implementation / incorporate size info into wxGCDC or context
980 m_graphicContext
->SetBrush( m_backgroundBrush
);
981 wxPen p
= *wxTRANSPARENT_PEN
;
982 m_graphicContext
->SetPen( p
);
983 DoDrawRectangle( 0, 0, 32000 , 32000 );
984 m_graphicContext
->SetPen( m_pen
);
985 m_graphicContext
->SetBrush( m_brush
);
988 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
990 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
992 m_graphicContext
->GetSize( &w
, &h
);
994 *height
= (int) (h
+0.5);
996 *width
= (int) (w
+0.5);
999 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1000 const wxColour
& initialColour
,
1001 const wxColour
& destColour
,
1002 wxDirection nDirection
)
1009 start
= rect
.GetRightBottom();
1011 end
= rect
.GetLeftBottom();
1014 start
= rect
.GetLeftBottom();
1015 end
= rect
.GetRightBottom();
1019 start
= rect
.GetLeftBottom();
1021 end
= rect
.GetLeftTop();
1024 start
= rect
.GetLeftTop();
1025 end
= rect
.GetLeftBottom();
1032 if (rect
.width
== 0 || rect
.height
== 0)
1035 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1036 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1037 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1038 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1039 m_graphicContext
->SetPen(m_pen
);
1042 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1043 const wxColour
& initialColour
,
1044 const wxColour
& destColour
,
1045 const wxPoint
& circleCenter
)
1048 wxInt32 cx
= rect
.GetWidth() / 2;
1049 wxInt32 cy
= rect
.GetHeight() / 2;
1056 // make sure the background is filled (todo move into specific platform implementation ?)
1057 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1058 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1059 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1061 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1062 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1063 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1064 nRadius
,initialColour
,destColour
));
1066 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1067 m_graphicContext
->SetPen(m_pen
);
1070 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1071 wxCoord width
, wxCoord height
)
1073 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1076 #endif // wxUSE_GRAPHICS_CONTEXT