]> 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 5493947c566b025df46b6aebbe26f865f3432f3f..e0d2bd81c150d62918bedd77b7eebe3b1537baac 100644 (file)
@@ -9,7 +9,6 @@
 #include "part.h"
 #include "style.h"
 #include "text.h"
-#include "vslider.h"
 
 using namespace std;
 
@@ -22,39 +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;
-       view_mode = LIST;
-       sel_index = -1;
-       focus_index = -1;
-       first_row = 0;
-       max_scroll = 0;
-       view_rows = 5;
-       view_columns = 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));
@@ -120,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)
@@ -141,7 +118,7 @@ void List::items_changed()
 
 List::Item *List::create_item(unsigned index)
 {
-       Item *item = 0;
+       Item *item = nullptr;
        if(item_factory)
                item = item_factory->create_item(index);
        else
@@ -185,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
@@ -353,7 +330,7 @@ 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)
@@ -507,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);
 }
 
@@ -577,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();
 
@@ -589,7 +567,7 @@ 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();
 }
@@ -605,10 +583,10 @@ 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);
                }
@@ -624,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);
        }
 }
 
@@ -651,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;
        }
 }
 
@@ -672,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++];
        }
 }
 
@@ -684,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);
@@ -702,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);
        }