3 This file is part of libmspgltk
4 Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/font.h>
20 Dropdown::Dropdown(const Resources &r):
27 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
32 void Dropdown::append(const string &item)
38 void Dropdown::insert(unsigned i, const string &v)
44 void Dropdown::remove(unsigned i)
50 void Dropdown::clear()
56 unsigned Dropdown::get_n_items() const
58 return list.get_n_items();
61 void Dropdown::set_selected_index(int i)
63 list.set_selected_index(i);
66 const string &Dropdown::get_selected() const
68 return list.get_selected();
71 int Dropdown::get_selected_index() const
73 return list.get_selected_index();
76 void Dropdown::button_press(int x, int y, unsigned btn)
80 Container::button_press(x, y, btn);
85 signal_ungrab_pointer.emit();
92 signal_grab_pointer.emit();
96 void Dropdown::render_special(const Part &part) const
98 if(part.get_name()=="text")
100 if(list.get_selected_index()>=0)
101 render_text(part, list.get_selected());
103 else if(part.get_name()=="list" && dropped)
107 void Dropdown::on_geometry_change()
112 void Dropdown::resize_list()
114 // XXX This is a hack.
115 unsigned n_items=list.get_n_items();
116 const Style &stl=list.get_style();
117 const GL::Font &font=*stl.get_font();
118 unsigned h=min(max(n_items, 1U), 10U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
119 for(std::list<Part>::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i)
120 if(i->get_name()=="items")
122 const Sides &margin=i->get_margin();
123 h+=margin.top+margin.bottom;
125 list.set_geometry(Geometry(0, -h, geom.w, h));
128 void Dropdown::list_item_selected(unsigned index, const std::string &item)
134 signal_ungrab_pointer.emit();
137 signal_item_selected.emit(index, item);
141 Dropdown::Loader::Loader(Dropdown &d):
144 add("item", &Loader::item);
147 void Dropdown::Loader::item(const string &str)
149 dynamic_cast<Dropdown &>(wdg).append(str);