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/canvas.h"
27 #include "canvas/debug.h"
28 #include "canvas/group.h"
29 #include "canvas/item.h"
30 #include "canvas/scroll_group.h"
34 using namespace ArdourCanvas;
36 Item::Item (Canvas* canvas)
43 Item::Item (Group* parent)
44 : _canvas (parent->canvas ())
50 Item::Item (Group* parent, Duple position)
51 : _canvas (parent->canvas())
53 , _position (position)
62 _bounding_box_dirty = true;
63 _ignore_events = false;
69 find_scroll_parent ();
71 DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
77 _parent->remove (this);
81 _canvas->item_going_away (this, _bounding_box);
86 Item::item_to_parent (ArdourCanvas::Rect const & r) const
88 return r.translate (_position);
92 Item::scroll_offset () const
95 return _scroll_parent->scroll_offset();
97 return _canvas->scroll_offset();
101 Item::position_offset() const
103 Item const * i = this;
107 offset = offset.translate (i->position());
115 Item::item_to_canvas (ArdourCanvas::Rect const & r) const
117 return r.translate (position_offset());
121 Item::item_to_canvas (ArdourCanvas::Duple const & d) const
123 return d.translate (position_offset());
127 Item::canvas_to_item (ArdourCanvas::Duple const & r) const
129 return r.translate (-position_offset());
133 Item::canvas_to_item (ArdourCanvas::Rect const & r) const
135 return r.translate (-position_offset());
139 Item::item_to_canvas (Coord& x, Coord& y) const
141 Duple d = item_to_canvas (Duple (x, y));
148 Item::canvas_to_item (Coord& x, Coord& y) const
150 Duple d = canvas_to_item (Duple (x, y));
158 Item::item_to_window (ArdourCanvas::Duple const & d, bool rounded) const
160 Duple ret = item_to_canvas (d).translate (-scroll_offset());
162 ret.x = round (ret.x);
163 ret.y = round (ret.y);
169 Item::window_to_item (ArdourCanvas::Duple const & d) const
171 return canvas_to_item (d.translate (scroll_offset()));
175 Item::item_to_window (ArdourCanvas::Rect const & r) const
177 Rect ret = item_to_canvas (r).translate (-scroll_offset());
179 ret.x0 = round (ret.x0);
180 ret.x1 = round (ret.x1);
181 ret.y0 = round (ret.y0);
182 ret.y1 = round (ret.y1);
188 Item::window_to_item (ArdourCanvas::Rect const & r) const
190 return canvas_to_item (r.translate (scroll_offset()));
193 /** Set the position of this item in the parent's coordinates */
195 Item::set_position (Duple p)
197 if (p == _position) {
201 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
202 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
205 /* see the comment in Canvas::item_moved() to understand
206 * why we use the parent's bounding box here.
208 pre_change_parent_bounding_box = item_to_parent (bbox.get());
213 _canvas->item_moved (this, pre_change_parent_bounding_box);
216 _parent->child_changed ();
221 Item::set_x_position (Coord x)
223 set_position (Duple (x, _position.y));
227 Item::set_y_position (Coord y)
229 set_position (Duple (_position.x, y));
233 Item::raise_to_top ()
236 _parent->raise_child_to_top (this);
240 Item::raise (int levels)
243 _parent->raise_child (this, levels);
247 Item::lower_to_bottom ()
250 _parent->lower_child_to_bottom (this);
258 _canvas->item_shown_or_hidden (this);
267 _canvas->item_shown_or_hidden (this);
272 Item::item_to_parent (Duple const & d) const
274 return d.translate (_position);
278 Item::parent_to_item (Duple const & d) const
280 return d.translate (- _position);
284 Item::parent_to_item (ArdourCanvas::Rect const & d) const
286 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 ();
310 find_scroll_parent ();
316 Item::find_scroll_parent ()
318 Item const * i = this;
319 ScrollGroup const * last_scroll_group = 0;
322 ScrollGroup const * sg = dynamic_cast<ScrollGroup const *> (i);
324 last_scroll_group = sg;
329 _scroll_parent = const_cast<ScrollGroup*> (last_scroll_group);
333 Item::common_ancestor_within (uint32_t limit, const Item& other) const
335 uint32_t d1 = depth();
336 uint32_t d2 = other.depth();
337 const Item* i1 = this;
338 const Item* i2 = &other;
340 /* move towards root until we are at the same level
359 /* now see if there is a common parent */
379 Item::closest_ancestor_with (const Item& other) const
381 uint32_t d1 = depth();
382 uint32_t d2 = other.depth();
383 const Item* i1 = this;
384 const Item* i2 = &other;
386 /* move towards root until we are at the same level
400 /* now see if there is a common parent */
415 Item::is_descendant_of (const Item& candidate) const
417 Item const * i = _parent;
420 if (i == &candidate) {
435 /** @return Bounding box in this item's coordinates */
436 boost::optional<ArdourCanvas::Rect>
437 Item::bounding_box () const
439 if (_bounding_box_dirty) {
440 compute_bounding_box ();
441 assert (!_bounding_box_dirty);
444 return _bounding_box;
448 Item::height () const
450 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
453 return bb->height ();
461 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
471 Item::redraw () const
473 if (_visible && _bounding_box && _canvas) {
474 _canvas->request_redraw (item_to_window (_bounding_box.get()));
479 Item::begin_change ()
481 _pre_change_bounding_box = bounding_box ();
487 _canvas->item_changed (this, _pre_change_bounding_box);
490 _parent->child_changed ();
495 Item::begin_visual_change ()
500 Item::end_visual_change ()
502 _canvas->item_visual_property_changed (this);
506 Item::move (Duple movement)
508 set_position (position() + movement);
515 _canvas->grab (this);
526 Item::set_data (string const & key, void* data)
532 Item::get_data (string const & key) const
534 map<string, void*>::const_iterator i = _data.find (key);
535 if (i == _data.end ()) {
543 Item::set_ignore_events (bool ignore)
545 _ignore_events = ignore;
549 Item::dump (ostream& o) const
551 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
553 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
554 o << " @ " << position();
563 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
564 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
573 Item::whatami () const
575 std::string type = demangle (typeid (*this).name());
576 return type.substr (type.find_last_of (':') + 1);
592 Item::covers (Duple const & point) const
594 Duple p = canvas_to_item (point);
596 if (_bounding_box_dirty) {
597 compute_bounding_box ();
600 boost::optional<Rect> r = bounding_box();
606 return r.get().contains (p);
610 ArdourCanvas::operator<< (ostream& o, const Item& i)