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 */
41 , _scroll_offset_x (0)
42 , _scroll_offset_y (0)
48 Canvas::scroll_to (Coord x, Coord y)
53 enter_leave_items (0); // no current mouse position
59 enter_leave_items (0); // no current mouse position
62 /** Render an area of the canvas.
63 * @param area Area in canvas coordinates.
64 * @param context Cairo context to render to.
67 Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
70 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
71 cerr << "RENDER: " << area << endl;
72 //cerr << "CANVAS @ " << this << endl;
74 //cerr << "-------------------------\n";
80 boost::optional<Rect> root_bbox = _root.bounding_box();
82 /* the root has no bounding box, so there's nothing to render */
86 boost::optional<Rect> draw = root_bbox->intersection (area);
88 /* there's a common area between the root and the requested
92 _root.render (*draw, context);
97 operator<< (ostream& o, Canvas& c)
104 Canvas::indent() const
108 for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
116 Canvas::render_indent() const
120 for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
128 Canvas::dump (ostream& o) const
134 /** Called when an item has been shown or hidden.
135 * @param item Item that has been shown or hidden.
138 Canvas::item_shown_or_hidden (Item* item)
140 boost::optional<Rect> bbox = item->bounding_box ();
142 queue_draw_item_area (item, bbox.get ());
146 /** Called when an item has a change to its visual properties
147 * that do NOT affect its bounding box.
148 * @param item Item that has been modified.
151 Canvas::item_visual_property_changed (Item* item)
153 boost::optional<Rect> bbox = item->bounding_box ();
155 queue_draw_item_area (item, bbox.get ());
159 /** Called when an item has changed, but not moved.
160 * @param item Item that has changed.
161 * @param pre_change_bounding_box The bounding box of item before the change,
162 * in the item's coordinates.
165 Canvas::item_changed (Item* item, boost::optional<Rect> pre_change_bounding_box)
167 if (pre_change_bounding_box) {
168 /* request a redraw of the item's old bounding box */
169 queue_draw_item_area (item, pre_change_bounding_box.get ());
172 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
173 if (post_change_bounding_box) {
174 /* request a redraw of the item's new bounding box */
175 queue_draw_item_area (item, post_change_bounding_box.get ());
180 Canvas::window_to_canvas (Duple const & d) const
182 return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
186 Canvas::canvas_to_window (Duple const & d) const
188 return d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
192 Canvas::window_to_canvas (Rect const & r) const
194 return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
198 Canvas::canvas_to_window (Rect const & r) const
200 return r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
203 /** Called when an item has moved.
204 * @param item Item that has moved.
205 * @param pre_change_parent_bounding_box The bounding box of the item before
206 * the move, in its parent's coordinates.
209 Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
211 if (pre_change_parent_bounding_box) {
212 /* request a redraw of where the item used to be. The box has
213 * to be in parent coordinate space since the bounding box of
214 * an item does not change when moved. If we use
215 * item->item_to_canvas() on the old bounding box, we will be
217 * using the item's new position, and so will compute the wrong
218 * invalidation area. If we use the parent (which has not
219 * moved, then this will work.
221 queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
224 boost::optional<Rect> post_change_bounding_box = item->bounding_box ();
225 if (post_change_bounding_box) {
226 /* request a redraw of where the item now is */
227 queue_draw_item_area (item, post_change_bounding_box.get ());
231 /** Request a redraw of a particular area in an item's coordinates.
233 * @param area Area to redraw in the item's coordinates.
236 Canvas::queue_draw_item_area (Item* item, Rect area)
238 ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
239 // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
240 request_redraw (canvas_area);
243 /** Construct a GtkCanvas */
244 GtkCanvas::GtkCanvas ()
248 /* these are the events we want to know about */
249 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
252 /** Handler for pointer motion events on the canvas.
253 * @param ev GDK event.
254 * @return true if the motion event was handled, otherwise false.
257 GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
259 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y));
262 /* if we have a grabbed item, it gets just the motion event,
263 since no enter/leave events can have happened.
265 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n",
266 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
267 return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (ev));
270 Duple point (ev->x, ev->y);
272 enter_leave_items (point, ev->state);
274 /* Now deliver the motion event. It may seem a little inefficient
275 to recompute the items under the event, but the enter notify/leave
276 events may have deleted canvas items so it is important to
277 recompute the list in deliver_event.
279 return deliver_event (point, reinterpret_cast<GdkEvent*> (ev));
283 GtkCanvas::enter_leave_items (int state)
288 /* this version of ::enter_leave_items() is called after an item is
289 * added or removed, so we have no coordinates to work from as is the
290 * case with a motion event. Find out where the mouse is and use that.
293 Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
295 if (pointer_window != get_window()) {
299 enter_leave_items (window_to_canvas (Duple (x, y)), state);
303 GtkCanvas::enter_leave_items (Duple const & point, int state)
305 /* we do not enter/leave items during a drag/grab */
311 GdkEventCrossing enter_event;
312 enter_event.type = GDK_ENTER_NOTIFY;
313 enter_event.window = get_window()->gobj();
314 enter_event.send_event = 0;
315 enter_event.subwindow = 0;
316 enter_event.mode = GDK_CROSSING_NORMAL;
317 enter_event.detail = GDK_NOTIFY_NONLINEAR;
318 enter_event.focus = FALSE;
319 enter_event.state = state;
320 enter_event.x = point.x;
321 enter_event.y = point.y;
322 enter_event.detail = GDK_NOTIFY_UNKNOWN;
324 GdkEventCrossing leave_event = enter_event;
325 leave_event.type = GDK_LEAVE_NOTIFY;
327 /* find the items at the given position */
329 vector<Item const *> items;
330 _root.add_items_at_point (point, items);
332 /* put all items at point that are event-sensitive and visible into within_items, and if this
333 is a new addition, also put them into newly_entered for later deliver of enter events.
336 vector<Item const *>::const_iterator i;
337 vector<Item const *> newly_entered;
338 Item const * new_item;
340 for (i = items.begin(); i != items.end(); ++i) {
344 if (new_item->ignore_events() || !new_item->visible()) {
348 pair<set<Item const *>::iterator,bool> res = within_items.insert (new_item);
351 newly_entered.push_back (new_item);
355 /* for every item in "within_items", check that we are still within them. if not,
356 send a leave event, and remove them from "within_items"
359 for (set<Item const *>::const_iterator i = within_items.begin(); i != within_items.end(); ) {
361 set<Item const *>::const_iterator tmp = i;
366 boost::optional<Rect> bbox = new_item->bounding_box();
370 if (!new_item->item_to_canvas (bbox.get()).contains (point)) {
371 leave_event.detail = GDK_NOTIFY_UNKNOWN;
372 cerr << string_compose ("\tLeave %1 %2\n", new_item->whatami(), new_item->name);
373 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Leave %1 %2\n", new_item->whatami(), new_item->name));
374 (*i)->Event (reinterpret_cast<GdkEvent*> (&leave_event));
375 within_items.erase (i);
382 /* for every item in "newly_entered", send an enter event (and propagate it up the
383 item tree until it is handled
386 for (vector<Item const*>::const_iterator i = newly_entered.begin(); i != newly_entered.end(); ++i) {
390 if (new_item->Event (reinterpret_cast<GdkEvent*> (&enter_event))) {
391 cerr << string_compose ("\tEntered %1 %2\n", new_item->whatami(), new_item->name);
392 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Enter %1 %2\n", new_item->whatami(), new_item->name));
399 for (set<Item const *>::const_iterator i = within_items.begin(); i != within_items.end(); ++i) {
400 cerr << '\t' << (*i)->whatami() << '/' << (*i)->name << endl;
406 /** Deliver an event to the appropriate item; either the grabbed item, or
407 * one of the items underneath the event.
408 * @param point Position that the event has occurred at, in canvas coordinates.
409 * @param event The event.
412 GtkCanvas::deliver_event (Duple point, GdkEvent* event)
414 /* Point in in canvas coordinate space */
417 /* we have a grabbed item, so everything gets sent there */
418 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
419 _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
420 return _grabbed_item->Event (event);
423 /* find the items that exist at the event's position */
424 vector<Item const *> items;
425 _root.add_items_at_point (point, items);
427 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 possible items to deliver event to\n", items.size()));
429 /* run through the items under the event, from top to bottom, until one claims the event */
430 vector<Item const *>::const_reverse_iterator i = items.rbegin ();
431 while (i != items.rend()) {
433 if ((*i)->ignore_events ()) {
435 PBD::DEBUG::CanvasEvents,
436 string_compose ("canvas event ignored by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
442 if ((*i)->Event (event)) {
443 /* this item has just handled the event */
445 PBD::DEBUG::CanvasEvents,
446 string_compose ("canvas event handled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
453 PBD::DEBUG::CanvasEvents,
454 string_compose ("canvas event left unhandled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
461 if (PBD::debug_bits & PBD::DEBUG::CanvasEvents) {
462 while (i != items.rend()) {
463 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas event not seen by %1\n", (*i)->name.empty() ? "[unknown]" : (*i)->name));
471 /** Called when an item is being destroyed.
472 * @param item Item being destroyed.
473 * @param bounding_box Last known bounding box of the item.
476 GtkCanvas::item_going_away (Item* item, boost::optional<Rect> bounding_box)
479 queue_draw_item_area (item, bounding_box.get ());
482 /* no need to send a leave event to this item, since it is going away
485 within_items.erase (item);
487 if (_grabbed_item == item) {
491 if (_focused_item == item) {
495 enter_leave_items (0); // no mouse state
499 /** Handler for GDK expose events.
501 * @return true if the event was handled.
504 GtkCanvas::on_expose_event (GdkEventExpose* ev)
506 Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
508 render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
513 /** @return Our Cairo context, or 0 if we don't have one */
514 Cairo::RefPtr<Cairo::Context>
515 GtkCanvas::context ()
517 Glib::RefPtr<Gdk::Window> w = get_window ();
519 return Cairo::RefPtr<Cairo::Context> ();
522 return w->create_cairo_context ();
525 /** Handler for GDK button press events.
527 * @return true if the event was handled.
530 GtkCanvas::on_button_press_event (GdkEventButton* ev)
532 /* translate event coordinates from window to canvas */
534 Duple where = window_to_canvas (Duple (ev->x, ev->y));
536 /* Coordinates in the event will be canvas coordinates, correctly adjusted
537 for scroll if this GtkCanvas is in a GtkCanvasViewport.
540 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
541 return deliver_event (where, reinterpret_cast<GdkEvent*>(ev));
544 /** Handler for GDK button release events.
546 * @return true if the event was handled.
549 GtkCanvas::on_button_release_event (GdkEventButton* ev)
551 /* translate event coordinates from window to canvas */
553 Duple where = window_to_canvas (Duple (ev->x, ev->y));
555 /* Coordinates in the event will be canvas coordinates, correctly adjusted
556 for scroll if this GtkCanvas is in a GtkCanvasViewport.
559 DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
560 return deliver_event (where, reinterpret_cast<GdkEvent*>(ev));
563 /** Handler for GDK motion events.
565 * @return true if the event was handled.
568 GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
570 /* translate event coordinates from window to canvas */
572 GdkEvent copy = *((GdkEvent*)ev);
573 Duple where = window_to_canvas (Duple (ev->x, ev->y));
575 copy.motion.x = where.x;
576 copy.motion.y = where.y;
578 /* Coordinates in the event will be canvas coordinates, correctly adjusted
579 for scroll if this GtkCanvas is in a GtkCanvasViewport.
581 return motion_notify_handler ((GdkEventMotion*) ©);
585 GtkCanvas::on_enter_notify_event (GdkEventCrossing* ev)
587 Duple where = window_to_canvas (Duple (ev->x, ev->y));
588 enter_leave_items (where, ev->state);
593 GtkCanvas::on_leave_notify_event (GdkEventCrossing* /*ev*/)
595 cerr << "Clear all within items as we leave\n";
596 within_items.clear ();
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 << this << " 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;
643 /** `Ungrab' any item that was previously grabbed */
647 /* XXX: should this be doing gdk_pointer_ungrab? */
651 /** Set keyboard focus on an item, so that all keyboard events are sent to that item until the focus
653 * @param item Item to grab.
656 GtkCanvas::focus (Item* item)
658 _focused_item = item;
662 GtkCanvas::unfocus (Item* item)
664 if (item == _focused_item) {
669 /** @return The visible area of the canvas, in canvas coordinates */
671 GtkCanvas::visible_area () const
673 Distance const xo = _scroll_offset_x;
674 Distance const yo = _scroll_offset_y;
675 return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
678 /** Create a GtkCanvaSViewport.
679 * @param hadj Adjustment to use for horizontal scrolling.
680 * @param vadj Adjustment to use for vertica scrolling.
682 GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
683 : Alignment (0, 0, 1.0, 1.0)
689 hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
690 vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
694 GtkCanvasViewport::scrolled ()
696 _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
700 /** Handler for when GTK asks us what minimum size we want.
701 * @param req Requsition to fill in.
704 GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
706 /* force the canvas to size itself */
707 // _canvas.root()->bounding_box();