]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Implement mouse wheel scrolling in List
[libs/gltk.git] / source / list.cpp
index 87b27a4dc641988b5438b9799500264da7d4fbd6..f61d7c26dec8f3ba194058050a4411f423e6e646 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/raii.h>
 #include <msp/debug/demangle.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/meshbuilder.h>
@@ -39,6 +40,7 @@ void List::init()
        first = 0;
        max_scroll = 0;
        view_size = 5;
+       ignore_slider_change = false;
 
        observer = new DataObserver(*this);
 
@@ -116,13 +118,16 @@ 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));
-       item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
+       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;
 }
 
@@ -163,6 +168,9 @@ void List::rebuild_special(const Part &part)
                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);
@@ -189,8 +197,6 @@ void List::rebuild_special(const Part &part)
                                }
                        }
                }
-
-               check_view_range();
        }
 
        Widget::rebuild_special(part);
@@ -209,15 +215,32 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const
 
 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])
-                       {
-                               set_selected_index(i);
-                               break;
-                       }
+               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)
+               {
+                       for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+                               if(click_focus==items[i])
+                               {
+                                       set_selected_index(i);
+                                       break;
+                               }
+               }
        }
 }
 
@@ -260,7 +283,7 @@ void List::check_view_range()
 
 void List::slider_value_changed(double value)
 {
-       if(max_scroll>0)
+       if(max_scroll>0 && !ignore_slider_change)
        {
                first = max_scroll-static_cast<unsigned>(value);
                rebuild();
@@ -311,6 +334,8 @@ void List::DataObserver::cleared()
 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();
 }
@@ -407,8 +432,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);
 }