get grid layout working
[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 (false)
61 {
62         self = new Rectangle (this);
63         self->set_outline (false);
64         self->set_fill (false);
65 }
66
67 void
68 Grid::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
69 {
70         Item::render_children (area, context);
71 }
72
73 void
74 Grid::compute_bounding_box () const
75 {
76         _bounding_box = boost::none;
77
78         if (_items.empty()) {
79                 _bounding_box_dirty = false;
80                 return;
81         }
82
83         add_child_bounding_boxes (!collapse_on_hide);
84
85         if (_bounding_box) {
86                 Rect r = _bounding_box.get();
87
88                 _bounding_box = r.expand (top_padding + outline_width() + top_margin,
89                                           right_padding + outline_width() + right_margin,
90                                           bottom_padding + outline_width() + bottom_margin,
91                                           left_padding + outline_width() + left_margin);
92         }
93
94         _bounding_box_dirty = false;
95 }
96
97 void
98 Grid::set_spacing (double s)
99 {
100         spacing = s;
101 }
102
103 void
104 Grid::set_padding (double t, double r, double b, double l)
105 {
106         double last = t;
107
108         top_padding = t;
109
110         if (r >= 0) {
111                 last = r;
112         }
113         right_padding = last;
114         if (b >= 0) {
115                 last = b;
116         }
117         bottom_padding = last;
118         if (l >= 0) {
119                 last = l;
120         }
121         left_padding = last;
122 }
123
124 void
125 Grid::set_margin (double t, double r, double b, double l)
126 {
127         double last = t;
128         top_margin = t;
129         if (r >= 0) {
130                 last = r;
131         }
132         right_margin = last;
133         if (b >= 0) {
134                 last = b;
135         }
136         bottom_margin = last;
137         if (l >= 0) {
138                 last = l;
139         }
140         left_margin = last;
141 }
142
143 void
144 Grid::reset_self ()
145 {
146         if (_bounding_box_dirty) {
147                 compute_bounding_box ();
148         }
149
150         if (!_bounding_box) {
151                 self->hide ();
152                 return;
153         }
154
155         Rect r (_bounding_box.get());
156
157         /* XXX need to shrink by margin */
158
159         self->set (r);
160 }
161
162 void
163 Grid::reposition_children ()
164 {
165         uint32_t max_row = 0;
166         uint32_t max_col = 0;
167
168         /* since we encourage dynamic and essentially random placement of
169          * children, begin by determining the maximum row and column given
170          * our current set of children and placements.
171          */
172
173         for (CoordsByItem::const_iterator c = coords_by_item.begin(); c != coords_by_item.end(); ++c) {
174                 max_col = max (max_col, (uint32_t) c->second.x);
175                 max_row = max (max_row, (uint32_t) c->second.y);
176         }
177
178         cerr << "max row = " << max_row << " max_col " << max_col << endl;
179         cerr << "with items = " << _items.size() << " coords " << coords_by_item.size()<< endl;
180
181         max_row++;
182         max_col++;
183
184         /* Now compute the width of the widest child for each column, and the
185          * height of the tallest child for each row.
186          */
187
188         vector<double> row_dimens;
189         vector<double> col_dimens;
190
191         row_dimens.assign (max_row, 0);
192         col_dimens.assign (max_col, 0);
193
194         for (std::list<Item*>::iterator i = _items.begin(); ++i != _items.end(); ++i) {
195                 boost::optional<Rect> bb = (*i)->bounding_box();
196
197                 if (!bb) {
198                         cerr << "no bounding box\n";
199                         continue;
200                 }
201
202                 CoordsByItem::const_iterator c = coords_by_item.find (*i);
203
204                 cerr << "item BB = " << bb.get() << endl;
205
206                 row_dimens[c->second.y] = max (row_dimens[c->second.y], bb.get().height());
207                 col_dimens[c->second.x] = max (col_dimens[c->second.x]  , bb.get().width());
208         }
209
210         /* now sum the row and column widths, so that row_dimens is transformed
211          * into the y coordinate of the upper left of each row, and col_dimens
212          * is transformed into the x coordinate of the left edge of each
213          * column.
214          */
215
216         double prev = row_dimens[0];
217         row_dimens[0] = 0;
218
219         for (uint32_t n = 1; n < max_row; ++n) {
220                 row_dimens[n] = row_dimens[n-1] + prev;
221                 cerr << "B: row[" << n << "] @ " << row_dimens[n] << endl;
222                 prev = row_dimens[n];
223         }
224
225         prev = col_dimens[0];
226         col_dimens[0] = 0;
227
228         for (uint32_t n = 1; n < max_col; ++n) {
229                 col_dimens[n] = col_dimens[n-1] + prev;
230                 cerr << "B: col[" << n << "] @ " << col_dimens[n] << endl;
231                 prev = col_dimens[n];
232         }
233
234         /* position each item at the upper left of its (row, col) coordinate,
235          * given the width of all rows or columns before it.
236          */
237
238         for (std::list<Item*>::iterator i = _items.begin(); ++i != _items.end(); ++i) {
239                 CoordsByItem::const_iterator c = coords_by_item.find (*i);
240
241                 if (c == coords_by_item.end()) {
242                         cerr << "item not found\n";
243                         continue;
244                 }
245
246                 cerr << " place item at " << Duple (col_dimens[c->second.x], row_dimens[c->second.y]) << endl;
247                 (*i)->set_position (Duple (col_dimens[c->second.x], row_dimens[c->second.y]));
248         }
249
250         _bounding_box_dirty = true;
251
252
253
254
255         reset_self ();
256 }
257
258 void
259 Grid::place (Item* i, Duple at)
260 {
261         add (i);
262         coords_by_item.insert (std::make_pair (i, at));
263         reposition_children ();
264 }
265
266 void
267 Grid::child_changed ()
268 {
269         cerr << "Child changed!\n";
270         /* catch visibility and size changes */
271
272         Item::child_changed ();
273         reposition_children ();
274 }
275
276 void
277 Grid::set_collapse_on_hide (bool yn)
278 {
279         if (collapse_on_hide != yn) {
280                 collapse_on_hide = yn;
281                 reposition_children ();
282         }
283 }