Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / dyna-draw-context.cpp
index 6925960ea7eccb4e06dc9e8e8f3e727981e1a012..c56ba7a4c197092d932ae979f8108b648530d833 100644 (file)
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 #include <glibmm/i18n.h>
-
+#include <string>
+#include <cstring>
 #include <numeric>
 
 #include "svg/svg.h"
 #include "display/canvas-bpath.h"
 #include "display/bezier-utils.h"
-
+#include "display/curve.h"
 #include <glib/gmem.h>
 #include "macros.h"
 #include "document.h"
 #include "desktop-affine.h"
 #include "desktop-style.h"
 #include "message-context.h"
+#include "prefs-utils.h"
 #include "pixmaps/cursor-calligraphy.xpm"
-#include "pixmaps/cursor-thin.xpm"
-#include "pixmaps/cursor-thicken.xpm"
-#include "libnr/n-art-bpath.h"
-#include "libnr/nr-path.h"
 #include "libnr/nr-matrix-ops.h"
 #include "libnr/nr-scale-translate-ops.h"
+#include "libnr/nr-convert2geom.h"
 #include "xml/repr.h"
 #include "context-fns.h"
 #include "sp-item.h"
 #include "inkscape.h"
 #include "color.h"
 #include "splivarot.h"
+#include "sp-item-group.h"
 #include "sp-shape.h"
 #include "sp-path.h"
 #include "sp-text.h"
 #include "display/canvas-bpath.h"
 #include "display/canvas-arena.h"
 #include "livarot/Shape.h"
-#include "isnan.h"
+#include <2geom/isnan.h>
+#include <2geom/pathvector.h>
 
 #include "dyna-draw-context.h"
 
 
 #define DYNA_MIN_WIDTH 1.0e-6
 
-#define DRAG_MIN 0.0
-#define DRAG_DEFAULT 1.0
-#define DRAG_MAX 1.0
-
-// FIXME: move it to some shared file to be reused by both calligraphy and dropper
-#define C1 0.552
-static NArtBpath const hatch_area_circle[] = {
-    { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
-    { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
-    { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
-    { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
-    { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
-    { NR_END, 0, 0, 0, 0, 0, 0 }
-};
-#undef C1
-
-
 static void sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass);
 static void sp_dyna_draw_context_init(SPDynaDrawContext *ddc);
 static void sp_dyna_draw_context_dispose(GObject *object);
@@ -105,8 +89,8 @@ static gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *even
 
 static void clear_current(SPDynaDrawContext *dc);
 static void set_to_accumulated(SPDynaDrawContext *dc, bool unionize);
-static void add_cap(SPCurve *curve, NR::Point const &pre, NR::Point const &from, NR::Point const &to, NR::Point const &post, double rounding);
-static void accumulate_calligraphic(SPDynaDrawContext *dc);
+static void add_cap(SPCurve *curve, NR::Point const &from, NR::Point const &to, double rounding);
+static bool accumulate_calligraphic(SPDynaDrawContext *dc);
 
 static void fit_and_split(SPDynaDrawContext *ddc, gboolean release);
 
@@ -116,24 +100,25 @@ static NR::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, NR::Point
 static void draw_temporary_box(SPDynaDrawContext *dc);
 
 
-static SPEventContextClass *parent_class;
+static SPEventContextClass *dd_parent_class = 0;
 
-GtkType
-sp_dyna_draw_context_get_type(void)
+GType sp_dyna_draw_context_get_type(void)
 {
     static GType type = 0;
     if (!type) {
         GTypeInfo info = {
             sizeof(SPDynaDrawContextClass),
-            NULL, NULL,
-            (GClassInitFunc) sp_dyna_draw_context_class_init,
-            NULL, NULL,
+            0, // base_init
+            0, // base_finalize
+            (GClassInitFunc)sp_dyna_draw_context_class_init,
+            0, // class_finalize
+            0, // class_data
             sizeof(SPDynaDrawContext),
-            4,
-            (GInstanceInitFunc) sp_dyna_draw_context_init,
-            NULL,   /* value_table */
+            0, // n_preallocs
+            (GInstanceInitFunc)sp_dyna_draw_context_init,
+            0 // value_table
         };
-        type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDynaDrawContext", &info, (GTypeFlags)0);
+        type = g_type_register_static(SP_TYPE_COMMON_CONTEXT, "SPDynaDrawContext", &info, static_cast<GTypeFlags>(0));
     }
     return type;
 }
@@ -144,7 +129,7 @@ sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass)
     GObjectClass *object_class = (GObjectClass *) klass;
     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
 
-    parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
+    dd_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
 
     object_class->dispose = sp_dyna_draw_context_dispose;
 
@@ -156,38 +141,9 @@ sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass)
 static void
 sp_dyna_draw_context_init(SPDynaDrawContext *ddc)
 {
-    SPEventContext *event_context = SP_EVENT_CONTEXT(ddc);
-
-    event_context->cursor_shape = cursor_calligraphy_xpm;
-    event_context->hot_x = 4;
-    event_context->hot_y = 4;
-
-    ddc->accumulated = NULL;
-    ddc->segments = NULL;
-    ddc->currentcurve = NULL;
-    ddc->currentshape = NULL;
-    ddc->npoints = 0;
-    ddc->cal1 = NULL;
-    ddc->cal2 = NULL;
-    ddc->repr = NULL;
-
-    /* DynaDraw values */
-    ddc->cur = NR::Point(0,0);
-    ddc->last = NR::Point(0,0);
-    ddc->vel = NR::Point(0,0);
-    ddc->vel_max = 0;
-    ddc->acc = NR::Point(0,0);
-    ddc->ang = NR::Point(0,0);
-    ddc->del = NR::Point(0,0);
-
-    /* attributes */
-    ddc->dragging = FALSE;
-
-    ddc->mass = 0.3;
-    ddc->drag = DRAG_DEFAULT;
-    ddc->angle = 30.0;
-    ddc->width = 0.2;
-    ddc->pressure = DDC_DEFAULT_PRESSURE;
+    ddc->cursor_shape = cursor_calligraphy_xpm;
+    ddc->hot_x = 4;
+    ddc->hot_y = 4;
 
     ddc->vel_thin = 0.1;
     ddc->flatness = 0.9;
@@ -197,6 +153,7 @@ sp_dyna_draw_context_init(SPDynaDrawContext *ddc)
     ddc->keep_selected = true;
 
     ddc->hatch_spacing = 0;
+    ddc->hatch_spacing_step = 0;
     new (&ddc->hatch_pointer_past) std::list<double>();
     new (&ddc->hatch_nearest_past) std::list<double>();
     ddc->hatch_last_nearest = NR::Point(0,0);
@@ -208,8 +165,6 @@ sp_dyna_draw_context_init(SPDynaDrawContext *ddc)
     ddc->hatch_livarot_path = NULL;
 
     ddc->trace_bg = false;
-
-    ddc->is_dilating = false;
 }
 
 static void
@@ -222,34 +177,8 @@ sp_dyna_draw_context_dispose(GObject *object)
         ddc->hatch_area = NULL;
     }
 
-    if (ddc->dilate_area) {
-        gtk_object_destroy(GTK_OBJECT(ddc->dilate_area));
-        ddc->dilate_area = NULL;
-    }
-
-    if (ddc->accumulated) {
-        ddc->accumulated = sp_curve_unref(ddc->accumulated);
-    }
-
-    while (ddc->segments) {
-        gtk_object_destroy(GTK_OBJECT(ddc->segments->data));
-        ddc->segments = g_slist_remove(ddc->segments, ddc->segments->data);
-    }
-
-    if (ddc->currentcurve) ddc->currentcurve = sp_curve_unref(ddc->currentcurve);
-    if (ddc->cal1) ddc->cal1 = sp_curve_unref(ddc->cal1);
-    if (ddc->cal2) ddc->cal2 = sp_curve_unref(ddc->cal2);
-
-    if (ddc->currentshape) {
-        gtk_object_destroy(GTK_OBJECT(ddc->currentshape));
-        ddc->currentshape = NULL;
-    }
-
-    if (ddc->_message_context) {
-        delete ddc->_message_context;
-    }
 
-    G_OBJECT_CLASS(parent_class)->dispose(object);
+    G_OBJECT_CLASS(dd_parent_class)->dispose(object);
 
     ddc->hatch_pointer_past.~list();
     ddc->hatch_nearest_past.~list();
@@ -260,14 +189,14 @@ sp_dyna_draw_context_setup(SPEventContext *ec)
 {
     SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
 
-    if (((SPEventContextClass *) parent_class)->setup)
-        ((SPEventContextClass *) parent_class)->setup(ec);
+    if (((SPEventContextClass *) dd_parent_class)->setup)
+        ((SPEventContextClass *) dd_parent_class)->setup(ec);
 
-    ddc->accumulated = sp_curve_new_sized(32);
-    ddc->currentcurve = sp_curve_new_sized(4);
+    ddc->accumulated = new SPCurve();
+    ddc->currentcurve = new SPCurve();
 
-    ddc->cal1 = sp_curve_new_sized(32);
-    ddc->cal2 = sp_curve_new_sized(32);
+    ddc->cal1 = new SPCurve();
+    ddc->cal2 = new SPCurve();
 
     ddc->currentshape = sp_canvas_item_new(sp_desktop_sketch(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL);
     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->currentshape), DDC_RED_RGBA, SP_WIND_RULE_EVENODD);
@@ -276,23 +205,22 @@ sp_dyna_draw_context_setup(SPEventContext *ec)
     g_signal_connect(G_OBJECT(ddc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop);
 
     {
-        SPCurve *c = sp_curve_new_from_foreign_bpath(hatch_area_circle);
+        /* TODO: this can be done either with an arcto, and should maybe also be put in a general file (other tools use this as well) */
+        SPCurve *c = new SPCurve();
+        const double C1 = 0.552;
+        c->moveto(-1,0);
+        c->curveto(-1, C1, -C1, 1, 0, 1 );
+        c->curveto(C1, 1, 1, C1, 1, 0 );
+        c->curveto(1, -C1, C1, -1, 0, -1 );
+        c->curveto(-C1, -1, -1, -C1, -1, 0 );
+        c->closepath();
         ddc->hatch_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
-        sp_curve_unref(c);
+        c->unref();
         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->hatch_area), 0x00000000,(SPWindRule)0);
         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->hatch_area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
         sp_canvas_item_hide(ddc->hatch_area);
     }
 
-    {
-        SPCurve *c = sp_curve_new_from_foreign_bpath(hatch_area_circle);
-        ddc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
-        sp_curve_unref(c);
-        sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->dilate_area), 0x00000000,(SPWindRule)0);
-        sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
-        sp_canvas_item_hide(ddc->dilate_area);
-    }
-
     sp_event_context_read(ec, "mass");
     sp_event_context_read(ec, "wiggle");
     sp_event_context_read(ec, "angle");
@@ -310,6 +238,10 @@ sp_dyna_draw_context_setup(SPEventContext *ec)
     ddc->is_drawing = false;
 
     ddc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
+
+    if (prefs_get_int_attribute("tools.calligraphic", "selcue", 0) != 0) {
+        ec->enableSelectionCue();
+    }
 }
 
 static void
@@ -317,39 +249,15 @@ sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
 {
     SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
 
-    if (!strcmp(key, "mass")) {
-        double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.2 );
-        ddc->mass = CLAMP(dval, -1000.0, 1000.0);
-    } else if (!strcmp(key, "wiggle")) {
-        double const dval = ( val ? g_ascii_strtod (val, NULL) : (1 - DRAG_DEFAULT));
-        ddc->drag = CLAMP((1 - dval), DRAG_MIN, DRAG_MAX); // drag is inverse to wiggle
-    } else if (!strcmp(key, "angle")) {
-        double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0);
-        ddc->angle = CLAMP (dval, -90, 90);
-    } else if (!strcmp(key, "width")) {
-        double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
-        ddc->width = CLAMP(dval, -1000.0, 1000.0);
-    } else if (!strcmp(key, "thinning")) {
-        double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
-        ddc->vel_thin = CLAMP(dval, -1.0, 1.0);
-    } else if (!strcmp(key, "tremor")) {
-        double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
-        ddc->tremor = CLAMP(dval, 0.0, 1.0);
-    } else if (!strcmp(key, "flatness")) {
-        double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
-        ddc->flatness = CLAMP(dval, 0, 1.0);
-    } else if (!strcmp(key, "tracebackground")) {
+    if (!strcmp(key, "tracebackground")) {
         ddc->trace_bg = (val && strcmp(val, "0"));
-    } else if (!strcmp(key, "usepressure")) {
-        ddc->usepressure = (val && strcmp(val, "0"));
-    } else if (!strcmp(key, "usetilt")) {
-        ddc->usetilt = (val && strcmp(val, "0"));
-    } else if (!strcmp(key, "abs_width")) {
-        ddc->abs_width = (val && strcmp(val, "0"));
     } else if (!strcmp(key, "keep_selected")) {
         ddc->keep_selected = (val && strcmp(val, "0"));
-    } else if (!strcmp(key, "cap_rounding")) {
-        ddc->cap_rounding = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
+    } else {
+        //pass on up to parent class to handle common attributes.
+        if ( dd_parent_class->set ) {
+            dd_parent_class->set(ec, key, val);
+        }
     }
 
     //g_print("DDC: %g %g %g %g\n", ddc->mass, ddc->drag, ddc->angle, ddc->width);
@@ -589,145 +497,6 @@ sp_dyna_draw_brush(SPDynaDrawContext *dc)
     dc->npoints++;
 }
 
