Implement best size calculation for report mode wxListCtrl.

Use the column labels to determine the minimal width required by the control
to show them all in full.

Also declare all image list and column-related wxListCtrl methods in
wxListCtrlBase now as we need some of them in DoGetBestClientSize()
implementation.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70282 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2012-01-07 15:09:43 +00:00
parent a9bd911fde
commit 94248d2ed1
10 changed files with 163 additions and 98 deletions

View File

@@ -27,6 +27,10 @@
#include "wx/listctrl.h"
#ifndef WX_PRECOMP
#include "wx/dcclient.h"
#endif
const char wxListCtrlNameStr[] = "listCtrl";
// ListCtrl events
@@ -128,4 +132,73 @@ IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
// ----------------------------------------------------------------------------
// wxListCtrlBase implementation
// ----------------------------------------------------------------------------
long
wxListCtrlBase::InsertColumn(long col,
const wxString& heading,
int format,
int width)
{
wxListItem item;
item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
item.m_text = heading;
if ( width > -1 )
{
item.m_mask |= wxLIST_MASK_WIDTH;
item.m_width = width;
}
item.m_format = format;
return InsertColumn(col, item);
}
long wxListCtrlBase::InsertColumn(long col, const wxListItem& info)
{
long rc = DoInsertColumn(col, info);
if ( rc != -1 )
{
// As our best size calculation depends on the column headers,
// invalidate the previously cached best size when a column is added.
InvalidateBestSize();
}
return rc;
}
wxSize wxListCtrlBase::DoGetBestClientSize() const
{
// There is no obvious way to determine the best size in icon and list
// modes so just don't do it for now.
if ( !InReportView() )
return wxControl::DoGetBestClientSize();
// In report mode, we use only the column headers, not items, to determine
// the best width. The reason for this is that it's easier (we can't just
// iterate over all items, especially not in a virtual control, so we'd
// have to do something relatively complicated such as checking the size of
// some items in the beginning and the end only) and also because the
// columns are usually static while the list contents is dynamic so it
// usually doesn't make much sense to adjust the control size to it anyhow.
// And finally, scrollbars can always be used with the items while the
// headers are just truncated if there is not enough place for them.
const int columns = GetColumnCount();
if ( HasFlag(wxLC_NO_HEADER) || !columns )
return wxControl::DoGetBestClientSize();
wxClientDC dc(const_cast<wxListCtrlBase*>(this));
// Total width of all headers determines the best control width.
int totalWidth = 0;
for ( int col = 0; col < columns; col++ )
{
totalWidth += GetColumnWidth(col);
}
// Use some arbitrary height, there is no good way to determine it.
return wxSize(totalWidth, 10*dc.GetCharHeight());
}
#endif // wxUSE_LISTCTRL