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