-/* $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():
+ dropped(false)
{
- list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
-
- update_style();
+ add(list);
+ list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
+ list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
}
-Dropdown::~Dropdown()
+void Dropdown::autosize()
{
- delete list;
+ if(!style)
+ return;
+
+ Widget::autosize();
+ geom.w = max(geom.w, list.get_geometry().w);
+
+ if(const Part *text_part = style->get_part("text"))
+ {
+ const Sides &margin = text_part->get_margin();
+ const GL::Font &font = style->get_font();
+ float font_size = style->get_font_size();
+ unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
+ geom.h = max(geom.h, line_height+margin.top+margin.bottom);
+ }
+
+ rebuild();
}
void Dropdown::append(const string &item)
{
- list->append(item);
+ list.append(item);
}
-void Dropdown::button_press(int x, int y, unsigned btn)
+void Dropdown::insert(unsigned i, const string &v)
{
- 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.insert(i, v);
+}
- if(parent)
- {
- parent->raise(*this);
- parent->grab_pointer(*this);
- }
- }
+void Dropdown::remove(unsigned i)
+{
+ list.remove(i);
+}
+
+void Dropdown::clear()
+{
+ list.clear();
}
-void Dropdown::button_release(int x, int y, unsigned btn)
+unsigned Dropdown::get_n_items() const
{
- if(list_active)
+ return list.get_n_items();
+}
+
+void Dropdown::set_selected_index(int i)
+{
+ list.set_selected_index(i);
+}
+
+const string &Dropdown::get_selected() const
+{
+ return list.get_selected();
+}
+
+int Dropdown::get_selected_index() const
+{
+ return list.get_selected_index();
+}
+
+void Dropdown::rebuild_special(const Part &part, CachedPart &cache)
+{
+ if(part.get_name()=="text")
{
- const Geometry &lgeom=list->get_geometry();
- list->button_release(x-lgeom.x, y-lgeom.y, btn);
- list_active=false;
+ if(list.get_selected_index()>=0)
+ Text(*style, list.get_selected()).build(part, geom, cache);
+ else
+ cache.texture = 0;
}
}
-void Dropdown::pointer_motion(int x, int y)
+void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
{
- if(list_active)
+ 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->pointer_motion(x-lgeom.x, y-lgeom.y);
+ Container::button_press(x, y, btn);
+ if(!click_focus)
+ {
+ dropped = false;
+ clear_state(ACTIVE);
+ signal_ungrab_pointer.emit();
+ }
+ }
+ else if(btn==1)
+ {
+ dropped = true;
+ set_state(ACTIVE);
+ signal_grab_pointer.emit();
}
}
-void Dropdown::pointer_enter()
+void Dropdown::on_geometry_change()
{
- if(state==NORMAL)
- state=HOVER;
+ resize_list();
}
-void Dropdown::pointer_leave()
+void Dropdown::on_style_change()
{
- if(state==HOVER)
- state=NORMAL;
+ resize_list();
}
-void Dropdown::render_special(const Part &part) const
+void Dropdown::list_autosize_changed()
{
- if(part.get_name()=="text")
- render_text(part, text);
- else if(part.get_name()=="list" && state==ACTIVE)
- list->render();
+ resize_list();
+ signal_autosize_changed.emit();
}
-void Dropdown::on_geometry_change()
+void Dropdown::resize_list()
{
- list->set_geometry(Geometry(0, -100, geom.w, 100));
+ list.autosize_all();
+ Geometry lgeom = list.get_geometry();
+ 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;
+ 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, const std::string &item)
{
- text=item;
-
- list_active=false;
- state=NORMAL;
- if(parent)
- parent->ungrab_pointer(*this);
+ if(dropped)
+ {
+ dropped = false;
+ clear_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 &>(obj).append(str);
}
} // namespace GLtk