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.
26 #include <gtkmm/adjustment.h>
27 #include <gtkmm/label.h>
29 #include "pbd/compose.h"
30 #include "pbd/stacktrace.h"
32 #include "canvas/canvas.h"
33 #include "canvas/debug.h"
36 using namespace ArdourCanvas;
38 /** Construct a new Canvas */
42 , _scroll_offset_x (0)
43 , _scroll_offset_y (0)
49 Canvas::scroll_to (Coord x, Coord y)
55 /** Render an area of the canvas.
56 * @param area Area in canvas coordinates.
57 * @param context Cairo context to render to.
60 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
63 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
64 cerr << "RENDER: " << area << endl;
65 //cerr << "CANVAS @ " << this << endl;
67 //cerr << "-------------------------\n";
71 // checkpoint ("render", "-> render");
77 if (getenv ("ARDOUR_REDRAW_CANVAS")) {
78 /* light up the canvas to show redraws */
79 context->set_source_rgba (random()%255 / 255.0,
83 context->rectangle (area.x0, area.y0, area.width(), area.height());
88 /* clip to the requested area */
89 context->rectangle (area.x0, area.y0, area.width(), area.height());
92 boost::optional<Rect> root_bbox = _root.bounding_box();
94 /* the root has no bounding box, so there's nothing to render */
95 // checkpoint ("render", "no root bbox");
100 boost::optional<Rect> draw = root_bbox.get().intersection (area);
102 /* there's a common area between the root and the requested
105 // checkpoint ("render", "... root");
108 _root.render (*draw, context);
112 _renders.push_back (area);
118 if (getenv ("ARDOUR_HARLEQUIN_CANVAS")) {
119 /* light up the canvas to show redraws */
120 context->set_source_rgba (random()%255 / 255.0,
121 random()%255 / 255.0,
122 random()%255 / 255.0,
124 context->rectangle (area.x0, area.y0, area.width(), area.height());
128 // checkpoint ("render", "<- render");
132 operator<< (ostream& o, Canvas& c)
139 Canvas::indent() const
143 for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
151 Canvas::render_indent() const
155 for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
163 Canvas::dump (ostream& o) const
169 /** Called when an item has been shown or hidden.
170 * @param item Item that has been shown or hidden.
173 Canvas::item_shown_or_hidden (Item* item)
175 boost::optional<Rect> bbox = item->bounding_box ();
177 queue_draw_item_area (item, bbox.get ());
181 /** Called when an item has a change to its visual properties
182 * that do NOT affect its bounding box.
183 * @param item Item that has been modified.
186 Canvas::item_visual_property_changed (Item* item)
188 boost::optional<Rect> bbox = item->bounding_box ();
190 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)
202 if (pre_change_bounding_box) {
203 /* request a redraw of the item's old bounding box */
204 queue_draw_item_area (item, pre_change_bounding_box.get ());
207 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
208 if (post_change_bounding_box) {
209 /* request a redraw of the item's new bounding box */
210 queue_draw_item_area (item, post_change_bounding_box.get ());
215 Canvas::window_to_canvas (Duple const & d) const
217 return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
221 Canvas::canvas_to_window (Duple const & d) const
223 return d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
227 Canvas::window_to_canvas (Rect const & r) const
229 return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
233 Canvas::canvas_to_window (Rect const & r) const
235 return r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
238 /** Called when an item has moved.
239 * @param item Item that has moved.
240 * @param pre_change_parent_bounding_box The bounding box of the item before
241 * the move, in its parent's coordinates.
244 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
246 if (pre_change_parent_bounding_box) {
247 /* request a redraw of where the item used to be. The box has
248 * to be in parent coordinate space since the bounding box of
249 * an item does not change when moved. If we use
250 * item->item_to_canvas() on the old bounding box, we will be
251 * using the item's new position, and so will compute the wrong
252 * invalidation area. If we use the parent (which has not
253 * moved, then this will work.
256 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
259 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
260 if (post_change_bounding_box) {
261 /* request a redraw of where the item now is */
262 queue_draw_item_area (item, post_change_bounding_box.get ());
266 /** Request a redraw of a particular area in an item's coordinates.
268 * @param area Area to redraw in the item's coordinates.
271 Canvas::queue_draw_item_area (Item* item, Rect area)
273 ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
274 // cerr << "CANVAS Invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
275 request_redraw (canvas_area);
278 /** Construct a GtkCanvas */
279 GtkCanvas::GtkCanvas ()
283 /* these are the events we want to know about */
284 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
287 /** Handler for button presses on the canvas.
288 * @param ev GDK event.
291 GtkCanvas::button_handler (GdkEventButton* ev)
293 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button %3 %1 %1\n", ev->x, ev->y, (ev->type == GDK_BUTTON_PRESS ? "press" : "release")));
294 /* The Duple that we are passing in here is in canvas coordinates */
295 return deliver_event (Duple (ev->x, ev->y), reinterpret_cast<GdkEvent*> (ev));
298 /** Handler for pointer motion events on the canvas.
299 * @param ev GDK event.
300 * @return true if the motion event was handled, otherwise false.
303 GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
306 /* if we have a grabbed item, it gets just the motion event,
307 since no enter/leave events can have happened.
309 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n",
310 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
311 return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (ev));
314 Duple point (ev->x, ev->y);
316 enter_leave_items (point);
318 /* Now deliver the motion event. It may seem a little inefficient
319 to recompute the items under the event, but the enter notify/leave
320 events may have deleted canvas items so it is important to
321 recompute the list in deliver_event.
323 return deliver_event (point, reinterpret_cast<GdkEvent*> (ev));
327 GtkCanvas::enter_leave_items ()
332 /* this version of ::enter_leave_items() is called after an item is
333 * added or removed, so we have no coordinates to work from as is the
334 * case with a motion event. Find out where the mouse is and use that.
337 Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
339 if (pointer_window != get_window()) {
343 enter_leave_items (window_to_canvas (Duple (x, y)));
347 GtkCanvas::enter_leave_items (Duple const & point)
349 /* find the items at the given position */
351 vector<Item const *> items;
352 _root.add_items_at_point (point, items);
357 GdkEventCrossing leave_event;
358 leave_event.type = GDK_LEAVE_NOTIFY;
359 leave_event.x = point.x;
360 leave_event.y = point.y;
361 cerr << "Leaving (without entering)" << _current_item->name << endl;
362 _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
368 /* items is sorted from bottom to top, so reverse through it from top
369 * to bottom to find the first event-sensitive item and notify that
373 for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
375 Item const * new_item = *i;
377 if (new_item->ignore_events()) {
380 if (_current_item && _current_item != new_item) {
382 GdkEventCrossing leave_event;
383 leave_event.type = GDK_LEAVE_NOTIFY;
384 leave_event.x = point.x;
385 leave_event.y = point.y;
386 cerr << "Leaving " << _current_item->name << endl;
387 _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
390 if (new_item && _current_item != new_item) {
392 GdkEventCrossing enter_event;
393 enter_event.type = GDK_ENTER_NOTIFY;
394 enter_event.x = point.x;
395 enter_event.y = point.y;
396 cerr << "Entering (" << new_item->name << ") " << new_item->whatami() << endl;
397 new_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
400 _current_item = new_item;
405 /** Deliver an event to the appropriate item; either the grabbed item, or
406 * one of the items underneath the event.
407 * @param point Position that the event has occurred at, in canvas coordinates.
408 * @param event The event.
411 GtkCanvas::deliver_event (Duple point, GdkEvent* event)
414 /* we have a grabbed item, so everything gets sent there */
415 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
416 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
417 return _grabbed_item->Event (event);
420 /* find the items that exist at the event's position */
421 vector<Item const *> items;
422 _root.add_items_at_point (point, items);
424 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 possible items to deliver event to\n", items.size()));
426 /* run through the items under the event, from top to bottom, until one claims the event */
427 vector<Item const *>::const_reverse_iterator i = items.rbegin ();
428 while (i != items.rend()) {
430 if ((*i)->ignore_events ()) {
432 PBD::DEBUG::CanvasEvents,
433 string_compose ("canvas event ignored by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
439 if ((*i)->Event (event)) {
440 /* this item has just handled the event */
442 PBD::DEBUG::CanvasEvents,
443 string_compose ("canvas event handled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
450 PBD::DEBUG::CanvasEvents,
451 string_compose ("canvas event left unhandled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
458 if (PBD::debug_bits & PBD::DEBUG::CanvasEvents) {
459 while (i != items.rend()) {
460 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event not seen by %1\n", (*i)->name.empty() ? "[unknown]" : (*i)->name));
468 /** Called when an item is being destroyed.
469 * @param item Item being destroyed.
470 * @param bounding_box Last known bounding box of the item.
473 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
476 queue_draw_item_area (item, bounding_box.get ());
479 if (_current_item == item) {
483 if (_grabbed_item == item) {
487 enter_leave_items ();
491 /** Handler for GDK expose events.
493 * @return true if the event was handled.
496 GtkCanvas::on_expose_event (GdkEventExpose* ev)
499 Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
502 * 0,0 _scroll_offset_x, _scroll_offset_y
505 /* render using canvas coordinates */
507 Rect canvas_area (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height);
508 canvas_area = canvas_area.translate (Duple (_scroll_offset_x, _scroll_offset_y));
510 /* things are going to render to the cairo surface with canvas
513 * an item at window/cairo 0,0 will have canvas_coords _scroll_offset_x,_scroll_offset_y
515 * let them render at their natural coordinates by using cairo_translate()
518 c->translate (-_scroll_offset_x, -_scroll_offset_y);
520 render (canvas_area, c);
525 /** @return Our Cairo context, or 0 if we don't have one */
526 Cairo::RefPtr<Cairo::Context>
527 GtkCanvas::context ()
529 Glib::RefPtr<Gdk::Window> w = get_window ();
531 return Cairo::RefPtr<Cairo::Context> ();
534 return w->create_cairo_context ();
537 /** Handler for GDK button press events.
539 * @return true if the event was handled.
542 GtkCanvas::on_button_press_event (GdkEventButton* ev)
544 /* translate event coordinates from window to canvas */
546 GdkEvent copy = *((GdkEvent*)ev);
547 Duple where = window_to_canvas (Duple (ev->x, ev->y));
549 copy.button.x = where.x;
550 copy.button.y = where.y;
552 /* Coordinates in the event will be canvas coordinates, correctly adjusted
553 for scroll if this GtkCanvas is in a GtkCanvasViewport.
555 return button_handler ((GdkEventButton*) ©);
558 /** Handler for GDK button release events.
560 * @return true if the event was handled.
563 GtkCanvas::on_button_release_event (GdkEventButton* ev)
565 /* translate event coordinates from window to canvas */
567 GdkEvent copy = *((GdkEvent*)ev);
568 Duple where = window_to_canvas (Duple (ev->x, ev->y));
570 copy.button.x = where.x;
571 copy.button.y = where.y;
573 /* Coordinates in the event will be canvas coordinates, correctly adjusted
574 for scroll if this GtkCanvas is in a GtkCanvasViewport.
576 return button_handler ((GdkEventButton*) ©);
579 /** Handler for GDK motion events.
581 * @return true if the event was handled.
584 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
586 /* translate event coordinates from window to canvas */
588 GdkEvent copy = *((GdkEvent*)ev);
589 Duple where = window_to_canvas (Duple (ev->x, ev->y));
591 copy.motion.x = where.x;
592 copy.motion.y = where.y;
594 /* Coordinates in the event will be canvas coordinates, correctly adjusted
595 for scroll if this GtkCanvas is in a GtkCanvasViewport.
597 return motion_notify_handler ((GdkEventMotion*) ©);
600 /** Called to request a redraw of our canvas.
601 * @param area Area to redraw, in canvas coordinates.
604 GtkCanvas::request_redraw (Rect const & request)
606 Rect area = canvas_to_window (request);
607 // cerr << "Invalidate " << request << " TRANSLATE AS " << area << endl;
608 queue_draw_area (floor (area.x0), floor (area.y0), ceil (area.x1) - floor (area.x0), ceil (area.y1) - floor (area.y0));
611 /** Called to request that we try to get a particular size for ourselves.
612 * @param size Size to request, in pixels.
615 GtkCanvas::request_size (Duple size)
619 if (req.x > INT_MAX) {
623 if (req.y > INT_MAX) {
627 set_size_request (req.x, req.y);
630 /** `Grab' an item, so that all events are sent to that item until it is `ungrabbed'.
631 * This is typically used for dragging items around, so that they are grabbed during
633 * @param item Item to grab.
636 GtkCanvas::grab (Item* item)
638 /* XXX: should this be doing gdk_pointer_grab? */
639 _grabbed_item = item;
642 /** `Ungrab' any item that was previously grabbed */
646 /* XXX: should this be doing gdk_pointer_ungrab? */
650 /** @return The visible area of the canvas, in canvas coordinates */
652 GtkCanvas::visible_area () const
654 Distance const xo = _scroll_offset_x;
655 Distance const yo = _scroll_offset_y;
656 return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
659 /** Create a GtkCanvaSViewport.
660 * @param hadj Adjustment to use for horizontal scrolling.
661 * @param vadj Adjustment to use for vertica scrolling.
663 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
664 : Alignment (0, 0, 1.0, 1.0)
670 hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
671 vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
675 GtkCanvasViewport::scrolled ()
677 _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
681 /** Handler for when GTK asks us what minimum size we want.
682 * @param req Requsition to fill in.
685 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
687 /* force the canvas to size itself */
688 // _canvas.root()->bounding_box();