Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
18typedef std::array<HTHEME, std::size(themeNames)> ThemeArray;
19typedef QHash<HWND, ThemeArray> ThemesCache;
21
23{
24 return theme >= 0 && theme < int(std::size(themeNames))
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;
70}
71
73{
74 ThemesCache *cache = themesCache();
75 for (auto &themeArray : *cache)
76 clearThemes(themeArray);
77}
78
\inmodule QtCore
Definition qhash.h:820
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromWCharArray(const wchar_t *string, qsizetype size=-1)
Definition qstring.h:1309
QCache< int, Employee > cache
[0]
QSet< QString >::iterator it
void qErrnoWarning(const char *msg,...)
Combined button and popup list for selecting options.
Q_GUI_EXPORT void clearAllThemeCaches()
Q_GUI_EXPORT void clearThemeCache(HWND hwnd)
Q_GUI_EXPORT HTHEME createTheme(int theme, HWND hwnd)
Q_GUI_EXPORT QString themeName(int theme)
#define Q_UNLIKELY(x)
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define qWarning
Definition qlogging.h:166
static QString themeName()
GLuint name
#define qPrintable(string)
Definition qstring.h:1531
QT_BEGIN_NAMESPACE constexpr const wchar_t * themeNames[]
static void clearThemes(ThemeArray &themes)
QHash< HWND, ThemeArray > ThemesCache
std::array< HTHEME, std::size(themeNames)> ThemeArray