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