From aacd26c27b887b24f889828c403797fb53355da1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 1 May 2020 03:34:44 +0200 Subject: [PATCH] Save specified, not actual, col width in wxPersistentDataViewCtrl This is more correct as saving the current width of the last column would prevent the user from shrinking it under the last automatically set size, i.e. the UI behaviour would change after restarting the program, which shouldn't be the case. Doing this required making WXGetManuallySetWidth(), which previously existed in the generic version only, available in all ports, so do it and also rename it to WXGetSpecifiedWidth() in the process, as this seems a somewhat better name (it doesn't have to be manually specified, i.e. it could also be done by the program itself or even implicitly by wxPersistentDataViewCtrl). Don't make this function public, at least for now, because it's not clear how could it be useful and it might still need to be changed to behave differently in the other ports. --- include/wx/dataview.h | 6 ++++++ include/wx/generic/dataview.h | 2 +- include/wx/persist/dataview.h | 10 +++++++++- src/generic/datavgen.cpp | 6 +++--- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/wx/dataview.h b/include/wx/dataview.h index 99b620d84d..48bd4efe85 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -514,6 +514,12 @@ public: virtual void SetBitmap( const wxBitmap& bitmap ) wxOVERRIDE { m_bitmap = bitmap; } virtual wxBitmap GetBitmap() const wxOVERRIDE { return m_bitmap; } + // Special accessor for use by wxWidgets only returning the width that was + // explicitly set, either by the application, using SetWidth(), or by the + // user, resizing the column interactively. It is usually the same as + // GetWidth(), but can be different for the last column. + virtual int WXGetSpecifiedWidth() const { return GetWidth(); } + protected: wxDataViewRenderer *m_renderer; int m_model_column; diff --git a/include/wx/generic/dataview.h b/include/wx/generic/dataview.h index e01d681123..12ee1ce1db 100644 --- a/include/wx/generic/dataview.h +++ b/include/wx/generic/dataview.h @@ -145,7 +145,7 @@ public: // user interactively. void WXOnResize(int width); - int WXGetManuallySetWidth() const; + virtual int WXGetSpecifiedWidth() const wxOVERRIDE; private: // common part of all ctors diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index c5f212b2ee..ee10c02480 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -61,7 +61,15 @@ public: SaveValue(columnPrefix + wxPERSIST_DVC_HIDDEN, column->IsHidden()); SaveValue(columnPrefix + wxPERSIST_DVC_POS, control->GetColumnPosition(column)); - SaveValue(columnPrefix + wxPERSIST_DVC_WIDTH, column->GetWidth()); + + // We take special care to save only the specified width instead of + // the currently used one. Usually they're one and the same, but + // they can be different for the last column, whose size can be + // greater than specified, as it's always expanded to fill the + // entire control width. + const int width = column->WXGetSpecifiedWidth(); + if ( width > 0 ) + SaveValue(columnPrefix + wxPERSIST_DVC_WIDTH, width); // Check if this column is the current sort key. if ( column->IsSortKey() ) diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 0e8689765d..0bfdc18df2 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -217,7 +217,7 @@ void wxDataViewColumn::WXOnResize(int width) m_owner->OnColumnResized(); } -int wxDataViewColumn::WXGetManuallySetWidth() const +int wxDataViewColumn::WXGetSpecifiedWidth() const { // Note that we need to return valid value even if no width was initially // specified, as otherwise the last column created without any explicit @@ -5130,7 +5130,7 @@ void wxDataViewMainWindow::UpdateColumnSizes() // reduce it until this size if it's currently wider, so this // comparison needs to be strict). if ( availableWidth < wxMax(lastCol->GetMinWidth(), - lastCol->WXGetManuallySetWidth()) ) + lastCol->WXGetSpecifiedWidth()) ) { return; } @@ -5319,7 +5319,7 @@ void wxDataViewCtrl::OnDPIChanged(wxDPIChangedEvent& event) minWidth = minWidth * event.GetNewDPI().x / event.GetOldDPI().x; m_cols[i]->SetMinWidth(minWidth); - int width = m_cols[i]->WXGetManuallySetWidth(); + int width = m_cols[i]->WXGetSpecifiedWidth(); if ( width > 0 ) width = width * event.GetNewDPI().x / event.GetOldDPI().x; m_cols[i]->SetWidth(width);