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 if (collapse_on_hide && !c->second.item->visible()) {
195 max_col = max (max_col, (uint32_t) (c->second.x + c->second.col_span));
196 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. Store the results in
201 * row_dimens and col_dimens, making sure they are suitably sized first.
204 vector<double> row_dimens;
205 vector<double> col_dimens;
207 row_dimens.assign (max_row + 1, 0);
208 col_dimens.assign (max_col + 1, 0);
210 Rect uniform_cell_size;
213 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
215 if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
219 Rect bb = (*i)->bounding_box();
225 CoordsByItem::const_iterator c = coords_by_item.find (*i);
227 uniform_cell_size.x1 = max (uniform_cell_size.x1, (bb.width()/c->second.col_span));
228 uniform_cell_size.y1 = max (uniform_cell_size.y1, (bb.height()/c->second.row_span));
231 for (uint32_t n = 0; n < max_col; ++n) {
232 col_dimens[n] = uniform_cell_size.width();
235 for (uint32_t n = 0; n < max_row; ++n) {
236 row_dimens[n] = uniform_cell_size.height();
239 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
241 if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
242 /* bg rect is not a normal child */
246 CoordsByItem::const_iterator c = coords_by_item.find (*i);
248 Rect r = uniform_cell_size;
249 r.x1 *= c->second.col_span;
250 r.y1 *= c->second.row_span;
252 (*i)->size_allocate (r);
256 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
258 if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
259 /* bg rect is not a normal child */
263 Rect bb = (*i)->bounding_box();
269 CoordsByItem::const_iterator c = coords_by_item.find (*i);
271 const double per_col_width = bb.width() / c->second.col_span;
272 const double per_row_height = bb.height() / c->second.row_span;
274 /* set the width of each column spanned by this item
277 for (int n = 0; n < (int) c->second.col_span; ++n) {
278 col_dimens[c->second.x + n] = max (col_dimens[c->second.x + n], per_col_width);
280 for (int n = 0; n < (int) c->second.row_span; ++n) {
281 row_dimens[c->second.y + n] = max (row_dimens[c->second.y + n], per_row_height);
286 /* now progressively sum the row and column widths, once we're done:
288 * col_dimens: transformed into the x coordinate of the left edge of each column.
290 * row_dimens: transformed into the y coordinate of the upper left of each row,
294 double current_right_edge = left_margin + left_padding;
296 for (uint32_t n = 0; n < max_col; ++n) {
298 /* a width was defined for this column */
299 const double w = col_dimens[n]; /* save width of this column */
300 col_dimens[n] = current_right_edge;
301 current_right_edge = current_right_edge + w + col_spacing;
305 double current_top_edge = top_margin + top_padding;
307 for (uint32_t n = 0; n < max_row; ++n) {
309 /* height defined for this row */
310 const double h = row_dimens[n]; /* save height */
311 row_dimens[n] = current_top_edge;
312 current_top_edge = current_top_edge + h + row_spacing;
316 /* position each item at the upper left of its (row, col) coordinate,
317 * given the width of all rows or columns before it.
320 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
321 CoordsByItem::const_iterator c = coords_by_item.find (*i);
323 if (c == coords_by_item.end()) {
327 /* do this even for hidden items - it will be corrected when
328 * they become visible again.
331 (*i)->set_position (Duple (col_dimens[c->second.x], row_dimens[c->second.y]));
334 _bounding_box_dirty = true;
339 Grid::place (Item* i, double x, double y, double col_span, double row_span)
348 ci.col_span = max (1.0, col_span);
349 ci.row_span = max (1.0, row_span);
351 coords_by_item.insert (std::make_pair (i, ci));
352 reposition_children ();
356 Grid::child_changed ()
358 /* catch visibility and size changes */
360 Item::child_changed ();
361 reposition_children ();
365 Grid::set_collapse_on_hide (bool yn)
367 if (collapse_on_hide != yn) {
368 collapse_on_hide = yn;
369 reposition_children ();