Code

1) Improve the way the distance to the pointer is taken into account when finding...
[inkscape.git] / src / object-snapper.cpp
index 7582a4c171f64292dec1168350c238f3e9622b04..06f24c47fa1129d28623bfbedea13f6710a82116 100644 (file)
@@ -12,7 +12,6 @@
  */
 
 #include "svg/svg.h"
-#include "libnr/nr-path.h"
 #include "libnr/nr-rect-ops.h"
 #include "libnr/nr-point-fns.h"
 #include <2geom/path-intersection.h>
 #include "sp-item.h"
 #include "sp-use.h"
 #include "display/curve.h"
-#include "desktop.h"
 #include "inkscape.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "sp-text.h"
 #include "sp-flowtext.h"
 #include "text-editing.h"
 #include "sp-clippath.h"
 #include "sp-mask.h"
 #include "helper/geom-curves.h"
+#include "desktop.h"
 
 Inkscape::SnapCandidate::SnapCandidate(SPItem* item, bool clip_or_mask, Geom::Matrix additional_affine)
     : item(item), clip_or_mask(clip_or_mask), additional_affine(additional_affine)
@@ -44,10 +43,10 @@ Inkscape::SnapCandidate::~SnapCandidate()
 {    
 }
 
-Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, Geom::Coord const d)
-    : Snapper(nv, d), _snap_to_itemnode(true), _snap_to_itempath(true),
+Inkscape::ObjectSnapper::ObjectSnapper(SnapManager const *sm, Geom::Coord const d)
+    : Snapper(sm, d), _snap_to_itemnode(true), _snap_to_itempath(true),
       _snap_to_bboxnode(true), _snap_to_bboxpath(true), _snap_to_page_border(false),
-      _strict_snapping(true), _include_item_center(false)
+      _strict_snapping(true)
 {
     _candidates = new std::vector<SnapCandidate>;
     _points_to_snap_to = new std::vector<Geom::Point>;
@@ -90,8 +89,6 @@ void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
         return;        
     }
     
-    SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
-
     if (first_point) {
         _candidates->clear();
     }
