Browse Source

More file dialog tweaks

Signed-off-by: falkTX <falktx@falktx.com>
pull/349/head
falkTX 4 years ago
parent
commit
3d3678c500
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
5 changed files with 74 additions and 82 deletions
  1. +10
    -54
      dgl/src/WindowPrivateData.cpp
  2. +1
    -1
      dgl/src/WindowPrivateData.hpp
  3. +37
    -9
      distrho/extra/FileBrowserDialog.cpp
  4. +25
    -17
      distrho/extra/FileBrowserDialog.hpp
  5. +1
    -1
      distrho/src/DistrhoUIPrivateData.hpp

+ 10
- 54
dgl/src/WindowPrivateData.cpp View File

@@ -19,8 +19,6 @@

#include "pugl.hpp"

#include "../../distrho/extra/String.hpp"

#ifdef DISTRHO_OS_WINDOWS
# include <direct.h>
# include <winsock2.h>
@@ -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;
}


+ 1
- 1
dgl/src/WindowPrivateData.hpp View File

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


+ 37
- 9
distrho/extra/FileBrowserDialog.cpp View File

@@ -16,12 +16,15 @@

#include "FileBrowserDialog.hpp"
#include "ScopedPointer.hpp"
#include "String.hpp"

#ifdef DISTRHO_OS_MAC
# import <Cocoa/Cocoa.h>
#endif
#ifdef DISTRHO_OS_WINDOWS
# include <process.h>
# include <winsock2.h>
# include <windows.h>
# include <vector>
#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<FileBrowserData> 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;



+ 25
- 17
distrho/extra/FileBrowserDialog.hpp View File

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

// --------------------------------------------------------------------------------------------------------------------


+ 1
- 1
distrho/src/DistrhoUIPrivateData.hpp View File

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


Loading…
Cancel
Save