2017-01-14 06:45:47 -05:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2017-01-14 06:45:47 -05:00
|
|
|
|
|
|
|
#include <algorithm>
|
2017-01-14 07:56:26 -05:00
|
|
|
#include <vector>
|
2017-01-14 06:45:47 -05:00
|
|
|
|
|
|
|
#include "canvas/grid.h"
|
|
|
|
#include "canvas/rectangle.h"
|
|
|
|
|
|
|
|
using namespace ArdourCanvas;
|
2017-01-14 07:56:26 -05:00
|
|
|
using std::vector;
|
|
|
|
using std::max;
|
2017-01-15 07:11:25 -05:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2017-01-14 06:45:47 -05:00
|
|
|
|
|
|
|
Grid::Grid (Canvas* canvas)
|
|
|
|
: Item (canvas)
|
2017-01-30 11:11:22 -05:00
|
|
|
, row_spacing (0)
|
|
|
|
, col_spacing (0)
|
2017-01-14 06:45:47 -05:00
|
|
|
, top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
|
|
|
|
, top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
|
2017-01-30 10:31:35 -05:00
|
|
|
, homogenous (false)
|
2017-01-14 06:45:47 -05:00
|
|
|
{
|
2017-01-30 11:11:22 -05:00
|
|
|
bg = new Rectangle (this);
|
|
|
|
bg->set_outline (false);
|
|
|
|
bg->set_fill (false);
|
|
|
|
bg->hide ();
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Grid::Grid (Item* parent)
|
|
|
|
: Item (parent)
|
2017-01-30 11:11:22 -05:00
|
|
|
, row_spacing (0)
|
|
|
|
, col_spacing (0)
|
2017-01-14 06:45:47 -05:00
|
|
|
, top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
|
|
|
|
, top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
|
2017-01-30 10:31:35 -05:00
|
|
|
, homogenous (false)
|
2017-01-14 06:45:47 -05:00
|
|
|
{
|
2017-01-30 11:11:22 -05:00
|
|
|
bg = new Rectangle (this);
|
|
|
|
bg->set_outline (false);
|
|
|
|
bg->set_fill (false);
|
|
|
|
bg->hide ();
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Grid::Grid (Item* parent, Duple const & p)
|
|
|
|
: Item (parent, p)
|
2017-01-30 11:11:22 -05:00
|
|
|
, row_spacing (0)
|
|
|
|
, col_spacing (0)
|
2017-01-14 06:45:47 -05:00
|
|
|
, top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
|
|
|
|
, top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
|
2017-01-30 08:46:14 -05:00
|
|
|
, homogenous (true)
|
2017-01-14 06:45:47 -05:00
|
|
|
{
|
2017-01-30 11:11:22 -05:00
|
|
|
bg = new Rectangle (this);
|
|
|
|
bg->set_outline (false);
|
|
|
|
bg->set_fill (false);
|
|
|
|
bg->hide ();
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:31:35 -05:00
|
|
|
void
|
|
|
|
Grid::set_homogenous (bool yn)
|
|
|
|
{
|
|
|
|
homogenous = yn;
|
|
|
|
}
|
|
|
|
|
2017-01-14 06:45:47 -05:00
|
|
|
void
|
|
|
|
Grid::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
|
|
|
{
|
|
|
|
Item::render_children (area, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Grid::compute_bounding_box () const
|
|
|
|
{
|
2017-01-19 14:54:24 -05:00
|
|
|
_bounding_box = Rect();
|
2017-01-14 06:45:47 -05:00
|
|
|
|
|
|
|
if (_items.empty()) {
|
|
|
|
_bounding_box_dirty = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_child_bounding_boxes (!collapse_on_hide);
|
|
|
|
|
|
|
|
if (_bounding_box) {
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect r = _bounding_box;
|
2017-01-14 06:45:47 -05:00
|
|
|
|
2017-01-30 11:11:22 -05:00
|
|
|
_bounding_box = r.expand (outline_width() + top_margin + top_padding,
|
|
|
|
outline_width() + right_margin + right_padding,
|
|
|
|
outline_width() + bottom_margin + bottom_padding,
|
|
|
|
outline_width() + left_margin + left_padding);
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_bounding_box_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-30 11:11:22 -05:00
|
|
|
Grid::set_row_spacing (double s)
|
2017-01-14 06:45:47 -05:00
|
|
|
{
|
2017-01-30 11:11:22 -05:00
|
|
|
row_spacing = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Grid::set_col_spacing (double s)
|
|
|
|
{
|
|
|
|
col_spacing = s;
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Grid::set_padding (double t, double r, double b, double l)
|
|
|
|
{
|
|
|
|
double last = t;
|
|
|
|
|
|
|
|
top_padding = t;
|
|
|
|
|
|
|
|
if (r >= 0) {
|
|
|
|
last = r;
|
|
|
|
}
|
|
|
|
right_padding = last;
|
|
|
|
if (b >= 0) {
|
|
|
|
last = b;
|
|
|
|
}
|
|
|
|
bottom_padding = last;
|
|
|
|
if (l >= 0) {
|
|
|
|
last = l;
|
|
|
|
}
|
|
|
|
left_padding = last;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Grid::set_margin (double t, double r, double b, double l)
|
|
|
|
{
|
|
|
|
double last = t;
|
|
|
|
top_margin = t;
|
|
|
|
if (r >= 0) {
|
|
|
|
last = r;
|
|
|
|
}
|
|
|
|
right_margin = last;
|
|
|
|
if (b >= 0) {
|
|
|
|
last = b;
|
|
|
|
}
|
|
|
|
bottom_margin = last;
|
|
|
|
if (l >= 0) {
|
|
|
|
last = l;
|
|
|
|
}
|
|
|
|
left_margin = last;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-30 11:11:22 -05:00
|
|
|
Grid::reset_bg ()
|
2017-01-14 06:45:47 -05:00
|
|
|
{
|
|
|
|
if (_bounding_box_dirty) {
|
|
|
|
compute_bounding_box ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_bounding_box) {
|
2017-01-30 11:11:22 -05:00
|
|
|
bg->hide ();
|
2017-01-14 06:45:47 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect r (_bounding_box);
|
2017-01-14 06:45:47 -05:00
|
|
|
|
|
|
|
/* XXX need to shrink by margin */
|
|
|
|
|
2017-01-30 11:11:22 -05:00
|
|
|
bg->set (r);
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Grid::reposition_children ()
|
|
|
|
{
|
2017-01-14 07:56:26 -05:00
|
|
|
uint32_t max_row = 0;
|
|
|
|
uint32_t max_col = 0;
|
|
|
|
|
2017-01-14 08:15:48 -05:00
|
|
|
/* since we encourage dynamic and essentially random placement of
|
2017-02-01 14:35:52 -05:00
|
|
|
* children, begin by determining the maximum row and column extents given
|
2017-01-14 08:15:48 -05:00
|
|
|
* our current set of children and placements.
|
|
|
|
*/
|
|
|
|
|
2017-01-14 07:56:26 -05:00
|
|
|
for (CoordsByItem::const_iterator c = coords_by_item.begin(); c != coords_by_item.end(); ++c) {
|
2017-02-01 15:30:29 -05:00
|
|
|
if (collapse_on_hide && !c->second.item->visible()) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-01 14:35:52 -05:00
|
|
|
max_col = max (max_col, (uint32_t) (c->second.x + c->second.col_span));
|
|
|
|
max_row = max (max_row, (uint32_t) (c->second.y + c->second.row_span));
|
2017-01-14 07:56:26 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 08:15:48 -05:00
|
|
|
/* Now compute the width of the widest child for each column, and the
|
2017-02-01 15:30:29 -05:00
|
|
|
* height of the tallest child for each row. Store the results in
|
|
|
|
* row_dimens and col_dimens, making sure they are suitably sized first.
|
2017-01-14 08:15:48 -05:00
|
|
|
*/
|
|
|
|
|
2017-01-14 07:56:26 -05:00
|
|
|
vector<double> row_dimens;
|
|
|
|
vector<double> col_dimens;
|
|
|
|
|
2017-02-01 15:30:29 -05:00
|
|
|
row_dimens.assign (max_row + 1, 0);
|
|
|
|
col_dimens.assign (max_col + 1, 0);
|
2017-01-14 07:56:26 -05:00
|
|
|
|
2017-02-01 15:30:29 -05:00
|
|
|
Rect uniform_cell_size;
|
2017-01-18 18:23:29 -05:00
|
|
|
|
2017-01-30 08:46:14 -05:00
|
|
|
if (homogenous) {
|
|
|
|
for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
|
|
|
|
|
2017-02-01 15:30:29 -05:00
|
|
|
if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
|
2017-01-30 10:31:35 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-30 08:46:14 -05:00
|
|
|
Rect bb = (*i)->bounding_box();
|
2017-01-18 18:23:29 -05:00
|
|
|
|
2017-01-30 08:46:14 -05:00
|
|
|
if (!bb) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-01 15:30:29 -05:00
|
|
|
|
|
|
|
CoordsByItem::const_iterator c = coords_by_item.find (*i);
|
|
|
|
|
|
|
|
uniform_cell_size.x1 = max (uniform_cell_size.x1, (bb.width()/c->second.col_span));
|
|
|
|
uniform_cell_size.y1 = max (uniform_cell_size.y1, (bb.height()/c->second.row_span));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t n = 0; n < max_col; ++n) {
|
|
|
|
col_dimens[n] = uniform_cell_size.width();
|
2017-01-30 08:46:14 -05:00
|
|
|
}
|
2017-01-14 07:56:26 -05:00
|
|
|
|
2017-02-01 15:30:29 -05:00
|
|
|
for (uint32_t n = 0; n < max_row; ++n) {
|
|
|
|
row_dimens[n] = uniform_cell_size.height();
|
|
|
|
}
|
2017-01-30 08:46:14 -05:00
|
|
|
|
|
|
|
for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
|
2017-02-01 15:30:29 -05:00
|
|
|
|
|
|
|
if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
|
2017-01-30 11:11:22 -05:00
|
|
|
/* bg rect is not a normal child */
|
2017-01-30 08:46:14 -05:00
|
|
|
continue;
|
|
|
|
}
|
2017-02-01 15:30:29 -05:00
|
|
|
|
|
|
|
CoordsByItem::const_iterator c = coords_by_item.find (*i);
|
|
|
|
|
|
|
|
Rect r = uniform_cell_size;
|
|
|
|
r.x1 *= c->second.col_span;
|
|
|
|
r.y1 *= c->second.row_span;
|
|
|
|
|
|
|
|
(*i)->size_allocate (r);
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
2017-02-01 15:30:29 -05:00
|
|
|
|
2017-01-30 08:46:14 -05:00
|
|
|
} else {
|
|
|
|
for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
|
|
|
|
|
2017-02-01 15:30:29 -05:00
|
|
|
if (*i == bg || (collapse_on_hide && !(*i)->visible())) {
|
2017-01-30 11:11:22 -05:00
|
|
|
/* bg rect is not a normal child */
|
2017-01-30 08:46:14 -05:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-14 07:56:26 -05:00
|
|
|
|
2017-01-30 08:46:14 -05:00
|
|
|
Rect bb = (*i)->bounding_box();
|
2017-01-14 07:56:26 -05:00
|
|
|
|
2017-01-30 08:46:14 -05:00
|
|
|
if (!bb) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoordsByItem::const_iterator c = coords_by_item.find (*i);
|
|
|
|
|
2017-02-01 14:22:19 -05:00
|
|
|
const double per_col_width = bb.width() / c->second.col_span;
|
|
|
|
const double per_row_height = bb.height() / c->second.row_span;
|
2017-01-14 08:15:48 -05:00
|
|
|
|
2017-02-01 14:22:19 -05:00
|
|
|
/* set the width of each column spanned by this item
|
|
|
|
*/
|
2017-01-14 08:15:48 -05:00
|
|
|
|
2017-02-01 14:22:19 -05:00
|
|
|
for (int n = 0; n < (int) c->second.col_span; ++n) {
|
|
|
|
col_dimens[c->second.x + n] = max (col_dimens[c->second.x + n], per_col_width);
|
|
|
|
}
|
|
|
|
for (int n = 0; n < (int) c->second.row_span; ++n) {
|
|
|
|
row_dimens[c->second.y + n] = max (row_dimens[c->second.y + n], per_row_height);
|
|
|
|
}
|
2017-01-18 18:23:29 -05:00
|
|
|
}
|
2017-01-14 08:15:48 -05:00
|
|
|
}
|
|
|
|
|
2017-02-01 14:22:19 -05:00
|
|
|
/* now progressively sum the row and column widths, once we're done:
|
|
|
|
*
|
|
|
|
* col_dimens: transformed into the x coordinate of the left edge of each column.
|
|
|
|
*
|
|
|
|
* row_dimens: transformed into the y coordinate of the upper left of each row,
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-01-30 11:11:22 -05:00
|
|
|
double current_right_edge = left_margin + left_padding;
|
2017-01-14 08:15:48 -05:00
|
|
|
|
2017-01-18 18:23:29 -05:00
|
|
|
for (uint32_t n = 0; n < max_col; ++n) {
|
|
|
|
if (col_dimens[n]) {
|
|
|
|
/* a width was defined for this column */
|
|
|
|
const double w = col_dimens[n]; /* save width of this column */
|
|
|
|
col_dimens[n] = current_right_edge;
|
2017-01-30 11:11:22 -05:00
|
|
|
current_right_edge = current_right_edge + w + col_spacing;
|
2017-01-18 18:23:29 -05:00
|
|
|
}
|
2017-01-14 08:15:48 -05:00
|
|
|
}
|
|
|
|
|
2017-02-01 14:22:19 -05:00
|
|
|
double current_top_edge = top_margin + top_padding;
|
|
|
|
|
|
|
|
for (uint32_t n = 0; n < max_row; ++n) {
|
|
|
|
if (row_dimens[n]) {
|
|
|
|
/* height defined for this row */
|
|
|
|
const double h = row_dimens[n]; /* save height */
|
|
|
|
row_dimens[n] = current_top_edge;
|
|
|
|
current_top_edge = current_top_edge + h + row_spacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 08:15:48 -05:00
|
|
|
/* position each item at the upper left of its (row, col) coordinate,
|
|
|
|
* given the width of all rows or columns before it.
|
|
|
|
*/
|
|
|
|
|
2017-01-18 18:23:29 -05:00
|
|
|
for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
|
2017-01-14 07:56:26 -05:00
|
|
|
CoordsByItem::const_iterator c = coords_by_item.find (*i);
|
|
|
|
|
|
|
|
if (c == coords_by_item.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-01 15:30:29 -05:00
|
|
|
/* do this even for hidden items - it will be corrected when
|
|
|
|
* they become visible again.
|
|
|
|
*/
|
|
|
|
|
2017-01-14 08:15:48 -05:00
|
|
|
(*i)->set_position (Duple (col_dimens[c->second.x], row_dimens[c->second.y]));
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
2017-01-15 16:40:23 -05:00
|
|
|
_bounding_box_dirty = true;
|
2017-01-30 11:11:22 -05:00
|
|
|
reset_bg ();
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-30 12:41:11 -05:00
|
|
|
Grid::place (Item* i, double x, double y, double col_span, double row_span)
|
2017-01-14 06:45:47 -05:00
|
|
|
{
|
2017-02-01 14:22:19 -05:00
|
|
|
ChildInfo ci;
|
|
|
|
|
2017-01-14 07:56:26 -05:00
|
|
|
add (i);
|
2017-02-01 14:22:19 -05:00
|
|
|
|
2017-02-01 15:30:29 -05:00
|
|
|
ci.item = i;
|
2017-02-01 14:22:19 -05:00
|
|
|
ci.x = x;
|
|
|
|
ci.y = y;
|
2017-02-01 15:30:29 -05:00
|
|
|
ci.col_span = max (1.0, col_span);
|
|
|
|
ci.row_span = max (1.0, row_span);
|
2017-02-01 14:22:19 -05:00
|
|
|
|
|
|
|
coords_by_item.insert (std::make_pair (i, ci));
|
2017-01-15 07:11:25 -05:00
|
|
|
reposition_children ();
|
2017-01-14 06:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-06-10 17:24:52 -04:00
|
|
|
Grid::child_changed (bool bbox_changed)
|
2017-01-14 06:45:47 -05:00
|
|
|
{
|
|
|
|
/* catch visibility and size changes */
|
|
|
|
|
2020-06-10 17:24:52 -04:00
|
|
|
Item::child_changed (bbox_changed);
|
2017-01-14 06:45:47 -05:00
|
|
|
reposition_children ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Grid::set_collapse_on_hide (bool yn)
|
|
|
|
{
|
|
|
|
if (collapse_on_hide != yn) {
|
|
|
|
collapse_on_hide = yn;
|
|
|
|
reposition_children ();
|
|
|
|
}
|
|
|
|
}
|