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