X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fcanvas.cc;h=d5d85623612c33f8bbe1a54ca28fada03a5c49b2;hb=e36f74e071d4c14862d23da5ff0d49df0940d536;hp=325fa77b980c0acdf4c8ca8d6ffccab406d99efa;hpb=4780c8425267a26ac5324687fd699ade79389d64;p=ardour.git diff --git a/libs/canvas/canvas.cc b/libs/canvas/canvas.cc index 325fa77b98..d5d8562361 100644 --- a/libs/canvas/canvas.cc +++ b/libs/canvas/canvas.cc @@ -22,6 +22,7 @@ * @brief Implementation of the main canvas classes. */ +#include #include #include #include @@ -31,6 +32,7 @@ #include "canvas/canvas.h" #include "canvas/debug.h" +#include "canvas/line.h" using namespace std; using namespace ArdourCanvas; @@ -50,13 +52,13 @@ Canvas::scroll_to (Coord x, Coord y) _scroll_offset_x = x; _scroll_offset_y = y; - enter_leave_items (0); // no current mouse position + pick_current_item (0); // no current mouse position } void Canvas::zoomed () { - enter_leave_items (0); // no current mouse position + pick_current_item (0); // no current mouse position } /** Render an area of the canvas. @@ -68,7 +70,7 @@ Canvas::render (Rect const & area, Cairo::RefPtr const & context { #ifdef CANVAS_DEBUG if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) { - cerr << "RENDER: " << area << endl; + cerr << this << " RENDER: " << area << endl; //cerr << "CANVAS @ " << this << endl; //dump (cerr); //cerr << "-------------------------\n"; @@ -85,16 +87,18 @@ Canvas::render (Rect const & area, Cairo::RefPtr const & context boost::optional draw = root_bbox->intersection (area); if (draw) { - - // context->rectangle (area.x0, area.y0, area.x1 - area.x0, area.y1 - area.y0); - // context->set_source_rgba (1.0, 0, 0, 1.0); - // context->fill (); - + /* there's a common area between the root and the requested area, so render it. */ _root.render (*draw, context); + + // This outlines the rect being rendered, after it has been drawn. + // context->rectangle (draw->x0, draw->y0, draw->x1 - draw->x0, draw->y1 - draw->y0); + // context->set_source_rgba (1.0, 0, 0, 1.0); + // context->stroke (); + } } @@ -192,8 +196,12 @@ Duple Canvas::canvas_to_window (Duple const & d) const { Duple wd = d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y)); + + /* Note that this intentionally always returns integer coordinates */ + wd.x = round (wd.x); wd.y = round (wd.y); + return wd; } @@ -207,10 +215,14 @@ Rect Canvas::canvas_to_window (Rect const & r) const { Rect wr = r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y)); - wr.x0 = floor (wr.x0); - wr.x1 = ceil (wr.x1); - wr.y0 = floor (wr.y0); - wr.y1 = ceil (wr.y1); + + /* Note that this intentionally always returns integer coordinates */ + + wr.x0 = round (wr.x0); + wr.x1 = round (wr.x1); + wr.y0 = round (wr.y0); + wr.y1 = round (wr.y1); + return wr; } @@ -250,13 +262,15 @@ void Canvas::queue_draw_item_area (Item* item, Rect area) { ArdourCanvas::Rect canvas_area = item->item_to_canvas (area); - // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl; + // cerr << "CANVAS " << this << " for " << item << ' ' << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << " window = " << canvas_to_window (canvas_area) << std::endl; request_redraw (canvas_area); } /** Construct a GtkCanvas */ GtkCanvas::GtkCanvas () - : _grabbed_item (0) + : _current_item (0) + , _new_current_item (0) + , _grabbed_item (0) , _focused_item (0) { /* these are the events we want to know about */ @@ -265,12 +279,12 @@ GtkCanvas::GtkCanvas () } void -GtkCanvas::enter_leave_items (int state) +GtkCanvas::pick_current_item (int state) { int x; int y; - /* this version of ::enter_leave_items() is called after an item is + /* this version of ::pick_current_item() is called after an item is * added or removed, so we have no coordinates to work from as is the * case with a motion event. Find out where the mouse is and use that. */ @@ -281,11 +295,11 @@ GtkCanvas::enter_leave_items (int state) return; } - enter_leave_items (window_to_canvas (Duple (x, y)), state); + pick_current_item (window_to_canvas (Duple (x, y)), state); } void -GtkCanvas::enter_leave_items (Duple const & point, int state) +GtkCanvas::pick_current_item (Duple const & point, int state) { /* we do not enter/leave items during a drag/grab */ @@ -293,157 +307,263 @@ GtkCanvas::enter_leave_items (Duple const & point, int state) return; } + /* find the items at the given position */ + + vector items; + _root.add_items_at_point (point, items); + + DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("%1 covers %2 items\n", point, items.size())); + +#ifndef NDEBUG + if (DEBUG_ENABLED(PBD::DEBUG::CanvasEnterLeave)) { + for (vector::const_iterator it = items.begin(); it != items.end(); ++it) { +#ifdef CANVAS_DEBUG + std::cerr << "\tItem " << (*it)->whatami() << '/' << (*it)->name << std::endl; +#else + std::cerr << "\tItem " << (*it)->whatami() << std::endl; +#endif + } + } +#endif + + /* put all items at point that are event-sensitive and visible and NOT + groups into within_items. Note that items is sorted from bottom to + top, but we're going to reverse that for within_items so that its + first item is the upper-most item that can be chosen as _current_item. + */ + + vector::const_iterator i; + list within_items; + + for (i = items.begin(); i != items.end(); ++i) { + + Item const * new_item = *i; + + /* We ignore invisible items, groups and items that ignore events */ + + if (!new_item->visible() || new_item->ignore_events() || dynamic_cast(new_item) != 0) { + continue; + } + + within_items.push_front (new_item); + } + + if (within_items.empty()) { + + /* no items at point, just send leave event below */ + _new_current_item = 0; + + } else { + + if (within_items.front() == _current_item) { + /* uppermost item at point is already _current_item */ + return; + } + + _new_current_item = const_cast (within_items.front()); + } + + if (_new_current_item != _current_item) { + deliver_enter_leave (point, state); + } +} + +void +GtkCanvas::deliver_enter_leave (Duple const & point, int state) +{ + /* setup enter & leave event structures */ + GdkEventCrossing enter_event; enter_event.type = GDK_ENTER_NOTIFY; enter_event.window = get_window()->gobj(); enter_event.send_event = 0; enter_event.subwindow = 0; enter_event.mode = GDK_CROSSING_NORMAL; - enter_event.detail = GDK_NOTIFY_NONLINEAR; enter_event.focus = FALSE; enter_event.state = state; enter_event.x = point.x; enter_event.y = point.y; - enter_event.detail = GDK_NOTIFY_UNKNOWN; GdkEventCrossing leave_event = enter_event; leave_event.type = GDK_LEAVE_NOTIFY; - /* find the items at the given position */ + Item* i; + GdkNotifyType enter_detail; + GdkNotifyType leave_detail; + vector items_to_leave_virtual; + vector items_to_enter_virtual; - vector items; - _root.add_items_at_point (point, items); + if (_new_current_item == 0) { - /* put all items at point that are event-sensitive and visible into within_items, and if this - is a new addition, also put them into newly_entered for later deliver of enter events. - */ - - vector::const_iterator i; - vector newly_entered; - Item const * new_item; + leave_detail = GDK_NOTIFY_UNKNOWN; - for (i = items.begin(); i != items.end(); ++i) { + if (_current_item) { - new_item = *i; + /* no current item, so also send virtual leave events to the + * entire heirarchy for the current item + */ - if (new_item->ignore_events() || !new_item->visible()) { - continue; + for (i = _current_item->parent(); i ; i = i->parent()) { + items_to_leave_virtual.push_back (i); + } } - pair::iterator,bool> res = within_items.insert (new_item); + } else if (_current_item == 0) { + + enter_detail = GDK_NOTIFY_UNKNOWN; - if (res.second) { - newly_entered.push_back (new_item); + /* no current item, so also send virtual enter events to the + * entire heirarchy for the new item + */ + + for (i = _new_current_item->parent(); i ; i = i->parent()) { + items_to_enter_virtual.push_back (i); } - } - /* for every item in "within_items", check that we are still within them. if not, - send a leave event, and remove them from "within_items" - */ + } else if (_current_item->is_descendant_of (*_new_current_item)) { - for (set::const_iterator i = within_items.begin(); i != within_items.end(); ) { + /* move from descendant to ancestor (X: "_current_item is an + * inferior ("child") of _new_current_item") + * + * Deliver "virtual" leave notifications to all items in the + * heirarchy between current and new_current. + */ + - set::const_iterator tmp = i; - ++tmp; + for (i = _current_item->parent(); i && i != _new_current_item; i = i->parent()) { + items_to_leave_virtual.push_back (i); + } - new_item = *i; + enter_detail = GDK_NOTIFY_INFERIOR; + leave_detail = GDK_NOTIFY_ANCESTOR; - boost::optional bbox = new_item->bounding_box(); - if (bbox) { - if (!new_item->item_to_canvas (bbox.get()).contains (point)) { - leave_event.detail = GDK_NOTIFY_UNKNOWN; - DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Leave %1 %2\n", new_item->whatami(), new_item->name)); - (*i)->Event (reinterpret_cast (&leave_event)); - within_items.erase (i); - } + } else if (_new_current_item->is_descendant_of (*_current_item)) { + /* move from ancestor to descendant (X: "_new_current_item is + * an inferior ("child") of _current_item") + * + * Deliver "virtual" enter notifications to all items in the + * heirarchy between current and new_current. + */ + + for (i = _new_current_item->parent(); i && i != _current_item; i = i->parent()) { + items_to_enter_virtual.push_back (i); + } + + enter_detail = GDK_NOTIFY_ANCESTOR; + leave_detail = GDK_NOTIFY_INFERIOR; + + } else { + + Item const * common_ancestor = _current_item->closest_ancestor_with (*_new_current_item); + + /* deliver virtual leave events to everything between _current + * and common_ancestor. + */ + + for (i = _current_item->parent(); i && i != common_ancestor; i = i->parent()) { + items_to_leave_virtual.push_back (i); + } + + /* deliver virtual enter events to everything between + * _new_current and common_ancestor. + */ + + for (i = _new_current_item->parent(); i && i != common_ancestor; i = i->parent()) { + items_to_enter_virtual.push_back (i); } - i = tmp; + enter_detail = GDK_NOTIFY_NONLINEAR; + leave_detail = GDK_NOTIFY_NONLINEAR; } - /* for every item in "newly_entered", send an enter event (and propagate it up the - item tree until it is handled - */ - for (vector::const_iterator i = newly_entered.begin(); i != newly_entered.end(); ++i) { - new_item = *i; + if (_current_item && !_current_item->ignore_events ()) { + leave_event.detail = leave_detail; + _current_item->Event ((GdkEvent*)&leave_event); + DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("LEAVE %1/%2\n", _current_item->whatami(), _current_item->name)); + } - new_item->Event (reinterpret_cast (&enter_event)); - DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Enter %1 %2\n", new_item->whatami(), new_item->name)); + leave_event.detail = GDK_NOTIFY_VIRTUAL; + enter_event.detail = GDK_NOTIFY_VIRTUAL; + + for (vector::iterator it = items_to_leave_virtual.begin(); it != items_to_leave_virtual.end(); ++it) { + if (!(*it)->ignore_events()) { + DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("leave %1/%2\n", (*it)->whatami(), (*it)->name)); + (*it)->Event ((GdkEvent*)&leave_event); + } } -#if 1 - cerr << "Within:\n"; - for (set::const_iterator i = within_items.begin(); i != within_items.end(); ++i) { - cerr << '\t' << (*i)->whatami() << '/' << (*i)->name << endl; + for (vector::iterator it = items_to_enter_virtual.begin(); it != items_to_enter_virtual.end(); ++it) { + if (!(*it)->ignore_events()) { + DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("enter %1/%2\n", (*it)->whatami(), (*it)->name)); + (*it)->Event ((GdkEvent*)&enter_event); + // std::cerr << "enter " << (*it)->whatami() << '/' << (*it)->name << std::endl; + } } - cerr << "----\n"; -#endif + + if (_new_current_item && !_new_current_item->ignore_events()) { + enter_event.detail = enter_detail; + DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("ENTER %1/%2\n", _new_current_item->whatami(), _new_current_item->name)); + _new_current_item->Event ((GdkEvent*)&enter_event); + } + + _current_item = _new_current_item; } + /** Deliver an event to the appropriate item; either the grabbed item, or * one of the items underneath the event. * @param point Position that the event has occurred at, in canvas coordinates. * @param event The event. */ bool -GtkCanvas::deliver_event (Duple point, GdkEvent* event) +GtkCanvas::deliver_event (GdkEvent* event) { /* Point in in canvas coordinate space */ + const Item* event_item; + if (_grabbed_item) { /* we have a grabbed item, so everything gets sent there */ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n", _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name)); - return _grabbed_item->Event (event); + event_item = _grabbed_item; + } else { + event_item = _current_item; } - /* find the items that exist at the event's position */ - vector items; - _root.add_items_at_point (point, items); + if (!event_item) { + return false; + } - DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 possible items at %2 to deliver event to\n", items.size(), point)); + /* run through the items from child to parent, until one claims the event */ - /* run through the items under the event, from top to bottom, until one claims the event */ - vector::const_reverse_iterator i = items.rbegin (); - while (i != items.rend()) { + Item* item = const_cast (event_item); + + while (item) { - if ((*i)->ignore_events ()) { - // DEBUG_TRACE ( - // PBD::DEBUG::CanvasEvents, - // string_compose ("canvas event ignored by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name) - // ); - ++i; - continue; - } - - if ((*i)->Event (event)) { + Item* parent = item->parent (); + + if (!item->ignore_events () && + item->Event (event)) { /* this item has just handled the event */ DEBUG_TRACE ( PBD::DEBUG::CanvasEvents, - string_compose ("canvas event handled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name) + string_compose ("canvas event handled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name) ); return true; } - DEBUG_TRACE ( - PBD::DEBUG::CanvasEvents, - string_compose ("canvas event left unhandled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name) - ); - - ++i; - } + DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event %3 left unhandled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name, event_type_string (event->type))); - /* debugging */ - if (PBD::debug_bits & PBD::DEBUG::CanvasEvents) { - while (i != items.rend()) { - DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event not seen by %1\n", (*i)->name.empty() ? "[unknown]" : (*i)->name)); - ++i; + if ((item = parent) == 0) { + break; } + } - + return false; } @@ -458,10 +578,9 @@ GtkCanvas::item_going_away (Item* item, boost::optional bounding_box) queue_draw_item_area (item, bounding_box.get ()); } - /* no need to send a leave event to this item, since it is going away - */ - - within_items.erase (item); + if (_new_current_item == item) { + _new_current_item = 0; + } if (_grabbed_item == item) { _grabbed_item = 0; @@ -471,7 +590,12 @@ GtkCanvas::item_going_away (Item* item, boost::optional bounding_box) _focused_item = 0; } - enter_leave_items (0); // no mouse state + if (_current_item == item) { + /* no need to send a leave event to this item, since it is going away + */ + _current_item = 0; + pick_current_item (0); // no mouse state + } } @@ -482,10 +606,8 @@ GtkCanvas::item_going_away (Item* item, boost::optional bounding_box) bool GtkCanvas::on_expose_event (GdkEventExpose* ev) { - Cairo::RefPtr c = get_window()->create_cairo_context (); - - render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c); - + Cairo::RefPtr cairo_context = get_window()->create_cairo_context (); + render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), cairo_context); return true; } @@ -520,8 +642,9 @@ GtkCanvas::on_button_press_event (GdkEventButton* ev) for scroll if this GtkCanvas is in a GtkCanvasViewport. */ + pick_current_item (where, ev->state); DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where)); - return deliver_event (where, reinterpret_cast(©)); + return deliver_event (reinterpret_cast(©)); } /** Handler for GDK button release events. @@ -536,7 +659,7 @@ GtkCanvas::on_button_release_event (GdkEventButton* ev) GdkEvent copy = *((GdkEvent*)ev); Duple where = window_to_canvas (Duple (ev->x, ev->y)); - enter_leave_items (where, ev->state); + pick_current_item (where, ev->state); copy.button.x = where.x; copy.button.y = where.y; @@ -545,8 +668,9 @@ GtkCanvas::on_button_release_event (GdkEventButton* ev) for scroll if this GtkCanvas is in a GtkCanvasViewport. */ + pick_current_item (where, ev->state); DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where)); - return deliver_event (where, reinterpret_cast(©)); + return deliver_event (reinterpret_cast(©)); } /** Handler for GDK motion events. @@ -568,18 +692,9 @@ GtkCanvas::on_motion_notify_event (GdkEventMotion* ev) /* Coordinates in "copy" will be canvas coordinates, */ - DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y)); + // DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y)); - if (_grabbed_item) { - /* if we have a grabbed item, it gets just the motion event, - since no enter/leave events can have happened. - */ - DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n", - _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name)); - return _grabbed_item->Event (reinterpret_cast (©)); - } - - enter_leave_items (where, ev->state); + pick_current_item (where, ev->state); /* Now deliver the motion event. It may seem a little inefficient to recompute the items under the event, but the enter notify/leave @@ -587,21 +702,23 @@ GtkCanvas::on_motion_notify_event (GdkEventMotion* ev) recompute the list in deliver_event. */ - return deliver_event (point, reinterpret_cast (©)); + return deliver_event (reinterpret_cast (©)); } bool GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev) { Duple where = window_to_canvas (Duple (ev->x, ev->y)); - enter_leave_items (where, ev->state); + pick_current_item (where, ev->state); return true; } bool -GtkCanvas::on_leave_notify_event (GdkEventCrossing* /*ev*/) +GtkCanvas::on_leave_notify_event (GdkEventCrossing* ev) { - within_items.clear (); + _new_current_item = 0; + Duple where = window_to_canvas (Duple (ev->x, ev->y)); + deliver_enter_leave (where, ev->state); return true; } @@ -611,8 +728,13 @@ GtkCanvas::on_leave_notify_event (GdkEventCrossing* /*ev*/) void GtkCanvas::request_redraw (Rect const & request) { - Rect area = canvas_to_window (request); - queue_draw_area (floor (area.x0), floor (area.y0), ceil (area.width()), ceil (area.height())); + boost::optional req = request.intersection (visible_area()); + + if (req) { + Rect r = req.get(); + Rect area = canvas_to_window (r); + queue_draw_area (area.x0, area.y0, area.width(), area.height()); + } } /** Called to request that we try to get a particular size for ourselves.