From 9cb4d8fbbe3ffaa67bc08ed7ac6ddb6bdf99df84 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek <7330332+a-wi@users.noreply.github.com> Date: Fri, 8 Sep 2023 20:49:11 +0200 Subject: [PATCH] Save keyboard actions as std::pair in wxPropertyGrid Store two possible action codes in std::pair instead of packing them into one int value with bitmasks. This reduces level of indirection in accessing keyboard actions and make the code more maintainable. --- include/wx/propgrid/propgrid.h | 3 +-- interface/wx/propgrid/propgrid.h | 2 +- src/propgrid/propgrid.cpp | 38 +++++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/include/wx/propgrid/propgrid.h b/include/wx/propgrid/propgrid.h index 85b909958e..cfaa02a408 100644 --- a/include/wx/propgrid/propgrid.h +++ b/include/wx/propgrid/propgrid.h @@ -604,7 +604,6 @@ public: virtual void Clear() override; // Clears action triggers for given action. - // action - Which action to trigger. void ClearActionTriggers( int action ); // Forces updating the value of property from the editor control. @@ -1479,7 +1478,7 @@ protected: wxPGValidationInfo m_validationInfo; // Actions and keys that trigger them. - std::unordered_map m_actionTriggers; + std::unordered_map> m_actionTriggers; // Appearance of currently active editor. wxPGCell m_editorAppearance; diff --git a/interface/wx/propgrid/propgrid.h b/interface/wx/propgrid/propgrid.h index bad9331534..496cb8f000 100644 --- a/interface/wx/propgrid/propgrid.h +++ b/interface/wx/propgrid/propgrid.h @@ -604,7 +604,7 @@ public: Clears action triggers for given action. @param action - Which action to trigger. @ref propgrid_keyboard_actions. + Which action to clear. @ref propgrid_keyboard_actions. */ void ClearActionTriggers( int action ); diff --git a/src/propgrid/propgrid.cpp b/src/propgrid/propgrid.cpp index 11cc1d5495..d962855e1e 100644 --- a/src/propgrid/propgrid.cpp +++ b/src/propgrid/propgrid.cpp @@ -5542,8 +5542,7 @@ std::pair wxPropertyGrid::KeyEventToActions(const wxKeyEvent& event) c if ( it == m_actionTriggers.end() ) return std::make_pair(0, 0); - wxInt32 actions = it->second; - return std::make_pair(actions & 0xFFFF, (actions >> 16) & 0xFFFF); + return it->second; } #if WXWIN_COMPATIBILITY_3_2 @@ -5572,35 +5571,52 @@ void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers ) int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16); - auto it = m_actionTriggers.find(hashMapKey); + std::pair curActions; + auto it = m_actionTriggers.find(hashMapKey); if ( it != m_actionTriggers.end() ) { // This key combination is already used + curActions = it->second; // Can add secondary? - wxASSERT_MSG( !(it->second&~(0xFFFF)), + wxASSERT_MSG( curActions.second == 0, wxS("You can only add up to two separate actions per key combination.") ); - action = it->second | (action<<16); + curActions.second = action; + } + else + { + curActions = std::make_pair(action, 0); } - m_actionTriggers[hashMapKey] = action; + m_actionTriggers[hashMapKey] = curActions; } -void wxPropertyGrid::ClearActionTriggers( int action ) +void wxPropertyGrid::ClearActionTriggers(int action) { + wxCHECK_RET(!(action & ~(0xFFFF)), wxS("You can only clear triggers for one action at a time.") + auto it = m_actionTriggers.begin(); while ( it != m_actionTriggers.end() ) { - if ( it->second == action ) + if ( it->second.second == action ) { - it = m_actionTriggers.erase(it); + it->second.second = 0; } - else + + if ( it->second.first == action ) { - ++it; + if ( it->second.second == 0 ) + { + it = m_actionTriggers.erase(it); + continue; + } + + it->second.first = it->second.second; } + + ++it; } }