diff --git a/src/common/textentrycmn.cpp b/src/common/textentrycmn.cpp index ad9a516ebb..b9aa7640d6 100644 --- a/src/common/textentrycmn.cpp +++ b/src/common/textentrycmn.cpp @@ -38,7 +38,7 @@ // wxTextEntryHintData // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_CORE wxTextEntryHintData +class WXDLLIMPEXP_CORE wxTextEntryHintData : public wxEvtHandler { public: wxTextEntryHintData(wxTextEntryBase *entry, wxWindow *win) @@ -46,16 +46,26 @@ public: m_win(win), m_text(m_entry->GetValue()) { - win->Bind(wxEVT_SET_FOCUS, &wxTextEntryHintData::OnSetFocus, this); - win->Bind(wxEVT_KILL_FOCUS, &wxTextEntryHintData::OnKillFocus, this); - win->Bind(wxEVT_TEXT, &wxTextEntryHintData::OnTextChanged, this); + // We push ourselves as the event handler because this allows us to + // handle events before the user-defined handlers and notably process + // wxEVT_TEXT even if the user code already handles it, which is vital + // as if we don't get this event, we would always set the control text + // to the hint when losing focus, instead of preserving the text + // entered by user. Of course, the same problem could still happen if + // the user code pushed their own event handler before this one and + // didn't skip wxEVT_TEXT in it, but there doesn't seem anything we can + // do about this anyhow and this at least takes care of the much more + // common case. + m_win->PushEventHandler(this); + + Bind(wxEVT_SET_FOCUS, &wxTextEntryHintData::OnSetFocus, this); + Bind(wxEVT_KILL_FOCUS, &wxTextEntryHintData::OnKillFocus, this); + Bind(wxEVT_TEXT, &wxTextEntryHintData::OnTextChanged, this); } ~wxTextEntryHintData() { - m_win->Unbind(wxEVT_SET_FOCUS, &wxTextEntryHintData::OnSetFocus, this); - m_win->Unbind(wxEVT_KILL_FOCUS, &wxTextEntryHintData::OnKillFocus, this); - m_win->Unbind(wxEVT_TEXT, &wxTextEntryHintData::OnTextChanged, this); + m_win->PopEventHandler(); } // Get the real text of the control such as it was before we replaced it