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