]> git.tdb.fi Git - libs/gltk.git/blob - source/list.h
Implement touch interactions for List
[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 MultiColumnItem: public Item
72         {
73         protected:
74                 virtual void check_widths(std::vector<unsigned> &) const;
75                 virtual void set_widths(const std::vector<unsigned> &);
76
77         private:
78                 virtual void on_style_change();
79         };
80
81 private:
82         class BasicItem: public Item
83         {
84         private:
85                 Label label;
86
87         public:
88                 BasicItem(const std::string &);
89
90         private:
91                 virtual void on_style_change();
92         };
93
94         class ItemFactory
95         {
96         protected:
97                 ItemFactory() { }
98         public:
99                 virtual ~ItemFactory() { }
100
101                 virtual void set_data(const ListData &) = 0;
102                 virtual Item *create_item(unsigned) const = 0;
103         };
104
105         template<typename I>
106         class TypedItemFactory: public ItemFactory
107         {
108         private:
109                 typedef typename I::ValueType ValueType;
110
111                 const ListDataStore<ValueType> *data;
112
113         public:
114                 TypedItemFactory(const ListData &d)
115                 { set_data(d); }
116
117                 virtual void set_data(const ListData &d)
118                 {
119                         if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
120                                 data = ds;
121                         else
122                                 throw incompatible_data(typeid(ValueType));
123                 }
124
125                 virtual Item *create_item(unsigned i) const
126                 {
127                         return new I(data->get(i));
128                 }
129         };
130
131 public:
132         sigc::signal<void, unsigned> signal_item_selected;
133         sigc::signal<void> signal_selection_cleared;
134
135 private:
136         ListData *data;
137         bool own_data;
138         DataObserver *observer;
139         ItemFactory *item_factory;
140         int sel_index;
141         int focus_index;
142         unsigned first;
143         unsigned max_scroll;
144         unsigned view_size;
145         bool ignore_slider_change;
146         bool dragging;
147         int drag_start_x;
148         int drag_start_y;
149
150         VSlider slider;
151         std::vector<Item *> items;
152
153 public:
154         List();
155         List(ListData &);
156 private:
157         void init();
158 public:
159         virtual ~List();
160
161         virtual const char *get_class() const { return "list"; }
162
163 private:
164         virtual void autosize_special(const Part &, Geometry &) const;
165
166 public:
167         void set_data(ListData &);
168         ListData &get_data() { return *data; }
169         const ListData &get_data() const { return *data; }
170 private:
171         void items_changed();
172
173 public:
174         template<typename I>
175         void set_item_type()
176         {
177                 ItemFactory *f = new TypedItemFactory<I>(*data);
178                 delete item_factory;
179                 item_factory = f;
180         }
181 private:
182         Item *create_item(unsigned);
183
184 public:
185         void set_view_size(unsigned);
186         void set_view_all();
187
188         void set_selected_index(int);
189         int get_selected_index() const { return sel_index; }
190 private:
191         void set_selected_item(Widget *);
192
193         virtual void rebuild_special(const Part &);
194         virtual void render_special(const Part &, GL::Renderer &) const;
195
196 public:
197         virtual void button_press(int, int, unsigned);
198         virtual void touch_press(int, int, unsigned);
199         virtual void touch_release(int, int, unsigned);
200         virtual void touch_motion(int, int, unsigned);
201         virtual void focus_in();
202         virtual bool navigate(Navigation);
203 private:
204         void set_focus_index(int);
205
206         void item_autosize_changed(Item *);
207         unsigned last_to_first(unsigned) const;
208         void check_view_range();
209         void scroll_to_focus();
210         void slider_value_changed(double);
211         static void adjust_index(int &, int, int);
212 };
213
214 } // namespace GLtk
215 } // namespace Msp
216
217 #endif