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