-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007 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 "root.h"
+#include "style.h"
+#include "text.h"
using namespace std;
namespace Msp {
namespace GLtk {
-Dropdown::Dropdown(const Resources &r):
- Widget(r),
- list(new List(res)),
- list_active(false)
+Dropdown::Dropdown()
{
- list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
-
- update_style();
+ init();
}
-Dropdown::~Dropdown()
+Dropdown::Dropdown(ListData &d):
+ list(d)
{
- delete list;
+ init();
}
-void Dropdown::append(const string &item)
+void Dropdown::init()
{
- list->append(item);
+ input_type = INPUT_NAVIGATION;
+
+ dropped = false;
+
+ add(list);
+ list.set_visible(false);
+ list.set_view_all();
+ list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
+ list.signal_selection_cleared.connect(sigc::mem_fun(this, &Dropdown::list_selection_cleared));
+ list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
}
-void Dropdown::button_press(int x, int y, unsigned btn)
+void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
{
- if(list->get_geometry().is_inside(x, y))
+ if(part.get_name()=="list")
{
- const Geometry &lgeom=list->get_geometry();
- list->button_press(x-lgeom.x, y-lgeom.y, btn);
- list_active=true;
+ Geometry lgeom;
+ list.autosize(lgeom);
+ ageom.w = max(ageom.w, list.get_geometry().w);
}
- else if(state==ACTIVE)
+ else if(part.get_name()=="text")
{
- state=HOVER;
- parent->ungrab_pointer(*this);
- }
- else if(btn==1)
- {
- state=ACTIVE;
+ const Sides &margin = part.get_margin();
+ const GL::Font &font = style->get_font();
+ float font_size = style->get_font_size();
- if(parent)
+ unsigned max_w = 0;
+ const ListData &data = list.get_data();
+ for(unsigned i=0; i<data.size(); ++i)
{
- parent->raise(*this);
- parent->grab_pointer(*this);
+ unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
+ max_w = max(max_w, w);
}
+ ageom.w = max(ageom.w, max_w+margin.left+margin.right);
+
+ unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
+ ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
}
}
-void Dropdown::button_release(int x, int y, unsigned btn)
+void Dropdown::rebuild_special(const Part &part)
{
- if(list_active)
+ if(part.get_name()=="text")
+ text.build(part, state, geom, part_cache);
+ else
+ Widget::rebuild_special(part);
+}
+
+void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
+{
+ if(part.get_name()=="list" && dropped)
+ list.render(renderer);
+}
+
+void Dropdown::button_press(int x, int y, unsigned btn)
+{
+ if(dropped)
{
- const Geometry &lgeom=list->get_geometry();
- list->button_release(x-lgeom.x, y-lgeom.y, btn);
- list_active=false;
+ Container::button_press(x, y, btn);
+ if(!click_focus)
+ close_list();
}
+ else if(btn==1)
+ open_list();
}
-void Dropdown::pointer_motion(int x, int y)
+bool Dropdown::navigate(Navigation nav)
{
- if(list_active)
+ if(dropped)
{
- const Geometry &lgeom=list->get_geometry();
- list->pointer_motion(x-lgeom.x, y-lgeom.y);
+ if(nav==NAV_CANCEL)
+ close_list();
+ else
+ list.navigate(nav);
}
+ else if(nav==NAV_ACTIVATE)
+ open_list();
+ else
+ return false;
+
+ return true;
}
-void Dropdown::pointer_enter()
+void Dropdown::on_geometry_change()
{
- if(state==NORMAL)
- state=HOVER;
+ if(dropped)
+ resize_list();
}
-void Dropdown::pointer_leave()
+void Dropdown::on_style_change()
{
- if(state==HOVER)
- state=NORMAL;
+ text.set_style(style);
+ if(dropped)
+ resize_list();
}
-void Dropdown::render_special(const Part &part) const
+void Dropdown::open_list()
{
- if(part.get_name()=="text")
- render_text(part, text);
- else if(part.get_name()=="list" && state==ACTIVE)
- list->render();
+ dropped = true;
+ list.set_visible(true);
+ resize_list();
+ set_state(ACTIVE);
+ set_input_focus(&list);
+ signal_grab_pointer.emit();
}
-void Dropdown::on_geometry_change()
+void Dropdown::close_list()
{
- list->set_geometry(Geometry(0, -100, geom.w, 100));
+ dropped = false;
+ list.set_visible(false);
+ clear_state(ACTIVE);
+ signal_ungrab_pointer.emit();
}
-void Dropdown::list_item_selected(unsigned index, const std::string &item)
+void Dropdown::list_autosize_changed()
{
- text=item;
+ if(dropped)
+ resize_list();
+ signal_autosize_changed.emit();
+}
- list_active=false;
- state=NORMAL;
- if(parent)
- parent->ungrab_pointer(*this);
+void Dropdown::resize_list()
+{
+ Geometry lgeom;
+ list.autosize(lgeom);
+ lgeom.x = 0;
+ lgeom.y = -lgeom.h;
+ lgeom.w = max(geom.w, lgeom.w);
+ int root_x = geom.x;
+ int root_y = geom.y;
+ for(Widget *p=parent; p; p=p->get_parent())
+ {
+ root_x += p->get_geometry().x;
+ root_y += p->get_geometry().y;
+ if(Root *root = dynamic_cast<Root *>(p))
+ {
+ const Geometry &rgeom = root->get_geometry();
+ if(lgeom.h*2>rgeom.h)
+ {
+ lgeom.h = rgeom.h/2;
+ lgeom.y = -lgeom.h;
+ }
+ if(root_y+lgeom.y<0)
+ lgeom.y = -root_y;
+ if(root_y+lgeom.y+lgeom.h>rgeom.h)
+ lgeom.y = rgeom.h-lgeom.h-root_y;
+ if(root_x+lgeom.x+lgeom.w>rgeom.w)
+ lgeom.x = rgeom.w-lgeom.w-root_x;
+ break;
+ }
+ }
+ list.set_geometry(lgeom);
+}
+
+void Dropdown::list_item_selected(unsigned index)
+{
+ if(dropped)
+ {
+ dropped = false;
+ clear_state(ACTIVE);
+ signal_ungrab_pointer.emit();
+ }
- signal_item_selected.emit(index, item);
+ text.set(list.get_data().get_string(index));
+
+ signal_item_selected.emit(index);
+ rebuild();
+}
+
+void Dropdown::list_selection_cleared()
+{
+ text.set(string());
}
Dropdown::Loader::Loader(Dropdown &d):
- Widget::Loader(d)
+ DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
{
add("item", &Loader::item);
}
-void Dropdown::Loader::item(const string &str)
+void Dropdown::Loader::item(const string &v)
{
- static_cast<Dropdown &>(wdg).append(str);
+ dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);
}
} // namespace GLtk