Add wxSizerFlags::DisableConsistencyChecks()

This allows to (hopefully temporarily) disable size flag check asserts.
This commit is contained in:
Vadim Zeitlin 2021-05-20 00:20:54 +01:00
parent 5502d2d86b
commit 2e289d7231
4 changed files with 46 additions and 5 deletions

View File

@ -25,7 +25,9 @@ Changes in behaviour not resulting in compilation errors
(it used to succeed in wxMSW).
- Using invalid flags with wxBoxSizer or wxGridSizer items now triggers asserts
when done from the code or error messages when done in XRC.
when done from the code or error messages when done in XRC. These asserts are
best avoided by fixing the flags, but wxSizerFlags::DisableConsistencyChecks()
can be used to globally suppress them until this can be done.
- wxWS_EX_VALIDATE_RECURSIVELY is now the default behaviour, i.e. calling
Validate() or TransferData{From,To}Window() will now also call the same

View File

@ -238,6 +238,9 @@ public:
int GetFlags() const { return m_flags; }
int GetBorderInPixels() const { return m_borderInPixels; }
// Disablee sizer flags (in)consistency asserts.
static void DisableConsistencyChecks();
private:
#ifdef wxNEEDS_BORDER_IN_PX
static float DoGetDefaultBorderInPx();

View File

@ -1480,6 +1480,29 @@ public:
*/
wxSizerFlags& CentreVertical();
/**
Globally disable checks for sizer flag consistency in debug builds.
By default, sizer classes such as wxBoxSizer and wxFlexGridSizer assert
when passed invalid flags, even though doing this usually doesn't
result in any catastrophic consequences and the invalid flags are
simply ignored later. Due to this, and the fact that these checks were
only added in wxWidgets 3.1, existing code may run into multiple
asserts warning about incorrect sizer flags use. Using this function
provides a temporary solution for avoiding such asserts when upgrading
to wxWidgets 3.1 from a previous version and will prevent such checks
from being done.
Please do note that correcting the code by removing the invalid flags
remains a much better solution as these asserts may be very helpful to
understand why some code using sizer flags doesn't work as expected, so
using this function, especially permanently, rather than a temporary
workaround, is @e not recommended.
@since 3.1.6
*/
static void DisableConsistencyChecks();
/**
Sets the border in the given @a direction having twice the default
border size.

View File

@ -147,6 +147,8 @@ static const int SIZER_FLAGS_MASK =
namespace
{
bool gs_disableFlagChecks = false;
wxString MakeFlagsCheckMessage(const char* start, const char* whatToRemove)
{
return wxString::Format
@ -158,7 +160,9 @@ wxString MakeFlagsCheckMessage(const char* start, const char* whatToRemove)
"reporting the problem to the program developers.\n"
"\n"
"If you're the developer, simply remove %s from your code to "
"avoid getting this message.\n",
"avoid getting this message. You can also call "
"wxSizerFlags::DisableConsistencyChecks() to globally disable "
"all such checks, but this is strongly not recommended.",
start,
whatToRemove
);
@ -171,7 +175,7 @@ wxString MakeFlagsCheckMessage(const char* start, const char* whatToRemove)
#define ASSERT_NO_IGNORED_FLAGS_IMPL(f, value, name, explanation) \
wxASSERT_MSG \
( \
!((f) & (value)), \
gs_disableFlagChecks || !((f) & (value)), \
MakeFlagsCheckMessage \
( \
name " will be ignored in this sizer: " explanation, \
@ -185,7 +189,7 @@ wxString MakeFlagsCheckMessage(const char* start, const char* whatToRemove)
#define ASSERT_INCOMPATIBLE_NOT_USED_IMPL(f, f1, n1, f2, n2) \
wxASSERT_MSG \
( \
((f) & (f1 | f2)) != (f1 | f2), \
gs_disableFlagChecks || ((f) & (f1 | f2)) != (f1 | f2), \
MakeFlagsCheckMessage \
( \
"One of " n1 " and " n2 " will be ignored in this sizer: " \
@ -203,6 +207,14 @@ wxString MakeFlagsCheckMessage(const char* start, const char* whatToRemove)
ASSERT_INCOMPATIBLE_NOT_USED(f, wxALIGN_CENTRE_VERTICAL, wxALIGN_BOTTOM)
/* static */
void wxSizerFlags::DisableConsistencyChecks()
{
#if wxDEBUG_LEVEL
gs_disableFlagChecks = true;
#endif // wxDEBUG_LEVEL
}
void wxSizerItem::Init(const wxSizerFlags& flags)
{
Init();
@ -1515,7 +1527,8 @@ wxSizerItem *wxGridSizer::DoInsert(size_t index, wxSizerItem *item)
// Check that expansion will happen in at least one of the directions.
wxASSERT_MSG
(
!(flags & (wxALIGN_BOTTOM | wxALIGN_CENTRE_VERTICAL)) ||
gs_disableFlagChecks ||
!(flags & (wxALIGN_BOTTOM | wxALIGN_CENTRE_VERTICAL)) ||
!(flags & (wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL)),
MakeFlagsCheckMessage
(