pen-context.cpp revision 5ef24b8088d7de1d545e852c2de9e7f21e3a290e
/** \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 "pen-context.h"
#include "sp-namedview.h"
#include "sp-metrics.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"
using Inkscape::ControlManager;
static void spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status = 0);
static bool pen_within_tolerance = false;
static int pen_next_paraxial_direction(const SPPenContext *const pc, Geom::Point const &pt, Geom::Point const &origin, guint state);
static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state, bool snap);
static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
#include "tool-factory.h"
namespace {
return new SPPenContext();
}
bool penContextRegistered = ToolFactory::instance().registerObject("/tools/freehand/pen", createPenContext);
}
return SPPenContext::prefsPath;
}
SPPenContext* pc = this;
pc->polylines_only = false;
pc->polylines_paraxial = false;
pc->expecting_clicks_for_LPE = 0;
pc->events_disabled = 0;
pc->num_clicks = 0;
}
SPPenContext::~SPPenContext() {
}
}
}
}
//G_OBJECT_CLASS(sp_pen_context_parent_class)->dispose(object);
if (pc->expecting_clicks_for_LPE > 0) {
// we received too few clicks to sanely set the parameter path so we remove the LPE from the item
}
}
}
/**
* Callback to initialize SPPenContext object.
*/
void SPPenContext::setup() {
SPEventContext* ec = this;
// if (((SPEventContextClass *) sp_pen_context_parent_class)->setup) {
// ((SPEventContextClass *) sp_pen_context_parent_class)->setup(ec);
// }
SPDrawContext::setup();
// Pen indicators
pc->c0 = mgr.createControl(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), Inkscape::CTRL_TYPE_ADJ_HANDLE);
pc->c1 = mgr.createControl(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), Inkscape::CTRL_TYPE_ADJ_HANDLE);
pc->anchor_statusbar = false;
ec->enableSelectionCue();
}
}
{
pc->num_clicks = 0;
}
/**
* Finalization callback.
*/
void SPPenContext::finish() {
SPEventContext* ec = this;
pen_cancel (pc);
}
// if (((SPEventContextClass *) sp_pen_context_parent_class)->finish) {
// ((SPEventContextClass *) sp_pen_context_parent_class)->finish(ec);
// }
SPDrawContext::finish();
}
/**
* Callback that sets key to value in pen context.
*/
SPEventContext* ec = this;
if (name == "mode") {
} else {
}
}
}
/**
* Snaps new node relative to the previous node.
*/
{
}
} 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
// snap constrained
} else {
// snap freely
spdc_endpoint_snap_free(pc, p, origin, state); // pass the origin, to allow for perpendicular / tangential snapping
}
}
}
/**
* Snaps new node's handle relative to the new node.
*/
static void spdc_endpoint_snap_handle(SPPenContext const *const pc, Geom::Point &p, guint const state)
{
} else {
}
}
}
SPEventContext* ec = this;
case GDK_BUTTON_PRESS:
break;
case GDK_BUTTON_RELEASE:
break;
default:
break;
}
if (!ret) {
// if (((SPEventContextClass *) sp_pen_context_parent_class)->item_handler)
// ret = ((SPEventContextClass *) sp_pen_context_parent_class)->item_handler(ec, item, event);
}
return ret;
}
/**
* Callback to handle all pen events.
*/
SPEventContext* ec = this;
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) {
// gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
// = ((SPEventContextClass *) sp_pen_context_parent_class)->root_handler;
// if (parent_root_handler) {
// ret = parent_root_handler(ec, event);
// }
}
return ret;
}
/**
* Handle mouse button press event.
*/
{
if (pc->events_disabled) {
// skip event processing if events are disabled
return FALSE;
}
// make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
return TRUE;
}
// Grab mouse, so release will not pass unnoticed
}
pen_within_tolerance = true;
// Test whether we hit any anchor.
// In click mode we add point on release
case SP_PEN_CONTEXT_POINT:
case SP_PEN_CONTEXT_CONTROL:
case SP_PEN_CONTEXT_CLOSE:
break;
case SP_PEN_CONTEXT_STOP:
// This is allowed, if we just canceled curve
break;
default:
break;
}
break;
case SP_PEN_CONTEXT_MODE_DRAG:
case SP_PEN_CONTEXT_STOP:
// This is allowed, if we just canceled curve
case SP_PEN_CONTEXT_POINT:
p = event_dt;
m.unSetup();
}
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
// 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;
}
} 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
}
break;
} else {
p = event_dt;
spdc_pen_set_subsequent_point(pc, p, true);
}
}
break;
case SP_PEN_CONTEXT_CONTROL:
g_warning("Button down in CONTROL state");
break;
case SP_PEN_CONTEXT_CLOSE:
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 (pc->green_closed) {
// finishing at the start anchor, close curve
} else {
// finishing at some other anchor, finish curve but not close
}
// right click - finish path
}
if (pc->expecting_clicks_for_LPE > 0) {
}
return ret;
}
/**
* Handle motion_notify event.
*/
{
if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
// allow scrolling
return FALSE;
}
if (pc->events_disabled) {
// skip motion events if pen events are disabled
return FALSE;
}
mevent.y);
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
case SP_PEN_CONTEXT_POINT:
// Only set point, if we are already appending
spdc_pen_set_subsequent_point(pc, p, true);
} else if (!sp_event_context_knot_mouseover(pc)) {
m.unSetup();
}
break;
case SP_PEN_CONTEXT_CONTROL:
case SP_PEN_CONTEXT_CLOSE:
// Placing controls is last operation in CLOSE state
break;
case SP_PEN_CONTEXT_STOP:
// This is perfectly valid
break;
default:
break;
}
break;
case SP_PEN_CONTEXT_MODE_DRAG:
case SP_PEN_CONTEXT_POINT:
// Only set point, if we are already appending
if (!anchor) { // Snap node only if not hitting anchor
} else {
}
pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
pc->anchor_statusbar = true;
pc->anchor_statusbar = false;
}
} else {
pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
pc->anchor_statusbar = true;
pc->anchor_statusbar = false;
}
if (!sp_event_context_knot_mouseover(pc)) {
m.unSetup();
}
}
break;
case SP_PEN_CONTEXT_CONTROL:
case SP_PEN_CONTEXT_CLOSE:
// Placing controls is last operation in CLOSE state
// snap the handle
if (!pc->polylines_only) {
} else {
}
break;
case SP_PEN_CONTEXT_STOP:
// This is perfectly valid
break;
default:
if (!sp_event_context_knot_mouseover(pc)) {
m.unSetup();
}
break;
}
break;
default:
break;
}
return ret;
}
/**
* Handle mouse button release event.
*/
{
if (pc->events_disabled) {
// skip event processing if events are disabled
return FALSE;
}
revent.y);
// Find desktop coordinates
// Test whether we hit any anchor.
case SP_PEN_CONTEXT_POINT:
// Start new thread only with button release
if (anchor) {
}
} else {
// Set end anchor here
if (anchor) {
}
}
break;
case SP_PEN_CONTEXT_CONTROL:
// End current segment
break;
case SP_PEN_CONTEXT_CLOSE:
// End current segment
if (!anchor) { // Snap node only if not hitting anchor
}
break;
case SP_PEN_CONTEXT_STOP:
// This is allowed, if we just canceled curve
break;
default:
break;
}
break;
case SP_PEN_CONTEXT_MODE_DRAG:
case SP_PEN_CONTEXT_POINT:
case SP_PEN_CONTEXT_CONTROL:
break;
case SP_PEN_CONTEXT_CLOSE:
if (pc->green_closed) {
// finishing at the start anchor, close curve
} else {
// finishing at some other anchor, finish curve but not close
}
break;
case SP_PEN_CONTEXT_STOP:
// This is allowed, if we just cancelled curve
break;
default:
break;
}
break;
default:
break;
}
// Release grab now
}
}
// 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 (pc->waiting_LPE) {
// we have an already created LPE waiting for a path
} 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;
}
{
// only end on LMB double click. Otherwise horizontal scrolling causes ending of the path
}
return ret;
}
{
// green
if (pc->green_bpaths) {
// remove old piecewise green canvasitems
while (pc->green_bpaths) {
}
// one canvas bpath for all of green_curve
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
}
if (pc->green_anchor)
// handles
} else {
}
if (last_seg) {
if ( cubic &&
{
} else {
}
}
}
{
return;
// green
} else {
// start anchor too
if (pc->green_anchor) {
}
}
// red
}
{
}
{
return;
Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
if ( cubic ) {
} else {
}
}
{
return;
}
{
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__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
break;
case GDK_KEY_Up: // move last point up
case GDK_KEY_KP_Up:
if (!MOD__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
break;
case GDK_KEY_Right: // move last point right
case GDK_KEY_KP_Right:
if (!MOD__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
break;
case GDK_KEY_Down: // move last point down
case GDK_KEY_KP_Down:
if (!MOD__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
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) {
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) {
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) {
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) {
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) {
}
break;
case GDK_KEY_L:
case GDK_KEY_l:
if (MOD__SHIFT_ONLY) {
}
break;
case GDK_KEY_Return:
case GDK_KEY_KP_Enter:
}
break;
case GDK_KEY_Escape:
// if drawing, cancel, otherwise pass it up for deselecting
pen_cancel (pc);
}
break;
case GDK_KEY_z:
case GDK_KEY_Z:
// if drawing, cancel, otherwise pass it up for undo
pen_cancel (pc);
}
break;
case GDK_KEY_g:
case GDK_KEY_G:
if (MOD__SHIFT_ONLY) {
ret = true;
}
break;
case GDK_KEY_BackSpace:
case GDK_KEY_Delete:
case GDK_KEY_KP_Delete:
pen_cancel (pc);
} else {
// do nothing; this event should be handled upstream
}
} else {
// Reset red curve
// Destroy topmost green bpath
if (pc->green_bpaths) {
}
// Get last segment
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 !
} else {
}
: pc->p[3] ));
}
break;
default:
break;
}
return ret;
}
{
// Red
// Blue
// Green
while (pc->green_bpaths) {
}
if (pc->green_anchor) {
}
pc->red_curve_is_valid = false;
}
{
pc->p[0] = p;
pc->p[1] = p;
}
/**
* two parameters ("angle %3.2f°, distance %s").
*/
static void spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, Geom::Point const p, int pc_point_to_compare, gchar const *message)
{
}
static void spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status)
{
// todo: Check callers to see whether 2 <= npoints is guaranteed.
pc->p[2] = p;
pc->p[3] = p;
pc->p[4] = p;
bool is_curve;
// we are drawing horizontal/vertical lines and hit an anchor;
// if the previous point and the anchor are not aligned either horizontally or vertically...
// ...then we should draw an L-shaped path, consisting of two paraxial segments
}
is_curve = false;
} else {
// one of the 'regular' modes
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");
}
}
{
pc->p[1] = p;
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"));
pc->p[4] = p;
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 (pc->polylines_paraxial) {
}
++pc->num_clicks;
/// \todo fixme:
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
}
}
{
// don't let the path be finished before we have collected the required number of mouse clicks
return;
}
pc->num_clicks = 0;
if (pc->green_anchor) {
}
}
pc->events_disabled++;
}
pc->events_disabled--;
}
void sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
unsigned int num_clicks, bool use_polylines)
{
return;
}
{
pc->expecting_clicks_for_LPE = 0;
}
//
// 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) {
// first mouse click
return pen_last_paraxial_dir;
} else {
// subsequent mouse click
}
}
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) {
// 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 :