X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpanel.cpp;h=97e13a2591b34dd9107f2dd65c22baa86750f676;hb=cfb830ca263defc307f9cfac74fb6771f6b7bfc6;hp=7e34c5947a7c7f4e0f93822eac08707c0ba3f104;hpb=ae62c05fe97d341c4f219656cdce7aadf321991b;p=libs%2Fgltk.git diff --git a/source/panel.cpp b/source/panel.cpp index 7e34c59..97e13a2 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "button.h" @@ -66,49 +66,34 @@ bool Panel::navigate(Navigation nav) if(nav==NAV_UP || nav==NAV_DOWN || nav==NAV_LEFT || nav==NAV_RIGHT) { - int x = geom.w/2; - int y = geom.h/2; + int nav_x = (nav==NAV_RIGHT ? 1 : nav==NAV_LEFT ? -1 : 0); + int nav_y = (nav==NAV_UP ? 1 : nav==NAV_DOWN ? -1 : 0); + + int origin_x, origin_y, origin_dim; if(input_focus) { const Geometry &fgeom = input_focus->get_geometry(); - x = fgeom.x+fgeom.w/2; - y = fgeom.y+fgeom.h/2; + origin_x = fgeom.x+(nav_x*0.5+0.5)*fgeom.w; + origin_y = fgeom.y+(nav_y*0.5+0.5)*fgeom.h; + origin_dim = abs(nav_x)*fgeom.h+abs(nav_y)*fgeom.w; + } + else + { + origin_x = geom.w*(0.5-nav_x*0.5); + origin_y = geom.h*(0.5-nav_y*0.5); + origin_dim = abs(nav_x)*geom.h+abs(nav_y)*geom.w; } - else if(nav==NAV_UP) - y = 0; - else if(nav==NAV_DOWN) - y = geom.h; - else if(nav==NAV_RIGHT) - x = 0; - else if(nav==NAV_LEFT) - x = geom.w; - - Widget *sibling = 0; - int best_score = 0; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + + if(pointer_grabbed && pointer_focus==input_focus) + return false; + + Widget *sibling = find_next_child(origin_x, origin_y, origin_dim, nav_x, nav_y); + if(!sibling && input_focus) { - if((*i)->widget==input_focus || !(*i)->widget->is_focusable()) - continue; - - const Geometry &cgeom = (*i)->widget->get_geometry(); - int dx = cgeom.x+cgeom.w/2-x; - int dy = cgeom.y+cgeom.h/2-y; - - int score = -1; - if(nav==NAV_UP && dy>0) - score = dy+abs(dx)*4; - else if(nav==NAV_DOWN && dy<0) - score = -dy+abs(dx)*4; - else if(nav==NAV_RIGHT && dx>0) - score = dx+abs(dy)*4; - else if(nav==NAV_LEFT && dx<0) - score = -dx+abs(dy)*4; - - if(score>0 && (!sibling || scorewidget; - best_score = score; - } + const Geometry &fgeom = input_focus->get_geometry(); + origin_x -= fgeom.w*(nav_x*0.5); + origin_y -= fgeom.h*(nav_y*0.5); + sibling = find_next_child(origin_x, origin_y, origin_dim, nav_x, nav_y); } if(sibling) @@ -119,10 +104,75 @@ bool Panel::navigate(Navigation nav) return true; } } + else if(nav==NAV_NEXT || nav==NAV_PREVIOUS) + { + vector::iterator i = find(nav_order, input_focus); + + if(nav==NAV_NEXT) + { + if(i!=nav_order.end()) + ++i; + if(i==nav_order.end()) + i = nav_order.begin(); + } + else + { + if(i==nav_order.begin()) + i = nav_order.end(); + --i; + } + + set_input_focus(*i); + + return true; + } return false; } +Widget *Panel::find_next_child(int origin_x, int origin_y, int origin_dim, int nav_x, int nav_y) const +{ + Widget *sibling = 0; + int best_score = 0; + for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + { + if((*i)->widget==input_focus || !(*i)->widget->is_focusable()) + continue; + + const Geometry &cgeom = (*i)->widget->get_geometry(); + int dx = compute_delta(cgeom.x, cgeom.w, origin_x, origin_dim, nav_x); + int dy = compute_delta(cgeom.y, cgeom.h, origin_y, origin_dim, nav_y); + + int score = -1; + if(nav_y && nav_y*dy>=0) + score = nav_y*dy+abs(dx)*4; + else if(nav_x && nav_x*dx>=0) + score = nav_x*dx+abs(dy)*4; + + if(score>=0 && (!sibling || scorewidget; + best_score = score; + } + } + + return sibling; +} + +int Panel::compute_delta(int pos, int dim, int origin_pos, int origin_dim, int nav) +{ + if(nav<0) + return pos+dim-origin_pos; + else if(nav>0) + return pos-origin_pos; + else if(pos+dimorigin_pos+origin_dim/2) + return pos-origin_pos-origin_dim/2; + else + return 0; +} + void Panel::on_geometry_change() { if(layout) @@ -131,6 +181,9 @@ void Panel::on_geometry_change() void Panel::on_child_added(Widget &wdg) { + if(wdg.get_input_type()!=INPUT_NONE) + nav_order.push_back(&wdg); + if(layout) { layout->add_widget(wdg); @@ -140,6 +193,10 @@ void Panel::on_child_added(Widget &wdg) void Panel::on_child_removed(Widget &wdg) { + vector::iterator i = std::remove(nav_order.begin(), nav_order.end(), &wdg); + if(i!=nav_order.end()) + nav_order.erase(i, nav_order.end()); + if(layout) { layout->remove_widget(wdg); @@ -153,27 +210,34 @@ Panel::Loader::Loader(Panel &p, map &m): wdg_map(m), last_widget(0) { - add("button", &Loader::child