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->canvas_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->canvas_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->canvas_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->canvas_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, bool rounded) const
165 Item const * i = this;
169 offset = offset.translate (i->scroll_offset());
173 return _canvas->canvas_to_window (d.translate (offset), rounded);
177 Item::window_to_item (ArdourCanvas::Duple const & d) const
179 return canvas_to_item (_canvas->window_to_canvas (d));
183 Item::item_to_window (ArdourCanvas::Rect const & r) const
185 return _canvas->canvas_to_window (item_to_canvas (r));
189 Item::window_to_item (ArdourCanvas::Rect const & r) const
191 return canvas_to_item (_canvas->window_to_canvas (r));
194 /** Set the position of this item in the parent's coordinates */
196 Item::set_position (Duple p)
198 if (p == _position) {
202 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
203 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
206 /* see the comment in Canvas::item_moved() to understand
207 * why we use the parent's bounding box here.
209 pre_change_parent_bounding_box = item_to_parent (bbox.get());
214 _canvas->item_moved (this, pre_change_parent_bounding_box);
217 _parent->child_changed ();
222 Item::set_x_position (Coord x)
224 set_position (Duple (x, _position.y));
228 Item::set_y_position (Coord y)
230 set_position (Duple (_position.x, y));
234 Item::raise_to_top ()
237 _parent->raise_child_to_top (this);
241 Item::raise (int levels)
244 _parent->raise_child (this, levels);
248 Item::lower_to_bottom ()
251 _parent->lower_child_to_bottom (this);
259 _canvas->item_shown_or_hidden (this);
268 _canvas->item_shown_or_hidden (this);
273 Item::item_to_parent (Duple const & d) const
275 return d.translate (_position);
279 Item::parent_to_item (Duple const & d) const
281 return d.translate (- _position);
285 Item::parent_to_item (ArdourCanvas::Rect const & d) const
287 return d.translate (- _position);
297 Item::reparent (Group* new_parent)
299 assert (_canvas == _parent->canvas());
302 _parent->remove (this);
307 _parent = new_parent;
308 _canvas = _parent->canvas ();
313 Item::common_ancestor_within (uint32_t limit, const Item& other) const
315 uint32_t d1 = depth();
316 uint32_t d2 = other.depth();
317 const Item* i1 = this;
318 const Item* i2 = &other;
320 /* move towards root until we are at the same level
339 /* now see if there is a common parent */
359 Item::closest_ancestor_with (const Item& other) const
361 uint32_t d1 = depth();
362 uint32_t d2 = other.depth();
363 const Item* i1 = this;
364 const Item* i2 = &other;
366 /* move towards root until we are at the same level
380 /* now see if there is a common parent */
395 Item::is_descendant_of (const Item& candidate) const
397 Item const * i = _parent;
400 if (i == &candidate) {
415 /** @return Bounding box in this item's coordinates */
416 boost::optional<ArdourCanvas::Rect>
417 Item::bounding_box () const
419 if (_bounding_box_dirty) {
420 compute_bounding_box ();
421 assert (!_bounding_box_dirty);
424 return _bounding_box;
428 Item::height () const
430 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
433 return bb->height ();
441 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
451 Item::redraw () const
453 if (_visible && _bounding_box && _canvas) {
454 _canvas->request_redraw (item_to_canvas (_bounding_box.get()));
459 Item::begin_change ()
461 _pre_change_bounding_box = bounding_box ();
467 _canvas->item_changed (this, _pre_change_bounding_box);
470 _parent->child_changed ();
475 Item::begin_visual_change ()
480 Item::end_visual_change ()
482 _canvas->item_visual_property_changed (this);
486 Item::move (Duple movement)
488 set_position (position() + movement);
495 _canvas->grab (this);
506 Item::set_data (string const & key, void* data)
512 Item::get_data (string const & key) const
514 map<string, void*>::const_iterator i = _data.find (key);
515 if (i == _data.end ()) {
523 Item::set_ignore_events (bool ignore)
525 _ignore_events = ignore;
529 Item::dump (ostream& o) const
531 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
533 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
534 o << " @ " << position();
543 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
544 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
553 Item::whatami () const
555 std::string type = demangle (typeid (*this).name());
556 return type.substr (type.find_last_of (':') + 1);
572 Item::covers (Duple const & point) const
574 Duple p = canvas_to_item (point);
576 if (_bounding_box_dirty) {
577 compute_bounding_box ();
580 boost::optional<Rect> r = bounding_box();
586 return r.get().contains (p);
590 ArdourCanvas::operator<< (ostream& o, const Item& i)