include/vcl/outdev.hxx                   |   16 +++++++++++
 vcl/source/control/button.cxx            |   32 +++++------------------
 vcl/source/control/edit.cxx              |    5 ---
 vcl/source/control/fixed.cxx             |   11 ++------
 vcl/source/control/imgctrl.cxx           |    7 +----
 vcl/source/control/imivctl1.cxx          |   10 ++-----
 vcl/source/control/listbox.cxx           |    4 --
 vcl/source/control/scrbar.cxx            |    9 ++----
 vcl/source/control/spinbtn.cxx           |    3 --
 vcl/source/edit/texteng.cxx              |    3 --
 vcl/source/edit/vclmedit.cxx             |    4 --
 vcl/source/filter/imet/ios2met.cxx       |    3 --
 vcl/source/filter/ipict/shape.cxx        |    3 --
 vcl/source/filter/wmf/emfwr.cxx          |    4 --
 vcl/source/gdi/gdimtf.cxx                |    7 +----
 vcl/source/gdi/graph.cxx                 |    4 --
 vcl/source/gdi/pdfwriter_impl.cxx        |    3 --
 vcl/source/gdi/print.cxx                 |    3 --
 vcl/source/gdi/print2.cxx                |    3 --
 vcl/source/gdi/print3.cxx                |   21 +++++----------
 vcl/source/graphic/GraphicObject2.cxx    |    4 --
 vcl/source/outdev/bitmap.cxx             |    3 --
 vcl/source/outdev/font.cxx               |    3 --
 vcl/source/outdev/gradient.cxx           |    7 +----
 vcl/source/outdev/rect.cxx               |    4 --
 vcl/source/outdev/text.cxx               |    3 --
 vcl/source/outdev/transparent.cxx        |    9 ++----
 vcl/source/toolkit/group.cxx             |    3 --
 vcl/source/treelist/headbar.cxx          |    4 --
 vcl/source/treelist/iconview.cxx         |    6 +---
 vcl/source/treelist/svimpbox.cxx         |    4 --
 vcl/source/treelist/svlbitm.cxx          |    3 --
 vcl/source/treelist/treelistbox.cxx      |    4 --
 vcl/source/window/decoview.cxx           |   42 +++++++++----------------------
 vcl/source/window/dialog.cxx             |    4 --
 vcl/source/window/menu.cxx               |    6 +---
 vcl/source/window/menubarwindow.cxx      |    3 --
 vcl/source/window/menufloatingwindow.cxx |    6 +---
 vcl/source/window/paint.cxx              |    4 --
 vcl/source/window/printdlg.cxx           |    8 +----
 vcl/source/window/status.cxx             |    4 --
 vcl/source/window/tabpage.cxx            |    4 --
 vcl/source/window/toolbox.cxx            |   17 ++----------
 vcl/source/window/window.cxx             |    4 --
 vcl/workben/svptest.cxx                  |    3 --
 45 files changed, 103 insertions(+), 214 deletions(-)

New commits:
commit 8e26cae8dd257a763de35523a9ff788a3d2e17a5
Author:     Mike Kaganski <[email protected]>
AuthorDate: Sat Aug 16 17:55:41 2025 +0500
Commit:     Mike Kaganski <[email protected]>
CommitDate: Sat Aug 16 20:09:56 2025 +0200

    Introduce OutputDevice::ScopedPush, and use it in vcl
    
    It allows to use RAII to pop pushed output device state. One benefit
    could be simplified early exit.
    
    Change-Id: I319afd38176daab6fc96a584326ec136b6d7474a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189794
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <[email protected]>

diff --git a/include/vcl/outdev.hxx b/include/vcl/outdev.hxx
index 01c9b6923088..fb61ff86b123 100644
--- a/include/vcl/outdev.hxx
+++ b/include/vcl/outdev.hxx
@@ -467,6 +467,9 @@ public:
     void                        Pop();
     SAL_DLLPRIVATE void         ClearStack();
 
+    // Pushes the state, and returns a RAII object that pops it in destructor
+    [[nodiscard]] inline auto ScopedPush(vcl::PushFlags nFlags = 
vcl::PushFlags::ALL);
+
     void                        EnableOutput( bool bEnable = true );
     bool                        IsOutputEnabled() const { return mbOutput; }
     bool                        IsDeviceOutputNecessary() const { return 
(mbOutput && mbDevOutput); }
@@ -1915,4 +1918,17 @@ protected:
 
 };
 
+[[nodiscard]] inline auto OutputDevice::ScopedPush(vcl::PushFlags nFlags)
+{
+    struct OutputDeviceRestoreStateGuard
+    {
+        OutputDevice& m_rDev;
+        ~OutputDeviceRestoreStateGuard() { m_rDev.Pop(); }
+    };
+
+    Push(nFlags);
+    // [-loplugin:redundantfcast]
+    return OutputDeviceRestoreStateGuard{ *this };
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/vcl/source/control/button.cxx b/vcl/source/control/button.cxx
index 1ade6af449b0..3712f00047ea 100644
--- a/vcl/source/control/button.cxx
+++ b/vcl/source/control/button.cxx
@@ -850,7 +850,7 @@ void PushButton::ImplDrawPushButtonContent(OutputDevice 
*pDev, SystemTextColorFl
     if (aInRect.Right() < aInRect.Left() || aInRect.Bottom() < aInRect.Top())
         return;
 
-    pDev->Push(vcl::PushFlags::CLIPREGION);
+    auto popIt = pDev->ScopedPush(vcl::PushFlags::CLIPREGION);
     pDev->IntersectClipRegion(aInRect);
 
     if (nSystemTextColorFlags & SystemTextColorFlags::Mono)
@@ -980,8 +980,6 @@ void PushButton::ImplDrawPushButtonContent(OutputDevice 
*pDev, SystemTextColorFl
             aDecoView.DrawSymbol( aSymbolRect, meSymbol, aColor, nStyle );
         }
     }
-
-    pDev->Pop();  // restore clipregion
 }
 
 void PushButton::ImplDrawPushButton(vcl::RenderContext& rRenderContext)
@@ -1439,7 +1437,7 @@ void PushButton::Draw( OutputDevice* pDev, const Point& 
rPos,
     tools::Rectangle   aRect( aPos, aSize );
     vcl::Font   aFont = GetDrawPixelFont( pDev );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetFont( aFont );
 
@@ -1483,8 +1481,6 @@ void PushButton::Draw( OutputDevice* pDev, const Point& 
rPos,
         aSettings.SetStyleSettings(*oOrigDevStyleSettings);
         pDev->OutputDevice::SetSettings( aSettings );
     }
-
-    pDev->Pop();
 }
 
 void PushButton::Resize()
@@ -2184,7 +2180,7 @@ void RadioButton::ImplDraw( OutputDevice* pDev, 
SystemTextColorFlags nSystemText
     WinBits                 nWinStyle = GetStyle();
     OUString                aText( GetText() );
 
-    pDev->Push( vcl::PushFlags::CLIPREGION );
+    auto popIt = pDev->ScopedPush(vcl::PushFlags::CLIPREGION);
     pDev->IntersectClipRegion( tools::Rectangle( rPos, rSize ) );
 
     // no image radio button
@@ -2253,8 +2249,6 @@ void RadioButton::ImplDraw( OutputDevice* pDev, 
SystemTextColorFlags nSystemText
         rMouseRect = aImageRect;
         rStateRect = aImageRect;
     }
-
-    pDev->Pop();
 }
 
 void RadioButton::ImplDrawRadioButton(vcl::RenderContext& rRenderContext)
@@ -2569,7 +2563,7 @@ void RadioButton::Draw( OutputDevice* pDev, const Point& 
rPos,
         if ( !aBrd2Size.Height() )
             aBrd2Size.setHeight( 1 );
 
-        pDev->Push();
+        auto popIt = pDev->ScopedPush();
         pDev->SetMapMode();
         pDev->SetFont( aFont );
         if ( nFlags & SystemTextColorFlags::Mono )
@@ -2603,8 +2597,6 @@ void RadioButton::Draw( OutputDevice* pDev, const Point& 
rPos,
             pDev->SetFillColor( COL_BLACK );
             pDev->DrawPolygon( tools::Polygon( aCenterPos, nRadX, nRadY ) );
         }
-
-        pDev->Pop();
     }
     else
     {
@@ -2944,7 +2936,7 @@ Image RadioButton::GetRadioImage( const AllSettings& 
rSettings, DrawButtonFlags
 
 void RadioButton::ImplAdjustNWFSizes()
 {
-    GetOutDev()->Push( vcl::PushFlags::MAPMODE );
+    auto popIt = GetOutDev()->ScopedPush(vcl::PushFlags::MAPMODE);
     SetMapMode(MapMode(MapUnit::MapPixel));
 
     ImplControlValue aControlValue;
@@ -2965,8 +2957,6 @@ void RadioButton::ImplAdjustNWFSizes()
             SetSizePixel( aCurSize );
         }
     }
-
-    GetOutDev()->Pop();
 }
 
 Size RadioButton::CalcMinimumSize(tools::Long nMaxWidth) const
@@ -3188,7 +3178,7 @@ void CheckBox::ImplDraw( OutputDevice* pDev, 
SystemTextColorFlags nSystemTextCol
     WinBits                 nWinStyle = GetStyle();
     OUString                aText( GetText() );
 
-    pDev->Push( vcl::PushFlags::CLIPREGION | vcl::PushFlags::LINECOLOR );
+    auto popIt = pDev->ScopedPush(vcl::PushFlags::CLIPREGION | 
vcl::PushFlags::LINECOLOR);
     pDev->IntersectClipRegion( tools::Rectangle( rPos, rSize ) );
 
     if (!aText.isEmpty() || HasImage())
@@ -3217,8 +3207,6 @@ void CheckBox::ImplDraw( OutputDevice* pDev, 
SystemTextColorFlags nSystemTextCol
 
         ImplSetFocusRect( rStateRect );
     }
-
-    pDev->Pop();
 }
 
 void CheckBox::ImplDrawCheckBox(vcl::RenderContext& rRenderContext)
@@ -3401,7 +3389,7 @@ void CheckBox::Draw( OutputDevice* pDev, const Point& 
rPos,
     if ( !nCheckWidth )
         nCheckWidth = 1;
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetFont( aFont );
     if ( nFlags & SystemTextColorFlags::Mono )
@@ -3463,8 +3451,6 @@ void CheckBox::Draw( OutputDevice* pDev, const Point& 
rPos,
             pDev->DrawLine( aTempPos21, aTempPos22 );
         }
     }
-
-    pDev->Pop();
 }
 
 void CheckBox::Resize()
@@ -3751,7 +3737,7 @@ Image CheckBox::GetCheckImage( const AllSettings& 
rSettings, DrawButtonFlags nFl
 
 void CheckBox::ImplAdjustNWFSizes()
 {
-    GetOutDev()->Push( vcl::PushFlags::MAPMODE );
+    auto popIt = GetOutDev()->ScopedPush(vcl::PushFlags::MAPMODE);
     SetMapMode(MapMode(MapUnit::MapPixel));
 
     ImplControlValue aControlValue;
@@ -3772,8 +3758,6 @@ void CheckBox::ImplAdjustNWFSizes()
             SetSizePixel( aCurSize );
         }
     }
-
-    GetOutDev()->Pop();
 }
 
 Size CheckBox::CalcMinimumSize( tools::Long nMaxWidth ) const
diff --git a/vcl/source/control/edit.cxx b/vcl/source/control/edit.cxx
index 04269032e6a9..666db74c89b6 100644
--- a/vcl/source/control/edit.cxx
+++ b/vcl/source/control/edit.cxx
@@ -494,7 +494,7 @@ void Edit::ImplRepaint(vcl::RenderContext& rRenderContext, 
const tools::Rectangl
     else
     {
         // save graphics state
-        rRenderContext.Push();
+        auto popIt = rRenderContext.ScopedPush();
         // first calculate highlighted and non highlighted clip regions
         vcl::Region aHighlightClipRegion;
         vcl::Region aNormalClipRegion;
@@ -621,9 +621,6 @@ void Edit::ImplRepaint(vcl::RenderContext& rRenderContext, 
const tools::Rectangl
                 }
             }
         }
-
-        // restore graphics state
-        rRenderContext.Pop();
     }
 
     if (bVisCursor && (!mpIMEInfos || mpIMEInfos->bCursor))
diff --git a/vcl/source/control/fixed.cxx b/vcl/source/control/fixed.cxx
index 9dc7a7939897..571cace0b8df 100644
--- a/vcl/source/control/fixed.cxx
+++ b/vcl/source/control/fixed.cxx
@@ -213,7 +213,7 @@ void FixedText::Draw( OutputDevice* pDev, const Point& rPos,
     Size        aSize = GetSizePixel();
     vcl::Font   aFont = GetDrawPixelFont( pDev );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetFont( aFont );
     if ( nFlags & SystemTextColorFlags::Mono )
@@ -239,7 +239,6 @@ void FixedText::Draw( OutputDevice* pDev, const Point& rPos,
     }
 
     ImplDraw( pDev, nFlags, aPos, aSize );
-    pDev->Pop();
 }
 
 void FixedText::Resize()
@@ -745,7 +744,7 @@ void FixedBitmap::Draw( OutputDevice* pDev, const Point& 
rPos,
     Size        aSize = GetSizePixel();
     tools::Rectangle   aRect( aPos, aSize );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
 
     // Border
@@ -756,8 +755,6 @@ void FixedBitmap::Draw( OutputDevice* pDev, const Point& 
rPos,
     }
     pDev->IntersectClipRegion( aRect );
     ImplDraw( pDev, aRect.TopLeft(), aRect.GetSize() );
-
-    pDev->Pop();
 }
 
 void FixedBitmap::Resize()
@@ -892,7 +889,7 @@ void FixedImage::Draw( OutputDevice* pDev, const Point& 
rPos,
     Size        aSize = GetSizePixel();
     tools::Rectangle   aRect( aPos, aSize );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
 
     // Border
@@ -902,8 +899,6 @@ void FixedImage::Draw( OutputDevice* pDev, const Point& 
rPos,
     }
     pDev->IntersectClipRegion( aRect );
     ImplDraw( pDev, aRect.TopLeft(), aRect.GetSize() );
-
-    pDev->Pop();
 }
 
 void FixedImage::Resize()
diff --git a/vcl/source/control/imgctrl.cxx b/vcl/source/control/imgctrl.cxx
index 132e10ca820b..901475d3cd85 100644
--- a/vcl/source/control/imgctrl.cxx
+++ b/vcl/source/control/imgctrl.cxx
@@ -133,7 +133,7 @@ void ImageControl::Paint(vcl::RenderContext& 
rRenderContext, const tools::Rectan
 
     bool bFlat = (GetBorderStyle() == WindowBorderStyle::MONO);
     tools::Rectangle aRect(Point(0,0), pBorderWindow->GetOutputSizePixel());
-    pBorderWindow->GetOutDev()->Push(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
+    auto popIt = 
pBorderWindow->GetOutDev()->ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
     pBorderWindow->GetOutDev()->SetFillColor();
     pBorderWindow->GetOutDev()->SetLineColor(bFlat ? COL_WHITE : COL_BLACK);
     pBorderWindow->GetOutDev()->DrawRect(aRect);
@@ -143,7 +143,6 @@ void ImageControl::Paint(vcl::RenderContext& 
rRenderContext, const tools::Rectan
     aRect.AdjustBottom( -1 );
     pBorderWindow->GetOutDev()->SetLineColor(bFlat ? COL_BLACK : COL_WHITE);
     pBorderWindow->GetOutDev()->DrawRect(aRect);
-    pBorderWindow->GetOutDev()->Pop();
 }
 
 void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, 
SystemTextColorFlags )
@@ -152,7 +151,7 @@ void ImageControl::Draw( OutputDevice* pDev, const Point& 
rPos, SystemTextColorF
     const Size      aSize = GetSizePixel();
     tools::Rectangle aRect( aPos, aSize );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
 
     // Border
@@ -162,8 +161,6 @@ void ImageControl::Draw( OutputDevice* pDev, const Point& 
rPos, SystemTextColorF
     }
     pDev->IntersectClipRegion( aRect );
     ImplDraw( *pDev, aRect.TopLeft(), aRect.GetSize() );
-
-    pDev->Pop();
 }
 
 void ImageControl::GetFocus()
diff --git a/vcl/source/control/imivctl1.cxx b/vcl/source/control/imivctl1.cxx
index e845a69504df..8ad79de5ba9d 100644
--- a/vcl/source/control/imivctl1.cxx
+++ b/vcl/source/control/imivctl1.cxx
@@ -369,7 +369,7 @@ void SvxIconChoiceCtrl_Impl::Paint(vcl::RenderContext& 
rRenderContext, const too
     if (!nCount)
         return;
 
-    rRenderContext.Push(vcl::PushFlags::CLIPREGION);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::CLIPREGION);
     rRenderContext.SetClipRegion(vcl::Region(rRect));
 
     std::vector< SvxIconChoiceCtrlEntry* > aNewZOrderList;
@@ -394,8 +394,6 @@ void SvxIconChoiceCtrl_Impl::Paint(vcl::RenderContext& 
rRenderContext, const too
     }
     maZOrderList = std::move( aNewZOrderList );
     maZOrderList.insert(maZOrderList.end(), aPaintedEntries.begin(), 
aPaintedEntries.end());
-
-    rRenderContext.Pop();
 }
 
 void SvxIconChoiceCtrl_Impl::RepaintSelectedEntries()
@@ -912,10 +910,9 @@ void SvxIconChoiceCtrl_Impl::PaintEmphasis(const 
tools::Rectangle& rTextRect,
         return;
 
     // draw text rectangle
-    rRenderContext.Push(vcl::PushFlags::FILLCOLOR);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::FILLCOLOR);
     rRenderContext.SetFillColor(rFillColor);
     rRenderContext.DrawRect(rTextRect);
-    rRenderContext.Pop();
 }
 
 
@@ -961,7 +958,7 @@ void SvxIconChoiceCtrl_Impl::PaintItem(const 
tools::Rectangle& rRect,
 
 void SvxIconChoiceCtrl_Impl::PaintEntry(SvxIconChoiceCtrlEntry* pEntry, const 
Point& rPos, vcl::RenderContext& rRenderContext)
 {
-    rRenderContext.Push(vcl::PushFlags::FONT | vcl::PushFlags::TEXTCOLOR);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::FONT | 
vcl::PushFlags::TEXTCOLOR);
 
     tools::Rectangle aTextRect(CalcTextRect(pEntry, &rPos));
     tools::Rectangle aBmpRect(CalcBmpRect(pEntry, &rPos));
@@ -1087,7 +1084,6 @@ void 
SvxIconChoiceCtrl_Impl::PaintEntry(SvxIconChoiceCtrlEntry* pEntry, const Po
 
     PaintItem(aTextRect, IcnViewFieldType::Text, pEntry, nTextPaintFlags, 
rRenderContext);
 
-    rRenderContext.Pop();
     if (bResetClipRegion)
         rRenderContext.SetClipRegion();
 }
diff --git a/vcl/source/control/listbox.cxx b/vcl/source/control/listbox.cxx
index 6dc023c7968a..ce1512b05d67 100644
--- a/vcl/source/control/listbox.cxx
+++ b/vcl/source/control/listbox.cxx
@@ -339,7 +339,7 @@ void ListBox::Draw( OutputDevice* pDev, const Point& rPos, 
SystemTextColorFlags
     Size aSize = GetSizePixel();
     vcl::Font aFont = mpImplLB->GetMainWindow()->GetDrawPixelFont( pDev );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetFont( aFont );
     pDev->SetTextFillColor();
@@ -449,8 +449,6 @@ void ListBox::Draw( OutputDevice* pDev, const Point& rPos, 
SystemTextColorFlags
                 pDev->SetTextColor( COL_BLACK );
         }
     }
-
-    pDev->Pop();
 }
 
 void ListBox::GetFocus()
diff --git a/vcl/source/control/scrbar.cxx b/vcl/source/control/scrbar.cxx
index d0252cf1cf97..e91977e3893a 100644
--- a/vcl/source/control/scrbar.cxx
+++ b/vcl/source/control/scrbar.cxx
@@ -380,7 +380,7 @@ void ScrollBar::Draw( OutputDevice* pDev, const Point& 
rPos, SystemTextColorFlag
 {
     Point aPos  = pDev->LogicToPixel( rPos );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     if ( !(nFlags & SystemTextColorFlags::Mono) )
     {
@@ -413,7 +413,6 @@ void ScrollBar::Draw( OutputDevice* pDev, const Point& 
rPos, SystemTextColorFlag
     maPage2Rect+=aPos;
 
     ImplDraw(*pDev);
-    pDev->Pop();
 
     mbCalcSize = true;
 }
@@ -953,13 +952,12 @@ void ScrollBar::MouseButtonDown( const MouseEvent& rMEvt )
     if (!IsMapModeEnabled() && GetMapMode().GetMapUnit() == MapUnit::MapTwip)
     {
         // rMEvt coordinates are in twips.
-        GetOutDev()->Push(vcl::PushFlags::MAPMODE);
+        auto popIt = GetOutDev()->ScopedPush(vcl::PushFlags::MAPMODE);
         EnableMapMode();
         MapMode aMapMode = GetMapMode();
         aMapMode.SetOrigin(Point(0, 0));
         SetMapMode(aMapMode);
         aPosPixel = LogicToPixel(rMEvt.GetPosPixel());
-        GetOutDev()->Pop();
     }
     const Point&        rMousePos = (GetMapMode().GetMapUnit() != 
MapUnit::MapTwip ? rMEvt.GetPosPixel() : aPosPixel);
     StartTrackingFlags  nTrackFlags = StartTrackingFlags::NONE;
@@ -1110,13 +1108,12 @@ void ScrollBar::Tracking( const TrackingEvent& rTEvt )
         if (!IsMapModeEnabled() && GetMapMode().GetMapUnit() == 
MapUnit::MapTwip)
         {
             // rTEvt coordinates are in twips.
-            GetOutDev()->Push(vcl::PushFlags::MAPMODE);
+            auto popIt = GetOutDev()->ScopedPush(vcl::PushFlags::MAPMODE);
             EnableMapMode();
             MapMode aMapMode = GetMapMode();
             aMapMode.SetOrigin(Point(0, 0));
             SetMapMode(aMapMode);
             aPosPixel = LogicToPixel(rTEvt.GetMouseEvent().GetPosPixel());
-            GetOutDev()->Pop();
         }
         const Point rMousePos = (GetMapMode().GetMapUnit() != MapUnit::MapTwip 
? rTEvt.GetMouseEvent().GetPosPixel() : aPosPixel);
 
diff --git a/vcl/source/control/spinbtn.cxx b/vcl/source/control/spinbtn.cxx
index 03a5a7374d06..f2399cbc288d 100644
--- a/vcl/source/control/spinbtn.cxx
+++ b/vcl/source/control/spinbtn.cxx
@@ -121,7 +121,7 @@ void SpinButton::Draw(OutputDevice* pDev, const Point& 
rPos, SystemTextColorFlag
     Point aPos  = pDev->LogicToPixel(rPos);
     Size aSize = GetSizePixel();
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     if ( !(nFlags & SystemTextColorFlags::Mono) )
     {
@@ -156,7 +156,6 @@ void SpinButton::Draw(OutputDevice* pDev, const Point& 
rPos, SystemTextColorFlag
     ImplDrawSpinButton(*pDev, this, aUpperRect, aLowerRect, false, false,
                        IsEnabled() && ImplIsUpperEnabled(),
                        IsEnabled() && ImplIsLowerEnabled(), mbHorz, true);
-    pDev->Pop();
 }
 
 void SpinButton::Paint(vcl::RenderContext& rRenderContext, const 
tools::Rectangle& /*rRect*/)
diff --git a/vcl/source/edit/texteng.cxx b/vcl/source/edit/texteng.cxx
index 9d60921ac082..82cc91f8c13b 100644
--- a/vcl/source/edit/texteng.cxx
+++ b/vcl/source/edit/texteng.cxx
@@ -2010,11 +2010,10 @@ void TextEngine::ImpPaint( OutputDevice* pOutDev, const 
Point& rStartPos, tools:
                                         const TextPaM aTextEnd(nPara, nIndex + 
1);
                                         if ((aTextStart < *pSelEnd) && 
(aTextEnd > *pSelStart))
                                         {
-                                            
pOutDev->Push(vcl::PushFlags::FILLCOLOR);
+                                            auto popIt = 
pOutDev->ScopedPush(vcl::PushFlags::FILLCOLOR);
                                             pOutDev->SetFillColor(
                                                 
rStyleSettings.GetHighlightColor());
                                             pOutDev->DrawRect(aTabArea);
-                                            pOutDev->Pop();
                                         }
                                         else
                                         {
diff --git a/vcl/source/edit/vclmedit.cxx b/vcl/source/edit/vclmedit.cxx
index 2b9f132b1ca6..56232b32b58e 100644
--- a/vcl/source/edit/vclmedit.cxx
+++ b/vcl/source/edit/vclmedit.cxx
@@ -1278,7 +1278,7 @@ void VclMultiLineEdit::Draw( OutputDevice* pDev, const 
Point& rPos, SystemTextCo
     vcl::Font aFont = pImpVclMEdit->GetTextWindow()->GetDrawPixelFont(pDev);
     aFont.SetTransparent( true );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetFont( aFont );
     pDev->SetTextFillColor();
@@ -1330,8 +1330,6 @@ void VclMultiLineEdit::Draw( OutputDevice* pDev, const 
Point& rPos, SystemTextCo
     aTE.SetFont( aFont );
     aTE.SetTextAlign( 
pImpVclMEdit->GetTextWindow()->GetTextEngine()->GetTextAlign() );
     aTE.Draw( pDev, Point( aPos.X() + nOffX, aPos.Y() + nOffY ) );
-
-    pDev->Pop();
 }
 
 bool VclMultiLineEdit::EventNotify( NotifyEvent& rNEvt )
diff --git a/vcl/source/filter/imet/ios2met.cxx 
b/vcl/source/filter/imet/ios2met.cxx
index b5b7e4c99415..59a81073d153 100644
--- a/vcl/source/filter/imet/ios2met.cxx
+++ b/vcl/source/filter/imet/ios2met.cxx
@@ -989,10 +989,9 @@ void OS2METReader::ReadBox(bool bGivenPos)
             tools::Polygon aPolygon( aBoxRect, nHRound, nVRound );
             if ( nFlags & 0x40 )
             {
-                pVirDev->Push( vcl::PushFlags::LINECOLOR );
+                auto popIt = pVirDev->ScopedPush(vcl::PushFlags::LINECOLOR);
                 pVirDev->SetLineColor( COL_TRANSPARENT );
                 pVirDev->DrawRect( aBoxRect, nHRound, nVRound );
-                pVirDev->Pop();
             }
             pVirDev->DrawPolyLine( aPolygon, aLineInfo );
         }
diff --git a/vcl/source/filter/ipict/shape.cxx 
b/vcl/source/filter/ipict/shape.cxx
index c1e30c541c23..83771158475d 100644
--- a/vcl/source/filter/ipict/shape.cxx
+++ b/vcl/source/filter/ipict/shape.cxx
@@ -98,12 +98,11 @@ namespace PictReaderShape {
     // HACK: here we use the line coloring when drawing the shape
     //       must be changed if other parameter are changed to draw
     //       a line/fill shape
-    dev->Push(vcl::PushFlags::LINECOLOR | vcl::PushFlags::FILLCOLOR);
+    auto popIt = dev->ScopedPush(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::FILLCOLOR);
     Color oldLColor = dev->GetLineColor();
     dev->SetFillColor(oldLColor);
     dev->SetLineColor();
     dev->DrawPolygon(poly);
-    dev->Pop();
     return true;
   }
 
diff --git a/vcl/source/filter/wmf/emfwr.cxx b/vcl/source/filter/wmf/emfwr.cxx
index a7fdc04be08c..3572980f20b5 100644
--- a/vcl/source/filter/wmf/emfwr.cxx
+++ b/vcl/source/filter/wmf/emfwr.cxx
@@ -954,7 +954,7 @@ void EMFWriter::Impl_handleLineInfoPolyPolygons(const 
LineInfo& rInfo, const bas
     if(!aFillPolyPolygon.count())
         return;
 
-    maVDev->Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR);
+    auto popIt = maVDev->ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
 
     const Color aOldLineColor(maVDev->GetLineColor());
 
@@ -965,8 +965,6 @@ void EMFWriter::Impl_handleLineInfoPolyPolygons(const 
LineInfo& rInfo, const bas
     {
         ImplWritePolyPolygonRecord(tools::PolyPolygon( 
tools::Polygon(rB2DPolygon) ));
     }
-
-    maVDev->Pop();
 }
 
 void EMFWriter::ImplWrite( const GDIMetaFile& rMtf )
diff --git a/vcl/source/gdi/gdimtf.cxx b/vcl/source/gdi/gdimtf.cxx
index 0163f4ab92fe..3a58ce2b6fc6 100644
--- a/vcl/source/gdi/gdimtf.cxx
+++ b/vcl/source/gdi/gdimtf.cxx
@@ -358,7 +358,7 @@ void GDIMetaFile::Play(OutputDevice& rOut, size_t nPos)
     // This is necessary, since old metafiles don't even know of these
     // recent add-ons. Newer metafiles must of course explicitly set
     // those states.
-    rOut.Push(vcl::PushFlags::TEXTLAYOUTMODE|vcl::PushFlags::TEXTLANGUAGE);
+    auto popIt = rOut.ScopedPush(vcl::PushFlags::TEXTLAYOUTMODE | 
vcl::PushFlags::TEXTLANGUAGE);
     rOut.SetLayoutMode(vcl::text::ComplexTextLayoutFlags::Default);
     rOut.SetDigitLanguage(LANGUAGE_SYSTEM);
 
@@ -383,7 +383,6 @@ void GDIMetaFile::Play(OutputDevice& rOut, size_t nPos)
             pAction = NextAction();
         }
     }
-    rOut.Pop();
 }
 
 bool GDIMetaFile::ImplPlayWithRenderer(OutputDevice& rOut, const Point& rPos, 
Size rLogicDestSize)
@@ -518,7 +517,7 @@ void GDIMetaFile::Play(OutputDevice& rOut, const Point& 
rPos,
     aDrawMap.SetOrigin(rOut.PixelToLogic(rOut.LogicToPixel(rPos), aDrawMap));
     rOut.SetPixelOffset(aOldOffset);
 
-    rOut.Push();
+    auto popIt = rOut.ScopedPush();
 
     bool bIsRecord = (pMtf && pMtf->IsRecord());
     rOut.SetMetafileMapMode(aDrawMap, bIsRecord);
@@ -531,8 +530,6 @@ void GDIMetaFile::Play(OutputDevice& rOut, const Point& 
rPos,
     rOut.SetDigitLanguage(LANGUAGE_SYSTEM);
 
     Play(rOut);
-
-    rOut.Pop();
 }
 
 void GDIMetaFile::Pause( bool _bPause )
diff --git a/vcl/source/gdi/graph.cxx b/vcl/source/gdi/graph.cxx
index 3575a7929c36..93f297d84c54 100644
--- a/vcl/source/gdi/graph.cxx
+++ b/vcl/source/gdi/graph.cxx
@@ -47,7 +47,7 @@ void ImplDrawDefault(OutputDevice& rOutDev, const OUString* 
pText,
     bool        bFilled = ( pBitmapEx != nullptr || pFont != nullptr );
     tools::Rectangle   aBorderRect( aPoint, aSize );
 
-    rOutDev.Push();
+    auto popIt = rOutDev.ScopedPush();
 
     rOutDev.SetFillColor();
 
@@ -155,8 +155,6 @@ void ImplDrawDefault(OutputDevice& rOutDev, const OUString* 
pText,
         rOutDev.DrawLine( aBorderRect.TopLeft(), aBorderRect.BottomRight() );
         rOutDev.DrawLine( aBorderRect.TopRight(), aBorderRect.BottomLeft() );
     }
-
-    rOutDev.Pop();
 }
 
 } // end anonymous namespace
diff --git a/vcl/source/gdi/pdfwriter_impl.cxx 
b/vcl/source/gdi/pdfwriter_impl.cxx
index 2eae76b667f2..2b1e2088eb41 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -6360,7 +6360,7 @@ bool PDFWriterImpl::emit()
 
 sal_Int32 PDFWriterImpl::getSystemFont( const vcl::Font& i_rFont )
 {
-    Push();
+    auto popIt = ScopedPush();
 
     SetFont( i_rFont );
 
@@ -6378,7 +6378,6 @@ sal_Int32 PDFWriterImpl::getSystemFont( const vcl::Font& 
i_rFont )
         m_aSystemFonts[ pFace ].m_nNormalFontID = nFontID;
     }
 
-    Pop();
     return nFontID;
 }
 
diff --git a/vcl/source/gdi/print.cxx b/vcl/source/gdi/print.cxx
index 0e598df58baa..cd26bd0bd871 100644
--- a/vcl/source/gdi/print.cxx
+++ b/vcl/source/gdi/print.cxx
@@ -1632,10 +1632,9 @@ void Printer::ClipAndDrawGradientMetafile ( const 
Gradient &rGradient, const too
 {
     const tools::Rectangle aBoundRect( rPolyPoly.GetBoundRect() );
 
-    Push( vcl::PushFlags::CLIPREGION );
+    auto popIt = ScopedPush(vcl::PushFlags::CLIPREGION);
     IntersectClipRegion(vcl::Region(rPolyPoly));
     DrawGradient( aBoundRect, rGradient );
-    Pop();
 }
 
 void Printer::SetFontOrientation( LogicalFontInstance* const pFontEntry ) const
diff --git a/vcl/source/gdi/print2.cxx b/vcl/source/gdi/print2.cxx
index 85b39c683d6d..79b224ee7616 100644
--- a/vcl/source/gdi/print2.cxx
+++ b/vcl/source/gdi/print2.cxx
@@ -56,11 +56,10 @@ void Printer::DrawGradientEx( OutputDevice* pOut, const 
tools::Rectangle& rRect,
                                    ( 
static_cast<tools::Long>(rEndColor.GetBlue()) * rGradient.GetEndIntensity() ) / 
100 ) >> 1;
             const Color     aColor( static_cast<sal_uInt8>(nR), 
static_cast<sal_uInt8>(nG), static_cast<sal_uInt8>(nB) );
 
-            pOut->Push( vcl::PushFlags::LINECOLOR | vcl::PushFlags::FILLCOLOR 
);
+            auto popIt = pOut->ScopedPush(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::FILLCOLOR);
             pOut->SetLineColor( aColor );
             pOut->SetFillColor( aColor );
             pOut->DrawRect( rRect );
-            pOut->Pop();
         }
     }
     else
diff --git a/vcl/source/gdi/print3.cxx b/vcl/source/gdi/print3.cxx
index 0da1d81fe127..98df26528e0e 100644
--- a/vcl/source/gdi/print3.cxx
+++ b/vcl/source/gdi/print3.cxx
@@ -863,7 +863,7 @@ void PrinterController::setPrinter( const VclPtr<Printer>& 
i_rPrinter )
     setValue( u"Name"_ustr,
               css::uno::Any( i_rPrinter->GetName() ) );
     mpImplData->mnDefaultPaperBin = mpImplData->mxPrinter->GetPaperBin();
-    mpImplData->mxPrinter->Push();
+    auto popIt = mpImplData->mxPrinter->ScopedPush();
     mpImplData->mxPrinter->SetMapMode(MapMode(MapUnit::Map100thMM));
     mpImplData->maDefaultPageSize = mpImplData->mxPrinter->GetPaperSize();
 
@@ -875,7 +875,6 @@ void PrinterController::setPrinter( const VclPtr<Printer>& 
i_rPrinter )
 
     mpImplData->mbPapersizeFromUser = false;
     mpImplData->mbOrientationFromUser = false;
-    mpImplData->mxPrinter->Pop();
     mpImplData->mnFixedPaperBin = -1;
 }
 
@@ -901,7 +900,7 @@ void PrinterController::setupPrinter( weld::Window* 
i_pParent )
     if( !xPrinter )
         return;
 
-    xPrinter->Push();
+    auto popIt = xPrinter->ScopedPush();
     xPrinter->SetMapMode(MapMode(MapUnit::Map100thMM));
 
     // get current data
@@ -956,7 +955,6 @@ void PrinterController::setupPrinter( weld::Window* 
i_pParent )
         if (aPaperSize != aNewPaperSize)
             xPrinter->SetPaperSizeUser(aPaperSize);
     }
-    xPrinter->Pop();
 }
 
 PrinterController::PageSize vcl::ImplPrinterControllerData::modifyJobSetup( 
const css::uno::Sequence< css::beans::PropertyValue >& i_rProps )
@@ -1037,34 +1035,29 @@ PrinterController::PageSize 
vcl::ImplPrinterControllerData::modifyJobSetup( cons
 //print dialog
 void vcl::ImplPrinterControllerData::resetPaperToLastConfigured()
 {
-    mxPrinter->Push();
+    auto popIt = mxPrinter->ScopedPush();
     mxPrinter->SetMapMode(MapMode(MapUnit::Map100thMM));
     Size aCurSize(mxPrinter->GetPaperSize());
     if (aCurSize != maDefaultPageSize)
         mxPrinter->SetPaperSizeUser(maDefaultPageSize);
-    mxPrinter->Pop();
 }
 
 int PrinterController::getPageCountProtected() const
 {
     const MapMode aMapMode( MapUnit::Map100thMM );
 
-    mpImplData->mxPrinter->Push();
+    auto popIt = mpImplData->mxPrinter->ScopedPush();
     mpImplData->mxPrinter->SetMapMode( aMapMode );
-    int nPages = getPageCount();
-    mpImplData->mxPrinter->Pop();
-    return nPages;
+    return getPageCount();
 }
 
 css::uno::Sequence< css::beans::PropertyValue > 
PrinterController::getPageParametersProtected( int i_nPage ) const
 {
     const MapMode aMapMode( MapUnit::Map100thMM );
 
-    mpImplData->mxPrinter->Push();
+    auto popIt = mpImplData->mxPrinter->ScopedPush();
     mpImplData->mxPrinter->SetMapMode( aMapMode );
-    css::uno::Sequence< css::beans::PropertyValue > aResult( 
getPageParameters( i_nPage ) );
-    mpImplData->mxPrinter->Pop();
-    return aResult;
+    return getPageParameters(i_nPage);
 }
 
 PrinterController::PageSize PrinterController::getPageFile( int 
i_nUnfilteredPage, GDIMetaFile& o_rMtf, bool i_bMayUseCache )
diff --git a/vcl/source/graphic/GraphicObject2.cxx 
b/vcl/source/graphic/GraphicObject2.cxx
index fb2584d53037..2fd570cb12b8 100644
--- a/vcl/source/graphic/GraphicObject2.cxx
+++ b/vcl/source/graphic/GraphicObject2.cxx
@@ -360,7 +360,7 @@ bool GraphicObject::ImplDrawTiled(OutputDevice& rOut, const 
tools::Rectangle& rA
         const Point aOutStart( aOutOrigin.X() + 
nInvisibleTilesX*rSizePixel.Width(),
                                aOutOrigin.Y() + 
nInvisibleTilesY*rSizePixel.Height() );
 
-        rOut.Push( vcl::PushFlags::CLIPREGION );
+        auto popIt = rOut.ScopedPush(vcl::PushFlags::CLIPREGION);
         rOut.IntersectClipRegion( rArea );
 
         // Paint all tiles
@@ -371,8 +371,6 @@ bool GraphicObject::ImplDrawTiled(OutputDevice& rOut, const 
tools::Rectangle& rA
                              (nOutAreaWidth + aOutArea.Left() - aOutStart.X() 
+ rSizePixel.Width() - 1) / rSizePixel.Width(),
                              (nOutAreaHeight + aOutArea.Top() - aOutStart.Y() 
+ rSizePixel.Height() - 1) / rSizePixel.Height(),
                              rSizePixel, pAttr);
-
-        rOut.Pop();
     }
 
     return bRet;
diff --git a/vcl/source/outdev/bitmap.cxx b/vcl/source/outdev/bitmap.cxx
index 745cf520decb..d9b5989c3e93 100644
--- a/vcl/source/outdev/bitmap.cxx
+++ b/vcl/source/outdev/bitmap.cxx
@@ -83,11 +83,10 @@ void OutputDevice::DrawBitmap( const Point& rDestPt, const 
Size& rDestSize,
             cCmpVal = 255;
 
         Color aCol(cCmpVal, cCmpVal, cCmpVal);
-        Push(vcl::PushFlags::LINECOLOR | vcl::PushFlags::FILLCOLOR);
+        auto popIt = ScopedPush(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::FILLCOLOR);
         SetLineColor(aCol);
         SetFillColor(aCol);
         DrawRect(tools::Rectangle(rDestPt, rDestSize));
-        Pop();
         return;
     }
 
diff --git a/vcl/source/outdev/font.cxx b/vcl/source/outdev/font.cxx
index 55a36f3aaa56..9d1942a8a2f9 100644
--- a/vcl/source/outdev/font.cxx
+++ b/vcl/source/outdev/font.cxx
@@ -886,7 +886,7 @@ void OutputDevice::ImplDrawEmphasisMark( tools::Long 
nBaseX, tools::Long nX, too
 
 void OutputDevice::ImplDrawEmphasisMarks( SalLayout& rSalLayout )
 {
-    Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::MAPMODE);
+    auto popIt = ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR | vcl::PushFlags::MAPMODE);
     GDIMetaFile*        pOldMetaFile    = mpMetaFile;
     mpMetaFile = nullptr;
     EnableMapMode( false );
@@ -970,7 +970,6 @@ void OutputDevice::ImplDrawEmphasisMarks( SalLayout& 
rSalLayout )
         }
     }
 
-    Pop();
     mpMetaFile = pOldMetaFile;
 }
 
diff --git a/vcl/source/outdev/gradient.cxx b/vcl/source/outdev/gradient.cxx
index 4efbc47c91b1..f8d3ca98d8a7 100644
--- a/vcl/source/outdev/gradient.cxx
+++ b/vcl/source/outdev/gradient.cxx
@@ -60,11 +60,10 @@ void OutputDevice::DrawGradient( const tools::PolyPolygon& 
rPolyPoly,
     {
         Color aColor = GetSingleColorGradientFill();
 
-        Push( vcl::PushFlags::LINECOLOR | vcl::PushFlags::FILLCOLOR );
+        auto popIt = ScopedPush(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::FILLCOLOR);
         SetLineColor( aColor );
         SetFillColor( aColor );
         DrawPolyPolygon( rPolyPoly );
-        Pop();
         return;
     }
 
@@ -97,7 +96,7 @@ void OutputDevice::DrawGradient( const tools::PolyPolygon& 
rPolyPoly,
                 return;
 
             // secure clip region
-            Push( vcl::PushFlags::CLIPREGION );
+            auto popIt = ScopedPush(vcl::PushFlags::CLIPREGION);
             IntersectClipRegion( aBoundRect );
 
             if (mbInitClipRegion)
@@ -139,8 +138,6 @@ void OutputDevice::DrawGradient( const tools::PolyPolygon& 
rPolyPoly,
                 else
                     DrawComplexGradient( aRect, aGradient, 
aClixPolyPoly.IsRect() ? nullptr : &aClixPolyPoly );
             }
-
-            Pop();
         }
     }
 }
diff --git a/vcl/source/outdev/rect.cxx b/vcl/source/outdev/rect.cxx
index efd693e13c82..c2731237d1cf 100644
--- a/vcl/source/outdev/rect.cxx
+++ b/vcl/source/outdev/rect.cxx
@@ -210,7 +210,7 @@ void OutputDevice::DrawCheckered(const Point& rPos, const 
Size& rSize, sal_uInt3
     const sal_uInt32 nMaxX(rPos.X() + rSize.Width());
     const sal_uInt32 nMaxY(rPos.Y() + rSize.Height());
 
-    Push(vcl::PushFlags::LINECOLOR|vcl::PushFlags::FILLCOLOR);
+    auto popIt = ScopedPush(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::FILLCOLOR);
     SetLineColor();
 
     for(sal_uInt32 x(0), nX(rPos.X()); nX < nMaxX; x++, nX += nLen)
@@ -225,8 +225,6 @@ void OutputDevice::DrawCheckered(const Point& rPos, const 
Size& rSize, sal_uInt3
             DrawRect(tools::Rectangle(nX, nY, nRight, nBottom));
         }
     }
-
-    Pop();
 }
 
 void OutputDevice::DrawGrid( const tools::Rectangle& rRect, const Size& rDist, 
DrawGridFlags nFlags )
diff --git a/vcl/source/outdev/text.cxx b/vcl/source/outdev/text.cxx
index 4bef5eef1ce2..febf5f433810 100644
--- a/vcl/source/outdev/text.cxx
+++ b/vcl/source/outdev/text.cxx
@@ -1657,12 +1657,11 @@ void OutputDevice::ImplDrawText( OutputDevice& 
rTargetDevice, const tools::Recta
 
         if ( nStyle & DrawTextFlags::Clip )
         {
-            rTargetDevice.Push( vcl::PushFlags::CLIPREGION );
+            auto popIt = rTargetDevice.ScopedPush(vcl::PushFlags::CLIPREGION);
             rTargetDevice.IntersectClipRegion( rRect );
             _rLayout.DrawText( aPos, aStr, 0, aStr.getLength(), pVector, 
pDisplayText );
             if ( bDrawMnemonics && nMnemonicPos != -1 )
                 rTargetDevice.ImplDrawMnemonicLine( nMnemonicX, nMnemonicY, 
nMnemonicWidth );
-            rTargetDevice.Pop();
         }
         else
         {
diff --git a/vcl/source/outdev/transparent.cxx 
b/vcl/source/outdev/transparent.cxx
index 22100e84aac1..9310182d611f 100644
--- a/vcl/source/outdev/transparent.cxx
+++ b/vcl/source/outdev/transparent.cxx
@@ -472,10 +472,9 @@ void OutputDevice::EmulateDrawTransparent ( const 
tools::PolyPolygon& rPolyPoly,
 
                     if( mbLineColor )
                     {
-                        Push( vcl::PushFlags::FILLCOLOR );
+                        auto popIt = ScopedPush(vcl::PushFlags::FILLCOLOR);
                         SetFillColor();
                         DrawPolyPolygon( rPolyPoly );
-                        Pop();
                     }
                 }
             }
@@ -1765,8 +1764,8 @@ bool OutputDevice::RemoveTransparenciesFromMetaFile( 
const GDIMetaFile& rInMtf,
                                 if( !tools::Rectangle( aDstPtPix, aDstSzPix 
).Intersection( aBoundRect ).IsEmpty() &&
                                     aPaintVDev->SetOutputSizePixel( aDstSzPix 
) )
                                 {
-                                    aPaintVDev->Push();
-                                    aMapVDev->Push();
+                                    auto popIt1 = aPaintVDev->ScopedPush();
+                                    auto popIt2 = aMapVDev->ScopedPush();
 
                                     aMapVDev->mnDPIX = aPaintVDev->mnDPIX = 
mnDPIX;
                                     aMapVDev->mnDPIY = aPaintVDev->mnDPIY = 
mnDPIY;
@@ -1837,8 +1836,6 @@ bool OutputDevice::RemoveTransparenciesFromMetaFile( 
const GDIMetaFile& rInMtf,
 
                                     aPaintVDev->mbMap = true;
                                     mbMap = bOldMap;
-                                    aMapVDev->Pop();
-                                    aPaintVDev->Pop();
                                 }
 
                                 // overlapping bands to avoid missing lines 
(e.g. PostScript)
diff --git a/vcl/source/toolkit/group.cxx b/vcl/source/toolkit/group.cxx
index 7b23e1006ef5..1c693de52d81 100644
--- a/vcl/source/toolkit/group.cxx
+++ b/vcl/source/toolkit/group.cxx
@@ -183,7 +183,7 @@ void GroupBox::Draw( OutputDevice* pDev, const Point& rPos,
     Size        aSize = GetSizePixel();
     vcl::Font   aFont = GetDrawPixelFont( pDev );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetFont( aFont );
     if ( nFlags & SystemTextColorFlags::Mono )
@@ -193,7 +193,6 @@ void GroupBox::Draw( OutputDevice* pDev, const Point& rPos,
     pDev->SetTextFillColor();
 
     ImplDraw( pDev, nFlags, aPos, aSize );
-    pDev->Pop();
 }
 
 void GroupBox::Resize()
diff --git a/vcl/source/treelist/headbar.cxx b/vcl/source/treelist/headbar.cxx
index bd4eaec8532f..8b60a6b3c382 100644
--- a/vcl/source/treelist/headbar.cxx
+++ b/vcl/source/treelist/headbar.cxx
@@ -883,7 +883,7 @@ void HeaderBar::Draw( OutputDevice* pDev, const Point& rPos,
     tools::Rectangle   aRect( aPos, aSize );
     vcl::Font   aFont = GetDrawPixelFont( pDev );
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetFont( aFont );
     if ( nFlags & SystemTextColorFlags::Mono )
@@ -925,8 +925,6 @@ void HeaderBar::Draw( OutputDevice* pDev, const Point& rPos,
         ImplDrawItem(*pDev, i, false, aItemRect, &aRect );
         pDev->SetClipRegion();
     }
-
-    pDev->Pop();
 }
 
 void HeaderBar::Resize()
diff --git a/vcl/source/treelist/iconview.cxx b/vcl/source/treelist/iconview.cxx
index b946ca1ea834..2eb254eb2fe0 100644
--- a/vcl/source/treelist/iconview.cxx
+++ b/vcl/source/treelist/iconview.cxx
@@ -149,8 +149,8 @@ void IconView::PaintEntry(SvTreeListEntry& rEntry, 
tools::Long nX, tools::Long n
 
     Point aEntryPos(nX, nY);
 
-    rRenderContext.Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR
-                        | vcl::PushFlags::FONT);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR
+                                           | vcl::PushFlags::FONT);
     const Color aBackupColor = rRenderContext.GetFillColor();
 
     const StyleSettings& rSettings = 
rRenderContext.GetSettings().GetStyleSettings();
@@ -262,8 +262,6 @@ void IconView::PaintEntry(SvTreeListEntry& rEntry, 
tools::Long nX, tools::Long n
 
         rItem.Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
     }
-
-    rRenderContext.Pop();
 }
 
 FactoryFunction IconView::GetUITestFactory() const { return 
IconViewUIObject::create; }
diff --git a/vcl/source/treelist/svimpbox.cxx b/vcl/source/treelist/svimpbox.cxx
index 640d0706548e..730d3ded3f01 100644
--- a/vcl/source/treelist/svimpbox.cxx
+++ b/vcl/source/treelist/svimpbox.cxx
@@ -1017,7 +1017,7 @@ void SvImpLBox::DrawNet(vcl::RenderContext& 
rRenderContext)
 
     DBG_ASSERT(pFirstDynamicTab,"No Tree!");
 
-    rRenderContext.Push(vcl::PushFlags::LINECOLOR);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::LINECOLOR);
 
     const StyleSettings& rStyleSettings = 
rRenderContext.GetSettings().GetStyleSettings();
 
@@ -1062,8 +1062,6 @@ void SvImpLBox::DrawNet(vcl::RenderContext& 
rRenderContext)
         nY += nEntryHeight;
         pEntry = m_pView->NextVisible(pEntry);
     }
-
-    rRenderContext.Pop();
 }
 
 void SvImpLBox::PositionScrollBars( Size& rSize, sal_uInt16 nMask )
diff --git a/vcl/source/treelist/svlbitm.cxx b/vcl/source/treelist/svlbitm.cxx
index 38f8d74cb9f0..0af34c8cedf2 100644
--- a/vcl/source/treelist/svlbitm.cxx
+++ b/vcl/source/treelist/svlbitm.cxx
@@ -177,14 +177,13 @@ namespace
 {
     void drawSeparator(vcl::RenderContext& rRenderContext, const 
tools::Rectangle& rRegion)
     {
-        rRenderContext.Push(vcl::PushFlags::LINECOLOR);
+        auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::LINECOLOR);
         const StyleSettings& rStyle = 
rRenderContext.GetSettings().GetStyleSettings();
         Point aTmpPos = rRegion.TopLeft();
         Size aSize = rRegion.GetSize();
         aTmpPos.AdjustY(aSize.Height() / 2 );
         rRenderContext.SetLineColor(rStyle.GetShadowColor());
         rRenderContext.DrawLine(aTmpPos, Point(aSize.Width() + aTmpPos.X(), 
aTmpPos.Y()));
-        rRenderContext.Pop();
     }
 }
 
diff --git a/vcl/source/treelist/treelistbox.cxx 
b/vcl/source/treelist/treelistbox.cxx
index 4f5c85947760..fcf069b14019 100644
--- a/vcl/source/treelist/treelistbox.cxx
+++ b/vcl/source/treelist/treelistbox.cxx
@@ -2835,7 +2835,7 @@ void SvTreeListBox::PaintEntry1(SvTreeListEntry& rEntry, 
tools::Long nLine, vcl:
 
     if (pViewDataEntry->IsDragTarget())
     {
-        rRenderContext.Push();
+        auto popIt = rRenderContext.ScopedPush();
         rRenderContext.SetLineColor(rSettings.GetDeactiveColor());
         rRenderContext.SetFillColor(rSettings.GetDeactiveColor());
 
@@ -2849,8 +2849,6 @@ void SvTreeListBox::PaintEntry1(SvTreeListEntry& rEntry, 
tools::Long nLine, vcl:
         {
             rRenderContext.DrawRect(tools::Rectangle(Point(0, nLine), 
Size(nWidth, 2)));
         }
-
-        rRenderContext.Pop();
     }
 
     if (bCurFontIsSel || rEntry.GetTextColor())
diff --git a/vcl/source/window/decoview.cxx b/vcl/source/window/decoview.cxx
index 9a336372beb1..1730b969ab8f 100644
--- a/vcl/source/window/decoview.cxx
+++ b/vcl/source/window/decoview.cxx
@@ -76,10 +76,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             arrow.SetPoint( Point( aCenter.X() + n8, nRect.Bottom()), 4 );
             arrow.SetPoint( Point( aCenter.X() + n8, nRect.Top() + n2 ), 5 );
             arrow.SetPoint( Point( aCenter.X() + n2, nRect.Top() + n2 ), 6 );
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( arrow );
-            pDev->Pop();
             break;
         }
 
@@ -93,10 +92,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             arrow.SetPoint( Point( aCenter.X() + n8, nRect.Top()), 4 );
             arrow.SetPoint( Point( aCenter.X() + n8, nRect.Bottom() - n2 ), 5 
);
             arrow.SetPoint( Point( aCenter.X() + n2, nRect.Bottom() - n2 ), 6 
);
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( arrow );
-            pDev->Pop();
             break;
         }
 
@@ -110,10 +108,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             arrow.SetPoint( Point( nRect.Right(), aCenter.Y() + n8 ), 4 );
             arrow.SetPoint( Point( nRect.Left() + n2, aCenter.Y() + n8 ), 5 );
             arrow.SetPoint( Point( nRect.Left() + n2, aCenter.Y() + n2 ), 6 );
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( arrow );
-            pDev->Pop();
             break;
         }
 
@@ -127,10 +124,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             arrow.SetPoint( Point( nRect.Left(), aCenter.Y() + n8 ), 4 );
             arrow.SetPoint( Point( nRect.Right() - n2, aCenter.Y() + n8 ), 5 );
             arrow.SetPoint( Point( nRect.Right() - n2, aCenter.Y() + n2 ), 6 );
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( arrow );
-            pDev->Pop();
             break;
         }
 
@@ -140,10 +136,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             triangle.SetPoint( Point( aCenter.X(), nRect.Top() + n4 ), 0 );
             triangle.SetPoint( Point( aCenter.X() - n2, nRect.Top() + n4 + n2 
), 1 );
             triangle.SetPoint( Point( aCenter.X() + n2, nRect.Top() + n4 + n2 
), 2 );
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( triangle );
-            pDev->Pop();
             break;
         }
 
@@ -153,10 +148,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             triangle.SetPoint( Point( aCenter.X(), nRect.Bottom() - n4 ), 0 );
             triangle.SetPoint( Point( aCenter.X() - n2, nRect.Bottom() - n4 - 
n2 ), 1 );
             triangle.SetPoint( Point( aCenter.X() + n2, nRect.Bottom() - n4 - 
n2 ), 2 );
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( triangle );
-            pDev->Pop();
             break;
         }
 
@@ -177,10 +171,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             aTriangle.SetPoint(Point(nRect.Left(), aCenter.Y()), 1);
             aTriangle.SetPoint(Point(nRect.Left() + n2, aCenter.Y() + n2), 2);
 
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon(aTriangle);
-            pDev->Pop();
 
             break;
         }
@@ -203,10 +196,9 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             aTriangle.SetPoint(Point(nRect.Right(), aCenter.Y()), 1);
             aTriangle.SetPoint(Point(nRect.Right() - n2, aCenter.Y() + n2), 2);
 
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon(aTriangle);
-            pDev->Pop();
             break;
         }
 
@@ -216,12 +208,11 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             triangle.SetPoint( Point( aCenter.X(), nRect.Top()), 0 );
             triangle.SetPoint( Point( aCenter.X() - n2, nRect.Top() + n2 ), 1 
);
             triangle.SetPoint( Point( aCenter.X() + n2, nRect.Top() + n2 ), 2 
);
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( triangle );
             triangle.Move( 0, n2 );
             pDev->DrawPolygon( triangle );
-            pDev->Pop();
             break;
         }
 
@@ -231,12 +222,11 @@ void ImplDrawSymbol( OutputDevice* pDev, tools::Rectangle 
nRect, const SymbolTyp
             triangle.SetPoint( Point( aCenter.X(), nRect.Bottom()), 0 );
             triangle.SetPoint( Point( aCenter.X() - n2, nRect.Bottom() - n2 ), 
1 );
             triangle.SetPoint( Point( aCenter.X() + n2, nRect.Bottom() - n2 ), 
2 );
-            pDev->Push(vcl::PushFlags::LINECOLOR);
+            auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR);
             pDev->SetLineColor();
             pDev->DrawPolygon( triangle );
             triangle.Move( 0, -n2 );
             pDev->DrawPolygon( triangle );
-            pDev->Pop();
             break;
         }
 
@@ -770,7 +760,7 @@ void DecorationView::DrawSymbol( const tools::Rectangle& 
rRect, SymbolType eType
 {
     const StyleSettings&    rStyleSettings  = 
mpOutDev->GetSettings().GetStyleSettings();
     const tools::Rectangle         aRect           = mpOutDev->LogicToPixel( 
rRect );
-    mpOutDev->Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::MAPMODE);
+    auto popIt = mpOutDev->ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR | vcl::PushFlags::MAPMODE);
     Color                   nColor(rColor);
     mpOutDev->EnableMapMode( false );
 
@@ -799,9 +789,6 @@ void DecorationView::DrawSymbol( const tools::Rectangle& 
rRect, SymbolType eType
     mpOutDev->SetLineColor( nColor );
     mpOutDev->SetFillColor( nColor );
     ImplDrawSymbol( mpOutDev, aRect, eType );
-
-    // Restore previous settings
-    mpOutDev->Pop();
 }
 
 void DecorationView::DrawFrame( const tools::Rectangle& rRect,
@@ -869,9 +856,8 @@ tools::Rectangle DecorationView::DrawFrame( const 
tools::Rectangle& rRect, DrawF
              ImplDrawFrame( mpOutDev, aRect, 
mpOutDev->GetSettings().GetStyleSettings(), nStyle, nFlags );
         else
         {
-             mpOutDev->Push(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
+             auto popIt = mpOutDev->ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
              ImplDrawFrame( mpOutDev, aRect, 
mpOutDev->GetSettings().GetStyleSettings(), nStyle, nFlags );
-             mpOutDev->Pop();
         }
     }
 
@@ -965,15 +951,13 @@ void DecorationView::DrawSeparator( const Point& rStart, 
const Point& rStop, boo
             return;
     }
 
-    mpOutDev->Push( vcl::PushFlags::LINECOLOR );
+    auto popIt = mpOutDev->ScopedPush(vcl::PushFlags::LINECOLOR);
     if ( rStyleSettings.GetOptions() & StyleSettingsOptions::Mono )
         mpOutDev->SetLineColor( COL_BLACK );
     else
         mpOutDev->SetLineColor( rStyleSettings.GetSeparatorColor() );
 
     mpOutDev->DrawLine( aStart, aStop );
-
-    mpOutDev->Pop();
 }
 
 void DecorationView::DrawHandle(const tools::Rectangle& rRect)
diff --git a/vcl/source/window/dialog.cxx b/vcl/source/window/dialog.cxx
index f5d52cd8e9f6..8dae1642defe 100644
--- a/vcl/source/window/dialog.cxx
+++ b/vcl/source/window/dialog.cxx
@@ -1397,7 +1397,7 @@ void Dialog::Draw( OutputDevice* pDev, const Point& rPos, 
SystemTextColorFlags )
     if ( !aWallpaper.IsBitmap() )
         ImplInitSettings();
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetLineColor();
 
@@ -1419,8 +1419,6 @@ void Dialog::Draw( OutputDevice* pDev, const Point& rPos, 
SystemTextColorFlags )
 
         aImplWin->Draw( pDev, aPos );
     }
-
-    pDev->Pop();
 }
 
 void Dialog::queue_resize(StateChangedType eReason)
diff --git a/vcl/source/window/menu.cxx b/vcl/source/window/menu.cxx
index 1b24ea96ca09..76e4eeae10eb 100644
--- a/vcl/source/window/menu.cxx
+++ b/vcl/source/window/menu.cxx
@@ -106,7 +106,7 @@ void ImplClosePopupToolBox( const VclPtr<vcl::Window>& pWin 
)
 // Draw the ">>" - more indicator at the coordinates
 void lclDrawMoreIndicator(vcl::RenderContext& rRenderContext, const 
tools::Rectangle& rRect)
 {
-    rRenderContext.Push(PushFlags::FILLCOLOR | PushFlags::LINECOLOR);
+    auto popIt = rRenderContext.ScopedPush(PushFlags::FILLCOLOR | 
PushFlags::LINECOLOR);
     rRenderContext.SetLineColor();
 
     if 
(rRenderContext.GetSettings().GetStyleSettings().GetFaceColor().IsDark())
@@ -140,7 +140,6 @@ void lclDrawMoreIndicator(vcl::RenderContext& 
rRenderContext, const tools::Recta
         else            x++;
         height--;
     }
-    rRenderContext.Pop();
 }
 
 } // end anonymous namespace
@@ -1568,7 +1567,7 @@ Size Menu::ImplCalcSize( vcl::Window* pWin )
     nTitleHeight = 0;
     if (!IsMenuBar() && aTitleText.getLength() > 0) {
         // Set expected font
-        pWin->GetOutDev()->Push(PushFlags::FONT);
+        auto popIt = pWin->GetOutDev()->ScopedPush(PushFlags::FONT);
         vcl::Font aFont = pWin->GetFont();
         aFont.SetWeight(WEIGHT_BOLD);
         pWin->SetFont(aFont);
@@ -1582,7 +1581,6 @@ Size Menu::ImplCalcSize( vcl::Window* pWin )
         aSz.AdjustHeight(nTitleHeight );
 
         tools::Long nWidth = aTextBoundRect.GetSize().Width() + 4 * 
SPACE_AROUND_TITLE;
-        pWin->GetOutDev()->Pop();
         if ( nWidth > nMaxWidth )
             nMaxWidth = nWidth;
     }
diff --git a/vcl/source/window/menubarwindow.cxx 
b/vcl/source/window/menubarwindow.cxx
index dc9c39421a35..47cc1abd19e1 100644
--- a/vcl/source/window/menubarwindow.cxx
+++ b/vcl/source/window/menubarwindow.cxx
@@ -915,13 +915,12 @@ void MenuBarWindow::Paint(vcl::RenderContext& 
rRenderContext, const tools::Recta
     if (!rRenderContext.IsNativeControlSupported( ControlType::Menubar, 
ControlPart::Entire) &&
         rStyleSettings.GetHighContrastMode())
     {
-        pBuffer->Push(vcl::PushFlags::LINECOLOR | vcl::PushFlags::MAPMODE);
+        auto popIt = pBuffer->ScopedPush(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::MAPMODE);
         pBuffer->SetLineColor(COL_WHITE);
         pBuffer->SetMapMode(MapMode(MapUnit::MapPixel));
         Size aSize = GetSizePixel();
         pBuffer->DrawLine(Point(0, aSize.Height() - 1),
                           Point(aSize.Width() - 1, aSize.Height() - 1));
-        pBuffer->Pop();
     }
 }
 
diff --git a/vcl/source/window/menufloatingwindow.cxx 
b/vcl/source/window/menufloatingwindow.cxx
index d5a94532502d..9c6359e6fb28 100644
--- a/vcl/source/window/menufloatingwindow.cxx
+++ b/vcl/source/window/menufloatingwindow.cxx
@@ -867,7 +867,7 @@ void 
MenuFloatingWindow::RenderHighlightItem(vcl::RenderContext& rRenderContext,
                 if 
(rRenderContext.IsNativeControlSupported(ControlType::MenuPopup, 
ControlPart::Entire))
                 {
                     Size aPxSize(GetOutputSizePixel());
-                    rRenderContext.Push(vcl::PushFlags::CLIPREGION);
+                    auto popIt = 
rRenderContext.ScopedPush(vcl::PushFlags::CLIPREGION);
                     
rRenderContext.IntersectClipRegion(tools::Rectangle(Point(nX, nY), 
Size(aSz.Width(), pData->aSz.Height())));
                     tools::Rectangle aCtrlRect(Point(nX, 0), 
Size(aPxSize.Width()-nX, aPxSize.Height()));
                     MenupopupValue aVal(pMenu->nTextPos-GUTTERBORDER, 
aItemRect);
@@ -887,7 +887,6 @@ void 
MenuFloatingWindow::RenderHighlightItem(vcl::RenderContext& rRenderContext,
                     }
                     else
                         bDrawItemRect = true;
-                    rRenderContext.Pop();
                 }
                 if (bDrawItemRect)
                 {
@@ -1209,7 +1208,7 @@ void MenuFloatingWindow::Paint(vcl::RenderContext& 
rRenderContext, const tools::
 
     // Set the clip before the buffering starts: rPaintRect may be larger than 
the current clip,
     // this way the buffer -> render context copy happens with this clip.
-    rRenderContext.Push(vcl::PushFlags::CLIPREGION);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::CLIPREGION);
     rRenderContext.SetClipRegion(vcl::Region(rPaintRect));
 
     // Make sure that all actual rendering happens in one go to avoid flicker.
@@ -1238,7 +1237,6 @@ void MenuFloatingWindow::Paint(vcl::RenderContext& 
rRenderContext, const tools::
         RenderHighlightItem(*pBuffer, nHighlightedItem);
 
     pBuffer.Dispose();
-    rRenderContext.Pop();
 }
 
 void MenuFloatingWindow::ImplDrawScroller(vcl::RenderContext& rRenderContext, 
bool bUp)
diff --git a/vcl/source/window/paint.cxx b/vcl/source/window/paint.cxx
index 67ec9e229224..34514f5fe878 100644
--- a/vcl/source/window/paint.cxx
+++ b/vcl/source/window/paint.cxx
@@ -373,7 +373,7 @@ void 
RenderTools::DrawSelectionBackground(vcl::RenderContext& rRenderContext, vc
         aRect.AdjustRight(1 );
         aRect.AdjustBottom(1 );
     }
-    rRenderContext.Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
 
     if (bDrawBorder)
         rRenderContext.SetLineColor(bDark ? COL_WHITE : (bBright ? COL_BLACK : 
aSelectionBorderColor));
@@ -472,8 +472,6 @@ void 
RenderTools::DrawSelectionBackground(vcl::RenderContext& rRenderContext, vc
             rRenderContext.DrawTransparent(aPolyPoly, nPercent);
         }
     }
-
-    rRenderContext.Pop(); // LINECOLOR | FILLCOLOR
 }
 
 void Window::PushPaintHelper(PaintHelper *pHelper, vcl::RenderContext& 
rRenderContext)
diff --git a/vcl/source/window/printdlg.cxx b/vcl/source/window/printdlg.cxx
index 3f00b73a3a37..55e69b211845 100644
--- a/vcl/source/window/printdlg.cxx
+++ b/vcl/source/window/printdlg.cxx
@@ -119,7 +119,7 @@ void PrintDialog::PrintPreviewWindow::Resize()
 
 void PrintDialog::PrintPreviewWindow::Paint(vcl::RenderContext& 
rRenderContext, const tools::Rectangle&)
 {
-    rRenderContext.Push();
+    auto popIt = rRenderContext.ScopedPush();
     weld::SetPointFont(rRenderContext, 
rRenderContext.GetSettings().GetStyleSettings().GetLabelFont());
     
rRenderContext.SetTextColor(rRenderContext.GetSettings().GetStyleSettings().GetLabelTextColor());
     
rRenderContext.SetBackground(Wallpaper(Application::GetSettings().GetStyleSettings().GetDialogColor()));
@@ -145,7 +145,7 @@ void 
PrintDialog::PrintPreviewWindow::Paint(vcl::RenderContext& rRenderContext,
 
     // vertical line
     {
-        rRenderContext.Push(PushFlags::FONT);
+        auto popIt2 = rRenderContext.ScopedPush(PushFlags::FONT);
         vcl::Font aFont(rRenderContext.GetFont());
         aFont.SetOrientation(900_deg10);
         rRenderContext.SetFont(aFont);
@@ -161,8 +161,6 @@ void 
PrintDialog::PrintPreviewWindow::Paint(vcl::RenderContext& rRenderContext,
         nLeft = aOffset.X() - (nTextHeight / 2);
         aDecoView.DrawSeparator(Point(nLeft, aOffset.Y()), Point(nLeft, nStart 
- nWidth - 2), true);
         aDecoView.DrawSeparator(Point(nLeft, nStart + 2), Point(nLeft, 
aOffset.Y() + maPreviewSize.Height()), true);
-
-        rRenderContext.Pop();
     }
 
     if (!maReplacementString.isEmpty())
@@ -188,8 +186,6 @@ void 
PrintDialog::PrintPreviewWindow::Paint(vcl::RenderContext& rRenderContext,
     tools::Rectangle aFrameRect(aOffset + Point(-1, -1), 
Size(maPreviewSize.Width() + 2, maPreviewSize.Height() + 2));
     DecorationView aDecorationView(&rRenderContext);
     aDecorationView.DrawFrame(aFrameRect, DrawFrameStyle::Group);
-
-    rRenderContext.Pop();
 }
 
 bool PrintDialog::PrintPreviewWindow::Command( const CommandEvent& rEvt )
diff --git a/vcl/source/window/status.cxx b/vcl/source/window/status.cxx
index 6fbf44d36c38..16e5d6ec4e0c 100644
--- a/vcl/source/window/status.cxx
+++ b/vcl/source/window/status.cxx
@@ -713,7 +713,7 @@ void StatusBar::Paint(vcl::RenderContext& rRenderContext, 
const tools::Rectangle
 
     if (mbProgressMode)
     {
-        rRenderContext.Push(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
+        auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
 
         const StyleSettings& rStyleSettings = 
rRenderContext.GetSettings().GetStyleSettings();
         Color aProgressColor = rStyleSettings.GetHighlightColor();
@@ -723,8 +723,6 @@ void StatusBar::Paint(vcl::RenderContext& rRenderContext, 
const tools::Rectangle
         rRenderContext.SetFillColor(aProgressColor);
 
         ImplDrawProgress(rRenderContext, mnPercent);
-
-        rRenderContext.Pop();
     }
     else
     {
diff --git a/vcl/source/window/tabpage.cxx b/vcl/source/window/tabpage.cxx
index f35729d66d76..ee7920c57f2c 100644
--- a/vcl/source/window/tabpage.cxx
+++ b/vcl/source/window/tabpage.cxx
@@ -170,7 +170,7 @@ void TabPage::Draw( OutputDevice* pDev, const Point& rPos, 
SystemTextColorFlags
     if ( !aWallpaper.IsBitmap() )
         ImplInitSettings();
 
-    pDev->Push();
+    auto popIt = pDev->ScopedPush();
     pDev->SetMapMode();
     pDev->SetLineColor();
 
@@ -184,8 +184,6 @@ void TabPage::Draw( OutputDevice* pDev, const Point& rPos, 
SystemTextColorFlags
             pDev->SetFillColor( aWallpaper.GetColor() );
         pDev->DrawRect( tools::Rectangle( aPos, aSize ) );
     }
-
-    pDev->Pop();
 }
 
 Size TabPage::GetOptimalSize() const
diff --git a/vcl/source/window/toolbox.cxx b/vcl/source/window/toolbox.cxx
index 945ac78784f0..202d99e647ac 100644
--- a/vcl/source/window/toolbox.cxx
+++ b/vcl/source/window/toolbox.cxx
@@ -484,7 +484,7 @@ void ToolBox::ImplDrawBackground(vcl::RenderContext& 
rRenderContext, const tools
     if (IsInPaint())
         aPaintRegion.Intersect(GetOutDev()->GetActiveClipRegion());
 
-    rRenderContext.Push(vcl::PushFlags::CLIPREGION);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::CLIPREGION);
     rRenderContext.IntersectClipRegion( aPaintRegion );
 
     if (!pWrapper)
@@ -515,9 +515,6 @@ void ToolBox::ImplDrawBackground(vcl::RenderContext& 
rRenderContext, const tools
                 ImplDrawGradientBackground(rRenderContext);
         }
     }
-
-    // restore clip region
-    rRenderContext.Pop();
 }
 
 void ToolBox::ImplErase(vcl::RenderContext& rRenderContext, const 
tools::Rectangle &rRect, bool bHighlight, bool bHasOpenPopup)
@@ -530,7 +527,7 @@ void ToolBox::ImplErase(vcl::RenderContext& rRenderContext, 
const tools::Rectang
     {
         if (GetStyle() & WB_3DLOOK)
         {
-            rRenderContext.Push(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::FILLCOLOR);
+            auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::LINECOLOR | 
vcl::PushFlags::FILLCOLOR);
             rRenderContext.SetLineColor();
             if (bHasOpenPopup)
                 // choose the same color as the popup will use
@@ -539,7 +536,6 @@ void ToolBox::ImplErase(vcl::RenderContext& rRenderContext, 
const tools::Rectang
                 
rRenderContext.SetFillColor(rRenderContext.GetSettings().GetStyleSettings().GetWindowColor());
 
             rRenderContext.DrawRect(rRect);
-            rRenderContext.Pop();
         }
         else
             ImplDrawBackground(rRenderContext, rRect);
@@ -2368,7 +2364,7 @@ static void ImplDrawMoreIndicator(vcl::RenderContext& 
rRenderContext, const tool
 
 static void ImplDrawDropdownArrow(vcl::RenderContext& rRenderContext, const 
tools::Rectangle& rDropDownRect, bool bSetColor, bool bRotate )
 {
-    rRenderContext.Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
     rRenderContext.SetLineColor();
 
     if ( bSetColor )
@@ -2405,8 +2401,6 @@ static void ImplDrawDropdownArrow(vcl::RenderContext& 
rRenderContext, const tool
     rRenderContext.SetAntialiasing(AntialiasingFlags::Enable);
     rRenderContext.DrawPolygon( aPoly );
     rRenderContext.SetAntialiasing(aaflags);
-
-    rRenderContext.Pop();
 }
 
 void ToolBox::ImplDrawMenuButton(vcl::RenderContext& rRenderContext, bool 
bHighlight)
@@ -2421,7 +2415,7 @@ void ToolBox::ImplDrawMenuButton(vcl::RenderContext& 
rRenderContext, bool bHighl
     // execute pending paint requests
     ImplCheckUpdate();
 
-    rRenderContext.Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR);
+    auto popIt = rRenderContext.ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
 
     // draw the 'more' indicator / button (>>)
     ImplErase(rRenderContext, mpData->maMenubuttonItem.maRect, bHighlight);
@@ -2434,9 +2428,6 @@ void ToolBox::ImplDrawMenuButton(vcl::RenderContext& 
rRenderContext, bool bHighl
 
     // store highlight state
     mpData->mbMenubuttonSelected = bHighlight;
-
-    // restore colors
-    rRenderContext.Pop();
 }
 
 void ToolBox::ImplDrawSpin(vcl::RenderContext& rRenderContext)
diff --git a/vcl/source/window/window.cxx b/vcl/source/window/window.cxx
index 420e78e1d46b..e6fc3d032f62 100644
--- a/vcl/source/window/window.cxx
+++ b/vcl/source/window/window.cxx
@@ -3512,7 +3512,7 @@ void Window::DrawSelectionBackground( const 
tools::Rectangle& rRect,
     }
 
     tools::Rectangle aRect( rRect );
-    GetOutDev()->Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR);
+    auto popIt = GetOutDev()->ScopedPush(vcl::PushFlags::FILLCOLOR | 
vcl::PushFlags::LINECOLOR);
 
     if( bDrawBorder )
         GetOutDev()->SetLineColor( bDark ? COL_WHITE : ( bBright ? COL_BLACK : 
aSelectionBorderCol ) );
@@ -3585,8 +3585,6 @@ void Window::DrawSelectionBackground( const 
tools::Rectangle& rRect,
         tools::PolyPolygon aPolyPoly( aPoly );
         GetOutDev()->DrawTransparent( aPolyPoly, nPercent );
     }
-
-    GetOutDev()->Pop();
 }
 
 bool Window::IsScrollable() const
diff --git a/vcl/workben/svptest.cxx b/vcl/workben/svptest.cxx
index fa6da5b1dfb0..b18b1ecb32db 100644
--- a/vcl/workben/svptest.cxx
+++ b/vcl/workben/svptest.cxx
@@ -200,7 +200,7 @@ void MyWin::Paint(vcl::RenderContext& rRenderContext, const 
tools::Rectangle& rR
 {
     WorkWindow::Paint(rRenderContext, rRect);
 
-    rRenderContext.Push();
+    auto popIt = rRenderContext.ScopedPush();
     MapMode aMapMode(MapUnit::Map100thMM);
 
     rRenderContext.SetMapMode(aMapMode);
@@ -315,7 +315,6 @@ void MyWin::Paint(vcl::RenderContext& rRenderContext, const 
tools::Rectangle& rR
         aPoint.setY( static_cast<int>((static_cast<double>(aP2.Y())*cosd + 
static_cast<double>(aP2.X())*sind)*factor) );
         aP2 = aPoint;
     }
-    rRenderContext.Pop();
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to