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