3 This file is part of libmspgltk
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/font.h>
20 Dropdown::Dropdown(const Resources &r):
26 list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
35 void Dropdown::append(const string &item)
41 void Dropdown::insert(unsigned i, const string &v)
47 void Dropdown::remove(unsigned i)
53 void Dropdown::clear()
59 unsigned Dropdown::get_n_items() const
61 return list.get_n_items();
64 void Dropdown::set_selected_index(int i)
66 list.set_selected_index(i);
69 const string &Dropdown::get_selected() const
71 return list.get_selected();
74 int Dropdown::get_selected_index() const
76 return list.get_selected_index();
79 void Dropdown::button_press(int x, int y, unsigned btn)
81 if(list.get_geometry().is_inside(x, y))
83 const Geometry &lgeom=list.get_geometry();
84 list.button_press(x-lgeom.x, y-lgeom.y, btn);
91 parent->ungrab_pointer(*this);
100 parent->raise(*this);
101 parent->grab_pointer(*this);
106 void Dropdown::button_release(int x, int y, unsigned btn)
110 const Geometry &lgeom=list.get_geometry();
111 list.button_release(x-lgeom.x, y-lgeom.y, btn);
116 void Dropdown::pointer_motion(int x, int y)
120 const Geometry &lgeom=list.get_geometry();
121 list.pointer_motion(x-lgeom.x, y-lgeom.y);
125 void Dropdown::render_special(const Part &part) const
127 if(part.get_name()=="text")
129 if(list.get_selected_index()>=0)
130 render_text(part, list.get_selected());
132 else if(part.get_name()=="list" && dropped)
136 void Dropdown::on_geometry_change()
141 void Dropdown::resize_list()
143 // XXX This is a hack.
144 unsigned n_items=list.get_n_items();
145 const Style &stl=list.get_style();
146 const GL::Font &font=*stl.get_font();
147 unsigned h=min(max(n_items, 1U), 20U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
148 for(std::list<Part>::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i)
149 if(i->get_name()=="items")
151 const Sides &margin=i->get_margin();
152 h+=margin.top+margin.bottom;
154 list.set_geometry(Geometry(0, -h, geom.w, h));
157 void Dropdown::list_item_selected(unsigned index, const std::string &item)
165 parent->ungrab_pointer(*this);
168 signal_item_selected.emit(index, item);
172 Dropdown::Loader::Loader(Dropdown &d):
175 add("item", &Loader::item);
178 void Dropdown::Loader::item(const string &str)
180 static_cast<Dropdown &>(wdg).append(str);