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 enter_leave_items (0); // no current mouse position
60 enter_leave_items (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 // context->rectangle (area.x0, area.y0, area.x1 - area.x0, area.y1 - area.y0);
91 // context->set_source_rgba (1.0, 0, 0, 1.0);
94 /* there's a common area between the root and the requested
98 _root.render (*draw, context);
104 operator<< (ostream& o, Canvas& c)
111 Canvas::indent() const
115 for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
123 Canvas::render_indent() const
127 for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
135 Canvas::dump (ostream& o) const
141 /** Called when an item has been shown or hidden.
142 * @param item Item that has been shown or hidden.
145 Canvas::item_shown_or_hidden (Item* item)
147 boost::optional<Rect> bbox = item->bounding_box ();
149 queue_draw_item_area (item, bbox.get ());
153 /** Called when an item has a change to its visual properties
154 * that do NOT affect its bounding box.
155 * @param item Item that has been modified.
158 Canvas::item_visual_property_changed (Item* item)
160 boost::optional<Rect> bbox = item->bounding_box ();
162 queue_draw_item_area (item, bbox.get ());
166 /** Called when an item has changed, but not moved.
167 * @param item Item that has changed.
168 * @param pre_change_bounding_box The bounding box of item before the change,
169 * in the item's coordinates.
172 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
174 if (pre_change_bounding_box) {
175 /* request a redraw of the item's old bounding box */
176 queue_draw_item_area (item, pre_change_bounding_box.get ());
179 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
180 if (post_change_bounding_box) {
181 /* request a redraw of the item's new bounding box */
182 queue_draw_item_area (item, post_change_bounding_box.get ());
187 Canvas::window_to_canvas (Duple const & d) const
189 return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
193 Canvas::canvas_to_window (Duple const & d) const
195 Duple wd = d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
202 Canvas::window_to_canvas (Rect const & r) const
204 return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
208 Canvas::canvas_to_window (Rect const & r) const
210 Rect wr = r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
211 wr.x0 = floor (wr.x0);
212 wr.x1 = ceil (wr.x1);
213 wr.y0 = floor (wr.y0);
214 wr.y1 = ceil (wr.y1);
218 /** Called when an item has moved.
219 * @param item Item that has moved.
220 * @param pre_change_parent_bounding_box The bounding box of the item before
221 * the move, in its parent's coordinates.
224 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
226 if (pre_change_parent_bounding_box) {
227 /* request a redraw of where the item used to be. The box has
228 * to be in parent coordinate space since the bounding box of
229 * an item does not change when moved. If we use
230 * item->item_to_canvas() on the old bounding box, we will be
232 * using the item's new position, and so will compute the wrong
233 * invalidation area. If we use the parent (which has not
234 * moved, then this will work.
236 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
239 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
240 if (post_change_bounding_box) {
241 /* request a redraw of where the item now is */
242 queue_draw_item_area (item, post_change_bounding_box.get ());
246 /** Request a redraw of a particular area in an item's coordinates.
248 * @param area Area to redraw in the item's coordinates.
251 Canvas::queue_draw_item_area (Item* item, Rect area)
253 ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
254 // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
255 request_redraw (canvas_area);
258 /** Construct a GtkCanvas */
259 GtkCanvas::GtkCanvas ()
261 , _new_current_item (0)
265 /* these are the events we want to know about */
266 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK |
267 Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
271 GtkCanvas::enter_leave_items (int state)
276 /* this version of ::enter_leave_items() is called after an item is
277 * added or removed, so we have no coordinates to work from as is the
278 * case with a motion event. Find out where the mouse is and use that.
281 Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
283 if (pointer_window != get_window()) {
287 enter_leave_items (window_to_canvas (Duple (x, y)), state);
291 GtkCanvas::enter_leave_items (Duple const & point, int state)
293 /* we do not enter/leave items during a drag/grab */
299 GdkEventCrossing enter_event;
300 enter_event.type = GDK_ENTER_NOTIFY;
301 enter_event.window = get_window()->gobj();
302 enter_event.send_event = 0;
303 enter_event.subwindow = 0;
304 enter_event.mode = GDK_CROSSING_NORMAL;
305 enter_event.focus = FALSE;
306 enter_event.state = state;
307 enter_event.x = point.x;
308 enter_event.y = point.y;
310 GdkEventCrossing leave_event = enter_event;
311 leave_event.type = GDK_LEAVE_NOTIFY;
313 /* find the items at the given position */
315 vector<Item const *> items;
316 _root.add_items_at_point (point, items);
318 /* put all items at point that are event-sensitive and visible and NOT
319 groups into within_items. Note that items is sorted from bottom to
320 top, but we're going to reverse that for within_items so that its
321 first item is the upper-most item that can be chosen as _current_item.
324 vector<Item const *>::const_iterator i;
325 list<Item const *> within_items;
327 for (i = items.begin(); i != items.end(); ++i) {
329 Item const * new_item = *i;
331 if (new_item->ignore_events() || dynamic_cast<Group const *>(new_item) != 0) {
335 within_items.push_front (new_item);
338 if (within_items.empty()) {
339 /* no items at point */
341 leave_event.detail = GDK_NOTIFY_UNKNOWN;
342 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Leave %1 %2\n", _current_item->whatami(), _current_item->name));
343 deliver_event (reinterpret_cast<GdkEvent*> (&leave_event));
349 if (within_items.front() == _current_item) {
350 /* uppermost item at point is already _current_item */
354 _new_current_item = within_items.front();
358 if (_new_current_item->is_descendant_of (*_current_item)) {
359 leave_event.detail = GDK_NOTIFY_INFERIOR;
360 enter_event.detail = GDK_NOTIFY_ANCESTOR;
361 } else if (_current_item->is_descendant_of (*_new_current_item)) {
362 leave_event.detail = GDK_NOTIFY_ANCESTOR;
363 enter_event.detail = GDK_NOTIFY_INFERIOR;
365 leave_event.detail = GDK_NOTIFY_UNKNOWN;
366 enter_event.detail = GDK_NOTIFY_UNKNOWN;
369 std::cerr << "CROSS from "
370 << _current_item->whatami() << '/' << _current_item->name
372 << _new_current_item->whatami() << '/' << _new_current_item->name
373 << " detail = " << leave_event.detail
376 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Leave %1 %2\n", _current_item->whatami(), _current_item->name));
377 deliver_event (reinterpret_cast<GdkEvent*> (&leave_event));
382 enter_event.detail = GDK_NOTIFY_UNKNOWN;
386 /* _new_current_item could potentially have been reset when handling
387 * the leave event. if it has, there is nothing to do here.
390 if (!_new_current_item) {
394 _current_item = _new_current_item;
395 deliver_event (reinterpret_cast<GdkEvent*> (&enter_event));
397 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Enter %1 %2\n", _current_item->whatami(), _current_item->name));
400 /** Deliver an event to the appropriate item; either the grabbed item, or
401 * one of the items underneath the event.
402 * @param point Position that the event has occurred at, in canvas coordinates.
403 * @param event The event.
406 GtkCanvas::deliver_event (GdkEvent* event)
408 /* Point in in canvas coordinate space */
411 /* we have a grabbed item, so everything gets sent there */
412 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
413 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
414 return _grabbed_item->Event (event);
417 if (!_current_item) {
421 /* run through the items from child to parent, until one claims the event */
423 for (Item* item = const_cast<Item*> (_current_item); item; item = item->parent()) {
425 if (item->ignore_events ()) {
427 // PBD::DEBUG::CanvasEvents,
428 // string_compose ("canvas event ignored by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name)
433 if (item->Event (event)) {
434 /* this item has just handled the event */
436 PBD::DEBUG::CanvasEvents,
437 string_compose ("canvas event handled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name)
443 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event left unhandled by %1 %2\n", item->whatami(), item->name.empty() ? "[unknown]" : item->name));
449 /** Called when an item is being destroyed.
450 * @param item Item being destroyed.
451 * @param bounding_box Last known bounding box of the item.
454 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
457 queue_draw_item_area (item, bounding_box.get ());
460 /* no need to send a leave event to this item, since it is going away
463 if (_new_current_item == item) {
464 _new_current_item = 0;
467 if (_current_item == item) {
471 if (_grabbed_item == item) {
475 if (_focused_item == item) {
479 enter_leave_items (0); // no mouse state
483 /** Handler for GDK expose events.
485 * @return true if the event was handled.
488 GtkCanvas::on_expose_event (GdkEventExpose* ev)
490 Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
492 render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
497 /** @return Our Cairo context, or 0 if we don't have one */
498 Cairo::RefPtr<Cairo::Context>
499 GtkCanvas::context ()
501 Glib::RefPtr<Gdk::Window> w = get_window ();
503 return Cairo::RefPtr<Cairo::Context> ();
506 return w->create_cairo_context ();
509 /** Handler for GDK button press events.
511 * @return true if the event was handled.
514 GtkCanvas::on_button_press_event (GdkEventButton* ev)
516 /* translate event coordinates from window to canvas */
518 GdkEvent copy = *((GdkEvent*)ev);
519 Duple where = window_to_canvas (Duple (ev->x, ev->y));
521 copy.button.x = where.x;
522 copy.button.y = where.y;
524 /* Coordinates in the event will be canvas coordinates, correctly adjusted
525 for scroll if this GtkCanvas is in a GtkCanvasViewport.
528 enter_leave_items (where, ev->state);
529 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
530 return deliver_event (reinterpret_cast<GdkEvent*>(©));
533 /** Handler for GDK button release events.
535 * @return true if the event was handled.
538 GtkCanvas::on_button_release_event (GdkEventButton* ev)
540 /* translate event coordinates from window to canvas */
542 GdkEvent copy = *((GdkEvent*)ev);
543 Duple where = window_to_canvas (Duple (ev->x, ev->y));
545 enter_leave_items (where, ev->state);
547 copy.button.x = where.x;
548 copy.button.y = where.y;
550 /* Coordinates in the event will be canvas coordinates, correctly adjusted
551 for scroll if this GtkCanvas is in a GtkCanvasViewport.
554 enter_leave_items (where, ev->state);
555 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
556 return deliver_event (reinterpret_cast<GdkEvent*>(©));
559 /** Handler for GDK motion events.
561 * @return true if the event was handled.
564 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
566 /* translate event coordinates from window to canvas */
568 GdkEvent copy = *((GdkEvent*)ev);
569 Duple point (ev->x, ev->y);
570 Duple where = window_to_canvas (point);
572 copy.motion.x = where.x;
573 copy.motion.y = where.y;
575 /* Coordinates in "copy" will be canvas coordinates,
578 // DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y));
581 /* if we have a grabbed item, it gets just the motion event,
582 since no enter/leave events can have happened.
584 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n",
585 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
586 return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (©));
589 enter_leave_items (where, ev->state);
591 /* Now deliver the motion event. It may seem a little inefficient
592 to recompute the items under the event, but the enter notify/leave
593 events may have deleted canvas items so it is important to
594 recompute the list in deliver_event.
597 return deliver_event (reinterpret_cast<GdkEvent*> (©));
601 GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev)
603 Duple where = window_to_canvas (Duple (ev->x, ev->y));
604 enter_leave_items (where, ev->state);
609 GtkCanvas::on_leave_notify_event (GdkEventCrossing* /*ev*/)
612 _new_current_item = 0;
617 GtkCanvas::on_key_press_event (GdkEventKey* ev)
623 GtkCanvas::on_key_release_event (GdkEventKey* ev)
628 /** Called to request a redraw of our canvas.
629 * @param area Area to redraw, in canvas coordinates.
632 GtkCanvas::request_redraw (Rect const & request)
634 Rect area = canvas_to_window (request);
635 queue_draw_area (floor (area.x0), floor (area.y0), ceil (area.width()), ceil (area.height()));
638 /** Called to request that we try to get a particular size for ourselves.
639 * @param size Size to request, in pixels.
642 GtkCanvas::request_size (Duple size)
646 if (req.x > INT_MAX) {
650 if (req.y > INT_MAX) {
654 set_size_request (req.x, req.y);
657 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
658 * This is typically used for dragging items around, so that they are grabbed during
660 * @param item Item to grab.
663 GtkCanvas::grab (Item* item)
665 /* XXX: should this be doing gdk_pointer_grab? */
666 _grabbed_item = item;
670 /** `Ungrab' any item that was previously grabbed */
674 /* XXX: should this be doing gdk_pointer_ungrab? */
678 /** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
680 * @param item Item to grab.
683 GtkCanvas::focus (Item* item)
685 _focused_item = item;
689 GtkCanvas::unfocus (Item* item)
691 if (item == _focused_item) {
696 /** @return The visible area of the canvas, in canvas coordinates */
698 GtkCanvas::visible_area () const
700 Distance const xo = _scroll_offset_x;
701 Distance const yo = _scroll_offset_y;
702 return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
705 /** Create a GtkCanvaSViewport.
706 * @param hadj Adjustment to use for horizontal scrolling.
707 * @param vadj Adjustment to use for vertica scrolling.
709 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
710 : Alignment (0, 0, 1.0, 1.0)
716 hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
717 vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
721 GtkCanvasViewport::scrolled ()
723 _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
727 /** Handler for when GTK asks us what minimum size we want.
728 * @param req Requsition to fill in.
731 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
733 /* force the canvas to size itself */
734 // _canvas.root()->bounding_box();