2 Copyright (C) 2018 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "canvas/grid.h"
23 #include "canvas/rectangle.h"
25 using namespace ArdourCanvas;
31 Grid::Grid (Canvas* canvas)
35 , top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
36 , top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
39 bg = new Rectangle (this);
40 bg->set_outline (false);
45 Grid::Grid (Item* parent)
49 , top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
50 , top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
53 bg = new Rectangle (this);
54 bg->set_outline (false);
59 Grid::Grid (Item* parent, Duple const & p)
63 , top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
64 , top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
67 bg = new Rectangle (this);
68 bg->set_outline (false);
74 Grid::set_homogenous (bool yn)
80 Grid::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
82 Item::render_children (area, context);
86 Grid::compute_bounding_box () const
88 _bounding_box = Rect();
91 _bounding_box_dirty = false;
95 add_child_bounding_boxes (!collapse_on_hide);
98 Rect r = _bounding_box;
100 _bounding_box = r.expand (outline_width() + top_margin + top_padding,
101 outline_width() + right_margin + right_padding,
102 outline_width() + bottom_margin + bottom_padding,
103 outline_width() + left_margin + left_padding);
106 _bounding_box_dirty = false;
110 Grid::set_row_spacing (double s)
116 Grid::set_col_spacing (double s)
122 Grid::set_padding (double t, double r, double b, double l)
131 right_padding = last;
135 bottom_padding = last;
143 Grid::set_margin (double t, double r, double b, double l)
154 bottom_margin = last;
164 if (_bounding_box_dirty) {
165 compute_bounding_box ();
168 if (!_bounding_box) {
173 Rect r (_bounding_box);
175 /* XXX need to shrink by margin */
181 Grid::reposition_children ()
183 uint32_t max_row = 0;
184 uint32_t max_col = 0;
186 /* since we encourage dynamic and essentially random placement of
187 * children, begin by determining the maximum row and column extents given
188 * our current set of children and placements.
191 for (CoordsByItem::const_iterator c = coords_by_item.begin(); c != coords_by_item.end(); ++c) {
192 max_col = max (max_col, (uint32_t) (c->second.x + c->second.col_span));
193 max_row = max (max_row, (uint32_t) (c->second.y + c->second.row_span));
199 /* Now compute the width of the widest child for each column, and the
200 * height of the tallest child for each row.
203 vector<double> row_dimens;
204 vector<double> col_dimens;
206 row_dimens.assign (max_row, 0);
207 col_dimens.assign (max_col, 0);
212 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
218 Rect bb = (*i)->bounding_box();
223 cerr << "\tbb for " << (*i)->whatami() << " is " << bb << endl;
224 uniform_size.y1 = max (uniform_size.y1, bb.height());
225 uniform_size.x1 = max (uniform_size.x1, bb.width());
228 cerr << "Uniform size will be " << uniform_size << endl;
230 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
232 /* bg rect is not a normal child */
235 (*i)->size_allocate (uniform_size);
236 for (uint32_t n = 0; n < max_col; ++n) {
237 col_dimens[n] = uniform_size.width();
239 for (uint32_t n = 0; n < max_row; ++n) {
240 row_dimens[n] = uniform_size.height();
244 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
247 /* bg rect is not a normal child */
251 Rect bb = (*i)->bounding_box();
257 CoordsByItem::const_iterator c = coords_by_item.find (*i);
259 const double per_col_width = bb.width() / c->second.col_span;
260 const double per_row_height = bb.height() / c->second.row_span;
262 /* set the width of each column spanned by this item
265 for (int n = 0; n < (int) c->second.col_span; ++n) {
266 col_dimens[c->second.x + n] = max (col_dimens[c->second.x + n], per_col_width);
268 for (int n = 0; n < (int) c->second.row_span; ++n) {
269 row_dimens[c->second.y + n] = max (row_dimens[c->second.y + n], per_row_height);
274 /* now progressively sum the row and column widths, once we're done:
276 * col_dimens: transformed into the x coordinate of the left edge of each column.
278 * row_dimens: transformed into the y coordinate of the upper left of each row,
282 double current_right_edge = left_margin + left_padding;
284 for (uint32_t n = 0; n < max_col; ++n) {
286 /* a width was defined for this column */
287 const double w = col_dimens[n]; /* save width of this column */
288 col_dimens[n] = current_right_edge;
289 cerr << "col[" << n << "] @ " << col_dimens[n] << endl;
290 current_right_edge = current_right_edge + w + col_spacing;
294 double current_top_edge = top_margin + top_padding;
296 for (uint32_t n = 0; n < max_row; ++n) {
298 /* height defined for this row */
299 const double h = row_dimens[n]; /* save height */
300 row_dimens[n] = current_top_edge;
301 cerr << "row[" << n << "] @ " << row_dimens[n] << endl;
302 current_top_edge = current_top_edge + h + row_spacing;
306 /* position each item at the upper left of its (row, col) coordinate,
307 * given the width of all rows or columns before it.
310 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
311 CoordsByItem::const_iterator c = coords_by_item.find (*i);
313 if (c == coords_by_item.end()) {
317 (*i)->set_position (Duple (col_dimens[c->second.x], row_dimens[c->second.y]));
318 cerr << "place " << (*i)->whatami() << " @ " << c->second.x << ", " << c->second.y << " at "
319 << Duple (col_dimens[c->second.x], row_dimens[c->second.y])
323 _bounding_box_dirty = true;
328 Grid::place (Item* i, double x, double y, double col_span, double row_span)
336 ci.col_span = col_span;
337 ci.row_span = row_span;
339 coords_by_item.insert (std::make_pair (i, ci));
340 reposition_children ();
344 Grid::child_changed ()
346 /* catch visibility and size changes */
348 Item::child_changed ();
349 reposition_children ();
353 Grid::set_collapse_on_hide (bool yn)
355 if (collapse_on_hide != yn) {
356 collapse_on_hide = yn;
357 reposition_children ();