1 #include <msp/gl/font.h>
20 Dropdown::Dropdown(ListData &d):
28 // Necessary to have the parent container raise the dropdown on click
29 input_type = INPUT_TEXT;
34 list.set_visible(false);
36 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
37 list.signal_selection_cleared.connect(sigc::mem_fun(this, &Dropdown::list_selection_cleared));
38 list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
41 void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
43 if(part.get_name()=="list")
47 ageom.w = max(ageom.w, list.get_geometry().w);
49 else if(part.get_name()=="text")
51 const Sides &margin = part.get_margin();
52 const GL::Font &font = style->get_font();
53 float font_size = style->get_font_size();
56 const ListData &data = list.get_data();
57 for(unsigned i=0; i<data.size(); ++i)
59 unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
60 max_w = max(max_w, w);
62 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
64 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
65 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
69 void Dropdown::rebuild_special(const Part &part)
71 if(part.get_name()=="text")
72 text.build(part, state, geom, part_cache);
74 Widget::rebuild_special(part);
77 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
79 if(part.get_name()=="list" && dropped)
80 list.render(renderer);
83 void Dropdown::button_press(int x, int y, unsigned btn)
87 Container::button_press(x, y, btn);
91 list.set_visible(false);
93 signal_ungrab_pointer.emit();
99 list.set_visible(true);
102 signal_grab_pointer.emit();
106 void Dropdown::on_geometry_change()
112 void Dropdown::on_style_change()
114 text.set_style(style);
119 void Dropdown::list_autosize_changed()
123 signal_autosize_changed.emit();
126 void Dropdown::resize_list()
129 list.autosize(lgeom);
132 lgeom.w = max(geom.w, lgeom.w);
135 for(Widget *p=parent; p; p=p->get_parent())
137 root_x += p->get_geometry().x;
138 root_y += p->get_geometry().y;
139 if(Root *root = dynamic_cast<Root *>(p))
141 const Geometry &rgeom = root->get_geometry();
142 if(lgeom.h*2>rgeom.h)
149 if(root_y+lgeom.y+lgeom.h>rgeom.h)
150 lgeom.y = rgeom.h-lgeom.h-root_y;
151 if(root_x+lgeom.x+lgeom.w>rgeom.w)
152 lgeom.x = rgeom.w-lgeom.w-root_x;
156 list.set_geometry(lgeom);
159 void Dropdown::list_item_selected(unsigned index)
165 signal_ungrab_pointer.emit();
168 text.set(list.get_data().get_string(index));
170 signal_item_selected.emit(index);
174 void Dropdown::list_selection_cleared()
180 Dropdown::Loader::Loader(Dropdown &d):
181 DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
183 add("item", &Loader::item);
186 void Dropdown::Loader::item(const string &v)
188 dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);