new folderbox class

This commit is contained in:
Jinhao 2018-06-20 01:09:56 +08:00
parent 69e6f4c4a4
commit c0836fbbec
2 changed files with 359 additions and 214 deletions

View File

@ -1,7 +1,7 @@
/**
* Filebox
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@ -15,6 +15,7 @@
#ifndef NANA_GUI_FILEBOX_HPP
#define NANA_GUI_FILEBOX_HPP
#include <nana/gui/basis.hpp>
#include <nana/filesystem/filesystem.hpp>
#include <vector>
#include <utility>
@ -80,5 +81,29 @@ namespace nana
private:
implement * impl_;
};
class folderbox
{
struct implement;
folderbox(const folderbox&) = delete;
folderbox& operator=(const folderbox&) = delete;
folderbox(folderbox&&) = delete;
folderbox& operator=(folderbox&&) = delete;
public:
using path_type = std::experimental::filesystem::path;
folderbox(window owner = nullptr, const path_type& init_path = {});
~folderbox();
std::optional<path_type> show() const;
std::optional<path_type> operator()() const
{
return show();
}
private:
implement* impl_;
};
}//end namespace nana
#endif

View File

@ -1,7 +1,7 @@
/*
* Filebox
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@ -15,18 +15,19 @@
#include <nana/filesystem/filesystem_ext.hpp>
#if defined(NANA_WINDOWS)
#include <windows.h>
# include <windows.h>
# include <Shobjidl.h>
#elif defined(NANA_POSIX)
#include <nana/gui/widgets/label.hpp>
#include <nana/gui/widgets/button.hpp>
#include <nana/gui/widgets/listbox.hpp>
#include <nana/gui/widgets/categorize.hpp>
#include <nana/gui/widgets/textbox.hpp>
#include <nana/gui/widgets/treebox.hpp>
#include <nana/gui/widgets/combox.hpp>
#include <nana/gui/place.hpp>
#include <stdexcept>
#include <algorithm>
# include <nana/gui/widgets/label.hpp>
# include <nana/gui/widgets/button.hpp>
# include <nana/gui/widgets/listbox.hpp>
# include <nana/gui/widgets/categorize.hpp>
# include <nana/gui/widgets/textbox.hpp>
# include <nana/gui/widgets/treebox.hpp>
# include <nana/gui/widgets/combox.hpp>
# include <nana/gui/place.hpp>
# include <stdexcept>
# include <algorithm>
#endif
namespace fs = std::experimental::filesystem;
@ -130,6 +131,13 @@ namespace nana
}
};
public:
enum class mode
{
open_file, //choose an existing file.
write_file, //choose a filename, it can be specified a new filename which doesn't exist.
open_directory, //choose an existing directory.
};
struct kind
{
enum t{none, filesystem};
@ -138,8 +146,10 @@ namespace nana
typedef treebox::item_proxy item_proxy;
public:
filebox_implement(window owner, bool io_read, const std::string& title)
: form(owner, API::make_center(owner, 630, 440)), io_read_(io_read)
filebox_implement(window owner, mode dialog_mode, const std::string& title, bool pick_directory = false):
form(owner, API::make_center(owner, 630, 440)),
pick_directory_(pick_directory),
mode_(dialog_mode)
{
internationalization i18n;
path_.create(*this);
@ -159,6 +169,7 @@ namespace nana
_m_load_cat_path(path);
});
filter_.create(*this);
filter_.multi_lines(false);
filter_.tip_string(i18n("NANA_FILEBOX_FILTER"));
@ -203,7 +214,7 @@ namespace nana
ls_file_.append_header(i18n("NANA_FILEBOX_HEADER_SIZE"), 70);
auto fn_sel_file = [this](const arg_mouse& arg){
_m_sel_file(arg);
_m_select_file(arg);
};
ls_file_.events().dbl_click.connect_unignorable(fn_sel_file);
ls_file_.events().mouse_down.connect_unignorable(fn_sel_file);
@ -278,7 +289,11 @@ namespace nana
});
lb_file_.create(*this);
lb_file_.i18n(i18n_eval("NANA_FILEBOX_FILE_COLON"));
const char* idstr = (mode::open_directory == dialog_mode? "NANA_FILEBOX_DIRECTORY_COLON" : "NANA_FILEBOX_FILE_COLON");
lb_file_.i18n(i18n_eval(idstr));
lb_file_.text_align(align::right, align_v::center);
tb_file_.create(*this);
tb_file_.multi_lines(false);
@ -289,9 +304,13 @@ namespace nana
_m_ok();
});
//Don't create the combox for choose a file extension if the dialog is used for picking a directory.
if(!pick_directory)
{
cb_types_.create(*this);
cb_types_.editable(false);
cb_types_.events().selected.connect_unignorable([this](const arg_combox&){ _m_list_fs(); });
}
btn_ok_.create(*this);
btn_ok_.i18n(i18n_eval("NANA_BUTTON_OK_SHORTKEY"));
@ -313,8 +332,23 @@ namespace nana
_m_layout();
_m_init_tree();
if(0 == title.size())
this->i18n(i18n_eval(io_read ? "NANA_FILEBOX_OPEN" : "NANA_FILEBOX_SAVE_AS"));
if(title.empty())
{
const char* idstr{ nullptr };
switch(dialog_mode)
{
case mode::open_file:
idstr = "NANA_FILEBOX_OPEN";
break;
case mode::write_file:
idstr = "NANA_FILEBOX_SAVE_AS";
break;
case mode::open_directory:
idstr = "NANA_FILEBOX_OPEN_DIRECTORY";
break;
}
this->i18n(i18n_eval(idstr));
}
else
caption(title);
}
@ -412,16 +446,21 @@ namespace nana
"<weight=34 margin=5 path arrange=[variable,200] gap=5>"
"<weight=30 margin=[0,0,5,10] new_folder arrange=[100]>"
"<content arrange=[180] gap=[5]><weight=8>"
"<weight=26<weight=100><vert weight=60 label margin=[0,0,0,5]>"
"<weight=26<weight=100><vert weight=80 label margin=[0,5,0,0]>"
"<file margin=[0,18,0,5] arrange=[variable,variable,190] gap=[10]>>"
"<weight=48 margin=[8,0,14]<>"
"<buttons weight=208 margin=[0,18,0] gap=[14]>>");
place_.field("path")<<path_<<filter_;
place_.field("new_folder")<<btn_folder_;
place_.field("content")<<tree_<<ls_file_;
place_.field("label")<<lb_file_;
place_.field("file")<<tb_file_<<cb_types_;
place_.field("file")<<tb_file_;
if(!pick_directory_)
place_.field("file")<<cb_types_;
place_.field("buttons")<<btn_ok_<<btn_cancel_;
place_.collocate();
}
@ -630,6 +669,9 @@ namespace nana
if(filter.size() && (name.find(filter) == filter.npos))
return false;
if(pick_directory_)
return is_dir;
if((is_dir || 0 == extension) || (0 == extension->size())) return true;
for(auto & extstr : *extension)
@ -744,7 +786,7 @@ namespace nana
return true;
}
private:
void _m_sel_file(const arg_mouse& arg)
void _m_select_file(const arg_mouse& arg)
{
auto sel = ls_file_.selected();
if(sel.empty())
@ -763,7 +805,7 @@ namespace nana
}
else
{
if(false == m.directory)
if((mode::open_directory == mode_) || (false == m.directory))
{
selection_.target = addr_.filesystem + m.name;
tb_file_.caption(m.name);
@ -816,13 +858,16 @@ namespace nana
return;
}
if(io_read_)
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;
@ -892,7 +937,8 @@ namespace nana
}
}
private:
bool io_read_;
bool const pick_directory_;
mode mode_;
std::string def_ext_;
place place_;
@ -1118,7 +1164,8 @@ namespace nana
wfile.resize(std::wcslen(wfile.data()));
impl_->file = to_utf8(wfile);
#elif defined(NANA_POSIX)
filebox_implement fb(impl_->owner, impl_->open_or_save, impl_->title);
using mode = filebox_implement::mode;
filebox_implement fb(impl_->owner, (impl_->open_or_save ? mode::open_file : mode::write_file), impl_->title);
if(impl_->filters.size())
{
@ -1153,4 +1200,77 @@ namespace nana
return true;
}//end class filebox
//class directory_picker
struct folderbox::implement
{
window owner;
path_type init_path;
};
folderbox::folderbox(window owner, const path_type& init_path)
: impl_(new implement)
{
impl_->owner = owner;
impl_->init_path = init_path;
}
folderbox::~folderbox()
{
delete impl_;
}
std::optional<folderbox::path_type> folderbox::show() const
{
#ifdef NANA_WINDOWS
path_type target;
CoInitialize(NULL);
IFileDialog *fd(nullptr);
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fd));
if (SUCCEEDED(hr))
{
IShellItem *init_path{ nullptr };
hr = SHCreateItemFromParsingName(impl_->init_path.wstring().c_str(), nullptr, IID_PPV_ARGS(&init_path));
if (SUCCEEDED(hr))
fd->SetDefaultFolder(init_path);
fd->SetOptions(FOS_PICKFOLDERS);
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
if (SUCCEEDED(hr))
{
PWSTR pwstr(nullptr);
hr = si->GetDisplayName(SIGDN_FILESYSPATH, &pwstr);
if (SUCCEEDED(hr))
{
target = pwstr;
// use the c-string pointed to by pwstr here
CoTaskMemFree(pwstr);
}
si->Release();
}
fd->Release();
}
CoUninitialize();
return target;
#elif defined(NANA_POSIX)
using mode = filebox_implement::mode;
filebox_implement fb(impl_->owner, mode::open_directory, {}, true);
fb.load_fs(impl_->init_path, "");
API::modal_window(fb);
std::string path_directory;
if(false == fb.file(path_directory))
return {};
return path_type{path_directory};
#endif
}
}//end namespace nana