X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fitem.cc;h=3c3816881ace6aeda091cfb0799207f9f70acf02;hb=98dec658ee4067bdb22262640a821334ddc2ed83;hp=dca3064bce9b273f79b884f54cd9f38a240f4d12;hpb=77f5f4c4bf9074d953a1653658c8f96f38ae258c;p=ardour.git diff --git a/libs/canvas/item.cc b/libs/canvas/item.cc index dca3064bce..3c3816881a 100644 --- a/libs/canvas/item.cc +++ b/libs/canvas/item.cc @@ -171,7 +171,7 @@ Item::window_to_item (ArdourCanvas::Duple const & d) const return _canvas->window_to_canvas (canvas_to_item (d)); } -Rect +ArdourCanvas::Rect Item::item_to_window (ArdourCanvas::Rect const & r) const { return _canvas->canvas_to_window (item_to_canvas (r)); @@ -287,6 +287,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 () { @@ -328,6 +425,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 +534,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; } +