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 static bool TranslateRasterOp(wxRasterOperationMode function
, wxCompositionMode
*op
)
60 case wxCOPY
: // (default) src
61 *op
= wxCOMPOSITION_SOURCE
; //
63 case wxOR
: // src OR dst
64 *op
= wxCOMPOSITION_ADD
;
67 *op
= wxCOMPOSITION_DEST
; // ignore the source
70 *op
= wxCOMPOSITION_CLEAR
;// clear dst
72 case wxXOR
: // src XOR dst
73 *op
= wxCOMPOSITION_XOR
;
76 case wxAND
: // src AND dst
77 case wxAND_INVERT
: // (NOT src) AND dst
78 case wxAND_REVERSE
:// src AND (NOT dst)
79 case wxEQUIV
: // (NOT src) XOR dst
80 case wxINVERT
: // NOT dst
81 case wxNAND
: // (NOT src) OR (NOT dst)
82 case wxNOR
: // (NOT src) AND (NOT dst)
83 case wxOR_INVERT
: // (NOT src) OR dst
84 case wxOR_REVERSE
: // src OR (NOT dst)
86 case wxSRC_INVERT
: // NOT src
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
99 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
100 wxDC( new wxGCDCImpl( this, dc
) )
104 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
105 wxDC( new wxGCDCImpl( this, dc
) )
109 #if wxUSE_PRINTING_ARCHITECTURE
110 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
111 wxDC( new wxGCDCImpl( this, dc
) )
117 wxDC( new wxGCDCImpl( this ) )
125 wxGraphicsContext
* wxGCDC::GetGraphicsContext()
127 if (!m_pimpl
) return NULL
;
128 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
129 return gc_impl
->GetGraphicsContext();
132 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
134 if (!m_pimpl
) return;
135 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
136 gc_impl
->SetGraphicsContext( ctx
);
139 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
141 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
147 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
149 delete m_graphicContext
;
150 m_graphicContext
= ctx
;
151 if ( m_graphicContext
)
153 m_matrixOriginal
= m_graphicContext
->GetTransform();
155 // apply the stored transformations to the passed in context
156 ComputeScaleAndOrigin();
157 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
158 m_graphicContext
->SetPen( m_pen
);
159 m_graphicContext
->SetBrush( m_brush
);
163 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
167 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
168 m_window
= dc
.GetWindow();
171 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
175 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
178 #if wxUSE_PRINTING_ARCHITECTURE
179 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
183 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
187 void wxGCDCImpl::Init()
191 m_mm_to_pix_x
= mm2pt
;
192 m_mm_to_pix_y
= mm2pt
;
194 m_pen
= *wxBLACK_PEN
;
195 m_font
= *wxNORMAL_FONT
;
196 m_brush
= *wxWHITE_BRUSH
;
198 m_graphicContext
= NULL
;
199 m_logicalFunctionSupported
= true;
203 wxGCDCImpl::~wxGCDCImpl()
205 delete m_graphicContext
;
208 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
211 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
212 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
214 int w
= bmp
.GetWidth();
215 int h
= bmp
.GetHeight();
216 if ( bmp
.GetDepth() == 1 )
218 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
219 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
220 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
221 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
222 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
223 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
224 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
226 else // not a monochrome bitmap, handle it normally
228 // make a copy in case we need to remove its mask, if we don't modify
229 // it the copy is cheap as bitmaps are reference-counted
230 wxBitmap
bmpCopy(bmp
);
231 if ( !useMask
&& bmp
.GetMask() )
232 bmpCopy
.SetMask(NULL
);
234 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
238 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
240 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
241 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
243 wxCoord w
= icon
.GetWidth();
244 wxCoord h
= icon
.GetHeight();
246 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
249 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
254 void wxGCDCImpl::EndDoc()
258 void wxGCDCImpl::StartPage()
262 void wxGCDCImpl::EndPage()
266 void wxGCDCImpl::Flush()
268 #ifdef __WXOSX_OR_COCOA__
269 CGContextFlush( (CGContextRef
) m_graphicContext
->GetNativeContext() );
273 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
275 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
277 m_graphicContext
->Clip( x
, y
, w
, h
);
280 m_clipX1
= wxMax( m_clipX1
, x
);
281 m_clipY1
= wxMax( m_clipY1
, y
);
282 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
283 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
296 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
298 // region is in device coordinates
299 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
303 //DestroyClippingRegion();
307 wxRegion
logRegion( region
);
310 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
311 logRegion
.GetBox( x
, y
, w
, h
);
313 m_graphicContext
->Clip( logRegion
);
316 m_clipX1
= wxMax( m_clipX1
, x
);
317 m_clipY1
= wxMax( m_clipY1
, y
);
318 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
319 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
332 void wxGCDCImpl::DestroyClippingRegion()
334 m_graphicContext
->ResetClip();
335 // currently the clip eg of a window extends to the area between the scrollbars
336 // so we must explicitely make sure it only covers the area we want it to draw
338 GetOwner()->GetSize( &width
, &height
) ;
339 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
341 m_graphicContext
->SetPen( m_pen
);
342 m_graphicContext
->SetBrush( m_brush
);
347 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
351 GetOwner()->GetSize( &w
, &h
);
353 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
355 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
358 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
360 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
362 if ( col
!= m_textForegroundColour
)
364 m_textForegroundColour
= col
;
365 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
369 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
371 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
373 m_textBackgroundColour
= col
;
376 void wxGCDCImpl::SetMapMode( wxMappingMode mode
)
381 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
385 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
389 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
393 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
398 SetLogicalScale( 1.0, 1.0 );
402 ComputeScaleAndOrigin();
405 wxSize
wxGCDCImpl::GetPPI() const
407 return wxSize(72, 72);
410 int wxGCDCImpl::GetDepth() const
415 void wxGCDCImpl::ComputeScaleAndOrigin()
417 wxDCImpl::ComputeScaleAndOrigin();
419 if ( m_graphicContext
)
421 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
423 // the logical origin sets the origin to have new coordinates
424 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
425 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
427 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
429 m_graphicContext
->SetTransform( m_matrixOriginal
);
430 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
434 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
439 void wxGCDCImpl::SetBackgroundMode( int mode
)
441 m_backgroundMode
= mode
;
444 void wxGCDCImpl::SetFont( const wxFont
&font
)
447 if ( m_graphicContext
)
451 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
452 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
456 void wxGCDCImpl::SetPen( const wxPen
&pen
)
462 if ( m_graphicContext
)
464 m_graphicContext
->SetPen( m_pen
);
468 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
470 if (m_brush
== brush
)
474 if ( m_graphicContext
)
476 m_graphicContext
->SetBrush( m_brush
);
480 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
482 if (m_backgroundBrush
== brush
)
485 m_backgroundBrush
= brush
;
486 if (!m_backgroundBrush
.IsOk())
490 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
492 if (m_logicalFunction
== function
)
495 m_logicalFunction
= function
;
497 wxCompositionMode mode
;
498 m_logicalFunctionSupported
= TranslateRasterOp( function
, &mode
);
499 if (m_logicalFunctionSupported
)
500 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
502 if (mode
== wxCOMPOSITION_XOR
)
503 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
505 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
508 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
509 const wxColour
& WXUNUSED(col
),
510 wxFloodFillStyle
WXUNUSED(style
))
515 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
517 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
521 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
523 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
525 if ( !m_logicalFunctionSupported
)
528 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
530 CalcBoundingBox(x1
, y1
);
531 CalcBoundingBox(x2
, y2
);
534 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
536 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
538 if ( !m_logicalFunctionSupported
)
543 GetOwner()->GetSize( &w
, &h
);
545 m_graphicContext
->StrokeLine(0,y
,w
,y
);
546 m_graphicContext
->StrokeLine(x
,0,x
,h
);
548 CalcBoundingBox(0, 0);
549 CalcBoundingBox(0+w
, 0+h
);
552 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
553 wxCoord x2
, wxCoord y2
,
554 wxCoord xc
, wxCoord yc
)
556 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
558 if ( !m_logicalFunctionSupported
)
563 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
564 wxCoord rad
= (wxCoord
)radius
;
566 if (x1
== x2
&& y1
== y2
)
571 else if (radius
== 0.0)
577 sa
= (x1
- xc
== 0) ?
578 (y1
- yc
< 0) ? 90.0 : -90.0 :
579 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
580 ea
= (x2
- xc
== 0) ?
581 (y2
- yc
< 0) ? 90.0 : -90.0 :
582 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
585 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
587 wxGraphicsPath path
= m_graphicContext
->CreatePath();
588 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
589 path
.MoveToPoint( xc
, yc
);
590 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
591 // get clockwise angles
592 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
593 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
594 path
.AddLineToPoint( xc
, yc
);
595 m_graphicContext
->DrawPath(path
);
598 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
599 double sa
, double ea
)
601 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
603 if ( !m_logicalFunctionSupported
)
606 m_graphicContext
->PushState();
607 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
608 wxDouble factor
= ((wxDouble
) w
) / h
;
609 m_graphicContext
->Scale( factor
, 1.0);
611 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
612 // get clockwise angles
613 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
615 wxGraphicsPath path
= m_graphicContext
->CreatePath();
616 path
.MoveToPoint( 0, 0 );
617 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
618 path
.AddLineToPoint( 0, 0 );
619 m_graphicContext
->FillPath( path
);
621 path
= m_graphicContext
->CreatePath();
622 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
623 m_graphicContext
->StrokePath( path
);
627 wxGraphicsPath path
= m_graphicContext
->CreatePath();
628 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
629 m_graphicContext
->DrawPath( path
);
632 m_graphicContext
->PopState();
635 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
637 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
639 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
642 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
643 wxCoord xoffset
, wxCoord yoffset
)
645 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
647 if ( !m_logicalFunctionSupported
)
650 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
651 for( int i
= 0; i
< n
; ++i
)
653 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
654 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
657 m_graphicContext
->StrokeLines( n
, pointsD
);
662 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
664 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
666 if ( !m_logicalFunctionSupported
)
669 wxGraphicsPath path
= m_graphicContext
->CreatePath();
671 wxPointList::compatibility_iterator node
= points
->GetFirst();
672 if (node
== wxPointList::compatibility_iterator())
676 wxPoint
*p
= node
->GetData();
681 node
= node
->GetNext();
686 wxCoord cx1
= ( x1
+ x2
) / 2;
687 wxCoord cy1
= ( y1
+ y2
) / 2;
689 path
.MoveToPoint( x1
, y1
);
690 path
.AddLineToPoint( cx1
, cy1
);
693 while ((node
= node
->GetNext()) != NULL
)
696 while ((node
= node
->GetNext()))
705 wxCoord cx4
= (x1
+ x2
) / 2;
706 wxCoord cy4
= (y1
+ y2
) / 2;
708 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
714 path
.AddLineToPoint( x2
, y2
);
716 m_graphicContext
->StrokePath( path
);
718 #endif // wxUSE_SPLINES
720 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
721 wxCoord xoffset
, wxCoord yoffset
,
722 wxPolygonFillMode fillStyle
)
724 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
726 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
728 if ( !m_logicalFunctionSupported
)
731 bool closeIt
= false;
732 if (points
[n
-1] != points
[0])
735 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
736 for( int i
= 0; i
< n
; ++i
)
738 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
739 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
742 pointsD
[n
] = pointsD
[0];
744 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
748 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
753 wxPolygonFillMode fillStyle
)
756 wxGraphicsPath path
= m_graphicContext
->CreatePath();
759 for ( int j
= 0; j
< n
; ++j
)
761 wxPoint start
= points
[i
];
762 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
765 for ( int k
= 1; k
< l
; ++k
)
767 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
771 if ( start
!= points
[i
-1])
772 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
774 m_graphicContext
->DrawPath( path
, fillStyle
);
777 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
779 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
781 if ( !m_logicalFunctionSupported
)
784 // CMB: draw nothing if transformed w or h is 0
785 if (w
== 0 || h
== 0)
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
->DrawRectangle(x
,y
,w
,h
);
798 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
799 wxCoord w
, wxCoord h
,
802 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
804 if ( !m_logicalFunctionSupported
)
808 radius
= - radius
* ((w
< h
) ? w
: h
);
810 // CMB: draw nothing if transformed w or h is 0
811 if (w
== 0 || h
== 0)
814 if ( m_graphicContext
->ShouldOffset() )
816 // if we are offsetting the entire rectangle is moved 0.5, so the
817 // border line gets off by 1
821 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
824 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
826 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
828 if ( !m_logicalFunctionSupported
)
831 if ( m_graphicContext
->ShouldOffset() )
833 // if we are offsetting the entire rectangle is moved 0.5, so the
834 // border line gets off by 1
838 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
841 bool wxGCDCImpl::CanDrawBitmap() const
846 bool wxGCDCImpl::DoBlit(
847 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
848 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
849 wxRasterOperationMode logical_func
, bool useMask
,
850 wxCoord xsrcMask
, wxCoord ysrcMask
)
852 return DoStretchBlit( xdest
, ydest
, width
, height
,
853 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
857 bool wxGCDCImpl::DoStretchBlit(
858 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
859 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
860 wxRasterOperationMode logical_func
, bool useMask
,
861 wxCoord xsrcMask
, wxCoord ysrcMask
)
863 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
864 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
866 if ( logical_func
== wxNO_OP
)
869 wxCompositionMode mode
;
870 if ( !TranslateRasterOp(logical_func
, &mode
) )
872 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
878 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
879 if (m_graphicContext
->SetCompositionMode(mode
))
881 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
882 if (mode
== wxCOMPOSITION_XOR
)
884 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
887 if (xsrcMask
== -1 && ysrcMask
== -1)
893 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
894 source
->LogicalToDeviceY(ysrc
),
895 source
->LogicalToDeviceXRel(srcWidth
),
896 source
->LogicalToDeviceYRel(srcHeight
));
898 // if needed clip the subrect down to the size of the source DC
900 source
->GetSize(&sw
, &sh
);
901 sw
= source
->LogicalToDeviceXRel(sw
);
902 sh
= source
->LogicalToDeviceYRel(sh
);
903 if (subrect
.x
+ subrect
.width
> sw
)
904 subrect
.width
= sw
- subrect
.x
;
905 if (subrect
.y
+ subrect
.height
> sh
)
906 subrect
.height
= sh
- subrect
.y
;
908 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
912 if ( !useMask
&& blit
.GetMask() )
915 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
916 dstWidth
, dstHeight
);
920 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
924 if (mode
== wxCOMPOSITION_XOR
)
926 m_graphicContext
->SetAntialiasMode(formerAa
);
930 m_graphicContext
->SetCompositionMode(formerMode
);
935 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
938 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
940 if ( str
.length() == 0 )
942 if ( !m_logicalFunctionSupported
)
945 if ( m_backgroundMode
== wxTRANSPARENT
)
946 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
948 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
951 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
953 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
955 if ( str
.length() == 0 )
958 if ( !m_logicalFunctionSupported
)
961 if ( m_backgroundMode
== wxTRANSPARENT
)
962 m_graphicContext
->DrawText( str
, x
,y
);
964 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
967 bool wxGCDCImpl::CanGetTextExtent() const
969 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
974 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
975 wxCoord
*descent
, wxCoord
*externalLeading
,
976 const wxFont
*theFont
) const
978 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
982 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
985 wxDouble h
, d
, e
, w
;
987 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
990 *height
= (wxCoord
)(h
+0.5);
992 *descent
= (wxCoord
)(d
+0.5);
993 if ( externalLeading
)
994 *externalLeading
= (wxCoord
)(e
+0.5);
996 *width
= (wxCoord
)(w
+0.5);
1000 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
1004 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1006 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1008 widths
.Add(0,text
.Length());
1009 if ( text
.IsEmpty() )
1012 wxArrayDouble widthsD
;
1014 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1015 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1016 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1021 wxCoord
wxGCDCImpl::GetCharWidth(void) const
1024 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1029 wxCoord
wxGCDCImpl::GetCharHeight(void) const
1032 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1037 void wxGCDCImpl::Clear(void)
1039 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1040 // TODO better implementation / incorporate size info into wxGCDC or context
1041 m_graphicContext
->SetBrush( m_backgroundBrush
);
1042 wxPen p
= *wxTRANSPARENT_PEN
;
1043 m_graphicContext
->SetPen( p
);
1044 DoDrawRectangle( 0, 0, 32000 , 32000 );
1045 m_graphicContext
->SetPen( m_pen
);
1046 m_graphicContext
->SetBrush( m_brush
);
1049 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
1051 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
1053 m_graphicContext
->GetSize( &w
, &h
);
1055 *height
= (int) (h
+0.5);
1057 *width
= (int) (w
+0.5);
1060 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1061 const wxColour
& initialColour
,
1062 const wxColour
& destColour
,
1063 wxDirection nDirection
)
1070 start
= rect
.GetRightBottom();
1072 end
= rect
.GetLeftBottom();
1075 start
= rect
.GetLeftBottom();
1076 end
= rect
.GetRightBottom();
1080 start
= rect
.GetLeftBottom();
1082 end
= rect
.GetLeftTop();
1085 start
= rect
.GetLeftTop();
1086 end
= rect
.GetLeftBottom();
1093 if (rect
.width
== 0 || rect
.height
== 0)
1096 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1097 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1098 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1099 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1100 m_graphicContext
->SetPen(m_pen
);
1103 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1104 const wxColour
& initialColour
,
1105 const wxColour
& destColour
,
1106 const wxPoint
& circleCenter
)
1109 wxInt32 cx
= rect
.GetWidth() / 2;
1110 wxInt32 cy
= rect
.GetHeight() / 2;
1117 // make sure the background is filled (todo move into specific platform implementation ?)
1118 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1119 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1120 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1122 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1123 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1124 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1125 nRadius
,initialColour
,destColour
));
1127 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1128 m_graphicContext
->SetPen(m_pen
);
1131 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1132 wxCoord width
, wxCoord height
)
1134 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1137 #endif // wxUSE_GRAPHICS_CONTEXT