]> git.tdb.fi Git - libs/gltk.git/blob - source/list.h
37d4d7a33201bdf80894508bd8ab8dd58a962fa2
[libs/gltk.git] / source / list.h
1 #ifndef MSP_GLTK_LIST_H_
2 #define MSP_GLTK_LIST_H_
3
4 #include <stdexcept>
5 #include <typeinfo>
6 #include <sigc++/signal.h>
7 #include "container.h"
8 #include "label.h"
9 #include "listdata.h"
10 #include "mspgltk_api.h"
11 #include "slider.h"
12
13 namespace Msp {
14 namespace GLtk {
15
16 /**
17 Thrown if a list's item type is incompatible with its data.
18 */
19 class MSPGLTK_API incompatible_data: public std::logic_error
20 {
21 public:
22         incompatible_data(const std::type_info &);
23 };
24
25
26 /**
27 Shows a list of items, allowing the user to select one.  A slider is included
28 to allow scrolling through a long list.
29 */
30 class MSPGLTK_API List: virtual public Widget, private Container
31 {
32 public:
33         enum ViewMode
34         {
35                 LIST,
36                 GRID
37         };
38
39         class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
40         {
41         public:
42                 Loader(List &);
43         private:
44                 void item(const std::string &);
45         };
46
47 private:
48         /// This exists to make disconnecting signals easier
49         class DataObserver: public sigc::trackable
50         {
51         private:
52                 List &list;
53
54         public:
55                 DataObserver(List &);
56
57                 void item_added(unsigned);
58                 void item_removed(unsigned);
59                 void cleared();
60                 void refresh_item(unsigned);
61         };
62
63 public:
64         class Item: virtual public Widget, protected Container
65         {
66         protected:
67                 Item();
68
69         public:
70                 virtual const char *get_class() const { return "listitem"; }
71
72         protected:
73                 virtual void autosize_special(const Part &, Geometry &) const;
74         public:
75                 void set_active(bool);
76
77                 virtual void render_special(const Part &, GL::Renderer &) const;
78         };
79
80         class SimpleItem: public Item
81         {
82         protected:
83                 SimpleItem() = default;
84
85                 virtual void on_style_change();
86         };
87
88         class MultiColumnItem: public Item
89         {
90         protected:
91                 MultiColumnItem() = default;
92
93                 virtual void check_widths(std::vector<unsigned> &) const;
94                 virtual void set_widths(const std::vector<unsigned> &);
95
96                 virtual void on_style_change();
97         };
98
99 private:
100         class BasicItem: public SimpleItem
101         {
102         private:
103                 Label label;
104
105         public:
106                 BasicItem(const std::string &);
107         };
108
109         class ItemFactory
110         {
111         protected:
112                 ItemFactory() = default;
113         public:
114                 virtual ~ItemFactory() = default;
115
116                 virtual void set_data(const ListData &) = 0;
117                 virtual Item *create_item(unsigned) const = 0;
118         };
119
120         template<typename I>
121         class TypedItemFactory: public ItemFactory
122         {
123         private:
124                 typedef typename I::ValueType ValueType;
125
126                 const ListDataStore<ValueType> *data;
127
128         public:
129                 TypedItemFactory(const ListData &d)
130                 { set_data(d); }
131
132                 virtual void set_data(const ListData &d)
133                 {
134                         if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
135                                 data = ds;
136                         else
137                                 throw incompatible_data(typeid(ValueType));
138                 }
139
140                 virtual Item *create_item(unsigned i) const
141                 {
142                         return new I(data->get(i));
143                 }
144         };
145
146         struct Row
147         {
148                 unsigned first;
149                 unsigned height;
150
151                 Row(unsigned f): first(f), height(0) { }
152         };
153
154 public:
155         sigc::signal<void, unsigned> signal_item_selected;
156         sigc::signal<void> signal_selection_cleared;
157
158 private:
159         ListData *data = nullptr;
160         bool own_data = false;
161         DataObserver *observer = nullptr;
162         ItemFactory *item_factory = nullptr;
163         ViewMode view_mode = LIST;
164         int sel_index = -1;
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;
175
176         VSlider slider;
177         std::vector<Item *> items;
178         std::vector<Row> rows;
179
180 public:
181         List();
182         List(ListData &);
183         virtual ~List();
184
185         virtual const char *get_class() const { return "list"; }
186
187 private:
188         virtual void autosize_special(const Part &, Geometry &) const;
189
190 public:
191         void set_data(ListData &);
192         ListData &get_data() { return *data; }
193         const ListData &get_data() const { return *data; }
194 private:
195         void items_changed();
196
197 public:
198         template<typename I>
199         void set_item_type()
200         {
201                 ItemFactory *f = new TypedItemFactory<I>(*data);
202                 delete item_factory;
203                 item_factory = f;
204         }
205 private:
206         Item *create_item(unsigned);
207
208 public:
209         void set_view_mode(ViewMode);
210         void set_view_size(unsigned);
211         void set_view_size(unsigned, unsigned);
212         void set_view_all();
213
214         void set_selected_index(int);
215         int get_selected_index() const { return sel_index; }
216 private:
217         void set_selected_item(Widget *);
218
219         virtual void rebuild_special(const Part &);
220         virtual void render_special(const Part &, GL::Renderer &) const;
221
222 public:
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);
230 private:
231         virtual void on_style_change();
232
233         void move_focus(Navigation, bool);
234         void set_focus_index(int);
235
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);
244 };
245
246 MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &);
247
248 } // namespace GLtk
249 } // namespace Msp
250
251 #endif