Improve wxTE_PROCESS_ENTER handling in wxUniv

This commit is contained in:
Kvaz1r
2021-08-07 09:56:02 +03:00
parent 5d1a936f73
commit e3acc50fbc
3 changed files with 49 additions and 4 deletions

View File

@@ -36,6 +36,7 @@ class WXDLLIMPEXP_FWD_CORE wxTextCtrlCommandProcessor;
#define wxACTION_TEXT_WORD_RIGHT wxT("wordright")
#define wxACTION_TEXT_PAGE_UP wxT("pageup")
#define wxACTION_TEXT_PAGE_DOWN wxT("pagedown")
#define wxACTION_TEXT_RETURN wxT("return")
// clipboard operations
#define wxACTION_TEXT_COPY wxT("copy")
@@ -451,6 +452,8 @@ protected:
bool DoCut();
bool DoPaste();
bool ClickDefaultButtonIfPossible();
private:
// all these methods are for multiline text controls only

View File

@@ -96,8 +96,12 @@ void wxDialog::OnOK(wxCommandEvent &WXUNUSED(event))
}
else
{
SetReturnCode(wxID_OK);
Show(false);
// don't change return code from event char if it was set earlier
if (GetReturnCode() == 0)
{
SetReturnCode(wxID_OK);
Show(false);
}
}
}
}

View File

@@ -4676,6 +4676,15 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig,
if ( CanRedo() )
Redo();
}
else if ( action == wxACTION_TEXT_RETURN )
{
// activate default button
if ( !HasFlag(wxTE_PROCESS_ENTER) && !HasFlag(wxTE_MULTILINE) )
{
return ClickDefaultButtonIfPossible();
}
return false;
}
else
{
return wxControl::PerformAction(action, numArg, strArg);
@@ -4768,13 +4777,18 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
#endif
if ( keycode == WXK_RETURN )
{
if ( IsSingleLine() || (GetWindowStyle() & wxTE_PROCESS_ENTER) )
if ( (GetWindowStyle() & wxTE_PROCESS_ENTER) )
{
wxCommandEvent event(wxEVT_TEXT_ENTER, GetId());
InitCommandEvent(event);
event.SetString(GetValue());
GetEventHandler()->ProcessEvent(event);
}
if ( IsSingleLine() )
{
ClickDefaultButtonIfPossible();
}
else // interpret <Enter> normally: insert new line
{
PerformAction(wxACTION_TEXT_INSERT, -1, wxT('\n'));
@@ -4805,6 +4819,23 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
event.Skip();
}
bool wxTextCtrl::ClickDefaultButtonIfPossible()
{
wxTopLevelWindow* tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
if ( tlw )
{
wxButton* btn = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
if ( btn )
{
wxCommandEvent evt(wxEVT_BUTTON, btn->GetId());
evt.SetEventObject(btn);
btn->Command(evt);
return true;
}
}
return false;
}
/* static */
wxInputHandler *wxTextCtrl::GetStdInputHandler(wxInputHandler *handlerDef)
{
@@ -4922,6 +4953,9 @@ bool wxStdTextCtrlInputHandler::HandleKey(wxInputConsumer *consumer,
action << wxACTION_TEXT_PREFIX_DEL << wxACTION_TEXT_LEFT;
break;
case WXK_RETURN:
action << wxACTION_TEXT_RETURN;
break;
// something else
default:
// reset the action as it could be already set to one of the
@@ -4957,7 +4991,11 @@ bool wxStdTextCtrlInputHandler::HandleKey(wxInputConsumer *consumer,
if ( (action != wxACTION_NONE) && (action != wxACTION_TEXT_PREFIX_SEL) )
{
consumer->PerformAction(action, -1, str);
bool result = consumer->PerformAction(action, -1, str);
if ( !result && action == wxACTION_TEXT_RETURN )
{
return wxStdInputHandler::HandleKey(consumer, event, pressed);
}
// the key down of WXK_UP/DOWN and WXK_PAGEUP/DOWN
// must generate a wxEVT_TEXT event. For the controls