Code

Snap while dragging knots, for rect and arc
[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 "sp-ellipse.h"
22 #include "sp-star.h"
23 #include "sp-spiral.h"
24 #include "sp-offset.h"
25 #include "sp-flowtext.h"
26 #include "prefs-utils.h"
27 #include "inkscape.h"
28 #include "snap.h"
29 #include "desktop-affine.h"
30 #include <style.h>
31 #include "desktop.h"
32 #include "sp-namedview.h"
34 #include "sp-pattern.h"
35 #include "sp-path.h"
37 #include <glibmm/i18n.h>
39 #include "object-edit.h"
41 #include <libnr/nr-scale-ops.h>
44 #include "xml/repr.h"
46 #include "isnan.h"
48 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
50 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
51 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
52 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
53 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
54 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
55 static SPKnotHolder *sp_path_knot_holder(SPItem *item, SPDesktop *desktop);
56 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
57 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
59 SPKnotHolder *
60 sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
61 {
62     if (SP_IS_RECT(item)) {
63         return sp_rect_knot_holder(item, desktop);
64     } else if (SP_IS_ARC(item)) {
65         return sp_arc_knot_holder(item, desktop);
66     } else if (SP_IS_STAR(item)) {
67         return sp_star_knot_holder(item, desktop);
68     } else if (SP_IS_SPIRAL(item)) {
69         return sp_spiral_knot_holder(item, desktop);
70     } else if (SP_IS_OFFSET(item)) {
71         return sp_offset_knot_holder(item, desktop);
72     } else if (SP_IS_PATH(item)) {
73         return sp_path_knot_holder(item, desktop);
74     } else if (SP_IS_FLOWTEXT(item) && SP_FLOWTEXT(item)->has_internal_frame()) {
75         return sp_flowtext_knot_holder(item, desktop);
76     }
78     return NULL;
79 }
82 /* Pattern manipulation */
84 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
85 {
86     gdouble theta = asin(pat->patternTransform[1] / scale);
87     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
88     return theta;
89 }
91 static gdouble sp_pattern_extract_scale(SPPattern *pat)
92 {
93     gdouble s = pat->patternTransform[1];
94     gdouble c = pat->patternTransform[0];
95     gdouble xscale = sqrt(c * c + s * s);
96     return xscale;
97 }
99 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
101     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
104 static void
105 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
107     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
109     NR::Point p_snapped = p;
111     if ( state & GDK_CONTROL_MASK ) {
112         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
113             p_snapped[NR::Y] = origin[NR::Y];
114         } else {
115             p_snapped[NR::X] = origin[NR::X];
116         }
117     }
119     if (state)  {
120         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
121         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
122     }
124     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
128 static NR::Point sp_pattern_xy_get(SPItem *item)
130     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
131     return sp_pattern_extract_trans(pat);
134 static NR::Point sp_pattern_angle_get(SPItem *item)
136     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
138     gdouble x = (pattern_width(pat)*0.5);
139     gdouble y = 0;
140     NR::Point delta = NR::Point(x,y);
141     gdouble scale = sp_pattern_extract_scale(pat);
142     gdouble theta = sp_pattern_extract_theta(pat, scale);
143     delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
144     delta = delta + sp_pattern_extract_trans(pat);
145     return delta;
148 static void
149 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
151     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
153     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
155     // get the angle from pattern 0,0 to the cursor pos
156     NR::Point delta = p - sp_pattern_extract_trans(pat);
157     gdouble theta = atan2(delta);
159     if ( state & GDK_CONTROL_MASK ) {
160         theta = sp_round(theta, M_PI/snaps);
161     }
163     // get the scale from the current transform so we can keep it.
164     gdouble scl = sp_pattern_extract_scale(pat);
165     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
166     NR::Point const t = sp_pattern_extract_trans(pat);
167     rot[4] = t[NR::X];
168     rot[5] = t[NR::Y];
169     sp_item_adjust_pattern(item, rot, true);
170     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
173 static void
174 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
176     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
178     // Get the scale from the position of the knotholder,
179     NR::Point d = p - sp_pattern_extract_trans(pat);
180     gdouble s = NR::L2(d);
181     gdouble pat_x = pattern_width(pat) * 0.5;
182     gdouble pat_y = pattern_height(pat) * 0.5;
183     gdouble pat_h = hypot(pat_x, pat_y);
184     gdouble scl = s / pat_h;
186     // get angle from current transform, (need get current scale first to calculate angle)
187     gdouble oldscale = sp_pattern_extract_scale(pat);
188     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
190     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
191     NR::Point const t = sp_pattern_extract_trans(pat);
192     rot[4] = t[NR::X];
193     rot[5] = t[NR::Y];
194     sp_item_adjust_pattern(item, rot, true);
195     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
199 static NR::Point sp_pattern_scale_get(SPItem *item)
201     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
203     gdouble x = pattern_width(pat)*0.5;
204     gdouble y = pattern_height(pat)*0.5;
205     NR::Point delta = NR::Point(x,y);
206     NR::Matrix a = pat->patternTransform;
207     a[4] = 0;
208     a[5] = 0;
209     delta = delta * a;
210     delta = delta + sp_pattern_extract_trans(pat);
211     return delta;
214 /* SPRect */
216 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
218     SPDesktop const *desktop = inkscape_active_desktop();
219     NR::Point s = sp_desktop_dt2root_xy_point(desktop, p);
220     SnapManager const &m = desktop->namedview->snap_manager;
221     s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, item).getPoint();
222     return sp_desktop_root2dt_xy_point(desktop, s);
225 static NR::Point sp_rect_rx_get(SPItem *item)
227     SPRect *rect = SP_RECT(item);
229     return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
232 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
234     SPRect *rect = SP_RECT(item);
235     
236     NR::Point const s = snap_knot_position(rect, p);
238     if (state & GDK_CONTROL_MASK) {
239         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
240         rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - s[NR::X], 0.0, temp);
241         rect->rx._set = rect->ry._set = true;
243     } else {
244         rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - s[NR::X], 0.0, rect->width.computed / 2.0);
245         rect->rx._set = true;
246     }
248     ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
252 static NR::Point sp_rect_ry_get(SPItem *item)
254     SPRect *rect = SP_RECT(item);
256     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
259 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
261     SPRect *rect = SP_RECT(item);
262     
263     NR::Point const s = snap_knot_position(rect, p);
265     if (state & GDK_CONTROL_MASK) {
266         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
267         rect->rx.computed = rect->ry.computed = CLAMP(s[NR::Y] - rect->y.computed, 0.0, temp);
268         rect->ry._set = rect->rx._set = true;
269     } else {
270         if (!rect->rx._set || rect->rx.computed == 0) {
271             rect->ry.computed = CLAMP(s[NR::Y] - rect->y.computed,
272                                       0.0,
273                                       MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
274         } else {
275             rect->ry.computed = CLAMP(s[NR::Y] - rect->y.computed,
276                                       0.0,
277                                       rect->height.computed / 2.0);
278         }
280         rect->ry._set = true;
281     }
283     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
286 /**
287  *  Remove rounding from a rectangle.
288  */
289 static void rect_remove_rounding(SPRect *rect)
291     SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
292     SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
295 /**
296  *  Called when the horizontal rounding radius knot is clicked.
297  */
298 static void sp_rect_rx_knot_click(SPItem *item, guint state)
300     SPRect *rect = SP_RECT(item);
302     if (state & GDK_SHIFT_MASK) {
303         rect_remove_rounding(rect);
304     } else if (state & GDK_CONTROL_MASK) {
305         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
306         SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
307     }
310 /**
311  *  Called when the vertical rounding radius knot is clicked.
312  */
313 static void sp_rect_ry_knot_click(SPItem *item, guint state)
315     SPRect *rect = SP_RECT(item);
317     if (state & GDK_SHIFT_MASK) {
318         rect_remove_rounding(rect);
319     } else if (state & GDK_CONTROL_MASK) {
320         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
321         SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
322     }
325 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
327 static void sp_rect_clamp_radii(SPRect *rect)
329     // clamp rounding radii so that they do not exceed width/height
330     if (2 * rect->rx.computed > rect->width.computed) {
331         rect->rx.computed = 0.5 * rect->width.computed;
332         rect->rx._set = true;
333     }
334     if (2 * rect->ry.computed > rect->height.computed) {
335         rect->ry.computed = 0.5 * rect->height.computed;
336         rect->ry._set = true;
337     }
340 static NR::Point sp_rect_wh_get(SPItem *item)
342     SPRect *rect = SP_RECT(item);
344     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
347 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
349     NR::Point const s = snap_knot_position(rect, p);
351     if (state & GDK_CONTROL_MASK) {
352         // original width/height when drag started
353         gdouble const w_orig = (origin[NR::X] - rect->x.computed);
354         gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
356         //original ratio
357         gdouble const ratio = (w_orig / h_orig);
359         // mouse displacement since drag started
360         gdouble const minx = s[NR::X] - origin[NR::X];
361         gdouble const miny = s[NR::Y] - origin[NR::Y];
363         if (fabs(minx) > fabs(miny)) {
365             // snap to horizontal or diagonal
366             rect->width.computed = MAX(w_orig + minx, 0);
367             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
368                 // closer to the diagonal and in same-sign quarters, change both using ratio
369                 rect->height.computed = MAX(h_orig + minx / ratio, 0);
370             } else {
371                 // closer to the horizontal, change only width, height is h_orig
372                 rect->height.computed = MAX(h_orig, 0);
373             }
375         } else {
376             // snap to vertical or diagonal
377             rect->height.computed = MAX(h_orig + miny, 0);
378             if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
379                 // closer to the diagonal and in same-sign quarters, change both using ratio
380                 rect->width.computed = MAX(w_orig + miny * ratio, 0);
381             } else {
382                 // closer to the vertical, change only height, width is w_orig
383                 rect->width.computed = MAX(w_orig, 0);
384             }
385         }
387         rect->width._set = rect->height._set = true;
389     } else {
390         // move freely
391         rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
392         rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
393         rect->width._set = rect->height._set = true;
394     }
396     sp_rect_clamp_radii(rect);
398     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
401 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
403     SPRect *rect = SP_RECT(item);
405     sp_rect_wh_set_internal(rect, p, origin, state);
408 static NR::Point sp_rect_xy_get(SPItem *item)
410     SPRect *rect = SP_RECT(item);
412     return NR::Point(rect->x.computed, rect->y.computed);
415 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
417     SPRect *rect = SP_RECT(item);
419     // opposite corner (unmoved)
420     gdouble opposite_x = (rect->x.computed + rect->width.computed);
421     gdouble opposite_y = (rect->y.computed + rect->height.computed);
423     // original width/height when drag started
424     gdouble w_orig = opposite_x - origin[NR::X];
425     gdouble h_orig = opposite_y - origin[NR::Y];
427     NR::Point const s = snap_knot_position(rect, p);
429     // mouse displacement since drag started
430     gdouble minx = s[NR::X] - origin[NR::X];
431     gdouble miny = s[NR::Y] - origin[NR::Y];
433     if (state & GDK_CONTROL_MASK) {
434         //original ratio
435         gdouble ratio = (w_orig / h_orig);
437         if (fabs(minx) > fabs(miny)) {
439             // snap to horizontal or diagonal
440             rect->x.computed = MIN(s[NR::X], opposite_x);
441             rect->width.computed = MAX(w_orig - minx, 0);
442             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
443                 // closer to the diagonal and in same-sign quarters, change both using ratio
444                 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
445                 rect->height.computed = MAX(h_orig - minx / ratio, 0);
446             } else {
447                 // closer to the horizontal, change only width, height is h_orig
448                 rect->y.computed = MIN(origin[NR::Y], opposite_y);
449                 rect->height.computed = MAX(h_orig, 0);
450             }
452         } else {
454             // snap to vertical or diagonal
455             rect->y.computed = MIN(s[NR::Y], opposite_y);
456             rect->height.computed = MAX(h_orig - miny, 0);
457             if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
458                 // closer to the diagonal and in same-sign quarters, change both using ratio
459                 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
460                 rect->width.computed = MAX(w_orig - miny * ratio, 0);
461             } else {
462                 // closer to the vertical, change only height, width is w_orig
463                 rect->x.computed = MIN(origin[NR::X], opposite_x);
464                 rect->width.computed = MAX(w_orig, 0);
465             }
467         }
469         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
471     } else {
472         // move freely
473         rect->x.computed = MIN(s[NR::X], opposite_x);
474         rect->width.computed = MAX(w_orig - minx, 0);
475         rect->y.computed = MIN(s[NR::Y], opposite_y);
476         rect->height.computed = MAX(h_orig - miny, 0);
477         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
478     }
480     sp_rect_clamp_radii(rect);
482     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
485 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
487     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
489     sp_knot_holder_add_full(
490       knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
491       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
492       _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
493         "radius the same"));
495     sp_knot_holder_add_full(
496       knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
497       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
498       _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
499         "radius the same")
500       );
502     sp_knot_holder_add_full(
503       knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
504       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
505       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
506         "or stretch in one dimension only")
507       );
509     sp_knot_holder_add_full(
510       knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
511       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
512       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
513         "or stretch in one dimension only")
514       );
516     sp_pat_knot_holder(item, knot_holder);
517     return knot_holder;
520 /* SPArc */
522 /*
523  * return values:
524  *   1  : inside
525  *   0  : on the curves
526  *   -1 : outside
527  */
528 static gint
529 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
531     gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
532     gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
534     gdouble s = dx * dx + dy * dy;
535     if (s < 1.0) return 1;
536     if (s > 1.0) return -1;
537     return 0;
540 static void
541 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
543     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
545     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
546     SPArc *arc = SP_ARC(item);
548     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
550     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
551     NR::scale sc(ge->rx.computed, ge->ry.computed);
552     ge->start = atan2(delta * sc.inverse());
553     if ( ( state & GDK_CONTROL_MASK )
554          && snaps )
555     {
556         ge->start = sp_round(ge->start, M_PI/snaps);
557     }
558     sp_genericellipse_normalize(ge);
559     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
562 static NR::Point sp_arc_start_get(SPItem *item)
564     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
565     SPArc *arc = SP_ARC(item);
567     return sp_arc_get_xy(arc, ge->start);
570 static void
571 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
573     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
575     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
576     SPArc *arc = SP_ARC(item);
578     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
580     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
581     NR::scale sc(ge->rx.computed, ge->ry.computed);
582     ge->end = atan2(delta * sc.inverse());
583     if ( ( state & GDK_CONTROL_MASK )
584          && snaps )
585     {
586         ge->end = sp_round(ge->end, M_PI/snaps);
587     }
588     sp_genericellipse_normalize(ge);
589     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
592 static NR::Point sp_arc_end_get(SPItem *item)
594     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
595     SPArc *arc = SP_ARC(item);
597     return sp_arc_get_xy(arc, ge->end);
600 static void
601 sp_arc_startend_click(SPItem *item, guint state)
603     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
605     if (state & GDK_SHIFT_MASK) {
606         ge->end = ge->start = 0;
607         ((SPObject *)ge)->updateRepr();
608     }
612 static void
613 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
615     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
616     SPArc *arc = SP_ARC(item);
617     
618     NR::Point const s = snap_knot_position(arc, p);
620     ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
622     if ( state & GDK_CONTROL_MASK ) {
623         ge->ry.computed = ge->rx.computed;
624     }
626     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
629 static NR::Point sp_arc_rx_get(SPItem *item)
631     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
633     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(ge->rx.computed, 0));
636 static void
637 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
639     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
640     SPArc *arc = SP_ARC(item);
641     
642     NR::Point const s = snap_knot_position(arc, p);
644     ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
646     if ( state & GDK_CONTROL_MASK ) {
647         ge->rx.computed = ge->ry.computed;
648     }
650     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
653 static NR::Point sp_arc_ry_get(SPItem *item)
655     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
657     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(0, ge->ry.computed));
660 static void
661 sp_arc_rx_click(SPItem *item, guint state)
663     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
665     if (state & GDK_CONTROL_MASK) {
666         ge->ry.computed = ge->rx.computed;
667         ((SPObject *)ge)->updateRepr();
668     }
671 static void
672 sp_arc_ry_click(SPItem *item, guint state)
674     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
676     if (state & GDK_CONTROL_MASK) {
677         ge->rx.computed = ge->ry.computed;
678         ((SPObject *)ge)->updateRepr();
679     }
682 static SPKnotHolder *
683 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
685     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
687     sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
688                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
689                             _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
690     sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
691                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
692                             _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
693     sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
694                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
695                             _("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"));
696     sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
697                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
698                             _("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"));
700     sp_pat_knot_holder(item, knot_holder);
702     return knot_holder;
705 /* SPStar */
707 static void
708 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
710     SPStar *star = SP_STAR(item);
712     NR::Point d = p - star->center;
714     double arg1 = atan2(d);
715     double darg1 = arg1 - star->arg[0];
717     if (state & GDK_MOD1_MASK) {
718         star->randomized = darg1/(star->arg[0] - star->arg[1]);
719     } else if (state & GDK_SHIFT_MASK) {
720         star->rounded = darg1/(star->arg[0] - star->arg[1]);
721     } else if (state & GDK_CONTROL_MASK) {
722         star->r[0]    = L2(d);
723     } else {
724         star->r[0]    = L2(d);
725         star->arg[0]  = arg1;
726         star->arg[1] += darg1;
727     }
728     ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
731 static void
732 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
734     SPStar *star = SP_STAR(item);
735     if (star->flatsided == false) {
736         NR::Point d = p - star->center;
738         double arg1 = atan2(d);
739         double darg1 = arg1 - star->arg[1];
741         if (state & GDK_MOD1_MASK) {
742             star->randomized = darg1/(star->arg[0] - star->arg[1]);
743         } else if (state & GDK_SHIFT_MASK) {
744             star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
745         } else if (state & GDK_CONTROL_MASK) {
746             star->r[1]   = L2(d);
747             star->arg[1] = star->arg[0] + M_PI / star->sides;
748         }
749         else {
750             star->r[1]   = L2(d);
751             star->arg[1] = atan2(d);
752         }
753         ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
754     }
757 static NR::Point sp_star_knot1_get(SPItem *item)
759     g_assert(item != NULL);
761     SPStar *star = SP_STAR(item);
763     return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
767 static NR::Point sp_star_knot2_get(SPItem *item)
769     g_assert(item != NULL);
771     SPStar *star = SP_STAR(item);
773     return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
776 static void
777 sp_star_knot_click(SPItem *item, guint state)
779     SPStar *star = SP_STAR(item);
781     if (state & GDK_MOD1_MASK) {
782         star->randomized = 0;
783         ((SPObject *)star)->updateRepr();
784     } else if (state & GDK_SHIFT_MASK) {
785         star->rounded = 0;
786         ((SPObject *)star)->updateRepr();
787     } else if (state & GDK_CONTROL_MASK) {
788         star->arg[1] = star->arg[0] + M_PI / star->sides;
789         ((SPObject *)star)->updateRepr();
790     }
793 static SPKnotHolder *
794 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
796     /* we don't need to get parent knot_holder */
797     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
798     g_assert(item != NULL);
800     SPStar *star = SP_STAR(item);
802     sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
803                        _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
804     if (star->flatsided == false)
805         sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
806                            _("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"));
808     sp_pat_knot_holder(item, knot_holder);
810     return knot_holder;
813 /* SPSpiral */
815 /*
816  * set attributes via inner (t=t0) knot point:
817  *   [default] increase/decrease inner point
818  *   [shift]   increase/decrease inner and outer arg synchronizely
819  *   [control] constrain inner arg to round per PI/4
820  */
821 static void
822 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
824     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
826     SPSpiral *spiral = SP_SPIRAL(item);
828     gdouble   dx = p[NR::X] - spiral->cx;
829     gdouble   dy = p[NR::Y] - spiral->cy;
831     if (state & GDK_MOD1_MASK) {
832         // adjust divergence by vertical drag, relative to rad
833         double new_exp = (spiral->rad + dy)/(spiral->rad);
834         spiral->exp = new_exp > 0? new_exp : 0;
835     } else {
836         // roll/unroll from inside
837         gdouble   arg_t0;
838         sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
840         gdouble   arg_tmp = atan2(dy, dx) - arg_t0;
841         gdouble   arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
842         spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
844         /* round inner arg per PI/snaps, if CTRL is pressed */
845         if ( ( state & GDK_CONTROL_MASK )
846              && ( fabs(spiral->revo) > SP_EPSILON_2 )
847              && ( snaps != 0 ) ) {
848             gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
849             spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
850         }
852         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
853     }
855     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
858 /*
859  * set attributes via outer (t=1) knot point:
860  *   [default] increase/decrease revolution factor
861  *   [control] constrain inner arg to round per PI/4
862  */
863 static void
864 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
866     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
868     SPSpiral *spiral = SP_SPIRAL(item);
870     gdouble  dx = p[NR::X] - spiral->cx;
871     gdouble  dy = p[NR::Y] - spiral->cy;
873     if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
874         spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
875         if (!(state & GDK_MOD1_MASK)) {
876             // if alt not pressed, change also rad; otherwise it is locked
877             spiral->rad = MAX(hypot(dx, dy), 0.001);
878         }
879         if ( ( state & GDK_CONTROL_MASK )
880              && snaps ) {
881             spiral->arg = sp_round(spiral->arg, M_PI/snaps);
882         }
883     } else { // roll/unroll
884         // arg of the spiral outer end
885         double arg_1;
886         sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
888         // its fractional part after the whole turns are subtracted
889         double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
891         // arg of the mouse point relative to spiral center
892         double mouse_angle = atan2(dy, dx);
893         if (mouse_angle < 0)
894             mouse_angle += 2*M_PI;
896         // snap if ctrl
897         if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
898             mouse_angle = sp_round(mouse_angle, M_PI/snaps);
899         }
901         // by how much we want to rotate the outer point
902         double diff = mouse_angle - arg_r;
903         if (diff > M_PI)
904             diff -= 2*M_PI;
905         else if (diff < -M_PI)
906             diff += 2*M_PI;
908         // calculate the new rad;
909         // the value of t corresponding to the angle arg_1 + diff:
910         double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
911         // the rad at that t:
912         double rad_new = 0;
913         if (t_temp > spiral->t0)
914             sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
916         // change the revo (converting diff from radians to the number of turns)
917         spiral->revo += diff/(2*M_PI);
918         if (spiral->revo < 1e-3)
919             spiral->revo = 1e-3;
921         // if alt not pressed and the values are sane, change the rad
922         if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
923             // adjust t0 too so that the inner point stays unmoved
924             double r0;
925             sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
926             spiral->rad = rad_new;
927             spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
928         }
929         if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
930         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
931     }
933     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
936 static NR::Point sp_spiral_inner_get(SPItem *item)
938     SPSpiral *spiral = SP_SPIRAL(item);
940     return sp_spiral_get_xy(spiral, spiral->t0);
943 static NR::Point sp_spiral_outer_get(SPItem *item)
945     SPSpiral *spiral = SP_SPIRAL(item);
947     return sp_spiral_get_xy(spiral, 1.0);
950 static void
951 sp_spiral_inner_click(SPItem *item, guint state)
953     SPSpiral *spiral = SP_SPIRAL(item);
955     if (state & GDK_MOD1_MASK) {
956         spiral->exp = 1;
957         ((SPObject *)spiral)->updateRepr();
958     } else if (state & GDK_SHIFT_MASK) {
959         spiral->t0 = 0;
960         ((SPObject *)spiral)->updateRepr();
961     }
964 static SPKnotHolder *
965 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
967     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
969     sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
970                        _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
971     sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
972                        _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
974     sp_pat_knot_holder(item, knot_holder);
976     return knot_holder;
979 /* SPOffset */
981 static void
982 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
984     SPOffset *offset = SP_OFFSET(item);
986     offset->rad = sp_offset_distance_to_original(offset, p);
987     offset->knot = p;
988     offset->knotSet = true;
990     ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
994 static NR::Point sp_offset_offset_get(SPItem *item)
996     SPOffset *offset = SP_OFFSET(item);
998     NR::Point np;
999     sp_offset_top_point(offset,&np);
1000     return np;
1003 static SPKnotHolder *
1004 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1006     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1008     sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1009                        _("Adjust the <b>offset distance</b>"));
1011     sp_pat_knot_holder(item, knot_holder);
1013     return knot_holder;
1016 static SPKnotHolder *
1017 sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1019     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1020         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1021     {
1022         SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1024         sp_pat_knot_holder(item, knot_holder);
1026         return knot_holder;
1027     }
1028     return NULL;
1031 static void
1032 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1034     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1035         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1036     {
1037         sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1038                                 // TRANSLATORS: This refers to the pattern that's inside the object
1039                                 _("<b>Move</b> the pattern fill inside the object"));
1040         sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1041                                 _("<b>Scale</b> the pattern fill uniformly"));
1042         sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1043                                 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1044     }
1047 static NR::Point sp_flowtext_corner_get(SPItem *item)
1049     SPRect *rect = SP_RECT(item);
1051     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1054 static void
1055 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1057     SPRect *rect = SP_RECT(item);
1059     sp_rect_wh_set_internal(rect, p, origin, state);
1062 static SPKnotHolder *
1063 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1065     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1067     sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1068                        _("Drag to resize the <b>flowed text frame</b>"));
1070     return knot_holder;
1074 /*
1075   Local Variables:
1076   mode:c++
1077   c-file-style:"stroustrup"
1078   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1079   indent-tabs-mode:nil
1080   fill-column:99
1081   End:
1082 */
1083 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :