Browse Source

macOS file panel dialog

pull/292/head
falkTX 4 years ago
parent
commit
378b305c06
4 changed files with 71 additions and 6 deletions
  1. +19
    -6
      dgl/src/WindowPrivateData.cpp
  2. +3
    -0
      dgl/src/WindowPrivateData.hpp
  3. +43
    -0
      dgl/src/pugl.cpp
  4. +6
    -0
      dgl/src/pugl.hpp

+ 19
- 6
dgl/src/WindowPrivateData.cpp View File

@@ -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


+ 3
- 0
dgl/src/WindowPrivateData.hpp View File

@@ -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


+ 43
- 0
dgl/src/pugl.cpp View File

@@ -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


+ 6
- 0
dgl/src/pugl.hpp View File

@@ -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


Loading…
Cancel
Save