From 3d167c0022e409c541a987a23352882445b29819 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 7 Jan 2012 15:09:54 +0000 Subject: [PATCH] Improve column auto-sizing code in generic wxListCtrl. Take the width of the header itself into account when setting width to wxLIST_AUTOSIZE. Also refactor the code to reuse the code used in wxLIST_AUTOSIZE_USEHEADER case in SetColumnWidth() when inserting or updating the column width to this value. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70285 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/private/listctrl.h | 5 +- src/generic/listctrl.cpp | 68 +++++++++++++-------------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/include/wx/generic/private/listctrl.h b/include/wx/generic/private/listctrl.h index 66752660c6..8b4a3db082 100644 --- a/include/wx/generic/private/listctrl.h +++ b/include/wx/generic/private/listctrl.h @@ -575,7 +575,6 @@ public: void DrawImage( int index, wxDC *dc, int x, int y ); void GetImageSize( int index, int &width, int &height ) const; - int GetTextLength( const wxString &s ) const; void SetImageList( wxImageList *imageList, int which ); void SetItemSpacing( int spacing, bool isSmall = false ); @@ -793,6 +792,10 @@ private: // delete all items but don't refresh: called from dtor void DoDeleteAllItems(); + // Compute the minimal width needed to fully display the column header. + int ComputeMinHeaderWidth(const wxListHeaderData* header) const; + + // the height of one line using the current font wxCoord m_lineHeight; diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index d6c70b065f..d66bb508bf 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -2964,17 +2964,6 @@ void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const } } -int wxListMainWindow::GetTextLength( const wxString &s ) const -{ - wxClientDC dc( wxConstCast(this, wxListMainWindow) ); - dc.SetFont( GetFont() ); - - wxCoord lw; - dc.GetTextExtent( s, &lw, NULL ); - - return lw + AUTOSIZE_COL_MARGIN; -} - void wxListMainWindow::SetImageList( wxImageList *imageList, int which ) { m_dirty = true; @@ -3017,6 +3006,30 @@ int wxListMainWindow::GetItemSpacing( bool isSmall ) // columns // ---------------------------------------------------------------------------- +int +wxListMainWindow::ComputeMinHeaderWidth(const wxListHeaderData* column) const +{ + wxClientDC dc(const_cast(this)); + + int width = dc.GetTextExtent(column->GetText()).x + AUTOSIZE_COL_MARGIN; + + width += 2*EXTRA_WIDTH; + + // check for column header's image availability + const int image = column->GetImage(); + if ( image != -1 ) + { + if ( m_small_image_list ) + { + int ix = 0, iy = 0; + m_small_image_list->GetSize(image, ix, iy); + width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE; + } + } + + return width; +} + void wxListMainWindow::SetColumn( int col, const wxListItem &item ) { wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col ); @@ -3027,7 +3040,7 @@ void wxListMainWindow::SetColumn( int col, const wxListItem &item ) column->SetItem( item ); if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER ) - column->SetWidth(GetTextLength( item.m_text )); + column->SetWidth(ComputeMinHeaderWidth(column)); wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin; if ( headerWin ) @@ -3062,29 +3075,13 @@ void wxListMainWindow::SetColumnWidth( int col, int width ) if (width == wxLIST_AUTOSIZE_USEHEADER) { - width = GetTextLength(column->GetText()); - width += 2*EXTRA_WIDTH; - - // check for column header's image availability - const int image = column->GetImage(); - if ( image != -1 ) - { - if ( m_small_image_list ) - { - int ix = 0, iy = 0; - m_small_image_list->GetSize(image, ix, iy); - width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE; - } - } + width = ComputeMinHeaderWidth(column); } else if ( width == wxLIST_AUTOSIZE ) { - if ( IsVirtual() ) - { - // TODO: determine the max width somehow... - width = WIDTH_COL_DEFAULT; - } - else // !virtual + width = ComputeMinHeaderWidth(column); + + if ( !IsVirtual() ) { wxClientDC dc(this); dc.SetFont( GetFont() ); @@ -3114,8 +3111,9 @@ void wxListMainWindow::SetColumnWidth( int col, int width ) m_aColWidths.Item(col)->nMaxWidth = max; } - max = m_aColWidths.Item(col)->nMaxWidth; - width = max + AUTOSIZE_COL_MARGIN; + max = m_aColWidths.Item(col)->nMaxWidth + AUTOSIZE_COL_MARGIN; + if ( width < max ) + width = max; } } @@ -4129,7 +4127,7 @@ void wxListMainWindow::InsertColumn( long col, const wxListItem &item ) { wxListHeaderData *column = new wxListHeaderData( item ); if (item.m_width == wxLIST_AUTOSIZE_USEHEADER) - column->SetWidth(GetTextLength( item.m_text )); + column->SetWidth(ComputeMinHeaderWidth(column)); wxColWidthInfo *colWidthInfo = new wxColWidthInfo(); -- 2.45.2