start of unwinding reference OutputDevice & Printer lifecycle.

Change-Id: I066337f46a3c39d1eaba5e7a1f4e1278816a0b27
diff --git a/chart2/source/inc/chartview/DrawModelWrapper.hxx b/chart2/source/inc/chartview/DrawModelWrapper.hxx
index 986a6b9..0e590a6 100644
--- a/chart2/source/inc/chartview/DrawModelWrapper.hxx
+++ b/chart2/source/inc/chartview/DrawModelWrapper.hxx
@@ -41,7 +41,7 @@ private:
    ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage > m_xMainDrawPage;
    ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage > m_xHiddenDrawPage;

    boost::scoped_ptr<OutputDevice> m_pRefDevice;
    VclPtr<OutputDevice> m_pRefDevice;

    //no default constructor
    DrawModelWrapper();
diff --git a/chart2/source/view/main/DrawModelWrapper.cxx b/chart2/source/view/main/DrawModelWrapper.cxx
index d917ee1..c65af68 100644
--- a/chart2/source/view/main/DrawModelWrapper.cxx
+++ b/chart2/source/view/main/DrawModelWrapper.cxx
@@ -150,7 +150,8 @@ DrawModelWrapper::DrawModelWrapper( const uno::Reference<uno::XComponentContext>
    OutputDevice* pDefaultDevice = rOutliner.GetRefDevice();
    if( !pDefaultDevice )
        pDefaultDevice = Application::GetDefaultDevice();
    m_pRefDevice.reset(new VirtualDevice(*pDefaultDevice));
    m_pRefDevice.disposeAndClear();
    m_pRefDevice = new VirtualDevice(*pDefaultDevice);
    MapMode aMapMode = m_pRefDevice->GetMapMode();
    aMapMode.SetMapUnit(MAP_100TH_MM);
    m_pRefDevice->SetMapMode(aMapMode);
@@ -176,6 +177,7 @@ DrawModelWrapper::~DrawModelWrapper()
        }
        SfxItemPool::Free(m_pChartItemPool);
    }
    m_pRefDevice.disposeAndClear();
}

uno::Reference< uno::XInterface > DrawModelWrapper
diff --git a/editeng/source/editeng/impedit.hxx b/editeng/source/editeng/impedit.hxx
index 80101aa..ac5db16 100644
--- a/editeng/source/editeng/impedit.hxx
+++ b/editeng/source/editeng/impedit.hxx
@@ -421,8 +421,8 @@ private:
    SfxStyleSheetPool*  pStylePool;
    SfxItemPool*        pTextObjectPool;

    VirtualDevice*      pVirtDev;
    OutputDevice*       pRefDev;
    VclPtr< VirtualDevice> pVirtDev;
    VclPtr< OutputDevice > pRefDev;

    svtools::ColorConfig*   pColorConfig;
    mutable SvtCTLOptions*  pCTLOptions;
@@ -663,7 +663,7 @@ private:
    bool                IsForceAutoColor() const { return bForceAutoColor; }

    inline VirtualDevice*   GetVirtualDevice( const MapMode& rMapMode, sal_uLong nDrawMode );
    inline void             EraseVirtualDevice();
    inline void             EraseVirtualDevice() { pVirtDev.disposeAndClear(); }

    DECL_LINK(StatusTimerHdl, void *);
    DECL_LINK(IdleFormatHdl, void *);
@@ -874,7 +874,7 @@ public:
    void                UpdateParagraphsWithStyleSheet( SfxStyleSheet* pStyle );
    void                RemoveStyleFromParagraphs( SfxStyleSheet* pStyle );

    OutputDevice*       GetRefDevice() const { return pRefDev; }
    OutputDevice*       GetRefDevice() const { return pRefDev.get(); }
    void                SetRefDevice( OutputDevice* pRefDef );

    const MapMode&      GetRefMapMode() { return pRefDev->GetMapMode(); }
@@ -1084,12 +1084,6 @@ inline VirtualDevice* ImpEditEngine::GetVirtualDevice( const MapMode& rMapMode, 
    return pVirtDev;
}

inline void ImpEditEngine::EraseVirtualDevice()
{
    delete pVirtDev;
    pVirtDev = 0;
}

inline void ImpEditEngine::IdleFormatAndUpdate( EditView* pCurView )
{
    aIdleFormatter.DoIdleFormat( pCurView );
diff --git a/editeng/source/editeng/impedit2.cxx b/editeng/source/editeng/impedit2.cxx
index bb964c1..14f0826 100644
--- a/editeng/source/editeng/impedit2.cxx
+++ b/editeng/source/editeng/impedit2.cxx
@@ -172,7 +172,7 @@ ImpEditEngine::~ImpEditEngine()
    bDowning = true;
    SetUpdateMode( false );

    delete pVirtDev;
    pVirtDev.disposeAndClear();
    delete pEmptyItemSet;
    delete pUndoManager;
    delete pTextRanger;
@@ -180,14 +180,14 @@ ImpEditEngine::~ImpEditEngine()
    delete pColorConfig;
    delete pCTLOptions;
    if ( bOwnerOfRefDev )
        delete pRefDev;
        pRefDev.disposeAndClear();
    delete pSpellInfo;
}

