diff options
Diffstat (limited to 'src/plugins/platforms/windows')
4 files changed, 84 insertions, 53 deletions
diff --git a/src/plugins/platforms/windows/qwindowswindowclassdescription.cpp b/src/plugins/platforms/windows/qwindowswindowclassdescription.cpp index e2e46a7b215..55e36b4587a 100644 --- a/src/plugins/platforms/windows/qwindowswindowclassdescription.cpp +++ b/src/plugins/platforms/windows/qwindowswindowclassdescription.cpp @@ -11,6 +11,55 @@ QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; +QString QWindowsWindowClassDescription::classNameSuffix(Qt::WindowFlags type, unsigned int style, bool hasIcon) +{ + QString suffix; + + switch (type) { + case Qt::Popup: + suffix += "Popup"_L1; + break; + case Qt::Tool: + suffix += "Tool"_L1; + break; + case Qt::ToolTip: + suffix += "ToolTip"_L1; + break; + default: + break; + } + + if (style & CS_DROPSHADOW) + suffix += "DropShadow"_L1; + if (style & CS_SAVEBITS) + suffix += "SaveBits"_L1; + if (style & CS_OWNDC) + suffix += "OwnDC"_L1; + if (hasIcon) + suffix += "Icon"_L1; + + return suffix; +} + +bool QWindowsWindowClassDescription::computeHasIcon(Qt::WindowFlags flags, Qt::WindowFlags type) +{ + bool hasIcon = true; + + switch (type) { + case Qt::Tool: + case Qt::ToolTip: + case Qt::Popup: + hasIcon = false; + break; + case Qt::Dialog: + if (!(flags & Qt::WindowSystemMenuHint)) + hasIcon = false; // QTBUG-2027, dialogs without system menu. + break; + } + + return hasIcon; +} + QWindowsWindowClassDescription QWindowsWindowClassDescription::fromName(QString name, WNDPROC procedure) { return { std::move(name), procedure }; @@ -27,7 +76,7 @@ QWindowsWindowClassDescription QWindowsWindowClassDescription::fromWindow(const const Qt::WindowFlags type = flags & Qt::WindowType_Mask; // Determine style and icon. description.style = CS_DBLCLKS; - description.hasIcon = true; + description.hasIcon = computeHasIcon(flags, type); // The following will not set CS_OWNDC for any widget window, even if it contains a // QOpenGLWidget or QQuickWidget later on. That cannot be detected at this stage. if (window->surfaceType() == QSurface::OpenGLSurface || (flags & Qt::MSWindowsOwnDC)) @@ -41,36 +90,9 @@ QWindowsWindowClassDescription QWindowsWindowClassDescription::fromWindow(const case Qt::ToolTip: case Qt::Popup: description.style |= CS_SAVEBITS; // Save/restore background - description.hasIcon = false; - break; - case Qt::Dialog: - if (!(flags & Qt::WindowSystemMenuHint)) - description.hasIcon = false; // QTBUG-2027, dialogs without system menu. - break; - } - // Create a unique name for the flag combination - description.name = "QWindow"_L1; - switch (type) { - case Qt::Tool: - description.name += "Tool"_L1; - break; - case Qt::ToolTip: - description.name += "ToolTip"_L1; - break; - case Qt::Popup: - description.name += "Popup"_L1; - break; - default: break; } - if (description.style & CS_DROPSHADOW) - description.name += "DropShadow"_L1; - if (description.style & CS_SAVEBITS) - description.name += "SaveBits"_L1; - if (description.style & CS_OWNDC) - description.name += "OwnDC"_L1; - if (description.hasIcon) - description.name += "Icon"_L1; + description.name = "QWindow"_L1 + classNameSuffix(type, description.style, description.hasIcon); return description; } diff --git a/src/plugins/platforms/windows/qwindowswindowclassdescription.h b/src/plugins/platforms/windows/qwindowswindowclassdescription.h index 3acca65b8a2..78d99b4c525 100644 --- a/src/plugins/platforms/windows/qwindowswindowclassdescription.h +++ b/src/plugins/platforms/windows/qwindowswindowclassdescription.h @@ -25,6 +25,9 @@ struct QWindowsWindowClassDescription bool shouldAddPrefix{ true }; private: + static QString classNameSuffix(Qt::WindowFlags type, unsigned int style, bool hasIcon); + static bool computeHasIcon(Qt::WindowFlags flags, Qt::WindowFlags type); + friend QDebug operator<<(QDebug dbg, const QWindowsWindowClassDescription &description); }; diff --git a/src/plugins/platforms/windows/qwindowswindowclassregistry.cpp b/src/plugins/platforms/windows/qwindowswindowclassregistry.cpp index 9c9ffcfeefa..8bf2fb887f1 100644 --- a/src/plugins/platforms/windows/qwindowswindowclassregistry.cpp +++ b/src/plugins/platforms/windows/qwindowswindowclassregistry.cpp @@ -18,8 +18,8 @@ Q_LOGGING_CATEGORY(lcQpaWindowClass, "qt.qpa.windowclass") QWindowsWindowClassRegistry *QWindowsWindowClassRegistry::m_instance = nullptr; -QWindowsWindowClassRegistry::QWindowsWindowClassRegistry(WNDPROC proc) - : m_proc(proc) +QWindowsWindowClassRegistry::QWindowsWindowClassRegistry(WNDPROC defaultProcedure) + : m_defaultProcedure(defaultProcedure) { m_instance = this; } @@ -69,25 +69,19 @@ QString QWindowsWindowClassRegistry::registerWindowClass(const QWindowsWindowCla // add a UUID. The check needs to be performed for each name // in case new message windows are added (QTBUG-81347). // Note: GetClassInfo() returns != 0 when a class exists. - const auto appInstance = static_cast<HINSTANCE>(GetModuleHandle(nullptr)); - WNDCLASS wcinfo; - const bool classExists = GetClassInfo(appInstance, reinterpret_cast<LPCWSTR>(className.utf16()), &wcinfo) != FALSE - && wcinfo.lpfnWndProc != description.procedure; - - if (classExists) + if (shouldDecorateWindowClassName(description)) className += QUuid::createUuid().toString(); if (m_registeredWindowClassNames.contains(className)) // already registered in our list return className; - WNDCLASSEX wc; + const auto appInstance = static_cast<HINSTANCE>(GetModuleHandle(nullptr)); + + WNDCLASSEX wc{}; wc.cbSize = sizeof(WNDCLASSEX); wc.style = description.style; wc.lpfnWndProc = description.procedure; - wc.cbClsExtra = 0; - wc.cbWndExtra = 0; wc.hInstance = appInstance; - wc.hCursor = nullptr; wc.hbrBackground = description.brush; if (description.hasIcon) { wc.hIcon = static_cast<HICON>(LoadImage(appInstance, L"IDI_ICON1", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE)); @@ -98,20 +92,14 @@ QString QWindowsWindowClassRegistry::registerWindowClass(const QWindowsWindowCla } else { wc.hIcon = static_cast<HICON>(LoadImage(nullptr, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED)); - wc.hIconSm = nullptr; } } - else { - wc.hIcon = nullptr; - wc.hIconSm = nullptr; - } - wc.lpszMenuName = nullptr; wc.lpszClassName = reinterpret_cast<LPCWSTR>(className.utf16()); ATOM atom = RegisterClassEx(&wc); if (!atom) - qErrnoWarning("QApplication::regClass: Registering window class '%s' failed.", - qPrintable(className)); + qCWarning(lcQpaWindowClass) << "Failed to register window class" << className + << "(" << qt_error_string(-1) << ")"; m_registeredWindowClassNames.insert(className); @@ -122,7 +110,7 @@ QString QWindowsWindowClassRegistry::registerWindowClass(const QWindowsWindowCla QString QWindowsWindowClassRegistry::registerWindowClass(const QWindow *window) { - return registerWindowClass(QWindowsWindowClassDescription::fromWindow(window, m_proc)); + return registerWindowClass(QWindowsWindowClassDescription::fromWindow(window, m_defaultProcedure)); } QString QWindowsWindowClassRegistry::registerWindowClass(QString name, WNDPROC procedure) @@ -130,13 +118,29 @@ QString QWindowsWindowClassRegistry::registerWindowClass(QString name, WNDPROC p return registerWindowClass(QWindowsWindowClassDescription::fromName(name, procedure)); } +bool QWindowsWindowClassRegistry::shouldDecorateWindowClassName(const QWindowsWindowClassDescription &description) const +{ + return shouldDecorateWindowClassName(description.name, description.procedure); +} + +bool QWindowsWindowClassRegistry::shouldDecorateWindowClassName(const QString &name, WNDPROC procedure) const +{ + const auto appInstance = static_cast<HINSTANCE>(GetModuleHandle(nullptr)); + + WNDCLASS wc{}; + + return GetClassInfo(appInstance, reinterpret_cast<LPCWSTR>(name.utf16()), &wc) != FALSE + && wc.lpfnWndProc != procedure; +} + void QWindowsWindowClassRegistry::unregisterWindowClasses() { const auto appInstance = static_cast<HINSTANCE>(GetModuleHandle(nullptr)); for (const QString &name : std::as_const(m_registeredWindowClassNames)) { if (!UnregisterClass(reinterpret_cast<LPCWSTR>(name.utf16()), appInstance) && QWindowsContext::verbose) - qErrnoWarning("UnregisterClass failed for '%s'", qPrintable(name)); + qCWarning(lcQpaWindowClass) << "Failed to unregister window class" << name + << "(" << qt_error_string(-1) << ")"; } m_registeredWindowClassNames.clear(); } diff --git a/src/plugins/platforms/windows/qwindowswindowclassregistry.h b/src/plugins/platforms/windows/qwindowswindowclassregistry.h index c19b4f616fb..c4a36b7f3c4 100644 --- a/src/plugins/platforms/windows/qwindowswindowclassregistry.h +++ b/src/plugins/platforms/windows/qwindowswindowclassregistry.h @@ -22,7 +22,7 @@ class QWindowsWindowClassRegistry { Q_DISABLE_COPY_MOVE(QWindowsWindowClassRegistry) public: - QWindowsWindowClassRegistry(WNDPROC proc); + QWindowsWindowClassRegistry(WNDPROC defaultProcedure); ~QWindowsWindowClassRegistry(); static QWindowsWindowClassRegistry *instance(); @@ -34,11 +34,13 @@ public: private: static QString classNamePrefix(); + bool shouldDecorateWindowClassName(const QWindowsWindowClassDescription &description) const; + bool shouldDecorateWindowClassName(const QString &name, WNDPROC procedure) const; void unregisterWindowClasses(); static QWindowsWindowClassRegistry *m_instance; - WNDPROC m_proc; + WNDPROC m_defaultProcedure; QSet<QString> m_registeredWindowClassNames; }; |
