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>
29 #include <gtkmm/window.h>
31 #include "pbd/compose.h"
32 #include "pbd/stacktrace.h"
34 #include "canvas/canvas.h"
35 #include "canvas/colors.h"
36 #include "canvas/debug.h"
37 #include "canvas/line.h"
38 #include "canvas/scroll_group.h"
39 #include "canvas/utils.h"
42 using namespace ArdourCanvas;
44 uint32_t Canvas::tooltip_timeout_msecs = 750;
46 /** Construct a new Canvas */
49 , _bg_color (rgba_to_color (0, 1.0, 0.0, 1.0))
55 Canvas::scroll_to (Coord x, Coord y)
57 /* We do things this way because we do not want to recurse through
58 the canvas for every scroll. In the presence of large MIDI
59 tracks this means traversing item lists that include
60 thousands of items (notes).
62 This design limits us to moving only those items (groups, typically)
63 that should move in certain ways as we scroll. In other terms, it
64 becomes O(1) rather than O(N).
67 for (list<ScrollGroup*>::iterator i = scrollers.begin(); i != scrollers.end(); ++i) {
68 (*i)->scroll_to (Duple (x, y));
71 pick_current_item (0); // no current mouse position
75 Canvas::add_scroller (ScrollGroup& i)
77 scrollers.push_back (&i);
83 pick_current_item (0); // no current mouse position
86 /** Render an area of the canvas.
87 * @param area Area in window coordinates.
88 * @param context Cairo context to render to.
91 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
94 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
95 cerr << this << " RENDER: " << area << endl;
96 //cerr << "CANVAS @ " << this << endl;
98 //cerr << "-------------------------\n";
104 boost::optional<Rect> root_bbox = _root.bounding_box();
106 /* the root has no bounding box, so there's nothing to render */
110 boost::optional<Rect> draw = root_bbox->intersection (area);
113 /* there's a common area between the root and the requested
117 _root.render (*draw, context);
119 #if defined CANVAS_DEBUG && !PLATFORM_WINDOWS
120 if (getenv ("CANVAS_HARLEQUIN_DEBUGGING")) {
121 // This transparently colors the rect being rendered, after it has been drawn.
122 double r = (random() % 65536) /65536.0;
123 double g = (random() % 65536) /65536.0;
124 double b = (random() % 65536) /65536.0;
125 context->rectangle (draw->x0, draw->y0, draw->x1 - draw->x0, draw->y1 - draw->y0);
126 context->set_source_rgba (r, g, b, 0.25);
135 operator<< (ostream& o, Canvas& c)
142 Canvas::indent() const
146 for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
154 Canvas::render_indent() const
158 for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
166 Canvas::dump (ostream& o) const
172 /** Called when an item has been shown or hidden.
173 * @param item Item that has been shown or hidden.
176 Canvas::item_shown_or_hidden (Item* item)
178 boost::optional<Rect> bbox = item->bounding_box ();
180 if (item->item_to_window (*bbox).intersection (visible_area ())) {
181 queue_draw_item_area (item, bbox.get ());
186 /** Called when an item has a change to its visual properties
187 * that do NOT affect its bounding box.
188 * @param item Item that has been modified.
191 Canvas::item_visual_property_changed (Item* item)
193 boost::optional<Rect> bbox = item->bounding_box ();
195 if (item->item_to_window (*bbox).intersection (visible_area ())) {
196 queue_draw_item_area (item, bbox.get ());
201 /** Called when an item has changed, but not moved.
202 * @param item Item that has changed.
203 * @param pre_change_bounding_box The bounding box of item before the change,
204 * in the item's coordinates.
207 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
210 Rect window_bbox = visible_area ();
212 if (pre_change_bounding_box) {
214 if (item->item_to_window (*pre_change_bounding_box).intersection (window_bbox)) {
215 /* request a redraw of the item's old bounding box */
216 queue_draw_item_area (item, pre_change_bounding_box.get ());
220 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
221 if (post_change_bounding_box) {
223 if (item->item_to_window (*post_change_bounding_box).intersection (window_bbox)) {
224 /* request a redraw of the item's new bounding box */
225 queue_draw_item_area (item, post_change_bounding_box.get ());
231 Canvas::window_to_canvas (Duple const & d) const
233 ScrollGroup* best_group = 0;
236 /* if the coordinates are negative, clamp to zero and find the item
237 * that covers that "edge" position.
242 if (in_window.x < 0) {
245 if (in_window.y < 0) {
249 for (list<ScrollGroup*>::const_iterator s = scrollers.begin(); s != scrollers.end(); ++s) {
251 if ((*s)->covers_window (in_window)) {
254 /* XXX January 22nd 2015: leaving this in place for now
255 * but I think it fixes a bug that really should be
256 * fixed in a different way (and will be) by my next
257 * commit. But it may still be relevant.
260 /* If scroll groups overlap, choose the one with the highest sensitivity,
261 that is, choose an HV scroll group over an H or V
264 if (!best_group || sg->sensitivity() > best_group->sensitivity()) {
266 if (sg->sensitivity() == (ScrollGroup::ScrollsVertically | ScrollGroup::ScrollsHorizontally)) {
267 /* Can't do any better than this. */
275 return d.translate (best_group->scroll_offset());
282 Canvas::canvas_to_window (Duple const & d, bool rounded) const
284 /* Find the scroll group that covers d (a canvas coordinate). Scroll groups are only allowed
285 * as children of the root group, so we just scan its first level
286 * children and see what we can find.
289 std::list<Item*> const& root_children (_root.items());
293 for (std::list<Item*>::const_iterator i = root_children.begin(); i != root_children.end(); ++i) {
294 if (((sg = dynamic_cast<ScrollGroup*>(*i)) != 0) && sg->covers_canvas (d)) {
300 wd = d.translate (-sg->scroll_offset());
305 /* Note that this intentionally almost always returns integer coordinates */
315 /** Called when an item has moved.
316 * @param item Item that has moved.
317 * @param pre_change_parent_bounding_box The bounding box of the item before
318 * the move, in its parent's coordinates.
321 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
323 if (pre_change_parent_bounding_box) {
324 /* request a redraw of where the item used to be. The box has
325 * to be in parent coordinate space since the bounding box of
326 * an item does not change when moved. If we use
327 * item->item_to_canvas() on the old bounding box, we will be
329 * using the item's new position, and so will compute the wrong
330 * invalidation area. If we use the parent (which has not
331 * moved, then this will work.
333 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
336 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
337 if (post_change_bounding_box) {
338 /* request a redraw of where the item now is */
339 queue_draw_item_area (item, post_change_bounding_box.get ());
343 /** Request a redraw of a particular area in an item's coordinates.
345 * @param area Area to redraw in the item's coordinates.
348 Canvas::queue_draw_item_area (Item* item, Rect area)
350 request_redraw (item->item_to_window (area));
354 Canvas::set_tooltip_timeout (uint32_t msecs)
356 tooltip_timeout_msecs = msecs;
360 Canvas::set_background_color (Color c)
364 boost::optional<Rect> r = _root.bounding_box();
367 request_redraw (_root.item_to_window (r.get()));
372 GtkCanvas::re_enter ()
374 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, "re-enter canvas by request\n");
376 pick_current_item (0);
379 /** Construct a GtkCanvas */
380 GtkCanvas::GtkCanvas ()
382 , _new_current_item (0)
385 , current_tooltip_item (0)
388 /* these are the events we want to know about */
389 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK |
390 Gdk::SCROLL_MASK | Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK |
391 Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
395 GtkCanvas::pick_current_item (int state)
400 /* this version of ::pick_current_item() is called after an item is
401 * added or removed, so we have no coordinates to work from as is the
402 * case with a motion event. Find out where the mouse is and use that.
405 Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
407 if (pointer_window != get_window()) {
411 pick_current_item (Duple (x, y), state);
414 /** Given @param point (a position in window coordinates)
415 * and mouse state @param state, check to see if _current_item
416 * (which will be used to deliver events) should change.
419 GtkCanvas::pick_current_item (Duple const & point, int state)
421 /* we do not enter/leave items during a drag/grab */
427 /* find the items at the given window position */
429 vector<Item const *> items;
430 _root.add_items_at_point (point, items);
432 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("%1 covers %2 items\n", point, items.size()));
435 if (DEBUG_ENABLED(PBD::DEBUG::CanvasEnterLeave)) {
436 for (vector<Item const*>::const_iterator it = items.begin(); it != items.end(); ++it) {
438 std::cerr << "\tItem " << (*it)->whatami() << '/' << (*it)->name << " ignore events ? " << (*it)->ignore_events() << " vis ? " << (*it)->visible() << std::endl;
440 std::cerr << "\tItem " << (*it)->whatami() << '/' << " ignore events ? " << (*it)->ignore_events() << " vis ? " << (*it)->visible() << std::endl;
446 /* put all items at point that are event-sensitive and visible and NOT
447 groups into within_items. Note that items is sorted from bottom to
448 top, but we're going to reverse that for within_items so that its
449 first item is the upper-most item that can be chosen as _current_item.
452 vector<Item const *>::const_iterator i;
453 list<Item const *> within_items;
455 for (i = items.begin(); i != items.end(); ++i) {
457 Item const * possible_item = *i;
459 /* We ignore invisible items, containers and items that ignore events */
461 if (!possible_item->visible() || possible_item->ignore_events() || dynamic_cast<ArdourCanvas::Container const *>(possible_item) != 0) {
464 within_items.push_front (possible_item);
467 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("after filtering insensitive + containers, we have %1 items\n", within_items.size()));
469 if (within_items.empty()) {
471 /* no items at point, just send leave event below */
472 _new_current_item = 0;
476 if (within_items.front() == _current_item) {
477 /* uppermost item at point is already _current_item */
478 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("CURRENT ITEM %1/%2\n", _new_current_item->whatami(), _current_item->name));
482 _new_current_item = const_cast<Item*> (within_items.front());
485 if (_new_current_item != _current_item) {
486 deliver_enter_leave (point, state);
490 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("CURRENT ITEM %1/%2\n", _new_current_item->whatami(), _current_item->name));
492 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, "--- no current item\n");
497 /** Deliver a series of enter & leave events based on the pointer position being at window
498 * coordinate @param point, and pointer @param state (modifier keys, etc)
501 GtkCanvas::deliver_enter_leave (Duple const & point, int state)
503 /* setup enter & leave event structures */
505 Glib::RefPtr<Gdk::Window> win = get_window();
511 GdkEventCrossing enter_event;
512 enter_event.type = GDK_ENTER_NOTIFY;
513 enter_event.window = win->gobj();
514 enter_event.send_event = 0;
515 enter_event.subwindow = 0;
516 enter_event.mode = GDK_CROSSING_NORMAL;
517 enter_event.focus = FALSE;
518 enter_event.state = state;
520 /* Events delivered to canvas items are expected to be in canvas
521 * coordinates but @param point is in window coordinates.
524 Duple c = window_to_canvas (point);
528 GdkEventCrossing leave_event = enter_event;
529 leave_event.type = GDK_LEAVE_NOTIFY;
532 GdkNotifyType enter_detail = GDK_NOTIFY_UNKNOWN;
533 GdkNotifyType leave_detail = GDK_NOTIFY_UNKNOWN;
534 vector<Item*> items_to_leave_virtual;
535 vector<Item*> items_to_enter_virtual;
537 if (_new_current_item == 0) {
539 leave_detail = GDK_NOTIFY_UNKNOWN;
543 /* no current item, so also send virtual leave events to the
544 * entire heirarchy for the current item
547 for (i = _current_item->parent(); i ; i = i->parent()) {
548 items_to_leave_virtual.push_back (i);
552 } else if (_current_item == 0) {
554 enter_detail = GDK_NOTIFY_UNKNOWN;
556 /* no current item, so also send virtual enter events to the
557 * entire heirarchy for the new item
560 for (i = _new_current_item->parent(); i ; i = i->parent()) {
561 items_to_enter_virtual.push_back (i);
564 } else if (_current_item->is_descendant_of (*_new_current_item)) {
566 /* move from descendant to ancestor (X: "_current_item is an
567 * inferior ("child") of _new_current_item")
569 * Deliver "virtual" leave notifications to all items in the
570 * heirarchy between current and new_current.
573 for (i = _current_item->parent(); i && i != _new_current_item; i = i->parent()) {
574 items_to_leave_virtual.push_back (i);
577 enter_detail = GDK_NOTIFY_INFERIOR;
578 leave_detail = GDK_NOTIFY_ANCESTOR;
580 } else if (_new_current_item->is_descendant_of (*_current_item)) {
581 /* move from ancestor to descendant (X: "_new_current_item is
582 * an inferior ("child") of _current_item")
584 * Deliver "virtual" enter notifications to all items in the
585 * heirarchy between current and new_current.
588 for (i = _new_current_item->parent(); i && i != _current_item; i = i->parent()) {
589 items_to_enter_virtual.push_back (i);
592 enter_detail = GDK_NOTIFY_ANCESTOR;
593 leave_detail = GDK_NOTIFY_INFERIOR;
597 Item const * common_ancestor = _current_item->closest_ancestor_with (*_new_current_item);
599 /* deliver virtual leave events to everything between _current
600 * and common_ancestor.
603 for (i = _current_item->parent(); i && i != common_ancestor; i = i->parent()) {
604 items_to_leave_virtual.push_back (i);
607 /* deliver virtual enter events to everything between
608 * _new_current and common_ancestor.
611 for (i = _new_current_item->parent(); i && i != common_ancestor; i = i->parent()) {
612 items_to_enter_virtual.push_back (i);
615 enter_detail = GDK_NOTIFY_NONLINEAR;
616 leave_detail = GDK_NOTIFY_NONLINEAR;
620 if (_current_item && !_current_item->ignore_events ()) {
621 leave_event.detail = leave_detail;
622 _current_item->Event ((GdkEvent*)&leave_event);
623 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("LEAVE %1/%2\n", _current_item->whatami(), _current_item->name));
626 leave_event.detail = GDK_NOTIFY_VIRTUAL;
627 enter_event.detail = GDK_NOTIFY_VIRTUAL;
629 for (vector<Item*>::iterator it = items_to_leave_virtual.begin(); it != items_to_leave_virtual.end(); ++it) {
630 if (!(*it)->ignore_events()) {
631 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("leave %1/%2\n", (*it)->whatami(), (*it)->name));
632 (*it)->Event ((GdkEvent*)&leave_event);
636 for (vector<Item*>::iterator it = items_to_enter_virtual.begin(); it != items_to_enter_virtual.end(); ++it) {
637 if (!(*it)->ignore_events()) {
638 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("enter %1/%2\n", (*it)->whatami(), (*it)->name));
639 (*it)->Event ((GdkEvent*)&enter_event);
640 // std::cerr << "enter " << (*it)->whatami() << '/' << (*it)->name << std::endl;
644 if (_new_current_item && !_new_current_item->ignore_events()) {
645 enter_event.detail = enter_detail;
646 DEBUG_TRACE (PBD::DEBUG::CanvasEnterLeave, string_compose ("ENTER %1/%2\n", _new_current_item->whatami(), _new_current_item->name));
647 start_tooltip_timeout (_new_current_item);
648 _new_current_item->Event ((GdkEvent*)&enter_event);
651 _current_item = _new_current_item;
655 /** Deliver an event to the appropriate item; either the grabbed item, or
656 * one of the items underneath the event.
657 * @param point Position that the event has occurred at, in canvas coordinates.
658 * @param event The event.
661 GtkCanvas::deliver_event (GdkEvent* event)
663 /* Point in in canvas coordinate space */
665 const Item* event_item;
668 /* we have a grabbed item, so everything gets sent there */
669 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
670 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
671 event_item = _grabbed_item;
673 event_item = _current_item;
680 /* run through the items from child to parent, until one claims the event */
682 Item* item = const_cast<Item*> (event_item);
686 Item* parent = item->parent ();
688 if (!item->ignore_events () &&
689 item->Event (event)) {
690 /* this item has just handled the event */
692 PBD::DEBUG::CanvasEvents,
693 string_compose ("canvas event handled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name)
699 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)));
701 if ((item = parent) == 0) {
710 /** Called when an item is being destroyed.
711 * @param item Item being destroyed.
712 * @param bounding_box Last known bounding box of the item.
715 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
718 queue_draw_item_area (item, bounding_box.get ());
721 if (_new_current_item == item) {
722 _new_current_item = 0;
725 if (_grabbed_item == item) {
729 if (_focused_item == item) {
733 if (current_tooltip_item) {
734 current_tooltip_item = 0;
735 stop_tooltip_timeout ();
738 ScrollGroup* sg = dynamic_cast<ScrollGroup*>(item);
740 scrollers.remove (sg);
743 if (_current_item == item) {
744 /* no need to send a leave event to this item, since it is going away
747 pick_current_item (0); // no mouse state
753 GtkCanvas::on_size_allocate (Gtk::Allocation& a)
755 EventBox::on_size_allocate (a);
756 #ifdef USE_CAIRO_IMAGE_SURFACE
757 /* allocate an image surface as large as the canvas itself */
759 canvas_image.clear ();
760 canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, a.get_width(), a.get_height());
764 /** Handler for GDK expose events.
766 * @return true if the event was handled.
769 GtkCanvas::on_expose_event (GdkEventExpose* ev)
771 #ifdef USE_CAIRO_IMAGE_SURFACE
773 canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
775 Cairo::RefPtr<Cairo::Context> draw_context = Cairo::Context::create (canvas_image);
776 Cairo::RefPtr<Cairo::Context> window_context = get_window()->create_cairo_context ();
778 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
781 /* draw background color */
783 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
784 draw_context->clip_preserve ();
785 set_source_rgba (draw_context, _bg_color);
786 draw_context->fill ();
790 render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), draw_context);
792 #ifdef USE_CAIRO_IMAGE_SURFACE
793 /* now blit our private surface back to the GDK one */
795 window_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
796 window_context->clip ();
797 window_context->set_source (canvas_image, 0, 0);
798 window_context->set_operator (Cairo::OPERATOR_SOURCE);
799 window_context->paint ();
805 /** Handler for GDK scroll events.
807 * @return true if the event was handled.
810 GtkCanvas::on_scroll_event (GdkEventScroll* ev)
812 /* translate event coordinates from window to canvas */
814 GdkEvent copy = *((GdkEvent*)ev);
815 Duple winpos = Duple (ev->x, ev->y);
816 Duple where = window_to_canvas (winpos);
818 pick_current_item (winpos, ev->state);
820 copy.button.x = where.x;
821 copy.button.y = where.y;
823 /* Coordinates in the event will be canvas coordinates, correctly adjusted
824 for scroll if this GtkCanvas is in a GtkCanvasViewport.
827 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas scroll @ %1, %2 => %3\n", ev->x, ev->y, where));
828 return deliver_event (reinterpret_cast<GdkEvent*>(©));
831 /** Handler for GDK key press events.
833 * @return true if the event was handled.
836 GtkCanvas::on_key_press_event (GdkEventKey* ev)
838 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, "canvas key press\n");
839 return deliver_event (reinterpret_cast<GdkEvent*>(ev));
842 /** Handler for GDK key release events.
844 * @return true if the event was handled.
847 GtkCanvas::on_key_release_event (GdkEventKey* ev)
849 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, "canvas key release\n");
850 return deliver_event (reinterpret_cast<GdkEvent*>(ev));
853 /** Handler for GDK button press events.
855 * @return true if the event was handled.
858 GtkCanvas::on_button_press_event (GdkEventButton* ev)
860 /* translate event coordinates from window to canvas */
862 GdkEvent copy = *((GdkEvent*)ev);
863 Duple winpos = Duple (ev->x, ev->y);
864 Duple where = window_to_canvas (winpos);
866 pick_current_item (winpos, ev->state);
868 copy.button.x = where.x;
869 copy.button.y = where.y;
871 /* Coordinates in the event will be canvas coordinates, correctly adjusted
872 for scroll if this GtkCanvas is in a GtkCanvasViewport.
875 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
876 return deliver_event (reinterpret_cast<GdkEvent*>(©));
879 /** Handler for GDK button release events.
881 * @return true if the event was handled.
884 GtkCanvas::on_button_release_event (GdkEventButton* ev)
886 /* translate event coordinates from window to canvas */
888 GdkEvent copy = *((GdkEvent*)ev);
889 Duple winpos = Duple (ev->x, ev->y);
890 Duple where = window_to_canvas (winpos);
892 pick_current_item (winpos, ev->state);
894 copy.button.x = where.x;
895 copy.button.y = where.y;
897 /* Coordinates in the event will be canvas coordinates, correctly adjusted
898 for scroll if this GtkCanvas is in a GtkCanvasViewport.
901 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
902 return deliver_event (reinterpret_cast<GdkEvent*>(©));
906 GtkCanvas::get_mouse_position (Duple& winpos) const
910 Gdk::ModifierType mask;
911 Glib::RefPtr<Gdk::Window> self = Glib::RefPtr<Gdk::Window>::cast_const (get_window ());
914 std::cerr << " no self window\n";
915 winpos = Duple (0, 0);
919 Glib::RefPtr<Gdk::Window> win = self->get_pointer (x, y, mask);
927 /** Handler for GDK motion events.
929 * @return true if the event was handled.
932 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
936 /* translate event coordinates from window to canvas */
938 GdkEvent copy = *((GdkEvent*)ev);
939 Duple point (ev->x, ev->y);
940 Duple where = window_to_canvas (point);
942 copy.motion.x = where.x;
943 copy.motion.y = where.y;
945 /* Coordinates in "copy" will be canvas coordinates,
948 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));
950 MouseMotion (point); /* EMIT SIGNAL */
952 pick_current_item (point, ev->state);
954 /* Now deliver the motion event. It may seem a little inefficient
955 to recompute the items under the event, but the enter notify/leave
956 events may have deleted canvas items so it is important to
957 recompute the list in deliver_event.
960 return deliver_event (reinterpret_cast<GdkEvent*> (©));
964 GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev)
966 pick_current_item (Duple (ev->x, ev->y), ev->state);
971 GtkCanvas::on_leave_notify_event (GdkEventCrossing* ev)
973 switch (ev->detail) {
974 case GDK_NOTIFY_ANCESTOR:
975 case GDK_NOTIFY_UNKNOWN:
976 case GDK_NOTIFY_VIRTUAL:
977 case GDK_NOTIFY_NONLINEAR:
978 case GDK_NOTIFY_NONLINEAR_VIRTUAL:
979 /* leaving window, cancel any tooltips */
980 stop_tooltip_timeout ();
984 /* we don't care about any other kind
985 of leave event (notably GDK_NOTIFY_INFERIOR)
989 _new_current_item = 0;
990 deliver_enter_leave (Duple (ev->x, ev->y), ev->state);
994 /** Called to request a redraw of our canvas.
995 * @param area Area to redraw, in window coordinates.
998 GtkCanvas::request_redraw (Rect const & request)
1002 Coord const w = width ();
1003 Coord const h = height ();
1005 /* clamp area requested to actual visible window */
1007 real_area.x0 = max (0.0, min (w, request.x0));
1008 real_area.x1 = max (0.0, min (w, request.x1));
1009 real_area.y0 = max (0.0, min (h, request.y0));
1010 real_area.y1 = max (0.0, min (h, request.y1));
1012 queue_draw_area (real_area.x0, real_area.y0, real_area.width(), real_area.height());
1015 /** Called to request that we try to get a particular size for ourselves.
1016 * @param size Size to request, in pixels.
1019 GtkCanvas::request_size (Duple size)
1023 if (req.x > INT_MAX) {
1027 if (req.y > INT_MAX) {
1031 set_size_request (req.x, req.y);
1034 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
1035 * This is typically used for dragging items around, so that they are grabbed during
1037 * @param item Item to grab.
1040 GtkCanvas::grab (Item* item)
1042 /* XXX: should this be doing gdk_pointer_grab? */
1043 _grabbed_item = item;
1047 /** `Ungrab' any item that was previously grabbed */
1049 GtkCanvas::ungrab ()
1051 /* XXX: should this be doing gdk_pointer_ungrab? */
1055 /** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
1057 * @param item Item to grab.
1060 GtkCanvas::focus (Item* item)
1062 _focused_item = item;
1066 GtkCanvas::unfocus (Item* item)
1068 if (item == _focused_item) {
1073 /** @return The visible area of the canvas, in window coordinates */
1075 GtkCanvas::visible_area () const
1077 return Rect (0, 0, get_allocation().get_width (), get_allocation().get_height ());
1081 GtkCanvas::width() const
1083 return get_allocation().get_width();
1087 GtkCanvas::height() const
1089 return get_allocation().get_height();
1093 GtkCanvas::start_tooltip_timeout (Item* item)
1095 stop_tooltip_timeout ();
1098 current_tooltip_item = item;
1100 /* wait for the first idle that happens after this is
1101 called. this means that we've stopped processing events, which
1102 in turn implies that the user has stopped doing stuff for a
1106 Glib::signal_idle().connect (sigc::mem_fun (*this, &GtkCanvas::really_start_tooltip_timeout));
1111 GtkCanvas::really_start_tooltip_timeout ()
1113 /* an idle has occured since we entered a tooltip-bearing widget. Now
1114 * wait 1 second and if the timeout isn't cancelled, show the tooltip.
1117 if (current_tooltip_item) {
1118 tooltip_timeout_connection = Glib::signal_timeout().connect (sigc::mem_fun (*this, &GtkCanvas::show_tooltip), tooltip_timeout_msecs);
1121 return false; /* this is called from an idle callback, don't call it again */
1125 GtkCanvas::stop_tooltip_timeout ()
1127 current_tooltip_item = 0;
1128 tooltip_timeout_connection.disconnect ();
1132 GtkCanvas::show_tooltip ()
1134 Rect tooltip_item_bbox;
1136 if (!current_tooltip_item || current_tooltip_item->tooltip().empty() || !current_tooltip_item->bounding_box()) {
1140 if (!tooltip_window) {
1141 tooltip_window = new Gtk::Window (Gtk::WINDOW_POPUP);
1142 tooltip_label = manage (new Gtk::Label);
1143 tooltip_label->show ();
1144 tooltip_window->add (*tooltip_label);
1145 tooltip_window->set_border_width (6);
1146 tooltip_window->set_name ("tooltip");
1149 tooltip_label->set_text (current_tooltip_item->tooltip());
1151 /* figure out where to position the tooltip */
1153 Gtk::Widget* toplevel = get_toplevel();
1155 int pointer_x, pointer_y;
1156 Gdk::ModifierType mask;
1158 (void) toplevel->get_window()->get_pointer (pointer_x, pointer_y, mask);
1160 Duple tooltip_window_origin (pointer_x, pointer_y);
1162 /* convert to root window coordinates */
1165 dynamic_cast<Gtk::Window*>(toplevel)->get_position (win_x, win_y);
1167 tooltip_window_origin = tooltip_window_origin.translate (Duple (win_x, win_y));
1169 /* we don't want the pointer to be inside the window when it is
1170 * displayed, because then we generate a leave/enter event pair when
1171 * the window is displayed then hidden - the enter event will
1172 * trigger a new tooltip timeout.
1174 * So move the window right of the pointer position by just a enough
1175 * to get it away from the pointer.
1178 tooltip_window_origin.x += 20;
1180 /* move the tooltip window into position */
1182 tooltip_window->move (tooltip_window_origin.x, tooltip_window_origin.y);
1186 tooltip_window->present ();
1188 /* called from a timeout handler, don't call it again */
1194 GtkCanvas::hide_tooltip ()
1196 /* hide it if its there */
1198 if (tooltip_window) {
1199 tooltip_window->hide ();
1203 /** Create a GtkCanvaSViewport.
1204 * @param hadj Adjustment to use for horizontal scrolling.
1205 * @param vadj Adjustment to use for vertica scrolling.
1207 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
1208 : Alignment (0, 0, 1.0, 1.0)
1209 , hadjustment (hadj)
1210 , vadjustment (vadj)
1214 hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
1215 vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
1219 GtkCanvasViewport::scrolled ()
1221 _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
1225 /** Handler for when GTK asks us what minimum size we want.
1226 * @param req Requsition to fill in.
1229 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
1231 /* force the canvas to size itself */
1232 // _canvas.root()->bounding_box();