]> git.tdb.fi Git - libs/gltk.git/blob - source/list.h
Add API declarations
[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() { }
85
86                 virtual void on_style_change();
87         };
88
89         class MultiColumnItem: public Item
90         {
91         protected:
92                 MultiColumnItem() { }
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() { }
114         public:
115                 virtual ~ItemFactory() { }
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;
161         bool own_data;
162         DataObserver *observer;
163         ItemFactory *item_factory;
164         ViewMode view_mode;
165         int sel_index;
166         int focus_index;
167         unsigned first_row;
168         unsigned max_scroll;
169         unsigned view_rows;
170         unsigned view_columns;
171         const Part *items_part;
172         bool ignore_slider_change;
173         bool dragging;
174         int drag_start_x;
175         int drag_start_y;
176
177         VSlider slider;
178         std::vector<Item *> items;
179         std::vector<Row> rows;
180
181 public:
182         List();
183         List(ListData &);
184 private:
185         void init();
186 public:
187         virtual ~List();
188
189         virtual const char *get_class() const { return "list"; }
190
191 private:
192         virtual void autosize_special(const Part &, Geometry &) const;
193
194 public:
195         void set_data(ListData &);
196         ListData &get_data() { return *data; }
197         const ListData &get_data() const { return *data; }
198 private:
199         void items_changed();
200
201 public:
202         template<typename I>
203         void set_item_type()
204         {
205                 ItemFactory *f = new TypedItemFactory<I>(*data);
206                 delete item_factory;
207                 item_factory = f;
208         }
209 private:
210         Item *create_item(unsigned);
211
212 public:
213         void set_view_mode(ViewMode);
214         void set_view_size(unsigned);
215         void set_view_size(unsigned, unsigned);
216         void set_view_all();
217
218         void set_selected_index(int);
219         int get_selected_index() const { return sel_index; }
220 private:
221         void set_selected_item(Widget *);
222
223         virtual void rebuild_special(const Part &);
224         virtual void render_special(const Part &, GL::Renderer &) const;
225
226 public:
227         virtual bool key_press(unsigned, unsigned);
228         virtual void button_press(int, int, unsigned);
229         virtual void touch_press(int, int, unsigned);
230         virtual void touch_release(int, int, unsigned);
231         virtual void touch_motion(int, int, unsigned);
232         virtual void focus_in();
233         virtual bool navigate(Navigation);
234 private:
235         virtual void on_style_change();
236
237         void move_focus(Navigation, bool);
238         void set_focus_index(int);
239
240         void item_autosize_changed(Item *);
241         void reposition_items(bool);
242         unsigned last_to_first_row(unsigned) const;
243         unsigned item_index_to_row(unsigned) const;
244         void check_view_range();
245         void scroll_to_focus();
246         void slider_value_changed(double);
247         static void adjust_index(int &, int, int);
248 };
249
250 MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &);
251
252 } // namespace GLtk
253 } // namespace Msp
254
255 #endif