pencil-tool.cpp revision 0dc651bab109d5f3a6362dbb287ea4c1e97fa5ef
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Pencil event context implementation.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Lauris Kaplinski <lauris@kaplinski.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * bulia byak <buliabyak@users.sf.net>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Jon A. Cruz <jon@joncruz.org>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2000 Lauris Kaplinski
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2000-2001 Ximian, Inc.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2002 Lauris Kaplinski
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2004 Monash University
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Released under GNU GPL, read the file 'COPYING' for more information
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstatic bool pencil_within_tolerance = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstatic bool in_svg_plane(Geom::Point const &p) { return Geom::LInfty(p) < 1e18; }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool pencilContextRegistered = ToolFactory::instance().registerObject("/tools/freehand/pencil", createPencilContext);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenconst std::string PencilTool::prefsPath = "/tools/freehand/pencil";
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Inkscape::Preferences *prefs = Inkscape::Preferences::get();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (prefs->getBool("/tools/freehand/pencil/selcue")) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->is_drawing = false;
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen/** Snaps new node relative to the previous node. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenvoid PencilTool::_endpointSnap(Geom::Point &p, guint const state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ((state & GDK_CONTROL_MASK)) { //CTRL enables constrained snapping
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (this->npoints > 0) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen spdc_endpoint_snap_rotation(this, p, this->p[0], state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //After all, the user explicitely asked for angular snapping by
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //pressing CTRL
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen boost::optional<Geom::Point> origin = this->npoints > 0 ? this->p[0] : boost::optional<Geom::Point>();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen spdc_endpoint_snap_free(this, p, origin, state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Callback for handling all pencil context events.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenbool PencilTool::root_handler(GdkEvent* event) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool ret = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleMotionNotify(event->motion);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleButtonRelease(event->button);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleKeyPress(get_group0_keyval (&event->key), event->key.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleKeyRelease(get_group0_keyval (&event->key), event->key.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenbool PencilTool::_handleButtonPress(GdkEventButton const &bevent) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool ret = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( bevent.button == 1 && !this->space_panning) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Inkscape::Selection *selection = sp_desktop_selection(desktop);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (Inkscape::have_viable_layer(desktop, this->message_context) == false) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!this->grab) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Grab mouse, so release will not pass unnoticed */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen sp_canvas_item_grab(this->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point const button_w(bevent.x, bevent.y);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Find desktop coordinates */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Test whether we hit any anchor. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SPDrawAnchor *anchor = spdc_test_inside(this, button_w);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen pencil_drag_origin_w = Geom::Point(bevent.x,bevent.y);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen switch (this->state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Current segment will be finished with release */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Set first point of sequence */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SnapManager &m = desktop->namedview->snap_manager;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen spdc_create_single_dot(this, p, "/tools/freehand/pencil", bevent.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // This is the first click of a new curve; deselect item so that
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // this curve is not combined with it (unless it is drawn from its
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // anchor, which is handled by the sibling branch above)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenbool PencilTool::_handleMotionNotify(GdkEventMotion const &mevent) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // mouse was accidentally moved during Ctrl+click;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // ignore the motion and create a single point
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->is_drawing = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool ret = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (this->space_panning || (mevent.state & GDK_BUTTON2_MASK) || (mevent.state & GDK_BUTTON3_MASK)) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // allow scrolling
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( ( mevent.state & GDK_BUTTON1_MASK ) && !this->grab && this->is_drawing) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Grab mouse, so release will not pass unnoticed */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen sp_canvas_item_grab(this->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Find desktop coordinates */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point p = desktop->w2d(Geom::Point(mevent.x, mevent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Test whether we hit any anchor. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SPDrawAnchor *anchor = spdc_test_inside(this, Geom::Point(mevent.x, mevent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Inkscape::Preferences *prefs = Inkscape::Preferences::get();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( Geom::LInfty( Geom::Point(mevent.x,mevent.y) - pencil_drag_origin_w ) < tolerance ) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return false; // Do not drag if we're within tolerance from origin.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // Once the user has moved farther than tolerance from the original location
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // (indicating they intend to move the object, not click), then always process the
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // motion notify coordinates as given (no snapping back to origin)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen switch (this->state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Set red endpoint */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* We may be idle or already freehand */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( (mevent.state & GDK_BUTTON1_MASK) && this->is_drawing ) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen sp_event_context_discard_delayed_snap_event(this);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Create green anchor */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->green_anchor = sp_draw_anchor_new(this, this->green_curve, TRUE, this->p[0]);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( this->npoints != 0) { // buttonpress may have happened before we entered draw context!
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // Only in freehand mode we have to add the first point also to pc->ps (apparently)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // - We cannot add this point in spdc_set_startpoint, because we only need it for freehand
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // - We cannot do this in the button press handler because at that point we don't know yet
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // wheter we're going into freehand mode or not
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (!anchor && this->anchor_statusbar) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (!anchor) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (!anchor && this->anchor_statusbar) {
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen // Show the pre-snap indicator to communicate to the user where we would snap to if he/she were to
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen // a) press the mousebutton to start a freehand drawing, or
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen // b) release the mousebutton to finish a freehand drawing
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen if (!this->sp_event_context_knot_mouseover()) {
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen SnapManager &m = desktop->namedview->snap_manager;
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelenbool PencilTool::_handleButtonRelease(GdkEventButton const &revent) {
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen bool ret = false;
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen if ( revent.button == 1 && this->is_drawing && !this->space_panning) {
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen this->is_drawing = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Find desktop coordinates */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point p = desktop->w2d(Geom::Point(revent.x, revent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Test whether we hit any anchor. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SPDrawAnchor *anchor = spdc_test_inside(this, Geom::Point(revent.x, revent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen switch (this->state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Releasing button in idle mode means single click */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* We have already set up start point/anchor in button_press */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
ret = true;
if (anchor) {
this->_setEndpoint(p);
this->_finishEndpoint();
ret = true;
/* sketch mode: interpolate the sketched path and improve the current output path with the new interpolation. don't finish sketch */
this->_sketchInterpolate();
if (this->green_anchor) {
if (anchor) {
if (p_end != p) {
this->_interpolate();
if (this->green_anchor) {
this->sketch_n = 0;
ret = true;
case SP_PENCIL_CONTEXT_SKETCH:
if (this->grab) {
ret = true;
return ret;
if (this->grab) {
this->is_drawing = false;
while (this->green_bpaths) {
if (this->green_anchor) {
bool ret = false;
switch (keyval) {
case GDK_KEY_Up:
case GDK_KEY_Down:
case GDK_KEY_KP_Up:
case GDK_KEY_KP_Down:
ret = true;
case GDK_KEY_Escape:
if (this->npoints != 0) {
this->_cancel();
ret = true;
case GDK_KEY_z:
case GDK_KEY_Z:
this->_cancel();
ret = true;
case GDK_KEY_g:
case GDK_KEY_G:
ret = true;
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Meta_L:
case GDK_KEY_Meta_R:
this->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("<b>Sketch mode</b>: holding <b>Alt</b> interpolates between sketched paths. Release <b>Alt</b> to finalize."));
return ret;
bool ret = false;
switch (keyval) {
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Meta_L:
case GDK_KEY_Meta_R:
spdc_concat_colors_and_flush(this, false);
this->sketch_n = 0;
if (this->green_anchor) {
ret = true;
return ret;
this->npoints = 0;
this->red_curve_is_valid = false;
if (in_svg_plane(p)) {
this->p[this->npoints++] = p;
if (this->npoints == 0) {
|| !in_svg_plane(p) )
this->red_curve_is_valid = true;
&& in_svg_plane(p) )
this->p[this->npoints++] = p;
this->_fitAndSplit();
square(double const x)
double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4;
double const tolerance_sq = 0.02 * square(this->desktop->w2d().descrim() * tol) * exp(0.2 * tol - 2);
this->red_curve_is_valid = false;
int const n_segs = Geom::bezier_fit_cubic_r(b.data(), this->ps.data(), n_points, tolerance_sq, max_segs);
if (n_segs > 0) {
for (int c = 0; c < n_segs; c++) {
delete last_seg_reverse;
double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4;
double const tolerance_sq = 0.02 * square(this->desktop->w2d().descrim() * tol) * exp(0.2 * tol - 2);
this->red_curve_is_valid = false;
int const n_segs = Geom::bezier_fit_cubic_r(b.data(), this->ps.data(), n_points, tolerance_sq, max_segs);
if (n_segs > 0) {
for (int c = 0; c < n_segs; c++) {
if (this->sketch_n > 0) {
if (average_all_sketches) {
delete pathv;
this->sketch_n++;
delete last_seg_reverse;
double const tolerance_sq = 0;
if ( n_segs > 0
this->red_curve_is_valid = true;
delete last_seg_reverse;
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), this->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
this->red_curve_is_valid = false;