X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/773ccc318e21e2ce170789aaaaaa18b8b0f1878e..c13d6ac1ea199d1e3908d40d129e2fd5a4d82df7:/src/common/graphcmn.cpp?ds=inline diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index fe68a237c5..8536a68762 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -3,7 +3,7 @@ // Purpose: graphics context methods common to all platforms // Author: Stefan Csomor // Modified by: -// Created: +// Created: // RCS-ID: $Id$ // Copyright: (c) Stefan Csomor // Licence: wxWindows licence @@ -28,15 +28,7 @@ #include "wx/log.h" #endif -#if !defined(wxMAC_USE_CORE_GRAPHICS_BLEND_MODES) -#define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0 -#endif - -//----------------------------------------------------------------------------- -// constants -//----------------------------------------------------------------------------- - -static const double RAD2DEG = 180.0 / M_PI; +#include "wx/private/graphics.h" //----------------------------------------------------------------------------- // Local functions @@ -63,42 +55,42 @@ wxGraphicsObjectRefData::wxGraphicsObjectRefData( const wxGraphicsObjectRefData* { m_renderer = data->m_renderer; } -wxGraphicsRenderer* wxGraphicsObjectRefData::GetRenderer() const -{ - return m_renderer ; +wxGraphicsRenderer* wxGraphicsObjectRefData::GetRenderer() const +{ + return m_renderer ; } -wxGraphicsObjectRefData* wxGraphicsObjectRefData::Clone() const +wxGraphicsObjectRefData* wxGraphicsObjectRefData::Clone() const { return new wxGraphicsObjectRefData(this); } -wxGraphicsObject::wxGraphicsObject() +wxGraphicsObject::wxGraphicsObject() { } -wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer* renderer ) +wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer* renderer ) { SetRefData( new wxGraphicsObjectRefData(renderer)); } -wxGraphicsObject::~wxGraphicsObject() +wxGraphicsObject::~wxGraphicsObject() { } -bool wxGraphicsObject::IsNull() const -{ - return m_refData == NULL; +bool wxGraphicsObject::IsNull() const +{ + return m_refData == NULL; } -wxGraphicsRenderer* wxGraphicsObject::GetRenderer() const -{ - return ( IsNull() ? NULL : GetGraphicsData()->GetRenderer() ); +wxGraphicsRenderer* wxGraphicsObject::GetRenderer() const +{ + return ( IsNull() ? NULL : GetGraphicsData()->GetRenderer() ); } -wxGraphicsObjectRefData* wxGraphicsObject::GetGraphicsData() const -{ - return (wxGraphicsObjectRefData*) m_refData; +wxGraphicsObjectRefData* wxGraphicsObject::GetGraphicsData() const +{ + return (wxGraphicsObjectRefData*) m_refData; } wxObjectRefData* wxGraphicsObject::CreateRefData() const @@ -120,10 +112,12 @@ wxObjectRefData* wxGraphicsObject::CloneRefData(const wxObjectRefData* data) con IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPen, wxGraphicsObject) IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBrush, wxGraphicsObject) IMPLEMENT_DYNAMIC_CLASS(wxGraphicsFont, wxGraphicsObject) +IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBitmap, wxGraphicsObject) WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen; WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush; WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont; +WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap; //----------------------------------------------------------------------------- // matrix @@ -140,13 +134,20 @@ void wxGraphicsMatrix::Concat( const wxGraphicsMatrix *t ) } // sets the matrix to the respective values -void wxGraphicsMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d, +void wxGraphicsMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d, wxDouble tx, wxDouble ty) { AllocExclusive(); GetMatrixData()->Set(a,b,c,d,tx,ty); } +// gets the component valuess of the matrix +void wxGraphicsMatrix::Get(wxDouble* a, wxDouble* b, wxDouble* c, + wxDouble* d, wxDouble* tx, wxDouble* ty) const +{ + GetMatrixData()->Get(a, b, c, d, tx, ty); +} + // makes this the inverse matrix void wxGraphicsMatrix::Invert() { @@ -185,7 +186,7 @@ void wxGraphicsMatrix::Rotate( wxDouble angle ) { AllocExclusive(); GetMatrixData()->Rotate(angle); -} +} // // apply the transforms @@ -247,12 +248,12 @@ void wxGraphicsPath::AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble star wxRect2DDouble wxGraphicsPath::GetBox() const { - wxDouble x,y,w,h; - GetBox(&x,&y,&w,&h); - return wxRect2DDouble( x,y,w,h ); + wxDouble x,y,w,h; + GetBox(&x,&y,&w,&h); + return wxRect2DDouble( x,y,w,h ); } -bool wxGraphicsPath::Contains( const wxPoint2DDouble& c, int fillStyle ) const +bool wxGraphicsPath::Contains( const wxPoint2DDouble& c, wxPolygonFillMode fillStyle ) const { return Contains( c.m_x, c.m_y, fillStyle); } @@ -266,7 +267,7 @@ void wxGraphicsPath::MoveToPoint( wxDouble x, wxDouble y ) GetPathData()->MoveToPoint(x,y); } -// adds a straight line from the current point to (x,y) +// adds a straight line from the current point to (x,y) void wxGraphicsPath::AddLineToPoint( wxDouble x, wxDouble y ) { AllocExclusive(); @@ -308,7 +309,7 @@ void wxGraphicsPath::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startA } // -// These are convenience functions which - if not available natively will be assembled +// These are convenience functions which - if not available natively will be assembled // using the primitives from above // @@ -319,7 +320,7 @@ void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, GetPathData()->AddQuadCurveToPoint(cx,cy,x,y); } -// appends a rectangle as a new closed subpath +// appends a rectangle as a new closed subpath void wxGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) { AllocExclusive(); @@ -334,7 +335,7 @@ void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r ) } // appends a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1) -void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) +void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) { GetPathData()->AddArcToPoint(x1,y1,x2,y2,r); } @@ -378,7 +379,7 @@ void wxGraphicsPath::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) GetPathData()->GetBox(x,y,w,h); } -bool wxGraphicsPath::Contains( wxDouble x, wxDouble y, int fillStyle ) const +bool wxGraphicsPath::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const { return GetPathData()->Contains(x,y,fillStyle); } @@ -420,6 +421,9 @@ void wxGraphicsPathData::AddCircle( wxDouble x, wxDouble y, wxDouble r ) void wxGraphicsPathData::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h) { + if (w <= 0. || h <= 0.) + return; + wxDouble rw = w/2; wxDouble rh = h/2; wxDouble xc = x + rw; @@ -450,7 +454,7 @@ void wxGraphicsPathData::AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1) void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) -{ +{ wxPoint2DDouble current; GetCurrentPoint(¤t.m_x,¤t.m_y); wxPoint2DDouble p1(x1,y1); @@ -472,7 +476,6 @@ void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble dist = r / sin(alpha/2) * cos(alpha/2); // calculate tangential points wxPoint2DDouble t1 = dist*v1 + p1; - wxPoint2DDouble t2 = dist*v2 + p1; wxPoint2DDouble nv1 = v1; nv1.SetVectorAngle(v1.GetVectorAngle()-90); @@ -486,6 +489,41 @@ void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, AddLineToPoint(p2.m_x,p2.m_y); } +//----------------------------------------------------------------------------- +// wxGraphicsGradientStops +//----------------------------------------------------------------------------- + +void wxGraphicsGradientStops::Add(const wxGraphicsGradientStop& stop) +{ + for ( wxVector::iterator it = m_stops.begin(); + it != m_stops.end(); + ++it ) + { + if ( stop.GetPosition() < it->GetPosition() ) + { + if ( it != m_stops.begin() ) + { + m_stops.insert(it, stop); + } + else // we shouldn't be inserting it at the beginning + { + wxFAIL_MSG( "invalid gradient stop position < 0" ); + } + + return; + } + } + + if ( stop.GetPosition() == 1. ) + { + m_stops.insert(m_stops.end() - 1, stop); + } + else + { + wxFAIL_MSG( "invalid gradient stop position > 1" ); + } +} + //----------------------------------------------------------------------------- // wxGraphicsContext Convenience Methods //----------------------------------------------------------------------------- @@ -493,63 +531,115 @@ void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext, wxObject) -wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) +wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) : + wxGraphicsObject(renderer), + m_antialias(wxANTIALIAS_DEFAULT), + m_composition(wxCOMPOSITION_OVER), + m_enableOffset(false) { } -wxGraphicsContext::~wxGraphicsContext() +wxGraphicsContext::~wxGraphicsContext() { } +bool wxGraphicsContext::StartDoc(const wxString& WXUNUSED(message)) +{ + return true; +} + +void wxGraphicsContext::EndDoc() +{ +} + +void wxGraphicsContext::StartPage(wxDouble WXUNUSED(width), + wxDouble WXUNUSED(height)) +{ +} + +void wxGraphicsContext::EndPage() +{ +} + +void wxGraphicsContext::Flush() +{ +} + +void wxGraphicsContext::EnableOffset(bool enable) +{ + m_enableOffset = enable; +} + +#if 0 +void wxGraphicsContext::SetAlpha( wxDouble WXUNUSED(alpha) ) +{ +} + +wxDouble wxGraphicsContext::GetAlpha() const +{ + return 1.0; +} +#endif + +void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY) +{ + *dpiX = 72.0; + *dpiY = 72.0; +} + // sets the pen -void wxGraphicsContext::SetPen( const wxGraphicsPen& pen ) +void wxGraphicsContext::SetPen( const wxGraphicsPen& pen ) { m_pen = pen; } void wxGraphicsContext::SetPen( const wxPen& pen ) { - if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT ) + if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT ) SetPen( wxNullGraphicsPen ); else SetPen( CreatePen( pen ) ); } - + // sets the brush for filling -void wxGraphicsContext::SetBrush( const wxGraphicsBrush& brush ) +void wxGraphicsContext::SetBrush( const wxGraphicsBrush& brush ) { m_brush = brush; } void wxGraphicsContext::SetBrush( const wxBrush& brush ) { - if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT ) + if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT ) SetBrush( wxNullGraphicsBrush ); else SetBrush( CreateBrush( brush ) ); } // sets the brush for filling -void wxGraphicsContext::SetFont( const wxGraphicsFont& font ) +void wxGraphicsContext::SetFont( const wxGraphicsFont& font ) { m_font = font; } void wxGraphicsContext::SetFont( const wxFont& font, const wxColour& colour ) { - if ( font.Ok() ) + if ( font.IsOk() ) SetFont( CreateFont( font, colour ) ); else SetFont( wxNullGraphicsFont ); } -void wxGraphicsContext::DrawPath( const wxGraphicsPath& path, int fillStyle ) +void wxGraphicsContext::DrawPath( const wxGraphicsPath& path, wxPolygonFillMode fillStyle ) { FillPath( path , fillStyle ); StrokePath( path ); } -void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle ) +void +wxGraphicsContext::DoDrawRotatedText(const wxString &str, + wxDouble x, + wxDouble y, + wxDouble angle) { Translate(x,y); Rotate( -angle ); @@ -558,27 +648,38 @@ void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, w Translate(-x,-y); } -void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, const wxGraphicsBrush& backgroundBrush ) +void +wxGraphicsContext::DoDrawFilledText(const wxString &str, + wxDouble x, + wxDouble y, + const wxGraphicsBrush& backgroundBrush) { wxGraphicsBrush formerBrush = m_brush; + wxGraphicsPen formerPen = m_pen; wxDouble width; wxDouble height; wxDouble descent; wxDouble externalLeading; GetTextExtent( str , &width, &height, &descent, &externalLeading ); SetBrush( backgroundBrush ); + // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape + SetPen( wxNullGraphicsPen ); - wxGraphicsPath path = CreatePath(); - path.AddRectangle( x , y, width, height ); - FillPath( path ); + DrawRectangle(x , y, width, height); DrawText( str, x ,y); SetBrush( formerBrush ); + SetPen( formerPen ); } -void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle, const wxGraphicsBrush& backgroundBrush ) +void +wxGraphicsContext::DoDrawRotatedFilledText(const wxString &str, + wxDouble x, wxDouble y, + wxDouble angle, + const wxGraphicsBrush& backgroundBrush) { wxGraphicsBrush formerBrush = m_brush; + wxGraphicsPen formerPen = m_pen; wxDouble width; wxDouble height; @@ -586,6 +687,8 @@ void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, w wxDouble externalLeading; GetTextExtent( str , &width, &height, &descent, &externalLeading ); SetBrush( backgroundBrush ); + // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape + SetPen( wxNullGraphicsPen ); wxGraphicsPath path = CreatePath(); path.MoveToPoint( x , y ); @@ -597,6 +700,7 @@ void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, w FillPath( path ); DrawText( str, x ,y, angle); SetBrush( formerBrush ); + SetPen( formerPen ); } void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2) @@ -638,7 +742,7 @@ void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points) StrokePath( path ); } -void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle) +void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle) { wxASSERT(n > 1); wxGraphicsPath path = CreatePath(); @@ -661,7 +765,7 @@ void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoint } // create a 'native' matrix corresponding to these values -wxGraphicsMatrix wxGraphicsContext::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d, +wxGraphicsMatrix wxGraphicsContext::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d, wxDouble tx, wxDouble ty) const { return GetRenderer()->CreateMatrix(a,b,c,d,tx,ty); @@ -682,19 +786,55 @@ wxGraphicsBrush wxGraphicsContext::CreateBrush(const wxBrush& brush ) const return GetRenderer()->CreateBrush(brush); } -// sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2 -wxGraphicsBrush wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, - const wxColour&c1, const wxColour&c2) const +wxGraphicsBrush +wxGraphicsContext::CreateLinearGradientBrush( + wxDouble x1, wxDouble y1, + wxDouble x2, wxDouble y2, + const wxColour& c1, const wxColour& c2) const { - return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2,c1,c2); + return GetRenderer()->CreateLinearGradientBrush + ( + x1, y1, + x2, y2, + wxGraphicsGradientStops(c1,c2) + ); } -// sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) -// with radius r and color cColor -wxGraphicsBrush wxGraphicsContext::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, - const wxColour &oColor, const wxColour &cColor) const +wxGraphicsBrush +wxGraphicsContext::CreateLinearGradientBrush( + wxDouble x1, wxDouble y1, + wxDouble x2, wxDouble y2, + const wxGraphicsGradientStops& gradientStops) const { - return GetRenderer()->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor); + return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2, gradientStops); +} + +wxGraphicsBrush +wxGraphicsContext::CreateRadialGradientBrush( + wxDouble xo, wxDouble yo, + wxDouble xc, wxDouble yc, wxDouble radius, + const wxColour &oColor, const wxColour &cColor) const +{ + return GetRenderer()->CreateRadialGradientBrush + ( + xo, yo, + xc, yc, radius, + wxGraphicsGradientStops(oColor, cColor) + ); +} + +wxGraphicsBrush +wxGraphicsContext::CreateRadialGradientBrush( + wxDouble xo, wxDouble yo, + wxDouble xc, wxDouble yc, wxDouble radius, + const wxGraphicsGradientStops& gradientStops) const +{ + return GetRenderer()->CreateRadialGradientBrush + ( + xo, yo, + xc, yc, radius, + gradientStops + ); } // sets the font @@ -703,16 +843,41 @@ wxGraphicsFont wxGraphicsContext::CreateFont( const wxFont &font , const wxColou return GetRenderer()->CreateFont(font,col); } -wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc) +wxGraphicsBitmap wxGraphicsContext::CreateBitmap( const wxBitmap& bmp ) const +{ + return GetRenderer()->CreateBitmap(bmp); +} + +wxGraphicsBitmap wxGraphicsContext::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) const +{ + return GetRenderer()->CreateSubBitmap(bmp,x,y,w,h); +} + +/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc) { return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc); } + +/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxMemoryDC& dc) +{ + return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc); +} + +#if wxUSE_PRINTING_ARCHITECTURE +/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxPrinterDC& dc) +{ + return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc); +} +#endif + #ifdef __WXMSW__ -wxGraphicsContext* wxGraphicsContext::Create( const wxMemoryDC& dc) +#if wxUSE_ENH_METAFILE +/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxEnhMetaFileDC& dc) { return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc); } #endif +#endif wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context ) {