1 #ifndef MSP_GLTK_LIST_H_
2 #define MSP_GLTK_LIST_H_
6 #include <sigc++/signal.h>
10 #include "mspgltk_api.h"
17 Thrown if a list's item type is incompatible with its data.
19 class MSPGLTK_API incompatible_data: public std::logic_error
22 incompatible_data(const std::type_info &);
27 Shows a list of items, allowing the user to select one. A slider is included
28 to allow scrolling through a long list.
30 class MSPGLTK_API List: virtual public Widget, private Container
39 static constexpr size_t INVALID_INDEX = std::numeric_limits<size_t>::max();
41 class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
47 void item(const std::string &);
51 /// This exists to make disconnecting signals easier
52 class DataObserver: public sigc::trackable
60 void item_added(std::size_t);
61 void item_removed(std::size_t);
63 void refresh_item(std::size_t);
67 class Item: virtual public Widget, protected Container
73 const char *get_class() const override { return "listitem"; }
76 void autosize_special(const Part &, Geometry &) const override;
78 void set_active(bool);
80 void render_special(const Part &, GL::Renderer &) const override;
83 class SimpleItem: public Item
86 SimpleItem() = default;
88 void on_style_change() override;
91 class MultiColumnItem: public Item
94 MultiColumnItem() = default;
96 virtual void check_widths(std::vector<unsigned> &) const;
97 virtual void set_widths(const std::vector<unsigned> &);
99 void on_style_change() override;
103 class BasicItem: public SimpleItem
109 BasicItem(const std::string &);
115 ItemFactory() = default;
117 virtual ~ItemFactory() = default;
119 virtual void set_data(const ListData &) = 0;
120 virtual Item *create_item(std::size_t) const = 0;
124 class TypedItemFactory: public ItemFactory
127 typedef typename I::ValueType ValueType;
129 const ListDataStore<ValueType> *data;
132 TypedItemFactory(const ListData &d) { set_data(d); }
134 void set_data(const ListData &d) override
136 if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
139 throw incompatible_data(typeid(ValueType));
142 Item *create_item(std::size_t i) const override
144 return new I(data->get(i));
153 Row(std::size_t f): first(f), height(0) { }
157 sigc::signal<void, std::size_t> signal_item_selected;
158 sigc::signal<void> signal_selection_cleared;
161 ListData *data = nullptr;
162 bool own_data = false;
163 DataObserver *observer = nullptr;
164 ItemFactory *item_factory = nullptr;
165 ViewMode view_mode = LIST;
166 std::size_t sel_index = INVALID_INDEX;
167 std::size_t focus_index = INVALID_INDEX;
168 std::size_t first_row = 0;
169 std::size_t max_scroll = 0;
170 unsigned view_rows = 5;
171 unsigned view_columns = 5;
172 const Part *items_part = nullptr;
173 bool ignore_slider_change = false;
174 bool dragging = false;
175 int drag_start_x = 0;
176 int drag_start_y = 0;
179 std::vector<Item *> items;
180 std::vector<Row> rows;
187 const char *get_class() const override { return "list"; }
190 void autosize_special(const Part &, Geometry &) const override;
193 void set_data(ListData &);
194 ListData &get_data() { return *data; }
195 const ListData &get_data() const { return *data; }
197 void items_changed();
203 ItemFactory *f = new TypedItemFactory<I>(*data);
208 Item *create_item(std::size_t);
211 void set_view_mode(ViewMode);
212 void set_view_size(unsigned);
213 void set_view_size(unsigned, unsigned);
216 void set_selected_index(std::size_t);
217 int get_selected_index() const { return sel_index; }
219 void set_selected_item(Widget *);
221 void rebuild_special(const Part &) override;
222 void render_special(const Part &, GL::Renderer &) const override;
225 bool key_press(unsigned, unsigned) override;
226 void button_press(int, int, unsigned) override;
227 void touch_press(int, int, unsigned) override;
228 void touch_release(int, int, unsigned) override;
229 void touch_motion(int, int, unsigned) override;
230 void focus_in() override;
231 bool navigate(Navigation) override;
233 void on_style_change() override;
235 void move_focus(Navigation, bool);
236 void set_focus_index(std::size_t);
238 void item_autosize_changed(Item *);
239 void reposition_items(bool);
240 std::size_t last_to_first_row(std::size_t) const;
241 std::size_t item_index_to_row(std::size_t) const;
242 void check_view_range();
243 void scroll_to_focus();
244 void slider_value_changed(double);
245 static void adjust_index(std::size_t &, std::size_t, std::ptrdiff_t);
248 MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &);