]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Use the maximum item height for autosizing List
[libs/gltk.git] / source / list.cpp
index f42d573866317c643473e8b678e76cf543a677a5..005f6fa8ea29cc2a57cd23152f46167dacf922a1 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"
@@ -35,12 +36,18 @@ List::List(ListData &d):
 
 void List::init()
 {
+       input_type = INPUT_NAVIGATION;
+
        item_factory = 0;
        sel_index = -1;
+       focus_index = -1;
        first = 0;
        max_scroll = 0;
        view_size = 5;
        ignore_slider_change = false;
+       dragging = false;
+       drag_start_x = 0;
+       drag_start_y = 0;
 
        observer = new DataObserver(*this);
 
@@ -64,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);
@@ -118,11 +123,13 @@ void List::items_changed()
 
 List::Item *List::create_item(unsigned index)
 {
-       Item *item = 0; 
+       Item *item = 0;
        if(item_factory)
                item = item_factory->create_item(index);
        else
                item = new BasicItem(data->get_string(index));
+       if(static_cast<int>(index)==sel_index)
+               item->set_active(true);
        add(*item);
        item->autosize();
        item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
@@ -145,21 +152,36 @@ void List::set_selected_index(int i)
        if(i>=static_cast<int>(data->size()))
                throw out_of_range("List::set_selected_index");
 
-       if(i==sel_index)
+       if(i==sel_index || (i<0 && sel_index<0))
                return;
 
        if(sel_index>=0)
                items[sel_index]->set_active(false);
        if(i<0)
+       {
                sel_index = -1;
+               focus_index = -1;
+               set_input_focus(0);
+               signal_selection_cleared.emit();
+       }
        else
        {
                sel_index = i;
+               focus_index = i;
                items[sel_index]->set_active(true);
+               if(state&FOCUS)
+                       set_input_focus(items[focus_index]);
                signal_item_selected.emit(sel_index);
        }
 }
 
+void List::set_selected_item(Widget *item)
+{
+       for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+               if(item==items[i])
+                       return set_selected_index(i);
+}
+
 void List::rebuild_special(const Part &part)
 {
        if(part.get_name()=="slider")
@@ -211,17 +233,147 @@ 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)
 {
-       Container::button_press(x, y, btn);
-       if(click_focus && btn==1)
+       if(btn==4 || btn==5)
        {
-               for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
-                       if(click_focus==items[i])
+               unsigned change = 3;
+               if(btn==4)
+               {
+                       change = min(first, change);
+                       slider.set_value(max_scroll-(first-change));
+               }
+               else if(btn==5)
+               {
+                       change = min(max_scroll-first, change);
+                       slider.set_value(max_scroll-(first+change));
+               }
+       }
+       else
+       {
+               Container::button_press(x, y, btn);
+               if(click_focus && btn==1)
+                       set_selected_item(click_focus);
+       }
+}
+
+void List::touch_press(int x, int y, unsigned finger)
+{
+       if(finger==0)
+       {
+               dragging = true;
+               drag_start_x = x;
+               drag_start_y = y;
+       }
+}
+
+void List::touch_release(int x, int y, unsigned finger)
+{
+       if(finger==0)
+       {
+               int dx = x-drag_start_x;
+               int dy = y-drag_start_y;
+               if(dx*dx+dy*dy<25)
+               {
+                       Container::touch_press(drag_start_x, drag_start_y, finger);
+                       if(touch_focus)
+                               set_selected_item(touch_focus);
+                       Container::touch_motion(x, y, finger);
+                       Container::touch_release(x, y, finger);
+               }
+               dragging = false;
+       }
+}
+
+void List::touch_motion(int, int y, unsigned finger)
+{
+       if(finger==0 && !items.empty() && dragging)
+       {
+               int dy = y-drag_start_y;
+               if(dy>0 && first<max_scroll)
+               {
+                       int item_h = items[first]->get_geometry().h;
+                       if(dy>item_h)
+                       {
+                               drag_start_y += item_h;
+                               slider.set_value(max_scroll-(first+1));
+                       }
+               }
+               else if(dy<0 && first>0)
+               {
+                       int item_h = items[first-1]->get_geometry().h;
+                       if(-dy>item_h)
                        {
-                               set_selected_index(i);
-                               break;
+                               drag_start_y -= item_h;
+                               slider.set_value(max_scroll-(first-1));
                        }
+               }
+       }
+}
+
+void List::focus_in()
+{
+       Container::focus_in();
+       if(focus_index>=0 && items[focus_index]->is_visible())
+               set_input_focus(items[focus_index]);
+       else
+       {
+               if(sel_index>=0 && items[sel_index]->is_visible())
+                       set_focus_index(sel_index);
+               else if(!items.empty())
+                       set_focus_index(first);
+       }
+}
+
+bool List::navigate(Navigation nav)
+{
+       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)
+       {
+               if(static_cast<unsigned>(focus_index+1)<items.size())
+                       set_focus_index(focus_index+1);
+       }
+
+       if(select)
+               set_selected_index(focus_index);
+}
+
+void List::set_focus_index(int i)
+{
+       focus_index = i;
+       if(focus_index>=0)
+       {
+               scroll_to_focus();
+               if(state&FOCUS)
+                       set_input_focus(items[focus_index]);
        }
 }
 
@@ -232,29 +384,39 @@ void List::item_autosize_changed(Item *item)
        rebuild();
 }
 
-void List::check_view_range()
+unsigned List::last_to_first(unsigned last) const
 {
        if(!style)
-               return;
+               return last;
 
-       unsigned h = geom.h;
+       unsigned view_h = geom.h;
        if(const Part *items_part = style->get_part("items"))
        {
                const Sides &margin = items_part->get_margin();
-               h -= margin.top+margin.bottom;
+               view_h -= margin.top+margin.bottom;
        }
 
-       max_scroll = items.size();
-       for(unsigned i=items.size(); i-->0; )
+       unsigned items_h = 0;
+       for(unsigned i=last; i<items.size(); --i)
        {
-               unsigned ih = items[i]->get_geometry().h;
-               if(ih<=h)
-               {
-                       h -= ih;
-                       --max_scroll;
-               }
+               items_h += items[i]->get_geometry().h;
+               if(items_h>view_h)
+                       return min(i+1, last);
        }
 
+       return 0;
+}
+
+void List::check_view_range()
+{
+       if(!style)
+               return;
+
+       if(items.empty())
+               max_scroll = 0;
+       else
+               max_scroll = last_to_first(items.size()-1);
+
        if(first>max_scroll)
                first = max_scroll;
 
@@ -262,6 +424,17 @@ void List::check_view_range()
        slider.set_value(max_scroll-first);
 }
 
+void List::scroll_to_focus()
+{
+       if(focus_index<0 || items[focus_index]->is_visible())
+               return;
+
+       if(static_cast<unsigned>(focus_index)<first)
+               slider.set_value(max_scroll-focus_index);
+       else
+               slider.set_value(max_scroll-last_to_first(focus_index));
+}
+
 void List::slider_value_changed(double value)
 {
        if(max_scroll>0 && !ignore_slider_change)
@@ -271,6 +444,14 @@ void List::slider_value_changed(double value)
        }
 }
 
+void List::adjust_index(int &index, int pos, int change)
+{
+       if(index>pos)
+               index += change;
+       else if(index==pos)
+               index = (change>0 ? index+change : -1);
+}
+
 
 List::DataObserver::DataObserver(List &l):
        list(l)
@@ -283,8 +464,8 @@ List::DataObserver::DataObserver(List &l):
 
 void List::DataObserver::item_added(unsigned i)
 {
-       if(list.sel_index>=static_cast<int>(i))
-               ++list.sel_index;
+       adjust_index(list.sel_index, i, 1);
+       adjust_index(list.focus_index, i, 1);
 
        Item *item = list.create_item(i);
        list.items.insert(list.items.begin()+i, item);
@@ -293,23 +474,28 @@ void List::DataObserver::item_added(unsigned i)
 
 void List::DataObserver::item_removed(unsigned i)
 {
-       if(list.sel_index>static_cast<int>(i))
-               --list.sel_index;
-       else if(list.sel_index==static_cast<int>(i))
-               list.sel_index = -1;
+       bool had_selection = (list.sel_index>=0);
+       adjust_index(list.sel_index, i, -1);
+       adjust_index(list.focus_index, i, -1);
 
        delete list.items[i];
        list.items.erase(list.items.begin()+i);
        list.items_changed();
+
+       if(had_selection && list.sel_index<0)
+               list.signal_selection_cleared.emit();
 }
 
 void List::DataObserver::cleared()
 {
        list.sel_index = -1;
+       list.focus_index = -1;
        for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
                delete *i;
        list.items.clear();
        list.items_changed();
+
+       list.signal_selection_cleared.emit();
 }
 
 void List::DataObserver::refresh_item(unsigned i)
@@ -413,8 +599,8 @@ void List::MultiColumnItem::on_style_change()
                        if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
                                mci->set_widths(self_widths);
        }
-       else
-               set_widths(self_widths);
+
+       set_widths(self_widths);
 }