X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.h;h=c0ea6d25a5a0b6353fd36bb45248f02659624610;hb=HEAD;hp=3701387455c86f40c5f552a9e426fa2c38fd57c1;hpb=b3de715986f0e5b6359a2109c27d052c3d9cc27a;p=libs%2Fgltk.git diff --git a/source/list.h b/source/list.h index 3701387..5fa687d 100644 --- a/source/list.h +++ b/source/list.h @@ -1,13 +1,15 @@ #ifndef MSP_GLTK_LIST_H_ #define MSP_GLTK_LIST_H_ +#include #include #include #include #include "container.h" #include "label.h" #include "listdata.h" -#include "vslider.h" +#include "mspgltk_api.h" +#include "slider.h" namespace Msp { namespace GLtk { @@ -15,11 +17,10 @@ namespace GLtk { /** Thrown if a list's item type is incompatible with its data. */ -class incompatible_data: public std::logic_error +class MSPGLTK_API incompatible_data: public std::logic_error { public: incompatible_data(const std::type_info &); - virtual ~incompatible_data() throw() { } }; @@ -27,13 +28,22 @@ public: Shows a list of items, allowing the user to select one. A slider is included to allow scrolling through a long list. */ -class List: virtual public Widget, private Container +class MSPGLTK_API List: virtual public Widget, private Container { public: + enum ViewMode + { + LIST, + GRID + }; + + static constexpr size_t INVALID_INDEX = std::numeric_limits::max(); + class Loader: public DataFile::DerivedObjectLoader { public: Loader(List &); + private: void item(const std::string &); }; @@ -48,58 +58,67 @@ private: public: DataObserver(List &); - void item_added(unsigned); - void item_removed(unsigned); + void item_added(std::size_t); + void item_removed(std::size_t); void cleared(); - void refresh_item(unsigned); + void refresh_item(std::size_t); }; public: - class Item: public Container + class Item: virtual public Widget, protected Container { + protected: + Item(); + public: - virtual const char *get_class() const { return "listitem"; } + const char *get_class() const override { return "listitem"; } protected: - virtual void autosize_special(const Part &, Geometry &) const; + void autosize_special(const Part &, Geometry &) const override; public: void set_active(bool); - virtual void render_special(const Part &, GL::Renderer &) const; + void render_special(const Part &, GL::Renderer &) const override; + }; + + class SimpleItem: public Item + { + protected: + SimpleItem() = default; + + void on_style_change() override; }; class MultiColumnItem: public Item { protected: + MultiColumnItem() = default; + virtual void check_widths(std::vector &) const; virtual void set_widths(const std::vector &); - private: - virtual void on_style_change(); + void on_style_change() override; }; private: - class BasicItem: public Item + class BasicItem: public SimpleItem { private: Label label; public: BasicItem(const std::string &); - - private: - virtual void on_style_change(); }; class ItemFactory { protected: - ItemFactory() { } + ItemFactory() = default; public: - virtual ~ItemFactory() { } + virtual ~ItemFactory() = default; virtual void set_data(const ListData &) = 0; - virtual Item *create_item(unsigned) const = 0; + virtual std::unique_ptr create_item(std::size_t) const = 0; }; template @@ -111,10 +130,9 @@ private: const ListDataStore *data; public: - TypedItemFactory(const ListData &d) - { set_data(d); } + TypedItemFactory(const ListData &d) { set_data(d); } - virtual void set_data(const ListData &d) + void set_data(const ListData &d) override { if(const ListDataStore *ds = dynamic_cast *>(&d)) data = ds; @@ -122,46 +140,55 @@ private: throw incompatible_data(typeid(ValueType)); } - virtual Item *create_item(unsigned i) const + std::unique_ptr create_item(std::size_t i) const override { - return new I(data->get(i)); + return std::make_unique(data->get(i)); } }; + struct Row + { + std::size_t first; + unsigned height; + + Row(std::size_t f): first(f), height(0) { } + }; + public: - sigc::signal signal_item_selected; + sigc::signal signal_item_selected; sigc::signal signal_selection_cleared; private: - ListData *data; - bool own_data; - DataObserver *observer; - ItemFactory *item_factory; - int sel_index; - int focus_index; - unsigned first; - unsigned max_scroll; - unsigned view_size; - bool ignore_slider_change; - bool dragging; - int drag_start_x; - int drag_start_y; + std::unique_ptr own_data; + ListData *data = nullptr; + std::unique_ptr observer = nullptr; + std::unique_ptr item_factory = nullptr; + ViewMode view_mode = LIST; + std::size_t sel_index = INVALID_INDEX; + std::size_t focus_index = INVALID_INDEX; + std::size_t first_row = 0; + std::size_t max_scroll = 0; + unsigned view_rows = 5; + unsigned view_columns = 5; + const Part *items_part = nullptr; + bool ignore_slider_change = false; + bool dragging = false; + int drag_start_x = 0; + int drag_start_y = 0; VSlider slider; - std::vector items; + std::vector> items; + std::vector rows; + List(std::unique_ptr); public: List(); List(ListData &); -private: - void init(); -public: - virtual ~List(); - virtual const char *get_class() const { return "list"; } + const char *get_class() const override { return "list"; } private: - virtual void autosize_special(const Part &, Geometry &) const; + void autosize_special(const Part &, Geometry &) const override; public: void set_data(ListData &); @@ -172,47 +199,50 @@ private: public: template - void set_item_type() - { - ItemFactory *f = new TypedItemFactory(*data); - delete item_factory; - item_factory = f; - } + void set_item_type() { item_factory = std::make_unique>(*data); } private: - Item *create_item(unsigned); + std::unique_ptr create_item(std::size_t); public: + void set_view_mode(ViewMode); void set_view_size(unsigned); + void set_view_size(unsigned, unsigned); void set_view_all(); - void set_selected_index(int); + void set_selected_index(std::size_t); 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; + void rebuild_special(const Part &) override; + void render_special(const Part &, GL::Renderer &) const override; 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); + bool key_press(unsigned, unsigned) override; + void button_press(int, int, unsigned) override; + void touch_press(int, int, unsigned) override; + void touch_release(int, int, unsigned) override; + void touch_motion(int, int, unsigned) override; + void focus_in() override; + bool navigate(Navigation) override; private: + void on_style_change() override; + void move_focus(Navigation, bool); - void set_focus_index(int); + void set_focus_index(std::size_t); void item_autosize_changed(Item *); - unsigned last_to_first(unsigned) const; + void reposition_items(bool); + std::size_t last_to_first_row(std::size_t) const; + std::size_t item_index_to_row(std::size_t) const; void check_view_range(); void scroll_to_focus(); void slider_value_changed(double); - static void adjust_index(int &, int, int); + static void adjust_index(std::size_t &, std::size_t, std::ptrdiff_t); }; +MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &); + } // namespace GLtk } // namespace Msp