diff --git a/docs/changes.txt b/docs/changes.txt index cf98b6a78f..21dc79d48d 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -113,6 +113,10 @@ Changes in behaviour not resulting in compilation errors but may require removing any compensation for not doing this before that may be present in the application code. +- Calling wxTextCtrl::SetLabel() now consistently does nothing and asserts + under all platforms instead of behaving as SetValue() under MSW and doing + nothing elsewhere. + Changes in behaviour which may result in build errors ----------------------------------------------------- diff --git a/include/wx/textctrl.h b/include/wx/textctrl.h index 19f89bb36a..a719a53f6c 100644 --- a/include/wx/textctrl.h +++ b/include/wx/textctrl.h @@ -913,6 +913,10 @@ public: return GetCompositeControlsDefaultAttributes(variant); } + // Setting label for text control doesn't work portably, use SetValue() or + // ChangeValue() instead. + virtual void SetLabel(const wxString& label) override; + virtual const wxTextEntry* WXGetTextEntry() const override { return this; } #if wxUSE_SPELLCHECK diff --git a/interface/wx/window.h b/interface/wx/window.h index 332db03416..d175cfb581 100644 --- a/interface/wx/window.h +++ b/interface/wx/window.h @@ -3399,6 +3399,13 @@ public: /** Sets the window's label. + Please note that not all windows have labels and this function may do + nothing in this case. And some other derived windows use different + functions for changing the text shown in them, e.g. wxTextCtrl uses + wxTextCtrl::SetValue() or wxTextCtrl::ChangeValue() and trying to use + SetLabel() on it will assert to help to detect possibly erroneous calls + to SetLabel(). + @param label The window label. diff --git a/src/common/textcmn.cpp b/src/common/textcmn.cpp index d31f761f9d..e7d6a30dbf 100644 --- a/src/common/textcmn.cpp +++ b/src/common/textcmn.cpp @@ -931,6 +931,11 @@ bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style) return true; } +void wxTextCtrlBase::SetLabel(const wxString& WXUNUSED(label)) +{ + wxFAIL_MSG("Use SetValue() or ChangeValue() instead."); +} + wxString wxTextAreaBase::GetRTFValue() const { wxFAIL_MSG("Not implemented for the current platform."); diff --git a/tests/controls/label.cpp b/tests/controls/label.cpp index ec72d1a3b7..c242f8f793 100644 --- a/tests/controls/label.cpp +++ b/tests/controls/label.cpp @@ -20,6 +20,7 @@ #include "wx/checkbox.h" #include "wx/control.h" #include "wx/stattext.h" +#include "wx/textctrl.h" #include "wx/generic/stattextg.h" @@ -118,6 +119,17 @@ TEST_CASE("wxControl::Label", "[wxControl][label]") cb(new wxCheckBox(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL)); DoTestLabel(cb.get()); } + + SECTION("wxTextCtrl") + { + const std::unique_ptr + tc(new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL)); + + // Setting the label of a wxTextCtrl should _not_ work, it has value + // and not a label. + WX_ASSERT_FAILS_WITH_ASSERT( tc->SetLabel("something else") ); + CHECK( tc->GetValue() == ORIGINAL_LABEL ); + } } TEST_CASE("wxControl::RemoveMnemonics", "[wxControl][label][mnemonics]")