pen-context.cpp revision 8952e0a5008b96fe895e2ba0ab76b949f5eb485e
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Pen event context implementation.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Authors:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Lauris Kaplinski <lauris@kaplinski.com>
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * bulia byak <buliabyak@users.sf.net>
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Copyright (C) 2000 Lauris Kaplinski
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Copyright (C) 2000-2001 Ximian, Inc.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Copyright (C) 2002 Lauris Kaplinski
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Copyright (C) 2004 Monash University
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Released under GNU GPL, read the file 'COPYING' for more information
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void sp_pen_context_class_init(SPPenContextClass *klass);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void spdc_pen_set_initial_point(SPPenContext *pc, Geom::Point const p);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status = 0);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void spdc_pen_set_ctrl(SPPenContext *pc, Geom::Point const p, guint state);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void spdc_pen_finish_segment(SPPenContext *pc, Geom::Point p, guint state);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void spdc_pen_finish(SPPenContext *pc, gboolean closed);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic bool pen_within_tolerance = false;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic int pen_next_paraxial_direction(const SPPenContext *const pc, Geom::Point const &pt, Geom::Point const &origin, guint state);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state, bool snap);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Register SPPenContext with Gdk and return its type.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Initialize the SPPenContext vtable.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm event_context_class->root_handler = sp_pen_context_root_handler;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm event_context_class->item_handler = sp_pen_context_item_handler;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Callback to initialize SPPenContext object.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Callback to destroy the SPPenContext object's members and itself.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmsp_pen_context_set_polyline_mode(SPPenContext *const pc) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Inkscape::Preferences *prefs = Inkscape::Preferences::get();
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm guint mode = prefs->getInt("/tools/freehand/pen/freehand-mode", 0);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Callback to initialize SPPenContext object.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (((SPEventContextClass *) pen_parent_class)->setup) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Pen indicators */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Inkscape::Preferences *prefs = Inkscape::Preferences::get();
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Finalization callback.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (((SPEventContextClass *) pen_parent_class)->finish) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm ((SPEventContextClass *) pen_parent_class)->finish(ec);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Callback that sets key to value in pen context.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmsp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Snaps new node relative to the previous node.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmspdc_endpoint_snap(SPPenContext const *const pc, Geom::Point &p, guint const state)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ((state & GDK_CONTROL_MASK) && !pc->polylines_paraxial) { //CTRL enables angular snapping
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // We cannot use shift here to disable snapping because the shift-key is already used
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // to toggle the paraxial direction; if the user wants to disable snapping (s)he will
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // have to use the %-key, the menu, or the snap toolbar
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // snap constrained
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // snap freely
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Snaps new node's handle relative to the new node.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmspdc_endpoint_snap_handle(SPPenContext const *const pc, Geom::Point &p, guint const state)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmsp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (((SPEventContextClass *) pen_parent_class)->item_handler)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Callback to handle all pen events.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmsp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm = ((SPEventContextClass *) pen_parent_class)->root_handler;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Handle mouse button press event.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // skip event processing if events are disabled
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (bevent.button == 1 && !event_context->space_panning
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Grab mouse, so release will not pass unnoticed */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Test whether we hit any anchor. */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* In click mode we add point on release */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* This is allowed, if we just canceled curve */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* This is allowed, if we just canceled curve */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ((bevent.state & GDK_CONTROL_MASK) && (pc->polylines_only || pc->polylines_paraxial)) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm spdc_create_single_dot(event_context, p, "/tools/freehand/pen", bevent.state);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // TODO: Perhaps it would be nicer to rearrange the following case
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // distinction so that the case of a waiting LPE is treated separately
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Set start anchor */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Adjust point to anchor if needed; if we have a waiting LPE, we need
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm a fresh path to be created so don't continue an existing one */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // This is the first click of a new curve; deselect item so that
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // this curve is not combined with it (unless it is drawn from its
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // anchor, which is handled by the sibling branch above)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Inkscape::Selection * const selection = sp_desktop_selection(desktop);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* if we have a waiting LPE, we need a fresh path to be created
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm so don't append to an existing one */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Create green anchor */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Set end anchor */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // we hit an anchor, will finish the curve (either with or without closing)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // in release handler
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // we clicked on the current curve start, so close it even if
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // we drag a handle away from it
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm } else if (pc->expecting_clicks_for_LPE == 1 && pc->npoints != 0) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // when the last click for a waiting LPE occurs we want to finish the path
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // finishing at the start anchor, close curve
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // finishing at some other anchor, finish curve but not close
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // right click - finish path
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Handle motion_notify event.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmpen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // allow scrolling
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // skip motion events if pen events are disabled
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Inkscape::Preferences *prefs = Inkscape::Preferences::get();
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ( Geom::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm return FALSE; // Do not drag if we're within tolerance from origin.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // Once the user has moved farther than tolerance from the original location
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // (indicating they intend to move the object, not click), then always process the
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // motion notify coordinates as given (no snapping back to origin)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Find desktop coordinates */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Test, whether we hit any anchor */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Only set point, if we are already appending */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Placing controls is last operation in CLOSE state */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* This is perfectly valid */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Only set point, if we are already appending */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (!anchor) { /* Snap node only if not hitting anchor */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm spdc_pen_set_subsequent_point(pc, p, true, mevent.state);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm spdc_pen_set_subsequent_point(pc, anchor->dp, false, mevent.state);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
ea8dd7683dd12883474f6cf9b5f424f8ed831166Kris pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Placing controls is last operation in CLOSE state */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // snap the handle
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* This is perfectly valid */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Handle mouse button release event.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmpen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // skip event processing if events are disabled
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ( revent.button == 1 && !event_context->space_panning) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Find desktop coordinates */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Test whether we hit any anchor. */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Start new thread only with button release */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Set end anchor here */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* End current segment */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* End current segment */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (!anchor) { /* Snap node only if not hitting anchor */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* This is allowed, if we just canceled curve */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // finishing at the start anchor, close curve
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // finishing at some other anchor, finish curve but not close
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* This is allowed, if we just cancelled curve */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Release grab now */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // TODO: can we be sure that the path was created correctly?
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // TODO: should we offer an option to collect the clicks in a list?
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // we have an already created LPE waiting for a path
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // the case that we need to create a new LPE and apply it to the just-drawn path is
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmpen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // only end on LMB double click. Otherwise horizontal scrolling causes ending of the path
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // remove old piecewise green canvasitems
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // one canvas bpath for all of green_curve
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // handles
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Geom::Curve const * last_seg = pc->green_curve->last_segment();
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmpen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // start anchor too
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmpen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->p[1] = pc->p[0] + (Geom::Point)( (*cubic)[3] - (*cubic)[2] );
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmpen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Inkscape::Preferences *prefs = Inkscape::Preferences::get();
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000, "px"); // in px
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm else { // no alt
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm else { // no alt
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm else { // no alt
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm else { // no alt
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm/* TODO: this is not yet enabled?? looks like some traces of the Geometry tool
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_P:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_p:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT_ONLY) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm ret = TRUE;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_C:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_c:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT_ONLY) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm ret = TRUE;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_B:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_b:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT_ONLY) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm ret = TRUE;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_A:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm case GDK_a:
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (MOD__SHIFT_ONLY) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm ret = TRUE;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // if drawing, cancel, otherwise pass it up for deselecting
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // if drawing, cancel, otherwise pass it up for undo
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ( pc->green_curve->is_empty() || (pc->green_curve->last_segment() == NULL) ) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // do nothing; this event should be handled upstream
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Reset red curve */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Destroy topmost green bpath */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Get last segment */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // The code below assumes that pc->green_curve has only ONE path !
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Geom::Curve const * crv = pc->green_curve->last_segment();
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Blue */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* Green */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmspdc_pen_set_initial_point(SPPenContext *const pc, Geom::Point const p)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->p[0] = p;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * Show the status message for the current line/curve segment.
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * This type of message always shows angle/distance as the last
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm * two parameters ("angle %3.2f°, distance %s").
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmspdc_pen_set_angle_distance_status_message(SPPenContext *const pc, Geom::Point const p, int pc_point_to_compare, gchar const *message)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm GString *dist = SP_PX_TO_METRIC_STRING(Geom::L2(rel), desktop->namedview->getDefaultMetric());
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm double angle = atan2(rel[Geom::Y], rel[Geom::X]) * 180 / M_PI;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm Inkscape::Preferences *prefs = Inkscape::Preferences::get();
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if (prefs->getBool("/options/compassangledisplay/value", 0) != 0)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmstatic void
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrmspdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status)
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // we are drawing horizontal/vertical lines and hit an anchor;
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // if the previous point and the anchor are not aligned either horizontally or vertically...
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm if ((abs(p[Geom::X] - origin[Geom::X]) > 1e-9) && (abs(p[Geom::Y] - origin[Geom::Y]) > 1e-9)) {
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // ...then we should draw an L-shaped path, consisting of two paraxial segments
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm pen_set_to_nearest_horiz_vert(pc, intermed, status, false);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm // one of the 'regular' modes
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm _("<b>Curve segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
da58597f9f9ecb17c4f545c4483a844a363bcc27pjrm _("<b>Line segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
a4030d5ca449e7e384bc699cd249ee704faaeab0Chris Morgan spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
spdc_pen_set_angle_distance_status_message(pc, p, 0, _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle"));
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");
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
return pen_last_paraxial_dir;
void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state, bool snap)
if (!snap) {
if (next_dir == 0) {
// selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
m.unSetup();