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