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