]> git.tdb.fi Git - libs/gltk.git/blob - source/list.h
Refactor the traversal logic out of List::check_view_range
[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         unsigned first;
142         unsigned max_scroll;
143         unsigned view_size;
144         bool ignore_slider_change;
145
146         VSlider slider;
147         std::vector<Item *> items;
148
149 public:
150         List();
151         List(ListData &);
152 private:
153         void init();
154 public:
155         virtual ~List();
156
157         virtual const char *get_class() const { return "list"; }
158
159 private:
160         virtual void autosize_special(const Part &, Geometry &) const;
161
162 public:
163         void set_data(ListData &);
164         ListData &get_data() { return *data; }
165         const ListData &get_data() const { return *data; }
166 private:
167         void items_changed();
168
169 public:
170         template<typename I>
171         void set_item_type()
172         {
173                 ItemFactory *f = new TypedItemFactory<I>(*data);
174                 delete item_factory;
175                 item_factory = f;
176         }
177 private:
178         Item *create_item(unsigned);
179
180 public:
181         void set_view_size(unsigned);
182         void set_view_all();
183
184         void set_selected_index(int);
185         int get_selected_index() const { return sel_index; }
186
187 private:
188         virtual void rebuild_special(const Part &);
189         virtual void render_special(const Part &, GL::Renderer &) const;
190
191 public:
192         virtual void button_press(int, int, unsigned);
193
194 private:
195         void item_autosize_changed(Item *);
196         unsigned last_to_first(unsigned) const;
197         void check_view_range();
198         void slider_value_changed(double);
199 };
200
201 } // namespace GLtk
202 } // namespace Msp
203
204 #endif