From 8e8d910979bd7c3c1dada49e87645a81d04e682e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 5 May 2012 11:22:09 +0000 Subject: [PATCH] Update the field widths on demand in wxStatusBarGeneric. This ensures that the value returned from GetFieldRect() is always up to date, even when this method is called from the user-defined wxEVT_SIZE handler, i.e. before our own OnSize() could run. Also remove the now unneeded hack with calling the base class OnSize() from the statbar sample. Closes #14268. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71365 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/statusbr.h | 8 +++++-- samples/statbar/statbar.cpp | 5 ----- src/generic/statusbr.cpp | 39 +++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/include/wx/generic/statusbr.h b/include/wx/generic/statusbr.h index 7d54f17010..a27eaf6dad 100644 --- a/include/wx/generic/statusbr.h +++ b/include/wx/generic/statusbr.h @@ -91,8 +91,9 @@ protected: // common part of all ctors void Init(); - // the last known height of the client rect - int m_lastClientHeight; + // the last known size, fields widths must be updated whenever it's out of + // date + wxSize m_lastClientSize; // the absolute widths of the status bar panes in pixels wxArrayInt m_widthsAbs; @@ -106,6 +107,9 @@ protected: virtual wxSize DoGetBestSize() const; private: + // Update m_lastClientSize and m_widthsAbs from the current size. + void DoUpdateFieldWidths(); + DECLARE_EVENT_TABLE() DECLARE_DYNAMIC_CLASS_NO_COPY(wxStatusBarGeneric) }; diff --git a/samples/statbar/statbar.cpp b/samples/statbar/statbar.cpp index e053fe1642..7ea8202d8e 100644 --- a/samples/statbar/statbar.cpp +++ b/samples/statbar/statbar.cpp @@ -947,11 +947,6 @@ void MyStatusBar::OnSize(wxSizeEvent& event) return; #endif - // TEMPORARY HACK: TODO find a more general solution -#ifdef wxStatusBarGeneric - wxStatusBar::OnSize(event); -#endif - wxRect rect; if (!GetFieldRect(Field_Checkbox, rect)) { diff --git a/src/generic/statusbr.cpp b/src/generic/statusbr.cpp index c7d209caef..7dc2839535 100644 --- a/src/generic/statusbr.cpp +++ b/src/generic/statusbr.cpp @@ -181,9 +181,15 @@ void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[]) wxStatusBarBase::SetStatusWidths(n, widths_field); // update cache - int width; - GetClientSize(&width, &m_lastClientHeight); - m_widthsAbs = CalculateAbsWidths(width); + DoUpdateFieldWidths(); +} + +void wxStatusBarGeneric::DoUpdateFieldWidths() +{ + m_lastClientSize = GetClientSize(); + + // recompute the cache of the field widths if the status bar width has changed + m_widthsAbs = CalculateAbsWidths(m_lastClientSize.x); } bool wxStatusBarGeneric::ShowsSizeGrip() const @@ -325,6 +331,16 @@ bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const wxCHECK_MSG( (n >= 0) && ((size_t)n < m_panes.GetCount()), false, wxT("invalid status bar field index") ); + // We can be called from the user-defined EVT_SIZE handler in which case + // the widths haven't been updated yet and we need to do it now. This is + // not very efficient as we keep testing the size but there is no other way + // to make the code needing the up-to-date fields sizes in its EVT_SIZE to + // work. + if ( GetClientSize().x != m_lastClientSize.x ) + { + const_cast(this)->DoUpdateFieldWidths(); + } + if (m_widthsAbs.IsEmpty()) return false; @@ -335,7 +351,7 @@ bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const rect.y = m_borderY; rect.width = m_widthsAbs[n] - 2*m_borderX; - rect.height = m_lastClientHeight - 2*m_borderY; + rect.height = m_lastClientSize.y - 2*m_borderY; return true; } @@ -348,7 +364,7 @@ int wxStatusBarGeneric::GetFieldFromPoint(const wxPoint& pt) const // NOTE: we explicitly don't take in count the borders since they are only // useful when rendering the status text, not for hit-test computations - if (pt.y <= 0 || pt.y >= m_lastClientHeight) + if (pt.y <= 0 || pt.y >= m_lastClientSize.y) return wxNOT_FOUND; int x = 0; @@ -525,18 +541,11 @@ void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event) #endif } -void wxStatusBarGeneric::OnSize(wxSizeEvent& WXUNUSED(event)) +void wxStatusBarGeneric::OnSize(wxSizeEvent& event) { - // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ) - int width; -#ifdef __WXPM__ - GetSize(&width, &m_lastClientHeight); -#else - GetClientSize(&width, &m_lastClientHeight); -#endif + DoUpdateFieldWidths(); - // recompute the cache of the field widths if the status bar width has changed - m_widthsAbs = CalculateAbsWidths(width); + event.Skip(); } #endif // wxUSE_STATUSBAR -- 2.45.2