sp-canvas.cpp revision 274f6d25a9facdc5bfbe8b4df42e6173e4328775
#define __SP_CANVAS_C__
/** \file
* Port of GnomeCanvas for Inkscape needs
*
* Authors:
* Federico Mena <federico@nuclecu.unam.mx>
* Raph Levien <raph@gimp.org>
* Lauris Kaplinski <lauris@kaplinski.com>
* fred
* bbyak
*
* Copyright (C) 1998 The Free Software Foundation
* Copyright (C) 2002-2006 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <libnr/nr-pixblock.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkversion.h>
#include <gtkmm.h>
#include <helper/sp-marshal.h>
#include <display/sp-canvas.h>
#include "display-forward.h"
#include <libnr/nr-matrix-fns.h>
#include <libnr/nr-matrix-ops.h>
#include <libnr/nr-convex-hull.h>
#include "prefs-utils.h"
#include "inkscape.h"
#include "sodipodi-ctrlrect.h"
#if ENABLE_LCMS
#include "color-profile-fns.h"
#endif // ENABLE_LCMS
#include "display/rendermode.h"
#include "display/inkscape-cairo.h"
// GTK_CHECK_VERSION returns false on failure
// gtk_check_version returns non-NULL on failure
static bool const HAS_BROKEN_MOTION_HINTS =
// 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.
#define TILE_SIZE 16
enum {
};
/**
* A group of Items.
*/
struct SPCanvasGroup {
};
/**
* The SPCanvasGroup vtable.
*/
struct SPCanvasGroupClass {
};
/**
* The SPCanvas vtable.
*/
struct SPCanvasClass {
};
/* SPCanvasItem */
enum {ITEM_EVENT, ITEM_LAST_SIGNAL};
static void sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, gchar const *first_arg_name, va_list args);
static GtkObjectClass *item_parent_class;
/**
* Registers the SPCanvasItem class with Glib and returns its type number.
*/
sp_canvas_item_get_type (void)
{
if (!type) {
sizeof (SPCanvasItemClass),
sizeof (SPCanvasItem),
0,
};
}
return type;
}
/**
* Initializes the SPCanvasItem vtable and the "event" signal.
*/
static void
{
/* fixme: Derive from GObject */
G_TYPE_BOOLEAN, 1,
}
/**
* Callback for initialization of SPCanvasItem.
*/
static void
{
}
/**
* Constructs new SPCanvasItem on SPCanvasGroup.
*/
{
return item;
}
/**
* Sets up the newly created SPCanvasItem.
*
* We make it static for encapsulation reasons since it was nowhere used.
*/
static void
sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, gchar const *first_arg_name, va_list args)
{
}
/**
* Helper function that requests redraw only if item's visible flag is set.
*/
static void
{
sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
}
}
}
/**
* Callback that removes item from all referers and destroys it.
*/
static void
{
// 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
if (SP_IS_CTRLRECT(item)) {
} else {
}
}
}
}
}
}
/**
* Helper function to update item and its children.
*
* NB! affine is parent2canvas.
*/
static void
{
/* 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.
*/
static double
{
return NR_HUGE;
}
/**
* Makes the item's affine transformation matrix be equal to the specified
* matrix.
*
* @item: A canvas item.
* @affine: An affine transformation matrix.
*/
void
{
} else {
}
}
}
/**
* Convenience function to reorder items in a group's child list.
*
* This puts the specified link after the "before" link.
*/
static void
{
return;
} else {
}
} else {
return;
else {
}
else
}
}
/**
* 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.
*/
void
{
g_return_if_fail (positions >= 0);
return;
if (!before)
}
/**
* 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.
**/
void
{
return;
else
}
/**
* Sets visible flag on item and requests a redraw.
*/
void
{
return;
sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
}
}
/**
* Clears visible flag on item and requests a redraw.
*/
void
{
return;
sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)(item->x2 + 1), (int)(item->y2 + 1));
}
}
/**
* Grab item under cursor.
*
* \pre !canvas->grabbed_item && item->flags & SP_CANVAS_ITEM_VISIBLE
*/
int
{
return -1;
return -1;
if (HAS_BROKEN_MOTION_HINTS) {
}
/* 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 */
return 0;
}
/**
* Ungrabs the item, which must have been grabbed in the canvas, and ungrabs the
* mouse.
*
* \param item A canvas item that holds a grab.
* \param etime The timestamp for ungrabbing the mouse.
*/
void
{
return;
}
/**
* Returns the product of all transformation matrices from the root item down
* to the item.
*/
{
while (item) {
}
return affine;
}
/**
* Helper that returns true iff item is descendant of parent.
*/
{
while (item) {
return true;
}
return false;
}
/**
* Focus canvas, and item under cursor if it is not already focussed.
*/
void
{
if (focused_item) {
}
if (focused_item) {
}
}
/**
* Requests that the canvas queue an update for the specified item.
*
* To be used only by item implementations.
*/
void
{
return;
/* Recurse up the tree */
} else {
/* Have reached the top of the tree, make sure the update call gets scheduled. */
}
}
/**
* Returns position of item in group.
*/
{
}
/* SPCanvasGroup */
static void sp_canvas_group_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
static SPCanvasItemClass *group_parent_class;
/**
* Registers SPCanvasGroup class with Gtk and returns its type number.
*/
{
if (!type) {
sizeof(SPCanvasGroupClass),
0, // base_init
0, // base_finalize
0, // class_finalize
0, // class_data
sizeof(SPCanvasGroup),
0, // n_preallocs
0 // value_table
};
type = g_type_register_static(sp_canvas_item_get_type(), "SPCanvasGroup", &info, static_cast<GTypeFlags>(0));
}
return type;
}
/**
* Class initialization function for SPCanvasGroupClass
*/
static void
{
}
/**
* Callback. Empty.
*/
static void
{
/* Nothing here */
}
/**
* Callback that destroys all items in group and calls group's virtual
* destroy() function.
*/
static void
{
while (list) {
}
}
/**
* Update handler for canvas groups
*/
static void
{
bool empty=true;
if (empty) {
empty = false;
} else {
}
}
}
if (bounds) {
} else {
// FIXME ?
}
}
/**
* Point handler for canvas groups.
*/
static double
{
double const x = p[NR::X];
double const y = p[NR::Y];
double best = 0.0;
*actual_item = NULL;
double dist = 0.0;
int has_point;
} else
}
}
}
return best;
}
/**
* Renders all visible canvas group items in buf rectangle.
*/
static void
{
}
}
}
}
/**
* Adds an item to a canvas group.
*/
static void
{
} else {
}
}
/**
* Removes an item from a canvas group
*/
static void
{
/* Unparent the child */
/* Remove it from the list */
break;
}
}
}
/* SPCanvas */
static GtkWidgetClass *canvas_parent_class;
/**
* Registers the SPCanvas class if necessary, and returns the type ID
* associated to it.
*
* \return The type ID of the SPCanvas class.
**/
GType sp_canvas_get_type(void)
{
if (!type) {
sizeof(SPCanvasClass),
0, // base_init
0, // base_finalize
0, // class_finalize
0, // class_data
sizeof(SPCanvas),
0, // n_preallocs
0 // value_table
};
}
return type;
}
/**
* Class initialization function for SPCanvasClass.
*/
static void
{
}
/**
* Callback: object initialization for SPCanvas.
*/
static void
{
/* Create the root item as a special case */
// See comment at in sp-canvas.h.
canvas->gen_all_enter_events = false;
canvas->forced_redraw_count = 0;
#if ENABLE_LCMS
canvas->enable_cms_display_adj = false;
#endif // ENABLE_LCMS
canvas->is_scrolling = false;
}
/**
* Convenience function to remove the idle handler of a canvas.
*/
static void
{
}
}
/*
* Removes the transient state of the canvas (idle handler, grabs).
*/
static void
{
/* 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
* itself.
*/
if (canvas->need_redraw) {
}
if (canvas->grabbed_item) {
}
}
/**
* Destroy handler for SPCanvas.
*/
static void
{
}
}
/**
* Returns new canvas as widget.
*/
sp_canvas_new_aa (void)
{
}
/**
* The canvas widget's realize callback.
*/
static void
{
0 : GDK_POINTER_MOTION_HINT_MASK ) |
widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
}
/**
* The canvas widget's unrealize callback.
*/
static void
{
}
/**
* The canvas widget's size_request callback.
*/
static void
{
}
/**
* The canvas widget's size_allocate callback.
*/
static void
{
/* Schedule redraw of new region */
sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+allocation->width,canvas->y0+allocation->height);
0,
}
0,
}
if (GTK_WIDGET_REALIZED (widget)) {
}
}
/**
* Helper that emits an event for an item in the canvas, be it the current
* item, grabbed item, or focused item, as appropriate.
*/
static int
{
if (canvas->grabbed_item) {
case GDK_ENTER_NOTIFY:
break;
case GDK_LEAVE_NOTIFY:
break;
case GDK_MOTION_NOTIFY:
break;
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
break;
case GDK_BUTTON_RELEASE:
break;
case GDK_KEY_PRESS:
break;
case GDK_KEY_RELEASE:
break;
case GDK_SCROLL:
mask = GDK_SCROLL;
break;
default:
mask = 0;
break;
}
}
/* Convert to world coordinates -- we have two cases because of diferent
* offsets of the fields in the event structures.
*/
case GDK_ENTER_NOTIFY:
case GDK_LEAVE_NOTIFY:
break;
case GDK_MOTION_NOTIFY:
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
case GDK_BUTTON_RELEASE:
break;
default:
break;
}
/* 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.
*/
} else {
}
if (canvas->focused_item &&
}
/* 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.
*/
}
return finished;
}
/**
* Helper that re-picks the current item in the canvas, based on the event's
*/
static int
{
int button_down = 0;
double x, y;
if (!canvas->root) // canvas may have already be destroyed by closing desktop durring interrupted display!
return FALSE;
if (canvas->gen_all_enter_events == false) {
// 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 */
} else {
}
} else {
}
}
/* 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 */
} else {
}
/* world coords */
/* find the closest item */
} else {
}
} else {
}
return retval; /* current item did not change */
}
/* Synthesize events for old and new current items */
&& !canvas->left_grabbed_item) {
}
if (canvas->gen_all_enter_events == false) {
// new_current_item may have been set to NULL during the call to
// emit_event() above
return retval;
}
}
/* Handle the rest of cases */
}
return retval;
}
/**
* Button event handler for the canvas.
*/
static gint
{
/* dispatch normally regardless of the event's window if an item has
has a pointer grab in effect */
if (!canvas->grabbed_item &&
return retval;
int mask;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
mask = 0;
}
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
/* Pick the current item as if the button were not pressed, and
* then process the event.
*/
break;
case GDK_BUTTON_RELEASE:
/* Process the event as if the button were pressed, then repick
* after the button has been released
*/
break;
default:
}
return retval;
}
/**
* Scroll event handler for the canvas.
*
* \todo FIXME: generate motion events to re-select items.
*/
static gint
{
}
}
#else
}
#endif
/**
* Motion event handler for the canvas.
*/
static int
{
int status;
return FALSE;
return FALSE;
}
return status;
}
static void
sp_canvas_paint_single_buffer (SPCanvas *canvas, int x0, int y0, int x1, int y1, int draw_x1, int draw_y1, int draw_x2, int draw_y2, int sw)
{
} else {
}
// Mark the region clean
}
#if ENABLE_LCMS
cmsHTRANSFORM transf = 0;
long long int fromDisplay = prefs_get_int_attribute_limited( "options.displayprofile", "from_display", 0, 0, 1 );
if ( fromDisplay ) {
} else {
}
#endif // ENABLE_LCMS
#if ENABLE_LCMS
}
#endif // ENABLE_LCMS
TRUE,
} else {
#if ENABLE_LCMS
}
}
#endif // ENABLE_LCMS
// Now we only need to output the prepared pixmap to the actual screen, and this define chooses one
// of the two ways to do it. The cairo way is direct and straightforward, but unfortunately
// noticeably slower. I asked Carl Worth but he was unable so far to suggest any specific reason
// for this slowness. So, for now we use the oldish method: squeeze out 32bpp buffer to 24bpp and
// use gdk_draw_rgb_image_dithalign, for unfortunately gdk can only handle 24 bpp, which cairo
// cannot handle at all. Still, this way is currently faster even despite the blit with squeeze.
///#define CANVAS_OUTPUT_VIA_CAIRO
#ifdef CANVAS_OUTPUT_VIA_CAIRO
CAIRO_FORMAT_ARGB32, // unpacked, i.e. 32 bits! one byte is unused
);
#else
// this does the 32->24 squishing, using an assembler routine:
sw * 3,
#endif
}
} else {
}
}
struct PaintRectSetup {
int max_pixels;
};
/**
* Paint the given rect, recursively subdividing the region until it is the size of a single
* buffer.
*
* @return true if the drawing completes
*/
static int
{
// 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.
if (elapsed > 1000) {
// 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).
}
return false;
}
}
// Find the optimal buffer dimensions
return 0;
// We are small enough
return 1;
}
/*
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
faster.
The default for now is the strips mode.
*/
// to correctly calculate the mean of two ints, we need to sum them into a larger int type
// Make sure that mid lies on a tile boundary
// Always paint towards the mouse first
} else {
}
} else {
// to correctly calculate the mean of two ints, we need to sum them into a larger int type
// Make sure that mid lies on a tile boundary
// Always paint towards the mouse first
} else {
}
}
}
/**
* Helper that draws a specific rectangular part of the canvas.
*
* @return true if the rectangle painting succeeds.
*/
static bool
{
// Clip rect-to-draw by the current visible area
#ifdef DEBUG_REDRAW
// paint the area to redraw yellow
TRUE,
#endif
// Save the mouse location
gint x, y;
// use 256K as a compromise to not slow down gradients
// 256K is the cached buffer and we need 4 channels
} else {
// paths only, so 1M works faster
// 1M is the cached buffer and we need 4 channels
}
// Start the clock
// Go
}
/**
* Force a full redraw after a specified number of interrupted redraws
*/
void
canvas->forced_redraw_count = 0;
}
/**
* End forced full redraw requests
*/
void
}
/**
* The canvas widget's expose callback.
*/
static gint
{
if (!GTK_WIDGET_DRAWABLE (widget) ||
return FALSE;
int n_rects;
for (int i = 0; i < n_rects; i++) {
}
if (n_rects > 0)
return FALSE;
}
/**
* The canvas widget's keypress callback.
*/
static gint
{
}
/**
* Crossing event handler for the canvas.
*/
static gint
{
return FALSE;
}
/**
* Focus in handler for the canvas.
*/
static gint
{
if (canvas->focused_item) {
} else {
return FALSE;
}
}
/**
* Focus out handler for the canvas.
*/
static gint
{
if (canvas->focused_item)
else
return FALSE;
}
/**
* Helper that repaints the areas in the canvas that need it.
*
* @return true if all the dirty parts have been redrawn
*/
static int
{
if (canvas->need_update) {
}
if (!canvas->need_redraw)
return TRUE;
}
}
}
// Aborted
return FALSE;
};
}
}
// we've had a full unaborted redraw, reset the full redraw counter
canvas->forced_redraw_count = 0;
}
return TRUE;
}
/**
* Helper that invokes update, paint, and repick on canvas.
*/
static int
{
if (!canvas->root || !canvas->pixmap_gc) // canvas may have already be destroyed by closing desktop durring interrupted display!
return TRUE;
/* Cause the update if necessary */
if (canvas->need_update) {
}
/* Paint if able to */
if (GTK_WIDGET_DRAWABLE (canvas)) {
}
/* Pick new current item */
while (canvas->need_repick) {
}
return TRUE;
}
/**
* Idle handler for the canvas that deals with pending updates and redraws.
*/
static gint
{
if (ret) {
/* Reset idle id */
}
return !ret;
}
/**
* Convenience function to add an idle handler to a canvas.
*/
static void
{
return;
}
/**
* Returns the root group of the specified canvas.
*/
{
}
/**
* Scrolls canvas to specific position (cx and cy are measured in screen pixels)
*/
void
{
sp_canvas_resize_tiles (canvas, canvas->x0, canvas->y0, canvas->x0+canvas->widget.allocation.width, canvas->y0+canvas->widget.allocation.height);
if (!clear) {
// scrolling without zoom; redraw only the newly exposed areas
if (GTK_WIDGET_REALIZED (canvas)) {
}
}
} else {
// scrolling as part of zoom; do nothing here - the next do_update will perform full redraw
}
}
/**
* Updates canvas if necessary.
*/
void
{
if (!(canvas->need_update ||
return;
}
/**
* Update callback for canvas widget.
*/
static void
{
}
/**
* Forces redraw of rectangular canvas area.
*/
void
{
if (!GTK_WIDGET_DRAWABLE (canvas)) return;
}
/**
* Sets world coordinates from win and canvas.
*/
void sp_canvas_window_to_world(SPCanvas const *canvas, double winx, double winy, double *worldx, double *worldy)
{
}
/**
* Sets win coordinates from world and canvas.
*/
void sp_canvas_world_to_window(SPCanvas const *canvas, double worldx, double worldy, double *winx, double *winy)
{
}
/**
* 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.
*/
{
using NR::X;
using NR::Y;
}
/**
* Return canvas window coordinates as NR::Rect.
*/
{
GtkWidget const *w = GTK_WIDGET(this);
}
/**
* Return canvas window coordinates as IRect (a rectangle defined by integers).
*/
{
GtkWidget const *w = GTK_WIDGET(this);
}
inline int sp_canvas_tile_floor(int x)
{
}
inline int sp_canvas_tile_ceil(int x)
{
}
/**
* Helper that allocates a new tile array for the canvas, copying overlapping tiles from the old array
*/
{
return;
}
ntiles[ind]=canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH]; // copy from the old tile
} else {
}
}
}
}
/*
* Helper that queues a canvas rectangle for redraw
*/
}
/**
* Helper that marks specific canvas rectangle as clean (val == 0) or dirty (otherwise)
*/
{
return;
}
if ( tl >= canvas->tRight || tr <= canvas->tLeft || tt >= canvas->tBottom || tb <= canvas->tTop ) return;
}
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :