Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindowsthemecache.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include <QtCore/qdebug.h>
7#include <QtCore/qhash.h>
8
10
11// Theme names matching the QWindowsVistaStylePrivate::Theme enumeration.
12constexpr const wchar_t *themeNames[] = {
13 L"BUTTON", L"COMBOBOX", L"EDIT", L"HEADER", L"LISTVIEW",
14 L"MENU", L"PROGRESS", L"REBAR", L"SCROLLBAR", L"SPIN",
15 L"TAB", L"TASKDIALOG", L"TOOLBAR", L"TOOLTIP", L"TRACKBAR",
16 L"WINDOW", L"STATUS", L"TREEVIEW"
17};
18
22
23QString QWindowsThemeCache::themeName(int theme)
24{
25 return theme >= 0 && theme < int(std::size(themeNames))
26 ? QString::fromWCharArray(themeNames[theme]) : QString();
27}
28
29HTHEME QWindowsThemeCache::createTheme(int theme, HWND hwnd)
30{
31 if (Q_UNLIKELY(theme < 0 || theme >= int(std::size(themeNames)) || !hwnd)) {
32 qWarning("Invalid parameters #%d, %p", theme, hwnd);
33 return nullptr;
34 }
35
36 // Get or create themes array for this window.
37 ThemesCache *cache = themesCache();
38 auto it = cache->find(hwnd);
39 if (it == cache->end())
40 it = cache->insert(hwnd, ThemeArray {});
41
42 // Get or create theme data
43 ThemeArray &themes = *it;
44 if (!themes[theme]) {
45 const wchar_t *name = themeNames[theme];
46 themes[theme] = OpenThemeData(hwnd, name);
47 if (Q_UNLIKELY(!themes[theme]))
48 qErrnoWarning("OpenThemeData() failed for theme %d (%s).",
49 theme, qPrintable(themeName(theme)));
50 }
51 return themes[theme];
52}
53
54static void clearThemes(ThemeArray &themes)
55{
56 for (auto &theme : themes) {
57 if (theme) {
58 CloseThemeData(theme);
59 theme = nullptr;
60 }
61 }
62}
63
65{
66 ThemesCache *cache = themesCache();
67 auto it = cache->find(hwnd);
68 if (it == cache->end())
69 return;
70 clearThemes(*it);
71}
72
74{
75 ThemesCache *cache = themesCache();
76 for (auto &themeArray : *cache)
77 clearThemes(themeArray);
78}
79
80QT_END_NAMESPACE
Combined button and popup list for selecting options.
Q_GUI_EXPORT void clearAllThemeCaches()
Q_GUI_EXPORT void clearThemeCache(HWND hwnd)
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
std::array< HTHEME, std::size(themeNames)> ThemeArray
static void clearThemes(ThemeArray &themes)
QHash< HWND, ThemeArray > ThemesCache
QT_BEGIN_NAMESPACE constexpr const wchar_t * themeNames[]