-double
-get_dilate_radius (SPDynaDrawContext *dc)
-{
-    // 10 times the pen width:
-    return 500 * dc->width/SP_EVENT_CONTEXT(dc)->desktop->current_zoom(); 
-}
-
-double
-get_dilate_force (SPDynaDrawContext *dc)
-{
-    return 8 * dc->pressure/SP_EVENT_CONTEXT(dc)->desktop->current_zoom(); 
-}
-
-bool
-sp_ddc_dilate (SPDynaDrawContext *dc, NR::Point p, bool expand)
-{
-    Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(dc)->desktop);
-
-    if (selection->isEmpty()) {
-        return false;
-    }
-
-    bool did = false;
-    double radius = get_dilate_radius(dc); 
-    double offset = get_dilate_force(dc); 
-
-    for (GSList *items = g_slist_copy((GSList *) selection->itemList());
-         items != NULL;
-         items = items->next) {
-
-        SPItem *item = (SPItem *) items->data;
-
-        // only paths
-        if (!SP_IS_PATH(item))
-            continue;
-
-        SPCurve *curve = NULL;
-        curve = sp_shape_get_curve(SP_SHAPE(item));
-        if (curve == NULL)
-            continue;
-
-        // skip those paths whose bboxes are entirely out of reach with our radius
-        NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item));
-        if (bbox) {
-            bbox->growBy(radius);
-            if (!bbox->contains(p)) {
-                continue;
-            }
-        }
-
-        Path *orig = Path_for_item(item, false);
-        if (orig == NULL) {
-            sp_curve_unref(curve);
-            continue;
-        }
-        Path *res = new Path;
-        res->SetBackData(false);
-
-        Shape *theShape = new Shape;
-        Shape *theRes = new Shape;
-
-        orig->ConvertWithBackData(0.05);
-        orig->Fill(theShape, 0);
-
-        SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
-        gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
-        if (val && strcmp(val, "nonzero") == 0)
-        {
-            theRes->ConvertToShape(theShape, fill_nonZero);
-        }
-        else if (val && strcmp(val, "evenodd") == 0)
-        {
-            theRes->ConvertToShape(theShape, fill_oddEven);
-        }
-        else
-        {
-            theRes->ConvertToShape(theShape, fill_nonZero);
-        }
-
-        bool did_this = false;
-        NR::Matrix i2doc(sp_item_i2doc_affine(item));
-        if (theShape->MakeOffset(theRes, 
-                                 expand? offset : -offset,
-                                 join_straight, butt_straight,
-                                 true, p[NR::X], p[NR::Y], radius, &i2doc) == 0) // 0 means the shape was actually changed
-            did_this = true;
-
-        // the rest only makes sense if we actually changed the path
-        if (did_this) {
-            theRes->ConvertToShape(theShape, fill_positive);
-
-            res->Reset();
-            theRes->ConvertToForme(res);
-
-            if (offset >= 0.5)
-            {
-                res->ConvertEvenLines(0.5);
-                res->Simplify(0.5);
-            }
-            else
-            {
-                res->ConvertEvenLines(0.5*offset);
-                res->Simplify(0.5 * offset);
-            }
-
-            sp_curve_unref(curve);
-            if (res->descr_cmd.size() > 1) { 
-                gchar *str = res->svg_dump_path();
-                SP_OBJECT_REPR(item)->setAttribute("d", str);
-                g_free(str);
-            } else {
-                // TODO: if there's 0 or 1 node left, delete this path altogether
-            }
-        }
-
-        delete theShape;
-        delete theRes;
-        delete orig;
-        delete res;
-
-        if (did_this) 
-            did = true;
-    }
-
-    return did;
-}
-
-void
-sp_ddc_update_cursors (SPDynaDrawContext *dc)
-{
-    if (dc->is_dilating) {
-        double radius = get_dilate_radius(dc);
-        NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(dc)->desktop->point()));
-        sp_canvas_item_affine_absolute(dc->dilate_area, sm);
-        sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
-        sp_canvas_item_show(dc->dilate_area);
-    }
-}
-
 void
 sp_ddc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
 {
@@ -747,7 +516,7 @@ calligraphic_cancel(SPDynaDrawContext *dc)
                 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
             }
             /* reset accumulated curve */
-            sp_curve_reset(dc->accumulated);
+            dc->accumulated->reset();
             clear_current(dc);
             if (dc->repr) {
                 dc->repr = NULL;
@@ -766,7 +535,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
     switch (event->type) {
         case GDK_BUTTON_PRESS:
-            if ( event->button.button == 1 ) {
+            if (event->button.button == 1 && !event_context->space_panning) {
 
                 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
 
@@ -780,7 +549,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                 sp_dyna_draw_reset(dc, button_dt);
                 sp_dyna_draw_extinput(dc, event);
                 sp_dyna_draw_apply(dc, button_dt);
-                sp_curve_reset(dc->accumulated);
+                dc->accumulated->reset();
                 if (dc->repr) {
                     dc->repr = NULL;
                 }
@@ -798,6 +567,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
                 ret = TRUE;
 
+                sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
                 dc->is_drawing = true;
             }
             break;
@@ -806,42 +576,9 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
             NR::Point const motion_w(event->motion.x,
                                      event->motion.y);
             NR::Point motion_dt(desktop->w2d(motion_w));
+            sp_dyna_draw_extinput(dc, event);
 
-            // draw the dilating cursor
-            if (event->motion.state & GDK_MOD1_MASK) {
-                double radius = get_dilate_radius(dc);
-                NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
-                sp_canvas_item_affine_absolute(dc->dilate_area, sm);
-                sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
-                sp_canvas_item_show(dc->dilate_area);
-
-                guint num = 0;
-                if (!desktop->selection->isEmpty()) {
-                    num = g_slist_length((GSList *) desktop->selection->itemList());
-                }
-                if (num == 0) {
-                    dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Select paths</b> to thin or thicken"));
-                } else {
-                    dc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
-                                           event->motion.state & GDK_SHIFT_MASK?
-                                           _("<b>Thickening %d</b> selected paths; without <b>Shift</b> to thin") :
-                                           _("<b>Thinning %d</b> selected paths; with <b>Shift</b> to thicken"), num);
-                }
-
-            } else {
-                dc->_message_context->clear();
-                sp_canvas_item_hide(dc->dilate_area);
-            }
-
-            // dilating:
-            if (dc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK ) && event->motion.state & GDK_MOD1_MASK) {  
-                sp_ddc_dilate (dc, desktop->dt2doc(motion_dt), event->motion.state & GDK_SHIFT_MASK? true : false);
-                dc->is_dilating = true;
-                // it's slow, so prevent clogging up with events
-                gobble_motion_events(GDK_BUTTON1_MASK);
-                return TRUE;
-            }
-
+            dc->_message_context->clear();
 
             // for hatching:
             double hatch_dist = 0;
@@ -870,7 +607,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                     pointer = motion_dt * motion_to_curve;
 
                     // calculate the nearest point on the guide path
-                    NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
+                    boost::optional<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
                     nearest = get_point_on_Path(dc->hatch_livarot_path, position->piece, position->t);
 
 
@@ -885,7 +622,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                 }
             } 
 
-            if ( dc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK ) ) {
+            if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
                 dc->dragging = TRUE;
 
                 if (event->motion.state & GDK_CONTROL_MASK && dc->hatch_item) { // hatching
@@ -961,7 +698,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                                 // so _gradually_ let go attraction to prevent jerks
                                 target = (dc->hatch_spacing * speed + hatch_dist * (SPEED_NORMAL - speed))/SPEED_NORMAL;                            
                             }
-                            if (!isNaN(dot) && dot < -0.5) {// flip
+                            if (!IS_NAN(dot) && dot < -0.5) {// flip
                                 target = -target;
                             }
 
@@ -992,7 +729,6 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                     dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> a calligraphic stroke"));
                 }
 
-                sp_dyna_draw_extinput(dc, event);
                 if (!sp_dyna_draw_apply(dc, motion_dt)) {
                     ret = TRUE;
                     break;
@@ -1033,7 +769,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                 } else {
                     // Not drawing but spacing set: gray, center snapped, fixed radius
                     NR::Point c = (nearest + dc->hatch_spacing * hatch_unit_vector) * motion_to_curve.inverse();
-                    if (!isNaN(c[NR::X]) && !isNaN(c[NR::Y])) {
+                    if (!IS_NAN(c[NR::X]) && !IS_NAN(c[NR::Y])) {
                         NR::Matrix const sm (NR::scale(dc->hatch_spacing, dc->hatch_spacing) * NR::translate(c));
                         sp_canvas_item_affine_absolute(dc->hatch_area, sm);
                         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
@@ -1048,20 +784,17 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
 
     case GDK_BUTTON_RELEASE:
+    {
+        NR::Point const motion_w(event->button.x, event->button.y);
+        NR::Point const motion_dt(desktop->w2d(motion_w));
+
         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
+        sp_canvas_end_forced_full_redraws(desktop->canvas);
         dc->is_drawing = false;
 
-        if ( dc->is_dilating && event->button.button == 1 ) {
-            dc->is_dilating = false;
-            sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(dc)->desktop), 
-                         SP_VERB_CONTEXT_CALLIGRAPHIC,
-                         (event->button.state & GDK_SHIFT_MASK ? _("Thicken paths") : _("Thin paths")));
-            ret = TRUE;
-        } else if ( dc->dragging && event->button.button == 1 ) {
+        if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
             dc->dragging = FALSE;
 
-            NR::Point const motion_w(event->button.x, event->button.y);
-            NR::Point const motion_dt(desktop->w2d(motion_w));
             sp_dyna_draw_apply(dc, motion_dt);
 
             /* Remove all temporary line segments */
@@ -1072,11 +805,13 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
             /* Create object */
             fit_and_split(dc, TRUE);
-            accumulate_calligraphic(dc);
-            set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
+            if (accumulate_calligraphic(dc))
+                set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
+            else
+                g_warning ("Failed to create path: invalid data in dc->cal1 or dc->cal2");
 
             /* reset accumulated curve */
-            sp_curve_reset(dc->accumulated);
+            dc->accumulated->reset();
 
             clear_current(dc);
             if (dc->repr) {
@@ -1092,10 +827,19 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
             dc->hatch_item = NULL;
             dc->hatch_livarot_path = NULL;
 
+            if (dc->hatch_spacing != 0 && !dc->keep_selected) { 
+                // we do not select the newly drawn path, so increase spacing by step
+                if (dc->hatch_spacing_step == 0) {
+                    dc->hatch_spacing_step = dc->hatch_spacing;
+                }
+                dc->hatch_spacing += dc->hatch_spacing_step;
+            }
+
             dc->_message_context->clear();
             ret = TRUE;
         }
         break;
+    }
 
     case GDK_KEY_PRESS:
         switch (get_group0_keyval (&event->key)) {
@@ -1126,7 +870,6 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                 if (dc->width > 1.0)
                     dc->width = 1.0;
                 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100); // the same spinbutton is for alt+x
-                sp_ddc_update_cursors(dc);
                 ret = TRUE;
             }
             break;
@@ -1137,7 +880,6 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                 if (dc->width < 0.01)
                     dc->width = 0.01;
                 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
-                sp_ddc_update_cursors(dc);
                 ret = TRUE;
             }
             break;
@@ -1145,14 +887,12 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
         case GDK_KP_Home:
             dc->width = 0.01;
             sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
-            sp_ddc_update_cursors(dc);
             ret = TRUE;
             break;
         case GDK_End:
         case GDK_KP_End:
             dc->width = 1.0;
             sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
-            sp_ddc_update_cursors(dc);
             ret = TRUE;
             break;
         case GDK_x:
@@ -1177,28 +917,6 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                 ret = TRUE;
             }
             break;
-        case GDK_Meta_L:
-        case GDK_Meta_R:
-            event_context->cursor_shape = cursor_thicken_xpm;
-            sp_event_context_update_cursor(event_context);
-            break;
-        case GDK_Alt_L:
-        case GDK_Alt_R:
-            if (MOD__SHIFT) {
-                event_context->cursor_shape = cursor_thicken_xpm;
-                sp_event_context_update_cursor(event_context);
-            } else {
-                event_context->cursor_shape = cursor_thin_xpm;
-                sp_event_context_update_cursor(event_context);
-            }
-            break;
-        case GDK_Shift_L:
-        case GDK_Shift_R:
-            if (MOD__ALT) {
-                event_context->cursor_shape = cursor_thicken_xpm;
-                sp_event_context_update_cursor(event_context);
-            }
-            break;
         default:
             break;
         }
@@ -1210,24 +928,8 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
             case GDK_Control_R:
                 dc->_message_context->clear();
                 dc->hatch_spacing = 0;
+                dc->hatch_spacing_step = 0;
                 break;
-            case GDK_Alt_L:
-            case GDK_Alt_R:
-                event_context->cursor_shape = cursor_calligraphy_xpm;
-                sp_event_context_update_cursor(event_context);
-                break;
-            case GDK_Shift_L:
-            case GDK_Shift_R:
-                if (MOD__ALT) {
-                    event_context->cursor_shape = cursor_thin_xpm;
-                    sp_event_context_update_cursor(event_context);
-                }
-                break;
-            case GDK_Meta_L:
-            case GDK_Meta_R:
-                event_context->cursor_shape = cursor_calligraphy_xpm;
-                sp_event_context_update_cursor(event_context);
-            break;
             default:
                 break;
         }
@@ -1237,8 +939,8 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
     }
 
     if (!ret) {
-        if (((SPEventContextClass *) parent_class)->root_handler) {
-            ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
+        if (((SPEventContextClass *) dd_parent_class)->root_handler) {
+            ret = ((SPEventContextClass *) dd_parent_class)->root_handler(event_context, event);
         }
     }
 
@@ -1252,9 +954,9 @@ clear_current(SPDynaDrawContext *dc)
     /* reset bpath */
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), NULL);
     /* reset curve */
-    sp_curve_reset(dc->currentcurve);
-    sp_curve_reset(dc->cal1);
-    sp_curve_reset(dc->cal2);
+    dc->currentcurve->reset();
+    dc->cal1->reset();
+    dc->cal2->reset();
     /* reset points */
     dc->npoints = 0;
 }
@@ -1264,10 +966,7 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
 {
     SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
 
-    if (!sp_curve_empty(dc->accumulated)) {
-        NArtBpath *abp;
-        gchar *str;
-
+    if (!dc->accumulated->is_empty()) {
         if (!dc->repr) {
             /* Create object */
             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
@@ -1283,10 +982,9 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
             item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
             item->updateRepr();
         }
-        abp = nr_artpath_affine(sp_curve_first_bpath(dc->accumulated), sp_desktop_dt2root_affine(desktop));
-        str = sp_svg_write_path(abp);
+        Geom::PathVector pathv = dc->accumulated->get_pathvector() * sp_desktop_dt2root_affine(desktop);
+        gchar *str = sp_svg_write_path(pathv);
         g_assert( str != NULL );
-        g_free(abp);
         dc->repr->setAttribute("d", str);
         g_free(str);
 
@@ -1296,9 +994,7 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
         } else {
             if (dc->keep_selected) {
                 sp_desktop_selection(desktop)->set(dc->repr);
-            } else {
-                sp_desktop_selection(desktop)->clear();
-            }
+            } 
         }
 
     } else {
@@ -1314,65 +1010,79 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
 
 static void
 add_cap(SPCurve *curve,
-        NR::Point const &pre, NR::Point const &from,
-        NR::Point const &to, NR::Point const &post,
+        NR::Point const &from,
+        NR::Point const &to, 
         double rounding)
 {
-    NR::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
-    double mag = NR::L2(vel);
+    if (NR::L2( to - from ) > DYNA_EPSILON) {
+        NR::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
+        double mag = NR::L2(vel);
 
-    NR::Point v_in = from - pre;
-    double mag_in = NR::L2(v_in);
-    if ( mag_in > DYNA_EPSILON ) {
-        v_in = mag * v_in / mag_in;
-    } else {
-        v_in = NR::Point(0, 0);
-    }
-
-    NR::Point v_out = to - post;
-    double mag_out = NR::L2(v_out);
-    if ( mag_out > DYNA_EPSILON ) {
-        v_out = mag * v_out / mag_out;
-    } else {
-        v_out = NR::Point(0, 0);
-    }
-
-    if ( NR::L2(v_in) > DYNA_EPSILON || NR::L2(v_out) > DYNA_EPSILON ) {
-        sp_curve_curveto(curve, from + v_in, to + v_out, to);
+        NR::Point v = mag * NR::rot90( to - from ) / NR::L2( to - from );
+        curve->curveto(from + v, to + v, to);
     }
 }
 
-static void
+static bool
 accumulate_calligraphic(SPDynaDrawContext *dc)
 {
-    if ( !sp_curve_empty(dc->cal1) && !sp_curve_empty(dc->cal2) ) {
-        sp_curve_reset(dc->accumulated); /*  Is this required ?? */
-        SPCurve *rev_cal2 = sp_curve_reverse(dc->cal2);
+        if (
+            dc->cal1->is_empty() ||
+            dc->cal2->is_empty() ||
+            (dc->cal1->get_segment_count() <= 0) ||
+            dc->cal1->first_path()->closed() 
+            ) {
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
 
-        g_assert(dc->cal1->end > 1);
-        g_assert(rev_cal2->end > 1);
-        g_assert(SP_CURVE_SEGMENT(dc->cal1, 0)->code == NR_MOVETO_OPEN);
-        g_assert(SP_CURVE_SEGMENT(rev_cal2, 0)->code == NR_MOVETO_OPEN);
-        g_assert(SP_CURVE_SEGMENT(dc->cal1, 1)->code == NR_CURVETO);
-        g_assert(SP_CURVE_SEGMENT(rev_cal2, 1)->code == NR_CURVETO);
-        g_assert(SP_CURVE_SEGMENT(dc->cal1, dc->cal1->end-1)->code == NR_CURVETO);
-        g_assert(SP_CURVE_SEGMENT(rev_cal2, rev_cal2->end-1)->code == NR_CURVETO);
+        SPCurve *rev_cal2 = dc->cal2->create_reverse();
+        if (
+            (rev_cal2->get_segment_count() <= 0) ||
+            rev_cal2->first_path()->closed() 
+            ) {
+            rev_cal2->unref();
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
+
+        Geom::CubicBezier const * dc_cal1_firstseg  = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->first_segment() );
+        Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->first_segment() );
+        Geom::CubicBezier const * dc_cal1_lastseg   = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->last_segment() );
+        Geom::CubicBezier const * rev_cal2_lastseg  = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->last_segment() );
+
+        if (
+            !dc_cal1_firstseg ||
+            !rev_cal2_firstseg ||
+            !dc_cal1_lastseg ||
+            !rev_cal2_lastseg 
+            ) {
+            rev_cal2->unref();
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
 
-        sp_curve_append(dc->accumulated, dc->cal1, FALSE);
+        dc->accumulated->reset(); /*  Is this required ?? */
 
-        add_cap(dc->accumulated, SP_CURVE_SEGMENT(dc->cal1, dc->cal1->end-1)->c(2), SP_CURVE_SEGMENT(dc->cal1, dc->cal1->end-1)->c(3), SP_CURVE_SEGMENT(rev_cal2, 0)->c(3), SP_CURVE_SEGMENT(rev_cal2, 1)->c(1), dc->cap_rounding);
+        dc->accumulated->append(dc->cal1, false);
 
-        sp_curve_append(dc->accumulated, rev_cal2, TRUE);
+        add_cap(dc->accumulated, (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[0], dc->cap_rounding);
 
-        add_cap(dc->accumulated, SP_CURVE_SEGMENT(rev_cal2, rev_cal2->end-1)->c(2), SP_CURVE_SEGMENT(rev_cal2, rev_cal2->end-1)->c(3), SP_CURVE_SEGMENT(dc->cal1, 0)->c(3), SP_CURVE_SEGMENT(dc->cal1, 1)->c(1), dc->cap_rounding);
+        dc->accumulated->append(rev_cal2, true);
 
-        sp_curve_closepath(dc->accumulated);
+        add_cap(dc->accumulated, (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[0], dc->cap_rounding);
 
-        sp_curve_unref(rev_cal2);
+        dc->accumulated->closepath();
 
-        sp_curve_reset(dc->cal1);
-        sp_curve_reset(dc->cal2);
-    }
+        rev_cal2->unref();
+
+        dc->cal1->reset();
+        dc->cal2->reset();
+
+        return true; // success
 }
 
 static double square(double const x)
@@ -1403,14 +1113,14 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
 #endif
 
         /* Current calligraphic */
-        if ( dc->cal1->end == 0 || dc->cal2->end == 0 ) {
+        if ( dc->cal1->is_empty() || dc->cal2->is_empty() ) {
             /* dc->npoints > 0 */
             /* g_print("calligraphics(1|2) reset\n"); */
-            sp_curve_reset(dc->cal1);
-            sp_curve_reset(dc->cal2);
+            dc->cal1->reset();
+            dc->cal2->reset();
 
-            sp_curve_moveto(dc->cal1, dc->point1[0]);
-            sp_curve_moveto(dc->cal2, dc->point2[0]);
+            dc->cal1->moveto(dc->point1[0]);
+            dc->cal2->moveto(dc->point2[0]);
         }
 
         NR::Point b1[BEZIER_MAX_LENGTH];
@@ -1430,31 +1140,29 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
 #endif
             /* CanvasShape */
             if (! release) {
-                sp_curve_reset(dc->currentcurve);
-                sp_curve_moveto(dc->currentcurve, b1[0]);
+                dc->currentcurve->reset();
+                dc->currentcurve->moveto(b1[0]);
                 for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
-                    sp_curve_curveto(dc->currentcurve, bp1[1],
-                                     bp1[2], bp1[3]);
+                    dc->currentcurve->curveto(bp1[1], bp1[2], bp1[3]);
                 }
-                sp_curve_lineto(dc->currentcurve,
-                                b2[BEZIER_SIZE*(nb2-1) + 3]);
+                dc->currentcurve->lineto(b2[BEZIER_SIZE*(nb2-1) + 3]);
                 for (NR::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
-                    sp_curve_curveto(dc->currentcurve, bp2[2], bp2[1], bp2[0]);
+                    dc->currentcurve->curveto(bp2[2], bp2[1], bp2[0]);
                 }
                 // FIXME: dc->segments is always NULL at this point??
                 if (!dc->segments) { // first segment
-                    add_cap(dc->currentcurve, b2[1], b2[0], b1[0], b1[1], dc->cap_rounding);
+                    add_cap(dc->currentcurve, b2[0], b1[0], dc->cap_rounding);
                 }
-                sp_curve_closepath(dc->currentcurve);
+                dc->currentcurve->closepath();
                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
             }
 
             /* Current calligraphic */
             for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
-                sp_curve_curveto(dc->cal1, bp1[1], bp1[2], bp1[3]);
+                dc->cal1->curveto(bp1[1], bp1[2], bp1[3]);
             }
             for (NR::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) {
-                sp_curve_curveto(dc->cal2, bp2[1], bp2[2], bp2[3]);
+                dc->cal2->curveto(bp2[1], bp2[2], bp2[3]);
             }
         } else {
             /* fixme: ??? */
@@ -1464,10 +1172,10 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
             draw_temporary_box(dc);
 
             for (gint i = 1; i < dc->npoints; i++) {
-                sp_curve_lineto(dc->cal1, dc->point1[i]);
+                dc->cal1->lineto(dc->point1[i]);
             }
             for (gint i = 1; i < dc->npoints; i++) {
-                sp_curve_lineto(dc->cal2, dc->point2[i]);
+                dc->cal2->lineto(dc->point2[i]);
             }
         }
 
@@ -1476,14 +1184,14 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
         g_print("[%d]Yup\n", dc->npoints);
 #endif
         if (!release) {
-            g_assert(!sp_curve_empty(dc->currentcurve));
+            g_assert(!dc->currentcurve->is_empty());
 
             SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(SP_EVENT_CONTEXT(dc)->desktop),
                                                    SP_TYPE_CANVAS_BPATH,
                                                    NULL);
-            SPCurve *curve = sp_curve_copy(dc->currentcurve);
+            SPCurve *curve = dc->currentcurve->copy();
             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve);
-            sp_curve_unref(curve);
+            curve->unref();
 
             guint32 fillColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", true);
             //guint32 strokeColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", false);
@@ -1511,20 +1219,21 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
 static void
 draw_temporary_box(SPDynaDrawContext *dc)
 {
-    sp_curve_reset(dc->currentcurve);
+    dc->currentcurve->reset();
 
-    sp_curve_moveto(dc->currentcurve, dc->point1[dc->npoints-1]);
+    dc->currentcurve->moveto(dc->point2[dc->npoints-1]);
     for (gint i = dc->npoints-2; i >= 0; i--) {
-        sp_curve_lineto(dc->currentcurve, dc->point1[i]);
+        dc->currentcurve->lineto(dc->point2[i]);
     }
     for (gint i = 0; i < dc->npoints; i++) {
-        sp_curve_lineto(dc->currentcurve, dc->point2[i]);
+        dc->currentcurve->lineto(dc->point1[i]);
     }
+
     if (dc->npoints >= 2) {
-        add_cap(dc->currentcurve, dc->point2[dc->npoints-2], dc->point2[dc->npoints-1], dc->point1[dc->npoints-1], dc->point1[dc->npoints-2], dc->cap_rounding);
+        add_cap(dc->currentcurve, dc->point1[dc->npoints-1], dc->point2[dc->npoints-1], dc->cap_rounding);
     }
 
-    sp_curve_closepath(dc->currentcurve);
+    dc->currentcurve->closepath();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
 }