@@ -100,7 +97,8 @@ void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
     bbox_to_snap_incl.expandBy(getSnapperTolerance()); // see?
     
     for (SPObject* o = sp_object_first_child(parent); o != NULL; o = SP_OBJECT_NEXT(o)) {
-        if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(desktop->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) {
+        g_assert(_snapmanager->getDesktop() != NULL);
+        if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(_snapmanager->getDesktop()->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) {
             // Don't snap to locked items, and
             // don't snap to hidden objects, unless they're a clipped path or a mask
             /* See if this item is on the ignore list */
@@ -140,20 +138,20 @@ void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
                 if (SP_IS_GROUP(o)) {
                     _findCandidates(o, it, false, bbox_to_snap, snap_dim, false, Geom::identity());
                 } else {
-                    boost::optional<NR::Rect> bbox_of_item = NR::Rect();
+                    Geom::OptRect bbox_of_item = Geom::Rect();
                     if (clip_or_mask) {
                         // Oh oh, this will get ugly. We cannot use sp_item_i2d_affine directly because we need to
                         // insert an additional transformation in document coordinates (code copied from sp_item_i2d_affine)
                         sp_item_invoke_bbox(item, 
-                            &bbox_of_item, 
-                            from_2geom(to_2geom(sp_item_i2doc_affine(item)) * matrix_to_desktop(additional_affine, item)),
+                            bbox_of_item,
+                            sp_item_i2doc_affine(item) * matrix_to_desktop(additional_affine, item),
                             true);
                     } else {
-                        sp_item_invoke_bbox(item, &bbox_of_item, sp_item_i2d_affine(item), true);
+                        sp_item_invoke_bbox(item, bbox_of_item, sp_item_i2d_affine(item), true);
                     }
                     if (bbox_of_item) {
                         // See if the item is within range                                    
-                        if (bbox_to_snap_incl.intersects(to_2geom(*bbox_of_item))) {
+                        if (bbox_to_snap_incl.intersects(*bbox_of_item)) {
                             // This item is within snapping range, so record it as a candidate
                             _candidates->push_back(SnapCandidate(item, clip_or_mask, additional_affine));
                         }                        
@@ -165,7 +163,7 @@ void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
 }
 
 
-void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &t,
+void Inkscape::ObjectSnapper::_collectNodes(Inkscape::SnapPreferences::PointType const &t,
                                          bool const &first_point) const
 {
     // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
@@ -177,16 +175,17 @@ void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &
          // Determine the type of bounding box we should snap to
         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
         
-        bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
-        bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
-        bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
+        bool p_is_a_node = t & Inkscape::SnapPreferences::SNAPPOINT_NODE;
+        bool p_is_a_bbox = t & Inkscape::SnapPreferences::SNAPPOINT_BBOX;
+        bool p_is_a_guide = t & Inkscape::SnapPreferences::SNAPPOINT_GUIDE;
         
         // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
-        g_assert(!(p_is_a_node && p_is_a_bbox || p_is_a_bbox && p_is_a_guide || p_is_a_node && p_is_a_guide));        
+        g_assert(!((p_is_a_node && p_is_a_bbox) || (p_is_a_bbox && p_is_a_guide) || (p_is_a_node && p_is_a_guide)));        
         
         if (_snap_to_bboxnode) {
-            int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
-            bbox_type = (prefs_bbox == 0)? 
+            Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+            bool prefs_bbox = prefs->getBool("/tools/bounding_box");
+            bbox_type = !prefs_bbox ? 
                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
         }
         
@@ -206,9 +205,7 @@ void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &
             //Collect all nodes so we can snap to them
             if (_snap_to_itemnode) {
                 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
-                    std::vector<NR::Point> dummy_vctr;
-                    sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(dummy_vctr));
-                    to_2geom(dummy_vctr, *_points_to_snap_to);
+                    sp_item_snappoints(root_item, SnapPointsIter(*_points_to_snap_to), &_snapmanager->snapprefs);
                 }
             }
 
@@ -218,10 +215,10 @@ void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &
                     // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
                     // of the item AND the bbox of the clipping path at the same time
                     if (!(*i).clip_or_mask) {  
-                        boost::optional<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
+                        Geom::OptRect b = sp_item_bbox_desktop(root_item, bbox_type);
                         if (b) {
                             for ( unsigned k = 0 ; k < 4 ; k++ ) {
-                                _points_to_snap_to->push_back(to_2geom(b->corner(k)));
+                                _points_to_snap_to->push_back(b->corner(k));
                             }
                         }
                     }
@@ -232,7 +229,7 @@ void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &
 }
 
 void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
-                                         Inkscape::Snapper::PointType const &t,
+                                         Inkscape::SnapPreferences::PointType const &t,
                                          Geom::Point const &p,
                                          bool const &first_point,
                                          std::vector<Geom::Point> *unselected_nodes) const
@@ -250,8 +247,8 @@ void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
     
     for (std::vector<Geom::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
         Geom::Coord dist = Geom::L2(*k - p);        
-        if (dist < getSnapperTolerance() && dist < s.getDistance()) {
-            s = SnappedPoint(*k, SNAPTARGET_NODE, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
+        if (dist < getSnapperTolerance() && dist < s.getSnapDistance()) {
+            s = SnappedPoint(*k, SNAPTARGET_NODE, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true);
             success = true;
         }
     }
@@ -262,7 +259,7 @@ void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
 }
 
 void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &sc,
-                                         Inkscape::Snapper::PointType const &t,
+                                         Inkscape::SnapPreferences::PointType const &t,
                                          Geom::Point const &p,
                                          Geom::Point const &guide_normal) const
 {
@@ -279,8 +276,8 @@ void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &s
         Geom::Point p_proj = project_on_linesegment(*k, p, p + Geom::rot90(guide_normal));
         Geom::Coord dist = Geom::L2(*k - p_proj); // distance from node to the guide         
         Geom::Coord dist2 = Geom::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
-        if ((dist < tol && dist2 < tol || getSnapperAlwaysSnap()) && dist < s.getDistance()) {
-            s = SnappedPoint(*k, SNAPTARGET_NODE, dist, tol, getSnapperAlwaysSnap());
+        if ((dist < tol && dist2 < tol) || (getSnapperAlwaysSnap() && dist < s.getSnapDistance())) {
+            s = SnappedPoint(*k, SNAPTARGET_NODE, dist, tol, getSnapperAlwaysSnap(), true);
             success = true;
         }
     }
@@ -295,19 +292,7 @@ void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &s
  * Returns index of first NR_END bpath in array.
  */
 
-/* Obsolete
-static unsigned sp_bpath_length(NArtBpath const bpath[])
-{
-    g_return_val_if_fail(bpath != NULL, FALSE);
-    unsigned ret = 0;
-    while ( bpath[ret].code != NR_END ) {
-        ++ret;
-    }
-    ++ret;
-    return ret;
-}*/
-
-void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &t,
+void Inkscape::ObjectSnapper::_collectPaths(Inkscape::SnapPreferences::PointType const &t,
                                          bool const &first_point) const
 {
     // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
@@ -319,11 +304,12 @@ void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &
         // Determine the type of bounding box we should snap to
         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
         
-        bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
+        bool p_is_a_node = t & Inkscape::SnapPreferences::SNAPPOINT_NODE;
         
         if (_snap_to_bboxpath) {
-            int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
-            bbox_type = (prefs_bbox ==0)? 
+            Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+            int prefs_bbox = prefs->getBool("/tools/bounding_box", 0);
+            bbox_type = !prefs_bbox ? 
                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
         }
         
@@ -392,11 +378,12 @@ void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &
                     // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
                     // of the item AND the bbox of the clipping path at the same time
                     if (!(*i).clip_or_mask) { 
-                        NRRect rect;
-                        sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
-                        Geom::Rect const rect2 = to_2geom(*rect.upgrade());
-                        Geom::PathVector *path = _getPathvFromRect(rect2);
-                        _paths_to_snap_to->push_back(path);                        
+                       Geom::OptRect rect;
+                       sp_item_invoke_bbox(root_item, rect, i2doc, TRUE, bbox_type);
+                        if (rect) {
+                               Geom::PathVector *path = _getPathvFromRect(*rect);
+                               _paths_to_snap_to->push_back(path);
+                        }
                     }
                 }
             }
@@ -405,7 +392,7 @@ void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &
 }
     
 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
-                                     Inkscape::Snapper::PointType const &t,
+                                     Inkscape::SnapPreferences::PointType const &t,
                                      Geom::Point const &p,
                                      bool const &first_point,
                                      std::vector<Geom::Point> *unselected_nodes,
@@ -414,22 +401,15 @@ void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
     _collectPaths(t, first_point);
     // Now we can finally do the real snapping, using the paths collected above
     
-    /* FIXME: this seems like a hack.  Perhaps Snappers should be
-    ** in SPDesktop rather than SPNamedView?
-    */
-    // TODO Diederik: shouldn't we just make all snapping code use document
-    // coordinates instead? Then we won't need a pointer to the desktop any longer
-    // At least we should define a clear boundary between those different coordinates,
-    // now this is not well defined
-    
-    SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
-    Geom::Point const p_doc = desktop->dt2doc(p);
+    g_assert(_snapmanager->getDesktop() != NULL);    
+    Geom::Point const p_doc = _snapmanager->getDesktop()->dt2doc(p);
     
     bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
     
     if (first_point) {
-        /* While editing a path in the node tool, findCandidates must ignore that path because 
-         * of the node snapping requirements (i.e. only unselected nodes must be snapable).
+        /* findCandidates() is used for snapping to both paths and nodes. It ignores the path that is
+         * currently being edited, because that path requires special care: when snapping to nodes 
+         * only the unselected nodes of that path should be considered, and these will be passed on separately.
          * This path must not be ignored however when snapping to the paths, so we add it here
          * manually when applicable. 
          * 
@@ -453,24 +433,18 @@ void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
         // std::cout << "Dumping the pathvector: " << svgd << std::endl;        
         
         for(Geom::PathVector::iterator it_pv = (*it_p)->begin(); it_pv != (*it_p)->end(); ++it_pv) {
-            std::vector<double> anp;
-            
             // Find a nearest point for each curve within this path
-            // (path->allNearestPoints() will not do this for us! It was originally 
-            // intended to find for example multiple equidistant solutions)
-            unsigned int num_curves = (*it_pv).size();
-            if ( (*it_pv).closed() ) ++num_curves;
-            for (double t = 0; (t+1) <= double(num_curves); t++) {
-                // Find a nearest point with time value in the range [t, t+1]
-                anp.push_back((*it_pv).nearestPoint(p_doc, t, t+1)); 
-            }
+            // n curves will return n time values with 0 <= t <= 1
+            std::vector<double> anp = (*it_pv).nearestPointPerCurve(p_doc);
             
-            for (std::vector<double>::const_iterator np = anp.begin(); np != anp.end(); np++) {
+            std::vector<double>::const_iterator np = anp.begin();
+            unsigned int index = 0;
+            for (; np != anp.end(); np++, index++) {
+                Geom::Curve const *curve = &((*it_pv).at_index(index));
+                Geom::Point const sp_doc = curve->pointAt(*np);
+               
                 bool c1 = true;
-                bool c2 = true;
-                Geom::Point start_pt = desktop->doc2dt((*it_pv).pointAt(floor(*np))); 
-                Geom::Point end_pt = desktop->doc2dt((*it_pv).pointAt(ceil(*np)));
-                
+                bool c2 = true;                                
                 if (being_edited) {
                     /* If the path is being edited, then we should only snap though to stationary pieces of the path
                      * and not to the pieces that are being dragged around. This way we avoid 
@@ -478,19 +452,24 @@ void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
                      * piece are unselected; if they are then this piece must be stationary 
                      */                    
                     g_assert(unselected_nodes != NULL);
-                    c1 = isUnselectedNode(start_pt, unselected_nodes);
-                    c2 = isUnselectedNode(end_pt, unselected_nodes);
+                    Geom::Point start_pt = _snapmanager->getDesktop()->doc2dt(curve->pointAt(0)); 
+                    Geom::Point end_pt = _snapmanager->getDesktop()->doc2dt(curve->pointAt(1));                                    
+                    c1 = isUnselectedNode(start_pt, unselected_nodes); 
+                    c2 = isUnselectedNode(end_pt, unselected_nodes);  
+                    /* Unfortunately, this might yield false positives for coincident nodes. Inkscape might therefore mistakenly
+                     * snap to path segments that are not stationary. There are at least two possible ways to overcome this:
+                     * - Linking the individual nodes of the SPPath we have here, to the nodes of the NodePath::SubPath class as being
+                     *   used in sp_nodepath_selected_nodes_move. This class has a member variable called "selected". For this the nodes
+                     *   should be in the exact same order for both classes, so we can index them
+                     * - Replacing the SPPath being used here by the the NodePath::SubPath class; but how?
+                     */ 
                 }
                 
-                Geom::Point const sp_doc = (*it_pv).pointAt(*np);
-                Geom::Point const sp_dt = desktop->doc2dt(sp_doc);
-                
+                Geom::Point const sp_dt = _snapmanager->getDesktop()->doc2dt(sp_doc);                
                 if (!being_edited || (c1 && c2)) {
                     Geom::Coord const dist = Geom::distance(sp_doc, p_doc);
                     if (dist < getSnapperTolerance()) {
-                        double t = MIN(*np, (*it_pv).size()); // make sure that t is within bounds;
-                        Geom::Curve const *curve = &((*it_pv).at_index(int(t)));                         
-                        sc.curves.push_back(Inkscape::SnappedCurve(from_2geom(sp_dt), dist, getSnapperTolerance(), getSnapperAlwaysSnap(), curve));   
+                        sc.curves.push_back(Inkscape::SnappedCurve(from_2geom(sp_dt), dist, getSnapperTolerance(), getSnapperAlwaysSnap(), false, curve));   
                     }
                 }
             }        
