#include <cassert>
#include <gtkmm/adjustment.h>
-#include "pbd/xml++.h"
+#include <gtkmm/label.h>
+
#include "pbd/compose.h"
+#include "pbd/stacktrace.h"
+
#include "canvas/canvas.h"
#include "canvas/debug.h"
/** Construct a new Canvas */
Canvas::Canvas ()
: _root (this)
- , _log_renders (true)
+ , _scroll_offset_x (0)
+ , _scroll_offset_y (0)
{
set_epoch ();
}
-/** Construct a new Canvas from an XML tree
- * @param tree XML Tree.
- */
-Canvas::Canvas (XMLTree const * tree)
- : _root (this)
- , _log_renders (true)
+void
+Canvas::scroll_to (Coord x, Coord y)
{
- set_epoch ();
-
- /* XXX: little bit hacky */
- _root.set_state (tree->root()->child ("Group"));
-
- XMLNodeList const & children = tree->root()->children ();
- for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
- if ((*i)->name() == ("Render")) {
- _renders.push_back (
- Rect (
- atof ((*i)->property ("x0")->value().c_str()),
- atof ((*i)->property ("y0")->value().c_str()),
- atof ((*i)->property ("x1")->value().c_str()),
- atof ((*i)->property ("x1")->value().c_str())
- )
- );
- }
- }
+ _scroll_offset_x = x;
+ _scroll_offset_y = y;
}
/** Render an area of the canvas.
void
Canvas::render (Rect const & area, Cairo::RefPtr<Cairo::Context> const & context) const
{
- checkpoint ("render", "-> render");
+#ifdef CANVAS_DEBUG
+ if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
+ cerr << "RENDER: " << area << endl;
+ //cerr << "CANVAS @ " << this << endl;
+ //dump (cerr);
+ //cerr << "-------------------------\n";
+ }
+#endif
+
render_count = 0;
- context->save ();
-
- /* clip to the requested area */
- context->rectangle (area.x0, area.y0, area.width(), area.height());
- context->clip ();
-
boost::optional<Rect> root_bbox = _root.bounding_box();
if (!root_bbox) {
/* the root has no bounding box, so there's nothing to render */
- context->restore ();
return;
}
- boost::optional<Rect> draw = root_bbox.get().intersection (area);
+ boost::optional<Rect> draw = root_bbox->intersection (area);
if (draw) {
/* there's a common area between the root and the requested
area, so render it.
*/
+
_root.render (*draw, context);
}
- if (_log_renders) {
- _renders.push_back (area);
+#if 0
+ /* debug render area */
+ Rect r = _root.item_to_window (area);
+ context->rectangle (r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0);
+ context->set_source_rgba (1.0, 0.0, 0.0, 1.0);
+ context->stroke ();
+#endif
+}
+
+ostream&
+operator<< (ostream& o, Canvas& c)
+{
+ c.dump (o);
+ return o;
+}
+
+std::string
+Canvas::indent() const
+{
+ string s;
+
+ for (int n = 0; n < ArdourCanvas::dump_depth; ++n) {
+ s += '\t';
}
- context->restore ();
+ return s;
+}
+
+std::string
+Canvas::render_indent() const
+{
+ string s;
- cout << "Rendered " << render_count << "\n";
- checkpoint ("render", "<- render");
+ for (int n = 0; n < ArdourCanvas::render_depth; ++n) {
+ s += ' ';
+ }
+
+ return s;
}
+void
+Canvas::dump (ostream& o) const
+{
+ dump_depth = 0;
+ _root.dump (o);
+}
+
/** Called when an item has been shown or hidden.
* @param item Item that has been shown or hidden.
*/
}
}
+/** Called when an item has a change to its visual properties
+ * that do NOT affect its bounding box.
+ * @param item Item that has been modified.
+ */
+void
+Canvas::item_visual_property_changed (Item* item)
+{
+ boost::optional<Rect> bbox = item->bounding_box ();
+ if (bbox) {
+ queue_draw_item_area (item, bbox.get ());
+ }
+}
+
/** Called when an item has changed, but not moved.
* @param item Item that has changed.
* @param pre_change_bounding_box The bounding box of item before the change,
}
}
+Duple
+Canvas::window_to_canvas (Duple const & d) const
+{
+ return d.translate (Duple (_scroll_offset_x, _scroll_offset_y));
+}
+
+Duple
+Canvas::canvas_to_window (Duple const & d) const
+{
+ return d.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
+}
+
+Rect
+Canvas::window_to_canvas (Rect const & r) const
+{
+ return r.translate (Duple (_scroll_offset_x, _scroll_offset_y));
+}
+
+Rect
+Canvas::canvas_to_window (Rect const & r) const
+{
+ return r.translate (Duple (-_scroll_offset_x, -_scroll_offset_y));
+}
+
/** Called when an item has moved.
* @param item Item that has moved.
* @param pre_change_parent_bounding_box The bounding box of the item before
Canvas::item_moved (Item* item, boost::optional<Rect> pre_change_parent_bounding_box)
{
if (pre_change_parent_bounding_box) {
- /* request a redraw of where the item used to be; we have to use the
- parent's coordinates here as item bounding boxes do not change
- when the item moves.
- */
+ /* request a redraw of where the item used to be. The box has
+ * to be in parent coordinate space since the bounding box of
+ * an item does not change when moved. If we use
+ * item->item_to_canvas() on the old bounding box, we will be
+
+ * using the item's new position, and so will compute the wrong
+ * invalidation area. If we use the parent (which has not
+ * moved, then this will work.
+ */
queue_draw_item_area (item->parent(), pre_change_parent_bounding_box.get ());
}
void
Canvas::queue_draw_item_area (Item* item, Rect area)
{
- request_redraw (item->item_to_canvas (area));
-}
-
-/** @return An XML description of the canvas and its objects */
-XMLTree *
-Canvas::get_state () const
-{
- XMLTree* tree = new XMLTree ();
- XMLNode* node = new XMLNode ("Canvas");
- node->add_child_nocopy (*_root.get_state ());
-
- for (list<Rect>::const_iterator i = _renders.begin(); i != _renders.end(); ++i) {
- XMLNode* render = new XMLNode ("Render");
- render->add_property ("x0", string_compose ("%1", i->x0));
- render->add_property ("y0", string_compose ("%1", i->y0));
- render->add_property ("x1", string_compose ("%1", i->x1));
- render->add_property ("y1", string_compose ("%1", i->y1));
- node->add_child_nocopy (*render);
- }
-
- tree->set_root (node);
- return tree;
+ ArdourCanvas::Rect canvas_area = item->item_to_canvas (area);
+ // cerr << "CANVAS " << this << " for " << item->whatami() << ' ' << item->name << " invalidate " << area << " TRANSLATE AS " << canvas_area << endl;
+ request_redraw (canvas_area);
}
/** Construct a GtkCanvas */
add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
}
-/** Construct a GtkCanvas from an XML tree.
- * @param tree XML Tree.
- */
-GtkCanvas::GtkCanvas (XMLTree const * tree)
- : Canvas (tree)
- , _current_item (0)
- , _grabbed_item (0)
-{
- /* these are the events we want to know about */
- add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
-}
-
-/** Handler for button presses on the canvas.
- * @param ev GDK event.
- */
-bool
-GtkCanvas::button_handler (GdkEventButton* ev)
-{
- DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press %1 %1\n", ev->x, ev->y));
- /* The Duple that we are passing in here is in canvas coordinates */
- return deliver_event (Duple (ev->x, ev->y), reinterpret_cast<GdkEvent*> (ev));
-}
-
/** Handler for pointer motion events on the canvas.
* @param ev GDK event.
* @return true if the motion event was handled, otherwise false.
bool
GtkCanvas::motion_notify_handler (GdkEventMotion* ev)
{
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas motion @ %1, %2\n", ev->x, ev->y));
+
if (_grabbed_item) {
/* if we have a grabbed item, it gets just the motion event,
since no enter/leave events can have happened.
*/
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send MOTION event there\n",
+ _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
return _grabbed_item->Event (reinterpret_cast<GdkEvent*> (ev));
}
- /* This is in canvas coordinates */
Duple point (ev->x, ev->y);
+
+ enter_leave_items (point, ev->state);
+
+ /* Now deliver the motion event. It may seem a little inefficient
+ to recompute the items under the event, but the enter notify/leave
+ events may have deleted canvas items so it is important to
+ recompute the list in deliver_event.
+ */
+ return deliver_event (point, reinterpret_cast<GdkEvent*> (ev));
+}
+
+void
+GtkCanvas::enter_leave_items (int state)
+{
+ int x;
+ int y;
+
+ /* this version of ::enter_leave_items() is called after an item is
+ * added or removed, so we have no coordinates to work from as is the
+ * case with a motion event. Find out where the mouse is and use that.
+ */
+
+ Glib::RefPtr<const Gdk::Window> pointer_window = Gdk::Display::get_default()->get_window_at_pointer (x, y);
+
+ if (pointer_window != get_window()) {
+ return;
+ }
+
+ enter_leave_items (window_to_canvas (Duple (x, y)), state);
+}
+
+void
+GtkCanvas::enter_leave_items (Duple const & point, int state)
+{
+ /* find the items at the given position */
- /* find the items at the new mouse position */
vector<Item const *> items;
_root.add_items_at_point (point, items);
- Item const * new_item = items.empty() ? 0 : items.back ();
-
- if (_current_item && _current_item != new_item) {
- /* leave event */
- GdkEventCrossing leave_event;
- leave_event.type = GDK_LEAVE_NOTIFY;
- leave_event.x = ev->x;
- leave_event.y = ev->y;
- _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
+ GdkEventCrossing enter_event;
+ enter_event.type = GDK_ENTER_NOTIFY;
+ enter_event.window = get_window()->gobj();
+ enter_event.send_event = 0;
+ enter_event.subwindow = 0;
+ enter_event.mode = GDK_CROSSING_NORMAL;
+ enter_event.detail = GDK_NOTIFY_NONLINEAR;
+ enter_event.focus = FALSE;
+ enter_event.state = state;
+ enter_event.x = point.x;
+ enter_event.y = point.y;
+
+ GdkEventCrossing leave_event = enter_event;
+ leave_event.type = GDK_LEAVE_NOTIFY;
+ leave_event.detail = GDK_NOTIFY_ANCESTOR;
+ leave_event.subwindow = 0;
+
+ if (items.empty()) {
+ if (_current_item) {
+ /* leave event */
+ cerr << "E/L: left item " << _current_item->whatami() << '/' << _current_item->name << " for ... nada" << endl;
+ _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
+ _current_item = 0;
+ }
+ return;
}
- if (new_item && _current_item != new_item) {
- /* enter event */
- GdkEventCrossing enter_event;
- enter_event.type = GDK_ENTER_NOTIFY;
- enter_event.x = ev->x;
- enter_event.y = ev->y;
- new_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
+ /* items is sorted from top to bottom, so reverse through it from bottom
+ * to top to find the lowest, first event-sensitive item and notify that
+ * we have entered it
+ */
+
+ cerr << "E/L: " << items.size() << " to check at " << point << endl;
+#ifdef CANVAS_DEBUG
+ for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
+ cerr << '\t' << (*i)->whatami() << ' ' << (*i)->name << " ignore ? " << (*i)->ignore_events() << " current ? " << (_current_item == (*i)) << endl;
}
+#endif
+ cerr << "------------\n";
- _current_item = new_item;
+ for (vector<Item const*>::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
- /* Now deliver the motion event. It may seem a little inefficient
- to recompute the items under the event, but the enter notify/leave
- events may have deleted canvas items so it is important to
- recompute the list in deliver_event.
- */
- return deliver_event (point, reinterpret_cast<GdkEvent*> (ev));
+ Item const * new_item = *i;
+#ifdef CANVAS_DEBUG
+ cerr << "\tE/L check out " << new_item->whatami() << ' ' << new_item->name << " ignore ? " << new_item->ignore_events() << " current ? " << (_current_item == new_item) << endl;
+#endif
+ if (new_item->ignore_events()) {
+ // cerr << "continue1\n";
+ continue;
+ }
+
+ if (_current_item == new_item) {
+ // cerr << "continue2\n";
+ continue;
+ }
+
+ if (_current_item) {
+ /* leave event */
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Leave %1 %2\n", _current_item->whatami(), _current_item->name));
+ _current_item->Event (reinterpret_cast<GdkEvent*> (&leave_event));
+ queue_draw ();
+ }
+
+ if (new_item && _current_item != new_item) {
+ /* enter event */
+ _current_item = new_item;
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("Enter %1 %2\n", _current_item->whatami(), _current_item->name));
+ _current_item->Event (reinterpret_cast<GdkEvent*> (&enter_event));
+ queue_draw ();
+ break;
+ }
+
+ // cerr << "Loop around again\n";
+ }
}
/** Deliver an event to the appropriate item; either the grabbed item, or
bool
GtkCanvas::deliver_event (Duple point, GdkEvent* event)
{
+ /* Point in in canvas coordinate space */
+
if (_grabbed_item) {
/* we have a grabbed item, so everything gets sent there */
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 %2 (%3) was grabbed, send event there\n",
+ _grabbed_item, _grabbed_item->whatami(), _grabbed_item->name));
return _grabbed_item->Event (event);
}
vector<Item const *> items;
_root.add_items_at_point (point, items);
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("%1 possible items to deliver event to\n", items.size()));
+
/* run through the items under the event, from top to bottom, until one claims the event */
vector<Item const *>::const_reverse_iterator i = items.rbegin ();
while (i != items.rend()) {
if ((*i)->ignore_events ()) {
+ DEBUG_TRACE (
+ PBD::DEBUG::CanvasEvents,
+ string_compose ("canvas event ignored by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
+ );
++i;
continue;
}
/* this item has just handled the event */
DEBUG_TRACE (
PBD::DEBUG::CanvasEvents,
- string_compose ("canvas event handled by %1\n", (*i)->name.empty() ? "[unknown]" : (*i)->name)
+ string_compose ("canvas event handled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
);
return true;
DEBUG_TRACE (
PBD::DEBUG::CanvasEvents,
- string_compose ("canvas event ignored by %1\n", (*i)->name.empty() ? "[unknown]" : (*i)->name)
+ string_compose ("canvas event left unhandled by %1 %2\n", (*i)->whatami(), (*i)->name.empty() ? "[unknown]" : (*i)->name)
);
++i;
if (_current_item == item) {
_current_item = 0;
+ queue_draw ();
}
if (_grabbed_item == item) {
_grabbed_item = 0;
}
-}
-
-/** Construct an ImageCanvas.
- * @param size Size in pixels.
- */
-ImageCanvas::ImageCanvas (Duple size)
- : _surface (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, size.x, size.y))
-{
- _context = Cairo::Context::create (_surface);
-}
-
-/** Construct an ImageCanvas from an XML tree.
- * @param tree XML Tree.
- * @param size Size in pixels.
- */
-ImageCanvas::ImageCanvas (XMLTree const * tree, Duple size)
- : Canvas (tree)
- , _surface (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, size.x, size.y))
-{
- _context = Cairo::Context::create (_surface);
-}
-/** Render the canvas to our pixbuf.
- * @param area Area to render, in canvas coordinates.
- */
-void
-ImageCanvas::render_to_image (Rect const & area) const
-{
- render (area, _context);
-}
-
-/** Write our pixbuf to a PNG file.
- * @param f PNG file name.
- */
-void
-ImageCanvas::write_to_png (string const & f)
-{
- assert (_surface);
- _surface->write_to_png (f);
-}
-
-/** @return Our Cairo context */
-Cairo::RefPtr<Cairo::Context>
-ImageCanvas::context ()
-{
- return _context;
+ enter_leave_items (0); // no mouse state
+
}
/** Handler for GDK expose events.
GtkCanvas::on_expose_event (GdkEventExpose* ev)
{
Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
+
render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), c);
+
+#if 1
+ if (_current_item) {
+ boost::optional<Rect> orect = _current_item->bounding_box();
+ if (orect) {
+ Rect r = _current_item->item_to_window (orect.get());
+ c->rectangle (r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0);
+ c->set_source_rgba (1.0, 0.0, 0.0, 1.0);
+ c->stroke ();
+ }
+ }
+#endif
+
+
return true;
}
bool
GtkCanvas::on_button_press_event (GdkEventButton* ev)
{
+ /* translate event coordinates from window to canvas */
+
+ Duple where = window_to_canvas (Duple (ev->x, ev->y));
+
/* Coordinates in the event will be canvas coordinates, correctly adjusted
for scroll if this GtkCanvas is in a GtkCanvasViewport.
*/
- return button_handler (ev);
+
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button press @ %1, %2 => %3\n", ev->x, ev->y, where));
+ return deliver_event (where, reinterpret_cast<GdkEvent*>(ev));
}
/** Handler for GDK button release events.
*/
bool
GtkCanvas::on_button_release_event (GdkEventButton* ev)
-{
+{
+ /* translate event coordinates from window to canvas */
+
+ Duple where = window_to_canvas (Duple (ev->x, ev->y));
+
/* Coordinates in the event will be canvas coordinates, correctly adjusted
for scroll if this GtkCanvas is in a GtkCanvasViewport.
*/
- return button_handler (ev);
+
+ DEBUG_TRACE (PBD::DEBUG::CanvasEvents, string_compose ("canvas button release @ %1, %2 => %3\n", ev->x, ev->y, where));
+ return deliver_event (where, reinterpret_cast<GdkEvent*>(ev));
}
/** Handler for GDK motion events.
bool
GtkCanvas::on_motion_notify_event (GdkEventMotion* ev)
{
+ /* translate event coordinates from window to canvas */
+
+ GdkEvent copy = *((GdkEvent*)ev);
+ Duple where = window_to_canvas (Duple (ev->x, ev->y));
+
+ copy.motion.x = where.x;
+ copy.motion.y = where.y;
+
/* Coordinates in the event will be canvas coordinates, correctly adjusted
for scroll if this GtkCanvas is in a GtkCanvasViewport.
*/
- return motion_notify_handler (ev);
+ return motion_notify_handler ((GdkEventMotion*) ©);
}
/** Called to request a redraw of our canvas.
* @param area Area to redraw, in canvas coordinates.
*/
void
-GtkCanvas::request_redraw (Rect const & area)
+GtkCanvas::request_redraw (Rect const & request)
{
+ Rect area = canvas_to_window (request);
+ // cerr << this << " Invalidate " << request << " TRANSLATE AS " << area << endl;
queue_draw_area (floor (area.x0), floor (area.y0), ceil (area.x1) - floor (area.x0), ceil (area.y1) - floor (area.y0));
}
if (req.y > INT_MAX) {
req.y = INT_MAX;
}
-
+
set_size_request (req.x, req.y);
}
_grabbed_item = 0;
}
+/** @return The visible area of the canvas, in canvas coordinates */
+Rect
+GtkCanvas::visible_area () const
+{
+ Distance const xo = _scroll_offset_x;
+ Distance const yo = _scroll_offset_y;
+ return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
+}
+
/** Create a GtkCanvaSViewport.
* @param hadj Adjustment to use for horizontal scrolling.
* @param vadj Adjustment to use for vertica scrolling.
*/
GtkCanvasViewport::GtkCanvasViewport (Gtk::Adjustment& hadj, Gtk::Adjustment& vadj)
- : Viewport (hadj, vadj)
+ : Alignment (0, 0, 1.0, 1.0)
+ , hadjustment (hadj)
+ , vadjustment (vadj)
{
add (_canvas);
+
+ hadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
+ vadj.signal_value_changed().connect (sigc::mem_fun (*this, &GtkCanvasViewport::scrolled));
+}
+
+void
+GtkCanvasViewport::scrolled ()
+{
+ _canvas.scroll_to (hadjustment.get_value(), vadjustment.get_value());
+ queue_draw ();
}
/** Handler for when GTK asks us what minimum size we want.
void
GtkCanvasViewport::on_size_request (Gtk::Requisition* req)
{
+ /* force the canvas to size itself */
+ // _canvas.root()->bounding_box();
+
req->width = 16;
req->height = 16;
}
-/** Convert window coordinates to canvas coordinates by taking into account
- * where we are scrolled to.
- * @param wx Window x.
- * @param wy Window y.
- * @param cx Filled in with canvas x.
- * @param cy Filled in with canvas y.
- */
-void
-GtkCanvasViewport::window_to_canvas (int wx, int wy, Coord& cx, Coord& cy) const
-{
- cx = wx + get_hadjustment()->get_value ();
- cy = wy + get_vadjustment()->get_value ();
-}
-
-/** @return The visible area of the canvas, in canvas coordinates */
-Rect
-GtkCanvasViewport::visible_area () const
-{
- Distance const xo = get_hadjustment()->get_value ();
- Distance const yo = get_vadjustment()->get_value ();
- return Rect (xo, yo, xo + get_allocation().get_width (), yo + get_allocation().get_height ());
-}