From 11b6af94ff4ca66bb80295702c55a0be0baf189e Mon Sep 17 00:00:00 2001 From: scawful Date: Fri, 24 Nov 2023 13:33:46 -0500 Subject: [PATCH] Add macOS default file loader, windows WIP --- src/app/core/platform/file_dialog.h | 58 ++++++++++++++++++++++++++++ src/app/core/platform/file_dialog.mm | 17 ++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/app/core/platform/file_dialog.h create mode 100644 src/app/core/platform/file_dialog.mm diff --git a/src/app/core/platform/file_dialog.h b/src/app/core/platform/file_dialog.h new file mode 100644 index 00000000..645ecced --- /dev/null +++ b/src/app/core/platform/file_dialog.h @@ -0,0 +1,58 @@ +#include + +#ifdef _WIN32 +// Include Windows-specific headers +#include +#include + +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); + if (SUCCEEDED(hr)) { + IShellItem *psiResult; + hr = pfd->GetResult(&psiResult); + if (SUCCEEDED(hr)) { + // Get the file path + PWSTR pszFilePath; + psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); + psiResult->Release(); + CoTaskMemFree(pszFilePath); + } + } + pfd->Release(); + } + + CoUninitialize(); + return L""; // Return an empty string if no file was selected or an error +} + +#elif defined(__APPLE__) + +#include + +class FileDialogWrapper { + public: + static std::string ShowOpenFileDialog(); +}; + + +#elif defined(__linux__) +#include + +std::string ShowOpenFileDialog() { + // Linux-specific file dialog implementation using GTK + // ... + return "file_path_linux"; +} + +#else +#error "Unsupported platform." +#endif \ No newline at end of file diff --git a/src/app/core/platform/file_dialog.mm b/src/app/core/platform/file_dialog.mm new file mode 100644 index 00000000..91c3581d --- /dev/null +++ b/src/app/core/platform/file_dialog.mm @@ -0,0 +1,17 @@ +#import +#include "app/core/platform/file_dialog.h" + +std::string FileDialogWrapper::ShowOpenFileDialog() { + NSOpenPanel* openPanel = [NSOpenPanel openPanel]; + [openPanel setCanChooseFiles:YES]; + [openPanel setCanChooseDirectories:NO]; + [openPanel setAllowsMultipleSelection:NO]; + + if ([openPanel runModal] == NSModalResponseOK) { + NSURL* url = [[openPanel URLs] objectAtIndex:0]; + NSString* path = [url path]; + return std::string([path UTF8String]); + } + + return ""; +}