]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
Improve Dropdown list management
[libs/gltk.git] / source / dropdown.cpp
1 #include <msp/gl/font.h>
2 #include "dropdown.h"
3 #include "list.h"
4 #include "panel.h"
5 #include "part.h"
6 #include "root.h"
7 #include "style.h"
8 #include "text.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GLtk {
14
15 Dropdown::Dropdown()
16 {
17         init();
18 }
19
20 Dropdown::Dropdown(ListData &d):
21         list(d)
22 {
23         init();
24 }
25
26 void Dropdown::init()
27 {
28         dropped = false;
29
30         add(list);
31         list.set_visible(false);
32         list.set_view_all();
33         list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
34         list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
35 }
36
37 void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
38 {
39         if(part.get_name()=="list")
40         {
41                 Geometry lgeom;
42                 list.autosize(lgeom);
43                 ageom.w = max(ageom.w, list.get_geometry().w);
44         }
45         else if(part.get_name()=="text")
46         {
47                 const Sides &margin = part.get_margin();
48                 const GL::Font &font = style->get_font();
49                 float font_size = style->get_font_size();
50
51                 unsigned max_w = 0;
52                 const ListData &data = list.get_data();
53                 for(unsigned i=0; i<data.size(); ++i)
54                 {
55                         unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
56                         max_w = max(max_w, w);
57                 }
58                 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
59
60                 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
61                 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
62         }
63 }
64
65 void Dropdown::set_selected_index(int index)
66 {
67         list.set_selected_index(index);
68         if(index<0)
69                 text.set(string());
70 }
71
72 void Dropdown::rebuild_special(const Part &part)
73 {
74         if(part.get_name()=="text")
75                 text.build(part, state, geom, part_cache);
76         else
77                 Widget::rebuild_special(part);
78 }
79
80 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
81 {
82         if(part.get_name()=="list" && dropped)
83                 list.render(renderer);
84 }
85
86 void Dropdown::button_press(int x, int y, unsigned btn)
87 {
88         if(dropped)
89         {
90                 Container::button_press(x, y, btn);
91                 if(!click_focus)
92                 {
93                         dropped = false;
94                         list.set_visible(false);
95                         clear_state(ACTIVE);
96                         signal_ungrab_pointer.emit();
97                 }
98         }
99         else if(btn==1)
100         {
101                 dropped = true;
102                 list.set_visible(true);
103                 resize_list();
104                 set_state(ACTIVE);
105                 signal_grab_pointer.emit();
106         }
107 }
108
109 void Dropdown::on_geometry_change()
110 {
111         if(dropped)
112                 resize_list();
113 }
114
115 void Dropdown::on_style_change()
116 {
117         text.set_style(style);
118         if(dropped)
119                 resize_list();
120 }
121
122 void Dropdown::list_autosize_changed()
123 {
124         if(dropped)
125                 resize_list();
126         signal_autosize_changed.emit();
127 }
128
129 void Dropdown::resize_list()
130 {
131         Geometry lgeom;
132         list.autosize(lgeom);
133         lgeom.x = 0;
134         lgeom.y = -lgeom.h;
135         lgeom.w = max(geom.w, lgeom.w);
136         int root_x = geom.x;
137         int root_y = geom.y;
138         for(Widget *p=parent; p; p=p->get_parent())
139         {
140                 root_x += p->get_geometry().x;
141                 root_y += p->get_geometry().y;
142                 if(Root *root = dynamic_cast<Root *>(p))
143                 {
144                         const Geometry &rgeom = root->get_geometry();
145                         if(lgeom.h*2>rgeom.h)
146                         {
147                                 lgeom.h = rgeom.h/2;
148                                 lgeom.y = -lgeom.h;
149                         }
150                         if(root_y+lgeom.y<0)
151                                 lgeom.y = -root_y;
152                         if(root_y+lgeom.y+lgeom.h>rgeom.h)
153                                 lgeom.y = rgeom.h-lgeom.h-root_y;
154                         if(root_x+lgeom.x+lgeom.w>rgeom.w)
155                                 lgeom.x = rgeom.w-lgeom.w-root_x;
156                         break;
157                 }
158         }
159         list.set_geometry(lgeom);
160 }
161
162 void Dropdown::list_item_selected(unsigned index)
163 {
164         if(dropped)
165         {
166                 dropped = false;
167                 clear_state(ACTIVE);
168                 signal_ungrab_pointer.emit();
169         }
170
171         text.set(list.get_data().get_string(index));
172
173         signal_item_selected.emit(index);
174         rebuild();
175 }
176
177
178 Dropdown::Loader::Loader(Dropdown &d):
179         DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
180 {
181         add("item", &Loader::item);
182 }
183
184 void Dropdown::Loader::item(const string &v)
185 {
186         dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);
187 }
188
189 } // namespace GLtk
190 } // namespace Msp