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