X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.h;h=6f6aec1f9dcf50d2dd5b5ead0fb66db4790b5d72;hb=707b59d45ae50b69c94918f8f74313283b304597;hp=1ecd854c26e048ddd9a10ba6178d2752e5f2b494;hpb=1d84ac50711ce9d23cb1dafd83158d4ffa938a15;p=libs%2Fgltk.git diff --git a/source/list.h b/source/list.h index 1ecd854..6f6aec1 100644 --- a/source/list.h +++ b/source/list.h @@ -1,14 +1,28 @@ #ifndef MSP_GLTK_LIST_H_ #define MSP_GLTK_LIST_H_ +#include +#include #include #include "container.h" +#include "label.h" #include "listdata.h" #include "vslider.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. @@ -16,7 +30,7 @@ to allow scrolling through a long list. class List: virtual public Widget, private Container { public: - class Loader: public Widget::Loader + class Loader: public DataFile::DerivedObjectLoader { public: Loader(List &); @@ -37,7 +51,71 @@ private: void item_added(unsigned); void item_removed(unsigned); void cleared(); - void refresh_strings(); + void refresh_item(unsigned); + }; + +public: + class Item: public Container + { + public: + virtual const char *get_class() const { return "listitem"; } + + protected: + virtual void autosize_special(const Part &, Geometry &) const; + public: + void set_active(bool); + + virtual void render_special(const Part &, GL::Renderer &) const; + }; + +private: + class BasicItem: public Item + { + private: + Label label; + + public: + BasicItem(const std::string &); + + 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 + class TypedItemFactory: public ItemFactory + { + private: + typedef typename I::ValueType ValueType; + + const ListDataStore *data; + + public: + TypedItemFactory(const ListData &d) + { set_data(d); } + + virtual void set_data(const ListData &d) + { + if(const ListDataStore *ds = dynamic_cast *>(&d)) + data = ds; + else + throw incompatible_data(typeid(ValueType)); + } + + virtual Item *create_item(unsigned i) const + { + return new I(data->get(i)); + } }; public: @@ -47,14 +125,14 @@ private: ListData *data; bool own_data; DataObserver *observer; + ItemFactory *item_factory; int sel_index; unsigned first; - unsigned n_visible; - unsigned row_height; - - const Part *items_part; + unsigned max_scroll; + unsigned view_size; VSlider slider; + std::vector items; public: List(); @@ -66,22 +144,35 @@ public: virtual const char *get_class() const { return "list"; } - virtual void autosize(); - void autosize_rows(unsigned); - void autosize_all(); +private: + virtual void autosize_special(const Part &, Geometry &) const; +public: void set_data(ListData &); ListData &get_data() { return *data; } const ListData &get_data() const { return *data; } private: void items_changed(); + +public: + template + void set_item_type() + { + ItemFactory *f = new TypedItemFactory(*data); + delete item_factory; + item_factory = f; + } +private: + Item *create_item(unsigned); + public: + void set_view_size(unsigned); + void set_view_all(); void set_selected_index(int); int get_selected_index() const { return sel_index; } private: - virtual void rebuild_special(const Part &, CachedPart &); virtual void render_special(const Part &, GL::Renderer &) const; public: @@ -91,6 +182,8 @@ private: virtual void on_style_change(); void reposition_slider(); + void item_autosize_changed(Item *); + void reposition_items(); void check_view_range(); void slider_value_changed(double); };