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 wxGraphicsContext
* context
;
177 wxGraphicsRenderer
* renderer
= wxGraphicsRenderer::GetCairoRenderer();
178 context
= renderer
->CreateContext(dc
);
180 context
= wxGraphicsContext::Create(dc
);
183 SetGraphicsContext( context
);
186 #if wxUSE_PRINTING_ARCHITECTURE
187 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
191 SetGraphicsContext( wxGraphicsContext::Create(dc
) );
195 void wxGCDCImpl::Init()
199 m_mm_to_pix_x
= mm2pt
;
200 m_mm_to_pix_y
= mm2pt
;
202 m_pen
= *wxBLACK_PEN
;
203 m_font
= *wxNORMAL_FONT
;
204 m_brush
= *wxWHITE_BRUSH
;
206 m_graphicContext
= wxGraphicsContext::Create();
207 m_logicalFunctionSupported
= true;
211 wxGCDCImpl::~wxGCDCImpl()
213 delete m_graphicContext
;
216 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
219 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
220 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
222 int w
= bmp
.GetWidth();
223 int h
= bmp
.GetHeight();
224 if ( bmp
.GetDepth() == 1 )
226 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
227 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
228 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
229 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
230 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
231 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
232 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
234 else // not a monochrome bitmap, handle it normally
236 // make a copy in case we need to remove its mask, if we don't modify
237 // it the copy is cheap as bitmaps are reference-counted
238 wxBitmap
bmpCopy(bmp
);
239 if ( !useMask
&& bmp
.GetMask() )
240 bmpCopy
.SetMask(NULL
);
242 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
246 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
248 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
249 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
251 wxCoord w
= icon
.GetWidth();
252 wxCoord h
= icon
.GetHeight();
254 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
257 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
262 void wxGCDCImpl::EndDoc()
266 void wxGCDCImpl::StartPage()
270 void wxGCDCImpl::EndPage()
274 void wxGCDCImpl::Flush()
276 m_graphicContext
->Flush();
279 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
281 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
283 m_graphicContext
->Clip( x
, y
, w
, h
);
286 m_clipX1
= wxMax( m_clipX1
, x
);
287 m_clipY1
= wxMax( m_clipY1
, y
);
288 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
289 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
302 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
304 // region is in device coordinates
305 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
309 //DestroyClippingRegion();
313 wxRegion
logRegion( region
);
316 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
317 logRegion
.GetBox( x
, y
, w
, h
);
319 m_graphicContext
->Clip( logRegion
);
322 m_clipX1
= wxMax( m_clipX1
, x
);
323 m_clipY1
= wxMax( m_clipY1
, y
);
324 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
325 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
338 void wxGCDCImpl::DestroyClippingRegion()
340 m_graphicContext
->ResetClip();
341 // currently the clip eg of a window extends to the area between the scrollbars
342 // so we must explicitely make sure it only covers the area we want it to draw
344 GetOwner()->GetSize( &width
, &height
) ;
345 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
347 m_graphicContext
->SetPen( m_pen
);
348 m_graphicContext
->SetBrush( m_brush
);
353 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
357 GetOwner()->GetSize( &w
, &h
);
359 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
361 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
364 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
366 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
368 if ( col
!= m_textForegroundColour
)
370 m_textForegroundColour
= col
;
371 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
375 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
377 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
379 m_textBackgroundColour
= col
;
382 void wxGCDCImpl::SetMapMode( wxMappingMode mode
)
387 SetLogicalScale( twips2mm
* m_mm_to_pix_x
, twips2mm
* m_mm_to_pix_y
);
391 SetLogicalScale( pt2mm
* m_mm_to_pix_x
, pt2mm
* m_mm_to_pix_y
);
395 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
399 SetLogicalScale( m_mm_to_pix_x
/ 10.0, m_mm_to_pix_y
/ 10.0 );
404 SetLogicalScale( 1.0, 1.0 );
408 ComputeScaleAndOrigin();
411 wxSize
wxGCDCImpl::GetPPI() const
413 return wxSize(72, 72);
416 int wxGCDCImpl::GetDepth() const
421 void wxGCDCImpl::ComputeScaleAndOrigin()
423 wxDCImpl::ComputeScaleAndOrigin();
425 if ( m_graphicContext
)
427 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
429 // the logical origin sets the origin to have new coordinates
430 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
431 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
433 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
435 m_graphicContext
->SetTransform( m_matrixOriginal
);
436 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
440 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
445 void wxGCDCImpl::SetBackgroundMode( int mode
)
447 m_backgroundMode
= mode
;
450 void wxGCDCImpl::SetFont( const wxFont
&font
)
453 if ( m_graphicContext
)
457 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
458 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
462 void wxGCDCImpl::SetPen( const wxPen
&pen
)
468 if ( m_graphicContext
)
470 m_graphicContext
->SetPen( m_pen
);
474 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
476 if (m_brush
== brush
)
480 if ( m_graphicContext
)
482 m_graphicContext
->SetBrush( m_brush
);
486 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
488 if (m_backgroundBrush
== brush
)
491 m_backgroundBrush
= brush
;
492 if (!m_backgroundBrush
.IsOk())
496 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
498 if (m_logicalFunction
== function
)
501 m_logicalFunction
= function
;
503 wxCompositionMode mode
;
504 m_logicalFunctionSupported
= TranslateRasterOp( function
, &mode
);
505 if (m_logicalFunctionSupported
)
506 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
508 if (mode
== wxCOMPOSITION_XOR
)
509 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
511 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
514 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
515 const wxColour
& WXUNUSED(col
),
516 wxFloodFillStyle
WXUNUSED(style
))
521 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
523 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
527 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
529 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
531 if ( !m_logicalFunctionSupported
)
534 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
536 CalcBoundingBox(x1
, y1
);
537 CalcBoundingBox(x2
, y2
);
540 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
542 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
544 if ( !m_logicalFunctionSupported
)
549 GetOwner()->GetSize( &w
, &h
);
551 m_graphicContext
->StrokeLine(0,y
,w
,y
);
552 m_graphicContext
->StrokeLine(x
,0,x
,h
);
554 CalcBoundingBox(0, 0);
555 CalcBoundingBox(0+w
, 0+h
);
558 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
559 wxCoord x2
, wxCoord y2
,
560 wxCoord xc
, wxCoord yc
)
562 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
564 if ( !m_logicalFunctionSupported
)
569 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
570 wxCoord rad
= (wxCoord
)radius
;
572 if (x1
== x2
&& y1
== y2
)
577 else if (radius
== 0.0)
583 sa
= (x1
- xc
== 0) ?
584 (y1
- yc
< 0) ? 90.0 : -90.0 :
585 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
586 ea
= (x2
- xc
== 0) ?
587 (y2
- yc
< 0) ? 90.0 : -90.0 :
588 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
591 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
593 wxGraphicsPath path
= m_graphicContext
->CreatePath();
594 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
595 path
.MoveToPoint( xc
, yc
);
596 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
597 // get clockwise angles
598 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
599 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
600 path
.AddLineToPoint( xc
, yc
);
601 m_graphicContext
->DrawPath(path
);
604 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
605 double sa
, double ea
)
607 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
609 if ( !m_logicalFunctionSupported
)
612 m_graphicContext
->PushState();
613 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
614 wxDouble factor
= ((wxDouble
) w
) / h
;
615 m_graphicContext
->Scale( factor
, 1.0);
617 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
618 // get clockwise angles
619 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
621 wxGraphicsPath path
= m_graphicContext
->CreatePath();
622 path
.MoveToPoint( 0, 0 );
623 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
624 path
.AddLineToPoint( 0, 0 );
625 m_graphicContext
->FillPath( path
);
627 path
= m_graphicContext
->CreatePath();
628 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
629 m_graphicContext
->StrokePath( path
);
633 wxGraphicsPath path
= m_graphicContext
->CreatePath();
634 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
635 m_graphicContext
->DrawPath( path
);
638 m_graphicContext
->PopState();
641 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
643 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
645 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
648 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
649 wxCoord xoffset
, wxCoord yoffset
)
651 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
653 if ( !m_logicalFunctionSupported
)
656 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
657 for( int i
= 0; i
< n
; ++i
)
659 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
660 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
663 m_graphicContext
->StrokeLines( n
, pointsD
);
668 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
670 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
672 if ( !m_logicalFunctionSupported
)
675 wxGraphicsPath path
= m_graphicContext
->CreatePath();
677 wxPointList::compatibility_iterator node
= points
->GetFirst();
678 if (node
== wxPointList::compatibility_iterator())
682 wxPoint
*p
= node
->GetData();
687 node
= node
->GetNext();
692 wxCoord cx1
= ( x1
+ x2
) / 2;
693 wxCoord cy1
= ( y1
+ y2
) / 2;
695 path
.MoveToPoint( x1
, y1
);
696 path
.AddLineToPoint( cx1
, cy1
);
699 while ((node
= node
->GetNext()) != NULL
)
702 while ((node
= node
->GetNext()))
711 wxCoord cx4
= (x1
+ x2
) / 2;
712 wxCoord cy4
= (y1
+ y2
) / 2;
714 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
720 path
.AddLineToPoint( x2
, y2
);
722 m_graphicContext
->StrokePath( path
);
724 #endif // wxUSE_SPLINES
726 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
727 wxCoord xoffset
, wxCoord yoffset
,
728 wxPolygonFillMode fillStyle
)
730 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
732 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
734 if ( !m_logicalFunctionSupported
)
737 bool closeIt
= false;
738 if (points
[n
-1] != points
[0])
741 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
742 for( int i
= 0; i
< n
; ++i
)
744 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
745 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
748 pointsD
[n
] = pointsD
[0];
750 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
754 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
759 wxPolygonFillMode fillStyle
)
762 wxGraphicsPath path
= m_graphicContext
->CreatePath();
765 for ( int j
= 0; j
< n
; ++j
)
767 wxPoint start
= points
[i
];
768 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
771 for ( int k
= 1; k
< l
; ++k
)
773 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
777 if ( start
!= points
[i
-1])
778 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
780 m_graphicContext
->DrawPath( path
, fillStyle
);
783 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
785 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
787 if ( !m_logicalFunctionSupported
)
790 // CMB: draw nothing if transformed w or h is 0
791 if (w
== 0 || h
== 0)
794 if ( m_graphicContext
->ShouldOffset() )
796 // if we are offsetting the entire rectangle is moved 0.5, so the
797 // border line gets off by 1
801 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
804 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
805 wxCoord w
, wxCoord h
,
808 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
810 if ( !m_logicalFunctionSupported
)
814 radius
= - radius
* ((w
< h
) ? w
: h
);
816 // CMB: draw nothing if transformed w or h is 0
817 if (w
== 0 || h
== 0)
820 if ( m_graphicContext
->ShouldOffset() )
822 // if we are offsetting the entire rectangle is moved 0.5, so the
823 // border line gets off by 1
827 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
830 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
832 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
834 if ( !m_logicalFunctionSupported
)
837 if ( m_graphicContext
->ShouldOffset() )
839 // if we are offsetting the entire rectangle is moved 0.5, so the
840 // border line gets off by 1
844 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
847 bool wxGCDCImpl::CanDrawBitmap() const
852 bool wxGCDCImpl::DoBlit(
853 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
854 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
855 wxRasterOperationMode logical_func
, bool useMask
,
856 wxCoord xsrcMask
, wxCoord ysrcMask
)
858 return DoStretchBlit( xdest
, ydest
, width
, height
,
859 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
863 bool wxGCDCImpl::DoStretchBlit(
864 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
865 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
866 wxRasterOperationMode logical_func
, bool useMask
,
867 wxCoord xsrcMask
, wxCoord ysrcMask
)
869 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
870 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
872 if ( logical_func
== wxNO_OP
)
875 wxCompositionMode mode
;
876 if ( !TranslateRasterOp(logical_func
, &mode
) )
878 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
884 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
885 if (m_graphicContext
->SetCompositionMode(mode
))
887 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
888 if (mode
== wxCOMPOSITION_XOR
)
890 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
893 if (xsrcMask
== -1 && ysrcMask
== -1)
899 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
900 source
->LogicalToDeviceY(ysrc
),
901 source
->LogicalToDeviceXRel(srcWidth
),
902 source
->LogicalToDeviceYRel(srcHeight
));
904 // if needed clip the subrect down to the size of the source DC
906 source
->GetSize(&sw
, &sh
);
907 sw
= source
->LogicalToDeviceXRel(sw
);
908 sh
= source
->LogicalToDeviceYRel(sh
);
909 if (subrect
.x
+ subrect
.width
> sw
)
910 subrect
.width
= sw
- subrect
.x
;
911 if (subrect
.y
+ subrect
.height
> sh
)
912 subrect
.height
= sh
- subrect
.y
;
914 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
918 if ( !useMask
&& blit
.GetMask() )
921 m_graphicContext
->DrawBitmap( blit
, xdest
, ydest
,
922 dstWidth
, dstHeight
);
926 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
930 if (mode
== wxCOMPOSITION_XOR
)
932 m_graphicContext
->SetAntialiasMode(formerAa
);
936 m_graphicContext
->SetCompositionMode(formerMode
);
941 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
944 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
946 if ( str
.length() == 0 )
948 if ( !m_logicalFunctionSupported
)
951 if ( m_backgroundMode
== wxTRANSPARENT
)
952 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
954 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
957 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
959 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
961 if ( str
.length() == 0 )
964 if ( !m_logicalFunctionSupported
)
967 if ( m_backgroundMode
== wxTRANSPARENT
)
968 m_graphicContext
->DrawText( str
, x
,y
);
970 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
973 bool wxGCDCImpl::CanGetTextExtent() const
975 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
980 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
981 wxCoord
*descent
, wxCoord
*externalLeading
,
982 const wxFont
*theFont
) const
984 wxCHECK_RET( m_graphicContext
, wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
988 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
991 wxDouble h
, d
, e
, w
;
993 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
996 *height
= (wxCoord
)(h
+0.5);
998 *descent
= (wxCoord
)(d
+0.5);
999 if ( externalLeading
)
1000 *externalLeading
= (wxCoord
)(e
+0.5);
1002 *width
= (wxCoord
)(w
+0.5);
1006 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
1010 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1012 wxCHECK_MSG( m_graphicContext
, false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1014 widths
.Add(0,text
.Length());
1015 if ( text
.IsEmpty() )
1018 wxArrayDouble widthsD
;
1020 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1021 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1022 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1027 wxCoord
wxGCDCImpl::GetCharWidth(void) const
1030 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1035 wxCoord
wxGCDCImpl::GetCharHeight(void) const
1038 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1043 void wxGCDCImpl::Clear(void)
1045 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1046 // TODO better implementation / incorporate size info into wxGCDC or context
1047 m_graphicContext
->SetBrush( m_backgroundBrush
);
1048 wxPen p
= *wxTRANSPARENT_PEN
;
1049 m_graphicContext
->SetPen( p
);
1050 DoDrawRectangle( 0, 0, 32000 , 32000 );
1051 m_graphicContext
->SetPen( m_pen
);
1052 m_graphicContext
->SetBrush( m_brush
);
1055 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
1057 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
1059 m_graphicContext
->GetSize( &w
, &h
);
1061 *height
= (int) (h
+0.5);
1063 *width
= (int) (w
+0.5);
1066 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1067 const wxColour
& initialColour
,
1068 const wxColour
& destColour
,
1069 wxDirection nDirection
)
1076 start
= rect
.GetRightBottom();
1078 end
= rect
.GetLeftBottom();
1081 start
= rect
.GetLeftBottom();
1082 end
= rect
.GetRightBottom();
1086 start
= rect
.GetLeftBottom();
1088 end
= rect
.GetLeftTop();
1091 start
= rect
.GetLeftTop();
1092 end
= rect
.GetLeftBottom();
1099 if (rect
.width
== 0 || rect
.height
== 0)
1102 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1103 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1104 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1105 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1106 m_graphicContext
->SetPen(m_pen
);
1109 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1110 const wxColour
& initialColour
,
1111 const wxColour
& destColour
,
1112 const wxPoint
& circleCenter
)
1115 wxInt32 cx
= rect
.GetWidth() / 2;
1116 wxInt32 cy
= rect
.GetHeight() / 2;
1123 // make sure the background is filled (todo move into specific platform implementation ?)
1124 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1125 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1126 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1128 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1129 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1130 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1131 nRadius
,initialColour
,destColour
));
1133 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1134 m_graphicContext
->SetPen(m_pen
);
1137 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1138 wxCoord width
, wxCoord height
)
1140 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1143 #endif // wxUSE_GRAPHICS_CONTEXT