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 void wxGCDCImpl
::Init(wxGraphicsContext
* ctx
)
212 m_mm_to_pix_x
= mm2pt
;
213 m_mm_to_pix_y
= mm2pt
;
215 m_pen
= *wxBLACK_PEN
;
216 m_font
= *wxNORMAL_FONT
;
217 m_brush
= *wxWHITE_BRUSH
;
219 m_graphicContext
= NULL
;
221 SetGraphicsContext(ctx
);
223 m_logicalFunctionSupported
= true;
226 wxGCDCImpl
::~wxGCDCImpl()
228 delete m_graphicContext
;
231 void wxGCDCImpl
::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
234 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
235 wxCHECK_RET( bmp
.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
237 int w
= bmp
.GetWidth();
238 int h
= bmp
.GetHeight();
239 if ( bmp
.GetDepth() == 1 )
241 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
242 m_graphicContext
->SetBrush( wxBrush( m_textBackgroundColour
, wxSOLID
) );
243 m_graphicContext
->DrawRectangle( x
, y
, w
, h
);
244 m_graphicContext
->SetBrush( wxBrush( m_textForegroundColour
, wxSOLID
) );
245 m_graphicContext
->DrawBitmap( bmp
, x
, y
, w
, h
);
246 m_graphicContext
->SetBrush( m_graphicContext
->CreateBrush(m_brush
));
247 m_graphicContext
->SetPen( m_graphicContext
->CreatePen(m_pen
));
249 else // not a monochrome bitmap, handle it normally
251 // make a copy in case we need to remove its mask, if we don't modify
252 // it the copy is cheap as bitmaps are reference-counted
253 wxBitmap
bmpCopy(bmp
);
254 if ( !useMask
&& bmp
.GetMask() )
255 bmpCopy
.SetMask(NULL
);
257 m_graphicContext
->DrawBitmap( bmpCopy
, x
, y
, w
, h
);
261 void wxGCDCImpl
::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
263 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
264 wxCHECK_RET( icon
.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
266 wxCoord w
= icon
.GetWidth();
267 wxCoord h
= icon
.GetHeight();
269 m_graphicContext
->DrawIcon( icon
, x
, y
, w
, h
);
272 bool wxGCDCImpl
::StartDoc( const wxString
& WXUNUSED(message
) )
277 void wxGCDCImpl
::EndDoc()
281 void wxGCDCImpl
::StartPage()
285 void wxGCDCImpl
::EndPage()
289 void wxGCDCImpl
::Flush()
291 m_graphicContext
->Flush();
294 void wxGCDCImpl
::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
296 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
298 m_graphicContext
->Clip( x
, y
, w
, h
);
301 m_clipX1
= wxMax( m_clipX1
, x
);
302 m_clipY1
= wxMax( m_clipY1
, y
);
303 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
304 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
317 void wxGCDCImpl
::DoSetDeviceClippingRegion( const wxRegion
®ion
)
319 // region is in device coordinates
320 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetDeviceClippingRegion - invalid DC") );
324 //DestroyClippingRegion();
328 wxRegion
logRegion( region
);
331 logRegion
.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
332 logRegion
.GetBox( x
, y
, w
, h
);
334 m_graphicContext
->Clip( logRegion
);
337 m_clipX1
= wxMax( m_clipX1
, x
);
338 m_clipY1
= wxMax( m_clipY1
, y
);
339 m_clipX2
= wxMin( m_clipX2
, (x
+ w
) );
340 m_clipY2
= wxMin( m_clipY2
, (y
+ h
) );
353 void wxGCDCImpl
::DestroyClippingRegion()
355 m_graphicContext
->ResetClip();
356 // currently the clip eg of a window extends to the area between the scrollbars
357 // so we must explicitly make sure it only covers the area we want it to draw
359 GetOwner()->GetSize( &width
, &height
) ;
360 m_graphicContext
->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width
), DeviceToLogicalYRel(height
) );
362 m_graphicContext
->SetPen( m_pen
);
363 m_graphicContext
->SetBrush( m_brush
);
368 void wxGCDCImpl
::DoGetSizeMM( int* width
, int* height
) const
372 GetOwner()->GetSize( &w
, &h
);
374 *width
= long( double(w
) / (m_scaleX
* m_mm_to_pix_x
) );
376 *height
= long( double(h
) / (m_scaleY
* m_mm_to_pix_y
) );
379 void wxGCDCImpl
::SetTextForeground( const wxColour
&col
)
381 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
383 // don't set m_textForegroundColour to an invalid colour as we'd crash
384 // later then (we use m_textForegroundColour.GetColor() without checking
388 m_textForegroundColour
= col
;
389 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
393 void wxGCDCImpl
::SetTextBackground( const wxColour
&col
)
395 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
397 m_textBackgroundColour
= col
;
400 wxSize wxGCDCImpl
::GetPPI() const
402 return wxSize(72, 72);
405 int wxGCDCImpl
::GetDepth() const
410 void wxGCDCImpl
::ComputeScaleAndOrigin()
412 wxDCImpl
::ComputeScaleAndOrigin();
414 if ( m_graphicContext
)
416 m_matrixCurrent
= m_graphicContext
->CreateMatrix();
418 // the logical origin sets the origin to have new coordinates
419 m_matrixCurrent
.Translate( m_deviceOriginX
- m_logicalOriginX
* m_signX
* m_scaleX
,
420 m_deviceOriginY
-m_logicalOriginY
* m_signY
* m_scaleY
);
422 m_matrixCurrent
.Scale( m_scaleX
* m_signX
, m_scaleY
* m_signY
);
424 m_graphicContext
->SetTransform( m_matrixOriginal
);
425 m_graphicContext
->ConcatTransform( m_matrixCurrent
);
429 void wxGCDCImpl
::SetPalette( const wxPalette
& WXUNUSED(palette
) )
434 void wxGCDCImpl
::SetBackgroundMode( int mode
)
436 m_backgroundMode
= mode
;
439 void wxGCDCImpl
::SetFont( const wxFont
&font
)
442 if ( m_graphicContext
)
446 f
.SetPointSize( /*LogicalToDeviceYRel*/(font
.GetPointSize()));
447 m_graphicContext
->SetFont( f
, m_textForegroundColour
);
451 void wxGCDCImpl
::SetPen( const wxPen
&pen
)
454 if ( m_graphicContext
)
456 m_graphicContext
->SetPen( m_pen
);
460 void wxGCDCImpl
::SetBrush( const wxBrush
&brush
)
463 if ( m_graphicContext
)
465 m_graphicContext
->SetBrush( m_brush
);
469 void wxGCDCImpl
::SetBackground( const wxBrush
&brush
)
471 m_backgroundBrush
= brush
;
472 if (!m_backgroundBrush
.IsOk())
476 void wxGCDCImpl
::SetLogicalFunction( wxRasterOperationMode function
)
478 m_logicalFunction
= function
;
480 wxCompositionMode mode
= TranslateRasterOp( function
);
481 m_logicalFunctionSupported
= mode
!= wxCOMPOSITION_INVALID
;
482 if (m_logicalFunctionSupported
)
483 m_logicalFunctionSupported
= m_graphicContext
->SetCompositionMode(mode
);
485 if ( function
== wxXOR
)
486 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
488 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_DEFAULT
);
491 bool wxGCDCImpl
::DoFloodFill(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
492 const wxColour
& WXUNUSED(col
),
493 wxFloodFillStyle
WXUNUSED(style
))
498 bool wxGCDCImpl
::DoGetPixel( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
*WXUNUSED(col
) ) const
500 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
504 void wxGCDCImpl
::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
506 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
508 if ( !m_logicalFunctionSupported
)
511 m_graphicContext
->StrokeLine(x1
,y1
,x2
,y2
);
513 CalcBoundingBox(x1
, y1
);
514 CalcBoundingBox(x2
, y2
);
517 void wxGCDCImpl
::DoCrossHair( wxCoord x
, wxCoord y
)
519 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
521 if ( !m_logicalFunctionSupported
)
526 GetOwner()->GetSize( &w
, &h
);
528 m_graphicContext
->StrokeLine(0,y
,w
,y
);
529 m_graphicContext
->StrokeLine(x
,0,x
,h
);
531 CalcBoundingBox(0, 0);
532 CalcBoundingBox(0+w
, 0+h
);
535 void wxGCDCImpl
::DoDrawArc( wxCoord x1
, wxCoord y1
,
536 wxCoord x2
, wxCoord y2
,
537 wxCoord xc
, wxCoord yc
)
539 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
541 if ( !m_logicalFunctionSupported
)
546 double radius
= sqrt((double)(dx
* dx
+ dy
* dy
));
547 wxCoord rad
= (wxCoord
)radius
;
549 if (x1
== x2
&& y1
== y2
)
554 else if (radius
== 0.0)
560 sa
= (x1
- xc
== 0) ?
561 (y1
- yc
< 0) ?
90.0 : -90.0 :
562 -atan2(double(y1
- yc
), double(x1
- xc
)) * RAD2DEG
;
563 ea
= (x2
- xc
== 0) ?
564 (y2
- yc
< 0) ?
90.0 : -90.0 :
565 -atan2(double(y2
- yc
), double(x2
- xc
)) * RAD2DEG
;
568 bool fill
= m_brush
.GetStyle() != wxTRANSPARENT
;
570 wxGraphicsPath path
= m_graphicContext
->CreatePath();
571 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
572 path
.MoveToPoint( xc
, yc
);
573 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
574 // get clockwise angles
575 path
.AddArc( xc
, yc
, rad
, DegToRad(-sa
) , DegToRad(-ea
), false );
576 if ( fill
&& ((x1
!=x2
)||(y1
!=y2
)) )
577 path
.AddLineToPoint( xc
, yc
);
578 m_graphicContext
->DrawPath(path
);
581 void wxGCDCImpl
::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
582 double sa
, double ea
)
584 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
586 if ( !m_logicalFunctionSupported
)
589 m_graphicContext
->PushState();
590 m_graphicContext
->Translate(x
+w
/2.0,y
+h
/2.0);
591 wxDouble factor
= ((wxDouble
) w
) / h
;
592 m_graphicContext
->Scale( factor
, 1.0);
594 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
595 // get clockwise angles
596 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
598 wxGraphicsPath path
= m_graphicContext
->CreatePath();
599 path
.MoveToPoint( 0, 0 );
600 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
601 path
.AddLineToPoint( 0, 0 );
602 m_graphicContext
->FillPath( path
);
604 path
= m_graphicContext
->CreatePath();
605 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
606 m_graphicContext
->StrokePath( path
);
610 wxGraphicsPath path
= m_graphicContext
->CreatePath();
611 path
.AddArc( 0, 0, h
/2.0 , DegToRad(-sa
) , DegToRad(-ea
), sa
> ea
);
612 m_graphicContext
->DrawPath( path
);
615 m_graphicContext
->PopState();
618 void wxGCDCImpl
::DoDrawPoint( wxCoord x
, wxCoord y
)
620 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
622 DoDrawLine( x
, y
, x
+ 1 , y
+ 1 );
625 void wxGCDCImpl
::DoDrawLines(int n
, wxPoint points
[],
626 wxCoord xoffset
, wxCoord yoffset
)
628 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
630 if ( !m_logicalFunctionSupported
)
633 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
];
634 for( int i
= 0; i
< n
; ++i
)
636 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
637 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
640 m_graphicContext
->StrokeLines( n
, pointsD
);
645 void wxGCDCImpl
::DoDrawSpline(const wxPointList
*points
)
647 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
649 if ( !m_logicalFunctionSupported
)
652 wxGraphicsPath path
= m_graphicContext
->CreatePath();
654 wxPointList
::compatibility_iterator node
= points
->GetFirst();
659 wxPoint
*p
= node
->GetData();
664 node
= node
->GetNext();
669 wxCoord cx1
= ( x1
+ x2
) / 2;
670 wxCoord cy1
= ( y1
+ y2
) / 2;
672 path
.MoveToPoint( x1
, y1
);
673 path
.AddLineToPoint( cx1
, cy1
);
674 #if !wxUSE_STD_CONTAINERS
676 while ((node
= node
->GetNext()) != NULL
)
679 while ((node
= node
->GetNext()))
680 #endif // !wxUSE_STD_CONTAINERS
688 wxCoord cx4
= (x1
+ x2
) / 2;
689 wxCoord cy4
= (y1
+ y2
) / 2;
691 path
.AddQuadCurveToPoint(x1
, y1
,cx4
, cy4
);
697 path
.AddLineToPoint( x2
, y2
);
699 m_graphicContext
->StrokePath( path
);
701 #endif // wxUSE_SPLINES
703 void wxGCDCImpl
::DoDrawPolygon( int n
, wxPoint points
[],
704 wxCoord xoffset
, wxCoord yoffset
,
705 wxPolygonFillMode fillStyle
)
707 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
709 if ( n
<= 0 || (m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
) )
711 if ( !m_logicalFunctionSupported
)
714 bool closeIt
= false;
715 if (points
[n
-1] != points
[0])
718 wxPoint2DDouble
* pointsD
= new wxPoint2DDouble
[n
+(closeIt?
1:0)];
719 for( int i
= 0; i
< n
; ++i
)
721 pointsD
[i
].m_x
= points
[i
].x
+ xoffset
;
722 pointsD
[i
].m_y
= points
[i
].y
+ yoffset
;
725 pointsD
[n
] = pointsD
[0];
727 m_graphicContext
->DrawLines( n
+(closeIt?
1:0) , pointsD
, fillStyle
);
731 void wxGCDCImpl
::DoDrawPolyPolygon(int n
,
736 wxPolygonFillMode fillStyle
)
739 wxGraphicsPath path
= m_graphicContext
->CreatePath();
742 for ( int j
= 0; j
< n
; ++j
)
744 wxPoint start
= points
[i
];
745 path
.MoveToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
748 for ( int k
= 1; k
< l
; ++k
)
750 path
.AddLineToPoint( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
754 if ( start
!= points
[i
-1])
755 path
.AddLineToPoint( start
.x
+ xoffset
, start
.y
+ yoffset
);
757 m_graphicContext
->DrawPath( path
, fillStyle
);
760 void wxGCDCImpl
::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
762 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
764 if ( !m_logicalFunctionSupported
)
767 // CMB: draw nothing if transformed w or h is 0
768 if (w
== 0 || h
== 0)
771 if ( m_graphicContext
->ShouldOffset() )
773 // if we are offsetting the entire rectangle is moved 0.5, so the
774 // border line gets off by 1
778 m_graphicContext
->DrawRectangle(x
,y
,w
,h
);
781 void wxGCDCImpl
::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
782 wxCoord w
, wxCoord h
,
785 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
787 if ( !m_logicalFunctionSupported
)
791 radius
= - radius
* ((w
< h
) ? w
: h
);
793 // CMB: draw nothing if transformed w or h is 0
794 if (w
== 0 || h
== 0)
797 if ( m_graphicContext
->ShouldOffset() )
799 // if we are offsetting the entire rectangle is moved 0.5, so the
800 // border line gets off by 1
804 m_graphicContext
->DrawRoundedRectangle( x
,y
,w
,h
,radius
);
807 void wxGCDCImpl
::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
809 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
811 if ( !m_logicalFunctionSupported
)
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
->DrawEllipse(x
,y
,w
,h
);
824 bool wxGCDCImpl
::CanDrawBitmap() const
829 bool wxGCDCImpl
::DoBlit(
830 wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
831 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
832 wxRasterOperationMode logical_func
, bool useMask
,
833 wxCoord xsrcMask
, wxCoord ysrcMask
)
835 return DoStretchBlit( xdest
, ydest
, width
, height
,
836 source
, xsrc
, ysrc
, width
, height
, logical_func
, useMask
,
840 bool wxGCDCImpl
::DoStretchBlit(
841 wxCoord xdest
, wxCoord ydest
, wxCoord dstWidth
, wxCoord dstHeight
,
842 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, wxCoord srcWidth
, wxCoord srcHeight
,
843 wxRasterOperationMode logical_func
, bool useMask
,
844 wxCoord xsrcMask
, wxCoord ysrcMask
)
846 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
847 wxCHECK_MSG( source
->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
849 if ( logical_func
== wxNO_OP
)
852 wxCompositionMode mode
= TranslateRasterOp(logical_func
);
853 if ( mode
== wxCOMPOSITION_INVALID
)
855 wxFAIL_MSG( wxT("Blitting is not supported with this logical operation.") );
859 wxRect
subrect(source
->LogicalToDeviceX(xsrc
),
860 source
->LogicalToDeviceY(ysrc
),
861 source
->LogicalToDeviceXRel(srcWidth
),
862 source
->LogicalToDeviceYRel(srcHeight
));
863 const wxRect subrectOrig
= subrect
;
864 // clip the subrect down to the size of the source DC
866 source
->GetSize(&clip
.width
, &clip
.height
);
867 subrect
.Intersect(clip
);
868 if (subrect
.width
== 0)
873 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
874 if (m_graphicContext
->SetCompositionMode(mode
))
876 wxAntialiasMode formerAa
= m_graphicContext
->GetAntialiasMode();
877 if (mode
== wxCOMPOSITION_XOR
)
879 m_graphicContext
->SetAntialiasMode(wxANTIALIAS_NONE
);
882 if (xsrcMask
== -1 && ysrcMask
== -1)
888 wxBitmap blit
= source
->GetAsBitmap( &subrect
);
892 if ( !useMask
&& blit
.GetMask() )
898 double h
= dstHeight
;
899 // adjust dest rect if source rect is clipped
900 if (subrect
.width
!= subrectOrig
.width
|| subrect
.height
!= subrectOrig
.height
)
902 x
+= (subrect
.x
- subrectOrig
.x
) / double(subrectOrig
.width
) * dstWidth
;
903 y
+= (subrect
.y
- subrectOrig
.y
) / double(subrectOrig
.height
) * dstHeight
;
904 w
*= double(subrect
.width
) / subrectOrig
.width
;
905 h
*= double(subrect
.height
) / subrectOrig
.height
;
907 m_graphicContext
->DrawBitmap(blit
, x
, y
, w
, h
);
911 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
915 if (mode
== wxCOMPOSITION_XOR
)
917 m_graphicContext
->SetAntialiasMode(formerAa
);
921 m_graphicContext
->SetCompositionMode(formerMode
);
926 void wxGCDCImpl
::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
929 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
933 if ( !m_logicalFunctionSupported
)
936 if ( m_backgroundMode
== wxTRANSPARENT
)
937 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
));
939 m_graphicContext
->DrawText( str
, x
,y
, DegToRad(angle
), m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
942 void wxGCDCImpl
::DoDrawText(const wxString
& str
, wxCoord x
, wxCoord y
)
944 // For compatibility with other ports (notably wxGTK) and because it's
945 // genuinely useful, we allow passing multiline strings to DrawText().
946 // However there is no native OSX function to draw them directly so we
947 // instead reuse the generic DrawLabel() method to render them. Of course,
948 // DrawLabel() itself will call back to us but with single line strings
949 // only so there won't be any infinite recursion here.
950 if ( str
.find('\n') != wxString
::npos
)
952 GetOwner()->DrawLabel(str
, wxRect(x
, y
, 0, 0));
956 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawText - invalid DC") );
961 if ( !m_logicalFunctionSupported
)
964 if ( m_backgroundMode
== wxTRANSPARENT
)
965 m_graphicContext
->DrawText( str
, x
,y
);
967 m_graphicContext
->DrawText( str
, x
,y
, m_graphicContext
->CreateBrush( wxBrush(m_textBackgroundColour
,wxSOLID
) ) );
970 bool wxGCDCImpl
::CanGetTextExtent() const
972 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
977 void wxGCDCImpl
::DoGetTextExtent( const wxString
&str
, wxCoord
*width
, wxCoord
*height
,
978 wxCoord
*descent
, wxCoord
*externalLeading
,
979 const wxFont
*theFont
) const
981 wxCHECK_RET( m_graphicContext
, wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
985 m_graphicContext
->SetFont( *theFont
, m_textForegroundColour
);
988 wxDouble h
, d
, e
, w
;
990 m_graphicContext
->GetTextExtent( str
, &w
, &h
, &d
, &e
);
993 *height
= (wxCoord
)(h
+0.5);
995 *descent
= (wxCoord
)(d
+0.5);
996 if ( externalLeading
)
997 *externalLeading
= (wxCoord
)(e
+0.5);
999 *width
= (wxCoord
)(w
+0.5);
1003 m_graphicContext
->SetFont( m_font
, m_textForegroundColour
);
1007 bool wxGCDCImpl
::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1009 wxCHECK_MSG( m_graphicContext
, false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1011 widths
.Add(0,text
.Length());
1012 if ( text
.IsEmpty() )
1015 wxArrayDouble widthsD
;
1017 m_graphicContext
->GetPartialTextExtents( text
, widthsD
);
1018 for ( size_t i
= 0; i
< widths
.GetCount(); ++i
)
1019 widths
[i
] = (wxCoord
)(widthsD
[i
] + 0.5);
1024 wxCoord wxGCDCImpl
::GetCharWidth(void) const
1027 DoGetTextExtent( wxT("g") , &width
, NULL
, NULL
, NULL
, NULL
);
1032 wxCoord wxGCDCImpl
::GetCharHeight(void) const
1035 DoGetTextExtent( wxT("g") , NULL
, &height
, NULL
, NULL
, NULL
);
1040 void wxGCDCImpl
::Clear(void)
1042 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1043 // TODO better implementation / incorporate size info into wxGCDC or context
1044 m_graphicContext
->SetBrush( m_backgroundBrush
);
1045 wxPen p
= *wxTRANSPARENT_PEN
;
1046 m_graphicContext
->SetPen( p
);
1047 wxCompositionMode formerMode
= m_graphicContext
->GetCompositionMode();
1048 m_graphicContext
->SetCompositionMode(wxCOMPOSITION_SOURCE
);
1049 DoDrawRectangle( 0, 0, INT_MAX
, INT_MAX
);
1050 m_graphicContext
->SetCompositionMode(formerMode
);
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
);
1107 m_graphicContext
->SetBrush(m_brush
);
1110 void wxGCDCImpl
::DoGradientFillConcentric(const wxRect
& rect
,
1111 const wxColour
& initialColour
,
1112 const wxColour
& destColour
,
1113 const wxPoint
& circleCenter
)
1116 wxInt32 cx
= rect
.GetWidth() / 2;
1117 wxInt32 cy
= rect
.GetHeight() / 2;
1124 // make sure the background is filled (todo move into specific platform implementation ?)
1125 m_graphicContext
->SetPen(*wxTRANSPARENT_PEN
);
1126 m_graphicContext
->SetBrush( wxBrush( destColour
) );
1127 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1129 m_graphicContext
->SetBrush( m_graphicContext
->CreateRadialGradientBrush(
1130 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1131 rect
.x
+circleCenter
.x
,rect
.y
+circleCenter
.y
,
1132 nRadius
,initialColour
,destColour
));
1134 m_graphicContext
->DrawRectangle(rect
.x
,rect
.y
,rect
.width
,rect
.height
);
1135 m_graphicContext
->SetPen(m_pen
);
1136 m_graphicContext
->SetBrush(m_brush
);
1139 void wxGCDCImpl
::DoDrawCheckMark(wxCoord x
, wxCoord y
,
1140 wxCoord width
, wxCoord height
)
1142 wxDCImpl
::DoDrawCheckMark(x
,y
,width
,height
);
1146 wxRect wxGCDCImpl
::MSWApplyGDIPlusTransform(const wxRect
& r
) const
1148 wxGraphicsContext
* const gc
= GetGraphicsContext();
1149 wxCHECK_MSG( gc
, r
, wxT("Invalid wxGCDC") );
1153 gc
->GetTransform().TransformPoint(&x
, &y
);
1162 #endif // wxUSE_GRAPHICS_CONTEXT