From d161252514c3bbc5a3eb1b79ab6b1e71ba17a99c Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 19 Jun 2025 15:36:14 -0500 Subject: [PATCH] fix stack widgets calling focus handlers during construction --- src/dialogxml/widgets/stack.cpp | 38 +++++++++++++++++---------------- src/dialogxml/widgets/stack.hpp | 2 +- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/dialogxml/widgets/stack.cpp b/src/dialogxml/widgets/stack.cpp index 125cca24..249b9a3c 100644 --- a/src/dialogxml/widgets/stack.cpp +++ b/src/dialogxml/widgets/stack.cpp @@ -59,24 +59,26 @@ void cStack::draw() { drawFrame(2, frameStyle); } -bool cStack::setPage(size_t n) { +bool cStack::setPage(size_t n, bool call_focus_handlers) { if(n >= nPages) return false; if(n == curPage) return true; cTextField* focus = getDialog()->getFocus(); bool failed = false; - for(auto p : controls) { - const std::string& id = p.first; - cControl& ctrl = *p.second; - // Only trigger focus handlers if the current page still exists. - if(curPage < nPages) { - storage[curPage][id] = ctrl.store(); - if(!ctrl.triggerFocusHandler(*getDialog(), id, true)) - failed = true; - if(!failed) { - ctrl.restore(storage[n][id]); - if(focus == &ctrl && !ctrl.triggerFocusHandler(*getDialog(), id, false)) { + if(call_focus_handlers){ + for(auto p : controls) { + const std::string& id = p.first; + cControl& ctrl = *p.second; + // Only trigger focus handlers if the current page still exists. + if(curPage < nPages) { + storage[curPage][id] = ctrl.store(); + if(!ctrl.triggerFocusHandler(*getDialog(), id, true)) failed = true; - ctrl.restore(storage[curPage][id]); + if(!failed) { + ctrl.restore(storage[n][id]); + if(focus == &ctrl && !ctrl.triggerFocusHandler(*getDialog(), id, false)) { + failed = true; + ctrl.restore(storage[curPage][id]); + } } } } @@ -102,7 +104,7 @@ size_t cStack::getPage() const { void cStack::setPageCount(size_t n) { if(curPage >= n && n > 0) - setPage(nPages - 1); + setPage(nPages - 1, false); auto added = n - nPages; if(n == 0) curPage = 0; nPages = n; @@ -110,10 +112,10 @@ void cStack::setPageCount(size_t n) { if(added > 0 && defaultTemplate < storage.size()) { auto saveCur = curPage; for(int i = n - added; i < nPages; i++) { - setPage(i); + setPage(i, false); applyTemplate(defaultTemplate); } - setPage(saveCur); + setPage(saveCur, false); } } @@ -235,10 +237,10 @@ void cStack::validatePostParse(ticpp::Element&, std::string, const std::set setPageCount(std::max(nPages, templates.size())); for(size_t i = 0; i < nPages; i++) { - setPage(i); + setPage(i, false); applyTemplate(i); } - setPage(0); + setPage(0, false); } void cStack::applyTemplate(size_t n) { diff --git a/src/dialogxml/widgets/stack.hpp b/src/dialogxml/widgets/stack.hpp index c6d491e3..594fbfb3 100644 --- a/src/dialogxml/widgets/stack.hpp +++ b/src/dialogxml/widgets/stack.hpp @@ -56,7 +56,7 @@ public: /// You need to do this before retrieving data from that page. /// @param The new page number /// @return false if the page could not be changed, usually due to a focus handler - bool setPage(size_t n); + bool setPage(size_t n, bool call_focus_handlers = true); /// Page forward or backward in the stack /// @param dir Usually -1 or 1 /// @param loop Beyond the first and last page, loop to the other side