]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Use nullptr instead of 0 for pointers
[libs/gltk.git] / source / list.cpp
index a5285832b98994f5f9cc4365d41c28c5b37b1176..e0d2bd81c150d62918bedd77b7eebe3b1537baac 100644 (file)
@@ -3,12 +3,12 @@
 #include <msp/gl/matrix.h>
 #include <msp/gl/meshbuilder.h>
 #include <msp/input/keys.h>
+#include <msp/strings/format.h>
 #include "graphic.h"
 #include "list.h"
 #include "part.h"
 #include "style.h"
 #include "text.h"
-#include "vslider.h"
 
 using namespace std;
 
@@ -21,37 +21,17 @@ incompatible_data::incompatible_data(const type_info &ti):
 
 
 List::List():
-       data(new BasicListData<string>),
-       own_data(true)
+       List(*new BasicListData<string>)
 {
-       init();
+       own_data = true;
 }
 
 List::List(ListData &d):
        data(&d),
-       own_data(false)
-{
-       init();
-}
-
-void List::init()
+       observer(new DataObserver(*this))
 {
        input_type = INPUT_NAVIGATION;
 
-       item_factory = 0;
-       sel_index = -1;
-       focus_index = -1;
-       first_row = 0;
-       max_scroll = 0;
-       view_rows = 5;
-       items_part = 0;
-       ignore_slider_change = false;
-       dragging = false;
-       drag_start_x = 0;
-       drag_start_y = 0;
-
-       observer = new DataObserver(*this);
-
        add(slider);
        slider.set_step(1);
        slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
@@ -71,20 +51,34 @@ void List::autosize_special(const Part &part, Geometry &ageom) const
        {
                const Sides &margin = part.get_margin();
 
-               unsigned max_w = 0;
-               unsigned max_h = 0;
+               unsigned items_w = 0;
+               unsigned items_h = 0;
                for(unsigned i=0; i<items.size(); ++i)
                {
                        Geometry igeom;
                        items[i]->autosize(igeom);
-                       max_w = max(max_w, igeom.w);
-                       max_h = max(max_h, igeom.h);
+                       items_w = max(items_w, igeom.w);
+                       items_h = max(items_h, igeom.h);
                }
 
-               unsigned total_h = max_h*(view_rows==0 ? items.size() : view_rows);
+               if(view_mode==GRID)
+               {
+                       unsigned r = view_rows;
+                       unsigned c = view_columns;
+                       if(r==0 && c==0)
+                               r = sqrt(items.size());
+                       if(r==0)
+                               r = (items.size()+c-1)/c;
+                       if(c==0)
+                               c = (items.size()+r-1)/r;
+                       items_w *= c;
+                       items_h *= r;
+               }
+               else
+                       items_h *= (view_rows==0 ? items.size() : view_rows);
 
-               ageom.w = max(ageom.w, max_w+margin.left+margin.right);
-               ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
+               ageom.w = max(ageom.w, items_w+margin.left+margin.right);
+               ageom.h = max(ageom.h, items_h+margin.top+margin.bottom);
        }
        else if(part.get_name()=="slider")
                autosize_child(slider, part, ageom);
@@ -103,8 +97,8 @@ void List::set_data(ListData &d)
        own_data = false;
        observer = new DataObserver(*this);
 
-       for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
-               delete *i;
+       for(Item *i: items)
+               delete i;
        items.clear();
        unsigned n_items = data->size();
        for(unsigned i=0; i<n_items; ++i)
@@ -119,12 +113,12 @@ void List::set_data(ListData &d)
 void List::items_changed()
 {
        signal_autosize_changed.emit();
-       rebuild();
+       mark_rebuild();
 }
 
 List::Item *List::create_item(unsigned index)
 {
-       Item *item = 0;
+       Item *item = nullptr;
        if(item_factory)
                item = item_factory->create_item(index);
        else
@@ -137,9 +131,15 @@ List::Item *List::create_item(unsigned index)
        return item;
 }
 
-void List::set_view_size(unsigned r)
+void List::set_view_size(unsigned s)
+{
+       set_view_size(s, s);
+}
+
+void List::set_view_size(unsigned r, unsigned c)
 {
        view_rows = r;
+       view_columns = c;
        signal_autosize_changed.emit();
 }
 
@@ -162,7 +162,7 @@ void List::set_selected_index(int i)
        {
                sel_index = -1;
                focus_index = -1;
-               set_input_focus(0);
+               set_input_focus(nullptr);
                signal_selection_cleared.emit();
        }
        else
@@ -318,7 +318,7 @@ void List::focus_in()
 
 bool List::navigate(Navigation nav)
 {
-       if((nav==NAV_UP || nav==NAV_DOWN) && !items.empty())
+       if((nav==NAV_UP || nav==NAV_DOWN || ((nav==NAV_LEFT || nav==NAV_RIGHT) && view_mode==GRID)) && !items.empty())
                move_focus(nav, true);
        else if(nav==NAV_ACTIVATE)
                set_selected_index(focus_index);
@@ -330,17 +330,33 @@ bool List::navigate(Navigation nav)
 
 void List::on_style_change()
 {
-       items_part = (style ? style->get_part("items") : 0);
+       items_part = (style ? style->get_part("items") : nullptr);
 }
 
 void List::move_focus(Navigation nav, bool select)
 {
-       if(nav==NAV_UP)
+       if(nav==NAV_UP && view_mode==GRID)
+       {
+               unsigned row = item_index_to_row(focus_index);
+               if(row>0)
+                       set_focus_index(rows[row-1].first+focus_index-rows[row].first);
+               else
+                       set_focus_index(0);
+       }
+       else if(nav==NAV_DOWN && view_mode==GRID)
+       {
+               unsigned row = item_index_to_row(focus_index);
+               if(row+1<rows.size())
+                       set_focus_index(rows[row+1].first+focus_index-rows[row].first);
+               else
+                       set_focus_index(items.size()-1);
+       }
+       else if(nav==NAV_UP || (nav==NAV_LEFT && view_mode==GRID))
        {
                if(focus_index>0)
                        set_focus_index(focus_index-1);
        }
-       else if(nav==NAV_DOWN)
+       else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID))
        {
                if(static_cast<unsigned>(focus_index+1)<items.size())
                        set_focus_index(focus_index+1);
@@ -365,7 +381,7 @@ void List::item_autosize_changed(Item *item)
 {
        item->autosize();
        signal_autosize_changed.emit();
-       rebuild();
+       mark_rebuild();
 }
 
 void List::reposition_items(bool record_rows)
@@ -381,20 +397,25 @@ void List::reposition_items(bool record_rows)
 
        const Sides &margin = items_part->get_margin();
        unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
+       unsigned x = 0;
        unsigned y = 0;
        unsigned row_h = 0;
        for(unsigned i=0; i<items.size(); ++i)
        {
                const Geometry &igeom = items[i]->get_geometry();
 
-               if(y)
-                       y -= row_h;
-               if(record_rows && i>0)
+               if(view_mode!=GRID || (x>0 && x+igeom.w>view_w))
                {
-                       rows.back().height = row_h;
-                       rows.push_back(i);
+                       x = 0;
+                       if(y)
+                               y -= row_h;
+                       if(record_rows && i>0)
+                       {
+                               rows.back().height = row_h;
+                               rows.push_back(i);
+                       }
+                       row_h = 0;
                }
-               row_h = 0;
 
                if(first_row<rows.size() && i==rows[first_row].first)
                        y = geom.h-min(geom.h, margin.top);
@@ -404,7 +425,8 @@ void List::reposition_items(bool record_rows)
                else if(igeom.h+margin.bottom<=y)
                {
                        items[i]->set_visible(true);
-                       items[i]->set_geometry(Geometry(margin.left, y-igeom.h, view_w, igeom.h));
+                       unsigned iw = (view_mode==GRID ? igeom.w : view_w);
+                       items[i]->set_geometry(Geometry(margin.left+x, y-igeom.h, iw, igeom.h));
                }
                else
                {
@@ -413,6 +435,7 @@ void List::reposition_items(bool record_rows)
                        y = 0;
                }
 
+               x += igeom.w;
                row_h = max(row_h, igeom.h);
        }
 
@@ -461,6 +484,7 @@ void List::check_view_range()
                first_row = max_scroll;
 
        slider.set_range(0, max_scroll);
+       slider.set_page_size(rows.size()-max_scroll);
        slider.set_value(max_scroll-first_row);
 }
 
@@ -481,7 +505,7 @@ void List::slider_value_changed(double value)
        if(max_scroll>0 && !ignore_slider_change)
        {
                first_row = max_scroll-static_cast<unsigned>(value);
-               rebuild();
+               mark_rebuild();
        }
 }
 
