pen-tool.cpp revision 4e47dd454b9d10cbca9ad0390cfbd176858287b8
/** \file
* Pen event context implementation.
*/
/*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* Jon A. Cruz <jon@joncruz.org>
*
* Copyright (C) 2000 Lauris Kaplinski
* Copyright (C) 2000-2001 Ximian, Inc.
* Copyright (C) 2002 Lauris Kaplinski
* Copyright (C) 2004 Monash University
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <gdk/gdkkeysyms.h>
#include <cstring>
#include <string>
#include "ui/tools/pen-tool.h"
#include "sp-namedview.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "selection.h"
#include "selection-chemistry.h"
#include "draw-anchor.h"
#include "message-stack.h"
#include "message-context.h"
#include "preferences.h"
#include "sp-path.h"
#include "display/sp-canvas.h"
#include "pixmaps/cursor-pen.xpm"
#include "display/canvas-bpath.h"
#include "display/sp-ctrlline.h"
#include "display/sodipodi-ctrl.h"
#include "macros.h"
#include "context-fns.h"
#include "tools-switch.h"
#include "ui/control-manager.h"
#include "tool-factory.h"
using Inkscape::ControlManager;
namespace Inkscape {
namespace UI {
namespace Tools {
static bool pen_within_tolerance = false;
static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
namespace {
ToolBase* createPenContext() {
return new PenTool();
}
bool penContextRegistered = ToolFactory::instance().registerObject("/tools/freehand/pen", createPenContext);
}
}
, p()
, npoints(0)
, mode(MODE_CLICK)
, polylines_only(false)
, polylines_paraxial(false)
, num_clicks(0)
, waiting_LPE(NULL)
, waiting_item(NULL)
, events_disabled(false)
{
}
, p()
, npoints(0)
, mode(MODE_CLICK)
, polylines_only(false)
, polylines_paraxial(false)
, num_clicks(0)
, waiting_LPE(NULL)
, waiting_item(NULL)
, events_disabled(false)
{
}
if (this->c0) {
sp_canvas_item_destroy(this->c0);
}
if (this->c1) {
sp_canvas_item_destroy(this->c1);
}
if (this->cl0) {
sp_canvas_item_destroy(this->cl0);
}
if (this->cl1) {
sp_canvas_item_destroy(this->cl1);
}
if (this->expecting_clicks_for_LPE > 0) {
// we received too few clicks to sanely set the parameter path so we remove the LPE from the item
this->waiting_item->removeCurrentPathEffect(false);
}
}
void PenTool::setPolylineMode() {
}
/**
* Callback to initialize PenTool object.
*/
FreehandBase::setup();
// Pen indicators
sp_canvas_item_hide(this->c0);
sp_canvas_item_hide(this->c1);
sp_canvas_item_hide(this->cl0);
sp_canvas_item_hide(this->cl1);
sp_event_context_read(this, "mode");
this->anchor_statusbar = false;
this->setPolylineMode();
this->enableSelectionCue();
}
}
this->num_clicks = 0;
this->_resetColors();
sp_canvas_item_hide(this->c0);
sp_canvas_item_hide(this->c1);
sp_canvas_item_hide(this->cl0);
sp_canvas_item_hide(this->cl1);
this->message_context->clear();
}
/**
* Finalization callback.
*/
if (this->npoints != 0) {
this->_cancel();
}
FreehandBase::finish();
}
/**
* Callback that sets key to value in pen context.
*/
if (name == "mode") {
} else {
this->mode = MODE_CLICK;
}
}
}
bool PenTool::hasWaitingLPE() {
// note: waiting_LPE_type is defined in SPDrawContext
return (this->waiting_LPE != NULL ||
}
/**
* Snaps new node relative to the previous node.
*/
if (this->npoints > 0) {
spdc_endpoint_snap_rotation(this, p, this->p[0], state);
}
} else {
// We cannot use shift here to disable snapping because the shift-key is already used
// to toggle the paraxial direction; if the user wants to disable snapping (s)he will
// have to use the %-key, the menu, or the snap toolbar
if ((this->npoints > 0) && this->polylines_paraxial) {
// snap constrained
this->_setToNearestHorizVert(p, state, true);
} else {
// snap freely
boost::optional<Geom::Point> origin = this->npoints > 0 ? this->p[0] : boost::optional<Geom::Point>();
spdc_endpoint_snap_free(this, p, origin, state); // pass the origin, to allow for perpendicular / tangential snapping
}
}
}
/**
* Snaps new node's handle relative to the new node.
*/
this->npoints == 5 ));
} else {
}
}
}
bool ret = false;
case GDK_BUTTON_PRESS:
break;
case GDK_BUTTON_RELEASE:
break;
default:
break;
}
if (!ret) {
}
return ret;
}
/**
* Callback to handle all pen events.
*/
bool ret = false;
case GDK_BUTTON_PRESS:
break;
case GDK_MOTION_NOTIFY:
break;
case GDK_BUTTON_RELEASE:
break;
case GDK_2BUTTON_PRESS:
break;
case GDK_KEY_PRESS:
break;
default:
break;
}
if (!ret) {
}
return ret;
}
/**
* Handle mouse button press event.
*/
if (this->events_disabled) {
// skip event processing if events are disabled
return false;
}
bool ret = false;
// make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
&& this->expecting_clicks_for_LPE != 1) {
return true;
}
if (!this->grab ) {
// Grab mouse, so release will not pass unnoticed
}
pen_within_tolerance = true;
// Test whether we hit any anchor.
switch (this->mode) {
case PenTool::MODE_CLICK:
// In click mode we add point on release
switch (this->state) {
break;
// This is allowed, if we just canceled curve
break;
default:
break;
}
break;
switch (this->state) {
// This is allowed, if we just canceled curve
if (this->npoints == 0) {
p = event_dt;
m.unSetup();
}
ret = true;
break;
}
// TODO: Perhaps it would be nicer to rearrange the following case
// distinction so that the case of a waiting LPE is treated separately
// Set start anchor
if (anchor && !this->hasWaitingLPE()) {
// Adjust point to anchor if needed; if we have a waiting LPE, we need
// a fresh path to be created so don't continue an existing one
} else {
// This is the first click of a new curve; deselect item so that
// this curve is not combined with it (unless it is drawn from its
// anchor, which is handled by the sibling branch above)
// if we have a waiting LPE, we need a fresh path to be created
// so don't append to an existing one
}
// Create green anchor
p = event_dt;
}
this->_setInitialPoint(p);
} else {
// Set end anchor
if (anchor) {
// we hit an anchor, will finish the curve (either with or without closing)
// in release handler
// we clicked on the current curve start, so close it even if
// we drag a handle away from it
this->green_closed = TRUE;
}
ret = true;
break;
} else {
p = event_dt;
this->_setSubsequentPoint(p, true);
}
}
ret = true;
break;
g_warning("Button down in CONTROL state");
break;
g_warning("Button down in CLOSE state");
break;
default:
break;
}
break;
default:
break;
}
// when the last click for a waiting LPE occurs we want to finish the path
if (this->green_closed) {
// finishing at the start anchor, close curve
this->_finish(true);
} else {
// finishing at some other anchor, finish curve but not close
this->_finish(false);
}
ret = true;
// right click - finish path
this->_finish(false);
ret = true;
}
if (this->expecting_clicks_for_LPE > 0) {
--this->expecting_clicks_for_LPE;
}
return ret;
}
/**
* Handle motion_notify event.
*/
bool ret = false;
// allow scrolling
return false;
}
if (this->events_disabled) {
// skip motion events if pen events are disabled
return false;
}
if (pen_within_tolerance) {
return false; // Do not drag if we're within tolerance from origin.
}
}
// Once the user has moved farther than tolerance from the original location
// (indicating they intend to move the object, not click), then always process the
// motion notify coordinates as given (no snapping back to origin)
pen_within_tolerance = false;
// Find desktop coordinates
// Test, whether we hit any anchor
switch (this->mode) {
case PenTool::MODE_CLICK:
switch (this->state) {
if ( this->npoints != 0 ) {
// Only set point, if we are already appending
this->_setSubsequentPoint(p, true);
} else if (!this->sp_event_context_knot_mouseover()) {
m.unSetup();
}
break;
// Placing controls is last operation in CLOSE state
ret = true;
break;
// This is perfectly valid
break;
default:
break;
}
break;
switch (this->state) {
if ( this->npoints > 0 ) {
// Only set point, if we are already appending
if (!anchor) { // Snap node only if not hitting anchor
} else {
}
if (anchor && !this->anchor_statusbar) {
this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
this->anchor_statusbar = true;
} else if (!anchor && this->anchor_statusbar) {
this->message_context->clear();
this->anchor_statusbar = false;
}
ret = true;
} else {
if (anchor && !this->anchor_statusbar) {
this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
this->anchor_statusbar = true;
} else if (!anchor && this->anchor_statusbar) {
this->message_context->clear();
this->anchor_statusbar = false;
}
if (!this->sp_event_context_knot_mouseover()) {
m.unSetup();
}
}
break;
// Placing controls is last operation in CLOSE state
// snap the handle
if (!this->polylines_only) {
} else {
}
ret = true;
break;
// This is perfectly valid
break;
default:
if (!this->sp_event_context_knot_mouseover()) {
m.unSetup();
}
break;
}
break;
default:
break;
}
return ret;
}
/**
* Handle mouse button release event.
*/
if (this->events_disabled) {
// skip event processing if events are disabled
return false;
}
bool ret = false;
// Find desktop coordinates
// Test whether we hit any anchor.
switch (this->mode) {
case PenTool::MODE_CLICK:
switch (this->state) {
if ( this->npoints == 0 ) {
// Start new thread only with button release
if (anchor) {
}
this->_setInitialPoint(p);
} else {
// Set end anchor here
if (anchor) {
}
}
ret = true;
break;
// End current segment
ret = true;
break;
// End current segment
if (!anchor) { // Snap node only if not hitting anchor
}
this->_finish(true);
ret = true;
break;
// This is allowed, if we just canceled curve
ret = true;
break;
default:
break;
}
break;
switch (this->state) {
break;
if (this->green_closed) {
// finishing at the start anchor, close curve
this->_finish(true);
} else {
// finishing at some other anchor, finish curve but not close
this->_finish(false);
}
break;
// This is allowed, if we just cancelled curve
break;
default:
break;
}
ret = true;
break;
default:
break;
}
if (this->grab) {
// Release grab now
}
ret = true;
this->green_closed = FALSE;
}
// TODO: can we be sure that the path was created correctly?
// TODO: should we offer an option to collect the clicks in a list?
if (this->expecting_clicks_for_LPE == 0 && this->hasWaitingLPE()) {
this->setPolylineMode();
if (this->waiting_LPE) {
// we have an already created LPE waiting for a path
this->waiting_LPE = NULL;
} else {
// the case that we need to create a new LPE and apply it to the just-drawn path is
// handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
}
}
return ret;
}
bool ret = false;
// only end on LMB double click. Otherwise horizontal scrolling causes ending of the path
ret = true;
}
return ret;
}
void PenTool::_redrawAll() {
// green
if (this->green_bpaths) {
// remove old piecewise green canvasitems
while (this->green_bpaths) {
}
// one canvas bpath for all of green_curve
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), this->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
}
if (this->green_anchor)
// handles
if (this->p[0] != this->p[1]) {
sp_canvas_item_show(this->c1);
sp_canvas_item_show(this->cl1);
} else {
sp_canvas_item_hide(this->c1);
sp_canvas_item_hide(this->cl1);
}
if (last_seg) {
if ( cubic &&
(*cubic)[2] != this->p[0] )
{
sp_canvas_item_show(this->c0);
sp_canvas_item_show(this->cl0);
} else {
sp_canvas_item_hide(this->c0);
sp_canvas_item_hide(this->cl0);
}
}
}
if (this->npoints != 5)
return;
// green
if (!this->green_curve->is_empty()) {
} else {
// start anchor too
if (this->green_anchor) {
}
}
// red
this->_redrawAll();
}
}
void PenTool::_lastpointToCurve() {
if (this->npoints != 5)
return;
Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( this->green_curve->last_segment() );
if ( cubic ) {
} else {
this->p[1] = this->p[0] + (1./3)*(this->p[3] - this->p[0]);
}
this->_redrawAll();
}
void PenTool::_lastpointToLine() {
if (this->npoints != 5)
return;
this->p[1] = this->p[0];
this->_redrawAll();
}
bool ret = false;
gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000, "px"); // in px
case GDK_KEY_Left: // move last point left
case GDK_KEY_KP_Left:
if (MOD__SHIFT(event)) {
}
else {
}
}
else { // no alt
if (MOD__SHIFT(event)) {
}
else {
}
}
ret = true;
}
break;
case GDK_KEY_Up: // move last point up
case GDK_KEY_KP_Up:
if (MOD__SHIFT(event)) {
}
else {
}
}
else { // no alt
if (MOD__SHIFT(event)) {
}
else {
}
}
ret = true;
}
break;
case GDK_KEY_Right: // move last point right
case GDK_KEY_KP_Right:
if (MOD__SHIFT(event)) {
}
else {
}
}
else { // no alt
if (MOD__SHIFT(event)) {
}
else {
}
}
ret = true;
}
break;
case GDK_KEY_Down: // move last point down
case GDK_KEY_KP_Down:
if (MOD__SHIFT(event)) {
}
else {
}
}
else { // no alt
if (MOD__SHIFT(event)) {
}
else {
}
}
ret = true;
}
break;
/*TODO: this is not yet enabled?? looks like some traces of the Geometry tool
case GDK_KEY_P:
case GDK_KEY_p:
if (MOD__SHIFT_ONLY(event)) {
sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
ret = TRUE;
}
break;
case GDK_KEY_C:
case GDK_KEY_c:
if (MOD__SHIFT_ONLY(event)) {
sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
ret = TRUE;
}
break;
case GDK_KEY_B:
case GDK_KEY_b:
if (MOD__SHIFT_ONLY(event)) {
sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
ret = TRUE;
}
break;
case GDK_KEY_A:
case GDK_KEY_a:
if (MOD__SHIFT_ONLY(event)) {
sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
ret = TRUE;
}
break;
*/
case GDK_KEY_U:
case GDK_KEY_u:
if (MOD__SHIFT_ONLY(event)) {
this->_lastpointToCurve();
ret = true;
}
break;
case GDK_KEY_L:
case GDK_KEY_l:
if (MOD__SHIFT_ONLY(event)) {
this->_lastpointToLine();
ret = true;
}
break;
case GDK_KEY_Return:
case GDK_KEY_KP_Enter:
if (this->npoints != 0) {
this->_finish(false);
ret = true;
}
break;
case GDK_KEY_Escape:
if (this->npoints != 0) {
// if drawing, cancel, otherwise pass it up for deselecting
this->_cancel ();
ret = true;
}
break;
case GDK_KEY_z:
case GDK_KEY_Z:
// if drawing, cancel, otherwise pass it up for undo
this->_cancel ();
ret = true;
}
break;
case GDK_KEY_g:
case GDK_KEY_G:
if (MOD__SHIFT_ONLY(event)) {
sp_selection_to_guides(this->desktop);
ret = true;
}
break;
case GDK_KEY_BackSpace:
case GDK_KEY_Delete:
case GDK_KEY_KP_Delete:
this->_cancel ();
ret = true;
} else {
// do nothing; this event should be handled upstream
}
} else {
// Reset red curve
// Destroy topmost green bpath
if (this->green_bpaths) {
if (this->green_bpaths->data) {
}
}
// Get last segment
if ( this->green_curve->is_empty() ) {
g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
break;
}
// The code below assumes that pc->green_curve has only ONE path !
this->p[0] = crv->initialPoint();
} else {
this->p[1] = this->p[0];
}
this->npoints = 2;
// delete the last segment of the green curve
this->npoints = 5;
if (this->green_bpaths) {
if (this->green_bpaths->data) {
}
}
this->green_curve->reset();
} else {
this->green_curve->backspace();
}
sp_canvas_item_hide(this->c0);
sp_canvas_item_hide(this->c1);
sp_canvas_item_hide(this->cl0);
sp_canvas_item_hide(this->cl1);
this->_setSubsequentPoint(pt, true);
ret = true;
}
break;
default:
break;
}
return ret;
}
void PenTool::_resetColors() {
// Red
// Blue
this->blue_curve->reset();
// Green
while (this->green_bpaths) {
}
this->green_curve->reset();
if (this->green_anchor) {
}
this->npoints = 0;
this->red_curve_is_valid = false;
}
this->p[0] = p;
this->p[1] = p;
this->npoints = 2;
}
/**
* two parameters ("angle %3.2f°, distance %s").
*/
void PenTool::_setAngleDistanceStatusMessage(Geom::Point const p, int pc_point_to_compare, gchar const *message) {
if (angle < 0) {
angle += 360;
}
}
}
// todo: Check callers to see whether 2 <= npoints is guaranteed.
this->p[2] = p;
this->p[3] = p;
this->p[4] = p;
this->npoints = 5;
bool is_curve;
if (this->polylines_paraxial && !statusbar) {
// we are drawing horizontal/vertical lines and hit an anchor;
// if the previous point and the anchor are not aligned either horizontally or vertically...
if ((std::abs(p[Geom::X] - origin[Geom::X]) > 1e-9) && (std::abs(p[Geom::Y] - origin[Geom::Y]) > 1e-9)) {
// ...then we should draw an L-shaped path, consisting of two paraxial segments
}
is_curve = false;
} else {
// one of the 'regular' modes
if (this->p[1] != this->p[0]) {
is_curve = true;
} else {
is_curve = false;
}
}
if (statusbar) {
_("<b>Curve segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
_("<b>Line segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
this->_setAngleDistanceStatusMessage(p, 0, message);
}
}
sp_canvas_item_show(this->c1);
sp_canvas_item_show(this->cl1);
if ( this->npoints == 2 ) {
this->p[1] = p;
sp_canvas_item_hide(this->c0);
sp_canvas_item_hide(this->cl0);
this->_setAngleDistanceStatusMessage(p, 0, _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle"));
} else if ( this->npoints == 5 ) {
this->p[4] = p;
sp_canvas_item_show(this->c0);
sp_canvas_item_show(this->cl0);
bool is_symm = false;
is_symm = true;
}
_("<b>Curve handle, symmetric</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only") :
_("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only");
} else {
}
}
if (this->polylines_paraxial) {
}
++this->num_clicks;
/// \todo fixme:
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), this->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
this->p[0] = this->p[3];
this->p[1] = this->p[4];
this->npoints = 2;
}
}
if (this->expecting_clicks_for_LPE > 1) {
// don't let the path be finished before we have collected the required number of mouse clicks
return;
}
this->num_clicks = 0;
this->_disableEvents();
this->message_context->clear();
spdc_concat_colors_and_flush(this, closed);
this->npoints = 0;
sp_canvas_item_hide(this->c0);
sp_canvas_item_hide(this->c1);
sp_canvas_item_hide(this->cl0);
sp_canvas_item_hide(this->cl1);
if (this->green_anchor) {
}
this->_enableEvents();
}
void PenTool::_disableEvents() {
this->events_disabled = true;
}
void PenTool::_enableEvents() {
g_return_if_fail(this->events_disabled != 0);
this->events_disabled = false;
}
void PenTool::waitForLPEMouseClicks(Inkscape::LivePathEffect::EffectType effect_type, unsigned int num_clicks, bool use_polylines) {
return;
this->waiting_LPE_type = effect_type;
this->expecting_clicks_for_LPE = num_clicks;
this->polylines_only = use_polylines;
this->polylines_paraxial = false; // TODO: think if this is correct for all cases
}
int PenTool::nextParaxialDirection(Geom::Point const &pt, Geom::Point const &origin, guint state) const {
//
// after the first mouse click we determine whether the mouse pointer is closest to a
// horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
// orthogonal to the last one; pressing Shift toggles the direction
//
// num_clicks is not reliable because spdc_pen_finish_segment is sometimes called too early
// (on first mouse release), in which case num_clicks immediately becomes 1.
// if (pc->num_clicks == 0) {
if (this->green_curve->is_empty()) {
// first mouse click
return pen_last_paraxial_dir;
} else {
// subsequent mouse click
}
}
if (!snap) {
if (next_dir == 0) {
// line is forced to be horizontal
} else {
// line is forced to be vertical
}
} else {
// Create a horizontal or vertical constraint line
// Snap along the constraint line; if we didn't snap then still the constraint will be applied
// selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
// TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
m.unSetup();
}
}
}
}
}
/*
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:fileencoding=utf-8:textwidth=99 :