Code

more unreffing temporary styles properly
[inkscape.git] / src / snapper.h
1 #ifndef SEEN_SNAPPER_H
2 #define SEEN_SNAPPER_H
4 /**
5  *    \file src/snapper.h
6  *    \brief Snapper class.
7  *
8  *    Authors:
9  *      Carl Hetherington <inkscape@carlh.net>
10  *
11  *    Released under GNU GPL, read the file 'COPYING' for more information.
12  */
14 #include <map>
15 #include <list>
16 #include "libnr/nr-coord.h"
17 #include "libnr/nr-point.h"
18 #include "snapped-point.h"
20 struct SPNamedView;
21 struct SPItem;
23 namespace Inkscape
24 {
26 /// Parent for classes that can snap points to something
27 class Snapper
28 {
29 public:
30     Snapper() {}
31     Snapper(SPNamedView const *nv, ::NR::Coord const d);
32     virtual ~Snapper() {}
34     /// Point types to snap.
35     typedef int PointType;
36     static const PointType SNAP_POINT;
37     static const PointType BBOX_POINT;
39     typedef std::pair<PointType, NR::Point> PointWithType;
41     void setSnapTo(PointType t, bool s);
42     void setDistance(::NR::Coord d);
44     bool getSnapTo(PointType t) const;
45     ::NR::Coord getDistance() const;
47     /**
48     *  \return true if this Snapper will snap at least one kind of point.
49     */
50     virtual bool ThisSnapperMightSnap() const {return (_enabled && _snap_to != 0);} // will likely be overridden by derived classes
53     void setEnabled(bool s);
55     SnappedPoint freeSnap(PointType t,
56                           NR::Point const &p,
57                           SPItem const *it) const;
59     SnappedPoint freeSnap(PointType t,
60                           NR::Point const &p,
61                           std::list<SPItem const *> const &it) const;
63     class ConstraintLine
64     {
65     public:
66         ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
67         ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
69         bool hasPoint() const {
70             return _has_point;
71         }
73         NR::Point getPoint() const {
74             return _point;
75         }
77         NR::Point getDirection() const {
78             return _direction;
79         }
80         
81     private:
83         bool _has_point;
84         NR::Point _point;
85         NR::Point _direction;
86     };
88     SnappedPoint constrainedSnap(PointType t,
89                                  NR::Point const &p,
90                                  ConstraintLine const &c,
91                                  SPItem const *it) const;
93     SnappedPoint constrainedSnap(PointType t,
94                                  NR::Point const &p,
95                                  ConstraintLine const &c,
96                                  std::list<SPItem const *> const &it) const;
97 protected:
98     SPNamedView const *_named_view;
99     int _snap_to; ///< bitmap of point types that we will snap to
100     bool _enabled; ///< true if this snapper is enabled, otherwise false
101     
102 private:
104     /**
105      *  Try to snap a point to whatever this snapper is interested in.  Any
106      *  snap that occurs will be to the nearest "interesting" thing (e.g. a
107      *  grid or guide line)
108      *
109      *  \param p Point to snap (desktop coordinates).
110      *  \param it Items that should not be snapped to.
111      *  \return Snapped point.
112      */
113     virtual SnappedPoint _doFreeSnap(NR::Point const &p,
114                                      std::list<SPItem const *> const &it) const = 0;
116     /**
117      *  Try to snap a point to whatever this snapper is interested in, where
118      *  the snap point is constrained to lie along a specified vector from the
119      *  original point.
120      *
121      *  \param p Point to snap (desktop coordinates).
122      *  \param c Vector to constrain the snap to.
123      *  \param it Items that should not be snapped to.
124      *  \return Snapped point.
125      */    
126     virtual SnappedPoint _doConstrainedSnap(NR::Point const &p,
127                                             ConstraintLine const &c,
128                                             std::list<SPItem const *> const &it) const = 0;
129     
130     ::NR::Coord _distance; ///< snap distance (desktop coordinates)
131 };
135 #endif /* !SEEN_SNAPPER_H */
137 /*
138   Local Variables:
139   mode:c++
140   c-file-style:"stroustrup"
141   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
142   indent-tabs-mode:nil
143   fill-column:99
144   End:
145 */
146 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :