1 #define __SP_OBJECT_EDIT_C__
3 /*
4 * Node editing extension to objects
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Mitsuru Oka
9 *
10 * Licensed under GNU GPL
11 */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
19 #include "sp-item.h"
20 #include "sp-rect.h"
21 #include "box3d.h"
22 #include "sp-ellipse.h"
23 #include "sp-star.h"
24 #include "sp-spiral.h"
25 #include "sp-offset.h"
26 #include "sp-flowtext.h"
27 #include "prefs-utils.h"
28 #include "inkscape.h"
29 #include "snap.h"
30 #include "desktop-affine.h"
31 #include <style.h>
32 #include "desktop.h"
33 #include "sp-namedview.h"
35 #include "sp-pattern.h"
36 #include "sp-path.h"
38 #include <glibmm/i18n.h>
40 #include "object-edit.h"
42 #include <libnr/nr-scale-ops.h>
45 #include "xml/repr.h"
47 #include "isnan.h"
49 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
51 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
52 //static
53 SPKnotHolder *sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop, guint number_of_handles);
54 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
55 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
56 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
57 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
58 static SPKnotHolder *sp_misc_knot_holder(SPItem *item, SPDesktop *desktop);
59 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
60 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
62 SPKnotHolder *
63 sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
64 {
65 if (SP_IS_RECT(item)) {
66 return sp_rect_knot_holder(item, desktop);
67 } else if (SP_IS_3DBOX(item)) {
68 return sp_3dbox_knot_holder(item, desktop, SP3DBoxContext::number_of_handles);
69 } else if (SP_IS_ARC(item)) {
70 return sp_arc_knot_holder(item, desktop);
71 } else if (SP_IS_STAR(item)) {
72 return sp_star_knot_holder(item, desktop);
73 } else if (SP_IS_SPIRAL(item)) {
74 return sp_spiral_knot_holder(item, desktop);
75 } else if (SP_IS_OFFSET(item)) {
76 return sp_offset_knot_holder(item, desktop);
77 } else if (SP_IS_FLOWTEXT(item) && SP_FLOWTEXT(item)->has_internal_frame()) {
78 return sp_flowtext_knot_holder(item, desktop);
79 } else {
80 return sp_misc_knot_holder(item, desktop);
81 }
83 return NULL;
84 }
87 /* Pattern manipulation */
89 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
90 {
91 gdouble theta = asin(pat->patternTransform[1] / scale);
92 if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
93 return theta;
94 }
96 static gdouble sp_pattern_extract_scale(SPPattern *pat)
97 {
98 gdouble s = pat->patternTransform[1];
99 gdouble c = pat->patternTransform[0];
100 gdouble xscale = sqrt(c * c + s * s);
101 return xscale;
102 }
104 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
105 {
106 return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
107 }
109 static void
110 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
111 {
112 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
114 NR::Point p_snapped = p;
116 if ( state & GDK_CONTROL_MASK ) {
117 if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
118 p_snapped[NR::Y] = origin[NR::Y];
119 } else {
120 p_snapped[NR::X] = origin[NR::X];
121 }
122 }
124 if (state) {
125 NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
126 sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
127 }
129 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
130 }
133 static NR::Point sp_pattern_xy_get(SPItem *item)
134 {
135 SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
136 return sp_pattern_extract_trans(pat);
137 }
139 static NR::Point sp_pattern_angle_get(SPItem *item)
140 {
141 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
143 gdouble x = (pattern_width(pat)*0.5);
144 gdouble y = 0;
145 NR::Point delta = NR::Point(x,y);
146 gdouble scale = sp_pattern_extract_scale(pat);
147 gdouble theta = sp_pattern_extract_theta(pat, scale);
148 delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
149 delta = delta + sp_pattern_extract_trans(pat);
150 return delta;
151 }
153 static void
154 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
155 {
156 int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
158 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
160 // get the angle from pattern 0,0 to the cursor pos
161 NR::Point delta = p - sp_pattern_extract_trans(pat);
162 gdouble theta = atan2(delta);
164 if ( state & GDK_CONTROL_MASK ) {
165 theta = sp_round(theta, M_PI/snaps);
166 }
168 // get the scale from the current transform so we can keep it.
169 gdouble scl = sp_pattern_extract_scale(pat);
170 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
171 NR::Point const t = sp_pattern_extract_trans(pat);
172 rot[4] = t[NR::X];
173 rot[5] = t[NR::Y];
174 sp_item_adjust_pattern(item, rot, true);
175 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
176 }
178 static void
179 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
180 {
181 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
183 // Get the scale from the position of the knotholder,
184 NR::Point d = p - sp_pattern_extract_trans(pat);
185 gdouble s = NR::L2(d);
186 gdouble pat_x = pattern_width(pat) * 0.5;
187 gdouble pat_y = pattern_height(pat) * 0.5;
188 gdouble pat_h = hypot(pat_x, pat_y);
189 gdouble scl = s / pat_h;
191 // get angle from current transform, (need get current scale first to calculate angle)
192 gdouble oldscale = sp_pattern_extract_scale(pat);
193 gdouble theta = sp_pattern_extract_theta(pat,oldscale);
195 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
196 NR::Point const t = sp_pattern_extract_trans(pat);
197 rot[4] = t[NR::X];
198 rot[5] = t[NR::Y];
199 sp_item_adjust_pattern(item, rot, true);
200 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
201 }
204 static NR::Point sp_pattern_scale_get(SPItem *item)
205 {
206 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
208 gdouble x = pattern_width(pat)*0.5;
209 gdouble y = pattern_height(pat)*0.5;
210 NR::Point delta = NR::Point(x,y);
211 NR::Matrix a = pat->patternTransform;
212 a[4] = 0;
213 a[5] = 0;
214 delta = delta * a;
215 delta = delta + sp_pattern_extract_trans(pat);
216 return delta;
217 }
219 /* SPRect */
221 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
222 {
223 SPDesktop const *desktop = inkscape_active_desktop();
224 NR::Matrix const i2d (sp_item_i2d_affine (item));
225 NR::Point s = p * i2d;
226 SnapManager const &m = desktop->namedview->snap_manager;
227 s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, s, item).getPoint();
228 return s * i2d.inverse();
229 }
231 static NR::Point sp_rect_rx_get(SPItem *item)
232 {
233 SPRect *rect = SP_RECT(item);
235 return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
236 }
238 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
239 {
240 SPRect *rect = SP_RECT(item);
242 //In general we cannot just snap this radius to an arbitrary point, as we have only a single
243 //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
244 //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
246 if (state & GDK_CONTROL_MASK) {
247 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
248 rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
249 rect->rx._set = rect->ry._set = true;
251 } else {
252 rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
253 rect->rx._set = true;
254 }
256 ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
257 }
260 static NR::Point sp_rect_ry_get(SPItem *item)
261 {
262 SPRect *rect = SP_RECT(item);
264 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
265 }
267 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
268 {
269 SPRect *rect = SP_RECT(item);
271 //In general we cannot just snap this radius to an arbitrary point, as we have only a single
272 //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
273 //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
275 if (state & GDK_CONTROL_MASK) {
276 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
277 rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
278 rect->ry._set = rect->rx._set = true;
279 } else {
280 if (!rect->rx._set || rect->rx.computed == 0) {
281 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
282 0.0,
283 MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
284 } else {
285 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
286 0.0,
287 rect->height.computed / 2.0);
288 }
290 rect->ry._set = true;
291 }
293 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
294 }
296 /**
297 * Remove rounding from a rectangle.
298 */
299 static void rect_remove_rounding(SPRect *rect)
300 {
301 SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
302 SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
303 }
305 /**
306 * Called when the horizontal rounding radius knot is clicked.
307 */
308 static void sp_rect_rx_knot_click(SPItem *item, guint state)
309 {
310 SPRect *rect = SP_RECT(item);
312 if (state & GDK_SHIFT_MASK) {
313 rect_remove_rounding(rect);
314 } else if (state & GDK_CONTROL_MASK) {
315 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
316 SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
317 }
318 }
320 /**
321 * Called when the vertical rounding radius knot is clicked.
322 */
323 static void sp_rect_ry_knot_click(SPItem *item, guint state)
324 {
325 SPRect *rect = SP_RECT(item);
327 if (state & GDK_SHIFT_MASK) {
328 rect_remove_rounding(rect);
329 } else if (state & GDK_CONTROL_MASK) {
330 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
331 SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
332 }
333 }
335 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
337 static void sp_rect_clamp_radii(SPRect *rect)
338 {
339 // clamp rounding radii so that they do not exceed width/height
340 if (2 * rect->rx.computed > rect->width.computed) {
341 rect->rx.computed = 0.5 * rect->width.computed;
342 rect->rx._set = true;
343 }
344 if (2 * rect->ry.computed > rect->height.computed) {
345 rect->ry.computed = 0.5 * rect->height.computed;
346 rect->ry._set = true;
347 }
348 }
350 static NR::Point sp_rect_wh_get(SPItem *item)
351 {
352 SPRect *rect = SP_RECT(item);
354 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
355 }
357 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
358 {
359 NR::Point const s = snap_knot_position(rect, p);
361 if (state & GDK_CONTROL_MASK) {
362 // original width/height when drag started
363 gdouble const w_orig = (origin[NR::X] - rect->x.computed);
364 gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
366 //original ratio
367 gdouble const ratio = (w_orig / h_orig);
369 // mouse displacement since drag started
370 gdouble const minx = s[NR::X] - origin[NR::X];
371 gdouble const miny = s[NR::Y] - origin[NR::Y];
373 if (fabs(minx) > fabs(miny)) {
375 // snap to horizontal or diagonal
376 rect->width.computed = MAX(w_orig + minx, 0);
377 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
378 // closer to the diagonal and in same-sign quarters, change both using ratio
379 rect->height.computed = MAX(h_orig + minx / ratio, 0);
380 } else {
381 // closer to the horizontal, change only width, height is h_orig
382 rect->height.computed = MAX(h_orig, 0);
383 }
385 } else {
386 // snap to vertical or diagonal
387 rect->height.computed = MAX(h_orig + miny, 0);
388 if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
389 // closer to the diagonal and in same-sign quarters, change both using ratio
390 rect->width.computed = MAX(w_orig + miny * ratio, 0);
391 } else {
392 // closer to the vertical, change only height, width is w_orig
393 rect->width.computed = MAX(w_orig, 0);
394 }
395 }
397 rect->width._set = rect->height._set = true;
399 } else {
400 // move freely
401 rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
402 rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
403 rect->width._set = rect->height._set = true;
404 }
406 sp_rect_clamp_radii(rect);
408 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
409 }
411 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
412 {
413 SPRect *rect = SP_RECT(item);
415 sp_rect_wh_set_internal(rect, p, origin, state);
416 }
418 static NR::Point sp_rect_xy_get(SPItem *item)
419 {
420 SPRect *rect = SP_RECT(item);
422 return NR::Point(rect->x.computed, rect->y.computed);
423 }
425 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
426 {
427 SPRect *rect = SP_RECT(item);
429 // opposite corner (unmoved)
430 gdouble opposite_x = (rect->x.computed + rect->width.computed);
431 gdouble opposite_y = (rect->y.computed + rect->height.computed);
433 // original width/height when drag started
434 gdouble w_orig = opposite_x - origin[NR::X];
435 gdouble h_orig = opposite_y - origin[NR::Y];
437 NR::Point const s = snap_knot_position(rect, p);
439 // mouse displacement since drag started
440 gdouble minx = s[NR::X] - origin[NR::X];
441 gdouble miny = s[NR::Y] - origin[NR::Y];
443 if (state & GDK_CONTROL_MASK) {
444 //original ratio
445 gdouble ratio = (w_orig / h_orig);
447 if (fabs(minx) > fabs(miny)) {
449 // snap to horizontal or diagonal
450 rect->x.computed = MIN(s[NR::X], opposite_x);
451 rect->width.computed = MAX(w_orig - minx, 0);
452 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
453 // closer to the diagonal and in same-sign quarters, change both using ratio
454 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
455 rect->height.computed = MAX(h_orig - minx / ratio, 0);
456 } else {
457 // closer to the horizontal, change only width, height is h_orig
458 rect->y.computed = MIN(origin[NR::Y], opposite_y);
459 rect->height.computed = MAX(h_orig, 0);
460 }
462 } else {
464 // snap to vertical or diagonal
465 rect->y.computed = MIN(s[NR::Y], opposite_y);
466 rect->height.computed = MAX(h_orig - miny, 0);
467 if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
468 // closer to the diagonal and in same-sign quarters, change both using ratio
469 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
470 rect->width.computed = MAX(w_orig - miny * ratio, 0);
471 } else {
472 // closer to the vertical, change only height, width is w_orig
473 rect->x.computed = MIN(origin[NR::X], opposite_x);
474 rect->width.computed = MAX(w_orig, 0);
475 }
477 }
479 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
481 } else {
482 // move freely
483 rect->x.computed = MIN(s[NR::X], opposite_x);
484 rect->width.computed = MAX(w_orig - minx, 0);
485 rect->y.computed = MIN(s[NR::Y], opposite_y);
486 rect->height.computed = MAX(h_orig - miny, 0);
487 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
488 }
490 sp_rect_clamp_radii(rect);
492 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
493 }
495 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
496 {
497 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
499 sp_knot_holder_add_full(
500 knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
501 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
502 _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
503 "radius the same"));
505 sp_knot_holder_add_full(
506 knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
507 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
508 _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
509 "radius the same")
510 );
512 sp_knot_holder_add_full(
513 knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
514 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
515 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
516 "or stretch in one dimension only")
517 );
519 sp_knot_holder_add_full(
520 knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
521 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
522 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
523 "or stretch in one dimension only")
524 );
526 sp_pat_knot_holder(item, knot_holder);
527 return knot_holder;
528 }
530 /* 3D Box */
532 static void sp_3dbox_knot_set(SPItem *item, guint knot_id, Box3D::Axis direction,
533 NR::Point const &new_pos, NR::Point const &origin, guint state)
534 {
535 g_assert(item != NULL);
536 SP3DBox *box = SP_3DBOX(item);
538 // FIXME: Why must the coordinates be flipped vertically???
539 SPDocument *doc = SP_OBJECT_DOCUMENT(box);
540 gdouble height = sp_document_height(doc);
542 NR::Matrix const i2d (sp_item_i2d_affine (item));
543 if (direction == Box3D::Z) {
544 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, !(state & GDK_SHIFT_MASK));
545 } else {
546 sp_3dbox_move_corner_in_XY_plane (box, knot_id, new_pos * i2d, (state & GDK_SHIFT_MASK) ? direction : Box3D::XY);
547 }
548 sp_3dbox_update_curves (box);
549 sp_3dbox_set_ratios (box);
550 sp_3dbox_update_perspective_lines ();
551 sp_3dbox_set_z_orders (box);
552 }
554 static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
555 {
556 g_assert(item != NULL);
557 SP3DBox *box = SP_3DBOX(item);
559 NR::Matrix const i2d (sp_item_i2d_affine (item));
560 return sp_3dbox_get_corner(box, knot_id) * i2d;
561 }
563 static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
564 {
565 sp_3dbox_knot_set (item, 1, Box3D::Y, new_pos, origin, state);
566 }
568 /*
569 static void sp_3dbox_knot1_set_constrained(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
570 {
571 sp_3dbox_knot_set (item, 1, Box3D::Y, new_pos, origin, state ^ GDK_SHIFT_MASK);
572 }
573 */
575 static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
576 {
577 sp_3dbox_knot_set (item, 2, Box3D::X, new_pos, origin, state);
578 }
580 /*
581 static void sp_3dbox_knot2_set_constrained(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
582 {
583 sp_3dbox_knot_set (item, 2, Box3D::X, new_pos, origin, state ^ GDK_SHIFT_MASK);
584 }
586 static void sp_3dbox_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
587 {
588 if (!(state & GDK_SHIFT_MASK)) {
589 sp_3dbox_knot_set (item, 3, Box3D::Y, new_pos, origin, state);
590 } else {
591 sp_3dbox_knot_set (item, 3, Box3D::Z, new_pos, origin, state ^ GDK_SHIFT_MASK);
592 }
593 }
594 */
596 static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
597 {
598 sp_3dbox_knot_set (item, 5, Box3D::Z, new_pos, origin, state);
599 }
601 /*
602 static void sp_3dbox_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
603 {
604 sp_3dbox_knot_set (item, 7, Box3D::Z, new_pos, origin, state);
605 }
606 */
608 // defined a uniform behaviour for all knots
609 static void sp_3dbox_knot_set_uniformly(SPItem *item, guint knot_id, Box3D::Axis direction, NR::Point const &new_pos, NR::Point const &origin, guint state)
610 {
611 g_assert(item != NULL);
612 SP3DBox *box = SP_3DBOX(item);
614 NR::Matrix const i2d (sp_item_i2d_affine (item));
615 if (direction == Box3D::Z) {
616 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, !(state & GDK_SHIFT_MASK));
617 } else {
618 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, (state & GDK_SHIFT_MASK));
619 }
620 sp_3dbox_update_curves (box);
621 sp_3dbox_set_ratios (box);
622 sp_3dbox_update_perspective_lines ();
623 sp_3dbox_set_z_orders (box);
624 }
626 static void sp_3dbox_knot0_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
627 {
628 sp_3dbox_knot_set_uniformly(item, 0, Box3D::XY, new_pos, origin, state);
629 }
631 static void sp_3dbox_knot1_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
632 {
633 sp_3dbox_knot_set_uniformly(item, 1, Box3D::XY, new_pos, origin, state);
634 }
636 static void sp_3dbox_knot2_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
637 {
638 sp_3dbox_knot_set_uniformly(item, 2, Box3D::XY, new_pos, origin, state);
639 }
641 static void sp_3dbox_knot3_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
642 {
643 sp_3dbox_knot_set_uniformly(item, 3, Box3D::XY, new_pos, origin, state);
644 }
646 static void sp_3dbox_knot4_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
647 {
648 sp_3dbox_knot_set_uniformly(item, 4, Box3D::Z, new_pos, origin, state);
649 }
651 static void sp_3dbox_knot5_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
652 {
653 sp_3dbox_knot_set_uniformly(item, 5, Box3D::Z, new_pos, origin, state);
654 }
656 static void sp_3dbox_knot6_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
657 {
658 sp_3dbox_knot_set_uniformly(item, 6, Box3D::Z, new_pos, origin, state);
659 }
661 static void sp_3dbox_knot7_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
662 {
663 sp_3dbox_knot_set_uniformly(item, 7, Box3D::Z, new_pos, origin, state);
664 }
666 static NR::Point sp_3dbox_knot0_get(SPItem *item)
667 {
668 return sp_3dbox_knot_get(item, 0);
669 }
671 static NR::Point sp_3dbox_knot1_get(SPItem *item)
672 {
673 return sp_3dbox_knot_get(item, 1);
674 }
676 static NR::Point sp_3dbox_knot2_get(SPItem *item)
677 {
678 return sp_3dbox_knot_get(item, 2);
679 }
681 static NR::Point sp_3dbox_knot3_get(SPItem *item)
682 {
683 return sp_3dbox_knot_get(item, 3);
684 }
686 static NR::Point sp_3dbox_knot4_get(SPItem *item)
687 {
688 return sp_3dbox_knot_get(item, 4);
689 }
691 static NR::Point sp_3dbox_knot5_get(SPItem *item)
692 {
693 return sp_3dbox_knot_get(item, 5);
694 }
696 static NR::Point sp_3dbox_knot6_get(SPItem *item)
697 {
698 return sp_3dbox_knot_get(item, 6);
699 }
701 static NR::Point sp_3dbox_knot7_get(SPItem *item)
702 {
703 return sp_3dbox_knot_get(item, 7);
704 }
707 //static
708 SPKnotHolder *
709 sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop, guint number_of_handles)
710 {
711 g_assert(item != NULL);
712 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
714 switch (number_of_handles) {
715 case 3:
716 sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,_("Resize box in X/Y direction"));
717 sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,_("Resize box in X/Y direction"));
718 sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,_("Resize box in Z direction"));
719 sp_pat_knot_holder(item, knot_holder);
720 break;
721 case 4:
722 /***
723 sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set_constrained, sp_3dbox_knot1_get, NULL,_("Resize box in X/Y direction"));
724 sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set_constrained, sp_3dbox_knot2_get, NULL,_("Resize box in X/Y direction"));
725 sp_knot_holder_add_full(knot_holder, sp_3dbox_knot3_set, sp_3dbox_knot3_get, NULL,
726 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR, _("Resize box in Y direction"));
727 sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set, sp_3dbox_knot7_get, NULL,_("Resize box in Z direction"));
728 ***/
729 sp_knot_holder_add(knot_holder, sp_3dbox_knot0_set_uniformly, sp_3dbox_knot0_get, NULL,
730 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
731 sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set_uniformly, sp_3dbox_knot1_get, NULL,
732 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
733 sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set_uniformly, sp_3dbox_knot2_get, NULL,
734 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
735 sp_knot_holder_add(knot_holder, sp_3dbox_knot3_set_uniformly, sp_3dbox_knot3_get, NULL,
736 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
737 sp_knot_holder_add(knot_holder, sp_3dbox_knot4_set_uniformly, sp_3dbox_knot4_get, NULL,
738 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
739 sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set_uniformly, sp_3dbox_knot5_get, NULL,
740 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
741 sp_knot_holder_add(knot_holder, sp_3dbox_knot6_set_uniformly, sp_3dbox_knot6_get, NULL,
742 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
743 sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set_uniformly, sp_3dbox_knot7_get, NULL,
744 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
745 sp_pat_knot_holder(item, knot_holder);
746 break;
747 default:
748 g_print ("Wrong number of handles (%d): Not implemented yet.\n", number_of_handles);
749 break;
750 }
752 return knot_holder;
753 }
755 /* SPArc */
757 /*
758 * return values:
759 * 1 : inside
760 * 0 : on the curves
761 * -1 : outside
762 */
763 static gint
764 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
765 {
766 gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
767 gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
769 gdouble s = dx * dx + dy * dy;
770 if (s < 1.0) return 1;
771 if (s > 1.0) return -1;
772 return 0;
773 }
775 static void
776 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
777 {
778 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
780 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
781 SPArc *arc = SP_ARC(item);
783 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
785 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
786 NR::scale sc(ge->rx.computed, ge->ry.computed);
787 ge->start = atan2(delta * sc.inverse());
788 if ( ( state & GDK_CONTROL_MASK )
789 && snaps )
790 {
791 ge->start = sp_round(ge->start, M_PI/snaps);
792 }
793 sp_genericellipse_normalize(ge);
794 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
795 }
797 static NR::Point sp_arc_start_get(SPItem *item)
798 {
799 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
800 SPArc *arc = SP_ARC(item);
802 return sp_arc_get_xy(arc, ge->start);
803 }
805 static void
806 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
807 {
808 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
810 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
811 SPArc *arc = SP_ARC(item);
813 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
815 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
816 NR::scale sc(ge->rx.computed, ge->ry.computed);
817 ge->end = atan2(delta * sc.inverse());
818 if ( ( state & GDK_CONTROL_MASK )
819 && snaps )
820 {
821 ge->end = sp_round(ge->end, M_PI/snaps);
822 }
823 sp_genericellipse_normalize(ge);
824 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
825 }
827 static NR::Point sp_arc_end_get(SPItem *item)
828 {
829 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
830 SPArc *arc = SP_ARC(item);
832 return sp_arc_get_xy(arc, ge->end);
833 }
835 static void
836 sp_arc_startend_click(SPItem *item, guint state)
837 {
838 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
840 if (state & GDK_SHIFT_MASK) {
841 ge->end = ge->start = 0;
842 ((SPObject *)ge)->updateRepr();
843 }
844 }
847 static void
848 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
849 {
850 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
851 SPArc *arc = SP_ARC(item);
853 NR::Point const s = snap_knot_position(arc, p);
855 ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
857 if ( state & GDK_CONTROL_MASK ) {
858 ge->ry.computed = ge->rx.computed;
859 }
861 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
862 }
864 static NR::Point sp_arc_rx_get(SPItem *item)
865 {
866 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
868 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(ge->rx.computed, 0));
869 }
871 static void
872 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
873 {
874 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
875 SPArc *arc = SP_ARC(item);
877 NR::Point const s = snap_knot_position(arc, p);
879 ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
881 if ( state & GDK_CONTROL_MASK ) {
882 ge->rx.computed = ge->ry.computed;
883 }
885 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
886 }
888 static NR::Point sp_arc_ry_get(SPItem *item)
889 {
890 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
892 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(0, ge->ry.computed));
893 }
895 static void
896 sp_arc_rx_click(SPItem *item, guint state)
897 {
898 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
900 if (state & GDK_CONTROL_MASK) {
901 ge->ry.computed = ge->rx.computed;
902 ((SPObject *)ge)->updateRepr();
903 }
904 }
906 static void
907 sp_arc_ry_click(SPItem *item, guint state)
908 {
909 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
911 if (state & GDK_CONTROL_MASK) {
912 ge->rx.computed = ge->ry.computed;
913 ((SPObject *)ge)->updateRepr();
914 }
915 }
917 static SPKnotHolder *
918 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
919 {
920 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
922 sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
923 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
924 _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
925 sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
926 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
927 _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
928 sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
929 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
930 _("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"));
931 sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
932 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
933 _("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"));
935 sp_pat_knot_holder(item, knot_holder);
937 return knot_holder;
938 }
940 /* SPStar */
942 static void
943 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
944 {
945 SPStar *star = SP_STAR(item);
947 NR::Point const s = snap_knot_position(star, p);
949 NR::Point d = s - star->center;
951 double arg1 = atan2(d);
952 double darg1 = arg1 - star->arg[0];
954 if (state & GDK_MOD1_MASK) {
955 star->randomized = darg1/(star->arg[0] - star->arg[1]);
956 } else if (state & GDK_SHIFT_MASK) {
957 star->rounded = darg1/(star->arg[0] - star->arg[1]);
958 } else if (state & GDK_CONTROL_MASK) {
959 star->r[0] = L2(d);
960 } else {
961 star->r[0] = L2(d);
962 star->arg[0] = arg1;
963 star->arg[1] += darg1;
964 }
965 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
966 }
968 static void
969 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
970 {
971 SPStar *star = SP_STAR(item);
973 NR::Point const s = snap_knot_position(star, p);
975 if (star->flatsided == false) {
976 NR::Point d = s - star->center;
978 double arg1 = atan2(d);
979 double darg1 = arg1 - star->arg[1];
981 if (state & GDK_MOD1_MASK) {
982 star->randomized = darg1/(star->arg[0] - star->arg[1]);
983 } else if (state & GDK_SHIFT_MASK) {
984 star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
985 } else if (state & GDK_CONTROL_MASK) {
986 star->r[1] = L2(d);
987 star->arg[1] = star->arg[0] + M_PI / star->sides;
988 }
989 else {
990 star->r[1] = L2(d);
991 star->arg[1] = atan2(d);
992 }
993 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
994 }
995 }
997 static NR::Point sp_star_knot1_get(SPItem *item)
998 {
999 g_assert(item != NULL);
1001 SPStar *star = SP_STAR(item);
1003 return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
1005 }
1007 static NR::Point sp_star_knot2_get(SPItem *item)
1008 {
1009 g_assert(item != NULL);
1011 SPStar *star = SP_STAR(item);
1013 return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
1014 }
1016 static void
1017 sp_star_knot_click(SPItem *item, guint state)
1018 {
1019 SPStar *star = SP_STAR(item);
1021 if (state & GDK_MOD1_MASK) {
1022 star->randomized = 0;
1023 ((SPObject *)star)->updateRepr();
1024 } else if (state & GDK_SHIFT_MASK) {
1025 star->rounded = 0;
1026 ((SPObject *)star)->updateRepr();
1027 } else if (state & GDK_CONTROL_MASK) {
1028 star->arg[1] = star->arg[0] + M_PI / star->sides;
1029 ((SPObject *)star)->updateRepr();
1030 }
1031 }
1033 static SPKnotHolder *
1034 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
1035 {
1036 /* we don't need to get parent knot_holder */
1037 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1038 g_assert(item != NULL);
1040 SPStar *star = SP_STAR(item);
1042 sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
1043 _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1044 if (star->flatsided == false)
1045 sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
1046 _("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"));
1048 sp_pat_knot_holder(item, knot_holder);
1050 return knot_holder;
1051 }
1053 /* SPSpiral */
1055 /*
1056 * set attributes via inner (t=t0) knot point:
1057 * [default] increase/decrease inner point
1058 * [shift] increase/decrease inner and outer arg synchronizely
1059 * [control] constrain inner arg to round per PI/4
1060 */
1061 static void
1062 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1063 {
1064 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1066 SPSpiral *spiral = SP_SPIRAL(item);
1068 gdouble dx = p[NR::X] - spiral->cx;
1069 gdouble dy = p[NR::Y] - spiral->cy;
1071 if (state & GDK_MOD1_MASK) {
1072 // adjust divergence by vertical drag, relative to rad
1073 double new_exp = (spiral->rad + dy)/(spiral->rad);
1074 spiral->exp = new_exp > 0? new_exp : 0;
1075 } else {
1076 // roll/unroll from inside
1077 gdouble arg_t0;
1078 sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
1080 gdouble arg_tmp = atan2(dy, dx) - arg_t0;
1081 gdouble arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
1082 spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
1084 /* round inner arg per PI/snaps, if CTRL is pressed */
1085 if ( ( state & GDK_CONTROL_MASK )
1086 && ( fabs(spiral->revo) > SP_EPSILON_2 )
1087 && ( snaps != 0 ) ) {
1088 gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
1089 spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
1090 }
1092 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1093 }
1095 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1096 }
1098 /*
1099 * set attributes via outer (t=1) knot point:
1100 * [default] increase/decrease revolution factor
1101 * [control] constrain inner arg to round per PI/4
1102 */
1103 static void
1104 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1105 {
1106 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1108 SPSpiral *spiral = SP_SPIRAL(item);
1110 gdouble dx = p[NR::X] - spiral->cx;
1111 gdouble dy = p[NR::Y] - spiral->cy;
1113 if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
1114 spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
1115 if (!(state & GDK_MOD1_MASK)) {
1116 // if alt not pressed, change also rad; otherwise it is locked
1117 spiral->rad = MAX(hypot(dx, dy), 0.001);
1118 }
1119 if ( ( state & GDK_CONTROL_MASK )
1120 && snaps ) {
1121 spiral->arg = sp_round(spiral->arg, M_PI/snaps);
1122 }
1123 } else { // roll/unroll
1124 // arg of the spiral outer end
1125 double arg_1;
1126 sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
1128 // its fractional part after the whole turns are subtracted
1129 double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
1131 // arg of the mouse point relative to spiral center
1132 double mouse_angle = atan2(dy, dx);
1133 if (mouse_angle < 0)
1134 mouse_angle += 2*M_PI;
1136 // snap if ctrl
1137 if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
1138 mouse_angle = sp_round(mouse_angle, M_PI/snaps);
1139 }
1141 // by how much we want to rotate the outer point
1142 double diff = mouse_angle - arg_r;
1143 if (diff > M_PI)
1144 diff -= 2*M_PI;
1145 else if (diff < -M_PI)
1146 diff += 2*M_PI;
1148 // calculate the new rad;
1149 // the value of t corresponding to the angle arg_1 + diff:
1150 double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
1151 // the rad at that t:
1152 double rad_new = 0;
1153 if (t_temp > spiral->t0)
1154 sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
1156 // change the revo (converting diff from radians to the number of turns)
1157 spiral->revo += diff/(2*M_PI);
1158 if (spiral->revo < 1e-3)
1159 spiral->revo = 1e-3;
1161 // if alt not pressed and the values are sane, change the rad
1162 if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
1163 // adjust t0 too so that the inner point stays unmoved
1164 double r0;
1165 sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
1166 spiral->rad = rad_new;
1167 spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
1168 }
1169 if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
1170 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1171 }
1173 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1174 }
1176 static NR::Point sp_spiral_inner_get(SPItem *item)
1177 {
1178 SPSpiral *spiral = SP_SPIRAL(item);
1180 return sp_spiral_get_xy(spiral, spiral->t0);
1181 }
1183 static NR::Point sp_spiral_outer_get(SPItem *item)
1184 {
1185 SPSpiral *spiral = SP_SPIRAL(item);
1187 return sp_spiral_get_xy(spiral, 1.0);
1188 }
1190 static void
1191 sp_spiral_inner_click(SPItem *item, guint state)
1192 {
1193 SPSpiral *spiral = SP_SPIRAL(item);
1195 if (state & GDK_MOD1_MASK) {
1196 spiral->exp = 1;
1197 ((SPObject *)spiral)->updateRepr();
1198 } else if (state & GDK_SHIFT_MASK) {
1199 spiral->t0 = 0;
1200 ((SPObject *)spiral)->updateRepr();
1201 }
1202 }
1204 static SPKnotHolder *
1205 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
1206 {
1207 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1209 sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
1210 _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
1211 sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
1212 _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
1214 sp_pat_knot_holder(item, knot_holder);
1216 return knot_holder;
1217 }
1219 /* SPOffset */
1221 static void
1222 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1223 {
1224 SPOffset *offset = SP_OFFSET(item);
1226 offset->rad = sp_offset_distance_to_original(offset, p);
1227 offset->knot = p;
1228 offset->knotSet = true;
1230 ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1231 }
1234 static NR::Point sp_offset_offset_get(SPItem *item)
1235 {
1236 SPOffset *offset = SP_OFFSET(item);
1238 NR::Point np;
1239 sp_offset_top_point(offset,&np);
1240 return np;
1241 }
1243 static SPKnotHolder *
1244 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1245 {
1246 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1248 sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1249 _("Adjust the <b>offset distance</b>"));
1251 sp_pat_knot_holder(item, knot_holder);
1253 return knot_holder;
1254 }
1256 static SPKnotHolder *
1257 sp_misc_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1258 {
1259 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1260 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1261 {
1262 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1264 sp_pat_knot_holder(item, knot_holder);
1266 return knot_holder;
1267 }
1268 return NULL;
1269 }
1271 static void
1272 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1273 {
1274 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1275 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1276 {
1277 sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1278 // TRANSLATORS: This refers to the pattern that's inside the object
1279 _("<b>Move</b> the pattern fill inside the object"));
1280 sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1281 _("<b>Scale</b> the pattern fill uniformly"));
1282 sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1283 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1284 }
1285 }
1287 static NR::Point sp_flowtext_corner_get(SPItem *item)
1288 {
1289 SPRect *rect = SP_RECT(item);
1291 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1292 }
1294 static void
1295 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1296 {
1297 SPRect *rect = SP_RECT(item);
1299 sp_rect_wh_set_internal(rect, p, origin, state);
1300 }
1302 static SPKnotHolder *
1303 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1304 {
1305 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1307 sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1308 _("Drag to resize the <b>flowed text frame</b>"));
1310 return knot_holder;
1311 }
1314 /*
1315 Local Variables:
1316 mode:c++
1317 c-file-style:"stroustrup"
1318 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1319 indent-tabs-mode:nil
1320 fill-column:99
1321 End:
1322 */
1323 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :