streamline button press event handling code a little, and tweak enter/leave debugging...
[ardour.git] / libs / canvas / canvas.cc
index ff624be8fbd7cf80ee6738f0ef341ed5ac567f0b..2675fab85d22ddf23af61d9a274c5396dfa9548d 100644 (file)
@@ -38,7 +38,6 @@ using namespace ArdourCanvas;
 /** Construct a new Canvas */
 Canvas::Canvas ()
        : _root (this)
-       , _log_renders (true)
        , _scroll_offset_x (0)
        , _scroll_offset_y (0)
 {
@@ -61,58 +60,37 @@ Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context
 {
 #ifdef CANVAS_DEBUG
        if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
-               cerr << "CANVAS @ " << this << endl;
-               dump (cerr);
-               cerr << "-------------------------\n";
+               cerr << "RENDER: " << area << endl;
+               //cerr << "CANVAS @ " << this << endl;
+               //dump (cerr);
+               //cerr << "-------------------------\n";
        }
 #endif
 
-       // checkpoint ("render", "-> render");
        render_count = 0;
        
-       context->save ();
-
-       /* clip to the requested area */
-       context->rectangle (area.x0, area.y0, area.width(), area.height());
-       context->clip ();
-
        boost::optional<Rect> root_bbox = _root.bounding_box();
        if (!root_bbox) {
                /* the root has no bounding box, so there's nothing to render */
-               // checkpoint ("render", "no root bbox");
-               context->restore ();
                return;
        }
 
-       boost::optional<Rect> draw = root_bbox.get().intersection (area);
+       boost::optional<Rect> draw = root_bbox->intersection (area);
        if (draw) {
                /* there's a common area between the root and the requested
                   area, so render it.
                */
-               // checkpoint ("render", "... root");
-               context->stroke ();
 
                _root.render (*draw, context);
        }
 
-       if (_log_renders) {
-               _renders.push_back (area);
-       }
-
-       context->restore ();
-
-#ifdef CANVAS_DEBUG
-       if (getenv ("ARDOUR_HARLEQUIN_CANVAS")) {
-               /* light up the canvas to show redraws */
-               context->set_source_rgba (random()%255 / 255.0,
-                                         random()%255 / 255.0,
-                                         random()%255 / 255.0,
-                                         255);
-               context->rectangle (area.x0, area.y0, area.width(), area.height());
-               context->fill ();
-       }
+#if 0
+       /* debug render area */
+       Rect r = _root.item_to_window (area);
+       context->rectangle (r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0);
+       context->set_source_rgba (1.0, 0.0, 0.0, 1.0);
+       context->stroke ();
 #endif
-       // checkpoint ("render", "<- render");
 }
 
 ostream&
@@ -235,11 +213,11 @@ Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding
                 * to be in parent coordinate space since the bounding box of
                 * an item does not change when moved. If we use
                 * item->item_to_canvas() on the old bounding box, we will be
+
                 * using the item's new position, and so will compute the wrong
                 * invalidation area. If we use the parent (which has not
                 * moved, then this will work.
                 */
-
                queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
        }
 
@@ -258,7 +236,7 @@ void
 Canvas::queue_draw_item_area (Item* item, Rect area)
 {
        ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
-       // cerr << "CANVAS Invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
+       // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
        request_redraw (canvas_area);
 }
 
@@ -271,17 +249,6 @@ GtkCanvas::GtkCanvas ()
        add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
 }
 
-/** Handler for button presses on the canvas.
- *  @param ev GDK event.
- */
-bool
-GtkCanvas::button_handler (GdkEventButton* ev)
-{
-       DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button %3 %1 %1\n", ev->x, ev->y, (ev->type == GDK_BUTTON_PRESS ? "press" : "release")));
-       /* The Duple that we are passing in here is in canvas coordinates */
-       return deliver_event (Duple (ev->x, ev->y), reinterpret_cast<GdkEvent*> (ev));
-}
-
 /** Handler for pointer motion events on the canvas.
  *  @param ev GDK event.
  *  @return true if the motion event was handled, otherwise false.
@@ -289,6 +256,8 @@ GtkCanvas::button_handler (GdkEventButton* ev)
 bool
 GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
 {
+       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.
@@ -298,41 +267,120 @@ GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
                return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (ev));
        }
 
-       /* This is in canvas coordinates */
        Duple point (ev->x, ev->y);
+       
+       enter_leave_items (point, 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
+          events may have deleted canvas items so it is important to
+          recompute the list in deliver_event.
+       */
+       return deliver_event (point, reinterpret_cast<GdkEvent*> (ev));
+}
+
+void
+GtkCanvas::enter_leave_items (int state)
+{
+       int x;
+       int y;
+
+       /* this version of ::enter_leave_items() 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.
+        */
+            
+       Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
+
+       if (pointer_window != get_window()) {
+               return;
+       }
+
+       enter_leave_items (window_to_canvas (Duple (x, y)), state);
+}
+               
+void
+GtkCanvas::enter_leave_items (Duple const & point, int state)
+{
+       /* find the items at the given position */
 
-       /* find the items at the new mouse position */
        vector<Item const *> items;
        _root.add_items_at_point (point, items);
 
-       Item const * new_item = items.empty() ? 0 : items.back ();
-
-       if (_current_item && _current_item != new_item) {
-               /* leave event */
-               GdkEventCrossing leave_event;
-               leave_event.type = GDK_LEAVE_NOTIFY;
-               leave_event.x = ev->x;
-               leave_event.y = ev->y;
-               _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
+       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;
+
+       GdkEventCrossing leave_event = enter_event;
+       leave_event.type = GDK_LEAVE_NOTIFY;
+       leave_event.detail = GDK_NOTIFY_ANCESTOR;
+       leave_event.subwindow = 0;
+
+       if (items.empty()) {
+               if (_current_item) {
+                       /* leave event */
+                       cerr << "E/L: left item " << _current_item->whatami() << '/' << _current_item->name << " for ... nada" << endl;
+                       _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
+                       _current_item = 0;
+               }
+               return;
        }
 
-       if (new_item && _current_item != new_item) {
-               /* enter event */
-               GdkEventCrossing enter_event;
-               enter_event.type = GDK_ENTER_NOTIFY;
-               enter_event.x = ev->x;
-               enter_event.y = ev->y;
-               new_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
+       /* items is sorted from top to bottom, so reverse through it from bottom
+        * to top to find the lowest, first event-sensitive item and notify that
+        * we have entered it
+        */
+
+       cerr << "E/L: " << items.size() << " to check at " << point << endl;
+#ifdef CANVAS_DEBUG
+       for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
+               cerr << '\t' << (*i)->whatami() << ' ' << (*i)->name << " ignore ? " << (*i)->ignore_events() << " current ? " << (_current_item == (*i)) << endl;
        }
+#endif
+       cerr << "------------\n";
 
-       _current_item = new_item;
+       for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
 
-       /* Now deliver the motion event.  It may seem a little inefficient
-          to recompute the items under the event, but the enter notify/leave
-          events may have deleted canvas items so it is important to
-          recompute the list in deliver_event.
-       */
-       return deliver_event (point, reinterpret_cast<GdkEvent*> (ev));
+               Item const *  new_item = *i;
+#ifdef CANVAS_DEBUG
+               cerr << "\tE/L check out " << new_item->whatami() << ' ' << new_item->name << " ignore ? " << new_item->ignore_events() << " current ? " << (_current_item == new_item) << endl;
+#endif
+               if (new_item->ignore_events()) {
+                       // cerr << "continue1\n";
+                       continue;
+               }
+
+               if (_current_item == new_item) {
+                       // cerr << "continue2\n";
+                       continue;
+               }
+
+               if (_current_item) {
+                       /* leave event */
+                       DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Leave %1 %2\n", _current_item->whatami(), _current_item->name));
+                       _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
+                       queue_draw ();
+               }
+
+               if (new_item && _current_item != new_item) {
+                       /* enter event */
+                       _current_item = new_item;
+                       DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Enter %1 %2\n", _current_item->whatami(), _current_item->name));
+                       _current_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
+                       queue_draw ();
+                       break;
+               }
+
+               // cerr << "Loop around again\n";
+       }
 }
 
 /** Deliver an event to the appropriate item; either the grabbed item, or
@@ -343,6 +391,8 @@ GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
 bool
 GtkCanvas::deliver_event (Duple point, GdkEvent* event)
 {
+       /* Point in in canvas coordinate space */
+
        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",
@@ -411,11 +461,15 @@ GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
        
        if (_current_item == item) {
                _current_item = 0;
+               queue_draw ();
        }
 
        if (_grabbed_item == item) {
                _grabbed_item = 0;
        }
+
+       enter_leave_items (0); // no mouse state
+       
 }
 
 /** Handler for GDK expose events.
@@ -425,29 +479,22 @@ 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 ();
-       
-       /* WINDOW  CANVAS
-        * 0,0     _scroll_offset_x, _scroll_offset_y
-        */
-
-       /* render using canvas coordinates */
-
-       Rect canvas_area (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height);
-       canvas_area  = canvas_area.translate (Duple (_scroll_offset_x, _scroll_offset_y));
 
-       /* things are going to render to the cairo surface with canvas
-        * coordinates:
-        *
-        *  an item at window/cairo 0,0 will have canvas_coords _scroll_offset_x,_scroll_offset_y
-        *
-        * let them render at their natural coordinates by using cairo_translate()
-        */
+       render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
 
-       c->translate (-_scroll_offset_x, -_scroll_offset_y);
+#if 1
+       if (_current_item) {
+               boost::optional<Rect> orect = _current_item->bounding_box();
+               if (orect) {
+                       Rect r = _current_item->item_to_window (orect.get());
+                       c->rectangle (r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0);
+                       c->set_source_rgba (1.0, 0.0, 0.0, 1.0);
+                       c->stroke ();
+               }
+       }
+#endif
 
-       render (canvas_area, c);
 
        return true;
 }
@@ -473,16 +520,14 @@ GtkCanvas::on_button_press_event (GdkEventButton* ev)
 {
        /* translate event coordinates from window to canvas */
 
-       GdkEvent copy = *((GdkEvent*)ev);
        Duple where = window_to_canvas (Duple (ev->x, ev->y));
-
-       copy.button.x = where.x;
-       copy.button.y = where.y;
                                 
        /* Coordinates in the event will be canvas coordinates, correctly adjusted
           for scroll if this GtkCanvas is in a GtkCanvasViewport.
        */
-       return button_handler ((GdkEventButton*) &copy);
+
+       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*>(ev));
 }
 
 /** Handler for GDK button release events.
@@ -494,16 +539,14 @@ GtkCanvas::on_button_release_event (GdkEventButton* ev)
 {      
        /* translate event coordinates from window to canvas */
 
-       GdkEvent copy = *((GdkEvent*)ev);
        Duple where = window_to_canvas (Duple (ev->x, ev->y));
 
-       copy.button.x = where.x;
-       copy.button.y = where.y;
-
        /* Coordinates in the event will be canvas coordinates, correctly adjusted
           for scroll if this GtkCanvas is in a GtkCanvasViewport.
        */
-       return button_handler ((GdkEventButton*) &copy);
+
+       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*>(ev));
 }
 
 /** Handler for GDK motion events.
@@ -534,7 +577,7 @@ void
 GtkCanvas::request_redraw (Rect const & request)
 {
        Rect area = canvas_to_window (request);
-       // cerr << "Invalidate " << request << " TRANSLATE AS " << area << endl;
+       // cerr << this << " Invalidate " << request << " TRANSLATE AS " << area << endl;
        queue_draw_area (floor (area.x0), floor (area.y0), ceil (area.x1) - floor (area.x0), ceil (area.y1) - floor (area.y0));
 }