| 1 |
// |
| 2 |
// Portable File Dialogs |
| 3 |
// |
| 4 |
// Copyright © 2018—2020 Sam Hocevar <[email protected]> |
| 5 |
// |
| 6 |
// This library is free software. It comes without any warranty, to |
| 7 |
// the extent permitted by applicable law. You can redistribute it |
| 8 |
// and/or modify it under the terms of the Do What the Fuck You Want |
| 9 |
// to Public License, Version 2, as published by the WTFPL Task Force. |
| 10 |
// See http://www.wtfpl.net/ for more details. |
| 11 |
// |
| 12 |
|
| 13 |
#pragma once |
| 14 |
|
| 15 |
#if _WIN32 |
| 16 |
#ifndef WIN32_LEAN_AND_MEAN |
| 17 |
# define WIN32_LEAN_AND_MEAN 1 |
| 18 |
#endif |
| 19 |
#include <windows.h> |
| 20 |
#include <commdlg.h> |
| 21 |
#include <shlobj.h> |
| 22 |
#include <shobjidl.h> // IFileDialog |
| 23 |
#include <shellapi.h> |
| 24 |
#include <strsafe.h> |
| 25 |
#include <future> // std::async |
| 26 |
|
| 27 |
#elif __EMSCRIPTEN__ |
| 28 |
#include <emscripten.h> |
| 29 |
|
| 30 |
#else |
| 31 |
#ifndef _POSIX_C_SOURCE |
| 32 |
# define _POSIX_C_SOURCE 2 // for popen() |
| 33 |
#endif |
| 34 |
#ifdef __APPLE__ |
| 35 |
# ifndef _DARWIN_C_SOURCE |
| 36 |
# define _DARWIN_C_SOURCE |
| 37 |
# endif |
| 38 |
#endif |
| 39 |
#include <cstdio> // popen() |
| 40 |
#include <cstdlib> // std::getenv() |
| 41 |
#include <fcntl.h> // fcntl() |
| 42 |
#include <unistd.h> // read(), pipe(), dup2() |
| 43 |
#include <csignal> // ::kill, std::signal |
| 44 |
#include <sys/wait.h> // waitpid() |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <string> // std::string |
| 48 |
#include <memory> // std::shared_ptr |
| 49 |
#include <iostream> // std::ostream |
| 50 |
#include <map> // std::map |
| 51 |
#include <set> // std::set |
| 52 |
#include <regex> // std::regex |
| 53 |
#include <thread> // std::mutex, std::this_thread |
| 54 |
#include <chrono> // std::chrono |
| 55 |
|
| 56 |
// Versions of mingw64 g++ up to 9.3.0 do not have a complete IFileDialog |
| 57 |
// we don't use PFD on Windows anyway |
| 58 |
#ifndef PFD_HAS_IFILEDIALOG |
| 59 |
#define PFD_HAS_IFILEDIALOG 0 |
| 60 |
//# define PFD_HAS_IFILEDIALOG 1 |
| 61 |
//# if (defined __MINGW64__ || defined __MINGW32__) && defined __GXX_ABI_VERSION |
| 62 |
//# if __GXX_ABI_VERSION <= 1014 |
| 63 |
//# undef PFD_HAS_IFILEDIALOG |
| 64 |
//# define PFD_HAS_IFILEDIALOG 0 |
| 65 |
//# endif |
| 66 |
//# endif |
| 67 |
#endif |
| 68 |
|
| 69 |
namespace pfd |
| 70 |
{ |
| 71 |
|
| 72 |
enum class button |
| 73 |
{ |
| 74 |
cancel = -1, |
| 75 |
ok, |
| 76 |
yes, |
| 77 |
no, |
| 78 |
abort, |
| 79 |
retry, |
| 80 |
ignore, |
| 81 |
}; |
| 82 |
|
| 83 |
enum class choice |
| 84 |
{ |
| 85 |
ok = 0, |
| 86 |
ok_cancel, |
| 87 |
yes_no, |
| 88 |
yes_no_cancel, |
| 89 |
retry_cancel, |
| 90 |
abort_retry_ignore, |
| 91 |
}; |
| 92 |
|
| 93 |
enum class icon |
| 94 |
{ |
| 95 |
info = 0, |
| 96 |
warning, |
| 97 |
error, |
| 98 |
question, |
| 99 |
}; |
| 100 |
|
| 101 |
// Additional option flags for various dialog constructors |
| 102 |
enum class opt : uint8_t |
| 103 |
{ |
| 104 |
none = 0, |
| 105 |
// For file open, allow multiselect. |
| 106 |
multiselect = 0x1, |
| 107 |
// For file save, force overwrite and disable the confirmation dialog. |
| 108 |
force_overwrite = 0x2, |
| 109 |
// For folder select, force path to be the provided argument instead |
| 110 |
// of the last opened directory, which is the Microsoft-recommended, |
| 111 |
// user-friendly behaviour. |
| 112 |
force_path = 0x4, |
| 113 |
}; |
| 114 |
|
| 115 |
inline opt operator |(opt a, opt b) { return opt(uint8_t(a) | uint8_t(b)); } |
| 116 |
inline bool operator &(opt a, opt b) { return bool(uint8_t(a) & uint8_t(b)); } |
| 117 |
|
| 118 |
// The settings class, only exposing to the user a way to set verbose mode |
| 119 |
// and to force a rescan of installed desktop helpers (zenity, kdialog…). |
| 120 |
class settings |
| 121 |
{ |
| 122 |
public: |
| 123 |
static bool available(); |
| 124 |
|
| 125 |
static void verbose(bool value); |
| 126 |
static void rescan(); |
| 127 |
|
| 128 |
protected: |
| 129 |
explicit settings(bool resync = false); |
| 130 |
|
| 131 |
bool check_program(std::string const &program); |
| 132 |
|
| 133 |
inline bool is_osascript() const; |
| 134 |
inline bool is_zenity() const; |
| 135 |
inline bool is_kdialog() const; |
| 136 |
|
| 137 |
enum class flag |
| 138 |
{ |
| 139 |
is_scanned = 0, |
| 140 |
is_verbose, |
| 141 |
|
| 142 |
has_zenity, |
| 143 |
has_matedialog, |
| 144 |
has_qarma, |
| 145 |
has_kdialog, |
| 146 |
is_vista, |
| 147 |
|
| 148 |
max_flag, |
| 149 |
}; |
| 150 |
|
| 151 |
// Static array of flags for internal state |
| 152 |
bool const &flags(flag in_flag) const; |
| 153 |
|
| 154 |
// Non-const getter for the static array of flags |
| 155 |
bool &flags(flag in_flag); |
| 156 |
}; |
| 157 |
|
| 158 |
// Internal classes, not to be used by client applications |
| 159 |
namespace internal |
| 160 |
{ |
| 161 |
|
| 162 |
// Process wait timeout, in milliseconds |
| 163 |
static int const default_wait_timeout = 20; |
| 164 |
|
| 165 |
class executor |
| 166 |
{ |
| 167 |
friend class dialog; |
| 168 |
|
| 169 |
public: |
| 170 |
// High level function to get the result of a command |
| 171 |
std::string result(int *exit_code = nullptr); |
| 172 |
|
| 173 |
// High level function to abort |
| 174 |
bool kill(); |
| 175 |
|
| 176 |
#if _WIN32 |
| 177 |
void start_func(std::function<std::string(int *)> const &fun); |
| 178 |
static BOOL CALLBACK enum_windows_callback(HWND hwnd, LPARAM lParam); |
| 179 |
#elif __EMSCRIPTEN__ |
| 180 |
void start(int exit_code); |
| 181 |
#else |
| 182 |
void start_process(std::vector<std::string> const &command); |
| 183 |
#endif |
| 184 |
|
| 185 |
~executor(); |
| 186 |
|
| 187 |
protected: |
| 188 |
bool ready(int timeout = default_wait_timeout); |
| 189 |
void stop(); |
| 190 |
|
| 191 |
private: |
| 192 |
bool m_running = false; |
| 193 |
std::string m_stdout; |
| 194 |
int m_exit_code = -1; |
| 195 |
#if _WIN32 |
| 196 |
std::future<std::string> m_future; |
| 197 |
std::set<HWND> m_windows; |
| 198 |
std::condition_variable m_cond; |
| 199 |
std::mutex m_mutex; |
| 200 |
DWORD m_tid; |
| 201 |
#elif __EMSCRIPTEN__ || __NX__ |
| 202 |
// FIXME: do something |
| 203 |
#else |
| 204 |
pid_t m_pid = 0; |
| 205 |
int m_fd = -1; |
| 206 |
#endif |
| 207 |
}; |
| 208 |
|
| 209 |
class platform |
| 210 |
{ |
| 211 |
protected: |
| 212 |
#if _WIN32 |
| 213 |
// Helper class around LoadLibraryA() and GetProcAddress() with some safety |
| 214 |
class dll |
| 215 |
{ |
| 216 |
public: |
| 217 |
dll(std::string const &name); |
| 218 |
~dll(); |
| 219 |
|
| 220 |
template<typename T> class proc |
| 221 |
{ |
| 222 |
public: |
| 223 |
proc(dll const &lib, std::string const &sym) |
| 224 |
: m_proc(reinterpret_cast<T *>(::GetProcAddress(lib.handle, sym.c_str()))) |
| 225 |
{} |
| 226 |
|
| 227 |
operator bool() const { return m_proc != nullptr; } |
| 228 |
operator T *() const { return m_proc; } |
| 229 |
|
| 230 |
private: |
| 231 |
T *m_proc; |
| 232 |
}; |
| 233 |
|
| 234 |
private: |
| 235 |
HMODULE handle; |
| 236 |
}; |
| 237 |
|
| 238 |
// Helper class around CoInitialize() and CoUnInitialize() |
| 239 |
class ole32_dll : public dll |
| 240 |
{ |
| 241 |
public: |
| 242 |
ole32_dll(); |
| 243 |
~ole32_dll(); |
| 244 |
bool is_initialized(); |
| 245 |
|
| 246 |
private: |
| 247 |
HRESULT m_state; |
| 248 |
}; |
| 249 |
|
| 250 |
// Helper class around CreateActCtx() and ActivateActCtx() |
| 251 |
class new_style_context |
| 252 |
{ |
| 253 |
public: |
| 254 |
new_style_context(); |
| 255 |
~new_style_context(); |
| 256 |
|
| 257 |
private: |
| 258 |
HANDLE create(); |
| 259 |
ULONG_PTR m_cookie = 0; |
| 260 |
}; |
| 261 |
#endif |
| 262 |
}; |
| 263 |
|
| 264 |
class dialog : protected settings, protected platform |
| 265 |
{ |
| 266 |
public: |
| 267 |
bool ready(int timeout = default_wait_timeout) const; |
| 268 |
bool kill() const; |
| 269 |
|
| 270 |
protected: |
| 271 |
explicit dialog(); |
| 272 |
|
| 273 |
std::vector<std::string> desktop_helper() const; |
| 274 |
static std::string buttons_to_name(choice _choice); |
| 275 |
static std::string get_icon_name(icon _icon); |
| 276 |
|
| 277 |
std::string powershell_quote(std::string const &str) const; |
| 278 |
std::string osascript_quote(std::string const &str) const; |
| 279 |
std::string shell_quote(std::string const &str) const; |
| 280 |
|
| 281 |
// Keep handle to executing command |
| 282 |
std::shared_ptr<executor> m_async; |
| 283 |
}; |
| 284 |
|
| 285 |
class file_dialog : public dialog |
| 286 |
{ |
| 287 |
protected: |
| 288 |
enum type |
| 289 |
{ |
| 290 |
open, |
| 291 |
save, |
| 292 |
folder, |
| 293 |
}; |
| 294 |
|
| 295 |
file_dialog(type in_type, |
| 296 |
std::string const &title, |
| 297 |
std::string const &default_path = "", |
| 298 |
std::vector<std::string> const &filters = {}, |
| 299 |
opt options = opt::none); |
| 300 |
|
| 301 |
protected: |
| 302 |
std::string string_result(); |
| 303 |
std::vector<std::string> vector_result(); |
| 304 |
|
| 305 |
#if _WIN32 |
| 306 |
static int CALLBACK bffcallback(HWND hwnd, UINT uMsg, LPARAM, LPARAM pData); |
| 307 |
#if PFD_HAS_IFILEDIALOG |
| 308 |
std::string select_folder_vista(IFileDialog *ifd, bool force_path); |
| 309 |
#endif |
| 310 |
|
| 311 |
std::wstring m_wtitle; |
| 312 |
std::wstring m_wdefault_path; |
| 313 |
|
| 314 |
std::vector<std::string> m_vector_result; |
| 315 |
#endif |
| 316 |
}; |
| 317 |
|
| 318 |
} // namespace internal |
| 319 |
|
| 320 |
// |
| 321 |
// The notify widget |
| 322 |
// |
| 323 |
|
| 324 |
class notify : public internal::dialog |
| 325 |
{ |
| 326 |
public: |
| 327 |
notify(std::string const &title, |
| 328 |
std::string const &message, |
| 329 |
icon _icon = icon::info); |
| 330 |
}; |
| 331 |
|
| 332 |
// |
| 333 |
// The message widget |
| 334 |
// |
| 335 |
|
| 336 |
class message : public internal::dialog |
| 337 |
{ |
| 338 |
public: |
| 339 |
message(std::string const &title, |
| 340 |
std::string const &text, |
| 341 |
choice _choice = choice::ok_cancel, |
| 342 |
icon _icon = icon::info); |
| 343 |
|
| 344 |
button result(); |
| 345 |
|
| 346 |
private: |
| 347 |
// Some extra logic to map the exit code to button number |
| 348 |
std::map<int, button> m_mappings; |
| 349 |
}; |
| 350 |
|
| 351 |
// |
| 352 |
// The open_file, save_file, and open_folder widgets |
| 353 |
// |
| 354 |
|
| 355 |
class open_file : public internal::file_dialog |
| 356 |
{ |
| 357 |
public: |
| 358 |
open_file(std::string const &title, |
| 359 |
std::string const &default_path = "", |
| 360 |
std::vector<std::string> const &filters = { "All Files", "*" }, |
| 361 |
opt options = opt::none); |
| 362 |
|
| 363 |
#if defined(__has_cpp_attribute) |
| 364 |
#if __has_cpp_attribute(deprecated) |
| 365 |
// Backwards compatibility |
| 366 |
[[deprecated("Use pfd::opt::multiselect instead of allow_multiselect")]] |
| 367 |
#endif |
| 368 |
#endif |
| 369 |
open_file(std::string const &title, |
| 370 |
std::string const &default_path, |
| 371 |
std::vector<std::string> const &filters, |
| 372 |
bool allow_multiselect); |
| 373 |
|
| 374 |
std::vector<std::string> result(); |
| 375 |
}; |
| 376 |
|
| 377 |
class save_file : public internal::file_dialog |
| 378 |
{ |
| 379 |
public: |
| 380 |
save_file(std::string const &title, |
| 381 |
std::string const &default_path = "", |
| 382 |
std::vector<std::string> const &filters = { "All Files", "*" }, |
| 383 |
opt options = opt::none); |
| 384 |
|
| 385 |
#if defined(__has_cpp_attribute) |
| 386 |
#if __has_cpp_attribute(deprecated) |
| 387 |
// Backwards compatibility |
| 388 |
[[deprecated("Use pfd::opt::force_overwrite instead of confirm_overwrite")]] |
| 389 |
#endif |
| 390 |
#endif |
| 391 |
save_file(std::string const &title, |
| 392 |
std::string const &default_path, |
| 393 |
std::vector<std::string> const &filters, |
| 394 |
bool confirm_overwrite); |
| 395 |
|
| 396 |
std::string result(); |
| 397 |
}; |
| 398 |
|
| 399 |
class select_folder : public internal::file_dialog |
| 400 |
{ |
| 401 |
public: |
| 402 |
select_folder(std::string const &title, |
| 403 |
std::string const &default_path = "", |
| 404 |
opt options = opt::none); |
| 405 |
|
| 406 |
std::string result(); |
| 407 |
}; |
| 408 |
|
| 409 |
// |
| 410 |
// Below this are all the method implementations. You may choose to define the |
| 411 |
// macro PFD_SKIP_IMPLEMENTATION everywhere before including this header except |
| 412 |
// in one place. This may reduce compilation times. |
| 413 |
// |
| 414 |
|
| 415 |
#if !defined PFD_SKIP_IMPLEMENTATION |
| 416 |
|
| 417 |
// internal free functions implementations |
| 418 |
|
| 419 |
namespace internal |
| 420 |
{ |
| 421 |
|
| 422 |
#if _WIN32 |
| 423 |
static inline std::wstring str2wstr(std::string const &str) |
| 424 |
{ |
| 425 |
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0); |
| 426 |
std::wstring ret(len, '\0'); |
| 427 |
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), (LPWSTR)ret.data(), (int)ret.size()); |
| 428 |
return ret; |
| 429 |
} |
| 430 |
|
| 431 |
static inline std::string wstr2str(std::wstring const &str) |
| 432 |
{ |
| 433 |
int len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0, nullptr, nullptr); |
| 434 |
std::string ret(len, '\0'); |
| 435 |
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size(), (LPSTR)ret.data(), (int)ret.size(), nullptr, nullptr); |
| 436 |
return ret; |
| 437 |
} |
| 438 |
|
| 439 |
static inline bool is_vista() |
| 440 |
{ |
| 441 |
OSVERSIONINFOEXW osvi; |
| 442 |
memset(&osvi, 0, sizeof(osvi)); |
| 443 |
DWORDLONG const mask = VerSetConditionMask( |
| 444 |
VerSetConditionMask( |
| 445 |
VerSetConditionMask( |
| 446 |
0, VER_MAJORVERSION, VER_GREATER_EQUAL), |
| 447 |
VER_MINORVERSION, VER_GREATER_EQUAL), |
| 448 |
VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); |
| 449 |
osvi.dwOSVersionInfoSize = sizeof(osvi); |
| 450 |
osvi.dwMajorVersion = HIBYTE(_WIN32_WINNT_VISTA); |
| 451 |
osvi.dwMinorVersion = LOBYTE(_WIN32_WINNT_VISTA); |
| 452 |
osvi.wServicePackMajor = 0; |
| 453 |
|
| 454 |
return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, mask) != FALSE; |
| 455 |
} |
| 456 |
#endif |
| 457 |
|
| 458 |
// This is necessary until C++20 which will have std::string::ends_with() etc. |
| 459 |
|
| 460 |
static inline bool ends_with(std::string const &str, std::string const &suffix) |
| 461 |
{ |
| 462 |
return suffix.size() <= str.size() && |
| 463 |
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; |
| 464 |
} |
| 465 |
|
| 466 |
static inline bool starts_with(std::string const &str, std::string const &prefix) |
| 467 |
{ |
| 468 |
return prefix.size() <= str.size() && |
| 469 |
str.compare(0, prefix.size(), prefix) == 0; |
| 470 |
} |
| 471 |
|
| 472 |
} // namespace internal |
| 473 |
|
| 474 |
// settings implementation |
| 475 |
|
| 476 |
inline settings::settings(bool resync) |
| 477 |
{ |
| 478 |
flags(flag::is_scanned) &= !resync; |
| 479 |
|
| 480 |
if (flags(flag::is_scanned)) |
| 481 |
return; |
| 482 |
|
| 483 |
#if _WIN32 |
| 484 |
flags(flag::is_vista) = internal::is_vista(); |
| 485 |
#elif !__APPLE__ |
| 486 |
flags(flag::has_zenity) = check_program("zenity"); |
| 487 |
flags(flag::has_matedialog) = check_program("matedialog"); |
| 488 |
flags(flag::has_qarma) = check_program("qarma"); |
| 489 |
flags(flag::has_kdialog) = check_program("kdialog"); |
| 490 |
|
| 491 |
// If multiple helpers are available, try to default to the best one |
| 492 |
if (flags(flag::has_zenity) && flags(flag::has_kdialog)) |
| 493 |
{ |
| 494 |
auto desktop_name = std::getenv("XDG_SESSION_DESKTOP"); |
| 495 |
if (desktop_name && desktop_name == std::string("gnome")) |
| 496 |
flags(flag::has_kdialog) = false; |
| 497 |
else if (desktop_name && desktop_name == std::string("KDE")) |
| 498 |
flags(flag::has_zenity) = false; |
| 499 |
} |
| 500 |
#endif |
| 501 |
|
| 502 |
flags(flag::is_scanned) = true; |
| 503 |
} |
| 504 |
|
| 505 |
inline bool settings::available() |
| 506 |
{ |
| 507 |
#if _WIN32 |
| 508 |
return true; |
| 509 |
#elif __APPLE__ |
| 510 |
return true; |
| 511 |
#else |
| 512 |
settings tmp; |
| 513 |
return tmp.flags(flag::has_zenity) || |
| 514 |
tmp.flags(flag::has_matedialog) || |
| 515 |
tmp.flags(flag::has_qarma) || |
| 516 |
tmp.flags(flag::has_kdialog); |
| 517 |
#endif |
| 518 |
} |
| 519 |
|
| 520 |
inline void settings::verbose(bool value) |
| 521 |
{ |
| 522 |
settings().flags(flag::is_verbose) = value; |
| 523 |
} |
| 524 |
|
| 525 |
inline void settings::rescan() |
| 526 |
{ |
| 527 |
settings(/* resync = */ true); |
| 528 |
} |
| 529 |
|
| 530 |
// Check whether a program is present using “which”. |
| 531 |
inline bool settings::check_program(std::string const &program) |
| 532 |
{ |
| 533 |
#if _WIN32 |
| 534 |
(void)program; |
| 535 |
return false; |
| 536 |
#elif __EMSCRIPTEN__ |
| 537 |
(void)program; |
| 538 |
return false; |
| 539 |
#else |
| 540 |
int exit_code = -1; |
| 541 |
internal::executor async; |
| 542 |
async.start_process({"/bin/sh", "-c", "which " + program}); |
| 543 |
async.result(&exit_code); |
| 544 |
return exit_code == 0; |
| 545 |
#endif |
| 546 |
} |
| 547 |
|
| 548 |
inline bool settings::is_osascript() const |
| 549 |
{ |
| 550 |
#if __APPLE__ |
| 551 |
return true; |
| 552 |
#else |
| 553 |
return false; |
| 554 |
#endif |
| 555 |
} |
| 556 |
|
| 557 |
inline bool settings::is_zenity() const |
| 558 |
{ |
| 559 |
return flags(flag::has_zenity) || |
| 560 |
flags(flag::has_matedialog) || |
| 561 |
flags(flag::has_qarma); |
| 562 |
} |
| 563 |
|
| 564 |
inline bool settings::is_kdialog() const |
| 565 |
{ |
| 566 |
return flags(flag::has_kdialog); |
| 567 |
} |
| 568 |
|
| 569 |
inline bool const &settings::flags(flag in_flag) const |
| 570 |
{ |
| 571 |
static bool flags[size_t(flag::max_flag)]; |
| 572 |
return flags[size_t(in_flag)]; |
| 573 |
} |
| 574 |
|
| 575 |
inline bool &settings::flags(flag in_flag) |
| 576 |
{ |
| 577 |
return const_cast<bool &>(static_cast<settings const *>(this)->flags(in_flag)); |
| 578 |
} |
| 579 |
|
| 580 |
// executor implementation |
| 581 |
|
| 582 |
inline std::string internal::executor::result(int *exit_code /* = nullptr */) |
| 583 |
{ |
| 584 |
stop(); |
| 585 |
if (exit_code) |
| 586 |
*exit_code = m_exit_code; |
| 587 |
return m_stdout; |
| 588 |
} |
| 589 |
|
| 590 |
inline bool internal::executor::kill() |
| 591 |
{ |
| 592 |
#if _WIN32 |
| 593 |
if (m_future.valid()) |
| 594 |
{ |
| 595 |
// Close all windows that weren’t open when we started the future |
| 596 |
auto previous_windows = m_windows; |
| 597 |
EnumWindows(&enum_windows_callback, (LPARAM)this); |
| 598 |
for (auto hwnd : m_windows) |
| 599 |
if (previous_windows.find(hwnd) == previous_windows.end()) |
| 600 |
SendMessage(hwnd, WM_CLOSE, 0, 0); |
| 601 |
} |
| 602 |
#elif __EMSCRIPTEN__ || __NX__ |
| 603 |
// FIXME: do something |
| 604 |
(void)timeout; |
| 605 |
return false; // cannot kill |
| 606 |
#else |
| 607 |
::kill(m_pid, SIGKILL); |
| 608 |
#endif |
| 609 |
stop(); |
| 610 |
return true; |
| 611 |
} |
| 612 |
|
| 613 |
#if _WIN32 |
| 614 |
inline BOOL CALLBACK internal::executor::enum_windows_callback(HWND hwnd, LPARAM lParam) |
| 615 |
{ |
| 616 |
auto that = (executor *)lParam; |
| 617 |
|
| 618 |
DWORD pid; |
| 619 |
auto tid = GetWindowThreadProcessId(hwnd, &pid); |
| 620 |
if (tid == that->m_tid) |
| 621 |
that->m_windows.insert(hwnd); |
| 622 |
return TRUE; |
| 623 |
} |
| 624 |
#endif |
| 625 |
|
| 626 |
#if _WIN32 |
| 627 |
inline void internal::executor::start_func(std::function<std::string(int *)> const &fun) |
| 628 |
{ |
| 629 |
stop(); |
| 630 |
|
| 631 |
auto trampoline = [fun, this]() |
| 632 |
{ |
| 633 |
// Save our thread id so that the caller can cancel us |
| 634 |
m_tid = GetCurrentThreadId(); |
| 635 |
EnumWindows(&enum_windows_callback, (LPARAM)this); |
| 636 |
m_cond.notify_all(); |
| 637 |
return fun(&m_exit_code); |
| 638 |
}; |
| 639 |
|
| 640 |
std::unique_lock<std::mutex> lock(m_mutex); |
| 641 |
m_future = std::async(std::launch::async, trampoline); |
| 642 |
m_cond.wait(lock); |
| 643 |
m_running = true; |
| 644 |
} |
| 645 |
|
| 646 |
#elif __EMSCRIPTEN__ |
| 647 |
inline void internal::executor::start(int exit_code) |
| 648 |
{ |
| 649 |
m_exit_code = exit_code; |
| 650 |
} |
| 651 |
|
| 652 |
#else |
| 653 |
inline void internal::executor::start_process(std::vector<std::string> const &command) |
| 654 |
{ |
| 655 |
printf("---- WILL EXECUTE:\n"); |
| 656 |
for (const std::string& i: command) { |
| 657 |
printf("- %s\n",i.c_str()); |
| 658 |
} |
| 659 |
printf("END\n"); |
| 660 |
stop(); |
| 661 |
m_stdout.clear(); |
| 662 |
m_exit_code = -1; |
| 663 |
|
| 664 |
int in[2], out[2]; |
| 665 |
if (pipe(in) != 0 || pipe(out) != 0) |
| 666 |
return; |
| 667 |
|
| 668 |
m_pid = fork(); |
| 669 |
if (m_pid < 0) |
| 670 |
return; |
| 671 |
|
| 672 |
close(in[m_pid ? 0 : 1]); |
| 673 |
close(out[m_pid ? 1 : 0]); |
| 674 |
|
| 675 |
if (m_pid == 0) |
| 676 |
{ |
| 677 |
dup2(in[0], STDIN_FILENO); |
| 678 |
dup2(out[1], STDOUT_FILENO); |
| 679 |
|
| 680 |
// Ignore stderr so that it doesn’t pollute the console (e.g. GTK+ errors from zenity) |
| 681 |
int fd = open("/dev/null", O_WRONLY); |
| 682 |
dup2(fd, STDERR_FILENO); |
| 683 |
close(fd); |
| 684 |
|
| 685 |
std::vector<char *> args; |
| 686 |
std::transform(command.cbegin(), command.cend(), std::back_inserter(args), |
| 687 |
[](std::string const &s) { return const_cast<char *>(s.c_str()); }); |
| 688 |
args.push_back(nullptr); // null-terminate argv[] |
| 689 |
|
| 690 |
execvp(args[0], args.data()); |
| 691 |
exit(1); |
| 692 |
} |
| 693 |
|
| 694 |
close(in[1]); |
| 695 |
m_fd = out[0]; |
| 696 |
auto flags = fcntl(m_fd, F_GETFL); |
| 697 |
fcntl(m_fd, F_SETFL, flags | O_NONBLOCK); |
| 698 |
|
| 699 |
m_running = true; |
| 700 |
} |
| 701 |
#endif |
| 702 |
|
| 703 |
inline internal::executor::~executor() |
| 704 |
{ |
| 705 |
stop(); |
| 706 |
} |
| 707 |
|
| 708 |
inline bool internal::executor::ready(int timeout /* = default_wait_timeout */) |
| 709 |
{ |
| 710 |
if (!m_running) |
| 711 |
return true; |
| 712 |
|
| 713 |
#if _WIN32 |
| 714 |
if (m_future.valid()) |
| 715 |
{ |
| 716 |
auto status = m_future.wait_for(std::chrono::milliseconds(timeout)); |
| 717 |
if (status != std::future_status::ready) |
| 718 |
{ |
| 719 |
// On Windows, we need to run the message pump. If the async |
| 720 |
// thread uses a Windows API dialog, it may be attached to the |
| 721 |
// main thread and waiting for messages that only we can dispatch. |
| 722 |
MSG msg; |
| 723 |
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) |
| 724 |
{ |
| 725 |
TranslateMessage(&msg); |
| 726 |
DispatchMessage(&msg); |
| 727 |
} |
| 728 |
return false; |
| 729 |
} |
| 730 |
|
| 731 |
m_stdout = m_future.get(); |
| 732 |
} |
| 733 |
#elif __EMSCRIPTEN__ || __NX__ |
| 734 |
// FIXME: do something |
| 735 |
(void)timeout; |
| 736 |
#else |
| 737 |
char buf[BUFSIZ]; |
| 738 |
ssize_t received = read(m_fd, buf, BUFSIZ); // Flawfinder: ignore |
| 739 |
if (received > 0) |
| 740 |
{ |
| 741 |
m_stdout += std::string(buf, received); |
| 742 |
return false; |
| 743 |
} |
| 744 |
|
| 745 |
// Reap child process if it is dead. It is possible that the system has already reaped it |
| 746 |
// (this happens when the calling application handles or ignores SIG_CHLD) and results in |
| 747 |
// waitpid() failing with ECHILD. Otherwise we assume the child is running and we sleep for |
| 748 |
// a little while. |
| 749 |
int status; |
| 750 |
pid_t child = waitpid(m_pid, &status, WNOHANG); |
| 751 |
if (child != m_pid && (child >= 0 || errno != ECHILD)) |
| 752 |
{ |
| 753 |
// FIXME: this happens almost always at first iteration |
| 754 |
std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 755 |
return false; |
| 756 |
} |
| 757 |
|
| 758 |
close(m_fd); |
| 759 |
m_exit_code = WEXITSTATUS(status); |
| 760 |
#endif |
| 761 |
|
| 762 |
m_running = false; |
| 763 |
return true; |
| 764 |
} |
| 765 |
|
| 766 |
inline void internal::executor::stop() |
| 767 |
{ |
| 768 |
// Loop until the user closes the dialog |
| 769 |
while (!ready()) |
| 770 |
; |
| 771 |
} |
| 772 |
|
| 773 |
// dll implementation |
| 774 |
|
| 775 |
#if _WIN32 |
| 776 |
inline internal::platform::dll::dll(std::string const &name) |
| 777 |
: handle(::LoadLibraryA(name.c_str())) |
| 778 |
{} |
| 779 |
|
| 780 |
inline internal::platform::dll::~dll() |
| 781 |
{ |
| 782 |
if (handle) |
| 783 |
::FreeLibrary(handle); |
| 784 |
} |
| 785 |
#endif // _WIN32 |
| 786 |
|
| 787 |
// ole32_dll implementation |
| 788 |
|
| 789 |
#if _WIN32 |
| 790 |
inline internal::platform::ole32_dll::ole32_dll() |
| 791 |
: dll("ole32.dll") |
| 792 |
{ |
| 793 |
// Use COINIT_MULTITHREADED because COINIT_APARTMENTTHREADED causes crashes. |
| 794 |
// See https://github.com/samhocevar/portable-file-dialogs/issues/51 |
| 795 |
auto coinit = proc<HRESULT WINAPI (LPVOID, DWORD)>(*this, "CoInitializeEx"); |
| 796 |
m_state = coinit(nullptr, COINIT_MULTITHREADED); |
| 797 |
} |
| 798 |
|
| 799 |
inline internal::platform::ole32_dll::~ole32_dll() |
| 800 |
{ |
| 801 |
if (is_initialized()) |
| 802 |
proc<void WINAPI ()>(*this, "CoUninitialize")(); |
| 803 |
} |
| 804 |
|
| 805 |
inline bool internal::platform::ole32_dll::is_initialized() |
| 806 |
{ |
| 807 |
return m_state == S_OK || m_state == S_FALSE; |
| 808 |
} |
| 809 |
#endif |
| 810 |
|
| 811 |
// new_style_context implementation |
| 812 |
|
| 813 |
#if _WIN32 |
| 814 |
inline internal::platform::new_style_context::new_style_context() |
| 815 |
{ |
| 816 |
// Only create one activation context for the whole app lifetime. |
| 817 |
static HANDLE hctx = create(); |
| 818 |
|
| 819 |
if (hctx != INVALID_HANDLE_VALUE) |
| 820 |
ActivateActCtx(hctx, &m_cookie); |
| 821 |
} |
| 822 |
|
| 823 |
inline internal::platform::new_style_context::~new_style_context() |
| 824 |
{ |
| 825 |
DeactivateActCtx(0, m_cookie); |
| 826 |
} |
| 827 |
|
| 828 |
inline HANDLE internal::platform::new_style_context::create() |
| 829 |
{ |
| 830 |
// This “hack” seems to be necessary for this code to work on windows XP. |
| 831 |
// Without it, dialogs do not show and close immediately. GetError() |
| 832 |
// returns 0 so I don’t know what causes this. I was not able to reproduce |
| 833 |
// this behavior on Windows 7 and 10 but just in case, let it be here for |
| 834 |
// those versions too. |
| 835 |
// This hack is not required if other dialogs are used (they load comdlg32 |
| 836 |
// automatically), only if message boxes are used. |
| 837 |
dll comdlg32("comdlg32.dll"); |
| 838 |
|
| 839 |
// Using approach as shown here: https://stackoverflow.com/a/10444161 |
| 840 |
UINT len = ::GetSystemDirectoryA(nullptr, 0); |
| 841 |
std::string sys_dir(len, '\0'); |
| 842 |
::GetSystemDirectoryA(&sys_dir[0], len); |
| 843 |
|
| 844 |
ACTCTXA act_ctx = |
| 845 |
{ |
| 846 |
// Do not set flag ACTCTX_FLAG_SET_PROCESS_DEFAULT, since it causes a |
| 847 |
// crash with error “default context is already set”. |
| 848 |
sizeof(act_ctx), |
| 849 |
ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID, |
| 850 |
"shell32.dll", 0, 0, sys_dir.c_str(), (LPCSTR)124, NULL, NULL |
| 851 |
}; |
| 852 |
|
| 853 |
return ::CreateActCtxA(&act_ctx); |
| 854 |
} |
| 855 |
#endif // _WIN32 |
| 856 |
|
| 857 |
// dialog implementation |
| 858 |
|
| 859 |
inline bool internal::dialog::ready(int timeout /* = default_wait_timeout */) const |
| 860 |
{ |
| 861 |
return m_async->ready(timeout); |
| 862 |
} |
| 863 |
|
| 864 |
inline bool internal::dialog::kill() const |
| 865 |
{ |
| 866 |
return m_async->kill(); |
| 867 |
} |
| 868 |
|
| 869 |
inline internal::dialog::dialog() |
| 870 |
: m_async(std::make_shared<executor>()) |
| 871 |
{ |
| 872 |
} |
| 873 |
|
| 874 |
inline std::vector<std::string> internal::dialog::desktop_helper() const |
| 875 |
{ |
| 876 |
#if __APPLE__ |
| 877 |
return { "osascript" }; |
| 878 |
#else |
| 879 |
return { flags(flag::has_zenity) ? "zenity" |
| 880 |
: flags(flag::has_matedialog) ? "matedialog" |
| 881 |
: flags(flag::has_qarma) ? "qarma" |
| 882 |
: flags(flag::has_kdialog) ? "kdialog" |
| 883 |
: "echo" }; |
| 884 |
#endif |
| 885 |
} |
| 886 |
|
| 887 |
inline std::string internal::dialog::buttons_to_name(choice _choice) |
| 888 |
{ |
| 889 |
switch (_choice) |
| 890 |
{ |
| 891 |
case choice::ok_cancel: return "okcancel"; |
| 892 |
case choice::yes_no: return "yesno"; |
| 893 |
case choice::yes_no_cancel: return "yesnocancel"; |
| 894 |
case choice::retry_cancel: return "retrycancel"; |
| 895 |
case choice::abort_retry_ignore: return "abortretryignore"; |
| 896 |
/* case choice::ok: */ default: return "ok"; |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
inline std::string internal::dialog::get_icon_name(icon _icon) |
| 901 |
{ |
| 902 |
switch (_icon) |
| 903 |
{ |
| 904 |
case icon::warning: return "warning"; |
| 905 |
case icon::error: return "error"; |
| 906 |
case icon::question: return "question"; |
| 907 |
// Zenity wants "information" but WinForms wants "info" |
| 908 |
/* case icon::info: */ default: |
| 909 |
#if _WIN32 |
| 910 |
return "info"; |
| 911 |
#else |
| 912 |
return "information"; |
| 913 |
#endif |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
// THis is only used for debugging purposes |
| 918 |
inline std::ostream& operator <<(std::ostream &s, std::vector<std::string> const &v) |
| 919 |
{ |
| 920 |
int not_first = 0; |
| 921 |
for (auto &e : v) |
| 922 |
s << (not_first++ ? " " : "") << e; |
| 923 |
return s; |
| 924 |
} |
| 925 |
|
| 926 |
// Properly quote a string for Powershell: replace ' or " with '' or "" |
| 927 |
// FIXME: we should probably get rid of newlines! |
| 928 |
// FIXME: the \" sequence seems unsafe, too! |
| 929 |
// XXX: this is no longer used but I would like to keep it around just in case |
| 930 |
inline std::string internal::dialog::powershell_quote(std::string const &str) const |
| 931 |
{ |
| 932 |
return "'" + std::regex_replace(str, std::regex("['\"]"), "$&$&") + "'"; |
| 933 |
} |
| 934 |
|
| 935 |
// Properly quote a string for osascript: replace \ or " with \\ or \" |
| 936 |
// XXX: this also used to replace ' with \' when popen was used, but it would be |
| 937 |
// smarter to do shell_quote(osascript_quote(...)) if this is needed again. |
| 938 |
inline std::string internal::dialog::osascript_quote(std::string const &str) const |
| 939 |
{ |
| 940 |
return "\"" + std::regex_replace(str, std::regex("[\\\\\"]"), "\\$&") + "\""; |
| 941 |
} |
| 942 |
|
| 943 |
// Properly quote a string for the shell: just replace ' with '\'' |
| 944 |
// XXX: this is no longer used but I would like to keep it around just in case |
| 945 |
inline std::string internal::dialog::shell_quote(std::string const &str) const |
| 946 |
{ |
| 947 |
return "'" + std::regex_replace(str, std::regex("'"), "'\\''") + "'"; |
| 948 |
} |
| 949 |
|
| 950 |
// file_dialog implementation |
| 951 |
|
| 952 |
inline internal::file_dialog::file_dialog(type in_type, |
| 953 |
std::string const &title, |
| 954 |
std::string const &default_path /* = "" */, |
| 955 |
std::vector<std::string> const &filters /* = {} */, |
| 956 |
opt options /* = opt::none */) |
| 957 |
{ |
| 958 |
#if _WIN32 |
| 959 |
std::string filter_list; |
| 960 |
std::regex whitespace(" *"); |
| 961 |
for (size_t i = 0; i + 1 < filters.size(); i += 2) |
| 962 |
{ |
| 963 |
filter_list += filters[i] + '\0'; |
| 964 |
filter_list += std::regex_replace(filters[i + 1], whitespace, ";") + '\0'; |
| 965 |
} |
| 966 |
filter_list += '\0'; |
| 967 |
|
| 968 |
m_async->start_func([this, in_type, title, default_path, filter_list, |
| 969 |
options](int *exit_code) -> std::string |
| 970 |
{ |
| 971 |
(void)exit_code; |
| 972 |
m_wtitle = internal::str2wstr(title); |
| 973 |
m_wdefault_path = internal::str2wstr(default_path); |
| 974 |
auto wfilter_list = internal::str2wstr(filter_list); |
| 975 |
|
| 976 |
// Initialise COM. This is required for the new folder selection window, |
| 977 |
// (see https://github.com/samhocevar/portable-file-dialogs/pull/21) |
| 978 |
// and to avoid random crashes with GetOpenFileNameW() (see |
| 979 |
// https://github.com/samhocevar/portable-file-dialogs/issues/51) |
| 980 |
ole32_dll ole32; |
| 981 |
|
| 982 |
// Folder selection uses a different method |
| 983 |
if (in_type == type::folder) |
| 984 |
{ |
| 985 |
#if PFD_HAS_IFILEDIALOG |
| 986 |
if (flags(flag::is_vista)) |
| 987 |
{ |
| 988 |
// On Vista and higher we should be able to use IFileDialog for folder selection |
| 989 |
IFileDialog *ifd; |
| 990 |
HRESULT hr = dll::proc<HRESULT WINAPI (REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID *)>(ole32, "CoCreateInstance") |
| 991 |
(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ifd)); |
| 992 |
|
| 993 |
// In case CoCreateInstance fails (which it should not), try legacy approach |
| 994 |
if (SUCCEEDED(hr)) |
| 995 |
return select_folder_vista(ifd, options & opt::force_path); |
| 996 |
} |
| 997 |
#endif |
| 998 |
|
| 999 |
BROWSEINFOW bi; |
| 1000 |
memset(&bi, 0, sizeof(bi)); |
| 1001 |
|
| 1002 |
bi.lpfn = &bffcallback; |
| 1003 |
bi.lParam = (LPARAM)this; |
| 1004 |
|
| 1005 |
if (flags(flag::is_vista)) |
| 1006 |
{ |
| 1007 |
if (ole32.is_initialized()) |
| 1008 |
bi.ulFlags |= BIF_NEWDIALOGSTYLE; |
| 1009 |
bi.ulFlags |= BIF_EDITBOX; |
| 1010 |
bi.ulFlags |= BIF_STATUSTEXT; |
| 1011 |
} |
| 1012 |
|
| 1013 |
auto *list = SHBrowseForFolderW(&bi); |
| 1014 |
std::string ret; |
| 1015 |
if (list) |
| 1016 |
{ |
| 1017 |
auto buffer = new wchar_t[MAX_PATH]; |
| 1018 |
SHGetPathFromIDListW(list, buffer); |
| 1019 |
dll::proc<void WINAPI (LPVOID)>(ole32, "CoTaskMemFree")(list); |
| 1020 |
ret = internal::wstr2str(buffer); |
| 1021 |
delete[] buffer; |
| 1022 |
} |
| 1023 |
return ret; |
| 1024 |
} |
| 1025 |
|
| 1026 |
OPENFILENAMEW ofn; |
| 1027 |
memset(&ofn, 0, sizeof(ofn)); |
| 1028 |
ofn.lStructSize = sizeof(OPENFILENAMEW); |
| 1029 |
ofn.hwndOwner = GetActiveWindow(); |
| 1030 |
|
| 1031 |
ofn.lpstrFilter = wfilter_list.c_str(); |
| 1032 |
|
| 1033 |
auto woutput = std::wstring(MAX_PATH * 256, L'\0'); |
| 1034 |
ofn.lpstrFile = (LPWSTR)woutput.data(); |
| 1035 |
ofn.nMaxFile = (DWORD)woutput.size(); |
| 1036 |
if (!m_wdefault_path.empty()) |
| 1037 |
{ |
| 1038 |
// If a directory was provided, use it as the initial directory. If |
| 1039 |
// a valid path was provided, use it as the initial file. Otherwise, |
| 1040 |
// let the Windows API decide. |
| 1041 |
auto path_attr = GetFileAttributesW(m_wdefault_path.c_str()); |
| 1042 |
if (path_attr != INVALID_FILE_ATTRIBUTES && (path_attr & FILE_ATTRIBUTE_DIRECTORY)) |
| 1043 |
ofn.lpstrInitialDir = m_wdefault_path.c_str(); |
| 1044 |
else if (m_wdefault_path.size() <= woutput.size()) |
| 1045 |
//second argument is size of buffer, not length of string |
| 1046 |
StringCchCopyW(ofn.lpstrFile, MAX_PATH*256+1, m_wdefault_path.c_str()); |
| 1047 |
else |
| 1048 |
{ |
| 1049 |
ofn.lpstrFileTitle = (LPWSTR)m_wdefault_path.data(); |
| 1050 |
ofn.nMaxFileTitle = (DWORD)m_wdefault_path.size(); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
ofn.lpstrTitle = m_wtitle.c_str(); |
| 1054 |
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER; |
| 1055 |
|
| 1056 |
dll comdlg32("comdlg32.dll"); |
| 1057 |
|
| 1058 |
// Apply new visual style (required for windows XP) |
| 1059 |
new_style_context ctx; |
| 1060 |
|
| 1061 |
if (in_type == type::save) |
| 1062 |
{ |
| 1063 |
if (!(options & opt::force_overwrite)) |
| 1064 |
ofn.Flags |= OFN_OVERWRITEPROMPT; |
| 1065 |
|
| 1066 |
dll::proc<BOOL WINAPI (LPOPENFILENAMEW)> get_save_file_name(comdlg32, "GetSaveFileNameW"); |
| 1067 |
if (get_save_file_name(&ofn) == 0) |
| 1068 |
return ""; |
| 1069 |
return internal::wstr2str(woutput.c_str()); |
| 1070 |
} |
| 1071 |
else |
| 1072 |
{ |
| 1073 |
if (options & opt::multiselect) |
| 1074 |
ofn.Flags |= OFN_ALLOWMULTISELECT; |
| 1075 |
ofn.Flags |= OFN_PATHMUSTEXIST; |
| 1076 |
|
| 1077 |
dll::proc<BOOL WINAPI (LPOPENFILENAMEW)> get_open_file_name(comdlg32, "GetOpenFileNameW"); |
| 1078 |
if (get_open_file_name(&ofn) == 0) |
| 1079 |
return ""; |
| 1080 |
} |
| 1081 |
|
| 1082 |
std::string prefix; |
| 1083 |
for (wchar_t const *p = woutput.c_str(); *p; ) |
| 1084 |
{ |
| 1085 |
auto filename = internal::wstr2str(p); |
| 1086 |
p += wcslen(p); |
| 1087 |
// In multiselect mode, we advance p one wchar further and |
| 1088 |
// check for another filename. If there is one and the |
| 1089 |
// prefix is empty, it means we just read the prefix. |
| 1090 |
if ((options & opt::multiselect) && *++p && prefix.empty()) |
| 1091 |
{ |
| 1092 |
prefix = filename + "/"; |
| 1093 |
continue; |
| 1094 |
} |
| 1095 |
|
| 1096 |
m_vector_result.push_back(prefix + filename); |
| 1097 |
} |
| 1098 |
|
| 1099 |
return ""; |
| 1100 |
}); |
| 1101 |
#else |
| 1102 |
auto command = desktop_helper(); |
| 1103 |
|
| 1104 |
if (is_osascript()) |
| 1105 |
{ |
| 1106 |
std::string script = "set ret to choose"; |
| 1107 |
switch (in_type) |
| 1108 |
{ |
| 1109 |
case type::save: |
| 1110 |
script += " file name"; |
| 1111 |
break; |
| 1112 |
case type::open: default: |
| 1113 |
script += " file"; |
| 1114 |
if (options & opt::multiselect) |
| 1115 |
script += " with multiple selections allowed"; |
| 1116 |
break; |
| 1117 |
case type::folder: |
| 1118 |
script += " folder"; |
| 1119 |
break; |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (default_path.size()) |
| 1123 |
script += " default location " + osascript_quote(default_path); |
| 1124 |
script += " with prompt " + osascript_quote(title); |
| 1125 |
|
| 1126 |
if (in_type == type::open) |
| 1127 |
{ |
| 1128 |
// Concatenate all user-provided filter patterns |
| 1129 |
std::string patterns; |
| 1130 |
for (size_t i = 0; i < filters.size() / 2; ++i) |
| 1131 |
patterns += " " + filters[2 * i + 1]; |
| 1132 |
|
| 1133 |
// Split the pattern list to check whether "*" is in there; if it |
| 1134 |
// is, we have to disable filters because there is no mechanism in |
| 1135 |
// OS X for the user to override the filter. |
| 1136 |
std::regex sep("\\s+"); |
| 1137 |
std::string filter_list; |
| 1138 |
bool has_filter = true; |
| 1139 |
std::sregex_token_iterator iter(patterns.begin(), patterns.end(), sep, -1); |
| 1140 |
std::sregex_token_iterator end; |
| 1141 |
for ( ; iter != end; ++iter) |
| 1142 |
{ |
| 1143 |
auto pat = iter->str(); |
| 1144 |
if (pat == "*" || pat == "*.*") |
| 1145 |
has_filter = false; |
| 1146 |
else if (internal::starts_with(pat, "*.")) |
| 1147 |
filter_list += (filter_list.size() == 0 ? "" : ",") + |
| 1148 |
osascript_quote(pat.substr(2, pat.size() - 2)); |
| 1149 |
} |
| 1150 |
if (has_filter && filter_list.size() > 0) |
| 1151 |
script += " of type {" + filter_list + "}"; |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (in_type == type::open && (options & opt::multiselect)) |
| 1155 |
{ |
| 1156 |
script += "\nset s to \"\""; |
| 1157 |
script += "\nrepeat with i in ret"; |
| 1158 |
script += "\n set s to s & (POSIX path of i) & \"\\n\""; |
| 1159 |
script += "\nend repeat"; |
| 1160 |
script += "\ncopy s to stdout"; |
| 1161 |
} |
| 1162 |
else |
| 1163 |
{ |
| 1164 |
script += "\nPOSIX path of ret"; |
| 1165 |
} |
| 1166 |
|
| 1167 |
command.push_back("-e"); |
| 1168 |
command.push_back(script); |
| 1169 |
} |
| 1170 |
else if (is_zenity()) |
| 1171 |
{ |
| 1172 |
command.push_back("--file-selection"); |
| 1173 |
command.push_back("--filename=" + default_path); |
| 1174 |
command.push_back("--title"); |
| 1175 |
command.push_back(title); |
| 1176 |
command.push_back("--separator=\n"); |
| 1177 |
|
| 1178 |
for (size_t i = 0; i < filters.size() / 2; ++i) |
| 1179 |
{ |
| 1180 |
command.push_back("--file-filter"); |
| 1181 |
command.push_back(filters[2 * i] + "|" + filters[2 * i + 1]); |
| 1182 |
} |
| 1183 |
|
| 1184 |
if (in_type == type::save) |
| 1185 |
command.push_back("--save"); |
| 1186 |
if (in_type == type::folder) |
| 1187 |
command.push_back("--directory"); |
| 1188 |
if (!(options & opt::force_overwrite)) |
| 1189 |
command.push_back("--confirm-overwrite"); |
| 1190 |
if (options & opt::multiselect) |
| 1191 |
command.push_back("--multiple"); |
| 1192 |
} |
| 1193 |
else if (is_kdialog()) |
| 1194 |
{ |
| 1195 |
switch (in_type) |
| 1196 |
{ |
| 1197 |
case type::save: command.push_back("--getsavefilename"); break; |
| 1198 |
case type::open: command.push_back("--getopenfilename"); break; |
| 1199 |
case type::folder: command.push_back("--getexistingdirectory"); break; |
| 1200 |
} |
| 1201 |
if (options & opt::multiselect) |
| 1202 |
{ |
| 1203 |
command.push_back("--multiple"); |
| 1204 |
command.push_back("--separate-output"); |
| 1205 |
} |
| 1206 |
|
| 1207 |
command.push_back(default_path); |
| 1208 |
|
| 1209 |
std::string filter; |
| 1210 |
for (size_t i = 0; i < filters.size() / 2; ++i) |
| 1211 |
filter += (i == 0 ? "" : " | ") + filters[2 * i] + "(" + filters[2 * i + 1] + ")"; |
| 1212 |
command.push_back(filter); |
| 1213 |
|
| 1214 |
command.push_back("--title"); |
| 1215 |
command.push_back(title); |
| 1216 |
} |
| 1217 |
|
| 1218 |
if (flags(flag::is_verbose)) |
| 1219 |
std::cerr << "pfd: " << command << std::endl; |
| 1220 |
|
| 1221 |
m_async->start_process(command); |
| 1222 |
#endif |
| 1223 |
} |
| 1224 |
|
| 1225 |
inline std::string internal::file_dialog::string_result() |
| 1226 |
{ |
| 1227 |
#if _WIN32 |
| 1228 |
return m_async->result(); |
| 1229 |
#else |
| 1230 |
auto ret = m_async->result(); |
| 1231 |
// Strip potential trailing newline (zenity). Also strip trailing slash |
| 1232 |
// added by osascript for consistency with other backends. |
| 1233 |
while (!ret.empty() && (ret.back() == '\n' || ret.back() == '/')) |
| 1234 |
ret.pop_back(); |
| 1235 |
return ret; |
| 1236 |
#endif |
| 1237 |
} |
| 1238 |
|
| 1239 |
inline std::vector<std::string> internal::file_dialog::vector_result() |
| 1240 |
{ |
| 1241 |
#if _WIN32 |
| 1242 |
m_async->result(); |
| 1243 |
return m_vector_result; |
| 1244 |
#else |
| 1245 |
std::vector<std::string> ret; |
| 1246 |
auto result = m_async->result(); |
| 1247 |
for (;;) |
| 1248 |
{ |
| 1249 |
// Split result along newline characters |
| 1250 |
auto i = result.find('\n'); |
| 1251 |
if (i == 0 || i == std::string::npos) |
| 1252 |
break; |
| 1253 |
ret.push_back(result.substr(0, i)); |
| 1254 |
result = result.substr(i + 1, result.size()); |
| 1255 |
} |
| 1256 |
return ret; |
| 1257 |
#endif |
| 1258 |
} |
| 1259 |
|
| 1260 |
#if _WIN32 |
| 1261 |
// Use a static function to pass as BFFCALLBACK for legacy folder select |
| 1262 |
inline int CALLBACK internal::file_dialog::bffcallback(HWND hwnd, UINT uMsg, |
| 1263 |
LPARAM, LPARAM pData) |
| 1264 |
{ |
| 1265 |
auto inst = (file_dialog *)pData; |
| 1266 |
switch (uMsg) |
| 1267 |
{ |
| 1268 |
case BFFM_INITIALIZED: |
| 1269 |
SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)inst->m_wdefault_path.c_str()); |
| 1270 |
break; |
| 1271 |
} |
| 1272 |
return 0; |
| 1273 |
} |
| 1274 |
|
| 1275 |
#if PFD_HAS_IFILEDIALOG |
| 1276 |
inline std::string internal::file_dialog::select_folder_vista(IFileDialog *ifd, bool force_path) |
| 1277 |
{ |
| 1278 |
std::string result; |
| 1279 |
|
| 1280 |
IShellItem *folder; |
| 1281 |
|
| 1282 |
// Load library at runtime so app doesn't link it at load time (which will fail on windows XP) |
| 1283 |
dll shell32("shell32.dll"); |
| 1284 |
dll::proc<HRESULT WINAPI (PCWSTR, IBindCtx*, REFIID, void**)> |
| 1285 |
create_item(shell32, "SHCreateItemFromParsingName"); |
| 1286 |
|
| 1287 |
if (!create_item) |
| 1288 |
return ""; |
| 1289 |
|
| 1290 |
auto hr = create_item(m_wdefault_path.c_str(), |
| 1291 |
nullptr, |
| 1292 |
IID_PPV_ARGS(&folder)); |
| 1293 |
|
| 1294 |
// Set default folder if found. This only sets the default folder. If |
| 1295 |
// Windows has any info about the most recently selected folder, it |
| 1296 |
// will display it instead. Generally, calling SetFolder() to set the |
| 1297 |
// current directory “is not a good or expected user experience and |
| 1298 |
// should therefore be avoided”: |
| 1299 |
// https://docs.microsoft.com/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfolder |
| 1300 |
if (SUCCEEDED(hr)) |
| 1301 |
{ |
| 1302 |
if (force_path) |
| 1303 |
ifd->SetFolder(folder); |
| 1304 |
else |
| 1305 |
ifd->SetDefaultFolder(folder); |
| 1306 |
folder->Release(); |
| 1307 |
} |
| 1308 |
|
| 1309 |
// Set the dialog title and option to select folders |
| 1310 |
ifd->SetOptions(FOS_PICKFOLDERS); |
| 1311 |
ifd->SetTitle(m_wtitle.c_str()); |
| 1312 |
|
| 1313 |
hr = ifd->Show(GetActiveWindow()); |
| 1314 |
if (SUCCEEDED(hr)) |
| 1315 |
{ |
| 1316 |
IShellItem* item; |
| 1317 |
hr = ifd->GetResult(&item); |
| 1318 |
if (SUCCEEDED(hr)) |
| 1319 |
{ |
| 1320 |
wchar_t* wselected = nullptr; |
| 1321 |
item->GetDisplayName(SIGDN_FILESYSPATH, &wselected); |
| 1322 |
item->Release(); |
| 1323 |
|
| 1324 |
if (wselected) |
| 1325 |
{ |
| 1326 |
result = internal::wstr2str(std::wstring(wselected)); |
| 1327 |
dll::proc<void WINAPI (LPVOID)>(ole32_dll(), "CoTaskMemFree")(wselected); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
ifd->Release(); |
| 1333 |
|
| 1334 |
return result; |
| 1335 |
} |
| 1336 |
#endif |
| 1337 |
#endif |
| 1338 |
|
| 1339 |
// notify implementation |
| 1340 |
|
| 1341 |
#if _WIN32 |
| 1342 |
inline BOOL WINAPI icon_enum_callback(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG_PTR lParam) |
| 1343 |
{ |
| 1344 |
((NOTIFYICONDATAW *)lParam)->hIcon = ::LoadIcon(GetModuleHandle(nullptr), lpName); |
| 1345 |
return false; |
| 1346 |
}; |
| 1347 |
#endif |
| 1348 |
|
| 1349 |
inline notify::notify(std::string const &title, |
| 1350 |
std::string const &message, |
| 1351 |
icon _icon /* = icon::info */) |
| 1352 |
{ |
| 1353 |
if (_icon == icon::question) // Not supported by notifications |
| 1354 |
_icon = icon::info; |
| 1355 |
|
| 1356 |
#if _WIN32 |
| 1357 |
// Use a static shared pointer for notify_icon so that we can delete |
| 1358 |
// it whenever we need to display a new one, and we can also wait |
| 1359 |
// until the program has finished running. |
| 1360 |
struct notify_icon_data : public NOTIFYICONDATAW |
| 1361 |
{ |
| 1362 |
~notify_icon_data() { Shell_NotifyIconW(NIM_DELETE, this); } |
| 1363 |
}; |
| 1364 |
|
| 1365 |
static std::shared_ptr<notify_icon_data> nid; |
| 1366 |
|
| 1367 |
// Release the previous notification icon, if any, and allocate a new |
| 1368 |
// one. Note that std::make_shared() does value initialization, so there |
| 1369 |
// is no need to memset the structure. |
| 1370 |
nid = nullptr; |
| 1371 |
nid = std::make_shared<notify_icon_data>(); |
| 1372 |
|
| 1373 |
// For XP support |
| 1374 |
nid->cbSize = NOTIFYICONDATAW_V2_SIZE; |
| 1375 |
nid->hWnd = nullptr; |
| 1376 |
nid->uID = 0; |
| 1377 |
|
| 1378 |
// Flag Description: |
| 1379 |
// - NIF_ICON The hIcon member is valid. |
| 1380 |
// - NIF_MESSAGE The uCallbackMessage member is valid. |
| 1381 |
// - NIF_TIP The szTip member is valid. |
| 1382 |
// - NIF_STATE The dwState and dwStateMask members are valid. |
| 1383 |
// - NIF_INFO Use a balloon ToolTip instead of a standard ToolTip. The szInfo, uTimeout, szInfoTitle, and dwInfoFlags members are valid. |
| 1384 |
// - NIF_GUID Reserved. |
| 1385 |
nid->uFlags = NIF_MESSAGE | NIF_ICON | NIF_INFO; |
| 1386 |
|
| 1387 |
// Flag Description |
| 1388 |
// - NIIF_ERROR An error icon. |
| 1389 |
// - NIIF_INFO An information icon. |
| 1390 |
// - NIIF_NONE No icon. |
| 1391 |
// - NIIF_WARNING A warning icon. |
| 1392 |
// - NIIF_ICON_MASK Version 6.0. Reserved. |
| 1393 |
// - NIIF_NOSOUND Version 6.0. Do not play the associated sound. Applies only to balloon ToolTips |
| 1394 |
switch (_icon) |
| 1395 |
{ |
| 1396 |
case icon::warning: nid->dwInfoFlags = NIIF_WARNING; break; |
| 1397 |
case icon::error: nid->dwInfoFlags = NIIF_ERROR; break; |
| 1398 |
/* case icon::info: */ default: nid->dwInfoFlags = NIIF_INFO; break; |
| 1399 |
} |
| 1400 |
|
| 1401 |
nid->hIcon = ::LoadIcon(nullptr, IDI_APPLICATION); |
| 1402 |
::EnumResourceNames(nullptr, RT_GROUP_ICON, (ENUMRESNAMEPROC)icon_enum_callback, (LONG_PTR)nid.get()); |
| 1403 |
|
| 1404 |
nid->uTimeout = 5000; |
| 1405 |
|
| 1406 |
StringCchCopyW(nid->szInfoTitle, ARRAYSIZE(nid->szInfoTitle), internal::str2wstr(title).c_str()); |
| 1407 |
StringCchCopyW(nid->szInfo, ARRAYSIZE(nid->szInfo), internal::str2wstr(message).c_str()); |
| 1408 |
|
| 1409 |
// Display the new icon |
| 1410 |
Shell_NotifyIconW(NIM_ADD, nid.get()); |
| 1411 |
#else |
| 1412 |
auto command = desktop_helper(); |
| 1413 |
|
| 1414 |
if (is_osascript()) |
| 1415 |
{ |
| 1416 |
command.push_back("-e"); |
| 1417 |
command.push_back("display notification " + osascript_quote(message) + |
| 1418 |
" with title " + osascript_quote(title)); |
| 1419 |
} |
| 1420 |
else if (is_zenity()) |
| 1421 |
{ |
| 1422 |
command.push_back("--notification"); |
| 1423 |
command.push_back("--window-icon"); |
| 1424 |
command.push_back(get_icon_name(_icon)); |
| 1425 |
command.push_back("--text"); |
| 1426 |
command.push_back(title + "\n" + message); |
| 1427 |
} |
| 1428 |
else if (is_kdialog()) |
| 1429 |
{ |
| 1430 |
command.push_back("--icon"); |
| 1431 |
command.push_back(get_icon_name(_icon)); |
| 1432 |
command.push_back("--title"); |
| 1433 |
command.push_back(title); |
| 1434 |
command.push_back("--passivepopup"); |
| 1435 |
command.push_back(message); |
| 1436 |
command.push_back("5"); |
| 1437 |
} |
| 1438 |
|
| 1439 |
if (flags(flag::is_verbose)) |
| 1440 |
std::cerr << "pfd: " << command << std::endl; |
| 1441 |
|
| 1442 |
m_async->start_process(command); |
| 1443 |
#endif |
| 1444 |
} |
| 1445 |
|
| 1446 |
// message implementation |
| 1447 |
|
| 1448 |
inline message::message(std::string const &title, |
| 1449 |
std::string const &text, |
| 1450 |
choice _choice /* = choice::ok_cancel */, |
| 1451 |
icon _icon /* = icon::info */) |
| 1452 |
{ |
| 1453 |
#if _WIN32 |
| 1454 |
// Use MB_SYSTEMMODAL rather than MB_TOPMOST to ensure the message window is brought |
| 1455 |
// to front. See https://github.com/samhocevar/portable-file-dialogs/issues/52 |
| 1456 |
UINT style = MB_SYSTEMMODAL; |
| 1457 |
switch (_icon) |
| 1458 |
{ |
| 1459 |
case icon::warning: style |= MB_ICONWARNING; break; |
| 1460 |
case icon::error: style |= MB_ICONERROR; break; |
| 1461 |
case icon::question: style |= MB_ICONQUESTION; break; |
| 1462 |
/* case icon::info: */ default: style |= MB_ICONINFORMATION; break; |
| 1463 |
} |
| 1464 |
|
| 1465 |
switch (_choice) |
| 1466 |
{ |
| 1467 |
case choice::ok_cancel: style |= MB_OKCANCEL; break; |
| 1468 |
case choice::yes_no: style |= MB_YESNO; break; |
| 1469 |
case choice::yes_no_cancel: style |= MB_YESNOCANCEL; break; |
| 1470 |
case choice::retry_cancel: style |= MB_RETRYCANCEL; break; |
| 1471 |
case choice::abort_retry_ignore: style |= MB_ABORTRETRYIGNORE; break; |
| 1472 |
/* case choice::ok: */ default: style |= MB_OK; break; |
| 1473 |
} |
| 1474 |
|
| 1475 |
m_mappings[IDCANCEL] = button::cancel; |
| 1476 |
m_mappings[IDOK] = button::ok; |
| 1477 |
m_mappings[IDYES] = button::yes; |
| 1478 |
m_mappings[IDNO] = button::no; |
| 1479 |
m_mappings[IDABORT] = button::abort; |
| 1480 |
m_mappings[IDRETRY] = button::retry; |
| 1481 |
m_mappings[IDIGNORE] = button::ignore; |
| 1482 |
|
| 1483 |
m_async->start_func([text, title, style](int* exit_code) -> std::string |
| 1484 |
{ |
| 1485 |
auto wtext = internal::str2wstr(text); |
| 1486 |
auto wtitle = internal::str2wstr(title); |
| 1487 |
// Apply new visual style (required for all Windows versions) |
| 1488 |
new_style_context ctx; |
| 1489 |
*exit_code = MessageBoxW(GetActiveWindow(), wtext.c_str(), wtitle.c_str(), style); |
| 1490 |
return ""; |
| 1491 |
}); |
| 1492 |
|
| 1493 |
#elif __EMSCRIPTEN__ |
| 1494 |
std::string full_message; |
| 1495 |
switch (_icon) |
| 1496 |
{ |
| 1497 |
case icon::warning: full_message = "⚠️"; break; |
| 1498 |
case icon::error: full_message = "⛔"; break; |
| 1499 |
case icon::question: full_message = "❓"; break; |
| 1500 |
/* case icon::info: */ default: full_message = "ℹ"; break; |
| 1501 |
} |
| 1502 |
|
| 1503 |
full_message += ' ' + title + "\n\n" + text; |
| 1504 |
|
| 1505 |
// This does not really start an async task; it just passes the |
| 1506 |
// EM_ASM_INT return value to a fake start() function. |
| 1507 |
m_async->start(EM_ASM_INT( |
| 1508 |
{ |
| 1509 |
if ($1) |
| 1510 |
return window.confirm(UTF8ToString($0)) ? 0 : -1; |
| 1511 |
alert(UTF8ToString($0)); |
| 1512 |
return 0; |
| 1513 |
}, full_message.c_str(), _choice == choice::ok_cancel)); |
| 1514 |
#else |
| 1515 |
auto command = desktop_helper(); |
| 1516 |
|
| 1517 |
if (is_osascript()) |
| 1518 |
{ |
| 1519 |
std::string script = "display dialog " + osascript_quote(text) + |
| 1520 |
" with title " + osascript_quote(title); |
| 1521 |
switch (_choice) |
| 1522 |
{ |
| 1523 |
case choice::ok_cancel: |
| 1524 |
script += "buttons {\"OK\", \"Cancel\"}" |
| 1525 |
" default button \"OK\"" |
| 1526 |
" cancel button \"Cancel\""; |
| 1527 |
m_mappings[256] = button::cancel; |
| 1528 |
break; |
| 1529 |
case choice::yes_no: |
| 1530 |
script += "buttons {\"Yes\", \"No\"}" |
| 1531 |
" default button \"Yes\"" |
| 1532 |
" cancel button \"No\""; |
| 1533 |
m_mappings[256] = button::no; |
| 1534 |
break; |
| 1535 |
case choice::yes_no_cancel: |
| 1536 |
script += "buttons {\"Yes\", \"No\", \"Cancel\"}" |
| 1537 |
" default button \"Yes\"" |
| 1538 |
" cancel button \"Cancel\""; |
| 1539 |
m_mappings[256] = button::cancel; |
| 1540 |
break; |
| 1541 |
case choice::retry_cancel: |
| 1542 |
script += "buttons {\"Retry\", \"Cancel\"}" |
| 1543 |
" default button \"Retry\"" |
| 1544 |
" cancel button \"Cancel\""; |
| 1545 |
m_mappings[256] = button::cancel; |
| 1546 |
break; |
| 1547 |
case choice::abort_retry_ignore: |
| 1548 |
script += "buttons {\"Abort\", \"Retry\", \"Ignore\"}" |
| 1549 |
" default button \"Retry\"" |
| 1550 |
" cancel button \"Retry\""; |
| 1551 |
m_mappings[256] = button::cancel; |
| 1552 |
break; |
| 1553 |
case choice::ok: default: |
| 1554 |
script += "buttons {\"OK\"}" |
| 1555 |
" default button \"OK\"" |
| 1556 |
" cancel button \"OK\""; |
| 1557 |
m_mappings[256] = button::ok; |
| 1558 |
break; |
| 1559 |
} |
| 1560 |
script += " with icon "; |
| 1561 |
switch (_icon) |
| 1562 |
{ |
| 1563 |
#define PFD_OSX_ICON(n) "alias ((path to library folder from system domain) as text " \ |
| 1564 |
"& \"CoreServices:CoreTypes.bundle:Contents:Resources:" n ".icns\")" |
| 1565 |
case icon::info: default: script += PFD_OSX_ICON("ToolBarInfo"); break; |
| 1566 |
case icon::warning: script += "caution"; break; |
| 1567 |
case icon::error: script += "stop"; break; |
| 1568 |
case icon::question: script += PFD_OSX_ICON("GenericQuestionMarkIcon"); break; |
| 1569 |
#undef PFD_OSX_ICON |
| 1570 |
} |
| 1571 |
|
| 1572 |
command.push_back("-e"); |
| 1573 |
command.push_back(script); |
| 1574 |
} |
| 1575 |
else if (is_zenity()) |
| 1576 |
{ |
| 1577 |
switch (_choice) |
| 1578 |
{ |
| 1579 |
case choice::ok_cancel: |
| 1580 |
command.insert(command.end(), { "--question", "--cancel-label=Cancel", "--ok-label=OK" }); break; |
| 1581 |
case choice::yes_no: |
| 1582 |
// Do not use standard --question because it causes “No” to return -1, |
| 1583 |
// which is inconsistent with the “Yes/No/Cancel” mode below. |
| 1584 |
command.insert(command.end(), { "--question", "--switch", "--extra-button=No", "--extra-button=Yes" }); break; |
| 1585 |
case choice::yes_no_cancel: |
| 1586 |
command.insert(command.end(), { "--question", "--switch", "--extra-button=Cancel", "--extra-button=No", "--extra-button=Yes" }); break; |
| 1587 |
case choice::retry_cancel: |
| 1588 |
command.insert(command.end(), { "--question", "--switch", "--extra-button=Cancel", "--extra-button=Retry" }); break; |
| 1589 |
case choice::abort_retry_ignore: |
| 1590 |
command.insert(command.end(), { "--question", "--switch", "--extra-button=Ignore", "--extra-button=Abort", "--extra-button=Retry" }); break; |
| 1591 |
case choice::ok: |
| 1592 |
default: |
| 1593 |
switch (_icon) |
| 1594 |
{ |
| 1595 |
case icon::error: command.push_back("--error"); break; |
| 1596 |
case icon::warning: command.push_back("--warning"); break; |
| 1597 |
default: command.push_back("--info"); break; |
| 1598 |
} |
| 1599 |
} |
| 1600 |
|
| 1601 |
command.insert(command.end(), { "--title", title, |
| 1602 |
"--width=300", "--height=0", // sensible defaults |
| 1603 |
"--text", text, |
| 1604 |
"--icon-name=dialog-" + get_icon_name(_icon) }); |
| 1605 |
} |
| 1606 |
else if (is_kdialog()) |
| 1607 |
{ |
| 1608 |
if (_choice == choice::ok) |
| 1609 |
{ |
| 1610 |
switch (_icon) |
| 1611 |
{ |
| 1612 |
case icon::error: command.push_back("--error"); break; |
| 1613 |
case icon::warning: command.push_back("--sorry"); break; |
| 1614 |
default: command.push_back("--msgbox"); break; |
| 1615 |
} |
| 1616 |
} |
| 1617 |
else |
| 1618 |
{ |
| 1619 |
std::string flag = "--"; |
| 1620 |
if (_icon == icon::warning || _icon == icon::error) |
| 1621 |
flag += "warning"; |
| 1622 |
flag += "yesno"; |
| 1623 |
if (_choice == choice::yes_no_cancel) |
| 1624 |
flag += "cancel"; |
| 1625 |
command.push_back(flag); |
| 1626 |
if (_choice == choice::yes_no || _choice == choice::yes_no_cancel) |
| 1627 |
{ |
| 1628 |
m_mappings[0] = button::yes; |
| 1629 |
m_mappings[256] = button::no; |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
command.push_back(text); |
| 1634 |
command.push_back("--title"); |
| 1635 |
command.push_back(title); |
| 1636 |
|
| 1637 |
// Must be after the above part |
| 1638 |
if (_choice == choice::ok_cancel) |
| 1639 |
command.insert(command.end(), { "--yes-label", "OK", "--no-label", "Cancel" }); |
| 1640 |
} |
| 1641 |
|
| 1642 |
if (flags(flag::is_verbose)) |
| 1643 |
std::cerr << "pfd: " << command << std::endl; |
| 1644 |
|
| 1645 |
m_async->start_process(command); |
| 1646 |
#endif |
| 1647 |
} |
| 1648 |
|
| 1649 |
inline button message::result() |
| 1650 |
{ |
| 1651 |
int exit_code; |
| 1652 |
auto ret = m_async->result(&exit_code); |
| 1653 |
// osascript will say "button returned:Cancel\n" |
| 1654 |
// and others will just say "Cancel\n" |
| 1655 |
if (exit_code < 0 || // this means cancel |
| 1656 |
internal::ends_with(ret, "Cancel\n")) |
| 1657 |
return button::cancel; |
| 1658 |
if (internal::ends_with(ret, "OK\n")) |
| 1659 |
return button::ok; |
| 1660 |
if (internal::ends_with(ret, "Yes\n")) |
| 1661 |
return button::yes; |
| 1662 |
if (internal::ends_with(ret, "No\n")) |
| 1663 |
return button::no; |
| 1664 |
if (internal::ends_with(ret, "Abort\n")) |
| 1665 |
return button::abort; |
| 1666 |
if (internal::ends_with(ret, "Retry\n")) |
| 1667 |
return button::retry; |
| 1668 |
if (internal::ends_with(ret, "Ignore\n")) |
| 1669 |
return button::ignore; |
| 1670 |
if (m_mappings.count(exit_code) != 0) |
| 1671 |
return m_mappings[exit_code]; |
| 1672 |
return exit_code == 0 ? button::ok : button::cancel; |
| 1673 |
} |
| 1674 |
|
| 1675 |
// open_file implementation |
| 1676 |
|
| 1677 |
inline open_file::open_file(std::string const &title, |
| 1678 |
std::string const &default_path /* = "" */, |
| 1679 |
std::vector<std::string> const &filters /* = { "All Files", "*" } */, |
| 1680 |
opt options /* = opt::none */) |
| 1681 |
: file_dialog(type::open, title, default_path, filters, options) |
| 1682 |
{ |
| 1683 |
} |
| 1684 |
|
| 1685 |
inline open_file::open_file(std::string const &title, |
| 1686 |
std::string const &default_path, |
| 1687 |
std::vector<std::string> const &filters, |
| 1688 |
bool allow_multiselect) |
| 1689 |
: open_file(title, default_path, filters, |
| 1690 |
(allow_multiselect ? opt::multiselect : opt::none)) |
| 1691 |
{ |
| 1692 |
} |
| 1693 |
|
| 1694 |
inline std::vector<std::string> open_file::result() |
| 1695 |
{ |
| 1696 |
return vector_result(); |
| 1697 |
} |
| 1698 |
|
| 1699 |
// save_file implementation |
| 1700 |
|
| 1701 |
inline save_file::save_file(std::string const &title, |
| 1702 |
std::string const &default_path /* = "" */, |
| 1703 |
std::vector<std::string> const &filters /* = { "All Files", "*" } */, |
| 1704 |
opt options /* = opt::none */) |
| 1705 |
: file_dialog(type::save, title, default_path, filters, options) |
| 1706 |
{ |
| 1707 |
} |
| 1708 |
|
| 1709 |
inline save_file::save_file(std::string const &title, |
| 1710 |
std::string const &default_path, |
| 1711 |
std::vector<std::string> const &filters, |
| 1712 |
bool confirm_overwrite) |
| 1713 |
: save_file(title, default_path, filters, |
| 1714 |
(confirm_overwrite ? opt::none : opt::force_overwrite)) |
| 1715 |
{ |
| 1716 |
} |
| 1717 |
|
| 1718 |
inline std::string save_file::result() |
| 1719 |
{ |
| 1720 |
return string_result(); |
| 1721 |
} |
| 1722 |
|
| 1723 |
// select_folder implementation |
| 1724 |
|
| 1725 |
inline select_folder::select_folder(std::string const &title, |
| 1726 |
std::string const &default_path /* = "" */, |
| 1727 |
opt options /* = opt::none */) |
| 1728 |
: file_dialog(type::folder, title, default_path, {}, options) |
| 1729 |
{ |
| 1730 |
} |
| 1731 |
|
| 1732 |
inline std::string select_folder::result() |
| 1733 |
{ |
| 1734 |
return string_result(); |
| 1735 |
} |
| 1736 |
|
| 1737 |
#endif // PFD_SKIP_IMPLEMENTATION |
| 1738 |
|
| 1739 |
} // namespace pfd |
| 1740 |
|