a better, more general fix for the previous Canvas::item_going_away() issue. There...
[ardour.git] / libs / canvas / canvas.cc
index dab8ce6f687bcc336591a627ab4708275accb046..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;
@@ -49,6 +51,14 @@ Canvas::scroll_to (Coord x, Coord y)
 {
        _scroll_offset_x = x;
        _scroll_offset_y = y;
+
+       pick_current_item (0); // no current mouse position 
+}
+
+void
+Canvas::zoomed ()
+{
+       pick_current_item (0); // no current mouse position
 }
 
 /** Render an area of the canvas.
@@ -60,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";
@@ -77,12 +87,20 @@ Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context
 
        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.
                */
 
                _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 ();
+
        }
+
 }
 
 ostream&
@@ -177,7 +195,14 @@ Canvas::window_to_canvas (Duple const & d) const
 Duple
 Canvas::canvas_to_window (Duple const & d) const
 {
-       return d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
+       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;
 }      
 
 Rect
@@ -189,7 +214,16 @@ Canvas::window_to_canvas (Rect const & r) const
 Rect
 Canvas::canvas_to_window (Rect const & r) const
 {
-       return r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
+       Rect wr = r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
+
+       /* 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;
 }      
 
 /** Called when an item has moved.
@@ -205,11 +239,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 ());
        }
 
@@ -228,65 +262,29 @@ 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 ()
        : _current_item (0)
+       , _new_current_item (0)
        , _grabbed_item (0)
+       , _focused_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.
- */
-bool
-GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
-{
-       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<GdkEvent*> (ev));
-       }
-
-       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));
+       add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK |
+                   Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
 }
 
 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.
         */
@@ -297,24 +295,75 @@ 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 */
+
+       if (_grabbed_item) {
+               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;
@@ -322,58 +371,141 @@ GtkCanvas::enter_leave_items (Duple const & point, int state)
 
        GdkEventCrossing leave_event = enter_event;
        leave_event.type = GDK_LEAVE_NOTIFY;
-       leave_event.detail = GDK_NOTIFY_ANCESTOR;
-       leave_event.subwindow = 0;
 
-       if (items.empty()) {
+       Item* i;
+       GdkNotifyType enter_detail;
+       GdkNotifyType leave_detail;
+       vector<Item*> items_to_leave_virtual;
+       vector<Item*> items_to_enter_virtual;
+
+       if (_new_current_item == 0) {
+
+               leave_detail = GDK_NOTIFY_UNKNOWN;
+
                if (_current_item) {
-                       /* leave event */
-                       _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
-                       _current_item = 0;
+
+                       /* no current item, so also send virtual leave events to the
+                        * entire heirarchy for the current item
+                        */
+
+                       for (i = _current_item->parent(); i ; i = i->parent()) {
+                               items_to_leave_virtual.push_back (i);
+                       }
                }
-               return;
-       }
 
-       /* 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
-        */
-       
-       for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
+       } else if (_current_item == 0) {
 
-               Item const *  new_item = *i;
+               enter_detail = GDK_NOTIFY_UNKNOWN;
 
-               if (new_item->ignore_events()) {
-                       continue;
+               /* 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);
                }
 
-               if (_current_item == new_item) {
-                       break;
+       } else if (_current_item->is_descendant_of (*_new_current_item)) {
+
+               /* 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.
+                */
+               
+
+               for (i = _current_item->parent(); i && i != _new_current_item; i = i->parent()) {
+                       items_to_leave_virtual.push_back (i);
                }
 
-               if (_current_item) {
-                       /* leave event */
-                       _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
+               enter_detail = GDK_NOTIFY_INFERIOR;
+               leave_detail = GDK_NOTIFY_ANCESTOR;
+
+
+       } 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);
                }
 
-               if (new_item && _current_item != new_item) {
-                       /* enter event */
-                       _current_item = new_item;
-                       _current_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
-                       break;
+               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;
+       }
        
+
+       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;
+
+       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;
+               }
        }
+
+       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;
+               }
+       }
+
+       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 */
+
        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",
@@ -381,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;
+       }
 
-       DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 possible items to deliver event to\n", items.size()));
+       /* 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<Item const *>::const_reverse_iterator i = items.rbegin ();
-       while (i != items.rend()) {
+       Item* item = const_cast<Item*> (_current_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;
 }
 
@@ -440,15 +558,24 @@ GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
                queue_draw_item_area (item, bounding_box.get ());
        }
        
-       if (_current_item == item) {
-               _current_item = 0;
+       if (_new_current_item == item) {
+               _new_current_item = 0;
        }
 
        if (_grabbed_item == item) {
                _grabbed_item = 0;
        }
 
-       enter_leave_items (0); // no mouse state
+       if (_focused_item == item) {
+               _focused_item = 0;
+       }
+
+       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
+       }
        
 }
 
@@ -459,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;
 }
@@ -492,11 +620,14 @@ GtkCanvas::on_button_press_event (GdkEventButton* ev)
 
        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);
+
+       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 (reinterpret_cast<GdkEvent*>(&copy));
 }
 
 /** Handler for GDK button release events.
@@ -510,6 +641,8 @@ GtkCanvas::on_button_release_event (GdkEventButton* ev)
 
        GdkEvent copy = *((GdkEvent*)ev);
        Duple where = window_to_canvas (Duple (ev->x, ev->y));
+       
+       pick_current_item (where, ev->state);
 
        copy.button.x = where.x;
        copy.button.y = where.y;
@@ -517,7 +650,10 @@ GtkCanvas::on_button_release_event (GdkEventButton* ev)
        /* Coordinates in the event will be canvas coordinates, correctly adjusted
           for scroll if this GtkCanvas is in a GtkCanvasViewport.
        */
-       return button_handler ((GdkEventButton*) &copy);
+
+       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 (reinterpret_cast<GdkEvent*>(&copy));
 }
 
 /** Handler for GDK motion events.
@@ -530,15 +666,52 @@ 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));
+       Duple point (ev->x, ev->y);
+       Duple where = window_to_canvas (point);
 
        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.
+       /* Coordinates in "copy" will be canvas coordinates, 
+       */
+
+       // 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<GdkEvent*> (&copy));
+       }
+
+       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
+          events may have deleted canvas items so it is important to
+          recompute the list in deliver_event.
        */
-       return motion_notify_handler ((GdkEventMotion*) &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));
+       pick_current_item (where, ev->state);
+       return true;
+}
+
+bool
+GtkCanvas::on_leave_notify_event (GdkEventCrossing* ev)
+{
+       _new_current_item = 0;
+       Duple where = window_to_canvas (Duple (ev->x, ev->y));
+       deliver_enter_leave (where, ev->state);
+       return true;
 }
 
 /** Called to request a redraw of our canvas.
@@ -547,9 +720,13 @@ GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
 void
 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));
+       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.
@@ -583,6 +760,7 @@ GtkCanvas::grab (Item* item)
        _grabbed_item = item;
 }
 
+
 /** `Ungrab' any item that was previously grabbed */
 void
 GtkCanvas::ungrab ()
@@ -591,6 +769,24 @@ GtkCanvas::ungrab ()
        _grabbed_item = 0;
 }
 
+/** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
+ *  moves elsewhere.
+ *  @param item Item to grab.
+ */
+void
+GtkCanvas::focus (Item* item)
+{
+       _focused_item = item;
+}
+
+void
+GtkCanvas::unfocus (Item* item)
+{
+       if (item == _focused_item) {
+               _focused_item = 0;
+       }
+}
+
 /** @return The visible area of the canvas, in canvas coordinates */
 Rect
 GtkCanvas::visible_area () const