Replace obsolete object array with vector in wxGenericListCtrl

Use wxVector<wxListLineData*> instead of WX_DECLARE_OBJARRAY().

This modernizes the code and allows to get rid of the static variables
previously used for sorting as now we can use std::sort().

Closes https://github.com/wxWidgets/wxWidgets/pull/924
This commit is contained in:
Vitaly Stakhovsky
2018-09-17 22:48:13 +02:00
committed by Vadim Zeitlin
parent f69dbaa1ae
commit 2af7e38153
2 changed files with 51 additions and 36 deletions

View File

@@ -258,7 +258,7 @@ public:
bool HasText() const { return !GetText(0).empty(); } bool HasText() const { return !GetText(0).empty(); }
void SetItem( int index, const wxListItem &info ); void SetItem( int index, const wxListItem &info );
void GetItem( int index, wxListItem &info ); void GetItem( int index, wxListItem &info ) const;
wxString GetText(int index) const; wxString GetText(int index) const;
void SetText( int index, const wxString& s ); void SetText( int index, const wxString& s );
@@ -313,7 +313,18 @@ private:
int width); int width);
}; };
WX_DECLARE_OBJARRAY(wxListLineData, wxListLineDataArray); class wxListLineDataArray : public wxVector<wxListLineData*>
{
public:
void Clear()
{
for ( size_t n = 0; n < size(); ++n )
delete (*this)[n];
clear();
}
~wxListLineDataArray() { Clear(); }
};
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxListHeaderWindow (internal) // wxListHeaderWindow (internal)
@@ -817,7 +828,7 @@ protected:
n = 0; n = 0;
} }
return &m_lines[n]; return m_lines[n];
} }
// get a dummy line which can be used for geometry calculations and such: // get a dummy line which can be used for geometry calculations and such:

View File

@@ -113,9 +113,6 @@ static const int MARGIN_AROUND_CHECKBOX = 5;
#include "wx/listimpl.cpp" #include "wx/listimpl.cpp"
WX_DEFINE_LIST(wxListItemDataList) WX_DEFINE_LIST(wxListItemDataList)
#include "wx/arrimpl.cpp"
WX_DEFINE_OBJARRAY(wxListLineDataArray)
#include "wx/listimpl.cpp" #include "wx/listimpl.cpp"
WX_DEFINE_LIST(wxListHeaderDataList) WX_DEFINE_LIST(wxListHeaderDataList)
@@ -602,7 +599,7 @@ void wxListLineData::SetItem( int index, const wxListItem &info )
item->SetItem( info ); item->SetItem( info );
} }
void wxListLineData::GetItem( int index, wxListItem &info ) void wxListLineData::GetItem( int index, wxListItem &info ) const
{ {
wxListItemDataList::compatibility_iterator node = m_items.Item( index ); wxListItemDataList::compatibility_iterator node = m_items.Item( index );
if (node) if (node)
@@ -1667,7 +1664,7 @@ void wxListMainWindow::SetReportView(bool inReportView)
const size_t count = m_lines.size(); const size_t count = m_lines.size();
for ( size_t n = 0; n < count; n++ ) for ( size_t n = 0; n < count; n++ )
{ {
m_lines[n].SetReportView(inReportView); m_lines[n]->SetReportView(inReportView);
} }
} }
@@ -1697,23 +1694,23 @@ wxListLineData *wxListMainWindow::GetDummyLine() const
// we need to recreate the dummy line if the number of columns in the // we need to recreate the dummy line if the number of columns in the
// control changed as it would have the incorrect number of fields // control changed as it would have the incorrect number of fields
// otherwise // otherwise
if ( !m_lines.IsEmpty() && if ( !m_lines.empty() &&
m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() ) m_lines[0]->m_items.GetCount() != (size_t)GetColumnCount() )
{ {
self->m_lines.Clear(); self->m_lines.Clear();
} }
if ( m_lines.IsEmpty() ) if ( m_lines.empty() )
{ {
wxListLineData *line = new wxListLineData(self); wxListLineData *line = new wxListLineData(self);
self->m_lines.Add(line); self->m_lines.push_back(line);
// don't waste extra memory -- there never going to be anything // don't waste extra memory -- there never going to be anything
// else/more in this array // else/more in this array
self->m_lines.Shrink(); self->m_lines.shrink_to_fit();
} }
return &m_lines[0]; return m_lines[0];
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -3566,7 +3563,7 @@ void wxListMainWindow::GetItem( wxListItem &item ) const
size_t wxListMainWindow::GetItemCount() const size_t wxListMainWindow::GetItemCount() const
{ {
return IsVirtual() ? m_countVirt : m_lines.GetCount(); return IsVirtual() ? m_countVirt : m_lines.size();
} }
void wxListMainWindow::SetItemCount(long count) void wxListMainWindow::SetItemCount(long count)
@@ -4084,7 +4081,8 @@ void wxListMainWindow::DeleteItem( long lindex )
} }
else else
{ {
m_lines.RemoveAt( index ); delete m_lines[index];
m_lines.erase( m_lines.begin() + index );
} }
// we need to refresh the (vert) scrollbar as the number of items changed // we need to refresh the (vert) scrollbar as the number of items changed
@@ -4113,7 +4111,7 @@ void wxListMainWindow::DeleteColumn( int col )
if ( !IsVirtual() ) if ( !IsVirtual() )
{ {
// update all the items // update all the items
for ( size_t i = 0; i < m_lines.GetCount(); i++ ) for ( size_t i = 0; i < m_lines.size(); i++ )
{ {
wxListLineData * const line = GetLine(i); wxListLineData * const line = GetLine(i);
@@ -4365,7 +4363,7 @@ void wxListMainWindow::InsertItem( wxListItem &item )
} }
} }
m_lines.Insert( line, id ); m_lines.insert( m_lines.begin() + id, line );
m_dirty = true; m_dirty = true;
@@ -4412,7 +4410,7 @@ long wxListMainWindow::InsertColumn( long col, const wxListItem &item )
if ( !IsVirtual() ) if ( !IsVirtual() )
{ {
// update all the items // update all the items
for ( size_t i = 0; i < m_lines.GetCount(); i++ ) for ( size_t i = 0; i < m_lines.size(); i++ )
{ {
wxListLineData * const line = GetLine(i); wxListLineData * const line = GetLine(i);
wxListItemData * const data = new wxListItemData(this); wxListItemData * const data = new wxListItemData(this);
@@ -4457,21 +4455,28 @@ int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
// sorting // sorting
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
static wxListCtrlCompare list_ctrl_compare_func_2; struct wxListLineComparator
static wxIntPtr list_ctrl_compare_data;
static
int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
{ {
wxListLineData *line1 = *arg1; wxListLineComparator(wxListCtrlCompare& f, wxIntPtr data)
wxListLineData *line2 = *arg2; : m_f(f),
wxListItem item; m_data(data)
line1->GetItem( 0, item ); {
wxUIntPtr data1 = item.m_data; }
line2->GetItem( 0, item );
wxUIntPtr data2 = item.m_data; bool operator()(wxListLineData* const& line1,
return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data ); wxListLineData* const& line2) const
} {
wxListItem item;
line1->GetItem( 0, item );
wxUIntPtr data1 = item.m_data;
line2->GetItem( 0, item );
wxUIntPtr data2 = item.m_data;
return m_f(data1, data2, m_data) < 0;
}
const wxListCtrlCompare m_f;
const wxIntPtr m_data;
};
void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data ) void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
{ {
@@ -4480,9 +4485,8 @@ void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
HighlightAll(false); HighlightAll(false);
ResetCurrent(); ResetCurrent();
list_ctrl_compare_func_2 = fn; std::sort(m_lines.begin(), m_lines.end(), wxListLineComparator(fn, data));
list_ctrl_compare_data = data;
m_lines.Sort( list_ctrl_compare_func_1 );
m_dirty = true; m_dirty = true;
} }