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 class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
44 void item(const std::string &);
48 /// This exists to make disconnecting signals easier
49 class DataObserver: public sigc::trackable
57 void item_added(unsigned);
58 void item_removed(unsigned);
60 void refresh_item(unsigned);
64 class Item: virtual public Widget, protected Container
70 virtual const char *get_class() const { return "listitem"; }
73 virtual void autosize_special(const Part &, Geometry &) const;
75 void set_active(bool);
77 virtual void render_special(const Part &, GL::Renderer &) const;
80 class SimpleItem: public Item
83 SimpleItem() = default;
85 virtual void on_style_change();
88 class MultiColumnItem: public Item
91 MultiColumnItem() = default;
93 virtual void check_widths(std::vector<unsigned> &) const;
94 virtual void set_widths(const std::vector<unsigned> &);
96 virtual void on_style_change();
100 class BasicItem: public SimpleItem
106 BasicItem(const std::string &);
112 ItemFactory() = default;
114 virtual ~ItemFactory() = default;
116 virtual void set_data(const ListData &) = 0;
117 virtual Item *create_item(unsigned) const = 0;
121 class TypedItemFactory: public ItemFactory
124 typedef typename I::ValueType ValueType;
126 const ListDataStore<ValueType> *data;
129 TypedItemFactory(const ListData &d)
132 virtual void set_data(const ListData &d)
134 if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
137 throw incompatible_data(typeid(ValueType));
140 virtual Item *create_item(unsigned i) const
142 return new I(data->get(i));
151 Row(unsigned f): first(f), height(0) { }
155 sigc::signal<void, unsigned> signal_item_selected;
156 sigc::signal<void> signal_selection_cleared;
159 ListData *data = nullptr;
160 bool own_data = false;
161 DataObserver *observer = nullptr;
162 ItemFactory *item_factory = nullptr;
163 ViewMode view_mode = LIST;
165 int focus_index = -1;
166 unsigned first_row = 0;
167 unsigned max_scroll = 0;
168 unsigned view_rows = 5;
169 unsigned view_columns = 5;
170 const Part *items_part = nullptr;
171 bool ignore_slider_change = false;
172 bool dragging = false;
173 int drag_start_x = 0;
174 int drag_start_y = 0;
177 std::vector<Item *> items;
178 std::vector<Row> rows;
185 virtual const char *get_class() const { return "list"; }
188 virtual void autosize_special(const Part &, Geometry &) const;
191 void set_data(ListData &);
192 ListData &get_data() { return *data; }
193 const ListData &get_data() const { return *data; }
195 void items_changed();
201 ItemFactory *f = new TypedItemFactory<I>(*data);
206 Item *create_item(unsigned);
209 void set_view_mode(ViewMode);
210 void set_view_size(unsigned);
211 void set_view_size(unsigned, unsigned);
214 void set_selected_index(int);
215 int get_selected_index() const { return sel_index; }
217 void set_selected_item(Widget *);
219 virtual void rebuild_special(const Part &);
220 virtual void render_special(const Part &, GL::Renderer &) const;
223 virtual bool key_press(unsigned, unsigned);
224 virtual void button_press(int, int, unsigned);
225 virtual void touch_press(int, int, unsigned);
226 virtual void touch_release(int, int, unsigned);
227 virtual void touch_motion(int, int, unsigned);
228 virtual void focus_in();
229 virtual bool navigate(Navigation);
231 virtual void on_style_change();
233 void move_focus(Navigation, bool);
234 void set_focus_index(int);
236 void item_autosize_changed(Item *);
237 void reposition_items(bool);
238 unsigned last_to_first_row(unsigned) const;
239 unsigned item_index_to_row(unsigned) const;
240 void check_view_range();
241 void scroll_to_focus();
242 void slider_value_changed(double);
243 static void adjust_index(int &, int, int);
246 MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &);