]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Improve list focus management
[libs/gltk.git] / source / list.cpp
index 113871ef8ed51f35e35944fd0b155e9f2d65e958..a5928ec91a76793d453ef67cba2bd062d89165be 100644 (file)
@@ -1,13 +1,7 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/gl/immediate.h>
+#include <msp/core/raii.h>
+#include <msp/debug/demangle.h>
 #include <msp/gl/matrix.h>
-#include <msp/gl/transform.h>
+#include <msp/gl/meshbuilder.h>
 #include "graphic.h"
 #include "list.h"
 #include "part.h"
@@ -20,239 +14,548 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-List::List(const Resources &r):
-       Widget(r),
-       Container(r),
-       sel_index(-1),
-       first(0),
-       n_visible(1),
-       row_height(1),
-       items_part(0),
-       slider(res)
+incompatible_data::incompatible_data(const type_info &ti):
+       logic_error("expected "+Debug::demangle(ti.name()))
+{ }
+
+
+List::List():
+       data(new BasicListData<string>),
+       own_data(true)
+{
+       init();
+}
+
+List::List(ListData &d):
+       data(&d),
+       own_data(false)
+{
+       init();
+}
+
+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;
+
+       observer = new DataObserver(*this);
+
        add(slider);
        slider.set_step(1);
        slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
-
-       update_style();
 }
 
-void List::autosize()
+List::~List()
 {
-       float font_size = style->get_font()->get_default_size();
+       delete item_factory;
+       delete observer;
+       if(own_data)
+               delete data;
+}
 
-       geom.w = 0;
-       for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+void List::autosize_special(const Part &part, Geometry &ageom) const
+{
+       if(part.get_name()=="items")
        {
-               unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
-               geom.w = max(geom.w, w);
+               const Sides &margin = part.get_margin();
+
+               unsigned max_w = 0;
+               unsigned total_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;
+               }
+
+               if(!items.empty() && items.size()<view_size)
+                       total_h = total_h*view_size/items.size();
+
+               ageom.w = max(ageom.w, max_w+margin.left+margin.right);
+               ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
        }
+       else if(part.get_name()=="slider")
+               autosize_child(slider, part, ageom);
+}
+
+void List::set_data(ListData &d)
+{
+       if(item_factory)
+               item_factory->set_data(d);
 
-       geom.h = items.size()*row_height;
+       delete observer;
+       if(own_data)
+               delete data;
 
-       if(items_part)
+       data = &d;
+       own_data = false;
+       observer = new DataObserver(*this);
+
+       for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
+               delete *i;
+       items.clear();
+       unsigned n_items = data->size();
+       for(unsigned i=0; i<n_items; ++i)
        {
-               const Sides &margin = items_part->get_margin();
-               geom.w += margin.left+margin.right;
-               geom.h += margin.top+margin.bottom;
+               Item *item = create_item(i);
+               items.push_back(item);
        }
+
+       items_changed();
 }
 
-void List::append(const string &v)
+void List::items_changed()
 {
-       items.push_back(v);
-       check_view_range();
+       signal_autosize_changed.emit();
+       rebuild();
 }
 
-void List::insert(unsigned i, const string &v)
+List::Item *List::create_item(unsigned index)
 {
-       if(i>items.size())
-               throw InvalidParameterValue("Index out of range");
-
-       items.insert(items.begin()+i, v);
-       check_view_range();
+       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));
+       return item;
 }
 
-void List::remove(unsigned i)
+void List::set_view_size(unsigned s)
 {
-       if(i>items.size())
-               throw InvalidParameterValue("Index out of range");
-
-       items.erase(items.begin()+i);
-       if(sel_index>static_cast<int>(i))
-               --sel_index;
-       else if(sel_index==static_cast<int>(i))
-               sel_index = -1;
-
-       check_view_range();
+       view_size = s;
+       signal_autosize_changed.emit();
 }
 
-void List::clear()
+void List::set_view_all()
 {
-       items.clear();
-       sel_index = -1;
-
-       check_view_range();
+       set_view_size(0);
 }
 
 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 || (i<0 && sel_index<0))
+               return;
+
+       if(sel_index>=0)
+               items[sel_index]->set_active(false);
        if(i<0)
+       {
                sel_index = -1;
-       else if(i<static_cast<int>(items.size()))
+               focus_index = -1;
+               set_input_focus(0);
+               signal_selection_cleared.emit();
+       }
+       else
        {
                sel_index = i;
-               signal_item_selected.emit(sel_index, items[sel_index]);
+               focus_index = i;
+               items[sel_index]->set_active(true);
+               if(state&FOCUS)
+                       set_input_focus(items[focus_index]);
+               signal_item_selected.emit(sel_index);
        }
-       else
-               throw InvalidParameterValue("Index out of range");
 }
 
-const string &List::get_selected() const
+void List::rebuild_special(const Part &part)
 {
-       if(sel_index<0)
-               throw InvalidState("No selection");
+       if(part.get_name()=="slider")
+               reposition_child(slider, part);
+       else if(part.get_name()=="items")
+       {
+               SetFlag flag(ignore_slider_change);
+               check_view_range();
+
+               const Sides &margin = part.get_margin();
+               unsigned w = geom.w-min(geom.w, margin.left+margin.right);
+               unsigned y = geom.h-min(geom.h, margin.top);
+               for(unsigned i=0; i<items.size(); ++i)
+               {
+                       if(i<first || !y)
+                               items[i]->set_visible(false);
+                       else
+                       {
+                               Geometry igeom = items[i]->get_geometry();
+                               if(igeom.h+margin.bottom<=y)
+                               {
+                                       items[i]->set_visible(true);
+                                       y -= igeom.h;
+                                       igeom.x = margin.left;
+                                       igeom.y = y;
+                                       igeom.w = w;
+                                       items[i]->set_geometry(igeom);
+                               }
+                               else
+                               {
+                                       items[i]->set_visible(false);
+                                       y = 0;
+                               }
+                       }
+               }
+       }
 
-       return items[sel_index];
+       Widget::rebuild_special(part);
 }
 
-void List::button_press(int x, int y, unsigned btn)
+void List::render_special(const Part &part, GL::Renderer &renderer) const
 {
-       Container::button_press(x, y, btn);
-       if(!click_focus && btn==1)
+       if(part.get_name()=="items")
        {
-               if(items_part)
-                       y += items_part->get_margin().top;
+               for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+                       items[i]->render(renderer);
+       }
+       else if(part.get_name()=="slider")
+               slider.render(renderer);
+}
 
-               unsigned i = (geom.h-1-y)/row_height;
-               if(i<n_visible && first+i<items.size())
+void List::button_press(int x, int y, unsigned btn)
+{
+       if(btn==4 || btn==5)
+       {
+               unsigned change = 3;
+               if(btn==4)
                {
-                       sel_index = first+i;
-
-                       signal_item_selected.emit(sel_index, items[sel_index]);
+                       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)
+               {
+                       for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+                               if(click_focus==items[i])
+                               {
+                                       set_selected_index(i);
+                                       break;
+                               }
                }
        }
 }
 
-void List::render_special(const Part &part) const
+void List::focus_in()
 {
-       if(part.get_name()=="items")
+       if(focus_index>=0 && items[focus_index]->is_visible())
+               set_input_focus(items[focus_index]);
+       else
        {
-               const Sides &margin = part.get_margin();
-               Geometry pgeom = geom;
-               pgeom.h = row_height+margin.top+margin.bottom;
+               if(sel_index>=0 && items[sel_index]->is_visible())
+                       set_focus_index(sel_index);
+               else if(!items.empty())
+                       set_focus_index(first);
+       }
+}
 
-               GL::PushMatrix push_mtx;
-               GL::translate(0, geom.h-pgeom.h, 0);
+bool List::navigate(Navigation nav)
+{
+       if(nav==NAV_UP && !items.empty())
+       {
+               if(focus_index>0)
+                       set_focus_index(focus_index-1);
+       }
+       else if(nav==NAV_DOWN && !items.empty())
+       {
+               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;
 
-               for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
-               {
-                       if(i!=0)
-                               GL::translate(0, -static_cast<int>(row_height), 0);
-                       Text(*style, items[first+i]).render(part, pgeom);
-               }
+       return true;
+}
+
+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]);
        }
-       else if(part.get_name()=="selection")
+}
+
+void List::item_autosize_changed(Item *item)
+{
+       item->autosize();
+       signal_autosize_changed.emit();
+       rebuild();
+}
+
+unsigned List::last_to_first(unsigned last) const
+{
+       if(!style)
+               return last;
+
+       unsigned view_h = geom.h;
+       if(const Part *items_part = style->get_part("items"))
        {
-               if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
-               {
-                       const Sides &margin = part.get_margin();
+               const Sides &margin = items_part->get_margin();
+               view_h -= margin.top+margin.bottom;
+       }
 
-                       Geometry pgeom = geom;
-                       pgeom.h = row_height;
-                       pgeom.w -= margin.left+margin.right;
+       unsigned items_h = 0;
+       for(unsigned i=last; i<items.size(); --i)
+       {
+               items_h += items[i]->get_geometry().h;
+               if(items_h>view_h)
+                       return min(i+1, last);
+       }
 
-                       Geometry rgeom = part.get_geometry();
-                       rgeom.y += geom.h-margin.top-row_height*(sel_index-first+1);
-                       rgeom.x += margin.left;
-                       part.get_alignment().apply(rgeom, pgeom);
+       return 0;
+}
 
-                       GL::push_matrix();
-                       GL::translate(rgeom.x, rgeom.y, 0);
-                       part.get_graphic(state)->render(rgeom.w, rgeom.h);
-                       GL::pop_matrix();
-               }
+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;
+
+       slider.set_range(0, max_scroll);
+       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)
+       {
+               first = max_scroll-static_cast<unsigned>(value);
+               rebuild();
        }
-       else if(part.get_name()=="slider")
-               slider.render();
 }
 
-void List::on_geometry_change()
+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)
+{
+       list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
+       list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
+       list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
+       list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
+}
+
+void List::DataObserver::item_added(unsigned i)
 {
-       reposition_slider();
+       adjust_index(list.sel_index, i, 1);
+       adjust_index(list.focus_index, i, 1);
 
-       check_view_range();
+       Item *item = list.create_item(i);
+       list.items.insert(list.items.begin()+i, item);
+       list.items_changed();
 }
 
-void List::on_style_change()
+void List::DataObserver::item_removed(unsigned i)
 {
-       reposition_slider();
+       bool had_selection = (list.sel_index>=0);
+       adjust_index(list.sel_index, i, -1);
+       adjust_index(list.focus_index, i, -1);
 
-       items_part = style->get_part("items");
+       delete list.items[i];
+       list.items.erase(list.items.begin()+i);
+       list.items_changed();
 
-       const GL::Font &font = *style->get_font();
-       row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
+       if(had_selection && list.sel_index<0)
+               list.signal_selection_cleared.emit();
+}
 
-       check_view_range();
+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)
+{
+       delete list.items[i];
+       // Avoid stale pointer while create_item is executing
+       list.items[i] = 0;
+       list.items[i] = list.create_item(i);
+       list.items_changed();
 }
 
-void List::reposition_slider()
+
+void List::Item::autosize_special(const Part &part, Geometry &ageom) const
 {
-       if(const Part *slider_part = style->get_part("slider"))
+       if(part.get_name()=="children")
        {
-               Geometry sgeom = slider_part->get_geometry();
-               slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
-               slider.set_geometry(sgeom);
+               const Sides &margin = part.get_margin();
+               for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+               {
+                       Geometry cgeom;
+                       (*i)->widget->autosize(cgeom);
+                       ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
+                       ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
+               }
        }
 }
 
-void List::check_view_range()
+void List::Item::set_active(bool a)
 {
-       unsigned h = geom.h;
-       if(items_part)
+       set_state(ACTIVE, (a ? ACTIVE : NORMAL));
+}
+
+void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
+{
+       if(part.get_name()=="children")
        {
-               const Sides &margin = items_part->get_margin();
-               h -= margin.top+margin.bottom;
+               for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+                       (*i)->widget->render(renderer);
        }
+}
 
-       n_visible = h/row_height;
 
-       if(first+n_visible>items.size())
+void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
+{
+       if(widths.size()<children.size())
+               widths.resize(children.size(), 0);
+
+       unsigned n = 0;
+       for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
        {
-               if(items.size()>n_visible)
-                       first = items.size()-n_visible;
-               else
-                       first = 0;
+               Geometry cgeom;
+               (*i)->widget->autosize(cgeom);
+               // TODO invent a better way to specify spacings
+               widths[n] = max(widths[n], cgeom.w+8);
        }
+}
+
+void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
+{
+       if(!style)
+               return;
 
-       if(items.size()>n_visible)
+       const Part *part = style->get_part("children");
+       if(!part)
+               return;
+
+       const Sides &margin = part->get_margin();
+       int x = margin.left;
+       unsigned n = 0;
+       for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
        {
-               slider.set_range(0, items.size()-n_visible);
-               slider.set_value(items.size()-n_visible-first);
+               (*i)->widget->set_position(x, margin.bottom);
+               x += widths[n];
        }
-       else
+}
+
+void List::MultiColumnItem::on_style_change()
+{
+       if(!style)
+               return;
+
+       for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+               (*i)->widget->autosize();
+
+       vector<unsigned> widths;
+       List *list = static_cast<List *>(parent);
+       for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
+               if(*i!=this)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
+                               mci->check_widths(widths);
+
+       vector<unsigned> self_widths(widths);
+       check_widths(self_widths);
+       bool update_all = false;
+       for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
+               update_all = self_widths[i]>widths[i];
+
+       if(update_all)
        {
-               slider.set_range(0, 0);
-               slider.set_value(0);
+               for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
+                               mci->set_widths(self_widths);
        }
+
+       set_widths(self_widths);
 }
 
-void List::slider_value_changed(double value)
+
+List::BasicItem::BasicItem(const string &text):
+       label(text)
 {
-       if(items.size()>n_visible)
-               first = items.size()-n_visible-static_cast<unsigned>(value);
+       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):
-       Widget::Loader(l)
+       DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
 {
        add("item", &Loader::item);
+       add("view_size", &List::view_size);
 }
 
 void List::Loader::item(const string &v)
 {
-       dynamic_cast<List &>(wdg).append(v);
+       dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
 }
 
 } // namespace GLtk