[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 0e2a694 16/46: Replace DatumSequenceEditorEvt
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 0e2a694 16/46: Replace DatumSequenceEditorEvtHandler with a lambda |
Date: |
Wed, 22 Jul 2020 11:05:11 -0400 (EDT) |
branch: master
commit 0e2a6944e80095215aba8c18b1e8674070be5706
Author: Vadim Zeitlin <vadim@tt-solutions.com>
Commit: Vadim Zeitlin <vadim@tt-solutions.com>
Replace DatumSequenceEditorEvtHandler with a lambda
There is no need to define a class just in order to bind a single event
handler, this can be done much more easily and concisely by using Bind()
with a lambda.
The only non-obvious part is that we have to call Bind() on the extra
event handler used for the grid editors and not the control itself, so
add a comment explaining it.
---
census_view.cpp | 85 ++++++++++++++++++++++++---------------------------------
1 file changed, 35 insertions(+), 50 deletions(-)
diff --git a/census_view.cpp b/census_view.cpp
index 577f9a6..8f29629 100644
--- a/census_view.cpp
+++ b/census_view.cpp
@@ -1003,52 +1003,11 @@ class RangeDateEditor
}
};
-// class DatumSequenceEditor
-
-class DatumSequenceEditorEvtHandler
- :public wxEvtHandler
-{
- public:
- explicit DatumSequenceEditorEvtHandler(InputSequenceEntry* entry)
- :entry_(entry)
- {
- Bind(wxEVT_KEY_DOWN, &DatumSequenceEditorEvtHandler::UponKeyDown,
this);
- }
-
- void UponKeyDown(wxKeyEvent& event)
- {
- switch(event.GetKeyCode())
- {
- case WXK_RETURN:
- case WXK_NUMPAD_ENTER:
- if(!wxGetKeyState(WXK_ALT))
- {
- event.Skip();
- return;
- }
-
- // Open the editor window when Alt-Enter is pressed instead of
- // just closing the editor, as would be done by default.
- entry_->open_editor();
- break;
- default:
- event.Skip();
- break;
- }
- }
-
- private:
- InputSequenceEntry* const entry_{};
-
- DECLARE_NO_COPY_CLASS(DatumSequenceEditorEvtHandler)
-};
-
class DatumSequenceEditor
:public wxGridCellEditor
{
public:
DatumSequenceEditor() = default;
- ~DatumSequenceEditor();
// We don't define a copy ctor because wxGridCellEditor doesn't provide one
// and its derived classes are supposed to override Clone() to support
@@ -1082,24 +1041,50 @@ class DatumSequenceEditor
std::string field_;
};
-DatumSequenceEditor::~DatumSequenceEditor()
-{
- // Pop the instance of DatumSequenceEditorEvtHandler.
- if(m_control)
- m_control->PopEventHandler(true /* delete it*/);
-}
-
void DatumSequenceEditor::Create
(wxWindow* parent
,wxWindowID id
,wxEvtHandler* evtHandler
)
{
- m_control = new(wx) InputSequenceEntry(parent, id, "sequence_editor");
+ auto* const entry = new(wx) InputSequenceEntry(parent, id,
"sequence_editor");
+ m_control = entry;
wxGridCellEditor::Create(parent, id, evtHandler);
- m_control->PushEventHandler(new(wx)
DatumSequenceEditorEvtHandler(Entry()));
+ // When an event handler is specified, it will get all the keys first and
+ // so we need to bind our wxEVT_KEY_DOWN handler to it, as we want to
+ // override the default keyboard handling. And currently it is always
+ // specified, but fall back to the control itself just in case this changes
+ // in the future.
+ if(!evtHandler)
+ {
+ evtHandler = m_control;
+ }
+
+ evtHandler->Bind
+ (wxEVT_KEY_DOWN
+ ,[entry](wxKeyEvent& event)
+ {
+ switch(event.GetKeyCode())
+ {
+ case WXK_RETURN:
+ case WXK_NUMPAD_ENTER:
+ if(!wxGetKeyState(WXK_ALT))
+ {
+ event.Skip();
+ return;
+ }
+
+ // Open the editor window when Alt-Enter is pressed
instead of
+ // just closing the editor, as would be done by default.
+ entry->open_editor();
+ break;
+ default:
+ event.Skip();
+ break;
+ }
+ });
}
void DatumSequenceEditor::BeginEdit(int row, int col, wxGrid* grid)
- [lmi-commits] [lmi] master updated (6977670 -> 713d56f), Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 14ac975 03/46: Use at() instead of operator[] in non-performance-critical code, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 193e804 01/46: Refactor census view to enable several implementations, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master c094807 05/46: Remove unnecessary altColor_ member variable, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 3d86a92 06/46: Use simpler wxGrid::AssignTable(), Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master c9e743f 08/46: Remove extra blank line from census_view.hpp, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master ba15191 11/46: Stop using wxRTTI in DatumSequenceEditorEvtHandler, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 5ee4b04 13/46: Improve comments in DatumSequenceEditorEvtHandler, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 03d33ab 14/46: Make DatumSequenceEditorEvtHandler::entry_ const, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 0e2a694 16/46: Replace DatumSequenceEditorEvtHandler with a lambda,
Greg Chicares <=
- [lmi-commits] [lmi] master 3e9f8af 02/46: Add census_view variant with wxGrid, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 804111d 04/46: Remove unnecessary use of std::swap(), Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 25a989e 10/46: Make table_type_converter::get_all() map const, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 12940dc 09/46: Define static map in more compact way, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master e1fdb77 12/46: Use Bind() in DatumSequenceEditorEvtHandler, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 7e048d0 15/46: Remove apparently unnecessary wxEVT_CHAR handler, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 7b62d49 21/46: Rename table_type_converter::get_by_value() to just get(), Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 1dacd5c 22/46: Move register_all() to table_custom_type_converter, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master a43eb0f 23/46: Add a comment explaining register_all() approach, Greg Chicares, 2020/07/22
- [lmi-commits] [lmi] master 4dcae52 34/46: Don't change default wxGrid lines colour, Greg Chicares, 2020/07/22