/* $Id$
This file is part of libmspgltk
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2011 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;
namespace Msp {
namespace GLtk {
-Dropdown::Dropdown(const Resources &r):
- Widget(r),
- list(new List(res)),
- dropped(false),
- list_active(false)
+Dropdown::Dropdown():
+ dropped(false)
{
- list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
-
- update_style();
-}
-
-Dropdown::~Dropdown()
-{
- delete list;
+ add(list);
+ list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
}
void Dropdown::append(const string &item)
{
- list->append(item);
+ list.append(item);
+ resize_list();
}
void Dropdown::insert(unsigned i, const string &v)
{
- list->insert(i, v);
+ list.insert(i, v);
+ resize_list();
}
void Dropdown::remove(unsigned i)
{
- list->remove(i);
+ list.remove(i);
+ resize_list();
}
void Dropdown::clear()
{
- list->clear();
+ list.clear();
+ resize_list();
+}
+
+unsigned Dropdown::get_n_items() const
+{
+ return list.get_n_items();
}
void Dropdown::set_selected_index(int i)
{
- list->set_selected_index(i);
+ list.set_selected_index(i);
}
const string &Dropdown::get_selected() const
{
- return list->get_selected();
+ return list.get_selected();
}
int Dropdown::get_selected_index() const
{
- return list->get_selected_index();
+ return list.get_selected_index();
}
-void Dropdown::button_press(int x, int y, unsigned btn)
+void Dropdown::render_special(const Part &part) const
{
- 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(dropped)
+ if(part.get_name()=="text")
{
- dropped=false;
- state&=~ACTIVE;
- parent->ungrab_pointer(*this);
+ if(list.get_selected_index()>=0)
+ Text(*style, list.get_selected()).render(part, geom);
}
- else if(btn==1)
- {
- dropped=true;
- state|=ACTIVE;
+ else if(part.get_name()=="list" && dropped)
+ list.render();
+}
- if(parent)
+void Dropdown::button_press(int x, int y, unsigned btn)
+{
+ if(dropped)
+ {
+ Container::button_press(x, y, btn);
+ if(!click_focus)
{
- parent->raise(*this);
- parent->grab_pointer(*this);
+ dropped = false;
+ state &= ~ACTIVE;
+ signal_ungrab_pointer.emit();
}
}
-}
-
-void Dropdown::button_release(int x, int y, unsigned btn)
-{
- if(list_active)
+ else if(btn==1)
{
- const Geometry &lgeom=list->get_geometry();
- list->button_release(x-lgeom.x, y-lgeom.y, btn);
- list_active=false;
+ dropped = true;
+ state |= ACTIVE;
+ signal_grab_pointer.emit();
}
}
-void Dropdown::pointer_motion(int x, int y)
+void Dropdown::on_geometry_change()
{
- if(list_active)
- {
- const Geometry &lgeom=list->get_geometry();
- list->pointer_motion(x-lgeom.x, y-lgeom.y);
- }
+ resize_list();
}
-void Dropdown::render_special(const Part &part) const
+void Dropdown::on_style_change()
{
- if(part.get_name()=="text")
- render_text(part, text);
- else if(part.get_name()=="list" && dropped)
- list->render();
+ resize_list();
}
-void Dropdown::on_geometry_change()
+void Dropdown::resize_list()
{
- list->set_geometry(Geometry(0, -100, geom.w, 100));
+ list.autosize();
+ const Geometry &lgeom = list.get_geometry();
+ list.set_geometry(Geometry(0, -lgeom.h, max(geom.w, lgeom.w), lgeom.h));
}
void Dropdown::list_item_selected(unsigned index, const std::string &item)
{
- text=item;
-
if(dropped)
{
- list_active=false;
- dropped=false;
- state&=~ACTIVE;
- if(parent)
- parent->ungrab_pointer(*this);
+ 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 &>(obj).append(str);
}
} // namespace GLtk