diff --git a/libs/canvas/canvas/grid.h b/libs/canvas/canvas/grid.h index a13cdf0a22..435a4e5eb1 100644 --- a/libs/canvas/canvas/grid.h +++ b/libs/canvas/canvas/grid.h @@ -19,6 +19,8 @@ #ifndef __CANVAS_GRID_H__ #define __CANVAS_GRID_H__ +#include + #include "canvas/item.h" namespace ArdourCanvas @@ -58,6 +60,9 @@ public: void child_changed (); private: + typedef std::map CoordsByItem; + CoordsByItem coords_by_item; + Rectangle *self; bool collapse_on_hide; bool homogenous; diff --git a/libs/canvas/grid.cc b/libs/canvas/grid.cc index cc12feb8db..de1602f33a 100644 --- a/libs/canvas/grid.cc +++ b/libs/canvas/grid.cc @@ -17,11 +17,14 @@ */ #include +#include #include "canvas/grid.h" #include "canvas/rectangle.h" using namespace ArdourCanvas; +using std::vector; +using std::max; Grid::Grid (Canvas* canvas) : Item (canvas) @@ -157,23 +160,48 @@ Grid::reset_self () void Grid::reposition_children () { - Duple previous_edge (0, 0); - Distance largest_width = 0; - Distance largest_height = 0; + uint32_t max_row = 0; + uint32_t max_col = 0; - if (homogenous) { + for (CoordsByItem::const_iterator c = coords_by_item.begin(); c != coords_by_item.end(); ++c) { + max_col = max (max_col, (uint32_t) c->second.x); + max_row = max (max_row, (uint32_t) c->second.y); + } - for (std::list::iterator i = _items.begin(); ++i != _items.end(); ++i) { - boost::optional bb = (*i)->bounding_box(); - if (bb) { - largest_height = std::max (largest_height, bb.get().height()); - largest_width = std::max (largest_width, bb.get().width()); - } + vector row_dimens; + vector col_dimens; + + row_dimens.assign (0, max_row); + col_dimens.assign (0, max_col); + + for (std::list::iterator i = _items.begin(); ++i != _items.end(); ++i) { + boost::optional bb = (*i)->bounding_box(); + + if (!bb) { + continue; } + + CoordsByItem::const_iterator c = coords_by_item.find (*i); + + row_dimens[c->second.x] = max (row_dimens[c->second.x], bb.get().width()); + col_dimens[c->second.y] = max (col_dimens[c->second.y], bb.get().height()); } for (std::list::iterator i = _items.begin(); ++i != _items.end(); ++i) { + CoordsByItem::const_iterator c = coords_by_item.find (*i); + if (c == coords_by_item.end()) { + continue; + } + + Duple pos (0,0); + + for (uint32_t n = 0; n < c->second.x; ++n) { + pos.x += row_dimens[n]; + pos.y += col_dimens[n]; + } + + (*i)->set_position (pos); } _bounding_box_dirty = true; @@ -183,7 +211,8 @@ Grid::reposition_children () void Grid::place (Item* i, Duple at) { - + add (i); + coords_by_item.insert (std::make_pair (i, at)); } void