1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
5 #include <glibmm/i18n.h>
6 #include "sp-item.h"
7 #include "desktop.h"
8 #include "message-context.h"
9 #include "message-stack.h"
10 #include "context-fns.h"
11 #include "snap.h"
12 #include "desktop-affine.h"
13 #include "event-context.h"
14 #include "sp-namedview.h"
15 #include "display/snap-indicator.h"
17 static const double midpt_1_goldenratio = (1 + goldenratio) / 2;
18 static const double midpt_goldenratio_2 = (goldenratio + 2) / 2;
20 /* FIXME: could probably use a template here */
22 /**
23 * Check to see if the current layer is both unhidden and unlocked. If not,
24 * set a message about it on the given context.
25 *
26 * \param desktop Desktop.
27 * \param message Message context to put messages on.
28 * \return true if the current layer is both unhidden and unlocked, otherwise false.
29 */
31 bool Inkscape::have_viable_layer(SPDesktop *desktop, MessageContext *message)
32 {
33 SPItem const *layer = SP_ITEM(desktop->currentLayer());
35 if ( !layer || desktop->itemIsHidden(layer) ) {
36 message->flash(Inkscape::ERROR_MESSAGE,
37 _("<b>Current layer is hidden</b>. Unhide it to be able to draw on it."));
38 return false;
39 }
41 if ( !layer || layer->isLocked() ) {
42 message->flash(Inkscape::ERROR_MESSAGE,
43 _("<b>Current layer is locked</b>. Unlock it to be able to draw on it."));
44 return false;
45 }
47 return true;
48 }
51 /**
52 * Check to see if the current layer is both unhidden and unlocked. If not,
53 * set a message about it on the given context.
54 *
55 * \param desktop Desktop.
56 * \param message Message context to put messages on.
57 * \return true if the current layer is both unhidden and unlocked, otherwise false.
58 */
60 bool Inkscape::have_viable_layer(SPDesktop *desktop, MessageStack *message)
61 {
62 SPItem const *layer = SP_ITEM(desktop->currentLayer());
64 if ( !layer || desktop->itemIsHidden(layer) ) {
65 message->flash(Inkscape::WARNING_MESSAGE,
66 _("<b>Current layer is hidden</b>. Unhide it to be able to draw on it."));
67 return false;
68 }
70 if ( !layer || layer->isLocked() ) {
71 message->flash(Inkscape::WARNING_MESSAGE,
72 _("<b>Current layer is locked</b>. Unlock it to be able to draw on it."));
73 return false;
74 }
76 return true;
77 }
80 NR::Rect Inkscape::snap_rectangular_box(SPDesktop const *desktop, SPItem *item,
81 NR::Point const &pt, NR::Point const ¢er, int state)
82 {
83 NR::Point p[2];
85 bool const shift = state & GDK_SHIFT_MASK;
86 bool const control = state & GDK_CONTROL_MASK;
88 SnapManager const &m = desktop->namedview->snap_manager;
89 Inkscape::SnappedPoint snappoint;
91 if (control) {
93 /* Control is down: we are constrained to producing integer-ratio rectangles */
95 /* Vector from the centre of the box to the point we are dragging to */
96 NR::Point delta = pt - center;
98 /* Round it so that we have an integer-ratio (or golden ratio) box */
99 if (fabs(delta[NR::X]) > fabs(delta[NR::Y]) && (delta[NR::Y] != 0.0)) {
100 double ratio = delta[NR::X] / delta[NR::Y];
101 double ratioabs = fabs (ratio);
102 double sign = (ratio < 0 ? -1 : 1);
103 if (midpt_1_goldenratio < ratioabs && ratioabs < midpt_goldenratio_2) {
104 delta[NR::X] = sign * goldenratio * delta[NR::Y];
105 } else {
106 delta[NR::X] = floor(ratio + 0.5) * delta[NR::Y];
107 }
108 } else if (delta[NR::X] != 0.0) {
109 double ratio = delta[NR::Y] / delta[NR::X];
110 double ratioabs = fabs (ratio);
111 double sign = (ratio < 0 ? -1 : 1);
112 if (midpt_1_goldenratio < ratioabs && ratioabs < midpt_goldenratio_2) {
113 delta[NR::Y] = sign * goldenratio * delta[NR::X];
114 } else {
115 delta[NR::Y] = floor(delta[NR::Y] / delta[NR::X] + 0.5) * delta[NR::X];
116 }
117 }
119 /* p[1] is the dragged point with the integer-ratio constraint */
120 p[1] = center + delta;
122 if (shift) {
124 /* Shift is down, so our origin is the centre point rather than the corner
125 ** point; this means that corner-point movements are bound to each other.
126 */
128 /* p[0] is the opposite corner of our box */
129 p[0] = center - delta;
131 Inkscape::SnappedPoint s[2];
133 /* Try to snap p[0] (the opposite corner) along the constraint vector */
134 s[0] = m.constrainedSnap(Inkscape::Snapper::SNAPPOINT_NODE, p[0],
135 Inkscape::Snapper::ConstraintLine(p[0] - p[1]), item);
137 /* Try to snap p[1] (the dragged corner) along the constraint vector */
138 s[1] = m.constrainedSnap(Inkscape::Snapper::SNAPPOINT_NODE, p[1],
139 Inkscape::Snapper::ConstraintLine(p[1] - p[0]), item);
141 /* Choose the best snap and update points accordingly */
142 if (s[0].getDistance() < s[1].getDistance()) {
143 p[0] = s[0].getPoint();
144 p[1] = 2 * center - s[0].getPoint();
145 snappoint = s[0];
146 } else {
147 p[0] = 2 * center - s[1].getPoint();
148 p[1] = s[1].getPoint();
149 snappoint = s[1];
150 }
152 } else {
154 /* Our origin is the opposite corner. Snap the drag point along the constraint vector */
155 p[0] = center;
156 snappoint = m.constrainedSnap(Inkscape::Snapper::SNAPPOINT_NODE, p[1],
157 Inkscape::Snapper::ConstraintLine(p[1] - p[0]), item);
158 p[1] = snappoint.getPoint();
159 }
161 } else if (shift) {
163 /* Shift is down, so our origin is the centre point rather than the corner point;
164 ** this means that corner-point movements are bound to each other.
165 */
167 p[1] = pt;
168 p[0] = 2 * center - p[1];
170 Inkscape::SnappedPoint s[2];
172 s[0] = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p[0], item);
173 s[1] = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p[1], item);
175 if (s[0].getDistance() < s[1].getDistance()) {
176 p[0] = s[0].getPoint();
177 p[1] = 2 * center - s[0].getPoint();
178 snappoint = s[0];
179 } else {
180 p[0] = 2 * center - s[1].getPoint();
181 p[1] = s[1].getPoint();
182 snappoint = s[1];
183 }
185 } else {
187 /* There's no constraint on the corner point, so just snap it to anything */
188 p[0] = center;
189 snappoint = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, pt, item);
190 p[1] = snappoint.getPoint();
191 }
193 if (snappoint.getSnapped()) {
194 // this does not work well enough yet.
195 // desktop->snapindicator->set_new_snappoint(snappoint);
196 }
198 p[0] = sp_desktop_dt2root_xy_point(desktop, p[0]);
199 p[1] = sp_desktop_dt2root_xy_point(desktop, p[1]);
201 return NR::Rect(NR::Point(MIN(p[0][NR::X], p[1][NR::X]), MIN(p[0][NR::Y], p[1][NR::Y])),
202 NR::Point(MAX(p[0][NR::X], p[1][NR::X]), MAX(p[0][NR::Y], p[1][NR::Y])));
203 }
207 NR::Point Inkscape::setup_for_drag_start(SPDesktop *desktop, SPEventContext* ec, GdkEvent *ev)
208 {
209 ec->xp = static_cast<gint>(ev->button.x);
210 ec->yp = static_cast<gint>(ev->button.y);
211 ec->within_tolerance = true;
213 NR::Point const p(ev->button.x, ev->button.y);
214 ec->item_to_select = sp_event_context_find_item(desktop, p, ev->button.state & GDK_MOD1_MASK, TRUE);
215 return ec->desktop->w2d(p);
216 }
219 /*
220 Local Variables:
221 mode:c++
222 c-file-style:"stroustrup"
223 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
224 indent-tabs-mode:nil
225 fill-column:99
226 End:
227 */
228 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :