sp-canvas.cpp revision 18a6bbc590e5390fc822b0d84dabdb07921f6a3e
6408N/A * Port of GnomeCanvas for Inkscape needs 6408N/A * Federico Mena <federico@nuclecu.unam.mx> 4892N/A * Raph Levien <raph@gimp.org> 6408N/A * Lauris Kaplinski <lauris@kaplinski.com> 6408N/A * Jon A. Cruz <jon@joncruz.org> 4892N/A * Copyright (C) 1998 The Free Software Foundation 6408N/A * Copyright (C) 2002-2006 authors 6408N/A * Released under GNU GPL, read the file 'COPYING' for more information // gtk_check_version returns non-NULL on failure // Define this to visualize the regions to be redrawn //#define DEBUG_REDRAW 1; // Tiles are a way to minimize the number of redraws, eliminating too small redraws. // The canvas stores a 2D array of ints, each representing a TILE_SIZExTILE_SIZE pixels tile. // If any part of it is dirtied, the entire tile is dirtied (its int is nonzero) and repainted. * The SPCanvasGroup vtable. * Adds an item to a canvas group. * Removes an item from a canvas group. * Class initialization function for SPCanvasGroupClass. * Callback that destroys all items in group and calls group's virtual * Update handler for canvas groups. * Point handler for canvas groups. * Renders all visible canvas group items in buf rectangle. // Data members: ---------------------------------------------------------- * Initializes the SPCanvasItem vtable and the "event" signal. * Callback for initialization of SPCanvasItem. * Callback that removes item from all referers and destroys it. * Sets up the newly created SPCanvasItem. * We make it static for encapsulation reasons since it was nowhere used. * Convenience function to reorder items in a group's child list. * This puts the specified link after the "before" link. * Helper that returns true iff item is descendant of parent. * Helper that emits an event for an item in the canvas, be it the current * item, grabbed item, or focused item, as appropriate. * Helper that re-picks the current item in the canvas, based on the event's * coordinates and emits enter/leave events for items as appropriate. * Class initialization function for SPCanvasClass. * Callback: object initialization for SPCanvas. * Destroy handler for SPCanvas. * The canvas widget's realize callback. * The canvas widget's unrealize callback. * The canvas widget's size request callback. * The canvas widget's size allocate callback. * Button event handler for the canvas. * Scroll event handler for the canvas. * @todo FIXME: generate motion events to re-select items. * Motion event handler for the canvas. * The canvas widget's expose callback. * @todo FIXME: function allways retruns false. * The canvas widget's keypress callback. * Crossing event handler for the canvas. * Focus in handler for the canvas. * Focus out handler for the canvas. * Helper that allocates a new tile array for the canvas, copying overlapping tiles from the old array * Helper that queues a canvas rectangle for redraw * Helper that marks specific canvas rectangle as clean (val == 0) or dirty (otherwise) * Helper that invokes update, paint, and repick on canvas. * Paint the given rect, recursively subdividing the region until it is the size of a single * @return true if the drawing completes * Helper that draws a specific rectangular part of the canvas. * @return true if the rectangle painting succeeds. * Helper that repaints the areas in the canvas that need it. * @return true if all the dirty parts have been redrawn * Idle handler for the canvas that deals with pending updates and redraws. * Convenience function to add an idle handler to a canvas. * Convenience function to remove the idle handler of a canvas. * Removes the transient state of the canvas (idle handler, grabs). * Update callback for canvas widget. // TODO items should not be visible on creation - this causes kludges with items // that should be initially invisible; examples of such items: node handles, the CtrlRect // used for rubberbanding, path outline, etc. * Constructs new SPCanvasItem on SPCanvasGroup. * Helper function that requests redraw only if item's visible flag is set. if (
x0 !=0 ||
x1 !=0 ||
y0 !=0 ||
y1 !=0) {
// Hack: if this is a ctrlrect, move it to 0,0; // this redraws only the stroke of the rect to be deleted, // avoiding redraw of the entire area * Helper function to update item and its children. * NB! affine is parent2canvas. // Apply the child item's transform // apply object flags to child flags * Helper function to invoke the point method of the item. * The argument x, y should be in the parent's item-relative coordinate * system. This routine applies the inverse of the item's transform, * maintaining the affine invariant. * Makes the item's affine transformation matrix be equal to the specified * @affine: An affine transformation matrix. * Raises the item in its parent's stack by the specified number of positions. * @param item A canvas item. * @param positions Number of steps to raise the item. * If the number of positions is greater than the distance to the top of the * stack, then the item is put at the top. * Lowers the item in its parent's stack by the specified number of positions. * @param item A canvas item. * @param positions Number of steps to lower the item. * If the number of positions is greater than the distance to the bottom of the * stack, then the item is put at the bottom. * Sets visible flag on item and requests a redraw. if (
x0 !=0 ||
x1 !=0 ||
y0 !=0 ||
y1 !=0) {
* Clears visible flag on item and requests a redraw. if (
x0 !=0 ||
x1 !=0 ||
y0 !=0 ||
y1 !=0) {
* Grab item under cursor. * \pre !canvas->grabbed_item && item->flags & SP_CANVAS_ITEM_VISIBLE // This test disallows grabbing events by an invisible item, which may be useful // sometimes. An example is the hidden control point used for the selector component, // where it is used for object selection and rubberbanding. There seems to be nothing // preventing this except this test, so I removed it. // -- Krzysztof Kosiński, 2009.08.12 //if (!(item->flags & SP_CANVAS_ITEM_VISIBLE)) // fixme: Top hack (Lauris) // fixme: If we add key masks to event mask, Gdk will abort (Lauris) // fixme: But Canvas actualle does get key events, so all we need is routing these here * Ungrabs the item, which must have been grabbed in the canvas, and ungrabs the * @param item A canvas item that holds a grab. * @param etime The timestamp for ungrabbing the mouse. * Returns the product of all transformation matrices from the root item down * Requests that the canvas queue an update for the specified item. * To be used only by item implementations. // Have reached the top of the tree, make sure the update call gets scheduled. * Returns position of item in group. * Registers SPCanvasGroup class with Gtk and returns its type number. if ( (i->
x2 > i->
x1) && (i->
y2 > i->
y1) ) {
double const x = p[
Geom::X];
double const y = p[
Geom::Y];
// This metric should be improved, because in case of (partly) overlapping items we will now // always select the last one that has been added to the group. We could instead select the one // of which the center is the closest, for example. One can then move to the center // of the item to be focused, and have that one selected. Of course this will only work if the // centers are not coincident, but at least it's better than what we have now. // See the extensive comment in Inkscape::SelTrans::_updateHandles() // Remove it from the list * Registers the SPCanvas class if necessary, and returns the type ID * @return The type ID of the SPCanvas class. // Create the root item as a special case #
endif // defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) // We turn off the need_redraw flag, since if the canvas is mapped again // it will request a redraw anyways. We do not turn off the need_update // flag, though, because updates are not queued when the canvas remaps //g_warning("Event latency reached %f sec (%1.4f)", *latency, tracker.getSkew()); // Schedule redraw of new region // Convert to world coordinates -- we have two cases because of different // offsets of the fields in the event structures. // Choose where we send the event // canvas->current_item becomes NULL in some cases under Win32 // (e.g. if the pointer leaves the window). So this is a hack that // Lauris applied to SP to get around the problem. // Make sure that current_item is up-to-date. If a snap indicator was just deleted, then // sp_canvas_item_dispose has been called and there is no current_item specified. We need // that though because otherwise we don't know where to send this event to, leading to a // lost event. We can't wait for idle events to have current_item updated, we need it now! // Otherwise, scrolling when hovering above a pre-snap indicator won't work (for example) // To avoid side effects, we'll only do this for scroll events, because this is the // only thing we want to fix here. An example of a reported side effect is that // otherwise selection of nodes in the node editor by dragging a rectangle using a // The event is propagated up the hierarchy (for if someone connected to // a group instead of a leaf event), and emission is stopped if a // handler returns TRUE, just like for GtkWidget events. if (!
canvas->
root)
// canvas may have already be destroyed by closing desktop durring interrupted display! // If a button is down, we'll perform enter and leave events on the // current item, but not enter on any other item. This is more or // less like X pointer grabbing for canvas items. // Save the event in the canvas. This is used to synthesize enter and // leave events in case the current item changes. It is also used to // re-pick the current item if the current one gets deleted. Also, // synthesize an enter event. // these fields have the same offsets in both types of events // these fields don't have the same offsets in both types of events // Don't do anything else if this is a recursive call // LeaveNotify means that there is no current item, so we don't look for one // these fields don't have the same offsets in both types of events return retval;
// current item did not change // Synthesize events for old and new current items // new_current_item may have been set to NULL during the call to // Handle the rest of cases // dispatch normally regardless of the event's window if an item // has a pointer grab in effect // Pick the current item as if the button were not pressed, and // then process the event. // Process the event as if the button were pressed, then repick // after the button has been released //buf.ct = gdk_cairo_create(widget->window); cairo_t *xctt = gdk_cairo_create(widget->window); cairo_translate(xctt, paint_rect.left() - canvas->x0, paint_rect.top() - canvas->y0); cairo_set_source_rgb(xctt, 1,0,0); cairo_rectangle(xctt, 0, 0, paint_rect.width(), paint_rect.height()); // create temporary surface //cairo_translate(buf.ct, -x0, -y0); // fix coordinates, clip all drawing to the tile and clear the background //cairo_translate(buf.ct, paint_rect.left() - canvas->x0, paint_rect.top() - canvas->y0); //cairo_set_line_width(buf.ct, 3); //cairo_set_source_rgba(buf.ct, 1.0, 0.0, 0.0, 0.1); //cairo_stroke_preserve(buf.ct); #
endif // defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) //cairo_surface_t *cst = cairo_get_target(buf.ct); //cairo_destroy (buf.ct); //cairo_surface_finish (cst); //cairo_surface_destroy (cst); // Allow only very fast buffers to be run together; // as soon as the total redraw time exceeds 1ms, cancel; // this returns control to the idle loop and allows Inkscape to process user input // (potentially interrupting the redraw); as soon as Inkscape has some more idle time, // it will get back and finish painting what remains to paint. // Interrupting redraw isn't always good. // For example, when you drag one node of a big path, only the buffer containing // the mouse cursor will be redrawn again and again, and the rest of the path // will remain stale because Inkscape never has enough idle time to redraw all // of the screen. To work around this, such operations set a forced_redraw_limit > 0. // If this limit is set, and if we have aborted redraw more times than is allowed, // interrupting is blocked and we're forced to redraw full screen once // (after which we can again interrupt forced_redraw_limit times). // Find the optimal buffer dimensions if ((
bw <
1) || (
bh <
1))
r.x = this_rect.x0 - setup->canvas->x0; r.y = this_rect.y0 - setup->canvas->y0; r.width = this_rect.x1 - this_rect.x0; r.height = this_rect.y1 - this_rect.y0; GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(setup->canvas)); gdk_window_begin_paint_rect(window, &r); //gdk_window_end_paint(window); This test determines the redraw strategy: bw < bh (strips mode) splits across the smaller dimension of the rect and therefore (on horizontally-stretched windows) results in redrawing in horizontal strips (from cursor point, in both directions if the cursor is in the middle). This is traditional for Inkscape since old days, and seems to be faster for drawings with many smaller objects at zoom-out. bw > bh (chunks mode) splits across the larger dimension of the rect and therefore paints in almost-square chunks, again from the cursor point. It's sometimes faster for drawings with few slow (e.g. blurred) objects crossing the entire screen. It also appears to be somewhat psychologically The default for now is the strips mode. // Make sure that mid lies on a tile boundary // Always paint towards the mouse first // Make sure that mid lies on a tile boundary // Always paint towards the mouse first // Save the mouse location // use 256K as a compromise to not slow down gradients // 256K is the cached buffer and we need 4 channels // paths only, so 1M works faster // 1M is the cached buffer and we need 4 channels for (
int i = 0; i <
n_rects; i++) {
// we've had a full unaborted redraw, reset the full redraw counter if (!
canvas->
root) {
// canvas may have already be destroyed by closing desktop during interrupted display! // Cause the update if necessary int ix = (
int)
round(
cx);
// ix and iy are the new canvas coordinates (integer screen pixels) int iy = (
int)
round(
cy);
// cx might be negative, so (int)(cx + 0.5) will not do! int dx =
ix -
x0;
// dx and dy specify the displacement (scroll) of the int dy =
iy -
y0;
// canvas w.r.t its previous position dx0 =
cx;
// here the 'd' stands for double, not delta! // scrolling without zoom; redraw only the newly exposed areas if ((
dx != 0) || (
dy != 0)) {
// scrolling as part of zoom; do nothing here - the next do_update will perform full redraw * Sets world coordinates from win and canvas. * Sets win coordinates from world and canvas. * Converts point from win to world coordinates. * Converts point from world to win coordinates. * Returns true if point given in world coordinates is inside window. * Return canvas window coordinates as Geom::Rect. * Return canvas window coordinates as integer rectangle. for (
int i=
tl; i<
tr; i++) {
for (
int j=
tt; j<
tb; j++) {
for (
int i=
tl; i<
tr; i++) {
for (
int j=
tt; j<
tb; j++) {
c-file-style:"stroustrup" c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :