Repository navigation
Expand file tree
/
Copy pathvcf_theme.cpp
More file actions
234 lines (218 loc) · 8.6 KB
/
Copy pathvcf_theme.cpp
File metadata and controls
234 lines (218 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "vcf_theme.hpp"
#include <algorithm>
#include <windows.h>
// =============================================================
// Theme: TC dark mode (cm_SwitchDarkMode) preferred over OS
// =============================================================
std::wstring g_iniPath;
bool g_dark = false;
bool g_tcRu = false;
int g_tcDarkMode = -1; // -1 unknown, 0 light, 1 dark (from ListLoad/ListSendCommand flags)
HBRUSH g_hbrBk = nullptr;
HBRUSH g_hbrListBg = nullptr;
COLORREF g_clrBk, g_clrTxt, g_clrSub, g_clrGrid, g_clrSeparator;
COLORREF g_clrListBg, g_clrListSel;
void SafeDelBrush(HBRUSH& b) { if (b) { DeleteObject(b); b = nullptr; } }
std::wstring GetPluginDirectory() {
HMODULE hMod = nullptr;
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)&GetPluginDirectory, &hMod) || !hMod) {
return L"";
}
wchar_t path[MAX_PATH]{};
DWORD len = GetModuleFileNameW(hMod, path, MAX_PATH);
if (len == 0 || len >= MAX_PATH) return L"";
wchar_t* slash = wcsrchr(path, L'\\');
if (!slash) slash = wcsrchr(path, L'/');
if (!slash) return L"";
*slash = L'\0';
return path;
}
std::wstring ResolvePluginIni() {
std::wstring dir = GetPluginDirectory();
if (dir.empty()) return L"";
std::wstring cand1 = dir + L"\\VCFLister.ini";
if (GetFileAttributesW(cand1.c_str()) != INVALID_FILE_ATTRIBUTES)
return cand1;
std::wstring cand2 = dir + L"\\VcfLister.ini";
if (GetFileAttributesW(cand2.c_str()) != INVALID_FILE_ATTRIBUTES)
return cand2;
return L"";
}
std::wstring ResolveWincmdIni() {
wchar_t out[MAX_PATH]{};
if (!g_iniPath.empty()) {
wcsncpy_s(out, g_iniPath.c_str(), _TRUNCATE);
wchar_t* slash = wcsrchr(out, L'\\');
if (!slash) slash = wcsrchr(out, L'/');
if (slash) {
wcsncpy_s(slash + 1, MAX_PATH - (size_t)(slash + 1 - out), L"wincmd.ini", _TRUNCATE);
if (GetFileAttributesW(out) != INVALID_FILE_ATTRIBUTES) return out;
wcsncpy_s(slash + 1, MAX_PATH - (size_t)(slash + 1 - out), L"WINCMD.INI", _TRUNCATE);
if (GetFileAttributesW(out) != INVALID_FILE_ATTRIBUTES) return out;
}
}
wchar_t appdata[MAX_PATH]{};
if (GetEnvironmentVariableW(L"APPDATA", appdata, MAX_PATH) > 0) {
swprintf_s(out, L"%s\\GHISLER\\wincmd.ini", appdata);
if (GetFileAttributesW(out) != INVALID_FILE_ATTRIBUTES) return out;
swprintf_s(out, L"%s\\GHISLER\\WINCMD.INI", appdata);
if (GetFileAttributesW(out) != INVALID_FILE_ATTRIBUTES) return out;
}
return g_iniPath;
}
static bool ReadRegDWORD(HKEY root, const wchar_t* subkey, const wchar_t* name, DWORD& out) {
HKEY h; if (RegOpenKeyExW(root, subkey, 0, KEY_READ, &h) != ERROR_SUCCESS) return false;
DWORD type = 0, size = sizeof(DWORD);
LONG r = RegGetValueW(h, nullptr, name, RRF_RT_REG_DWORD, &type, &out, &size);
RegCloseKey(h); return r == ERROR_SUCCESS;
}
static bool DetectSystemDark() {
DWORD v = 1; // 1=Light, 0=Dark
if (ReadRegDWORD(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", L"AppsUseLightTheme", v))
return v == 0;
return false;
}
// TC stores panel colors; dark mode typically uses dark BackColor.
// Used when ShowFlags not yet known (Dark=2 auto).
static bool DetectTCDarkFromIniColors() {
std::wstring wincmd = ResolveWincmdIni();
if (wincmd.empty()) return false;
wchar_t buf[64]{};
// Prefer Lister colors if set; fall back to file panel BackColor
GetPrivateProfileStringW(L"Lister", L"BackColor", L"", buf, 64, wincmd.c_str());
if (!buf[0])
GetPrivateProfileStringW(L"Colors", L"BackColor", L"", buf, 64, wincmd.c_str());
if (!buf[0]) return false;
// Formats: "R,G,B" or single COLORREF decimal
int r = 0, g = 0, b = 0;
if (swscanf_s(buf, L"%d,%d,%d", &r, &g, &b) == 3) {
// luminance
return (0.299 * r + 0.587 * g + 0.114 * b) < 128.0;
}
long v = wcstol(buf, nullptr, 10);
if (v != 0 || buf[0] == L'0') {
r = GetRValue((COLORREF)v);
g = GetGValue((COLORREF)v);
b = GetBValue((COLORREF)v);
return (0.299 * r + 0.587 * g + 0.114 * b) < 128.0;
}
return false;
}
static int ReadIniDarkMode() {
wchar_t buf[16]{};
std::wstring pluginIni = ResolvePluginIni();
if (!pluginIni.empty()) {
GetPrivateProfileStringW(L"VCFLister", L"Dark", L"", buf, 16, pluginIni.c_str());
if (buf[0] == L'0') return 0;
if (buf[0] == L'1') return 1;
if (buf[0] == L'2') return 2;
}
if (!g_iniPath.empty()) {
GetPrivateProfileStringW(L"VCFLister", L"Dark", L"2", buf, 16, g_iniPath.c_str());
if (buf[0] == L'0') return 0;
if (buf[0] == L'1') return 1;
return 2; // auto → TC dark mode
}
return 2;
}
static bool DetectTCRussian() {
wchar_t buf[MAX_PATH]{};
std::wstring pluginIni = ResolvePluginIni();
// 1) Explicit setting: [VCFLister] Language=auto|RUS|ENG
if (!pluginIni.empty()) {
GetPrivateProfileStringW(L"VCFLister", L"Language", L"", buf, MAX_PATH, pluginIni.c_str());
}
if (!buf[0] && !g_iniPath.empty()) {
GetPrivateProfileStringW(L"VCFLister", L"Language", L"", buf, MAX_PATH, g_iniPath.c_str());
}
if (buf[0]) {
std::wstring s = buf;
std::wstring low; low.resize(s.size());
std::transform(s.begin(), s.end(), low.begin(), ::towlower);
if (low.find(L"rus") != std::wstring::npos || low.find(L"russian") != std::wstring::npos) return true;
if (low.find(L"eng") != std::wstring::npos || low.find(L"english") != std::wstring::npos) return false;
}
// 2) TC configuration from wincmd.ini
std::wstring wincmd = ResolveWincmdIni();
if (!wincmd.empty()) {
buf[0] = 0;
GetPrivateProfileStringW(L"Configuration", L"LanguageIni", L"", buf, MAX_PATH, wincmd.c_str());
if (!buf[0])
GetPrivateProfileStringW(L"Configuration", L"LanguageDll", L"", buf, MAX_PATH, wincmd.c_str());
if (buf[0]) {
std::wstring s = buf;
std::wstring low; low.resize(s.size());
std::transform(s.begin(), s.end(), low.begin(), ::towlower);
if (low.find(L"rus") != std::wstring::npos || low.find(L"russian") != std::wstring::npos)
return true;
return false;
}
}
return PRIMARYLANGID(GetUserDefaultUILanguage()) == LANG_RUSSIAN;
}
void RecomputeTheme() {
const int ini = ReadIniDarkMode();
g_tcRu = DetectTCRussian();
if (ini == 0) {
g_dark = false;
} else if (ini == 1) {
g_dark = true;
} else {
// Auto: follow Total Commander dark mode (cm_SwitchDarkMode), NOT OS
if (g_tcDarkMode == 0) g_dark = false;
else if (g_tcDarkMode == 1) g_dark = true;
else if (DetectTCDarkFromIniColors()) g_dark = true;
else g_dark = false; // default light when unknown (do not use Windows theme)
}
if (g_dark) {
g_clrBk = RGB(32, 32, 32);
g_clrTxt = RGB(220, 220, 220);
g_clrSub = RGB(160, 160, 160);
g_clrGrid = RGB(64, 64, 64);
g_clrSeparator = RGB(70, 70, 70);
g_clrListBg = RGB(28, 28, 28);
g_clrListSel = RGB(60, 80, 120);
}
else {
g_clrBk = RGB(255, 255, 255);
g_clrTxt = RGB(30, 30, 30);
g_clrSub = RGB(110, 110, 110);
g_clrGrid = RGB(220, 220, 220);
g_clrSeparator = RGB(200, 200, 200);
g_clrListBg = RGB(248, 248, 248);
g_clrListSel = RGB(219, 234, 254);
}
SafeDelBrush(g_hbrBk);
g_hbrBk = CreateSolidBrush(g_clrBk);
SafeDelBrush(g_hbrListBg);
g_hbrListBg = CreateSolidBrush(g_clrListBg);
}
extern "C" void VCFView_SetIniPath(const wchar_t* iniPath) {
if (iniPath && *iniPath) g_iniPath = iniPath;
RecomputeTheme();
}
extern "C" void VCFView_SetTCDarkMode(int dark /*0/1*/, HWND hWnd) {
g_tcDarkMode = (dark != 0) ? 1 : 0;
RecomputeTheme();
if (hWnd && IsWindow(hWnd)) {
VCFView_ApplyChrome(hWnd);
InvalidateRect(hWnd, nullptr, TRUE);
// repaint children (filter, edit, checkbox)
EnumChildWindows(hWnd, [](HWND c, LPARAM) -> BOOL {
InvalidateRect(c, nullptr, TRUE);
return TRUE;
}, 0);
}
}
extern "C" void VCFView_RefreshTheme(HWND hWnd) {
RecomputeTheme();
if (IsWindow(hWnd)) {
VCFView_ApplyChrome(hWnd);
InvalidateRect(hWnd, nullptr, TRUE);
EnumChildWindows(hWnd, [](HWND c, LPARAM) -> BOOL {
InvalidateRect(c, nullptr, TRUE);
return TRUE;
}, 0);
}
}