Add native helper for the question dialog

This commit is contained in:
Jean Pierre Cimalando 2021-02-22 09:11:16 +01:00
parent 2dbfbf3148
commit 8de978df91
3 changed files with 86 additions and 2 deletions

View file

@ -11,9 +11,20 @@
#include <windows.h>
#include <cstring>
static WCHAR *stringToWideChar(const char *str, int strCch = -1)
{
unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0);
if (strSize == 0)
return {};
std::unique_ptr<WCHAR[]> strW(new WCHAR[strSize]);
if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0)
return {};
return strW.release();
}
bool openFileInExternalEditor(const char *filename)
{
std::wstring path = fs::u8path(filename).wstring();
std::wstring path = stringToWideChar(filename);
SHELLEXECUTEINFOW info;
memset(&info, 0, sizeof(info));
@ -30,7 +41,7 @@ bool openFileInExternalEditor(const char *filename)
bool openDirectoryInExplorer(const char *filename)
{
std::wstring path = fs::u8path(filename).wstring();
std::wstring path = stringToWideChar(filename);
SHELLEXECUTEINFOW info;
memset(&info, 0, sizeof(info));
@ -42,10 +53,23 @@ bool openDirectoryInExplorer(const char *filename)
return ShellExecuteExW(&info);
}
bool askQuestion(const char *text)
{
int ret = MessageBoxW(nullptr, stringToWideChar(text), L"Question", MB_YESNO);
return ret == IDYES;
}
#elif defined(__APPLE__)
// implemented in NativeHelpers.mm
#else
#include <gio/gio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>
#include <cstring>
#include <cerrno>
extern "C" { extern char **environ; }
static bool openFileByMimeType(const char *filename, const char *mimetype)
{
@ -72,4 +96,53 @@ bool openDirectoryInExplorer(const char *filename)
{
return openFileByMimeType(filename, "inode/directory");
}
static std::vector<char *> createForkEnviron()
{
std::vector<char *> newEnv;
newEnv.reserve(256);
for (char **envp = environ; *envp; ++envp) {
// ensure the process will link with system libraries,
// and not these from the Ardour bundle.
if (strncmp(*envp, "LD_LIBRARY_PATH=", 16) == 0)
continue;
newEnv.push_back(*envp);
}
newEnv.push_back(nullptr);
return newEnv;
}
bool askQuestion(const char *text)
{
char *argv[] = {
const_cast<char *>("/usr/bin/zenity"),
const_cast<char *>("--question"),
const_cast<char *>("--text"),
const_cast<char *>(text),
nullptr,
};
std::vector<char *> newEnv = createForkEnviron();
char **envp = newEnv.data();
pid_t forkPid = vfork();
if (forkPid == -1)
return false;
if (forkPid == 0) {
execve(argv[0], argv, envp);
_exit(1);
}
int wret;
int wstatus;
do {
wret = waitpid(forkPid, &wstatus, 0);
} while (wret == -1 && errno == EINTR);
if (wret == -1 || !WIFEXITED(wstatus))
return false;
return WEXITSTATUS(wstatus) == 0;
}
#endif

View file

@ -8,3 +8,4 @@
bool openFileInExternalEditor(const char *filename);
bool openDirectoryInExplorer(const char *filename);
bool askQuestion(const char *text);

View file

@ -31,4 +31,14 @@ bool openDirectoryInExplorer(const char *fileName)
{
return openFileWithApplication(fileName, @"Finder");
}
bool askQuestion(const char *text)
{
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:[NSString stringWithUTF8String:text]];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
NSInteger button = [alert runModal];
return button == NSAlertFirstButtonReturn;
}
#endif