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.
This commit is contained in:
fuscated 2019-04-27 21:49:14 +03:00 committed by Stefan Csomor
parent 3d65840f8f
commit 158288cb35
2 changed files with 50 additions and 0 deletions

View File

@ -131,6 +131,7 @@ protected:
virtual void EnableTextChangedEvents(bool enable) wxOVERRIDE; virtual void EnableTextChangedEvents(bool enable) wxOVERRIDE;
// callbacks // callbacks
void OnChar(wxKeyEvent& event); // Process 'enter' if required
void OnKeyDown(wxKeyEvent& event); // Process clipboard shortcuts void OnKeyDown(wxKeyEvent& event); // Process clipboard shortcuts
// the subcontrols // the subcontrols

View File

@ -16,11 +16,14 @@
#include "wx/osx/private.h" #include "wx/osx/private.h"
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/button.h"
#include "wx/toplevel.h"
#endif #endif
typedef wxWindowWithItems<wxControl, wxComboBoxBase> RealwxComboBoxBase; typedef wxWindowWithItems<wxControl, wxComboBoxBase> RealwxComboBoxBase;
wxBEGIN_EVENT_TABLE(wxComboBox, RealwxComboBoxBase) wxBEGIN_EVENT_TABLE(wxComboBox, RealwxComboBoxBase)
EVT_CHAR(wxComboBox::OnChar)
EVT_KEY_DOWN(wxComboBox::OnKeyDown) EVT_KEY_DOWN(wxComboBox::OnKeyDown)
wxEND_EVENT_TABLE() wxEND_EVENT_TABLE()
@ -240,6 +243,52 @@ void wxComboBox::Dismiss()
GetComboPeer()->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) void wxComboBox::OnKeyDown(wxKeyEvent& event)
{ {
if (event.GetModifiers() == wxMOD_CONTROL) if (event.GetModifiers() == wxMOD_CONTROL)