X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.h;h=72b5b8890668fdb0c31946c9e17632f7569a6528;hb=b59fab7e533ef96d72c92b224d4f24718bc6b0a1;hp=77d7e9f49166ac53e62c5a06785e0e14bd12ed28;hpb=95210598ff214bbc8d05657aeffc4ce7801f211a;p=libs%2Fgltk.git diff --git a/source/list.h b/source/list.h index 77d7e9f..72b5b88 100644 --- a/source/list.h +++ b/source/list.h @@ -1,76 +1,252 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #ifndef MSP_GLTK_LIST_H_ #define MSP_GLTK_LIST_H_ +#include +#include #include -#include "widget.h" +#include "container.h" +#include "label.h" +#include "listdata.h" +#include "mspgltk_api.h" +#include "slider.h" namespace Msp { namespace GLtk { -class VSlider; +/** +Thrown if a list's item type is incompatible with its data. +*/ +class MSPGLTK_API incompatible_data: public std::logic_error +{ +public: + incompatible_data(const std::type_info &); +}; + /** Shows a list of items, allowing the user to select one. A slider is included to allow scrolling through a long list. */ -class List: public Widget +class MSPGLTK_API List: virtual public Widget, private Container { public: - class Loader: public Widget::Loader + 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 &); }; private: - std::vector items; - int sel_index; - unsigned first; - unsigned n_visible; + /// This exists to make disconnecting signals easier + class DataObserver: public sigc::trackable + { + private: + List &list; - const Part *items_part; + public: + DataObserver(List &); - VSlider *slider; - bool slider_active; + void item_added(std::size_t); + void item_removed(std::size_t); + void cleared(); + void refresh_item(std::size_t); + }; public: - sigc::signal signal_item_selected; + class Item: virtual public Widget, protected Container + { + protected: + Item(); - List(const Resources &); - ~List(); + public: + const char *get_class() const override { return "listitem"; } - void append(const std::string &); - void insert(unsigned, const std::string &); - void remove(unsigned); - void clear(); + protected: + void autosize_special(const Part &, Geometry &) const override; + public: + void set_active(bool); + + 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 &); + + void on_style_change() override; + }; + +private: + class BasicItem: public SimpleItem + { + private: + Label label; + + public: + BasicItem(const std::string &); + }; - const std::string &get_selected() const; + class ItemFactory + { + protected: + ItemFactory() = default; + public: + virtual ~ItemFactory() = default; + + virtual void set_data(const ListData &) = 0; + virtual Item *create_item(std::size_t) const = 0; + }; + + template + class TypedItemFactory: public ItemFactory + { + private: + typedef typename I::ValueType ValueType; + + const ListDataStore *data; + + public: + TypedItemFactory(const ListData &d) { set_data(d); } + + void set_data(const ListData &d) override + { + if(const ListDataStore *ds = dynamic_cast *>(&d)) + data = ds; + else + throw incompatible_data(typeid(ValueType)); + } + + Item *create_item(std::size_t i) const override + { + return new I(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_selection_cleared; + +private: + ListData *data = nullptr; + bool own_data = false; + DataObserver *observer = nullptr; + ItemFactory *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 rows; + +public: + List(); + List(ListData &); + virtual ~List(); + + const char *get_class() const override { return "list"; } + +private: + void autosize_special(const Part &, Geometry &) const override; + +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(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(std::size_t); int get_selected_index() const { return sel_index; } +private: + void set_selected_item(Widget *); - virtual void button_press(int, int, unsigned); - virtual void button_release(int, int, unsigned); - virtual void pointer_motion(int, int); + void rebuild_special(const Part &) override; + void render_special(const Part &, GL::Renderer &) const override; +public: + 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: - virtual const char *get_class() const { return "list"; } - virtual void render_special(const Part &) const; + void on_style_change() override; - virtual void on_geometry_change(); - virtual void on_style_change(); - void reposition_slider(); - void recalculate_parameters(); + void move_focus(Navigation, bool); + void set_focus_index(std::size_t); + + void item_autosize_changed(Item *); + 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(std::size_t &, std::size_t, std::ptrdiff_t); }; +MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &); + } // namespace GLtk } // namespace Msp