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::window_origin () const
88 /* This is slightly subtle. Our _position is in the coordinate space of
89 our parent. So to find out where that is in window coordinates, we
90 have to ask our parent.
93 return _parent->item_to_window (_position);
95 return _parent->item_to_window (Duple (0,0));
100 Item::item_to_parent (ArdourCanvas::Rect const & r) const
102 return r.translate (_position);
106 Item::scroll_offset () const
108 if (_scroll_parent) {
109 return _scroll_parent->scroll_offset();
111 return _canvas->scroll_offset();
115 Item::position_offset() const
117 Item const * i = this;
121 offset = offset.translate (i->position());
129 Item::item_to_canvas (ArdourCanvas::Rect const & r) const
131 return r.translate (position_offset());
135 Item::item_to_canvas (ArdourCanvas::Duple const & d) const
137 return d.translate (position_offset());
141 Item::canvas_to_item (ArdourCanvas::Duple const & r) const
143 return r.translate (-position_offset());
147 Item::canvas_to_item (ArdourCanvas::Rect const & r) const
149 return r.translate (-position_offset());
153 Item::item_to_canvas (Coord& x, Coord& y) const
155 Duple d = item_to_canvas (Duple (x, y));
162 Item::canvas_to_item (Coord& x, Coord& y) const
164 Duple d = canvas_to_item (Duple (x, y));
172 Item::item_to_window (ArdourCanvas::Duple const & d, bool rounded) const
174 Duple ret = item_to_canvas (d).translate (-scroll_offset());
176 ret.x = round (ret.x);
177 ret.y = round (ret.y);
183 Item::window_to_item (ArdourCanvas::Duple const & d) const
185 return canvas_to_item (d.translate (scroll_offset()));
189 Item::item_to_window (ArdourCanvas::Rect const & r) const
191 Rect ret = item_to_canvas (r).translate (-scroll_offset());
193 ret.x0 = round (ret.x0);
194 ret.x1 = round (ret.x1);
195 ret.y0 = round (ret.y0);
196 ret.y1 = round (ret.y1);
202 Item::window_to_item (ArdourCanvas::Rect const & r) const
204 return canvas_to_item (r.translate (scroll_offset()));
207 /** Set the position of this item in the parent's coordinates */
209 Item::set_position (Duple p)
211 if (p == _position) {
215 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
216 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
219 /* see the comment in Canvas::item_moved() to understand
220 * why we use the parent's bounding box here.
222 pre_change_parent_bounding_box = item_to_parent (bbox.get());
227 _canvas->item_moved (this, pre_change_parent_bounding_box);
230 _parent->child_changed ();
235 Item::set_x_position (Coord x)
237 set_position (Duple (x, _position.y));
241 Item::set_y_position (Coord y)
243 set_position (Duple (_position.x, y));
247 Item::raise_to_top ()
250 _parent->raise_child_to_top (this);
254 Item::raise (int levels)
257 _parent->raise_child (this, levels);
261 Item::lower_to_bottom ()
264 _parent->lower_child_to_bottom (this);
272 _canvas->item_shown_or_hidden (this);
281 _canvas->item_shown_or_hidden (this);
286 Item::item_to_parent (Duple const & d) const
288 return d.translate (_position);
292 Item::parent_to_item (Duple const & d) const
294 return d.translate (- _position);
298 Item::parent_to_item (ArdourCanvas::Rect const & d) const
300 return d.translate (- _position);
311 Item::reparent (Group* new_parent)
313 assert (_canvas == _parent->canvas());
316 _parent->remove (this);
321 _parent = new_parent;
322 _canvas = _parent->canvas ();
324 find_scroll_parent ();
330 Item::find_scroll_parent ()
332 Item const * i = this;
333 ScrollGroup const * last_scroll_group = 0;
335 /* Don't allow a scroll group to find itself as its own scroll parent
341 ScrollGroup const * sg = dynamic_cast<ScrollGroup const *> (i);
343 last_scroll_group = sg;
348 _scroll_parent = const_cast<ScrollGroup*> (last_scroll_group);
352 Item::common_ancestor_within (uint32_t limit, const Item& other) const
354 uint32_t d1 = depth();
355 uint32_t d2 = other.depth();
356 const Item* i1 = this;
357 const Item* i2 = &other;
359 /* move towards root until we are at the same level
378 /* now see if there is a common parent */
398 Item::closest_ancestor_with (const Item& other) const
400 uint32_t d1 = depth();
401 uint32_t d2 = other.depth();
402 const Item* i1 = this;
403 const Item* i2 = &other;
405 /* move towards root until we are at the same level
419 /* now see if there is a common parent */
434 Item::is_descendant_of (const Item& candidate) const
436 Item const * i = _parent;
439 if (i == &candidate) {
454 /** @return Bounding box in this item's coordinates */
455 boost::optional<ArdourCanvas::Rect>
456 Item::bounding_box () const
458 if (_bounding_box_dirty) {
459 compute_bounding_box ();
460 assert (!_bounding_box_dirty);
463 return _bounding_box;
467 Item::height () const
469 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
472 return bb->height ();
480 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
490 Item::redraw () const
492 if (_visible && _bounding_box && _canvas) {
493 _canvas->request_redraw (item_to_window (_bounding_box.get()));
498 Item::begin_change ()
500 _pre_change_bounding_box = bounding_box ();
506 _canvas->item_changed (this, _pre_change_bounding_box);
509 _parent->child_changed ();
514 Item::begin_visual_change ()
519 Item::end_visual_change ()
521 _canvas->item_visual_property_changed (this);
525 Item::move (Duple movement)
527 set_position (position() + movement);
534 _canvas->grab (this);
545 Item::set_data (string const & key, void* data)
551 Item::get_data (string const & key) const
553 map<string, void*>::const_iterator i = _data.find (key);
554 if (i == _data.end ()) {
562 Item::set_ignore_events (bool ignore)
564 _ignore_events = ignore;
568 Item::dump (ostream& o) const
570 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
572 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
573 o << " @ " << position();
582 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
583 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
592 Item::whatami () const
594 std::string type = demangle (typeid (*this).name());
595 return type.substr (type.find_last_of (':') + 1);
611 Item::covers (Duple const & point) const
613 Duple p = window_to_item (point);
615 if (_bounding_box_dirty) {
616 compute_bounding_box ();
619 boost::optional<Rect> r = bounding_box();
625 return r.get().contains (p);
629 ArdourCanvas::operator<< (ostream& o, const Item& i)