Merge branch 'master' into windows+cc
[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 <cassert>
26 #include <gtkmm/adjustment.h>
27 #include <gtkmm/label.h>
28
29 #include "pbd/compose.h"
30 #include "pbd/stacktrace.h"
31
32 #include "canvas/canvas.h"
33 #include "canvas/debug.h"
34
35 using namespace std;
36 using namespace ArdourCanvas;
37
38 /** Construct a new Canvas */
39 Canvas::Canvas ()
40         : _root (this)
41         , _scroll_offset_x (0)
42         , _scroll_offset_y (0)
43 {
44         set_epoch ();
45 }
46
47 void
48 Canvas::scroll_to (Coord x, Coord y)
49 {
50         _scroll_offset_x = x;
51         _scroll_offset_y = y;
52 }
53
54 /** Render an area of the canvas.
55  *  @param area Area in canvas coordinates.
56  *  @param context Cairo context to render to.
57  */
58 void
59 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
60 {
61 #ifdef CANVAS_DEBUG
62         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
63                 cerr << "RENDER: " << area << endl;
64                 //cerr << "CANVAS @ " << this << endl;
65                 //dump (cerr);
66                 //cerr << "-------------------------\n";
67         }
68 #endif
69
70         render_count = 0;
71         
72         boost::optional<Rect> root_bbox = _root.bounding_box();
73         if (!root_bbox) {
74                 /* the root has no bounding box, so there's nothing to render */
75                 return;
76         }
77
78         boost::optional<Rect> draw = root_bbox->intersection (area);
79         if (draw) {
80                 /* there's a common area between the root and the requested
81                    area, so render it.
82                 */
83
84                 _root.render (*draw, context);
85         }
86 }
87
88 ostream&
89 operator<< (ostream& o, Canvas& c)
90 {
91         c.dump (o);
92         return o;
93 }
94
95 std::string
96 Canvas::indent() const
97
98         string s;
99
100         for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
101                 s += '\t';
102         }
103
104         return s;
105 }
106
107 std::string
108 Canvas::render_indent() const
109
110         string s;
111
112         for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
113                 s += ' ';
114         }
115
116         return s;
117 }
118
119 void
120 Canvas::dump (ostream& o) const
121 {
122         dump_depth = 0;
123         _root.dump (o);
124 }       
125
126 /** Called when an item has been shown or hidden.
127  *  @param item Item that has been shown or hidden.
128  */
129 void
130 Canvas::item_shown_or_hidden (Item* item)
131 {
132         boost::optional<Rect> bbox = item->bounding_box ();
133         if (bbox) {
134                 queue_draw_item_area (item, bbox.get ());
135         }
136 }
137
138 /** Called when an item has a change to its visual properties
139  *  that do NOT affect its bounding box.
140  *  @param item Item that has been modified.
141  */
142 void
143 Canvas::item_visual_property_changed (Item* item)
144 {
145         boost::optional<Rect> bbox = item->bounding_box ();
146         if (bbox) {
147                 queue_draw_item_area (item, bbox.get ());
148         }
149 }
150
151 /** Called when an item has changed, but not moved.
152  *  @param item Item that has changed.
153  *  @param pre_change_bounding_box The bounding box of item before the change,
154  *  in the item's coordinates.
155  */
156 void
157 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
158 {
159         if (pre_change_bounding_box) {
160                 /* request a redraw of the item's old bounding box */
161                 queue_draw_item_area (item, pre_change_bounding_box.get ());
162         }
163
164         boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
165         if (post_change_bounding_box) {
166                 /* request a redraw of the item's new bounding box */
167                 queue_draw_item_area (item, post_change_bounding_box.get ());
168         }
169 }
170
171 Duple
172 Canvas::window_to_canvas (Duple const & d) const
173 {
174         return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
175 }
176
177 Duple
178 Canvas::canvas_to_window (Duple const & d) const
179 {
180         return d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
181 }       
182
183 Rect
184 Canvas::window_to_canvas (Rect const & r) const
185 {
186         return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
187 }
188
189 Rect
190 Canvas::canvas_to_window (Rect const & r) const
191 {
192         return r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
193 }       
194
195 /** Called when an item has moved.
196  *  @param item Item that has moved.
197  *  @param pre_change_parent_bounding_box The bounding box of the item before
198  *  the move, in its parent's coordinates.
199  */
200 void
201 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
202 {
203         if (pre_change_parent_bounding_box) {
204                 /* request a redraw of where the item used to be. The box has
205                  * to be in parent coordinate space since the bounding box of
206                  * an item does not change when moved. If we use
207                  * item->item_to_canvas() on the old bounding box, we will be
208                  * using the item's new position, and so will compute the wrong
209                  * invalidation area. If we use the parent (which has not
210                  * moved, then this will work.
211                  */
212
213                 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
214         }
215
216         boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
217         if (post_change_bounding_box) {
218                 /* request a redraw of where the item now is */
219                 queue_draw_item_area (item, post_change_bounding_box.get ());
220         }
221 }
222
223 /** Request a redraw of a particular area in an item's coordinates.
224  *  @param item Item.
225  *  @param area Area to redraw in the item's coordinates.
226  */
227 void
228 Canvas::queue_draw_item_area (Item* item, Rect area)
229 {
230         ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
231         // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
232         request_redraw (canvas_area);
233 }
234
235 /** Construct a GtkCanvas */
236 GtkCanvas::GtkCanvas ()
237         : _current_item (0)
238         , _grabbed_item (0)
239 {
240         /* these are the events we want to know about */
241         add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
242 }
243
244 /** Handler for button presses on the canvas.
245  *  @param ev GDK event.
246  */
247 bool
248 GtkCanvas::button_handler (GdkEventButton* ev)
249 {
250         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button %3 %1 %1\n", ev->x, ev->y, (ev->type == GDK_BUTTON_PRESS ? "press" : "release")));
251         /* The Duple that we are passing in here is in canvas coordinates */
252         return deliver_event (Duple (ev->x, ev->y), reinterpret_cast<GdkEvent*> (ev));
253 }
254
255 /** Handler for pointer motion events on the canvas.
256  *  @param ev GDK event.
257  *  @return true if the motion event was handled, otherwise false.
258  */
259 bool
260 GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
261 {
262         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y));
263
264         if (_grabbed_item) {
265                 /* if we have a grabbed item, it gets just the motion event,
266                    since no enter/leave events can have happened.
267                 */
268                 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n",
269                                                                        _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
270                 return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (ev));
271         }
272
273         Duple point (ev->x, ev->y);
274         
275         enter_leave_items (point, ev->state);
276
277         /* Now deliver the motion event.  It may seem a little inefficient
278            to recompute the items under the event, but the enter notify/leave
279            events may have deleted canvas items so it is important to
280            recompute the list in deliver_event.
281         */
282         return deliver_event (point, reinterpret_cast<GdkEvent*> (ev));
283 }
284
285 void
286 GtkCanvas::enter_leave_items (int state)
287 {
288         int x;
289         int y;
290
291         /* this version of ::enter_leave_items() is called after an item is
292          * added or removed, so we have no coordinates to work from as is the
293          * case with a motion event. Find out where the mouse is and use that.
294          */
295              
296         Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
297
298         if (pointer_window != get_window()) {
299                 return;
300         }
301
302         enter_leave_items (window_to_canvas (Duple (x, y)), state);
303 }
304                 
305 void
306 GtkCanvas::enter_leave_items (Duple const & point, int state)
307 {
308         /* find the items at the given position */
309
310         vector<Item const *> items;
311         _root.add_items_at_point (point, items);
312
313         GdkEventCrossing enter_event;
314         enter_event.type = GDK_ENTER_NOTIFY;
315         enter_event.window = get_window()->gobj();
316         enter_event.send_event = 0;
317         enter_event.subwindow = 0;
318         enter_event.mode = GDK_CROSSING_NORMAL;
319         enter_event.detail = GDK_NOTIFY_NONLINEAR;
320         enter_event.focus = FALSE;
321         enter_event.state = state;
322         enter_event.x = point.x;
323         enter_event.y = point.y;
324
325         GdkEventCrossing leave_event = enter_event;
326         leave_event.type = GDK_LEAVE_NOTIFY;
327         leave_event.detail = GDK_NOTIFY_ANCESTOR;
328         leave_event.subwindow = 0;
329
330         if (items.empty()) {
331                 if (_current_item) {
332                         /* leave event */
333                         _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
334                         _current_item = 0;
335                 }
336                 return;
337         }
338
339         /* items is sorted from top to bottom, so reverse through it from bottom
340          * to top to find the lowest, first event-sensitive item and notify that
341          * we have entered it
342          */
343
344         cerr << "E/L: " << items.size() << " to check at " << point << endl;
345 #ifdef CANVAS_DEBUG
346         for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
347                 cerr << '\t' << (*i)->whatami() << ' ' << (*i)->name << " ignore ? " << (*i)->ignore_events() << " current ? " << (_current_item == (*i)) << endl;
348         }
349 #endif
350         cerr << "------------\n";
351
352         for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
353
354                 Item const *  new_item = *i;
355 #ifdef CANVAS_DEBUG
356                 cerr << "\tE/L check out " << new_item->whatami() << ' ' << new_item->name << " ignore ? " << new_item->ignore_events() << " current ? " << (_current_item == new_item) << endl;
357 #endif
358                 if (new_item->ignore_events()) {
359                         cerr << "continue1\n";
360                         continue;
361                 }
362
363                 if (_current_item == new_item) {
364                         cerr << "continue2\n";
365                         continue;
366                 }
367
368                 if (_current_item) {
369                         /* leave event */
370                         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Leave %1 %2\n", _current_item->whatami(), _current_item->name));
371                         _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
372                 }
373
374                 if (new_item && _current_item != new_item) {
375                         /* enter event */
376                         _current_item = new_item;
377                         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Enter %1 %2\n", _current_item->whatami(), _current_item->name));
378                         _current_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
379                         break;
380                 }
381
382                 cerr << "Loop around again\n";
383         }
384 }
385
386 /** Deliver an event to the appropriate item; either the grabbed item, or
387  *  one of the items underneath the event.
388  *  @param point Position that the event has occurred at, in canvas coordinates.
389  *  @param event The event.
390  */
391 bool
392 GtkCanvas::deliver_event (Duple point, GdkEvent* event)
393 {
394         if (_grabbed_item) {
395                 /* we have a grabbed item, so everything gets sent there */
396                 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
397                                                                        _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
398                 return _grabbed_item->Event (event);
399         }
400
401         /* find the items that exist at the event's position */
402         vector<Item const *> items;
403         _root.add_items_at_point (point, items);
404
405         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 possible items to deliver event to\n", items.size()));
406
407         /* run through the items under the event, from top to bottom, until one claims the event */
408         vector<Item const *>::const_reverse_iterator i = items.rbegin ();
409         while (i != items.rend()) {
410
411                 if ((*i)->ignore_events ()) {
412                         DEBUG_TRACE (
413                                 PBD::DEBUG::CanvasEvents,
414                                 string_compose ("canvas event ignored by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
415                                 );
416                         ++i;
417                         continue;
418                 }
419                 
420                 if ((*i)->Event (event)) {
421                         /* this item has just handled the event */
422                         DEBUG_TRACE (
423                                 PBD::DEBUG::CanvasEvents,
424                                 string_compose ("canvas event handled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
425                                 );
426                         
427                         return true;
428                 }
429                 
430                 DEBUG_TRACE (
431                         PBD::DEBUG::CanvasEvents,
432                         string_compose ("canvas event left unhandled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
433                         );
434                 
435                 ++i;
436         }
437
438         /* debugging */
439         if (PBD::debug_bits & PBD::DEBUG::CanvasEvents) {
440                 while (i != items.rend()) {
441                         DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event not seen by %1\n", (*i)->name.empty() ? "[unknown]" : (*i)->name));
442                         ++i;
443                 }
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         if (_current_item == item) {
461                 _current_item = 0;
462         }
463
464         if (_grabbed_item == item) {
465                 _grabbed_item = 0;
466         }
467
468         enter_leave_items (0); // no mouse state
469         
470 }
471
472 /** Handler for GDK expose events.
473  *  @param ev Event.
474  *  @return true if the event was handled.
475  */
476 bool
477 GtkCanvas::on_expose_event (GdkEventExpose* ev)
478 {
479         Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
480
481         render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
482
483         return true;
484 }
485
486 /** @return Our Cairo context, or 0 if we don't have one */
487 Cairo::RefPtr<Cairo::Context>
488 GtkCanvas::context ()
489 {
490         Glib::RefPtr<Gdk::Window> w = get_window ();
491         if (!w) {
492                 return Cairo::RefPtr<Cairo::Context> ();
493         }
494
495         return w->create_cairo_context ();
496 }
497
498 /** Handler for GDK button press events.
499  *  @param ev Event.
500  *  @return true if the event was handled.
501  */
502 bool
503 GtkCanvas::on_button_press_event (GdkEventButton* ev)
504 {
505         /* translate event coordinates from window to canvas */
506
507         GdkEvent copy = *((GdkEvent*)ev);
508         Duple where = window_to_canvas (Duple (ev->x, ev->y));
509
510         copy.button.x = where.x;
511         copy.button.y = where.y;
512                                  
513         /* Coordinates in the event will be canvas coordinates, correctly adjusted
514            for scroll if this GtkCanvas is in a GtkCanvasViewport.
515         */
516         return button_handler ((GdkEventButton*) &copy);
517 }
518
519 /** Handler for GDK button release events.
520  *  @param ev Event.
521  *  @return true if the event was handled.
522  */
523 bool
524 GtkCanvas::on_button_release_event (GdkEventButton* ev)
525 {       
526         /* translate event coordinates from window to canvas */
527
528         GdkEvent copy = *((GdkEvent*)ev);
529         Duple where = window_to_canvas (Duple (ev->x, ev->y));
530
531         copy.button.x = where.x;
532         copy.button.y = where.y;
533
534         /* Coordinates in the event will be canvas coordinates, correctly adjusted
535            for scroll if this GtkCanvas is in a GtkCanvasViewport.
536         */
537         return button_handler ((GdkEventButton*) &copy);
538 }
539
540 /** Handler for GDK motion events.
541  *  @param ev Event.
542  *  @return true if the event was handled.
543  */
544 bool
545 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
546 {
547         /* translate event coordinates from window to canvas */
548
549         GdkEvent copy = *((GdkEvent*)ev);
550         Duple where = window_to_canvas (Duple (ev->x, ev->y));
551
552         copy.motion.x = where.x;
553         copy.motion.y = where.y;
554
555         /* Coordinates in the event will be canvas coordinates, correctly adjusted
556            for scroll if this GtkCanvas is in a GtkCanvasViewport.
557         */
558         return motion_notify_handler ((GdkEventMotion*) &copy);
559 }
560
561 /** Called to request a redraw of our canvas.
562  *  @param area Area to redraw, in canvas coordinates.
563  */
564 void
565 GtkCanvas::request_redraw (Rect const & request)
566 {
567         Rect area = canvas_to_window (request);
568         // cerr << this << " Invalidate " << request << " TRANSLATE AS " << area << endl;
569         queue_draw_area (floor (area.x0), floor (area.y0), ceil (area.x1) - floor (area.x0), ceil (area.y1) - floor (area.y0));
570 }
571
572 /** Called to request that we try to get a particular size for ourselves.
573  *  @param size Size to request, in pixels.
574  */
575 void
576 GtkCanvas::request_size (Duple size)
577 {
578         Duple req = size;
579
580         if (req.x > INT_MAX) {
581                 req.x = INT_MAX;
582         }
583
584         if (req.y > INT_MAX) {
585                 req.y = INT_MAX;
586         }
587
588         set_size_request (req.x, req.y);
589 }
590
591 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
592  *  This is typically used for dragging items around, so that they are grabbed during
593  *  the drag.
594  *  @param item Item to grab.
595  */
596 void
597 GtkCanvas::grab (Item* item)
598 {
599         /* XXX: should this be doing gdk_pointer_grab? */
600         _grabbed_item = item;
601 }
602
603 /** `Ungrab' any item that was previously grabbed */
604 void
605 GtkCanvas::ungrab ()
606 {
607         /* XXX: should this be doing gdk_pointer_ungrab? */
608         _grabbed_item = 0;
609 }
610
611 /** @return The visible area of the canvas, in canvas coordinates */
612 Rect
613 GtkCanvas::visible_area () const
614 {
615         Distance const xo = _scroll_offset_x;
616         Distance const yo = _scroll_offset_y;
617         return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
618 }
619
620 /** Create a GtkCanvaSViewport.
621  *  @param hadj Adjustment to use for horizontal scrolling.
622  *  @param vadj Adjustment to use for vertica scrolling.
623  */
624 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
625         : Alignment (0, 0, 1.0, 1.0)
626         , hadjustment (hadj)
627         , vadjustment (vadj)
628 {
629         add (_canvas);
630
631         hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
632         vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
633 }
634
635 void
636 GtkCanvasViewport::scrolled ()
637 {
638         _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
639         queue_draw ();
640 }
641
642 /** Handler for when GTK asks us what minimum size we want.
643  *  @param req Requsition to fill in.
644  */
645 void
646 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
647 {
648         /* force the canvas to size itself */
649         // _canvas.root()->bounding_box(); 
650
651         req->width = 16;
652         req->height = 16;
653 }
654