]> git.tdb.fi Git - libs/gltk.git/commitdiff
Move navigation logic from Container to Panel
authorMikko Rasa <tdb@tdb.fi>
Thu, 1 Sep 2016 07:46:57 +0000 (10:46 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 1 Sep 2016 07:46:57 +0000 (10:46 +0300)
A number of widgets inherit from Container because they have internal
sub-widgets, but have their own navigation logic.  Panel is the user-
visible generic container.

source/container.cpp
source/panel.cpp
source/panel.h

index b50bc29e2f9c120c5fee986fa319cafde6d8c459..c38509fd931fe37ca756818dbb575b5c3d20b210 100644 (file)
@@ -304,66 +304,10 @@ void Container::focus_out()
 
 bool Container::navigate(Navigation nav)
 {
-       if(input_focus && input_focus->navigate(nav))
-               return true;
-
-       if(nav==NAV_UP || nav==NAV_DOWN || nav==NAV_LEFT || nav==NAV_RIGHT)
-       {
-               int x = geom.w/2;
-               int y = geom.h/2;
-               if(input_focus)
-               {
-                       const Geometry &fgeom = input_focus->get_geometry();
-                       x = fgeom.x+fgeom.w/2;
-                       y = fgeom.y+fgeom.h/2;
-               }
-               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<Child *>::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 = 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 || score<best_score))
-                       {
-                               sibling = (*i)->widget;
-                               best_score = score;
-                       }
-               }
-
-               if(sibling)
-               {
-                       set_input_focus(sibling);
-                       if(Container *container = dynamic_cast<Container *>(sibling))
-                               container->navigate(nav);
-                       return true;
-               }
-       }
-
-       return false;
+       if(input_focus)
+               return input_focus->navigate(nav);
+       else
+               return false;
 }
 
 void Container::on_reparent()
index c17fe4b261e74650a55349c62f314b39d710b06f..7e34c5947a7c7f4e0f93822eac08707c0ba3f104 100644 (file)
@@ -59,6 +59,70 @@ void Panel::render_special(const Part &part, GL::Renderer &renderer) const
        }
 }
 
+bool Panel::navigate(Navigation nav)
+{
+       if(Container::navigate(nav))
+               return true;
+
+       if(nav==NAV_UP || nav==NAV_DOWN || nav==NAV_LEFT || nav==NAV_RIGHT)
+       {
+               int x = geom.w/2;
+               int y = geom.h/2;
+               if(input_focus)
+               {
+                       const Geometry &fgeom = input_focus->get_geometry();
+                       x = fgeom.x+fgeom.w/2;
+                       y = fgeom.y+fgeom.h/2;
+               }
+               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<Child *>::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 = 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 || score<best_score))
+                       {
+                               sibling = (*i)->widget;
+                               best_score = score;
+                       }
+               }
+
+               if(sibling)
+               {
+                       set_input_focus(sibling);
+                       if(Panel *panel = dynamic_cast<Panel *>(sibling))
+                               panel->navigate(nav);
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 void Panel::on_geometry_change()
 {
        if(layout)
index e0eed014855030453f3dd62ce4e2ddf2e506ae57..ebe765ddcf5f541c8d661231b1957536999978a8 100644 (file)
@@ -71,6 +71,10 @@ protected:
        virtual void autosize_special(const Part &, Geometry &) const;
        virtual void render_special(const Part &, GL::Renderer &) const;
 
+public:
+       virtual bool navigate(Navigation);
+
+protected:
        virtual void on_geometry_change();
        virtual void on_child_added(Widget &);
        virtual void on_child_removed(Widget &);