]> git.tdb.fi Git - libs/gltk.git/blob - source/list.h
43a24576a1ecf1904d29d1a58eb104c61a790d00
[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 public:
135         sigc::signal<void, unsigned> signal_item_selected;
136         sigc::signal<void> signal_selection_cleared;
137
138 private:
139         ListData *data;
140         bool own_data;
141         DataObserver *observer;
142         ItemFactory *item_factory;
143         int sel_index;
144         int focus_index;
145         unsigned first;
146         unsigned max_scroll;
147         unsigned view_size;
148         const Part *items_part;
149         bool ignore_slider_change;
150         bool dragging;
151         int drag_start_x;
152         int drag_start_y;
153
154         VSlider slider;
155         std::vector<Item *> items;
156
157 public:
158         List();
159         List(ListData &);
160 private:
161         void init();
162 public:
163         virtual ~List();
164
165         virtual const char *get_class() const { return "list"; }
166
167 private:
168         virtual void autosize_special(const Part &, Geometry &) const;
169
170 public:
171         void set_data(ListData &);
172         ListData &get_data() { return *data; }
173         const ListData &get_data() const { return *data; }
174 private:
175         void items_changed();
176
177 public:
178         template<typename I>
179         void set_item_type()
180         {
181                 ItemFactory *f = new TypedItemFactory<I>(*data);
182                 delete item_factory;
183                 item_factory = f;
184         }
185 private:
186         Item *create_item(unsigned);
187
188 public:
189         void set_view_size(unsigned);
190         void set_view_all();
191
192         void set_selected_index(int);
193         int get_selected_index() const { return sel_index; }
194 private:
195         void set_selected_item(Widget *);
196
197         virtual void rebuild_special(const Part &);
198         virtual void render_special(const Part &, GL::Renderer &) const;
199
200 public:
201         virtual bool key_press(unsigned, unsigned);
202         virtual void button_press(int, int, unsigned);
203         virtual void touch_press(int, int, unsigned);
204         virtual void touch_release(int, int, unsigned);
205         virtual void touch_motion(int, int, unsigned);
206         virtual void focus_in();
207         virtual bool navigate(Navigation);
208 private:
209         virtual void on_style_change();
210
211         void move_focus(Navigation, bool);
212         void set_focus_index(int);
213
214         void item_autosize_changed(Item *);
215         unsigned last_to_first(unsigned) const;
216         void check_view_range();
217         void scroll_to_focus();
218         void slider_value_changed(double);
219         static void adjust_index(int &, int, int);
220 };
221
222 } // namespace GLtk
223 } // namespace Msp
224
225 #endif