canvas items must be able to use fractional positions when rendering.
[ardour.git] / libs / canvas / item.cc
index 1b0bb0416ab5e2704f434d00f4122680cdfe91b7..ab8764a97a9677ad5ac138d4d85c27e054b83bb3 100644 (file)
@@ -104,6 +104,21 @@ Item::~Item ()
        delete _lut;
 }
 
+bool
+Item::visible() const
+{
+       Item const * i = this;
+
+       while (i) {
+               if (!i->self_visible()) {
+                       return false;
+               }
+               i = i->parent();
+       }
+
+       return true;
+}
+
 Duple
 Item::canvas_origin () const
 {
@@ -216,14 +231,16 @@ Item::window_to_item (ArdourCanvas::Duple const & d) const
 }
 
 ArdourCanvas::Rect
-Item::item_to_window (ArdourCanvas::Rect const & r) const
+Item::item_to_window (ArdourCanvas::Rect const & r, bool rounded) const
 {
        Rect ret = item_to_canvas (r).translate (-scroll_offset());
 
-       ret.x0 = round (ret.x0);
-       ret.x1 = round (ret.x1);
-       ret.y0 = round (ret.y0);
-       ret.y1 = round (ret.y1);
+       if (rounded) {
+               ret.x0 = round (ret.x0);
+               ret.x1 = round (ret.x1);
+               ret.y0 = round (ret.y0);
+               ret.y1 = round (ret.y1);
+       }
 
        return ret;
 }
@@ -254,10 +271,17 @@ Item::set_position (Duple p)
        
        _position = p;
 
-       _canvas->item_moved (this, pre_change_parent_bounding_box);
+       /* only update canvas and parent if visible. Otherwise, this
+          will be done when ::show() is called.
+       */
+       
+       if (visible()) {
+               _canvas->item_moved (this, pre_change_parent_bounding_box);
+               
 
-       if (_parent) {
-               _parent->child_changed ();
+               if (_parent) {
+                       _parent->child_changed ();
+               }
        }
 }
 
@@ -303,15 +327,28 @@ Item::hide ()
        if (_visible) {
                _visible = false;
 
-               /* recompute parent bounding box, which may alter now that this
-                * child is hidden.
-                */
+               /* children are all hidden because we are hidden, no need
+                  to propagate change because our bounding box necessarily
+                  includes them all already. thus our being hidden results
+                  in (a) a redraw of the entire bounding box (b) no children
+                  will be drawn.
 
-               if (_parent) {
-                       _parent->child_changed ();
+                  BUT ... current item in canvas might be one of our children,
+                  which is now hidden. So propagate away.
+               */
+
+               for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
+
+                       if ((*i)->self_visible()) {
+                               /* item was visible but is now hidden because
+                                  we (its parent) are hidden
+                               */
+                               (*i)->propagate_show_hide ();
+                       }
                }
 
-               _canvas->item_shown_or_hidden (this);
+               
+               propagate_show_hide ();
        }
 }
 
@@ -319,18 +356,34 @@ void
 Item::show ()
 {
        if (!_visible) {
-               _visible = true;
 
-               /* bounding box may have changed while we were hidden */
+               _visible = true;
 
-               if (_parent) {
-                       _parent->child_changed ();
+               for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
+                       if ((*i)->self_visible()) {
+                               /* item used to be hidden by us (its parent),
+                                  but is now visible
+                               */
+                               (*i)->propagate_show_hide ();
+                       }
                }
 
-               _canvas->item_shown_or_hidden (this);
+               propagate_show_hide ();
        }
 }
 
+void
+Item::propagate_show_hide ()
+{
+       /* bounding box may have changed while we were hidden */
+       
+       if (_parent) {
+               _parent->child_changed ();
+       }
+       
+       _canvas->item_shown_or_hidden (this);
+}
+
 Duple
 Item::item_to_parent (Duple const & d) const
 {
@@ -543,7 +596,7 @@ Item::height () const
 Coord
 Item::width () const 
 {
-       boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
+       boost::optional<ArdourCanvas::Rect> bb = bounding_box();
 
        if (bb) {
                return bb->width ();
@@ -555,7 +608,7 @@ Item::width () const
 void
 Item::redraw () const
 {
-       if (_visible && _bounding_box && _canvas) {
+       if (visible() && _bounding_box && _canvas) {
                _canvas->request_redraw (item_to_window (_bounding_box.get()));
        }
 }      
@@ -569,7 +622,7 @@ Item::begin_change ()
 void
 Item::end_change ()
 {
-       if (_visible) {
+       if (visible()) {
                _canvas->item_changed (this, _pre_change_bounding_box);
                
                if (_parent) {
@@ -586,7 +639,7 @@ Item::begin_visual_change ()
 void
 Item::end_visual_change ()
 {
-       if (_visible) {
+       if (visible()) {
                _canvas->item_visual_property_changed (this);
        }
 }
@@ -715,7 +768,7 @@ Item::render_children (Rect const & area, Cairo::RefPtr<Cairo::Context> context)
                        continue;
                }
                
-               Rect item = (*i)->item_to_window (item_bbox.get());
+               Rect item = (*i)->item_to_window (item_bbox.get(), false);
                boost::optional<Rect> d = item.intersection (area);
                
                if (d) {
@@ -1014,7 +1067,7 @@ Item::dump (ostream& o) const
 {
        boost::optional<ArdourCanvas::Rect> bb = bounding_box();
 
-       o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
+       o << _canvas->indent() << whatami() << ' ' << this << " self-Visible ? " << self_visible() << " visible ? " << visible();
        o << " @ " << position();
        
 #ifdef CANVAS_DEBUG
@@ -1038,7 +1091,8 @@ Item::dump (ostream& o) const
                o << _canvas->indent();
                o << " @ " << position();
                o << " Items: " << _items.size();
-               o << " Visible ? " << _visible;
+               o << " Self-Visible ? " << self_visible();
+               o << " Visible ? " << visible();
                
                boost::optional<Rect> bb = bounding_box();