1 #include <msp/gl/font.h>
20 Dropdown::Dropdown(ListData &d):
32 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
33 list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
36 void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
38 if(part.get_name()=="list")
42 ageom.w = max(ageom.w, list.get_geometry().w);
44 else if(part.get_name()=="text")
46 const Sides &margin = part.get_margin();
47 const GL::Font &font = style->get_font();
48 float font_size = style->get_font_size();
51 const ListData &data = list.get_data();
52 for(unsigned i=0; i<data.size(); ++i)
54 unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
55 max_w = max(max_w, w);
57 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
59 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
60 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
64 void Dropdown::set_selected_index(int index)
66 list.set_selected_index(index);
71 void Dropdown::rebuild_special(const Part &part)
73 if(part.get_name()=="text")
74 text.build(part, state, geom, part_cache);
76 Widget::rebuild_special(part);
79 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
81 if(part.get_name()=="list" && dropped)
82 list.render(renderer);
85 void Dropdown::button_press(int x, int y, unsigned btn)
89 Container::button_press(x, y, btn);
94 signal_ungrab_pointer.emit();
101 signal_grab_pointer.emit();
105 void Dropdown::on_geometry_change()
110 void Dropdown::on_style_change()
112 text.set_style(style);
116 void Dropdown::list_autosize_changed()
119 signal_autosize_changed.emit();
122 void Dropdown::resize_list()
125 list.autosize(lgeom);
128 lgeom.w = max(geom.w, lgeom.w);
131 for(Widget *p=parent; p; p=p->get_parent())
133 root_x += p->get_geometry().x;
134 root_y += p->get_geometry().y;
135 if(Root *root = dynamic_cast<Root *>(p))
137 const Geometry &rgeom = root->get_geometry();
138 if(lgeom.h*2>rgeom.h)
142 if(root_y+lgeom.y+lgeom.h>rgeom.h)
143 lgeom.y = rgeom.h-lgeom.h-root_y;
144 if(root_x+lgeom.x+lgeom.w>rgeom.w)
145 lgeom.x = rgeom.w-lgeom.w-root_x;
149 list.set_geometry(lgeom);
152 void Dropdown::list_item_selected(unsigned index)
158 signal_ungrab_pointer.emit();
161 text.set(list.get_data().get_string(index));
163 signal_item_selected.emit(index);
168 Dropdown::Loader::Loader(Dropdown &d):
169 DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
171 add("item", &Loader::item);
174 void Dropdown::Loader::item(const string &v)
176 dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);