2 Copyright (C) 2011 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 /** @file canvas/canvas.cc
22 * @brief Implementation of the main canvas classes.
27 #include <gtkmm/adjustment.h>
28 #include <gtkmm/label.h>
30 #include "pbd/compose.h"
31 #include "pbd/stacktrace.h"
33 #include "canvas/canvas.h"
34 #include "canvas/debug.h"
35 #include "canvas/line.h"
38 using namespace ArdourCanvas;
40 /** Construct a new Canvas */
43 , _scroll_offset_x (0)
44 , _scroll_offset_y (0)
50 Canvas::scroll_to (Coord x, Coord y)
55 pick_current_item (0); // no current mouse position
61 pick_current_item (0); // no current mouse position
64 /** Render an area of the canvas.
65 * @param area Area in canvas coordinates.
66 * @param context Cairo context to render to.
69 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
72 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
73 cerr << this << " RENDER: " << area << endl;
74 //cerr << "CANVAS @ " << this << endl;
76 //cerr << "-------------------------\n";
82 boost::optional<Rect> root_bbox = _root.bounding_box();
84 /* the root has no bounding box, so there's nothing to render */
88 boost::optional<Rect> draw = root_bbox->intersection (area);
91 /* there's a common area between the root and the requested
95 _root.render (*draw, context);
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 ();
107 operator<< (ostream& o, Canvas& c)
114 Canvas::indent() const
118 for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
126 Canvas::render_indent() const
130 for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
138 Canvas::dump (ostream& o) const
144 /** Called when an item has been shown or hidden.
145 * @param item Item that has been shown or hidden.
148 Canvas::item_shown_or_hidden (Item* item)
150 boost::optional<Rect> bbox = item->bounding_box ();
152 queue_draw_item_area (item, bbox.get ());
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.
161 Canvas::item_visual_property_changed (Item* item)
163 boost::optional<Rect> bbox = item->bounding_box ();
165 queue_draw_item_area (item, bbox.get ());
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.
175 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
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 ());
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 ());
190 Canvas::window_to_canvas (Duple const & d) const
192 return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
196 Canvas::canvas_to_window (Duple const & d) const
198 Duple wd = d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
200 /* Note that this intentionally always returns integer coordinates */
209 Canvas::window_to_canvas (Rect const & r) const
211 return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
215 Canvas::canvas_to_window (Rect const & r) const
217 Rect wr = r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
219 /* Note that this intentionally always returns integer coordinates */
221 wr.x0 = round (wr.x0);
222 wr.x1 = round (wr.x1);
223 wr.y0 = round (wr.y0);
224 wr.y1 = round (wr.y1);
229 /** Called when an item has moved.
230 * @param item Item that has moved.
231 * @param pre_change_parent_bounding_box The bounding box of the item before
232 * the move, in its parent's coordinates.
235 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
237 if (pre_change_parent_bounding_box) {
238 /* request a redraw of where the item used to be. The box has
239 * to be in parent coordinate space since the bounding box of
240 * an item does not change when moved. If we use
241 * item->item_to_canvas() on the old bounding box, we will be
243 * using the item's new position, and so will compute the wrong
244 * invalidation area. If we use the parent (which has not
245 * moved, then this will work.
247 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
250 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
251 if (post_change_bounding_box) {
252 /* request a redraw of where the item now is */
253 queue_draw_item_area (item, post_change_bounding_box.get ());
257 /** Request a redraw of a particular area in an item's coordinates.
259 * @param area Area to redraw in the item's coordinates.
262 Canvas::queue_draw_item_area (Item* item, Rect area)
264 ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
265 // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << " window = " << canvas_to_window (canvas_area) << std::endl;
266 request_redraw (canvas_area);
269 /** Construct a GtkCanvas */
270 GtkCanvas::GtkCanvas ()
272 , _new_current_item (0)
276 /* these are the events we want to know about */
277 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK |
278 Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
282 GtkCanvas::pick_current_item (int state)
287 /* this version of ::pick_current_item() is called after an item is
288 * added or removed, so we have no coordinates to work from as is the
289 * case with a motion event. Find out where the mouse is and use that.
292 Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
294 if (pointer_window != get_window()) {
298 pick_current_item (window_to_canvas (Duple (x, y)), state);
302 GtkCanvas::pick_current_item (Duple const & point, int state)
304 /* we do not enter/leave items during a drag/grab */
310 /* find the items at the given position */
312 vector<Item const *> items;
313 _root.add_items_at_point (point, items);
315 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("%1 covers %2 items\n", point, items.size()));
318 if (DEBUG_ENABLED(PBD::DEBUG::CanvasEnterLeave)) {
319 for (vector<Item const*>::const_iterator it = items.begin(); it != items.end(); ++it) {
321 std::cerr << "\tItem " << (*it)->whatami() << '/' << (*it)->name << std::endl;
323 std::cerr << "\tItem " << (*it)->whatami() << std::endl;
329 /* put all items at point that are event-sensitive and visible and NOT
330 groups into within_items. Note that items is sorted from bottom to
331 top, but we're going to reverse that for within_items so that its
332 first item is the upper-most item that can be chosen as _current_item.
335 vector<Item const *>::const_iterator i;
336 list<Item const *> within_items;
338 for (i = items.begin(); i != items.end(); ++i) {
340 Item const * new_item = *i;
342 /* We ignore invisible items, groups and items that ignore events */
344 if (!new_item->visible() || new_item->ignore_events() || dynamic_cast<Group const *>(new_item) != 0) {
348 within_items.push_front (new_item);
351 if (within_items.empty()) {
353 /* no items at point, just send leave event below */
354 _new_current_item = 0;
358 if (within_items.front() == _current_item) {
359 /* uppermost item at point is already _current_item */
363 _new_current_item = const_cast<Item*> (within_items.front());
366 if (_new_current_item != _current_item) {
367 deliver_enter_leave (point, state);
372 GtkCanvas::deliver_enter_leave (Duple const & point, int state)
374 /* setup enter & leave event structures */
376 GdkEventCrossing enter_event;
377 enter_event.type = GDK_ENTER_NOTIFY;
378 enter_event.window = get_window()->gobj();
379 enter_event.send_event = 0;
380 enter_event.subwindow = 0;
381 enter_event.mode = GDK_CROSSING_NORMAL;
382 enter_event.focus = FALSE;
383 enter_event.state = state;
384 enter_event.x = point.x;
385 enter_event.y = point.y;
387 GdkEventCrossing leave_event = enter_event;
388 leave_event.type = GDK_LEAVE_NOTIFY;
391 GdkNotifyType enter_detail;
392 GdkNotifyType leave_detail;
393 vector<Item*> items_to_leave_virtual;
394 vector<Item*> items_to_enter_virtual;
396 if (_new_current_item == 0) {
398 leave_detail = GDK_NOTIFY_UNKNOWN;
402 /* no current item, so also send virtual leave events to the
403 * entire heirarchy for the current item
406 for (i = _current_item->parent(); i ; i = i->parent()) {
407 items_to_leave_virtual.push_back (i);
411 } else if (_current_item == 0) {
413 enter_detail = GDK_NOTIFY_UNKNOWN;
415 /* no current item, so also send virtual enter events to the
416 * entire heirarchy for the new item
419 for (i = _new_current_item->parent(); i ; i = i->parent()) {
420 items_to_enter_virtual.push_back (i);
423 } else if (_current_item->is_descendant_of (*_new_current_item)) {
425 /* move from descendant to ancestor (X: "_current_item is an
426 * inferior ("child") of _new_current_item")
428 * Deliver "virtual" leave notifications to all items in the
429 * heirarchy between current and new_current.
433 for (i = _current_item->parent(); i && i != _new_current_item; i = i->parent()) {
434 items_to_leave_virtual.push_back (i);
437 enter_detail = GDK_NOTIFY_INFERIOR;
438 leave_detail = GDK_NOTIFY_ANCESTOR;
441 } else if (_new_current_item->is_descendant_of (*_current_item)) {
442 /* move from ancestor to descendant (X: "_new_current_item is
443 * an inferior ("child") of _current_item")
445 * Deliver "virtual" enter notifications to all items in the
446 * heirarchy between current and new_current.
449 for (i = _new_current_item->parent(); i && i != _current_item; i = i->parent()) {
450 items_to_enter_virtual.push_back (i);
453 enter_detail = GDK_NOTIFY_ANCESTOR;
454 leave_detail = GDK_NOTIFY_INFERIOR;
458 Item const * common_ancestor = _current_item->closest_ancestor_with (*_new_current_item);
460 /* deliver virtual leave events to everything between _current
461 * and common_ancestor.
464 for (i = _current_item->parent(); i && i != common_ancestor; i = i->parent()) {
465 items_to_leave_virtual.push_back (i);
468 /* deliver virtual enter events to everything between
469 * _new_current and common_ancestor.
472 for (i = _new_current_item->parent(); i && i != common_ancestor; i = i->parent()) {
473 items_to_enter_virtual.push_back (i);
476 enter_detail = GDK_NOTIFY_NONLINEAR;
477 leave_detail = GDK_NOTIFY_NONLINEAR;
481 if (_current_item && !_current_item->ignore_events ()) {
482 leave_event.detail = leave_detail;
483 _current_item->Event ((GdkEvent*)&leave_event);
484 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("LEAVE %1/%2\n", _current_item->whatami(), _current_item->name));
487 leave_event.detail = GDK_NOTIFY_VIRTUAL;
488 enter_event.detail = GDK_NOTIFY_VIRTUAL;
490 for (vector<Item*>::iterator it = items_to_leave_virtual.begin(); it != items_to_leave_virtual.end(); ++it) {
491 if (!(*it)->ignore_events()) {
492 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("leave %1/%2\n", (*it)->whatami(), (*it)->name));
493 (*it)->Event ((GdkEvent*)&leave_event);
497 for (vector<Item*>::iterator it = items_to_enter_virtual.begin(); it != items_to_enter_virtual.end(); ++it) {
498 if (!(*it)->ignore_events()) {
499 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("enter %1/%2\n", (*it)->whatami(), (*it)->name));
500 (*it)->Event ((GdkEvent*)&enter_event);
501 // std::cerr << "enter " << (*it)->whatami() << '/' << (*it)->name << std::endl;
505 if (_new_current_item && !_new_current_item->ignore_events()) {
506 enter_event.detail = enter_detail;
507 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("ENTER %1/%2\n", _new_current_item->whatami(), _new_current_item->name));
508 _new_current_item->Event ((GdkEvent*)&enter_event);
511 _current_item = _new_current_item;
515 /** Deliver an event to the appropriate item; either the grabbed item, or
516 * one of the items underneath the event.
517 * @param point Position that the event has occurred at, in canvas coordinates.
518 * @param event The event.
521 GtkCanvas::deliver_event (GdkEvent* event)
523 /* Point in in canvas coordinate space */
525 const Item* event_item;
528 /* we have a grabbed item, so everything gets sent there */
529 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
530 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
531 event_item = _grabbed_item;
533 event_item = _current_item;
540 /* run through the items from child to parent, until one claims the event */
542 Item* item = const_cast<Item*> (event_item);
546 Item* parent = item->parent ();
548 if (!item->ignore_events () &&
549 item->Event (event)) {
550 /* this item has just handled the event */
552 PBD::DEBUG::CanvasEvents,
553 string_compose ("canvas event handled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name)
559 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)));
561 if ((item = parent) == 0) {
570 /** Called when an item is being destroyed.
571 * @param item Item being destroyed.
572 * @param bounding_box Last known bounding box of the item.
575 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
578 queue_draw_item_area (item, bounding_box.get ());
581 if (_new_current_item == item) {
582 _new_current_item = 0;
585 if (_grabbed_item == item) {
589 if (_focused_item == item) {
593 if (_current_item == item) {
594 /* no need to send a leave event to this item, since it is going away
597 pick_current_item (0); // no mouse state
602 /** Handler for GDK expose events.
604 * @return true if the event was handled.
607 GtkCanvas::on_expose_event (GdkEventExpose* ev)
609 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
610 render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), cairo_context);
614 /** @return Our Cairo context, or 0 if we don't have one */
615 Cairo::RefPtr<Cairo::Context>
616 GtkCanvas::context ()
618 Glib::RefPtr<Gdk::Window> w = get_window ();
620 return Cairo::RefPtr<Cairo::Context> ();
623 return w->create_cairo_context ();
626 /** Handler for GDK button press events.
628 * @return true if the event was handled.
631 GtkCanvas::on_button_press_event (GdkEventButton* ev)
633 /* translate event coordinates from window to canvas */
635 GdkEvent copy = *((GdkEvent*)ev);
636 Duple where = window_to_canvas (Duple (ev->x, ev->y));
638 copy.button.x = where.x;
639 copy.button.y = where.y;
641 /* Coordinates in the event will be canvas coordinates, correctly adjusted
642 for scroll if this GtkCanvas is in a GtkCanvasViewport.
645 pick_current_item (where, ev->state);
646 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
647 return deliver_event (reinterpret_cast<GdkEvent*>(©));
650 /** Handler for GDK button release events.
652 * @return true if the event was handled.
655 GtkCanvas::on_button_release_event (GdkEventButton* ev)
657 /* translate event coordinates from window to canvas */
659 GdkEvent copy = *((GdkEvent*)ev);
660 Duple where = window_to_canvas (Duple (ev->x, ev->y));
662 pick_current_item (where, ev->state);
664 copy.button.x = where.x;
665 copy.button.y = where.y;
667 /* Coordinates in the event will be canvas coordinates, correctly adjusted
668 for scroll if this GtkCanvas is in a GtkCanvasViewport.
671 pick_current_item (where, ev->state);
672 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
673 return deliver_event (reinterpret_cast<GdkEvent*>(©));
676 /** Handler for GDK motion events.
678 * @return true if the event was handled.
681 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
683 /* translate event coordinates from window to canvas */
685 GdkEvent copy = *((GdkEvent*)ev);
686 Duple point (ev->x, ev->y);
687 Duple where = window_to_canvas (point);
689 copy.motion.x = where.x;
690 copy.motion.y = where.y;
692 /* Coordinates in "copy" will be canvas coordinates,
695 // DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y));
697 pick_current_item (where, ev->state);
699 /* Now deliver the motion event. It may seem a little inefficient
700 to recompute the items under the event, but the enter notify/leave
701 events may have deleted canvas items so it is important to
702 recompute the list in deliver_event.
705 return deliver_event (reinterpret_cast<GdkEvent*> (©));
709 GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev)
711 Duple where = window_to_canvas (Duple (ev->x, ev->y));
712 pick_current_item (where, ev->state);
717 GtkCanvas::on_leave_notify_event (GdkEventCrossing* ev)
719 _new_current_item = 0;
720 Duple where = window_to_canvas (Duple (ev->x, ev->y));
721 deliver_enter_leave (where, ev->state);
725 /** Called to request a redraw of our canvas.
726 * @param area Area to redraw, in canvas coordinates.
729 GtkCanvas::request_redraw (Rect const & request)
731 boost::optional<Rect> req = request.intersection (visible_area());
735 Rect area = canvas_to_window (r);
736 queue_draw_area (area.x0, area.y0, area.width(), area.height());
740 /** Called to request that we try to get a particular size for ourselves.
741 * @param size Size to request, in pixels.
744 GtkCanvas::request_size (Duple size)
748 if (req.x > INT_MAX) {
752 if (req.y > INT_MAX) {
756 set_size_request (req.x, req.y);
759 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
760 * This is typically used for dragging items around, so that they are grabbed during
762 * @param item Item to grab.
765 GtkCanvas::grab (Item* item)
767 /* XXX: should this be doing gdk_pointer_grab? */
768 _grabbed_item = item;
772 /** `Ungrab' any item that was previously grabbed */
776 /* XXX: should this be doing gdk_pointer_ungrab? */
780 /** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
782 * @param item Item to grab.
785 GtkCanvas::focus (Item* item)
787 _focused_item = item;
791 GtkCanvas::unfocus (Item* item)
793 if (item == _focused_item) {
798 /** @return The visible area of the canvas, in canvas coordinates */
800 GtkCanvas::visible_area () const
802 Distance const xo = _scroll_offset_x;
803 Distance const yo = _scroll_offset_y;
804 return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
807 /** Create a GtkCanvaSViewport.
808 * @param hadj Adjustment to use for horizontal scrolling.
809 * @param vadj Adjustment to use for vertica scrolling.
811 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
812 : Alignment (0, 0, 1.0, 1.0)
818 hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
819 vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
823 GtkCanvasViewport::scrolled ()
825 _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
829 /** Handler for when GTK asks us what minimum size we want.
830 * @param req Requsition to fill in.
833 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
835 /* force the canvas to size itself */
836 // _canvas.root()->bounding_box();