seltrans.cpp revision 8b9a820756fdf348239872236be2257f854e094a
#define __SELTRANS_C__
/*
* Helper object for transforming selected items
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* Carl Hetherington <inkscape@carlh.net>
* Diederik van Lierop <mail@diedenrezi.nl>
*
* Copyright (C) 1999-2002 Lauris Kaplinski
* Copyright (C) 1999-2008 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <cstring>
#include <string>
#include <libnr/nr-matrix-ops.h>
#include <libnr/nr-matrix-translate-ops.h>
#include <libnr/nr-rotate-ops.h>
#include <libnr/nr-scale-ops.h>
#include <libnr/nr-translate-matrix-ops.h>
#include <libnr/nr-translate-ops.h>
#include <gdk/gdkkeysyms.h>
#include "document.h"
#include "sp-namedview.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "desktop-style.h"
#include "knot.h"
#include "snap.h"
#include "selection.h"
#include "select-context.h"
#include "sp-item.h"
#include "sp-item-transform.h"
#include "seltrans-handles.h"
#include "seltrans.h"
#include "selection-chemistry.h"
#include "sp-metrics.h"
#include "verbs.h"
#include "display/sp-ctrlline.h"
#include "prefs-utils.h"
#include "mod360.h"
#include "display/snap-indicator.h"
static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint32 state, gpointer data);
static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *p, guint state, gboolean *data);
{
case GDK_MOTION_NOTIFY:
break;
case GDK_KEY_PRESS:
/* stamping mode: both mode(show content and outline) operation with knot */
if (!SP_KNOT_IS_GRABBED(knot)) {
return FALSE;
}
return TRUE;
}
break;
default:
break;
}
return FALSE;
}
_grabbed(false),
_show_handles(true),
_bbox(),
{
_snap_bbox_type = (prefs_bbox ==0)?
for (int i = 0; i < 8; i++) {
}
_center_is_set = false; // reread _center from items, or set to bbox midpoint
"anchor", GTK_ANCHOR_CENTER,
"mode", SP_CTRL_MODE_COLOR,
"shape", SP_CTRL_SHAPE_BITMAP,
"size", 13.0,
"filled", TRUE,
"fill_color", 0x00000000,
"stroked", TRUE,
"stroke_color", 0x000000a0,
NULL);
"anchor", GTK_ANCHOR_CENTER,
"mode", SP_CTRL_MODE_XOR,
"shape", SP_CTRL_SHAPE_CROSS,
"size", 7.0,
"filled", TRUE,
"fill_color", 0xffffff7f,
"stroked", TRUE,
"stroke_color", 0xffffffff,
NULL);
for (int i = 0; i < 4; i++) {
sp_canvas_item_hide(_l[i]);
}
);
);
}
{
for (unsigned int i = 0; i < 8; i++) {
if (_shandle[i]) {
}
if (_rhandle[i]) {
}
}
if (_chandle) {
}
if (_norm) {
}
if (_grip) {
}
for (int i = 0; i < 4; i++) {
if (_l[i]) {
}
}
}
}
{
}
{
if (_state == STATE_SCALE) {
} else {
}
_center_is_set = true; // no need to reread center
}
{
_center = p;
_center_is_set = true;
// Write the new center position into all selected items
// only set the value; updating repr and document_done will be done once, on ungrab
}
}
{
_grabbed = true;
_changed = false;
if (_empty) {
return;
}
_items_centers.push_back(it->getCenter()); // for content-dragging, we need to remember original centers
}
_handle_x = x;
_handle_y = y;
// The selector tool should snap the bbox, special snappoints, and path nodes
// (The special points are the handles, center, rotation axis, font baseline, ends of spiral, etc.)
// First, determine the bounding box for snapping ...
_approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX); // Used for correctly scaling the strokewidth
_point = p;
if (_geometric_bbox) {
} else {
_point_geom = p;
}
// Next, get all points to consider for snapping
/* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
An average user would rarely ever try to snap such a large number of nodes anyway, because
(s)he could hardly discern which node would be snapping */
// Unfortunately, by now we will have lost the font-baseline snappoints :-(
}
// Find bbox hulling all special points, which excludes stroke width. Here we need to include the
// path nodes, for example because a rectangle which has been converted to a path doesn't have
// any other special points
if ( snap_points_hull.empty() == false ) {
i++;
while (i != snap_points_hull.end()) {
snap_points_bbox.expandTo(*i);
i++;
}
}
if (_bbox) {
// ... and add the bbox corners to _bbox_points
for ( unsigned i = 0 ; i < 4 ; i++ ) {
}
// There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
// - one for snapping the boundingbox, which can be either visual or geometric
// - one for snapping the special points
// The "opposite" in case of a geometric boundingbox always coincides with the "opposite" for the special points
// These distinct "opposites" are needed in the snapmanager to avoid bugs such as #sf1540195 (in which
// a box is caught between two guides)
_opposite_for_specpoints = snap_points_bbox.min() + snap_points_bbox.dimensions() * NR::scale(1-x, 1-y);
}
// The lines below are usefull for debugging any snapping issues, as they'll spit out all points that are considered for snapping
/*std::cout << "Number of snap points: " << _snap_points.size() << std::endl;
for (std::vector<NR::Point>::const_iterator i = _snap_points.begin(); i != _snap_points.end(); i++)
{
std::cout << " " << *i << std::endl;
}
std::cout << "Number of bbox points: " << _bbox_points.size() << std::endl;
for (std::vector<NR::Point>::const_iterator i = _bbox_points.begin(); i != _bbox_points.end(); i++)
{
std::cout << " " << *i << std::endl;
}*/
if ((x != -1) && (y != -1)) {
}
if (_show == SHOW_OUTLINE) {
for (int i = 0; i < 4; i++)
sp_canvas_item_show(_l[i]);
}
}
{
if (_show == SHOW_CONTENT) {
// update the content
}
} else {
if (_bbox) {
/* update the outline */
for (unsigned i = 0 ; i < 4 ; i++) {
}
for (unsigned i = 0 ; i < 4 ; i++) {
}
}
}
_changed = true;
}
{
_grabbed = false;
_show_handles = true;
}
if (_show == SHOW_OUTLINE) {
for (int i = 0; i < 4; i++)
sp_canvas_item_hide(_l[i]);
}
if (_stamp_cache) {
_stamp_cache = NULL;
}
sp_selection_apply_affine(selection, _current_relative_affine, (_show == SHOW_OUTLINE)? true : false);
if (_center) {
_center_is_set = true;
}
// If dragging showed content live, sp_selection_apply_affine cannot change the centers
// appropriately - it does not know the original positions of the centers (all objects already have
// the new bboxes). So we need to reset the centers from our saved array.
for (unsigned i = 0; i < _items_centers.size(); i++) {
}
}
}
if (_current_relative_affine.is_translation()) {
_("Move"));
} else if (_current_relative_affine.is_scale()) {
_("Scale"));
} else if (_current_relative_affine.is_rotation()) {
_("Rotate"));
} else {
_("Skew"));
}
} else {
if (_center_is_set) {
// we were dragging center; update reprs and commit undoable action
}
_("Set center"));
}
}
}
/* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
/* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
{
if ( fixup && _stamp_cache ) {
// TODO - give a proper fix. Simple temproary work-around for the grab() issue
_stamp_cache = NULL;
}
/* stamping mode */
if (!_empty) {
GSList *l;
if (_stamp_cache) {
l = _stamp_cache;
} else {
/* Build cache */
_stamp_cache = l;
}
while (l) {
// remember the position of the item
// remember parent
// add the new repr to the parent
// move to the saved position
if (_show == SHOW_OUTLINE) {
} else {
}
}
l = l->next;
}
_("Stamp"));
}
if ( fixup && _stamp_cache ) {
// TODO - give a proper fix. Simple temproary work-around for the grab() issue
_stamp_cache = NULL;
}
}
{
if ( !_show_handles || _empty )
{
return;
}
// center handle
_chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
}
if ( _state == STATE_SCALE ) {
_("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
_("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
} else {
_("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
_("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
}
if (!_center_is_set) {
_center_is_set = true;
}
} else {
}
}
{
if (_empty) {
return;
}
//Update the bboxes
if (!_bbox) {
_empty = true;
return;
}
}
{
for (int i = 0; i < num; i++) {
sp_knot_hide(knot[i]);
}
}
}
{
g_return_if_fail( !_empty );
for (int i = 0; i < num; i++) {
sp_knot_update_ctrl(knot[i]);
g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
}
sp_knot_show(knot[i]);
// shouldn't have nullary bbox, but knots
+ ( _bbox->dimensions()
sp_knot_moveto(knot[i], p);
}
}
{
);
}
{
}
static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint state, gpointer data)
{
);
}
static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, guint state, gboolean *data)
{
);
}
{
);
}
{
case GTK_ANCHOR_CENTER:
if (state & GDK_SHIFT_MASK) {
// Unset the center position for all selected items
it->unsetCenter();
_center_is_set = false; // center has changed
}
_("Reset center"));
}
break;
default:
break;
}
}
{
case GTK_ANCHOR_CENTER:
"shape", SP_CTRL_SHAPE_BITMAP,
"size", 13.0,
NULL);
break;
default:
"shape", SP_CTRL_SHAPE_CROSS,
"size", 7.0,
NULL);
break;
}
}
void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
{
if (!SP_KNOT_IS_GRABBED(knot)) {
return;
}
// in case items have been unhooked from the document, don't
// try to continue processing events for them.
return;
}
}
}
gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, guint state, SPSelTransHandle const &handle)
{
if (!SP_KNOT_IS_GRABBED(knot)) {
return TRUE;
}
// When holding shift while rotating or skewing, the transformation will be
// relative to the point opposite of the handle; otherwise it will be relative
// to the center as set for the selection
} else if (_center) {
} else {
// FIXME
return TRUE;
}
}
return TRUE;
}
{
if (!_grabbed) {
// reread in case it changed on the fly:
_snap_bbox_type = (prefs_bbox ==0)?
//SPItem::APPROXIMATE_BBOX will be replaced by SPItem::VISUAL_BBOX, as soon as the latter is implemented properly
_center_is_set = false; // center(s) may have changed
}
}
{
if (!_grabbed) {
// reset internal flag
_changed = false;
_center_is_set = false; // center(s) may have changed
}
}
/*
* handlers for handle move-request
*/
/** Returns -1 or 1 according to the sign of x. Returns 1 for 0 and NaN. */
static double sign(double const x)
{
return ( x < 0
? -1
: 1 );
}
{
}
{
}
{
}
{
}
{
}
{
// Calculate the scale factors, which can be either visual or geometric
// depending on which type of bbox is currently being used (see preferences -> selector tool)
// Find the scale factors for the geometric bbox
// We're scaling either the visual or the geometric bbox here (see the comment above)
for ( unsigned int i = 0 ; i < 2 ; i++ ) {
} else if (default_scale[i] != 0) {
}
}
// Update the knot position
// When scaling by an integer, snapping is not needed
} else {
// In all other cases we should try to snap now
// Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
//
// The aspect-ratio must be locked before snapping
} else {
}
// Snap along a suitable constraint vector from the origin.
bb = m.constrainedSnapScale(Snapper::SNAPPOINT_BBOX, _bbox_points, default_scale, _origin_for_bboxpoints);
sn = m.constrainedSnapScale(Snapper::SNAPPOINT_NODE, _snap_points, geom_scale, _origin_for_specpoints);
/* Choose the smaller difference in scale. Since s[X] == s[Y] we can
** just compare difference in s[X].
*/
} else {
/* Scale aspect ratio is unlocked */
/* Pick the snap that puts us closest to the original scale */
bd = bb.getSnapped() ? fabs(NR::L2(bb.getTransformation()) - NR::L2(default_scale.point())) : NR_HUGE;
}
// We didn't snap at all! Don't update the handle position, just calculate the new transformation
// We snapped the bbox (which is either visual or geometric)
// Calculate the new transformation and update the handle position
} else {
// We snapped the special points (e.g. nodes), which are not at the visual bbox
// The handle location however (pt) might however be at the visual bbox, so we
// will have to calculate pt taking the stroke width into account
}
}
/* Status text */
_("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
return TRUE;
}
gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
{
case GDK_TOP_SIDE:
case GDK_BOTTOM_SIDE:
break;
case GDK_LEFT_SIDE:
case GDK_RIGHT_SIDE:
break;
default:
return TRUE;
};
// Calculate the scale factors, which can be either visual or geometric
// depending on which type of bbox is currently being used (see preferences -> selector tool)
// Find the scale factors for the geometric bbox
} else if (default_scale[axis] != 0) {
}
// Calculate the new transformation and update the handle position
// When stretching by an integer, snapping is not needed
} else {
// In all other cases we should try to snap now
bb = m.constrainedSnapStretch(Snapper::SNAPPOINT_BBOX, _bbox_points, default_scale[axis], _origin_for_bboxpoints, axis, symmetrical);
sn = m.constrainedSnapStretch(Snapper::SNAPPOINT_NODE, _snap_points, geom_scale[axis], _origin_for_specpoints, axis, symmetrical);
if (bb.getSnapped()) {
// We snapped the bbox (which is either visual or geometric)
}
if (sn.getSnapped()) {
}
if (symmetrical) {
// on ctrl, apply symmetrical scaling instead of stretching
// Preserve aspect ratio, but never flip in the dimension not being edited (by using fabs())
}
// We didn't snap at all! Don't update the handle position, just calculate the new transformation
// Calculate the new transformation and update the handle position
} else {
// We snapped the special points (e.g. nodes), which are not at the visual bbox
// The handle location however (pt) might however be at the visual bbox, so we
// will have to calculate pt taking the stroke width into account
}
}
// status text
_("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
return TRUE;
}
gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Point &pt, guint state)
{
/* When skewing (or rotating):
* 1) the stroke width will not change. This makes life much easier because we don't have to
* account for that (like for scaling or stretching). As a consequence, all points will
* have the same origin for the transformation and for the snapping.
* 2) When holding shift, the transformation will be relative to the point opposite of
* the handle; otherwise it will be relative to the center as set for the selection
*/
case GDK_SB_H_DOUBLE_ARROW:
break;
case GDK_SB_V_DOUBLE_ARROW:
break;
default:
abort();
break;
}
return false;
}
// Calculate the scale factors, which can be either visual or geometric
// depending on which type of bbox is currently being used (see preferences -> selector tool)
// Prevent shrinking of the selected object, while allowing mirroring
} else {
// Allow expanding of the selected object by integer multiples
}
if (state & GDK_CONTROL_MASK) {
// Snap to defined angle increments
if (snaps) {
}
}
} else {
// Snap to objects, grids, guides
Inkscape::SnappedPoint bb = m.constrainedSnapSkew(Inkscape::Snapper::SNAPPOINT_BBOX, _bbox_points, constraint, s, _origin, dim_b);
Inkscape::SnappedPoint sn = m.constrainedSnapSkew(Inkscape::Snapper::SNAPPOINT_NODE, _snap_points, constraint, s, _origin, dim_b);
// We snapped something, so change the skew to reflect it
} else {
}
} else {
}
}
// Update the handle position
// Calculate the relative affine
for (int i = 0; i < 2; i++) {
}
}
// Update the status text
// TRANSLATORS: don't modify the first ";"
// (it will NOT be displayed as ";" - only the second one will be)
_("<b>Skew</b>: %0.2f°; with <b>Ctrl</b> to snap angle"),
degrees);
return TRUE;
}
{
/* When rotating (or skewing):
* 1) the stroke width will not change. This makes life much easier because we don't have to
* account for that (like for scaling or stretching). As a consequence, all points will
* have the same origin for the transformation and for the snapping.
* 2) When holding shift, the transformation will be relative to the point opposite of
* the handle; otherwise it will be relative to the center as set for the selection
*/
// rotate affine in rotate
double radians;
if (state & GDK_CONTROL_MASK) {
// Snap to defined angle increments
if (snaps) {
}
} else {
}
// Calculate the relative affine
// Update the handle position
// Update the status text
// TRANSLATORS: don't modify the first ";"
// (it will NOT be displayed as ";" - only the second one will be)
_("<b>Rotate</b>: %0.2f°; with <b>Ctrl</b> to snap angle"), degrees);
return TRUE;
}
{
if (state & GDK_CONTROL_MASK) {
} else {
}
}
// screen pixels to snap center to bbox
#define SNAP_DIST 5
// FIXME: take from prefs
for (int i = 0; i < 2; i++) {
}
}
}
}
}
// status text
_message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
return TRUE;
}
/*
* handlers for handle movement
*
*/
void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
{
}
void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
{
}
void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, NR::Point &pt, guint state)
{
}
void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint state)
{
}
void Inkscape::SelTrans::stretch(SPSelTransHandle const &/*handle*/, NR::Point &/*pt*/, guint /*state*/)
{
}
{
}
void Inkscape::SelTrans::skew(SPSelTransHandle const &/*handle*/, NR::Point &/*pt*/, guint /*state*/)
{
}
{
}
void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, NR::Point &pt, guint /*state*/)
{
}
{
/* The amount that we've moved by during this drag */
if (alt) {
/* Alt pressed means keep offset: snap the moved distance to the grid.
** FIXME: this will snap to more than just the grid, nowadays.
*/
} else if (!shift) {
/* We're snapping to things, possibly with a constraint to horizontal or
** vertical movement. Obtain a list of possible translations and then
** pick the smallest.
*/
/* This will be our list of possible translations */
if (control) {
/* Snap to things, and also constrain to horizontal or vertical movement */
// When doing a constrained translation, all points will move in the same direction, i.e.
// either horizontally or vertically. Therefore we only have to specify the direction of
// the constraint-line once. The constraint lines are parallel, but might not be colinear.
// Therefore we will have to set the point through which the constraint-line runs
// individually for each point to be snapped; this will be handled however by _snapTransformed()
dxy));
dxy));
}
} else {
// Let's leave this timer code here for a while. I'll probably need it in the near future (Diederik van Lierop)
/* GTimeVal starttime;
GTimeVal endtime;
g_get_current_time(&starttime); */
/* Snap to things with no constraint */
/*g_get_current_time(&endtime);
double elapsed = ((((double)endtime.tv_sec - starttime.tv_sec) * G_USEC_PER_SEC + (endtime.tv_usec - starttime.tv_usec))) / 1000.0;
std::cout << "Time spent snapping: " << elapsed << std::endl; */
}
/* Pick one */
if (i->getSnapped()) {
best_snapped_point = *i;
dxy = i->getTransformation();
}
}
}
if (best_snapped_point.getSnapped()) {
} else {
// We didn't snap, so remove any previous snap indicator
if (control) {
// If we didn't snap, then we should still constrain horizontally or vertically
// (When we did snap, then this constraint has already been enforced by
// calling constrainedSnapTranslation() above)
} else {
}
}
}
}
// status text
_message_context.setF(Inkscape::NORMAL_MESSAGE, _("<b>Move</b> by %s, %s; with <b>Ctrl</b> to restrict to horizontal/vertical; with <b>Shift</b> to disable snapping"), xs->str, ys->str);
}
// Given a location of a handle at the visual bounding box, find the corresponding location at the
// geometrical bounding box
{
// When the selector tool is using geometric bboxes, then the handle is already
// located at one of the geometric bbox corners
return visual_handle_pos;
}
if (!_geometric_bbox) {
//_getGeomHandlePos() can only be used after _geometric_bbox has been defined!
return visual_handle_pos;
}
// Using the NR::Rect constructor below ensures that "min() < max()", which is important
// because this will also hold for _bbox, and which is required for get_scale_transform_with_stroke()
// Please note that the new_bbox might in fact be just a single line, for example when stretching (in
// which case the handle and origin will be aligned vertically or horizontally)
NR::Point normalized_handle_pos = (visual_handle_pos - new_bbox.min()) * NR::scale(new_bbox.dimensions()).inverse();
// Calculate the absolute affine while taking into account the scaling of the stroke width
// Calculate the scaled geometrical bbox
NR::Rect new_geom_bbox = NR::Rect(_geometric_bbox->min() * abs_affine, _geometric_bbox->max() * abs_affine);
// Find the location of the handle on this new geometrical bbox
return normalized_handle_pos * NR::scale(new_geom_bbox.dimensions()) + new_geom_bbox.min(); //new position of the geometric handle
}
NR::scale Inkscape::calcScaleFactors(NR::Point const &initial_point, NR::Point const &new_point, NR::Point const &origin, bool const skew)
{
// Work out the new scale factors for the bbox
for ( unsigned int i = 0 ; i < 2 ; i++ ) {
if (skew) {
} else {
}
}
}
return scale;
}
// Only for scaling/stretching
{
NR::Matrix abs_affine = NR::translate(-_origin) * NR::Matrix(default_scale) * NR::translate(_origin);
int transform_stroke = false;
gdouble strokewidth = 0;
}
_absolute_affine = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
// return the new handle position
}
// Only for scaling/stretching
{
_absolute_affine = NR::translate(-_origin_for_specpoints) * _relative_affine * NR::translate(_origin_for_specpoints);
NR::Rect visual_bbox = get_visual_bbox(_geometric_bbox, _absolute_affine, _strokewidth, transform_stroke);
// return the new handle position
}
/*
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 :