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