]> git.tdb.fi Git - libs/gltk.git/blob - source/list.h
dd3ddaa6d5e12100deff93187e068af07eaf1ceb
[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 &);
65         public:
66                 void set_active(bool);
67
68                 virtual void render_special(const Part &, GL::Renderer &) const;
69         };
70
71 private:
72         class BasicItem: public Item
73         {
74         private:
75                 Label label;
76
77         public:
78                 BasicItem(const std::string &);
79
80         private:
81                 virtual void on_style_change();
82         };
83
84         class ItemFactory
85         {
86         protected:
87                 ItemFactory() { }
88         public:
89                 virtual ~ItemFactory() { }
90
91                 virtual void set_data(const ListData &) = 0;
92                 virtual Item *create_item(unsigned) const = 0;
93         };
94
95         template<typename I>
96         class TypedItemFactory: public ItemFactory
97         {
98         private:
99                 typedef typename I::ValueType ValueType;
100
101                 const ListDataStore<ValueType> *data;
102
103         public:
104                 TypedItemFactory(const ListData &d)
105                 { set_data(d); }
106
107                 virtual void set_data(const ListData &d)
108                 {
109                         if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
110                                 data = ds;
111                         else
112                                 throw incompatible_data(typeid(ValueType));
113                 }
114
115                 virtual Item *create_item(unsigned i) const
116                 {
117                         return new I(data->get(i));
118                 }
119         };
120
121 public:
122         sigc::signal<void, unsigned> signal_item_selected;
123
124 private:
125         ListData *data;
126         bool own_data;
127         DataObserver *observer;
128         ItemFactory *item_factory;
129         int sel_index;
130         unsigned first;
131         unsigned max_scroll;
132         unsigned view_size;
133
134         VSlider slider;
135         std::vector<Item *> items;
136
137 public:
138         List();
139         List(ListData &);
140 private:
141         void init();
142 public:
143         virtual ~List();
144
145         virtual const char *get_class() const { return "list"; }
146
147 private:
148         virtual void autosize_special(const Part &, Geometry &);
149
150 public:
151         void set_data(ListData &);
152         ListData &get_data() { return *data; }
153         const ListData &get_data() const { return *data; }
154 private:
155         void items_changed();
156
157 public:
158         template<typename I>
159         void set_item_type()
160         {
161                 ItemFactory *f = new TypedItemFactory<I>(*data);
162                 delete item_factory;
163                 item_factory = f;
164         }
165 private:
166         Item *create_item(unsigned);
167
168 public:
169         void set_view_size(unsigned);
170         void set_view_all();
171
172         void set_selected_index(int);
173         int get_selected_index() const { return sel_index; }
174
175 private:
176         virtual void render_special(const Part &, GL::Renderer &) const;
177
178 public:
179         virtual void button_press(int, int, unsigned);
180 private:
181         virtual void on_geometry_change();
182         virtual void on_style_change();
183
184         void reposition_slider();
185         void item_autosize_changed(Item *);
186         void reposition_items();
187         void check_view_range();
188         void slider_value_changed(double);
189 };
190
191 } // namespace GLtk
192 } // namespace Msp
193
194 #endif