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/group.h"
27 #include "canvas/item.h"
28 #include "canvas/canvas.h"
29 #include "canvas/debug.h"
33 using namespace ArdourCanvas;
35 Item::Item (Canvas* canvas)
42 Item::Item (Group* parent)
43 : _canvas (parent->canvas ())
49 Item::Item (Group* parent, Duple position)
50 : _canvas (parent->canvas())
52 , _position (position)
61 _bounding_box_dirty = true;
62 _ignore_events = false;
68 DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
74 _parent->remove (this);
78 _canvas->item_going_away (this, _bounding_box);
83 Item::item_to_parent (ArdourCanvas::Rect const & r) const
85 return r.translate (_position);
89 Item::item_to_canvas (ArdourCanvas::Rect const & r) const
91 Item const * i = this;
95 offset = offset.translate (i->position());
99 return r.translate (offset);
103 Item::item_to_canvas (ArdourCanvas::Duple const & d) const
105 Item const * i = this;
109 offset = offset.translate (i->position());
113 return d.translate (offset);
117 Item::canvas_to_item (ArdourCanvas::Duple const & d) const
119 Item const * i = this;
123 offset = offset.translate (-(i->position()));
127 return d.translate (offset);
131 Item::item_to_canvas (Coord& x, Coord& y) const
133 Duple d = item_to_canvas (Duple (x, y));
140 Item::canvas_to_item (Coord& x, Coord& y) const
142 Duple d = canvas_to_item (Duple (x, y));
148 /** Set the position of this item in the parent's coordinates */
150 Item::set_position (Duple p)
152 boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
153 boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
156 /* see the comment in Canvas::item_moved() to understand
157 * why we use the parent's bounding box here.
159 pre_change_parent_bounding_box = item_to_parent (bbox.get());
164 _canvas->item_moved (this, pre_change_parent_bounding_box);
167 _parent->child_changed ();
172 Item::set_x_position (Coord x)
174 set_position (Duple (x, _position.y));
178 Item::set_y_position (Coord y)
180 set_position (Duple (_position.x, y));
184 Item::raise_to_top ()
187 _parent->raise_child_to_top (this);
191 Item::raise (int levels)
194 _parent->raise_child (this, levels);
198 Item::lower_to_bottom ()
201 _parent->lower_child_to_bottom (this);
208 _canvas->item_shown_or_hidden (this);
215 _canvas->item_shown_or_hidden (this);
219 Item::item_to_parent (Duple const & d) const
221 return d.translate (_position);
225 Item::parent_to_item (Duple const & d) const
227 return d.translate (- _position);
231 Item::parent_to_item (ArdourCanvas::Rect const & d) const
233 return d.translate (- _position);
243 Item::reparent (Group* new_parent)
245 assert (_canvas == _parent->canvas());
248 _parent->remove (this);
253 _parent = new_parent;
254 _canvas = _parent->canvas ();
264 /** @return Bounding box in this item's coordinates */
265 boost::optional<ArdourCanvas::Rect>
266 Item::bounding_box () const
268 if (_bounding_box_dirty) {
269 compute_bounding_box ();
272 assert (!_bounding_box_dirty);
273 return _bounding_box;
277 Item::height () const
279 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
282 return bb->height ();
290 boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
300 Item::begin_change ()
302 _pre_change_bounding_box = bounding_box ();
308 _canvas->item_changed (this, _pre_change_bounding_box);
311 _parent->child_changed ();
316 Item::begin_visual_change ()
321 Item::end_visual_change ()
323 _canvas->item_visual_property_changed (this);
327 Item::move (Duple movement)
329 set_position (position() + movement);
336 _canvas->grab (this);
347 Item::set_data (string const & key, void* data)
353 Item::get_data (string const & key) const
355 map<string, void*>::const_iterator i = _data.find (key);
356 if (i == _data.end ()) {
364 Item::set_ignore_events (bool ignore)
366 _ignore_events = ignore;
370 Item::dump (ostream& o) const
372 boost::optional<ArdourCanvas::Rect> bb = bounding_box();
374 o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
375 o << " @ " << position();
384 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
385 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
394 Item::whatami () const
396 std::string type = demangle (typeid (*this).name());
397 return type.substr (type.find_last_of (':') + 1);
401 ArdourCanvas::operator<< (ostream& o, const Item& i)