X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fitem.cc;h=e4e3c3054667ee54143bd67435933178a5e9da2e;hb=e36f74e071d4c14862d23da5ff0d49df0940d536;hp=b553d8d6ae76ff0e9e9249f0e73523fa3822d2ed;hpb=fa71d82dda08558caf4a9c5102016f2746883d10;p=ardour.git diff --git a/libs/canvas/item.cc b/libs/canvas/item.cc index b553d8d6ae..e4e3c30546 100644 --- a/libs/canvas/item.cc +++ b/libs/canvas/item.cc @@ -181,6 +181,10 @@ Item::item_to_window (ArdourCanvas::Rect const & r) const void Item::set_position (Duple p) { + if (p == _position) { + return; + } + boost::optional bbox = bounding_box (); boost::optional pre_change_parent_bounding_box; @@ -236,15 +240,19 @@ Item::lower_to_bottom () void Item::hide () { - _visible = false; - _canvas->item_shown_or_hidden (this); + if (_visible) { + _visible = false; + _canvas->item_shown_or_hidden (this); + } } void Item::show () { - _visible = true; - _canvas->item_shown_or_hidden (this); + if (!_visible) { + _visible = true; + _canvas->item_shown_or_hidden (this); + } } Duple @@ -287,6 +295,103 @@ Item::reparent (Group* new_parent) _parent->add (this); } +bool +Item::common_ancestor_within (uint32_t limit, const Item& other) const +{ + uint32_t d1 = depth(); + 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 + */ + + while (d1 != d2) { + if (d1 > d2) { + i1 = i1->parent(); + d1--; + limit--; + } else { + i2 = i2->parent(); + d2--; + limit--; + } + if (limit == 0) { + return false; + } + } + + /* now see if there is a common parent */ + + while (i1 != i2) { + if (i1) { + i1 = i1->parent(); + } + if (i2) { + i2 = i2->parent (); + } + + limit--; + if (limit == 0) { + return false; + } + } + + return true; +} + +const Item* +Item::closest_ancestor_with (const Item& other) const +{ + uint32_t d1 = depth(); + 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 + */ + + while (d1 != d2) { + if (d1 > d2) { + i1 = i1->parent(); + d1--; + } else { + i2 = i2->parent(); + d2--; + } + } + + /* now see if there is a common parent */ + + while (i1 != i2) { + if (i1) { + i1 = i1->parent(); + } + if (i2) { + i2 = i2->parent (); + } + } + + return i1; +} + +bool +Item::is_descendant_of (const Item& candidate) const +{ + Item const * i = _parent; + + while (i) { + if (i == &candidate) { + return true; + } + i = i->parent(); + } + + return false; +} + void Item::grab_focus () { @@ -299,9 +404,9 @@ Item::bounding_box () const { if (_bounding_box_dirty) { compute_bounding_box (); + assert (!_bounding_box_dirty); } - assert (!_bounding_box_dirty); return _bounding_box; } @@ -328,6 +433,14 @@ Item::width () const return 0; } +void +Item::redraw () const +{ + if (_visible && _bounding_box && _canvas) { + _canvas->request_redraw (item_to_canvas (_bounding_box.get())); + } +} + void Item::begin_change () { @@ -429,9 +542,40 @@ Item::whatami () const return type.substr (type.find_last_of (':') + 1); } +uint32_t +Item::depth () const +{ + Item* i = _parent; + int d = 0; + while (i) { + ++d; + i = i->parent(); + } + return d; +} + +bool +Item::covers (Duple const & point) const +{ + Duple p = canvas_to_item (point); + + if (_bounding_box_dirty) { + compute_bounding_box (); + } + + boost::optional r = bounding_box(); + + if (!r) { + return false; + } + + return r.get().contains (p); +} + ostream& ArdourCanvas::operator<< (ostream& o, const Item& i) { i.dump (o); return o; } +