Code

Enable center-dragging of boxes in Z direction by using Shift
[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);
242     
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);
271     
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 = 20;
559 static guint remember_snap_index = 0;
561 static NR::Point snap_knot_position_3dbox (SP3DBox *box, guint corner, Box3D::Axis direction, NR::Point const &origin, NR::Point const &p, guint state)
563     SPDesktop * desktop = inkscape_active_desktop();
564     Box3D::Perspective3D *persp = sp_desktop_document (desktop)->get_persp_of_box (box);
566     g_return_val_if_fail (!is_single_axis_direction (direction), p);
568     Box3D::Axis axis1 = Box3D::extract_first_axis_direction (direction);
569     Box3D::Axis axis2 = Box3D::extract_second_axis_direction (direction);
571     NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (box)));
572     NR::Point origin_dt = origin * i2d;
573     NR::Point p_dt = p * i2d;
575     Box3D::PerspectiveLine pl1 (origin_dt, axis1, persp);
576     Box3D::PerspectiveLine pl2 (origin_dt, axis2, persp);
577     Box3D::Line diag1 (origin_dt, box->corners[corner ^ Box3D::XY]);
579     int num_snap_lines = 3;
580     NR::Point snap_pts[num_snap_lines];
582     snap_pts[0] = pl1.closest_to (p_dt);
583     snap_pts[1] = pl2.closest_to (p_dt);
584     snap_pts[2] = diag1.closest_to (p_dt);
586     gdouble const zoom = desktop->current_zoom();
588     double snap_dists[num_snap_lines];
590     for (int i = 0; i < num_snap_lines; ++i) {
591         snap_dists[i] = NR::L2 (snap_pts[i] - p_dt) * zoom;
592     }
594     bool within_tolerance = true;
595     for (int i = 0; i < num_snap_lines; ++i) {
596         if (snap_dists[i] > remember_snap_threshold) {
597             within_tolerance = false;
598             break;
599         }
600     }
602     int snap_index = -1;
603     double snap_dist = NR_HUGE;
604     for (int i = 0; i < num_snap_lines; ++i) {
605         if (snap_dists[i] < snap_dist) {
606             snap_index = i;
607             snap_dist = snap_dists[i];
608         }
609     }
611     if (within_tolerance) {
612         return snap_pts[remember_snap_index] * i2d.inverse();
613     } else {
614         remember_snap_index = snap_index;
615         return snap_pts[snap_index] * i2d.inverse();
616     }
619 static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
621     g_assert(item != NULL);
622     SP3DBox *box = SP_3DBOX(item);
624     NR::Matrix const i2d (sp_item_i2d_affine (item));
625     return sp_3dbox_get_corner(box, knot_id) * i2d;
628 static void sp_3dbox_knot_set(SPItem *item, guint knot_id, NR::Point const &new_pos, NR::Point const &origin, guint state)
630     g_assert(item != NULL);
631     SP3DBox *box = SP_3DBOX(item);
633     NR::Matrix const i2d (sp_item_i2d_affine (item));
634     Box3D::Axis direction = movement_axis_of_3dbox_corner (knot_id, state);
635     if ((state & GDK_CONTROL_MASK) && !is_single_axis_direction (direction)) {
636         // snap if Ctrl is pressed and movement isn't already constrained to a single axis
637         NR::Point const s = snap_knot_position_3dbox (box, knot_id, direction, origin, new_pos, state);
638         sp_3dbox_move_corner_in_Z_direction (box, knot_id, s * i2d, false);
639     } else {
640         if (direction == Box3D::Z) {
641             sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, true);
642         } else {
643             sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, false);
644         }
645     }
646     sp_3dbox_update_curves (box);
647     sp_3dbox_set_ratios (box);
648     sp_3dbox_update_perspective_lines ();
649     sp_3dbox_set_z_orders (box);
652 static void sp_3dbox_knot_center_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
654     NR::Matrix const i2d (sp_item_i2d_affine (item));
655     if (state & GDK_SHIFT_MASK) {
656         sp_3dbox_recompute_Z_corners_from_new_center (SP_3DBOX (item), new_pos * i2d);
657     } else {
658         sp_3dbox_recompute_XY_corners_from_new_center (SP_3DBOX (item), new_pos * i2d);
659     }
661     sp_3dbox_update_curves (SP_3DBOX(item));
664 static NR::Point sp_3dbox_knot_center_get(SPItem *item)
666     NR::Maybe<NR::Point> center = sp_3dbox_get_center(SP_3DBOX(item));
667     if (!center) return NR::Point (0, 0);
668     NR::Matrix const i2d (sp_item_i2d_affine (item));
669     return (*center) * i2d;
672 static void sp_3dbox_knot0_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
674     sp_3dbox_knot_set(item, 0, new_pos, origin, state);
677 static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
679     sp_3dbox_knot_set(item, 1, new_pos, origin, state);
682 static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
684     sp_3dbox_knot_set(item, 2, new_pos, origin, state);
687 static void sp_3dbox_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
689     sp_3dbox_knot_set(item, 3, new_pos, origin, state);
692 static void sp_3dbox_knot4_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
694     sp_3dbox_knot_set(item, 4, new_pos, origin, state);
697 static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
699     sp_3dbox_knot_set(item, 5, new_pos, origin, state);
702 static void sp_3dbox_knot6_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
704     sp_3dbox_knot_set(item, 6, new_pos, origin, state);
707 static void sp_3dbox_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
709     sp_3dbox_knot_set(item, 7, new_pos, origin, state);
712 static NR::Point sp_3dbox_knot0_get(SPItem *item)
714     return sp_3dbox_knot_get(item, 0);
717 static NR::Point sp_3dbox_knot1_get(SPItem *item)
719     return sp_3dbox_knot_get(item, 1);
722 static NR::Point sp_3dbox_knot2_get(SPItem *item)
724     return sp_3dbox_knot_get(item, 2);
727 static NR::Point sp_3dbox_knot3_get(SPItem *item)
729     return sp_3dbox_knot_get(item, 3);
732 static NR::Point sp_3dbox_knot4_get(SPItem *item)
734     return sp_3dbox_knot_get(item, 4);
737 static NR::Point sp_3dbox_knot5_get(SPItem *item)
739     return sp_3dbox_knot_get(item, 5);
742 static NR::Point sp_3dbox_knot6_get(SPItem *item)
744     return sp_3dbox_knot_get(item, 6);
747 static NR::Point sp_3dbox_knot7_get(SPItem *item)
749     return sp_3dbox_knot_get(item, 7);
753 //static
754 SPKnotHolder *
755 sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop)
757     g_assert(item != NULL);
758     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
760     sp_knot_holder_add(knot_holder, sp_3dbox_knot0_set, sp_3dbox_knot0_get, NULL,
761                        _("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"));
762     sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,
763                        _("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"));
764     sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,
765                        _("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"));
766     sp_knot_holder_add(knot_holder, sp_3dbox_knot3_set, sp_3dbox_knot3_get, NULL,
767                        _("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"));
768     sp_knot_holder_add(knot_holder, sp_3dbox_knot4_set, sp_3dbox_knot4_get, NULL,
769                        _("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"));
770     sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,
771                        _("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"));
772     sp_knot_holder_add(knot_holder, sp_3dbox_knot6_set, sp_3dbox_knot6_get, NULL,
773                        _("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"));
774     sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set, sp_3dbox_knot7_get, NULL,
775                        _("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"));
777     // center dragging
778     sp_knot_holder_add_full(knot_holder, sp_3dbox_knot_center_set, sp_3dbox_knot_center_get, NULL,
779                             SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,_("Move the box in perspective."));
781     sp_pat_knot_holder(item, knot_holder);
783     return knot_holder;
786 /* SPArc */
788 /*
789  * return values:
790  *   1  : inside
791  *   0  : on the curves
792  *   -1 : outside
793  */
794 static gint
795 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
797     gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
798     gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
800     gdouble s = dx * dx + dy * dy;
801     if (s < 1.0) return 1;
802     if (s > 1.0) return -1;
803     return 0;
806 static void
807 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
809     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
811     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
812     SPArc *arc = SP_ARC(item);
814     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
816     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
817     NR::scale sc(ge->rx.computed, ge->ry.computed);
818     ge->start = atan2(delta * sc.inverse());
819     if ( ( state & GDK_CONTROL_MASK )
820          && snaps )
821     {
822         ge->start = sp_round(ge->start, M_PI/snaps);
823     }
824     sp_genericellipse_normalize(ge);
825     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
828 static NR::Point sp_arc_start_get(SPItem *item)
830     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
831     SPArc *arc = SP_ARC(item);
833     return sp_arc_get_xy(arc, ge->start);
836 static void
837 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
839     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
841     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
842     SPArc *arc = SP_ARC(item);
844     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
846     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
847     NR::scale sc(ge->rx.computed, ge->ry.computed);
848     ge->end = atan2(delta * sc.inverse());
849     if ( ( state & GDK_CONTROL_MASK )
850          && snaps )
851     {
852         ge->end = sp_round(ge->end, M_PI/snaps);
853     }
854     sp_genericellipse_normalize(ge);
855     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
858 static NR::Point sp_arc_end_get(SPItem *item)
860     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
861     SPArc *arc = SP_ARC(item);
863     return sp_arc_get_xy(arc, ge->end);
866 static void
867 sp_arc_startend_click(SPItem *item, guint state)
869     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
871     if (state & GDK_SHIFT_MASK) {
872         ge->end = ge->start = 0;
873         ((SPObject *)ge)->updateRepr();
874     }
878 static void
879 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
881     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
882     SPArc *arc = SP_ARC(item);
883     
884     NR::Point const s = snap_knot_position(arc, p);
886     ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
888     if ( state & GDK_CONTROL_MASK ) {
889         ge->ry.computed = ge->rx.computed;
890     }
892     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
895 static NR::Point sp_arc_rx_get(SPItem *item)
897     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
899     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(ge->rx.computed, 0));
902 static void
903 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
905     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
906     SPArc *arc = SP_ARC(item);
907     
908     NR::Point const s = snap_knot_position(arc, p);
910     ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
912     if ( state & GDK_CONTROL_MASK ) {
913         ge->rx.computed = ge->ry.computed;
914     }
916     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
919 static NR::Point sp_arc_ry_get(SPItem *item)
921     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
923     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(0, ge->ry.computed));
926 static void
927 sp_arc_rx_click(SPItem *item, guint state)
929     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
931     if (state & GDK_CONTROL_MASK) {
932         ge->ry.computed = ge->rx.computed;
933         ((SPObject *)ge)->updateRepr();
934     }
937 static void
938 sp_arc_ry_click(SPItem *item, guint state)
940     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
942     if (state & GDK_CONTROL_MASK) {
943         ge->rx.computed = ge->ry.computed;
944         ((SPObject *)ge)->updateRepr();
945     }
948 static SPKnotHolder *
949 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
951     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
953     sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
954                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
955                             _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
956     sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
957                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
958                             _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
959     sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
960                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
961                             _("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"));
962     sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
963                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
964                             _("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"));
966     sp_pat_knot_holder(item, knot_holder);
968     return knot_holder;
971 /* SPStar */
973 static void
974 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
976     SPStar *star = SP_STAR(item);
977     
978     NR::Point const s = snap_knot_position(star, p);
980     NR::Point d = s - star->center;
982     double arg1 = atan2(d);
983     double darg1 = arg1 - star->arg[0];
985     if (state & GDK_MOD1_MASK) {
986         star->randomized = darg1/(star->arg[0] - star->arg[1]);
987     } else if (state & GDK_SHIFT_MASK) {
988         star->rounded = darg1/(star->arg[0] - star->arg[1]);
989     } else if (state & GDK_CONTROL_MASK) {
990         star->r[0]    = L2(d);
991     } else {
992         star->r[0]    = L2(d);
993         star->arg[0]  = arg1;
994         star->arg[1] += darg1;
995     }
996     ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
999 static void
1000 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1002     SPStar *star = SP_STAR(item);
1003     
1004     NR::Point const s = snap_knot_position(star, p);
1005     
1006     if (star->flatsided == false) {
1007         NR::Point d = s - star->center;
1009         double arg1 = atan2(d);
1010         double darg1 = arg1 - star->arg[1];
1012         if (state & GDK_MOD1_MASK) {
1013             star->randomized = darg1/(star->arg[0] - star->arg[1]);
1014         } else if (state & GDK_SHIFT_MASK) {
1015             star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
1016         } else if (state & GDK_CONTROL_MASK) {
1017             star->r[1]   = L2(d);
1018             star->arg[1] = star->arg[0] + M_PI / star->sides;
1019         }
1020         else {
1021             star->r[1]   = L2(d);
1022             star->arg[1] = atan2(d);
1023         }
1024         ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1025     }
1028 static NR::Point sp_star_knot1_get(SPItem *item)
1030     g_assert(item != NULL);
1032     SPStar *star = SP_STAR(item);
1034     return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
1038 static NR::Point sp_star_knot2_get(SPItem *item)
1040     g_assert(item != NULL);
1042     SPStar *star = SP_STAR(item);
1044     return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
1047 static void
1048 sp_star_knot_click(SPItem *item, guint state)
1050     SPStar *star = SP_STAR(item);
1052     if (state & GDK_MOD1_MASK) {
1053         star->randomized = 0;
1054         ((SPObject *)star)->updateRepr();
1055     } else if (state & GDK_SHIFT_MASK) {
1056         star->rounded = 0;
1057         ((SPObject *)star)->updateRepr();
1058     } else if (state & GDK_CONTROL_MASK) {
1059         star->arg[1] = star->arg[0] + M_PI / star->sides;
1060         ((SPObject *)star)->updateRepr();
1061     }
1064 static SPKnotHolder *
1065 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
1067     /* we don't need to get parent knot_holder */
1068     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1069     g_assert(item != NULL);
1071     SPStar *star = SP_STAR(item);
1073     sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
1074                        _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1075     if (star->flatsided == false)
1076         sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
1077                            _("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"));
1079     sp_pat_knot_holder(item, knot_holder);
1081     return knot_holder;
1084 /* SPSpiral */
1086 /*
1087  * set attributes via inner (t=t0) knot point:
1088  *   [default] increase/decrease inner point
1089  *   [shift]   increase/decrease inner and outer arg synchronizely
1090  *   [control] constrain inner arg to round per PI/4
1091  */
1092 static void
1093 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1095     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1097     SPSpiral *spiral = SP_SPIRAL(item);
1099     gdouble   dx = p[NR::X] - spiral->cx;
1100     gdouble   dy = p[NR::Y] - spiral->cy;
1102     if (state & GDK_MOD1_MASK) {
1103         // adjust divergence by vertical drag, relative to rad
1104         double new_exp = (spiral->rad + dy)/(spiral->rad);
1105         spiral->exp = new_exp > 0? new_exp : 0;
1106     } else {
1107         // roll/unroll from inside
1108         gdouble   arg_t0;
1109         sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
1111         gdouble   arg_tmp = atan2(dy, dx) - arg_t0;
1112         gdouble   arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
1113         spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
1115         /* round inner arg per PI/snaps, if CTRL is pressed */
1116         if ( ( state & GDK_CONTROL_MASK )
1117              && ( fabs(spiral->revo) > SP_EPSILON_2 )
1118              && ( snaps != 0 ) ) {
1119             gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
1120             spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
1121         }
1123         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1124     }
1126     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1129 /*
1130  * set attributes via outer (t=1) knot point:
1131  *   [default] increase/decrease revolution factor
1132  *   [control] constrain inner arg to round per PI/4
1133  */
1134 static void
1135 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1137     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1139     SPSpiral *spiral = SP_SPIRAL(item);
1141     gdouble  dx = p[NR::X] - spiral->cx;
1142     gdouble  dy = p[NR::Y] - spiral->cy;
1144     if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
1145         spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
1146         if (!(state & GDK_MOD1_MASK)) {
1147             // if alt not pressed, change also rad; otherwise it is locked
1148             spiral->rad = MAX(hypot(dx, dy), 0.001);
1149         }
1150         if ( ( state & GDK_CONTROL_MASK )
1151              && snaps ) {
1152             spiral->arg = sp_round(spiral->arg, M_PI/snaps);
1153         }
1154     } else { // roll/unroll
1155         // arg of the spiral outer end
1156         double arg_1;
1157         sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
1159         // its fractional part after the whole turns are subtracted
1160         double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
1162         // arg of the mouse point relative to spiral center
1163         double mouse_angle = atan2(dy, dx);
1164         if (mouse_angle < 0)
1165             mouse_angle += 2*M_PI;
1167         // snap if ctrl
1168         if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
1169             mouse_angle = sp_round(mouse_angle, M_PI/snaps);
1170         }
1172         // by how much we want to rotate the outer point
1173         double diff = mouse_angle - arg_r;
1174         if (diff > M_PI)
1175             diff -= 2*M_PI;
1176         else if (diff < -M_PI)
1177             diff += 2*M_PI;
1179         // calculate the new rad;
1180         // the value of t corresponding to the angle arg_1 + diff:
1181         double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
1182         // the rad at that t:
1183         double rad_new = 0;
1184         if (t_temp > spiral->t0)
1185             sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
1187         // change the revo (converting diff from radians to the number of turns)
1188         spiral->revo += diff/(2*M_PI);
1189         if (spiral->revo < 1e-3)
1190             spiral->revo = 1e-3;
1192         // if alt not pressed and the values are sane, change the rad
1193         if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
1194             // adjust t0 too so that the inner point stays unmoved
1195             double r0;
1196             sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
1197             spiral->rad = rad_new;
1198             spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
1199         }
1200         if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
1201         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1202     }
1204     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1207 static NR::Point sp_spiral_inner_get(SPItem *item)
1209     SPSpiral *spiral = SP_SPIRAL(item);
1211     return sp_spiral_get_xy(spiral, spiral->t0);
1214 static NR::Point sp_spiral_outer_get(SPItem *item)
1216     SPSpiral *spiral = SP_SPIRAL(item);
1218     return sp_spiral_get_xy(spiral, 1.0);
1221 static void
1222 sp_spiral_inner_click(SPItem *item, guint state)
1224     SPSpiral *spiral = SP_SPIRAL(item);
1226     if (state & GDK_MOD1_MASK) {
1227         spiral->exp = 1;
1228         ((SPObject *)spiral)->updateRepr();
1229     } else if (state & GDK_SHIFT_MASK) {
1230         spiral->t0 = 0;
1231         ((SPObject *)spiral)->updateRepr();
1232     }
1235 static SPKnotHolder *
1236 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
1238     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1240     sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
1241                        _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
1242     sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
1243                        _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
1245     sp_pat_knot_holder(item, knot_holder);
1247     return knot_holder;
1250 /* SPOffset */
1252 static void
1253 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1255     SPOffset *offset = SP_OFFSET(item);
1257     offset->rad = sp_offset_distance_to_original(offset, p);
1258     offset->knot = p;
1259     offset->knotSet = true;
1261     ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1265 static NR::Point sp_offset_offset_get(SPItem *item)
1267     SPOffset *offset = SP_OFFSET(item);
1269     NR::Point np;
1270     sp_offset_top_point(offset,&np);
1271     return np;
1274 static SPKnotHolder *
1275 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1277     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1279     sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1280                        _("Adjust the <b>offset distance</b>"));
1282     sp_pat_knot_holder(item, knot_holder);
1284     return knot_holder;
1287 static SPKnotHolder *
1288 sp_misc_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1290     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1291         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1292     {
1293         SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1295         sp_pat_knot_holder(item, knot_holder);
1297         return knot_holder;
1298     }
1299     return NULL;
1302 static void
1303 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1305     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1306         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1307     {
1308         sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1309                                 // TRANSLATORS: This refers to the pattern that's inside the object
1310                                 _("<b>Move</b> the pattern fill inside the object"));
1311         sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1312                                 _("<b>Scale</b> the pattern fill uniformly"));
1313         sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1314                                 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1315     }
1318 static NR::Point sp_flowtext_corner_get(SPItem *item)
1320     SPRect *rect = SP_RECT(item);
1322     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1325 static void
1326 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1328     SPRect *rect = SP_RECT(item);
1330     sp_rect_wh_set_internal(rect, p, origin, state);
1333 static SPKnotHolder *
1334 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1336     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1338     sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1339                        _("Drag to resize the <b>flowed text frame</b>"));
1341     return knot_holder;
1345 /*
1346   Local Variables:
1347   mode:c++
1348   c-file-style:"stroustrup"
1349   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1350   indent-tabs-mode:nil
1351   fill-column:99
1352   End:
1353 */
1354 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :