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