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"
37 using namespace ArdourCanvas;
39 /** Construct a new Canvas */
42 , _scroll_offset_x (0)
43 , _scroll_offset_y (0)
49 Canvas::scroll_to (Coord x, Coord y)
54 pick_current_item (0); // no current mouse position
60 pick_current_item (0); // no current mouse position
63 /** Render an area of the canvas.
64 * @param area Area in canvas coordinates.
65 * @param context Cairo context to render to.
68 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
71 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
72 cerr << "RENDER: " << area << endl;
73 //cerr << "CANVAS @ " << this << endl;
75 //cerr << "-------------------------\n";
81 boost::optional<Rect> root_bbox = _root.bounding_box();
83 /* the root has no bounding box, so there's nothing to render */
87 boost::optional<Rect> draw = root_bbox->intersection (area);
90 /* there's a common area between the root and the requested
94 _root.render (*draw, context);
96 // This outlines the rect being rendered, after it has been drawn.
97 // context->rectangle (draw->x0, draw->y0, draw->x1 - draw->x0, draw->y1 - draw->y0);
98 // context->set_source_rgba (1.0, 0, 0, 1.0);
99 // context->stroke ();
106 operator<< (ostream& o, Canvas& c)
113 Canvas::indent() const
117 for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
125 Canvas::render_indent() const
129 for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
137 Canvas::dump (ostream& o) const
143 /** Called when an item has been shown or hidden.
144 * @param item Item that has been shown or hidden.
147 Canvas::item_shown_or_hidden (Item* item)
149 boost::optional<Rect> bbox = item->bounding_box ();
151 queue_draw_item_area (item, bbox.get ());
155 /** Called when an item has a change to its visual properties
156 * that do NOT affect its bounding box.
157 * @param item Item that has been modified.
160 Canvas::item_visual_property_changed (Item* item)
162 boost::optional<Rect> bbox = item->bounding_box ();
164 queue_draw_item_area (item, bbox.get ());
168 /** Called when an item has changed, but not moved.
169 * @param item Item that has changed.
170 * @param pre_change_bounding_box The bounding box of item before the change,
171 * in the item's coordinates.
174 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
176 if (pre_change_bounding_box) {
177 /* request a redraw of the item's old bounding box */
178 queue_draw_item_area (item, pre_change_bounding_box.get ());
181 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
182 if (post_change_bounding_box) {
183 /* request a redraw of the item's new bounding box */
184 queue_draw_item_area (item, post_change_bounding_box.get ());
189 Canvas::window_to_canvas (Duple const & d) const
191 return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
195 Canvas::canvas_to_window (Duple const & d) const
197 Duple wd = d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
199 /* Note that this intentionally always returns integer coordinates */
208 Canvas::window_to_canvas (Rect const & r) const
210 return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
214 Canvas::canvas_to_window (Rect const & r) const
216 Rect wr = r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
218 /* Note that this intentionally always returns integer coordinates */
220 wr.x0 = round (wr.x0);
221 wr.x1 = round (wr.x1);
222 wr.y0 = round (wr.y0);
223 wr.y1 = round (wr.y1);
228 /** Called when an item has moved.
229 * @param item Item that has moved.
230 * @param pre_change_parent_bounding_box The bounding box of the item before
231 * the move, in its parent's coordinates.
234 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
236 if (pre_change_parent_bounding_box) {
237 /* request a redraw of where the item used to be. The box has
238 * to be in parent coordinate space since the bounding box of
239 * an item does not change when moved. If we use
240 * item->item_to_canvas() on the old bounding box, we will be
242 * using the item's new position, and so will compute the wrong
243 * invalidation area. If we use the parent (which has not
244 * moved, then this will work.
246 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
249 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
250 if (post_change_bounding_box) {
251 /* request a redraw of where the item now is */
252 queue_draw_item_area (item, post_change_bounding_box.get ());
256 /** Request a redraw of a particular area in an item's coordinates.
258 * @param area Area to redraw in the item's coordinates.
261 Canvas::queue_draw_item_area (Item* item, Rect area)
263 ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
264 // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
265 request_redraw (canvas_area);
268 /** Construct a GtkCanvas */
269 GtkCanvas::GtkCanvas ()
271 , _new_current_item (0)
275 /* these are the events we want to know about */
276 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK |
277 Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
281 GtkCanvas::pick_current_item (int state)
286 /* this version of ::pick_current_item() is called after an item is
287 * added or removed, so we have no coordinates to work from as is the
288 * case with a motion event. Find out where the mouse is and use that.
291 Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
293 if (pointer_window != get_window()) {
297 pick_current_item (window_to_canvas (Duple (x, y)), state);
301 GtkCanvas::pick_current_item (Duple const & point, int state)
303 /* we do not enter/leave items during a drag/grab */
309 /* find the items at the given position */
311 vector<Item const *> items;
312 _root.add_items_at_point (point, items);
314 /* put all items at point that are event-sensitive and visible and NOT
315 groups into within_items. Note that items is sorted from bottom to
316 top, but we're going to reverse that for within_items so that its
317 first item is the upper-most item that can be chosen as _current_item.
320 vector<Item const *>::const_iterator i;
321 list<Item const *> within_items;
323 for (i = items.begin(); i != items.end(); ++i) {
325 Item const * new_item = *i;
327 /* We ignore invisible items, groups and items that ignore events */
329 if (!new_item->visible() || new_item->ignore_events() || dynamic_cast<Group const *>(new_item) != 0) {
333 within_items.push_front (new_item);
336 if (within_items.empty()) {
338 /* no items at point, just send leave event below */
342 if (within_items.front() == _current_item) {
343 /* uppermost item at point is already _current_item */
347 _new_current_item = const_cast<Item*> (within_items.front());
350 if (_new_current_item != _current_item) {
351 deliver_enter_leave (point, state);
356 GtkCanvas::deliver_enter_leave (Duple const & point, int state)
358 /* setup enter & leave event structures */
360 GdkEventCrossing enter_event;
361 enter_event.type = GDK_ENTER_NOTIFY;
362 enter_event.window = get_window()->gobj();
363 enter_event.send_event = 0;
364 enter_event.subwindow = 0;
365 enter_event.mode = GDK_CROSSING_NORMAL;
366 enter_event.focus = FALSE;
367 enter_event.state = state;
368 enter_event.x = point.x;
369 enter_event.y = point.y;
371 GdkEventCrossing leave_event = enter_event;
372 leave_event.type = GDK_LEAVE_NOTIFY;
375 GdkNotifyType enter_detail;
376 GdkNotifyType leave_detail;
377 vector<Item*> items_to_leave_virtual;
378 vector<Item*> items_to_enter_virtual;
380 if (_new_current_item == 0) {
382 leave_detail = GDK_NOTIFY_UNKNOWN;
386 /* no current item, so also send virtual leave events to the
387 * entire heirarchy for the current item
390 for (i = _current_item->parent(); i ; i = i->parent()) {
391 items_to_leave_virtual.push_back (i);
395 } else if (_current_item == 0) {
397 enter_detail = GDK_NOTIFY_UNKNOWN;
399 /* no current item, so also send virtual enter events to the
400 * entire heirarchy for the new item
403 for (i = _new_current_item->parent(); i ; i = i->parent()) {
404 items_to_enter_virtual.push_back (i);
407 } else if (_current_item->is_descendant_of (*_new_current_item)) {
409 /* move from descendant to ancestor (X: "_current_item is an
410 * inferior ("child") of _new_current_item")
412 * Deliver "virtual" leave notifications to all items in the
413 * heirarchy between current and new_current.
417 for (i = _current_item->parent(); i && i != _new_current_item; i = i->parent()) {
418 items_to_leave_virtual.push_back (i);
421 enter_detail = GDK_NOTIFY_INFERIOR;
422 leave_detail = GDK_NOTIFY_ANCESTOR;
425 } else if (_new_current_item->is_descendant_of (*_current_item)) {
426 /* move from ancestor to descendant (X: "_new_current_item is
427 * an inferior ("child") of _current_item")
429 * Deliver "virtual" enter notifications to all items in the
430 * heirarchy between current and new_current.
433 for (i = _new_current_item->parent(); i && i != _current_item; i = i->parent()) {
434 items_to_enter_virtual.push_back (i);
437 enter_detail = GDK_NOTIFY_ANCESTOR;
438 leave_detail = GDK_NOTIFY_INFERIOR;
442 Item const * common_ancestor = _current_item->closest_ancestor_with (*_new_current_item);
444 /* deliver virtual leave events to everything between _current
445 * and common_ancestor.
448 for (i = _current_item->parent(); i && i != common_ancestor; i = i->parent()) {
449 items_to_leave_virtual.push_back (i);
452 /* deliver virtual enter events to everything between
453 * _new_current and common_ancestor.
456 for (i = _new_current_item->parent(); i && i != common_ancestor; i = i->parent()) {
457 items_to_enter_virtual.push_back (i);
460 enter_detail = GDK_NOTIFY_NONLINEAR;
461 leave_detail = GDK_NOTIFY_NONLINEAR;
465 if (_current_item && !_current_item->ignore_events ()) {
466 leave_event.detail = leave_detail;
467 _current_item->Event ((GdkEvent*)&leave_event);
468 // std::cerr << "LEAVE " << _current_item->whatami() << '/' << _current_item->name << std::endl;
471 leave_event.detail = GDK_NOTIFY_VIRTUAL;
472 enter_event.detail = GDK_NOTIFY_VIRTUAL;
474 for (vector<Item*>::iterator it = items_to_leave_virtual.begin(); it != items_to_leave_virtual.end(); ++it) {
475 if (!(*it)->ignore_events()) {
476 (*it)->Event ((GdkEvent*)&leave_event);
477 // std::cerr << "leave " << (*it)->whatami() << '/' << (*it)->name << std::endl;
481 for (vector<Item*>::iterator it = items_to_enter_virtual.begin(); it != items_to_enter_virtual.end(); ++it) {
482 if (!(*it)->ignore_events()) {
483 (*it)->Event ((GdkEvent*)&enter_event);
484 // std::cerr << "enter " << (*it)->whatami() << '/' << (*it)->name << std::endl;
488 if (_new_current_item && !_new_current_item->ignore_events()) {
489 enter_event.detail = enter_detail;
490 _new_current_item->Event ((GdkEvent*)&enter_event);
491 // std::cerr << "ENTER " << _new_current_item->whatami() << '/' << _new_current_item->name << std::endl;
494 _current_item = _new_current_item;
498 /** Deliver an event to the appropriate item; either the grabbed item, or
499 * one of the items underneath the event.
500 * @param point Position that the event has occurred at, in canvas coordinates.
501 * @param event The event.
504 GtkCanvas::deliver_event (GdkEvent* event)
506 /* Point in in canvas coordinate space */
509 /* we have a grabbed item, so everything gets sent there */
510 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
511 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
512 return _grabbed_item->Event (event);
515 if (!_current_item) {
519 /* run through the items from child to parent, until one claims the event */
521 Item* item = const_cast<Item*> (_current_item);
525 Item* parent = item->parent ();
527 if (!item->ignore_events () &&
528 item->Event (event)) {
529 /* this item has just handled the event */
531 PBD::DEBUG::CanvasEvents,
532 string_compose ("canvas event handled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name)
538 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event left unhandled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name));
540 if ((item = parent) == 0) {
549 /** Called when an item is being destroyed.
550 * @param item Item being destroyed.
551 * @param bounding_box Last known bounding box of the item.
554 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
557 queue_draw_item_area (item, bounding_box.get ());
560 /* no need to send a leave event to this item, since it is going away
563 if (_new_current_item == item) {
564 _new_current_item = 0;
567 if (_current_item == item) {
571 if (_grabbed_item == item) {
575 if (_focused_item == item) {
579 pick_current_item (0); // no mouse state
583 /** Handler for GDK expose events.
585 * @return true if the event was handled.
588 GtkCanvas::on_expose_event (GdkEventExpose* ev)
590 Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
592 render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
597 /** @return Our Cairo context, or 0 if we don't have one */
598 Cairo::RefPtr<Cairo::Context>
599 GtkCanvas::context ()
601 Glib::RefPtr<Gdk::Window> w = get_window ();
603 return Cairo::RefPtr<Cairo::Context> ();
606 return w->create_cairo_context ();
609 /** Handler for GDK button press events.
611 * @return true if the event was handled.
614 GtkCanvas::on_button_press_event (GdkEventButton* ev)
616 /* translate event coordinates from window to canvas */
618 GdkEvent copy = *((GdkEvent*)ev);
619 Duple where = window_to_canvas (Duple (ev->x, ev->y));
621 copy.button.x = where.x;
622 copy.button.y = where.y;
624 /* Coordinates in the event will be canvas coordinates, correctly adjusted
625 for scroll if this GtkCanvas is in a GtkCanvasViewport.
628 pick_current_item (where, ev->state);
629 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
630 return deliver_event (reinterpret_cast<GdkEvent*>(©));
633 /** Handler for GDK button release events.
635 * @return true if the event was handled.
638 GtkCanvas::on_button_release_event (GdkEventButton* ev)
640 /* translate event coordinates from window to canvas */
642 GdkEvent copy = *((GdkEvent*)ev);
643 Duple where = window_to_canvas (Duple (ev->x, ev->y));
645 pick_current_item (where, ev->state);
647 copy.button.x = where.x;
648 copy.button.y = where.y;
650 /* Coordinates in the event will be canvas coordinates, correctly adjusted
651 for scroll if this GtkCanvas is in a GtkCanvasViewport.
654 pick_current_item (where, ev->state);
655 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
656 return deliver_event (reinterpret_cast<GdkEvent*>(©));
659 /** Handler for GDK motion events.
661 * @return true if the event was handled.
664 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
666 /* translate event coordinates from window to canvas */
668 GdkEvent copy = *((GdkEvent*)ev);
669 Duple point (ev->x, ev->y);
670 Duple where = window_to_canvas (point);
672 copy.motion.x = where.x;
673 copy.motion.y = where.y;
675 /* Coordinates in "copy" will be canvas coordinates,
678 // DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y));
681 /* if we have a grabbed item, it gets just the motion event,
682 since no enter/leave events can have happened.
684 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n",
685 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
686 return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (©));
689 pick_current_item (where, ev->state);
691 /* Now deliver the motion event. It may seem a little inefficient
692 to recompute the items under the event, but the enter notify/leave
693 events may have deleted canvas items so it is important to
694 recompute the list in deliver_event.
697 return deliver_event (reinterpret_cast<GdkEvent*> (©));
701 GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev)
703 Duple where = window_to_canvas (Duple (ev->x, ev->y));
704 pick_current_item (where, ev->state);
709 GtkCanvas::on_leave_notify_event (GdkEventCrossing* ev)
711 _new_current_item = 0;
712 Duple where = window_to_canvas (Duple (ev->x, ev->y));
713 deliver_enter_leave (where, ev->state);
717 /** Called to request a redraw of our canvas.
718 * @param area Area to redraw, in canvas coordinates.
721 GtkCanvas::request_redraw (Rect const & request)
723 Rect area = canvas_to_window (request);
724 queue_draw_area (area.x0, area.y0, area.width(), area.height());
727 /** Called to request that we try to get a particular size for ourselves.
728 * @param size Size to request, in pixels.
731 GtkCanvas::request_size (Duple size)
735 if (req.x > INT_MAX) {
739 if (req.y > INT_MAX) {
743 set_size_request (req.x, req.y);
746 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
747 * This is typically used for dragging items around, so that they are grabbed during
749 * @param item Item to grab.
752 GtkCanvas::grab (Item* item)
754 /* XXX: should this be doing gdk_pointer_grab? */
755 _grabbed_item = item;
759 /** `Ungrab' any item that was previously grabbed */
763 /* XXX: should this be doing gdk_pointer_ungrab? */
767 /** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
769 * @param item Item to grab.
772 GtkCanvas::focus (Item* item)
774 _focused_item = item;
778 GtkCanvas::unfocus (Item* item)
780 if (item == _focused_item) {
785 /** @return The visible area of the canvas, in canvas coordinates */
787 GtkCanvas::visible_area () const
789 Distance const xo = _scroll_offset_x;
790 Distance const yo = _scroll_offset_y;
791 return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
794 /** Create a GtkCanvaSViewport.
795 * @param hadj Adjustment to use for horizontal scrolling.
796 * @param vadj Adjustment to use for vertica scrolling.
798 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
799 : Alignment (0, 0, 1.0, 1.0)
805 hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
806 vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
810 GtkCanvasViewport::scrolled ()
812 _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
816 /** Handler for when GTK asks us what minimum size we want.
817 * @param req Requsition to fill in.
820 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
822 /* force the canvas to size itself */
823 // _canvas.root()->bounding_box();