Code

Give SPNamedView a SnapManager instance and use it for all management of snapping...
[inkscape.git] / src / context-fns.cpp
1 #include <glibmm/i18n.h>
2 #include "sp-item.h"
3 #include "desktop.h"
4 #include "message-context.h"
5 #include "message-stack.h"
6 #include "context-fns.h"
7 #include "snap.h"
8 #include "desktop-affine.h"
9 #include "event-context.h"
10 #include "sp-namedview.h"
12 /* FIXME: could probably use a template here */
14 /**
15  *  Check to see if the current layer is both unhidden and unlocked.  If not,
16  *  set a message about it on the given context.
17  *
18  *  \param desktop Desktop.
19  *  \param message Message context to put messages on.
20  *  \return true if the current layer is both unhidden and unlocked, otherwise false.
21  */
23 bool Inkscape::have_viable_layer(SPDesktop *desktop, MessageContext *message)
24 {
25     SPItem const *layer = SP_ITEM(desktop->currentLayer());
27     if ( !layer || desktop->itemIsHidden(layer) ) {
28             message->flash(Inkscape::ERROR_MESSAGE,
29                          _("<b>Current layer is hidden</b>. Unhide it to be able to draw on it."));
30             return false;
31     }
33     if ( !layer || layer->isLocked() ) {
34             message->flash(Inkscape::ERROR_MESSAGE,
35                          _("<b>Current layer is locked</b>. Unlock it to be able to draw on it."));
36             return false;
37     }
39     return true;
40 }
43 /**
44  *  Check to see if the current layer is both unhidden and unlocked.  If not,
45  *  set a message about it on the given context.
46  *
47  *  \param desktop Desktop.
48  *  \param message Message context to put messages on.
49  *  \return true if the current layer is both unhidden and unlocked, otherwise false.
50  */
52 bool Inkscape::have_viable_layer(SPDesktop *desktop, MessageStack *message)
53 {
54     SPItem const *layer = SP_ITEM(desktop->currentLayer());
56     if ( !layer || desktop->itemIsHidden(layer) ) {
57             message->flash(Inkscape::WARNING_MESSAGE,
58                          _("<b>Current layer is hidden</b>. Unhide it to be able to draw on it."));
59             return false;
60     }
62     if ( !layer || layer->isLocked() ) {
63             message->flash(Inkscape::WARNING_MESSAGE,
64                          _("<b>Current layer is locked</b>. Unlock it to be able to draw on it."));
65             return false;
66     }
68     return true;
69 }
72 NR::Rect Inkscape::snap_rectangular_box(SPDesktop const *desktop, SPItem *item,
73                                         NR::Point const &pt, NR::Point const &center, int state)
74 {
75     NR::Point p[2];
77     bool const shift = state & GDK_SHIFT_MASK;
78     bool const control = state & GDK_CONTROL_MASK;
80     SnapManager const &m = desktop->namedview->snap_manager;
82     if (control) {
84         /* Control is down: we are constrained to producing integer-ratio rectangles */
86         /* Vector from the centre of the box to the point we are dragging to */
87         NR::Point delta = pt - center;
89         /* Round it so that we have an integer-ratio box */
90         if (fabs(delta[NR::X]) > fabs(delta[NR::Y]) && (delta[NR::Y] != 0.0)) {
91             delta[NR::X] = floor(delta[NR::X] / delta[NR::Y] + 0.5) * delta[NR::Y];
92         } else if (delta[NR::X] != 0.0) {
93             delta[NR::Y] = floor(delta[NR::Y] / delta[NR::X] + 0.5) * delta[NR::X];
94         }
96         /* p[1] is the dragged point with the integer-ratio constraint */
97         p[1] = center + delta;
99         if (shift) {
101             /* Shift is down, so our origin is the centre point rather than the corner
102             ** point; this means that corner-point movements are bound to each other.
103             */
105             /* p[0] is the opposite corner of our box */
106             p[0] = center - delta;
108             Inkscape::SnappedPoint s[2];
110             /* Try to snap p[0] (the opposite corner) along the constraint vector */
111             s[0] = m.constrainedSnap(Inkscape::Snapper::SNAP_POINT,
112                                      p[0], p[0] - p[1], item);
114             /* Try to snap p[1] (the dragged corner) along the constraint vector */
115             s[1] = m.constrainedSnap(Inkscape::Snapper::SNAP_POINT,
116                                      p[1], p[1] - p[0], item);
118             /* Choose the best snap and update points accordingly */
119             if (s[0].getDistance() < s[1].getDistance()) {
120                 p[0] = s[0].getPoint();
121                 p[1] = 2 * center - s[0].getPoint();
122             } else {
123                 p[0] = 2 * center - s[1].getPoint();
124                 p[1] = s[1].getPoint();
125             }
127         } else {
129             /* Our origin is the opposite corner.  Snap the drag point along the constraint vector */
130             p[0] = center;
131             p[1] = m.constrainedSnap(Inkscape::Snapper::SNAP_POINT, p[1], p[1] - p[0], item).getPoint();
132         }
134     } else if (shift) {
136         /* Shift is down, so our origin is the centre point rather than the corner point;
137         ** this means that corner-point movements are bound to each other.
138         */
140         p[1] = pt;
141         p[0] = 2 * center - p[1];
143         Inkscape::SnappedPoint s[2];
145         s[0] = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p[0], item);
146         s[1] = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p[1], item);
148         if (s[0].getDistance() < s[1].getDistance()) {
149             p[0] = s[0].getPoint();
150             p[1] = 2 * center - s[0].getPoint();
151         } else {
152             p[0] = 2 * center - s[1].getPoint();
153             p[1] = s[1].getPoint();
154         }
156     } else {
158         /* There's no constraint on the corner point, so just snap it to anything */
159         p[0] = center;
160         p[1] = m.freeSnap(Inkscape::Snapper::SNAP_POINT, pt, item).getPoint();
161     }
163     p[0] = sp_desktop_dt2root_xy_point(desktop, p[0]);
164     p[1] = sp_desktop_dt2root_xy_point(desktop, p[1]);
166     return NR::Rect(NR::Point(MIN(p[0][NR::X], p[1][NR::X]), MIN(p[0][NR::Y], p[1][NR::Y])),
167                     NR::Point(MAX(p[0][NR::X], p[1][NR::X]), MAX(p[0][NR::Y], p[1][NR::Y])));
172 NR::Point Inkscape::setup_for_drag_start(SPDesktop *desktop, SPEventContext* ec, GdkEvent *ev)
174     ec->xp = static_cast<gint>(ev->button.x);
175     ec->yp = static_cast<gint>(ev->button.y);
176     ec->within_tolerance = true;
178     NR::Point const p(ev->button.x, ev->button.y);
179     ec->item_to_select = sp_event_context_find_item(desktop, p, ev->button.state & GDK_MOD1_MASK, TRUE);
180     return ec->desktop->w2d(p);
184 /*
185   Local Variables:
186   mode:c++
187   c-file-style:"stroustrup"
188   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
189   indent-tabs-mode:nil
190   fill-column:99
191   End:
192 */
193 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :