/* $Id$
This file is part of libmspgltk
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+#include <msp/gl/font.h>
#include "dropdown.h"
#include "list.h"
#include "panel.h"
#include "part.h"
+#include "style.h"
+#include "text.h"
using namespace std;
Dropdown::Dropdown(const Resources &r):
Widget(r),
- list(new List(res)),
- list_active(false)
+ Container(r),
+ list(r),
+ dropped(false)
{
- list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
+ add(list);
+ list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
update_style();
}
-Dropdown::~Dropdown()
+void Dropdown::append(const string &item)
{
- delete list;
+ list.append(item);
+ resize_list();
}
-void Dropdown::append(const string &item)
+void Dropdown::insert(unsigned i, const string &v)
{
- list->append(item);
+ list.insert(i, v);
+ resize_list();
}
-void Dropdown::button_press(int x, int y, unsigned btn)
+void Dropdown::remove(unsigned i)
{
- if(list->get_geometry().is_inside(x, y))
- {
- const Geometry &lgeom=list->get_geometry();
- list->button_press(x-lgeom.x, y-lgeom.y, btn);
- list_active=true;
- }
- else if(state==ACTIVE)
- {
- state=HOVER;
- parent->ungrab_pointer(*this);
- }
- else if(btn==1)
- {
- state=ACTIVE;
+ list.remove(i);
+ resize_list();
+}
- if(parent)
- {
- parent->raise(*this);
- parent->grab_pointer(*this);
- }
- }
+void Dropdown::clear()
+{
+ list.clear();
+ resize_list();
}
-void Dropdown::button_release(int x, int y, unsigned btn)
+unsigned Dropdown::get_n_items() const
{
- if(list_active)
- {
- const Geometry &lgeom=list->get_geometry();
- list->button_release(x-lgeom.x, y-lgeom.y, btn);
- list_active=false;
- }
+ return list.get_n_items();
}
-void Dropdown::pointer_motion(int x, int y)
+void Dropdown::set_selected_index(int i)
{
- if(list_active)
- {
- const Geometry &lgeom=list->get_geometry();
- list->pointer_motion(x-lgeom.x, y-lgeom.y);
- }
+ list.set_selected_index(i);
}
-void Dropdown::pointer_enter()
+const string &Dropdown::get_selected() const
{
- if(state==NORMAL)
- state=HOVER;
+ return list.get_selected();
}
-void Dropdown::pointer_leave()
+int Dropdown::get_selected_index() const
{
- if(state==HOVER)
- state=NORMAL;
+ return list.get_selected_index();
+}
+
+void Dropdown::button_press(int x, int y, unsigned btn)
+{
+ if(dropped)
+ {
+ Container::button_press(x, y, btn);
+ if(!click_focus)
+ {
+ dropped=false;
+ state&=~ACTIVE;
+ signal_ungrab_pointer.emit();
+ }
+ }
+ else if(btn==1)
+ {
+ dropped=true;
+ state|=ACTIVE;
+ signal_grab_pointer.emit();
+ }
}
void Dropdown::render_special(const Part &part) const
{
if(part.get_name()=="text")
- render_text(part, text);
- else if(part.get_name()=="list" && state==ACTIVE)
- list->render();
+ {
+ if(list.get_selected_index()>=0)
+ Text(style, list.get_selected()).render(part, geom);
+ }
+ else if(part.get_name()=="list" && dropped)
+ list.render();
}
void Dropdown::on_geometry_change()
{
- list->set_geometry(Geometry(0, -100, geom.w, 100));
+ resize_list();
}
-void Dropdown::list_item_selected(unsigned index, const std::string &item)
+void Dropdown::resize_list()
{
- text=item;
+ // XXX This is a hack.
+ unsigned n_items=list.get_n_items();
+ const Style &stl=list.get_style();
+ const GL::Font &font=*stl.get_font();
+ unsigned h=min(max(n_items, 1U), 10U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
+ for(std::list<Part>::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i)
+ if(i->get_name()=="items")
+ {
+ const Sides &margin=i->get_margin();
+ h+=margin.top+margin.bottom;
+ }
+ list.set_geometry(Geometry(0, -h, geom.w, h));
+}
- list_active=false;
- state=NORMAL;
- if(parent)
- parent->ungrab_pointer(*this);
+void Dropdown::list_item_selected(unsigned index, const std::string &item)
+{
+ if(dropped)
+ {
+ dropped=false;
+ state&=~ACTIVE;
+ signal_ungrab_pointer.emit();
+ }
signal_item_selected.emit(index, item);
}
void Dropdown::Loader::item(const string &str)
{
- static_cast<Dropdown &>(wdg).append(str);
+ dynamic_cast<Dropdown &>(wdg).append(str);
}
} // namespace GLtk