]> git.tdb.fi Git - libs/gltk.git/blob - source/list.h
Add a persistent view size attribute to List
[libs/gltk.git] / source / list.h
1 #ifndef MSP_GLTK_LIST_H_
2 #define MSP_GLTK_LIST_H_
3
4 #include <sigc++/signal.h>
5 #include "container.h"
6 #include "label.h"
7 #include "listdata.h"
8 #include "vslider.h"
9
10 namespace Msp {
11 namespace GLtk {
12
13 /**
14 Shows a list of items, allowing the user to select one.  A slider is included
15 to allow scrolling through a long list.
16 */
17 class List: virtual public Widget, private Container
18 {
19 public:
20         class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
21         {
22         public:
23                 Loader(List &);
24         private:
25                 void item(const std::string &);
26         };
27
28 private:
29         /// This exists to make disconnecting signals easier
30         class DataObserver: public sigc::trackable
31         {
32         private:
33                 List &list;
34
35         public:
36                 DataObserver(List &);
37
38                 void item_added(unsigned);
39                 void item_removed(unsigned);
40                 void cleared();
41                 void refresh_strings();
42         };
43
44 protected:
45         class Item: public Container
46         {
47         public:
48                 virtual const char *get_class() const { return "listitem"; }
49
50                 virtual void autosize();
51                 void set_active(bool);
52
53                 virtual void render_special(const Part &, GL::Renderer &) const;
54         };
55
56 public:
57         class BasicItem: public Item
58         {
59         private:
60                 Label label;
61
62         public:
63                 BasicItem(const std::string &);
64
65         private:
66                 virtual void on_style_change();
67         };
68
69 public:
70         sigc::signal<void, unsigned> signal_item_selected;
71
72 private:
73         ListData *data;
74         bool own_data;
75         DataObserver *observer;
76         int sel_index;
77         unsigned first;
78         unsigned max_scroll;
79         unsigned view_size;
80
81         VSlider slider;
82         std::vector<Item *> items;
83
84 public:
85         List();
86         List(ListData &);
87 private:
88         void init();
89 public:
90         virtual ~List();
91
92         virtual const char *get_class() const { return "list"; }
93
94         virtual void autosize();
95
96         void set_data(ListData &);
97         ListData &get_data() { return *data; }
98         const ListData &get_data() const { return *data; }
99 private:
100         void items_changed();
101 protected:
102         virtual Item *create_item(unsigned);
103
104 public:
105         void set_view_size(unsigned);
106         void set_view_all();
107
108         void set_selected_index(int);
109         int get_selected_index() const { return sel_index; }
110
111 private:
112         virtual void render_special(const Part &, GL::Renderer &) const;
113
114 public:
115         virtual void button_press(int, int, unsigned);
116 private:
117         virtual void on_geometry_change();
118         virtual void on_style_change();
119
120         void reposition_slider();
121         void reposition_items();
122         void check_view_range();
123         void slider_value_changed(double);
124 };
125
126 } // namespace GLtk
127 } // namespace Msp
128
129 #endif