diff --git a/src/dialogxml/dialogs/strchoice.cpp b/src/dialogxml/dialogs/strchoice.cpp index ed5dac34..a95f0e01 100644 --- a/src/dialogxml/dialogs/strchoice.cpp +++ b/src/dialogxml/dialogs/strchoice.cpp @@ -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& 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::iterator begin, std::vector::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); +} diff --git a/src/dialogxml/dialogs/strchoice.hpp b/src/dialogxml/dialogs/strchoice.hpp index f11e9163..c0ad1178 100644 --- a/src/dialogxml/dialogs/strchoice.hpp +++ b/src/dialogxml/dialogs/strchoice.hpp @@ -31,6 +31,7 @@ class cStringChoice { size_t page, cur; cLedGroup* leds; std::function 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::iterator begin, std::vector::iterator end, std::string title, cDialog* parent = nullptr); + /// @tparam Iter The iterator type + template + 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 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