summaryrefslogtreecommitdiff
path: root/wx+/choice_enum.h
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2020-05-17 17:14:47 +0000
committerB Stack <bgstack15@gmail.com>2020-05-17 17:14:47 +0000
commitcca3f71f16f85f5d506bf4bb7b3ec38fda31516a (patch)
tree1f52055b2f26fc2389d3ab4eb8d8d1e234a6316a /wx+/choice_enum.h
parentMerge branch '10.23' into 'master' (diff)
parentadd upstream 10.24 (diff)
downloadFreeFileSync-cca3f71f16f85f5d506bf4bb7b3ec38fda31516a.tar.gz
FreeFileSync-cca3f71f16f85f5d506bf4bb7b3ec38fda31516a.tar.bz2
FreeFileSync-cca3f71f16f85f5d506bf4bb7b3ec38fda31516a.zip
Merge branch '10.24' into 'master'
add upstream 10.24 See merge request opensource-tracking/FreeFileSync!21
Diffstat (limited to 'wx+/choice_enum.h')
-rw-r--r--wx+/choice_enum.h54
1 files changed, 36 insertions, 18 deletions
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 <unordered_map>
#include <vector>
#include <wx/choice.h>
@@ -43,8 +44,11 @@ struct EnumDescrList
descrList.push_back({ value, { text, tooltip } });
return *this;
}
+
using DescrList = std::vector<std::pair<Enum, std::pair<wxString, wxString>>>;
DescrList descrList;
+
+ std::unordered_map<const wxChoice*, std::vector<wxString>> itemsSetLast;
};
template <class Enum> void setEnumVal(const EnumDescrList<Enum>& mapping, wxChoice& ctrl, Enum value);
template <class Enum> Enum getEnumVal(const EnumDescrList<Enum>& mapping, const wxChoice& ctrl);
@@ -65,26 +69,34 @@ template <class Enum> void updateTooltipEnumVal(const EnumDescrList<Enum>& mappi
//--------------- impelementation -------------------------------------------
template <class Enum>
-void setEnumVal(const EnumDescrList<Enum>& mapping, wxChoice& ctrl, Enum value)
+void setEnumVal(EnumDescrList<Enum>& mapping, wxChoice& ctrl, Enum value)
{
- ctrl.Clear();
+ auto& itemsSetLast = mapping.itemsSetLast[&ctrl];
- int selectedPos = 0;
+ std::vector<wxString> 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 <class Enum>
@@ -103,11 +115,17 @@ Enum getEnumVal(const EnumDescrList<Enum>& mapping, const wxChoice& ctrl)
template <class Enum> void updateTooltipEnumVal(const EnumDescrList<Enum>& 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<int>(mapping.descrList.size()))
+ {
+ if (const auto& [text, tooltip] = mapping.descrList[selectedPos].second;
+ !tooltip.empty())
+ ctrl.SetToolTip(tooltip);
+ else
+ ctrl.UnsetToolTip();
+ }
+ else assert(false);
}
}
bgstack15