X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.h;h=ef4311e0702c73b818a19ab3abaadb07e1427abb;hb=7e328215742ee672b1a93e5f5f358828c53ce921;hp=14d9eac291e3bb2b7daee7c9862a268108689182;hpb=3f77f3542a6405fd832ababe48b950d1a65b00ed;p=libs%2Fgltk.git diff --git a/source/list.h b/source/list.h index 14d9eac..ef4311e 100644 --- a/source/list.h +++ b/source/list.h @@ -1,6 +1,8 @@ #ifndef MSP_GLTK_LIST_H_ #define MSP_GLTK_LIST_H_ +#include +#include #include #include "container.h" #include "label.h" @@ -10,6 +12,17 @@ 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,47 +54,114 @@ private: void refresh_item(unsigned); }; -protected: +public: class Item: public Container { public: virtual const char *get_class() const { return "listitem"; } protected: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_active(bool); virtual void render_special(const Part &, GL::Renderer &) const; }; -public: - class BasicItem: public Item + class SimpleItem: public Item + { + private: + virtual void on_style_change(); + }; + + class MultiColumnItem: public Item + { + protected: + virtual void check_widths(std::vector &) const; + virtual void set_widths(const std::vector &); + + private: + virtual void on_style_change(); + }; + +private: + class BasicItem: public SimpleItem { private: Label label; public: BasicItem(const std::string &); + }; + 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: - virtual void on_style_change(); + 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)); + } + }; + + struct Row + { + unsigned first; + unsigned height; + + Row(unsigned f): first(f), height(0) { } }; public: sigc::signal signal_item_selected; + sigc::signal signal_selection_cleared; private: ListData *data; bool own_data; DataObserver *observer; + ItemFactory *item_factory; int sel_index; - unsigned first; + int focus_index; + unsigned first_row; unsigned max_scroll; - unsigned view_size; + unsigned view_rows; + const Part *items_part; + bool ignore_slider_change; + bool dragging; + int drag_start_x; + int drag_start_y; VSlider slider; std::vector items; + std::vector rows; public: List(); @@ -94,7 +174,7 @@ public: virtual const char *get_class() const { return "list"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_data(ListData &); @@ -102,8 +182,17 @@ public: const ListData &get_data() const { return *data; } private: void items_changed(); -protected: - virtual Item *create_item(unsigned); + +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); @@ -111,20 +200,34 @@ public: void set_selected_index(int); int get_selected_index() const { return sel_index; } - private: + void set_selected_item(Widget *); + + virtual void rebuild_special(const Part &); virtual void render_special(const Part &, GL::Renderer &) const; public: + virtual bool key_press(unsigned, unsigned); virtual void button_press(int, int, unsigned); + virtual void touch_press(int, int, unsigned); + virtual void touch_release(int, int, unsigned); + virtual void touch_motion(int, int, unsigned); + virtual void focus_in(); + virtual bool navigate(Navigation); private: - virtual void on_geometry_change(); virtual void on_style_change(); - void reposition_slider(); - void reposition_items(); + void move_focus(Navigation, bool); + void set_focus_index(int); + + void item_autosize_changed(Item *); + void reposition_items(bool); + unsigned last_to_first_row(unsigned) const; + unsigned item_index_to_row(unsigned) const; void check_view_range(); + void scroll_to_focus(); void slider_value_changed(double); + static void adjust_index(int &, int, int); }; } // namespace GLtk