+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "dropdown.h"
+#include "list.h"
+#include "panel.h"
+#include "part.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GLtk {
+
+Dropdown::Dropdown(const Resources &r):
+ Widget(r),
+ list(new List(res)),
+ list_active(false)
+{
+ list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
+
+ update_style();
+}
+
+Dropdown::~Dropdown()
+{
+ delete list;
+}
+
+void Dropdown::append(const string &item)
+{
+ list->append(item);
+}
+
+void Dropdown::button_press(int x, int y, unsigned btn)
+{
+ 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;
+
+ if(parent)
+ {
+ parent->raise(*this);
+ parent->grab_pointer(*this);
+ }
+ }
+}
+
+void Dropdown::button_release(int x, int y, unsigned btn)
+{
+ if(list_active)
+ {
+ const Geometry &lgeom=list->get_geometry();
+ list->button_release(x-lgeom.x, y-lgeom.y, btn);
+ list_active=false;
+ }
+}
+
+void Dropdown::pointer_motion(int x, int y)
+{
+ if(list_active)
+ {
+ const Geometry &lgeom=list->get_geometry();
+ list->pointer_motion(x-lgeom.x, y-lgeom.y);
+ }
+}
+
+void Dropdown::pointer_enter()
+{
+ if(state==NORMAL)
+ state=HOVER;
+}
+
+void Dropdown::pointer_leave()
+{
+ if(state==HOVER)
+ state=NORMAL;
+}
+
+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();
+}
+
+void Dropdown::on_geometry_change()
+{
+ list->set_geometry(Geometry(0, -100, geom.w, 100));
+}
+
+void Dropdown::list_item_selected(unsigned index, const std::string &item)
+{
+ text=item;
+
+ list_active=false;
+ state=NORMAL;
+ if(parent)
+ parent->ungrab_pointer(*this);
+
+ signal_item_selected.emit(index, item);
+}
+
+} // namespace GLtk
+} // namespace Msp