void ImpEditEngine::SetRefDevice( OutputDevice* pRef )
{
    if ( bOwnerOfRefDev )
        delete pRefDev;
        pRefDev.disposeAndClear();

    if ( !pRef )
    {
diff --git a/include/svx/svdmodel.hxx b/include/svx/svdmodel.hxx
index 4d238a2..e2f7e25 100644
--- a/include/svx/svdmodel.hxx
+++ b/include/svx/svdmodel.hxx
@@ -170,7 +170,7 @@ protected:
    SdrOutliner*    pDrawOutliner;  // an Outliner for outputting text
    SdrOutliner*    pHitTestOutliner;// an Outliner for the HitTest
    sal_uIntPtr           nDefTextHgt;    // Default text heigth in logical units
    OutputDevice*   pRefOutDev;     // ReferenceDevice for the EditEngine
    Vclptr<OutputDevice>  pRefOutDev;     // ReferenceDevice for the EditEngine
    /// Set if we are doing tiled rendering.
    bool mbTiledRendering;
    LibreOfficeKitCallback mpLibreOfficeKitCallback;
@@ -335,7 +335,7 @@ public:

    // ReferenceDevice for the EditEngine
    void                 SetRefDevice(OutputDevice* pDev);
    OutputDevice*        GetRefDevice() const                   { return pRefOutDev; }
    OutputDevice*        GetRefDevice() const                   { return pRefOutDev.get(); }
    /// Set if we are doing tiled rendering.
    void setTiledRendering(bool bTiledRendering);
    /// Are we doing tiled rendering?
diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx
index a9f5380..6e9e247 100644
--- a/sc/inc/document.hxx
+++ b/sc/inc/document.hxx
@@ -285,8 +285,8 @@ private:
    ScFieldEditEngine*  pEditEngine;                    // uses pEditPool from xPoolHelper
    ScNoteEditEngine*   pNoteEngine;                    // uses pEditPool from xPoolHelper
    SfxObjectShell*     pShell;
    SfxPrinter*         pPrinter;
    VirtualDevice*      pVirtualDevice_100th_mm;
    VclPtr<SfxPrinter>    pPrinter;
    VclPtr<VirtualDevice> pVirtualDevice_100th_mm;
    ScDrawLayer*        pDrawLayer;                     // SdrModel
    rtl::Reference<XColorList> pColorList;
    ScValidationDataList* pValidationList;              // validity
diff --git a/sc/source/core/data/documen2.cxx b/sc/source/core/data/documen2.cxx
index 15a5a46..05274b5 100644
--- a/sc/source/core/data/documen2.cxx
+++ b/sc/source/core/data/documen2.cxx
@@ -417,7 +417,7 @@ ScDocument::~ScDocument()
    delete pChartCollection;
    DeleteDrawLayer();
    delete pFormatExchangeList;
    delete pPrinter;
    pPrinter.disposeAndClear();
    ImplDeleteOptions();
    delete pConsolidateDlgData;
    delete pClipData;
@@ -426,7 +426,7 @@ ScDocument::~ScDocument()
    delete pEditEngine;
    delete pNoteEngine;
    delete pChangeViewSettings;         // and delete
    delete pVirtualDevice_100th_mm;
    pVirtualDevice_100th_mm.disposeAndClear();

    delete pDPCollection;

diff --git a/sc/source/core/data/documen8.cxx b/sc/source/core/data/documen8.cxx
index 74b233e..101c1db 100644
--- a/sc/source/core/data/documen8.cxx
+++ b/sc/source/core/data/documen8.cxx
@@ -149,7 +149,7 @@ SfxPrinter* ScDocument::GetPrinter(bool bCreateIfNotExist)

void ScDocument::SetPrinter( SfxPrinter* pNewPrinter )
{
    if ( pNewPrinter == pPrinter )
    if ( pNewPrinter == pPrinter.get() )
    {
        //  #i6706# SetPrinter is called with the same printer again if
        //  the JobSetup has changed. In that case just call UpdateDrawPrinter
@@ -158,11 +158,11 @@ void ScDocument::SetPrinter( SfxPrinter* pNewPrinter )
    }
    else
    {
        SfxPrinter* pOld = pPrinter;
        VclPtr<SfxPrinter> pOld = pPrinter;
        pPrinter = pNewPrinter;
        UpdateDrawPrinter();
        pPrinter->SetDigitLanguage( SC_MOD()->GetOptDigitLanguage() );
        delete pOld;
        pOld.disposeAndClear();
    }
    InvalidateTextWidth(NULL, NULL, false);     // in both cases
}
diff --git a/sc/source/ui/dbgui/csvgrid.cxx b/sc/source/ui/dbgui/csvgrid.cxx
index eb23e5d..76e0395 100644
--- a/sc/source/ui/dbgui/csvgrid.cxx
+++ b/sc/source/ui/dbgui/csvgrid.cxx
@@ -62,6 +62,8 @@ struct Func_Select

ScCsvGrid::ScCsvGrid( ScCsvControl& rParent ) :
    ScCsvControl( rParent ),
    mpBackgrDev( new VirtualDevice() ),
    mpGridDev( new VirtualDevice() ),
    mpColorConfig( 0 ),
    mpEditEngine( new ScEditEngineDefaulter( EditEngine::CreatePool(), true ) ),
    maHeaderFont( GetFont() ),
@@ -72,7 +74,7 @@ ScCsvGrid::ScCsvGrid( ScCsvControl& rParent ) :
    mnMTCurrCol( SAL_MAX_UINT32 ),
    mbMTSelecting( false )
{
    mpEditEngine->SetRefDevice( &maBackgrDev );
    mpEditEngine->SetRefDevice( mpBackgrDev.get() );
    mpEditEngine->SetRefMapMode( MapMode( MAP_PIXEL ) );
    maEdEngSize = mpEditEngine->GetPaperSize();

@@ -93,6 +95,8 @@ void ScCsvGrid::dispose()
    OSL_ENSURE(mpColorConfig, "the object hasn't been initialized properly");
    if (mpColorConfig)
        mpColorConfig->RemoveListener(this);
    mpBackgrDev.disposeAndClear();
    mpGridDev.disposeAndClear();
    ScCsvControl::dispose();
}

@@ -263,8 +267,8 @@ void ScCsvGrid::InitFonts()
void ScCsvGrid::InitSizeData()
{
    maWinSize = GetSizePixel();
    maBackgrDev.SetOutputSizePixel( maWinSize );
    maGridDev.SetOutputSizePixel( maWinSize );
    mpBackgrDev->SetOutputSizePixel( maWinSize );
    mpGridDev->SetOutputSizePixel( maWinSize );
    InvalidateGfx();
}

@@ -1021,7 +1025,7 @@ void ScCsvGrid::ImplRedraw()
            ImplDrawBackgrDev();
            ImplDrawGridDev();
        }
        DrawOutDev( Point(), maWinSize, Point(), maWinSize, maGridDev );
        DrawOutDev( Point(), maWinSize, Point(), maWinSize, *mpGridDev.get() );
        ImplDrawTrackingRect( GetFocusColumn() );
    }
}
@@ -1078,7 +1082,7 @@ void ScCsvGrid::ImplDrawCellText( const Point& rPos, const OUString& rText )
        {
            sal_Int32 nX = rPos.X() + GetCharWidth() * nBeginIx;
            mpEditEngine->SetText( aToken );
            mpEditEngine->Draw( &maBackgrDev, Point( nX, rPos.Y() ) );
            mpEditEngine->Draw( mpBackgrDev.get(), Point( nX, rPos.Y() ) );
        }
    }

@@ -1089,10 +1093,10 @@ void ScCsvGrid::ImplDrawCellText( const Point& rPos, const OUString& rText )
        sal_Int32 nX2 = nX1 + GetCharWidth() - 2;
        sal_Int32 nY = rPos.Y() + GetLineHeight() / 2;
        Color aColor( maTextColor );
        maBackgrDev.SetLineColor( aColor );
        maBackgrDev.DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
        maBackgrDev.DrawLine( Point( nX2 - 2, nY - 2 ), Point( nX2, nY ) );
        maBackgrDev.DrawLine( Point( nX2 - 2, nY + 2 ), Point( nX2, nY ) );
        mpBackgrDev->SetLineColor( aColor );
        mpBackgrDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
        mpBackgrDev->DrawLine( Point( nX2 - 2, nY - 2 ), Point( nX2, nY ) );
        mpBackgrDev->DrawLine( Point( nX2 - 2, nY + 2 ), Point( nX2, nY ) );
        ++nCharIx;
    }
    nCharIx = 0;
@@ -1102,11 +1106,11 @@ void ScCsvGrid::ImplDrawCellText( const Point& rPos, const OUString& rText )
        sal_Int32 nX2 = nX1 + GetCharWidth() - 2;
        sal_Int32 nY = rPos.Y() + GetLineHeight() / 2;
        Color aColor( maTextColor );
        maBackgrDev.SetLineColor( aColor );
        maBackgrDev.DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
        maBackgrDev.DrawLine( Point( nX1 + 2, nY - 2 ), Point( nX1, nY ) );
        maBackgrDev.DrawLine( Point( nX1 + 2, nY + 2 ), Point( nX1, nY ) );
        maBackgrDev.DrawLine( Point( nX2, nY - 2 ), Point( nX2, nY ) );
        mpBackgrDev->SetLineColor( aColor );
        mpBackgrDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
        mpBackgrDev->DrawLine( Point( nX1 + 2, nY - 2 ), Point( nX1, nY ) );
        mpBackgrDev->DrawLine( Point( nX1 + 2, nY + 2 ), Point( nX1, nY ) );
        mpBackgrDev->DrawLine( Point( nX2, nY - 2 ), Point( nX2, nY ) );
        ++nCharIx;
    }
}
@@ -1117,8 +1121,8 @@ void ScCsvGrid::ImplDrawFirstLineSep( bool bSet )
    {
        sal_Int32 nY = GetY( mnFirstImpLine );
        sal_Int32 nX = std::min( GetColumnX( GetLastVisColumn() + 1 ), GetLastX() );
        maBackgrDev.SetLineColor( bSet ? maGridPBColor : maGridColor );
        maBackgrDev.DrawLine( Point( GetFirstX() + 1, nY ), Point( nX, nY ) );
        mpBackgrDev->SetLineColor( bSet ? maGridPBColor : maGridColor );
        mpBackgrDev->DrawLine( Point( GetFirstX() + 1, nY ), Point( nX, nY ) );
    }
}

@@ -1127,20 +1131,20 @@ void ScCsvGrid::ImplDrawColumnBackgr( sal_uInt32 nColIndex )
    if( !IsVisibleColumn( nColIndex ) )
        return;

    ImplSetColumnClipRegion( maBackgrDev, nColIndex );
    ImplSetColumnClipRegion( *mpBackgrDev.get(), nColIndex );

    // grid
    maBackgrDev.SetLineColor();
    maBackgrDev.SetFillColor( maBackColor );
    mpBackgrDev->SetLineColor();
    mpBackgrDev->SetFillColor( maBackColor );
    sal_Int32 nX1 = GetColumnX( nColIndex ) + 1;
    sal_Int32 nX2 = GetColumnX( nColIndex + 1 );
    sal_Int32 nY2 = GetY( GetLastVisLine() + 1 );
    sal_Int32 nHdrHt = GetHdrHeight();
    Rectangle aRect( nX1, nHdrHt, nX2, nY2 );
    maBackgrDev.DrawRect( aRect );
    maBackgrDev.SetLineColor( maGridColor );
    maBackgrDev.DrawGrid( aRect, Size( 1, GetLineHeight() ), GRID_HORZLINES );
    maBackgrDev.DrawLine( Point( nX2, nHdrHt ), Point( nX2, nY2 ) );
    mpBackgrDev->DrawRect( aRect );
    mpBackgrDev->SetLineColor( maGridColor );
    mpBackgrDev->DrawGrid( aRect, Size( 1, GetLineHeight() ), GRID_HORZLINES );
    mpBackgrDev->DrawLine( Point( nX2, nHdrHt ), Point( nX2, nY2 ) );
    ImplDrawFirstLineSep( true );

    // cell texts
@@ -1164,53 +1168,53 @@ void ScCsvGrid::ImplDrawColumnBackgr( sal_uInt32 nColIndex )
    }

    // header
    ImplDrawColumnHeader( maBackgrDev, nColIndex, maHeaderBackColor );
    ImplDrawColumnHeader( *mpBackgrDev.get(), nColIndex, maHeaderBackColor );

    maBackgrDev.SetClipRegion();
    mpBackgrDev->SetClipRegion();
}

void ScCsvGrid::ImplDrawRowHeaders()
{
    maBackgrDev.SetLineColor();
    maBackgrDev.SetFillColor( maAppBackColor );
    mpBackgrDev->SetLineColor();
    mpBackgrDev->SetFillColor( maAppBackColor );
    Point aPoint( GetHdrX(), 0 );
    Rectangle aRect( aPoint, Size( GetHdrWidth() + 1, GetHeight() ) );
    maBackgrDev.DrawRect( aRect );
    mpBackgrDev->DrawRect( aRect );

    maBackgrDev.SetFillColor( maHeaderBackColor );
    mpBackgrDev->SetFillColor( maHeaderBackColor );
    aRect.Bottom() = GetY( GetLastVisLine() + 1 );
    maBackgrDev.DrawRect( aRect );
    mpBackgrDev->DrawRect( aRect );

    // line numbers
    maBackgrDev.SetFont( maHeaderFont );
    maBackgrDev.SetTextColor( maHeaderTextColor );
    maBackgrDev.SetTextFillColor();
    mpBackgrDev->SetFont( maHeaderFont );
    mpBackgrDev->SetTextColor( maHeaderTextColor );
    mpBackgrDev->SetTextFillColor();
    sal_Int32 nLastLine = GetLastVisLine();
    for( sal_Int32 nLine = GetFirstVisLine(); nLine <= nLastLine; ++nLine )
    {
        OUString aText( OUString::number( nLine + 1 ) );
        sal_Int32 nX = GetHdrX() + (GetHdrWidth() - maBackgrDev.GetTextWidth( aText )) / 2;
        maBackgrDev.DrawText( Point( nX, GetY( nLine ) ), aText );
        sal_Int32 nX = GetHdrX() + (GetHdrWidth() - mpBackgrDev->GetTextWidth( aText )) / 2;
        mpBackgrDev->DrawText( Point( nX, GetY( nLine ) ), aText );
    }

    // grid
    maBackgrDev.SetLineColor( maHeaderGridColor );
    mpBackgrDev->SetLineColor( maHeaderGridColor );
    if( IsRTL() )
    {
        maBackgrDev.DrawLine( Point( 0, 0 ), Point( 0, GetHeight() - 1 ) );
        maBackgrDev.DrawLine( aRect.TopLeft(), aRect.BottomLeft() );
        mpBackgrDev->DrawLine( Point( 0, 0 ), Point( 0, GetHeight() - 1 ) );
        mpBackgrDev->DrawLine( aRect.TopLeft(), aRect.BottomLeft() );
    }
    else
        maBackgrDev.DrawLine( aRect.TopRight(), aRect.BottomRight() );
        mpBackgrDev->DrawLine( aRect.TopRight(), aRect.BottomRight() );
    aRect.Top() = GetHdrHeight();
    maBackgrDev.DrawGrid( aRect, Size( 1, GetLineHeight() ), GRID_HORZLINES );
    mpBackgrDev->DrawGrid( aRect, Size( 1, GetLineHeight() ), GRID_HORZLINES );
}

void ScCsvGrid::ImplDrawBackgrDev()
{
    maBackgrDev.SetLineColor();
    maBackgrDev.SetFillColor( maAppBackColor );
    maBackgrDev.DrawRect( Rectangle(
    mpBackgrDev->SetLineColor();
    mpBackgrDev->SetFillColor( maAppBackColor );
    mpBackgrDev->DrawRect( Rectangle(
        Point( GetFirstX() + 1, 0 ), Size( GetWidth() - GetHdrWidth(), GetHeight() ) ) );

    sal_uInt32 nLastCol = GetLastVisColumn();
@@ -1225,8 +1229,8 @@ void ScCsvGrid::ImplDrawBackgrDev()
void ScCsvGrid::ImplDrawColumnSelection( sal_uInt32 nColIndex )
{
    ImplInvertCursor( GetRulerCursorPos() );
    ImplSetColumnClipRegion( maGridDev, nColIndex );
    maGridDev.DrawOutDev( Point(), maWinSize, Point(), maWinSize, maBackgrDev );
    ImplSetColumnClipRegion( *mpGridDev.get(), nColIndex );
    mpGridDev->DrawOutDev( Point(), maWinSize, Point(), maWinSize, *mpBackgrDev.get() );

    if( IsSelected( nColIndex ) )
    {
@@ -1235,29 +1239,29 @@ void ScCsvGrid::ImplDrawColumnSelection( sal_uInt32 nColIndex )

        // header
        Rectangle aRect( nX1, 0, nX2, GetHdrHeight() );
        maGridDev.SetLineColor();
        mpGridDev->SetLineColor();
        if( maHeaderBackColor.IsDark() )
            // redraw with light gray background in dark mode
            ImplDrawColumnHeader( maGridDev, nColIndex, COL_LIGHTGRAY );
            ImplDrawColumnHeader( *mpGridDev.get(), nColIndex, COL_LIGHTGRAY );
        else
        {
            // use transparent active color
            maGridDev.SetFillColor( maSelectColor );
            maGridDev.DrawTransparent( tools::PolyPolygon( Polygon( aRect ) ), CSV_HDR_TRANSPARENCY );
            mpGridDev->SetFillColor( maSelectColor );
            mpGridDev->DrawTransparent( tools::PolyPolygon( Polygon( aRect ) ), CSV_HDR_TRANSPARENCY );
        }

        // column selection
        aRect = Rectangle( nX1, GetHdrHeight() + 1, nX2, GetY( GetLastVisLine() + 1 ) - 1 );
        ImplInvertRect( maGridDev, aRect );
        ImplInvertRect( *mpGridDev.get(), aRect );
    }

    maGridDev.SetClipRegion();
    mpGridDev->SetClipRegion();
    ImplInvertCursor( GetRulerCursorPos() );
}

void ScCsvGrid::ImplDrawGridDev()
{
    maGridDev.DrawOutDev( Point(), maWinSize, Point(), maWinSize, maBackgrDev );
    mpGridDev->DrawOutDev( Point(), maWinSize, Point(), maWinSize, *mpBackgrDev );
    sal_uInt32 nLastCol = GetLastVisColumn();
    if (nLastCol == CSV_COLUMN_INVALID)
        return;
@@ -1303,12 +1307,12 @@ void ScCsvGrid::ImplDrawHorzScrolled( sal_Int32 nOldPos )
    ImplInvertCursor( GetRulerCursorPos() + (nPos - nOldPos) );
    Rectangle aRectangle( GetFirstX(), 0, GetLastX(), GetHeight() - 1 );
    vcl::Region aClipReg( aRectangle );
    maBackgrDev.SetClipRegion( aClipReg );
    maBackgrDev.CopyArea( aDest, aSrc, maWinSize );
    maBackgrDev.SetClipRegion();
    maGridDev.SetClipRegion( aClipReg );
    maGridDev.CopyArea( aDest, aSrc, maWinSize );
    maGridDev.SetClipRegion();
    mpBackgrDev->SetClipRegion( aClipReg );
    mpBackgrDev->CopyArea( aDest, aSrc, maWinSize );
    mpBackgrDev->SetClipRegion();
    mpGridDev->SetClipRegion( aClipReg );
    mpGridDev->CopyArea( aDest, aSrc, maWinSize );
    mpGridDev->SetClipRegion();
    ImplInvertCursor( GetRulerCursorPos() );

    for( sal_uInt32 nColIx = nFirstColIx; nColIx <= nLastColIx; ++nColIx )
@@ -1318,12 +1322,12 @@ void ScCsvGrid::ImplDrawHorzScrolled( sal_Int32 nOldPos )
    if( nLastX <= GetLastX() )
    {
        Rectangle aRect( nLastX, 0, GetLastX(), GetHeight() - 1 );
        maBackgrDev.SetLineColor();
        maBackgrDev.SetFillColor( maAppBackColor );
        maBackgrDev.DrawRect( aRect );
        maGridDev.SetLineColor();
        maGridDev.SetFillColor( maAppBackColor );
        maGridDev.DrawRect( aRect );
        mpBackgrDev->SetLineColor();
        mpBackgrDev->SetFillColor( maAppBackColor );
        mpBackgrDev->DrawRect( aRect );
        mpGridDev->SetLineColor();
        mpGridDev->SetFillColor( maAppBackColor );
        mpGridDev->DrawRect( aRect );
    }
}

@@ -1333,10 +1337,10 @@ void ScCsvGrid::ImplInvertCursor( sal_Int32 nPos )
    {
        sal_Int32 nX = GetX( nPos ) - 1;
        Rectangle aRect( Point( nX, 0 ), Size( 3, GetHdrHeight() ) );
        ImplInvertRect( maGridDev, aRect );
        ImplInvertRect( *mpGridDev.get(), aRect );
        aRect.Top() = GetHdrHeight() + 1;
        aRect.Bottom() = GetY( GetLastVisLine() + 1 );
        ImplInvertRect( maGridDev, aRect );
        ImplInvertRect( *mpGridDev.get(), aRect );
    }
}

diff --git a/sc/source/ui/docshell/sizedev.cxx b/sc/source/ui/docshell/sizedev.cxx
index e0ff33c..744266e 100644
--- a/sc/source/ui/docshell/sizedev.cxx
+++ b/sc/source/ui/docshell/sizedev.cxx
@@ -55,7 +55,7 @@ ScSizeDeviceProvider::ScSizeDeviceProvider( ScDocShell* pDocSh )
ScSizeDeviceProvider::~ScSizeDeviceProvider()
{
    if (bOwner)
        delete pDevice;
        pDevice.disposeAndClear();
    else
        pDevice->SetMapMode( aOldMapMode );
}
diff --git a/sc/source/ui/inc/csvgrid.hxx b/sc/source/ui/inc/csvgrid.hxx
index 3229eae..1c5b1dc 100644
--- a/sc/source/ui/inc/csvgrid.hxx
+++ b/sc/source/ui/inc/csvgrid.hxx
@@ -75,8 +75,8 @@ class SC_DLLPUBLIC ScCsvGrid : public ScCsvControl, public utl::ConfigurationLis
private:
    typedef ::std::unique_ptr< ScEditEngineDefaulter > ScEditEnginePtr;

    VirtualDevice               maBackgrDev;        /// Grid background, headers, cell texts.
    VirtualDevice               maGridDev;          /// Data grid with selection and cursor.
    VclPtr<VirtualDevice>       mpBackgrDev;        /// Grid background, headers, cell texts.
    VclPtr<VirtualDevice>       mpGridDev;          /// Data grid with selection and cursor.
    PopupMenu                   maPopup;            /// Popup menu for column types.

    ::svtools::ColorConfig*     mpColorConfig;      /// Application color configuration.
diff --git a/sc/source/ui/inc/output.hxx b/sc/source/ui/inc/output.hxx
index d9e1a46..d98ef82 100644
--- a/sc/source/ui/inc/output.hxx
+++ b/sc/source/ui/inc/output.hxx
@@ -141,9 +141,9 @@ private:
        void adjustForHyperlinkInPDF(Point aURLStart, OutputDevice* pDev);
    };

    OutputDevice* mpDev;        // Device
    OutputDevice* mpRefDevice;  // printer if used for preview
    OutputDevice* pFmtDevice;   // reference for text formatting
    VclPtr<OutputDevice> mpDev;        // Device
    VclPtr<OutputDevice> mpRefDevice;  // printer if used for preview
    VclPtr<OutputDevice> pFmtDevice;   // reference for text formatting
    ScTableInfo& mrTabInfo;
    RowInfo* pRowInfo;          // Info block
    SCSIZE nArrCount;           // occupied lines in info block
diff --git a/sc/source/ui/inc/sizedev.hxx b/sc/source/ui/inc/sizedev.hxx
index 770279f..95bfac3 100644
--- a/sc/source/ui/inc/sizedev.hxx
+++ b/sc/source/ui/inc/sizedev.hxx
@@ -27,17 +27,17 @@ class ScDocShell;

class ScSizeDeviceProvider
{
    OutputDevice*   pDevice;
    bool            bOwner;
    double          nPPTX;
    double          nPPTY;
    MapMode         aOldMapMode;
    VclPtr<OutputDevice> pDevice;
    bool                 bOwner;
    double               nPPTX;
    double               nPPTY;
    MapMode              aOldMapMode;

public:
                ScSizeDeviceProvider( ScDocShell* pDocSh );
                ~ScSizeDeviceProvider();

    OutputDevice*   GetDevice() const   { return pDevice; }
    OutputDevice*   GetDevice() const   { return pDevice.get(); }
    double          GetPPTX() const     { return nPPTX; }
    double          GetPPTY() const     { return nPPTY; }
    bool            IsPrinter() const   { return !bOwner; }
diff --git a/sd/inc/sdmod.hxx b/sd/inc/sdmod.hxx
index 624b0a8..a981363 100644
--- a/sd/inc/sdmod.hxx
+++ b/sd/inc/sdmod.hxx
@@ -144,7 +144,7 @@ protected:
        in the sense that it does not represent a printer.  The pointer may
        be NULL when the virtual device could not be created.
    */
    OutputDevice*           mpVirtualRefDevice;
    VclPtr< OutputDevice >  mpVirtualRefDevice;

    virtual void            Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) SAL_OVERRIDE;

diff --git a/sd/source/ui/app/sdmod.cxx b/sd/source/ui/app/sdmod.cxx
index b982264..5c3b2dd 100644
--- a/sd/source/ui/app/sdmod.cxx
+++ b/sd/source/ui/app/sdmod.cxx
@@ -119,7 +119,7 @@ SdModule::~SdModule()
        (*ppShellPointer) = NULL;

    delete mpErrorHdl;
    delete static_cast< VirtualDevice* >( mpVirtualRefDevice );
    mpVirtualRefDevice.disposeAndClear();
}

/// get notifications
diff --git a/sd/source/ui/docshell/docshel4.cxx b/sd/source/ui/docshell/docshel4.cxx
index 3eb55845..c273d2d 100644
--- a/sd/source/ui/docshell/docshel4.cxx
+++ b/sd/source/ui/docshell/docshel4.cxx
@@ -146,7 +146,7 @@ void DrawDocShell::SetPrinter(SfxPrinter *pNewPrinter)

    if ( mpPrinter && mbOwnPrinter && (mpPrinter != pNewPrinter) )
    {
        delete mpPrinter;
        mpPrinter.disposeAndClear();
    }

    mpPrinter = pNewPrinter;
@@ -204,11 +204,11 @@ void DrawDocShell::UpdateRefDevice()
    if( mpDoc )
    {
        // Determine the device for which the output will be formatted.
        OutputDevice* pRefDevice = NULL;
        VclPtr< OutputDevice > pRefDevice;
        switch (mpDoc->GetPrinterIndependentLayout())
        {
            case ::com::sun::star::document::PrinterIndependentLayout::DISABLED:
                pRefDevice = mpPrinter;
                pRefDevice = mpPrinter.get();
                break;

            case ::com::sun::star::document::PrinterIndependentLayout::ENABLED:
@@ -224,7 +224,7 @@ void DrawDocShell::UpdateRefDevice()
                pRefDevice = mpPrinter;
                break;
        }
        mpDoc->SetRefDevice( pRefDevice );
        mpDoc->SetRefDevice( pRefDevice.get() );

        ::sd::Outliner* pOutl = mpDoc->GetOutliner( false );

diff --git a/sd/source/ui/docshell/docshell.cxx b/sd/source/ui/docshell/docshell.cxx
index c5108a3..04ebc63 100644
--- a/sd/source/ui/docshell/docshell.cxx
+++ b/sd/source/ui/docshell/docshell.cxx
@@ -185,7 +185,7 @@ DrawDocShell::~DrawDocShell()
    delete mpUndoManager;

    if (mbOwnPrinter)
        delete mpPrinter;
        mpPrinter.disposeAndClear();

    if( mbOwnDocument )
        delete mpDoc;
diff --git a/sd/source/ui/inc/DrawDocShell.hxx b/sd/source/ui/inc/DrawDocShell.hxx
index dc2d510..71e9022 100644
--- a/sd/source/ui/inc/DrawDocShell.hxx
+++ b/sd/source/ui/inc/DrawDocShell.hxx
@@ -208,7 +208,7 @@ protected:

    SdDrawDocument*         mpDoc;
    SfxUndoManager*         mpUndoManager;
    SfxPrinter*             mpPrinter;
    VclPtr<SfxPrinter>      mpPrinter;
    ::sd::ViewShell*        mpViewShell;
    FontList*               mpFontList;
    rtl::Reference<FuPoor> mxDocShellFunction;
diff --git a/starmath/inc/document.hxx b/starmath/inc/document.hxx
index e437dd1..2704a34 100644
--- a/starmath/inc/document.hxx
+++ b/starmath/inc/document.hxx
@@ -69,13 +69,13 @@ class EditEngine;

class SmPrinterAccess
{
    Printer* pPrinter;
    OutputDevice* pRefDev;
    VclPtr<Printer> pPrinter;
    VclPtr<OutputDevice> pRefDev;
public:
    SmPrinterAccess( SmDocShell &rDocShell );
    ~SmPrinterAccess();
    Printer* GetPrinter()  { return pPrinter; }
    OutputDevice* GetRefDev()  { return pRefDev; }
    Printer* GetPrinter()  { return pPrinter.get(); }
    OutputDevice* GetRefDev()  { return pRefDev.get(); }
};


@@ -98,8 +98,8 @@ class SM_DLLPUBLIC SmDocShell : public SfxObjectShell, public SfxListener
    SmNode             *pTree;
    SfxItemPool        *pEditEngineItemPool;
    EditEngine         *pEditEngine;
    SfxPrinter         *pPrinter;       //q.v. comment to SmPrinter Access!
    Printer            *pTmpPrinter;    //ditto
    VclPtr<SfxPrinter>  pPrinter;       //q.v. comment to SmPrinter Access!
    VclPtr<Printer>     pTmpPrinter;    //ditto
    sal_uInt16          nModifyCount;
    bool                bIsFormulaArranged;
    SmCursor           *pCursor;
diff --git a/starmath/source/document.cxx b/starmath/source/document.cxx
index e671535..a78f678 100644
--- a/starmath/source/document.cxx
+++ b/starmath/source/document.cxx
@@ -531,7 +531,7 @@ SmPrinterAccess::SmPrinterAccess( SmDocShell &rDocShell )
            }
        }
    }
    if ( 0 != (pRefDev = rDocShell.GetRefDev()) && pPrinter != pRefDev )
    if ( !!(pRefDev = rDocShell.GetRefDev()) && pPrinter.get() != pRefDev.get() )
    {
        pRefDev->Push( PushFlags::MAPMODE );
        if ( SFX_CREATE_MODE_EMBEDDED == rDocShell.GetCreateMode() )
@@ -616,7 +616,7 @@ OutputDevice* SmDocShell::GetRefDev()

void SmDocShell::SetPrinter( SfxPrinter *pNew )
{
    delete pPrinter;
    pPrinter.disposeAndClear();
    pPrinter = pNew;    //Transfer ownership
    pPrinter->SetMapMode( MapMode(MAP_100TH_MM) );
    SetFormulaArranged(false);
@@ -693,7 +693,7 @@ SmDocShell::~SmDocShell()
    delete pEditEngine;
    SfxItemPool::Free(pEditEngineItemPool);
    delete pTree;
    delete pPrinter;
    pPrinter.disposeAndClear();
}

bool SmDocShell::ConvertFrom(SfxMedium &rMedium)
diff --git a/sw/source/core/doc/DocumentDeviceManager.cxx b/sw/source/core/doc/DocumentDeviceManager.cxx
index f2f7517..5d38a93 100644
--- a/sw/source/core/doc/DocumentDeviceManager.cxx
+++ b/sw/source/core/doc/DocumentDeviceManager.cxx
@@ -55,8 +55,8 @@ DocumentDeviceManager::DocumentDeviceManager( SwDoc& i_rSwdoc ) : m_rDoc( i_rSwd
SfxPrinter* DocumentDeviceManager::getPrinter(/*[in]*/ bool bCreate ) const
{
    SfxPrinter* pRet = 0;
    if ( !bCreate ||  mpPrt )
        pRet =  mpPrt;
    if ( !bCreate || mpPrt )
        pRet = mpPrt;
    else
        pRet = &CreatePrinter_();

@@ -65,10 +65,10 @@ SfxPrinter* DocumentDeviceManager::getPrinter(/*[in]*/ bool bCreate ) const

void DocumentDeviceManager::setPrinter(/*[in]*/ SfxPrinter *pP,/*[in]*/ bool bDeleteOld,/*[in]*/ bool bCallPrtDataChanged )
{
    if ( pP !=  mpPrt )
    if ( pP != mpPrt )
    {
        if ( bDeleteOld )
            delete  mpPrt;
            mpPrt.disposeAndClear();
         mpPrt = pP;

        // our printer should always use TWIP. Don't rely on this being set in SwViewShell::InitPrt, there
@@ -78,7 +78,7 @@ void DocumentDeviceManager::setPrinter(/*[in]*/ SfxPrinter *pP,/*[in]*/ bool bDe
        {
            MapMode aMapMode( mpPrt->GetMapMode() );
            aMapMode.SetMapUnit( MAP_TWIP );
             mpPrt->SetMapMode( aMapMode );
            mpPrt->SetMapMode( aMapMode );
        }

        if ( m_rDoc.getIDocumentDrawModelAccess().GetDrawModel() && !m_rDoc.GetDocumentSettingManager().get( DocumentSettingId::USE_VIRTUAL_DEVICE ) )
@@ -105,10 +105,10 @@ VirtualDevice* DocumentDeviceManager::getVirtualDevice(/*[in]*/ bool bCreate ) c

void DocumentDeviceManager::setVirtualDevice(/*[in]*/ VirtualDevice* pVd,/*[in]*/ bool bDeleteOld, /*[in]*/ bool )
{
    if ( mpVirDev != pVd )
    if ( mpVirDev.get() != pVd )
    {
        if ( bDeleteOld )
            delete mpVirDev;
            mpVirDev.disposeAndClear();
        mpVirDev = pVd;

        if ( m_rDoc.getIDocumentDrawModelAccess().GetDrawModel() && m_rDoc.GetDocumentSettingManager().get( DocumentSettingId::USE_VIRTUAL_DEVICE ) )
@@ -193,7 +193,7 @@ void DocumentDeviceManager::setJobsetup(/*[in]*/ const JobSetup &rJobSetup )
            }
        }
        else
            delete mpPrt, mpPrt = 0;
            mpPrt.disposeAndClear();
    }

    if( !mpPrt )
@@ -247,8 +247,8 @@ void DocumentDeviceManager::setPrintData(/*[in]*/ const SwPrintData& rPrtData )
DocumentDeviceManager::~DocumentDeviceManager()
{
    delete mpPrtData;
    delete mpVirDev;
    DELETEZ( mpPrt );
    mpVirDev.disposeAndClear();
    mpPrt.disposeAndClear();
}

VirtualDevice& DocumentDeviceManager::CreateVirtualDevice_() const
@@ -292,7 +292,7 @@ SfxPrinter& DocumentDeviceManager::CreatePrinter_() const

    SfxPrinter* pNewPrt = new SfxPrinter( pSet );
    const_cast<DocumentDeviceManager*>(this)->setPrinter( pNewPrt, true, true );
    return *mpPrt;
    return *mpPrt.get();
}

void DocumentDeviceManager::PrtDataChanged()
diff --git a/sw/source/core/inc/DocumentDeviceManager.hxx b/sw/source/core/inc/DocumentDeviceManager.hxx
index 1440b63..f388b4d 100644
--- a/sw/source/core/inc/DocumentDeviceManager.hxx
+++ b/sw/source/core/inc/DocumentDeviceManager.hxx
@@ -22,7 +22,7 @@
#include <IDocumentDeviceAccess.hxx>
#include <boost/utility.hpp>
#include <sal/types.h>

#include <vcl/vclptr.hxx>

class SwDoc;
class SfxPrinter;
@@ -72,8 +72,8 @@ private:
                                invalidations and notifications. */

    SwDoc& m_rDoc;
    SfxPrinter* mpPrt;
    VirtualDevice* mpVirDev;
    VclPtr<SfxPrinter> mpPrt;
    VclPtr<VirtualDevice> mpVirDev;
    SwPrintData* mpPrtData;
};