From 158288cb3568852655cade34037706786b6dc1da Mon Sep 17 00:00:00 2001 From: fuscated Date: Sat, 27 Apr 2019 21:49:14 +0300 Subject: [PATCH] Improve handing of the enter key in wxComboBox control * Copy-paste the code for OnChar in the wxTextCtrl. * This implements two features: 1. This adds support for the wxTE_PROCESS_ENTER style. 2. Support for pressing the default button in a dialog if there is one. --- include/wx/osx/combobox.h | 1 + src/osx/combobox_osx.cpp | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/wx/osx/combobox.h b/include/wx/osx/combobox.h index 65c7edd869..ab85a712bd 100644 --- a/include/wx/osx/combobox.h +++ b/include/wx/osx/combobox.h @@ -131,6 +131,7 @@ protected: virtual void EnableTextChangedEvents(bool enable) wxOVERRIDE; // callbacks + void OnChar(wxKeyEvent& event); // Process 'enter' if required void OnKeyDown(wxKeyEvent& event); // Process clipboard shortcuts // the subcontrols diff --git a/src/osx/combobox_osx.cpp b/src/osx/combobox_osx.cpp index fde159e927..5bc0224a50 100644 --- a/src/osx/combobox_osx.cpp +++ b/src/osx/combobox_osx.cpp @@ -16,11 +16,14 @@ #include "wx/osx/private.h" #ifndef WX_PRECOMP + #include "wx/button.h" + #include "wx/toplevel.h" #endif typedef wxWindowWithItems RealwxComboBoxBase; wxBEGIN_EVENT_TABLE(wxComboBox, RealwxComboBoxBase) + EVT_CHAR(wxComboBox::OnChar) EVT_KEY_DOWN(wxComboBox::OnKeyDown) wxEND_EVENT_TABLE() @@ -240,6 +243,52 @@ void wxComboBox::Dismiss() GetComboPeer()->Dismiss(); } +void wxComboBox::OnChar(wxKeyEvent& event) +{ + const int key = event.GetKeyCode(); + bool eat_key = false; + + switch (key) + { + case WXK_RETURN: + case WXK_NUMPAD_ENTER: + if (m_windowStyle & wxTE_PROCESS_ENTER) + { + wxCommandEvent event(wxEVT_TEXT_ENTER, m_windowId); + event.SetEventObject(this); + event.SetString(GetValue()); + if (HandleWindowEvent(event)) + return; + } + else + { + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if (tlw && tlw->GetDefaultItem()) + { + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); + if (def && def->IsEnabled()) + { + wxCommandEvent event(wxEVT_BUTTON, def->GetId()); + event.SetEventObject(def); + def->Command(event); + return; + } + } + + // this will make wxWidgets eat the ENTER key so that + // we actually prevent line wrapping in a single line text control + eat_key = true; + } + break; + } + + if (!eat_key) + { + // perform keystroke handling + event.Skip(true); + } +} + void wxComboBox::OnKeyDown(wxKeyEvent& event) { if (event.GetModifiers() == wxMOD_CONTROL)