whate wxListCtrl's virtual list mean? how to insert Item?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
wrhclone
In need of some credit
In need of some credit
Posts: 3
Joined: Mon Jan 17, 2011 3:11 am

whate wxListCtrl's virtual list mean? how to insert Item?

Post by wrhclone »

Hello everyone!
I studied Class wxListCtrl many times.
I still don't know what a virtual list meaning!
I read the sample.

void MyFrame::InitWithVirtualItems()
{
m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); if ( m_smallVirtual )
{
m_listCtrl->InsertColumn(0, wxT("Animal"));
m_listCtrl->InsertColumn(1, wxT("Sound"));
m_listCtrl->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS));
}
else
{
m_listCtrl->InsertColumn(0, wxT("First Column"));
m_listCtrl->InsertColumn(1, wxT("Second Column"));
m_listCtrl->SetColumnWidth(0, 150);
m_listCtrl->SetColumnWidth(1, 150);
m_listCtrl->SetItemCount(1000000); }
}
They don't call function insertitem(),
I don't know the list's item how and when being inserted.
Thanks!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Post by PB »

wxWidgets Reference Manual wrote:A special case of report view quite different from the other modes of the list control is a virtual control in which the items data (including text, images and attributes) is managed by the main program and is requested by the control itself only when needed which allows to have controls with millions of items without consuming much memory. To use virtual list control you must use wxListCtrl::SetItemCount first and override at least wxListCtrl::OnGetItemText (and optionally wxListCtrl::OnGetItemImage or wxListCtrl::OnGetItemColumnImage and wxListCtrl::OnGetItemAttr) to return the information about the items when the control requests it.

Virtual list control can be used as a normal one except that no operations which can take time proportional to the number of items in the control happen -- this is required to allow having a practically infinite number of items. For example, in a multiple selection virtual list control, the selections won't be sent when many items are selected at once because this could mean iterating over all the items.
(Emphasis mine)
What exactly you don't understand?

Edit
The simplest implementation might look like this:

Code: Select all

#ifndef WX_PRECOMP
    #include "wx/wx.h"    
#endif

#include <wx/listctrl.h>
#include <vector>

struct CountryInfo {    
    wxString name;
    wxString capital;
    wxString continent;
};

typedef std::vector<CountryInfo> CountryList;

/**** MyListCtrl ****/
class MyListCtrl : public wxListCtrl {
public:
    enum Columns {
        COL_NAME = 0,
        COL_CAPITAL,
        COL_CONTINENT
    };

    MyListCtrl(wxWindow* parent, wxWindowID id = wxID_ANY);
protected:
    virtual wxString OnGetItemText(long    item, long column) const;
private:
    CountryList m_countries;
};

MyListCtrl::MyListCtrl(wxWindow* parent, wxWindowID id)
    : wxListCtrl(parent, id, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_VIRTUAL)
{
    CountryInfo ci;
        
    ci.name = "Canada"; ci.capital = "Ottawa"; ci.continent = "America";
    m_countries.push_back(ci);    
    ci.name = "Germany"; ci.capital = "Berlin"; ci.continent = "Europe";
    m_countries.push_back(ci);    
    ci.name = "Japan"; ci.capital = "Tokyo"; ci.continent = "Asia";
    m_countries.push_back(ci);

    SetItemCount(m_countries.size());
    InsertColumn(COL_NAME, _("Name"));
    InsertColumn(COL_CAPITAL, _("Capital"));
    InsertColumn(COL_CONTINENT, _("Continent"));        
}

wxString MyListCtrl::OnGetItemText(long item, long column) const
{
    wxCHECK_MSG(item >= 0 && item < m_countries.size(),
        "", "Invalid item index in MyListCtrl::OnGetItemText");
    
    switch (column) {
        case COL_NAME:
            return m_countries[item].name; // no break needed
        case COL_CAPITAL:
            return m_countries[item].capital; // no break needed
        case COL_CONTINENT:
            return m_countries[item].continent; // no break needed
        default:
            wxFAIL_MSG("Invalid column index in MyListCtrl::OnGetItemText");
    }
    return "";    
}


/**** MyFrame ****/
class MyFrame : public wxFrame {
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, "List of Countries (wxListCtrl in Virtual Mode)")
    {                           
        MyListCtrl* pListCtrl = new MyListCtrl(this);       
    }    
};

/**** MyApp ****/
class MyApp : public wxApp {
public:    
    virtual bool OnInit()
    {
        if (!wxApp::OnInit())
            return false;    

        MyFrame* pFrame = new MyFrame();                
        pFrame->Show();
        return true;
    }
};

IMPLEMENT_APP(MyApp)
pcunite
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Nov 17, 2004 5:16 pm

Post by pcunite »

Virtual List controls don't contain data, they only enumerate your class whenever they feel like it ... typically when a mouse moves over the control for example. A virtual list is the best way to use a list, you will love it when you get the hang of it. You can easily manage millions of items with ease.
Post Reply