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
5#include <QtCore/qdebug.h>
6#include <QtCore/qhash.h>
7
9
10// Theme names matching the QWindowsVistaStylePrivate::Theme enumeration.
11constexpr const wchar_t *themeNames[] = {
12 L"BUTTON", L"COMBOBOX", L"EDIT", L"HEADER", L"LISTVIEW",
13 L"MENU", L"PROGRESS", L"REBAR", L"SCROLLBAR", L"SPIN",
14 L"TAB", L"TASKDIALOG", L"TOOLBAR", L"TOOLTIP", L"TRACKBAR",
15 L"WINDOW", L"STATUS", L"TREEVIEW"
16};
17
21
22QString QWindowsThemeCache::themeName(int theme)
23{
24 return theme >= 0 && theme < int(std::size(themeNames))
25 ? QString::fromWCharArray(themeNames[theme]) : QString();
26}
27
28HTHEME QWindowsThemeCache::createTheme(int theme, HWND hwnd)
29{
30 if (Q_UNLIKELY(theme < 0 || theme >= int(std::size(themeNames)) || !hwnd)) {
31 qWarning("Invalid parameters #%d, %p", theme, hwnd);
32 return nullptr;
33 }
34
35 // Get or create themes array for this window.
36 ThemesCache *cache = themesCache();
37 auto it = cache->find(hwnd);
38 if (it == cache->end())
39 it = cache->insert(hwnd, ThemeArray {});
40
41 // Get or create theme data
42 ThemeArray &themes = *it;
43 if (!themes[theme]) {
44 const wchar_t *name = themeNames[theme];
45 themes[theme] = OpenThemeData(hwnd, name);
46 if (Q_UNLIKELY(!themes[theme]))
47 qErrnoWarning("OpenThemeData() failed for theme %d (%s).",
48 theme, qPrintable(themeName(theme)));
49 }
50 return themes[theme];
51}
52
53static void clearThemes(ThemeArray &themes)
54{
55 for (auto &theme : themes) {
56 if (theme) {
57 CloseThemeData(theme);
58 theme = nullptr;
59 }
60 }
61}
62
64{
65 ThemesCache *cache = themesCache();
66 auto it = cache->find(hwnd);
67 if (it == cache->end())
68 return;
69 clearThemes(*it);
70}
71
73{
74 ThemesCache *cache = themesCache();
75 for (auto &themeArray : *cache)
76 clearThemes(themeArray);
77}
78
79QT_END_NAMESPACE
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[]