323cdbf1ad9ee2bba89ad1cb4bf999bddae7ad23
[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         , spacing (0)
34         , top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
35         , top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
36         , homogenous (false)
37 {
38         self = new Rectangle (this);
39         self->set_outline (false);
40         self->set_fill (false);
41 }
42
43 Grid::Grid (Item* parent)
44         : Item (parent)
45         , spacing (0)
46         , top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
47         , top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
48         , homogenous (false)
49 {
50         self = new Rectangle (this);
51         self->set_outline (false);
52         self->set_fill (false);
53 }
54
55 Grid::Grid (Item* parent, Duple const & p)
56         : Item (parent, p)
57         , spacing (0)
58         , top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
59         , top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
60         , homogenous (true)
61 {
62         self = new Rectangle (this);
63         self->set_outline (false);
64         self->set_fill (false);
65 }
66
67 void
68 Grid::set_homogenous (bool yn)
69 {
70         homogenous = yn;
71 }
72
73 void
74 Grid::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
75 {
76         Item::render_children (area, context);
77 }
78
79 void
80 Grid::compute_bounding_box () const
81 {
82         _bounding_box = Rect();
83
84         if (_items.empty()) {
85                 _bounding_box_dirty = false;
86                 return;
87         }
88
89         add_child_bounding_boxes (!collapse_on_hide);
90
91         if (_bounding_box) {
92                 Rect r = _bounding_box;
93
94                 _bounding_box = r.expand (outline_width() + top_margin,
95                                           outline_width() + right_margin,
96                                           outline_width() + bottom_margin,
97                                           outline_width() + left_margin);
98         }
99
100         _bounding_box_dirty = false;
101 }
102
103 void
104 Grid::set_spacing (double s)
105 {
106         spacing = s;
107 }
108
109 void
110 Grid::set_padding (double t, double r, double b, double l)
111 {
112         double last = t;
113
114         top_padding = t;
115
116         if (r >= 0) {
117                 last = r;
118         }
119         right_padding = last;
120         if (b >= 0) {
121                 last = b;
122         }
123         bottom_padding = last;
124         if (l >= 0) {
125                 last = l;
126         }
127         left_padding = last;
128 }
129
130 void
131 Grid::set_margin (double t, double r, double b, double l)
132 {
133         double last = t;
134         top_margin = t;
135         if (r >= 0) {
136                 last = r;
137         }
138         right_margin = last;
139         if (b >= 0) {
140                 last = b;
141         }
142         bottom_margin = last;
143         if (l >= 0) {
144                 last = l;
145         }
146         left_margin = last;
147 }
148
149 void
150 Grid::reset_self ()
151 {
152         if (_bounding_box_dirty) {
153                 compute_bounding_box ();
154         }
155
156         if (!_bounding_box) {
157                 self->hide ();
158                 return;
159         }
160
161         Rect r (_bounding_box);
162
163         /* XXX need to shrink by margin */
164
165         self->set (r);
166 }
167
168 void
169 Grid::reposition_children ()
170 {
171         uint32_t max_row = 0;
172         uint32_t max_col = 0;
173
174         /* since we encourage dynamic and essentially random placement of
175          * children, begin by determining the maximum row and column given
176          * our current set of children and placements.
177          */
178
179         for (CoordsByItem::const_iterator c = coords_by_item.begin(); c != coords_by_item.end(); ++c) {
180                 max_col = max (max_col, (uint32_t) c->second.x);
181                 max_row = max (max_row, (uint32_t) c->second.y);
182         }
183
184         max_row++;
185         max_col++;
186
187         /* Now compute the width of the widest child for each column, and the
188          * height of the tallest child for each row.
189          */
190
191         vector<double> row_dimens;
192         vector<double> col_dimens;
193
194         row_dimens.assign (max_row, 0);
195         col_dimens.assign (max_col, 0);
196
197         Rect uniform_size;
198
199         if (homogenous) {
200                 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
201
202                         if (*i == self) {
203                                 continue;
204                         }
205
206                         Rect bb = (*i)->bounding_box();
207
208                         if (!bb) {
209                                 continue;
210                         }
211                         cerr << "\tbb for " << (*i)->whatami() << " is " << bb << endl;
212                         uniform_size.y1 = max (uniform_size.y1, bb.height());
213                         uniform_size.x1 = max (uniform_size.x1, bb.width());
214                 }
215
216                 cerr << "Uniform size will be " << uniform_size << endl;
217
218                 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
219                         if (*i == self) {
220                                 /* self-rect is not a normal child */
221                                 continue;
222                         }
223                         (*i)->size_allocate (uniform_size);
224                         for (uint32_t n = 0; n < max_col; ++n) {
225                                 col_dimens[n] = uniform_size.width();
226                         }
227                         for (uint32_t n = 0; n < max_row; ++n) {
228                                 row_dimens[n] = uniform_size.height();
229                         }
230                 }
231         } else {
232                 for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
233
234                         if (*i == self) {
235                                 /* self-rect is not a normal child */
236                                 continue;
237                         }
238
239                         Rect bb = (*i)->bounding_box();
240
241                         if (!bb) {
242                                 continue;
243                         }
244
245                         CoordsByItem::const_iterator c = coords_by_item.find (*i);
246
247                         row_dimens[c->second.y] = max (row_dimens[c->second.y], bb.height());
248                         col_dimens[c->second.x] = max (col_dimens[c->second.x]  , bb.width());
249                 }
250         }
251
252         /* now sum the row and column widths, so that row_dimens is transformed
253          * into the y coordinate of the upper left of each row, and col_dimens
254          * is transformed into the x coordinate of the left edge of each
255          * column.
256          */
257
258         double current_top_edge = top_margin;
259
260         for (uint32_t n = 0; n < max_row; ++n) {
261                 if (row_dimens[n]) {
262                         /* height defined for this row */
263                         const double h = row_dimens[n]; /* save height */
264                         row_dimens[n] = current_top_edge;
265                         cerr << "row[" << n << "] @ " << row_dimens[n] << endl;
266                         current_top_edge = current_top_edge + h + top_padding + bottom_padding;
267                 }
268         }
269
270         double current_right_edge = left_margin;
271
272         for (uint32_t n = 0; n < max_col; ++n) {
273                 if (col_dimens[n]) {
274                         /* a width was defined for this column */
275                         const double w = col_dimens[n]; /* save width of this column */
276                         col_dimens[n] = current_right_edge;
277                         cerr << "col[" << n << "] @ " << col_dimens[n] << endl;
278                         current_right_edge = current_right_edge + w + left_padding + right_padding;
279                 }
280         }
281
282         /* position each item at the upper left of its (row, col) coordinate,
283          * given the width of all rows or columns before it.
284          */
285
286         for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
287                 CoordsByItem::const_iterator c = coords_by_item.find (*i);
288
289                 if (c == coords_by_item.end()) {
290                         continue;
291                 }
292
293                 (*i)->set_position (Duple (col_dimens[c->second.x], row_dimens[c->second.y]));
294                 cerr << "place " << (*i)->whatami() << " @ " << c->second.x << ", " << c->second.y << " at "
295                      << Duple (col_dimens[c->second.x], row_dimens[c->second.y])
296                      << endl;
297         }
298
299         _bounding_box_dirty = true;
300         reset_self ();
301 }
302
303 void
304 Grid::place (Item* i, Duple at)
305 {
306         add (i);
307         coords_by_item.insert (std::make_pair (i, at));
308         reposition_children ();
309 }
310
311 void
312 Grid::child_changed ()
313 {
314         /* catch visibility and size changes */
315
316         Item::child_changed ();
317         reposition_children ();
318 }
319
320 void
321 Grid::set_collapse_on_hide (bool yn)
322 {
323         if (collapse_on_hide != yn) {
324                 collapse_on_hide = yn;
325                 reposition_children ();
326         }
327 }