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::canvas_origin () const
88 return item_to_canvas (Duple (0,0));
92 Item::window_origin () const
94 /* This is slightly subtle. Our _position is in the coordinate space of
95 our parent. So to find out where that is in window coordinates, we
96 have to ask our parent.
99 return _parent->item_to_window (_position);
101 return _parent->item_to_window (Duple (0,0));
106 Item::item_to_parent (ArdourCanvas::Rect const & r) const
108 return r.translate (_position);
112 Item::scroll_offset () const
114 if (_scroll_parent) {
115 return _scroll_parent->scroll_offset();
121 Item::position_offset() const
123 Item const * i = this;
127 offset = offset.translate (i->position());
135 Item::item_to_canvas (ArdourCanvas::Rect const & r) const
137 return r.translate (position_offset());
141 Item::item_to_canvas (ArdourCanvas::Duple const & d) const
143 return d.translate (position_offset());
147 Item::canvas_to_item (ArdourCanvas::Duple const & r) const
149 return r.translate (-position_offset());
153 Item::canvas_to_item (ArdourCanvas::Rect const & r) const
155 return r.translate (-position_offset());
159 Item::item_to_canvas (Coord& x, Coord& y) const
161 Duple d = item_to_canvas (Duple (x, y));
168 Item::canvas_to_item (Coord& x, Coord& y) const
170 Duple d = canvas_to_item (Duple (x, y));
178 Item::item_to_window (ArdourCanvas::Duple const & d, bool rounded) const
180 Duple ret = item_to_canvas (d).translate (-scroll_offset());
183 ret.x = round (ret.x);
184 ret.y = round (ret.y);
191 Item::window_to_item (ArdourCanvas::Duple const & d) const
193 return canvas_to_item (d.translate (scroll_offset()));
197 Item::item_to_window (ArdourCanvas::Rect const & r) const
199 Rect ret = item_to_canvas (r).translate (-scroll_offset());
201 ret.x0 = round (ret.x0);
202 ret.x1 = round (ret.x1);
203 ret.y0 = round (ret.y0);
204 ret.y1 = round (ret.y1);
210 Item::window_to_item (ArdourCanvas::Rect const & r) const
212 return canvas_to_item (r.translate (scroll_offset()));
215 /** Set the position of this item in the parent's coordinates */
217 Item::set_position (Duple p)
219 if (p == _position) {
223 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
224 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
227 /* see the comment in Canvas::item_moved() to understand
228 * why we use the parent's bounding box here.
230 pre_change_parent_bounding_box = item_to_parent (bbox.get());
235 _canvas->item_moved (this, pre_change_parent_bounding_box);
238 _parent->child_changed ();
243 Item::set_x_position (Coord x)
245 set_position (Duple (x, _position.y));
249 Item::set_y_position (Coord y)
251 set_position (Duple (_position.x, y));
255 Item::raise_to_top ()
258 _parent->raise_child_to_top (this);
262 Item::raise (int levels)
265 _parent->raise_child (this, levels);
269 Item::lower_to_bottom ()
272 _parent->lower_child_to_bottom (this);
280 _canvas->item_shown_or_hidden (this);
289 _canvas->item_shown_or_hidden (this);
294 Item::item_to_parent (Duple const & d) const
296 return d.translate (_position);
300 Item::parent_to_item (Duple const & d) const
302 return d.translate (- _position);
306 Item::parent_to_item (ArdourCanvas::Rect const & d) const
308 return d.translate (- _position);
319 Item::reparent (Group* new_parent)
321 assert (_canvas == _parent->canvas());
324 _parent->remove (this);
329 _parent = new_parent;
330 _canvas = _parent->canvas ();
332 find_scroll_parent ();
338 Item::find_scroll_parent ()
340 Item const * i = this;
341 ScrollGroup const * last_scroll_group = 0;
343 /* Don't allow a scroll group to find itself as its own scroll parent
349 ScrollGroup const * sg = dynamic_cast<ScrollGroup const *> (i);
351 last_scroll_group = sg;
356 _scroll_parent = const_cast<ScrollGroup*> (last_scroll_group);
360 Item::common_ancestor_within (uint32_t limit, const Item& other) const
362 uint32_t d1 = depth();
363 uint32_t d2 = other.depth();
364 const Item* i1 = this;
365 const Item* i2 = &other;
367 /* move towards root until we are at the same level
386 /* now see if there is a common parent */
406 Item::closest_ancestor_with (const Item& other) const
408 uint32_t d1 = depth();
409 uint32_t d2 = other.depth();
410 const Item* i1 = this;
411 const Item* i2 = &other;
413 /* move towards root until we are at the same level
427 /* now see if there is a common parent */
442 Item::is_descendant_of (const Item& candidate) const
444 Item const * i = _parent;
447 if (i == &candidate) {
462 /** @return Bounding box in this item's coordinates */
463 boost::optional<ArdourCanvas::Rect>
464 Item::bounding_box () const
466 if (_bounding_box_dirty) {
467 compute_bounding_box ();
468 assert (!_bounding_box_dirty);
471 return _bounding_box;
475 Item::height () const
477 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
480 return bb->height ();
488 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
498 Item::redraw () const
500 if (_visible && _bounding_box && _canvas) {
501 _canvas->request_redraw (item_to_window (_bounding_box.get()));
506 Item::begin_change ()
508 _pre_change_bounding_box = bounding_box ();
514 _canvas->item_changed (this, _pre_change_bounding_box);
517 _parent->child_changed ();
522 Item::begin_visual_change ()
527 Item::end_visual_change ()
529 _canvas->item_visual_property_changed (this);
533 Item::move (Duple movement)
535 set_position (position() + movement);
542 _canvas->grab (this);
553 Item::set_data (string const & key, void* data)
559 Item::get_data (string const & key) const
561 map<string, void*>::const_iterator i = _data.find (key);
562 if (i == _data.end ()) {
570 Item::set_ignore_events (bool ignore)
572 _ignore_events = ignore;
576 Item::dump (ostream& o) const
578 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
580 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
581 o << " @ " << position();
590 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
591 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
600 Item::whatami () const
602 std::string type = demangle (typeid (*this).name());
603 return type.substr (type.find_last_of (':') + 1);
619 Item::covers (Duple const & point) const
621 Duple p = window_to_item (point);
623 if (_bounding_box_dirty) {
624 compute_bounding_box ();
627 boost::optional<Rect> r = bounding_box();
633 return r.get().contains (p);
637 ArdourCanvas::operator<< (ostream& o, const Item& i)