@@ -531,8 +555,8 @@ 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;
+       for(Item *i: list.items)
+               delete i;
        list.items.clear();
        list.items_changed();
 
@@ -543,21 +567,26 @@ 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] = nullptr;
        list.items[i] = list.create_item(i);
        list.items_changed();
 }
 
 
+List::Item::Item()
+{
+       input_type = INPUT_NAVIGATION;
+}
+
 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
 {
        if(part.get_name()=="children")
        {
                const Sides &margin = part.get_margin();
-               for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+               for(const Child *c: children)
                {
                        Geometry cgeom;
-                       (*i)->widget->autosize(cgeom);
+                       c->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);
                }
@@ -573,8 +602,8 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
 {
        if(part.get_name()=="children")
        {
-               for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
-                       (*i)->widget->render(renderer);
+               for(const Child *c: children)
+                       c->widget->render(renderer);
        }
 }
 
@@ -600,12 +629,13 @@ void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
                widths.resize(children.size(), 0);
 
        unsigned n = 0;
-       for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
+       for(const Child *c: children)
        {
                Geometry cgeom;
-               (*i)->widget->autosize(cgeom);
+               c->widget->autosize(cgeom);
                // TODO invent a better way to specify spacings
                widths[n] = max(widths[n], cgeom.w+8);
+               ++n;
        }
 }
 
@@ -621,10 +651,10 @@ void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
        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)
+       for(const Child *c: children)
        {
-               (*i)->widget->set_position(x, margin.bottom);
-               x += widths[n];
+               c->widget->set_position(x, margin.bottom);
+               x += widths[n++];
        }
 }
 
@@ -633,14 +663,14 @@ 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();
+       for(const Child *c: children)
+               c->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))
+       for(Item *i: list->items)
+               if(i!=this)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
                                mci->check_widths(widths);
 
        vector<unsigned> self_widths(widths);
@@ -651,8 +681,8 @@ void List::MultiColumnItem::on_style_change()
 
        if(update_all)
        {
-               for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
-                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
+               for(Item *i: list->items)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
                                mci->set_widths(self_widths);
        }
 
@@ -671,7 +701,9 @@ List::Loader::Loader(List &l):
        DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
 {
        add("item", &Loader::item);
+       add("view_mode", &List::view_mode);
        add("view_size", &List::view_rows);
+       add("view_size", &List::view_rows, &List::view_columns);
 }
 
 void List::Loader::item(const string &v)
@@ -679,5 +711,17 @@ void List::Loader::item(const string &v)
        dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
 }
 
+
+void operator>>(const LexicalConverter &conv, List::ViewMode &vm)
+{
+       const string &str = conv.get();
+       if(str=="LIST")
+               vm = List::LIST;
+       else if(str=="GRID")
+               vm = List::GRID;
+       else
+               throw lexical_error(format("conversion of '%s' to List::ViewMode", str));
+}
+
 } // namespace GLtk
 } // namespace Msp