X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdropdown.cpp;h=1084612304f03b68a303af130aa2b3bd3b1b2f82;hb=878faa0c9283ee1e6e5e67b6ea1324cc52385742;hp=fcf35babdf132234476f9989eeac7c37a83a583e;hpb=1d84ac50711ce9d23cb1dafd83158d4ffa938a15;p=libs%2Fgltk.git diff --git a/source/dropdown.cpp b/source/dropdown.cpp index fcf35ba..1084612 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -25,25 +25,29 @@ Dropdown::Dropdown(ListData &d): void Dropdown::init() { + 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::autosize() +void Dropdown::autosize_special(const Part &part, Geometry &ageom) const { - if(!style) - return; - - Widget::autosize(); - list.autosize_all(); - geom.w = max(geom.w, list.get_geometry().w); - - if(const Part *text_part = style->get_part("text")) + 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 = text_part->get_margin(); + const Sides &margin = part.get_margin(); const GL::Font &font = style->get_font(); float font_size = style->get_font_size(); @@ -54,25 +58,19 @@ void Dropdown::autosize() unsigned w = static_cast(font.get_string_width(data.get_string(i))*font_size); max_w = max(max_w, w); } - geom.w = max(geom.w, max_w+margin.left+margin.right); + ageom.w = max(ageom.w, max_w+margin.left+margin.right); unsigned line_height = static_cast((font.get_ascent()-font.get_descent())*font_size); - geom.h = max(geom.h, line_height+margin.top+margin.bottom); + ageom.h = max(ageom.h, line_height+margin.top+margin.bottom); } - - rebuild(); } -void Dropdown::rebuild_special(const Part &part, CachedPart &cache) +void Dropdown::rebuild_special(const Part &part) { if(part.get_name()=="text") - { - int sel = list.get_selected_index(); - if(sel>=0) - Text(*style, list.get_data().get_string(sel)).build(part, geom, cache); - else - cache.texture = 0; - } + text.build(part, state, geom, part_cache); + else + Widget::rebuild_special(part); } void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const @@ -87,40 +85,71 @@ void Dropdown::button_press(int x, int y, unsigned btn) { Container::button_press(x, y, btn); if(!click_focus) - { - dropped = false; - clear_state(ACTIVE); - signal_ungrab_pointer.emit(); - } + close_list(); } else if(btn==1) + open_list(); +} + +bool Dropdown::navigate(Navigation nav) +{ + if(dropped) { - dropped = true; - set_state(ACTIVE); - signal_grab_pointer.emit(); + if(nav==NAV_CANCEL) + close_list(); + else + list.navigate(nav); } + else if(nav==NAV_ACTIVATE) + open_list(); + else + return false; + + return true; } -void Dropdown::on_geometry_change() +void Dropdown::on_size_change() { - resize_list(); + if(dropped) + resize_list(); } void Dropdown::on_style_change() { + text.set_style(style); + if(dropped) + resize_list(); +} + +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::close_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); @@ -134,7 +163,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) @@ -156,20 +188,27 @@ void Dropdown::list_item_selected(unsigned index) signal_ungrab_pointer.emit(); } + text.set(list.get_data().get_string(index)); + signal_item_selected.emit(index); - rebuild(); + 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 &v) { - dynamic_cast &>(dynamic_cast(obj).list.get_data()).append(v); + dynamic_cast &>(obj.list.get_data()).append(v); } } // namespace GLtk