Merge branch 'listctrl-col-count'

Ensure wxListCtrl::GetColumnCount() behaves consistently on all
platforms with wxLC_LIST style.

See #22497.

Closes #22482.
This commit is contained in:
Vadim Zeitlin 2022-06-10 17:16:47 +01:00
commit 7faa70a4a9
3 changed files with 49 additions and 5 deletions

View File

@ -566,6 +566,11 @@ public:
/**
Returns the number of columns.
The control can have multiple columns only in wxLC_REPORT mode. In
wxLC_LIST mode this function returns 1, as a list is still considered
to have a (single) column. In wxLC_SMALL_ICON and wxLC_ICON modes, it
returns 0 as there are no columns at all.
*/
int GetColumnCount() const;

View File

@ -2331,8 +2331,10 @@ void wxListMainWindow::SendNotify( size_t line,
wxEventType command,
const wxPoint& point )
{
wxListEvent le( command, GetParent()->GetId() );
le.SetEventObject( GetParent() );
wxGenericListCtrl* const listctrl = GetListCtrl();
wxListEvent le( command, listctrl->GetId() );
le.SetEventObject( listctrl );
le.m_item.m_itemId =
le.m_itemIndex = line;
@ -2349,12 +2351,12 @@ void wxListMainWindow::SendNotify( size_t line,
// provide information about the (first column of the) item in the event if
// we have a valid item and any columns at all
if ( line != (size_t)-1 && GetColumnCount() )
if ( line != (size_t)-1 && listctrl->GetColumnCount() )
{
GetLine(line)->GetItem( 0, le.m_item );
}
GetParent()->GetEventHandler()->ProcessEvent( le );
listctrl->GetEventHandler()->ProcessEvent( le );
}
bool wxListMainWindow::ChangeCurrentWithoutEvent(size_t current)
@ -4906,6 +4908,10 @@ void wxGenericListCtrl::Init()
wxGenericListCtrl::~wxGenericListCtrl()
{
// Don't wait until the base class does it because our subwindows expect
// their parent window to be a wxListCtrl, but this won't be the case any
// more when we get to the base class dtor (it will be only a wxWindow).
DestroyChildren();
}
void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
@ -5353,7 +5359,11 @@ int wxGenericListCtrl::GetItemCount() const
int wxGenericListCtrl::GetColumnCount() const
{
return m_mainWin->GetColumnCount();
// wxLC_LIST is special as we want to return 1 for it, for compatibility
// with the native wxMSW version and not the real number of columns, which
// is 0. For the other non-wxLC_REPORT modes returning 0 is fine, however,
// as wxMSW does it too.
return HasFlag(wxLC_LIST) ? 1 : m_mainWin->GetColumnCount();
}
void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )

View File

@ -48,10 +48,12 @@ private:
WXUISIM_TEST( ColumnClick );
WXUISIM_TEST( ColumnDrag );
CPPUNIT_TEST( SubitemRect );
CPPUNIT_TEST( ColumnCount );
CPPUNIT_TEST_SUITE_END();
void EditLabel();
void SubitemRect();
void ColumnCount();
#if wxUSE_UIACTIONSIMULATOR
// Column events are only supported in wxListCtrl currently so we test them
// here rather than in ListBaseTest
@ -139,6 +141,33 @@ void ListCtrlTestCase::SubitemRect()
CHECK(rectLabel.GetRight() == rectItem.GetRight());
}
void ListCtrlTestCase::ColumnCount()
{
CHECK(m_list->GetColumnCount() == 0);
m_list->InsertColumn(0, "Column 0");
m_list->InsertColumn(1, "Column 1");
CHECK(m_list->GetColumnCount() == 2);
// Recreate the control in other modes to check the count there as well.
delete m_list;
m_list = new wxListCtrl(wxTheApp->GetTopWindow(), wxID_ANY,
wxDefaultPosition, wxDefaultSize,
wxLC_LIST);
CHECK(m_list->GetColumnCount() == 1);
delete m_list;
m_list = new wxListCtrl(wxTheApp->GetTopWindow(), wxID_ANY,
wxDefaultPosition, wxDefaultSize,
wxLC_ICON);
CHECK(m_list->GetColumnCount() == 0);
delete m_list;
m_list = new wxListCtrl(wxTheApp->GetTopWindow(), wxID_ANY,
wxDefaultPosition, wxDefaultSize,
wxLC_SMALL_ICON);
CHECK(m_list->GetColumnCount() == 0);
}
#if wxUSE_UIACTIONSIMULATOR
void ListCtrlTestCase::ColumnDrag()
{