From e5a16353710b361f045c1055cf5c5da3bdcce56d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 8 Dec 2008 12:28:41 +0000 Subject: [PATCH] added wxHeaderCtrlSimple::GetBestFittingWidth() for even more automatic column resizing git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57187 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/headerctrl.h | 10 ++++++++++ interface/wx/headerctrl.h | 16 ++++++++++++++++ src/common/headerctrlcmn.cpp | 13 +++++++++++++ 3 files changed, 39 insertions(+) diff --git a/include/wx/headerctrl.h b/include/wx/headerctrl.h index f7f8dda827..bf3a6632ba 100644 --- a/include/wx/headerctrl.h +++ b/include/wx/headerctrl.h @@ -224,7 +224,17 @@ public: void RemoveSortIndicator(); protected: + // implement/override base class methods virtual wxHeaderColumnBase& GetColumn(unsigned int idx); + virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle); + + // and define another one to be overridden in the derived classes: it + // should return the best width for the given column contents or -1 if not + // implemented, we use it to implement UpdateColumnWidthToFit() + virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx)) const + { + return -1; + } private: // functions implementing our public API diff --git a/interface/wx/headerctrl.h b/interface/wx/headerctrl.h index c7e5114da6..cca1310638 100644 --- a/interface/wx/headerctrl.h +++ b/interface/wx/headerctrl.h @@ -356,6 +356,22 @@ public: The column to remove sort indicator for. */ void RemoveSortIndicator(unsigned int idx); + +protected: + /** + This function can be overridden in the classes deriving from this + control instead of overriding UpdateColumnWidthToFit(). + + To implement automatic column resizing to fit its contents width when + the column divider is double clicked, you need to simply return the + fitting width for the given column @a idx from this method, the control + will automatically use the biggest value between the one returned from + here and the one needed for the display of the column title itself. + + The base class version returns -1 indicating that this function is not + implemented. + */ + virtual int GetBestFittingWidth(unsigned int idx) const; }; /** diff --git a/src/common/headerctrlcmn.cpp b/src/common/headerctrlcmn.cpp index fa9766ab81..a424e924d2 100644 --- a/src/common/headerctrlcmn.cpp +++ b/src/common/headerctrlcmn.cpp @@ -149,6 +149,19 @@ void wxHeaderCtrlSimple::RemoveSortIndicator() } } +bool +wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx, int widthTitle) +{ + const int widthContents = GetBestFittingWidth(idx); + if ( widthContents == -1 ) + return false; + + m_cols[idx].SetWidth(wxMax(widthContents, widthTitle)); + UpdateColumn(idx); + + return true; +} + // ============================================================================ // wxHeaderCtrlEvent implementation // ============================================================================ -- 2.47.2