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:
@@ -37,6 +37,7 @@
|
|||||||
|
|
||||||
#if wxUSE_GUI
|
#if wxUSE_GUI
|
||||||
#include "wx/window.h"
|
#include "wx/window.h"
|
||||||
|
#include "wx/combobox.h"
|
||||||
#include "wx/control.h"
|
#include "wx/control.h"
|
||||||
#include "wx/dc.h"
|
#include "wx/dc.h"
|
||||||
#include "wx/spinbutt.h"
|
#include "wx/spinbutt.h"
|
||||||
@@ -435,20 +436,27 @@ wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
|
|||||||
|
|
||||||
wxString wxCommandEvent::GetString() const
|
wxString wxCommandEvent::GetString() const
|
||||||
{
|
{
|
||||||
if (m_eventType != wxEVT_TEXT || !m_eventObject)
|
// 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
|
||||||
return m_cmdString;
|
// strings coming from multiline text controls. For consistency we also do
|
||||||
}
|
// it for combo boxes, even though there are no real performance advantages
|
||||||
else
|
// in doing this for them.
|
||||||
|
if (m_eventType == wxEVT_TEXT && m_eventObject)
|
||||||
{
|
{
|
||||||
#if wxUSE_TEXTCTRL
|
#if wxUSE_TEXTCTRL
|
||||||
wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl);
|
wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl);
|
||||||
if ( txt )
|
if ( txt )
|
||||||
return txt->GetValue();
|
return txt->GetValue();
|
||||||
else
|
|
||||||
#endif // wxUSE_TEXTCTRL
|
#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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@@ -177,9 +177,32 @@ void TextEntryTestCase::Replace()
|
|||||||
CPPUNIT_ASSERT_EQUAL(2, entry->GetInsertionPoint());
|
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()
|
void TextEntryTestCase::Editable()
|
||||||
{
|
{
|
||||||
#if wxUSE_UIACTIONSIMULATOR
|
|
||||||
|
|
||||||
#ifdef __WXGTK__
|
#ifdef __WXGTK__
|
||||||
// FIXME: For some reason this test regularly (although not always) fails
|
// FIXME: For some reason this test regularly (although not always) fails
|
||||||
@@ -203,6 +226,7 @@ void TextEntryTestCase::Editable()
|
|||||||
window->SetFocus();
|
window->SetFocus();
|
||||||
wxYield();
|
wxYield();
|
||||||
|
|
||||||
|
// Check that we get the expected number of events.
|
||||||
wxUIActionSimulator sim;
|
wxUIActionSimulator sim;
|
||||||
sim.Text("abcdef");
|
sim.Text("abcdef");
|
||||||
wxYield();
|
wxYield();
|
||||||
@@ -210,6 +234,26 @@ void TextEntryTestCase::Editable()
|
|||||||
CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
|
CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
|
||||||
CPPUNIT_ASSERT_EQUAL(6, updated.GetCount());
|
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();
|
updated.Clear();
|
||||||
|
|
||||||
entry->SetEditable(false);
|
entry->SetEditable(false);
|
||||||
@@ -218,9 +262,10 @@ void TextEntryTestCase::Editable()
|
|||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
|
CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
|
||||||
CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
|
CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif // wxUSE_UIACTIONSIMULATOR
|
||||||
|
|
||||||
void TextEntryTestCase::Hint()
|
void TextEntryTestCase::Hint()
|
||||||
{
|
{
|
||||||
GetTestEntry()->SetHint("This is a hint");
|
GetTestEntry()->SetHint("This is a hint");
|
||||||
|
Reference in New Issue
Block a user