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)
100 {
101 return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
102 }
104 static void
105 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
106 {
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);
125 }
128 static NR::Point sp_pattern_xy_get(SPItem *item)
129 {
130 SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
131 return sp_pattern_extract_trans(pat);
132 }
134 static NR::Point sp_pattern_angle_get(SPItem *item)
135 {
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;
146 }
148 static void
149 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
150 {
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);
171 }
173 static void
174 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
175 {
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);
196 }
199 static NR::Point sp_pattern_scale_get(SPItem *item)
200 {
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;
212 }
214 /* SPRect */
216 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
217 {
218 SPDesktop const *desktop = inkscape_active_desktop();
219 NR::Point s = sp_desktop_dt2root_xy_point(desktop, p);
220 SnapManager const &m = desktop->namedview->snap_manager;
221 s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, item).getPoint();
222 return sp_desktop_root2dt_xy_point(desktop, s);
223 }
225 static NR::Point sp_rect_rx_get(SPItem *item)
226 {
227 SPRect *rect = SP_RECT(item);
229 return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
230 }
232 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
233 {
234 SPRect *rect = SP_RECT(item);
236 NR::Point const s = snap_knot_position(rect, p);
238 if (state & GDK_CONTROL_MASK) {
239 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
240 rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - s[NR::X], 0.0, temp);
241 rect->rx._set = rect->ry._set = true;
243 } else {
244 rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - s[NR::X], 0.0, rect->width.computed / 2.0);
245 rect->rx._set = true;
246 }
248 ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
249 }
252 static NR::Point sp_rect_ry_get(SPItem *item)
253 {
254 SPRect *rect = SP_RECT(item);
256 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
257 }
259 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
260 {
261 SPRect *rect = SP_RECT(item);
263 NR::Point const s = snap_knot_position(rect, p);
265 if (state & GDK_CONTROL_MASK) {
266 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
267 rect->rx.computed = rect->ry.computed = CLAMP(s[NR::Y] - rect->y.computed, 0.0, temp);
268 rect->ry._set = rect->rx._set = true;
269 } else {
270 if (!rect->rx._set || rect->rx.computed == 0) {
271 rect->ry.computed = CLAMP(s[NR::Y] - rect->y.computed,
272 0.0,
273 MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
274 } else {
275 rect->ry.computed = CLAMP(s[NR::Y] - rect->y.computed,
276 0.0,
277 rect->height.computed / 2.0);
278 }
280 rect->ry._set = true;
281 }
283 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
284 }
286 /**
287 * Remove rounding from a rectangle.
288 */
289 static void rect_remove_rounding(SPRect *rect)
290 {
291 SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
292 SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
293 }
295 /**
296 * Called when the horizontal rounding radius knot is clicked.
297 */
298 static void sp_rect_rx_knot_click(SPItem *item, guint state)
299 {
300 SPRect *rect = SP_RECT(item);
302 if (state & GDK_SHIFT_MASK) {
303 rect_remove_rounding(rect);
304 } else if (state & GDK_CONTROL_MASK) {
305 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
306 SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
307 }
308 }
310 /**
311 * Called when the vertical rounding radius knot is clicked.
312 */
313 static void sp_rect_ry_knot_click(SPItem *item, guint state)
314 {
315 SPRect *rect = SP_RECT(item);
317 if (state & GDK_SHIFT_MASK) {
318 rect_remove_rounding(rect);
319 } else if (state & GDK_CONTROL_MASK) {
320 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
321 SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
322 }
323 }
325 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
327 static void sp_rect_clamp_radii(SPRect *rect)
328 {
329 // clamp rounding radii so that they do not exceed width/height
330 if (2 * rect->rx.computed > rect->width.computed) {
331 rect->rx.computed = 0.5 * rect->width.computed;
332 rect->rx._set = true;
333 }
334 if (2 * rect->ry.computed > rect->height.computed) {
335 rect->ry.computed = 0.5 * rect->height.computed;
336 rect->ry._set = true;
337 }
338 }
340 static NR::Point sp_rect_wh_get(SPItem *item)
341 {
342 SPRect *rect = SP_RECT(item);
344 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
345 }
347 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
348 {
349 NR::Point const s = snap_knot_position(rect, p);
351 if (state & GDK_CONTROL_MASK) {
352 // original width/height when drag started
353 gdouble const w_orig = (origin[NR::X] - rect->x.computed);
354 gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
356 //original ratio
357 gdouble const ratio = (w_orig / h_orig);
359 // mouse displacement since drag started
360 gdouble const minx = s[NR::X] - origin[NR::X];
361 gdouble const miny = s[NR::Y] - origin[NR::Y];
363 if (fabs(minx) > fabs(miny)) {
365 // snap to horizontal or diagonal
366 rect->width.computed = MAX(w_orig + minx, 0);
367 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
368 // closer to the diagonal and in same-sign quarters, change both using ratio
369 rect->height.computed = MAX(h_orig + minx / ratio, 0);
370 } else {
371 // closer to the horizontal, change only width, height is h_orig
372 rect->height.computed = MAX(h_orig, 0);
373 }
375 } else {
376 // snap to vertical or diagonal
377 rect->height.computed = MAX(h_orig + miny, 0);
378 if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
379 // closer to the diagonal and in same-sign quarters, change both using ratio
380 rect->width.computed = MAX(w_orig + miny * ratio, 0);
381 } else {
382 // closer to the vertical, change only height, width is w_orig
383 rect->width.computed = MAX(w_orig, 0);
384 }
385 }
387 rect->width._set = rect->height._set = true;
389 } else {
390 // move freely
391 rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
392 rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
393 rect->width._set = rect->height._set = true;
394 }
396 sp_rect_clamp_radii(rect);
398 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
399 }
401 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
402 {
403 SPRect *rect = SP_RECT(item);
405 sp_rect_wh_set_internal(rect, p, origin, state);
406 }
408 static NR::Point sp_rect_xy_get(SPItem *item)
409 {
410 SPRect *rect = SP_RECT(item);
412 return NR::Point(rect->x.computed, rect->y.computed);
413 }
415 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
416 {
417 SPRect *rect = SP_RECT(item);
419 // opposite corner (unmoved)
420 gdouble opposite_x = (rect->x.computed + rect->width.computed);
421 gdouble opposite_y = (rect->y.computed + rect->height.computed);
423 // original width/height when drag started
424 gdouble w_orig = opposite_x - origin[NR::X];
425 gdouble h_orig = opposite_y - origin[NR::Y];
427 NR::Point const s = snap_knot_position(rect, p);
429 // mouse displacement since drag started
430 gdouble minx = s[NR::X] - origin[NR::X];
431 gdouble miny = s[NR::Y] - origin[NR::Y];
433 if (state & GDK_CONTROL_MASK) {
434 //original ratio
435 gdouble ratio = (w_orig / h_orig);
437 if (fabs(minx) > fabs(miny)) {
439 // snap to horizontal or diagonal
440 rect->x.computed = MIN(s[NR::X], opposite_x);
441 rect->width.computed = MAX(w_orig - minx, 0);
442 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
443 // closer to the diagonal and in same-sign quarters, change both using ratio
444 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
445 rect->height.computed = MAX(h_orig - minx / ratio, 0);
446 } else {
447 // closer to the horizontal, change only width, height is h_orig
448 rect->y.computed = MIN(origin[NR::Y], opposite_y);
449 rect->height.computed = MAX(h_orig, 0);
450 }
452 } else {
454 // snap to vertical or diagonal
455 rect->y.computed = MIN(s[NR::Y], opposite_y);
456 rect->height.computed = MAX(h_orig - miny, 0);
457 if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
458 // closer to the diagonal and in same-sign quarters, change both using ratio
459 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
460 rect->width.computed = MAX(w_orig - miny * ratio, 0);
461 } else {
462 // closer to the vertical, change only height, width is w_orig
463 rect->x.computed = MIN(origin[NR::X], opposite_x);
464 rect->width.computed = MAX(w_orig, 0);
465 }
467 }
469 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
471 } else {
472 // move freely
473 rect->x.computed = MIN(s[NR::X], opposite_x);
474 rect->width.computed = MAX(w_orig - minx, 0);
475 rect->y.computed = MIN(s[NR::Y], opposite_y);
476 rect->height.computed = MAX(h_orig - miny, 0);
477 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
478 }
480 sp_rect_clamp_radii(rect);
482 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
483 }
485 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
486 {
487 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
489 sp_knot_holder_add_full(
490 knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
491 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
492 _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
493 "radius the same"));
495 sp_knot_holder_add_full(
496 knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
497 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
498 _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
499 "radius the same")
500 );
502 sp_knot_holder_add_full(
503 knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
504 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
505 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
506 "or stretch in one dimension only")
507 );
509 sp_knot_holder_add_full(
510 knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
511 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
512 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
513 "or stretch in one dimension only")
514 );
516 sp_pat_knot_holder(item, knot_holder);
517 return knot_holder;
518 }
520 /* SPArc */
522 /*
523 * return values:
524 * 1 : inside
525 * 0 : on the curves
526 * -1 : outside
527 */
528 static gint
529 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
530 {
531 gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
532 gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
534 gdouble s = dx * dx + dy * dy;
535 if (s < 1.0) return 1;
536 if (s > 1.0) return -1;
537 return 0;
538 }
540 static void
541 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
542 {
543 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
545 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
546 SPArc *arc = SP_ARC(item);
548 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
550 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
551 NR::scale sc(ge->rx.computed, ge->ry.computed);
552 ge->start = atan2(delta * sc.inverse());
553 if ( ( state & GDK_CONTROL_MASK )
554 && snaps )
555 {
556 ge->start = sp_round(ge->start, M_PI/snaps);
557 }
558 sp_genericellipse_normalize(ge);
559 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
560 }
562 static NR::Point sp_arc_start_get(SPItem *item)
563 {
564 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
565 SPArc *arc = SP_ARC(item);
567 return sp_arc_get_xy(arc, ge->start);
568 }
570 static void
571 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
572 {
573 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
575 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
576 SPArc *arc = SP_ARC(item);
578 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
580 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
581 NR::scale sc(ge->rx.computed, ge->ry.computed);
582 ge->end = atan2(delta * sc.inverse());
583 if ( ( state & GDK_CONTROL_MASK )
584 && snaps )
585 {
586 ge->end = sp_round(ge->end, M_PI/snaps);
587 }
588 sp_genericellipse_normalize(ge);
589 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
590 }
592 static NR::Point sp_arc_end_get(SPItem *item)
593 {
594 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
595 SPArc *arc = SP_ARC(item);
597 return sp_arc_get_xy(arc, ge->end);
598 }
600 static void
601 sp_arc_startend_click(SPItem *item, guint state)
602 {
603 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
605 if (state & GDK_SHIFT_MASK) {
606 ge->end = ge->start = 0;
607 ((SPObject *)ge)->updateRepr();
608 }
609 }
612 static void
613 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
614 {
615 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
616 SPArc *arc = SP_ARC(item);
618 NR::Point const s = snap_knot_position(arc, p);
620 ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
622 if ( state & GDK_CONTROL_MASK ) {
623 ge->ry.computed = ge->rx.computed;
624 }
626 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
627 }
629 static NR::Point sp_arc_rx_get(SPItem *item)
630 {
631 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
633 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(ge->rx.computed, 0));
634 }
636 static void
637 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
638 {
639 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
640 SPArc *arc = SP_ARC(item);
642 NR::Point const s = snap_knot_position(arc, p);
644 ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
646 if ( state & GDK_CONTROL_MASK ) {
647 ge->rx.computed = ge->ry.computed;
648 }
650 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
651 }
653 static NR::Point sp_arc_ry_get(SPItem *item)
654 {
655 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
657 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(0, ge->ry.computed));
658 }
660 static void
661 sp_arc_rx_click(SPItem *item, guint state)
662 {
663 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
665 if (state & GDK_CONTROL_MASK) {
666 ge->ry.computed = ge->rx.computed;
667 ((SPObject *)ge)->updateRepr();
668 }
669 }
671 static void
672 sp_arc_ry_click(SPItem *item, guint state)
673 {
674 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
676 if (state & GDK_CONTROL_MASK) {
677 ge->rx.computed = ge->ry.computed;
678 ((SPObject *)ge)->updateRepr();
679 }
680 }
682 static SPKnotHolder *
683 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
684 {
685 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
687 sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
688 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
689 _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
690 sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
691 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
692 _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
693 sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
694 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
695 _("Position the <b>start point</b> of the arc or segment; with <b>Ctrl</b> to snap angle; drag <b>inside</b> the ellipse for arc, <b>outside</b> for segment"));
696 sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
697 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
698 _("Position the <b>end point</b> of the arc or segment; with <b>Ctrl</b> to snap angle; drag <b>inside</b> the ellipse for arc, <b>outside</b> for segment"));
700 sp_pat_knot_holder(item, knot_holder);
702 return knot_holder;
703 }
705 /* SPStar */
707 static void
708 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
709 {
710 SPStar *star = SP_STAR(item);
712 NR::Point const s = snap_knot_position(star, p);
714 NR::Point d = s - star->center;
716 double arg1 = atan2(d);
717 double darg1 = arg1 - star->arg[0];
719 if (state & GDK_MOD1_MASK) {
720 star->randomized = darg1/(star->arg[0] - star->arg[1]);
721 } else if (state & GDK_SHIFT_MASK) {
722 star->rounded = darg1/(star->arg[0] - star->arg[1]);
723 } else if (state & GDK_CONTROL_MASK) {
724 star->r[0] = L2(d);
725 } else {
726 star->r[0] = L2(d);
727 star->arg[0] = arg1;
728 star->arg[1] += darg1;
729 }
730 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
731 }
733 static void
734 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
735 {
736 SPStar *star = SP_STAR(item);
738 NR::Point const s = snap_knot_position(star, p);
740 if (star->flatsided == false) {
741 NR::Point d = s - star->center;
743 double arg1 = atan2(d);
744 double darg1 = arg1 - star->arg[1];
746 if (state & GDK_MOD1_MASK) {
747 star->randomized = darg1/(star->arg[0] - star->arg[1]);
748 } else if (state & GDK_SHIFT_MASK) {
749 star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
750 } else if (state & GDK_CONTROL_MASK) {
751 star->r[1] = L2(d);
752 star->arg[1] = star->arg[0] + M_PI / star->sides;
753 }
754 else {
755 star->r[1] = L2(d);
756 star->arg[1] = atan2(d);
757 }
758 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
759 }
760 }
762 static NR::Point sp_star_knot1_get(SPItem *item)
763 {
764 g_assert(item != NULL);
766 SPStar *star = SP_STAR(item);
768 return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
770 }
772 static NR::Point sp_star_knot2_get(SPItem *item)
773 {
774 g_assert(item != NULL);
776 SPStar *star = SP_STAR(item);
778 return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
779 }
781 static void
782 sp_star_knot_click(SPItem *item, guint state)
783 {
784 SPStar *star = SP_STAR(item);
786 if (state & GDK_MOD1_MASK) {
787 star->randomized = 0;
788 ((SPObject *)star)->updateRepr();
789 } else if (state & GDK_SHIFT_MASK) {
790 star->rounded = 0;
791 ((SPObject *)star)->updateRepr();
792 } else if (state & GDK_CONTROL_MASK) {
793 star->arg[1] = star->arg[0] + M_PI / star->sides;
794 ((SPObject *)star)->updateRepr();
795 }
796 }
798 static SPKnotHolder *
799 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
800 {
801 /* we don't need to get parent knot_holder */
802 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
803 g_assert(item != NULL);
805 SPStar *star = SP_STAR(item);
807 sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
808 _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
809 if (star->flatsided == false)
810 sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
811 _("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"));
813 sp_pat_knot_holder(item, knot_holder);
815 return knot_holder;
816 }
818 /* SPSpiral */
820 /*
821 * set attributes via inner (t=t0) knot point:
822 * [default] increase/decrease inner point
823 * [shift] increase/decrease inner and outer arg synchronizely
824 * [control] constrain inner arg to round per PI/4
825 */
826 static void
827 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
828 {
829 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
831 SPSpiral *spiral = SP_SPIRAL(item);
833 gdouble dx = p[NR::X] - spiral->cx;
834 gdouble dy = p[NR::Y] - spiral->cy;
836 if (state & GDK_MOD1_MASK) {
837 // adjust divergence by vertical drag, relative to rad
838 double new_exp = (spiral->rad + dy)/(spiral->rad);
839 spiral->exp = new_exp > 0? new_exp : 0;
840 } else {
841 // roll/unroll from inside
842 gdouble arg_t0;
843 sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
845 gdouble arg_tmp = atan2(dy, dx) - arg_t0;
846 gdouble arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
847 spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
849 /* round inner arg per PI/snaps, if CTRL is pressed */
850 if ( ( state & GDK_CONTROL_MASK )
851 && ( fabs(spiral->revo) > SP_EPSILON_2 )
852 && ( snaps != 0 ) ) {
853 gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
854 spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
855 }
857 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
858 }
860 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
861 }
863 /*
864 * set attributes via outer (t=1) knot point:
865 * [default] increase/decrease revolution factor
866 * [control] constrain inner arg to round per PI/4
867 */
868 static void
869 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
870 {
871 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
873 SPSpiral *spiral = SP_SPIRAL(item);
875 gdouble dx = p[NR::X] - spiral->cx;
876 gdouble dy = p[NR::Y] - spiral->cy;
878 if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
879 spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
880 if (!(state & GDK_MOD1_MASK)) {
881 // if alt not pressed, change also rad; otherwise it is locked
882 spiral->rad = MAX(hypot(dx, dy), 0.001);
883 }
884 if ( ( state & GDK_CONTROL_MASK )
885 && snaps ) {
886 spiral->arg = sp_round(spiral->arg, M_PI/snaps);
887 }
888 } else { // roll/unroll
889 // arg of the spiral outer end
890 double arg_1;
891 sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
893 // its fractional part after the whole turns are subtracted
894 double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
896 // arg of the mouse point relative to spiral center
897 double mouse_angle = atan2(dy, dx);
898 if (mouse_angle < 0)
899 mouse_angle += 2*M_PI;
901 // snap if ctrl
902 if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
903 mouse_angle = sp_round(mouse_angle, M_PI/snaps);
904 }
906 // by how much we want to rotate the outer point
907 double diff = mouse_angle - arg_r;
908 if (diff > M_PI)
909 diff -= 2*M_PI;
910 else if (diff < -M_PI)
911 diff += 2*M_PI;
913 // calculate the new rad;
914 // the value of t corresponding to the angle arg_1 + diff:
915 double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
916 // the rad at that t:
917 double rad_new = 0;
918 if (t_temp > spiral->t0)
919 sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
921 // change the revo (converting diff from radians to the number of turns)
922 spiral->revo += diff/(2*M_PI);
923 if (spiral->revo < 1e-3)
924 spiral->revo = 1e-3;
926 // if alt not pressed and the values are sane, change the rad
927 if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
928 // adjust t0 too so that the inner point stays unmoved
929 double r0;
930 sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
931 spiral->rad = rad_new;
932 spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
933 }
934 if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
935 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
936 }
938 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
939 }
941 static NR::Point sp_spiral_inner_get(SPItem *item)
942 {
943 SPSpiral *spiral = SP_SPIRAL(item);
945 return sp_spiral_get_xy(spiral, spiral->t0);
946 }
948 static NR::Point sp_spiral_outer_get(SPItem *item)
949 {
950 SPSpiral *spiral = SP_SPIRAL(item);
952 return sp_spiral_get_xy(spiral, 1.0);
953 }
955 static void
956 sp_spiral_inner_click(SPItem *item, guint state)
957 {
958 SPSpiral *spiral = SP_SPIRAL(item);
960 if (state & GDK_MOD1_MASK) {
961 spiral->exp = 1;
962 ((SPObject *)spiral)->updateRepr();
963 } else if (state & GDK_SHIFT_MASK) {
964 spiral->t0 = 0;
965 ((SPObject *)spiral)->updateRepr();
966 }
967 }
969 static SPKnotHolder *
970 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
971 {
972 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
974 sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
975 _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
976 sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
977 _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
979 sp_pat_knot_holder(item, knot_holder);
981 return knot_holder;
982 }
984 /* SPOffset */
986 static void
987 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
988 {
989 SPOffset *offset = SP_OFFSET(item);
991 offset->rad = sp_offset_distance_to_original(offset, p);
992 offset->knot = p;
993 offset->knotSet = true;
995 ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
996 }
999 static NR::Point sp_offset_offset_get(SPItem *item)
1000 {
1001 SPOffset *offset = SP_OFFSET(item);
1003 NR::Point np;
1004 sp_offset_top_point(offset,&np);
1005 return np;
1006 }
1008 static SPKnotHolder *
1009 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1010 {
1011 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1013 sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1014 _("Adjust the <b>offset distance</b>"));
1016 sp_pat_knot_holder(item, knot_holder);
1018 return knot_holder;
1019 }
1021 static SPKnotHolder *
1022 sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1023 {
1024 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1025 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1026 {
1027 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1029 sp_pat_knot_holder(item, knot_holder);
1031 return knot_holder;
1032 }
1033 return NULL;
1034 }
1036 static void
1037 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1038 {
1039 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1040 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1041 {
1042 sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1043 // TRANSLATORS: This refers to the pattern that's inside the object
1044 _("<b>Move</b> the pattern fill inside the object"));
1045 sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1046 _("<b>Scale</b> the pattern fill uniformly"));
1047 sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1048 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1049 }
1050 }
1052 static NR::Point sp_flowtext_corner_get(SPItem *item)
1053 {
1054 SPRect *rect = SP_RECT(item);
1056 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1057 }
1059 static void
1060 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1061 {
1062 SPRect *rect = SP_RECT(item);
1064 sp_rect_wh_set_internal(rect, p, origin, state);
1065 }
1067 static SPKnotHolder *
1068 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1069 {
1070 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1072 sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1073 _("Drag to resize the <b>flowed text frame</b>"));
1075 return knot_holder;
1076 }
1079 /*
1080 Local Variables:
1081 mode:c++
1082 c-file-style:"stroustrup"
1083 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1084 indent-tabs-mode:nil
1085 fill-column:99
1086 End:
1087 */
1088 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :