Code

Give SPNamedView a SnapManager instance and use it for all management of snapping...
[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 sp_rect_rx_get(SPItem *item)
218     SPRect *rect = SP_RECT(item);
220     return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
223 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
225     SPRect *rect = SP_RECT(item);
227     if (state & GDK_CONTROL_MASK) {
228         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
229         rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
230         rect->rx._set = rect->ry._set = true;
232     } else {
233         rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
234         rect->rx._set = true;
235     }
237     ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
241 static NR::Point sp_rect_ry_get(SPItem *item)
243     SPRect *rect = SP_RECT(item);
245     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
248 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
250     SPRect *rect = SP_RECT(item);
252     if (state & GDK_CONTROL_MASK) {
253         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
254         rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
255         rect->ry._set = rect->rx._set = true;
256     } else {
257         if (!rect->rx._set || rect->rx.computed == 0) {
258             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
259                                       0.0,
260                                       MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
261         } else {
262             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
263                                       0.0,
264                                       rect->height.computed / 2.0);
265         }
267         rect->ry._set = true;
268     }
270     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
273 /**
274  *  Remove rounding from a rectangle.
275  */
276 static void rect_remove_rounding(SPRect *rect)
278     SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
279     SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
282 /**
283  *  Called when the horizontal rounding radius knot is clicked.
284  */
285 static void sp_rect_rx_knot_click(SPItem *item, guint state)
287     SPRect *rect = SP_RECT(item);
289     if (state & GDK_SHIFT_MASK) {
290         rect_remove_rounding(rect);
291     } else if (state & GDK_CONTROL_MASK) {
292         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
293         SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
294     }
297 /**
298  *  Called when the vertical rounding radius knot is clicked.
299  */
300 static void sp_rect_ry_knot_click(SPItem *item, guint state)
302     SPRect *rect = SP_RECT(item);
304     if (state & GDK_SHIFT_MASK) {
305         rect_remove_rounding(rect);
306     } else if (state & GDK_CONTROL_MASK) {
307         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
308         SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
309     }
312 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
314 static void sp_rect_clamp_radii(SPRect *rect)
316     // clamp rounding radii so that they do not exceed width/height
317     if (2 * rect->rx.computed > rect->width.computed) {
318         rect->rx.computed = 0.5 * rect->width.computed;
319         rect->rx._set = true;
320     }
321     if (2 * rect->ry.computed > rect->height.computed) {
322         rect->ry.computed = 0.5 * rect->height.computed;
323         rect->ry._set = true;
324     }
327 static NR::Point sp_rect_wh_get(SPItem *item)
329     SPRect *rect = SP_RECT(item);
331     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
334 static NR::Point rect_snap_knot_position(NR::Point const &p)
336     SPDesktop const *desktop = inkscape_active_desktop();
337     NR::Point s = sp_desktop_dt2root_xy_point(desktop, p);
338     SnapManager const &m = desktop->namedview->snap_manager;
339     s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, NULL).getPoint();
340     return sp_desktop_root2dt_xy_point(desktop, s);
343 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
345     NR::Point const s = rect_snap_knot_position(p);
347     if (state & GDK_CONTROL_MASK) {
348         // original width/height when drag started
349         gdouble const w_orig = (origin[NR::X] - rect->x.computed);
350         gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
352         //original ratio
353         gdouble const ratio = (w_orig / h_orig);
355         // mouse displacement since drag started
356         gdouble const minx = s[NR::X] - origin[NR::X];
357         gdouble const miny = s[NR::Y] - origin[NR::Y];
359         if (fabs(minx) > fabs(miny)) {
361             // snap to horizontal or diagonal
362             rect->width.computed = MAX(w_orig + minx, 0);
363             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
364                 // closer to the diagonal and in same-sign quarters, change both using ratio
365                 rect->height.computed = MAX(h_orig + minx / ratio, 0);
366             } else {
367                 // closer to the horizontal, change only width, height is h_orig
368                 rect->height.computed = MAX(h_orig, 0);
369             }
371         } else {
372             // snap to vertical or diagonal
373             rect->height.computed = MAX(h_orig + miny, 0);
374             if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
375                 // closer to the diagonal and in same-sign quarters, change both using ratio
376                 rect->width.computed = MAX(w_orig + miny * ratio, 0);
377             } else {
378                 // closer to the vertical, change only height, width is w_orig
379                 rect->width.computed = MAX(w_orig, 0);
380             }
381         }
383         rect->width._set = rect->height._set = true;
385     } else {
386         // move freely
387         rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
388         rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
389         rect->width._set = rect->height._set = true;
390     }
392     sp_rect_clamp_radii(rect);
394     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
397 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
399     SPRect *rect = SP_RECT(item);
401     sp_rect_wh_set_internal(rect, p, origin, state);
404 static NR::Point sp_rect_xy_get(SPItem *item)
406     SPRect *rect = SP_RECT(item);
408     return NR::Point(rect->x.computed, rect->y.computed);
411 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
413     SPRect *rect = SP_RECT(item);
415     // opposite corner (unmoved)
416     gdouble opposite_x = (rect->x.computed + rect->width.computed);
417     gdouble opposite_y = (rect->y.computed + rect->height.computed);
419     // original width/height when drag started
420     gdouble w_orig = opposite_x - origin[NR::X];
421     gdouble h_orig = opposite_y - origin[NR::Y];
423     NR::Point const s = rect_snap_knot_position(p);
425     // mouse displacement since drag started
426     gdouble minx = s[NR::X] - origin[NR::X];
427     gdouble miny = s[NR::Y] - origin[NR::Y];
429     if (state & GDK_CONTROL_MASK) {
430         //original ratio
431         gdouble ratio = (w_orig / h_orig);
433         if (fabs(minx) > fabs(miny)) {
435             // snap to horizontal or diagonal
436             rect->x.computed = MIN(s[NR::X], opposite_x);
437             rect->width.computed = MAX(w_orig - minx, 0);
438             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
439                 // closer to the diagonal and in same-sign quarters, change both using ratio
440                 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
441                 rect->height.computed = MAX(h_orig - minx / ratio, 0);
442             } else {
443                 // closer to the horizontal, change only width, height is h_orig
444                 rect->y.computed = MIN(origin[NR::Y], opposite_y);
445                 rect->height.computed = MAX(h_orig, 0);
446             }
448         } else {
450             // snap to vertical or diagonal
451             rect->y.computed = MIN(s[NR::Y], opposite_y);
452             rect->height.computed = MAX(h_orig - miny, 0);
453             if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
454                 // closer to the diagonal and in same-sign quarters, change both using ratio
455                 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
456                 rect->width.computed = MAX(w_orig - miny * ratio, 0);
457             } else {
458                 // closer to the vertical, change only height, width is w_orig
459                 rect->x.computed = MIN(origin[NR::X], opposite_x);
460                 rect->width.computed = MAX(w_orig, 0);
461             }
463         }
465         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
467     } else {
468         // move freely
469         rect->x.computed = MIN(s[NR::X], opposite_x);
470         rect->width.computed = MAX(w_orig - minx, 0);
471         rect->y.computed = MIN(s[NR::Y], opposite_y);
472         rect->height.computed = MAX(h_orig - miny, 0);
473         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
474     }
476     sp_rect_clamp_radii(rect);
478     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
481 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
483     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
485     sp_knot_holder_add_full(
486       knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
487       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
488       _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
489         "radius the same"));
491     sp_knot_holder_add_full(
492       knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
493       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
494       _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
495         "radius the same")
496       );
498     sp_knot_holder_add_full(
499       knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
500       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
501       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
502         "or stretch in one dimension only")
503       );
505     sp_knot_holder_add_full(
506       knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
507       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
508       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
509         "or stretch in one dimension only")
510       );
512     sp_pat_knot_holder(item, knot_holder);
513     return knot_holder;
516 /* SPArc */
518 /*
519  * return values:
520  *   1  : inside
521  *   0  : on the curves
522  *   -1 : outside
523  */
524 static gint
525 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
527     gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
528     gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
530     gdouble s = dx * dx + dy * dy;
531     if (s < 1.0) return 1;
532     if (s > 1.0) return -1;
533     return 0;
536 static void
537 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
539     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
541     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
542     SPArc *arc = SP_ARC(item);
544     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
546     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
547     NR::scale sc(ge->rx.computed, ge->ry.computed);
548     ge->start = atan2(delta * sc.inverse());
549     if ( ( state & GDK_CONTROL_MASK )
550          && snaps )
551     {
552         ge->start = sp_round(ge->start, M_PI/snaps);
553     }
554     sp_genericellipse_normalize(ge);
555     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
558 static NR::Point sp_arc_start_get(SPItem *item)
560     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
561     SPArc *arc = SP_ARC(item);
563     return sp_arc_get_xy(arc, ge->start);
566 static void
567 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
569     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
571     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
572     SPArc *arc = SP_ARC(item);
574     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
576     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
577     NR::scale sc(ge->rx.computed, ge->ry.computed);
578     ge->end = atan2(delta * sc.inverse());
579     if ( ( state & GDK_CONTROL_MASK )
580          && snaps )
581     {
582         ge->end = sp_round(ge->end, M_PI/snaps);
583     }
584     sp_genericellipse_normalize(ge);
585     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
588 static NR::Point sp_arc_end_get(SPItem *item)
590     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
591     SPArc *arc = SP_ARC(item);
593     return sp_arc_get_xy(arc, ge->end);
596 static void
597 sp_arc_startend_click(SPItem *item, guint state)
599     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
601     if (state & GDK_SHIFT_MASK) {
602         ge->end = ge->start = 0;
603         ((SPObject *)ge)->updateRepr();
604     }
608 static void
609 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
611     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
612     SPArc *arc = SP_ARC(item);
614     ge->rx.computed = fabs( ge->cx.computed - p[NR::X] );
616     if ( state & GDK_CONTROL_MASK ) {
617         ge->ry.computed = ge->rx.computed;
618     }
620     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
623 static NR::Point sp_arc_rx_get(SPItem *item)
625     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
627     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(ge->rx.computed, 0));
630 static void
631 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
633     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
634     SPArc *arc = SP_ARC(item);
636     ge->ry.computed = fabs( ge->cy.computed - p[NR::Y] );
638     if ( state & GDK_CONTROL_MASK ) {
639         ge->rx.computed = ge->ry.computed;
640     }
642     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
645 static NR::Point sp_arc_ry_get(SPItem *item)
647     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
649     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(0, ge->ry.computed));
652 static void
653 sp_arc_rx_click(SPItem *item, guint state)
655     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
657     if (state & GDK_CONTROL_MASK) {
658         ge->ry.computed = ge->rx.computed;
659         ((SPObject *)ge)->updateRepr();
660     }
663 static void
664 sp_arc_ry_click(SPItem *item, guint state)
666     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
668     if (state & GDK_CONTROL_MASK) {
669         ge->rx.computed = ge->ry.computed;
670         ((SPObject *)ge)->updateRepr();
671     }
674 static SPKnotHolder *
675 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
677     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
679     sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
680                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
681                             _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
682     sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
683                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
684                             _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
685     sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
686                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
687                             _("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"));
688     sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
689                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
690                             _("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"));
692     sp_pat_knot_holder(item, knot_holder);
694     return knot_holder;
697 /* SPStar */
699 static void
700 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
702     SPStar *star = SP_STAR(item);
704     NR::Point d = p - star->center;
706     double arg1 = atan2(d);
707     double darg1 = arg1 - star->arg[0];
709     if (state & GDK_MOD1_MASK) {
710         star->randomized = darg1/(star->arg[0] - star->arg[1]);
711     } else if (state & GDK_SHIFT_MASK) {
712         star->rounded = darg1/(star->arg[0] - star->arg[1]);
713     } else if (state & GDK_CONTROL_MASK) {
714         star->r[0]    = L2(d);
715     } else {
716         star->r[0]    = L2(d);
717         star->arg[0]  = arg1;
718         star->arg[1] += darg1;
719     }
720     ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
723 static void
724 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
726     SPStar *star = SP_STAR(item);
727     if (star->flatsided == false) {
728         NR::Point d = p - star->center;
730         double arg1 = atan2(d);
731         double darg1 = arg1 - star->arg[1];
733         if (state & GDK_MOD1_MASK) {
734             star->randomized = darg1/(star->arg[0] - star->arg[1]);
735         } else if (state & GDK_SHIFT_MASK) {
736             star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
737         } else if (state & GDK_CONTROL_MASK) {
738             star->r[1]   = L2(d);
739             star->arg[1] = star->arg[0] + M_PI / star->sides;
740         }
741         else {
742             star->r[1]   = L2(d);
743             star->arg[1] = atan2(d);
744         }
745         ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
746     }
749 static NR::Point sp_star_knot1_get(SPItem *item)
751     g_assert(item != NULL);
753     SPStar *star = SP_STAR(item);
755     return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
759 static NR::Point sp_star_knot2_get(SPItem *item)
761     g_assert(item != NULL);
763     SPStar *star = SP_STAR(item);
765     return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
768 static void
769 sp_star_knot_click(SPItem *item, guint state)
771     SPStar *star = SP_STAR(item);
773     if (state & GDK_MOD1_MASK) {
774         star->randomized = 0;
775         ((SPObject *)star)->updateRepr();
776     } else if (state & GDK_SHIFT_MASK) {
777         star->rounded = 0;
778         ((SPObject *)star)->updateRepr();
779     } else if (state & GDK_CONTROL_MASK) {
780         star->arg[1] = star->arg[0] + M_PI / star->sides;
781         ((SPObject *)star)->updateRepr();
782     }
785 static SPKnotHolder *
786 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
788     /* we don't need to get parent knot_holder */
789     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
790     g_assert(item != NULL);
792     SPStar *star = SP_STAR(item);
794     sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
795                        _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
796     if (star->flatsided == false)
797         sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
798                            _("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"));
800     sp_pat_knot_holder(item, knot_holder);
802     return knot_holder;
805 /* SPSpiral */
807 /*
808  * set attributes via inner (t=t0) knot point:
809  *   [default] increase/decrease inner point
810  *   [shift]   increase/decrease inner and outer arg synchronizely
811  *   [control] constrain inner arg to round per PI/4
812  */
813 static void
814 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
816     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
818     SPSpiral *spiral = SP_SPIRAL(item);
820     gdouble   dx = p[NR::X] - spiral->cx;
821     gdouble   dy = p[NR::Y] - spiral->cy;
823     if (state & GDK_MOD1_MASK) {
824         // adjust divergence by vertical drag, relative to rad
825         double new_exp = (spiral->rad + dy)/(spiral->rad);
826         spiral->exp = new_exp > 0? new_exp : 0;
827     } else {
828         // roll/unroll from inside
829         gdouble   arg_t0;
830         sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
832         gdouble   arg_tmp = atan2(dy, dx) - arg_t0;
833         gdouble   arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
834         spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
836         /* round inner arg per PI/snaps, if CTRL is pressed */
837         if ( ( state & GDK_CONTROL_MASK )
838              && ( fabs(spiral->revo) > SP_EPSILON_2 )
839              && ( snaps != 0 ) ) {
840             gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
841             spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
842         }
844         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
845     }
847     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
850 /*
851  * set attributes via outer (t=1) knot point:
852  *   [default] increase/decrease revolution factor
853  *   [control] constrain inner arg to round per PI/4
854  */
855 static void
856 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
858     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
860     SPSpiral *spiral = SP_SPIRAL(item);
862     gdouble  dx = p[NR::X] - spiral->cx;
863     gdouble  dy = p[NR::Y] - spiral->cy;
865     if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
866         spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
867         if (!(state & GDK_MOD1_MASK)) {
868             // if alt not pressed, change also rad; otherwise it is locked
869             spiral->rad = MAX(hypot(dx, dy), 0.001);
870         }
871         if ( ( state & GDK_CONTROL_MASK )
872              && snaps ) {
873             spiral->arg = sp_round(spiral->arg, M_PI/snaps);
874         }
875     } else { // roll/unroll
876         // arg of the spiral outer end
877         double arg_1;
878         sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
880         // its fractional part after the whole turns are subtracted
881         double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
883         // arg of the mouse point relative to spiral center
884         double mouse_angle = atan2(dy, dx);
885         if (mouse_angle < 0)
886             mouse_angle += 2*M_PI;
888         // snap if ctrl
889         if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
890             mouse_angle = sp_round(mouse_angle, M_PI/snaps);
891         }
893         // by how much we want to rotate the outer point
894         double diff = mouse_angle - arg_r;
895         if (diff > M_PI)
896             diff -= 2*M_PI;
897         else if (diff < -M_PI)
898             diff += 2*M_PI;
900         // calculate the new rad;
901         // the value of t corresponding to the angle arg_1 + diff:
902         double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
903         // the rad at that t:
904         double rad_new = 0;
905         if (t_temp > spiral->t0)
906             sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
908         // change the revo (converting diff from radians to the number of turns)
909         spiral->revo += diff/(2*M_PI);
910         if (spiral->revo < 1e-3)
911             spiral->revo = 1e-3;
913         // if alt not pressed and the values are sane, change the rad
914         if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
915             // adjust t0 too so that the inner point stays unmoved
916             double r0;
917             sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
918             spiral->rad = rad_new;
919             spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
920         }
921         if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
922         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
923     }
925     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
928 static NR::Point sp_spiral_inner_get(SPItem *item)
930     SPSpiral *spiral = SP_SPIRAL(item);
932     return sp_spiral_get_xy(spiral, spiral->t0);
935 static NR::Point sp_spiral_outer_get(SPItem *item)
937     SPSpiral *spiral = SP_SPIRAL(item);
939     return sp_spiral_get_xy(spiral, 1.0);
942 static void
943 sp_spiral_inner_click(SPItem *item, guint state)
945     SPSpiral *spiral = SP_SPIRAL(item);
947     if (state & GDK_MOD1_MASK) {
948         spiral->exp = 1;
949         ((SPObject *)spiral)->updateRepr();
950     } else if (state & GDK_SHIFT_MASK) {
951         spiral->t0 = 0;
952         ((SPObject *)spiral)->updateRepr();
953     }
956 static SPKnotHolder *
957 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
959     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
961     sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
962                        _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
963     sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
964                        _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
966     sp_pat_knot_holder(item, knot_holder);
968     return knot_holder;
971 /* SPOffset */
973 static void
974 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
976     SPOffset *offset = SP_OFFSET(item);
978     offset->rad = sp_offset_distance_to_original(offset, p);
979     offset->knot = p;
980     offset->knotSet = true;
982     ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
986 static NR::Point sp_offset_offset_get(SPItem *item)
988     SPOffset *offset = SP_OFFSET(item);
990     NR::Point np;
991     sp_offset_top_point(offset,&np);
992     return np;
995 static SPKnotHolder *
996 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
998     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1000     sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1001                        _("Adjust the <b>offset distance</b>"));
1003     sp_pat_knot_holder(item, knot_holder);
1005     return knot_holder;
1008 static SPKnotHolder *
1009 sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1011     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1012         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1013     {
1014         SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1016         sp_pat_knot_holder(item, knot_holder);
1018         return knot_holder;
1019     }
1020     return NULL;
1023 static void
1024 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1026     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1027         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1028     {
1029         sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1030                                 // TRANSLATORS: This refers to the pattern that's inside the object
1031                                 _("<b>Move</b> the pattern fill inside the object"));
1032         sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1033                                 _("<b>Scale</b> the pattern fill uniformly"));
1034         sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1035                                 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1036     }
1039 static NR::Point sp_flowtext_corner_get(SPItem *item)
1041     SPRect *rect = SP_RECT(item);
1043     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1046 static void
1047 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1049     SPRect *rect = SP_RECT(item);
1051     sp_rect_wh_set_internal(rect, p, origin, state);
1054 static SPKnotHolder *
1055 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1057     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1059     sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1060                        _("Drag to resize the <b>flowed text frame</b>"));
1062     return knot_holder;
1066 /*
1067   Local Variables:
1068   mode:c++
1069   c-file-style:"stroustrup"
1070   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1071   indent-tabs-mode:nil
1072   fill-column:99
1073   End:
1074 */
1075 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :