a few changes to fix region dragging, all related to coordinate system handling,...
[ardour.git] / libs / canvas / item.cc
index 90b53b5bfd5cadb1f33b20f29acbfe0ee4353bf7..75a5718271af928eb54a5f08813c9a1ffce066c7 100644 (file)
@@ -61,19 +61,83 @@ Item::~Item ()
        }
 }
 
-Rect
-Item::item_to_parent (Rect const & r) const
+ArdourCanvas::Rect
+Item::item_to_parent (ArdourCanvas::Rect const & r) const
 {
        return r.translate (_position);
 }
 
+ArdourCanvas::Rect
+Item::item_to_canvas (ArdourCanvas::Rect const & r) const
+{
+       Item const * i = this;
+       Duple offset;
+
+       while (i) {
+               offset = offset.translate (i->position());
+               i = i->parent();
+       }
+
+       return r.translate (offset);
+}
+
+ArdourCanvas::Duple
+Item::item_to_canvas (ArdourCanvas::Duple const & d) const
+{
+       Item const * i = this;
+       Duple offset;
+
+       while (i) {
+               offset = offset.translate (i->position());
+               i = i->parent();
+       }
+
+       return d.translate (offset);
+}
+
+ArdourCanvas::Duple
+Item::canvas_to_item (ArdourCanvas::Duple const & d) const
+{
+       Item const * i = this;
+       Duple offset;
+
+       while (i) {
+               offset = offset.translate (-(i->position()));
+               i = i->parent();
+       }
+
+       return d.translate (offset);
+}
+
+void
+Item::item_to_canvas (Coord& x, Coord& y) const
+{
+       Duple d = item_to_canvas (Duple (x, y));
+               
+       x = d.x;
+       y = d.y;
+}
+
+void
+Item::canvas_to_item (Coord& x, Coord& y) const
+{
+       Duple d = canvas_to_item (Duple (x, y));
+
+       x = d.x;
+       y = d.y;
+}
+
 /** Set the position of this item in the parent's coordinates */
 void
 Item::set_position (Duple p)
 {
-       boost::optional<Rect> bbox = bounding_box ();
-       boost::optional<Rect> pre_change_parent_bounding_box;
+       boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
+       boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
+
        if (bbox) {
+               /* see the comment in Canvas::item_moved() to understand
+                * why we use the parent's bounding box here.
+                */
                pre_change_parent_bounding_box = item_to_parent (bbox.get());
        }
        
@@ -145,8 +209,8 @@ Item::parent_to_item (Duple const & d) const
        return d.translate (- _position);
 }
 
-Rect
-Item::parent_to_item (Rect const & d) const
+ArdourCanvas::Rect
+Item::parent_to_item (ArdourCanvas::Rect const & d) const
 {
        return d.translate (- _position);
 }
@@ -179,7 +243,7 @@ Item::grab_focus ()
 }
 
 /** @return Bounding box in this item's coordinates */
-boost::optional<Rect>
+boost::optional<ArdourCanvas::Rect>
 Item::bounding_box () const
 {
        if (_bounding_box_dirty) {
@@ -193,7 +257,7 @@ Item::bounding_box () const
 Coord
 Item::height () const 
 {
-       boost::optional<Rect> bb  = bounding_box();
+       boost::optional<ArdourCanvas::Rect> bb  = bounding_box();
 
        if (bb) {
                return bb->height ();
@@ -204,7 +268,7 @@ Item::height () const
 Coord
 Item::width () const 
 {
-       boost::optional<Rect> bb = bounding_box().get();
+       boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
 
        if (bb) {
                return bb->width ();
@@ -283,50 +347,6 @@ Item::get_data (string const & key) const
        return i->second;
 }
 
-void
-Item::item_to_canvas (Coord& x, Coord& y) const
-{
-       Duple d (x, y);
-       Item const * i = this;
-       
-       while (i) {
-               d = i->item_to_parent (d);
-               i = i->_parent;
-       }
-               
-       x = d.x;
-       y = d.y;
-}
-
-void
-Item::canvas_to_item (Coord& x, Coord& y) const
-{
-       Duple d (x, y);
-       Item const * i = this;
-
-       while (i) {
-               d = i->parent_to_item (d);
-               i = i->_parent;
-       }
-
-       x = d.x;
-       y = d.y;
-}
-
-Rect
-Item::item_to_canvas (Rect const & area) const
-{
-       Rect r = area;
-       Item const * i = this;
-
-       while (i) {
-               r = i->item_to_parent (r);
-               i = i->parent ();
-       }
-
-       return r;
-}
-
 void
 Item::set_ignore_events (bool ignore)
 {
@@ -336,9 +356,10 @@ Item::set_ignore_events (bool ignore)
 void
 Item::dump (ostream& o) const
 {
-       boost::optional<Rect> bb = bounding_box();
+       boost::optional<ArdourCanvas::Rect> bb = bounding_box();
 
        o << _canvas->indent() << whatami() << ' ' << this;
+       o << " @ " << position();
        
 #ifdef CANVAS_DEBUG
        if (!name.empty()) {
@@ -348,6 +369,7 @@ Item::dump (ostream& o) const
 
        if (bb) {
                o << endl << _canvas->indent() << "\tbbox: " << bb.get();
+               o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
        } else {
                o << " bbox unset";
        }