From f771926ed10f4467be5eb2301792a9819ab0d8c7 Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Fri, 24 Nov 2023 22:01:02 -0500 Subject: [PATCH] Update file dialog for windows --- src/app/core/platform/file_dialog.h | 50 ++++++++++++++++------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/app/core/platform/file_dialog.h b/src/app/core/platform/file_dialog.h index 6576f2f3..d6437a22 100644 --- a/src/app/core/platform/file_dialog.h +++ b/src/app/core/platform/file_dialog.h @@ -5,34 +5,40 @@ #include #include -std::string ShowOpenFileDialog() { - CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); +class FileDialogWrapper { + public: + static std::string ShowOpenFileDialog() { + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); - IFileDialog *pfd = NULL; - HRESULT hr = - CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog, - reinterpret_cast(&pfd)); - - if (SUCCEEDED(hr)) { - // Show the dialog - hr = pfd->Show(NULL); + IFileDialog *pfd = NULL; + HRESULT hr = + CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, + IID_IFileDialog, reinterpret_cast(&pfd)); + std::string file_path_windows; if (SUCCEEDED(hr)) { - IShellItem *psiResult; - hr = pfd->GetResult(&psiResult); + // Show the dialog + hr = pfd->Show(NULL); if (SUCCEEDED(hr)) { - // Get the file path - PWSTR pszFilePath; - psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); - psiResult->Release(); - CoTaskMemFree(pszFilePath); + IShellItem *psiResult; + hr = pfd->GetResult(&psiResult); + if (SUCCEEDED(hr)) { + // Get the file path + PWSTR pszFilePath; + psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); + char str[128]; + wcstombs(str, pszFilePath, 128); + file_path_windows = str; + psiResult->Release(); + CoTaskMemFree(pszFilePath); + } } + pfd->Release(); } - pfd->Release(); - } - CoUninitialize(); - return L""; // Return an empty string if no file was selected or an error -} + CoUninitialize(); + return file_path_windows; + } +}; #elif defined(__APPLE__)