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));
29 void Dropdown::autosize()
35 geom.w = max(geom.w, list.get_geometry().w);
37 if(const Part *text_part = style->get_part("text"))
39 const Sides &margin = text_part->get_margin();
40 const GL::Font *font = style->get_font();
41 float font_size = font->get_default_size();
42 unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
43 geom.h = max(geom.h, line_height+margin.top+margin.bottom);
47 void Dropdown::append(const string &item)
53 void Dropdown::insert(unsigned i, const string &v)
59 void Dropdown::remove(unsigned i)
65 void Dropdown::clear()
71 unsigned Dropdown::get_n_items() const
73 return list.get_n_items();
76 void Dropdown::set_selected_index(int i)
78 list.set_selected_index(i);
81 const string &Dropdown::get_selected() const
83 return list.get_selected();
86 int Dropdown::get_selected_index() const
88 return list.get_selected_index();
91 void Dropdown::render_special(const Part &part) const
93 if(part.get_name()=="text")
95 if(list.get_selected_index()>=0)
96 Text(*style, list.get_selected()).render(part, geom);
98 else if(part.get_name()=="list" && dropped)
102 void Dropdown::button_press(int x, int y, unsigned btn)
106 Container::button_press(x, y, btn);
111 signal_ungrab_pointer.emit();
118 signal_grab_pointer.emit();
122 void Dropdown::on_geometry_change()
127 void Dropdown::on_style_change()
132 void Dropdown::resize_list()
135 Geometry lgeom = list.get_geometry();
138 lgeom.w = max(geom.w, lgeom.w);
141 for(Widget *p=parent; p; p=p->get_parent())
143 root_x += p->get_geometry().x;
144 root_y += p->get_geometry().y;
145 if(Root *root = dynamic_cast<Root *>(p))
147 const Geometry &rgeom = root->get_geometry();
148 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, const std::string &item)
168 signal_ungrab_pointer.emit();
171 signal_item_selected.emit(index, item);
175 Dropdown::Loader::Loader(Dropdown &d):
178 add("item", &Loader::item);
181 void Dropdown::Loader::item(const string &str)
183 dynamic_cast<Dropdown &>(obj).append(str);