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
17 #include "sp-item.h"
18 #include "sp-rect.h"
19 #include "sp-ellipse.h"
20 #include "sp-star.h"
21 #include "sp-spiral.h"
22 #include "sp-offset.h"
23 #include "sp-flowtext.h"
24 #include "prefs-utils.h"
25 #include "inkscape.h"
26 #include "desktop-affine.h"
27 #include <style.h>
28 #include "desktop.h"
29 #include "sp-namedview.h"
31 #include "sp-pattern.h"
32 #include "sp-path.h"
34 #include <glibmm/i18n.h>
36 #include "object-edit.h"
38 #include <libnr/nr-scale-ops.h>
41 #include "xml/repr.h"
43 #include "isnan.h"
45 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
47 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
48 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
49 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
50 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
51 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
52 static SPKnotHolder *sp_path_knot_holder(SPItem *item, SPDesktop *desktop);
53 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
54 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
56 SPKnotHolder *
57 sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
58 {
59 if (SP_IS_RECT(item)) {
60 return sp_rect_knot_holder(item, desktop);
61 } else if (SP_IS_ARC(item)) {
62 return sp_arc_knot_holder(item, desktop);
63 } else if (SP_IS_STAR(item)) {
64 return sp_star_knot_holder(item, desktop);
65 } else if (SP_IS_SPIRAL(item)) {
66 return sp_spiral_knot_holder(item, desktop);
67 } else if (SP_IS_OFFSET(item)) {
68 return sp_offset_knot_holder(item, desktop);
69 } else if (SP_IS_PATH(item)) {
70 return sp_path_knot_holder(item, desktop);
71 } else if (SP_IS_FLOWTEXT(item) && SP_FLOWTEXT(item)->has_internal_frame()) {
72 return sp_flowtext_knot_holder(item, desktop);
73 }
75 return NULL;
76 }
79 /* Pattern manipulation */
81 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
82 {
83 gdouble theta = asin(pat->patternTransform[1] / scale);
84 if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
85 return theta;
86 }
88 static gdouble sp_pattern_extract_scale(SPPattern *pat)
89 {
90 gdouble s = pat->patternTransform[1];
91 gdouble c = pat->patternTransform[0];
92 gdouble xscale = sqrt(c * c + s * s);
93 return xscale;
94 }
96 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
97 {
98 return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
99 }
101 static void
102 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
103 {
104 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
106 NR::Point p_snapped = p;
108 if ( state & GDK_CONTROL_MASK ) {
109 if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
110 p_snapped[NR::Y] = origin[NR::Y];
111 } else {
112 p_snapped[NR::X] = origin[NR::X];
113 }
114 }
116 if (state) {
117 NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
118 sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
119 }
121 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
122 }
125 static NR::Point sp_pattern_xy_get(SPItem *item)
126 {
127 SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
128 return sp_pattern_extract_trans(pat);
129 }
131 static NR::Point sp_pattern_angle_get(SPItem *item)
132 {
133 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
135 gdouble x = (pattern_width(pat)*0.5);
136 gdouble y = 0;
137 NR::Point delta = NR::Point(x,y);
138 gdouble scale = sp_pattern_extract_scale(pat);
139 gdouble theta = sp_pattern_extract_theta(pat, scale);
140 delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
141 delta = delta + sp_pattern_extract_trans(pat);
142 return delta;
143 }
145 static void
146 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
147 {
148 int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
150 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
152 // get the angle from pattern 0,0 to the cursor pos
153 NR::Point delta = p - sp_pattern_extract_trans(pat);
154 gdouble theta = atan2(delta);
156 if ( state & GDK_CONTROL_MASK ) {
157 theta = sp_round(theta, M_PI/snaps);
158 }
160 // get the scale from the current transform so we can keep it.
161 gdouble scl = sp_pattern_extract_scale(pat);
162 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
163 NR::Point const t = sp_pattern_extract_trans(pat);
164 rot[4] = t[NR::X];
165 rot[5] = t[NR::Y];
166 sp_item_adjust_pattern(item, rot, true);
167 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
168 }
170 static void
171 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
172 {
173 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
175 // Get the scale from the position of the knotholder,
176 NR::Point d = p - sp_pattern_extract_trans(pat);
177 gdouble s = NR::L2(d);
178 gdouble pat_x = pattern_width(pat) * 0.5;
179 gdouble pat_y = pattern_height(pat) * 0.5;
180 gdouble pat_h = hypot(pat_x, pat_y);
181 gdouble scl = s / pat_h;
183 // get angle from current transform, (need get current scale first to calculate angle)
184 gdouble oldscale = sp_pattern_extract_scale(pat);
185 gdouble theta = sp_pattern_extract_theta(pat,oldscale);
187 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
188 NR::Point const t = sp_pattern_extract_trans(pat);
189 rot[4] = t[NR::X];
190 rot[5] = t[NR::Y];
191 sp_item_adjust_pattern(item, rot, true);
192 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
193 }
196 static NR::Point sp_pattern_scale_get(SPItem *item)
197 {
198 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
200 gdouble x = pattern_width(pat)*0.5;
201 gdouble y = pattern_height(pat)*0.5;
202 NR::Point delta = NR::Point(x,y);
203 NR::Matrix a = pat->patternTransform;
204 a[4] = 0;
205 a[5] = 0;
206 delta = delta * a;
207 delta = delta + sp_pattern_extract_trans(pat);
208 return delta;
209 }
211 static NR::Point sp_rect_rx_get(SPItem *item)
212 {
213 SPRect *rect = SP_RECT(item);
215 return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
216 }
218 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
219 {
220 SPRect *rect = SP_RECT(item);
222 if (state & GDK_CONTROL_MASK) {
223 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
224 rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
225 rect->rx._set = rect->ry._set = true;
227 } else {
228 rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
229 rect->rx._set = true;
230 }
232 ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
233 }
236 static NR::Point sp_rect_ry_get(SPItem *item)
237 {
238 SPRect *rect = SP_RECT(item);
240 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
241 }
243 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
244 {
245 SPRect *rect = SP_RECT(item);
247 if (state & GDK_CONTROL_MASK) {
248 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
249 rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
250 rect->ry._set = rect->rx._set = true;
251 } else {
252 if (!rect->rx._set || rect->rx.computed == 0) {
253 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
254 0.0,
255 MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
256 } else {
257 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
258 0.0,
259 rect->height.computed / 2.0);
260 }
262 rect->ry._set = true;
263 }
265 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
266 }
268 /**
269 * Remove rounding from a rectangle.
270 */
271 static void rect_remove_rounding(SPRect *rect)
272 {
273 SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
274 SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
275 }
277 /**
278 * Called when the horizontal rounding radius knot is clicked.
279 */
280 static void sp_rect_rx_knot_click(SPItem *item, guint state)
281 {
282 SPRect *rect = SP_RECT(item);
284 if (state & GDK_SHIFT_MASK) {
285 rect_remove_rounding(rect);
286 } else if (state & GDK_CONTROL_MASK) {
287 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
288 SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
289 }
290 }
292 /**
293 * Called when the vertical rounding radius knot is clicked.
294 */
295 static void sp_rect_ry_knot_click(SPItem *item, guint state)
296 {
297 SPRect *rect = SP_RECT(item);
299 if (state & GDK_SHIFT_MASK) {
300 rect_remove_rounding(rect);
301 } else if (state & GDK_CONTROL_MASK) {
302 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
303 SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
304 }
305 }
307 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
309 static void sp_rect_clamp_radii(SPRect *rect)
310 {
311 // clamp rounding radii so that they do not exceed width/height
312 if (2 * rect->rx.computed > rect->width.computed) {
313 rect->rx.computed = 0.5 * rect->width.computed;
314 rect->rx._set = true;
315 }
316 if (2 * rect->ry.computed > rect->height.computed) {
317 rect->ry.computed = 0.5 * rect->height.computed;
318 rect->ry._set = true;
319 }
320 }
322 static NR::Point sp_rect_wh_get(SPItem *item)
323 {
324 SPRect *rect = SP_RECT(item);
326 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
327 }
329 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
330 {
331 if (state & GDK_CONTROL_MASK) {
332 // original width/height when drag started
333 gdouble const w_orig = (origin[NR::X] - rect->x.computed);
334 gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
336 //original ratio
337 gdouble const ratio = (w_orig / h_orig);
339 // mouse displacement since drag started
340 gdouble const minx = p[NR::X] - origin[NR::X];
341 gdouble const miny = p[NR::Y] - origin[NR::Y];
343 if (fabs(minx) > fabs(miny)) {
345 // snap to horizontal or diagonal
346 rect->width.computed = MAX(w_orig + minx, 0);
347 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
348 // closer to the diagonal and in same-sign quarters, change both using ratio
349 rect->height.computed = MAX(h_orig + minx / ratio, 0);
350 } else {
351 // closer to the horizontal, change only width, height is h_orig
352 rect->height.computed = MAX(h_orig, 0);
353 }
355 } else {
356 // snap to vertical or diagonal
357 rect->height.computed = MAX(h_orig + miny, 0);
358 if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
359 // closer to the diagonal and in same-sign quarters, change both using ratio
360 rect->width.computed = MAX(w_orig + miny * ratio, 0);
361 } else {
362 // closer to the vertical, change only height, width is w_orig
363 rect->width.computed = MAX(w_orig, 0);
364 }
365 }
367 rect->width._set = rect->height._set = true;
369 } else {
370 // move freely
371 rect->width.computed = MAX(p[NR::X] - rect->x.computed, 0);
372 rect->height.computed = MAX(p[NR::Y] - rect->y.computed, 0);
373 rect->width._set = rect->height._set = true;
374 }
376 sp_rect_clamp_radii(rect);
378 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
379 }
381 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
382 {
383 SPRect *rect = SP_RECT(item);
385 sp_rect_wh_set_internal(rect, p, origin, state);
386 }
388 static NR::Point sp_rect_xy_get(SPItem *item)
389 {
390 SPRect *rect = SP_RECT(item);
392 return NR::Point(rect->x.computed, rect->y.computed);
393 }
395 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
396 {
397 SPRect *rect = SP_RECT(item);
399 // opposite corner (unmoved)
400 gdouble opposite_x = (rect->x.computed + rect->width.computed);
401 gdouble opposite_y = (rect->y.computed + rect->height.computed);
403 // original width/height when drag started
404 gdouble w_orig = opposite_x - origin[NR::X];
405 gdouble h_orig = opposite_y - origin[NR::Y];
407 // mouse displacement since drag started
408 gdouble minx = p[NR::X] - origin[NR::X];
409 gdouble miny = p[NR::Y] - origin[NR::Y];
411 if (state & GDK_CONTROL_MASK) {
412 //original ratio
413 gdouble ratio = (w_orig / h_orig);
415 if (fabs(minx) > fabs(miny)) {
417 // snap to horizontal or diagonal
418 rect->x.computed = MIN(p[NR::X], opposite_x);
419 rect->width.computed = MAX(w_orig - minx, 0);
420 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
421 // closer to the diagonal and in same-sign quarters, change both using ratio
422 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
423 rect->height.computed = MAX(h_orig - minx / ratio, 0);
424 } else {
425 // closer to the horizontal, change only width, height is h_orig
426 rect->y.computed = MIN(origin[NR::Y], opposite_y);
427 rect->height.computed = MAX(h_orig, 0);
428 }
430 } else {
432 // snap to vertical or diagonal
433 rect->y.computed = MIN(p[NR::Y], opposite_y);
434 rect->height.computed = MAX(h_orig - miny, 0);
435 if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
436 // closer to the diagonal and in same-sign quarters, change both using ratio
437 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
438 rect->width.computed = MAX(w_orig - miny * ratio, 0);
439 } else {
440 // closer to the vertical, change only height, width is w_orig
441 rect->x.computed = MIN(origin[NR::X], opposite_x);
442 rect->width.computed = MAX(w_orig, 0);
443 }
445 }
447 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
449 } else {
450 // move freely
451 rect->x.computed = MIN(p[NR::X], opposite_x);
452 rect->width.computed = MAX(w_orig - minx, 0);
453 rect->y.computed = MIN(p[NR::Y], opposite_y);
454 rect->height.computed = MAX(h_orig - miny, 0);
455 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
456 }
458 sp_rect_clamp_radii(rect);
460 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
461 }
463 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
464 {
465 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
467 sp_knot_holder_add_full(
468 knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
469 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
470 _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
471 "radius the same"));
473 sp_knot_holder_add_full(
474 knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
475 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
476 _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
477 "radius the same")
478 );
480 sp_knot_holder_add_full(
481 knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
482 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
483 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
484 "or stretch in one dimension only")
485 );
487 sp_knot_holder_add_full(
488 knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
489 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
490 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
491 "or stretch in one dimension only")
492 );
494 SPRect *rect = SP_RECT(item);
496 sp_pat_knot_holder(item, knot_holder);
497 return knot_holder;
498 }
500 /* SPArc */
502 /*
503 * return values:
504 * 1 : inside
505 * 0 : on the curves
506 * -1 : outside
507 */
508 static gint
509 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
510 {
511 gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
512 gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
514 gdouble s = dx * dx + dy * dy;
515 if (s < 1.0) return 1;
516 if (s > 1.0) return -1;
517 return 0;
518 }
520 static void
521 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
522 {
523 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
525 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
526 SPArc *arc = SP_ARC(item);
528 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
530 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
531 NR::scale sc(ge->rx.computed, ge->ry.computed);
532 ge->start = atan2(delta * sc.inverse());
533 if ( ( state & GDK_CONTROL_MASK )
534 && snaps )
535 {
536 ge->start = sp_round(ge->start, M_PI/snaps);
537 }
538 sp_genericellipse_normalize(ge);
539 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
540 }
542 static NR::Point sp_arc_start_get(SPItem *item)
543 {
544 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
545 SPArc *arc = SP_ARC(item);
547 return sp_arc_get_xy(arc, ge->start);
548 }
550 static void
551 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
552 {
553 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
555 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
556 SPArc *arc = SP_ARC(item);
558 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
560 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
561 NR::scale sc(ge->rx.computed, ge->ry.computed);
562 ge->end = atan2(delta * sc.inverse());
563 if ( ( state & GDK_CONTROL_MASK )
564 && snaps )
565 {
566 ge->end = sp_round(ge->end, M_PI/snaps);
567 }
568 sp_genericellipse_normalize(ge);
569 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
570 }
572 static NR::Point sp_arc_end_get(SPItem *item)
573 {
574 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
575 SPArc *arc = SP_ARC(item);
577 return sp_arc_get_xy(arc, ge->end);
578 }
580 static void
581 sp_arc_startend_click(SPItem *item, guint state)
582 {
583 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
585 if (state & GDK_SHIFT_MASK) {
586 ge->end = ge->start = 0;
587 ((SPObject *)ge)->updateRepr();
588 }
589 }
592 static void
593 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
594 {
595 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
596 SPArc *arc = SP_ARC(item);
598 ge->rx.computed = fabs( ge->cx.computed - p[NR::X] );
600 if ( state & GDK_CONTROL_MASK ) {
601 ge->ry.computed = ge->rx.computed;
602 }
604 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
605 }
607 static NR::Point sp_arc_rx_get(SPItem *item)
608 {
609 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
611 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(ge->rx.computed, 0));
612 }
614 static void
615 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
616 {
617 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
618 SPArc *arc = SP_ARC(item);
620 ge->ry.computed = fabs( ge->cy.computed - p[NR::Y] );
622 if ( state & GDK_CONTROL_MASK ) {
623 ge->rx.computed = ge->ry.computed;
624 }
626 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
627 }
629 static NR::Point sp_arc_ry_get(SPItem *item)
630 {
631 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
633 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(0, ge->ry.computed));
634 }
636 static void
637 sp_arc_rx_click(SPItem *item, guint state)
638 {
639 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
641 if (state & GDK_CONTROL_MASK) {
642 ge->ry.computed = ge->rx.computed;
643 ((SPObject *)ge)->updateRepr();
644 }
645 }
647 static void
648 sp_arc_ry_click(SPItem *item, guint state)
649 {
650 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
652 if (state & GDK_CONTROL_MASK) {
653 ge->rx.computed = ge->ry.computed;
654 ((SPObject *)ge)->updateRepr();
655 }
656 }
658 static SPKnotHolder *
659 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
660 {
661 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
663 sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
664 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
665 _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
666 sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
667 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
668 _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
669 sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
670 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
671 _("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"));
672 sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
673 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
674 _("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"));
676 sp_pat_knot_holder(item, knot_holder);
678 return knot_holder;
679 }
681 /* SPStar */
683 static void
684 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
685 {
686 SPStar *star = SP_STAR(item);
688 NR::Point d = p - star->center;
690 double arg1 = atan2(d);
691 double darg1 = arg1 - star->arg[0];
693 if (state & GDK_MOD1_MASK) {
694 star->randomized = darg1/(star->arg[0] - star->arg[1]);
695 } else if (state & GDK_SHIFT_MASK) {
696 star->rounded = darg1/(star->arg[0] - star->arg[1]);
697 } else if (state & GDK_CONTROL_MASK) {
698 star->r[0] = L2(d);
699 } else {
700 star->r[0] = L2(d);
701 star->arg[0] = arg1;
702 star->arg[1] += darg1;
703 }
704 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
705 }
707 static void
708 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
709 {
710 SPStar *star = SP_STAR(item);
712 if (star->flatsided == false) {
713 NR::Point d = p - star->center;
715 double arg1 = atan2(d);
716 double darg1 = arg1 - star->arg[1];
718 if (state & GDK_MOD1_MASK) {
719 star->randomized = darg1/(star->arg[0] - star->arg[1]);
720 } else if (state & GDK_SHIFT_MASK) {
721 star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
722 } else if (state & GDK_CONTROL_MASK) {
723 star->r[1] = L2(d);
724 star->arg[1] = star->arg[0] + M_PI / star->sides;
725 }
726 else {
727 star->r[1] = L2(d);
728 star->arg[1] = atan2(d);
729 }
730 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
731 }
732 }
734 static NR::Point sp_star_knot1_get(SPItem *item)
735 {
736 g_assert(item != NULL);
738 SPStar *star = SP_STAR(item);
740 return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
742 }
744 static NR::Point sp_star_knot2_get(SPItem *item)
745 {
746 g_assert(item != NULL);
748 SPStar *star = SP_STAR(item);
750 return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
751 }
753 static void
754 sp_star_knot_click(SPItem *item, guint state)
755 {
756 SPStar *star = SP_STAR(item);
758 if (state & GDK_MOD1_MASK) {
759 star->randomized = 0;
760 ((SPObject *)star)->updateRepr();
761 } else if (state & GDK_SHIFT_MASK) {
762 star->rounded = 0;
763 ((SPObject *)star)->updateRepr();
764 } else if (state & GDK_CONTROL_MASK) {
765 star->arg[1] = star->arg[0] + M_PI / star->sides;
766 ((SPObject *)star)->updateRepr();
767 }
768 }
770 static SPKnotHolder *
771 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
772 {
773 /* we don't need to get parent knot_holder */
774 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
775 g_assert(item != NULL);
777 SPStar *star = SP_STAR(item);
779 sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
780 _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
781 if (star->flatsided == false)
782 sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
783 _("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"));
785 sp_pat_knot_holder(item, knot_holder);
787 return knot_holder;
788 }
790 /* SPSpiral */
792 /*
793 * set attributes via inner (t=t0) knot point:
794 * [default] increase/decrease inner point
795 * [shift] increase/decrease inner and outer arg synchronizely
796 * [control] constrain inner arg to round per PI/4
797 */
798 static void
799 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
800 {
801 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
803 SPSpiral *spiral = SP_SPIRAL(item);
805 gdouble dx = p[NR::X] - spiral->cx;
806 gdouble dy = p[NR::Y] - spiral->cy;
808 if (state & GDK_MOD1_MASK) {
809 // adjust divergence by vertical drag, relative to rad
810 double new_exp = (spiral->rad + dy)/(spiral->rad);
811 spiral->exp = new_exp > 0? new_exp : 0;
812 } else {
813 // roll/unroll from inside
814 gdouble arg_t0;
815 sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
817 gdouble arg_tmp = atan2(dy, dx) - arg_t0;
818 gdouble arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
819 spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
821 /* round inner arg per PI/snaps, if CTRL is pressed */
822 if ( ( state & GDK_CONTROL_MASK )
823 && ( fabs(spiral->revo) > SP_EPSILON_2 )
824 && ( snaps != 0 ) ) {
825 gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
826 spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
827 }
829 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
830 }
832 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
833 }
835 /*
836 * set attributes via outer (t=1) knot point:
837 * [default] increase/decrease revolution factor
838 * [control] constrain inner arg to round per PI/4
839 */
840 static void
841 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
842 {
843 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
845 SPSpiral *spiral = SP_SPIRAL(item);
847 gdouble dx = p[NR::X] - spiral->cx;
848 gdouble dy = p[NR::Y] - spiral->cy;
850 if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
851 spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
852 if (!(state & GDK_MOD1_MASK)) {
853 // if alt not pressed, change also rad; otherwise it is locked
854 spiral->rad = MAX(hypot(dx, dy), 0.001);
855 }
856 if ( ( state & GDK_CONTROL_MASK )
857 && snaps ) {
858 spiral->arg = sp_round(spiral->arg, M_PI/snaps);
859 }
860 } else { // roll/unroll
861 // arg of the spiral outer end
862 double arg_1;
863 sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
865 // its fractional part after the whole turns are subtracted
866 double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
868 // arg of the mouse point relative to spiral center
869 double mouse_angle = atan2(dy, dx);
870 if (mouse_angle < 0)
871 mouse_angle += 2*M_PI;
873 // snap if ctrl
874 if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
875 mouse_angle = sp_round(mouse_angle, M_PI/snaps);
876 }
878 // by how much we want to rotate the outer point
879 double diff = mouse_angle - arg_r;
880 if (diff > M_PI)
881 diff -= 2*M_PI;
882 else if (diff < -M_PI)
883 diff += 2*M_PI;
885 // calculate the new rad;
886 // the value of t corresponding to the angle arg_1 + diff:
887 double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
888 // the rad at that t:
889 double rad_new = 0;
890 if (t_temp > spiral->t0)
891 sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
893 // change the revo (converting diff from radians to the number of turns)
894 spiral->revo += diff/(2*M_PI);
895 if (spiral->revo < 1e-3)
896 spiral->revo = 1e-3;
898 // if alt not pressed and the values are sane, change the rad
899 if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
900 // adjust t0 too so that the inner point stays unmoved
901 double r0;
902 sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
903 spiral->rad = rad_new;
904 spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
905 }
906 if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
907 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
908 }
910 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
911 }
913 static NR::Point sp_spiral_inner_get(SPItem *item)
914 {
915 SPSpiral *spiral = SP_SPIRAL(item);
917 return sp_spiral_get_xy(spiral, spiral->t0);
918 }
920 static NR::Point sp_spiral_outer_get(SPItem *item)
921 {
922 SPSpiral *spiral = SP_SPIRAL(item);
924 return sp_spiral_get_xy(spiral, 1.0);
925 }
927 static void
928 sp_spiral_inner_click(SPItem *item, guint state)
929 {
930 SPSpiral *spiral = SP_SPIRAL(item);
932 if (state & GDK_MOD1_MASK) {
933 spiral->exp = 1;
934 ((SPObject *)spiral)->updateRepr();
935 } else if (state & GDK_SHIFT_MASK) {
936 spiral->t0 = 0;
937 ((SPObject *)spiral)->updateRepr();
938 }
939 }
941 static SPKnotHolder *
942 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
943 {
944 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
946 sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
947 _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
948 sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
949 _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
951 sp_pat_knot_holder(item, knot_holder);
953 return knot_holder;
954 }
956 /* SPOffset */
958 static void
959 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
960 {
961 SPOffset *offset = SP_OFFSET(item);
963 offset->rad = sp_offset_distance_to_original(offset, p);
964 offset->knot = p;
965 offset->knotSet = true;
967 ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
968 }
971 static NR::Point sp_offset_offset_get(SPItem *item)
972 {
973 SPOffset *offset = SP_OFFSET(item);
975 NR::Point np;
976 sp_offset_top_point(offset,&np);
977 return np;
978 }
980 static SPKnotHolder *
981 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
982 {
983 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
985 sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
986 _("Adjust the <b>offset distance</b>"));
988 sp_pat_knot_holder(item, knot_holder);
990 return knot_holder;
991 }
993 static SPKnotHolder *
994 sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
995 {
996 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
997 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
998 {
999 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1001 sp_pat_knot_holder(item, knot_holder);
1003 return knot_holder;
1004 }
1005 return NULL;
1006 }
1008 static void
1009 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1010 {
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 sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1015 // TRANSLATORS: This refers to the pattern that's inside the object
1016 _("<b>Move</b> the pattern fill inside the object"));
1017 sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1018 _("<b>Scale</b> the pattern fill uniformly"));
1019 sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1020 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1021 }
1022 }
1024 static NR::Point sp_flowtext_corner_get(SPItem *item)
1025 {
1026 SPRect *rect = SP_RECT(item);
1028 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1029 }
1031 static void
1032 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1033 {
1034 SPRect *rect = SP_RECT(item);
1036 sp_rect_wh_set_internal(rect, p, origin, state);
1037 }
1039 static SPKnotHolder *
1040 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1041 {
1042 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1044 sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1045 _("Drag to resize the <b>flowed text frame</b>"));
1047 return knot_holder;
1048 }
1051 /*
1052 Local Variables:
1053 mode:c++
1054 c-file-style:"stroustrup"
1055 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1056 indent-tabs-mode:nil
1057 fill-column:99
1058 End:
1059 */
1060 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :