From 71f02295f2dca557fddd7dbdd19bfd464de06508 Mon Sep 17 00:00:00 2001 From: Dmitriy Shilin Date: Fri, 14 Dec 2018 09:34:34 +0300 Subject: tdf#107792 vcl/win/gdi: introduce ScopedHBRUSH Change-Id: Ieca634892f4af1b0c38fa6bd8a8bbbb8f1c7370d Reviewed-on: https://gerrit.libreoffice.org/65141 Reviewed-by: Mike Kaganski Tested-by: Mike Kaganski --- vcl/win/gdi/gdiimpl.cxx | 40 +++++++++++++++++++++------------------- vcl/win/gdi/scoped_gdi.hxx | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 vcl/win/gdi/scoped_gdi.hxx (limited to 'vcl') diff --git a/vcl/win/gdi/gdiimpl.cxx b/vcl/win/gdi/gdiimpl.cxx index 50238bf801a9..7b489e32261a 100644 --- a/vcl/win/gdi/gdiimpl.cxx +++ b/vcl/win/gdi/gdiimpl.cxx @@ -20,6 +20,7 @@ #include #include "gdiimpl.hxx" +#include "scoped_gdi.hxx" #include #include @@ -772,9 +773,9 @@ bool WinSalGraphicsImpl::drawAlphaRect( long nX, long nY, long nWidth, return bRet; } -void WinSalGraphicsImpl::drawMask( const SalTwoRect& rPosAry, - const SalBitmap& rSSalBitmap, - Color nMaskColor ) +void WinSalGraphicsImpl::drawMask(const SalTwoRect& rPosAry, + const SalBitmap& rSSalBitmap, + Color nMaskColor) { SAL_WARN_IF( mrParent.isPrinter(), "vcl", "No transparency print possible!" ); @@ -783,12 +784,12 @@ void WinSalGraphicsImpl::drawMask( const SalTwoRect& rPosAry, const WinSalBitmap& rSalBitmap = static_cast(rSSalBitmap); SalTwoRect aPosAry = rPosAry; - const BYTE cRed = nMaskColor.GetRed(); - const BYTE cGreen = nMaskColor.GetGreen(); - const BYTE cBlue = nMaskColor.GetBlue(); - HDC hDC = mrParent.getHDC(); - HBRUSH hMaskBrush = CreateSolidBrush( RGB( cRed, cGreen, cBlue ) ); - HBRUSH hOldBrush = SelectBrush( hDC, hMaskBrush ); + const HDC hDC = mrParent.getHDC(); + + ScopedHBRUSH hMaskBrush(CreateSolidBrush(RGB(nMaskColor.GetRed(), + nMaskColor.GetGreen(), + nMaskColor.GetBlue()))); + HBRUSH hOldBrush = SelectBrush(hDC, hMaskBrush.get()); // WIN/WNT seems to have a minor problem mapping the correct color of the // mask to the palette if we draw the DIB directly ==> draw DDB @@ -802,8 +803,7 @@ void WinSalGraphicsImpl::drawMask( const SalTwoRect& rPosAry, else ImplDrawBitmap( hDC, aPosAry, rSalBitmap, false, 0x00B8074AUL ); - SelectBrush( hDC, hOldBrush ); - DeleteBrush( hMaskBrush ); + SelectBrush(hDC, hOldBrush); } std::shared_ptr WinSalGraphicsImpl::getBitmap( long nX, long nY, long nDX, long nDY ) @@ -1575,16 +1575,18 @@ void WinSalGraphicsImpl::SetROPFillColor( SalROPColor nROPColor ) void WinSalGraphicsImpl::DrawPixelImpl( long nX, long nY, COLORREF crColor ) { - if ( mbXORMode ) + const HDC hDC = mrParent.getHDC(); + + if (!mbXORMode) { - HBRUSH hBrush = CreateSolidBrush( crColor ); - HBRUSH hOldBrush = SelectBrush( mrParent.getHDC(), hBrush ); - PatBlt( mrParent.getHDC(), static_cast(nX), static_cast(nY), int(1), int(1), PATINVERT ); - SelectBrush( mrParent.getHDC(), hOldBrush ); - DeleteBrush( hBrush ); + SetPixel(hDC, static_cast(nX), static_cast(nY), crColor); + return; } - else - SetPixel( mrParent.getHDC(), static_cast(nX), static_cast(nY), crColor ); + + ScopedHBRUSH hBrush(CreateSolidBrush(crColor)); + HBRUSH hOldBrush = SelectBrush(hDC, hBrush.get()); + PatBlt(hDC, static_cast(nX), static_cast(nY), int(1), int(1), PATINVERT); + SelectBrush(hDC, hOldBrush); } void WinSalGraphicsImpl::drawPixel( long nX, long nY ) diff --git a/vcl/win/gdi/scoped_gdi.hxx b/vcl/win/gdi/scoped_gdi.hxx new file mode 100644 index 000000000000..64dbd180b969 --- /dev/null +++ b/vcl/win/gdi/scoped_gdi.hxx @@ -0,0 +1,28 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_VCL_WIN_GDI_SCOPED_GDI_HXX +#define INCLUDED_VCL_WIN_GDI_SCOPED_GDI_HXX + +#include +#include + +#include + +struct HBRUSHDeleter +{ + using pointer = HBRUSH; + void operator()(HBRUSH hBrush) { DeleteBrush(hBrush); } +}; + +using ScopedHBRUSH = std::unique_ptr; + +#endif // INCLUDED_VCL_WIN_GDI_SCOPED_GDI_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit