Make wxTextCtrl::SetLabel() consistently do nothing everywhere

Previously it did the same thing as SetValue() in wxMSW but didn't do
anything in wxGTK (and probably other ports), which made it easy to
write code that only worked under Windows but mysteriously didn't show
any text under Linux.

Change it to not do anything anywhere and add an assert to help
detecting wrong uses of this function.

Also add a unit test checking this behaviour.

See #24982.
This commit is contained in:
Vadim Zeitlin
2024-12-02 01:16:37 +01:00
parent 5ff83b400c
commit 79f06970a8
5 changed files with 32 additions and 0 deletions
+4
View File
@@ -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
-----------------------------------------------------
+4
View File
@@ -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
+7
View File
@@ -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.
+5
View File
@@ -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.");
+12
View File
@@ -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<wxTextCtrl>
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]")