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"
36 #include "canvas/scroll_group.h"
39 using namespace ArdourCanvas;
41 /** Construct a new Canvas */
49 Canvas::scroll_to (Coord x, Coord y)
51 /* We do things this way because we do not want to recurse through
52 the canvas for every scroll. In the presence of large MIDI
53 tracks this means traversing item lists that include
54 thousands of items (notes).
56 This design limits us to moving only those items (groups, typically)
57 that should move in certain ways as we scroll. In other terms, it
58 becomes O(1) rather than O(N).
61 for (list<ScrollGroup*>::iterator i = scrollers.begin(); i != scrollers.end(); ++i) {
62 (*i)->scroll_to (Duple (x, y));
65 pick_current_item (0); // no current mouse position
69 Canvas::add_scroller (ScrollGroup& i)
71 scrollers.push_back (&i);
77 pick_current_item (0); // no current mouse position
80 /** Render an area of the canvas.
81 * @param area Area in window coordinates.
82 * @param context Cairo context to render to.
85 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
88 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
89 cerr << this << " RENDER: " << area << endl;
90 //cerr << "CANVAS @ " << this << endl;
92 //cerr << "-------------------------\n";
98 boost::optional<Rect> root_bbox = _root.bounding_box();
100 /* the root has no bounding box, so there's nothing to render */
104 boost::optional<Rect> draw = root_bbox->intersection (area);
107 /* there's a common area between the root and the requested
111 _root.render (*draw, context);
114 // This transparently colors the rect being rendered, after it has been drawn.
115 double r = (random() % 65536) /65536.0;
116 double g = (random() % 65536) /65536.0;
117 double b = (random() % 65536) /65536.0;
118 context->rectangle (draw->x0, draw->y0, draw->x1 - draw->x0, draw->y1 - draw->y0);
119 context->set_source_rgba (r, g, b, 0.25);
128 operator<< (ostream& o, Canvas& c)
135 Canvas::indent() const
139 for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
147 Canvas::render_indent() const
151 for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
159 Canvas::dump (ostream& o) const
165 /** Called when an item has been shown or hidden.
166 * @param item Item that has been shown or hidden.
169 Canvas::item_shown_or_hidden (Item* item)
171 boost::optional<Rect> bbox = item->bounding_box ();
173 if (item->item_to_window (*bbox).intersection (visible_area ())) {
174 queue_draw_item_area (item, bbox.get ());
179 /** Called when an item has a change to its visual properties
180 * that do NOT affect its bounding box.
181 * @param item Item that has been modified.
184 Canvas::item_visual_property_changed (Item* item)
186 boost::optional<Rect> bbox = item->bounding_box ();
188 if (item->item_to_window (*bbox).intersection (visible_area ())) {
189 queue_draw_item_area (item, bbox.get ());
194 /** Called when an item has changed, but not moved.
195 * @param item Item that has changed.
196 * @param pre_change_bounding_box The bounding box of item before the change,
197 * in the item's coordinates.
200 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
203 Rect window_bbox = visible_area ();
205 if (pre_change_bounding_box) {
207 if (item->item_to_window (*pre_change_bounding_box).intersection (window_bbox)) {
208 /* request a redraw of the item's old bounding box */
209 queue_draw_item_area (item, pre_change_bounding_box.get ());
213 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
214 if (post_change_bounding_box) {
216 if (item->item_to_window (*post_change_bounding_box).intersection (window_bbox)) {
217 /* request a redraw of the item's new bounding box */
218 queue_draw_item_area (item, post_change_bounding_box.get ());
224 Canvas::window_to_canvas (Duple const & d) const
226 /* Find the scroll group that covers d (a window coordinate). Scroll groups are only allowed
227 * as children of the root group, so we just scan its first level
228 * children and see what we can find.
231 std::list<Item*> const& root_children (_root.items());
234 for (std::list<Item*>::const_iterator i = root_children.begin(); i != root_children.end(); ++i) {
235 if (((sg = dynamic_cast<ScrollGroup*>(*i)) != 0) && sg->covers_window (d)) {
241 return d.translate (sg->scroll_offset());
248 Canvas::canvas_to_window (Duple const & d, bool rounded) const
250 /* Find the scroll group that covers d (a canvas coordinate). Scroll groups are only allowed
251 * as children of the root group, so we just scan its first level
252 * children and see what we can find.
255 std::list<Item*> const& root_children (_root.items());
259 for (std::list<Item*>::const_iterator i = root_children.begin(); i != root_children.end(); ++i) {
260 if (((sg = dynamic_cast<ScrollGroup*>(*i)) != 0) && sg->covers_canvas (d)) {
267 wd = d.translate (-sg->scroll_offset());
272 /* Note that this intentionally almost always returns integer coordinates */
282 /** Called when an item has moved.
283 * @param item Item that has moved.
284 * @param pre_change_parent_bounding_box The bounding box of the item before
285 * the move, in its parent's coordinates.
288 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
290 if (pre_change_parent_bounding_box) {
291 /* request a redraw of where the item used to be. The box has
292 * to be in parent coordinate space since the bounding box of
293 * an item does not change when moved. If we use
294 * item->item_to_canvas() on the old bounding box, we will be
296 * using the item's new position, and so will compute the wrong
297 * invalidation area. If we use the parent (which has not
298 * moved, then this will work.
300 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
303 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
304 if (post_change_bounding_box) {
305 /* request a redraw of where the item now is */
306 queue_draw_item_area (item, post_change_bounding_box.get ());
310 /** Request a redraw of a particular area in an item's coordinates.
312 * @param area Area to redraw in the item's coordinates.
315 Canvas::queue_draw_item_area (Item* item, Rect area)
317 request_redraw (item->item_to_window (area));
320 /** Construct a GtkCanvas */
321 GtkCanvas::GtkCanvas ()
323 , _new_current_item (0)
327 /* these are the events we want to know about */
328 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK |
329 Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
333 GtkCanvas::pick_current_item (int state)
338 /* this version of ::pick_current_item() is called after an item is
339 * added or removed, so we have no coordinates to work from as is the
340 * case with a motion event. Find out where the mouse is and use that.
343 Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
345 if (pointer_window != get_window()) {
349 pick_current_item (Duple (x, y), state);
352 /** Given @param point (a position in window coordinates)
353 * and mouse state @param state, check to see if _current_item
354 * (which will be used to deliver events) should change.
357 GtkCanvas::pick_current_item (Duple const & point, int state)
359 /* we do not enter/leave items during a drag/grab */
365 /* find the items at the given window position */
367 vector<Item const *> items;
368 _root.add_items_at_point (point, items);
370 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("%1 covers %2 items\n", point, items.size()));
373 if (DEBUG_ENABLED(PBD::DEBUG::CanvasEnterLeave)) {
374 for (vector<Item const*>::const_iterator it = items.begin(); it != items.end(); ++it) {
376 std::cerr << "\tItem " << (*it)->whatami() << '/' << (*it)->name << std::endl;
378 std::cerr << "\tItem " << (*it)->whatami() << std::endl;
384 /* put all items at point that are event-sensitive and visible and NOT
385 groups into within_items. Note that items is sorted from bottom to
386 top, but we're going to reverse that for within_items so that its
387 first item is the upper-most item that can be chosen as _current_item.
390 vector<Item const *>::const_iterator i;
391 list<Item const *> within_items;
393 for (i = items.begin(); i != items.end(); ++i) {
395 Item const * new_item = *i;
397 /* We ignore invisible items, groups and items that ignore events */
399 if (!new_item->visible() || new_item->ignore_events() || dynamic_cast<Group const *>(new_item) != 0) {
403 within_items.push_front (new_item);
406 if (within_items.empty()) {
408 /* no items at point, just send leave event below */
409 _new_current_item = 0;
413 if (within_items.front() == _current_item) {
414 /* uppermost item at point is already _current_item */
418 _new_current_item = const_cast<Item*> (within_items.front());
421 if (_new_current_item != _current_item) {
422 deliver_enter_leave (point, state);
426 /** Deliver a series of enter & leave events based on the pointer position being at window
427 * coordinate @param point, and pointer @param state (modifier keys, etc)
430 GtkCanvas::deliver_enter_leave (Duple const & point, int state)
432 /* setup enter & leave event structures */
434 GdkEventCrossing enter_event;
435 enter_event.type = GDK_ENTER_NOTIFY;
436 enter_event.window = get_window()->gobj();
437 enter_event.send_event = 0;
438 enter_event.subwindow = 0;
439 enter_event.mode = GDK_CROSSING_NORMAL;
440 enter_event.focus = FALSE;
441 enter_event.state = state;
443 /* Events delivered to canvas items are expected to be in canvas
444 * coordinates but @param point is in window coordinates.
447 Duple c = window_to_canvas (point);
451 GdkEventCrossing leave_event = enter_event;
452 leave_event.type = GDK_LEAVE_NOTIFY;
455 GdkNotifyType enter_detail;
456 GdkNotifyType leave_detail;
457 vector<Item*> items_to_leave_virtual;
458 vector<Item*> items_to_enter_virtual;
460 if (_new_current_item == 0) {
462 leave_detail = GDK_NOTIFY_UNKNOWN;
466 /* no current item, so also send virtual leave events to the
467 * entire heirarchy for the current item
470 for (i = _current_item->parent(); i ; i = i->parent()) {
471 items_to_leave_virtual.push_back (i);
475 } else if (_current_item == 0) {
477 enter_detail = GDK_NOTIFY_UNKNOWN;
479 /* no current item, so also send virtual enter events to the
480 * entire heirarchy for the new item
483 for (i = _new_current_item->parent(); i ; i = i->parent()) {
484 items_to_enter_virtual.push_back (i);
487 } else if (_current_item->is_descendant_of (*_new_current_item)) {
489 /* move from descendant to ancestor (X: "_current_item is an
490 * inferior ("child") of _new_current_item")
492 * Deliver "virtual" leave notifications to all items in the
493 * heirarchy between current and new_current.
496 for (i = _current_item->parent(); i && i != _new_current_item; i = i->parent()) {
497 items_to_leave_virtual.push_back (i);
500 enter_detail = GDK_NOTIFY_INFERIOR;
501 leave_detail = GDK_NOTIFY_ANCESTOR;
504 } else if (_new_current_item->is_descendant_of (*_current_item)) {
505 /* move from ancestor to descendant (X: "_new_current_item is
506 * an inferior ("child") of _current_item")
508 * Deliver "virtual" enter notifications to all items in the
509 * heirarchy between current and new_current.
512 for (i = _new_current_item->parent(); i && i != _current_item; i = i->parent()) {
513 items_to_enter_virtual.push_back (i);
516 enter_detail = GDK_NOTIFY_ANCESTOR;
517 leave_detail = GDK_NOTIFY_INFERIOR;
521 Item const * common_ancestor = _current_item->closest_ancestor_with (*_new_current_item);
523 /* deliver virtual leave events to everything between _current
524 * and common_ancestor.
527 for (i = _current_item->parent(); i && i != common_ancestor; i = i->parent()) {
528 items_to_leave_virtual.push_back (i);
531 /* deliver virtual enter events to everything between
532 * _new_current and common_ancestor.
535 for (i = _new_current_item->parent(); i && i != common_ancestor; i = i->parent()) {
536 items_to_enter_virtual.push_back (i);
539 enter_detail = GDK_NOTIFY_NONLINEAR;
540 leave_detail = GDK_NOTIFY_NONLINEAR;
544 if (_current_item && !_current_item->ignore_events ()) {
545 leave_event.detail = leave_detail;
546 _current_item->Event ((GdkEvent*)&leave_event);
547 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("LEAVE %1/%2\n", _current_item->whatami(), _current_item->name));
550 leave_event.detail = GDK_NOTIFY_VIRTUAL;
551 enter_event.detail = GDK_NOTIFY_VIRTUAL;
553 for (vector<Item*>::iterator it = items_to_leave_virtual.begin(); it != items_to_leave_virtual.end(); ++it) {
554 if (!(*it)->ignore_events()) {
555 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("leave %1/%2\n", (*it)->whatami(), (*it)->name));
556 (*it)->Event ((GdkEvent*)&leave_event);
560 for (vector<Item*>::iterator it = items_to_enter_virtual.begin(); it != items_to_enter_virtual.end(); ++it) {
561 if (!(*it)->ignore_events()) {
562 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("enter %1/%2\n", (*it)->whatami(), (*it)->name));
563 (*it)->Event ((GdkEvent*)&enter_event);
564 // std::cerr << "enter " << (*it)->whatami() << '/' << (*it)->name << std::endl;
568 if (_new_current_item && !_new_current_item->ignore_events()) {
569 enter_event.detail = enter_detail;
570 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("ENTER %1/%2\n", _new_current_item->whatami(), _new_current_item->name));
571 _new_current_item->Event ((GdkEvent*)&enter_event);
574 _current_item = _new_current_item;
578 /** Deliver an event to the appropriate item; either the grabbed item, or
579 * one of the items underneath the event.
580 * @param point Position that the event has occurred at, in canvas coordinates.
581 * @param event The event.
584 GtkCanvas::deliver_event (GdkEvent* event)
586 /* Point in in canvas coordinate space */
588 const Item* event_item;
591 /* we have a grabbed item, so everything gets sent there */
592 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
593 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
594 event_item = _grabbed_item;
596 event_item = _current_item;
603 /* run through the items from child to parent, until one claims the event */
605 Item* item = const_cast<Item*> (event_item);
609 Item* parent = item->parent ();
611 if (!item->ignore_events () &&
612 item->Event (event)) {
613 /* this item has just handled the event */
615 PBD::DEBUG::CanvasEvents,
616 string_compose ("canvas event handled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name)
622 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)));
624 if ((item = parent) == 0) {
633 /** Called when an item is being destroyed.
634 * @param item Item being destroyed.
635 * @param bounding_box Last known bounding box of the item.
638 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
641 queue_draw_item_area (item, bounding_box.get ());
644 if (_new_current_item == item) {
645 _new_current_item = 0;
648 if (_grabbed_item == item) {
652 if (_focused_item == item) {
656 ScrollGroup* sg = dynamic_cast<ScrollGroup*>(item);
658 scrollers.remove (sg);
661 if (_current_item == item) {
662 /* no need to send a leave event to this item, since it is going away
665 pick_current_item (0); // no mouse state
670 /** Handler for GDK expose events.
672 * @return true if the event was handled.
675 GtkCanvas::on_expose_event (GdkEventExpose* ev)
677 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
678 render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), cairo_context);
682 /** @return Our Cairo context, or 0 if we don't have one */
683 Cairo::RefPtr<Cairo::Context>
684 GtkCanvas::context ()
686 Glib::RefPtr<Gdk::Window> w = get_window ();
688 return Cairo::RefPtr<Cairo::Context> ();
691 return w->create_cairo_context ();
694 /** Handler for GDK button press events.
696 * @return true if the event was handled.
699 GtkCanvas::on_button_press_event (GdkEventButton* ev)
701 /* translate event coordinates from window to canvas */
703 GdkEvent copy = *((GdkEvent*)ev);
704 Duple winpos = Duple (ev->x, ev->y);
705 Duple where = window_to_canvas (winpos);
707 pick_current_item (winpos, ev->state);
709 copy.button.x = where.x;
710 copy.button.y = where.y;
712 /* Coordinates in the event will be canvas coordinates, correctly adjusted
713 for scroll if this GtkCanvas is in a GtkCanvasViewport.
716 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
717 return deliver_event (reinterpret_cast<GdkEvent*>(©));
720 /** Handler for GDK button release events.
722 * @return true if the event was handled.
725 GtkCanvas::on_button_release_event (GdkEventButton* ev)
727 /* translate event coordinates from window to canvas */
729 GdkEvent copy = *((GdkEvent*)ev);
730 Duple winpos = Duple (ev->x, ev->y);
731 Duple where = window_to_canvas (winpos);
733 pick_current_item (winpos, ev->state);
735 copy.button.x = where.x;
736 copy.button.y = where.y;
738 /* Coordinates in the event will be canvas coordinates, correctly adjusted
739 for scroll if this GtkCanvas is in a GtkCanvasViewport.
742 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
743 return deliver_event (reinterpret_cast<GdkEvent*>(©));
746 /** Handler for GDK motion events.
748 * @return true if the event was handled.
751 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
753 /* translate event coordinates from window to canvas */
755 GdkEvent copy = *((GdkEvent*)ev);
756 Duple point (ev->x, ev->y);
757 Duple where = window_to_canvas (point);
759 copy.motion.x = where.x;
760 copy.motion.y = where.y;
762 /* Coordinates in "copy" will be canvas coordinates,
765 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2 canvas @ %3, %4\n", ev->x, ev->y, copy.motion.x, copy.motion.y));
767 pick_current_item (point, ev->state);
769 /* Now deliver the motion event. It may seem a little inefficient
770 to recompute the items under the event, but the enter notify/leave
771 events may have deleted canvas items so it is important to
772 recompute the list in deliver_event.
775 return deliver_event (reinterpret_cast<GdkEvent*> (©));
779 GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev)
781 pick_current_item (Duple (ev->x, ev->y), ev->state);
786 GtkCanvas::on_leave_notify_event (GdkEventCrossing* ev)
788 _new_current_item = 0;
789 deliver_enter_leave (Duple (ev->x, ev->y), ev->state);
793 /** Called to request a redraw of our canvas.
794 * @param area Area to redraw, in window coordinates.
797 GtkCanvas::request_redraw (Rect const & request)
801 Coord const w = width ();
802 Coord const h = height ();
804 /* clamp area requested to actual visible window */
806 real_area.x0 = max (0.0, min (w, request.x0));
807 real_area.x1 = max (0.0, min (w, request.x1));
808 real_area.y0 = max (0.0, min (h, request.y0));
809 real_area.y1 = max (0.0, min (h, request.y1));
811 queue_draw_area (real_area.x0, real_area.y0, real_area.width(), real_area.height());
814 /** Called to request that we try to get a particular size for ourselves.
815 * @param size Size to request, in pixels.
818 GtkCanvas::request_size (Duple size)
822 if (req.x > INT_MAX) {
826 if (req.y > INT_MAX) {
830 set_size_request (req.x, req.y);
833 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
834 * This is typically used for dragging items around, so that they are grabbed during
836 * @param item Item to grab.
839 GtkCanvas::grab (Item* item)
841 /* XXX: should this be doing gdk_pointer_grab? */
842 _grabbed_item = item;
846 /** `Ungrab' any item that was previously grabbed */
850 /* XXX: should this be doing gdk_pointer_ungrab? */
854 /** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
856 * @param item Item to grab.
859 GtkCanvas::focus (Item* item)
861 _focused_item = item;
865 GtkCanvas::unfocus (Item* item)
867 if (item == _focused_item) {
872 /** @return The visible area of the canvas, in window coordinates */
874 GtkCanvas::visible_area () const
876 return Rect (0, 0, get_allocation().get_width (), get_allocation().get_height ());
880 GtkCanvas::width() const
882 return get_allocation().get_width();
886 GtkCanvas::height() const
888 return get_allocation().get_height();
891 /** Create a GtkCanvaSViewport.
892 * @param hadj Adjustment to use for horizontal scrolling.
893 * @param vadj Adjustment to use for vertica scrolling.
895 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
896 : Alignment (0, 0, 1.0, 1.0)
902 hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
903 vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
907 GtkCanvasViewport::scrolled ()
909 _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
913 /** Handler for when GTK asks us what minimum size we want.
914 * @param req Requsition to fill in.
917 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
919 /* force the canvas to size itself */
920 // _canvas.root()->bounding_box();