ignore invisible items when picking the current item
[ardour.git] / libs / canvas / canvas.cc
1 /*
2     Copyright (C) 2011 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 /** @file  canvas/canvas.cc
22  *  @brief Implementation of the main canvas classes.
23  */
24
25 #include <list>
26 #include <cassert>
27 #include <gtkmm/adjustment.h>
28 #include <gtkmm/label.h>
29
30 #include "pbd/compose.h"
31 #include "pbd/stacktrace.h"
32
33 #include "canvas/canvas.h"
34 #include "canvas/debug.h"
35
36 using namespace std;
37 using namespace ArdourCanvas;
38
39 /** Construct a new Canvas */
40 Canvas::Canvas ()
41         : _root (this)
42         , _scroll_offset_x (0)
43         , _scroll_offset_y (0)
44 {
45         set_epoch ();
46 }
47
48 void
49 Canvas::scroll_to (Coord x, Coord y)
50 {
51         _scroll_offset_x = x;
52         _scroll_offset_y = y;
53
54         pick_current_item (0); // no current mouse position 
55 }
56
57 void
58 Canvas::zoomed ()
59 {
60         pick_current_item (0); // no current mouse position
61 }
62
63 /** Render an area of the canvas.
64  *  @param area Area in canvas coordinates.
65  *  @param context Cairo context to render to.
66  */
67 void
68 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
69 {
70 #ifdef CANVAS_DEBUG
71         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
72                 cerr << "RENDER: " << area << endl;
73                 //cerr << "CANVAS @ " << this << endl;
74                 //dump (cerr);
75                 //cerr << "-------------------------\n";
76         }
77 #endif
78
79         render_count = 0;
80         
81         boost::optional<Rect> root_bbox = _root.bounding_box();
82         if (!root_bbox) {
83                 /* the root has no bounding box, so there's nothing to render */
84                 return;
85         }
86
87         boost::optional<Rect> draw = root_bbox->intersection (area);
88         if (draw) {
89                 
90                 /* there's a common area between the root and the requested
91                    area, so render it.
92                 */
93
94                 _root.render (*draw, context);
95
96                 // This outlines the rect being rendered, after it has been drawn.
97                 // context->rectangle (draw->x0, draw->y0, draw->x1 - draw->x0, draw->y1 - draw->y0);
98                 // context->set_source_rgba (1.0, 0, 0, 1.0);
99                 // context->stroke ();
100
101         }
102
103 }
104
105 ostream&
106 operator<< (ostream& o, Canvas& c)
107 {
108         c.dump (o);
109         return o;
110 }
111
112 std::string
113 Canvas::indent() const
114
115         string s;
116
117         for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
118                 s += '\t';
119         }
120
121         return s;
122 }
123
124 std::string
125 Canvas::render_indent() const
126
127         string s;
128
129         for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
130                 s += ' ';
131         }
132
133         return s;
134 }
135
136 void
137 Canvas::dump (ostream& o) const
138 {
139         dump_depth = 0;
140         _root.dump (o);
141 }       
142
143 /** Called when an item has been shown or hidden.
144  *  @param item Item that has been shown or hidden.
145  */
146 void
147 Canvas::item_shown_or_hidden (Item* item)
148 {
149         boost::optional<Rect> bbox = item->bounding_box ();
150         if (bbox) {
151                 queue_draw_item_area (item, bbox.get ());
152         }
153 }
154
155 /** Called when an item has a change to its visual properties
156  *  that do NOT affect its bounding box.
157  *  @param item Item that has been modified.
158  */
159 void
160 Canvas::item_visual_property_changed (Item* item)
161 {
162         boost::optional<Rect> bbox = item->bounding_box ();
163         if (bbox) {
164                 queue_draw_item_area (item, bbox.get ());
165         }
166 }
167
168 /** Called when an item has changed, but not moved.
169  *  @param item Item that has changed.
170  *  @param pre_change_bounding_box The bounding box of item before the change,
171  *  in the item's coordinates.
172  */
173 void
174 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
175 {
176         if (pre_change_bounding_box) {
177                 /* request a redraw of the item's old bounding box */
178                 queue_draw_item_area (item, pre_change_bounding_box.get ());
179         }
180
181         boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
182         if (post_change_bounding_box) {
183                 /* request a redraw of the item's new bounding box */
184                 queue_draw_item_area (item, post_change_bounding_box.get ());
185         }
186 }
187
188 Duple
189 Canvas::window_to_canvas (Duple const & d) const
190 {
191         return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
192 }
193
194 Duple
195 Canvas::canvas_to_window (Duple const & d) const
196 {
197         Duple wd = d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
198         wd.x = round (wd.x);
199         wd.y = round (wd.y);
200         return wd;
201 }       
202
203 Rect
204 Canvas::window_to_canvas (Rect const & r) const
205 {
206         return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
207 }
208
209 Rect
210 Canvas::canvas_to_window (Rect const & r) const
211 {
212         Rect wr = r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
213         wr.x0 = floor (wr.x0);
214         wr.x1 = ceil (wr.x1);
215         wr.y0 = floor (wr.y0);
216         wr.y1 = ceil (wr.y1);
217         return wr;
218 }       
219
220 /** Called when an item has moved.
221  *  @param item Item that has moved.
222  *  @param pre_change_parent_bounding_box The bounding box of the item before
223  *  the move, in its parent's coordinates.
224  */
225 void
226 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
227 {
228         if (pre_change_parent_bounding_box) {
229                 /* request a redraw of where the item used to be. The box has
230                  * to be in parent coordinate space since the bounding box of
231                  * an item does not change when moved. If we use
232                  * item->item_to_canvas() on the old bounding box, we will be
233
234                  * using the item's new position, and so will compute the wrong
235                  * invalidation area. If we use the parent (which has not
236                  * moved, then this will work.
237                  */
238                 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
239         }
240
241         boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
242         if (post_change_bounding_box) {
243                 /* request a redraw of where the item now is */
244                 queue_draw_item_area (item, post_change_bounding_box.get ());
245         }
246 }
247
248 /** Request a redraw of a particular area in an item's coordinates.
249  *  @param item Item.
250  *  @param area Area to redraw in the item's coordinates.
251  */
252 void
253 Canvas::queue_draw_item_area (Item* item, Rect area)
254 {
255         ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
256         // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
257         request_redraw (canvas_area);
258 }
259
260 /** Construct a GtkCanvas */
261 GtkCanvas::GtkCanvas ()
262         : _current_item (0)
263         , _new_current_item (0)
264         , _grabbed_item (0)
265         , _focused_item (0)
266 {
267         /* these are the events we want to know about */
268         add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK |
269                     Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
270 }
271
272 void
273 GtkCanvas::pick_current_item (int state)
274 {
275         int x;
276         int y;
277
278         /* this version of ::pick_current_item() is called after an item is
279          * added or removed, so we have no coordinates to work from as is the
280          * case with a motion event. Find out where the mouse is and use that.
281          */
282              
283         Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
284
285         if (pointer_window != get_window()) {
286                 return;
287         }
288
289         pick_current_item (window_to_canvas (Duple (x, y)), state);
290 }
291                 
292 void
293 GtkCanvas::pick_current_item (Duple const & point, int state)
294 {
295         /* we do not enter/leave items during a drag/grab */
296
297         if (_grabbed_item) {
298                 return;
299         }
300
301         /* find the items at the given position */
302
303         vector<Item const *> items;
304         _root.add_items_at_point (point, items);
305
306         /* put all items at point that are event-sensitive and visible and NOT
307            groups into within_items. Note that items is sorted from bottom to
308            top, but we're going to reverse that for within_items so that its
309            first item is the upper-most item that can be chosen as _current_item.
310         */
311         
312         vector<Item const *>::const_iterator i;
313         list<Item const *> within_items;
314
315         for (i = items.begin(); i != items.end(); ++i) {
316
317                 Item const * new_item = *i;
318
319                 /* We ignore invisible items, groups and items that ignore events */
320
321                 if (!new_item->visible() || new_item->ignore_events() || dynamic_cast<Group const *>(new_item) != 0) {
322                         continue;
323                 }
324                 
325                 within_items.push_front (new_item);
326         }
327
328         if (within_items.empty()) {
329
330                 /* no items at point, just send leave event below */
331
332         } else {
333                 if (within_items.front() == _current_item) {
334                         /* uppermost item at point is already _current_item */
335                         return;
336                 }
337                 
338                 _new_current_item = const_cast<Item*> (within_items.front());
339         }
340
341         deliver_enter_leave (point, state);
342 }
343
344 void
345 GtkCanvas::deliver_enter_leave (Duple const & point, int state)
346 {
347         /* setup enter & leave event structures */
348
349         GdkEventCrossing enter_event;
350         enter_event.type = GDK_ENTER_NOTIFY;
351         enter_event.window = get_window()->gobj();
352         enter_event.send_event = 0;
353         enter_event.subwindow = 0;
354         enter_event.mode = GDK_CROSSING_NORMAL;
355         enter_event.focus = FALSE;
356         enter_event.state = state;
357         enter_event.x = point.x;
358         enter_event.y = point.y;
359
360         GdkEventCrossing leave_event = enter_event;
361         leave_event.type = GDK_LEAVE_NOTIFY;
362
363         Item* i;
364         GdkNotifyType enter_detail;
365         GdkNotifyType leave_detail;
366         vector<Item*> items_to_leave_virtual;
367         vector<Item*> items_to_enter_virtual;
368
369         if (_new_current_item == 0) {
370
371                 leave_detail = GDK_NOTIFY_UNKNOWN;
372
373                 if (_current_item) {
374
375                         /* no current item, so also send virtual leave events to the
376                          * entire heirarchy for the current item
377                          */
378
379                         for (i = _current_item->parent(); i ; i = i->parent()) {
380                                 items_to_leave_virtual.push_back (i);
381                         }
382                 }
383
384         } else if (_current_item == 0) {
385
386                 enter_detail = GDK_NOTIFY_UNKNOWN;
387
388                 /* no current item, so also send virtual enter events to the
389                  * entire heirarchy for the new item 
390                  */
391
392                 for (i = _new_current_item->parent(); i ; i = i->parent()) {
393                         items_to_enter_virtual.push_back (i);
394                 }
395
396         } else if (_current_item->is_descendant_of (*_new_current_item)) {
397
398                 /* move from descendant to ancestor (X: "_current_item is an
399                  * inferior of _new_current_item") 
400                  *
401                  * Deliver "virtual" leave notifications to all items in the
402                  * heirarchy between current and new_current.
403                  */
404                 
405
406                 for (i = _current_item->parent(); i && i != _new_current_item; i = i->parent()) {
407                         items_to_leave_virtual.push_back (i);
408                 }
409
410                 enter_detail = GDK_NOTIFY_INFERIOR;
411                 leave_detail = GDK_NOTIFY_ANCESTOR;
412
413
414         } else if (_new_current_item->is_descendant_of (*_current_item)) {
415                 /* move from ancestor to descendant (X: "_new_current_item is
416                  * an inferior of _current_item")
417                  *
418                  * Deliver "virtual" enter notifications to all items in the
419                  * heirarchy between current and new_current.
420                  */
421
422                 for (i = _new_current_item->parent(); i && i != _current_item; i = i->parent()) {
423                         items_to_enter_virtual.push_back (i);
424                 }
425
426                 enter_detail = GDK_NOTIFY_ANCESTOR;
427                 leave_detail = GDK_NOTIFY_INFERIOR;
428
429         } else {
430
431                 Item const * common_ancestor = _current_item->closest_ancestor_with (*_new_current_item);
432
433                 /* deliver virtual leave events to everything between _current
434                  * and common_ancestor.
435                  */
436
437                 for (i = _current_item->parent(); i && i != common_ancestor; i = i->parent()) {
438                         items_to_leave_virtual.push_back (i);
439                 }
440
441                 /* deliver virtual enter events to everything between
442                  * _new_current and common_ancestor.
443                  */
444
445                 for (i = _new_current_item->parent(); i && i != common_ancestor; i = i->parent()) {
446                         items_to_enter_virtual.push_back (i);
447                 }
448
449                 enter_detail = GDK_NOTIFY_NONLINEAR;
450                 leave_detail = GDK_NOTIFY_NONLINEAR;
451         }
452         
453
454         if (_current_item && !_current_item->ignore_events ()) {
455                 leave_event.detail = leave_detail;
456                 _current_item->Event ((GdkEvent*)&leave_event);
457                 // std::cerr << "LEAVE " << _current_item->whatami() << '/' << _current_item->name << std::endl;
458         }
459
460         leave_event.detail = GDK_NOTIFY_VIRTUAL;
461         enter_event.detail = GDK_NOTIFY_VIRTUAL;
462
463         for (vector<Item*>::iterator it = items_to_leave_virtual.begin(); it != items_to_leave_virtual.end(); ++it) {
464                 if (!(*it)->ignore_events()) {
465                         (*it)->Event ((GdkEvent*)&leave_event);
466                         // std::cerr << "leave " << (*it)->whatami() << '/' << (*it)->name << std::endl;
467                 }
468         }
469
470         for (vector<Item*>::iterator it = items_to_enter_virtual.begin(); it != items_to_enter_virtual.end(); ++it) {
471                 if (!(*it)->ignore_events()) {
472                         (*it)->Event ((GdkEvent*)&enter_event);
473                         // std::cerr << "enter " << (*it)->whatami() << '/' << (*it)->name << std::endl;
474                 }
475         }
476
477         if (_new_current_item && !_new_current_item->ignore_events()) {
478                 enter_event.detail = enter_detail;
479                 _new_current_item->Event ((GdkEvent*)&enter_event);
480                 // std::cerr << "ENTER " << _new_current_item->whatami() << '/' << _new_current_item->name << std::endl;
481         }
482
483         _current_item = _new_current_item;
484 }
485
486
487 /** Deliver an event to the appropriate item; either the grabbed item, or
488  *  one of the items underneath the event.
489  *  @param point Position that the event has occurred at, in canvas coordinates.
490  *  @param event The event.
491  */
492 bool
493 GtkCanvas::deliver_event (GdkEvent* event)
494 {
495         /* Point in in canvas coordinate space */
496
497         if (_grabbed_item) {
498                 /* we have a grabbed item, so everything gets sent there */
499                 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
500                                                                        _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
501                 return _grabbed_item->Event (event);
502         }
503
504         if (!_current_item) {
505                 return false;
506         }
507
508         /* run through the items from child to parent, until one claims the event */
509
510         Item* item = const_cast<Item*> (_current_item);
511         
512         while (item) {
513
514                 Item* parent = item->parent ();
515
516                 if (!item->ignore_events () && 
517                     item->Event (event)) {
518                         /* this item has just handled the event */
519                         DEBUG_TRACE (
520                                 PBD::DEBUG::CanvasEvents,
521                                 string_compose ("canvas event handled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name)
522                                 );
523                         
524                         return true;
525                 }
526                 
527                 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event left unhandled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name));
528
529                 if ((item = parent) == 0) {
530                         break;
531                 }
532
533         }
534
535         return false;
536 }
537
538 /** Called when an item is being destroyed.
539  *  @param item Item being destroyed.
540  *  @param bounding_box Last known bounding box of the item.
541  */
542 void
543 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
544 {
545         if (bounding_box) {
546                 queue_draw_item_area (item, bounding_box.get ());
547         }
548         
549         /* no need to send a leave event to this item, since it is going away 
550          */
551
552         if (_new_current_item == item) {
553                 _new_current_item = 0;
554         }
555
556         if (_current_item == item) {
557                 _current_item = 0;
558         }
559
560         if (_grabbed_item == item) {
561                 _grabbed_item = 0;
562         }
563
564         if (_focused_item == item) {
565                 _focused_item = 0;
566         }
567
568         pick_current_item (0); // no mouse state
569         
570 }
571
572 /** Handler for GDK expose events.
573  *  @param ev Event.
574  *  @return true if the event was handled.
575  */
576 bool
577 GtkCanvas::on_expose_event (GdkEventExpose* ev)
578 {
579         Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
580
581         render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
582
583         return true;
584 }
585
586 /** @return Our Cairo context, or 0 if we don't have one */
587 Cairo::RefPtr<Cairo::Context>
588 GtkCanvas::context ()
589 {
590         Glib::RefPtr<Gdk::Window> w = get_window ();
591         if (!w) {
592                 return Cairo::RefPtr<Cairo::Context> ();
593         }
594
595         return w->create_cairo_context ();
596 }
597
598 /** Handler for GDK button press events.
599  *  @param ev Event.
600  *  @return true if the event was handled.
601  */
602 bool
603 GtkCanvas::on_button_press_event (GdkEventButton* ev)
604 {
605         /* translate event coordinates from window to canvas */
606
607         GdkEvent copy = *((GdkEvent*)ev);
608         Duple where = window_to_canvas (Duple (ev->x, ev->y));
609
610         copy.button.x = where.x;
611         copy.button.y = where.y;
612         
613         /* Coordinates in the event will be canvas coordinates, correctly adjusted
614            for scroll if this GtkCanvas is in a GtkCanvasViewport.
615         */
616
617         pick_current_item (where, ev->state);
618         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
619         return deliver_event (reinterpret_cast<GdkEvent*>(&copy));
620 }
621
622 /** Handler for GDK button release events.
623  *  @param ev Event.
624  *  @return true if the event was handled.
625  */
626 bool
627 GtkCanvas::on_button_release_event (GdkEventButton* ev)
628 {       
629         /* translate event coordinates from window to canvas */
630
631         GdkEvent copy = *((GdkEvent*)ev);
632         Duple where = window_to_canvas (Duple (ev->x, ev->y));
633         
634         pick_current_item (where, ev->state);
635
636         copy.button.x = where.x;
637         copy.button.y = where.y;
638
639         /* Coordinates in the event will be canvas coordinates, correctly adjusted
640            for scroll if this GtkCanvas is in a GtkCanvasViewport.
641         */
642
643         pick_current_item (where, ev->state);
644         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
645         return deliver_event (reinterpret_cast<GdkEvent*>(&copy));
646 }
647
648 /** Handler for GDK motion events.
649  *  @param ev Event.
650  *  @return true if the event was handled.
651  */
652 bool
653 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
654 {
655         /* translate event coordinates from window to canvas */
656
657         GdkEvent copy = *((GdkEvent*)ev);
658         Duple point (ev->x, ev->y);
659         Duple where = window_to_canvas (point);
660
661         copy.motion.x = where.x;
662         copy.motion.y = where.y;
663
664         /* Coordinates in "copy" will be canvas coordinates, 
665         */
666
667         // DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y));
668
669         if (_grabbed_item) {
670                 /* if we have a grabbed item, it gets just the motion event,
671                    since no enter/leave events can have happened.
672                 */
673                 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n",
674                                                                        _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
675                 return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (&copy));
676         }
677
678         pick_current_item (where, ev->state);
679
680         /* Now deliver the motion event.  It may seem a little inefficient
681            to recompute the items under the event, but the enter notify/leave
682            events may have deleted canvas items so it is important to
683            recompute the list in deliver_event.
684         */
685
686         return deliver_event (reinterpret_cast<GdkEvent*> (&copy));
687 }
688
689 bool
690 GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev)
691 {
692         Duple where = window_to_canvas (Duple (ev->x, ev->y));
693         pick_current_item (where, ev->state);
694         return true;
695 }
696
697 bool
698 GtkCanvas::on_leave_notify_event (GdkEventCrossing* ev)
699 {
700         _new_current_item = 0;
701         Duple where = window_to_canvas (Duple (ev->x, ev->y));
702         deliver_enter_leave (where, ev->state);
703         return true;
704 }
705
706 /** Called to request a redraw of our canvas.
707  *  @param area Area to redraw, in canvas coordinates.
708  */
709 void
710 GtkCanvas::request_redraw (Rect const & request)
711 {
712         Rect area = canvas_to_window (request);
713         queue_draw_area (area.x0, area.y0, area.width(), area.height());
714 }
715
716 /** Called to request that we try to get a particular size for ourselves.
717  *  @param size Size to request, in pixels.
718  */
719 void
720 GtkCanvas::request_size (Duple size)
721 {
722         Duple req = size;
723
724         if (req.x > INT_MAX) {
725                 req.x = INT_MAX;
726         }
727
728         if (req.y > INT_MAX) {
729                 req.y = INT_MAX;
730         }
731
732         set_size_request (req.x, req.y);
733 }
734
735 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
736  *  This is typically used for dragging items around, so that they are grabbed during
737  *  the drag.
738  *  @param item Item to grab.
739  */
740 void
741 GtkCanvas::grab (Item* item)
742 {
743         /* XXX: should this be doing gdk_pointer_grab? */
744         _grabbed_item = item;
745 }
746
747
748 /** `Ungrab' any item that was previously grabbed */
749 void
750 GtkCanvas::ungrab ()
751 {
752         /* XXX: should this be doing gdk_pointer_ungrab? */
753         _grabbed_item = 0;
754 }
755
756 /** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
757  *  moves elsewhere.
758  *  @param item Item to grab.
759  */
760 void
761 GtkCanvas::focus (Item* item)
762 {
763         _focused_item = item;
764 }
765
766 void
767 GtkCanvas::unfocus (Item* item)
768 {
769         if (item == _focused_item) {
770                 _focused_item = 0;
771         }
772 }
773
774 /** @return The visible area of the canvas, in canvas coordinates */
775 Rect
776 GtkCanvas::visible_area () const
777 {
778         Distance const xo = _scroll_offset_x;
779         Distance const yo = _scroll_offset_y;
780         return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
781 }
782
783 /** Create a GtkCanvaSViewport.
784  *  @param hadj Adjustment to use for horizontal scrolling.
785  *  @param vadj Adjustment to use for vertica scrolling.
786  */
787 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
788         : Alignment (0, 0, 1.0, 1.0)
789         , hadjustment (hadj)
790         , vadjustment (vadj)
791 {
792         add (_canvas);
793
794         hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
795         vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
796 }
797
798 void
799 GtkCanvasViewport::scrolled ()
800 {
801         _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
802         queue_draw ();
803 }
804
805 /** Handler for when GTK asks us what minimum size we want.
806  *  @param req Requsition to fill in.
807  */
808 void
809 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
810 {
811         /* force the canvas to size itself */
812         // _canvas.root()->bounding_box(); 
813
814         req->width = 16;
815         req->height = 16;
816 }
817