From 1bdc2839ddd27a3a4f411bb04b8634620ade1f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= Date: Thu, 22 Aug 2019 17:14:53 +0300 Subject: [PATCH] Unix: Added stub for file selection dialog To be implemented with Doomsday's own widgets to adding dependencies to native UI toolkits. --- .../libs/gui/src/dialogs/filedialog_x11.cpp | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 doomsday/libs/gui/src/dialogs/filedialog_x11.cpp diff --git a/doomsday/libs/gui/src/dialogs/filedialog_x11.cpp b/doomsday/libs/gui/src/dialogs/filedialog_x11.cpp new file mode 100644 index 0000000000..8315a3904f --- /dev/null +++ b/doomsday/libs/gui/src/dialogs/filedialog_x11.cpp @@ -0,0 +1,171 @@ +/** @file filedialog_x11.cpp Native file chooser dialog. + * + * @authors Copyright (c) 2019 Jaakko Keränen + * + * @par License + * LGPL: http://www.gnu.org/licenses/lgpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser + * General Public License for more details. You should have received a copy of + * the GNU Lesser General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#include "de/FileDialog" + +namespace de { + +DE_PIMPL_NOREF(FileDialog) +{ + String title = "Select File"; + String prompt = "OK"; + Behaviors behavior = AcceptFiles; + List selection; + NativePath initialLocation; + FileTypes fileTypes; // empty list: eveything allowed + + // using Filters = List>; + + // Filters filters() const + // { + // Filters list; + // for (const FileType &fileType : fileTypes) + // { + // const String exts = + // (fileType.extensions ? "*." + String::join(fileType.extensions, ";*.") : DE_STR("*")); + // list << std::make_pair(fileType.label.toUtf16(), exts.toUtf16()); + // } + // return list; + // } +}; + +FileDialog::FileDialog() : d(new Impl) +{} + +void FileDialog::setTitle(const String &title) +{ + d->title = title; +} + +void FileDialog::setPrompt(const String &prompt) +{ + d->prompt = prompt; +} + +void FileDialog::setBehavior(Behaviors behaviors, FlagOp flagOp) +{ + applyFlagOperation(d->behavior, behaviors, flagOp); +} + +void FileDialog::setInitialLocation(const NativePath &initialLocation) +{ + d->initialLocation = initialLocation; +} + +void FileDialog::setFileTypes(const FileTypes &fileTypes) +{ + d->fileTypes = fileTypes; +} + +NativePath FileDialog::selectedPath() const +{ + return d->selection ? d->selection.front() : NativePath(); +} + +List FileDialog::selectedPaths() const +{ + return d->selection; +} + +bool FileDialog::exec() +{ + d->selection.clear(); + + // IFileOpenDialog *dlg = nullptr; + // if (FAILED(CoCreateInstance( + // CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dlg)))) + // { + // return false; + // } + + // // Configure the dialog according to user-specified options. + // DWORD options; + // dlg->GetOptions(&options); + // options |= FOS_FORCEFILESYSTEM; + // if (d->behavior & MultipleSelection) + // { + // options |= FOS_ALLOWMULTISELECT; + // } + // if (d->behavior & AcceptFiles) + // { + // options |= FOS_FILEMUSTEXIST; + // } + // if (d->behavior & AcceptDirectories) + // { + // options |= FOS_PICKFOLDERS | FOS_PATHMUSTEXIST; + // } + // dlg->SetOptions(options); + // { + // const auto path = d->initialLocation.toString().toUtf16(); + // IShellItem *folder = nullptr; + // if (SUCCEEDED(SHCreateItemFromParsingName(path.c_wstr(), nullptr, IID_PPV_ARGS(&folder)))) + // { + // dlg->SetDefaultFolder(folder); + // folder->Release(); + // } + // } + // dlg->SetTitle(d->title.toUtf16().c_wstr()); + // dlg->SetOkButtonLabel(d->prompt.toUtf16().c_wstr()); + // if (d->fileTypes) + // { + // List filters; + // const auto strings = d->filters(); + // for (const auto &str : strings) + // { + // COMDLG_FILTERSPEC spec; + // spec.pszName = str.first.c_wstr(); + // spec.pszSpec = str.second.c_wstr(); + // filters << spec; + // } + // dlg->SetFileTypes(UINT(filters.size()), filters.data()); + // } + + // if (SUCCEEDED(dlg->Show(nullptr))) + // { + // IShellItemArray *results = nullptr; + // dlg->GetResults(&results); + // if (results) + // { + // DWORD resultCount = 0; + // results->GetCount(&resultCount); + // for (unsigned i = 0; i < resultCount; ++i) + // { + // IShellItem *result = nullptr; + // if (SUCCEEDED(results->GetItemAt(i, &result))) + // { + // PWSTR itemPath; + // if (SUCCEEDED(result->GetDisplayName(SIGDN_FILESYSPATH, &itemPath))) + // { + // d->selection << NativePath( + // String::fromUtf16(Block(itemPath, 2 * (wcslen(itemPath) + 1)))); + // CoTaskMemFree(itemPath); + // } + // result->Release(); + // } + // } + // results->Release(); + // } + // } + + // // Cleanup. + // dlg->Release(); + + return !d->selection.empty(); +} + +} // namespace de