Code

Warning cleanup
[inkscape.git] / src / object-edit.cpp
1 #define __SP_OBJECT_EDIT_C__
3 /*
4  * Node editing extension to objects
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Mitsuru Oka
9  *
10  * Licensed under GNU GPL
11  */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
19 #include "sp-item.h"
20 #include "sp-rect.h"
21 #include "box3d.h"
22 #include "sp-ellipse.h"
23 #include "sp-star.h"
24 #include "sp-spiral.h"
25 #include "sp-offset.h"
26 #include "sp-flowtext.h"
27 #include "prefs-utils.h"
28 #include "inkscape.h"
29 #include "snap.h"
30 #include "desktop-affine.h"
31 #include <style.h>
32 #include "desktop.h"
33 #include "desktop-handles.h"
34 #include "sp-namedview.h"
36 #include "sp-pattern.h"
37 #include "sp-path.h"
39 #include <glibmm/i18n.h>
41 #include "object-edit.h"
43 #include <libnr/nr-scale-ops.h>
46 #include "xml/repr.h"
48 #include "isnan.h"
50 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
52 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
53 //static
54 SPKnotHolder *sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop);
55 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
56 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
57 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
58 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
59 static SPKnotHolder *sp_misc_knot_holder(SPItem *item, SPDesktop *desktop);
60 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
61 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
63 SPKnotHolder *
64 sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
65 {
66     if (SP_IS_RECT(item)) {
67         return sp_rect_knot_holder(item, desktop);
68     } else if (SP_IS_3DBOX(item)) {
69         return sp_3dbox_knot_holder(item, desktop);
70     } else if (SP_IS_ARC(item)) {
71         return sp_arc_knot_holder(item, desktop);
72     } else if (SP_IS_STAR(item)) {
73         return sp_star_knot_holder(item, desktop);
74     } else if (SP_IS_SPIRAL(item)) {
75         return sp_spiral_knot_holder(item, desktop);
76     } else if (SP_IS_OFFSET(item)) {
77         return sp_offset_knot_holder(item, desktop);
78     } else if (SP_IS_FLOWTEXT(item) && SP_FLOWTEXT(item)->has_internal_frame()) {
79         return sp_flowtext_knot_holder(item, desktop);
80     } else {
81         return sp_misc_knot_holder(item, desktop);
82     }
84     return NULL;
85 }
88 /* Pattern manipulation */
90 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
91 {
92     gdouble theta = asin(pat->patternTransform[1] / scale);
93     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
94     return theta;
95 }
97 static gdouble sp_pattern_extract_scale(SPPattern *pat)
98 {
99     gdouble s = pat->patternTransform[1];
100     gdouble c = pat->patternTransform[0];
101     gdouble xscale = sqrt(c * c + s * s);
102     return xscale;
105 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
107     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
110 static void
111 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
113     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
115     NR::Point p_snapped = p;
117     if ( state & GDK_CONTROL_MASK ) {
118         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
119             p_snapped[NR::Y] = origin[NR::Y];
120         } else {
121             p_snapped[NR::X] = origin[NR::X];
122         }
123     }
125     if (state)  {
126         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
127         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
128     }
130     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
134 static NR::Point sp_pattern_xy_get(SPItem *item)
136     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
137     return sp_pattern_extract_trans(pat);
140 static NR::Point sp_pattern_angle_get(SPItem *item)
142     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
144     gdouble x = (pattern_width(pat)*0.5);
145     gdouble y = 0;
146     NR::Point delta = NR::Point(x,y);
147     gdouble scale = sp_pattern_extract_scale(pat);
148     gdouble theta = sp_pattern_extract_theta(pat, scale);
149     delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
150     delta = delta + sp_pattern_extract_trans(pat);
151     return delta;
154 static void
155 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
157     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
159     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
161     // get the angle from pattern 0,0 to the cursor pos
162     NR::Point delta = p - sp_pattern_extract_trans(pat);
163     gdouble theta = atan2(delta);
165     if ( state & GDK_CONTROL_MASK ) {
166         theta = sp_round(theta, M_PI/snaps);
167     }
169     // get the scale from the current transform so we can keep it.
170     gdouble scl = sp_pattern_extract_scale(pat);
171     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
172     NR::Point const t = sp_pattern_extract_trans(pat);
173     rot[4] = t[NR::X];
174     rot[5] = t[NR::Y];
175     sp_item_adjust_pattern(item, rot, true);
176     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
179 static void
180 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
182     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
184     // Get the scale from the position of the knotholder,
185     NR::Point d = p - sp_pattern_extract_trans(pat);
186     gdouble s = NR::L2(d);
187     gdouble pat_x = pattern_width(pat) * 0.5;
188     gdouble pat_y = pattern_height(pat) * 0.5;
189     gdouble pat_h = hypot(pat_x, pat_y);
190     gdouble scl = s / pat_h;
192     // get angle from current transform, (need get current scale first to calculate angle)
193     gdouble oldscale = sp_pattern_extract_scale(pat);
194     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
196     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
197     NR::Point const t = sp_pattern_extract_trans(pat);
198     rot[4] = t[NR::X];
199     rot[5] = t[NR::Y];
200     sp_item_adjust_pattern(item, rot, true);
201     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
205 static NR::Point sp_pattern_scale_get(SPItem *item)
207     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
209     gdouble x = pattern_width(pat)*0.5;
210     gdouble y = pattern_height(pat)*0.5;
211     NR::Point delta = NR::Point(x,y);
212     NR::Matrix a = pat->patternTransform;
213     a[4] = 0;
214     a[5] = 0;
215     delta = delta * a;
216     delta = delta + sp_pattern_extract_trans(pat);
217     return delta;
220 /* SPRect */
222 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
224     SPDesktop const *desktop = inkscape_active_desktop();
225     NR::Matrix const i2d (sp_item_i2d_affine (item));
226     NR::Point s = p * i2d;
227     SnapManager const &m = desktop->namedview->snap_manager;
228     s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, s, item).getPoint();
229     return s * i2d.inverse();
232 static NR::Point sp_rect_rx_get(SPItem *item)
234     SPRect *rect = SP_RECT(item);
236     return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
239 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
241     SPRect *rect = SP_RECT(item);
243     //In general we cannot just snap this radius to an arbitrary point, as we have only a single
244     //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
245     //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
247     if (state & GDK_CONTROL_MASK) {
248         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
249         rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
250         rect->rx._set = rect->ry._set = true;
252     } else {
253         rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
254         rect->rx._set = true;
255     }
257     ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
261 static NR::Point sp_rect_ry_get(SPItem *item)
263     SPRect *rect = SP_RECT(item);
265     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
268 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
270     SPRect *rect = SP_RECT(item);
272     //In general we cannot just snap this radius to an arbitrary point, as we have only a single
273     //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
274     //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
276     if (state & GDK_CONTROL_MASK) {
277         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
278         rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
279         rect->ry._set = rect->rx._set = true;
280     } else {
281         if (!rect->rx._set || rect->rx.computed == 0) {
282             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
283                                       0.0,
284                                       MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
285         } else {
286             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
287                                       0.0,
288                                       rect->height.computed / 2.0);
289         }
291         rect->ry._set = true;
292     }
294     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
297 /**
298  *  Remove rounding from a rectangle.
299  */
300 static void rect_remove_rounding(SPRect *rect)
302     SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
303     SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
306 /**
307  *  Called when the horizontal rounding radius knot is clicked.
308  */
309 static void sp_rect_rx_knot_click(SPItem *item, guint state)
311     SPRect *rect = SP_RECT(item);
313     if (state & GDK_SHIFT_MASK) {
314         rect_remove_rounding(rect);
315     } else if (state & GDK_CONTROL_MASK) {
316         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
317         SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
318     }
321 /**
322  *  Called when the vertical rounding radius knot is clicked.
323  */
324 static void sp_rect_ry_knot_click(SPItem *item, guint state)
326     SPRect *rect = SP_RECT(item);
328     if (state & GDK_SHIFT_MASK) {
329         rect_remove_rounding(rect);
330     } else if (state & GDK_CONTROL_MASK) {
331         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
332         SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
333     }
336 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
338 static void sp_rect_clamp_radii(SPRect *rect)
340     // clamp rounding radii so that they do not exceed width/height
341     if (2 * rect->rx.computed > rect->width.computed) {
342         rect->rx.computed = 0.5 * rect->width.computed;
343         rect->rx._set = true;
344     }
345     if (2 * rect->ry.computed > rect->height.computed) {
346         rect->ry.computed = 0.5 * rect->height.computed;
347         rect->ry._set = true;
348     }
351 static NR::Point sp_rect_wh_get(SPItem *item)
353     SPRect *rect = SP_RECT(item);
355     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
358 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
360     NR::Point const s = snap_knot_position(rect, p);
362     if (state & GDK_CONTROL_MASK) {
363         // original width/height when drag started
364         gdouble const w_orig = (origin[NR::X] - rect->x.computed);
365         gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
367         //original ratio
368         gdouble const ratio = (w_orig / h_orig);
370         // mouse displacement since drag started
371         gdouble const minx = s[NR::X] - origin[NR::X];
372         gdouble const miny = s[NR::Y] - origin[NR::Y];
374         if (fabs(minx) > fabs(miny)) {
376             // snap to horizontal or diagonal
377             rect->width.computed = MAX(w_orig + minx, 0);
378             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
379                 // closer to the diagonal and in same-sign quarters, change both using ratio
380                 rect->height.computed = MAX(h_orig + minx / ratio, 0);
381             } else {
382                 // closer to the horizontal, change only width, height is h_orig
383                 rect->height.computed = MAX(h_orig, 0);
384             }
386         } else {
387             // snap to vertical or diagonal
388             rect->height.computed = MAX(h_orig + miny, 0);
389             if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
390                 // closer to the diagonal and in same-sign quarters, change both using ratio
391                 rect->width.computed = MAX(w_orig + miny * ratio, 0);
392             } else {
393                 // closer to the vertical, change only height, width is w_orig
394                 rect->width.computed = MAX(w_orig, 0);
395             }
396         }
398         rect->width._set = rect->height._set = true;
400     } else {
401         // move freely
402         rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
403         rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
404         rect->width._set = rect->height._set = true;
405     }
407     sp_rect_clamp_radii(rect);
409     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
412 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
414     SPRect *rect = SP_RECT(item);
416     sp_rect_wh_set_internal(rect, p, origin, state);
419 static NR::Point sp_rect_xy_get(SPItem *item)
421     SPRect *rect = SP_RECT(item);
423     return NR::Point(rect->x.computed, rect->y.computed);
426 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
428     SPRect *rect = SP_RECT(item);
430     // opposite corner (unmoved)
431     gdouble opposite_x = (rect->x.computed + rect->width.computed);
432     gdouble opposite_y = (rect->y.computed + rect->height.computed);
434     // original width/height when drag started
435     gdouble w_orig = opposite_x - origin[NR::X];
436     gdouble h_orig = opposite_y - origin[NR::Y];
438     NR::Point const s = snap_knot_position(rect, p);
440     // mouse displacement since drag started
441     gdouble minx = s[NR::X] - origin[NR::X];
442     gdouble miny = s[NR::Y] - origin[NR::Y];
444     if (state & GDK_CONTROL_MASK) {
445         //original ratio
446         gdouble ratio = (w_orig / h_orig);
448         if (fabs(minx) > fabs(miny)) {
450             // snap to horizontal or diagonal
451             rect->x.computed = MIN(s[NR::X], opposite_x);
452             rect->width.computed = MAX(w_orig - minx, 0);
453             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
454                 // closer to the diagonal and in same-sign quarters, change both using ratio
455                 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
456                 rect->height.computed = MAX(h_orig - minx / ratio, 0);
457             } else {
458                 // closer to the horizontal, change only width, height is h_orig
459                 rect->y.computed = MIN(origin[NR::Y], opposite_y);
460                 rect->height.computed = MAX(h_orig, 0);
461             }
463         } else {
465             // snap to vertical or diagonal
466             rect->y.computed = MIN(s[NR::Y], opposite_y);
467             rect->height.computed = MAX(h_orig - miny, 0);
468             if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
469                 // closer to the diagonal and in same-sign quarters, change both using ratio
470                 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
471                 rect->width.computed = MAX(w_orig - miny * ratio, 0);
472             } else {
473                 // closer to the vertical, change only height, width is w_orig
474                 rect->x.computed = MIN(origin[NR::X], opposite_x);
475                 rect->width.computed = MAX(w_orig, 0);
476             }
478         }
480         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
482     } else {
483         // move freely
484         rect->x.computed = MIN(s[NR::X], opposite_x);
485         rect->width.computed = MAX(w_orig - minx, 0);
486         rect->y.computed = MIN(s[NR::Y], opposite_y);
487         rect->height.computed = MAX(h_orig - miny, 0);
488         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
489     }
491     sp_rect_clamp_radii(rect);
493     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
496 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
498     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
500     sp_knot_holder_add_full(
501       knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
502       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
503       _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
504         "radius the same"));
506     sp_knot_holder_add_full(
507       knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
508       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
509       _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
510         "radius the same")
511       );
513     sp_knot_holder_add_full(
514       knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
515       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
516       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
517         "or stretch in one dimension only")
518       );
520     sp_knot_holder_add_full(
521       knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
522       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
523       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
524         "or stretch in one dimension only")
525       );
527     sp_pat_knot_holder(item, knot_holder);
528     return knot_holder;
531 /* 3D Box */
533 static inline Box3D::Axis movement_axis_of_3dbox_corner (guint corner, guint state)
535     // this function has the purpose to simplify a change in the resizing behaviour of boxes
536     switch (corner) {
537         case 0:
538         case 1:
539         case 2:
540         case 3:
541             return ((state & GDK_SHIFT_MASK) ? Box3D::Z : Box3D::XY);
542         case 4:
543         case 5:
544         case 6:
545         case 7:
546             return ((state & GDK_SHIFT_MASK) ? Box3D::XY : Box3D::Z);
547     }
548     return Box3D::NONE;
551 /*
552  * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
553  * their intersection, we remember the last snapped line and keep snapping to this specific line as long
554  * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
555  */
557 // Should we make the threshold settable in the preferences?
558 static double remember_snap_threshold = 30;
559 static guint remember_snap_index = 0;
560 static guint remember_snap_index_center = 0;
562 static NR::Point snap_knot_position_3dbox (SP3DBox *box, guint corner, Box3D::Axis direction, NR::Point const &origin, NR::Point const &p, guint /*state*/)
564     SPDesktop * desktop = inkscape_active_desktop();
565     Box3D::Perspective3D *persp = sp_desktop_document (desktop)->get_persp_of_box (box);
567     if (is_single_axis_direction (direction)) return p;
569     Box3D::Axis axis1 = Box3D::extract_first_axis_direction (direction);
570     Box3D::Axis axis2 = Box3D::extract_second_axis_direction (direction);
572     NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (box)));
573     NR::Point origin_dt = origin * i2d;
574     NR::Point p_dt = p * i2d;
576     Box3D::PerspectiveLine pl1 (origin_dt, axis1, persp);
577     Box3D::PerspectiveLine pl2 (origin_dt, axis2, persp);
578     Box3D::Line diag1 (origin_dt, box->corners[corner ^ Box3D::XY]);
580     int num_snap_lines = 3;
581     NR::Point snap_pts[num_snap_lines];
583     snap_pts[0] = pl1.closest_to (p_dt);
584     snap_pts[1] = pl2.closest_to (p_dt);
585     snap_pts[2] = diag1.closest_to (p_dt);
587     gdouble const zoom = desktop->current_zoom();
589     double snap_dists[num_snap_lines];
591     for (int i = 0; i < num_snap_lines; ++i) {
592         snap_dists[i] = NR::L2 (snap_pts[i] - p_dt) * zoom;
593     }
595     bool within_tolerance = true;
596     for (int i = 0; i < num_snap_lines; ++i) {
597         if (snap_dists[i] > remember_snap_threshold) {
598             within_tolerance = false;
599             break;
600         }
601     }
603     int snap_index = -1;
604     double snap_dist = NR_HUGE;
605     for (int i = 0; i < num_snap_lines; ++i) {
606         if (snap_dists[i] < snap_dist) {
607             snap_index = i;
608             snap_dist = snap_dists[i];
609         }
610     }
612     if (within_tolerance) {
613         return snap_pts[remember_snap_index] * i2d.inverse();
614     } else {
615         remember_snap_index = snap_index;
616         return snap_pts[snap_index] * i2d.inverse();
617     }
620 static NR::Point snap_center_position_3dbox (SP3DBox *box, NR::Point const &origin, NR::Point const &p)
622     SPDesktop * desktop = inkscape_active_desktop();
623     Box3D::Perspective3D *persp = sp_desktop_document (desktop)->get_persp_of_box (box);
625     Box3D::Axis axis1 = Box3D::X;
626     Box3D::Axis axis2 = Box3D::Y;
628     NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (box)));
629     NR::Point origin_dt = origin * i2d;
630     NR::Point p_dt = p * i2d;
632     Box3D::PerspectiveLine pl1 (origin_dt, axis1, persp);
633     Box3D::PerspectiveLine pl2 (origin_dt, axis2, persp);
634     NR::Point midpt1 = sp_3dbox_get_midpoint_in_axis_direction (box->old_corner1, box->old_corner5, Box3D::Z, persp);
635     NR::Point midpt2 = sp_3dbox_get_midpoint_in_axis_direction (box->old_corner3, box->old_corner7, Box3D::Z, persp);
636     Box3D::Line diag1 (origin_dt, midpt1);
637     Box3D::Line diag2 (origin_dt, midpt2);
639     int num_snap_lines = 4;
640     NR::Point snap_pts[num_snap_lines];
642     // should we snap to the closest point or to the projection along perspective lines?
643     snap_pts[0] = pl1.closest_to (p_dt);
644     snap_pts[1] = pl2.closest_to (p_dt);
645     snap_pts[2] = diag1.closest_to (p_dt);
646     snap_pts[3] = diag2.closest_to (p_dt);
648     gdouble const zoom = desktop->current_zoom();
650     double snap_dists[num_snap_lines];
652     for (int i = 0; i < num_snap_lines; ++i) {
653         snap_dists[i] = NR::L2 (snap_pts[i] - p_dt) * zoom;
654     }
656     bool within_tolerance = true;
657     for (int i = 0; i < num_snap_lines; ++i) {
658         if (snap_dists[i] > remember_snap_threshold) {
659             within_tolerance = false;
660             break;
661         }
662     }
664     int snap_index = -1;
665     double snap_dist = NR_HUGE;
666     for (int i = 0; i < num_snap_lines; ++i) {
667         if (snap_dists[i] < snap_dist) {
668             snap_index = i;
669             snap_dist = snap_dists[i];
670         }
671     }
673     if (within_tolerance) {
674         return snap_pts[remember_snap_index_center] * i2d.inverse();
675     } else {
676         remember_snap_index_center = snap_index;
677         return snap_pts[snap_index] * i2d.inverse();
678     }
681 static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
683     g_assert(item != NULL);
684     SP3DBox *box = SP_3DBOX(item);
686     NR::Matrix const i2d (sp_item_i2d_affine (item));
687     return sp_3dbox_get_corner(box, knot_id) * i2d;
690 static void sp_3dbox_knot_set(SPItem *item, guint knot_id, NR::Point const &new_pos, NR::Point const &origin, guint state)
692     g_assert(item != NULL);
693     SP3DBox *box = SP_3DBOX(item);
695     NR::Matrix const i2d (sp_item_i2d_affine (item));
696     Box3D::Axis direction = movement_axis_of_3dbox_corner (knot_id, state);
697     if ((state & GDK_CONTROL_MASK) && !is_single_axis_direction (direction)) {
698         // snap if Ctrl is pressed and movement isn't already constrained to a single axis
699         NR::Point const s = snap_knot_position_3dbox (box, knot_id, direction, origin, new_pos, state);
700         sp_3dbox_move_corner_in_Z_direction (box, knot_id, s * i2d, false);
701     } else {
702         if (direction == Box3D::Z) {
703             sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, true);
704         } else {
705             sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, false);
706         }
707     }
708     sp_3dbox_update_curves (box);
709     sp_3dbox_set_ratios (box);
710     sp_3dbox_update_perspective_lines ();
711     sp_3dbox_set_z_orders_later_on (box);
714 static void sp_3dbox_knot_center_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
716     SP3DBox *box = SP_3DBOX(item);
718     NR::Matrix const i2d (sp_item_i2d_affine (item));
719     NR::Point new_pt (new_pos);
721     if ((state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
722         // snap if Ctrl is pressed and movement isn't already constrained to a single axis
723         new_pt = snap_center_position_3dbox (box, origin, new_pos);
724     }
726     if (state & GDK_SHIFT_MASK) {
727         sp_3dbox_recompute_Z_corners_from_new_center (box, new_pt * i2d);
728     } else {
729         sp_3dbox_recompute_XY_corners_from_new_center (box, new_pt * i2d);
730     }
732     sp_3dbox_update_curves (box);
733     sp_3dbox_set_z_orders_later_on (box);
736 static NR::Point sp_3dbox_knot_center_get(SPItem *item)
738     NR::Maybe<NR::Point> center = sp_3dbox_get_center(SP_3DBOX(item));
739     if (!center) return NR::Point (0, 0);
740     NR::Matrix const i2d (sp_item_i2d_affine (item));
741     return (*center) * i2d;
744 static void sp_3dbox_knot0_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
746     sp_3dbox_knot_set(item, 0, new_pos, origin, state);
749 static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
751     sp_3dbox_knot_set(item, 1, new_pos, origin, state);
754 static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
756     sp_3dbox_knot_set(item, 2, new_pos, origin, state);
759 static void sp_3dbox_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
761     sp_3dbox_knot_set(item, 3, new_pos, origin, state);
764 static void sp_3dbox_knot4_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
766     sp_3dbox_knot_set(item, 4, new_pos, origin, state);
769 static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
771     sp_3dbox_knot_set(item, 5, new_pos, origin, state);
774 static void sp_3dbox_knot6_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
776     sp_3dbox_knot_set(item, 6, new_pos, origin, state);
779 static void sp_3dbox_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
781     sp_3dbox_knot_set(item, 7, new_pos, origin, state);
784 static NR::Point sp_3dbox_knot0_get(SPItem *item)
786     return sp_3dbox_knot_get(item, 0);
789 static NR::Point sp_3dbox_knot1_get(SPItem *item)
791     return sp_3dbox_knot_get(item, 1);
794 static NR::Point sp_3dbox_knot2_get(SPItem *item)
796     return sp_3dbox_knot_get(item, 2);
799 static NR::Point sp_3dbox_knot3_get(SPItem *item)
801     return sp_3dbox_knot_get(item, 3);
804 static NR::Point sp_3dbox_knot4_get(SPItem *item)
806     return sp_3dbox_knot_get(item, 4);
809 static NR::Point sp_3dbox_knot5_get(SPItem *item)
811     return sp_3dbox_knot_get(item, 5);
814 static NR::Point sp_3dbox_knot6_get(SPItem *item)
816     return sp_3dbox_knot_get(item, 6);
819 static NR::Point sp_3dbox_knot7_get(SPItem *item)
821     return sp_3dbox_knot_get(item, 7);
825 //static
826 SPKnotHolder *
827 sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop)
829     g_assert(item != NULL);
830     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
832     sp_knot_holder_add(knot_holder, sp_3dbox_knot0_set, sp_3dbox_knot0_get, NULL,
833                        _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
834     sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,
835                        _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
836     sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,
837                        _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
838     sp_knot_holder_add(knot_holder, sp_3dbox_knot3_set, sp_3dbox_knot3_get, NULL,
839                        _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
840     sp_knot_holder_add(knot_holder, sp_3dbox_knot4_set, sp_3dbox_knot4_get, NULL,
841                        _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
842     sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,
843                        _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
844     sp_knot_holder_add(knot_holder, sp_3dbox_knot6_set, sp_3dbox_knot6_get, NULL,
845                        _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
846     sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set, sp_3dbox_knot7_get, NULL,
847                        _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
849     // center dragging
850     sp_knot_holder_add_full(knot_holder, sp_3dbox_knot_center_set, sp_3dbox_knot_center_get, NULL,
851                             SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,_("Move the box in perspective."));
853     sp_pat_knot_holder(item, knot_holder);
855     return knot_holder;
858 /* SPArc */
860 /*
861  * return values:
862  *   1  : inside
863  *   0  : on the curves
864  *   -1 : outside
865  */
866 static gint
867 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
869     gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
870     gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
872     gdouble s = dx * dx + dy * dy;
873     if (s < 1.0) return 1;
874     if (s > 1.0) return -1;
875     return 0;
878 static void
879 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
881     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
883     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
884     SPArc *arc = SP_ARC(item);
886     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
888     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
889     NR::scale sc(ge->rx.computed, ge->ry.computed);
890     ge->start = atan2(delta * sc.inverse());
891     if ( ( state & GDK_CONTROL_MASK )
892          && snaps )
893     {
894         ge->start = sp_round(ge->start, M_PI/snaps);
895     }
896     sp_genericellipse_normalize(ge);
897     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
900 static NR::Point sp_arc_start_get(SPItem *item)
902     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
903     SPArc *arc = SP_ARC(item);
905     return sp_arc_get_xy(arc, ge->start);
908 static void
909 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
911     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
913     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
914     SPArc *arc = SP_ARC(item);
916     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
918     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
919     NR::scale sc(ge->rx.computed, ge->ry.computed);
920     ge->end = atan2(delta * sc.inverse());
921     if ( ( state & GDK_CONTROL_MASK )
922          && snaps )
923     {
924         ge->end = sp_round(ge->end, M_PI/snaps);
925     }
926     sp_genericellipse_normalize(ge);
927     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
930 static NR::Point sp_arc_end_get(SPItem *item)
932     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
933     SPArc *arc = SP_ARC(item);
935     return sp_arc_get_xy(arc, ge->end);
938 static void
939 sp_arc_startend_click(SPItem *item, guint state)
941     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
943     if (state & GDK_SHIFT_MASK) {
944         ge->end = ge->start = 0;
945         ((SPObject *)ge)->updateRepr();
946     }
950 static void
951 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
953     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
954     SPArc *arc = SP_ARC(item);
956     NR::Point const s = snap_knot_position(arc, p);
958     ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
960     if ( state & GDK_CONTROL_MASK ) {
961         ge->ry.computed = ge->rx.computed;
962     }
964     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
967 static NR::Point sp_arc_rx_get(SPItem *item)
969     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
971     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(ge->rx.computed, 0));
974 static void
975 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
977     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
978     SPArc *arc = SP_ARC(item);
980     NR::Point const s = snap_knot_position(arc, p);
982     ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
984     if ( state & GDK_CONTROL_MASK ) {
985         ge->rx.computed = ge->ry.computed;
986     }
988     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
991 static NR::Point sp_arc_ry_get(SPItem *item)
993     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
995     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(0, ge->ry.computed));
998 static void
999 sp_arc_rx_click(SPItem *item, guint state)
1001     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
1003     if (state & GDK_CONTROL_MASK) {
1004         ge->ry.computed = ge->rx.computed;
1005         ((SPObject *)ge)->updateRepr();
1006     }
1009 static void
1010 sp_arc_ry_click(SPItem *item, guint state)
1012     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
1014     if (state & GDK_CONTROL_MASK) {
1015         ge->rx.computed = ge->ry.computed;
1016         ((SPObject *)ge)->updateRepr();
1017     }
1020 static SPKnotHolder *
1021 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
1023     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1025     sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
1026                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1027                             _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
1028     sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
1029                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1030                             _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
1031     sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
1032                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1033                             _("Position the <b>start point</b> of the arc or segment; with <b>Ctrl</b> to snap angle; drag <b>inside</b> the ellipse for arc, <b>outside</b> for segment"));
1034     sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
1035                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1036                             _("Position the <b>end point</b> of the arc or segment; with <b>Ctrl</b> to snap angle; drag <b>inside</b> the ellipse for arc, <b>outside</b> for segment"));
1038     sp_pat_knot_holder(item, knot_holder);
1040     return knot_holder;
1043 /* SPStar */
1045 static void
1046 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
1048     SPStar *star = SP_STAR(item);
1050     NR::Point const s = snap_knot_position(star, p);
1052     NR::Point d = s - star->center;
1054     double arg1 = atan2(d);
1055     double darg1 = arg1 - star->arg[0];
1057     if (state & GDK_MOD1_MASK) {
1058         star->randomized = darg1/(star->arg[0] - star->arg[1]);
1059     } else if (state & GDK_SHIFT_MASK) {
1060         star->rounded = darg1/(star->arg[0] - star->arg[1]);
1061     } else if (state & GDK_CONTROL_MASK) {
1062         star->r[0]    = L2(d);
1063     } else {
1064         star->r[0]    = L2(d);
1065         star->arg[0]  = arg1;
1066         star->arg[1] += darg1;
1067     }
1068     ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1071 static void
1072 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
1074     SPStar *star = SP_STAR(item);
1076     NR::Point const s = snap_knot_position(star, p);
1078     if (star->flatsided == false) {
1079         NR::Point d = s - star->center;
1081         double arg1 = atan2(d);
1082         double darg1 = arg1 - star->arg[1];
1084         if (state & GDK_MOD1_MASK) {
1085             star->randomized = darg1/(star->arg[0] - star->arg[1]);
1086         } else if (state & GDK_SHIFT_MASK) {
1087             star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
1088         } else if (state & GDK_CONTROL_MASK) {
1089             star->r[1]   = L2(d);
1090             star->arg[1] = star->arg[0] + M_PI / star->sides;
1091         }
1092         else {
1093             star->r[1]   = L2(d);
1094             star->arg[1] = atan2(d);
1095         }
1096         ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1097     }
1100 static NR::Point sp_star_knot1_get(SPItem *item)
1102     g_assert(item != NULL);
1104     SPStar *star = SP_STAR(item);
1106     return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
1110 static NR::Point sp_star_knot2_get(SPItem *item)
1112     g_assert(item != NULL);
1114     SPStar *star = SP_STAR(item);
1116     return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
1119 static void
1120 sp_star_knot_click(SPItem *item, guint state)
1122     SPStar *star = SP_STAR(item);
1124     if (state & GDK_MOD1_MASK) {
1125         star->randomized = 0;
1126         ((SPObject *)star)->updateRepr();
1127     } else if (state & GDK_SHIFT_MASK) {
1128         star->rounded = 0;
1129         ((SPObject *)star)->updateRepr();
1130     } else if (state & GDK_CONTROL_MASK) {
1131         star->arg[1] = star->arg[0] + M_PI / star->sides;
1132         ((SPObject *)star)->updateRepr();
1133     }
1136 static SPKnotHolder *
1137 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
1139     /* we don't need to get parent knot_holder */
1140     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1141     g_assert(item != NULL);
1143     SPStar *star = SP_STAR(item);
1145     sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
1146                        _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1147     if (star->flatsided == false)
1148         sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
1149                            _("Adjust the <b>base radius</b> of the star; with <b>Ctrl</b> to keep star rays radial (no skew); with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1151     sp_pat_knot_holder(item, knot_holder);
1153     return knot_holder;
1156 /* SPSpiral */
1158 /*
1159  * set attributes via inner (t=t0) knot point:
1160  *   [default] increase/decrease inner point
1161  *   [shift]   increase/decrease inner and outer arg synchronizely
1162  *   [control] constrain inner arg to round per PI/4
1163  */
1164 static void
1165 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
1167     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1169     SPSpiral *spiral = SP_SPIRAL(item);
1171     gdouble   dx = p[NR::X] - spiral->cx;
1172     gdouble   dy = p[NR::Y] - spiral->cy;
1174     if (state & GDK_MOD1_MASK) {
1175         // adjust divergence by vertical drag, relative to rad
1176         double new_exp = (spiral->rad + dy)/(spiral->rad);
1177         spiral->exp = new_exp > 0? new_exp : 0;
1178     } else {
1179         // roll/unroll from inside
1180         gdouble   arg_t0;
1181         sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
1183         gdouble   arg_tmp = atan2(dy, dx) - arg_t0;
1184         gdouble   arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
1185         spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
1187         /* round inner arg per PI/snaps, if CTRL is pressed */
1188         if ( ( state & GDK_CONTROL_MASK )
1189              && ( fabs(spiral->revo) > SP_EPSILON_2 )
1190              && ( snaps != 0 ) ) {
1191             gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
1192             spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
1193         }
1195         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1196     }
1198     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1201 /*
1202  * set attributes via outer (t=1) knot point:
1203  *   [default] increase/decrease revolution factor
1204  *   [control] constrain inner arg to round per PI/4
1205  */
1206 static void
1207 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
1209     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1211     SPSpiral *spiral = SP_SPIRAL(item);
1213     gdouble  dx = p[NR::X] - spiral->cx;
1214     gdouble  dy = p[NR::Y] - spiral->cy;
1216     if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
1217         spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
1218         if (!(state & GDK_MOD1_MASK)) {
1219             // if alt not pressed, change also rad; otherwise it is locked
1220             spiral->rad = MAX(hypot(dx, dy), 0.001);
1221         }
1222         if ( ( state & GDK_CONTROL_MASK )
1223              && snaps ) {
1224             spiral->arg = sp_round(spiral->arg, M_PI/snaps);
1225         }
1226     } else { // roll/unroll
1227         // arg of the spiral outer end
1228         double arg_1;
1229         sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
1231         // its fractional part after the whole turns are subtracted
1232         double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
1234         // arg of the mouse point relative to spiral center
1235         double mouse_angle = atan2(dy, dx);
1236         if (mouse_angle < 0)
1237             mouse_angle += 2*M_PI;
1239         // snap if ctrl
1240         if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
1241             mouse_angle = sp_round(mouse_angle, M_PI/snaps);
1242         }
1244         // by how much we want to rotate the outer point
1245         double diff = mouse_angle - arg_r;
1246         if (diff > M_PI)
1247             diff -= 2*M_PI;
1248         else if (diff < -M_PI)
1249             diff += 2*M_PI;
1251         // calculate the new rad;
1252         // the value of t corresponding to the angle arg_1 + diff:
1253         double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
1254         // the rad at that t:
1255         double rad_new = 0;
1256         if (t_temp > spiral->t0)
1257             sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
1259         // change the revo (converting diff from radians to the number of turns)
1260         spiral->revo += diff/(2*M_PI);
1261         if (spiral->revo < 1e-3)
1262             spiral->revo = 1e-3;
1264         // if alt not pressed and the values are sane, change the rad
1265         if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
1266             // adjust t0 too so that the inner point stays unmoved
1267             double r0;
1268             sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
1269             spiral->rad = rad_new;
1270             spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
1271         }
1272         if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
1273         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1274     }
1276     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1279 static NR::Point sp_spiral_inner_get(SPItem *item)
1281     SPSpiral *spiral = SP_SPIRAL(item);
1283     return sp_spiral_get_xy(spiral, spiral->t0);
1286 static NR::Point sp_spiral_outer_get(SPItem *item)
1288     SPSpiral *spiral = SP_SPIRAL(item);
1290     return sp_spiral_get_xy(spiral, 1.0);
1293 static void
1294 sp_spiral_inner_click(SPItem *item, guint state)
1296     SPSpiral *spiral = SP_SPIRAL(item);
1298     if (state & GDK_MOD1_MASK) {
1299         spiral->exp = 1;
1300         ((SPObject *)spiral)->updateRepr();
1301     } else if (state & GDK_SHIFT_MASK) {
1302         spiral->t0 = 0;
1303         ((SPObject *)spiral)->updateRepr();
1304     }
1307 static SPKnotHolder *
1308 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
1310     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1312     sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
1313                        _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
1314     sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
1315                        _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
1317     sp_pat_knot_holder(item, knot_holder);
1319     return knot_holder;
1322 /* SPOffset */
1324 static void
1325 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
1327     SPOffset *offset = SP_OFFSET(item);
1329     offset->rad = sp_offset_distance_to_original(offset, p);
1330     offset->knot = p;
1331     offset->knotSet = true;
1333     ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1337 static NR::Point sp_offset_offset_get(SPItem *item)
1339     SPOffset *offset = SP_OFFSET(item);
1341     NR::Point np;
1342     sp_offset_top_point(offset,&np);
1343     return np;
1346 static SPKnotHolder *
1347 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1349     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1351     sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1352                        _("Adjust the <b>offset distance</b>"));
1354     sp_pat_knot_holder(item, knot_holder);
1356     return knot_holder;
1359 static SPKnotHolder *
1360 sp_misc_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1362     if ((SP_OBJECT(item)->style->fill.isPaintserver())
1363         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1364     {
1365         SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1367         sp_pat_knot_holder(item, knot_holder);
1369         return knot_holder;
1370     }
1371     return NULL;
1374 static void
1375 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1377     if ((SP_OBJECT(item)->style->fill.isPaintserver())
1378         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1379     {
1380         sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1381                                 // TRANSLATORS: This refers to the pattern that's inside the object
1382                                 _("<b>Move</b> the pattern fill inside the object"));
1383         sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1384                                 _("<b>Scale</b> the pattern fill uniformly"));
1385         sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1386                                 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1387     }
1390 static NR::Point sp_flowtext_corner_get(SPItem *item)
1392     SPRect *rect = SP_RECT(item);
1394     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1397 static void
1398 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1400     SPRect *rect = SP_RECT(item);
1402     sp_rect_wh_set_internal(rect, p, origin, state);
1405 static SPKnotHolder *
1406 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1408     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1410     sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1411                        _("Drag to resize the <b>flowed text frame</b>"));
1413     return knot_holder;
1417 /*
1418   Local Variables:
1419   mode:c++
1420   c-file-style:"stroustrup"
1421   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1422   indent-tabs-mode:nil
1423   fill-column:99
1424   End:
1425 */
1426 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :