streamline button press event handling code a little, and tweak enter/leave debugging...
[ardour.git] / libs / canvas / canvas.cc
index 207011409e1cf65061746e52b2efd5b0a73be806..2675fab85d22ddf23af61d9a274c5396dfa9548d 100644 (file)
@@ -24,8 +24,8 @@
 
 #include <cassert>
 #include <gtkmm/adjustment.h>
+#include <gtkmm/label.h>
 
-#include "pbd/xml++.h"
 #include "pbd/compose.h"
 #include "pbd/stacktrace.h"
 
@@ -38,36 +38,17 @@ using namespace ArdourCanvas;
 /** Construct a new Canvas */
 Canvas::Canvas ()
        : _root (this)
-       , _log_renders (true)
+       , _scroll_offset_x (0)
+       , _scroll_offset_y (0)
 {
        set_epoch ();
 }
 
-/** Construct a new Canvas from an XML tree
- *  @param tree XML Tree.
- */
-Canvas::Canvas (XMLTree const * tree)
-       : _root (this)
-       , _log_renders (true)
+void
+Canvas::scroll_to (Coord x, Coord y)
 {
-       set_epoch ();
-       
-       /* XXX: little bit hacky */
-       _root.set_state (tree->root()->child ("Group"));
-
-       XMLNodeList const & children = tree->root()->children ();
-       for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
-               if ((*i)->name() == ("Render")) {
-                       _renders.push_back (
-                               Rect (
-                                       atof ((*i)->property ("x0")->value().c_str()),
-                                       atof ((*i)->property ("y0")->value().c_str()),
-                                       atof ((*i)->property ("x1")->value().c_str()),
-                                       atof ((*i)->property ("x1")->value().c_str())
-                                       )
-                               );
-               }
-       }
+       _scroll_offset_x = x;
+       _scroll_offset_y = y;
 }
 
 /** Render an area of the canvas.
@@ -79,45 +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");
-               _root.render (*draw, context);
-       }
 
-       if (_log_renders) {
-               _renders.push_back (area);
+               _root.render (*draw, context);
        }
 
-       context->restore ();
-
-       checkpoint ("render", "<- render");
+#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&
@@ -170,6 +143,19 @@ Canvas::item_shown_or_hidden (Item* item)
        }
 }
 
+/** Called when an item has a change to its visual properties
+ *  that do NOT affect its bounding box.
+ *  @param item Item that has been modified.
+ */
+void
+Canvas::item_visual_property_changed (Item* item)
+{
+       boost::optional<Rect> bbox = item->bounding_box ();
+       if (bbox) {
+               queue_draw_item_area (item, bbox.get ());
+       }
+}
+
 /** Called when an item has changed, but not moved.
  *  @param item Item that has changed.
  *  @param pre_change_bounding_box The bounding box of item before the change,
@@ -190,6 +176,30 @@ Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
        }
 }
 
+Duple
+Canvas::window_to_canvas (Duple const & d) const
+{
+       return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
+}
+
+Duple
+Canvas::canvas_to_window (Duple const & d) const
+{
+       return d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
+}      
+
+Rect
+Canvas::window_to_canvas (Rect const & r) const
+{
+       return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
+}
+
+Rect
+Canvas::canvas_to_window (Rect const & r) const
+{
+       return r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
+}      
+
 /** Called when an item has moved.
  *  @param item Item that has moved.
  *  @param pre_change_parent_bounding_box The bounding box of the item before
@@ -199,10 +209,15 @@ void
 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
 {
        if (pre_change_parent_bounding_box) {
-               /* request a redraw of where the item used to be; we have to use the
-                  parent's coordinates here as item bounding boxes do not change
-                  when the item moves.
-               */
+               /* request a redraw of where the item used to be. The box has
+                * 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 ());
        }
 
@@ -220,28 +235,9 @@ Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding
 void
 Canvas::queue_draw_item_area (Item* item, Rect area)
 {
-       request_redraw (item->item_to_canvas (area));
-}
-
-/** @return An XML description of the canvas and its objects */
-XMLTree *
-Canvas::get_state () const
-{
-       XMLTree* tree = new XMLTree ();
-       XMLNode* node = new XMLNode ("Canvas");
-       node->add_child_nocopy (*_root.get_state ());
-
-       for (list<Rect>::const_iterator i = _renders.begin(); i != _renders.end(); ++i) {
-               XMLNode* render = new XMLNode ("Render");
-               render->add_property ("x0", string_compose ("%1", i->x0));
-               render->add_property ("y0", string_compose ("%1", i->y0));
-               render->add_property ("x1", string_compose ("%1", i->x1));
-               render->add_property ("y1", string_compose ("%1", i->y1));
-               node->add_child_nocopy (*render);
-       }
-
-       tree->set_root (node);
-       return tree;
+       ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
+       // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
+       request_redraw (canvas_area);
 }
 
 /** Construct a GtkCanvas */
@@ -253,29 +249,6 @@ GtkCanvas::GtkCanvas ()
        add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
 }
 
-/** Construct a GtkCanvas from an XML tree.
- *  @param tree XML Tree.
- */
-GtkCanvas::GtkCanvas (XMLTree const * tree)
-       : Canvas (tree)
-       , _current_item (0)
-       , _grabbed_item (0)
-{
-       /* these are the events we want to know about */
-       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.
@@ -283,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.
@@ -292,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
@@ -337,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",
@@ -405,57 +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;
        }
-}
-
-/** Construct an ImageCanvas.
- *  @param size Size in pixels.
- */
-ImageCanvas::ImageCanvas (Duple size)
-       : _surface (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, size.x, size.y))
-{
-       _context = Cairo::Context::create (_surface);
-}
-
-/** Construct an ImageCanvas from an XML tree.
- *  @param tree XML Tree.
- *  @param size Size in pixels.
- */
-ImageCanvas::ImageCanvas (XMLTree const * tree, Duple size)
-       : Canvas (tree)
-       , _surface (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, size.x, size.y))
-{
-       _context = Cairo::Context::create (_surface);
-}
-
-/** Render the canvas to our pixbuf.
- *  @param area Area to render, in canvas coordinates.
- */
-void
-ImageCanvas::render_to_image (Rect const & area) const
-{
-       render (area, _context);
-}
-
-/** Write our pixbuf to a PNG file.
- *  @param f PNG file name.
- */
-void
-ImageCanvas::write_to_png (string const & f)
-{
-       assert (_surface);
-       _surface->write_to_png (f);
-}
 
-/** @return Our Cairo context */
-Cairo::RefPtr<Cairo::Context>
-ImageCanvas::context ()
-{
-       return _context;
+       enter_leave_items (0); // no mouse state
+       
 }
 
 /** Handler for GDK expose events.
@@ -466,7 +480,22 @@ bool
 GtkCanvas::on_expose_event (GdkEventExpose* ev)
 {
        Cairo::RefPtr<Cairo::Context> 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);
+
+#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;
 }
 
@@ -489,10 +518,16 @@ GtkCanvas::context ()
 bool
 GtkCanvas::on_button_press_event (GdkEventButton* ev)
 {
+       /* translate event coordinates from window to canvas */
+
+       Duple where = window_to_canvas (Duple (ev->x, ev->y));
+                                
        /* Coordinates in the event will be canvas coordinates, correctly adjusted
           for scroll if this GtkCanvas is in a GtkCanvasViewport.
        */
-       return button_handler (ev);
+
+       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.
@@ -501,11 +536,17 @@ GtkCanvas::on_button_press_event (GdkEventButton* ev)
  */
 bool
 GtkCanvas::on_button_release_event (GdkEventButton* ev)
-{
+{      
+       /* translate event coordinates from window to canvas */
+
+       Duple where = window_to_canvas (Duple (ev->x, ev->y));
+
        /* Coordinates in the event will be canvas coordinates, correctly adjusted
           for scroll if this GtkCanvas is in a GtkCanvasViewport.
        */
-       return button_handler (ev);
+
+       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.
@@ -515,18 +556,28 @@ GtkCanvas::on_button_release_event (GdkEventButton* ev)
 bool
 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
 {
+       /* translate event coordinates from window to canvas */
+
+       GdkEvent copy = *((GdkEvent*)ev);
+       Duple where = window_to_canvas (Duple (ev->x, ev->y));
+
+       copy.motion.x = where.x;
+       copy.motion.y = where.y;
+
        /* Coordinates in the event will be canvas coordinates, correctly adjusted
           for scroll if this GtkCanvas is in a GtkCanvasViewport.
        */
-       return motion_notify_handler (ev);
+       return motion_notify_handler ((GdkEventMotion*) &copy);
 }
 
 /** Called to request a redraw of our canvas.
  *  @param area Area to redraw, in canvas coordinates.
  */
 void
-GtkCanvas::request_redraw (Rect const & area)
+GtkCanvas::request_redraw (Rect const & request)
 {
+       Rect area = canvas_to_window (request);
+       // 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));
 }
 
@@ -569,14 +620,35 @@ GtkCanvas::ungrab ()
        _grabbed_item = 0;
 }
 
+/** @return The visible area of the canvas, in canvas coordinates */
+Rect
+GtkCanvas::visible_area () const
+{
+       Distance const xo = _scroll_offset_x;
+       Distance const yo = _scroll_offset_y;
+       return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
+}
+
 /** Create a GtkCanvaSViewport.
  *  @param hadj Adjustment to use for horizontal scrolling.
  *  @param vadj Adjustment to use for vertica scrolling.
  */
 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
-       : Viewport (hadj, vadj)
+       : Alignment (0, 0, 1.0, 1.0)
+       , hadjustment (hadj)
+       , vadjustment (vadj)
 {
        add (_canvas);
+
+       hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
+       vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
+}
+
+void
+GtkCanvasViewport::scrolled ()
+{
+       _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
+       queue_draw ();
 }
 
 /** Handler for when GTK asks us what minimum size we want.
@@ -585,29 +657,10 @@ GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& va
 void
 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
 {
+       /* force the canvas to size itself */
+       // _canvas.root()->bounding_box(); 
+
        req->width = 16;
        req->height = 16;
 }
 
-/** Convert window coordinates to canvas coordinates by taking into account
- *  where we are scrolled to.
- *  @param wx Window x.
- *  @param wy Window y.
- *  @param cx Filled in with canvas x.
- *  @param cy Filled in with canvas y.
- */
-void
-GtkCanvasViewport::window_to_canvas (int wx, int wy, Coord& cx, Coord& cy) const
-{
-       cx = wx + get_hadjustment()->get_value ();
-       cy = wy + get_vadjustment()->get_value ();
-}
-
-/** @return The visible area of the canvas, in canvas coordinates */
-Rect
-GtkCanvasViewport::visible_area () const
-{
-       Distance const xo = get_hadjustment()->get_value ();
-       Distance const yo = get_vadjustment()->get_value ();
-       return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
-}