refactor folderbox and filebox

This commit is contained in:
Jinhao
2019-02-26 04:24:40 +08:00
parent bf27549beb
commit 29cee29f5f
4 changed files with 180 additions and 133 deletions

View File

@@ -16,7 +16,6 @@
#define NANA_GUI_FILEBOX_HPP #define NANA_GUI_FILEBOX_HPP
#include <nana/gui/basis.hpp> #include <nana/gui/basis.hpp>
#include <nana/filesystem/filesystem.hpp> #include <nana/filesystem/filesystem.hpp>
#include <nana/optional.hpp>
#include <vector> #include <vector>
#include <utility> #include <utility>
@@ -30,6 +29,7 @@ namespace nana
filebox& operator=(filebox&&) = delete; filebox& operator=(filebox&&) = delete;
public: public:
using filters = std::vector<std::pair< ::std::string, ::std::string>>; using filters = std::vector<std::pair< ::std::string, ::std::string>>;
using path_type = std::filesystem::path;
explicit filebox(bool is_open_mode); explicit filebox(bool is_open_mode);
filebox(window owner, bool is_open_mode); filebox(window owner, bool is_open_mode);
@@ -69,16 +69,14 @@ namespace nana
const ::std::string& path() const; const ::std::string& path() const;
::std::string file() const;
const ::std::vector<::std::string>& files() const;
void allow_multi_select(bool allow); void allow_multi_select(bool allow);
/// Display the filebox dialog /// Display the filebox dialog
bool show() const; std::vector<path_type> show() const;
/// a function object method alternative to show() to display the filebox dialog, /// a function object method alternative to show() to display the filebox dialog,
bool operator()() const std::vector<path_type> operator()() const
{ {
return show(); return show();
} }
@@ -100,9 +98,12 @@ namespace nana
explicit folderbox(window owner = nullptr, const path_type& init_path = {}, std::string title={}); explicit folderbox(window owner = nullptr, const path_type& init_path = {}, std::string title={});
~folderbox(); ~folderbox();
std::optional<path_type> show() const; /// Enables/disables multi select
void allow_multi_select(bool allow);
std::optional<path_type> operator()() const std::vector<path_type> show() const;
std::vector<path_type> operator()() const
{ {
return show(); return show();
} }

View File

@@ -184,7 +184,7 @@ namespace nana
throw std::runtime_error("Nana.GUI.Filebox: Wrong categorize path"); throw std::runtime_error("Nana.GUI.Filebox: Wrong categorize path");
if(path.size() == 0) path = "/"; /// \todo : use nana::filesystem_ext::def_rootstr? if(path.size() == 0) path = "/"; /// \todo : use nana::filesystem_ext::def_rootstr?
_m_load_cat_path(path); _m_enter_folder(path);
}); });
@@ -260,7 +260,7 @@ namespace nana
selection_.is_deselect_delayed = false; selection_.is_deselect_delayed = false;
} }
else if(event_code::dbl_click == arg.evt_code) else if(event_code::dbl_click == arg.evt_code)
_m_click_select_file(arg); _m_list_dbl_clicked();
}; };
ls_file_.events().dbl_click.connect_unignorable(fn_list_handler); ls_file_.events().dbl_click.connect_unignorable(fn_list_handler);
@@ -354,6 +354,7 @@ namespace nana
tb_file_.events().key_char.connect_unignorable([this](const arg_keyboard& arg) tb_file_.events().key_char.connect_unignorable([this](const arg_keyboard& arg)
{ {
allow_fall_back_ = false;
if(arg.key == nana::keyboard::enter) if(arg.key == nana::keyboard::enter)
_m_try_select(tb_file_.caption()); _m_try_select(tb_file_.caption());
}); });
@@ -444,7 +445,7 @@ namespace nana
else else
dir = saved_selected_path; dir = saved_selected_path;
_m_load_cat_path(dir.size() ? dir : fs_ext::path_user().native()); _m_enter_folder(dir.size() ? dir : fs_ext::path_user().native());
tb_file_.caption(filename); tb_file_.caption(filename);
} }
@@ -570,7 +571,7 @@ namespace nana
{ {
auto path = tree_.make_key_path(arg.item, "/") + "/"; auto path = tree_.make_key_path(arg.item, "/") + "/";
_m_resolute_path(path); _m_resolute_path(path);
_m_load_cat_path(path); _m_enter_folder(path);
} }
}); });
} }
@@ -630,7 +631,7 @@ namespace nana
std::sort(file_container_.begin(), file_container_.end(), pred_sort_fs()); std::sort(file_container_.begin(), file_container_.end(), pred_sort_fs());
} }
void _m_load_cat_path(std::string path) void _m_enter_folder(std::string path)
{ {
if((path.size() == 0) || (path[path.size() - 1] != '/')) if((path.size() == 0) || (path[path.size() - 1] != '/'))
path += '/'; path += '/';
@@ -763,7 +764,7 @@ namespace nana
{ {
auto m = cat.append(fs); auto m = cat.append(fs);
m.value(fs); m.value(fs);
if(fs.directory) if(fs.directory)
m.icon(images_.folder); m.icon(images_.folder);
else else
@@ -791,7 +792,7 @@ namespace nana
{ {
auto ext4 = filename.substr(size - 4); auto ext4 = filename.substr(size - 4);
if( (".exe" == ext4) || if( (".exe" == ext4) ||
(".dll" == ext4)) (".dll" == ext4))
use_image = images_.exec; use_image = images_.exec;
else if((".zip" == ext4) || (".rar" == ext4) || else if((".zip" == ext4) || (".rar" == ext4) ||
@@ -818,7 +819,7 @@ namespace nana
else if(".html" == ext5) else if(".html" == ext5)
use_image = images_.xml; use_image = images_.xml;
} }
if(use_image.empty()) if(use_image.empty())
m.icon(images_.file); m.icon(images_.file);
else else
@@ -875,7 +876,7 @@ namespace nana
return; return;
} }
fb_._m_load_cat_path(fb_.addr_.filesystem); fb_._m_enter_folder(fb_.addr_.filesystem);
fm_.close(); fm_.close();
} }
@@ -968,22 +969,28 @@ namespace nana
return false; return false;
} }
void _m_click_select_file(const arg_mouse& arg) //pos must be valid
item_fs _m_item_fs(const listbox::index_pair& pos) const
{
item_fs m;
ls_file_.at(pos).resolve_to(m);
return m;
}
void _m_list_dbl_clicked()
{ {
auto selected_item = ls_file_.hovered(false); auto selected_item = ls_file_.hovered(false);
if(selected_item.empty()) if(selected_item.empty())
return; return;
item_fs m; item_fs m = _m_item_fs(selected_item);
ls_file_.at(selected_item).resolve_to(m); if(m.directory)
if(event_code::dbl_click == arg.evt_code)
{ {
if(!m.directory) _m_enter_folder(addr_.filesystem + m.name + "/");
_m_try_select(m.name); allow_fall_back_ = true;
else
_m_load_cat_path(addr_.filesystem + m.name + "/");
} }
else
_m_try_select(m.name);
} }
void _m_select_file(const listbox::item_proxy& item) void _m_select_file(const listbox::item_proxy& item)
@@ -1076,19 +1083,27 @@ namespace nana
} }
return files; return files;
} }
void _m_msgbox(const char* i18n_idstr, const std::string& arg) const
{
internationalization i18n;
msgbox mb(*this, caption());
mb.icon(msgbox::icon_warning);
mb<<i18n(i18n_idstr, arg);
mb();
}
void _m_try_select(const std::string& files_text) void _m_try_select(const std::string& files_text)
{ {
selection_.targets.clear(); selection_.targets.clear();
//auto file = tb_file_.caption();
auto targets = _m_strip_files(files_text); auto targets = _m_strip_files(files_text);
if(targets.empty()) if(targets.empty())
{ {
//No file is selected if(mode::open_directory != mode_)
if(files_text.empty())
return; return;
targets.push_back(files_text); targets.push_back(files_text);
@@ -1105,10 +1120,7 @@ namespace nana
++tar_idx; ++tar_idx;
if(tar[0] == '.') if(tar[0] == '.')
{ {
msgbox mb(*this, caption()); _m_msgbox("NANA_FILEBOX_ERROR_INVALID_FILENAME", tar);
mb.icon(msgbox::icon_warning);
mb<<tar<<std::endl<<i18n("NANA_FILEBOX_ERROR_INVALID_FILENAME");
mb();
return; return;
} }
@@ -1129,51 +1141,82 @@ namespace nana
is_dir = fs::is_directory(fattr); is_dir = fs::is_directory(fattr);
} }
if(is_dir) if(mode::open_directory == mode_)
{ {
//Enter the directory if this is the first tar. //In open_directory mode, all the folders must be valid
if(0 == tar_idx) if(!is_dir)
{ {
_m_load_cat_path(tar); fs::path p{tar};
tb_file_.caption(std::string{}); auto p1 = p.filename();
return; auto p2 = p.parent_path().filename();
} if(allow_fall_back_ && (p.filename() == p.parent_path().filename()))
{
//Other folders are ignored //fallback check, and redirects to its parent path.
continue; tar = p.parent_path().string();
} }
if(mode::write_file != mode_)
{
if(fs::file_type::not_found == ftype)
{
msgbox mb(*this, caption());
mb.icon(msgbox::icon_information);
if(mode::open_file == mode_)
mb << i18n("NANA_FILEBOX_ERROR_NOT_EXISTING_AND_RETRY", tar);
else else
mb << i18n("NANA_FILEBOX_ERROR_DIRECTORY_NOT_EXISTING_AND_RETRY", tar); {
mb();
return; const char* i18n_idstr = "NANA_FILEBOX_ERROR_DIRECTORY_INVALID";
if(fs::file_type::not_found == ftype)
i18n_idstr = "NANA_FILEBOX_ERROR_DIRECTORY_NOT_EXISTING_AND_RETRY";
_m_msgbox(i18n_idstr, p.filename().string());
return;
}
} }
} }
else else
{ {
if(fs::file_type::not_found != ftype) //Enter the directory if this is the first tar.
if(is_dir)
{ {
msgbox mb(*this, caption(), msgbox::yes_no); if(0 == tar_idx)
mb.icon(msgbox::icon_question); {
mb<<i18n("NANA_FILEBOX_ERROR_QUERY_REWRITE_BECAUSE_OF_EXISTING"); _m_enter_folder(tar);
if(msgbox::pick_no == mb()) tb_file_.caption(std::string{});
return; return;
}
//Other folders are ignored
continue;
}
if(mode::write_file != mode_)
{
if(fs::file_type::not_found == ftype)
{
msgbox mb(*this, caption());
mb.icon(msgbox::icon_information);
if(mode::open_file == mode_)
mb << i18n("NANA_FILEBOX_ERROR_NOT_EXISTING_AND_RETRY", tar);
else
mb << i18n("NANA_FILEBOX_ERROR_DIRECTORY_NOT_EXISTING_AND_RETRY", tar);
mb();
return;
}
}
else
{
if(fs::file_type::not_found != ftype)
{
msgbox mb(*this, caption(), msgbox::yes_no);
mb.icon(msgbox::icon_question);
mb<<i18n("NANA_FILEBOX_ERROR_QUERY_REWRITE_BECAUSE_OF_EXISTING");
if(msgbox::pick_no == mb())
return;
}
} }
} }
auto pos = tar.find_last_not_of("\\/");
if(pos != tar.npos)
tar.erase(pos + 1);
selection_.targets.push_back(tar); selection_.targets.push_back(tar);
} }
_m_finish(kind::filesystem); _m_finish(kind::filesystem);
} }
void _m_tr_expand(item_proxy node, bool exp) void _m_tr_expand(item_proxy node, bool exp)
@@ -1239,6 +1282,7 @@ namespace nana
textbox tb_file_; textbox tb_file_;
combox cb_types_; combox cb_types_;
button btn_ok_, btn_cancel_; button btn_ok_, btn_cancel_;
bool allow_fall_back_{false};
struct tree_node_tag struct tree_node_tag
{ {
@@ -1292,7 +1336,7 @@ namespace nana
bool open_or_save; bool open_or_save;
bool allow_multi_select; bool allow_multi_select;
std::vector<std::string> files; std::string init_file;
std::string title; std::string title;
std::string path; std::string path;
@@ -1367,7 +1411,7 @@ namespace nana
filebox& filebox::init_file(const std::string& ifstr) filebox& filebox::init_file(const std::string& ifstr)
{ {
impl_->files = {ifstr}; impl_->init_file = ifstr;
return *this; return *this;
} }
@@ -1383,23 +1427,12 @@ namespace nana
return impl_->path; return impl_->path;
} }
std::string filebox::file() const std::vector<filebox::path_type> filebox::show() const
{ {
if(impl_->files.empty()) std::vector<path_type> targets;
return {};
return impl_->files.front();
}
const std::vector<std::string>& filebox::files() const
{
return impl_->files;
}
bool filebox::show() const
{
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
std::wstring wfile(impl_->files.empty() ? L"" : to_wstring(impl_->files.front())); std::wstring wfile = to_wstring(impl_->init_file);
wfile.resize(impl_->allow_multi_select ? (520 + 32*256) : 520); wfile.resize(impl_->allow_multi_select ? (520 + 32*256) : 520);
OPENFILENAME ofn; OPENFILENAME ofn;
@@ -1473,34 +1506,31 @@ namespace nana
{ {
internal_revert_guard revert; internal_revert_guard revert;
if (FALSE == (impl_->open_or_save ? ::GetOpenFileName(&ofn) : ::GetSaveFileName(&ofn))) if (FALSE == (impl_->open_or_save ? ::GetOpenFileName(&ofn) : ::GetSaveFileName(&ofn)))
return false; return targets;
} }
if(impl_->allow_multi_select) if(impl_->allow_multi_select)
{ {
wchar_t* str = ofn.lpstrFile; const wchar_t* str = ofn.lpstrFile;
std::wstring dir = str; auto len = ::wcslen(str);
str += (dir.length() + 1);
impl_->files.clear(); path_type parent_path{ str };
str += (len + 1);
while(*str) while(*str)
{ {
std::wstring filename = str; len = ::wcslen(str);
std::wstring file_path = dir + L"\\" + filename; targets.emplace_back(parent_path / path_type{str});
impl_->files.emplace_back(to_utf8(file_path)); str += (len + 1);
str += (filename.length() + 1);
} }
impl_->path = to_utf8(dir); impl_->path = parent_path.u8string();
} }
else else
{ {
wfile.resize(std::wcslen(wfile.data())); wfile.resize(std::wcslen(wfile.data()));
auto file = to_utf8(wfile);
impl_->files = {file}; targets.emplace_back(wfile);
auto tpos = file.find_last_of("\\/"); impl_->path = targets.front().parent_path().u8string();
if(tpos != file.npos)
impl_->path = file.substr(0, tpos);
else
impl_->path.clear();
} }
#elif defined(NANA_POSIX) #elif defined(NANA_POSIX)
@@ -1526,23 +1556,23 @@ namespace nana
else else
fb.add_filter("All Files", "*.*"); fb.add_filter("All Files", "*.*");
fb.load_fs(impl_->path, this->file()); fb.load_fs(impl_->path, impl_->init_file);
API::modal_window(fb); API::modal_window(fb);
fb.files().swap(impl_->files);
if(impl_->files.empty())
return false;
auto tpos = impl_->files.front().find_last_of("\\/"); for(auto & f : fb.files())
if(tpos != impl_->files.front().npos) targets.emplace_back(f);
impl_->path = impl_->files.front().substr(0, tpos);
if(!targets.empty())
impl_->path = targets.front().parent_path().u8string();
else else
impl_->path.clear(); impl_->path.clear();
#endif #endif
return true; return targets;
} }
void filebox::allow_multi_select(bool allow) void filebox::allow_multi_select(bool allow)
{ {
@@ -1556,11 +1586,11 @@ namespace nana
window owner; window owner;
path_type init_path; path_type init_path;
std::string title; std::string title;
bool allow_multi_select;
}; };
folderbox::folderbox(window owner, const path_type& init_path, std::string title) folderbox::folderbox(window owner, const path_type& init_path, std::string title)
: impl_(new implement{ owner, fs::canonical(init_path).make_preferred(), title }) : impl_(new implement{ owner, fs::canonical(init_path).make_preferred(), title, false})
{} {}
@@ -1596,14 +1626,19 @@ namespace nana
} }
#endif #endif
std::optional<folderbox::path_type> folderbox::show() const void folderbox::allow_multi_select(bool allow)
{ {
impl_->allow_multi_select = allow;
}
std::vector<folderbox::path_type> folderbox::show() const
{
std::vector<path_type> targets;
#ifdef NANA_WINDOWS #ifdef NANA_WINDOWS
std::optional<folderbox::path_type> target;
nana::detail::platform_spec::co_initializer co_init; nana::detail::platform_spec::co_initializer co_init;
#ifndef NANA_MINGW #ifndef NANA_MINGW
IFileDialog *fd(nullptr); IFileOpenDialog *fd(nullptr);
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fd)); HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fd));
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
@@ -1612,21 +1647,32 @@ namespace nana
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
fd->SetFolder(init_path); fd->SetFolder(init_path);
fd->SetOptions(FOS_PICKFOLDERS); fd->SetOptions(FOS_PICKFOLDERS | (impl_->allow_multi_select ? FOS_ALLOWMULTISELECT : 0));
fd->Show(reinterpret_cast<HWND>(API::root(impl_->owner))); // the native handle of the parent nana form goes here fd->Show(reinterpret_cast<HWND>(API::root(impl_->owner))); // the native handle of the parent nana form goes here
IShellItem *si;
hr = fd->GetResult(&si); // fails if user cancelled ::IShellItemArray *sia;
if (SUCCEEDED(hr)) if (SUCCEEDED(fd->GetResults(&sia))) // fails if user cancelled
{ {
PWSTR pwstr(nullptr); DWORD num_items;
hr = si->GetDisplayName(SIGDN_FILESYSPATH, &pwstr); if (SUCCEEDED(sia->GetCount(&num_items)))
if (SUCCEEDED(hr))
{ {
target = path_type{ pwstr }; for (DWORD i = 0; i < num_items; ++i)
// use the c-string pointed to by pwstr here {
CoTaskMemFree(pwstr); ::IShellItem* si;
if (SUCCEEDED(sia->GetItemAt(i, &si)))
{
PWSTR pwstr(nullptr);
if (SUCCEEDED(si->GetDisplayName(SIGDN_FILESYSPATH, &pwstr)))
{
targets.emplace_back(pwstr);
// use the c-string pointed to by pwstr here
::CoTaskMemFree(pwstr);
}
si->Release();
}
}
} }
si->Release(); sia->Release();
} }
fd->Release(); fd->Release();
} }
@@ -1649,27 +1695,25 @@ namespace nana
{ {
wchar_t folder_path[MAX_PATH]; wchar_t folder_path[MAX_PATH];
if (FALSE != SHGetPathFromIDList(pidl, folder_path)) if (FALSE != SHGetPathFromIDList(pidl, folder_path))
target = folder_path; targets.emplace_back(folder_path);
co_init.task_mem_free(pidl); co_init.task_mem_free(pidl);
} }
#endif #endif
return target;
#elif defined(NANA_POSIX) #elif defined(NANA_POSIX)
using mode = filebox_implement::mode; using mode = filebox_implement::mode;
filebox_implement fb(impl_->owner, mode::open_directory, {}, true, false/*single select*/); filebox_implement fb(impl_->owner, mode::open_directory, {}, true, impl_->allow_multi_select);
fb.load_fs(impl_->init_path, ""); fb.load_fs(impl_->init_path, "");
API::modal_window(fb); API::modal_window(fb);
std::string path_directory; auto path_dirs = fb.files();
auto path_dir = fb.files();
if(path_dir.empty())
return {};
return path_type{path_dir.front()}; for(auto & p: path_dirs)
targets.push_back(p);
#endif #endif
return targets;
} }
}//end namespace nana }//end namespace nana

