3 This file is part of libmspgltk
4 Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/font.h>
26 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
27 list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
30 void Dropdown::autosize()
36 geom.w = max(geom.w, list.get_geometry().w);
38 if(const Part *text_part = style->get_part("text"))
40 const Sides &margin = text_part->get_margin();
41 const GL::Font *font = style->get_font();
42 float font_size = font->get_default_size();
43 unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
44 geom.h = max(geom.h, line_height+margin.top+margin.bottom);
48 void Dropdown::append(const string &item)
53 void Dropdown::insert(unsigned i, const string &v)
58 void Dropdown::remove(unsigned i)
63 void Dropdown::clear()
68 unsigned Dropdown::get_n_items() const
70 return list.get_n_items();
73 void Dropdown::set_selected_index(int i)
75 list.set_selected_index(i);
78 const string &Dropdown::get_selected() const
80 return list.get_selected();
83 int Dropdown::get_selected_index() const
85 return list.get_selected_index();
88 void Dropdown::render_special(const Part &part) const
90 if(part.get_name()=="text")
92 if(list.get_selected_index()>=0)
93 Text(*style, list.get_selected()).render(part, geom);
95 else if(part.get_name()=="list" && dropped)
99 void Dropdown::button_press(int x, int y, unsigned btn)
103 Container::button_press(x, y, btn);
108 signal_ungrab_pointer.emit();
115 signal_grab_pointer.emit();
119 void Dropdown::on_geometry_change()
124 void Dropdown::on_style_change()
129 void Dropdown::list_autosize_changed()
132 signal_autosize_changed.emit();
135 void Dropdown::resize_list()
138 Geometry lgeom = list.get_geometry();
141 lgeom.w = max(geom.w, lgeom.w);
144 for(Widget *p=parent; p; p=p->get_parent())
146 root_x += p->get_geometry().x;
147 root_y += p->get_geometry().y;
148 if(Root *root = dynamic_cast<Root *>(p))
150 const Geometry &rgeom = root->get_geometry();
151 if(lgeom.h*2>rgeom.h)
155 if(root_y+lgeom.y+lgeom.h>rgeom.h)
156 lgeom.y = rgeom.h-lgeom.h-root_y;
157 if(root_x+lgeom.x+lgeom.w>rgeom.w)
158 lgeom.x = rgeom.w-lgeom.w-root_x;
162 list.set_geometry(lgeom);
165 void Dropdown::list_item_selected(unsigned index, const std::string &item)
171 signal_ungrab_pointer.emit();
174 signal_item_selected.emit(index, item);
178 Dropdown::Loader::Loader(Dropdown &d):
181 add("item", &Loader::item);
184 void Dropdown::Loader::item(const string &str)
186 dynamic_cast<Dropdown &>(obj).append(str);