Code

Don't force focus on the canvas when the desktop is given
[inkscape.git] / src / snap.cpp
1 #define __SP_DESKTOP_SNAP_C__
3 /**
4  * \file snap.cpp
5  * \brief SnapManager class.
6  *
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   Frank Felfe <innerspace@iname.com>
10  *   Nathan Hurst <njh@njhurst.com>
11  *   Carl Hetherington <inkscape@carlh.net>
12  *
13  * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
14  * Copyrigth (C) 2004      Nathan Hurst
15  * Copyright (C) 1999-2002 Authors
16  *
17  * Released under GNU GPL, read the file 'COPYING' for more information
18  */
20 #include <utility>
22 #include "sp-namedview.h"
23 #include "snap.h"
24 #include "snapped-line.h"
26 #include <libnr/nr-point-fns.h>
27 #include <libnr/nr-scale-ops.h>
28 #include <libnr/nr-values.h>
30 #include "display/canvas-grid.h"
32 #include "inkscape.h"
33 #include "desktop.h"
34 #include "sp-guide.h"
35 using std::vector;
37 /**
38  *  Construct a SnapManager for a SPNamedView.
39  *
40  *  \param v `Owning' SPNamedView.
41  */
43 SnapManager::SnapManager(SPNamedView const *v) :
44     guide(v, 0),
45     object(v, 0),
46     _named_view(v),
47     _include_item_center(false)
48 {
49     
50 }
53 /**
54  *  \return List of snappers that we use.
55  */
56 SnapManager::SnapperList 
57 SnapManager::getSnappers() const
58 {
59     SnapManager::SnapperList s;
60     s.push_back(&guide);
61     s.push_back(&object);
63     SnapManager::SnapperList gs = getGridSnappers();
64     s.splice(s.begin(), gs);
66     return s;
67 }
69 /**
70  *  \return List of gridsnappers that we use.
71  */
72 SnapManager::SnapperList 
73 SnapManager::getGridSnappers() const
74 {
75     SnapperList s;
77     //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
78     SPDesktop* desktop = SP_ACTIVE_DESKTOP;
79     if (desktop && desktop->gridsEnabled()) {
80         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
81             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
82             s.push_back(grid->snapper);
83         }
84     }
86     return s;
87 }
89 /**
90  * \return true if one of the snappers will try to snap something.
91  */
93 bool SnapManager::SomeSnapperMightSnap() const
94 {
95     SnapperList const s = getSnappers();
96     SnapperList::const_iterator i = s.begin();
97     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
98         i++;
99     }
100     
101     return (i != s.end());
104 /*
105  *  The snappers have too many parameters to adjust individually. Therefore only
106  *  two snapping modes are presented to the user: snapping bounding box corners (to 
107  *     other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
108  *  paths, grids or guides). To select either of these modes (or both), use the 
109  *  methods defined below: setSnapModeBBox() and setSnapModeNode().
110  * 
111  * */
114 void SnapManager::setSnapModeBBox(bool enabled)
116     //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
117     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
118     
119     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
120         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
121         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
122     }
123     
124     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
125     //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
126     //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
127     object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa    
130 bool SnapManager::getSnapModeBBox() const
132     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
135 void SnapManager::setSnapModeNode(bool enabled)
137     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
138     
139     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
140         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
141         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
142     }
143         
144     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
145     //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
146     //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog 
147     object.setStrictSnapping(true);
150 bool SnapManager::getSnapModeNode() const
152     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
155 void SnapManager::setSnapModeGuide(bool enabled)
157     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
160 bool SnapManager::getSnapModeGuide() const
162     return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
165 /**
166  *  Try to snap a point to any interested snappers.
167  *
168  *  \param t Type of point.
169  *  \param p Point.
170  *  \param it Item to ignore when snapping.
171  *  \return Snapped point.
172  */
174 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
175                                              NR::Point const &p,
176                                              SPItem const *it) const
179     std::list<SPItem const *> lit;
180     lit.push_back(it);
181     
182     std::vector<NR::Point> points_to_snap;
183     points_to_snap.push_back(p);
184     
185     return freeSnap(t, p, true, points_to_snap, lit);
188 /**
189  *  Try to snap a point to any of the specified snappers.
190  *
191  *  \param t Type of point.
192  *  \param p Point.
193  *  \param first_point If true then this point is the first one from a whole bunch of points 
194  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
195  *  \param it List of items to ignore when snapping.
196  * \param snappers  List of snappers to try to snap to
197  *  \return Snapped point.
198  */
200 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
201                                              NR::Point const &p,
202                                              bool const &first_point,
203                                              std::vector<NR::Point> &points_to_snap,
204                                              std::list<SPItem const *> const &it) const
206     
207     SnappedConstraints sc;        
208     
209     SnapperList const snappers = getSnappers();
211     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
212         (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it);
213     }
215     return findBestSnap(p, sc);
218 /**
219  *  Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
220  *
221  *  \param t Type of point.
222  *  \param p Point.
223  *  \param it Item to ignore when snapping.
224  *  \param snappers  List of snappers to try to snap to
225  *  \return Snapped point.
226  */
228 Inkscape::SnappedPoint
229 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
230                              NR::Point const &p,
231                              SPItem const *it,
232                              SnapperList &snappers )
234     std::list<SPItem const *> lit;
235     lit.push_back(it);
236     return freeSnapAlways(t, p, lit, snappers);
239 /**
240  *  Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
241  *
242  *  \param t Type of point.
243  *  \param p Point.
244  *  \param it List of items to ignore when snapping.
245  *  \param snappers  List of snappers to try to snap to
246  *  \return Snapped point.
247  */
249 Inkscape::SnappedPoint
250 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
251                              NR::Point const &p,
252                              std::list<SPItem const *> const &it,
253                              SnapperList &snappers )
255     
256     SnappedConstraints sc;                
258     for (SnapperList::iterator i = snappers.begin(); i != snappers.end(); i++) {
259         gdouble const curr_gridsnap = (*i)->getDistance();
260         const_cast<Inkscape::Snapper*> (*i)->setDistance(NR_HUGE);
261         std::vector<NR::Point> points_to_snap;
262         points_to_snap.push_back(p);    
263         (*i)->freeSnap(sc, t, p, true, points_to_snap, it);
264         const_cast<Inkscape::Snapper*> (*i)->setDistance(curr_gridsnap);
265     }
267     return findBestSnap(p, sc);
272 /**
273  *  Try to snap a point to any interested snappers.  A snap will only occur along
274  *  a line described by a Inkscape::Snapper::ConstraintLine.
275  *
276  *  \param t Type of point.
277  *  \param p Point.
278  *  \param c Constraint line.
279  *  \param it Item to ignore when snapping.
280  *  \return Snapped point.
281  */
283 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
284                                                     NR::Point const &p,
285                                                     Inkscape::Snapper::ConstraintLine const &c,
286                                                     SPItem const *it) const
288     std::list<SPItem const *> lit;
289     lit.push_back(it);
290     
291     std::vector<NR::Point> points_to_snap;
292     points_to_snap.push_back(p);
293     
294     return constrainedSnap(t, p, true, points_to_snap, c, lit);
299 /**
300  *  Try to snap a point to any interested snappers.  A snap will only occur along
301  *  a line described by a Inkscape::Snapper::ConstraintLine.
302  *
303  *  \param t Type of point.
304  *  \param p Point.
305  *  \param first_point If true then this point is the first one from a whole bunch of points 
306  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
307  *  \param c Constraint line.
308  *  \param it List of items to ignore when snapping.
309  *  \return Snapped point.
310  */
312 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
313                                                     NR::Point const &p,
314                                                     bool const &first_point,
315                                                      std::vector<NR::Point> &points_to_snap,
316                                                     Inkscape::Snapper::ConstraintLine const &c,
317                                                     std::list<SPItem const *> const &it) const
319     
320     SnappedConstraints sc;
321         
322     SnapperList const snappers = getSnappers();
323     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
324         (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
325     }
327     return findBestSnap(p, sc);
330 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
331                                              NR::Point const &guide_normal) const
333     Inkscape::ObjectSnapper::DimensionToSnap snap_dim;
334     if (guide_normal == component_vectors[NR::Y]) {
335         snap_dim = Inkscape::ObjectSnapper::SNAP_Y;
336     } else if (guide_normal == component_vectors[NR::X]) {
337         snap_dim = Inkscape::ObjectSnapper::SNAP_X;
338     } else {
339         g_warning("WARNING: snapping of angled guides is not supported yet!");
340         snap_dim = Inkscape::ObjectSnapper::SNAP_XY;
341     }
342     
343     return object.guideSnap(p, snap_dim);    
347 /**
348  *  Main internal snapping method, which is called by the other, friendlier, public
349  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
350  *  of duplicated code.
351  *
352  *  \param type Type of points being snapped.
353  *  \param points List of points to snap.
354  *  \param ignore List of items to ignore while snapping.
355  *  \param constrained true if the snap is constrained.
356  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
357  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
358  *  \param transformation Description of the transformation; details depend on the type.
359  *  \param origin Origin of the transformation, if applicable.
360  *  \param dim Dimension of the transformation, if applicable.
361  *  \param uniform true if the transformation should be uniform, if applicable.
362  */
364 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
365     Inkscape::Snapper::PointType type,
366     std::vector<NR::Point> const &points,
367     std::list<SPItem const *> const &ignore,
368     bool constrained,
369     Inkscape::Snapper::ConstraintLine const &constraint,
370     Transformation transformation_type,
371     NR::Point const &transformation,
372     NR::Point const &origin,
373     NR::Dim2 dim,
374     bool uniform) const
376     /* We have a list of points, which we are proposing to transform in some way.  We need to see
377     ** if any of these points, when transformed, snap to anything.  If they do, we return the
378     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
379     */
381     /* Quick check to see if we have any snappers that are enabled */
382     if (SomeSnapperMightSnap() == false) {
383         return std::make_pair(transformation, false);
384     }
385     
386     std::vector<NR::Point> transformed_points;
387     
388     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
390         /* Work out the transformed version of this point */
391         NR::Point transformed;
392         switch (transformation_type) {
393             case TRANSLATION:
394                 transformed = *i + transformation;
395                 break;
396             case SCALE:
397                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
398                 break;
399             case STRETCH:
400             {
401                 NR::scale s(1, 1);
402                 if (uniform)
403                     s[NR::X] = s[NR::Y] = transformation[dim];
404                 else {
405                     s[dim] = transformation[dim];
406                     s[1 - dim] = 1;
407                 }
408                 transformed = ((*i - origin) * s) + origin;
409                 break;
410             }
411             case SKEW:
412                 transformed = *i;
413                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
414                 break;
415             default:
416                 g_assert_not_reached();
417         }
418         
419         // add the current transformed point to the box hulling all transformed points
420         transformed_points.push_back(transformed);
421     }    
422     
423     /* The current best transformation */
424     NR::Point best_transformation = transformation;
426     /* The current best metric for the best transformation; lower is better, NR_HUGE
427     ** means that we haven't snapped anything.
428     */
429     double best_metric = NR_HUGE;
431     std::vector<NR::Point>::const_iterator j = transformed_points.begin();
433     // std::cout << std::endl;
435     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
436         
437         /* Snap it */
438         Inkscape::SnappedPoint const snapped = constrained ?
439             constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
441         NR::Point result;
442         NR::Coord metric;
443         
444         if (snapped.getDistance() < NR_HUGE) {
445             /* We snapped.  Find the transformation that describes where the snapped point has
446             ** ended up, and also the metric for this transformation.
447             */
448             switch (transformation_type) {
449                 case TRANSLATION:
450                     result = snapped.getPoint() - *i;
451                     /* Consider the case in which a box is almost aligned with a grid in both 
452                      * horizontal and vertical directions. The distance to the intersection of
453                      * the grid lines will always be larger then the distance to a single grid
454                      * line. If we prefer snapping to an intersection instead of to a single 
455                      * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
456                      * snapped distance will be used as a metric. Please note that the snapped
457                      * distance is defined as the distance to the nearest line of the intersection,
458                      * and not to the intersection itself! 
459                      */
460                     metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
461                     break;
462                 case SCALE:
463                 {
464                     NR::Point const a = (snapped.getPoint() - origin);
465                     NR::Point const b = (*i - origin);
466                     result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
467                     metric = std::abs(NR::L2(result) - NR::L2(transformation));
468                     break;
469                 }
470                 case STRETCH:
471                 {
472                     for (int a = 0; a < 2; a++) {
473                         if (uniform || a == dim) {
474                             result[a] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
475                         } else {
476                             result[a] = 1;
477                         }
478                     }
479                     metric = std::abs(result[dim] - transformation[dim]);
480                     break;
481                 }
482                 case SKEW:
483                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
484                     metric = std::abs(result[dim] - transformation[dim]);
485                     break;
486                 default:
487                     g_assert_not_reached();
488             }
490             /* Note it if it's the best so far */
491             if ((metric < best_metric) || ((metric == best_metric) && snapped.getAtIntersection() == true)) {
492                 best_transformation = result;
493                 best_metric = metric;
494                 // std::cout << "SEL ";;
495             } //else { std::cout << "    ";}
496         }
497         
498         // std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | P_snap = " << snapped.getPoint() << std::endl;
499         j++;
500     }
501     
502     // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors
503     // These rounding errors might be caused by NRRects, see bug #1584301
504     return std::make_pair(best_transformation, best_metric < 1e6);
508 /**
509  *  Try to snap a list of points to any interested snappers after they have undergone
510  *  a translation.
511  *
512  *  \param t Type of points.
513  *  \param p Points.
514  *  \param it List of items to ignore when snapping.
515  *  \param tr Proposed translation.
516  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
517  */
519 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
520                                                             std::vector<NR::Point> const &p,
521                                                             std::list<SPItem const *> const &it,
522                                                             NR::Point const &tr) const
524     return _snapTransformed(
525         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
526         );
530 /**
531  *  Try to snap a list of points to any interested snappers after they have undergone a
532  *  translation.  A snap will only occur along a line described by a
533  *  Inkscape::Snapper::ConstraintLine.
534  *
535  *  \param t Type of points.
536  *  \param p Points.
537  *  \param it List of items to ignore when snapping.
538  *  \param c Constraint line.
539  *  \param tr Proposed translation.
540  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
541  */
543 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
544                                                                    std::vector<NR::Point> const &p,
545                                                                    std::list<SPItem const *> const &it,
546                                                                    Inkscape::Snapper::ConstraintLine const &c,
547                                                                    NR::Point const &tr) const
549     return _snapTransformed(
550         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
551         );
555 /**
556  *  Try to snap a list of points to any interested snappers after they have undergone
557  *  a scale.
558  *
559  *  \param t Type of points.
560  *  \param p Points.
561  *  \param it List of items to ignore when snapping.
562  *  \param s Proposed scale.
563  *  \param o Origin of proposed scale.
564  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
565  */
567 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
568                                                       std::vector<NR::Point> const &p,
569                                                       std::list<SPItem const *> const &it,
570                                                       NR::scale const &s,
571                                                       NR::Point const &o) const
573     return _snapTransformed(
574         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
575         );
579 /**
580  *  Try to snap a list of points to any interested snappers after they have undergone
581  *  a scale.  A snap will only occur along a line described by a
582  *  Inkscape::Snapper::ConstraintLine.
583  *
584  *  \param t Type of points.
585  *  \param p Points.
586  *  \param it List of items to ignore when snapping.
587  *  \param s Proposed scale.
588  *  \param o Origin of proposed scale.
589  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
590  */
592 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
593                                                              std::vector<NR::Point> const &p,
594                                                              std::list<SPItem const *> const &it,
595                                                              Inkscape::Snapper::ConstraintLine const &c,
596                                                              NR::scale const &s,
597                                                              NR::Point const &o) const
599     return _snapTransformed(
600         t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
601         );
605 /**
606  *  Try to snap a list of points to any interested snappers after they have undergone
607  *  a stretch.
608  *
609  *  \param t Type of points.
610  *  \param p Points.
611  *  \param it List of items to ignore when snapping.
612  *  \param s Proposed stretch.
613  *  \param o Origin of proposed stretch.
614  *  \param d Dimension in which to apply proposed stretch.
615  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
616  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
617  */
619 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
620                                                         std::vector<NR::Point> const &p,
621                                                         std::list<SPItem const *> const &it,
622                                                         NR::Coord const &s,
623                                                         NR::Point const &o,
624                                                         NR::Dim2 d,
625                                                         bool u) const
627    std::pair<NR::Point, bool> const r = _snapTransformed(
628         t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
629         );
631    return std::make_pair(r.first[d], r.second);
635 /**
636  *  Try to snap a list of points to any interested snappers after they have undergone
637  *  a skew.
638  *
639  *  \param t Type of points.
640  *  \param p Points.
641  *  \param it List of items to ignore when snapping.
642  *  \param s Proposed skew.
643  *  \param o Origin of proposed skew.
644  *  \param d Dimension in which to apply proposed skew.
645  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
646  */
648 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
649                                                      std::vector<NR::Point> const &p,
650                                                      std::list<SPItem const *> const &it,
651                                                      NR::Coord const &s,
652                                                      NR::Point const &o,
653                                                      NR::Dim2 d) const
655    std::pair<NR::Point, bool> const r = _snapTransformed(
656         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
657         );
659    return std::make_pair(r.first[d], r.second);
662 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc) const
664     NR::Coord const guide_sens = guide.getDistance();
665     NR::Coord grid_sens = 0;
666     
667     SnapManager::SnapperList const gs = getGridSnappers();
668     SnapperList::const_iterator i = gs.begin();
669     if (i != gs.end()) {        
670         grid_sens = (*i)->getDistance();
671     }
672     
673     // Store all snappoints, optionally together with their specific snapping range
674     std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> > sp_list;
675     // Most of these snapped points are already within the snapping range, because
676     // they have already been filtered by their respective snappers. In that case
677     // we can set the snapping range to NR_HUGE here. If however we're looking at
678     // intersections of e.g. a grid and guide line, then we'll have to determine 
679     // once again whether we're within snapping range. In this case we will set
680     // the snapping range to e.g. min(guide_sens, grid_sens)
681     
682     // search for the closest snapped point
683     Inkscape::SnappedPoint closestPoint;
684     if (getClosestSP(sc.points, closestPoint)) {
685         sp_list.push_back(std::make_pair(closestPoint, NR_HUGE));
686     } 
687     
688     // search for the closest snapped grid line
689     Inkscape::SnappedLine closestGridLine;
690     if (getClosestSL(sc.grid_lines, closestGridLine)) {    
691         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGridLine), NR_HUGE));
692     }
693     
694     // search for the closest snapped guide line
695     Inkscape::SnappedLine closestGuideLine;
696     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
697         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGuideLine), NR_HUGE));
698     }
699     
700     // search for the closest snapped intersection of grid lines
701     Inkscape::SnappedPoint closestGridPoint;
702     if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
703         sp_list.push_back(std::make_pair(closestGridPoint, NR_HUGE));
704     }
705     
706     // search for the closest snapped intersection of guide lines
707     Inkscape::SnappedPoint closestGuidePoint;
708     if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
709         sp_list.push_back(std::make_pair(closestGuidePoint, NR_HUGE));
710     }
711     
712     // search for the closest snapped intersection of grid with guide lines
713     Inkscape::SnappedPoint closestGridGuidePoint;
714     if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
715         sp_list.push_back(std::make_pair(closestGridGuidePoint, std::min(guide_sens, grid_sens)));
716     }
717     
718     // now let's see which snapped point gets a thumbs up
719      Inkscape::SnappedPoint bestPoint(p, NR_HUGE);
720     for (std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
721          // first find out if this snapped point is within snapping range
722          if ((*i).first.getDistance() <= (*i).second) {
723              // if it's the first point
724              bool c1 = (i == sp_list.begin());  
725              // or, if it's closer
726              bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); 
727              // or, if it's just as close but at an intersection
728              bool c3 = ((*i).first.getDistance() == bestPoint.getDistance()) && (*i).first.getAtIntersection(); 
729              // then prefer this point over the previous one
730              if (c1 || c2 || c3) {
731                  bestPoint = (*i).first;
732              }
733          }
734      }
735      return bestPoint;         
737 /*
738   Local Variables:
739   mode:c++
740   c-file-style:"stroustrup"
741   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
742   indent-tabs-mode:nil
743   fill-column:99
744   End:
745 */
746 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :