forked from texus/TGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDialogIconLoaderWindows.cpp
More file actions
483 lines (390 loc) · 18.3 KB
/
FileDialogIconLoaderWindows.cpp
File metadata and controls
483 lines (390 loc) · 18.3 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus' Graphical User Interface
// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <TGUI/FileDialogIconLoader.hpp>
#if defined(TGUI_SYSTEM_WINDOWS)
#include <TGUI/extlibs/IncludeWindows.hpp>
#if __has_include(<shellapi.h>)
#define TGUI_SHELL_API_HEADER_INCLUDED
#include <shellapi.h>
// MinGW.org based TDM-GCC doesn't define SHGFI_ADDOVERLAYS
#ifndef SHGFI_ADDOVERLAYS
#define SHGFI_ADDOVERLAYS 0x000000020
#endif
#endif
#if !defined(TGUI_SHELL_API_HEADER_INCLUDED)
// We define the required contents of shellapi.h ourselves if the Windows API can't be found
#ifndef SHSTDAPI_
#if !defined(_SHELL32_)
#define SHSTDAPI_(type) extern "C" DECLSPEC_IMPORT type STDAPICALLTYPE
#else
#define SHSTDAPI_(type) STDAPI_(type)
#endif
#endif
#define SHGFI_ICON 0x000000100 // get icon
#define SHGFI_LARGEICON 0x000000000 // get large icon
#define SHGFI_USEFILEATTRIBUTES 0x000000010 // use passed dwFileAttribute
#define SHGFI_ADDOVERLAYS 0x000000020 // apply the appropriate overlays
typedef struct _SHFILEINFOW
{
HICON hIcon;
int iIcon;
DWORD dwAttributes;
WCHAR szDisplayName[MAX_PATH];
WCHAR szTypeName[80];
} SHFILEINFOW;
SHSTDAPI_(DWORD_PTR)
SHGetFileInfoW(_In_ LPCWSTR pszPath,
DWORD dwFileAttributes,
_Inout_updates_bytes_opt_(cbFileInfo) SHFILEINFOW* psfi,
UINT cbFileInfo,
UINT uFlags);
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class FileDialogIconLoaderWindows : public FileDialogIconLoader
{
public:
FileDialogIconLoaderWindows();
~FileDialogIconLoaderWindows() override;
[[nodiscard]] bool update() override;
[[nodiscard]] bool supportsSystemIcons() const override;
[[nodiscard]] bool hasGenericIcons() const override;
[[nodiscard]] Texture getGenericFileIcon(const Filesystem::FileInfo& file) override;
void requestFileIcons(const std::vector<Filesystem::FileInfo>&) override;
[[nodiscard]] std::vector<Texture> retrieveFileIcons() override;
private:
struct IconData
{
std::unique_ptr<std::uint8_t[]> pixels = nullptr;
unsigned int width = 0;
unsigned int height = 0;
};
struct ThreadData
{
decltype(&SHGetFileInfoW) dllGetFileInfoFuncHandle = nullptr;
std::vector<Filesystem::FileInfo> files; // List of files for which the thread should load the icons
std::vector<IconData> icons; // List of loaded icons
HANDLE cancelEvent = nullptr;
HANDLE finishedEvent = nullptr;
};
[[nodiscard]] static IconData loadIconPixels(decltype(&SHGetFileInfoW) dllGetFileInfoFuncHandle,
const String& filename,
bool isDirectory);
[[nodiscard]] static Texture loadIcon(decltype(&SHGetFileInfoW) dllGetFileInfoFuncHandle, const String& filename, bool isDirectory);
static DWORD WINAPI loadIconsThread(void* parameter);
private:
HMODULE m_dllModuleHandle = nullptr;
decltype(&SHGetFileInfoW) m_dllGetFileInfoFuncHandle = nullptr;
Optional<Texture> m_defaultFileIcon;
Optional<Texture> m_defaultFolderIcon;
bool m_threadStarted = false; // Is the thread potentially running?
HANDLE m_thread = nullptr;
HANDLE m_cancelEvent = nullptr;
HANDLE m_finishedEvent = nullptr;
ThreadData m_threadData;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<FileDialogIconLoader> FileDialogIconLoader::createInstance()
{
return std::make_shared<FileDialogIconLoaderWindows>();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialogIconLoaderWindows::FileDialogIconLoaderWindows()
{
m_dllModuleHandle = LoadLibraryW(L"shell32.dll");
if (m_dllModuleHandle)
{
#if defined(__clang__)
#pragma clang diagnostic push
#if defined(__clang_major__) && (__clang_major__ >= 19)
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
#endif
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
m_dllGetFileInfoFuncHandle = reinterpret_cast<decltype(&SHGetFileInfoW)>(
GetProcAddress(m_dllModuleHandle, "SHGetFileInfoW"));
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
if (!m_dllGetFileInfoFuncHandle)
return;
m_cancelEvent = CreateEventW(nullptr, true, false, nullptr);
m_finishedEvent = CreateEventW(nullptr, true, false, nullptr);
m_threadData.dllGetFileInfoFuncHandle = m_dllGetFileInfoFuncHandle;
m_threadData.cancelEvent = m_cancelEvent;
m_threadData.finishedEvent = m_finishedEvent;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialogIconLoaderWindows::~FileDialogIconLoaderWindows()
{
// If the thread is still running then stop it
if (m_threadStarted)
SignalObjectAndWait(m_cancelEvent, m_thread, INFINITE, false);
if (m_finishedEvent)
CloseHandle(m_finishedEvent);
if (m_cancelEvent)
CloseHandle(m_cancelEvent);
if (m_dllModuleHandle)
FreeLibrary(m_dllModuleHandle);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Texture FileDialogIconLoaderWindows::getGenericFileIcon(const Filesystem::FileInfo& file)
{
if (file.directory)
{
if (!m_defaultFolderIcon)
m_defaultFolderIcon = loadIcon(m_dllGetFileInfoFuncHandle, "DefaultFolderIcon", true); // The string passed to this function can be anything
return *m_defaultFolderIcon;
}
else
{
if (!m_defaultFileIcon)
m_defaultFileIcon = loadIcon(m_dllGetFileInfoFuncHandle, "DefaultFileIcon", false); // The string passed to this function can be anything
return *m_defaultFileIcon;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialogIconLoaderWindows::requestFileIcons(const std::vector<Filesystem::FileInfo>& files)
{
if (!m_dllGetFileInfoFuncHandle || !m_cancelEvent || !m_finishedEvent)
return;
if (m_threadStarted)
{
TGUI_ASSERT(m_thread != nullptr, "Thead handle has to be valid if thread is running");
// Abort the old thread that was still running.
// We don't check the return type, if it differs from WAIT_OBJECT_0 then we have a big problem
SignalObjectAndWait(m_cancelEvent, m_thread, INFINITE, false);
ResetEvent(m_cancelEvent);
}
ResetEvent(m_finishedEvent);
m_threadData.icons.clear();
m_threadData.files = files;
m_threadStarted = true;
m_thread = CreateThread(nullptr, 0, &loadIconsThread, &m_threadData, 0, nullptr);
if (!m_thread)
{
m_threadStarted = false;
m_threadData.files.clear();
return;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialogIconLoaderWindows::update()
{
if (!m_threadStarted)
return false;
return WaitForSingleObject(m_finishedEvent, 0) == WAIT_OBJECT_0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialogIconLoaderWindows::supportsSystemIcons() const
{
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialogIconLoaderWindows::hasGenericIcons() const
{
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<Texture> FileDialogIconLoaderWindows::retrieveFileIcons()
{
std::vector<Texture> icons;
if (!m_threadStarted)
return icons;
if (WaitForSingleObject(m_finishedEvent, 0) != WAIT_OBJECT_0)
return icons;
for (const auto& iconData : m_threadData.icons)
{
// The texture is always added, even if loadFromPixelData fails
auto& texture = icons.emplace_back();
if (iconData.pixels)
texture.loadFromPixelData({iconData.width, iconData.height}, iconData.pixels.get());
}
m_threadStarted = false;
m_threadData.files.clear();
return icons;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialogIconLoaderWindows::IconData FileDialogIconLoaderWindows::loadIconPixels(
decltype(&SHGetFileInfoW) dllGetFileInfoFuncHandle,
const String& filename,
bool isDirectory)
{
IconData iconData;
if (!dllGetFileInfoFuncHandle)
return iconData;
SHFILEINFOW shellFileInfo;
std::memset(&shellFileInfo, 0, sizeof(SHFILEINFOW));
const DWORD fileAttributes = isDirectory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
const UINT flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_ADDOVERLAYS | SHGFI_LARGEICON;
if (dllGetFileInfoFuncHandle(filename.toWideString().c_str(), fileAttributes, &shellFileInfo, sizeof(shellFileInfo), flags)
== 0)
return iconData;
if (!shellFileInfo.hIcon)
return iconData;
ICONINFO iconInfo;
if (!GetIconInfo(shellFileInfo.hIcon, &iconInfo))
{
DestroyIcon(shellFileInfo.hIcon);
return iconData;
}
HDC hDC = nullptr;
auto releaseResources = [&]
{
if (iconInfo.hbmColor)
DeleteObject(iconInfo.hbmColor);
if (iconInfo.hbmMask)
DeleteObject(iconInfo.hbmMask);
if (hDC)
DeleteDC(hDC);
DestroyIcon(shellFileInfo.hIcon);
};
BITMAP bitmap;
if (GetObjectW(iconInfo.hbmColor, sizeof(BITMAP), &bitmap) == 0)
{
releaseResources();
return iconData;
}
const DWORD iconWidth = static_cast<DWORD>(bitmap.bmWidth);
const DWORD iconHeight = static_cast<DWORD>(bitmap.bmHeight);
hDC = CreateCompatibleDC(nullptr);
if (!hDC)
{
releaseResources();
return iconData;
}
BITMAPINFO infoheader;
infoheader.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
infoheader.bmiHeader.biWidth = static_cast<LONG>(iconWidth);
infoheader.bmiHeader.biHeight = static_cast<LONG>(iconHeight);
infoheader.bmiHeader.biPlanes = 1;
infoheader.bmiHeader.biCompression = BI_RGB;
if (bitmap.bmBitsPixel == 32)
{
const DWORD bitmapSize = iconWidth * iconHeight * 4;
infoheader.bmiHeader.biBitCount = 32;
infoheader.bmiHeader.biSizeImage = bitmapSize;
// Allocate memory for the icon BGRA bitmap
auto buffer = MakeUniqueForOverwrite<std::uint8_t[]>(2 * static_cast<std::size_t>(bitmapSize));
std::uint8_t* pixelsBGRA = &buffer[0];
if (!GetDIBits(hDC, iconInfo.hbmColor, 0, iconHeight, static_cast<void*>(pixelsBGRA), &infoheader, DIB_RGB_COLORS))
{
releaseResources();
return iconData;
}
iconData.pixels = MakeUniqueForOverwrite<std::uint8_t[]>(static_cast<std::size_t>(iconWidth) * iconHeight * 4);
iconData.width = iconWidth;
iconData.height = iconHeight;
std::uint8_t* imagePixels = iconData.pixels.get();
for (DWORD y = 0; y < iconHeight; ++y)
{
for (DWORD x = 0; x < iconWidth; ++x)
{
// Source pixels are stored in BGRA with the origin at the bottom left of the bitmap
const DWORD srcPos = ((iconHeight - 1 - y) * (iconWidth * 4)) + (x * 4);
const DWORD destPos = (y * iconWidth * 4) + (x * 4);
imagePixels[destPos] = pixelsBGRA[srcPos + 2]; // R
imagePixels[destPos + 1] = pixelsBGRA[srcPos + 1]; // G
imagePixels[destPos + 2] = pixelsBGRA[srcPos]; // B
imagePixels[destPos + 3] = pixelsBGRA[srcPos + 3]; // A
}
}
}
else // Icon bitmap doesn't contain an alpha channel
{
// Each scanline consists of 3 bytes per pixel, rounded upwards to multiple of 4
const DWORD bytesPerScanLine = (iconWidth * 3 + 3) & 0xFFFFFFFC;
const DWORD bitmapSize = bytesPerScanLine * iconHeight;
infoheader.bmiHeader.biBitCount = 24;
infoheader.bmiHeader.biSizeImage = bitmapSize;
// Allocate memory for 2 bitmaps: the icon BGR bitmap and the icon mask bitmap
auto buffer = MakeUniqueForOverwrite<std::uint8_t[]>(2 * static_cast<std::size_t>(bitmapSize));
std::uint8_t* pixelsBGR = &buffer[0];
std::uint8_t* pixelsAlpha = &buffer[bitmapSize];
if (!GetDIBits(hDC, iconInfo.hbmColor, 0, iconHeight, static_cast<void*>(pixelsBGR), &infoheader, DIB_RGB_COLORS))
{
releaseResources();
return iconData;
}
if (!GetDIBits(hDC, iconInfo.hbmMask, 0, iconHeight, static_cast<void*>(pixelsAlpha), &infoheader, DIB_RGB_COLORS))
{
releaseResources();
return iconData;
}
iconData.pixels = MakeUniqueForOverwrite<std::uint8_t[]>(static_cast<std::size_t>(iconWidth) * iconHeight * 4);
iconData.width = iconWidth;
iconData.height = iconHeight;
std::uint8_t* imagePixels = iconData.pixels.get();
for (DWORD y = 0; y < iconHeight; ++y)
{
for (DWORD x = 0; x < iconWidth; ++x)
{
// Source pixels are stored in BGR with the origin at the bottom left of the bitmap
const DWORD srcPos = ((iconHeight - 1 - y) * (iconWidth * 3)) + (x * 3);
const DWORD destPos = (y * iconWidth * 4) + (x * 4);
imagePixels[destPos] = pixelsBGR[srcPos + 2]; // R
imagePixels[destPos + 1] = pixelsBGR[srcPos + 1]; // G
imagePixels[destPos + 2] = pixelsBGR[srcPos]; // B
imagePixels[destPos + 3] = (pixelsAlpha[srcPos] ? 0 : 255); // A
}
}
}
releaseResources();
return iconData;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Texture FileDialogIconLoaderWindows::loadIcon(decltype(&SHGetFileInfoW) dllGetFileInfoFuncHandle, const String& filename, bool isDirectory)
{
const auto iconData = loadIconPixels(dllGetFileInfoFuncHandle, filename, isDirectory);
Texture texture;
if (iconData.pixels)
texture.loadFromPixelData({iconData.width, iconData.height},
iconData.pixels.get()); // Don't care if this fails or succeeds, we return the texture anyway
return texture;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DWORD WINAPI FileDialogIconLoaderWindows::loadIconsThread(void* parameter)
{
TGUI_ASSERT(parameter != nullptr, "Thread parameter can't be a nullptr");
auto threadData = static_cast<ThreadData*>(parameter);
threadData->icons.clear();
threadData->icons.reserve(threadData->files.size());
for (const auto& file : threadData->files)
{
if (WaitForSingleObject(threadData->cancelEvent, 0) == WAIT_OBJECT_0)
return 0; // Abort execution as the user no longer needs the icons that we were loading
threadData->icons.push_back(loadIconPixels(threadData->dllGetFileInfoFuncHandle, file.filename, file.directory));
}
SetEvent(threadData->finishedEvent);
return 0;
}
} // namespace tgui
#endif // TGUI_SYSTEM_WINDOWS