View File

@@ -1228,9 +1228,10 @@ namespace nana
impl->browse.events().click.connect_unignorable([wd, impl](const arg_click&) impl->browse.events().click.connect_unignorable([wd, impl](const arg_click&)
{ {
impl->fbox.owner(wd); impl->fbox.owner(wd);
if (impl->fbox.show()) auto files = impl->fbox.show();
if(!files.empty())
{ {
impl->value = impl->fbox.file(); impl->value = files.front().u8string();
impl->path_edit.caption(impl->value); impl->path_edit.caption(impl->value);
} }
}); });

View File

@@ -193,9 +193,10 @@ namespace nana
table["NANA_FILEBOX_ERROR_INVALID_FOLDER_NAME"] = "Please input a valid name for the new folder."; table["NANA_FILEBOX_ERROR_INVALID_FOLDER_NAME"] = "Please input a valid name for the new folder.";
table["NANA_FILEBOX_ERROR_RENAME_FOLDER_BECAUSE_OF_EXISTING"] = "The folder is existing, please rename it."; table["NANA_FILEBOX_ERROR_RENAME_FOLDER_BECAUSE_OF_EXISTING"] = "The folder is existing, please rename it.";
table["NANA_FILEBOX_ERROR_RENAME_FOLDER_BECAUSE_OF_FAILED_CREATION"] = "Failed to create the folder, please rename it."; table["NANA_FILEBOX_ERROR_RENAME_FOLDER_BECAUSE_OF_FAILED_CREATION"] = "Failed to create the folder, please rename it.";
table["NANA_FILEBOX_ERROR_INVALID_FILENAME"] = "The filename is invalid."; table["NANA_FILEBOX_ERROR_INVALID_FILENAME"] = "\"%arg0\"\nThe filename is invalid.";
table["NANA_FILEBOX_ERROR_NOT_EXISTING_AND_RETRY"] = "The file \"%arg0\"\n is not existing. Please check and retry."; table["NANA_FILEBOX_ERROR_NOT_EXISTING_AND_RETRY"] = "The file \"%arg0\"\n is not existing. Please check and retry.";
table["NANA_FILEBOX_ERROR_DIRECTORY_NOT_EXISTING_AND_RETRY"] = "The directory \"%arg0\"\n is not existing. Please check and retry."; table["NANA_FILEBOX_ERROR_DIRECTORY_NOT_EXISTING_AND_RETRY"] = "The directory \"%arg0\"\n is not existing. Please check and retry.";
table["NANA_FILEBOX_ERROR_DIRECTORY_INVALID"] = "The directory \"%arg0\"\n is invalid. Please check and retry.";
table["NANA_FILEBOX_ERROR_QUERY_REWRITE_BECAUSE_OF_EXISTING"] = "The input file is existing, do you want to overwrite it?"; table["NANA_FILEBOX_ERROR_QUERY_REWRITE_BECAUSE_OF_EXISTING"] = "The input file is existing, do you want to overwrite it?";
} }
}; };