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 "desktop-handles.h"
34 #include "sp-namedview.h"
36 #include "sp-pattern.h"
37 #include "sp-path.h"
39 #include <glibmm/i18n.h>
41 #include "object-edit.h"
43 #include <libnr/nr-scale-ops.h>
46 #include "xml/repr.h"
48 #include "isnan.h"
50 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
52 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
53 //static
54 SPKnotHolder *sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop);
55 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
56 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
57 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
58 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
59 static SPKnotHolder *sp_misc_knot_holder(SPItem *item, SPDesktop *desktop);
60 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
61 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
63 SPKnotHolder *
64 sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
65 {
66 if (SP_IS_RECT(item)) {
67 return sp_rect_knot_holder(item, desktop);
68 } else if (SP_IS_3DBOX(item)) {
69 return sp_3dbox_knot_holder(item, desktop);
70 } else if (SP_IS_ARC(item)) {
71 return sp_arc_knot_holder(item, desktop);
72 } else if (SP_IS_STAR(item)) {
73 return sp_star_knot_holder(item, desktop);
74 } else if (SP_IS_SPIRAL(item)) {
75 return sp_spiral_knot_holder(item, desktop);
76 } else if (SP_IS_OFFSET(item)) {
77 return sp_offset_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 } else {
81 return sp_misc_knot_holder(item, desktop);
82 }
84 return NULL;
85 }
88 /* Pattern manipulation */
90 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
91 {
92 gdouble theta = asin(pat->patternTransform[1] / scale);
93 if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
94 return theta;
95 }
97 static gdouble sp_pattern_extract_scale(SPPattern *pat)
98 {
99 gdouble s = pat->patternTransform[1];
100 gdouble c = pat->patternTransform[0];
101 gdouble xscale = sqrt(c * c + s * s);
102 return xscale;
103 }
105 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
106 {
107 return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
108 }
110 static void
111 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
112 {
113 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
115 NR::Point p_snapped = p;
117 if ( state & GDK_CONTROL_MASK ) {
118 if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
119 p_snapped[NR::Y] = origin[NR::Y];
120 } else {
121 p_snapped[NR::X] = origin[NR::X];
122 }
123 }
125 if (state) {
126 NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
127 sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
128 }
130 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
131 }
134 static NR::Point sp_pattern_xy_get(SPItem *item)
135 {
136 SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
137 return sp_pattern_extract_trans(pat);
138 }
140 static NR::Point sp_pattern_angle_get(SPItem *item)
141 {
142 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
144 gdouble x = (pattern_width(pat)*0.5);
145 gdouble y = 0;
146 NR::Point delta = NR::Point(x,y);
147 gdouble scale = sp_pattern_extract_scale(pat);
148 gdouble theta = sp_pattern_extract_theta(pat, scale);
149 delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
150 delta = delta + sp_pattern_extract_trans(pat);
151 return delta;
152 }
154 static void
155 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
156 {
157 int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
159 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
161 // get the angle from pattern 0,0 to the cursor pos
162 NR::Point delta = p - sp_pattern_extract_trans(pat);
163 gdouble theta = atan2(delta);
165 if ( state & GDK_CONTROL_MASK ) {
166 theta = sp_round(theta, M_PI/snaps);
167 }
169 // get the scale from the current transform so we can keep it.
170 gdouble scl = sp_pattern_extract_scale(pat);
171 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
172 NR::Point const t = sp_pattern_extract_trans(pat);
173 rot[4] = t[NR::X];
174 rot[5] = t[NR::Y];
175 sp_item_adjust_pattern(item, rot, true);
176 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
177 }
179 static void
180 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
181 {
182 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
184 // Get the scale from the position of the knotholder,
185 NR::Point d = p - sp_pattern_extract_trans(pat);
186 gdouble s = NR::L2(d);
187 gdouble pat_x = pattern_width(pat) * 0.5;
188 gdouble pat_y = pattern_height(pat) * 0.5;
189 gdouble pat_h = hypot(pat_x, pat_y);
190 gdouble scl = s / pat_h;
192 // get angle from current transform, (need get current scale first to calculate angle)
193 gdouble oldscale = sp_pattern_extract_scale(pat);
194 gdouble theta = sp_pattern_extract_theta(pat,oldscale);
196 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
197 NR::Point const t = sp_pattern_extract_trans(pat);
198 rot[4] = t[NR::X];
199 rot[5] = t[NR::Y];
200 sp_item_adjust_pattern(item, rot, true);
201 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202 }
205 static NR::Point sp_pattern_scale_get(SPItem *item)
206 {
207 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
209 gdouble x = pattern_width(pat)*0.5;
210 gdouble y = pattern_height(pat)*0.5;
211 NR::Point delta = NR::Point(x,y);
212 NR::Matrix a = pat->patternTransform;
213 a[4] = 0;
214 a[5] = 0;
215 delta = delta * a;
216 delta = delta + sp_pattern_extract_trans(pat);
217 return delta;
218 }
220 /* SPRect */
222 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
223 {
224 SPDesktop const *desktop = inkscape_active_desktop();
225 NR::Matrix const i2d (sp_item_i2d_affine (item));
226 NR::Point s = p * i2d;
227 SnapManager const &m = desktop->namedview->snap_manager;
228 s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, s, item).getPoint();
229 return s * i2d.inverse();
230 }
232 static NR::Point sp_rect_rx_get(SPItem *item)
233 {
234 SPRect *rect = SP_RECT(item);
236 return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
237 }
239 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
240 {
241 SPRect *rect = SP_RECT(item);
243 //In general we cannot just snap this radius to an arbitrary point, as we have only a single
244 //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
245 //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
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(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
250 rect->rx._set = rect->ry._set = true;
252 } else {
253 rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
254 rect->rx._set = true;
255 }
257 ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
258 }
261 static NR::Point sp_rect_ry_get(SPItem *item)
262 {
263 SPRect *rect = SP_RECT(item);
265 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
266 }
268 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
269 {
270 SPRect *rect = SP_RECT(item);
272 //In general we cannot just snap this radius to an arbitrary point, as we have only a single
273 //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
274 //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
276 if (state & GDK_CONTROL_MASK) {
277 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
278 rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
279 rect->ry._set = rect->rx._set = true;
280 } else {
281 if (!rect->rx._set || rect->rx.computed == 0) {
282 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
283 0.0,
284 MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
285 } else {
286 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
287 0.0,
288 rect->height.computed / 2.0);
289 }
291 rect->ry._set = true;
292 }
294 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
295 }
297 /**
298 * Remove rounding from a rectangle.
299 */
300 static void rect_remove_rounding(SPRect *rect)
301 {
302 SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
303 SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
304 }
306 /**
307 * Called when the horizontal rounding radius knot is clicked.
308 */
309 static void sp_rect_rx_knot_click(SPItem *item, guint state)
310 {
311 SPRect *rect = SP_RECT(item);
313 if (state & GDK_SHIFT_MASK) {
314 rect_remove_rounding(rect);
315 } else if (state & GDK_CONTROL_MASK) {
316 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
317 SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
318 }
319 }
321 /**
322 * Called when the vertical rounding radius knot is clicked.
323 */
324 static void sp_rect_ry_knot_click(SPItem *item, guint state)
325 {
326 SPRect *rect = SP_RECT(item);
328 if (state & GDK_SHIFT_MASK) {
329 rect_remove_rounding(rect);
330 } else if (state & GDK_CONTROL_MASK) {
331 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
332 SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
333 }
334 }
336 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
338 static void sp_rect_clamp_radii(SPRect *rect)
339 {
340 // clamp rounding radii so that they do not exceed width/height
341 if (2 * rect->rx.computed > rect->width.computed) {
342 rect->rx.computed = 0.5 * rect->width.computed;
343 rect->rx._set = true;
344 }
345 if (2 * rect->ry.computed > rect->height.computed) {
346 rect->ry.computed = 0.5 * rect->height.computed;
347 rect->ry._set = true;
348 }
349 }
351 static NR::Point sp_rect_wh_get(SPItem *item)
352 {
353 SPRect *rect = SP_RECT(item);
355 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
356 }
358 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
359 {
360 NR::Point const s = snap_knot_position(rect, p);
362 if (state & GDK_CONTROL_MASK) {
363 // original width/height when drag started
364 gdouble const w_orig = (origin[NR::X] - rect->x.computed);
365 gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
367 //original ratio
368 gdouble const ratio = (w_orig / h_orig);
370 // mouse displacement since drag started
371 gdouble const minx = s[NR::X] - origin[NR::X];
372 gdouble const miny = s[NR::Y] - origin[NR::Y];
374 if (fabs(minx) > fabs(miny)) {
376 // snap to horizontal or diagonal
377 rect->width.computed = MAX(w_orig + minx, 0);
378 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
379 // closer to the diagonal and in same-sign quarters, change both using ratio
380 rect->height.computed = MAX(h_orig + minx / ratio, 0);
381 } else {
382 // closer to the horizontal, change only width, height is h_orig
383 rect->height.computed = MAX(h_orig, 0);
384 }
386 } else {
387 // snap to vertical or diagonal
388 rect->height.computed = MAX(h_orig + miny, 0);
389 if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
390 // closer to the diagonal and in same-sign quarters, change both using ratio
391 rect->width.computed = MAX(w_orig + miny * ratio, 0);
392 } else {
393 // closer to the vertical, change only height, width is w_orig
394 rect->width.computed = MAX(w_orig, 0);
395 }
396 }
398 rect->width._set = rect->height._set = true;
400 } else {
401 // move freely
402 rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
403 rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
404 rect->width._set = rect->height._set = true;
405 }
407 sp_rect_clamp_radii(rect);
409 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
410 }
412 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
413 {
414 SPRect *rect = SP_RECT(item);
416 sp_rect_wh_set_internal(rect, p, origin, state);
417 }
419 static NR::Point sp_rect_xy_get(SPItem *item)
420 {
421 SPRect *rect = SP_RECT(item);
423 return NR::Point(rect->x.computed, rect->y.computed);
424 }
426 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
427 {
428 SPRect *rect = SP_RECT(item);
430 // opposite corner (unmoved)
431 gdouble opposite_x = (rect->x.computed + rect->width.computed);
432 gdouble opposite_y = (rect->y.computed + rect->height.computed);
434 // original width/height when drag started
435 gdouble w_orig = opposite_x - origin[NR::X];
436 gdouble h_orig = opposite_y - origin[NR::Y];
438 NR::Point const s = snap_knot_position(rect, p);
440 // mouse displacement since drag started
441 gdouble minx = s[NR::X] - origin[NR::X];
442 gdouble miny = s[NR::Y] - origin[NR::Y];
444 if (state & GDK_CONTROL_MASK) {
445 //original ratio
446 gdouble ratio = (w_orig / h_orig);
448 if (fabs(minx) > fabs(miny)) {
450 // snap to horizontal or diagonal
451 rect->x.computed = MIN(s[NR::X], opposite_x);
452 rect->width.computed = MAX(w_orig - minx, 0);
453 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
454 // closer to the diagonal and in same-sign quarters, change both using ratio
455 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
456 rect->height.computed = MAX(h_orig - minx / ratio, 0);
457 } else {
458 // closer to the horizontal, change only width, height is h_orig
459 rect->y.computed = MIN(origin[NR::Y], opposite_y);
460 rect->height.computed = MAX(h_orig, 0);
461 }
463 } else {
465 // snap to vertical or diagonal
466 rect->y.computed = MIN(s[NR::Y], opposite_y);
467 rect->height.computed = MAX(h_orig - miny, 0);
468 if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
469 // closer to the diagonal and in same-sign quarters, change both using ratio
470 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
471 rect->width.computed = MAX(w_orig - miny * ratio, 0);
472 } else {
473 // closer to the vertical, change only height, width is w_orig
474 rect->x.computed = MIN(origin[NR::X], opposite_x);
475 rect->width.computed = MAX(w_orig, 0);
476 }
478 }
480 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
482 } else {
483 // move freely
484 rect->x.computed = MIN(s[NR::X], opposite_x);
485 rect->width.computed = MAX(w_orig - minx, 0);
486 rect->y.computed = MIN(s[NR::Y], opposite_y);
487 rect->height.computed = MAX(h_orig - miny, 0);
488 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
489 }
491 sp_rect_clamp_radii(rect);
493 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
494 }
496 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
497 {
498 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
500 sp_knot_holder_add_full(
501 knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
502 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
503 _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
504 "radius the same"));
506 sp_knot_holder_add_full(
507 knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
508 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
509 _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
510 "radius the same")
511 );
513 sp_knot_holder_add_full(
514 knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
515 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
516 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
517 "or stretch in one dimension only")
518 );
520 sp_knot_holder_add_full(
521 knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
522 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
523 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
524 "or stretch in one dimension only")
525 );
527 sp_pat_knot_holder(item, knot_holder);
528 return knot_holder;
529 }
531 /* 3D Box */
533 static inline Box3D::Axis movement_axis_of_3dbox_corner (guint corner, guint state)
534 {
535 // this function has the purpose to simplify a change in the resizing behaviour of boxes
536 switch (corner) {
537 case 0:
538 case 1:
539 case 2:
540 case 3:
541 return ((state & GDK_SHIFT_MASK) ? Box3D::Z : Box3D::XY);
542 case 4:
543 case 5:
544 case 6:
545 case 7:
546 return ((state & GDK_SHIFT_MASK) ? Box3D::XY : Box3D::Z);
547 }
548 }
550 /*
551 * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
552 * their intersection, we remember the last snapped line and keep snapping to this specific line as long
553 * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
554 */
556 // Should we make the threshold settable in the preferences?
557 static double remember_snap_threshold = 20;
558 static guint remember_snap_index = 0;
560 static NR::Point snap_knot_position_3dbox (SP3DBox *box, guint corner, Box3D::Axis direction, NR::Point const &origin, NR::Point const &p, guint state)
561 {
562 SPDesktop * desktop = inkscape_active_desktop();
563 Box3D::Perspective3D *persp = sp_desktop_document (desktop)->get_persp_of_box (box);
565 g_return_val_if_fail (!is_single_axis_direction (direction), p);
567 Box3D::Axis axis1 = Box3D::extract_first_axis_direction (direction);
568 Box3D::Axis axis2 = Box3D::extract_second_axis_direction (direction);
570 NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (box)));
571 NR::Point origin_dt = origin * i2d;
572 NR::Point p_dt = p * i2d;
574 Box3D::PerspectiveLine pl1 (origin_dt, axis1, persp);
575 Box3D::PerspectiveLine pl2 (origin_dt, axis2, persp);
576 Box3D::Line diag1 (origin_dt, box->corners[corner ^ Box3D::XY]);
578 int num_snap_lines = 3;
579 NR::Point snap_pts[num_snap_lines];
581 snap_pts[0] = pl1.closest_to (p_dt);
582 snap_pts[1] = pl2.closest_to (p_dt);
583 snap_pts[2] = diag1.closest_to (p_dt);
585 gdouble const zoom = desktop->current_zoom();
587 double snap_dists[num_snap_lines];
589 for (int i = 0; i < num_snap_lines; ++i) {
590 snap_dists[i] = NR::L2 (snap_pts[i] - p_dt) * zoom;
591 }
593 bool within_tolerance = true;
594 for (int i = 0; i < num_snap_lines; ++i) {
595 if (snap_dists[i] > remember_snap_threshold) {
596 within_tolerance = false;
597 break;
598 }
599 }
601 int snap_index = -1;
602 double snap_dist = NR_HUGE;
603 for (int i = 0; i < num_snap_lines; ++i) {
604 if (snap_dists[i] < snap_dist) {
605 snap_index = i;
606 snap_dist = snap_dists[i];
607 }
608 }
610 if (within_tolerance) {
611 return snap_pts[remember_snap_index] * i2d.inverse();
612 } else {
613 remember_snap_index = snap_index;
614 return snap_pts[snap_index] * i2d.inverse();
615 }
616 }
618 static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
619 {
620 g_assert(item != NULL);
621 SP3DBox *box = SP_3DBOX(item);
623 NR::Matrix const i2d (sp_item_i2d_affine (item));
624 return sp_3dbox_get_corner(box, knot_id) * i2d;
625 }
627 static void sp_3dbox_knot_set(SPItem *item, guint knot_id, NR::Point const &new_pos, NR::Point const &origin, guint state)
628 {
629 g_assert(item != NULL);
630 SP3DBox *box = SP_3DBOX(item);
632 NR::Matrix const i2d (sp_item_i2d_affine (item));
633 Box3D::Axis direction = movement_axis_of_3dbox_corner (knot_id, state);
634 if ((state & GDK_CONTROL_MASK) && !is_single_axis_direction (direction)) {
635 // snap if Ctrl is pressed and movement isn't already constrained to a single axis
636 NR::Point const s = snap_knot_position_3dbox (box, knot_id, direction, origin, new_pos, state);
637 sp_3dbox_move_corner_in_Z_direction (box, knot_id, s * i2d, false);
638 } else {
639 if (direction == Box3D::Z) {
640 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, true);
641 } else {
642 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, false);
643 }
644 }
645 sp_3dbox_update_curves (box);
646 sp_3dbox_set_ratios (box);
647 sp_3dbox_update_perspective_lines ();
648 sp_3dbox_set_z_orders (box);
649 }
651 static void sp_3dbox_knot0_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
652 {
653 sp_3dbox_knot_set(item, 0, new_pos, origin, state);
654 }
656 static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
657 {
658 sp_3dbox_knot_set(item, 1, new_pos, origin, state);
659 }
661 static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
662 {
663 sp_3dbox_knot_set(item, 2, new_pos, origin, state);
664 }
666 static void sp_3dbox_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
667 {
668 sp_3dbox_knot_set(item, 3, new_pos, origin, state);
669 }
671 static void sp_3dbox_knot4_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
672 {
673 sp_3dbox_knot_set(item, 4, new_pos, origin, state);
674 }
676 static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
677 {
678 sp_3dbox_knot_set(item, 5, new_pos, origin, state);
679 }
681 static void sp_3dbox_knot6_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
682 {
683 sp_3dbox_knot_set(item, 6, new_pos, origin, state);
684 }
686 static void sp_3dbox_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
687 {
688 sp_3dbox_knot_set(item, 7, new_pos, origin, state);
689 }
691 static NR::Point sp_3dbox_knot0_get(SPItem *item)
692 {
693 return sp_3dbox_knot_get(item, 0);
694 }
696 static NR::Point sp_3dbox_knot1_get(SPItem *item)
697 {
698 return sp_3dbox_knot_get(item, 1);
699 }
701 static NR::Point sp_3dbox_knot2_get(SPItem *item)
702 {
703 return sp_3dbox_knot_get(item, 2);
704 }
706 static NR::Point sp_3dbox_knot3_get(SPItem *item)
707 {
708 return sp_3dbox_knot_get(item, 3);
709 }
711 static NR::Point sp_3dbox_knot4_get(SPItem *item)
712 {
713 return sp_3dbox_knot_get(item, 4);
714 }
716 static NR::Point sp_3dbox_knot5_get(SPItem *item)
717 {
718 return sp_3dbox_knot_get(item, 5);
719 }
721 static NR::Point sp_3dbox_knot6_get(SPItem *item)
722 {
723 return sp_3dbox_knot_get(item, 6);
724 }
726 static NR::Point sp_3dbox_knot7_get(SPItem *item)
727 {
728 return sp_3dbox_knot_get(item, 7);
729 }
732 //static
733 SPKnotHolder *
734 sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop)
735 {
736 g_assert(item != NULL);
737 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
739 sp_knot_holder_add(knot_holder, sp_3dbox_knot0_set, sp_3dbox_knot0_get, NULL,
740 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
741 sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,
742 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
743 sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,
744 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
745 sp_knot_holder_add(knot_holder, sp_3dbox_knot3_set, sp_3dbox_knot3_get, NULL,
746 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
747 sp_knot_holder_add(knot_holder, sp_3dbox_knot4_set, sp_3dbox_knot4_get, NULL,
748 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
749 sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,
750 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
751 sp_knot_holder_add(knot_holder, sp_3dbox_knot6_set, sp_3dbox_knot6_get, NULL,
752 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
753 sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set, sp_3dbox_knot7_get, NULL,
754 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
755 sp_pat_knot_holder(item, knot_holder);
757 return knot_holder;
758 }
760 /* SPArc */
762 /*
763 * return values:
764 * 1 : inside
765 * 0 : on the curves
766 * -1 : outside
767 */
768 static gint
769 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
770 {
771 gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
772 gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
774 gdouble s = dx * dx + dy * dy;
775 if (s < 1.0) return 1;
776 if (s > 1.0) return -1;
777 return 0;
778 }
780 static void
781 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
782 {
783 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
785 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
786 SPArc *arc = SP_ARC(item);
788 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
790 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
791 NR::scale sc(ge->rx.computed, ge->ry.computed);
792 ge->start = atan2(delta * sc.inverse());
793 if ( ( state & GDK_CONTROL_MASK )
794 && snaps )
795 {
796 ge->start = sp_round(ge->start, M_PI/snaps);
797 }
798 sp_genericellipse_normalize(ge);
799 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
800 }
802 static NR::Point sp_arc_start_get(SPItem *item)
803 {
804 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
805 SPArc *arc = SP_ARC(item);
807 return sp_arc_get_xy(arc, ge->start);
808 }
810 static void
811 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
812 {
813 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
815 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
816 SPArc *arc = SP_ARC(item);
818 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
820 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
821 NR::scale sc(ge->rx.computed, ge->ry.computed);
822 ge->end = atan2(delta * sc.inverse());
823 if ( ( state & GDK_CONTROL_MASK )
824 && snaps )
825 {
826 ge->end = sp_round(ge->end, M_PI/snaps);
827 }
828 sp_genericellipse_normalize(ge);
829 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
830 }
832 static NR::Point sp_arc_end_get(SPItem *item)
833 {
834 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
835 SPArc *arc = SP_ARC(item);
837 return sp_arc_get_xy(arc, ge->end);
838 }
840 static void
841 sp_arc_startend_click(SPItem *item, guint state)
842 {
843 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
845 if (state & GDK_SHIFT_MASK) {
846 ge->end = ge->start = 0;
847 ((SPObject *)ge)->updateRepr();
848 }
849 }
852 static void
853 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
854 {
855 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
856 SPArc *arc = SP_ARC(item);
858 NR::Point const s = snap_knot_position(arc, p);
860 ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
862 if ( state & GDK_CONTROL_MASK ) {
863 ge->ry.computed = ge->rx.computed;
864 }
866 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
867 }
869 static NR::Point sp_arc_rx_get(SPItem *item)
870 {
871 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
873 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(ge->rx.computed, 0));
874 }
876 static void
877 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
878 {
879 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
880 SPArc *arc = SP_ARC(item);
882 NR::Point const s = snap_knot_position(arc, p);
884 ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
886 if ( state & GDK_CONTROL_MASK ) {
887 ge->rx.computed = ge->ry.computed;
888 }
890 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
891 }
893 static NR::Point sp_arc_ry_get(SPItem *item)
894 {
895 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
897 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(0, ge->ry.computed));
898 }
900 static void
901 sp_arc_rx_click(SPItem *item, guint state)
902 {
903 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
905 if (state & GDK_CONTROL_MASK) {
906 ge->ry.computed = ge->rx.computed;
907 ((SPObject *)ge)->updateRepr();
908 }
909 }
911 static void
912 sp_arc_ry_click(SPItem *item, guint state)
913 {
914 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
916 if (state & GDK_CONTROL_MASK) {
917 ge->rx.computed = ge->ry.computed;
918 ((SPObject *)ge)->updateRepr();
919 }
920 }
922 static SPKnotHolder *
923 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
924 {
925 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
927 sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
928 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
929 _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
930 sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
931 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
932 _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
933 sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
934 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
935 _("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"));
936 sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
937 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
938 _("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"));
940 sp_pat_knot_holder(item, knot_holder);
942 return knot_holder;
943 }
945 /* SPStar */
947 static void
948 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
949 {
950 SPStar *star = SP_STAR(item);
952 NR::Point const s = snap_knot_position(star, p);
954 NR::Point d = s - star->center;
956 double arg1 = atan2(d);
957 double darg1 = arg1 - star->arg[0];
959 if (state & GDK_MOD1_MASK) {
960 star->randomized = darg1/(star->arg[0] - star->arg[1]);
961 } else if (state & GDK_SHIFT_MASK) {
962 star->rounded = darg1/(star->arg[0] - star->arg[1]);
963 } else if (state & GDK_CONTROL_MASK) {
964 star->r[0] = L2(d);
965 } else {
966 star->r[0] = L2(d);
967 star->arg[0] = arg1;
968 star->arg[1] += darg1;
969 }
970 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
971 }
973 static void
974 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
975 {
976 SPStar *star = SP_STAR(item);
978 NR::Point const s = snap_knot_position(star, p);
980 if (star->flatsided == false) {
981 NR::Point d = s - star->center;
983 double arg1 = atan2(d);
984 double darg1 = arg1 - star->arg[1];
986 if (state & GDK_MOD1_MASK) {
987 star->randomized = darg1/(star->arg[0] - star->arg[1]);
988 } else if (state & GDK_SHIFT_MASK) {
989 star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
990 } else if (state & GDK_CONTROL_MASK) {
991 star->r[1] = L2(d);
992 star->arg[1] = star->arg[0] + M_PI / star->sides;
993 }
994 else {
995 star->r[1] = L2(d);
996 star->arg[1] = atan2(d);
997 }
998 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
999 }
1000 }
1002 static NR::Point sp_star_knot1_get(SPItem *item)
1003 {
1004 g_assert(item != NULL);
1006 SPStar *star = SP_STAR(item);
1008 return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
1010 }
1012 static NR::Point sp_star_knot2_get(SPItem *item)
1013 {
1014 g_assert(item != NULL);
1016 SPStar *star = SP_STAR(item);
1018 return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
1019 }
1021 static void
1022 sp_star_knot_click(SPItem *item, guint state)
1023 {
1024 SPStar *star = SP_STAR(item);
1026 if (state & GDK_MOD1_MASK) {
1027 star->randomized = 0;
1028 ((SPObject *)star)->updateRepr();
1029 } else if (state & GDK_SHIFT_MASK) {
1030 star->rounded = 0;
1031 ((SPObject *)star)->updateRepr();
1032 } else if (state & GDK_CONTROL_MASK) {
1033 star->arg[1] = star->arg[0] + M_PI / star->sides;
1034 ((SPObject *)star)->updateRepr();
1035 }
1036 }
1038 static SPKnotHolder *
1039 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
1040 {
1041 /* we don't need to get parent knot_holder */
1042 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1043 g_assert(item != NULL);
1045 SPStar *star = SP_STAR(item);
1047 sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
1048 _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1049 if (star->flatsided == false)
1050 sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
1051 _("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"));
1053 sp_pat_knot_holder(item, knot_holder);
1055 return knot_holder;
1056 }
1058 /* SPSpiral */
1060 /*
1061 * set attributes via inner (t=t0) knot point:
1062 * [default] increase/decrease inner point
1063 * [shift] increase/decrease inner and outer arg synchronizely
1064 * [control] constrain inner arg to round per PI/4
1065 */
1066 static void
1067 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1068 {
1069 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1071 SPSpiral *spiral = SP_SPIRAL(item);
1073 gdouble dx = p[NR::X] - spiral->cx;
1074 gdouble dy = p[NR::Y] - spiral->cy;
1076 if (state & GDK_MOD1_MASK) {
1077 // adjust divergence by vertical drag, relative to rad
1078 double new_exp = (spiral->rad + dy)/(spiral->rad);
1079 spiral->exp = new_exp > 0? new_exp : 0;
1080 } else {
1081 // roll/unroll from inside
1082 gdouble arg_t0;
1083 sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
1085 gdouble arg_tmp = atan2(dy, dx) - arg_t0;
1086 gdouble arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
1087 spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
1089 /* round inner arg per PI/snaps, if CTRL is pressed */
1090 if ( ( state & GDK_CONTROL_MASK )
1091 && ( fabs(spiral->revo) > SP_EPSILON_2 )
1092 && ( snaps != 0 ) ) {
1093 gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
1094 spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
1095 }
1097 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1098 }
1100 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1101 }
1103 /*
1104 * set attributes via outer (t=1) knot point:
1105 * [default] increase/decrease revolution factor
1106 * [control] constrain inner arg to round per PI/4
1107 */
1108 static void
1109 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1110 {
1111 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1113 SPSpiral *spiral = SP_SPIRAL(item);
1115 gdouble dx = p[NR::X] - spiral->cx;
1116 gdouble dy = p[NR::Y] - spiral->cy;
1118 if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
1119 spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
1120 if (!(state & GDK_MOD1_MASK)) {
1121 // if alt not pressed, change also rad; otherwise it is locked
1122 spiral->rad = MAX(hypot(dx, dy), 0.001);
1123 }
1124 if ( ( state & GDK_CONTROL_MASK )
1125 && snaps ) {
1126 spiral->arg = sp_round(spiral->arg, M_PI/snaps);
1127 }
1128 } else { // roll/unroll
1129 // arg of the spiral outer end
1130 double arg_1;
1131 sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
1133 // its fractional part after the whole turns are subtracted
1134 double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
1136 // arg of the mouse point relative to spiral center
1137 double mouse_angle = atan2(dy, dx);
1138 if (mouse_angle < 0)
1139 mouse_angle += 2*M_PI;
1141 // snap if ctrl
1142 if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
1143 mouse_angle = sp_round(mouse_angle, M_PI/snaps);
1144 }
1146 // by how much we want to rotate the outer point
1147 double diff = mouse_angle - arg_r;
1148 if (diff > M_PI)
1149 diff -= 2*M_PI;
1150 else if (diff < -M_PI)
1151 diff += 2*M_PI;
1153 // calculate the new rad;
1154 // the value of t corresponding to the angle arg_1 + diff:
1155 double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
1156 // the rad at that t:
1157 double rad_new = 0;
1158 if (t_temp > spiral->t0)
1159 sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
1161 // change the revo (converting diff from radians to the number of turns)
1162 spiral->revo += diff/(2*M_PI);
1163 if (spiral->revo < 1e-3)
1164 spiral->revo = 1e-3;
1166 // if alt not pressed and the values are sane, change the rad
1167 if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
1168 // adjust t0 too so that the inner point stays unmoved
1169 double r0;
1170 sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
1171 spiral->rad = rad_new;
1172 spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
1173 }
1174 if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
1175 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1176 }
1178 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1179 }
1181 static NR::Point sp_spiral_inner_get(SPItem *item)
1182 {
1183 SPSpiral *spiral = SP_SPIRAL(item);
1185 return sp_spiral_get_xy(spiral, spiral->t0);
1186 }
1188 static NR::Point sp_spiral_outer_get(SPItem *item)
1189 {
1190 SPSpiral *spiral = SP_SPIRAL(item);
1192 return sp_spiral_get_xy(spiral, 1.0);
1193 }
1195 static void
1196 sp_spiral_inner_click(SPItem *item, guint state)
1197 {
1198 SPSpiral *spiral = SP_SPIRAL(item);
1200 if (state & GDK_MOD1_MASK) {
1201 spiral->exp = 1;
1202 ((SPObject *)spiral)->updateRepr();
1203 } else if (state & GDK_SHIFT_MASK) {
1204 spiral->t0 = 0;
1205 ((SPObject *)spiral)->updateRepr();
1206 }
1207 }
1209 static SPKnotHolder *
1210 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
1211 {
1212 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1214 sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
1215 _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
1216 sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
1217 _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
1219 sp_pat_knot_holder(item, knot_holder);
1221 return knot_holder;
1222 }
1224 /* SPOffset */
1226 static void
1227 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1228 {
1229 SPOffset *offset = SP_OFFSET(item);
1231 offset->rad = sp_offset_distance_to_original(offset, p);
1232 offset->knot = p;
1233 offset->knotSet = true;
1235 ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1236 }
1239 static NR::Point sp_offset_offset_get(SPItem *item)
1240 {
1241 SPOffset *offset = SP_OFFSET(item);
1243 NR::Point np;
1244 sp_offset_top_point(offset,&np);
1245 return np;
1246 }
1248 static SPKnotHolder *
1249 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1250 {
1251 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1253 sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1254 _("Adjust the <b>offset distance</b>"));
1256 sp_pat_knot_holder(item, knot_holder);
1258 return knot_holder;
1259 }
1261 static SPKnotHolder *
1262 sp_misc_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1263 {
1264 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1265 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1266 {
1267 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1269 sp_pat_knot_holder(item, knot_holder);
1271 return knot_holder;
1272 }
1273 return NULL;
1274 }
1276 static void
1277 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1278 {
1279 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1280 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1281 {
1282 sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1283 // TRANSLATORS: This refers to the pattern that's inside the object
1284 _("<b>Move</b> the pattern fill inside the object"));
1285 sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1286 _("<b>Scale</b> the pattern fill uniformly"));
1287 sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1288 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1289 }
1290 }
1292 static NR::Point sp_flowtext_corner_get(SPItem *item)
1293 {
1294 SPRect *rect = SP_RECT(item);
1296 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1297 }
1299 static void
1300 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1301 {
1302 SPRect *rect = SP_RECT(item);
1304 sp_rect_wh_set_internal(rect, p, origin, state);
1305 }
1307 static SPKnotHolder *
1308 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1309 {
1310 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1312 sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1313 _("Drag to resize the <b>flowed text frame</b>"));
1315 return knot_holder;
1316 }
1319 /*
1320 Local Variables:
1321 mode:c++
1322 c-file-style:"stroustrup"
1323 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1324 indent-tabs-mode:nil
1325 fill-column:99
1326 End:
1327 */
1328 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :