Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / draw-context.cpp
index 7fdda437c391263de727bd0d453dfd891d2ce7b8..a3c3601cc8ccd98f35740bd8a8ae89fdfbd05a8e 100644 (file)
@@ -26,7 +26,6 @@
 #include <glibmm/i18n.h>
 #include "display/curve.h"
 #include "desktop.h"
-#include "desktop-affine.h"
 #include "desktop-handles.h"
 #include "desktop-style.h"
 #include "document.h"
@@ -278,7 +277,7 @@ spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *
 
     // TODO: Don't paste path if nothing is on the clipboard
 
-    Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item);
+    Effect::createAndApply(PATTERN_ALONG_PATH, dc->desktop->doc(), item);
     Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
     gchar *svgd = sp_svg_write_path(c->get_pathvector());
     static_cast<LPEPatternAlongPath*>(lpe)->pattern.paste_param_path(svgd);
@@ -304,7 +303,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
         SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
         const char *cstroke = sp_repr_css_property(css_item, "stroke", "none");
 
-#define SHAPE_LENGTH 100
+#define SHAPE_LENGTH 10
 #define SHAPE_HEIGHT 10
 
         switch (shape) {
@@ -360,7 +359,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
             case 4:
             {
                 // take shape from clipboard; TODO: catch the case where clipboard is empty
-                Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
+                Effect::createAndApply(PATTERN_ALONG_PATH, dc->desktop->doc(), item);
                 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
                 static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
 
@@ -373,7 +372,12 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
         if (shape_applied) {
             // apply original stroke color as fill and unset stroke; then return
             SPCSSAttr *css = sp_repr_css_attr_new();
-            sp_repr_css_set_property (css, "fill", cstroke);
+
+            if (!strcmp(cstroke, "none")){
+                sp_repr_css_set_property (css, "fill", "black");
+            } else {
+                sp_repr_css_set_property (css, "fill", cstroke);
+            }
             sp_repr_css_set_property (css, "stroke", "none");
             sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
             sp_repr_css_attr_unref(css);
@@ -386,7 +390,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
 
             if (SP_IS_LPETOOL_CONTEXT(dc)) {
                 // since a geometric LPE was applied, we switch back to "inactive" mode
-                lpetool_context_switch_mode(SP_LPETOOL_CONTEXT(dc), Inkscape::LivePathEffect::INVALID_LPE);
+                lpetool_context_switch_mode(SP_LPETOOL_CONTEXT(dc), INVALID_LPE);
             }
         }
         if (SP_IS_PEN_CONTEXT(dc)) {
@@ -434,7 +438,7 @@ spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
         /* Curve list */
         /* We keep it in desktop coordinates to eliminate calculation errors */
         SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
-        norm->transform(sp_item_i2d_affine(dc->white_item));
+        norm->transform((dc->white_item)->i2d_affine());
         g_return_if_fail( norm != NULL );
         dc->white_curves = g_slist_reverse(norm->split());
         norm->unref();
@@ -502,14 +506,12 @@ void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p,
         p = o + bdot * best;
 
         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
-                                         //After all, the user explicitely asked for angular snapping by
+                                         //After all, the user explicitly asked for angular snapping by
                                          //pressing CTRL
             /* Snap it along best vector */
             SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
             m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
-            Geom::Point pt2g = to_2geom(p);
-            m.constrainedSnapReturnByRef( Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::Snapper::ConstraintLine(best));
-            p = from_2geom(pt2g);
+            m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE, Inkscape::Snapper::ConstraintLine(best));
         }
     }
 }
@@ -517,11 +519,15 @@ void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p,
 
 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
 {
-    SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
-    m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
-    Geom::Point pt2g = to_2geom(p);
-    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
-    p = from_2geom(pt2g);
+    SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(ec);
+    SnapManager &m = dt->namedview->snap_manager;
+    Inkscape::Selection *selection = sp_desktop_selection (dt);
+
+    // 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.setup(dt, true, selection->singleItem());
+    m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
 }
 
 static SPCurve *
@@ -650,8 +656,8 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
 
     /* Now we have to go back to item coordinates at last */
     c->transform( dc->white_item
-                            ? sp_item_dt2i_affine(dc->white_item)
-                            : to_2geom(sp_desktop_dt2doc_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) );
+                            ? (dc->white_item)->dt2i_affine()
+                            : SP_EVENT_CONTEXT_DESKTOP(dc)->dt2doc() );
 
     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
     SPDocument *doc = sp_desktop_document(desktop);
@@ -688,11 +694,11 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
 
             dc->selection->set(repr);
             Inkscape::GC::release(repr);
-            item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
+            item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
             item->updateRepr();
         }
 
-        sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, 
+        SPDocumentUndo::done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
                          _("Draw path"));
 
         // When quickly drawing several subpaths with Shift, the next subpath may be finished and
@@ -706,7 +712,7 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
     c->unref();
 
     /* Flush pending updates */
-    sp_document_ensure_up_to_date(doc);
+    doc->ensure_up_to_date();
 }
 
 /**
@@ -829,9 +835,9 @@ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char cons
     /* put the circle where the mouse click occurred and set the diameter to the
        current stroke width, multiplied by the amount specified in the preferences */
     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
-    
-    Geom::Matrix const i2d (sp_item_i2d_affine (item));
-    Geom::Point pp = pt * i2d;
+
+    Geom::Matrix const i2d (item->i2d_affine ());
+    Geom::Point pp = pt;
     double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0);
     if (event_state & GDK_MOD1_MASK) {
         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
@@ -850,11 +856,12 @@ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char cons
     sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
     sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
     item->updateRepr();
+    item->set_item_transform(i2d.inverse());
 
     sp_desktop_selection(desktop)->set(item);
 
     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
-    sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
+    SPDocumentUndo::done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
 }
 
 /*