]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.h
Use a different approach for custom item widgets
[libs/gltk.git] / source / list.h
index 14d9eac291e3bb2b7daee7c9862a268108689182..351b0a461e27aed15c0d271835317301926b70aa 100644 (file)
@@ -1,6 +1,8 @@
 #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.
@@ -41,7 +54,7 @@ private:
                void refresh_item(unsigned);
        };
 
-protected:
+public:
        class Item: public Container
        {
        public:
@@ -55,7 +68,7 @@ protected:
                virtual void render_special(const Part &, GL::Renderer &) const;
        };
 
-public:
+private:
        class BasicItem: public Item
        {
        private:
@@ -68,6 +81,43 @@ public:
                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;
 
@@ -75,6 +125,7 @@ private:
        ListData *data;
        bool own_data;
        DataObserver *observer;
+       ItemFactory *item_factory;
        int sel_index;
        unsigned first;
        unsigned max_scroll;
@@ -102,8 +153,17 @@ public:
        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);