+#include <msp/debug/demangle.h>
#include <msp/gl/matrix.h>
#include <msp/gl/meshbuilder.h>
#include "graphic.h"
namespace Msp {
namespace GLtk {
+incompatible_data::incompatible_data(const type_info &ti):
+ logic_error("expected "+Debug::demangle(ti.name()))
+{ }
+
+
List::List():
data(new BasicListData<string>),
own_data(true)
void List::init()
{
+ item_factory = 0;
sel_index = -1;
first = 0;
max_scroll = 0;
List::~List()
{
+ delete item_factory;
delete observer;
if(own_data)
delete data;
void List::set_data(ListData &d)
{
+ if(item_factory)
+ item_factory->set_data(d);
+
delete observer;
if(own_data)
delete data;
List::Item *List::create_item(unsigned index)
{
- return new BasicItem(data->get_string(index));
+ if(item_factory)
+ return item_factory->create_item(index);
+ else
+ return new BasicItem(data->get_string(index));
}
void List::set_view_size(unsigned s)
#ifndef MSP_GLTK_LIST_H_
#define MSP_GLTK_LIST_H_
+#include <stdexcept>
+#include <typeinfo>
#include <sigc++/signal.h>
#include "container.h"
#include "label.h"
namespace Msp {
namespace GLtk {
+/**
+Thrown if a list's item type is incompatible with its data.
+*/
+class incompatible_data: public std::logic_error
+{
+public:
+ incompatible_data(const std::type_info &);
+ virtual ~incompatible_data() throw() { }
+};
+
+
/**
Shows a list of items, allowing the user to select one. A slider is included
to allow scrolling through a long list.
void refresh_item(unsigned);
};
-protected:
+public:
class Item: public Container
{
public:
virtual void render_special(const Part &, GL::Renderer &) const;
};
-public:
+private:
class BasicItem: public Item
{
private:
virtual void on_style_change();
};
+ class ItemFactory
+ {
+ protected:
+ ItemFactory() { }
+ public:
+ virtual ~ItemFactory() { }
+
+ virtual void set_data(const ListData &) = 0;
+ virtual Item *create_item(unsigned) const = 0;
+ };
+
+ template<typename I>
+ class TypedItemFactory: public ItemFactory
+ {
+ private:
+ typedef typename I::ValueType ValueType;
+
+ const ListDataStore<ValueType> *data;
+
+ public:
+ TypedItemFactory(const ListData &d)
+ { set_data(d); }
+
+ virtual void set_data(const ListData &d)
+ {
+ if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
+ data = ds;
+ else
+ throw incompatible_data(typeid(ValueType));
+ }
+
+ virtual Item *create_item(unsigned i) const
+ {
+ return new I(data->get(i));
+ }
+ };
+
public:
sigc::signal<void, unsigned> signal_item_selected;
ListData *data;
bool own_data;
DataObserver *observer;
+ ItemFactory *item_factory;
int sel_index;
unsigned first;
unsigned max_scroll;
const ListData &get_data() const { return *data; }
private:
void items_changed();
-protected:
- virtual Item *create_item(unsigned);
+
+public:
+ template<typename I>
+ void set_item_type()
+ {
+ ItemFactory *f = new TypedItemFactory<I>(*data);
+ delete item_factory;
+ item_factory = f;
+ }
+private:
+ Item *create_item(unsigned);
public:
void set_view_size(unsigned);