]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add a list item class to manage multiple columns
authorMikko Rasa <tdb@tdb.fi>
Fri, 15 Nov 2013 21:23:28 +0000 (23:23 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 15 Nov 2013 21:23:28 +0000 (23:23 +0200)
source/list.cpp
source/list.h

index a4550c4ee9ad220dfd22336657894b9cca13de9a..7be93632a86447347e63e012c01f7bbca847348d 100644 (file)
@@ -374,6 +374,72 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
 }
 
 
+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);
+       }
+       else
+               set_widths(widths);
+}
+
+
 List::BasicItem::BasicItem(const string &text):
        label(text)
 {
index 0761caf63ed93858d20fd1cf7aabfea7888dfbb3..05ba852949281698a6e4d32fd21706766a42c853 100644 (file)
@@ -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<unsigned> &) const;
+               virtual void set_widths(const std::vector<unsigned> &);
+
+       private:
+               virtual void on_style_change();
+       };
+
 private:
        class BasicItem: public Item
        {