Code

Now nodes of transformed items will snap correctly
[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::Matrix const i2d (sp_item_i2d_affine (item));
220     NR::Point s = p * i2d;    
221     SnapManager const &m = desktop->namedview->snap_manager;
222     s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, item).getPoint();
223     return s * i2d.inverse();
226 static NR::Point sp_rect_rx_get(SPItem *item)
228     SPRect *rect = SP_RECT(item);
230     return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
233 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
235     SPRect *rect = SP_RECT(item);
236     
237     //In general we cannot just snap this radius to an arbitrary point, as we have only a single
238     //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
239     //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
241     if (state & GDK_CONTROL_MASK) {
242         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
243         rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
244         rect->rx._set = rect->ry._set = true;
246     } else {
247         rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
248         rect->rx._set = true;
249     }
251     ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
255 static NR::Point sp_rect_ry_get(SPItem *item)
257     SPRect *rect = SP_RECT(item);
259     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
262 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
264     SPRect *rect = SP_RECT(item);
265     
266     //In general we cannot just snap this radius to an arbitrary point, as we have only a single
267     //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
268     //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
270     if (state & GDK_CONTROL_MASK) {
271         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
272         rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
273         rect->ry._set = rect->rx._set = true;
274     } else {
275         if (!rect->rx._set || rect->rx.computed == 0) {
276             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
277                                       0.0,
278                                       MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
279         } else {
280             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
281                                       0.0,
282                                       rect->height.computed / 2.0);
283         }
285         rect->ry._set = true;
286     }
288     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
291 /**
292  *  Remove rounding from a rectangle.
293  */
294 static void rect_remove_rounding(SPRect *rect)
296     SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
297     SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
300 /**
301  *  Called when the horizontal rounding radius knot is clicked.
302  */
303 static void sp_rect_rx_knot_click(SPItem *item, guint state)
305     SPRect *rect = SP_RECT(item);
307     if (state & GDK_SHIFT_MASK) {
308         rect_remove_rounding(rect);
309     } else if (state & GDK_CONTROL_MASK) {
310         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
311         SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
312     }
315 /**
316  *  Called when the vertical rounding radius knot is clicked.
317  */
318 static void sp_rect_ry_knot_click(SPItem *item, guint state)
320     SPRect *rect = SP_RECT(item);
322     if (state & GDK_SHIFT_MASK) {
323         rect_remove_rounding(rect);
324     } else if (state & GDK_CONTROL_MASK) {
325         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
326         SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
327     }
330 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
332 static void sp_rect_clamp_radii(SPRect *rect)
334     // clamp rounding radii so that they do not exceed width/height
335     if (2 * rect->rx.computed > rect->width.computed) {
336         rect->rx.computed = 0.5 * rect->width.computed;
337         rect->rx._set = true;
338     }
339     if (2 * rect->ry.computed > rect->height.computed) {
340         rect->ry.computed = 0.5 * rect->height.computed;
341         rect->ry._set = true;
342     }
345 static NR::Point sp_rect_wh_get(SPItem *item)
347     SPRect *rect = SP_RECT(item);
349     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
352 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
354     NR::Point const s = snap_knot_position(rect, p);
356     if (state & GDK_CONTROL_MASK) {
357         // original width/height when drag started
358         gdouble const w_orig = (origin[NR::X] - rect->x.computed);
359         gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
361         //original ratio
362         gdouble const ratio = (w_orig / h_orig);
364         // mouse displacement since drag started
365         gdouble const minx = s[NR::X] - origin[NR::X];
366         gdouble const miny = s[NR::Y] - origin[NR::Y];
368         if (fabs(minx) > fabs(miny)) {
370             // snap to horizontal or diagonal
371             rect->width.computed = MAX(w_orig + minx, 0);
372             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
373                 // closer to the diagonal and in same-sign quarters, change both using ratio
374                 rect->height.computed = MAX(h_orig + minx / ratio, 0);
375             } else {
376                 // closer to the horizontal, change only width, height is h_orig
377                 rect->height.computed = MAX(h_orig, 0);
378             }
380         } else {
381             // snap to vertical or diagonal
382             rect->height.computed = MAX(h_orig + miny, 0);
383             if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
384                 // closer to the diagonal and in same-sign quarters, change both using ratio
385                 rect->width.computed = MAX(w_orig + miny * ratio, 0);
386             } else {
387                 // closer to the vertical, change only height, width is w_orig
388                 rect->width.computed = MAX(w_orig, 0);
389             }
390         }
392         rect->width._set = rect->height._set = true;
394     } else {
395         // move freely
396         rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
397         rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
398         rect->width._set = rect->height._set = true;
399     }
401     sp_rect_clamp_radii(rect);
403     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
406 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
408     SPRect *rect = SP_RECT(item);
410     sp_rect_wh_set_internal(rect, p, origin, state);
413 static NR::Point sp_rect_xy_get(SPItem *item)
415     SPRect *rect = SP_RECT(item);
417     return NR::Point(rect->x.computed, rect->y.computed);
420 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
422     SPRect *rect = SP_RECT(item);
424     // opposite corner (unmoved)
425     gdouble opposite_x = (rect->x.computed + rect->width.computed);
426     gdouble opposite_y = (rect->y.computed + rect->height.computed);
428     // original width/height when drag started
429     gdouble w_orig = opposite_x - origin[NR::X];
430     gdouble h_orig = opposite_y - origin[NR::Y];
432     NR::Point const s = snap_knot_position(rect, p);
434     // mouse displacement since drag started
435     gdouble minx = s[NR::X] - origin[NR::X];
436     gdouble miny = s[NR::Y] - origin[NR::Y];
438     if (state & GDK_CONTROL_MASK) {
439         //original ratio
440         gdouble ratio = (w_orig / h_orig);
442         if (fabs(minx) > fabs(miny)) {
444             // snap to horizontal or diagonal
445             rect->x.computed = MIN(s[NR::X], opposite_x);
446             rect->width.computed = MAX(w_orig - minx, 0);
447             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
448                 // closer to the diagonal and in same-sign quarters, change both using ratio
449                 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
450                 rect->height.computed = MAX(h_orig - minx / ratio, 0);
451             } else {
452                 // closer to the horizontal, change only width, height is h_orig
453                 rect->y.computed = MIN(origin[NR::Y], opposite_y);
454                 rect->height.computed = MAX(h_orig, 0);
455             }
457         } else {
459             // snap to vertical or diagonal
460             rect->y.computed = MIN(s[NR::Y], opposite_y);
461             rect->height.computed = MAX(h_orig - miny, 0);
462             if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
463                 // closer to the diagonal and in same-sign quarters, change both using ratio
464                 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
465                 rect->width.computed = MAX(w_orig - miny * ratio, 0);
466             } else {
467                 // closer to the vertical, change only height, width is w_orig
468                 rect->x.computed = MIN(origin[NR::X], opposite_x);
469                 rect->width.computed = MAX(w_orig, 0);
470             }
472         }
474         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
476     } else {
477         // move freely
478         rect->x.computed = MIN(s[NR::X], opposite_x);
479         rect->width.computed = MAX(w_orig - minx, 0);
480         rect->y.computed = MIN(s[NR::Y], opposite_y);
481         rect->height.computed = MAX(h_orig - miny, 0);
482         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
483     }
485     sp_rect_clamp_radii(rect);
487     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
490 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
492     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
494     sp_knot_holder_add_full(
495       knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
496       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
497       _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
498         "radius the same"));
500     sp_knot_holder_add_full(
501       knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
502       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
503       _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
504         "radius the same")
505       );
507     sp_knot_holder_add_full(
508       knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
509       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
510       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
511         "or stretch in one dimension only")
512       );
514     sp_knot_holder_add_full(
515       knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
516       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
517       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
518         "or stretch in one dimension only")
519       );
521     sp_pat_knot_holder(item, knot_holder);
522     return knot_holder;
525 /* SPArc */
527 /*
528  * return values:
529  *   1  : inside
530  *   0  : on the curves
531  *   -1 : outside
532  */
533 static gint
534 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
536     gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
537     gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
539     gdouble s = dx * dx + dy * dy;
540     if (s < 1.0) return 1;
541     if (s > 1.0) return -1;
542     return 0;
545 static void
546 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
548     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
550     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
551     SPArc *arc = SP_ARC(item);
553     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
555     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
556     NR::scale sc(ge->rx.computed, ge->ry.computed);
557     ge->start = atan2(delta * sc.inverse());
558     if ( ( state & GDK_CONTROL_MASK )
559          && snaps )
560     {
561         ge->start = sp_round(ge->start, M_PI/snaps);
562     }
563     sp_genericellipse_normalize(ge);
564     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
567 static NR::Point sp_arc_start_get(SPItem *item)
569     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
570     SPArc *arc = SP_ARC(item);
572     return sp_arc_get_xy(arc, ge->start);
575 static void
576 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
578     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
580     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
581     SPArc *arc = SP_ARC(item);
583     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
585     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
586     NR::scale sc(ge->rx.computed, ge->ry.computed);
587     ge->end = atan2(delta * sc.inverse());
588     if ( ( state & GDK_CONTROL_MASK )
589          && snaps )
590     {
591         ge->end = sp_round(ge->end, M_PI/snaps);
592     }
593     sp_genericellipse_normalize(ge);
594     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
597 static NR::Point sp_arc_end_get(SPItem *item)
599     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
600     SPArc *arc = SP_ARC(item);
602     return sp_arc_get_xy(arc, ge->end);
605 static void
606 sp_arc_startend_click(SPItem *item, guint state)
608     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
610     if (state & GDK_SHIFT_MASK) {
611         ge->end = ge->start = 0;
612         ((SPObject *)ge)->updateRepr();
613     }
617 static void
618 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
620     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
621     SPArc *arc = SP_ARC(item);
622     
623     NR::Point const s = snap_knot_position(arc, p);
625     ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
627     if ( state & GDK_CONTROL_MASK ) {
628         ge->ry.computed = ge->rx.computed;
629     }
631     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
634 static NR::Point sp_arc_rx_get(SPItem *item)
636     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
638     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(ge->rx.computed, 0));
641 static void
642 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
644     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
645     SPArc *arc = SP_ARC(item);
646     
647     NR::Point const s = snap_knot_position(arc, p);
649     ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
651     if ( state & GDK_CONTROL_MASK ) {
652         ge->rx.computed = ge->ry.computed;
653     }
655     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
658 static NR::Point sp_arc_ry_get(SPItem *item)
660     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
662     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(0, ge->ry.computed));
665 static void
666 sp_arc_rx_click(SPItem *item, guint state)
668     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
670     if (state & GDK_CONTROL_MASK) {
671         ge->ry.computed = ge->rx.computed;
672         ((SPObject *)ge)->updateRepr();
673     }
676 static void
677 sp_arc_ry_click(SPItem *item, guint state)
679     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
681     if (state & GDK_CONTROL_MASK) {
682         ge->rx.computed = ge->ry.computed;
683         ((SPObject *)ge)->updateRepr();
684     }
687 static SPKnotHolder *
688 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
690     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
692     sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
693                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
694                             _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
695     sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
696                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
697                             _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
698     sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
699                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
700                             _("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"));
701     sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
702                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
703                             _("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"));
705     sp_pat_knot_holder(item, knot_holder);
707     return knot_holder;
710 /* SPStar */
712 static void
713 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
715     SPStar *star = SP_STAR(item);
716     
717     NR::Point const s = snap_knot_position(star, p);
719     NR::Point d = s - star->center;
721     double arg1 = atan2(d);
722     double darg1 = arg1 - star->arg[0];
724     if (state & GDK_MOD1_MASK) {
725         star->randomized = darg1/(star->arg[0] - star->arg[1]);
726     } else if (state & GDK_SHIFT_MASK) {
727         star->rounded = darg1/(star->arg[0] - star->arg[1]);
728     } else if (state & GDK_CONTROL_MASK) {
729         star->r[0]    = L2(d);
730     } else {
731         star->r[0]    = L2(d);
732         star->arg[0]  = arg1;
733         star->arg[1] += darg1;
734     }
735     ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
738 static void
739 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
741     SPStar *star = SP_STAR(item);
742     
743     NR::Point const s = snap_knot_position(star, p);
744     
745     if (star->flatsided == false) {
746         NR::Point d = s - star->center;
748         double arg1 = atan2(d);
749         double darg1 = arg1 - star->arg[1];
751         if (state & GDK_MOD1_MASK) {
752             star->randomized = darg1/(star->arg[0] - star->arg[1]);
753         } else if (state & GDK_SHIFT_MASK) {
754             star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
755         } else if (state & GDK_CONTROL_MASK) {
756             star->r[1]   = L2(d);
757             star->arg[1] = star->arg[0] + M_PI / star->sides;
758         }
759         else {
760             star->r[1]   = L2(d);
761             star->arg[1] = atan2(d);
762         }
763         ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
764     }
767 static NR::Point sp_star_knot1_get(SPItem *item)
769     g_assert(item != NULL);
771     SPStar *star = SP_STAR(item);
773     return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
777 static NR::Point sp_star_knot2_get(SPItem *item)
779     g_assert(item != NULL);
781     SPStar *star = SP_STAR(item);
783     return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
786 static void
787 sp_star_knot_click(SPItem *item, guint state)
789     SPStar *star = SP_STAR(item);
791     if (state & GDK_MOD1_MASK) {
792         star->randomized = 0;
793         ((SPObject *)star)->updateRepr();
794     } else if (state & GDK_SHIFT_MASK) {
795         star->rounded = 0;
796         ((SPObject *)star)->updateRepr();
797     } else if (state & GDK_CONTROL_MASK) {
798         star->arg[1] = star->arg[0] + M_PI / star->sides;
799         ((SPObject *)star)->updateRepr();
800     }
803 static SPKnotHolder *
804 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
806     /* we don't need to get parent knot_holder */
807     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
808     g_assert(item != NULL);
810     SPStar *star = SP_STAR(item);
812     sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
813                        _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
814     if (star->flatsided == false)
815         sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
816                            _("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"));
818     sp_pat_knot_holder(item, knot_holder);
820     return knot_holder;
823 /* SPSpiral */
825 /*
826  * set attributes via inner (t=t0) knot point:
827  *   [default] increase/decrease inner point
828  *   [shift]   increase/decrease inner and outer arg synchronizely
829  *   [control] constrain inner arg to round per PI/4
830  */
831 static void
832 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
834     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
836     SPSpiral *spiral = SP_SPIRAL(item);
838     gdouble   dx = p[NR::X] - spiral->cx;
839     gdouble   dy = p[NR::Y] - spiral->cy;
841     if (state & GDK_MOD1_MASK) {
842         // adjust divergence by vertical drag, relative to rad
843         double new_exp = (spiral->rad + dy)/(spiral->rad);
844         spiral->exp = new_exp > 0? new_exp : 0;
845     } else {
846         // roll/unroll from inside
847         gdouble   arg_t0;
848         sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
850         gdouble   arg_tmp = atan2(dy, dx) - arg_t0;
851         gdouble   arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
852         spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
854         /* round inner arg per PI/snaps, if CTRL is pressed */
855         if ( ( state & GDK_CONTROL_MASK )
856              && ( fabs(spiral->revo) > SP_EPSILON_2 )
857              && ( snaps != 0 ) ) {
858             gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
859             spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
860         }
862         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
863     }
865     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
868 /*
869  * set attributes via outer (t=1) knot point:
870  *   [default] increase/decrease revolution factor
871  *   [control] constrain inner arg to round per PI/4
872  */
873 static void
874 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
876     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
878     SPSpiral *spiral = SP_SPIRAL(item);
880     gdouble  dx = p[NR::X] - spiral->cx;
881     gdouble  dy = p[NR::Y] - spiral->cy;
883     if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
884         spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
885         if (!(state & GDK_MOD1_MASK)) {
886             // if alt not pressed, change also rad; otherwise it is locked
887             spiral->rad = MAX(hypot(dx, dy), 0.001);
888         }
889         if ( ( state & GDK_CONTROL_MASK )
890              && snaps ) {
891             spiral->arg = sp_round(spiral->arg, M_PI/snaps);
892         }
893     } else { // roll/unroll
894         // arg of the spiral outer end
895         double arg_1;
896         sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
898         // its fractional part after the whole turns are subtracted
899         double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
901         // arg of the mouse point relative to spiral center
902         double mouse_angle = atan2(dy, dx);
903         if (mouse_angle < 0)
904             mouse_angle += 2*M_PI;
906         // snap if ctrl
907         if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
908             mouse_angle = sp_round(mouse_angle, M_PI/snaps);
909         }
911         // by how much we want to rotate the outer point
912         double diff = mouse_angle - arg_r;
913         if (diff > M_PI)
914             diff -= 2*M_PI;
915         else if (diff < -M_PI)
916             diff += 2*M_PI;
918         // calculate the new rad;
919         // the value of t corresponding to the angle arg_1 + diff:
920         double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
921         // the rad at that t:
922         double rad_new = 0;
923         if (t_temp > spiral->t0)
924             sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
926         // change the revo (converting diff from radians to the number of turns)
927         spiral->revo += diff/(2*M_PI);
928         if (spiral->revo < 1e-3)
929             spiral->revo = 1e-3;
931         // if alt not pressed and the values are sane, change the rad
932         if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
933             // adjust t0 too so that the inner point stays unmoved
934             double r0;
935             sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
936             spiral->rad = rad_new;
937             spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
938         }
939         if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
940         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
941     }
943     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
946 static NR::Point sp_spiral_inner_get(SPItem *item)
948     SPSpiral *spiral = SP_SPIRAL(item);
950     return sp_spiral_get_xy(spiral, spiral->t0);
953 static NR::Point sp_spiral_outer_get(SPItem *item)
955     SPSpiral *spiral = SP_SPIRAL(item);
957     return sp_spiral_get_xy(spiral, 1.0);
960 static void
961 sp_spiral_inner_click(SPItem *item, guint state)
963     SPSpiral *spiral = SP_SPIRAL(item);
965     if (state & GDK_MOD1_MASK) {
966         spiral->exp = 1;
967         ((SPObject *)spiral)->updateRepr();
968     } else if (state & GDK_SHIFT_MASK) {
969         spiral->t0 = 0;
970         ((SPObject *)spiral)->updateRepr();
971     }
974 static SPKnotHolder *
975 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
977     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
979     sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
980                        _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
981     sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
982                        _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
984     sp_pat_knot_holder(item, knot_holder);
986     return knot_holder;
989 /* SPOffset */
991 static void
992 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
994     SPOffset *offset = SP_OFFSET(item);
996     offset->rad = sp_offset_distance_to_original(offset, p);
997     offset->knot = p;
998     offset->knotSet = true;
1000     ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1004 static NR::Point sp_offset_offset_get(SPItem *item)
1006     SPOffset *offset = SP_OFFSET(item);
1008     NR::Point np;
1009     sp_offset_top_point(offset,&np);
1010     return np;
1013 static SPKnotHolder *
1014 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1016     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1018     sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1019                        _("Adjust the <b>offset distance</b>"));
1021     sp_pat_knot_holder(item, knot_holder);
1023     return knot_holder;
1026 static SPKnotHolder *
1027 sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1029     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1030         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1031     {
1032         SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1034         sp_pat_knot_holder(item, knot_holder);
1036         return knot_holder;
1037     }
1038     return NULL;
1041 static void
1042 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1044     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1045         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1046     {
1047         sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1048                                 // TRANSLATORS: This refers to the pattern that's inside the object
1049                                 _("<b>Move</b> the pattern fill inside the object"));
1050         sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1051                                 _("<b>Scale</b> the pattern fill uniformly"));
1052         sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1053                                 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1054     }
1057 static NR::Point sp_flowtext_corner_get(SPItem *item)
1059     SPRect *rect = SP_RECT(item);
1061     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1064 static void
1065 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1067     SPRect *rect = SP_RECT(item);
1069     sp_rect_wh_set_internal(rect, p, origin, state);
1072 static SPKnotHolder *
1073 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1075     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1077     sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1078                        _("Drag to resize the <b>flowed text frame</b>"));
1080     return knot_holder;
1084 /*
1085   Local Variables:
1086   mode:c++
1087   c-file-style:"stroustrup"
1088   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1089   indent-tabs-mode:nil
1090   fill-column:99
1091   End:
1092 */
1093 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :