From 3d3678c500d44bf3223060ff741e0b1fe129d51f Mon Sep 17 00:00:00 2001 From: falkTX Date: Fri, 12 Nov 2021 11:56:03 +0000 Subject: [PATCH] More file dialog tweaks Signed-off-by: falkTX --- dgl/src/WindowPrivateData.cpp | 64 +++++----------------------- dgl/src/WindowPrivateData.hpp | 2 +- distrho/extra/FileBrowserDialog.cpp | 46 ++++++++++++++++---- distrho/extra/FileBrowserDialog.hpp | 42 ++++++++++-------- distrho/src/DistrhoUIPrivateData.hpp | 2 +- 5 files changed, 74 insertions(+), 82 deletions(-) diff --git a/dgl/src/WindowPrivateData.cpp b/dgl/src/WindowPrivateData.cpp index 6c1621c0..a30f10f1 100644 --- a/dgl/src/WindowPrivateData.cpp +++ b/dgl/src/WindowPrivateData.cpp @@ -19,8 +19,6 @@ #include "pugl.hpp" -#include "../../distrho/extra/String.hpp" - #ifdef DISTRHO_OS_WINDOWS # include # include @@ -451,62 +449,20 @@ bool Window::PrivateData::removeIdleCallback(IdleCallback* const callback) // ----------------------------------------------------------------------- // file handling -bool Window::PrivateData::openFileBrowser(const Window::FileBrowserOptions& options) +bool Window::PrivateData::openFileBrowser(const FileBrowserOptions& options) { - using DISTRHO_NAMESPACE::String; - - // -------------------------------------------------------------------------- - // configure start dir - - String startDir(options.startDir); - - if (startDir.isEmpty()) - { - // TESTING verify this whole thing... -# ifdef DISTRHO_OS_WINDOWS - if (char* const cwd = _getcwd(nullptr, 0)) - { - startDir = cwd; - std::free(cwd); - } -# else - if (char* const cwd = getcwd(nullptr, 0)) - { - startDir = cwd; - std::free(cwd); - } -# endif - } - - DISTRHO_SAFE_ASSERT_RETURN(startDir.isNotEmpty(), false); - - if (! startDir.endsWith(DISTRHO_OS_SEP)) - startDir += DISTRHO_OS_SEP_STR; - - // -------------------------------------------------------------------------- - // configure window title - - String windowTitle(options.title); - - if (windowTitle.isEmpty()) - { - windowTitle = puglGetWindowTitle(view); - - if (windowTitle.isEmpty()) - windowTitle = "FileBrowser"; - } - - // -------------------------------------------------------------------------- - // show - if (fileBrowserHandle != nullptr) fileBrowserClose(fileBrowserHandle); - fileBrowserHandle = fileBrowserOpen(isEmbed, - puglGetNativeWindow(view), autoScaling ? autoScaleFactor : scaleFactor, - startDir, - windowTitle.buffer(), - options); + FileBrowserOptions options2 = options; + + if (options2.title == nullptr) + options2.title = puglGetWindowTitle(view); + + fileBrowserHandle = fileBrowserCreate(isEmbed, + puglGetNativeWindow(view), + autoScaling ? autoScaleFactor : scaleFactor, + options2); return fileBrowserHandle != nullptr; } diff --git a/dgl/src/WindowPrivateData.hpp b/dgl/src/WindowPrivateData.hpp index a8ffe39f..68b2e375 100644 --- a/dgl/src/WindowPrivateData.hpp +++ b/dgl/src/WindowPrivateData.hpp @@ -159,7 +159,7 @@ struct Window::PrivateData : IdleCallback { #ifndef DGL_FILE_BROWSER_DISABLED // file handling - bool openFileBrowser(const Window::FileBrowserOptions& options); + bool openFileBrowser(const FileBrowserOptions& options); #endif static void renderToPicture(const char* filename, const GraphicsContext& context, uint width, uint height); diff --git a/distrho/extra/FileBrowserDialog.cpp b/distrho/extra/FileBrowserDialog.cpp index 8618474c..e021798b 100644 --- a/distrho/extra/FileBrowserDialog.cpp +++ b/distrho/extra/FileBrowserDialog.cpp @@ -16,12 +16,15 @@ #include "FileBrowserDialog.hpp" #include "ScopedPointer.hpp" +#include "String.hpp" #ifdef DISTRHO_OS_MAC # import #endif #ifdef DISTRHO_OS_WINDOWS # include +# include +# include # include #endif #ifdef HAVE_DBUS @@ -226,13 +229,40 @@ namespace DISTRHO_FILE_BROWSER_DIALOG_EXTRA_NAMESPACE { // -------------------------------------------------------------------------------------------------------------------- -FileBrowserHandle fileBrowserOpen(const bool isEmbed, - const uintptr_t windowId, - const double scaleFactor, - const char* const startDir, - const char* const windowTitle, - const FileBrowserOptions& options) +FileBrowserHandle fileBrowserCreate(const bool isEmbed, + const uintptr_t windowId, + const double scaleFactor, + const FileBrowserOptions& options) { + String startDir(options.startDir); + + if (startDir.isEmpty()) + { +#ifdef DISTRHO_OS_WINDOWS + if (char* const cwd = _getcwd(nullptr, 0)) + { + startDir = cwd; + std::free(cwd); + } +#else + if (char* const cwd = getcwd(nullptr, 0)) + { + startDir = cwd; + std::free(cwd); + } +#endif + } + + DISTRHO_SAFE_ASSERT_RETURN(startDir.isNotEmpty(), nullptr); + + if (! startDir.endsWith(DISTRHO_OS_SEP)) + startDir += DISTRHO_OS_SEP_STR; + + String windowTitle(options.title); + + if (windowTitle.isEmpty()) + windowTitle = "FileBrowser"; + ScopedPointer handle; handle = new FileBrowserData(isEmbed); @@ -360,9 +390,6 @@ FileBrowserHandle fileBrowserOpen(const bool isEmbed, // might be unused (void)windowId; (void)scaleFactor; - (void)startDir; - (void)windowTitle; - (void)options; } // -------------------------------------------------------------------------------------------------------------------- @@ -447,6 +474,7 @@ bool fileBrowserIdle(const FileBrowserHandle handle) } } #endif + #ifdef HAVE_X11 Display* const x11display = handle->x11display; diff --git a/distrho/extra/FileBrowserDialog.hpp b/distrho/extra/FileBrowserDialog.hpp index 36465864..cd3237d8 100644 --- a/distrho/extra/FileBrowserDialog.hpp +++ b/distrho/extra/FileBrowserDialog.hpp @@ -30,11 +30,8 @@ typedef FileBrowserData* FileBrowserHandle; // -------------------------------------------------------------------------------------------------------------------- /** - File browser options. - @see Window::openFileBrowser - - @note This is exactly the same API as provided by the Window class, - but redefined so that non-embed/DGL based UIs can still use file browser related functions. + File browser options, for customizing the file browser dialog.@n + By default the file browser dialog will be work as "open file" in the current working directory. */ struct FileBrowserOptions { /** Whether we are saving, opening files otherwise (default) */ @@ -91,24 +88,35 @@ struct FileBrowserOptions { namespace DISTRHO_FILE_BROWSER_DIALOG_EXTRA_NAMESPACE { #endif -// -------------------------------------------------------------------------------------------------------------------- - -FileBrowserHandle fileBrowserOpen(bool isEmbed, uintptr_t windowId, double scaleFactor, - const char* startDir, const char* windowTitle, const FileBrowserOptions& options); +/** + Create a new file browser dialog. -// -------------------------------------------------------------------------------------------------------------------- -// returns true if dialog was closed (with or without a file selection) + @p isEmbed: Whether the window this dialog belongs to is an embed/child window (needed to close dialog on Windows) + @p windowId: The native window id to attach this dialog to as transient parent (X11 Window, HWND or NSView*) + @p scaleFactor: Scale factor to use (only used on X11) + @p options: Extra options, optional + By default the file browser dialog will be work as "open file" in the current working directory. +*/ +FileBrowserHandle fileBrowserCreate(bool isEmbed, + uintptr_t windowId, + double scaleFactor, + const FileBrowserOptions& options = {}); +/** + Idle the file browser dialog handle.@n + Returns true if dialog was closed (with or without a file selection), handle must not be used afterwards. +*/ bool fileBrowserIdle(const FileBrowserHandle handle); -// -------------------------------------------------------------------------------------------------------------------- -// close sofd file dialog - +/** + Close the file browser dialog, handle must not be used afterwards. +*/ void fileBrowserClose(const FileBrowserHandle handle); -// -------------------------------------------------------------------------------------------------------------------- -// get path chosen via sofd file dialog - +/** + Get the path chosen by the user or null.@n + Should only be called after fileBrowserIdle returns true. +*/ const char* fileBrowserGetPath(const FileBrowserHandle handle); // -------------------------------------------------------------------------------------------------------------------- diff --git a/distrho/src/DistrhoUIPrivateData.hpp b/distrho/src/DistrhoUIPrivateData.hpp index 0b03160a..6d837cb1 100644 --- a/distrho/src/DistrhoUIPrivateData.hpp +++ b/distrho/src/DistrhoUIPrivateData.hpp @@ -433,7 +433,7 @@ inline bool UI::PrivateData::fileRequestCallback(const char* const key) snprintf(title, sizeof(title)-1u, DISTRHO_PLUGIN_NAME ": %s", key); title[sizeof(title)-1u] = '\0'; - DGL_NAMESPACE::Window::FileBrowserOptions opts; + FileBrowserOptions opts; opts.title = title; return window->openFileBrowser(opts); #endif