add Item::add_front()
[ardour.git] / libs / canvas / item.cc
index 593e7b4316a783af4fc521744765e13c453ec01f..711743d9f1f759240f9a15f03404f4abed1f21b6 100644 (file)
@@ -18,7 +18,7 @@
 */
 
 #include "pbd/compose.h"
-#include "pbd/stacktrace.h"
+#include "pbd/demangle.h"
 #include "pbd/convert.h"
 
 #include "ardour/utils.h"
@@ -46,7 +46,7 @@ Item::Item (Canvas* canvas)
        , _ignore_events (false)
 {
        DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
-}      
+}
 
 Item::Item (Item* parent)
        : Fill (*this)
@@ -66,7 +66,7 @@ Item::Item (Item* parent)
        }
 
        find_scroll_parent ();
-}      
+}
 
 Item::Item (Item* parent, Duple const& p)
        : Fill (*this)
@@ -88,7 +88,7 @@ Item::Item (Item* parent, Duple const& p)
 
        find_scroll_parent ();
 
-}      
+}
 
 Item::~Item ()
 {
@@ -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
 {
@@ -111,9 +126,9 @@ Item::canvas_origin () const
 }
 
 Duple
-Item::window_origin () const 
+Item::window_origin () const
 {
-       /* This is slightly subtle. Our _position is in the coordinate space of 
+       /* This is slightly subtle. Our _position is in the coordinate space of
           our parent. So to find out where that is in window coordinates, we
           have to ask our parent.
        */
@@ -135,7 +150,7 @@ Item::scroll_offset () const
 {
        if (_scroll_parent) {
                return _scroll_parent->scroll_offset();
-       } 
+       }
        return Duple (0,0);
 }
 
@@ -181,7 +196,7 @@ void
 Item::item_to_canvas (Coord& x, Coord& y) const
 {
        Duple d = item_to_canvas (Duple (x, y));
-               
+
        x = d.x;
        y = d.y;
 }
@@ -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;
 }
@@ -251,13 +268,20 @@ Item::set_position (Duple p)
                 */
                pre_change_parent_bounding_box = item_to_parent (bbox.get());
        }
-       
+
        _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 (_parent) {
-               _parent->child_changed ();
+       if (visible()) {
+               _canvas->item_moved (this, pre_change_parent_bounding_box);
+
+
+               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
 {
@@ -397,7 +450,7 @@ Item::find_scroll_parent ()
                }
                i = i->parent();
        }
-       
+
        _scroll_parent = const_cast<ScrollGroup*> (last_scroll_group);
 }
 
@@ -408,7 +461,7 @@ Item::common_ancestor_within (uint32_t limit, const Item& other) const
        uint32_t d2 = other.depth();
        const Item* i1 = this;
        const Item* i2 = &other;
-       
+
        /* move towards root until we are at the same level
           for both items
        */
@@ -449,7 +502,7 @@ Item::common_ancestor_within (uint32_t limit, const Item& other) const
                        return false;
                }
        }
-       
+
        return true;
 }
 
@@ -491,7 +544,7 @@ Item::closest_ancestor_with (const Item& other) const
                        i2 = i2->parent ();
                }
        }
-       
+
        return i1;
 }
 
@@ -530,7 +583,7 @@ Item::bounding_box () const
 }
 
 Coord
-Item::height () const 
+Item::height () const
 {
        boost::optional<ArdourCanvas::Rect> bb  = bounding_box();
 
@@ -541,9 +594,9 @@ Item::height () const
 }
 
 Coord
-Item::width () const 
+Item::width () const
 {
-       boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
+       boost::optional<ArdourCanvas::Rect> bb = bounding_box();
 
        if (bb) {
                return bb->width ();
@@ -555,10 +608,10 @@ 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()));
        }
-}      
+}
 
 void
 Item::begin_change ()
@@ -569,9 +622,9 @@ Item::begin_change ()
 void
 Item::end_change ()
 {
-       if (_visible) {
+       if (visible()) {
                _canvas->item_changed (this, _pre_change_bounding_box);
-               
+
                if (_parent) {
                        _parent->child_changed ();
                }
@@ -586,7 +639,7 @@ Item::begin_visual_change ()
 void
 Item::end_visual_change ()
 {
-       if (_visible) {
+       if (visible()) {
                _canvas->item_visual_property_changed (this);
        }
 }
@@ -624,7 +677,7 @@ Item::get_data (string const & key) const
        if (i == _data.end ()) {
                return 0;
        }
-       
+
        return i->second;
 }
 
@@ -635,7 +688,7 @@ Item::set_ignore_events (bool ignore)
 }
 
 std::string
-Item::whatami () const 
+Item::whatami () const
 {
        std::string type = demangle (typeid (*this).name());
        return type.substr (type.find_last_of (':') + 1);
@@ -685,14 +738,14 @@ Item::render_children (Rect const & area, Cairo::RefPtr<Cairo::Context> context)
 
 #ifdef CANVAS_DEBUG
        if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
-               cerr << string_compose ("%1%7 %2 @ %7 render %5 @ %6 %3 items out of %4\n", 
+               cerr << string_compose ("%1%7 %2 @ %7 render %5 @ %6 %3 items out of %4\n",
                                        _canvas->render_indent(), (name.empty() ? string ("[unnamed]") : name), items.size(), _items.size(), area, _position, this,
                                        whatami());
        }
 #endif
 
        ++render_depth;
-               
+
        for (std::vector<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {
 
                if (!(*i)->visible ()) {
@@ -703,7 +756,7 @@ Item::render_children (Rect const & area, Cairo::RefPtr<Cairo::Context> context)
 #endif
                        continue;
                }
-               
+
                boost::optional<Rect> item_bbox = (*i)->bounding_box ();
 
                if (!item_bbox) {
@@ -714,10 +767,10 @@ Item::render_children (Rect const & area, Cairo::RefPtr<Cairo::Context> context)
 #endif
                        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) {
                        Rect draw = d.get();
                        if (draw.width() && draw.height()) {
@@ -725,7 +778,7 @@ Item::render_children (Rect const & area, Cairo::RefPtr<Cairo::Context> context)
                                if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
                                        if (dynamic_cast<Container*>(*i) == 0) {
                                                cerr << _canvas->render_indent() << "render "
-                                                    << ' ' 
+                                                    << ' '
                                                     << (*i)
                                                     << ' '
                                                     << (*i)->whatami()
@@ -733,11 +786,11 @@ Item::render_children (Rect const & area, Cairo::RefPtr<Cairo::Context> context)
                                                     << (*i)->name
                                                     << " item "
                                                     << item_bbox.get()
-                                                    << " window = " 
+                                                    << " window = "
                                                     << item
                                                     << " intersect = "
                                                     << draw
-                                                    << " @ " 
+                                                    << " @ "
                                                     << _position
                                                     << endl;
                                        }
@@ -814,6 +867,17 @@ Item::add (Item* i)
        _bounding_box_dirty = true;
 }
 
+void
+Item::add_front (Item* i)
+{
+       /* XXX should really notify canvas about this */
+
+       _items.push_front (i);
+       i->reparent (this);
+       invalidate_lut ();
+       _bounding_box_dirty = true;
+}
+
 void
 Item::remove (Item* i)
 {
@@ -838,7 +902,7 @@ Item::remove (Item* i)
        _items.remove (i);
        invalidate_lut ();
        _bounding_box_dirty = true;
-       
+
        end_change ();
 }
 
@@ -872,7 +936,7 @@ Item::clear_items (bool with_delete)
 
                _items.erase (i);
                item->unparent ();
-               
+
                if (with_delete) {
                        delete item;
                }
@@ -892,7 +956,9 @@ Item::raise_child_to_top (Item* i)
 
        _items.remove (i);
        _items.push_back (i);
+
        invalidate_lut ();
+        redraw ();
 }
 
 void
@@ -911,6 +977,7 @@ Item::raise_child (Item* i, int levels)
 
        _items.insert (j, i);
        invalidate_lut ();
+        redraw ();
 }
 
 void
@@ -924,6 +991,7 @@ Item::lower_child_to_bottom (Item* i)
        _items.remove (i);
        _items.push_front (i);
        invalidate_lut ();
+        redraw ();
 }
 
 void
@@ -1010,9 +1078,9 @@ 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
        if (!name.empty()) {
                o << ' ' << name;
@@ -1034,26 +1102,27 @@ 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();
-               
+
                if (bb) {
                        o << endl << _canvas->indent() << "  bbox: " << bb.get();
                        o << endl << _canvas->indent() << "  CANVAS bbox: " << item_to_canvas (bb.get());
                } else {
                        o << "  bbox unset";
                }
-               
+
                o << endl;
 #endif
-               
+
                ArdourCanvas::dump_depth++;
-               
+
                for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
                        o << **i;
                }
-               
+
                ArdourCanvas::dump_depth--;
        }
 }