DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

commit 378b305c06652f20e840c74a4c387d8df89c4e9b
parent 15bd5e296414d05d85f31894025dd8ed7902dba7
Author: falkTX <falktx@falktx.com>
Date:   Sun, 13 Jun 2021 15:35:23 +0100

macOS file panel dialog

Diffstat:
Mdgl/src/WindowPrivateData.cpp | 25+++++++++++++++++++------
Mdgl/src/WindowPrivateData.hpp | 3+++
Mdgl/src/pugl.cpp | 43+++++++++++++++++++++++++++++++++++++++++++
Mdgl/src/pugl.hpp | 6++++++
4 files changed, 71 insertions(+), 6 deletions(-)

diff --git a/dgl/src/WindowPrivateData.cpp b/dgl/src/WindowPrivateData.cpp @@ -392,7 +392,6 @@ void Window::PrivateData::idleCallback() char* path; if (sofdFileDialogGetPath(&path)) { - // TODO ignore null path?? self->onFileSelected(path); sofdFileDialogFree(path); } @@ -481,7 +480,13 @@ bool Window::PrivateData::openFileBrowser(const Window::FileBrowserOptions& opti // -------------------------------------------------------------------------- // show -#ifdef DISTRHO_OS_WINDOWS +# ifdef DISTRHO_OS_MAC + uint flags = 0x0; + // TODO flags + return puglMacOSFilePanelOpen(view, startDir, title, flags, openPanelCallback); +# endif + +# ifdef DISTRHO_OS_WINDOWS // the old and compatible dialog API OPENFILENAMEW ofn; memset(&ofn, 0, sizeof(ofn)); @@ -528,16 +533,24 @@ bool Window::PrivateData::openFileBrowser(const Window::FileBrowserOptions& opti win32SelectedFile = kWin32SelectedFileCancelled; return true; -#endif -#ifdef HAVE_X11 +# endif + +# ifdef HAVE_X11 uint flags = 0x0; // TODO flags return sofdFileDialogShow(view, startDir, title, flags, options.width, options.height); -#endif +# endif return false; } -#endif + +# ifdef DISTRHO_OS_MAC +void Window::PrivateData::openPanelCallback(PuglView* const view, const char* const path) +{ + ((Window::PrivateData*)puglGetHandle(view))->self->onFileSelected(path); +} +# endif +#endif // ! DGL_FILE_BROWSER_DISABLED // ----------------------------------------------------------------------- // modal handling diff --git a/dgl/src/WindowPrivateData.hpp b/dgl/src/WindowPrivateData.hpp @@ -151,6 +151,9 @@ struct Window::PrivateData : IdleCallback { #ifndef DGL_FILE_BROWSER_DISABLED // file handling bool openFileBrowser(const Window::FileBrowserOptions& options); +# ifdef DISTRHO_OS_MAC + static void openPanelCallback(PuglView* view, const char* path); +# endif #endif // modal handling diff --git a/dgl/src/pugl.cpp b/dgl/src/pugl.cpp @@ -342,6 +342,49 @@ void puglFallbackOnResize(PuglView* const view) #endif } +#ifdef DISTRHO_OS_MAC +// -------------------------------------------------------------------------------------------------------------------- +// macOS specific, setup file browser dialog + +bool puglMacOSFilePanelOpen(PuglView* const view, + const char* const startDir, const char* const title, const uint flags, + openPanelCallback callback) +{ + PuglInternals* impl = view->impl; + + NSOpenPanel* const panel = [NSOpenPanel openPanel]; + + // TODO flags + [panel setCanChooseFiles:YES]; + [panel setCanChooseDirectories:NO]; + [panel setAllowsMultipleSelection:NO]; + + [panel setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:startDir]]]; + + NSString* titleString = [[NSString alloc] + initWithBytes:title + length:strlen(title) + encoding:NSUTF8StringEncoding]; + [panel setTitle:titleString]; + + [panel beginSheetModalForWindow:(impl->window ? impl->window : [view->impl->wrapperView window]) + completionHandler:^(NSInteger result) + { + if (result == NSModalResponseOK && [[panel URL] isFileURL]) + { + NSString* const path = [[panel URL] path]; + callback(view, [path UTF8String]); + } + else + { + callback(view, nullptr); + } + }]; + + return true; +} +#endif + #ifdef DISTRHO_OS_WINDOWS // -------------------------------------------------------------------------------------------------------------------- // win32 specific, call ShowWindow with SW_RESTORE diff --git a/dgl/src/pugl.hpp b/dgl/src/pugl.hpp @@ -82,6 +82,12 @@ puglOnDisplayPrepare(PuglView* view); PUGL_API void puglFallbackOnResize(PuglView* view); +#ifdef DISTRHO_OS_MAC +// macOS specific, setup file browser dialog +typedef void (*openPanelCallback)(PuglView* view, const char* path); +bool puglMacOSFilePanelOpen(PuglView* view, const char* startDir, const char* title, uint flags, openPanelCallback callback); +#endif + #ifdef DISTRHO_OS_WINDOWS // win32 specific, call ShowWindow with SW_RESTORE PUGL_API void