From 66519997198d05848f07b15c4f1bd33c8fc25d12 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jan 2018 16:22:51 +0100 Subject: [PATCH 1/4] Fold best size and ellipsis handling in AutoResizeIfNecessary() Refactor wxStaticText code to use AutoResizeIfNecessary() for all the updates done after the label extent changes (whether it's due to the change of the label itself or the font used for rendering it). There should be no changes in behaviour after this commit except for a small bug fix in wxGenericStaticText when setting label with markup: this didn't invalidate the best size before, but does now. --- src/common/stattextcmn.cpp | 21 ++++++++++++++------- src/generic/stattextg.cpp | 5 +---- src/gtk/stattext.cpp | 14 +++----------- src/msw/stattext.cpp | 7 +------ src/osx/stattext_osx.cpp | 14 ++------------ 5 files changed, 21 insertions(+), 40 deletions(-) diff --git a/src/common/stattextcmn.cpp b/src/common/stattextcmn.cpp index 203e1f7a51..aaa61e87ec 100644 --- a/src/common/stattextcmn.cpp +++ b/src/common/stattextcmn.cpp @@ -205,13 +205,20 @@ void wxStaticTextBase::Wrap(int width) void wxStaticTextBase::AutoResizeIfNecessary() { - // adjust the size of the window to fit to the label unless autoresizing is - // disabled - if ( !HasFlag(wxST_NO_AUTORESIZE) ) - { - DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, - wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); - } + // This method is only called if either the label or the font changed, i.e. + // if the label extent changed, so the best size is not the same neither + // any more. + InvalidateBestSize(); + + if ( IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized! + return; + + // This flag is specifically used to prevent the control from resizing even + // when its label changes. + if ( HasFlag(wxST_NO_AUTORESIZE) ) + return; + + SetSize(GetBestSize()); } // ---------------------------------------------------------------------------- diff --git a/src/generic/stattextg.cpp b/src/generic/stattextg.cpp index a8b4b5c150..fa1db47c4a 100644 --- a/src/generic/stattextg.cpp +++ b/src/generic/stattextg.cpp @@ -102,10 +102,7 @@ void wxGenericStaticText::SetLabel(const wxString& label) wxControl::SetLabel(label); DoSetLabel(GetEllipsizedLabel()); - InvalidateBestSize(); - - if ( !IsEllipsized() ) - AutoResizeIfNecessary(); + AutoResizeIfNecessary(); #if wxUSE_MARKUP if ( m_markupText ) diff --git a/src/gtk/stattext.cpp b/src/gtk/stattext.cpp index 9c1031f9fb..bda1f6d135 100644 --- a/src/gtk/stattext.cpp +++ b/src/gtk/stattext.cpp @@ -138,14 +138,9 @@ void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label) { wxCHECK_RET( m_widget != NULL, wxT("invalid static text") ); - InvalidateBestSize(); - (this->*setter)(GTK_LABEL(m_widget), label); - // adjust the label size to the new label unless disabled - if ( !HasFlag(wxST_NO_AUTORESIZE) && - !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized! - SetSize( GetBestSize() ); + AutoResizeIfNecessary(); } void wxStaticText::SetLabel(const wxString& label) @@ -222,11 +217,8 @@ bool wxStaticText::SetFont( const wxFont &font ) gtk_label_set_use_underline(GTK_LABEL(m_widget), !isUnderlined); } - // adjust the label size to the new label unless disabled - if (!HasFlag(wxST_NO_AUTORESIZE)) - { - SetSize( GetBestSize() ); - } + AutoResizeIfNecessary(); + return ret; } diff --git a/src/msw/stattext.cpp b/src/msw/stattext.cpp index 97b28d06e8..f5a29f380e 100644 --- a/src/msw/stattext.cpp +++ b/src/msw/stattext.cpp @@ -184,12 +184,7 @@ void wxStaticText::SetLabel(const wxString& label) #endif // SS_ENDELLIPSIS DoSetLabel(GetEllipsizedLabel()); - InvalidateBestSize(); - - if ( !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized! - { - AutoResizeIfNecessary(); - } + AutoResizeIfNecessary(); } bool wxStaticText::SetFont(const wxFont& font) diff --git a/src/osx/stattext_osx.cpp b/src/osx/stattext_osx.cpp index 4176add134..53e0e0ced9 100644 --- a/src/osx/stattext_osx.cpp +++ b/src/osx/stattext_osx.cpp @@ -78,13 +78,7 @@ void wxStaticText::SetLabel(const wxString& label) DoSetLabel(GetEllipsizedLabel()); } - InvalidateBestSize(); - - if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) && - !IsEllipsized() ) // don't resize if we adjust to current size - { - SetSize( GetBestSize() ); - } + AutoResizeIfNecessary(); Refresh(); @@ -98,11 +92,7 @@ bool wxStaticText::SetFont(const wxFont& font) if ( ret ) { - if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) - { - InvalidateBestSize(); - SetSize( GetBestSize() ); - } + AutoResizeIfNecessary(); } return ret; From 51bfc68423d735dfa24e7c1e6dd807110bfeafae Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jan 2018 16:26:35 +0100 Subject: [PATCH 2/4] Avoid unnecessary checks in wxStaticText::SetFont() in wxGTK There is no need to do anything if the font didn't actually change. --- src/gtk/stattext.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/gtk/stattext.cpp b/src/gtk/stattext.cpp index bda1f6d135..78e154c006 100644 --- a/src/gtk/stattext.cpp +++ b/src/gtk/stattext.cpp @@ -175,7 +175,8 @@ bool wxStaticText::SetFont( const wxFont &font ) const bool wasUnderlined = GetFont().GetUnderlined(); const bool wasStrickenThrough = GetFont().GetStrikethrough(); - bool ret = wxControl::SetFont(font); + if ( !wxControl::SetFont(font) ) + return false; const bool isUnderlined = GetFont().GetUnderlined(); const bool isStrickenThrough = GetFont().GetStrikethrough(); @@ -219,7 +220,7 @@ bool wxStaticText::SetFont( const wxFont &font ) AutoResizeIfNecessary(); - return ret; + return true; } wxSize wxStaticText::DoGetBestSize() const From c19e53e75e61134d8ef478a2316eef87e915f37c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jan 2018 16:27:17 +0100 Subject: [PATCH 3/4] Don't skip wxStaticText auto-resizing if it's ellipsized There doesn't seem to be any good reason for this, as it's perfectly possible to use ellipsize the text if it's too long to fit even when the control is resized to its maximal extent, but still allow the control to resize in the first place. See https://groups.google.com/d/msg/wx-dev/58xFP4FIxXc/d5lj6901CQAJ --- src/common/stattextcmn.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/common/stattextcmn.cpp b/src/common/stattextcmn.cpp index aaa61e87ec..a83d937246 100644 --- a/src/common/stattextcmn.cpp +++ b/src/common/stattextcmn.cpp @@ -210,9 +210,6 @@ void wxStaticTextBase::AutoResizeIfNecessary() // any more. InvalidateBestSize(); - if ( IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized! - return; - // This flag is specifically used to prevent the control from resizing even // when its label changes. if ( HasFlag(wxST_NO_AUTORESIZE) ) From 3294b9a63a30f385b7c061acda069dc4e5820599 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jan 2018 16:47:11 +0100 Subject: [PATCH 4/4] Don't invalidate best size for controls with wxST_NO_AUTORESIZE This results in non-intuitive behaviour, as the controls with this style still get resized after changing their label, so don't do it. See https://groups.google.com/d/msg/wx-dev/58xFP4FIxXc/hm5WO0xNBwAJ --- src/common/stattextcmn.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/common/stattextcmn.cpp b/src/common/stattextcmn.cpp index a83d937246..811aceb190 100644 --- a/src/common/stattextcmn.cpp +++ b/src/common/stattextcmn.cpp @@ -205,16 +205,21 @@ void wxStaticTextBase::Wrap(int width) void wxStaticTextBase::AutoResizeIfNecessary() { - // This method is only called if either the label or the font changed, i.e. - // if the label extent changed, so the best size is not the same neither - // any more. - InvalidateBestSize(); - // This flag is specifically used to prevent the control from resizing even // when its label changes. if ( HasFlag(wxST_NO_AUTORESIZE) ) return; + // This method is only called if either the label or the font changed, i.e. + // if the label extent changed, so the best size is not the same neither + // any more. + // + // Note that we don't invalidate it when wxST_NO_AUTORESIZE is on because + // this would result in the control being effectively resized during the + // next Layout() and this style is used expressly to prevent this from + // happening. + InvalidateBestSize(); + SetSize(GetBestSize()); }