1 #include <msp/gl/font.h>
20 Dropdown::Dropdown(ListData &d):
28 input_type = INPUT_NAVIGATION;
31 list.set_visible(false);
33 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
34 list.signal_selection_cleared.connect(sigc::mem_fun(this, &Dropdown::list_selection_cleared));
35 list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
38 void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
40 if(part.get_name()=="list")
44 ageom.w = max(ageom.w, list.get_geometry().w);
46 else if(part.get_name()=="text")
48 const Sides &margin = part.get_margin();
49 const GL::Font &font = style->get_font();
50 float font_size = style->get_font_size();
53 const ListData &data = list.get_data();
54 for(unsigned i=0; i<data.size(); ++i)
56 unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
57 max_w = max(max_w, w);
59 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
61 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
62 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
66 void Dropdown::rebuild_special(const Part &part)
68 if(part.get_name()=="text")
69 text.build(part, state, geom, part_cache);
71 Widget::rebuild_special(part);
74 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
76 if(part.get_name()=="list" && dropped)
77 list.render(renderer);
80 void Dropdown::button_press(int x, int y, unsigned btn)
84 Container::button_press(x, y, btn);
92 bool Dropdown::navigate(Navigation nav)
101 else if(nav==NAV_ACTIVATE)
109 void Dropdown::on_size_change()
115 void Dropdown::on_style_change()
117 text.set_style(style);
122 void Dropdown::open_list()
125 list.set_visible(true);
128 set_input_focus(&list);
129 signal_grab_pointer.emit();
132 void Dropdown::close_list()
135 list.set_visible(false);
137 signal_ungrab_pointer.emit();
140 void Dropdown::list_autosize_changed()
144 signal_autosize_changed.emit();
147 void Dropdown::resize_list()
150 list.autosize(lgeom);
153 lgeom.w = max(geom.w, lgeom.w);
156 for(Widget *p=parent; p; p=p->get_parent())
158 root_x += p->get_geometry().x;
159 root_y += p->get_geometry().y;
160 if(Root *root = dynamic_cast<Root *>(p))
162 const Geometry &rgeom = root->get_geometry();
163 if(lgeom.h*2>rgeom.h)
170 if(root_y+lgeom.y+lgeom.h>rgeom.h)
171 lgeom.y = rgeom.h-lgeom.h-root_y;
172 if(root_x+lgeom.x+lgeom.w>rgeom.w)
173 lgeom.x = rgeom.w-lgeom.w-root_x;
177 list.set_geometry(lgeom);
180 void Dropdown::list_item_selected(unsigned index)
186 signal_ungrab_pointer.emit();
189 text.set(list.get_data().get_string(index));
191 signal_item_selected.emit(index);
195 void Dropdown::list_selection_cleared()
201 Dropdown::Loader::Loader(Dropdown &d):
202 DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
204 add("item", &Loader::item);
207 void Dropdown::Loader::item(const string &v)
209 dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);