From: Mikko Rasa Date: Fri, 15 Nov 2013 21:23:28 +0000 (+0200) Subject: Add a list item class to manage multiple columns X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f88706e34dbcc9238e63f52200ff535304c9cbe7;p=libs%2Fgltk.git Add a list item class to manage multiple columns --- diff --git a/source/list.cpp b/source/list.cpp index a4550c4..7be9363 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -374,6 +374,72 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const } +void List::MultiColumnItem::check_widths(vector &widths) const +{ + if(widths.size()::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 &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::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::const_iterator i=children.begin(); i!=children.end(); ++i) + (*i)->widget->autosize(); + + vector widths; + List *list = static_cast(parent); + for(vector::const_iterator i=list->items.begin(); i!=list->items.end(); ++i) + if(*i!=this) + if(MultiColumnItem *mci = dynamic_cast(*i)) + mci->check_widths(widths); + + vector self_widths(widths); + check_widths(self_widths); + bool update_all = false; + for(unsigned i=0; (!update_all && iwidths[i]; + + if(update_all) + { + for(vector::const_iterator i=list->items.begin(); i!=list->items.end(); ++i) + if(MultiColumnItem *mci = dynamic_cast(*i)) + mci->set_widths(self_widths); + } + else + set_widths(widths); +} + + List::BasicItem::BasicItem(const string &text): label(text) { diff --git a/source/list.h b/source/list.h index 0761caf..05ba852 100644 --- a/source/list.h +++ b/source/list.h @@ -68,6 +68,16 @@ public: virtual void render_special(const Part &, GL::Renderer &) const; }; + class MultiColumnItem: public Item + { + protected: + virtual void check_widths(std::vector &) const; + virtual void set_widths(const std::vector &); + + private: + virtual void on_style_change(); + }; + private: class BasicItem: public Item {