The listbox model is a new feature which will be introduced into Nana 1.4. The listbox model is to use an existing STL container object.
//Definition of a person
struct person
{
std::string name;
unsigned age;
bool gender;
};
//A container stores persons
std::vector<person> persons;
//Insert 3 persons.
persons.push_back(person{"Steve", 20, true});
persons.push_back(person{"Nolan", 25, true});
persons.push_back(person{"Susan", 21, false});
//A listbox to list persons
nana::listbox lsb(form, nana::rectangle{10, 10, 300, 200});
lsb.append_header("name");
lsb.append_header("age");
lsb.append_header("gender");
//We have a container and a listbox.
//Now we need to defines two function to make the listbox 'recognize'
//the struct person.
auto value_translator = [](const std::vector<nana::listbox::cell>& cells)
{
person p;
p.name = cells[0].text;
p.age = std::stoul(cells[1].text);
p.gender = (cells[2].text == "male");
return p;
};
auto cell_translator = [](const person& p)
{
std::vector<nana::listbox::cell> cells;
cells.emplace_back(p.name);
cells.emplace_back(std::to_string(p.age));
cells.emplace_back(p.gender ? "male" : "female");
return cells;
};
//Everything is ready but last step
lsb.at(0).model<std::recursive_mutex>(persons, value_translator, cell_translator);
1, Standalone model
2, Shared model
2.1, Immutable model
Anonymous