+#include <msp/core/raii.h>
#include <msp/debug/demangle.h>
#include <msp/gl/matrix.h>
#include <msp/gl/meshbuilder.h>
first = 0;
max_scroll = 0;
view_size = 5;
+ ignore_slider_change = false;
observer = new DataObserver(*this);
unsigned total_h = 0;
for(unsigned i=0; i<items.size(); ++i)
{
- const Geometry &igeom = items[i]->get_geometry();
+ Geometry igeom;
+ items[i]->autosize(igeom);
max_w = max(max_w, igeom.w);
if(view_size==0 || i<view_size)
total_h += igeom.h;
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)
void List::items_changed()
{
- check_view_range();
signal_autosize_changed.emit();
- reposition_items();
+ rebuild();
}
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));
}
}
-void List::render_special(const Part &part, GL::Renderer &renderer) const
-{
- if(part.get_name()=="items")
- {
- 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);
-}
-
-void List::button_press(int x, int y, unsigned btn)
-{
- 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::on_geometry_change()
-{
- reposition_slider();
- reposition_items();
-
- check_view_range();
-}
-
-void List::on_style_change()
-{
- if(!style)
- return;
-
- reposition_slider();
- reposition_items();
-
- check_view_range();
-}
-
-void List::reposition_slider()
+void List::rebuild_special(const Part &part)
{
- if(!style)
- return;
-
- if(const Part *slider_part = style->get_part("slider"))
+ if(part.get_name()=="slider")
+ reposition_child(slider, part);
+ else if(part.get_name()=="items")
{
- Geometry sgeom = slider_part->get_geometry();
- slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
- slider.set_geometry(sgeom);
- }
-}
+ SetFlag flag(ignore_slider_change);
+ check_view_range();
-void List::item_autosize_changed(Item *item)
-{
- signal_autosize_changed.emit();
- item->autosize();
- reposition_items();
-}
-
-void List::reposition_items()
-{
- if(!style)
- return;
-
- if(const Part *items_part = style->get_part("items"))
- {
- const Sides &margin = items_part->get_margin();
- unsigned w = geom.w-margin.left-margin.right;
- unsigned y = geom.h-margin.top;
+ 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)
}
}
}
+
+ Widget::rebuild_special(part);
+}
+
+void List::render_special(const Part &part, GL::Renderer &renderer) const
+{
+ if(part.get_name()=="items")
+ {
+ 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);
+}
+
+void List::button_press(int x, int y, unsigned btn)
+{
+ if(btn==4 || btn==5)
+ {
+ 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;
+ }
+ }
+ }
+}
+
+void List::item_autosize_changed(Item *item)
+{
+ item->autosize();
+ signal_autosize_changed.emit();
+ rebuild();
}
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);
- reposition_items();
+ rebuild();
}
}
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();
}
const Sides &margin = part.get_margin();
for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
{
- const Geometry &cgeom = (*i)->widget->get_geometry();
+ 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::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)
+ {
+ 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;
+
+ 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)
+ {
+ (*i)->widget->set_position(x, margin.bottom);
+ x += widths[n];
+ }
+}
+
+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)
+ {
+ 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);
+}
+
+
List::BasicItem::BasicItem(const string &text):
label(text)
{