Code

c597f3a265f7b748912b3e55a938386af825f698
[inkscape.git] / src / object-edit.cpp
1 #define __SP_OBJECT_EDIT_C__
3 /*
4  * Node editing extension to objects
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Mitsuru Oka
9  *
10  * Licensed under GNU GPL
11  */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
19 #include "sp-item.h"
20 #include "sp-rect.h"
21 #include "box3d.h"
22 #include "sp-ellipse.h"
23 #include "sp-star.h"
24 #include "sp-spiral.h"
25 #include "sp-offset.h"
26 #include "sp-flowtext.h"
27 #include "prefs-utils.h"
28 #include "inkscape.h"
29 #include "snap.h"
30 #include "desktop-affine.h"
31 #include <style.h>
32 #include "desktop.h"
33 #include "sp-namedview.h"
35 #include "sp-pattern.h"
36 #include "sp-path.h"
38 #include <glibmm/i18n.h>
40 #include "object-edit.h"
42 #include <libnr/nr-scale-ops.h>
45 #include "xml/repr.h"
47 #include "isnan.h"
49 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
51 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
52 static SPKnotHolder *sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop);
53 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
54 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
55 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
56 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
57 static SPKnotHolder *sp_path_knot_holder(SPItem *item, SPDesktop *desktop);
58 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
59 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
61 SPKnotHolder *
62 sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
63 {
64     if (SP_IS_RECT(item)) {
65         return sp_rect_knot_holder(item, desktop);
66     } else if (SP_IS_3DBOX(item)) {
67         return sp_3dbox_knot_holder(item, desktop);
68     } else if (SP_IS_ARC(item)) {
69         return sp_arc_knot_holder(item, desktop);
70     } else if (SP_IS_STAR(item)) {
71         return sp_star_knot_holder(item, desktop);
72     } else if (SP_IS_SPIRAL(item)) {
73         return sp_spiral_knot_holder(item, desktop);
74     } else if (SP_IS_OFFSET(item)) {
75         return sp_offset_knot_holder(item, desktop);
76     } else if (SP_IS_PATH(item)) {
77         return sp_path_knot_holder(item, desktop);
78     } else if (SP_IS_FLOWTEXT(item) && SP_FLOWTEXT(item)->has_internal_frame()) {
79         return sp_flowtext_knot_holder(item, desktop);
80     }
82     return NULL;
83 }
86 /* Pattern manipulation */
88 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
89 {
90     gdouble theta = asin(pat->patternTransform[1] / scale);
91     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
92     return theta;
93 }
95 static gdouble sp_pattern_extract_scale(SPPattern *pat)
96 {
97     gdouble s = pat->patternTransform[1];
98     gdouble c = pat->patternTransform[0];
99     gdouble xscale = sqrt(c * c + s * s);
100     return xscale;
103 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
105     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
108 static void
109 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
111     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
113     NR::Point p_snapped = p;
115     if ( state & GDK_CONTROL_MASK ) {
116         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
117             p_snapped[NR::Y] = origin[NR::Y];
118         } else {
119             p_snapped[NR::X] = origin[NR::X];
120         }
121     }
123     if (state)  {
124         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
125         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
126     }
128     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
132 static NR::Point sp_pattern_xy_get(SPItem *item)
134     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
135     return sp_pattern_extract_trans(pat);
138 static NR::Point sp_pattern_angle_get(SPItem *item)
140     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
142     gdouble x = (pattern_width(pat)*0.5);
143     gdouble y = 0;
144     NR::Point delta = NR::Point(x,y);
145     gdouble scale = sp_pattern_extract_scale(pat);
146     gdouble theta = sp_pattern_extract_theta(pat, scale);
147     delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
148     delta = delta + sp_pattern_extract_trans(pat);
149     return delta;
152 static void
153 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
155     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
157     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
159     // get the angle from pattern 0,0 to the cursor pos
160     NR::Point delta = p - sp_pattern_extract_trans(pat);
161     gdouble theta = atan2(delta);
163     if ( state & GDK_CONTROL_MASK ) {
164         theta = sp_round(theta, M_PI/snaps);
165     }
167     // get the scale from the current transform so we can keep it.
168     gdouble scl = sp_pattern_extract_scale(pat);
169     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
170     NR::Point const t = sp_pattern_extract_trans(pat);
171     rot[4] = t[NR::X];
172     rot[5] = t[NR::Y];
173     sp_item_adjust_pattern(item, rot, true);
174     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
177 static void
178 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
180     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
182     // Get the scale from the position of the knotholder,
183     NR::Point d = p - sp_pattern_extract_trans(pat);
184     gdouble s = NR::L2(d);
185     gdouble pat_x = pattern_width(pat) * 0.5;
186     gdouble pat_y = pattern_height(pat) * 0.5;
187     gdouble pat_h = hypot(pat_x, pat_y);
188     gdouble scl = s / pat_h;
190     // get angle from current transform, (need get current scale first to calculate angle)
191     gdouble oldscale = sp_pattern_extract_scale(pat);
192     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
194     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
195     NR::Point const t = sp_pattern_extract_trans(pat);
196     rot[4] = t[NR::X];
197     rot[5] = t[NR::Y];
198     sp_item_adjust_pattern(item, rot, true);
199     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
203 static NR::Point sp_pattern_scale_get(SPItem *item)
205     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
207     gdouble x = pattern_width(pat)*0.5;
208     gdouble y = pattern_height(pat)*0.5;
209     NR::Point delta = NR::Point(x,y);
210     NR::Matrix a = pat->patternTransform;
211     a[4] = 0;
212     a[5] = 0;
213     delta = delta * a;
214     delta = delta + sp_pattern_extract_trans(pat);
215     return delta;
218 /* SPRect */
220 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
222     SPDesktop const *desktop = inkscape_active_desktop();
223     NR::Matrix const i2d (sp_item_i2d_affine (item));
224     NR::Point s = p * i2d;    
225     SnapManager const &m = desktop->namedview->snap_manager;
226     s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, item).getPoint();
227     return s * i2d.inverse();
230 static NR::Point sp_rect_rx_get(SPItem *item)
232     SPRect *rect = SP_RECT(item);
234     return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
237 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
239     SPRect *rect = SP_RECT(item);
240     
241     //In general we cannot just snap this radius to an arbitrary point, as we have only a single
242     //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
243     //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
245     if (state & GDK_CONTROL_MASK) {
246         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
247         rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
248         rect->rx._set = rect->ry._set = true;
250     } else {
251         rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
252         rect->rx._set = true;
253     }
255     ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
259 static NR::Point sp_rect_ry_get(SPItem *item)
261     SPRect *rect = SP_RECT(item);
263     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
266 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
268     SPRect *rect = SP_RECT(item);
269     
270     //In general we cannot just snap this radius to an arbitrary point, as we have only a single
271     //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
272     //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
274     if (state & GDK_CONTROL_MASK) {
275         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
276         rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
277         rect->ry._set = rect->rx._set = true;
278     } else {
279         if (!rect->rx._set || rect->rx.computed == 0) {
280             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
281                                       0.0,
282                                       MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
283         } else {
284             rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
285                                       0.0,
286                                       rect->height.computed / 2.0);
287         }
289         rect->ry._set = true;
290     }
292     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
295 /**
296  *  Remove rounding from a rectangle.
297  */
298 static void rect_remove_rounding(SPRect *rect)
300     SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
301     SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
304 /**
305  *  Called when the horizontal rounding radius knot is clicked.
306  */
307 static void sp_rect_rx_knot_click(SPItem *item, guint state)
309     SPRect *rect = SP_RECT(item);
311     if (state & GDK_SHIFT_MASK) {
312         rect_remove_rounding(rect);
313     } else if (state & GDK_CONTROL_MASK) {
314         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
315         SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
316     }
319 /**
320  *  Called when the vertical rounding radius knot is clicked.
321  */
322 static void sp_rect_ry_knot_click(SPItem *item, guint state)
324     SPRect *rect = SP_RECT(item);
326     if (state & GDK_SHIFT_MASK) {
327         rect_remove_rounding(rect);
328     } else if (state & GDK_CONTROL_MASK) {
329         /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
330         SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
331     }
334 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
336 static void sp_rect_clamp_radii(SPRect *rect)
338     // clamp rounding radii so that they do not exceed width/height
339     if (2 * rect->rx.computed > rect->width.computed) {
340         rect->rx.computed = 0.5 * rect->width.computed;
341         rect->rx._set = true;
342     }
343     if (2 * rect->ry.computed > rect->height.computed) {
344         rect->ry.computed = 0.5 * rect->height.computed;
345         rect->ry._set = true;
346     }
349 static NR::Point sp_rect_wh_get(SPItem *item)
351     SPRect *rect = SP_RECT(item);
353     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
356 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
358     NR::Point const s = snap_knot_position(rect, p);
360     if (state & GDK_CONTROL_MASK) {
361         // original width/height when drag started
362         gdouble const w_orig = (origin[NR::X] - rect->x.computed);
363         gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
365         //original ratio
366         gdouble const ratio = (w_orig / h_orig);
368         // mouse displacement since drag started
369         gdouble const minx = s[NR::X] - origin[NR::X];
370         gdouble const miny = s[NR::Y] - origin[NR::Y];
372         if (fabs(minx) > fabs(miny)) {
374             // snap to horizontal or diagonal
375             rect->width.computed = MAX(w_orig + minx, 0);
376             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
377                 // closer to the diagonal and in same-sign quarters, change both using ratio
378                 rect->height.computed = MAX(h_orig + minx / ratio, 0);
379             } else {
380                 // closer to the horizontal, change only width, height is h_orig
381                 rect->height.computed = MAX(h_orig, 0);
382             }
384         } else {
385             // snap to vertical or diagonal
386             rect->height.computed = MAX(h_orig + miny, 0);
387             if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
388                 // closer to the diagonal and in same-sign quarters, change both using ratio
389                 rect->width.computed = MAX(w_orig + miny * ratio, 0);
390             } else {
391                 // closer to the vertical, change only height, width is w_orig
392                 rect->width.computed = MAX(w_orig, 0);
393             }
394         }
396         rect->width._set = rect->height._set = true;
398     } else {
399         // move freely
400         rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
401         rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
402         rect->width._set = rect->height._set = true;
403     }
405     sp_rect_clamp_radii(rect);
407     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
410 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
412     SPRect *rect = SP_RECT(item);
414     sp_rect_wh_set_internal(rect, p, origin, state);
417 static NR::Point sp_rect_xy_get(SPItem *item)
419     SPRect *rect = SP_RECT(item);
421     return NR::Point(rect->x.computed, rect->y.computed);
424 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
426     SPRect *rect = SP_RECT(item);
428     // opposite corner (unmoved)
429     gdouble opposite_x = (rect->x.computed + rect->width.computed);
430     gdouble opposite_y = (rect->y.computed + rect->height.computed);
432     // original width/height when drag started
433     gdouble w_orig = opposite_x - origin[NR::X];
434     gdouble h_orig = opposite_y - origin[NR::Y];
436     NR::Point const s = snap_knot_position(rect, p);
438     // mouse displacement since drag started
439     gdouble minx = s[NR::X] - origin[NR::X];
440     gdouble miny = s[NR::Y] - origin[NR::Y];
442     if (state & GDK_CONTROL_MASK) {
443         //original ratio
444         gdouble ratio = (w_orig / h_orig);
446         if (fabs(minx) > fabs(miny)) {
448             // snap to horizontal or diagonal
449             rect->x.computed = MIN(s[NR::X], opposite_x);
450             rect->width.computed = MAX(w_orig - minx, 0);
451             if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
452                 // closer to the diagonal and in same-sign quarters, change both using ratio
453                 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
454                 rect->height.computed = MAX(h_orig - minx / ratio, 0);
455             } else {
456                 // closer to the horizontal, change only width, height is h_orig
457                 rect->y.computed = MIN(origin[NR::Y], opposite_y);
458                 rect->height.computed = MAX(h_orig, 0);
459             }
461         } else {
463             // snap to vertical or diagonal
464             rect->y.computed = MIN(s[NR::Y], opposite_y);
465             rect->height.computed = MAX(h_orig - miny, 0);
466             if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
467                 // closer to the diagonal and in same-sign quarters, change both using ratio
468                 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
469                 rect->width.computed = MAX(w_orig - miny * ratio, 0);
470             } else {
471                 // closer to the vertical, change only height, width is w_orig
472                 rect->x.computed = MIN(origin[NR::X], opposite_x);
473                 rect->width.computed = MAX(w_orig, 0);
474             }
476         }
478         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
480     } else {
481         // move freely
482         rect->x.computed = MIN(s[NR::X], opposite_x);
483         rect->width.computed = MAX(w_orig - minx, 0);
484         rect->y.computed = MIN(s[NR::Y], opposite_y);
485         rect->height.computed = MAX(h_orig - miny, 0);
486         rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
487     }
489     sp_rect_clamp_radii(rect);
491     ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
494 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
496     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
498     sp_knot_holder_add_full(
499       knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
500       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
501       _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
502         "radius the same"));
504     sp_knot_holder_add_full(
505       knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
506       SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
507       _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
508         "radius the same")
509       );
511     sp_knot_holder_add_full(
512       knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
513       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
514       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
515         "or stretch in one dimension only")
516       );
518     sp_knot_holder_add_full(
519       knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
520       SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
521       _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
522         "or stretch in one dimension only")
523       );
525     sp_pat_knot_holder(item, knot_holder);
526     return knot_holder;
529 /* 3D Box */
531 static void sp_3dbox_knot_set(SPItem *item, guint knot_id, Box3D::Axis direction,
532                                NR::Point const &new_pos, NR::Point const &origin, guint state)
534     g_assert(item != NULL);
535     SP3DBox *box = SP_3DBOX(item);
537     // FIXME: Why must the coordinates be flipped vertically???
538     SPDocument *doc = SP_OBJECT_DOCUMENT(box);
539     gdouble height = sp_document_height(doc);
541     if (direction == Box3D::Z) {
542         sp_3dbox_move_corner_in_constrained_Z_direction (box, knot_id,
543                                                          NR::Point (new_pos[NR::X], height - new_pos[NR::Y]));
544     } else {
545         sp_3dbox_move_corner_in_XY_plane (box, knot_id, NR::Point (new_pos[NR::X], height - new_pos[NR::Y]));
546     }
547     sp_3dbox_update_curves (box);
550 static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
552     g_assert(item != NULL);
553     SP3DBox *box = SP_3DBOX(item);
555     // FIXME: Why must the coordinates be flipped vertically???
556     SPDocument *doc = SP_OBJECT_DOCUMENT(box);
557     gdouble height = sp_document_height(doc);
559     return NR::Point(sp_3dbox_get_corner(box, knot_id)[NR::X], height - sp_3dbox_get_corner(box, knot_id)[NR::Y]);
562 static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
564     sp_3dbox_knot_set (item, 1, Box3D::XY, new_pos, origin, state);
567 static NR::Point sp_3dbox_knot1_get(SPItem *item)
569     return sp_3dbox_knot_get(item, 1);
572 static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
574     sp_3dbox_knot_set (item, 2, Box3D::XY, new_pos, origin, state);
577 static NR::Point sp_3dbox_knot2_get(SPItem *item)
579     return sp_3dbox_knot_get(item, 2);
582 static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
584     sp_3dbox_knot_set (item, 5, Box3D::Z, new_pos, origin, state);
587 static NR::Point sp_3dbox_knot5_get(SPItem *item)
589     return sp_3dbox_knot_get(item, 5);
592 static SPKnotHolder *
593 sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop)
595     g_assert(item != NULL);
596     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
598     sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,_("Resize box in X/Y direction"));
599     sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,_("Resize box in X/Y direction"));
600     sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,_("Resize box in Z direction"));
601     sp_pat_knot_holder(item, knot_holder);
603     return knot_holder;
606 /* SPArc */
608 /*
609  * return values:
610  *   1  : inside
611  *   0  : on the curves
612  *   -1 : outside
613  */
614 static gint
615 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
617     gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
618     gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
620     gdouble s = dx * dx + dy * dy;
621     if (s < 1.0) return 1;
622     if (s > 1.0) return -1;
623     return 0;
626 static void
627 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
629     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
631     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
632     SPArc *arc = SP_ARC(item);
634     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
636     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
637     NR::scale sc(ge->rx.computed, ge->ry.computed);
638     ge->start = atan2(delta * sc.inverse());
639     if ( ( state & GDK_CONTROL_MASK )
640          && snaps )
641     {
642         ge->start = sp_round(ge->start, M_PI/snaps);
643     }
644     sp_genericellipse_normalize(ge);
645     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
648 static NR::Point sp_arc_start_get(SPItem *item)
650     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
651     SPArc *arc = SP_ARC(item);
653     return sp_arc_get_xy(arc, ge->start);
656 static void
657 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
659     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
661     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
662     SPArc *arc = SP_ARC(item);
664     ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
666     NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
667     NR::scale sc(ge->rx.computed, ge->ry.computed);
668     ge->end = atan2(delta * sc.inverse());
669     if ( ( state & GDK_CONTROL_MASK )
670          && snaps )
671     {
672         ge->end = sp_round(ge->end, M_PI/snaps);
673     }
674     sp_genericellipse_normalize(ge);
675     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
678 static NR::Point sp_arc_end_get(SPItem *item)
680     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
681     SPArc *arc = SP_ARC(item);
683     return sp_arc_get_xy(arc, ge->end);
686 static void
687 sp_arc_startend_click(SPItem *item, guint state)
689     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
691     if (state & GDK_SHIFT_MASK) {
692         ge->end = ge->start = 0;
693         ((SPObject *)ge)->updateRepr();
694     }
698 static void
699 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
701     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
702     SPArc *arc = SP_ARC(item);
703     
704     NR::Point const s = snap_knot_position(arc, p);
706     ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
708     if ( state & GDK_CONTROL_MASK ) {
709         ge->ry.computed = ge->rx.computed;
710     }
712     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
715 static NR::Point sp_arc_rx_get(SPItem *item)
717     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
719     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(ge->rx.computed, 0));
722 static void
723 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
725     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
726     SPArc *arc = SP_ARC(item);
727     
728     NR::Point const s = snap_knot_position(arc, p);
730     ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
732     if ( state & GDK_CONTROL_MASK ) {
733         ge->rx.computed = ge->ry.computed;
734     }
736     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
739 static NR::Point sp_arc_ry_get(SPItem *item)
741     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
743     return (NR::Point(ge->cx.computed, ge->cy.computed) -  NR::Point(0, ge->ry.computed));
746 static void
747 sp_arc_rx_click(SPItem *item, guint state)
749     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
751     if (state & GDK_CONTROL_MASK) {
752         ge->ry.computed = ge->rx.computed;
753         ((SPObject *)ge)->updateRepr();
754     }
757 static void
758 sp_arc_ry_click(SPItem *item, guint state)
760     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
762     if (state & GDK_CONTROL_MASK) {
763         ge->rx.computed = ge->ry.computed;
764         ((SPObject *)ge)->updateRepr();
765     }
768 static SPKnotHolder *
769 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
771     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
773     sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
774                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
775                             _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
776     sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
777                             SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
778                             _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
779     sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
780                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
781                             _("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"));
782     sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
783                             SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
784                             _("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"));
786     sp_pat_knot_holder(item, knot_holder);
788     return knot_holder;
791 /* SPStar */
793 static void
794 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
796     SPStar *star = SP_STAR(item);
797     
798     NR::Point const s = snap_knot_position(star, p);
800     NR::Point d = s - star->center;
802     double arg1 = atan2(d);
803     double darg1 = arg1 - star->arg[0];
805     if (state & GDK_MOD1_MASK) {
806         star->randomized = darg1/(star->arg[0] - star->arg[1]);
807     } else if (state & GDK_SHIFT_MASK) {
808         star->rounded = darg1/(star->arg[0] - star->arg[1]);
809     } else if (state & GDK_CONTROL_MASK) {
810         star->r[0]    = L2(d);
811     } else {
812         star->r[0]    = L2(d);
813         star->arg[0]  = arg1;
814         star->arg[1] += darg1;
815     }
816     ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
819 static void
820 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
822     SPStar *star = SP_STAR(item);
823     
824     NR::Point const s = snap_knot_position(star, p);
825     
826     if (star->flatsided == false) {
827         NR::Point d = s - star->center;
829         double arg1 = atan2(d);
830         double darg1 = arg1 - star->arg[1];
832         if (state & GDK_MOD1_MASK) {
833             star->randomized = darg1/(star->arg[0] - star->arg[1]);
834         } else if (state & GDK_SHIFT_MASK) {
835             star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
836         } else if (state & GDK_CONTROL_MASK) {
837             star->r[1]   = L2(d);
838             star->arg[1] = star->arg[0] + M_PI / star->sides;
839         }
840         else {
841             star->r[1]   = L2(d);
842             star->arg[1] = atan2(d);
843         }
844         ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
845     }
848 static NR::Point sp_star_knot1_get(SPItem *item)
850     g_assert(item != NULL);
852     SPStar *star = SP_STAR(item);
854     return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
858 static NR::Point sp_star_knot2_get(SPItem *item)
860     g_assert(item != NULL);
862     SPStar *star = SP_STAR(item);
864     return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
867 static void
868 sp_star_knot_click(SPItem *item, guint state)
870     SPStar *star = SP_STAR(item);
872     if (state & GDK_MOD1_MASK) {
873         star->randomized = 0;
874         ((SPObject *)star)->updateRepr();
875     } else if (state & GDK_SHIFT_MASK) {
876         star->rounded = 0;
877         ((SPObject *)star)->updateRepr();
878     } else if (state & GDK_CONTROL_MASK) {
879         star->arg[1] = star->arg[0] + M_PI / star->sides;
880         ((SPObject *)star)->updateRepr();
881     }
884 static SPKnotHolder *
885 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
887     /* we don't need to get parent knot_holder */
888     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
889     g_assert(item != NULL);
891     SPStar *star = SP_STAR(item);
893     sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
894                        _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
895     if (star->flatsided == false)
896         sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
897                            _("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"));
899     sp_pat_knot_holder(item, knot_holder);
901     return knot_holder;
904 /* SPSpiral */
906 /*
907  * set attributes via inner (t=t0) knot point:
908  *   [default] increase/decrease inner point
909  *   [shift]   increase/decrease inner and outer arg synchronizely
910  *   [control] constrain inner arg to round per PI/4
911  */
912 static void
913 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
915     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
917     SPSpiral *spiral = SP_SPIRAL(item);
919     gdouble   dx = p[NR::X] - spiral->cx;
920     gdouble   dy = p[NR::Y] - spiral->cy;
922     if (state & GDK_MOD1_MASK) {
923         // adjust divergence by vertical drag, relative to rad
924         double new_exp = (spiral->rad + dy)/(spiral->rad);
925         spiral->exp = new_exp > 0? new_exp : 0;
926     } else {
927         // roll/unroll from inside
928         gdouble   arg_t0;
929         sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
931         gdouble   arg_tmp = atan2(dy, dx) - arg_t0;
932         gdouble   arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
933         spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
935         /* round inner arg per PI/snaps, if CTRL is pressed */
936         if ( ( state & GDK_CONTROL_MASK )
937              && ( fabs(spiral->revo) > SP_EPSILON_2 )
938              && ( snaps != 0 ) ) {
939             gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
940             spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
941         }
943         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
944     }
946     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
949 /*
950  * set attributes via outer (t=1) knot point:
951  *   [default] increase/decrease revolution factor
952  *   [control] constrain inner arg to round per PI/4
953  */
954 static void
955 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
957     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
959     SPSpiral *spiral = SP_SPIRAL(item);
961     gdouble  dx = p[NR::X] - spiral->cx;
962     gdouble  dy = p[NR::Y] - spiral->cy;
964     if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
965         spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
966         if (!(state & GDK_MOD1_MASK)) {
967             // if alt not pressed, change also rad; otherwise it is locked
968             spiral->rad = MAX(hypot(dx, dy), 0.001);
969         }
970         if ( ( state & GDK_CONTROL_MASK )
971              && snaps ) {
972             spiral->arg = sp_round(spiral->arg, M_PI/snaps);
973         }
974     } else { // roll/unroll
975         // arg of the spiral outer end
976         double arg_1;
977         sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
979         // its fractional part after the whole turns are subtracted
980         double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
982         // arg of the mouse point relative to spiral center
983         double mouse_angle = atan2(dy, dx);
984         if (mouse_angle < 0)
985             mouse_angle += 2*M_PI;
987         // snap if ctrl
988         if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
989             mouse_angle = sp_round(mouse_angle, M_PI/snaps);
990         }
992         // by how much we want to rotate the outer point
993         double diff = mouse_angle - arg_r;
994         if (diff > M_PI)
995             diff -= 2*M_PI;
996         else if (diff < -M_PI)
997             diff += 2*M_PI;
999         // calculate the new rad;
1000         // the value of t corresponding to the angle arg_1 + diff:
1001         double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
1002         // the rad at that t:
1003         double rad_new = 0;
1004         if (t_temp > spiral->t0)
1005             sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
1007         // change the revo (converting diff from radians to the number of turns)
1008         spiral->revo += diff/(2*M_PI);
1009         if (spiral->revo < 1e-3)
1010             spiral->revo = 1e-3;
1012         // if alt not pressed and the values are sane, change the rad
1013         if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
1014             // adjust t0 too so that the inner point stays unmoved
1015             double r0;
1016             sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
1017             spiral->rad = rad_new;
1018             spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
1019         }
1020         if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
1021         spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1022     }
1024     ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1027 static NR::Point sp_spiral_inner_get(SPItem *item)
1029     SPSpiral *spiral = SP_SPIRAL(item);
1031     return sp_spiral_get_xy(spiral, spiral->t0);
1034 static NR::Point sp_spiral_outer_get(SPItem *item)
1036     SPSpiral *spiral = SP_SPIRAL(item);
1038     return sp_spiral_get_xy(spiral, 1.0);
1041 static void
1042 sp_spiral_inner_click(SPItem *item, guint state)
1044     SPSpiral *spiral = SP_SPIRAL(item);
1046     if (state & GDK_MOD1_MASK) {
1047         spiral->exp = 1;
1048         ((SPObject *)spiral)->updateRepr();
1049     } else if (state & GDK_SHIFT_MASK) {
1050         spiral->t0 = 0;
1051         ((SPObject *)spiral)->updateRepr();
1052     }
1055 static SPKnotHolder *
1056 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
1058     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1060     sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
1061                        _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
1062     sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
1063                        _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
1065     sp_pat_knot_holder(item, knot_holder);
1067     return knot_holder;
1070 /* SPOffset */
1072 static void
1073 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1075     SPOffset *offset = SP_OFFSET(item);
1077     offset->rad = sp_offset_distance_to_original(offset, p);
1078     offset->knot = p;
1079     offset->knotSet = true;
1081     ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1085 static NR::Point sp_offset_offset_get(SPItem *item)
1087     SPOffset *offset = SP_OFFSET(item);
1089     NR::Point np;
1090     sp_offset_top_point(offset,&np);
1091     return np;
1094 static SPKnotHolder *
1095 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1097     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1099     sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1100                        _("Adjust the <b>offset distance</b>"));
1102     sp_pat_knot_holder(item, knot_holder);
1104     return knot_holder;
1107 static SPKnotHolder *
1108 sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1110     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1111         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1112     {
1113         SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1115         sp_pat_knot_holder(item, knot_holder);
1117         return knot_holder;
1118     }
1119     return NULL;
1122 static void
1123 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1125     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1126         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1127     {
1128         sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1129                                 // TRANSLATORS: This refers to the pattern that's inside the object
1130                                 _("<b>Move</b> the pattern fill inside the object"));
1131         sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1132                                 _("<b>Scale</b> the pattern fill uniformly"));
1133         sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1134                                 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1135     }
1138 static NR::Point sp_flowtext_corner_get(SPItem *item)
1140     SPRect *rect = SP_RECT(item);
1142     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1145 static void
1146 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1148     SPRect *rect = SP_RECT(item);
1150     sp_rect_wh_set_internal(rect, p, origin, state);
1153 static SPKnotHolder *
1154 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1156     SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1158     sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1159                        _("Drag to resize the <b>flowed text frame</b>"));
1161     return knot_holder;
1165 /*
1166   Local Variables:
1167   mode:c++
1168   c-file-style:"stroustrup"
1169   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1170   indent-tabs-mode:nil
1171   fill-column:99
1172   End:
1173 */
1174 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :