1 /*
2  * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 
Grid(Canvas * canvas)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 
Grid(Item * parent)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 
Grid(Item * parent,Duple const & p)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
set_homogenous(bool yn)74 Grid::set_homogenous (bool yn)
75 {
76 	homogenous = yn;
77 }
78 
79 void
render(Rect const & area,Cairo::RefPtr<Cairo::Context> context) const80 Grid::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
81 {
82 	Item::render_children (area, context);
83 }
84 
85 void
compute_bounding_box() const86 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
set_row_spacing(double s)110 Grid::set_row_spacing (double s)
111 {
112 	row_spacing = s;
113 }
114 
115 void
set_col_spacing(double s)116 Grid::set_col_spacing (double s)
117 {
118 	col_spacing = s;
119 }
120 
121 void
set_padding(double t,double r,double b,double l)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
set_margin(double t,double r,double b,double l)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
reset_bg()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
reposition_children()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 		if (collapse_on_hide && !c->second.item->visible()) {
193 			continue;
194 		}
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));
197 	}
198 
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.
202 	 */
203 
204 	vector<double> row_dimens;
205 	vector<double> col_dimens;
206 
207 	row_dimens.assign (max_row + 1, 0);
208 	col_dimens.assign (max_col + 1, 0);
209 
210 	Rect uniform_cell_size;
211 
212 	if (homogenous) {
213 		for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
214 
215 			if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
216 				continue;
217 			}
218 
219 			Rect bb = (*i)->bounding_box();
220 
221 			if (!bb) {
222 				continue;
223 			}
224 
225 			CoordsByItem::const_iterator c = coords_by_item.find (*i);
226 
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));
229 		}
230 
231 		for (uint32_t n = 0; n < max_col; ++n) {
232 			col_dimens[n] = uniform_cell_size.width();
233 		}
234 
235 		for (uint32_t n = 0; n < max_row; ++n) {
236 			row_dimens[n] = uniform_cell_size.height();
237 		}
238 
239 		for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
240 
241 			if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
242 				/* bg rect is not a normal child */
243 				continue;
244 			}
245 
246 			CoordsByItem::const_iterator c = coords_by_item.find (*i);
247 
248 			Rect r = uniform_cell_size;
249 			r.x1 *= c->second.col_span;
250 			r.y1 *= c->second.row_span;
251 
252 			(*i)->size_allocate (r);
253 		}
254 
255 	} else {
256 		for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
257 
258 			if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
259 				/* bg rect is not a normal child */
260 				continue;
261 			}
262 
263 			Rect bb = (*i)->bounding_box();
264 
265 			if (!bb) {
266 				continue;
267 			}
268 
269 			CoordsByItem::const_iterator c = coords_by_item.find (*i);
270 
271 			const double per_col_width = bb.width() / c->second.col_span;
272 			const double per_row_height = bb.height() / c->second.row_span;
273 
274 			/* set the width of each column spanned by this item
275 			 */
276 
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);
279 			}
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);
282 			}
283 		}
284 	}
285 
286 	/* now progressively sum the row and column widths, once we're done:
287 	 *
288 	 * col_dimens: transformed into the x coordinate of the left edge of each column.
289 	 *
290 	 * row_dimens: transformed into the y coordinate of the upper left of each row,
291 	 *
292 	 */
293 
294 	double current_right_edge = left_margin + left_padding;
295 
296 	for (uint32_t n = 0; n < max_col; ++n) {
297 		if (col_dimens[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;
302 		}
303 	}
304 
305 	double current_top_edge = top_margin + top_padding;
306 
307 	for (uint32_t n = 0; n < max_row; ++n) {
308 		if (row_dimens[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;
313 		}
314 	}
315 
316 	/* position each item at the upper left of its (row, col) coordinate,
317 	 * given the width of all rows or columns before it.
318 	 */
319 
320 	for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
321 		CoordsByItem::const_iterator c = coords_by_item.find (*i);
322 
323 		if (c == coords_by_item.end()) {
324 			continue;
325 		}
326 
327 		/* do this even for hidden items - it will be corrected when
328 		 * they become visible again.
329 		 */
330 
331 		(*i)->set_position (Duple (col_dimens[c->second.x], row_dimens[c->second.y]));
332 	}
333 
334 	_bounding_box_dirty = true;
335 	reset_bg ();
336 }
337 
338 void
place(Item * i,double x,double y,double col_span,double row_span)339 Grid::place (Item* i, double x, double y, double col_span, double row_span)
340 {
341 	ChildInfo ci;
342 
343 	add (i);
344 
345 	ci.item = i;
346 	ci.x = x;
347 	ci.y = y;
348 	ci.col_span = max (1.0, col_span);
349 	ci.row_span = max (1.0, row_span);
350 
351 	coords_by_item.insert (std::make_pair (i, ci));
352 	reposition_children ();
353 }
354 
355 void
child_changed()356 Grid::child_changed ()
357 {
358 	/* catch visibility and size changes */
359 
360 	Item::child_changed ();
361 	reposition_children ();
362 }
363 
364 void
set_collapse_on_hide(bool yn)365 Grid::set_collapse_on_hide (bool yn)
366 {
367 	if (collapse_on_hide != yn) {
368 		collapse_on_hide = yn;
369 		reposition_children ();
370 	}
371 }
372