1 #include <msp/gl/font.h>
20 Dropdown::Dropdown(ListData &d):
28 input_type = INPUT_NAVIGATION;
33 list.set_visible(false);
35 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
36 list.signal_selection_cleared.connect(sigc::mem_fun(this, &Dropdown::list_selection_cleared));
37 list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
40 void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
42 if(part.get_name()=="list")
46 ageom.w = max(ageom.w, list.get_geometry().w);
48 else if(part.get_name()=="text")
50 const Sides &margin = part.get_margin();
51 const GL::Font &font = style->get_font();
52 float font_size = style->get_font_size();
55 const ListData &data = list.get_data();
56 for(unsigned i=0; i<data.size(); ++i)
58 unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
59 max_w = max(max_w, w);
61 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
63 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
64 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
68 void Dropdown::rebuild_special(const Part &part)
70 if(part.get_name()=="text")
71 text.build(part, state, geom, part_cache);
73 Widget::rebuild_special(part);
76 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
78 if(part.get_name()=="list" && dropped)
79 list.render(renderer);
82 void Dropdown::button_press(int x, int y, unsigned btn)
86 Container::button_press(x, y, btn);
94 bool Dropdown::navigate(Navigation nav)
103 else if(nav==NAV_ACTIVATE)
111 void Dropdown::on_geometry_change()
117 void Dropdown::on_style_change()
119 text.set_style(style);
124 void Dropdown::open_list()
127 list.set_visible(true);
130 set_input_focus(&list);
131 signal_grab_pointer.emit();
134 void Dropdown::close_list()
137 list.set_visible(false);
139 signal_ungrab_pointer.emit();
142 void Dropdown::list_autosize_changed()
146 signal_autosize_changed.emit();
149 void Dropdown::resize_list()
152 list.autosize(lgeom);
155 lgeom.w = max(geom.w, lgeom.w);
158 for(Widget *p=parent; p; p=p->get_parent())
160 root_x += p->get_geometry().x;
161 root_y += p->get_geometry().y;
162 if(Root *root = dynamic_cast<Root *>(p))
164 const Geometry &rgeom = root->get_geometry();
165 if(lgeom.h*2>rgeom.h)
172 if(root_y+lgeom.y+lgeom.h>rgeom.h)
173 lgeom.y = rgeom.h-lgeom.h-root_y;
174 if(root_x+lgeom.x+lgeom.w>rgeom.w)
175 lgeom.x = rgeom.w-lgeom.w-root_x;
179 list.set_geometry(lgeom);
182 void Dropdown::list_item_selected(unsigned index)
188 signal_ungrab_pointer.emit();
191 text.set(list.get_data().get_string(index));
193 signal_item_selected.emit(index);
197 void Dropdown::list_selection_cleared()
203 Dropdown::Loader::Loader(Dropdown &d):
204 DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
206 add("item", &Loader::item);
209 void Dropdown::Loader::item(const string &v)
211 dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);