From 1e69d4e3ac4263776ae13095dec213b24c51e22f Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Date: Wed, 6 Apr 2016 09:41:54 +0200
Subject: [PATCH] Refactor QWindowsXPStylePrivate::drawBackground() and
 helpers.

Pass the DC obtained drawBackground() to drawBackgroundDirectly(),
saving a call to hdcForWidgetBackingStore() to find it.
Pass the error code from the Win32 API DrawThemeBackground()
to the calling functions.

Change-Id: I1c25241f53d87e4429ad924f7b2e29da215d7e4f
Reviewed-by: Oliver Wolff <oliver.wolff@theqtcompany.com>
---
 src/widgets/styles/qwindowsxpstyle.cpp   | 14 ++++++--------
 src/widgets/styles/qwindowsxpstyle_p_p.h |  2 +-
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/widgets/styles/qwindowsxpstyle.cpp b/src/widgets/styles/qwindowsxpstyle.cpp
index 88b1b41f151..2c98ebb740a 100644
--- a/src/widgets/styles/qwindowsxpstyle.cpp
+++ b/src/widgets/styles/qwindowsxpstyle.cpp
@@ -774,7 +774,7 @@ bool QWindowsXPStylePrivate::drawBackground(XPThemeData &themeData)
 
     const HDC dc = canDrawDirectly ? hdcForWidgetBackingStore(themeData.widget) : HDC(0);
     const bool result = dc
-        ? drawBackgroundDirectly(themeData, qRound(aditionalDevicePixelRatio))
+        ? drawBackgroundDirectly(dc, themeData, qRound(aditionalDevicePixelRatio))
         : drawBackgroundThruNativeBuffer(themeData, qRound(aditionalDevicePixelRatio));
     painter->restore();
     return result;
@@ -804,12 +804,9 @@ static QRegion scaleRegion(const QRegion &region, int factor)
     Do not use this if you need to perform other transformations on the
     resulting data.
 */
-bool QWindowsXPStylePrivate::drawBackgroundDirectly(XPThemeData &themeData, int additionalDevicePixelRatio)
+bool QWindowsXPStylePrivate::drawBackgroundDirectly(HDC dc, XPThemeData &themeData, int additionalDevicePixelRatio)
 {
     QPainter *painter = themeData.painter;
-    HDC dc = 0;
-    if (themeData.widget)
-        dc = hdcForWidgetBackingStore(themeData.widget);
 
     QPoint redirectionDelta(int(painter->deviceMatrix().dx()),
                             int(painter->deviceMatrix().dy()));
@@ -841,8 +838,9 @@ bool QWindowsXPStylePrivate::drawBackgroundDirectly(XPThemeData &themeData, int
                           | (themeData.noContent ? DTBG_OMITCONTENT : 0)
                           | (themeData.mirrorHorizontally ? DTBG_MIRRORDC : 0);
 
+    HRESULT result = S_FALSE;
     if (pDrawThemeBackgroundEx != 0) {
-        pDrawThemeBackgroundEx(themeData.handle(), dc, themeData.partId, themeData.stateId, &(drawRECT), &drawOptions);
+        result = pDrawThemeBackgroundEx(themeData.handle(), dc, themeData.partId, themeData.stateId, &(drawRECT), &drawOptions);
     } else {
         // We are running on a system where the uxtheme.dll does not have
         // the DrawThemeBackgroundEx function, so we need to clip away
@@ -876,11 +874,11 @@ bool QWindowsXPStylePrivate::drawBackgroundDirectly(XPThemeData &themeData, int
             }
         }
 
-        pDrawThemeBackground(themeData.handle(), dc, themeData.partId, themeData.stateId, &(drawRECT), &(drawOptions.rcClip));
+        result = pDrawThemeBackground(themeData.handle(), dc, themeData.partId, themeData.stateId, &(drawRECT), &(drawOptions.rcClip));
     }
     SelectClipRgn(dc, 0);
     DeleteObject(hrgn);
-    return true;
+    return SUCCEEDED(result);
 }
 
 /*! \internal
diff --git a/src/widgets/styles/qwindowsxpstyle_p_p.h b/src/widgets/styles/qwindowsxpstyle_p_p.h
index 240a338c3fe..d75c064369b 100644
--- a/src/widgets/styles/qwindowsxpstyle_p_p.h
+++ b/src/widgets/styles/qwindowsxpstyle_p_p.h
@@ -388,7 +388,7 @@ public:
     void setTransparency(QWidget *widget, XPThemeData &themeData);
     bool drawBackground(XPThemeData &themeData);
     bool drawBackgroundThruNativeBuffer(XPThemeData &themeData, int aditionalDevicePixelRatio);
-    bool drawBackgroundDirectly(XPThemeData &themeData, int aditionalDevicePixelRatio);
+    bool drawBackgroundDirectly(HDC dc, XPThemeData &themeData, int aditionalDevicePixelRatio);
 
     bool hasAlphaChannel(const QRect &rect);
     bool fixAlphaChannel(const QRect &rect);
-- 
GitLab