X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2986407b5416ec85c0a9928d26e4ef84dfb1d75d..850df2d78866c3edcf848103b5dbc7e7fa1ee5fa:/src/common/dcgraph.cpp?ds=inline diff --git a/src/common/dcgraph.cpp b/src/common/dcgraph.cpp index c0bcd97b1a..54ecf302cb 100644 --- a/src/common/dcgraph.cpp +++ b/src/common/dcgraph.cpp @@ -27,10 +27,9 @@ #include "wx/region.h" #endif -#ifndef wxMAC_USE_CORE_GRAPHICS_BLEND_MODES - #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0 +#ifdef __WXMAC__ +#include "wx/mac/private.h" #endif - //----------------------------------------------------------------------------- // constants //----------------------------------------------------------------------------- @@ -69,6 +68,11 @@ void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx ) { m_matrixOriginal = m_graphicContext->GetTransform(); m_ok = true; + // apply the stored transformations to the passed in context + ComputeScaleAndOrigin(); + m_graphicContext->SetFont( m_font , m_textForegroundColour ); + m_graphicContext->SetPen( m_pen ); + m_graphicContext->SetBrush( m_brush); } } @@ -76,14 +80,16 @@ wxGCDC::wxGCDC(const wxWindowDC& dc) { Init(); SetGraphicsContext( wxGraphicsContext::Create(dc) ); - if ( dc.GetFont().Ok()) - m_graphicContext->SetFont( m_graphicContext->CreateFont(dc.GetFont(),dc.GetTextForeground())); - if ( dc.GetPen().Ok()) - m_graphicContext->SetPen( m_graphicContext->CreatePen(dc.GetPen())); - if ( dc.GetBrush().Ok()) - m_graphicContext->SetBrush( m_graphicContext->CreateBrush(dc.GetBrush())); } +#ifdef __WXMSW__ +wxGCDC::wxGCDC(const wxMemoryDC& dc) +{ + Init(); + SetGraphicsContext( wxGraphicsContext::Create(dc) ); +} +#endif + void wxGCDC::Init() { m_ok = false; @@ -96,6 +102,7 @@ void wxGCDC::Init() m_brush = *wxWHITE_BRUSH; m_graphicContext = NULL; + m_logicalFunctionSupported = true; } @@ -109,7 +116,18 @@ void wxGCDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNU wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") ); wxCHECK_RET( bmp.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") ); - m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() ); + if ( bmp.GetDepth() == 1 ) + { + m_graphicContext->SetPen(*wxTRANSPARENT_PEN); + m_graphicContext->SetBrush( wxBrush( m_textBackgroundColour , wxSOLID ) ); + m_graphicContext->DrawRectangle( x , y , bmp.GetWidth() , bmp.GetHeight() ); + m_graphicContext->SetBrush( wxBrush( m_textForegroundColour , wxSOLID ) ); + m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() ); + m_graphicContext->SetBrush( m_graphicContext->CreateBrush(m_brush)); + m_graphicContext->SetPen( m_graphicContext->CreatePen(m_pen)); + } + else + m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() ); } void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y ) @@ -123,6 +141,30 @@ void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y ) m_graphicContext->DrawIcon( icon , x, y, w, h ); } +bool wxGCDC::StartDoc( const wxString& WXUNUSED(message) ) +{ + return false; +} + +void wxGCDC::EndDoc() +{ +} + +void wxGCDC::StartPage() +{ +} + +void wxGCDC::EndPage() +{ +} + +void wxGCDC::Flush() +{ +#ifdef __WXMAC__ + CGContextFlush( (CGContextRef) m_graphicContext->GetNativeContext() ); +#endif +} + void wxGCDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord w, wxCoord h ) { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") ); @@ -148,18 +190,22 @@ void wxGCDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord w, wxCoord h ) void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion ®ion ) { + // region is in device coordinates wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") ); if (region.Empty()) { - DestroyClippingRegion(); + //DestroyClippingRegion(); return; } + wxRegion logRegion( region ); wxCoord x, y, w, h; - region.GetBox( x, y, w, h ); - m_graphicContext->Clip( region ); + logRegion.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) ); + logRegion.GetBox( x, y, w, h ); + + m_graphicContext->Clip( logRegion ); if ( m_clipping ) { m_clipX1 = wxMax( m_clipX1, x ); @@ -181,6 +227,12 @@ void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion ®ion ) void wxGCDC::DestroyClippingRegion() { m_graphicContext->ResetClip(); + // currently the clip eg of a window extends to the area between the scrollbars + // so we must explicitely make sure it only covers the area we want it to draw + int width, height ; + GetSize( &width , &height ) ; + m_graphicContext->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width), DeviceToLogicalYRel(height) ); + m_graphicContext->SetPen( m_pen ); m_graphicContext->SetBrush( m_brush ); @@ -298,14 +350,17 @@ void wxGCDC::ComputeScaleAndOrigin() m_scaleX = m_logicalScaleX * m_userScaleX; m_scaleY = m_logicalScaleY * m_userScaleY; - m_matrixCurrent = m_graphicContext->CreateMatrix(); - m_matrixCurrent.Translate( m_deviceOriginX, m_deviceOriginY ); - m_matrixCurrent.Scale( m_scaleX, m_scaleY ); - // the logical origin sets the origin to have new coordinates - m_matrixCurrent.Translate( -m_logicalOriginX, -m_logicalOriginY ); - - m_graphicContext->SetTransform( m_matrixOriginal ); - m_graphicContext->ConcatTransform( m_matrixCurrent ); + if ( m_graphicContext ) + { + m_matrixCurrent = m_graphicContext->CreateMatrix(); + m_matrixCurrent.Translate( m_deviceOriginX, m_deviceOriginY ); + m_matrixCurrent.Scale( m_scaleX, m_scaleY ); + // the logical origin sets the origin to have new coordinates + m_matrixCurrent.Translate( -m_logicalOriginX, -m_logicalOriginY ); + + m_graphicContext->SetTransform( m_matrixOriginal ); + m_graphicContext->ConcatTransform( m_matrixCurrent ); + } } void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) ) @@ -353,7 +408,7 @@ void wxGCDC::SetBrush( const wxBrush &brush ) m_graphicContext->SetBrush( m_brush ); } } - + void wxGCDC::SetBackground( const wxBrush &brush ) { if (m_backgroundBrush == brush) @@ -370,17 +425,10 @@ void wxGCDC::SetLogicalFunction( int function ) return; m_logicalFunction = function; -#if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES - - CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext(); - if ( m_logicalFunction == wxCOPY ) - CGContextSetBlendMode( cgContext, kCGBlendModeNormal ); - else if ( m_logicalFunction == wxINVERT ) - CGContextSetBlendMode( cgContext, kCGBlendModeExclusion ); + if ( m_graphicContext->SetLogicalFunction( function ) ) + m_logicalFunctionSupported=true; else - CGContextSetBlendMode( cgContext, kCGBlendModeNormal ); -#endif - + m_logicalFunctionSupported=false; } bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), @@ -399,11 +447,8 @@ void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 ) { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") ); -#if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES - - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; -#endif m_graphicContext->StrokeLine(x1,y1,x2,y2); @@ -415,7 +460,7 @@ void wxGCDC::DoCrossHair( wxCoord x, wxCoord y ) { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") ); - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; int w = 0, h = 0; @@ -435,7 +480,7 @@ void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1, { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") ); - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; double dx = x1 - xc; @@ -467,7 +512,9 @@ void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1, wxGraphicsPath path = m_graphicContext->CreatePath(); if ( fill && ((x1!=x2)||(y1!=y2)) ) path.MoveToPoint( xc, yc ); - path.AddArc( xc, yc , rad , DegToRad(sa) , DegToRad(ea), false ); + // since these angles (ea,sa) are measured counter-clockwise, we invert them to + // get clockwise angles + path.AddArc( xc, yc , rad , DegToRad(-sa) , DegToRad(-ea), false ); if ( fill && ((x1!=x2)||(y1!=y2)) ) path.AddLineToPoint( xc, yc ); m_graphicContext->DrawPath(path); @@ -478,24 +525,37 @@ void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h, { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") ); - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; - bool fill = m_brush.GetStyle() != wxTRANSPARENT; - - wxGraphicsPath path = m_graphicContext->CreatePath(); m_graphicContext->PushState(); - m_graphicContext->Translate(x+w/2,y+h/2); + m_graphicContext->Translate(x+w/2.0,y+h/2.0); wxDouble factor = ((wxDouble) w) / h; m_graphicContext->Scale( factor , 1.0); - if ( fill && (sa!=ea) ) - path.MoveToPoint(0,0); + // since these angles (ea,sa) are measured counter-clockwise, we invert them to // get clockwise angles - path.AddArc( 0, 0, h/2 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); - if ( fill && (sa!=ea) ) - path.AddLineToPoint(0,0); + if ( m_brush.GetStyle() != wxTRANSPARENT ) + { + wxGraphicsPath path = m_graphicContext->CreatePath(); + path.MoveToPoint( 0, 0 ); + path.AddLineToPoint( h / 2.0 * cos(DegToRad(sa)) , h / 2.0 * sin(DegToRad(-sa)) ); + path.AddLineToPoint( h / 2.0 * cos(DegToRad(ea)) , h / 2.0 * sin(DegToRad(-ea)) ); + path.AddLineToPoint( 0, 0 ); + m_graphicContext->FillPath( path ); + + path = m_graphicContext->CreatePath(); + path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); + m_graphicContext->FillPath( path ); + m_graphicContext->StrokePath( path ); + } + else + { + wxGraphicsPath path = m_graphicContext->CreatePath(); + path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); m_graphicContext->DrawPath( path ); + } + m_graphicContext->PopState(); } @@ -511,11 +571,8 @@ void wxGCDC::DoDrawLines(int n, wxPoint points[], { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") ); -#if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES - - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; -#endif wxPoint2DDouble* pointsD = new wxPoint2DDouble[n]; for( int i = 0; i < n; ++i) @@ -529,27 +586,27 @@ void wxGCDC::DoDrawLines(int n, wxPoint points[], } #if wxUSE_SPLINES -void wxGCDC::DoDrawSpline(wxList *points) +void wxGCDC::DoDrawSpline(const wxPointList *points) { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") ); - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; wxGraphicsPath path = m_graphicContext->CreatePath(); - wxList::compatibility_iterator node = points->GetFirst(); - if (node == wxList::compatibility_iterator()) + wxPointList::compatibility_iterator node = points->GetFirst(); + if (node == wxPointList::compatibility_iterator()) // empty list return; - wxPoint *p = (wxPoint *)node->GetData(); + wxPoint *p = node->GetData(); wxCoord x1 = p->x; wxCoord y1 = p->y; node = node->GetNext(); - p = (wxPoint *)node->GetData(); + p = node->GetData(); wxCoord x2 = p->x; wxCoord y2 = p->y; @@ -567,7 +624,7 @@ void wxGCDC::DoDrawSpline(wxList *points) #endif // !wxUSE_STL { - p = (wxPoint *)node->GetData(); + p = node->GetData(); x1 = x2; y1 = y2; x2 = p->x; @@ -595,7 +652,7 @@ void wxGCDC::DoDrawPolygon( int n, wxPoint points[], if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) ) return; - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; bool closeIt = false; @@ -648,7 +705,7 @@ void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord w, wxCoord h) { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") ); - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; // CMB: draw nothing if transformed w or h is 0 @@ -671,7 +728,7 @@ void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") ); - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; if (radius < 0.0) @@ -681,6 +738,13 @@ void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, if (w == 0 || h == 0) return; + if ( m_graphicContext->ShouldOffset() ) + { + // if we are offsetting the entire rectangle is moved 0.5, so the + // border line gets off by 1 + w -= 1; + h -= 1; + } m_graphicContext->DrawRoundedRectangle( x,y,w,h,radius); } @@ -688,9 +752,16 @@ void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord w, wxCoord h) { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") ); - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; + if ( m_graphicContext->ShouldOffset() ) + { + // if we are offsetting the entire rectangle is moved 0.5, so the + // border line gets off by 1 + w -= 1; + h -= 1; + } m_graphicContext->DrawEllipse(x,y,w,h); } @@ -701,15 +772,27 @@ bool wxGCDC::CanDrawBitmap() const bool wxGCDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, - wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool WXUNUSED(useMask), + wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask, + wxCoord xsrcMask, wxCoord ysrcMask ) +{ + return DoStretchBlit( xdest, ydest, width, height, + source, xsrc, ysrc, width, height, logical_func, useMask, + xsrcMask,ysrcMask ); +} + +bool wxGCDC::DoStretchBlit( + wxCoord xdest, wxCoord ydest, wxCoord dstWidth, wxCoord dstHeight, + wxDC *source, wxCoord xsrc, wxCoord ysrc, wxCoord srcWidth, wxCoord srcHeight, + int logical_func , bool WXUNUSED(useMask), wxCoord xsrcMask, wxCoord ysrcMask ) { - wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") ); - wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") ); + wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") ); + wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") ); if ( logical_func == wxNO_OP ) return true; - else if ( logical_func != wxCOPY ) + else if ( !m_graphicContext->SetLogicalFunction( logical_func ) ) + { wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") ); return false; @@ -721,14 +804,27 @@ bool wxGCDC::DoBlit( ysrcMask = ysrc; } - wxRect subrect(source-> LogicalToDeviceX(xsrc),source-> LogicalToDeviceY(ysrc), - source-> LogicalToDeviceXRel(width),source-> LogicalToDeviceYRel(height)); + wxRect subrect(source->LogicalToDeviceX(xsrc), + source->LogicalToDeviceY(ysrc), + source->LogicalToDeviceXRel(srcWidth), + source->LogicalToDeviceYRel(srcHeight)); + + // if needed clip the subrect down to the size of the source DC + wxCoord sw, sh; + source->GetSize(&sw, &sh); + sw = source->LogicalToDeviceXRel(sw); + sh = source->LogicalToDeviceYRel(sh); + if (subrect.x + subrect.width > sw) + subrect.width = sw - subrect.x; + if (subrect.y + subrect.height > sh) + subrect.height = sh - subrect.y; wxBitmap blit = source->GetAsBitmap( &subrect ); if ( blit.Ok() ) { - m_graphicContext->DrawBitmap( blit, xdest , ydest , width , height ); + m_graphicContext->DrawBitmap( blit, xdest, ydest, + dstWidth, dstHeight); } else { @@ -736,6 +832,9 @@ bool wxGCDC::DoBlit( return false; } + // reset logical function + m_graphicContext->SetLogicalFunction( m_logicalFunction ); + return true; } @@ -746,10 +845,13 @@ void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y, if ( str.length() == 0 ) return; - if ( m_logicalFunction != wxCOPY ) + if ( !m_logicalFunctionSupported ) return; - m_graphicContext->DrawText( str, x ,y , DegToRad(angle )); + if ( m_backgroundMode == wxTRANSPARENT ) + m_graphicContext->DrawText( str, x ,y , DegToRad(angle )); + else + m_graphicContext->DrawText( str, x ,y , DegToRad(angle ), m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) ); } void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y) @@ -758,10 +860,14 @@ void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y) if ( str.length() == 0 ) return; - if ( m_logicalFunction != wxCOPY ) + + if ( !m_logicalFunctionSupported ) return; - m_graphicContext->DrawText( str, x ,y); + if ( m_backgroundMode == wxTRANSPARENT ) + m_graphicContext->DrawText( str, x ,y); + else + m_graphicContext->DrawText( str, x ,y , m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) ); } bool wxGCDC::CanGetTextExtent() const @@ -773,7 +879,7 @@ bool wxGCDC::CanGetTextExtent() const void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height, wxCoord *descent, wxCoord *externalLeading , - wxFont *theFont ) const + const wxFont *theFont ) const { wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") ); @@ -787,13 +893,13 @@ void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *heig m_graphicContext->GetTextExtent( str, &w, &h, &d, &e ); if ( height ) - *height = (wxCoord)h; + *height = (wxCoord)(h+0.5); if ( descent ) - *descent = (wxCoord)d; + *descent = (wxCoord)(d+0.5); if ( externalLeading ) - *externalLeading = (wxCoord)e; + *externalLeading = (wxCoord)(e+0.5); if ( width ) - *width = (wxCoord)w; + *width = (wxCoord)(w+0.5); if ( theFont ) { @@ -848,8 +954,8 @@ void wxGCDC::Clear(void) void wxGCDC::DoGetSize(int *width, int *height) const { - *width = 1000; - *height = 1000; + *width = 10000; + *height = 10000; } void wxGCDC::DoGradientFillLinear(const wxRect& rect,