Templatize cStringChoice iterator constructor

This commit is contained in:
2025-02-22 23:24:01 -05:00
committed by Celtic Minstrel
parent 719f7dcf47
commit 58890f1b57
2 changed files with 20 additions and 15 deletions

View File

@@ -15,26 +15,18 @@
#include "fileio/resmgr/res_dialog.hpp"
static DialogDefn& loadDefn() {
return *ResMgr::dialogs.get("choose-string");
}
cStringChoice::cStringChoice(cDialog* parent)
: dlg(*ResMgr::dialogs.get("choose-string"), parent)
{}
cStringChoice::cStringChoice(std::vector<std::string>& strs, std::string title, cDialog* parent)
: dlg(loadDefn(),parent)
: cStringChoice(parent)
{
if(!title.empty()) dlg["title"].setText(title);
setTitle(title);
strings = strs;
attachHandlers();
}
cStringChoice::cStringChoice(std::vector<std::string>::iterator begin, std::vector<std::string>::iterator end, std::string title, cDialog* parent)
: dlg(loadDefn(),parent)
{
if(!title.empty()) dlg["title"].setText(title);
copy(begin,end,std::inserter(strings, strings.begin()));
attachHandlers();
}
void cStringChoice::attachHandlers() {
using namespace std::placeholders;
dlg["left"].attachClickHandler(std::bind(&cStringChoice::onLeft,this));
@@ -122,3 +114,7 @@ bool cStringChoice::onSelect(bool losing) {
select_handler(*this, cur);
return true;
}
void cStringChoice::setTitle(const std::string &title) {
if(!title.empty()) dlg["title"].setText(title);
}

View File

@@ -31,6 +31,7 @@ class cStringChoice {
size_t page, cur;
cLedGroup* leds;
std::function<void(cStringChoice&,int)> select_handler;
cStringChoice(cDialog* parent);
public:
/// Initializes a dialog from a list of strings.
/// @param strs A list of all strings in the dialog.
@@ -42,8 +43,13 @@ public:
/// @param end An iterator to one past the last string in the dialog.
/// @param title The title to show in the dialog.
/// @param parent Optionally, a parent dialog.
/// @note Currently, only vector iterators are supported.
cStringChoice(std::vector<std::string>::iterator begin, std::vector<std::string>::iterator end, std::string title, cDialog* parent = nullptr);
/// @tparam Iter The iterator type
template<typename Iter>
cStringChoice(Iter begin, Iter end, std::string title, cDialog* parent = nullptr) : cStringChoice(parent) {
setTitle(title);
std::copy(begin, end, std::back_inserter(strings));
attachHandlers();
}
/// Attach a handler to be called when the selected item changes.
/// @param f A function that takes a reference to the dialog and the index of the newly selected item.
void attachSelectHandler(std::function<void(cStringChoice&,int)> f);
@@ -56,6 +62,9 @@ public:
/// selectedIndex you provide. (So, pass -1 or something to signify that cancelling means the result is invalid.)
/// If initialized from an iterator range, this will be relative to begin.
size_t show(size_t selectedIndex);
/// Set the dialog's title.
/// @param title The new title.
void setTitle(const std::string& title);
};
#endif