refactor filebox and folderbox

This commit is contained in:
Jinhao 2019-02-28 07:38:13 +08:00
parent 29cee29f5f
commit 71f57bbf92
2 changed files with 57 additions and 51 deletions

View File

@ -28,10 +28,8 @@ namespace nana
filebox(filebox&&) = delete;
filebox& operator=(filebox&&) = delete;
public:
using filters = std::vector<std::pair< ::std::string, ::std::string>>;
using path_type = std::filesystem::path;
explicit filebox(bool is_open_mode);
filebox(window owner, bool is_open_mode);
filebox(const filebox&);
~filebox();
@ -39,20 +37,35 @@ namespace nana
filebox& operator=(const filebox&);
/// Change owner window
void owner(window);
/// Set a new title for the dialog
/// @param string a text for title
/// @return the old title.
::std::string title( ::std::string new_title);
/** @brief Suggest initial path used to locate a directory when the filebox starts.
* @param string initial_directory a path of initial directory
* @note the behavior of init_path is different between Win7 and Win2K/XP/Vista, but its behavior under Linux is conformed with Win7.
/**
* Changes the owner window for the filebox. When #show()/operator()# are invoked, the dialog of filebox will be created with the specified owner.
* @param handle A handle to a window which will be used for the owner of filebox
*/
filebox& init_path(const ::std::string& initial_directory);
void owner(window handle);
filebox& init_file(const ::std::string&); ///< Init file, if it contains a path, the init path is replaced by the path of init file.
/// Changes new title
/**
* Changes the title. When #show()/operator()# are invoked, the dialog of filebox will be created with the specified title.
* @param text Text of title
*/
void title( ::std::string text);
/// Sets a initial path
/**
* Suggest initial path used to locate a directory when the filebox starts.
* @note the behavior of init_path is different between Win7 and Win2K/XP/Vista, but its behavior under Linux is conformed with Win7.
* @param path a path of initial directory
* @return reference of *this.
*/
filebox& init_path(const path_type& path);
/// Sets a initial filename
/**
* Suggest a filename when filebox starts. If the filename contains a path, the initial path will be replaced with the path presents in initial filename.
* @param filename a filename used for a initial filename when filebox starts.
* @return reference of *this.
*/
filebox& init_file(const ::std::string& filename); ///< Init file, if it contains a path, the init path is replaced by the path of init file.
/// \brief Add a filetype filter.
/// To specify multiple filter in a single description, use a semicolon to separate the patterns(for example,"*.TXT;*.DOC;*.BAK").
@ -60,17 +73,11 @@ namespace nana
const ::std::string& filetype ///< filter pattern(for example: "*.TXT")
);
filebox& add_filter(const filters &ftres)
{
for (auto &f : ftres)
add_filter(f.first, f.second);
return *this;
};
filebox& add_filter(const std::vector<std::pair<std::string, std::string>> &filters);
const ::std::string& path() const;
const path_type& path() const;
void allow_multi_select(bool allow);
filebox& allow_multi_select(bool allow);
/// Display the filebox dialog
std::vector<path_type> show() const;
@ -99,7 +106,7 @@ namespace nana
~folderbox();
/// Enables/disables multi select
void allow_multi_select(bool allow);
folderbox& allow_multi_select(bool allow);
std::vector<path_type> show() const;
@ -108,10 +115,11 @@ namespace nana
return show();
}
/// Set a new title for the dialog
/// @param string a text for title
/// @return the old title.
::std::string title( ::std::string new_title);
/// Changes title
/**
* @param text Text of title
*/
folderbox& title(std::string text);
private:
implement* impl_;
};

View File

@ -1339,15 +1339,10 @@ namespace nana
std::string init_file;
std::string title;
std::string path;
path_type path;
std::vector<filter> filters;
};
filebox::filebox(bool is_openmode)
: filebox(nullptr, is_openmode)
{
}
filebox::filebox(window owner, bool open)
: impl_(new implement)
{
@ -1389,23 +1384,17 @@ namespace nana
impl_->owner = wd;
}
std::string filebox::title(std::string s)
void filebox::title(std::string s)
{
impl_->title.swap(s);
return s;
}
filebox& filebox::init_path(const std::string& ipstr)
filebox& filebox::init_path(const path_type& p)
{
if(ipstr.empty())
{
impl_->path.clear();
}
else
{
if (fs::is_directory(ipstr))
impl_->path = ipstr;
}
std::error_code err;
if (p.empty() || is_directory(p, err))
impl_->path = p;
return *this;
}
@ -1422,7 +1411,14 @@ namespace nana
return *this;
}
const std::string& filebox::path() const
filebox& filebox::add_filter(const std::vector<std::pair<std::string, std::string>> &filters)
{
for (auto &f : filters)
add_filter(f.first, f.second);
return *this;
}
const filebox::path_type& filebox::path() const
{
return impl_->path;
}
@ -1574,9 +1570,10 @@ namespace nana
}
void filebox::allow_multi_select(bool allow)
filebox& filebox::allow_multi_select(bool allow)
{
impl_->allow_multi_select = allow;
return *this;
}
//end class filebox
@ -1600,10 +1597,10 @@ namespace nana
}
std::string folderbox::title(std::string s)
folderbox& folderbox::title(std::string s)
{
impl_->title.swap(s);
return s;
return *this;
}
@ -1626,9 +1623,10 @@ namespace nana
}
#endif
void folderbox::allow_multi_select(bool allow)
folderbox& folderbox::allow_multi_select(bool allow)
{
impl_->allow_multi_select = allow;
return *this;
}
std::vector<folderbox::path_type> folderbox::show() const