Return correct string from wxEVT_TEXT wxComboBox events.

wxCommandEvent::GetString() could return empty string for the
programmatically-generated wxEVT_TEXT events from a wxComboBox.

Fix this by extending the on-demand string retrieval in wxCommandEvent to
wxComboBox as well (it was done only for wxTextCtrl).

Also add a unit test checking that the string has the expected value in the
events sent by all wxTextEntry-derived controls.

Closes #3901.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77057 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2014-08-11 11:53:01 +00:00
parent 19e8a43827
commit edad9616e7
2 changed files with 62 additions and 9 deletions

View File

@ -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;
}
// ----------------------------------------------------------------------------

View File

@ -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");