1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dcgraph.cpp
3 // Purpose: graphics context methods common to all platforms
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if defined(__BORLANDC__)
18 #if wxUSE_GRAPHICS_CONTEXT
20 #include "wx/dcgraph.h"
24 #include "wx/dcclient.h"
25 #include "wx/dcmemory.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 static const double RAD2DEG
= 180.0 / M_PI
;
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 static inline double DegToRad(double deg
)
40 return (deg
* M_PI
) / 180.0;
43 static wxCompositionMode
TranslateRasterOp(wxRasterOperationMode function
)
48 // since we are supporting alpha, _OVER is closer to the intention than _SOURCE
49 // since the latter would overwrite even when alpha is not set to opaque
50 return wxCOMPOSITION_OVER
;
52 case wxOR
: // src OR dst
53 return wxCOMPOSITION_ADD
;
56 return wxCOMPOSITION_DEST
; // ignore the source
59 return wxCOMPOSITION_CLEAR
;// clear dst
61 case wxXOR
: // src XOR dst
62 return wxCOMPOSITION_XOR
;
64 case wxAND
: // src AND dst
65 case wxAND_INVERT
: // (NOT src) AND dst
66 case wxAND_REVERSE
:// src AND (NOT dst)
67 case wxEQUIV
: // (NOT src) XOR dst
68 case wxINVERT
: // NOT dst
69 case wxNAND
: // (NOT src) OR (NOT dst)
70 case wxNOR
: // (NOT src) AND (NOT dst)
71 case wxOR_INVERT
: // (NOT src) OR dst
72 case wxOR_REVERSE
: // src OR (NOT dst)
74 case wxSRC_INVERT
: // NOT src
78 return wxCOMPOSITION_INVALID
;
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 IMPLEMENT_DYNAMIC_CLASS(wxGCDC
, wxDC
)
87 wxGCDC::wxGCDC(const wxWindowDC
& dc
) :
88 wxDC( new wxGCDCImpl( this, dc
) )
92 wxGCDC::wxGCDC( const wxMemoryDC
& dc
) :
93 wxDC( new wxGCDCImpl( this, dc
) )
97 #if wxUSE_PRINTING_ARCHITECTURE
98 wxGCDC::wxGCDC( const wxPrinterDC
& dc
) :
99 wxDC( new wxGCDCImpl( this, dc
) )
104 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
105 wxGCDC::wxGCDC(const wxEnhMetaFileDC
& dc
)
106 : wxDC(new wxGCDCImpl(this, dc
))
111 wxGCDC::wxGCDC(wxGraphicsContext
* context
) :
112 wxDC( new wxGCDCImpl( this ) )
114 SetGraphicsContext(context
);
118 wxDC( new wxGCDCImpl( this ) )
126 wxGraphicsContext
* wxGCDC::GetGraphicsContext() const
128 if (!m_pimpl
) return NULL
;
129 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
130 return gc_impl
->GetGraphicsContext();
133 void wxGCDC::SetGraphicsContext( wxGraphicsContext
* ctx
)
135 if (!m_pimpl
) return;
136 wxGCDCImpl
*gc_impl
= (wxGCDCImpl
*) m_pimpl
;
137 gc_impl
->SetGraphicsContext( ctx
);
140 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl
, wxDCImpl
)
142 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
) :
145 Init(wxGraphicsContext::Create());
148 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext
* ctx
)
150 delete m_graphicContext
;
151 m_graphicContext
= ctx
;
152 if ( m_graphicContext
)
154 m_matrixOriginal
= m_graphicContext
->GetTransform();
156 // apply the stored transformations to the passed in context
157 ComputeScaleAndOrigin();
158 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
159 m_graphicContext
->SetPen( m_pen
);
160 m_graphicContext
->SetBrush( m_brush
);
164 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxWindowDC
& dc
) :
167 Init(wxGraphicsContext::Create(dc
));
168 m_window
= dc
.GetWindow();
171 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxMemoryDC
& dc
) :
174 Init(wxGraphicsContext::Create(dc
));
177 #if wxUSE_PRINTING_ARCHITECTURE
178 wxGCDCImpl::wxGCDCImpl( wxDC
*owner
, const wxPrinterDC
& dc
) :
181 Init(wxGraphicsContext::Create(dc
));
185 #if defined(__WXMSW__) && wxUSE_ENH_METAFILE
186 wxGCDCImpl::wxGCDCImpl(wxDC
*owner
, const wxEnhMetaFileDC
& dc
)
189 Init(wxGraphicsContext::Create(dc
));
193 wxGCDCImpl::wxGCDCImpl(wxDC
* owner
, int)
196 // derived class will set a context
200 void wxGCDCImpl::Init(wxGraphicsContext
* ctx
)
204 m_mm_to_pix_x
= mm2pt
;
205 m_mm_to_pix_y
= mm2pt
;
207 m_pen
= *wxBLACK_PEN
;
208 m_font
= *wxNORMAL_FONT
;
209 m_brush
= *wxWHITE_BRUSH
;
211 m_graphicContext
= NULL
;
213 SetGraphicsContext(ctx
);
215 m_logicalFunctionSupported
= true;
218 wxGCDCImpl::~wxGCDCImpl()
220 delete m_graphicContext
;
223 void wxGCDCImpl::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
226 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
227 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
229 int w
= bmp
.GetScaledWidth();
230 int h
= bmp
.GetScaledHeight();
231 if ( bmp
.GetDepth() == 1 )
233 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
234 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
235 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
236 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
237 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
238 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
239 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
241 else // not a monochrome bitmap, handle it normally
243 // make a copy in case we need to remove its mask, if we don't modify
244 // it the copy is cheap as bitmaps are reference-counted
245 wxBitmap
bmpCopy(bmp
);
246 if ( !useMask
&& bmp
.GetMask() )
247 bmpCopy
.SetMask(NULL
);
249 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
253 void wxGCDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
255 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
256 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
258 wxCoord w
= icon
.GetWidth();
259 wxCoord h
= icon
.GetHeight();
261 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
264 bool wxGCDCImpl::StartDoc( const wxString
& WXUNUSED(message
) )
269 void wxGCDCImpl::EndDoc()
273 void wxGCDCImpl::StartPage()
277 void wxGCDCImpl::EndPage()
281 void wxGCDCImpl::Flush()
283 m_graphicContext
->Flush();
286 void wxGCDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
288 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
290 m_graphicContext
->Clip( x
, y
, w
, h
);
292 wxDCImpl::DoSetClippingRegion(x
, y
, w
, h
);
295 void wxGCDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
297 // region is in device coordinates
298 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
302 //DestroyClippingRegion();
306 wxRegion
logRegion( region
);
309 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
310 logRegion
.GetBox( x
, y
, w
, h
);
312 m_graphicContext
->Clip( logRegion
);
315 m_clipX1
= wxMax( m_clipX1
, x
);
316 m_clipY1
= wxMax( m_clipY1
, y
);
317 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
318 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
331 void wxGCDCImpl::DestroyClippingRegion()
333 m_graphicContext
->ResetClip();
334 // currently the clip eg of a window extends to the area between the scrollbars
335 // so we must explicitly make sure it only covers the area we want it to draw
337 GetOwner()->GetSize( &width
, &height
) ;
338 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
340 m_graphicContext
->SetPen( m_pen
);
341 m_graphicContext
->SetBrush( m_brush
);
346 void wxGCDCImpl::DoGetSizeMM( int* width
, int* height
) const
350 GetOwner()->GetSize( &w
, &h
);
352 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
354 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
357 void wxGCDCImpl::SetTextForeground( const wxColour
&col
)
359 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
361 // don't set m_textForegroundColour to an invalid colour as we'd crash
362 // later then (we use m_textForegroundColour.GetColor() without checking
366 m_textForegroundColour
= col
;
367 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
371 void wxGCDCImpl::SetTextBackground( const wxColour
&col
)
373 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
375 m_textBackgroundColour
= col
;
378 wxSize
wxGCDCImpl::GetPPI() const
380 return wxSize(72, 72);
383 int wxGCDCImpl::GetDepth() const
388 void wxGCDCImpl::ComputeScaleAndOrigin()
390 wxDCImpl::ComputeScaleAndOrigin();
392 if ( m_graphicContext
)
394 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
396 // the logical origin sets the origin to have new coordinates
397 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
398 m_deviceOriginY
- m_logicalOriginY
* m_signY
* m_scaleY
);
400 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
402 m_graphicContext
->SetTransform( m_matrixOriginal
);
403 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
407 void* wxGCDCImpl::GetHandle() const
410 wxGraphicsContext
* gc
= GetGraphicsContext();
412 cgctx
= gc
->GetNativeContext();
417 void wxGCDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
422 void wxGCDCImpl::SetBackgroundMode( int mode
)
424 m_backgroundMode
= mode
;
427 void wxGCDCImpl::SetFont( const wxFont
&font
)
430 if ( m_graphicContext
)
432 m_graphicContext
->SetFont(font
, m_textForegroundColour
);
436 void wxGCDCImpl::SetPen( const wxPen
&pen
)
439 if ( m_graphicContext
)
441 m_graphicContext
->SetPen( m_pen
);
445 void wxGCDCImpl::SetBrush( const wxBrush
&brush
)
448 if ( m_graphicContext
)
450 m_graphicContext
->SetBrush( m_brush
);
454 void wxGCDCImpl::SetBackground( const wxBrush
&brush
)
456 m_backgroundBrush
= brush
;
457 if (!m_backgroundBrush
.IsOk())
461 void wxGCDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
463 m_logicalFunction
= function
;
465 wxCompositionMode mode
= TranslateRasterOp( function
);
466 m_logicalFunctionSupported
= mode
!= wxCOMPOSITION_INVALID
;
467 if (m_logicalFunctionSupported
)
468 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
470 if ( function
== wxXOR
)
471 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
473 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
476 bool wxGCDCImpl::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
477 const wxColour
& WXUNUSED(col
),
478 wxFloodFillStyle
WXUNUSED(style
))
483 bool wxGCDCImpl::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
485 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
489 void wxGCDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
491 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
493 if ( !m_logicalFunctionSupported
)
496 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
498 CalcBoundingBox(x1
, y1
);
499 CalcBoundingBox(x2
, y2
);
502 void wxGCDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
504 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
506 if ( !m_logicalFunctionSupported
)
511 GetOwner()->GetSize( &w
, &h
);
513 m_graphicContext
->StrokeLine(0,y
,w
,y
);
514 m_graphicContext
->StrokeLine(x
,0,x
,h
);
516 CalcBoundingBox(0, 0);
517 CalcBoundingBox(0+w
, 0+h
);
520 void wxGCDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
,
521 wxCoord x2
, wxCoord y2
,
522 wxCoord xc
, wxCoord yc
)
524 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
526 if ( !m_logicalFunctionSupported
)
531 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
532 wxCoord rad
= (wxCoord
)radius
;
534 if (x1
== x2
&& y1
== y2
)
539 else if (radius
== 0.0)
545 sa
= (x1
- xc
== 0) ?
546 (y1
- yc
< 0) ? 90.0 : -90.0 :
547 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
548 ea
= (x2
- xc
== 0) ?
549 (y2
- yc
< 0) ? 90.0 : -90.0 :
550 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
553 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
555 wxGraphicsPath path
= m_graphicContext
->CreatePath();
556 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
557 path
.MoveToPoint( xc
, yc
);
558 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
559 // get clockwise angles
560 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
561 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
562 path
.AddLineToPoint( xc
, yc
);
563 m_graphicContext
->DrawPath(path
);
566 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
567 double sa
, double ea
)
569 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
571 if ( !m_logicalFunctionSupported
)
574 m_graphicContext
->PushState();
575 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
576 wxDouble factor
= ((wxDouble
) w
) / h
;
577 m_graphicContext
->Scale( factor
, 1.0);
579 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
580 // get clockwise angles
581 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
583 wxGraphicsPath path
= m_graphicContext
->CreatePath();
584 path
.MoveToPoint( 0, 0 );
585 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
586 path
.AddLineToPoint( 0, 0 );
587 m_graphicContext
->FillPath( path
);
589 path
= m_graphicContext
->CreatePath();
590 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
591 m_graphicContext
->StrokePath( path
);
595 wxGraphicsPath path
= m_graphicContext
->CreatePath();
596 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
597 m_graphicContext
->DrawPath( path
);
600 m_graphicContext
->PopState();
603 void wxGCDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
605 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
607 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
610 void wxGCDCImpl::DoDrawLines(int n
, const wxPoint points
[],
611 wxCoord xoffset
, wxCoord yoffset
)
613 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
615 if ( !m_logicalFunctionSupported
)
618 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
619 for( int i
= 0; i
< n
; ++i
)
621 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
622 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
625 m_graphicContext
->StrokeLines( n
, pointsD
);
630 void wxGCDCImpl::DoDrawSpline(const wxPointList
*points
)
632 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
634 if ( !m_logicalFunctionSupported
)
637 wxGraphicsPath path
= m_graphicContext
->CreatePath();
639 wxPointList::compatibility_iterator node
= points
->GetFirst();
644 const wxPoint
*p
= node
->GetData();
649 node
= node
->GetNext();
654 wxCoord cx1
= ( x1
+ x2
) / 2;
655 wxCoord cy1
= ( y1
+ y2
) / 2;
657 path
.MoveToPoint( x1
, y1
);
658 path
.AddLineToPoint( cx1
, cy1
);
659 #if !wxUSE_STD_CONTAINERS
661 while ((node
= node
->GetNext()) != NULL
)
664 while ((node
= node
->GetNext()))
665 #endif // !wxUSE_STD_CONTAINERS
673 wxCoord cx4
= (x1
+ x2
) / 2;
674 wxCoord cy4
= (y1
+ y2
) / 2;
676 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
682 path
.AddLineToPoint( x2
, y2
);
684 m_graphicContext
->StrokePath( path
);
686 #endif // wxUSE_SPLINES
688 void wxGCDCImpl::DoDrawPolygon( int n
, const wxPoint points
[],
689 wxCoord xoffset
, wxCoord yoffset
,
690 wxPolygonFillMode fillStyle
)
692 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
694 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
696 if ( !m_logicalFunctionSupported
)
699 bool closeIt
= false;
700 if (points
[n
-1] != points
[0])
703 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt
?1:0)];
704 for( int i
= 0; i
< n
; ++i
)
706 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
707 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
710 pointsD
[n
] = pointsD
[0];
712 m_graphicContext
->DrawLines( n
+(closeIt
?1:0) , pointsD
, fillStyle
);
716 void wxGCDCImpl::DoDrawPolyPolygon(int n
,
718 const wxPoint points
[],
721 wxPolygonFillMode fillStyle
)
724 wxGraphicsPath path
= m_graphicContext
->CreatePath();
727 for ( int j
= 0; j
< n
; ++j
)
729 wxPoint start
= points
[i
];
730 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
733 for ( int k
= 1; k
< l
; ++k
)
735 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
739 if ( start
!= points
[i
-1])
740 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
742 m_graphicContext
->DrawPath( path
, fillStyle
);
745 void wxGCDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
747 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
749 if ( !m_logicalFunctionSupported
)
752 // CMB: draw nothing if transformed w or h is 0
753 if (w
== 0 || h
== 0)
756 if ( m_graphicContext
->ShouldOffset() )
758 // if we are offsetting the entire rectangle is moved 0.5, so the
759 // border line gets off by 1
763 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
766 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
767 wxCoord w
, wxCoord h
,
770 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
772 if ( !m_logicalFunctionSupported
)
776 radius
= - radius
* ((w
< h
) ? w
: h
);
778 // CMB: draw nothing if transformed w or h is 0
779 if (w
== 0 || h
== 0)
782 if ( m_graphicContext
->ShouldOffset() )
784 // if we are offsetting the entire rectangle is moved 0.5, so the
785 // border line gets off by 1
789 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
792 void wxGCDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
794 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
796 if ( !m_logicalFunctionSupported
)
799 if ( m_graphicContext
->ShouldOffset() )
801 // if we are offsetting the entire rectangle is moved 0.5, so the
802 // border line gets off by 1
806 m_graphicContext
->DrawEllipse(x
,y
,w
,h
);
809 bool wxGCDCImpl::CanDrawBitmap() const
814 bool wxGCDCImpl::DoBlit(
815 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
816 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
817 wxRasterOperationMode logical_func
, bool useMask
,
818 wxCoord xsrcMask
, wxCoord ysrcMask
)
820 return DoStretchBlit( xdest
, ydest
, width
, height
,
821 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
825 bool wxGCDCImpl::DoStretchBlit(
826 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
827 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
828 wxRasterOperationMode logical_func
, bool useMask
,
829 wxCoord xsrcMask
, wxCoord ysrcMask
)
831 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
832 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
834 if ( logical_func
== wxNO_OP
)
837 wxCompositionMode mode
= TranslateRasterOp(logical_func
);
838 if ( mode
== wxCOMPOSITION_INVALID
)
840 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
844 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
845 source
->LogicalToDeviceY(ysrc
),
846 source
->LogicalToDeviceXRel(srcWidth
),
847 source
->LogicalToDeviceYRel(srcHeight
));
848 const wxRect subrectOrig
= subrect
;
849 // clip the subrect down to the size of the source DC
851 source
->GetSize(&clip
.width
, &clip
.height
);
852 subrect
.Intersect(clip
);
853 if (subrect
.width
== 0)
858 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
859 if (m_graphicContext
->SetCompositionMode(mode
))
861 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
862 if (mode
== wxCOMPOSITION_XOR
)
864 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
867 if (xsrcMask
== -1 && ysrcMask
== -1)
873 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
877 if ( !useMask
&& blit
.GetMask() )
883 double h
= dstHeight
;
884 // adjust dest rect if source rect is clipped
885 if (subrect
.width
!= subrectOrig
.width
|| subrect
.height
!= subrectOrig
.height
)
887 x
+= (subrect
.x
- subrectOrig
.x
) / double(subrectOrig
.width
) * dstWidth
;
888 y
+= (subrect
.y
- subrectOrig
.y
) / double(subrectOrig
.height
) * dstHeight
;
889 w
*= double(subrect
.width
) / subrectOrig
.width
;
890 h
*= double(subrect
.height
) / subrectOrig
.height
;
892 m_graphicContext
->DrawBitmap(blit
, x
, y
, w
, h
);
896 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
900 if (mode
== wxCOMPOSITION_XOR
)
902 m_graphicContext
->SetAntialiasMode(formerAa
);
906 m_graphicContext
->SetCompositionMode(formerMode
);
911 void wxGCDCImpl::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
914 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
918 if ( !m_logicalFunctionSupported
)
921 if ( m_backgroundMode
== wxTRANSPARENT
)
922 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
924 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
927 void wxGCDCImpl::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
929 // For compatibility with other ports (notably wxGTK) and because it's
930 // genuinely useful, we allow passing multiline strings to DrawText().
931 // However there is no native OSX function to draw them directly so we
932 // instead reuse the generic DrawLabel() method to render them. Of course,
933 // DrawLabel() itself will call back to us but with single line strings
934 // only so there won't be any infinite recursion here.
935 if ( str
.find('\n') != wxString::npos
)
937 GetOwner()->DrawLabel(str
, wxRect(x
, y
, 0, 0));
941 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
946 if ( !m_logicalFunctionSupported
)
949 if ( m_backgroundMode
== wxTRANSPARENT
)
950 m_graphicContext
->DrawText( str
, x
,y
);
952 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
955 bool wxGCDCImpl::CanGetTextExtent() const
957 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
962 void wxGCDCImpl::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
963 wxCoord
*descent
, wxCoord
*externalLeading
,
964 const wxFont
*theFont
) const
966 wxCHECK_RET( m_graphicContext
, wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
970 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
973 wxDouble h
, d
, e
, w
;
975 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
978 *height
= (wxCoord
)(h
+0.5);
980 *descent
= (wxCoord
)(d
+0.5);
981 if ( externalLeading
)
982 *externalLeading
= (wxCoord
)(e
+0.5);
984 *width
= (wxCoord
)(w
+0.5);
988 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
992 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
994 wxCHECK_MSG( m_graphicContext
, false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
996 widths
.Add(0,text
.Length());
997 if ( text
.IsEmpty() )
1000 wxArrayDouble widthsD
;
1002 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1003 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1004 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1009 wxCoord
wxGCDCImpl::GetCharWidth(void) const
1012 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1017 wxCoord
wxGCDCImpl::GetCharHeight(void) const
1020 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1025 void wxGCDCImpl::Clear(void)
1027 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1028 // TODO better implementation / incorporate size info into wxGCDC or context
1029 m_graphicContext
->SetBrush( m_backgroundBrush
);
1030 wxPen p
= *wxTRANSPARENT_PEN
;
1031 m_graphicContext
->SetPen( p
);
1032 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
1033 m_graphicContext
->SetCompositionMode(wxCOMPOSITION_SOURCE
);
1034 // maximum positive coordinate Cairo can handle is 2^23 - 1
1036 DeviceToLogicalX(0), DeviceToLogicalY(0),
1037 DeviceToLogicalXRel(0x007fffff), DeviceToLogicalYRel(0x007fffff));
1038 m_graphicContext
->SetCompositionMode(formerMode
);
1039 m_graphicContext
->SetPen( m_pen
);
1040 m_graphicContext
->SetBrush( m_brush
);
1043 void wxGCDCImpl::DoGetSize(int *width
, int *height
) const
1045 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetSize - invalid DC") );
1047 m_graphicContext
->GetSize( &w
, &h
);
1049 *height
= (int) (h
+0.5);
1051 *width
= (int) (w
+0.5);
1054 void wxGCDCImpl::DoGradientFillLinear(const wxRect
& rect
,
1055 const wxColour
& initialColour
,
1056 const wxColour
& destColour
,
1057 wxDirection nDirection
)
1064 start
= rect
.GetRightBottom();
1066 end
= rect
.GetLeftBottom();
1069 start
= rect
.GetLeftBottom();
1070 end
= rect
.GetRightBottom();
1074 start
= rect
.GetLeftBottom();
1076 end
= rect
.GetLeftTop();
1079 start
= rect
.GetLeftTop();
1080 end
= rect
.GetLeftBottom();
1087 if (rect
.width
== 0 || rect
.height
== 0)
1090 m_graphicContext
->SetBrush( m_graphicContext
->CreateLinearGradientBrush(
1091 start
.x
,start
.y
,end
.x
,end
.y
, initialColour
, destColour
));
1092 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1093 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1094 m_graphicContext
->SetPen(m_pen
);
1095 m_graphicContext
->SetBrush(m_brush
);
1098 void wxGCDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1099 const wxColour
& initialColour
,
1100 const wxColour
& destColour
,
1101 const wxPoint
& circleCenter
)
1104 wxInt32 cx
= rect
.GetWidth() / 2;
1105 wxInt32 cy
= rect
.GetHeight() / 2;
1112 // make sure the background is filled (todo move into specific platform implementation ?)
1113 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1114 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1115 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1117 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1118 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1119 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1120 nRadius
,initialColour
,destColour
));
1122 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1123 m_graphicContext
->SetPen(m_pen
);
1124 m_graphicContext
->SetBrush(m_brush
);
1127 void wxGCDCImpl::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1128 wxCoord width
, wxCoord height
)
1130 wxDCImpl::DoDrawCheckMark(x
,y
,width
,height
);
1134 wxRect
wxGCDCImpl::MSWApplyGDIPlusTransform(const wxRect
& r
) const
1136 wxGraphicsContext
* const gc
= GetGraphicsContext();
1137 wxCHECK_MSG( gc
, r
, wxT("Invalid wxGCDC") );
1141 gc
->GetTransform().TransformPoint(&x
, &y
);
1150 #endif // wxUSE_GRAPHICS_CONTEXT