From ba2ea837deb337cc12f11c6f64718c053d5e5628 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 6 Sep 2019 17:44:56 +0200 Subject: [PATCH] Actually make wxTE_PROCESS_ENTER example in dialogs sample work This didn't work any more since the changes done in the branch merged by fb2c17c193fdc96b4b368e37b0c9e4c245212589 as using wxTE_PROCESS_ENTER without actually handling the resulting event doesn't prevent Enter from activating the default button any longer. --- samples/dialogs/dialogs.cpp | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp index 1f9f8b37c0..e511f8db65 100644 --- a/samples/dialogs/dialogs.cpp +++ b/samples/dialogs/dialogs.cpp @@ -2581,6 +2581,34 @@ wxBEGIN_EVENT_TABLE(TestDefaultActionDialog, wxDialog) EVT_TEXT_ENTER(wxID_ANY, TestDefaultActionDialog::OnTextEnter) wxEND_EVENT_TABLE() +// TODO-C++11: We can't declare this class inside TestDefaultActionDialog +// itself when using C++98, so we have to do it here instead. +namespace +{ + +// We have to define a new class in order to actually handle pressing +// Enter, if we didn't do it, pressing it would still close the dialog. +class EnterHandlingTextCtrl : public wxTextCtrl +{ +public: + EnterHandlingTextCtrl(wxWindow* parent, int id, const wxString& value) + : wxTextCtrl(parent, id, value, + wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER) + { + Bind(wxEVT_TEXT_ENTER, &EnterHandlingTextCtrl::OnEnter, this); + + SetInitialSize(GetSizeFromTextSize(GetTextExtent(value).x)); + } + +private: + void OnEnter(wxCommandEvent& WXUNUSED(event)) + { + wxLogMessage("Enter pressed"); + } +}; + +} // anonymous namespace + TestDefaultActionDialog::TestDefaultActionDialog( wxWindow *parent ) : wxDialog( parent, -1, "Test default action" ) { @@ -2603,12 +2631,12 @@ TestDefaultActionDialog::TestDefaultActionDialog( wxWindow *parent ) : grid_sizer->Add(new wxCheckBox(this, ID_CATCH_LISTBOX_DCLICK, "Catch DoubleClick from wxListBox"), wxSizerFlags().CentreVertical()); - grid_sizer->Add(new wxTextCtrl(this, wxID_ANY, ""), - wxSizerFlags().CentreVertical()); + grid_sizer->Add(new wxTextCtrl(this, wxID_ANY, "Enter here closes the dialog"), + wxSizerFlags().Expand().CentreVertical()); grid_sizer->Add(new wxStaticText(this, wxID_ANY, "wxTextCtrl without wxTE_PROCESS_ENTER"), wxSizerFlags().CentreVertical()); - grid_sizer->Add(new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER), + grid_sizer->Add(new EnterHandlingTextCtrl(this, wxID_ANY, "Enter here is handled by the application"), wxSizerFlags().CentreVertical()); grid_sizer->Add(new wxStaticText(this, wxID_ANY, "wxTextCtrl with wxTE_PROCESS_ENTER"), wxSizerFlags().CentreVertical());