16#include <QtGui/private/qtgui-config_p.h>
17#if QT_CONFIG(directwrite3)
18#include <QtGui/private/qwindowsdirectwritefontdatabase_p.h>
21# include <QtGui/private/qwindowsfontdatabase_ft_p.h>
23#include <QtGui/private/qwindowsfontdatabase_p.h>
24#if QT_CONFIG(clipboard)
25# include "qwindowsclipboard.h"
26# if QT_CONFIG(draganddrop)
27# include "qwindowsdrag.h"
32#if QT_CONFIG(accessibility)
33# include "uiautomation/qwindowsuiaaccessibility.h"
36#include <qpa/qplatformnativeinterface.h>
37#include <qpa/qwindowsysteminterface.h>
38#if QT_CONFIG(sessionmanager)
39# include "qwindowssessionmanager.h"
41#include <QtGui/qpainter.h>
42#include <QtGui/qpen.h>
43#include <QtGui/qpointingdevice.h>
44#include <QtGui/private/qguiapplication_p.h>
45#include <QtGui/private/qhighdpiscaling_p.h>
46#include <QtGui/qpa/qplatforminputcontextfactory_p.h>
47#include <QtGui/qpa/qplatformcursor.h>
49#include <QtGui/private/qwindowsguieventdispatcher_p.h>
51#include <QtCore/qdebug.h>
52#include <QtCore/qvariant.h>
54#include <QtCore/qoperatingsystemversion.h>
55#include <QtCore/private/qfunctions_win_p.h>
56#include <QtCore/private/qcomptr_p.h>
60#if !defined(QT_NO_OPENGL)
66#if QT_CONFIG(cpp_winrt)
67# include <QtCore/private/qt_winrtbase_p.h>
68# include <winrt/Windows.UI.Notifications.h>
69# include <winrt/Windows.Data.Xml.Dom.h>
70# include <winrt/Windows.Foundation.h>
71# include <winrt/Windows.UI.ViewManagement.h>
78 Q_INIT_RESOURCE(openglblacklists);
83using namespace Qt::StringLiterals;
96#if QT_CONFIG(clipboard)
98# if QT_CONFIG(draganddrop)
107#if QT_CONFIG(accessibility)
113template <
typename IntType>
115 IntType minimumValue, IntType maximumValue, IntType *target)
117 const int valueLength = parameter.size() - option.size() - 1;
118 if (valueLength < 1 || !parameter.startsWith(option) || parameter.at(option.size()) != u'=')
121 const auto valueRef = QStringView{parameter}.right(valueLength);
122 const int value = valueRef.toInt(&ok);
124 if (value >=
int(minimumValue) && value <=
int(maximumValue))
125 *target =
static_cast<IntType>(value);
127 qWarning() <<
"Value" << value <<
"for option" << option <<
"out of range"
128 << minimumValue <<
".." << maximumValue;
131 qWarning() <<
"Invalid value" << valueRef <<
"for option" << option;
136using DarkModeHandlingFlag = QNativeInterface::Private::QWindowsApplication::DarkModeHandlingFlag;
137using DarkModeHandling = QNativeInterface::Private::QWindowsApplication::DarkModeHandling;
140 int *tabletAbsoluteRange,
141 QtWindows::DpiAwareness *dpiAwareness,
142 DarkModeHandling *darkModeHandling)
144 unsigned options = 0;
145 for (
const QString ¶m : paramList) {
146 if (param.startsWith(u"fontengine=")) {
147 if (param.endsWith(u"gdi")) {
148 options |= QWindowsIntegration::FontDatabaseGDI;
149 }
else if (param.endsWith(u"freetype")) {
150 options |= QWindowsIntegration::FontDatabaseFreeType;
151 }
else if (param.endsWith(u"native")) {
152 options |= QWindowsIntegration::FontDatabaseNative;
154 }
else if (param.startsWith(u"dialogs=")) {
155 if (param.endsWith(u"xp")) {
156 options |= QWindowsIntegration::XpNativeDialogs;
157 }
else if (param.endsWith(u"none")) {
158 options |= QWindowsIntegration::NoNativeDialogs;
160 }
else if (param == u"altgr") {
161 options |= QWindowsIntegration::DetectAltGrModifier;
162 }
else if (param == u"gl=gdi") {
163 options |= QWindowsIntegration::DisableArb;
164 }
else if (param == u"nodirectwrite") {
165 options |= QWindowsIntegration::DontUseDirectWriteFonts;
166 }
else if (param == u"nocolorfonts") {
167 options |= QWindowsIntegration::DontUseColorFonts;
168 }
else if (param == u"nomousefromtouch") {
169 options |= QWindowsIntegration::DontPassOsMouseEventsSynthesizedFromTouch;
170 }
else if (parseIntOption(param,
"verbose"_L1, 0, INT_MAX, &QWindowsContext::verbose)
171 || parseIntOption(param,
"tabletabsoluterange"_L1, 0, INT_MAX, tabletAbsoluteRange)
172 || parseIntOption(param,
"dpiawareness"_L1, QtWindows::DpiAwareness::Invalid,
173 QtWindows::DpiAwareness::PerMonitorVersion2, dpiAwareness)) {
174 }
else if (param == u"menus=native") {
175 options |= QWindowsIntegration::AlwaysUseNativeMenus;
176 }
else if (param == u"menus=none") {
177 options |= QWindowsIntegration::NoNativeMenus;
178 }
else if (param == u"reverse") {
179 options |= QWindowsIntegration::RtlEnabled;
180 }
else if (param == u"darkmode=0") {
181 *darkModeHandling = {};
182 }
else if (param == u"darkmode=1") {
183 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
184 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle,
false);
185 }
else if (param == u"darkmode=2") {
186 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
187 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle);
189 qWarning() <<
"Unknown option" << param;
199 static bool dpiAwarenessSet =
false;
201 QtWindows::DpiAwareness dpiAwareness = QtWindows::DpiAwareness::PerMonitorVersion2;
203 int tabletAbsoluteRange = -1;
204 DarkModeHandling darkModeHandling = DarkModeHandlingFlag::DarkModeWindowFrames
205 | DarkModeHandlingFlag::DarkModeStyle;
206 m_options = ::parseOptions(paramList, &tabletAbsoluteRange, &dpiAwareness, &darkModeHandling);
207 q->setDarkModeHandling(darkModeHandling);
208 QWindowsFontDatabase::setFontOptions(m_options);
209 if (tabletAbsoluteRange >= 0)
212 QCoreApplication::setAttribute(Qt::AA_CompressHighFrequencyEvents);
213 QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(
false);
215 if (!dpiAwarenessSet) {
216 if (!QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
217 m_context.setProcessDpiAwareness(dpiAwareness);
218 qCDebug(lcQpaWindow) <<
"DpiAwareness=" << dpiAwareness
219 <<
"effective process DPI awareness=" << QWindowsContext::processDpiAwareness();
221 dpiAwarenessSet =
true;
224 m_context.initTouch(m_options);
225 QPlatformCursor::setCapability(QPlatformCursor::OverrideCursor);
227 m_context.initPowerNotificationHandler();
232 delete m_fontDatabase;
241 d->parseOptions(
this, paramList);
242#if QT_CONFIG(clipboard)
243 d->m_clipboard.registerViewer();
245 d->m_context.screenManager().initialize();
246 d->m_context.setDetectAltGrModifier((d->m_options & DetectAltGrModifier) != 0);
251 m_instance =
nullptr;
256 auto icStrs = QPlatformInputContextFactory::requested();
257 icStrs.isEmpty() ? d->m_inputContext.reset(
new QWindowsInputContext)
258 : d->m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
264 case ThreadedPixmaps:
268#if !QT_CONFIG(run_opengl_tests)
281 case MultipleWindows:
287 case AllGLFunctionsQueryable:
289 case SwitchableWidgetComposition:
291 case BackingStoreStaticContents:
293 case MouseCursorPositioning:
296 return QPlatformIntegration::hasCapability(cap);
304 requested.flags = window->flags();
305 requested.geometry = window->isTopLevel()
306 ? QHighDpi::toNativePixels(window->geometry(), window)
307 : QHighDpi::toNativeLocalPosition(window->geometry(), window);
308 if (!(requested.flags & Qt::FramelessWindowHint)) {
310 const QVariant customMarginsV = window->property(
"_q_windowsCustomMargins");
311 if (customMarginsV.isValid())
312 requested.customMargins = qvariant_cast<QMargins>(customMarginsV);
316 QWindowsWindowData::create(window, requested,
317 QWindowsWindow::formatWindowTitle(window->title()));
318 qCDebug(lcQpaWindow).nospace()
319 <<
__FUNCTION__ <<
' ' << window
320 <<
"\n Requested: " << requested.geometry <<
" frame incl.="
321 << QWindowsGeometryHint::positionIncludesFrame(window)
322 <<
' ' << requested.flags
323 <<
"\n Obtained : " << obtained.geometry <<
" margins=" << obtained.fullFrameMargins
324 <<
" handle=" << obtained.hwnd <<
' ' << obtained.flags <<
'\n';
326 if (Q_UNLIKELY(!obtained.hwnd))
329 QWindowsWindow *result = createPlatformWindowHelper(window, obtained);
332 if (window->isTopLevel() && !QWindowsContext::shouldHaveNonClientDpiScaling(window))
335 if (
QWindowsMenuBar *menuBarToBeInstalled = QWindowsMenuBar::menuBarOf(window))
343 const HWND hwnd =
reinterpret_cast<HWND>(nativeHandle);
344 if (!IsWindow(hwnd)) {
345 qWarning(
"Windows QPA: Invalid foreign window ID %p.", hwnd);
349 const QRect obtainedGeometry = result->geometry();
350 QScreen *screen =
nullptr;
351 if (
const QPlatformScreen *pScreen = result->screenForGeometry(obtainedGeometry))
352 screen = pScreen->screen();
353 if (screen && screen != window->screen())
354 window->setScreen(screen);
355 qCDebug(lcQpaWindow) <<
"Foreign window:" << window << Qt::showbase << Qt::hex
356 << result->winId() << Qt::noshowbase << Qt::dec << obtainedGeometry << screen;
363 return new QWindowsWindow(window, data);
370#if defined(QT_OPENGL_DYNAMIC)
371 QWindowsOpenGLTester::Renderer requestedRenderer = QWindowsOpenGLTester::requestedRenderer();
372 switch (requestedRenderer) {
373 case QWindowsOpenGLTester::DesktopGl:
374 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
375 if ((QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DisableRotationFlag)
376 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
377 qCWarning(lcQpaGl,
"Unable to disable rotation.");
381 qCWarning(lcQpaGl,
"System OpenGL failed. Falling back to Software OpenGL.");
382 return QOpenGLStaticContext::create(
true);
383 case QWindowsOpenGLTester::SoftwareRasterizer:
384 if (QWindowsStaticOpenGLContext *swCtx = QOpenGLStaticContext::create(
true))
386 qCWarning(lcQpaGl,
"Software OpenGL failed. Falling back to system OpenGL.");
387 if (QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DesktopGl)
388 return QOpenGLStaticContext::create();
394 const QWindowsOpenGLTester::Renderers supportedRenderers = QWindowsOpenGLTester::supportedRenderers(requestedRenderer);
395 if (supportedRenderers.testFlag(QWindowsOpenGLTester::DisableProgramCacheFlag)
396 && !QCoreApplication::testAttribute(Qt::AA_DisableShaderDiskCache)) {
397 QCoreApplication::setAttribute(Qt::AA_DisableShaderDiskCache);
399 if (supportedRenderers & QWindowsOpenGLTester::DesktopGl) {
400 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
401 if ((supportedRenderers & QWindowsOpenGLTester::DisableRotationFlag)
402 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
403 qCWarning(lcQpaGl,
"Unable to disable rotation.");
408 return QOpenGLStaticContext::create(
true);
421 qCDebug(lcQpaGl) <<
__FUNCTION__ << context->format();
423 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(context));
424 if (result->isValid())
425 return result.release();
432#if !defined(QT_OPENGL_DYNAMIC)
433 return QOpenGLContext::LibGL;
435 if (
const QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext())
436 return staticOpenGLContext->moduleType();
437 return QOpenGLContext::LibGL;
455 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(ctx, window));
456 if (result->isValid()) {
457 auto *context =
new QOpenGLContext;
458 context->setShareContext(shareContext);
459 auto *contextPrivate = QOpenGLContextPrivate::get(context);
460 contextPrivate->adopt(result.release());
474 QMutexLocker lock(&d->m_staticContextLock);
475 if (d->m_staticOpenGLContext.isNull())
476 d->m_staticOpenGLContext.reset(QWindowsStaticOpenGLContext::create());
477 return d->m_staticOpenGLContext.data();
483 if (!d->m_fontDatabase) {
484#ifndef QT_NO_FREETYPE
485 if (d->m_options & QWindowsIntegration::FontDatabaseFreeType)
486 d->m_fontDatabase =
new QWindowsFontDatabaseFT;
489#if QT_CONFIG(directwrite3)
490 if (!(d->m_options & (QWindowsIntegration::FontDatabaseGDI | QWindowsIntegration::DontUseDirectWriteFonts)))
491 d->m_fontDatabase =
new QWindowsDirectWriteFontDatabase;
494 d->m_fontDatabase =
new QWindowsFontDatabase;
496 return d->m_fontDatabase;
499#ifdef SPI_GETKEYBOARDSPEED
500static inline int keyBoardAutoRepeatRateMS()
503 if (SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &time, 0))
504 return time ? 1000 /
static_cast<
int>(time) : 500;
512 case QPlatformIntegration::CursorFlashTime:
513 if (
const unsigned timeMS = GetCaretBlinkTime())
514 return QVariant(timeMS != INFINITE ?
int(timeMS) * 2 : 0);
516#ifdef SPI_GETKEYBOARDSPEED
517 case KeyboardAutoRepeatRate:
518 return QVariant(keyBoardAutoRepeatRateMS());
520 case QPlatformIntegration::ShowIsMaximized:
521 case QPlatformIntegration::StartDragTime:
522 case QPlatformIntegration::StartDragDistance:
523 case QPlatformIntegration::KeyboardInputInterval:
524 case QPlatformIntegration::ShowIsFullScreen:
525 case QPlatformIntegration::PasswordMaskDelay:
526 case QPlatformIntegration::StartDragVelocity:
528 case QPlatformIntegration::FontSmoothingGamma:
529 return QVariant(QWindowsFontDatabase::fontSmoothingGamma());
530 case QPlatformIntegration::MouseDoubleClickInterval:
531 if (
const UINT ms = GetDoubleClickTime())
532 return QVariant(
int(ms));
534 case QPlatformIntegration::UseRtlExtensions:
535 return QVariant(d->m_context.useRTLExtensions());
539 return QPlatformIntegration::styleHint(hint);
544 return d->m_context.keyMapper();
547#if QT_CONFIG(clipboard)
548QPlatformClipboard * QWindowsIntegration::clipboard()
const
550 return &d->m_clipboard;
552# if QT_CONFIG(draganddrop)
553QPlatformDrag *QWindowsIntegration::drag()
const
562 return d->m_inputContext.data();
565#if QT_CONFIG(accessibility)
566QPlatformAccessibility *QWindowsIntegration::accessibility()
const
568 return &d->m_accessibility;
577#if QT_CONFIG(sessionmanager)
578QPlatformSessionManager *QWindowsIntegration::createPlatformSessionManager(
const QString &id,
const QString &key)
const
580 return new QWindowsSessionManager(id, key);
586 return new QWindowsGuiEventDispatcher;
598 return QPlatformIntegration::createPlatformTheme(name);
603 if (d->m_services.isNull())
604 d->m_services.reset(
new QWindowsServices);
606 return d->m_services.data();
617 number = qMax(0, number);
620 m_applicationBadgeNumber = number;
622 static const bool isWindows11 = QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11;
624#if QT_CONFIG(cpp_winrt)
629 if (isWindows11 && qt_win_hasPackageIdentity()) {
630 using namespace winrt::Windows::UI::Notifications;
631 auto badgeXml = BadgeUpdateManager::GetTemplateContent(BadgeTemplateType::BadgeNumber);
632 badgeXml.SelectSingleNode(L"//badge/@value").NodeValue(winrt::box_value(winrt::to_hstring(number)));
633 BadgeUpdateManager::CreateBadgeUpdaterForApplication().Update(BadgeNotification(badgeXml));
645 setApplicationBadge(QImage());
650 == Qt::ColorScheme::Dark;
655#if QT_CONFIG(cpp_winrt)
658 static const auto fromUIColor = [](winrt::Windows::UI::Color &&color) {
659 return QColor(color.R, color.G, color.B, color.A);
661 using namespace winrt::Windows::UI::ViewManagement;
662 const auto settings = UISettings();
663 badgeColor = fromUIColor(settings.GetColorValue(isDarkMode ?
664 UIColorType::AccentLight2 : UIColorType::Accent));
665 textColor = fromUIColor(settings.GetColorValue(UIColorType::Background));
669 if (!badgeColor.isValid()) {
671 badgeColor = isDarkMode ? Qt::black : QColor(220, 220, 220);
672 badgeColor.setAlphaF(0.5f);
673 textColor = isDarkMode ? Qt::white : Qt::black;
676 const auto devicePixelRatio = qApp->devicePixelRatio();
678 static const QSize iconBaseSize(16, 16);
679 QImage image(iconBaseSize * devicePixelRatio,
680 QImage::Format_ARGB32_Premultiplied);
681 image.fill(Qt::transparent);
683 QPainter painter(&image);
685 QRect badgeRect = image.rect();
686 QPen badgeBorderPen = Qt::NoPen;
688 QColor badgeBorderColor = textColor;
689 badgeBorderColor.setAlphaF(0.5f);
690 badgeBorderPen = badgeBorderColor;
691 badgeRect.adjust(1, 1, -1, -1);
693 painter.setBrush(badgeColor);
694 painter.setPen(badgeBorderPen);
695 painter.setRenderHint(QPainter::Antialiasing);
696 painter.drawEllipse(badgeRect);
698 auto pixelSize = qCeil(10.5 * devicePixelRatio);
701 const bool textOverflow = number > 99;
705 QFont font = painter.font();
706 font.setPixelSize(pixelSize);
707 font.setWeight(isWindows11 ? QFont::Medium : QFont::DemiBold);
708 painter.setFont(font);
710 painter.setRenderHint(QPainter::TextAntialiasing, devicePixelRatio > 1);
711 painter.setPen(textColor);
713 auto text = textOverflow ? u"99+"_s : QString::number(number);
714 painter.translate(textOverflow ? 1 : 0, textOverflow ? 0 : -1);
715 painter.drawText(image.rect(), Qt::AlignCenter, text);
719 setApplicationBadge(image);
724 QComHelper comHelper;
726 ComPtr<ITaskbarList3> taskbarList;
727 CoCreateInstance(CLSID_TaskbarList,
nullptr,
728 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&taskbarList));
736 const auto hIcon = image.toHICON();
741 const auto topLevelWindows = QGuiApplication::topLevelWindows();
742 for (
auto *topLevelWindow : topLevelWindows) {
743 if (!topLevelWindow->handle())
745 auto hwnd =
reinterpret_cast<HWND>(topLevelWindow->winId());
746 taskbarList->SetOverlayIcon(hwnd, hIcon, L"");
763 if (m_applicationBadgeNumber)
764 setApplicationBadge(m_applicationBadgeNumber);
768QPlatformVulkanInstance *QWindowsIntegration::createPlatformVulkanInstance(QVulkanInstance *instance)
const
770 return new QWindowsVulkanInstance(instance);
Static Open GL context containing version information, extension function pointers,...
static QOpenGLStaticContext * create(bool softwareRendering=false)
Singleton container for all relevant information.
static void setTabletAbsoluteRange(int a)
Window wrapping a foreign native window.
QStringList themeNames() const override
QPlatformKeyMapper * keyMapper() const override
Accessor for the platform integration's key mapper.
static QWindowsStaticOpenGLContext * staticOpenGLContext()
void setApplicationBadge(const QImage &image)
~QWindowsIntegration() override
QOpenGLContext * createOpenGLContext(HGLRC context, HWND window, QOpenGLContext *shareContext) const override
QWindowsIntegration(const QStringList ¶mList)
void updateApplicationBadge()
QAbstractEventDispatcher * createEventDispatcher() const override
Factory function for the GUI event dispatcher.
QPlatformTheme * createPlatformTheme(const QString &name) const override
bool hasCapability(QPlatformIntegration::Capability cap) const override
QOpenGLContext::OpenGLModuleType openGLModuleType() override
Platform integration function for querying the OpenGL implementation type.
QPlatformFontDatabase * fontDatabase() const override
Accessor for the platform integration's fontdatabase.
void initialize() override
Performs initialization steps that depend on having an event dispatcher available.
void setApplicationBadge(qint64 number) override
static QWindowsIntegration * instance()
HMODULE openGLModuleHandle() const override
void beep() const override
QPlatformInputContext * inputContext() const override
Returns the platforms input context.
QPlatformServices * services() const override
QVariant styleHint(StyleHint hint) const override
QPlatformOpenGLContext * createPlatformOpenGLContext(QOpenGLContext *context) const override
Factory function for QPlatformOpenGLContext.
virtual void * moduleHandle() const =0
virtual bool supportsThreadedOpenGL() const
static QWindowsTheme * instance()
void setFlag(unsigned f) const
@ DisableNonClientScaling
QWindowsIntegrationPrivate()=default
QWindowsContext m_context
QScopedPointer< QWindowsStaticOpenGLContext > m_staticOpenGLContext
void parseOptions(QWindowsIntegration *q, const QStringList ¶mList)
~QWindowsIntegrationPrivate()
QScopedPointer< QWindowsServices > m_services
QMutex m_staticContextLock
QPlatformFontDatabase * m_fontDatabase
QScopedPointer< QPlatformInputContext > m_inputContext