Nana C++ Library  
open-source C++ framework project 
The Programmer's Guide 
nana::gui::listbox

Description

The listbox is a rectangle containing a list of strings from which the user can select.

Model of

Widget Window 

Public base classes

class widget_object<widget_tag, DrawerTrigger>: public widget
concepts::any_objective

Typedefs

template<typename T> class resolver_interface An interface for the item resolver. (See Note 1)
size_type An unsigned integer type.
index_pair_t A pair type.
cat_proxy A category proxy type used to access the categories.
item_proxy An item proxy type used to access the items of a category.
selection A container type for items.

Members

listbox() The default construction.
listbox(window wd, bool visible) Creates a listbox.
listbox(window wd, const rectangle& r = rectangle(), bool visible = true) Ditto.
template<typename T>
void anyobj(size_type catg, size_type index, const T& t)
Sets an object for the specified item.
template<typename T>
T* anyobj(size_type categ, size_type index)
Returns the address of the object.
template<typename T>
T* anyobj(size_type categ, size_type index) const
Returns the address of the object.
void append_header(const nana::string& text, unsigned width) Appends an item into header column with the text and the specified width.
cat_proxy append(const nana::string&) Appends a new category at the end.
cat_proxy at(std::size_t pos) const Returns a proxy which refers to the pos'th category.
item_proxy at(std::size_t pos, std::size_t index) const Returns a proxy which refers to the index'th item in the pos'th category.
void auto_draw(bool) Set the listbox auto-draw state. If it is true, the listbox will redraw automatically after an operation.
void checkable(bool) Enables or disables the checkable state of the listbox.
selection checked() const Returns the items which are checked.
void clear(size_t cat) Removes the items which are in a specified category.
void clear() Removes the items of all categories.
item_proxy erase(item_proxy) Erases an item.
void erase(size_type cat) Erases a category.
void erase() Erases all categories.
void icon(size_type cat, size_type pos, const nana::paint::image&) Sets an icon for a specified item.
nana::paint::image icon(size_type cat, size_type pos) const Gets an icon of a specified item.
void insert(size_t cat, size_t pos, const nana::string&) Insert a new item.
void move_select(bool upwards) Selects an item besides the current selected item. 
template<typename Resolver>
void resolver(const Resolver&)
Sets an item resolver.
set_sort_compare Refer to the C++03/11 members.
selection selected() const Get the indexs of all the selected items.
void show_header(bool) Set the visible of header column.
size_type size_categ() const Get the number of category item.
size_type size_item() const Returns the number of default category item.
size_type size_item(size_type categ) const Returns the number of items which are the specified cateory.
bool visible_header() const Retrieves the visible of header column.

C++03 Members

void set_sort_compare(size_type subindex, nana::functor<bool(
   const nana::string&,
   nana::any*,
   const nana::string&,
   nana::any*, bool reverse)>& strick_ordering)
Sets a strick weak ordering comparer for a column. (See Note 4)

C++11 Members

void set_sort_compare(size_type subindex,
std::function<bool(
   const nana::string&,
   nana::any*,
   const nana::string&,
   nana::any*, bool reverse)> strick_ordering)
Sets a strick weak ordering comparer for a column. (See Note 4)


Category Proxy (on concept of Input Iterator)
template<typename T>
item_proxy append(const T& t)
Appends a new item, and the object 't' will be resolved by current resolver.
std::size_t columns() const Return the number of columns.
void push_back(const nana::string&) Inserts a new item at the end.
void push_back(nana::string&&)   since C++11  
item_proxy begin() const Returns an item_proxy referring to the beginning of the category.
item_proxy end() const Returns an item_proxy referring to the end of the category.
item_proxy cbegin() const Returns an item_proxy referring to the beginning of the category.
item_proxy cend() const Returns an item_proxy referring to the end of the category.
item_proxy at(std::size_t pos) const Returns an item_proxy referring to the pos'th item.
std::size_t size() const Returns the number of items.

Item Proxy (on the concept of Input Iterator)
item_proxy & bgcolor(nana::color_t) Set the background color for the item.
nana::color_t bgcolor() const Get the background color of the item.
item_proxy& fgcolor(nana::color_t) Set the foreground color of the item.
nana::color_t fgcolor() const Get the foreground color of the item.
item_proxy& check(bool)  Check/Uncheck the item.
bool checked() const Determinte whether the item is checked.
std::size_t columns() const Returns the number of columns.
std::pair<std::size_t, std::size_t> pos() const Returns the index pair of the item.
template<typename T> item_proxy & resolve(const T& t) Set the item with the specified object t, and the t will be resolved by current resolver.
template<typename T> T resolver() const Get a T object which is resolved by the item.
item_proxy & select(bool) Select the item.
bool selected() const Determines whether the item is selected.
item_proxy & text(std::size_t pos, const nana::string&) Set the text of a specified column.
item_proxy & text(std::size_t pos, nana::string&&) since C++11 Set the text of a specified column.
nana::string text(std::size_t pos) const Get the text of a specified column.
template<typename T> T* value_ptr() const Returns an address of the user object. If the user object is empty or the specified type is mismatched, it returns a nullptr.
template<typename T> T& value() const Returns a reference of the user object. If the user object is not existing or the specified type is mismatched, it throws an exception.
template<typename T> item_proxy & value(const T&) Set a user object.
bool operator==(const nana::string& s) const Returns true when the text of the first column is equal to the s.
bool operator==(const char* s) const Ditto.
bool operator==(const wchar_t* s) const Ditto.




File

nana/gui/widgets/listbox.hpp

Notes

1, The resolver is used to resolute an object of the specified type for a listbox item.

#include <nana/gui/wvl.hpp>
#include <nana/gui/widgets/listbox.hpp>

struct person
{
    nana::string name;
    bool gender;
    unsigned age;
};

class resolver
    : public nana::gui::listbox::resolver_interface<person>
{
    nana::string decode(std::size_t i, const person& pr) const
    {
        std::stringstream ss;
        switch(i)
        {
        case 0:
            return pr.name;
        case 1:
            return (pr.gender ? STR("Male") : STR("Female"));
        case 2:
            ss<<pr.age;
            return nana::charset(ss.str());
        }
        return nana::string();
    }

    void encode(person& pr, std::size_t i, const nana::string& s)
    {
        std::stringstream ss;
        switch(i)
        {
        case 0:
            pr.name = s;
            break;
        case 1:
            pr.gender = (s == STR("Male"));
            break;
        case 2:
            ss<<static_cast<std::string>(nana::charset(s));
            ss>>pr.age;
            break;
        }
    }
}

int main()
{
    using namespace nana::gui;

    form fm;
    listbox  lb(fm, nana::rectangle(10, 10, 280, 120));
    lb.append_header(STR("Name"), 100);
    lb.append_header(STR("Gender"), 60);
    lb.append_header(STR("Age"), 60);

    lb.resolver(resolver()); //Pass the user defined resolver to the listbox

    person pr;
    pr.name = STR("Peter");
    pr.gender = true;
    pr.age = 10;

    lb.at(0).append(pr);  //person to item. resolver::decode() is required
   
    lb.at(0, 0).resolve_to(pr); //item to person. resolver::encode() is required

    fm.show();
    exec();
}

2, The user object is an object which needs to be associated with an item.

int main()
{
    using namespace nana::gui;
    form fm;
    listbox lb(fm, nana::rectangle(10, 10, 280, 120));
    lb.append_header(STR("Header"), 200);
    lb.append_item(STR("int"));
    lb.append_item(STR("double"));

    lb.at(0, 0).value(10);
    lb.at(0, 1).value(0.1);

    int i = lb.at(0, 0).value<int>();          //it throws an exception if there is not an int object to be bound.
    int * p = lb.at(0, 0).value_ptr<double>();  //it returns a nullptr if there is not an double object to be bound.

    fm.show();
    exec();
}



3, A sort compare is used for sorting the items. It is a strick weak ordering comparer that must meet the requirement:
Irreflexivity(comp(x, x) returns false) and antisymmetry(comp(a, b) != comp(b, a) returns true)

A simple example.

bool sort_compare(const nana::string& s1, nana::any*, const nana::string& s2, nana::any*, bool reverse)
{
    return (reverse ? s1 > s2 : s1 < s2);
}

listbox.set_sort_compare(0, sort_compare);


The listbox supports attaching a customer's object for each item, therefore the items can be sorted by comparing these customer's object.

bool sort_compare(const nana::string&, nana::any* o1, const nana::string&, nana::any* o2, bool reverse)
{
    if(o1 && o2) //some items may not attach a customer object.
    {
        int * i1 = o1->get<int>();
        int * i2 = o2->get<int>();
        return (i1 && i2 && (reverse ? *i1 > *i2 : *i1 < *i2)); //some types may not be int.
    }
    return false;
}

listbox.at(0, 0).value(10); //the type of customer's object is int.
listbox.at(0, 0).value(20);




See also


None.


Move to The Nana Programmer's Guide Main Page