X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdropdown.cpp;h=ef0fa22f97c8f150009988b3487c716b1e576cf2;hb=HEAD;hp=c2ceefa78957348ee6edd188e71e8ec1faf1fd95;hpb=1aa6cd9b865e366737dcc9d2d36c4f8faed5bc4f;p=libs%2Fgltk.git diff --git a/source/dropdown.cpp b/source/dropdown.cpp index c2ceefa..ab00c4c 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -12,131 +12,142 @@ using namespace std; namespace Msp { namespace GLtk { -Dropdown::Dropdown(): - dropped(false) +Dropdown::Dropdown() { - 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)); + init(); } -void Dropdown::autosize() +Dropdown::Dropdown(ListData &d): + list(d) { - 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((font.get_ascent()-font.get_descent())*font_size); - geom.h = max(geom.h, line_height+margin.top+margin.bottom); - } - - rebuild(); + init(); } -void Dropdown::append(const string &item) +void Dropdown::init() { - list.append(item); -} + input_type = INPUT_NAVIGATION; -void Dropdown::insert(unsigned i, const string &v) -{ - list.insert(i, v); + 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::remove(unsigned i) +void Dropdown::autosize_special(const Part &part, Geometry &ageom) const { - list.remove(i); -} + if(part.get_name()=="list") + { + Geometry lgeom; + list.autosize(lgeom); + ageom.w = max(ageom.w, list.get_geometry().w); + } + else if(part.get_name()=="text") + { + const Sides &margin = part.get_margin(); + const GL::Font &font = style->get_font(); + float font_size = style->get_font_size(); -void Dropdown::clear() -{ - list.clear(); -} + unsigned max_w = 0; + const ListData &data = list.get_data(); + for(size_t i=0; i(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 Dropdown::get_n_items() const -{ - return list.get_n_items(); + unsigned line_height = static_cast((font.get_ascent()-font.get_descent())*font_size); + ageom.h = max(ageom.h, line_height+margin.top+margin.bottom); + } } -void Dropdown::set_selected_index(int i) +void Dropdown::rebuild_special(const Part &part) { - list.set_selected_index(i); + if(part.get_name()=="text") + text.build(part, state, geom, part_cache); + else + Widget::rebuild_special(part); } -const string &Dropdown::get_selected() const +void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const { - return list.get_selected(); + if(part.get_name()=="list" && dropped) + list.render(renderer); } -int Dropdown::get_selected_index() const +void Dropdown::button_press(int x, int y, unsigned btn) { - return list.get_selected_index(); + if(dropped) + { + Container::button_press(x, y, btn); + if(!click_focus) + close_list(); + } + else if(btn==1) + open_list(); } -void Dropdown::rebuild_special(const Part &part, CachedPart &cache) +bool Dropdown::navigate(Navigation nav) { - if(part.get_name()=="text") + if(dropped) { - if(list.get_selected_index()>=0) - Text(*style, list.get_selected()).build(part, geom, cache); + if(nav==NAV_CANCEL) + close_list(); else - cache.texture = 0; + list.navigate(nav); } + else if(nav==NAV_ACTIVATE) + open_list(); + else + return false; + + return true; } -void Dropdown::render_special(const Part &part) const +void Dropdown::on_size_change() { - if(part.get_name()=="list" && dropped) - list.render(); + if(dropped) + resize_list(); } -void Dropdown::button_press(int x, int y, unsigned btn) +void Dropdown::on_style_change() { + text.set_style(style); if(dropped) - { - 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(); - } + resize_list(); } -void Dropdown::on_geometry_change() +void Dropdown::open_list() { + dropped = true; + list.set_visible(true); resize_list(); + set_state(ACTIVE); + set_input_focus(&list); + signal_grab_pointer.emit(); } -void Dropdown::on_style_change() +void Dropdown::close_list() { - resize_list(); + dropped = false; + list.set_visible(false); + clear_state(ACTIVE); + signal_ungrab_pointer.emit(); } void Dropdown::list_autosize_changed() { - resize_list(); + if(dropped) + resize_list(); signal_autosize_changed.emit(); } void Dropdown::resize_list() { - list.autosize_all(); - Geometry lgeom = list.get_geometry(); + Geometry lgeom; + list.autosize(lgeom); lgeom.x = 0; lgeom.y = -lgeom.h; lgeom.w = max(geom.w, lgeom.w); @@ -150,7 +161,10 @@ void Dropdown::resize_list() { 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) @@ -163,7 +177,7 @@ void Dropdown::resize_list() list.set_geometry(lgeom); } -void Dropdown::list_item_selected(unsigned index, const std::string &item) +void Dropdown::list_item_selected(size_t index) { if(dropped) { @@ -172,19 +186,27 @@ void Dropdown::list_item_selected(unsigned index, const std::string &item) signal_ungrab_pointer.emit(); } - signal_item_selected.emit(index, item); + text.set(list.get_data().get_string(index)); + + signal_item_selected.emit(index); + mark_rebuild(); +} + +void Dropdown::list_selection_cleared() +{ + text.set(string()); } Dropdown::Loader::Loader(Dropdown &d): - Widget::Loader(d) + DataFile::DerivedObjectLoader(d) { add("item", &Loader::item); } -void Dropdown::Loader::item(const string &str) +void Dropdown::Loader::item(const string &v) { - dynamic_cast(obj).append(str); + dynamic_cast &>(obj.list.get_data()).append(v); } } // namespace GLtk