1 #include <msp/gl/font.h>
20 Dropdown::Dropdown(ListData &d):
31 list.set_visible(false);
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));
37 void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
39 if(part.get_name()=="list")
43 ageom.w = max(ageom.w, list.get_geometry().w);
45 else if(part.get_name()=="text")
47 const Sides &margin = part.get_margin();
48 const GL::Font &font = style->get_font();
49 float font_size = style->get_font_size();
52 const ListData &data = list.get_data();
53 for(unsigned i=0; i<data.size(); ++i)
55 unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
56 max_w = max(max_w, w);
58 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
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);
65 void Dropdown::set_selected_index(int index)
67 list.set_selected_index(index);
72 void Dropdown::rebuild_special(const Part &part)
74 if(part.get_name()=="text")
75 text.build(part, state, geom, part_cache);
77 Widget::rebuild_special(part);
80 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
82 if(part.get_name()=="list" && dropped)
83 list.render(renderer);
86 void Dropdown::button_press(int x, int y, unsigned btn)
90 Container::button_press(x, y, btn);
94 list.set_visible(false);
96 signal_ungrab_pointer.emit();
102 list.set_visible(true);
105 signal_grab_pointer.emit();
109 void Dropdown::on_geometry_change()
115 void Dropdown::on_style_change()
117 text.set_style(style);
122 void Dropdown::list_autosize_changed()
126 signal_autosize_changed.emit();
129 void Dropdown::resize_list()
132 list.autosize(lgeom);
135 lgeom.w = max(geom.w, lgeom.w);
138 for(Widget *p=parent; p; p=p->get_parent())
140 root_x += p->get_geometry().x;
141 root_y += p->get_geometry().y;
142 if(Root *root = dynamic_cast<Root *>(p))
144 const Geometry &rgeom = root->get_geometry();
145 if(lgeom.h*2>rgeom.h)
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;
159 list.set_geometry(lgeom);
162 void Dropdown::list_item_selected(unsigned index)
168 signal_ungrab_pointer.emit();
171 text.set(list.get_data().get_string(index));
173 signal_item_selected.emit(index);
178 Dropdown::Loader::Loader(Dropdown &d):
179 DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
181 add("item", &Loader::item);
184 void Dropdown::Loader::item(const string &v)
186 dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);