Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / canvas.cc
index 8b0586c9aeeae209c92bc29abf5e8005faf323b7..8a31d1fa9fede0faddf400d374d05d572f0aec6b 100644 (file)
@@ -22,6 +22,7 @@
  *  @brief Implementation of the main canvas classes.
  */
 
+#include <list>
 #include <cassert>
 #include <gtkmm/adjustment.h>
 #include <gtkmm/label.h>
@@ -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<Cairo::Context> 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<Cairo::Context> const & context
 
        boost::optional<Rect> 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->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,102 +307,202 @@ GtkCanvas::enter_leave_items (Duple const & point, int state)
                return;
        }
 
+       /* find the items at the given position */
+
+       vector<Item const *> items;
+       _root.add_items_at_point (point, items);
+
+       /* 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<Item const *>::const_iterator i;
+       list<Item const *> 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<Group const *>(new_item) != 0) {
+                       continue;
+               }
+               
+               within_items.push_front (new_item);
+       }
+
+       if (within_items.empty()) {
+
+               /* no items at point, just send leave event below */
+
+       } else {
+
+               if (within_items.front() == _current_item) {
+                       /* uppermost item at point is already _current_item */
+                       return;
+               }
+               
+               _new_current_item = const_cast<Item*> (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<Item*> items_to_leave_virtual;
+       vector<Item*> items_to_enter_virtual;
 
-       vector<Item const *> 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<Item const *>::const_iterator i;
-       vector<Item const *> 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<set<Item const *>::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<Item const *>::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<Item const *>::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<Rect> 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<GdkEvent*> (&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);
                }
 
-               i = tmp;
+               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);
+               }
+
+               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<Item const*>::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);
+               // std::cerr << "LEAVE " << _current_item->whatami() << '/' << _current_item->name << std::endl;
+       }
+
+       leave_event.detail = GDK_NOTIFY_VIRTUAL;
+       enter_event.detail = GDK_NOTIFY_VIRTUAL;
 
-               new_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
-               DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Enter %1 %2\n", new_item->whatami(), new_item->name));
+       for (vector<Item*>::iterator it = items_to_leave_virtual.begin(); it != items_to_leave_virtual.end(); ++it) {
+               if (!(*it)->ignore_events()) {
+                       (*it)->Event ((GdkEvent*)&leave_event);
+                       // std::cerr << "leave " << (*it)->whatami() << '/' << (*it)->name << std::endl;
+               }
        }
 
-#if 1
-       cerr << "Within:\n";
-       for (set<Item const *>::const_iterator i = within_items.begin(); i != within_items.end(); ++i) {
-               cerr << '\t' << (*i)->whatami() << '/' << (*i)->name << endl;
+       for (vector<Item*>::iterator it = items_to_enter_virtual.begin(); it != items_to_enter_virtual.end(); ++it) {
+               if (!(*it)->ignore_events()) {
+                       (*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;
+               _new_current_item->Event ((GdkEvent*)&enter_event);
+               // std::cerr << "ENTER " << _new_current_item->whatami() << '/' << _new_current_item->name << std::endl;
+       }
+
+       _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 */
 
@@ -399,51 +513,37 @@ GtkCanvas::deliver_event (Duple point, GdkEvent* event)
                return _grabbed_item->Event (event);
        }
 
-       /* find the items that exist at the event's position */
-       vector<Item const *> items;
-       _root.add_items_at_point (point, items);
+       if (!_current_item) {
+               return false;
+       }
+
+       /* run through the items from child to parent, until one claims the event */
 
-       DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 possible items to deliver event to\n", items.size()));
+       Item* item = const_cast<Item*> (_current_item);
+       
+       while (item) {
 
-       /* run through the items under the event, from top to bottom, until one claims the event */
-       vector<Item const *>::const_reverse_iterator i = items.rbegin ();
-       while (i != items.rend()) {
+               Item* parent = item->parent ();
 
-               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)) {
+               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 +558,9 @@ GtkCanvas::item_going_away (Item* item, boost::optional<Rect> 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 +570,12 @@ GtkCanvas::item_going_away (Item* item, boost::optional<Rect> 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,9 +586,10 @@ GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
 bool
 GtkCanvas::on_expose_event (GdkEventExpose* ev)
 {
-       Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
+       Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
+       Rect area (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height);
 
-       render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
+       render (area, cairo_context);
 
        return true;
 }
@@ -520,8 +625,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<GdkEvent*>(&copy));
+       return deliver_event (reinterpret_cast<GdkEvent*>(&copy));
 }
 
 /** Handler for GDK button release events.
@@ -536,7 +642,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 +651,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<GdkEvent*>(&copy));
+       return deliver_event (reinterpret_cast<GdkEvent*>(&copy));
 }
 
 /** Handler for GDK motion events.
@@ -568,7 +675,7 @@ 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,
@@ -579,7 +686,7 @@ GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
                return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (&copy));
        }
 
-       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 +694,23 @@ GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
           recompute the list in deliver_event.
        */
 
-       return deliver_event (point, reinterpret_cast<GdkEvent*> (&copy));
+       return deliver_event (reinterpret_cast<GdkEvent*> (&copy));
 }
 
 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 +720,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<Rect> 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.