@@ -519,7 +498,7 @@ bool Inkscape::ObjectSnapper::isUnselectedNode(Geom::Point const &point, std::ve
 }
 
 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
-                                     Inkscape::Snapper::PointType const &t,
+                                     Inkscape::SnapPreferences::PointType const &t,
                                      Geom::Point const &p,
                                      bool const &first_point,
                                      ConstraintLine const &c) const
@@ -529,11 +508,8 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
     
     // Now we can finally do the real snapping, using the paths collected above
     
-    /* FIXME: this seems like a hack.  Perhaps Snappers should be
-    ** in SPDesktop rather than SPNamedView?
-    */
-    SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
-    Geom::Point const p_doc = desktop->dt2doc(p);    
+    g_assert(_snapmanager->getDesktop() != NULL);
+    Geom::Point const p_doc = _snapmanager->getDesktop()->dt2doc(p);    
     
     Geom::Point direction_vector = c.getDirection();
     if (!is_zero(direction_vector)) {
@@ -547,8 +523,8 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
     // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
     // The distance between those points is twice the snapping tolerance
     Geom::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
-    Geom::Point const p_min_on_cl = desktop->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);    
-    Geom::Point const p_max_on_cl = desktop->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
+    Geom::Point const p_min_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);    
+    Geom::Point const p_max_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
     
     Geom::Path cl;
     std::vector<Geom::Path> clv;    
