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/qpointingdevice.h>
42#include <QtGui/private/qguiapplication_p.h>
43#include <QtGui/private/qhighdpiscaling_p.h>
44#include <QtGui/qpa/qplatforminputcontextfactory_p.h>
45#include <QtGui/qpa/qplatformcursor.h>
47#include <QtGui/private/qwindowsguieventdispatcher_p.h>
49#include <QtCore/qdebug.h>
50#include <QtCore/qvariant.h>
52#include <QtCore/qoperatingsystemversion.h>
53#include <QtCore/private/qfunctions_win_p.h>
54#include <QtCore/private/qcomptr_p.h>
58#if !defined(QT_NO_OPENGL)
64#if QT_CONFIG(cpp_winrt)
65# include <QtCore/private/qt_winrtbase_p.h>
66# include <winrt/Windows.UI.Notifications.h>
67# include <winrt/Windows.Data.Xml.Dom.h>
68# include <winrt/Windows.Foundation.h>
69# include <winrt/Windows.UI.ViewManagement.h>
76 Q_INIT_RESOURCE(openglblacklists);
81using namespace Qt::StringLiterals;
94#if QT_CONFIG(clipboard)
96# if QT_CONFIG(draganddrop)
105#if QT_CONFIG(accessibility)
111template <
typename IntType>
113 IntType minimumValue, IntType maximumValue, IntType *target)
115 const int valueLength = parameter.size() - option.size() - 1;
116 if (valueLength < 1 || !parameter.startsWith(option) || parameter.at(option.size()) != u'=')
119 const auto valueRef = QStringView{parameter}.right(valueLength);
120 const int value = valueRef.toInt(&ok);
122 if (value >=
int(minimumValue) && value <=
int(maximumValue))
123 *target =
static_cast<IntType>(value);
125 qWarning() <<
"Value" << value <<
"for option" << option <<
"out of range"
126 << minimumValue <<
".." << maximumValue;
129 qWarning() <<
"Invalid value" << valueRef <<
"for option" << option;
134using DarkModeHandlingFlag = QNativeInterface::Private::QWindowsApplication::DarkModeHandlingFlag;
135using DarkModeHandling = QNativeInterface::Private::QWindowsApplication::DarkModeHandling;
138 int *tabletAbsoluteRange,
139 QtWindows::DpiAwareness *dpiAwareness,
140 DarkModeHandling *darkModeHandling)
142 unsigned options = 0;
143 for (
const QString ¶m : paramList) {
144 if (param.startsWith(u"fontengine=")) {
145 if (param.endsWith(u"gdi")) {
146 options |= QWindowsIntegration::FontDatabaseGDI;
147 }
else if (param.endsWith(u"freetype")) {
148 options |= QWindowsIntegration::FontDatabaseFreeType;
149 }
else if (param.endsWith(u"native")) {
150 options |= QWindowsIntegration::FontDatabaseNative;
152 }
else if (param.startsWith(u"dialogs=")) {
153 if (param.endsWith(u"xp")) {
154 options |= QWindowsIntegration::XpNativeDialogs;
155 }
else if (param.endsWith(u"none")) {
156 options |= QWindowsIntegration::NoNativeDialogs;
158 }
else if (param == u"altgr") {
159 options |= QWindowsIntegration::DetectAltGrModifier;
160 }
else if (param == u"gl=gdi") {
161 options |= QWindowsIntegration::DisableArb;
162 }
else if (param == u"nodirectwrite") {
163 options |= QWindowsIntegration::DontUseDirectWriteFonts;
164 }
else if (param == u"nocolorfonts") {
165 options |= QWindowsIntegration::DontUseColorFonts;
166 }
else if (param == u"nomousefromtouch") {
167 options |= QWindowsIntegration::DontPassOsMouseEventsSynthesizedFromTouch;
168 }
else if (parseIntOption(param,
"verbose"_L1, 0, INT_MAX, &QWindowsContext::verbose)
169 || parseIntOption(param,
"tabletabsoluterange"_L1, 0, INT_MAX, tabletAbsoluteRange)
170 || parseIntOption(param,
"dpiawareness"_L1, QtWindows::DpiAwareness::Invalid,
171 QtWindows::DpiAwareness::PerMonitorVersion2, dpiAwareness)) {
172 }
else if (param == u"menus=native") {
173 options |= QWindowsIntegration::AlwaysUseNativeMenus;
174 }
else if (param == u"menus=none") {
175 options |= QWindowsIntegration::NoNativeMenus;
176 }
else if (param == u"reverse") {
177 options |= QWindowsIntegration::RtlEnabled;
178 }
else if (param == u"darkmode=0") {
179 *darkModeHandling = {};
180 }
else if (param == u"darkmode=1") {
181 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
182 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle,
false);
183 }
else if (param == u"darkmode=2") {
184 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
185 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle);
187 qWarning() <<
"Unknown option" << param;
197 static bool dpiAwarenessSet =
false;
199 QtWindows::DpiAwareness dpiAwareness = QtWindows::DpiAwareness::PerMonitorVersion2;
201 int tabletAbsoluteRange = -1;
202 DarkModeHandling darkModeHandling = DarkModeHandlingFlag::DarkModeWindowFrames
203 | DarkModeHandlingFlag::DarkModeStyle;
204 m_options = ::parseOptions(paramList, &tabletAbsoluteRange, &dpiAwareness, &darkModeHandling);
205 q->setDarkModeHandling(darkModeHandling);
206 QWindowsFontDatabase::setFontOptions(m_options);
207 if (tabletAbsoluteRange >= 0)
210 QCoreApplication::setAttribute(Qt::AA_CompressHighFrequencyEvents);
211 QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(
false);
213 if (!dpiAwarenessSet) {
214 if (!QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
215 m_context.setProcessDpiAwareness(dpiAwareness);
216 qCDebug(lcQpaWindow) <<
"DpiAwareness=" << dpiAwareness
217 <<
"effective process DPI awareness=" << QWindowsContext::processDpiAwareness();
219 dpiAwarenessSet =
true;
222 m_context.initTouch(m_options);
223 QPlatformCursor::setCapability(QPlatformCursor::OverrideCursor);
225 m_context.initPowerNotificationHandler();
230 delete m_fontDatabase;
239 d->parseOptions(
this, paramList);
240#if QT_CONFIG(clipboard)
241 d->m_clipboard.registerViewer();
243 d->m_context.screenManager().initialize();
244 d->m_context.setDetectAltGrModifier((d->m_options & DetectAltGrModifier) != 0);
249 m_instance =
nullptr;
254 auto icStrs = QPlatformInputContextFactory::requested();
255 icStrs.isEmpty() ? d->m_inputContext.reset(
new QWindowsInputContext)
256 : d->m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
262 case ThreadedPixmaps:
266#if !QT_CONFIG(run_opengl_tests)
279 case MultipleWindows:
283 case AllGLFunctionsQueryable:
285 case SwitchableWidgetComposition:
287 case BackingStoreStaticContents:
290 return QPlatformIntegration::hasCapability(cap);
298 requested.flags = window->flags();
299 requested.geometry = window->isTopLevel()
300 ? QHighDpi::toNativePixels(window->geometry(), window)
301 : QHighDpi::toNativeLocalPosition(window->geometry(), window);
302 if (!(requested.flags & Qt::FramelessWindowHint)) {
304 const QVariant customMarginsV = window->property(
"_q_windowsCustomMargins");
305 if (customMarginsV.isValid())
306 requested.customMargins = qvariant_cast<QMargins>(customMarginsV);
310 QWindowsWindowData::create(window, requested,
311 QWindowsWindow::formatWindowTitle(window->title()));
312 qCDebug(lcQpaWindow).nospace()
313 <<
__FUNCTION__ <<
' ' << window
314 <<
"\n Requested: " << requested.geometry <<
" frame incl.="
315 << QWindowsGeometryHint::positionIncludesFrame(window)
316 <<
' ' << requested.flags
317 <<
"\n Obtained : " << obtained.geometry <<
" margins=" << obtained.fullFrameMargins
318 <<
" handle=" << obtained.hwnd <<
' ' << obtained.flags <<
'\n';
320 if (Q_UNLIKELY(!obtained.hwnd))
323 QWindowsWindow *result = createPlatformWindowHelper(window, obtained);
326 if (window->isTopLevel() && !QWindowsContext::shouldHaveNonClientDpiScaling(window))
329 if (
QWindowsMenuBar *menuBarToBeInstalled = QWindowsMenuBar::menuBarOf(window))
337 const HWND hwnd =
reinterpret_cast<HWND>(nativeHandle);
338 if (!IsWindow(hwnd)) {
339 qWarning(
"Windows QPA: Invalid foreign window ID %p.", hwnd);
343 const QRect obtainedGeometry = result->geometry();
344 QScreen *screen =
nullptr;
345 if (
const QPlatformScreen *pScreen = result->screenForGeometry(obtainedGeometry))
346 screen = pScreen->screen();
347 if (screen && screen != window->screen())
348 window->setScreen(screen);
349 qCDebug(lcQpaWindow) <<
"Foreign window:" << window << Qt::showbase << Qt::hex
350 << result->winId() << Qt::noshowbase << Qt::dec << obtainedGeometry << screen;
357 return new QWindowsWindow(window, data);
364#if defined(QT_OPENGL_DYNAMIC)
365 QWindowsOpenGLTester::Renderer requestedRenderer = QWindowsOpenGLTester::requestedRenderer();
366 switch (requestedRenderer) {
367 case QWindowsOpenGLTester::DesktopGl:
368 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
369 if ((QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DisableRotationFlag)
370 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
371 qCWarning(lcQpaGl,
"Unable to disable rotation.");
375 qCWarning(lcQpaGl,
"System OpenGL failed. Falling back to Software OpenGL.");
376 return QOpenGLStaticContext::create(
true);
377 case QWindowsOpenGLTester::SoftwareRasterizer:
378 if (QWindowsStaticOpenGLContext *swCtx = QOpenGLStaticContext::create(
true))
380 qCWarning(lcQpaGl,
"Software OpenGL failed. Falling back to system OpenGL.");
381 if (QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DesktopGl)
382 return QOpenGLStaticContext::create();
388 const QWindowsOpenGLTester::Renderers supportedRenderers = QWindowsOpenGLTester::supportedRenderers(requestedRenderer);
389 if (supportedRenderers.testFlag(QWindowsOpenGLTester::DisableProgramCacheFlag)
390 && !QCoreApplication::testAttribute(Qt::AA_DisableShaderDiskCache)) {
391 QCoreApplication::setAttribute(Qt::AA_DisableShaderDiskCache);
393 if (supportedRenderers & QWindowsOpenGLTester::DesktopGl) {
394 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
395 if ((supportedRenderers & QWindowsOpenGLTester::DisableRotationFlag)
396 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
397 qCWarning(lcQpaGl,
"Unable to disable rotation.");
402 return QOpenGLStaticContext::create(
true);
415 qCDebug(lcQpaGl) <<
__FUNCTION__ << context->format();
417 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(context));
418 if (result->isValid())
419 return result.release();
426#if !defined(QT_OPENGL_DYNAMIC)
427 return QOpenGLContext::LibGL;
429 if (
const QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext())
430 return staticOpenGLContext->moduleType();
431 return QOpenGLContext::LibGL;
449 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(ctx, window));
450 if (result->isValid()) {
451 auto *context =
new QOpenGLContext;
452 context->setShareContext(shareContext);
453 auto *contextPrivate = QOpenGLContextPrivate::get(context);
454 contextPrivate->adopt(result.release());
468 QMutexLocker lock(&d->m_staticContextLock);
469 if (d->m_staticOpenGLContext.isNull())
470 d->m_staticOpenGLContext.reset(QWindowsStaticOpenGLContext::create());
471 return d->m_staticOpenGLContext.data();
477 if (!d->m_fontDatabase) {
478#ifndef QT_NO_FREETYPE
479 if (d->m_options & QWindowsIntegration::FontDatabaseFreeType)
480 d->m_fontDatabase =
new QWindowsFontDatabaseFT;
483#if QT_CONFIG(directwrite3)
484 if (!(d->m_options & (QWindowsIntegration::FontDatabaseGDI | QWindowsIntegration::DontUseDirectWriteFonts)))
485 d->m_fontDatabase =
new QWindowsDirectWriteFontDatabase;
488 d->m_fontDatabase =
new QWindowsFontDatabase;
490 return d->m_fontDatabase;
493#ifdef SPI_GETKEYBOARDSPEED
494static inline int keyBoardAutoRepeatRateMS()
497 if (SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &time, 0))
498 return time ? 1000 /
static_cast<
int>(time) : 500;
506 case QPlatformIntegration::CursorFlashTime:
507 if (
const unsigned timeMS = GetCaretBlinkTime())
508 return QVariant(timeMS != INFINITE ?
int(timeMS) * 2 : 0);
510#ifdef SPI_GETKEYBOARDSPEED
511 case KeyboardAutoRepeatRate:
512 return QVariant(keyBoardAutoRepeatRateMS());
514 case QPlatformIntegration::ShowIsMaximized:
515 case QPlatformIntegration::StartDragTime:
516 case QPlatformIntegration::StartDragDistance:
517 case QPlatformIntegration::KeyboardInputInterval:
518 case QPlatformIntegration::ShowIsFullScreen:
519 case QPlatformIntegration::PasswordMaskDelay:
520 case QPlatformIntegration::StartDragVelocity:
522 case QPlatformIntegration::FontSmoothingGamma:
523 return QVariant(QWindowsFontDatabase::fontSmoothingGamma());
524 case QPlatformIntegration::MouseDoubleClickInterval:
525 if (
const UINT ms = GetDoubleClickTime())
526 return QVariant(
int(ms));
528 case QPlatformIntegration::UseRtlExtensions:
529 return QVariant(d->m_context.useRTLExtensions());
533 return QPlatformIntegration::styleHint(hint);
538 return d->m_context.keyMapper();
541#if QT_CONFIG(clipboard)
542QPlatformClipboard * QWindowsIntegration::clipboard()
const
544 return &d->m_clipboard;
546# if QT_CONFIG(draganddrop)
547QPlatformDrag *QWindowsIntegration::drag()
const
556 return d->m_inputContext.data();
559#if QT_CONFIG(accessibility)
560QPlatformAccessibility *QWindowsIntegration::accessibility()
const
562 return &d->m_accessibility;
571#if QT_CONFIG(sessionmanager)
572QPlatformSessionManager *QWindowsIntegration::createPlatformSessionManager(
const QString &id,
const QString &key)
const
574 return new QWindowsSessionManager(id, key);
580 return new QWindowsGuiEventDispatcher;
592 return QPlatformIntegration::createPlatformTheme(name);
597 if (d->m_services.isNull())
598 d->m_services.reset(
new QWindowsServices);
600 return d->m_services.data();
611 number = qMax(0, number);
614 m_applicationBadgeNumber = number;
616 static const bool isWindows11 = QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11;
618#if QT_CONFIG(cpp_winrt)
623 if (isWindows11 && qt_win_hasPackageIdentity()) {
624 using namespace winrt::Windows::UI::Notifications;
625 auto badgeXml = BadgeUpdateManager::GetTemplateContent(BadgeTemplateType::BadgeNumber);
626 badgeXml.SelectSingleNode(L"//badge/@value").NodeValue(winrt::box_value(winrt::to_hstring(number)));
627 BadgeUpdateManager::CreateBadgeUpdaterForApplication().Update(BadgeNotification(badgeXml));
639 setApplicationBadge(QImage());
643 const bool isDarkMode = QWindowsTheme::instance()->colorScheme()
644 == Qt::ColorScheme::Dark;
649#if QT_CONFIG(cpp_winrt)
652 static const auto fromUIColor = [](winrt::Windows::UI::Color &&color) {
653 return QColor(color.R, color.G, color.B, color.A);
655 using namespace winrt::Windows::UI::ViewManagement;
656 const auto settings = UISettings();
657 badgeColor = fromUIColor(settings.GetColorValue(isDarkMode ?
658 UIColorType::AccentLight2 : UIColorType::Accent));
659 textColor = fromUIColor(settings.GetColorValue(UIColorType::Background));
663 if (!badgeColor.isValid()) {
665 badgeColor = isDarkMode ? Qt::black : QColor(220, 220, 220);
666 badgeColor.setAlphaF(0.5f);
667 textColor = isDarkMode ? Qt::white : Qt::black;
670 const auto devicePixelRatio = qApp->devicePixelRatio();
672 static const QSize iconBaseSize(16, 16);
673 QImage image(iconBaseSize * devicePixelRatio,
674 QImage::Format_ARGB32_Premultiplied);
675 image.fill(Qt::transparent);
677 QPainter painter(&image);
679 QRect badgeRect = image.rect();
680 QPen badgeBorderPen = Qt::NoPen;
682 QColor badgeBorderColor = textColor;
683 badgeBorderColor.setAlphaF(0.5f);
684 badgeBorderPen = badgeBorderColor;
685 badgeRect.adjust(1, 1, -1, -1);
687 painter.setBrush(badgeColor);
688 painter.setPen(badgeBorderPen);
689 painter.setRenderHint(QPainter::Antialiasing);
690 painter.drawEllipse(badgeRect);
692 auto pixelSize = qCeil(10.5 * devicePixelRatio);
695 const bool textOverflow = number > 99;
699 QFont font = painter.font();
700 font.setPixelSize(pixelSize);
701 font.setWeight(isWindows11 ? QFont::Medium : QFont::DemiBold);
702 painter.setFont(font);
704 painter.setRenderHint(QPainter::TextAntialiasing, devicePixelRatio > 1);
705 painter.setPen(textColor);
707 auto text = textOverflow ? u"99+"_s : QString::number(number);
708 painter.translate(textOverflow ? 1 : 0, textOverflow ? 0 : -1);
709 painter.drawText(image.rect(), Qt::AlignCenter, text);
713 setApplicationBadge(image);
718 QComHelper comHelper;
720 ComPtr<ITaskbarList3> taskbarList;
721 CoCreateInstance(CLSID_TaskbarList,
nullptr,
722 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&taskbarList));
730 const auto hIcon = image.toHICON();
735 const auto topLevelWindows = QGuiApplication::topLevelWindows();
736 for (
auto *topLevelWindow : topLevelWindows) {
737 if (!topLevelWindow->handle())
739 auto hwnd =
reinterpret_cast<HWND>(topLevelWindow->winId());
740 taskbarList->SetOverlayIcon(hwnd, hIcon, L"");
757 if (m_applicationBadgeNumber)
758 setApplicationBadge(m_applicationBadgeNumber);
762QPlatformVulkanInstance *QWindowsIntegration::createPlatformVulkanInstance(QVulkanInstance *instance)
const
764 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
void setFlag(unsigned f) const
@ DisableNonClientScaling
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