Fix "Enter" behaviour when wxEVT_TEXT_ENTER is skipped in wxGTK

Bring wxGTK in sync with wxMSW and wxMac by activating the default
button manually if wxEVT_TEXT_ENTER handler skips the event.
This commit is contained in:
Vadim Zeitlin
2019-07-14 01:56:54 +02:00
parent ef03d3bb93
commit f7ead9f844
4 changed files with 47 additions and 0 deletions

View File

@@ -86,6 +86,10 @@ protected:
static int GTKGetEntryTextLength(GtkEntry* entry); static int GTKGetEntryTextLength(GtkEntry* entry);
// Helper for wxTE_PROCESS_ENTER handling: activates the default button in
// the dialog containing this control if any.
bool ClickDefaultButtonIfPossible();
private: private:
// implement this to return the associated GtkEntry or another widget // implement this to return the associated GtkEntry or another widget
// implementing GtkEditable // implementing GtkEditable

View File

@@ -241,6 +241,12 @@ void wxComboBox::OnChar( wxKeyEvent &event )
// down list upon RETURN. // down list upon RETURN.
return; return;
} }
// We disable built-in default button activation when
// wxTE_PROCESS_ENTER is used, but we still should activate it
// if the event wasn't handled, so do it from here.
if ( ClickDefaultButtonIfPossible() )
return;
} }
break; break;
} }

View File

@@ -1693,6 +1693,12 @@ void wxTextCtrl::OnChar( wxKeyEvent &key_event )
event.SetString(GetValue()); event.SetString(GetValue());
if ( HandleWindowEvent(event) ) if ( HandleWindowEvent(event) )
return; return;
// We disable built-in default button activation when
// wxTE_PROCESS_ENTER is used, but we still should activate it
// if the event wasn't handled, so do it from here.
if ( ClickDefaultButtonIfPossible() )
return;
} }
} }

View File

@@ -986,4 +986,35 @@ wxString wxTextEntry::GetHint() const
} }
#endif // __WXGTK3__ #endif // __WXGTK3__
bool wxTextEntry::ClickDefaultButtonIfPossible()
{
GtkWidget* const widget = GTK_WIDGET(GetEntry());
// This does the same thing as gtk_entry_real_activate() in GTK itself.
//
// Note: in GTK 4 we should probably just use gtk_widget_activate_default().
GtkWidget* const toplevel = gtk_widget_get_toplevel(widget);
if ( GTK_IS_WINDOW (toplevel) )
{
GtkWindow* const window = GTK_WINDOW(toplevel);
if ( window )
{
GtkWidget* const default_widget = gtk_window_get_default_widget(window);
GtkWidget* const focus_widget = gtk_window_get_focus(window);
if ( widget != default_widget &&
!(widget == focus_widget &&
(!default_widget ||
!gtk_widget_get_sensitive(default_widget))) )
{
if ( gtk_window_activate_default(window) )
return true;
}
}
}
return false;
}
#endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX