diff --git a/src/common/event.cpp b/src/common/event.cpp index 072c82e953..b41f31cb8c 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -37,6 +37,7 @@ #if wxUSE_GUI #include "wx/window.h" + #include "wx/combobox.h" #include "wx/control.h" #include "wx/dc.h" #include "wx/spinbutt.h" @@ -435,20 +436,27 @@ wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId) wxString wxCommandEvent::GetString() const { - if (m_eventType != wxEVT_TEXT || !m_eventObject) - { - return m_cmdString; - } - else + // This is part of the hack retrieving the event string from the control + // itself only when/if it's really needed to avoid copying potentially huge + // strings coming from multiline text controls. For consistency we also do + // it for combo boxes, even though there are no real performance advantages + // in doing this for them. + if (m_eventType == wxEVT_TEXT && m_eventObject) { #if wxUSE_TEXTCTRL wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl); if ( txt ) return txt->GetValue(); - else #endif // wxUSE_TEXTCTRL - return m_cmdString; + +#if wxUSE_COMBOBOX + wxComboBox* combo = wxDynamicCast(m_eventObject, wxComboBox); + if ( combo ) + return combo->GetValue(); +#endif // wxUSE_COMBOBOX } + + return m_cmdString; } // ---------------------------------------------------------------------------- diff --git a/tests/controls/textentrytest.cpp b/tests/controls/textentrytest.cpp index a5027b9ecf..588e61bc72 100644 --- a/tests/controls/textentrytest.cpp +++ b/tests/controls/textentrytest.cpp @@ -177,9 +177,32 @@ void TextEntryTestCase::Replace() CPPUNIT_ASSERT_EQUAL(2, entry->GetInsertionPoint()); } +#if wxUSE_UIACTIONSIMULATOR + +class TextEventHandler +{ +public: + explicit TextEventHandler(wxWindow* win) + { + win->Bind(wxEVT_TEXT, &TextEventHandler::OnText, this); + } + + const wxString& GetLastString() const + { + return m_string; + } + +private: + void OnText(wxCommandEvent& event) + { + m_string = event.GetString(); + } + + wxString m_string; +}; + void TextEntryTestCase::Editable() { -#if wxUSE_UIACTIONSIMULATOR #ifdef __WXGTK__ // FIXME: For some reason this test regularly (although not always) fails @@ -203,6 +226,7 @@ void TextEntryTestCase::Editable() window->SetFocus(); wxYield(); + // Check that we get the expected number of events. wxUIActionSimulator sim; sim.Text("abcdef"); wxYield(); @@ -210,6 +234,26 @@ void TextEntryTestCase::Editable() CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue()); CPPUNIT_ASSERT_EQUAL(6, updated.GetCount()); + + // And that the event carries the right value. + TextEventHandler handler(window); + + sim.Text("g"); + wxYield(); + + CPPUNIT_ASSERT_EQUAL("abcdefg", handler.GetLastString()); + + // ... even if we generate the event programmatically and whether it uses + // the same value as the control has right now + entry->SetValue("abcdefg"); + CPPUNIT_ASSERT_EQUAL("abcdefg", handler.GetLastString()); + + // ... or not + entry->SetValue("abcdef"); + CPPUNIT_ASSERT_EQUAL("abcdef", handler.GetLastString()); + + // Check that making the control not editable does indeed prevent it from + // being edited. updated.Clear(); entry->SetEditable(false); @@ -218,9 +262,10 @@ void TextEntryTestCase::Editable() CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue()); CPPUNIT_ASSERT_EQUAL(0, updated.GetCount()); -#endif } +#endif // wxUSE_UIACTIONSIMULATOR + void TextEntryTestCase::Hint() { GetTestEntry()->SetHint("This is a hint");