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());
177 ret.x = round (ret.x);
178 ret.y = round (ret.y);
185 Item::window_to_item (ArdourCanvas::Duple const & d) const
187 return canvas_to_item (d.translate (scroll_offset()));
191 Item::item_to_window (ArdourCanvas::Rect const & r) const
193 Rect ret = item_to_canvas (r).translate (-scroll_offset());
195 ret.x0 = round (ret.x0);
196 ret.x1 = round (ret.x1);
197 ret.y0 = round (ret.y0);
198 ret.y1 = round (ret.y1);
204 Item::window_to_item (ArdourCanvas::Rect const & r) const
206 return canvas_to_item (r.translate (scroll_offset()));
209 /** Set the position of this item in the parent's coordinates */
211 Item::set_position (Duple p)
213 if (p == _position) {
217 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
218 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
221 /* see the comment in Canvas::item_moved() to understand
222 * why we use the parent's bounding box here.
224 pre_change_parent_bounding_box = item_to_parent (bbox.get());
229 _canvas->item_moved (this, pre_change_parent_bounding_box);
232 _parent->child_changed ();
237 Item::set_x_position (Coord x)
239 set_position (Duple (x, _position.y));
243 Item::set_y_position (Coord y)
245 set_position (Duple (_position.x, y));
249 Item::raise_to_top ()
252 _parent->raise_child_to_top (this);
256 Item::raise (int levels)
259 _parent->raise_child (this, levels);
263 Item::lower_to_bottom ()
266 _parent->lower_child_to_bottom (this);
274 _canvas->item_shown_or_hidden (this);
283 _canvas->item_shown_or_hidden (this);
288 Item::item_to_parent (Duple const & d) const
290 return d.translate (_position);
294 Item::parent_to_item (Duple const & d) const
296 return d.translate (- _position);
300 Item::parent_to_item (ArdourCanvas::Rect const & d) const
302 return d.translate (- _position);
313 Item::reparent (Group* new_parent)
315 assert (_canvas == _parent->canvas());
318 _parent->remove (this);
323 _parent = new_parent;
324 _canvas = _parent->canvas ();
326 find_scroll_parent ();
332 Item::find_scroll_parent ()
334 Item const * i = this;
335 ScrollGroup const * last_scroll_group = 0;
337 /* Don't allow a scroll group to find itself as its own scroll parent
343 ScrollGroup const * sg = dynamic_cast<ScrollGroup const *> (i);
345 last_scroll_group = sg;
350 _scroll_parent = const_cast<ScrollGroup*> (last_scroll_group);
354 Item::common_ancestor_within (uint32_t limit, const Item& other) const
356 uint32_t d1 = depth();
357 uint32_t d2 = other.depth();
358 const Item* i1 = this;
359 const Item* i2 = &other;
361 /* move towards root until we are at the same level
380 /* now see if there is a common parent */
400 Item::closest_ancestor_with (const Item& other) const
402 uint32_t d1 = depth();
403 uint32_t d2 = other.depth();
404 const Item* i1 = this;
405 const Item* i2 = &other;
407 /* move towards root until we are at the same level
421 /* now see if there is a common parent */
436 Item::is_descendant_of (const Item& candidate) const
438 Item const * i = _parent;
441 if (i == &candidate) {
456 /** @return Bounding box in this item's coordinates */
457 boost::optional<ArdourCanvas::Rect>
458 Item::bounding_box () const
460 if (_bounding_box_dirty) {
461 compute_bounding_box ();
462 assert (!_bounding_box_dirty);
465 return _bounding_box;
469 Item::height () const
471 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
474 return bb->height ();
482 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
492 Item::redraw () const
494 if (_visible && _bounding_box && _canvas) {
495 _canvas->request_redraw (item_to_window (_bounding_box.get()));
500 Item::begin_change ()
502 _pre_change_bounding_box = bounding_box ();
508 _canvas->item_changed (this, _pre_change_bounding_box);
511 _parent->child_changed ();
516 Item::begin_visual_change ()
521 Item::end_visual_change ()
523 _canvas->item_visual_property_changed (this);
527 Item::move (Duple movement)
529 set_position (position() + movement);
536 _canvas->grab (this);
547 Item::set_data (string const & key, void* data)
553 Item::get_data (string const & key) const
555 map<string, void*>::const_iterator i = _data.find (key);
556 if (i == _data.end ()) {
564 Item::set_ignore_events (bool ignore)
566 _ignore_events = ignore;
570 Item::dump (ostream& o) const
572 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
574 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
575 o << " @ " << position();
584 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
585 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
594 Item::whatami () const
596 std::string type = demangle (typeid (*this).name());
597 return type.substr (type.find_last_of (':') + 1);
613 Item::covers (Duple const & point) const
615 Duple p = window_to_item (point);
617 if (_bounding_box_dirty) {
618 compute_bounding_box ();
621 boost::optional<Rect> r = bounding_box();
627 return r.get().contains (p);
631 ArdourCanvas::operator<< (ostream& o, const Item& i)