e5d05e9f05ef872c070dd46dd11d78d29305e3f5
[ardour.git] / libs / canvas / grid.cc
1 /*
2     Copyright (C) 2018 Paul Davis
3
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.
8
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.
13
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.
17 */
18
19 #include <algorithm>
20 #include <vector>
21
22 #include "canvas/grid.h"
23 #include "canvas/rectangle.h"
24
25 using namespace ArdourCanvas;
26 using std::vector;
27 using std::max;
28 using std::cerr;
29 using std::endl;
30
31 Grid::Grid (Canvas* canvas)
32         : Item (canvas)
33         , row_spacing (0)
34         , col_spacing (0)
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)
37         , homogenous (false)
38 {
39         bg = new Rectangle (this);
40         bg->set_outline (false);
41         bg->set_fill (false);
42         bg->hide ();
43 }
44
45 Grid::Grid (Item* parent)
46         : Item (parent)
47         , row_spacing (0)
48         , col_spacing (0)
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)
51         , homogenous (false)
52 {
53         bg = new Rectangle (this);
54         bg->set_outline (false);
55         bg->set_fill (false);
56         bg->hide ();
57 }
58
59 Grid::Grid (Item* parent, Duple const & p)
60         : Item (parent, p)
61         , row_spacing (0)
62         , col_spacing (0)
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)
65         , homogenous (true)
66 {
67         bg = new Rectangle (this);
68         bg->set_outline (false);
69         bg->set_fill (false);
70         bg->hide ();
71 }
72
73 void
74 Grid::set_homogenous (bool yn)
75 {
76         homogenous = yn;
77 }
78
79 void
80 Grid::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
81 {
82         Item::render_children (area, context);
83 }
84
85 void
86 Grid::compute_bounding_box () const
87 {
88         _bounding_box = Rect();
89
90         if (_items.empty()) {
91                 _bounding_box_dirty = false;
92                 return;
93         }
94
95         add_child_bounding_boxes (!collapse_on_hide);
96
97         if (_bounding_box) {
98                 Rect r = _bounding_box;
99
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);
104         }
105
106         _bounding_box_dirty = false;
107 }
108
109 void
110 Grid::set_row_spacing (double s)
111 {
112         row_spacing = s;
113 }
114
115 void
116 Grid::set_col_spacing (double s)
117 {
118         col_spacing = s;
119 }
120
121 void
122 Grid::set_padding (double t, double r, double b, double l)
123 {
124         double last = t;
125
126         top_padding = t;
127
128         if (r >= 0) {
129                 last = r;
130         }
131         right_padding = last;
132         if (b >= 0) {
133                 last = b;
134         }
135         bottom_padding = last;
136         if (l >= 0) {
137                 last = l;
138         }
139         left_padding = last;
140 }
141
142 void
143 Grid::set_margin (double t, double r, double b, double l)
144 {
145         double last = t;
146         top_margin = t;
147         if (r >= 0) {
148                 last = r;
149         }
150         right_margin = last;
151         if (b >= 0) {
152                 last = b;
153         }
154         bottom_margin = last;
155         if (l >= 0) {
156                 last = l;
157         }
158         left_margin = last;
159 }
160
161 void
162 Grid::reset_bg ()
163 {
164         if (_bounding_box_dirty) {
165                 compute_bounding_box ();
166         }
167
168         if (!_bounding_box) {
169                 bg->hide ();
170                 return;
171         }
172
173         Rect r (_bounding_box);
174
175         /* XXX need to shrink by margin */
176
177         bg->set (r);
178 }
179
180 void
181 Grid::reposition_children ()
182 {
183         uint32_t max_row = 0;
184         uint32_t max_col = 0;
185
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.
189          */
190
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));
194         }
195
196         max_row++;
197         max_col++;
198
199         /* Now compute the width of the widest child for each column, and the
200          * height of the tallest child for each row.
201          */
202
203         vector<double> row_dimens;
204         vector<double> col_dimens;
205
206         row_dimens.assign (max_row, 0);
207         col_dimens.assign (max_col, 0);
208
209         Rect uniform_size;
210
211         if (homogenous) {
212                 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
213
214                         if (*i == bg) {
215                                 continue;
216                         }
217
218                         Rect bb = (*i)->bounding_box();
219
220                         if (!bb) {
221                                 continue;
222                         }
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());
226                 }
227
228                 cerr << "Uniform size will be " << uniform_size << endl;
229
230                 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
231                         if (*i == bg) {
232                                 /* bg rect is not a normal child */
233                                 continue;
234                         }
235                         (*i)->size_allocate (uniform_size);
236                         for (uint32_t n = 0; n < max_col; ++n) {
237                                 col_dimens[n] = uniform_size.width();
238                         }
239                         for (uint32_t n = 0; n < max_row; ++n) {
240                                 row_dimens[n] = uniform_size.height();
241                         }
242                 }
243         } else {
244                 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
245
246                         if (*i == bg) {
247                                 /* bg rect is not a normal child */
248                                 continue;
249                         }
250
251                         Rect bb = (*i)->bounding_box();
252
253                         if (!bb) {
254                                 continue;
255                         }
256
257                         CoordsByItem::const_iterator c = coords_by_item.find (*i);
258
259                         const double per_col_width = bb.width() / c->second.col_span;
260                         const double per_row_height = bb.height() / c->second.row_span;
261
262                         /* set the width of each column spanned by this item
263                          */
264
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);
267                         }
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);
270                         }
271                 }
272         }
273
274         /* now progressively sum the row and column widths, once we're done:
275          *
276          * col_dimens: transformed into the x coordinate of the left edge of each column.
277          *
278          * row_dimens: transformed into the y coordinate of the upper left of each row,
279          *
280          */
281
282         double current_right_edge = left_margin + left_padding;
283
284         for (uint32_t n = 0; n < max_col; ++n) {
285                 if (col_dimens[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;
291                 }
292         }
293
294         double current_top_edge = top_margin + top_padding;
295
296         for (uint32_t n = 0; n < max_row; ++n) {
297                 if (row_dimens[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;
303                 }
304         }
305
306         /* position each item at the upper left of its (row, col) coordinate,
307          * given the width of all rows or columns before it.
308          */
309
310         for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
311                 CoordsByItem::const_iterator c = coords_by_item.find (*i);
312
313                 if (c == coords_by_item.end()) {
314                         continue;
315                 }
316
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])
320                      << endl;
321         }
322
323         _bounding_box_dirty = true;
324         reset_bg ();
325 }
326
327 void
328 Grid::place (Item* i, double x, double y, double col_span, double row_span)
329 {
330         ChildInfo ci;
331
332         add (i);
333
334         ci.x = x;
335         ci.y = y;
336         ci.col_span = col_span;
337         ci.row_span = row_span;
338
339         coords_by_item.insert (std::make_pair (i, ci));
340         reposition_children ();
341 }
342
343 void
344 Grid::child_changed ()
345 {
346         /* catch visibility and size changes */
347
348         Item::child_changed ();
349         reposition_children ();
350 }
351
352 void
353 Grid::set_collapse_on_hide (bool yn)
354 {
355         if (collapse_on_hide != yn) {
356                 collapse_on_hide = yn;
357                 reposition_children ();
358         }
359 }