2013-04-15 22:10:18 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011-2013 Paul Davis
|
|
|
|
Author: Carl Hetherington <cth@carlh.net>
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <cairomm/context.h>
|
2013-04-09 14:22:58 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "pbd/stacktrace.h"
|
2013-04-09 14:22:58 -04:00
|
|
|
#include "pbd/compose.h"
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "canvas/group.h"
|
|
|
|
#include "canvas/types.h"
|
|
|
|
#include "canvas/debug.h"
|
2013-04-05 11:27:26 -04:00
|
|
|
#include "canvas/item.h"
|
|
|
|
#include "canvas/canvas.h"
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
|
|
|
int Group::default_items_per_cell = 64;
|
|
|
|
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
Group::Group (Canvas* canvas)
|
|
|
|
: Item (canvas)
|
|
|
|
, _lut (0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Group::Group (Group* parent)
|
|
|
|
: Item (parent)
|
|
|
|
, _lut (0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Group::Group (Group* parent, Duple position)
|
|
|
|
: Item (parent, position)
|
|
|
|
, _lut (0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Group::~Group ()
|
|
|
|
{
|
2014-02-12 15:15:27 -05:00
|
|
|
clear_items (true);
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2014-05-22 23:05:18 -04:00
|
|
|
/** @param area Area to draw in window coordinates.
|
2013-04-04 00:32:52 -04:00
|
|
|
* @param context Context, set up with its origin at this group's position.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Group::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
|
|
|
{
|
|
|
|
ensure_lut ();
|
|
|
|
vector<Item*> items = _lut->get (area);
|
|
|
|
|
2013-04-09 14:22:58 -04:00
|
|
|
#ifdef CANVAS_DEBUG
|
|
|
|
if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
|
2014-01-10 11:02:34 -05:00
|
|
|
cerr << string_compose ("%1GROUP %2 @ %7 render %5 @ %6 %3 items out of %4\n",
|
|
|
|
_canvas->render_indent(), (name.empty() ? string ("[unnamed]") : name), items.size(), _items.size(), area, _position, this);
|
2013-04-09 14:22:58 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-30 23:36:30 -04:00
|
|
|
++render_depth;
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
for (vector<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
if (!(*i)->visible ()) {
|
2013-04-09 14:22:58 -04:00
|
|
|
#ifdef CANVAS_DEBUG
|
|
|
|
if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
|
2013-10-24 17:14:12 -04:00
|
|
|
cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] invisible - skipped\n";
|
2013-04-09 14:22:58 -04:00
|
|
|
}
|
|
|
|
#endif
|
2013-04-04 00:32:52 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<Rect> item_bbox = (*i)->bounding_box ();
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
if (!item_bbox) {
|
2013-04-09 14:22:58 -04:00
|
|
|
#ifdef CANVAS_DEBUG
|
|
|
|
if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
|
2013-11-03 10:07:00 -05:00
|
|
|
cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] empty - skipped\n";
|
2013-04-09 14:22:58 -04:00
|
|
|
}
|
|
|
|
#endif
|
2013-04-04 00:32:52 -04:00
|
|
|
continue;
|
|
|
|
}
|
2013-10-24 17:14:12 -04:00
|
|
|
|
2013-06-18 08:23:06 -04:00
|
|
|
Rect item = (*i)->item_to_window (item_bbox.get());
|
2013-10-31 11:49:36 -04:00
|
|
|
boost::optional<Rect> d = item.intersection (area);
|
2013-06-18 08:23:06 -04:00
|
|
|
|
2013-10-31 11:49:36 -04:00
|
|
|
if (d) {
|
|
|
|
Rect draw = d.get();
|
|
|
|
if (draw.width() && draw.height()) {
|
2013-04-09 14:22:58 -04:00
|
|
|
#ifdef CANVAS_DEBUG
|
2013-10-31 11:49:36 -04:00
|
|
|
if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
|
|
|
|
if (dynamic_cast<Group*>(*i) == 0) {
|
|
|
|
cerr << _canvas->render_indent() << "render "
|
|
|
|
<< ' '
|
2014-01-10 11:02:34 -05:00
|
|
|
<< (*i)
|
|
|
|
<< ' '
|
2013-10-31 11:49:36 -04:00
|
|
|
<< (*i)->whatami()
|
|
|
|
<< ' '
|
|
|
|
<< (*i)->name
|
2014-05-20 23:08:15 -04:00
|
|
|
<< " item "
|
|
|
|
<< item_bbox.get()
|
|
|
|
<< " window = "
|
2013-10-31 11:49:36 -04:00
|
|
|
<< item
|
|
|
|
<< " intersect = "
|
|
|
|
<< draw
|
2014-01-10 11:02:34 -05:00
|
|
|
<< " @ "
|
|
|
|
<< _position
|
2013-10-31 11:49:36 -04:00
|
|
|
<< endl;
|
|
|
|
}
|
2013-10-30 23:36:30 -04:00
|
|
|
}
|
2013-04-09 14:22:58 -04:00
|
|
|
#endif
|
2013-06-18 13:46:24 -04:00
|
|
|
|
2013-10-31 11:49:36 -04:00
|
|
|
(*i)->render (area, context);
|
|
|
|
++render_count;
|
|
|
|
}
|
2013-06-18 13:46:24 -04:00
|
|
|
|
2013-04-09 14:22:58 -04:00
|
|
|
} else {
|
2013-10-31 11:49:36 -04:00
|
|
|
|
2013-04-09 14:22:58 -04:00
|
|
|
#ifdef CANVAS_DEBUG
|
|
|
|
if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
|
2014-05-20 23:08:15 -04:00
|
|
|
cerr << string_compose ("%1skip render of %2 %3, no intersection between %4 and %5\n", _canvas->render_indent(), (*i)->whatami(),
|
|
|
|
(*i)->name, item, area);
|
2013-04-09 14:22:58 -04:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-31 11:49:36 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
2013-04-09 14:22:58 -04:00
|
|
|
|
|
|
|
--render_depth;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2014-05-18 12:22:23 -04:00
|
|
|
void
|
|
|
|
Group::scroll_to (Duple const& d)
|
|
|
|
{
|
|
|
|
Item::scroll_to (d);
|
|
|
|
|
2014-05-20 23:08:15 -04:00
|
|
|
for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
|
2014-05-18 12:22:23 -04:00
|
|
|
(*i)->scroll_to (d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
void
|
|
|
|
Group::compute_bounding_box () const
|
|
|
|
{
|
|
|
|
Rect bbox;
|
|
|
|
bool have_one = false;
|
|
|
|
|
|
|
|
for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
|
2013-10-28 12:26:40 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
boost::optional<Rect> item_bbox = (*i)->bounding_box ();
|
2013-10-28 12:26:40 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
if (!item_bbox) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect group_bbox = (*i)->item_to_parent (item_bbox.get ());
|
|
|
|
if (have_one) {
|
|
|
|
bbox = bbox.extend (group_bbox);
|
|
|
|
} else {
|
|
|
|
bbox = group_bbox;
|
|
|
|
have_one = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_one) {
|
|
|
|
_bounding_box = boost::optional<Rect> ();
|
|
|
|
} else {
|
|
|
|
_bounding_box = bbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
_bounding_box_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::add (Item* i)
|
|
|
|
{
|
2013-04-21 15:03:50 -04:00
|
|
|
/* XXX should really notify canvas about this */
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
_items.push_back (i);
|
|
|
|
invalidate_lut ();
|
|
|
|
_bounding_box_dirty = true;
|
2013-04-24 18:31:00 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::remove (Item* i)
|
|
|
|
{
|
2013-04-21 15:03:50 -04:00
|
|
|
|
|
|
|
if (i->parent() != this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-12 15:15:27 -05:00
|
|
|
/* we cannot call bounding_box() here because that will iterate over
|
|
|
|
_items, one of which (the argument, i) may be in the middle of
|
|
|
|
deletion, making it impossible to call compute_bounding_box()
|
|
|
|
on it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (_bounding_box) {
|
|
|
|
_pre_change_bounding_box = _bounding_box;
|
|
|
|
} else {
|
|
|
|
_pre_change_bounding_box = Rect();
|
|
|
|
}
|
2013-04-21 15:03:50 -04:00
|
|
|
|
|
|
|
i->unparent ();
|
2013-04-04 00:32:52 -04:00
|
|
|
_items.remove (i);
|
|
|
|
invalidate_lut ();
|
|
|
|
_bounding_box_dirty = true;
|
|
|
|
|
2013-04-21 15:03:50 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::clear (bool with_delete)
|
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
|
2014-02-12 15:15:27 -05:00
|
|
|
clear_items (with_delete);
|
2013-04-21 15:03:50 -04:00
|
|
|
|
|
|
|
invalidate_lut ();
|
|
|
|
_bounding_box_dirty = true;
|
|
|
|
|
|
|
|
end_change ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 15:15:27 -05:00
|
|
|
void
|
|
|
|
Group::clear_items (bool with_delete)
|
|
|
|
{
|
|
|
|
for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ) {
|
|
|
|
|
|
|
|
list<Item*>::iterator tmp = i;
|
|
|
|
Item *item = *i;
|
|
|
|
|
|
|
|
++tmp;
|
|
|
|
|
|
|
|
/* remove from list before doing anything else, because we
|
|
|
|
* don't want to find the item in _items during any activity
|
|
|
|
* driven by unparent-ing or deletion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
_items.erase (i);
|
|
|
|
item->unparent ();
|
|
|
|
|
|
|
|
if (with_delete) {
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
void
|
|
|
|
Group::raise_child_to_top (Item* i)
|
|
|
|
{
|
2014-01-02 16:49:42 -05:00
|
|
|
if (!_items.empty()) {
|
2014-01-10 11:54:05 -05:00
|
|
|
if (_items.back() == i) {
|
2014-01-02 16:49:42 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
_items.remove (i);
|
|
|
|
_items.push_back (i);
|
|
|
|
invalidate_lut ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::raise_child (Item* i, int levels)
|
|
|
|
{
|
|
|
|
list<Item*>::iterator j = find (_items.begin(), _items.end(), i);
|
|
|
|
assert (j != _items.end ());
|
|
|
|
|
|
|
|
++j;
|
|
|
|
_items.remove (i);
|
|
|
|
|
|
|
|
while (levels > 0 && j != _items.end ()) {
|
|
|
|
++j;
|
|
|
|
--levels;
|
|
|
|
}
|
|
|
|
|
|
|
|
_items.insert (j, i);
|
|
|
|
invalidate_lut ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::lower_child_to_bottom (Item* i)
|
|
|
|
{
|
2014-01-02 16:49:42 -05:00
|
|
|
if (!_items.empty()) {
|
2014-01-10 11:54:05 -05:00
|
|
|
if (_items.front() == i) {
|
2014-01-02 16:49:42 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
_items.remove (i);
|
|
|
|
_items.push_front (i);
|
|
|
|
invalidate_lut ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::ensure_lut () const
|
|
|
|
{
|
|
|
|
if (!_lut) {
|
|
|
|
_lut = new DumbLookupTable (*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::invalidate_lut () const
|
|
|
|
{
|
|
|
|
delete _lut;
|
|
|
|
_lut = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::child_changed ()
|
|
|
|
{
|
|
|
|
invalidate_lut ();
|
|
|
|
_bounding_box_dirty = true;
|
|
|
|
|
|
|
|
if (_parent) {
|
|
|
|
_parent->child_changed ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Group::add_items_at_point (Duple const point, vector<Item const *>& items) const
|
|
|
|
{
|
|
|
|
boost::optional<Rect> const bbox = bounding_box ();
|
2013-04-04 18:45:27 -04:00
|
|
|
|
2014-05-23 22:05:08 -04:00
|
|
|
/* Point is in window coordinate system */
|
2013-12-12 10:03:33 -05:00
|
|
|
|
2014-05-23 22:05:08 -04:00
|
|
|
if (!bbox || !item_to_window (bbox.get()).contains (point)) {
|
2013-04-04 00:32:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-28 12:26:40 -04:00
|
|
|
/* now recurse and add any items within our group that contain point */
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
ensure_lut ();
|
|
|
|
vector<Item*> our_items = _lut->items_at_point (point);
|
2013-10-28 12:26:40 -04:00
|
|
|
|
2013-12-12 10:03:33 -05:00
|
|
|
if (!our_items.empty()) {
|
|
|
|
/* this adds this group itself to the list of items at point */
|
|
|
|
Item::add_items_at_point (point, items);
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
for (vector<Item*>::iterator i = our_items.begin(); i != our_items.end(); ++i) {
|
2013-11-03 10:07:00 -05:00
|
|
|
(*i)->add_items_at_point (point, items);
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 11:27:26 -04:00
|
|
|
void
|
|
|
|
Group::dump (ostream& o) const
|
|
|
|
{
|
2013-09-25 10:38:04 -04:00
|
|
|
#ifdef CANVAS_DEBUG
|
2013-04-05 11:27:26 -04:00
|
|
|
o << _canvas->indent();
|
2013-04-11 20:19:22 -04:00
|
|
|
o << "Group " << this << " [" << name << ']';
|
2013-04-15 10:38:12 -04:00
|
|
|
o << " @ " << position();
|
2013-04-05 11:27:26 -04:00
|
|
|
o << " Items: " << _items.size();
|
2013-04-10 14:42:36 -04:00
|
|
|
o << " Visible ? " << _visible;
|
2013-04-05 11:27:26 -04:00
|
|
|
|
|
|
|
boost::optional<Rect> bb = bounding_box();
|
|
|
|
|
|
|
|
if (bb) {
|
|
|
|
o << endl << _canvas->indent() << " bbox: " << bb.get();
|
2013-04-15 10:38:12 -04:00
|
|
|
o << endl << _canvas->indent() << " CANVAS bbox: " << item_to_canvas (bb.get());
|
2013-04-05 11:27:26 -04:00
|
|
|
} else {
|
|
|
|
o << " bbox unset";
|
|
|
|
}
|
|
|
|
|
|
|
|
o << endl;
|
2013-09-25 10:38:04 -04:00
|
|
|
#endif
|
2013-04-05 11:27:26 -04:00
|
|
|
|
|
|
|
ArdourCanvas::dump_depth++;
|
|
|
|
|
|
|
|
for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
|
|
|
|
o << **i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArdourCanvas::dump_depth--;
|
|
|
|
}
|