1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dcgraph.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 #include <limits.h> // for INT_MAX
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 static const double RAD2DEG
= 180.0 / M_PI
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 static inline double DegToRad(double deg
)
55 return (deg
* M_PI
) / 180.0;
58 static wxCompositionMode
TranslateRasterOp(wxRasterOperationMode function
)
63 // since we are supporting alpha, _OVER is closer to the intention than _SOURCE
64 // since the latter would overwrite even when alpha is not set to opaque
65 return wxCOMPOSITION_OVER
;
67 case wxOR
: // src OR dst
68 return wxCOMPOSITION_ADD
;
71 return wxCOMPOSITION_DEST
; // ignore the source
74 return wxCOMPOSITION_CLEAR
;// clear dst
76 case wxXOR
: // src XOR dst
77 return wxCOMPOSITION_XOR
;
79 case wxAND
: // src AND dst
80 case wxAND_INVERT
: // (NOT src) AND dst
81 case wxAND_REVERSE
:// src AND (NOT dst)
82 case wxEQUIV
: // (NOT src) XOR dst
83 case wxINVERT
: // NOT dst
84 case wxNAND
: // (NOT src) OR (NOT dst)
85 case wxNOR
: // (NOT src) AND (NOT dst)
86 case wxOR_INVERT
: // (NOT src) OR dst
87 case wxOR_REVERSE
: // src OR (NOT dst)
89 case wxSRC_INVERT
: // NOT src
93 return wxCOMPOSITION_INVALID
;
96 //-----------------------------------------------------------------------------
98 //-----------------------------------------------------------------------------
100 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
102 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
103 wxDC( new wxGCDCImpl( this, dc
) )
107 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
108 wxDC( new wxGCDCImpl( this, dc
) )
112 #if wxUSE_PRINTING_ARCHITECTURE
113 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
114 wxDC( new wxGCDCImpl( this, dc
) )
119 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
120 wxGCDC::wxGCDC(const wxEnhMetaFileDC
& dc
)
121 : wxDC(new wxGCDCImpl(this, dc
))
126 wxGCDC::wxGCDC(wxGraphicsContext
* context
) :
127 wxDC( new wxGCDCImpl( this ) )
129 SetGraphicsContext(context
);
133 wxDC( new wxGCDCImpl( this ) )
141 wxGraphicsContext
* wxGCDC::GetGraphicsContext() const
143 if (!m_pimpl
) return NULL
;
144 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
145 return gc_impl
->GetGraphicsContext();
148 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
150 if (!m_pimpl
) return;
151 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
152 gc_impl
->SetGraphicsContext( ctx
);
155 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
157 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
160 Init(wxGraphicsContext::Create());
163 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
165 delete m_graphicContext
;
166 m_graphicContext
= ctx
;
167 if ( m_graphicContext
)
169 m_matrixOriginal
= m_graphicContext
->GetTransform();
171 // apply the stored transformations to the passed in context
172 ComputeScaleAndOrigin();
173 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
174 m_graphicContext
->SetPen( m_pen
);
175 m_graphicContext
->SetBrush( m_brush
);
179 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
182 Init(wxGraphicsContext::Create(dc
));
183 m_window
= dc
.GetWindow();
186 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
189 Init(wxGraphicsContext::Create(dc
));
192 #if wxUSE_PRINTING_ARCHITECTURE
193 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
196 Init(wxGraphicsContext::Create(dc
));
200 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
201 wxGCDCImpl::wxGCDCImpl(wxDC
*owner
, const wxEnhMetaFileDC
& dc
)
204 Init(wxGraphicsContext::Create(dc
));
208 wxGCDCImpl::wxGCDCImpl(wxDC
* owner
, int)
211 // derived class will set a context
215 void wxGCDCImpl::Init(wxGraphicsContext
* ctx
)
219 m_mm_to_pix_x
= mm2pt
;
220 m_mm_to_pix_y
= mm2pt
;
222 m_pen
= *wxBLACK_PEN
;
223 m_font
= *wxNORMAL_FONT
;
224 m_brush
= *wxWHITE_BRUSH
;
226 m_graphicContext
= NULL
;
228 SetGraphicsContext(ctx
);
230 m_logicalFunctionSupported
= true;
233 wxGCDCImpl::~wxGCDCImpl()
235 delete m_graphicContext
;
238 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
241 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
242 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
244 int w
= bmp
.GetWidth();
245 int h
= bmp
.GetHeight();
246 if ( bmp
.GetDepth() == 1 )
248 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
249 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
250 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
251 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
252 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
253 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
254 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
256 else // not a monochrome bitmap, handle it normally
258 // make a copy in case we need to remove its mask, if we don't modify
259 // it the copy is cheap as bitmaps are reference-counted
260 wxBitmap
bmpCopy(bmp
);
261 if ( !useMask
&& bmp
.GetMask() )
262 bmpCopy
.SetMask(NULL
);
264 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
268 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
270 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
271 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
273 wxCoord w
= icon
.GetWidth();
274 wxCoord h
= icon
.GetHeight();
276 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
279 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
284 void wxGCDCImpl::EndDoc()
288 void wxGCDCImpl::StartPage()
292 void wxGCDCImpl::EndPage()
296 void wxGCDCImpl::Flush()
298 m_graphicContext
->Flush();
301 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
303 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
305 m_graphicContext
->Clip( x
, y
, w
, h
);
308 m_clipX1
= wxMax( m_clipX1
, x
);
309 m_clipY1
= wxMax( m_clipY1
, y
);
310 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
311 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
324 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
326 // region is in device coordinates
327 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
331 //DestroyClippingRegion();
335 wxRegion
logRegion( region
);
338 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
339 logRegion
.GetBox( x
, y
, w
, h
);
341 m_graphicContext
->Clip( logRegion
);
344 m_clipX1
= wxMax( m_clipX1
, x
);
345 m_clipY1
= wxMax( m_clipY1
, y
);
346 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
347 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
360 void wxGCDCImpl::DestroyClippingRegion()
362 m_graphicContext
->ResetClip();
363 // currently the clip eg of a window extends to the area between the scrollbars
364 // so we must explicitly make sure it only covers the area we want it to draw
366 GetOwner()->GetSize( &width
, &height
) ;
367 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
369 m_graphicContext
->SetPen( m_pen
);
370 m_graphicContext
->SetBrush( m_brush
);
375 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
379 GetOwner()->GetSize( &w
, &h
);
381 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
383 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
386 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
388 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
390 // don't set m_textForegroundColour to an invalid colour as we'd crash
391 // later then (we use m_textForegroundColour.GetColor() without checking
395 m_textForegroundColour
= col
;
396 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
400 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
402 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
404 m_textBackgroundColour
= col
;
407 wxSize
wxGCDCImpl::GetPPI() const
409 return wxSize(72, 72);
412 int wxGCDCImpl::GetDepth() const
417 void wxGCDCImpl::ComputeScaleAndOrigin()
419 wxDCImpl::ComputeScaleAndOrigin();
421 if ( m_graphicContext
)
423 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
425 // the logical origin sets the origin to have new coordinates
426 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
427 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
429 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
431 m_graphicContext
->SetTransform( m_matrixOriginal
);
432 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
436 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
441 void wxGCDCImpl::SetBackgroundMode( int mode
)
443 m_backgroundMode
= mode
;
446 void wxGCDCImpl::SetFont( const wxFont
&font
)
449 if ( m_graphicContext
)
451 m_graphicContext
->SetFont(font
, m_textForegroundColour
);
455 void wxGCDCImpl::SetPen( const wxPen
&pen
)
458 if ( m_graphicContext
)
460 m_graphicContext
->SetPen( m_pen
);
464 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
467 if ( m_graphicContext
)
469 m_graphicContext
->SetBrush( m_brush
);
473 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
475 m_backgroundBrush
= brush
;
476 if (!m_backgroundBrush
.IsOk())
480 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
482 m_logicalFunction
= function
;
484 wxCompositionMode mode
= TranslateRasterOp( function
);
485 m_logicalFunctionSupported
= mode
!= wxCOMPOSITION_INVALID
;
486 if (m_logicalFunctionSupported
)
487 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
489 if ( function
== wxXOR
)
490 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
492 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
495 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
496 const wxColour
& WXUNUSED(col
),
497 wxFloodFillStyle
WXUNUSED(style
))
502 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
504 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
508 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
510 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
512 if ( !m_logicalFunctionSupported
)
515 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
517 CalcBoundingBox(x1
, y1
);
518 CalcBoundingBox(x2
, y2
);
521 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
523 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
525 if ( !m_logicalFunctionSupported
)
530 GetOwner()->GetSize( &w
, &h
);
532 m_graphicContext
->StrokeLine(0,y
,w
,y
);
533 m_graphicContext
->StrokeLine(x
,0,x
,h
);
535 CalcBoundingBox(0, 0);
536 CalcBoundingBox(0+w
, 0+h
);
539 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
540 wxCoord x2
, wxCoord y2
,
541 wxCoord xc
, wxCoord yc
)
543 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
545 if ( !m_logicalFunctionSupported
)
550 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
551 wxCoord rad
= (wxCoord
)radius
;
553 if (x1
== x2
&& y1
== y2
)
558 else if (radius
== 0.0)
564 sa
= (x1
- xc
== 0) ?
565 (y1
- yc
< 0) ? 90.0 : -90.0 :
566 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
567 ea
= (x2
- xc
== 0) ?
568 (y2
- yc
< 0) ? 90.0 : -90.0 :
569 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
572 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
574 wxGraphicsPath path
= m_graphicContext
->CreatePath();
575 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
576 path
.MoveToPoint( xc
, yc
);
577 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
578 // get clockwise angles
579 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
580 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
581 path
.AddLineToPoint( xc
, yc
);
582 m_graphicContext
->DrawPath(path
);
585 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
586 double sa
, double ea
)
588 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
590 if ( !m_logicalFunctionSupported
)
593 m_graphicContext
->PushState();
594 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
595 wxDouble factor
= ((wxDouble
) w
) / h
;
596 m_graphicContext
->Scale( factor
, 1.0);
598 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
599 // get clockwise angles
600 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
602 wxGraphicsPath path
= m_graphicContext
->CreatePath();
603 path
.MoveToPoint( 0, 0 );
604 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
605 path
.AddLineToPoint( 0, 0 );
606 m_graphicContext
->FillPath( path
);
608 path
= m_graphicContext
->CreatePath();
609 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
610 m_graphicContext
->StrokePath( path
);
614 wxGraphicsPath path
= m_graphicContext
->CreatePath();
615 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
616 m_graphicContext
->DrawPath( path
);
619 m_graphicContext
->PopState();
622 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
624 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
626 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
629 void wxGCDCImpl::DoDrawLines(int n
, wxPoint points
[],
630 wxCoord xoffset
, wxCoord yoffset
)
632 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
634 if ( !m_logicalFunctionSupported
)
637 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
638 for( int i
= 0; i
< n
; ++i
)
640 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
641 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
644 m_graphicContext
->StrokeLines( n
, pointsD
);
649 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
651 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
653 if ( !m_logicalFunctionSupported
)
656 wxGraphicsPath path
= m_graphicContext
->CreatePath();
658 wxPointList::compatibility_iterator node
= points
->GetFirst();
663 wxPoint
*p
= node
->GetData();
668 node
= node
->GetNext();
673 wxCoord cx1
= ( x1
+ x2
) / 2;
674 wxCoord cy1
= ( y1
+ y2
) / 2;
676 path
.MoveToPoint( x1
, y1
);
677 path
.AddLineToPoint( cx1
, cy1
);
678 #if !wxUSE_STD_CONTAINERS
680 while ((node
= node
->GetNext()) != NULL
)
683 while ((node
= node
->GetNext()))
684 #endif // !wxUSE_STD_CONTAINERS
692 wxCoord cx4
= (x1
+ x2
) / 2;
693 wxCoord cy4
= (y1
+ y2
) / 2;
695 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
701 path
.AddLineToPoint( x2
, y2
);
703 m_graphicContext
->StrokePath( path
);
705 #endif // wxUSE_SPLINES
707 void wxGCDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
708 wxCoord xoffset
, wxCoord yoffset
,
709 wxPolygonFillMode fillStyle
)
711 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
713 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
715 if ( !m_logicalFunctionSupported
)
718 bool closeIt
= false;
719 if (points
[n
-1] != points
[0])
722 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
723 for( int i
= 0; i
< n
; ++i
)
725 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
726 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
729 pointsD
[n
] = pointsD
[0];
731 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
735 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
740 wxPolygonFillMode fillStyle
)
743 wxGraphicsPath path
= m_graphicContext
->CreatePath();
746 for ( int j
= 0; j
< n
; ++j
)
748 wxPoint start
= points
[i
];
749 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
752 for ( int k
= 1; k
< l
; ++k
)
754 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
758 if ( start
!= points
[i
-1])
759 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
761 m_graphicContext
->DrawPath( path
, fillStyle
);
764 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
766 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
768 if ( !m_logicalFunctionSupported
)
771 // CMB: draw nothing if transformed w or h is 0
772 if (w
== 0 || h
== 0)
775 if ( m_graphicContext
->ShouldOffset() )
777 // if we are offsetting the entire rectangle is moved 0.5, so the
778 // border line gets off by 1
782 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
785 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
786 wxCoord w
, wxCoord h
,
789 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
791 if ( !m_logicalFunctionSupported
)
795 radius
= - radius
* ((w
< h
) ? w
: h
);
797 // CMB: draw nothing if transformed w or h is 0
798 if (w
== 0 || h
== 0)
801 if ( m_graphicContext
->ShouldOffset() )
803 // if we are offsetting the entire rectangle is moved 0.5, so the
804 // border line gets off by 1
808 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
811 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
813 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
815 if ( !m_logicalFunctionSupported
)
818 if ( m_graphicContext
->ShouldOffset() )
820 // if we are offsetting the entire rectangle is moved 0.5, so the
821 // border line gets off by 1
825 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
828 bool wxGCDCImpl::CanDrawBitmap() const
833 bool wxGCDCImpl::DoBlit(
834 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
835 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
836 wxRasterOperationMode logical_func
, bool useMask
,
837 wxCoord xsrcMask
, wxCoord ysrcMask
)
839 return DoStretchBlit( xdest
, ydest
, width
, height
,
840 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
844 bool wxGCDCImpl::DoStretchBlit(
845 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
846 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
847 wxRasterOperationMode logical_func
, bool useMask
,
848 wxCoord xsrcMask
, wxCoord ysrcMask
)
850 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
851 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
853 if ( logical_func
== wxNO_OP
)
856 wxCompositionMode mode
= TranslateRasterOp(logical_func
);
857 if ( mode
== wxCOMPOSITION_INVALID
)
859 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
863 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
864 source
->LogicalToDeviceY(ysrc
),
865 source
->LogicalToDeviceXRel(srcWidth
),
866 source
->LogicalToDeviceYRel(srcHeight
));
867 const wxRect subrectOrig
= subrect
;
868 // clip the subrect down to the size of the source DC
870 source
->GetSize(&clip
.width
, &clip
.height
);
871 subrect
.Intersect(clip
);
872 if (subrect
.width
== 0)
877 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
878 if (m_graphicContext
->SetCompositionMode(mode
))
880 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
881 if (mode
== wxCOMPOSITION_XOR
)
883 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
886 if (xsrcMask
== -1 && ysrcMask
== -1)
892 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
896 if ( !useMask
&& blit
.GetMask() )
902 double h
= dstHeight
;
903 // adjust dest rect if source rect is clipped
904 if (subrect
.width
!= subrectOrig
.width
|| subrect
.height
!= subrectOrig
.height
)
906 x
+= (subrect
.x
- subrectOrig
.x
) / double(subrectOrig
.width
) * dstWidth
;
907 y
+= (subrect
.y
- subrectOrig
.y
) / double(subrectOrig
.height
) * dstHeight
;
908 w
*= double(subrect
.width
) / subrectOrig
.width
;
909 h
*= double(subrect
.height
) / subrectOrig
.height
;
911 m_graphicContext
->DrawBitmap(blit
, x
, y
, w
, h
);
915 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
919 if (mode
== wxCOMPOSITION_XOR
)
921 m_graphicContext
->SetAntialiasMode(formerAa
);
925 m_graphicContext
->SetCompositionMode(formerMode
);
930 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
933 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
937 if ( !m_logicalFunctionSupported
)
940 if ( m_backgroundMode
== wxTRANSPARENT
)
941 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
943 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
946 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
948 // For compatibility with other ports (notably wxGTK) and because it's
949 // genuinely useful, we allow passing multiline strings to DrawText().
950 // However there is no native OSX function to draw them directly so we
951 // instead reuse the generic DrawLabel() method to render them. Of course,
952 // DrawLabel() itself will call back to us but with single line strings
953 // only so there won't be any infinite recursion here.
954 if ( str
.find('\n') != wxString::npos
)
956 GetOwner()->DrawLabel(str
, wxRect(x
, y
, 0, 0));
960 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
965 if ( !m_logicalFunctionSupported
)
968 if ( m_backgroundMode
== wxTRANSPARENT
)
969 m_graphicContext
->DrawText( str
, x
,y
);
971 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
974 bool wxGCDCImpl::CanGetTextExtent() const
976 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
981 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
982 wxCoord
*descent
, wxCoord
*externalLeading
,
983 const wxFont
*theFont
) const
985 wxCHECK_RET( m_graphicContext
, wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
989 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
992 wxDouble h
, d
, e
, w
;
994 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
997 *height
= (wxCoord
)(h
+0.5);
999 *descent
= (wxCoord
)(d
+0.5);
1000 if ( externalLeading
)
1001 *externalLeading
= (wxCoord
)(e
+0.5);
1003 *width
= (wxCoord
)(w
+0.5);
1007 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
1011 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1013 wxCHECK_MSG( m_graphicContext
, false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1015 widths
.Add(0,text
.Length());
1016 if ( text
.IsEmpty() )
1019 wxArrayDouble widthsD
;
1021 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1022 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1023 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1028 wxCoord
wxGCDCImpl::GetCharWidth(void) const
1031 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1036 wxCoord
wxGCDCImpl::GetCharHeight(void) const
1039 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1044 void wxGCDCImpl::Clear(void)
1046 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1047 // TODO better implementation / incorporate size info into wxGCDC or context
1048 m_graphicContext
->SetBrush( m_backgroundBrush
);
1049 wxPen p
= *wxTRANSPARENT_PEN
;
1050 m_graphicContext
->SetPen( p
);
1051 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
1052 m_graphicContext
->SetCompositionMode(wxCOMPOSITION_SOURCE
);
1053 DoDrawRectangle( 0, 0, INT_MAX
, INT_MAX
);
1054 m_graphicContext
->SetCompositionMode(formerMode
);
1055 m_graphicContext
->SetPen( m_pen
);
1056 m_graphicContext
->SetBrush( m_brush
);
1059 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
1061 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
1063 m_graphicContext
->GetSize( &w
, &h
);
1065 *height
= (int) (h
+0.5);
1067 *width
= (int) (w
+0.5);
1070 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1071 const wxColour
& initialColour
,
1072 const wxColour
& destColour
,
1073 wxDirection nDirection
)
1080 start
= rect
.GetRightBottom();
1082 end
= rect
.GetLeftBottom();
1085 start
= rect
.GetLeftBottom();
1086 end
= rect
.GetRightBottom();
1090 start
= rect
.GetLeftBottom();
1092 end
= rect
.GetLeftTop();
1095 start
= rect
.GetLeftTop();
1096 end
= rect
.GetLeftBottom();
1103 if (rect
.width
== 0 || rect
.height
== 0)
1106 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1107 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1108 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1109 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1110 m_graphicContext
->SetPen(m_pen
);
1111 m_graphicContext
->SetBrush(m_brush
);
1114 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1115 const wxColour
& initialColour
,
1116 const wxColour
& destColour
,
1117 const wxPoint
& circleCenter
)
1120 wxInt32 cx
= rect
.GetWidth() / 2;
1121 wxInt32 cy
= rect
.GetHeight() / 2;
1128 // make sure the background is filled (todo move into specific platform implementation ?)
1129 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1130 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1131 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1133 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1134 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1135 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1136 nRadius
,initialColour
,destColour
));
1138 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1139 m_graphicContext
->SetPen(m_pen
);
1140 m_graphicContext
->SetBrush(m_brush
);
1143 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1144 wxCoord width
, wxCoord height
)
1146 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1150 wxRect
wxGCDCImpl::MSWApplyGDIPlusTransform(const wxRect
& r
) const
1152 wxGraphicsContext
* const gc
= GetGraphicsContext();
1153 wxCHECK_MSG( gc
, r
, wxT("Invalid wxGCDC") );
1157 gc
->GetTransform().TransformPoint(&x
, &y
);
1166 #endif // wxUSE_GRAPHICS_CONTEXT