]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Add a helper class for single-child list items
[libs/gltk.git] / source / list.cpp
index 5a9f749fd0363b974ee1bfab98c37f37264a4354..9be88e52c9ce65acecdf98b550d78a5db9a672ee 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/debug/demangle.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/meshbuilder.h>
+#include <msp/input/keys.h>
 #include "graphic.h"
 #include "list.h"
 #include "part.h"
@@ -70,18 +71,16 @@ void List::autosize_special(const Part &part, Geometry &ageom) const
                const Sides &margin = part.get_margin();
 
                unsigned max_w = 0;
-               unsigned total_h = 0;
+               unsigned max_h = 0;
                for(unsigned i=0; i<items.size(); ++i)
                {
                        Geometry igeom;
                        items[i]->autosize(igeom);
                        max_w = max(max_w, igeom.w);
-                       if(view_size==0 || i<view_size)
-                               total_h += igeom.h;
+                       max_h = max(max_h, igeom.h);
                }
 
-               if(!items.empty() && items.size()<view_size)
-                       total_h = total_h*view_size/items.size();
+               unsigned total_h = max_h*(view_size==0 ? items.size() : view_size);
 
                ageom.w = max(ageom.w, max_w+margin.left+margin.right);
                ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
@@ -234,6 +233,18 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const
                slider.render(renderer);
 }
 
+bool List::key_press(unsigned key, unsigned mod)
+{
+       if(key==Input::KEY_UP && mod==MOD_CTRL)
+               move_focus(NAV_UP, false);
+       else if(key==Input::KEY_DOWN && mod==MOD_CTRL)
+               move_focus(NAV_DOWN, false);
+       else
+               return false;
+
+       return true;
+}
+
 void List::button_press(int x, int y, unsigned btn)
 {
        if(btn==4 || btn==5)
@@ -314,6 +325,7 @@ void List::touch_motion(int, int y, unsigned finger)
 
 void List::focus_in()
 {
+       Container::focus_in();
        if(focus_index>=0 && items[focus_index]->is_visible())
                set_input_focus(items[focus_index]);
        else
@@ -327,22 +339,31 @@ void List::focus_in()
 
 bool List::navigate(Navigation nav)
 {
-       if(nav==NAV_UP && !items.empty())
+       if((nav==NAV_UP || nav==NAV_DOWN) && !items.empty())
+               move_focus(nav, true);
+       else if(nav==NAV_ACTIVATE)
+               set_selected_index(focus_index);
+       else
+               return false;
+
+       return true;
+}
+
+void List::move_focus(Navigation nav, bool select)
+{
+       if(nav==NAV_UP)
        {
                if(focus_index>0)
                        set_focus_index(focus_index-1);
        }
-       else if(nav==NAV_DOWN && !items.empty())
+       else if(nav==NAV_DOWN)
        {
                if(static_cast<unsigned>(focus_index+1)<items.size())
                        set_focus_index(focus_index+1);
        }
-       else if(nav==NAV_ACTIVATE)
-               set_selected_index(focus_index);
-       else
-               return false;
 
-       return true;
+       if(select)
+               set_selected_index(focus_index);
 }
 
 void List::set_focus_index(int i)
@@ -517,6 +538,21 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
 }
 
 
+void List::SimpleItem::on_style_change()
+{
+       if(!style || children.empty())
+               return;
+
+       Widget *child = children.front()->widget;
+       child->autosize();
+       if(const Part *part = style->get_part("children"))
+       {
+               const Sides &margin = part->get_margin();
+               child->set_position(margin.left, margin.bottom);
+       }
+}
+
+
 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
 {
        if(widths.size()<children.size())
@@ -589,19 +625,6 @@ List::BasicItem::BasicItem(const string &text):
        add(label);
 }
 
-void List::BasicItem::on_style_change()
-{
-       if(!style)
-               return;
-
-       label.autosize();
-       if(const Part *part = style->get_part("children"))
-       {
-               const Sides &margin = part->get_margin();
-               label.set_position(margin.left, margin.bottom);
-       }
-}
-
 
 List::Loader::Loader(List &l):
        DataFile::DerivedObjectLoader<List, Widget::Loader>(l)