From f0f3f094c5fa05bafe1963d1ea13f1be39a6673b Mon Sep 17 00:00:00 2001 From: B Stack Date: Sun, 17 May 2020 11:17:28 -0400 Subject: add upstream 10.24 --- wx+/choice_enum.h | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'wx+/choice_enum.h') diff --git a/wx+/choice_enum.h b/wx+/choice_enum.h index f2c93927..e11b9991 100644 --- a/wx+/choice_enum.h +++ b/wx+/choice_enum.h @@ -7,6 +7,7 @@ #ifndef CHOICE_ENUM_H_132413545345687 #define CHOICE_ENUM_H_132413545345687 +#include #include #include @@ -43,8 +44,11 @@ struct EnumDescrList descrList.push_back({ value, { text, tooltip } }); return *this; } + using DescrList = std::vector>>; DescrList descrList; + + std::unordered_map> itemsSetLast; }; template void setEnumVal(const EnumDescrList& mapping, wxChoice& ctrl, Enum value); template Enum getEnumVal(const EnumDescrList& mapping, const wxChoice& ctrl); @@ -65,26 +69,34 @@ template void updateTooltipEnumVal(const EnumDescrList& mappi //--------------- impelementation ------------------------------------------- template -void setEnumVal(const EnumDescrList& mapping, wxChoice& ctrl, Enum value) +void setEnumVal(EnumDescrList& mapping, wxChoice& ctrl, Enum value) { - ctrl.Clear(); + auto& itemsSetLast = mapping.itemsSetLast[&ctrl]; - int selectedPos = 0; + std::vector items; for (auto it = mapping.descrList.begin(); it != mapping.descrList.end(); ++it) + items.push_back(it->second.first); + + if (items != itemsSetLast) { - ctrl.Append(it->second.first); - if (it->first == value) - { - selectedPos = it - mapping.descrList.begin(); - - if (it->second.second.empty()) - ctrl.UnsetToolTip(); - else - ctrl.SetToolTip(it->second.second); - } + ctrl.Set(items); //expensive as fuck! => only call when absolutely needed! + itemsSetLast = std::move(items); } + //----------------------------------------------------------------- - ctrl.SetSelection(selectedPos); + const auto it = std::find_if(mapping.descrList.begin(), mapping.descrList.end(), [&](const auto& mapItem) { return mapItem.first == value; }); + if (it != mapping.descrList.end()) + { + if (const wxString& tooltip = it->second.second; + !tooltip.empty()) + ctrl.SetToolTip(tooltip); + else + ctrl.UnsetToolTip(); + + const int selectedPos = it - mapping.descrList.begin(); + ctrl.SetSelection(selectedPos); + } + else assert(false); } template @@ -103,11 +115,17 @@ Enum getEnumVal(const EnumDescrList& mapping, const wxChoice& ctrl) template void updateTooltipEnumVal(const EnumDescrList& mapping, wxChoice& ctrl) { - const Enum currentValue = getEnumVal(mapping, ctrl); + const int selectedPos = ctrl.GetSelection(); - for (const auto& [enumValue, textAndTooltip] : mapping.descrList) - if (currentValue == enumValue) - ctrl.SetToolTip(textAndTooltip.second); + if (0 <= selectedPos && selectedPos < static_cast(mapping.descrList.size())) + { + if (const auto& [text, tooltip] = mapping.descrList[selectedPos].second; + !tooltip.empty()) + ctrl.SetToolTip(tooltip); + else + ctrl.UnsetToolTip(); + } + else assert(false); } } -- cgit