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 , _bounding_box_dirty (true)
44 , _ignore_events (false)
46 DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
49 Item::Item (Group* parent)
52 , _canvas (parent->canvas())
56 , _bounding_box_dirty (true)
57 , _ignore_events (false)
59 DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
65 find_scroll_parent ();
68 Item::Item (Group* parent, Duple const& p)
71 , _canvas (parent->canvas())
76 , _bounding_box_dirty (true)
77 , _ignore_events (false)
79 DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
85 find_scroll_parent ();
92 _parent->remove (this);
96 _canvas->item_going_away (this, _bounding_box);
101 Item::canvas_origin () const
103 return item_to_canvas (Duple (0,0));
107 Item::window_origin () const
109 /* This is slightly subtle. Our _position is in the coordinate space of
110 our parent. So to find out where that is in window coordinates, we
111 have to ask our parent.
114 return _parent->item_to_window (_position);
121 Item::item_to_parent (ArdourCanvas::Rect const & r) const
123 return r.translate (_position);
127 Item::scroll_offset () const
129 if (_scroll_parent) {
130 return _scroll_parent->scroll_offset();
136 Item::position_offset() const
138 Item const * i = this;
142 offset = offset.translate (i->position());
150 Item::item_to_canvas (ArdourCanvas::Rect const & r) const
152 return r.translate (position_offset());
156 Item::item_to_canvas (ArdourCanvas::Duple const & d) const
158 return d.translate (position_offset());
162 Item::canvas_to_item (ArdourCanvas::Duple const & r) const
164 return r.translate (-position_offset());
168 Item::canvas_to_item (ArdourCanvas::Rect const & r) const
170 return r.translate (-position_offset());
174 Item::item_to_canvas (Coord& x, Coord& y) const
176 Duple d = item_to_canvas (Duple (x, y));
183 Item::canvas_to_item (Coord& x, Coord& y) const
185 Duple d = canvas_to_item (Duple (x, y));
193 Item::item_to_window (ArdourCanvas::Duple const & d, bool rounded) const
195 Duple ret = item_to_canvas (d).translate (-scroll_offset());
198 ret.x = round (ret.x);
199 ret.y = round (ret.y);
206 Item::window_to_item (ArdourCanvas::Duple const & d) const
208 return canvas_to_item (d.translate (scroll_offset()));
212 Item::item_to_window (ArdourCanvas::Rect const & r) const
214 Rect ret = item_to_canvas (r).translate (-scroll_offset());
216 ret.x0 = round (ret.x0);
217 ret.x1 = round (ret.x1);
218 ret.y0 = round (ret.y0);
219 ret.y1 = round (ret.y1);
225 Item::window_to_item (ArdourCanvas::Rect const & r) const
227 return canvas_to_item (r.translate (scroll_offset()));
230 /** Set the position of this item in the parent's coordinates */
232 Item::set_position (Duple p)
234 if (p == _position) {
238 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
239 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
242 /* see the comment in Canvas::item_moved() to understand
243 * why we use the parent's bounding box here.
245 pre_change_parent_bounding_box = item_to_parent (bbox.get());
250 _canvas->item_moved (this, pre_change_parent_bounding_box);
253 _parent->child_changed ();
258 Item::set_x_position (Coord x)
260 set_position (Duple (x, _position.y));
264 Item::set_y_position (Coord y)
266 set_position (Duple (_position.x, y));
270 Item::raise_to_top ()
273 _parent->raise_child_to_top (this);
278 Item::raise (int levels)
281 _parent->raise_child (this, levels);
286 Item::lower_to_bottom ()
289 _parent->lower_child_to_bottom (this);
298 _canvas->item_shown_or_hidden (this);
307 _canvas->item_shown_or_hidden (this);
312 Item::item_to_parent (Duple const & d) const
314 return d.translate (_position);
318 Item::parent_to_item (Duple const & d) const
320 return d.translate (- _position);
324 Item::parent_to_item (ArdourCanvas::Rect const & d) const
326 return d.translate (- _position);
337 Item::reparent (Group* new_parent)
339 if (new_parent == _parent) {
343 assert (_canvas == new_parent->canvas());
346 _parent->remove (this);
351 _parent = new_parent;
352 _canvas = _parent->canvas ();
354 find_scroll_parent ();
360 Item::find_scroll_parent ()
362 Item const * i = this;
363 ScrollGroup const * last_scroll_group = 0;
365 /* Don't allow a scroll group to find itself as its own scroll parent
371 ScrollGroup const * sg = dynamic_cast<ScrollGroup const *> (i);
373 last_scroll_group = sg;
378 _scroll_parent = const_cast<ScrollGroup*> (last_scroll_group);
382 Item::common_ancestor_within (uint32_t limit, const Item& other) const
384 uint32_t d1 = depth();
385 uint32_t d2 = other.depth();
386 const Item* i1 = this;
387 const Item* i2 = &other;
389 /* move towards root until we are at the same level
414 /* now see if there is a common parent */
434 Item::closest_ancestor_with (const Item& other) const
436 uint32_t d1 = depth();
437 uint32_t d2 = other.depth();
438 const Item* i1 = this;
439 const Item* i2 = &other;
441 /* move towards root until we are at the same level
461 /* now see if there is a common parent */
476 Item::is_descendant_of (const Item& candidate) const
478 Item const * i = _parent;
481 if (i == &candidate) {
496 /** @return Bounding box in this item's coordinates */
497 boost::optional<ArdourCanvas::Rect>
498 Item::bounding_box () const
500 if (_bounding_box_dirty) {
501 compute_bounding_box ();
502 assert (!_bounding_box_dirty);
505 return _bounding_box;
509 Item::height () const
511 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
514 return bb->height ();
522 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
532 Item::redraw () const
534 if (_visible && _bounding_box && _canvas) {
535 _canvas->request_redraw (item_to_window (_bounding_box.get()));
540 Item::begin_change ()
542 _pre_change_bounding_box = bounding_box ();
549 _canvas->item_changed (this, _pre_change_bounding_box);
552 _parent->child_changed ();
558 Item::begin_visual_change ()
563 Item::end_visual_change ()
566 _canvas->item_visual_property_changed (this);
571 Item::move (Duple movement)
573 set_position (position() + movement);
580 _canvas->grab (this);
591 Item::set_data (string const & key, void* data)
597 Item::get_data (string const & key) const
599 map<string, void*>::const_iterator i = _data.find (key);
600 if (i == _data.end ()) {
608 Item::set_ignore_events (bool ignore)
610 _ignore_events = ignore;
614 Item::dump (ostream& o) const
616 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
618 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
619 o << " @ " << position();
628 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
629 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
638 Item::whatami () const
640 std::string type = demangle (typeid (*this).name());
641 return type.substr (type.find_last_of (':') + 1);
657 Item::covers (Duple const & point) const
659 Duple p = window_to_item (point);
661 if (_bounding_box_dirty) {
662 compute_bounding_box ();
665 boost::optional<Rect> r = bounding_box();
671 return r.get().contains (p);
675 ArdourCanvas::operator<< (ostream& o, const Item& i)