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>
21 Dropdown::Dropdown(const Resources &r):
28 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
33 void Dropdown::append(const string &item)
39 void Dropdown::insert(unsigned i, const string &v)
45 void Dropdown::remove(unsigned i)
51 void Dropdown::clear()
57 unsigned Dropdown::get_n_items() const
59 return list.get_n_items();
62 void Dropdown::set_selected_index(int i)
64 list.set_selected_index(i);
67 const string &Dropdown::get_selected() const
69 return list.get_selected();
72 int Dropdown::get_selected_index() const
74 return list.get_selected_index();
77 void Dropdown::button_press(int x, int y, unsigned btn)
81 Container::button_press(x, y, btn);
86 signal_ungrab_pointer.emit();
93 signal_grab_pointer.emit();
97 void Dropdown::render_special(const Part &part) const
99 if(part.get_name()=="text")
101 if(list.get_selected_index()>=0)
102 Text(style, list.get_selected()).render(part, geom);
103 //render_text(part, list.get_selected());
105 else if(part.get_name()=="list" && dropped)
109 void Dropdown::on_geometry_change()
114 void Dropdown::resize_list()
116 // XXX This is a hack.
117 unsigned n_items=list.get_n_items();
118 const Style &stl=list.get_style();
119 const GL::Font &font=*stl.get_font();
120 unsigned h=min(max(n_items, 1U), 10U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
121 for(std::list<Part>::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i)
122 if(i->get_name()=="items")
124 const Sides &margin=i->get_margin();
125 h+=margin.top+margin.bottom;
127 list.set_geometry(Geometry(0, -h, geom.w, h));
130 void Dropdown::list_item_selected(unsigned index, const std::string &item)
136 signal_ungrab_pointer.emit();
139 signal_item_selected.emit(index, item);
143 Dropdown::Loader::Loader(Dropdown &d):
146 add("item", &Loader::item);
149 void Dropdown::Loader::item(const string &str)
151 dynamic_cast<Dropdown &>(wdg).append(str);