2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "pbd/compose.h"
21 #include "pbd/stacktrace.h"
22 #include "pbd/convert.h"
24 #include "ardour/utils.h"
26 #include "canvas/group.h"
27 #include "canvas/item.h"
28 #include "canvas/canvas.h"
29 #include "canvas/debug.h"
33 using namespace ArdourCanvas;
35 Item::Item (Canvas* canvas)
42 Item::Item (Group* parent)
43 : _canvas (parent->canvas ())
49 Item::Item (Group* parent, Duple position)
50 : _canvas (parent->canvas())
52 , _position (position)
61 _bounding_box_dirty = true;
62 _ignore_events = false;
68 DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
74 _parent->remove (this);
78 _canvas->item_going_away (this, _bounding_box);
83 Item::item_to_parent (ArdourCanvas::Rect const & r) const
85 return r.translate (_position);
89 Item::item_to_canvas (ArdourCanvas::Rect const & r) const
91 Item const * i = this;
95 offset = offset.translate (i->position());
99 return r.translate (offset);
103 Item::item_to_canvas (ArdourCanvas::Duple const & d) const
105 Item const * i = this;
109 offset = offset.translate (i->position());
113 return d.translate (offset);
117 Item::canvas_to_item (ArdourCanvas::Duple const & d) const
119 Item const * i = this;
123 offset = offset.translate (-(i->position()));
127 return d.translate (offset);
131 Item::canvas_to_item (ArdourCanvas::Rect const & d) const
133 Item const * i = this;
137 offset = offset.translate (-(i->position()));
141 return d.translate (offset);
145 Item::item_to_canvas (Coord& x, Coord& y) const
147 Duple d = item_to_canvas (Duple (x, y));
154 Item::canvas_to_item (Coord& x, Coord& y) const
156 Duple d = canvas_to_item (Duple (x, y));
163 Item::item_to_window (ArdourCanvas::Duple const & d) const
165 return _canvas->canvas_to_window (item_to_canvas (d));
169 Item::window_to_item (ArdourCanvas::Duple const & d) const
171 return _canvas->window_to_canvas (canvas_to_item (d));
175 Item::item_to_window (ArdourCanvas::Rect const & r) const
177 return _canvas->canvas_to_window (item_to_canvas (r));
180 /** Set the position of this item in the parent's coordinates */
182 Item::set_position (Duple p)
184 if (p == _position) {
188 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
189 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
192 /* see the comment in Canvas::item_moved() to understand
193 * why we use the parent's bounding box here.
195 pre_change_parent_bounding_box = item_to_parent (bbox.get());
200 _canvas->item_moved (this, pre_change_parent_bounding_box);
203 _parent->child_changed ();
208 Item::set_x_position (Coord x)
210 set_position (Duple (x, _position.y));
214 Item::set_y_position (Coord y)
216 set_position (Duple (_position.x, y));
220 Item::raise_to_top ()
223 _parent->raise_child_to_top (this);
227 Item::raise (int levels)
230 _parent->raise_child (this, levels);
234 Item::lower_to_bottom ()
237 _parent->lower_child_to_bottom (this);
245 _canvas->item_shown_or_hidden (this);
254 _canvas->item_shown_or_hidden (this);
259 Item::item_to_parent (Duple const & d) const
261 return d.translate (_position);
265 Item::parent_to_item (Duple const & d) const
267 return d.translate (- _position);
271 Item::parent_to_item (ArdourCanvas::Rect const & d) const
273 return d.translate (- _position);
283 Item::reparent (Group* new_parent)
285 assert (_canvas == _parent->canvas());
288 _parent->remove (this);
293 _parent = new_parent;
294 _canvas = _parent->canvas ();
299 Item::common_ancestor_within (uint32_t limit, const Item& other) const
301 uint32_t d1 = depth();
302 uint32_t d2 = other.depth();
303 const Item* i1 = this;
304 const Item* i2 = &other;
306 /* move towards root until we are at the same level
325 /* now see if there is a common parent */
345 Item::closest_ancestor_with (const Item& other) const
347 uint32_t d1 = depth();
348 uint32_t d2 = other.depth();
349 const Item* i1 = this;
350 const Item* i2 = &other;
352 /* move towards root until we are at the same level
366 /* now see if there is a common parent */
381 Item::is_descendant_of (const Item& candidate) const
383 Item const * i = _parent;
386 if (i == &candidate) {
401 /** @return Bounding box in this item's coordinates */
402 boost::optional<ArdourCanvas::Rect>
403 Item::bounding_box () const
405 if (_bounding_box_dirty) {
406 compute_bounding_box ();
409 assert (!_bounding_box_dirty);
410 return _bounding_box;
414 Item::height () const
416 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
419 return bb->height ();
427 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
437 Item::redraw () const
439 if (_visible && _bounding_box && _canvas) {
440 _canvas->request_redraw (item_to_canvas (_bounding_box.get()));
445 Item::begin_change ()
447 _pre_change_bounding_box = bounding_box ();
453 _canvas->item_changed (this, _pre_change_bounding_box);
456 _parent->child_changed ();
461 Item::begin_visual_change ()
466 Item::end_visual_change ()
468 _canvas->item_visual_property_changed (this);
472 Item::move (Duple movement)
474 set_position (position() + movement);
481 _canvas->grab (this);
492 Item::set_data (string const & key, void* data)
498 Item::get_data (string const & key) const
500 map<string, void*>::const_iterator i = _data.find (key);
501 if (i == _data.end ()) {
509 Item::set_ignore_events (bool ignore)
511 _ignore_events = ignore;
515 Item::dump (ostream& o) const
517 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
519 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
520 o << " @ " << position();
529 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
530 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
539 Item::whatami () const
541 std::string type = demangle (typeid (*this).name());
542 return type.substr (type.find_last_of (':') + 1);
558 Item::covers (Duple const & point) const
560 Duple p = canvas_to_item (point);
562 if (_bounding_box_dirty) {
563 compute_bounding_box ();
566 boost::optional<Rect> r = bounding_box();
572 return r.get().contains (p);
576 ArdourCanvas::operator<< (ostream& o, const Item& i)