@@ -568,8 +544,8 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
                         Geom::Point p_inters = p_min_on_cl + ((*m).ta) * (p_max_on_cl - p_min_on_cl);
                         // When it's within snapping range, then return it
                         // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < ta < 1)                        
-                        Geom::Coord dist = Geom::L2(desktop->dt2doc(p_proj_on_cl) - p_inters);
-                        SnappedPoint s(desktop->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
+                        Geom::Coord dist = Geom::L2(_snapmanager->getDesktop()->dt2doc(p_proj_on_cl) - p_inters);
+                        SnappedPoint s(_snapmanager->getDesktop()->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true);
                         sc.points.push_back(s);
                     }  
                 } 
@@ -580,21 +556,21 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
 
 
 void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
-                                            Inkscape::Snapper::PointType const &t,
+                                            Inkscape::SnapPreferences::PointType const &t,
                                             Geom::Point const &p,
                                             bool const &first_point,
-                                            boost::optional<Geom::Rect> const &bbox_to_snap,
+                                            Geom::OptRect const &bbox_to_snap,
                                             std::vector<SPItem const *> const *it,
                                             std::vector<Geom::Point> *unselected_nodes) const
 {
-    if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
+       if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false ) {
         return;
     }
 
     /* Get a list of all the SPItems that we will try to snap to */
     if (first_point) {
         Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
-        _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
+        _findCandidates(sp_document_root(_snapmanager->getDocument()), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
     }
     
     if (_snap_to_itemnode || _snap_to_bboxnode || _snap_to_page_border) {
@@ -623,21 +599,21 @@ void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
 }
 
 void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
-                                                  Inkscape::Snapper::PointType const &t,
+                                                  Inkscape::SnapPreferences::PointType const &t,
                                                   Geom::Point const &p,
                                                   bool const &first_point,
-                                                  boost::optional<Geom::Rect> const &bbox_to_snap,
+                                                  Geom::OptRect const &bbox_to_snap,
                                                   ConstraintLine const &c,
                                                   std::vector<SPItem const *> const *it) const
 {
-    if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
+    if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false) {
         return;
     }
 
     /* Get a list of all the SPItems that we will try to snap to */
     if (first_point) {
         Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
-        _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
+        _findCandidates(sp_document_root(_snapmanager->getDocument()), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
     }
     
     // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
@@ -660,10 +636,6 @@ void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
                                         Geom::Point const &p,
                                         Geom::Point const &guide_normal) const
 {
-    if ( NULL == _named_view ) {
-        return;
-    }
-    
     /* Get a list of all the SPItems that we will try to snap to */
     std::vector<SPItem*> cand;
     std::vector<SPItem const *> const it; //just an empty list
@@ -687,8 +659,8 @@ void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
     // second time on an object; but should this point then be constrained to the
     // line, or can it be located anywhere?)
     
-    _findCandidates(sp_document_root(_named_view->document), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity());
-    _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal);
+    _findCandidates(sp_document_root(_snapmanager->getDocument()), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity());
+    _snapTranslatingGuideToNodes(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, guide_normal);
     // _snapRotatingGuideToNodes has not been implemented yet. 
 }
 
