X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdropdown.cpp;h=a4816d48d593bb430e8b1aa764c35aac131b5744;hb=3f4817441626e1abb5556ae53c16746634a57ad9;hp=bc44f27c3ec774a4b982ea9ab72c5dbf5bf40e71;hpb=95210598ff214bbc8d05657aeffc4ce7801f211a;p=libs%2Fgltk.git diff --git a/source/dropdown.cpp b/source/dropdown.cpp index bc44f27..a4816d4 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -5,10 +5,12 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include "dropdown.h" #include "list.h" #include "panel.h" #include "part.h" +#include "style.h" using namespace std; @@ -17,40 +19,81 @@ namespace GLtk { Dropdown::Dropdown(const Resources &r): Widget(r), - list(new List(res)), + list(r), + dropped(false), list_active(false) { - list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected)); + 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); + list.append(item); + resize_list(); +} + +void Dropdown::insert(unsigned i, const string &v) +{ + list.insert(i, v); + resize_list(); +} + +void Dropdown::remove(unsigned i) +{ + list.remove(i); + resize_list(); +} + +void Dropdown::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); +} + +const string &Dropdown::get_selected() const +{ + return list.get_selected(); +} + +int Dropdown::get_selected_index() const +{ + return list.get_selected_index(); } void Dropdown::button_press(int x, int y, unsigned btn) { - if(list->get_geometry().is_inside(x, y)) + if(list.get_geometry().is_inside(x, y)) { - const Geometry &lgeom=list->get_geometry(); - list->button_press(x-lgeom.x, y-lgeom.y, btn); + const Geometry &lgeom=list.get_geometry(); + list.button_press(x-lgeom.x, y-lgeom.y, btn); list_active=true; } - else if(state==ACTIVE) + else if(dropped) { - state=HOVER; + dropped=false; + state&=~ACTIVE; parent->ungrab_pointer(*this); } else if(btn==1) { - state=ACTIVE; + dropped=true; + state|=ACTIVE; if(parent) { @@ -64,8 +107,8 @@ 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); + const Geometry &lgeom=list.get_geometry(); + list.button_release(x-lgeom.x, y-lgeom.y, btn); list_active=false; } } @@ -74,46 +117,67 @@ 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); + const Geometry &lgeom=list.get_geometry(); + list.pointer_motion(x-lgeom.x, y-lgeom.y); } } -void Dropdown::pointer_enter() +void Dropdown::render_special(const Part &part) const { - if(state==NORMAL) - state=HOVER; + if(part.get_name()=="text") + { + if(list.get_selected_index()>=0) + render_text(part, list.get_selected()); + } + else if(part.get_name()=="list" && dropped) + list.render(); } -void Dropdown::pointer_leave() +void Dropdown::on_geometry_change() { - if(state==HOVER) - state=NORMAL; + resize_list(); } -void Dropdown::render_special(const Part &part) const +void Dropdown::resize_list() { - if(part.get_name()=="text") - render_text(part, text); - else if(part.get_name()=="list" && state==ACTIVE) - list->render(); + // 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), 20U)*static_cast((font.get_ascent()-font.get_descent())*font.get_default_size()); + for(std::list::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)); } -void Dropdown::on_geometry_change() +void Dropdown::list_item_selected(unsigned index, const std::string &item) { - list->set_geometry(Geometry(0, -100, geom.w, 100)); + if(dropped) + { + list_active=false; + dropped=false; + state&=~ACTIVE; + if(parent) + parent->ungrab_pointer(*this); + } + + signal_item_selected.emit(index, item); } -void Dropdown::list_item_selected(unsigned index, const std::string &item) -{ - text=item; - list_active=false; - state=NORMAL; - if(parent) - parent->ungrab_pointer(*this); +Dropdown::Loader::Loader(Dropdown &d): + Widget::Loader(d) +{ + add("item", &Loader::item); +} - signal_item_selected.emit(index, item); +void Dropdown::Loader::item(const string &str) +{ + static_cast(wdg).append(str); } } // namespace GLtk