streamline button press event handling code a little, and tweak enter/leave debugging...
[ardour.git] / libs / canvas / canvas.cc
index 9c49eb4a87594324b6041c0671d4be689f7aa26b..2675fab85d22ddf23af61d9a274c5396dfa9548d 100644 (file)
@@ -80,8 +80,17 @@ Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context
                /* there's a common area between the root and the requested
                   area, so render it.
                */
+
                _root.render (*draw, context);
        }
+
+#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
 }
 
 ostream&
@@ -204,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 ());
        }
 
@@ -227,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);
 }
 
@@ -240,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.
@@ -258,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.
@@ -327,6 +327,7 @@ GtkCanvas::enter_leave_items (Duple const & point, int state)
        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;
                }
@@ -337,31 +338,48 @@ GtkCanvas::enter_leave_items (Duple const & point, int state)
         * 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";
 
-               Item const *  new_item = *i;
+       for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
 
+               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) {
-                       break;
+                       // 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";
        }
 }
 
@@ -373,6 +391,8 @@ GtkCanvas::enter_leave_items (Duple const & point, int state)
 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",
@@ -441,6 +461,7 @@ GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
        
        if (_current_item == item) {
                _current_item = 0;
+               queue_draw ();
        }
 
        if (_grabbed_item == item) {
@@ -462,6 +483,19 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
 
        render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
 
+#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
+
+
        return true;
 }
 
@@ -486,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.
@@ -507,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.
@@ -547,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));
 }