@@ -698,13 +670,13 @@ void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
 {
     bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
-    return (_snap_enabled && _snap_from != 0 && snap_to_something);
+    return (_snap_enabled && _snapmanager->snapprefs.getSnapModeBBoxOrNodes() && snap_to_something);
 }
 
 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
 {
     bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
-    return (_snap_enabled && (_snap_from & SNAPPOINT_GUIDE) && snap_to_something);
+    return (_snap_enabled && _snapmanager->snapprefs.getSnapModeGuide() && snap_to_something);
 }
 
 void Inkscape::ObjectSnapper::_clear_paths() const 
@@ -717,7 +689,7 @@ void Inkscape::ObjectSnapper::_clear_paths() const
 
 Geom::PathVector* Inkscape::ObjectSnapper::_getBorderPathv() const
 {
-    Geom::Rect const border_rect = Geom::Rect(Geom::Point(0,0), Geom::Point(sp_document_width(_named_view->document),sp_document_height(_named_view->document)));
+    Geom::Rect const border_rect = Geom::Rect(Geom::Point(0,0), Geom::Point(sp_document_width(_snapmanager->getDocument()),sp_document_height(_snapmanager->getDocument())));
     return _getPathvFromRect(border_rect);        
 }
 
@@ -734,8 +706,8 @@ Geom::PathVector* Inkscape::ObjectSnapper::_getPathvFromRect(Geom::Rect const re
 
 void Inkscape::ObjectSnapper::_getBorderNodes(std::vector<Geom::Point> *points) const
 {
-    Geom::Coord w = sp_document_width(_named_view->document);
-    Geom::Coord h = sp_document_height(_named_view->document);
+    Geom::Coord w = sp_document_width(_snapmanager->getDocument());
+    Geom::Coord h = sp_document_height(_snapmanager->getDocument());
     points->push_back(Geom::Point(0,0));
     points->push_back(Geom::Point(0,h));
     points->push_back(Geom::Point(w,h));