1 #define __GRADIENT_DRAG_C__
3 /*
4 * On-canvas gradient dragging
5 *
6 * Authors:
7 * bulia byak <buliabyak@users.sf.net>
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 *
10 * Copyright (C) 2007 Johan Engelen
11 * Copyright (C) 2005 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include <glibmm/i18n.h>
21 #include <cstring>
22 #include <string>
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-style.h"
28 #include "document.h"
29 #include "display/sp-ctrlline.h"
30 #include "display/sp-canvas-util.h"
31 #include "xml/repr.h"
32 #include "svg/css-ostringstream.h"
33 #include "svg/svg.h"
34 #include "libnr/nr-point-fns.h"
35 #include "preferences.h"
36 #include "sp-item.h"
37 #include "style.h"
38 #include "knot.h"
39 #include "sp-linear-gradient.h"
40 #include "sp-radial-gradient.h"
41 #include "gradient-chemistry.h"
42 #include "gradient-drag.h"
43 #include "sp-stop.h"
44 #include "snap.h"
45 #include "sp-namedview.h"
46 #include "selection-chemistry.h"
48 #define GR_KNOT_COLOR_NORMAL 0xffffff00
49 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
50 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
52 #define GR_LINE_COLOR_FILL 0x0000ff7f
53 #define GR_LINE_COLOR_STROKE 0x9999007f
55 // screen pixels between knots when they snap:
56 #define SNAP_DIST 5
58 // absolute distance between gradient points for them to become a single dragger when the drag is created:
59 #define MERGE_DIST 0.1
61 // knot shapes corresponding to GrPointType enum
62 SPKnotShapeType gr_knot_shapes [] = {
63 SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
64 SP_KNOT_SHAPE_CIRCLE, //POINT_LG_END
65 SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
66 SP_KNOT_SHAPE_SQUARE, // POINT_RG_CENTER
67 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R1
68 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R2
69 SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
70 SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
71 SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
72 };
74 const gchar *gr_knot_descr [] = {
75 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
76 N_("Linear gradient <b>end</b>"),
77 N_("Linear gradient <b>mid stop</b>"),
78 N_("Radial gradient <b>center</b>"),
79 N_("Radial gradient <b>radius</b>"),
80 N_("Radial gradient <b>radius</b>"),
81 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
82 N_("Radial gradient <b>mid stop</b>"),
83 N_("Radial gradient <b>mid stop</b>")
84 };
86 static void
87 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
88 {
89 GrDrag *drag = (GrDrag *) data;
90 drag->updateDraggers ();
91 drag->updateLines ();
92 drag->updateLevels ();
93 }
95 static void
96 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
97 {
98 GrDrag *drag = (GrDrag *) data;
99 if (drag->local_change) {
100 drag->local_change = false;
101 } else {
102 drag->updateDraggers ();
103 }
104 drag->updateLines ();
105 drag->updateLevels ();
106 }
108 /**
109 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
110 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
111 any.
112 */
113 int
114 gr_drag_style_query (SPStyle *style, int property, gpointer data)
115 {
116 GrDrag *drag = (GrDrag *) data;
118 if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
119 return QUERY_STYLE_NOTHING;
120 }
122 if (!drag->selected) {
123 return QUERY_STYLE_NOTHING;
124 } else {
125 int ret = QUERY_STYLE_NOTHING;
127 float cf[4];
128 cf[0] = cf[1] = cf[2] = cf[3] = 0;
130 int count = 0;
132 for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
133 GrDragger *d = (GrDragger *) i->data;
134 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
135 GrDraggable *draggable = (GrDraggable *) j->data;
137 if (ret == QUERY_STYLE_NOTHING) {
138 ret = QUERY_STYLE_SINGLE;
139 } else if (ret == QUERY_STYLE_SINGLE) {
140 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
141 }
143 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
144 cf[0] += SP_RGBA32_R_F (c);
145 cf[1] += SP_RGBA32_G_F (c);
146 cf[2] += SP_RGBA32_B_F (c);
147 cf[3] += SP_RGBA32_A_F (c);
149 count ++;
150 }
151 }
153 if (count) {
154 cf[0] /= count;
155 cf[1] /= count;
156 cf[2] /= count;
157 cf[3] /= count;
159 // set both fill and stroke with our stop-color and stop-opacity
160 style->fill.clear();
161 style->fill.setColor( cf[0], cf[1], cf[2] );
162 style->fill.set = TRUE;
163 style->stroke.clear();
164 style->stroke.setColor( cf[0], cf[1], cf[2] );
165 style->stroke.set = TRUE;
167 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
168 style->fill_opacity.set = TRUE;
169 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
170 style->stroke_opacity.set = TRUE;
172 style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
173 style->opacity.set = TRUE;
174 }
176 return ret;
177 }
178 }
180 bool
181 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
182 {
183 GrDrag *drag = (GrDrag *) data;
185 if (!drag->selected)
186 return false;
188 SPCSSAttr *stop = sp_repr_css_attr_new ();
190 // See if the css contains interesting properties, and if so, translate them into the format
191 // acceptable for gradient stops
193 // any of color properties, in order of increasing priority:
194 if (css->attribute("flood-color"))
195 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
197 if (css->attribute("lighting-color"))
198 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
200 if (css->attribute("color"))
201 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
203 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
204 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
206 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
207 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
209 if (css->attribute("stop-color"))
210 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
213 if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
214 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
215 } else { // multiply all opacity properties:
216 gdouble accumulated = 1.0;
217 accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
218 accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
219 accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
220 accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
222 Inkscape::CSSOStringStream os;
223 os << accumulated;
224 sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
226 if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
227 (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
228 sp_repr_css_set_property (stop, "stop-opacity", "0"); // if a single fill/stroke property is set to none, don't change color, set opacity to 0
229 }
231 if (!stop->attributeList()) { // nothing for us here, pass it on
232 sp_repr_css_attr_unref(stop);
233 return false;
234 }
236 for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
237 GrDragger* dragger = (GrDragger*) sel->data;
238 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
239 GrDraggable *draggable = (GrDraggable *) i->data;
241 drag->local_change = true;
242 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
243 }
244 }
246 //sp_repr_css_print(stop);
247 sp_repr_css_attr_unref(stop);
248 return true;
249 }
251 guint32 GrDrag::getColor()
252 {
253 if (!selected) return 0;
255 float cf[4];
256 cf[0] = cf[1] = cf[2] = cf[3] = 0;
258 int count = 0;
260 for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
261 GrDragger *d = (GrDragger *) i->data;
262 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
263 GrDraggable *draggable = (GrDraggable *) j->data;
265 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
266 cf[0] += SP_RGBA32_R_F (c);
267 cf[1] += SP_RGBA32_G_F (c);
268 cf[2] += SP_RGBA32_B_F (c);
269 cf[3] += SP_RGBA32_A_F (c);
271 count ++;
272 }
273 }
275 if (count) {
276 cf[0] /= count;
277 cf[1] /= count;
278 cf[2] /= count;
279 cf[3] /= count;
280 }
282 return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
283 }
285 SPStop *
286 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
287 {
288 gfloat offset; // type of SPStop.offset = gfloat
289 SPGradient *gradient;
290 bool fill_or_stroke = true;
291 bool r1_knot = false;
293 bool addknot = false;
294 do {
295 gradient = sp_item_gradient (item, fill_or_stroke);
296 if (SP_IS_LINEARGRADIENT(gradient)) {
297 Geom::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
298 Geom::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
300 Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
301 double dist_screen = Geom::L2 (mouse_p - nearest);
302 if ( dist_screen < tolerance ) {
303 // add the knot
304 offset = get_offset_between_points(nearest, begin, end);
305 addknot = true;
306 break; // break out of the while loop: add only one knot
307 }
308 } else if (SP_IS_RADIALGRADIENT(gradient)) {
309 Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
310 Geom::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
311 Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
312 double dist_screen = Geom::L2 (mouse_p - nearest);
313 if ( dist_screen < tolerance ) {
314 offset = get_offset_between_points(nearest, begin, end);
315 addknot = true;
316 r1_knot = true;
317 break; // break out of the while loop: add only one knot
318 }
320 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
321 nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
322 dist_screen = Geom::L2 (mouse_p - nearest);
323 if ( dist_screen < tolerance ) {
324 offset = get_offset_between_points(nearest, begin, end);
325 addknot = true;
326 r1_knot = false;
327 break; // break out of the while loop: add only one knot
328 }
329 }
330 fill_or_stroke = !fill_or_stroke;
331 } while (!fill_or_stroke && !addknot) ;
333 if (addknot) {
334 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
335 SPStop* prev_stop = sp_first_stop(vector);
336 SPStop* next_stop = sp_next_stop(prev_stop);
337 guint i = 1;
338 while ( (next_stop) && (next_stop->offset < offset) ) {
339 prev_stop = next_stop;
340 next_stop = sp_next_stop(next_stop);
341 i++;
342 }
343 if (!next_stop) {
344 // logical error: the endstop should have offset 1 and should always be more than this offset here
345 return NULL;
346 }
349 SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
350 sp_gradient_ensure_vector (gradient);
351 updateDraggers();
353 return newstop;
354 }
356 return NULL;
357 }
360 bool
361 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
362 {
363 // first, see if we can drop onto one of the existing draggers
364 for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
365 GrDragger *d = (GrDragger *) i->data;
367 if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
368 SPCSSAttr *stop = sp_repr_css_attr_new ();
369 sp_repr_css_set_property (stop, "stop-color", c);
370 sp_repr_css_set_property (stop, "stop-opacity", "1");
371 for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
372 GrDraggable *draggable = (GrDraggable *) j->data;
373 local_change = true;
374 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
375 }
376 sp_repr_css_attr_unref(stop);
377 return true;
378 }
379 }
381 // now see if we're over line and create a new stop
382 bool over_line = false;
383 SPCtrlLine *line = NULL;
384 if (lines) {
385 for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
386 line = (SPCtrlLine*) l->data;
387 Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
388 double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
389 if (line->item && dist_screen < 5) {
390 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
391 if (stop) {
392 SPCSSAttr *css = sp_repr_css_attr_new ();
393 sp_repr_css_set_property (css, "stop-color", c);
394 sp_repr_css_set_property (css, "stop-opacity", "1");
395 sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
396 return true;
397 }
398 }
399 }
400 }
402 return false;
403 }
406 GrDrag::GrDrag(SPDesktop *desktop) {
408 this->desktop = desktop;
410 this->selection = sp_desktop_selection(desktop);
412 this->draggers = NULL;
413 this->lines = NULL;
414 this->selected = NULL;
416 this->hor_levels.clear();
417 this->vert_levels.clear();
419 this->local_change = false;
421 this->sel_changed_connection = this->selection->connectChanged(
422 sigc::bind (
423 sigc::ptr_fun(&gr_drag_sel_changed),
424 (gpointer)this )
426 );
427 this->sel_modified_connection = this->selection->connectModified(
428 sigc::bind(
429 sigc::ptr_fun(&gr_drag_sel_modified),
430 (gpointer)this )
431 );
433 this->style_set_connection = this->desktop->connectSetStyle(
434 sigc::bind(
435 sigc::ptr_fun(&gr_drag_style_set),
436 (gpointer)this )
437 );
439 this->style_query_connection = this->desktop->connectQueryStyle(
440 sigc::bind(
441 sigc::ptr_fun(&gr_drag_style_query),
442 (gpointer)this )
443 );
445 this->updateDraggers ();
446 this->updateLines ();
447 this->updateLevels ();
449 if (desktop->gr_item) {
450 this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
451 }
452 }
454 GrDrag::~GrDrag()
455 {
456 this->sel_changed_connection.disconnect();
457 this->sel_modified_connection.disconnect();
458 this->style_set_connection.disconnect();
459 this->style_query_connection.disconnect();
461 if (this->selected) {
462 GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data;
463 desktop->gr_item = draggable->item;
464 desktop->gr_point_type = draggable->point_type;
465 desktop->gr_point_i = draggable->point_i;
466 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
467 } else {
468 desktop->gr_item = NULL;
469 desktop->gr_point_type = 0;
470 desktop->gr_point_i = 0;
471 desktop->gr_fill_or_stroke = true;
472 }
474 deselect_all();
475 for (GList *l = this->draggers; l != NULL; l = l->next) {
476 delete ((GrDragger *) l->data);
477 }
478 g_list_free (this->draggers);
479 this->draggers = NULL;
480 this->selected = NULL;
482 for (GSList *l = this->lines; l != NULL; l = l->next) {
483 gtk_object_destroy( GTK_OBJECT (l->data));
484 }
485 g_slist_free (this->lines);
486 this->lines = NULL;
487 }
489 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
490 {
491 this->item = item;
492 this->point_type = point_type;
493 this->point_i = point_i;
494 this->fill_or_stroke = fill_or_stroke;
496 g_object_ref (G_OBJECT (this->item));
497 }
499 GrDraggable::~GrDraggable ()
500 {
501 g_object_unref (G_OBJECT (this->item));
502 }
505 SPObject *
506 GrDraggable::getServer ()
507 {
508 if (!item)
509 return NULL;
511 SPObject *server = NULL;
512 if (fill_or_stroke)
513 server = SP_OBJECT_STYLE_FILL_SERVER (item);
514 else
515 server = SP_OBJECT_STYLE_STROKE_SERVER (item);
517 return server;
518 }
520 static
521 boost::optional<Geom::Point>
522 get_snap_vector (Geom::Point p, Geom::Point o, double snap, double initial)
523 {
524 double r = L2 (p - o);
525 if (r < 1e-3) {
526 return boost::optional<Geom::Point>();
527 }
529 double angle = atan2 (p - o);
530 // snap angle to snaps increments, starting from initial:
531 double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
532 // calculate the new position and subtract p to get the vector:
533 return (o + r * Geom::Point(cos(a_snapped), sin(a_snapped)) - p);
534 }
536 static void
537 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
538 {
539 GrDragger *dragger = (GrDragger *) data;
540 GrDrag *drag = dragger->parent;
542 Geom::Point p = ppointer;
544 SPDesktop *desktop = dragger->parent->desktop;
545 SnapManager &m = desktop->namedview->snap_manager;
546 double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom();
548 if (state & GDK_SHIFT_MASK) {
549 // with Shift; unsnap if we carry more than one draggable
550 if (dragger->draggables && dragger->draggables->next) {
551 // create a new dragger
552 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
553 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
554 // relink to it all but the first draggable in the list
555 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
556 GrDraggable *draggable = (GrDraggable *) i->data;
557 dr_new->addDraggable (draggable);
558 }
559 dr_new->updateKnotShape();
560 g_slist_free (dragger->draggables->next);
561 dragger->draggables->next = NULL;
562 dragger->updateKnotShape();
563 dragger->updateTip();
564 }
565 } else if (!(state & GDK_CONTROL_MASK)) {
566 // without Shift or Ctrl; see if we need to snap to another dragger
567 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
568 GrDragger *d_new = (GrDragger *) di->data;
569 if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
571 // Merge draggers:
572 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
573 GrDraggable *draggable = (GrDraggable *) i->data;
574 // copy draggable to d_new:
575 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
576 d_new->addDraggable (da_new);
577 }
579 // unlink and delete this dragger
580 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
581 delete dragger;
583 // update the new merged dragger
584 d_new->fireDraggables(true, false, true);
585 d_new->parent->updateLines();
586 d_new->parent->setSelected (d_new);
587 d_new->updateKnotShape ();
588 d_new->updateTip ();
589 d_new->updateDependencies(true);
590 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
591 _("Merge gradient handles"));
592 return;
593 }
594 }
595 }
597 m.setup(desktop);
598 if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
599 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
600 if (s.getSnapped()) {
601 p = s.getPoint();
602 sp_knot_moveto (knot, p);
603 }
604 } else if (state & GDK_CONTROL_MASK) {
605 SnappedConstraints sc;
606 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
607 unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
608 /* 0 means no snapping. */
610 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
611 GrDraggable *draggable = (GrDraggable *) i->data;
613 Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
615 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
616 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
617 GrDragger *d_new = (GrDragger *) di->data;
618 if (d_new == dragger)
619 continue;
620 if (d_new->isA (draggable->item,
621 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
622 draggable->fill_or_stroke)) {
623 // found the other end of the linear gradient;
624 if (state & GDK_SHIFT_MASK) {
625 // moving linear around center
626 Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
627 dr_snap = center;
628 } else {
629 // moving linear around the other end
630 dr_snap = d_new->point;
631 }
632 }
633 }
634 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
635 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
636 GrDragger *d_new = (GrDragger *) di->data;
637 if (d_new == dragger)
638 continue;
639 if (d_new->isA (draggable->item,
640 POINT_RG_CENTER,
641 draggable->fill_or_stroke)) {
642 // found the center of the radial gradient;
643 dr_snap = d_new->point;
644 }
645 }
646 } else if (draggable->point_type == POINT_RG_CENTER) {
647 // radial center snaps to hor/vert relative to its original position
648 dr_snap = dragger->point_original;
649 }
651 boost::optional<Geom::Point> snap_vector;
652 if (dr_snap.isFinite()) {
653 if (state & GDK_MOD1_MASK) {
654 // with Alt, snap to the original angle and its perpendiculars
655 snap_vector = get_snap_vector (p, dr_snap, M_PI/2, Geom::atan2 (dragger->point_original - dr_snap));
656 } else {
657 // with Ctrl, snap to M_PI/snaps
658 snap_vector = get_snap_vector (p, dr_snap, M_PI/snaps, 0);
659 }
660 if (snap_vector) {
661 Inkscape::Snapper::ConstraintLine cl(dr_snap, p + *snap_vector - dr_snap);
662 Inkscape::SnappedPoint s = m.constrainedSnap(Inkscape::SnapCandidatePoint(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE), cl);
663 if (s.getSnapped()) {
664 s.setTransformation(s.getPoint() - p);
665 sc.points.push_back(s);
666 } else {
667 Inkscape::SnappedPoint dummy(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE, 0, Inkscape::SNAPTARGET_CONSTRAINED_ANGLE, Geom::L2(*snap_vector), 10000, true, false);
668 dummy.setTransformation(*snap_vector);
669 sc.points.push_back(dummy);
670 }
671 }
672 }
673 }
675 Inkscape::SnappedPoint bsp = m.findBestSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE), sc, true); // snap indicator will be displayed if needed
677 if (bsp.getSnapped()) {
678 p += bsp.getTransformation();
679 sp_knot_moveto (knot, p);
680 }
681 }
683 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
684 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
686 if (drag->keep_selection) {
687 Geom::Point diff = p - dragger->point;
688 drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
689 } else {
690 dragger->point = p;
691 dragger->fireDraggables (false, scale_radial);
692 dragger->updateDependencies(false);
693 }
695 }
698 static void
699 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
700 {
702 GrDrag *drag = dragger->parent;
703 // a midpoint dragger can (logically) only contain one GrDraggable
704 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
706 // get begin and end points between which dragging is allowed:
707 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
708 *moving = g_slist_append(*moving, dragger);
710 guint lowest_i = draggable->point_i;
711 guint highest_i = draggable->point_i;
712 GrDragger *lowest_dragger = dragger;
713 GrDragger *highest_dragger = dragger;
714 if (dragger->isSelected()) {
715 GrDragger* d_add;
716 while ( true )
717 {
718 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
719 if ( d_add && g_list_find(drag->selected, d_add) ) {
720 lowest_i = lowest_i - 1;
721 *moving = g_slist_prepend(*moving, d_add);
722 lowest_dragger = d_add;
723 } else {
724 break;
725 }
726 }
728 while ( true )
729 {
730 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
731 if ( d_add && g_list_find(drag->selected, d_add) ) {
732 highest_i = highest_i + 1;
733 *moving = g_slist_append(*moving, d_add);
734 highest_dragger = d_add;
735 } else {
736 break;
737 }
738 }
739 }
741 if ( SP_IS_LINEARGRADIENT(server) ) {
742 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
743 GrDragger *d_temp;
744 if (lowest_i == 1) {
745 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
746 } else {
747 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
748 }
749 if (d_temp)
750 *begin = d_temp->point;
752 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
753 if (d_temp == NULL) {
754 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
755 }
756 if (d_temp)
757 *end = d_temp->point;
758 } else if ( SP_IS_RADIALGRADIENT(server) ) {
759 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
760 GrDragger *d_temp;
761 if (lowest_i == 1) {
762 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
763 } else {
764 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
765 }
766 if (d_temp)
767 *begin = d_temp->point;
769 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
770 if (d_temp == NULL) {
771 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
772 }
773 if (d_temp)
774 *end = d_temp->point;
775 }
777 *low_lim = dragger->point - (lowest_dragger->point - *begin);
778 *high_lim = dragger->point - (highest_dragger->point - *end);
779 }
783 /**
784 Called when a midpoint knot is dragged.
785 */
786 static void
787 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
788 {
789 GrDragger *dragger = (GrDragger *) data;
790 GrDrag *drag = dragger->parent;
791 // a midpoint dragger can (logically) only contain one GrDraggable
792 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
794 // FIXME: take from prefs
795 double snap_fraction = 0.1;
797 Geom::Point p = ppointer;
798 Geom::Point begin(0,0), end(0,0);
799 Geom::Point low_lim(0,0), high_lim(0,0);
801 SPObject *server = draggable->getServer();
803 GSList *moving = NULL;
804 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
806 if (state & GDK_CONTROL_MASK) {
807 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
808 } else {
809 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
810 if (!(state & GDK_SHIFT_MASK)) {
811 SPDesktop *desktop = dragger->parent->desktop;
812 SnapManager &m = desktop->namedview->snap_manager;
813 Inkscape::Snapper::ConstraintLine cl(low_lim, high_lim - low_lim);
814 m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
815 }
816 }
817 Geom::Point displacement = p - dragger->point;
819 for (GSList const* i = moving; i != NULL; i = i->next) {
820 GrDragger *drg = (GrDragger*) i->data;
821 SPKnot *drgknot = drg->knot;
822 Geom::Point this_move = displacement;
823 if (state & GDK_MOD1_MASK) {
824 // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
825 double alpha = 1.0;
826 if (Geom::L2(drg->point - dragger->point) + Geom::L2(drg->point - begin) - 1e-3 > Geom::L2(dragger->point - begin)) { // drg is on the end side from dragger
827 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
828 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
829 } else { // drg is on the begin side from dragger
830 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
831 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
832 }
833 }
834 drg->point += this_move;
835 sp_knot_moveto (drgknot, drg->point);
836 drg->fireDraggables (false);
837 drg->updateDependencies(false);
838 }
840 g_slist_free(moving);
842 drag->keep_selection = dragger->isSelected();
843 }
847 static void
848 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
849 {
850 GrDragger *dragger = (GrDragger *) data;
852 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
853 }
855 /**
856 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
857 */
858 static void
859 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
860 {
861 GrDragger *dragger = (GrDragger *) data;
863 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
865 dragger->point_original = dragger->point = knot->pos;
867 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
868 dragger->fireDraggables (true, true);
869 } else {
870 dragger->fireDraggables (true);
871 }
873 for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
874 GrDragger *d = (GrDragger *) i->data;
875 if (d == dragger)
876 continue;
877 d->fireDraggables (true);
878 }
880 // make this dragger selected
881 if (!dragger->parent->keep_selection) {
882 dragger->parent->setSelected (dragger);
883 }
884 dragger->parent->keep_selection = false;
886 dragger->updateDependencies(true);
888 // we did an undoable action
889 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
890 _("Move gradient handle"));
891 }
893 /**
894 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
895 state of the keyboard keys
896 */
897 static void
898 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
899 {
900 GrDragger *dragger = (GrDragger *) data;
901 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
902 if (!draggable) return;
904 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
905 // delete this knot from vector
906 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
907 gradient = sp_gradient_get_vector (gradient, false);
908 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
909 SPStop *stop = NULL;
910 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
911 case POINT_LG_BEGIN:
912 case POINT_RG_CENTER:
913 stop = sp_first_stop(gradient);
914 {
915 SPStop *next = sp_next_stop (stop);
916 if (next) {
917 next->offset = 0;
918 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
919 }
920 }
921 break;
922 case POINT_LG_END:
923 case POINT_RG_R1:
924 case POINT_RG_R2:
925 stop = sp_last_stop(gradient);
926 {
927 SPStop *prev = sp_prev_stop (stop, gradient);
928 if (prev) {
929 prev->offset = 1;
930 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
931 }
932 }
933 break;
934 case POINT_LG_MID:
935 case POINT_RG_MID1:
936 case POINT_RG_MID2:
937 stop = sp_get_stop_i(gradient, draggable->point_i);
938 break;
939 }
941 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
942 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
943 _("Delete gradient stop"));
944 }
945 } else {
946 // select the dragger
947 dragger->point_original = dragger->point;
949 if ( state & GDK_SHIFT_MASK ) {
950 dragger->parent->setSelected (dragger, true, false);
951 } else {
952 dragger->parent->setSelected (dragger);
953 }
954 }
955 }
957 /**
958 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
959 */
960 static void
961 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
962 {
963 GrDragger *dragger = (GrDragger *) data;
965 dragger->point_original = dragger->point;
967 if (dragger->draggables == NULL)
968 return;
970 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
971 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
972 }
974 /**
975 Act upon all draggables of the dragger, setting them to the dragger's point
976 */
977 void
978 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
979 {
980 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
981 GrDraggable *draggable = (GrDraggable *) i->data;
983 // set local_change flag so that selection_changed callback does not regenerate draggers
984 this->parent->local_change = true;
986 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
987 // to the center, unless it's the first update upon merge when we must snap it to the point
988 if (merging_focus ||
989 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
990 {
991 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
992 }
993 }
994 }
996 /**
997 Checks if the dragger has a draggable with this point_type
998 */
999 bool
1000 GrDragger::isA (gint point_type)
1001 {
1002 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1003 GrDraggable *draggable = (GrDraggable *) i->data;
1004 if (draggable->point_type == point_type) {
1005 return true;
1006 }
1007 }
1008 return false;
1009 }
1011 /**
1012 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1013 */
1014 bool
1015 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1016 {
1017 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1018 GrDraggable *draggable = (GrDraggable *) i->data;
1019 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1020 return true;
1021 }
1022 }
1023 return false;
1024 }
1026 /**
1027 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1028 */
1029 bool
1030 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1031 {
1032 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1033 GrDraggable *draggable = (GrDraggable *) i->data;
1034 if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1035 return true;
1036 }
1037 }
1038 return false;
1039 }
1041 bool
1042 GrDraggable::mayMerge (GrDraggable *da2)
1043 {
1044 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1045 // we must not merge the points of the same gradient!
1046 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1047 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1048 // except that we can snap center and focus together
1049 return false;
1050 }
1051 }
1052 // disable merging of midpoints.
1053 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1054 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1055 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1056 return false;
1058 return true;
1059 }
1061 bool
1062 GrDragger::mayMerge (GrDragger *other)
1063 {
1064 if (this == other)
1065 return false;
1067 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1068 GrDraggable *da1 = (GrDraggable *) i->data;
1069 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1070 GrDraggable *da2 = (GrDraggable *) j->data;
1071 if (!da1->mayMerge(da2))
1072 return false;
1073 }
1074 }
1075 return true;
1076 }
1078 bool
1079 GrDragger::mayMerge (GrDraggable *da2)
1080 {
1081 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1082 GrDraggable *da1 = (GrDraggable *) i->data;
1083 if (!da1->mayMerge(da2))
1084 return false;
1085 }
1086 return true;
1087 }
1089 /**
1090 Updates the statusbar tip of the dragger knot, based on its draggables
1091 */
1092 void
1093 GrDragger::updateTip ()
1094 {
1095 if (this->knot && this->knot->tip) {
1096 g_free (this->knot->tip);
1097 this->knot->tip = NULL;
1098 }
1100 if (g_slist_length (this->draggables) == 1) {
1101 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1102 char *item_desc = sp_item_description(draggable->item);
1103 switch (draggable->point_type) {
1104 case POINT_LG_MID:
1105 case POINT_RG_MID1:
1106 case POINT_RG_MID2:
1107 this->knot->tip = g_strdup_printf (_("%s %d for: %s%s; drag with <b>Ctrl</b> to snap offset; click with <b>Ctrl+Alt</b> to delete stop"),
1108 _(gr_knot_descr[draggable->point_type]),
1109 draggable->point_i,
1110 item_desc,
1111 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1112 break;
1114 default:
1115 this->knot->tip = g_strdup_printf (_("%s for: %s%s; drag with <b>Ctrl</b> to snap angle, with <b>Ctrl+Alt</b> to preserve angle, with <b>Ctrl+Shift</b> to scale around center"),
1116 _(gr_knot_descr[draggable->point_type]),
1117 item_desc,
1118 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1119 break;
1120 }
1121 g_free(item_desc);
1122 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1123 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1124 } else {
1125 int length = g_slist_length (this->draggables);
1126 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1127 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1128 length),
1129 length);
1130 }
1131 }
1133 /**
1134 Adds a draggable to the dragger
1135 */
1136 void
1137 GrDragger::updateKnotShape ()
1138 {
1139 if (!draggables)
1140 return;
1141 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1142 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1143 }
1145 /**
1146 Adds a draggable to the dragger
1147 */
1148 void
1149 GrDragger::addDraggable (GrDraggable *draggable)
1150 {
1151 this->draggables = g_slist_prepend (this->draggables, draggable);
1153 this->updateTip();
1154 }
1157 /**
1158 Moves this dragger to the point of the given draggable, acting upon all other draggables
1159 */
1160 void
1161 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1162 {
1163 GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1164 if (!dr_first) return;
1166 this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1167 this->point_original = this->point;
1169 sp_knot_moveto (this->knot, this->point);
1171 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1172 GrDraggable *da = (GrDraggable *) i->data;
1173 if ( (da->item == item) &&
1174 (point_type == -1 || da->point_type == point_type) &&
1175 (point_i == -1 || da->point_i == point_i) &&
1176 (da->fill_or_stroke == fill_or_stroke) ) {
1177 continue;
1178 }
1179 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1180 }
1181 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1182 }
1185 /**
1186 Moves all midstop draggables that depend on this one
1187 */
1188 void
1189 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1190 SPObject *server = draggable->getServer();
1191 if (!server)
1192 return;
1193 guint num = SP_GRADIENT(server)->vector.stops.size();
1194 if (num <= 2) return;
1196 if ( SP_IS_LINEARGRADIENT(server) ) {
1197 for ( guint i = 1; i < num - 1; i++ ) {
1198 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1199 }
1200 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1201 for ( guint i = 1; i < num - 1; i++ ) {
1202 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1203 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1204 }
1205 }
1206 }
1209 /**
1210 Moves all draggables that depend on this one
1211 */
1212 void
1213 GrDragger::updateDependencies (bool write_repr)
1214 {
1215 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1216 GrDraggable *draggable = (GrDraggable *) i->data;
1217 switch (draggable->point_type) {
1218 case POINT_LG_BEGIN:
1219 {
1220 // the end point is dependent only when dragging with ctrl+shift
1221 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1223 this->updateMidstopDependencies (draggable, write_repr);
1224 }
1225 break;
1226 case POINT_LG_END:
1227 {
1228 // the begin point is dependent only when dragging with ctrl+shift
1229 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1231 this->updateMidstopDependencies (draggable, write_repr);
1232 }
1233 break;
1234 case POINT_LG_MID:
1235 // no other nodes depend on mid points.
1236 break;
1237 case POINT_RG_R2:
1238 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1239 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1240 this->updateMidstopDependencies (draggable, write_repr);
1241 break;
1242 case POINT_RG_R1:
1243 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1244 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1245 this->updateMidstopDependencies (draggable, write_repr);
1246 break;
1247 case POINT_RG_CENTER:
1248 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1249 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1250 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1251 this->updateMidstopDependencies (draggable, write_repr);
1252 break;
1253 case POINT_RG_FOCUS:
1254 // nothing can depend on that
1255 break;
1256 case POINT_RG_MID1:
1257 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1258 break;
1259 case POINT_RG_MID2:
1260 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1261 break;
1262 default:
1263 break;
1264 }
1265 }
1266 }
1270 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1271 : point(p),
1272 point_original(p)
1273 {
1274 this->draggables = NULL;
1276 this->parent = parent;
1278 // create the knot
1279 this->knot = sp_knot_new (parent->desktop, NULL);
1280 this->knot->setMode(SP_KNOT_MODE_XOR);
1281 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1282 this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1283 sp_knot_update_ctrl(this->knot);
1285 // move knot to the given point
1286 sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1287 sp_knot_show (this->knot);
1289 // connect knot's signals
1290 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1291 // luckily, midstops never snap to other nodes so are never unsnapped...
1292 && ( (draggable->point_type == POINT_LG_MID)
1293 || (draggable->point_type == POINT_RG_MID1)
1294 || (draggable->point_type == POINT_RG_MID2) ) )
1295 {
1296 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1297 } else {
1298 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1299 }
1300 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1301 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1302 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1303 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1305 // add the initial draggable
1306 if (draggable)
1307 this->addDraggable (draggable);
1308 updateKnotShape();
1309 }
1311 GrDragger::~GrDragger ()
1312 {
1313 // unselect if it was selected
1314 this->parent->setDeselected(this);
1316 // disconnect signals
1317 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1318 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1319 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1320 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1321 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1323 /* unref should call destroy */
1324 g_object_unref (G_OBJECT (this->knot));
1326 // delete all draggables
1327 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1328 delete ((GrDraggable *) i->data);
1329 }
1330 g_slist_free (this->draggables);
1331 this->draggables = NULL;
1332 }
1334 /**
1335 Select the dragger which has the given draggable.
1336 */
1337 GrDragger *
1338 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1339 {
1340 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1341 GrDragger *dragger = (GrDragger *) i->data;
1342 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1343 GrDraggable *da2 = (GrDraggable *) j->data;
1344 if ( (da2->item == item) &&
1345 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1346 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1347 (da2->fill_or_stroke == fill_or_stroke)) {
1348 return (dragger);
1349 }
1350 }
1351 }
1352 return NULL;
1353 }
1356 void
1357 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1358 {
1359 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1360 if (d && d != this) {
1361 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1362 }
1363 }
1366 /**
1367 Draw this dragger as selected
1368 */
1369 void
1370 GrDragger::select()
1371 {
1372 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1373 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1374 }
1376 /**
1377 Draw this dragger as normal (deselected)
1378 */
1379 void
1380 GrDragger::deselect()
1381 {
1382 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1383 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1384 }
1386 bool
1387 GrDragger::isSelected()
1388 {
1389 return g_list_find (parent->selected, this);
1390 }
1392 /**
1393 \brief Deselect all stops/draggers (private)
1394 */
1395 void
1396 GrDrag::deselect_all()
1397 {
1398 while (selected) {
1399 ( (GrDragger*) selected->data)->deselect();
1400 selected = g_list_remove(selected, selected->data);
1401 }
1402 }
1404 /**
1405 \brief Deselect all stops/draggers (public; emits signal)
1406 */
1407 void
1408 GrDrag::deselectAll()
1409 {
1410 deselect_all();
1411 this->desktop->emitToolSubselectionChanged(NULL);
1412 }
1414 /**
1415 \brief Select all stops/draggers
1416 */
1417 void
1418 GrDrag::selectAll()
1419 {
1420 for (GList *l = this->draggers; l != NULL; l = l->next) {
1421 GrDragger *d = ((GrDragger *) l->data);
1422 setSelected (d, true, true);
1423 }
1424 }
1426 /**
1427 \brief Select all stops/draggers that match the coords
1428 */
1429 void
1430 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1431 {
1432 for (GList *l = this->draggers; l != NULL; l = l->next) {
1433 GrDragger *d = ((GrDragger *) l->data);
1434 for (guint k = 0; k < coords.size(); k++) {
1435 if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1436 setSelected (d, true, true);
1437 }
1438 }
1439 }
1440 }
1443 /**
1444 \brief Select all stops/draggers that fall within the rect
1445 */
1446 void
1447 GrDrag::selectRect(Geom::Rect const &r)
1448 {
1449 for (GList *l = this->draggers; l != NULL; l = l->next) {
1450 GrDragger *d = ((GrDragger *) l->data);
1451 if (r.contains(d->point)) {
1452 setSelected (d, true, true);
1453 }
1454 }
1455 }
1457 /**
1458 \brief Select a dragger
1459 \param dragger The dragger to select
1460 \param add_to_selection If true, add to selection, otherwise deselect others
1461 \param override If true, always select this node, otherwise toggle selected status
1462 */
1463 void
1464 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1465 {
1466 GrDragger *seldragger = NULL;
1468 if (add_to_selection) {
1469 if (!dragger) return;
1470 if (override) {
1471 if (!g_list_find(selected, dragger)) {
1472 selected = g_list_prepend(selected, dragger);
1473 }
1474 dragger->select();
1475 seldragger = dragger;
1476 } else { // toggle
1477 if (g_list_find(selected, dragger)) {
1478 selected = g_list_remove(selected, dragger);
1479 dragger->deselect();
1480 if (selected) {
1481 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1482 }
1483 } else {
1484 selected = g_list_prepend(selected, dragger);
1485 dragger->select();
1486 seldragger = dragger;
1487 }
1488 }
1489 } else {
1490 deselect_all();
1491 if (dragger) {
1492 selected = g_list_prepend(selected, dragger);
1493 dragger->select();
1494 seldragger = dragger;
1495 }
1496 }
1497 if (seldragger) {
1498 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1499 }
1500 }
1502 /**
1503 \brief Deselect a dragger
1504 \param dragger The dragger to deselect
1505 */
1506 void
1507 GrDrag::setDeselected (GrDragger *dragger)
1508 {
1509 if (g_list_find(selected, dragger)) {
1510 selected = g_list_remove(selected, dragger);
1511 dragger->deselect();
1512 }
1513 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1514 }
1518 /**
1519 Create a line from p1 to p2 and add it to the lines list
1520 */
1521 void
1522 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1523 {
1524 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1525 SP_TYPE_CTRLLINE, NULL);
1526 sp_canvas_item_move_to_z(line, 0);
1527 SP_CTRLLINE(line)->item = item;
1528 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1529 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1530 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1531 sp_canvas_item_show (line);
1532 this->lines = g_slist_append (this->lines, line);
1533 }
1535 /**
1536 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1537 new dragger and add it to draggers list
1538 */
1539 void
1540 GrDrag::addDragger (GrDraggable *draggable)
1541 {
1542 Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1544 for (GList *i = this->draggers; i != NULL; i = i->next) {
1545 GrDragger *dragger = (GrDragger *) i->data;
1546 if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1547 // distance is small, merge this draggable into dragger, no need to create new dragger
1548 dragger->addDraggable (draggable);
1549 dragger->updateKnotShape();
1550 return;
1551 }
1552 }
1554 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1555 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1556 this->draggers = g_list_append (this->draggers, new_dragger);
1557 }
1559 /**
1560 Add draggers for the radial gradient rg on item
1561 */
1562 void
1563 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1564 {
1565 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1566 guint num = rg->vector.stops.size();
1567 if (num > 2) {
1568 for ( guint i = 1; i < num - 1; i++ ) {
1569 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1570 }
1571 }
1572 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1573 if (num > 2) {
1574 for ( guint i = 1; i < num - 1; i++ ) {
1575 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1576 }
1577 }
1578 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1579 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1580 }
1582 /**
1583 Add draggers for the linear gradient lg on item
1584 */
1585 void
1586 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1587 {
1588 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1589 guint num = lg->vector.stops.size();
1590 if (num > 2) {
1591 for ( guint i = 1; i < num - 1; i++ ) {
1592 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1593 }
1594 }
1595 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1596 }
1598 /**
1599 Artificially grab the knot of this dragger; used by the gradient context
1600 */
1601 void
1602 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1603 {
1604 if (dragger) {
1605 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1606 }
1607 }
1609 /**
1610 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1611 */
1612 void
1613 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1614 {
1615 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1616 if (dragger) {
1617 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1618 }
1619 }
1621 /**
1622 Regenerates the draggers list from the current selection; is called when selection is changed or
1623 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1624 */
1625 void
1626 GrDrag::updateDraggers ()
1627 {
1628 while (selected) {
1629 selected = g_list_remove(selected, selected->data);
1630 }
1631 // delete old draggers
1632 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1633 delete ((GrDragger *) i->data);
1634 }
1635 g_list_free (this->draggers);
1636 this->draggers = NULL;
1638 g_return_if_fail (this->selection != NULL);
1640 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1642 SPItem *item = SP_ITEM(i->data);
1643 SPStyle *style = SP_OBJECT_STYLE (item);
1645 if (style && (style->fill.isPaintserver())) {
1646 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1647 if (SP_IS_LINEARGRADIENT (server)) {
1648 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1649 } else if (SP_IS_RADIALGRADIENT (server)) {
1650 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1651 }
1652 }
1654 if (style && (style->stroke.isPaintserver())) {
1655 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1656 if (SP_IS_LINEARGRADIENT (server)) {
1657 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1658 } else if (SP_IS_RADIALGRADIENT (server)) {
1659 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1660 }
1661 }
1662 }
1663 }
1665 /**
1666 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1667 lines are always in sync with the actual gradient
1668 */
1669 void
1670 GrDrag::updateLines ()
1671 {
1672 // delete old lines
1673 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1674 gtk_object_destroy( GTK_OBJECT (i->data));
1675 }
1676 g_slist_free (this->lines);
1677 this->lines = NULL;
1679 g_return_if_fail (this->selection != NULL);
1681 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1683 SPItem *item = SP_ITEM(i->data);
1685 SPStyle *style = SP_OBJECT_STYLE (item);
1687 if (style && (style->fill.isPaintserver())) {
1688 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1689 if (SP_IS_LINEARGRADIENT (server)) {
1690 this->addLine (item, sp_item_gradient_get_coords (item, POINT_LG_BEGIN, 0, true), sp_item_gradient_get_coords (item, POINT_LG_END, 0, true), GR_LINE_COLOR_FILL);
1691 } else if (SP_IS_RADIALGRADIENT (server)) {
1692 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1693 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1694 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1695 }
1696 }
1698 if (style && (style->stroke.isPaintserver())) {
1699 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1700 if (SP_IS_LINEARGRADIENT (server)) {
1701 this->addLine (item, sp_item_gradient_get_coords (item, POINT_LG_BEGIN, 0, false), sp_item_gradient_get_coords (item, POINT_LG_END, 0, false), GR_LINE_COLOR_STROKE);
1702 } else if (SP_IS_RADIALGRADIENT (server)) {
1703 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1704 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1705 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1706 }
1707 }
1708 }
1709 }
1711 /**
1712 Regenerates the levels list from the current selection
1713 */
1714 void
1715 GrDrag::updateLevels ()
1716 {
1717 hor_levels.clear();
1718 vert_levels.clear();
1720 g_return_if_fail (this->selection != NULL);
1722 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1723 SPItem *item = SP_ITEM(i->data);
1724 Geom::OptRect rect = sp_item_bbox_desktop (item);
1725 if (rect) {
1726 // Remember the edges of the bbox and the center axis
1727 hor_levels.push_back(rect->min()[Geom::Y]);
1728 hor_levels.push_back(rect->max()[Geom::Y]);
1729 hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1730 vert_levels.push_back(rect->min()[Geom::X]);
1731 vert_levels.push_back(rect->max()[Geom::X]);
1732 vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1733 }
1734 }
1735 }
1737 void
1738 GrDrag::selected_reverse_vector ()
1739 {
1740 if (selected == NULL)
1741 return;
1743 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1744 GrDraggable *draggable = (GrDraggable *) i->data;
1746 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1747 }
1748 }
1750 void
1751 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1752 {
1753 selected_move (x, y, false, scale_radial);
1754 }
1756 void
1757 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1758 {
1759 if (selected == NULL)
1760 return;
1762 bool did = false;
1764 for (GList *i = selected; i != NULL; i = i->next) {
1765 GrDragger *d = (GrDragger *) i->data;
1767 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1768 // if this is an endpoint,
1770 // Moving an rg center moves its focus and radii as well.
1771 // therefore, if this is a focus or radius and if selection
1772 // contains the center as well, do not move this one
1773 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1774 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1775 bool skip_radius_with_center = false;
1776 for (GList *di = selected; di != NULL; di = di->next) {
1777 GrDragger *d_new = (GrDragger *) di->data;
1778 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1779 POINT_RG_CENTER,
1780 0,
1781 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1782 // FIXME: here we take into account only the first draggable!
1783 skip_radius_with_center = true;
1784 }
1785 }
1786 if (skip_radius_with_center)
1787 continue;
1788 }
1790 did = true;
1791 d->point += Geom::Point (x, y);
1792 d->point_original = d->point;
1793 sp_knot_moveto (d->knot, d->point);
1795 d->fireDraggables (write_repr, scale_radial);
1797 d->updateDependencies(write_repr);
1798 }
1799 }
1801 if (write_repr && did) {
1802 // we did an undoable action
1803 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1804 _("Move gradient handle(s)"));
1805 return;
1806 }
1808 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1810 GrDragger *dragger = (GrDragger *) selected->data;
1811 // a midpoint dragger can (logically) only contain one GrDraggable
1812 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1814 Geom::Point begin(0,0), end(0,0);
1815 Geom::Point low_lim(0,0), high_lim(0,0);
1817 SPObject *server = draggable->getServer();
1818 GSList *moving = NULL;
1819 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1821 Geom::Point p(x, y);
1822 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1823 Geom::Point displacement = p - dragger->point;
1825 for (GSList const* i = moving; i != NULL; i = i->next) {
1826 GrDragger *drg = (GrDragger*) i->data;
1827 SPKnot *drgknot = drg->knot;
1828 drg->point += displacement;
1829 sp_knot_moveto (drgknot, drg->point);
1830 drg->fireDraggables (true);
1831 drg->updateDependencies(true);
1832 did = true;
1833 }
1835 g_slist_free(moving);
1837 if (write_repr && did) {
1838 // we did an undoable action
1839 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1840 _("Move gradient mid stop(s)"));
1841 }
1842 }
1843 }
1845 void
1846 GrDrag::selected_move_screen (double x, double y)
1847 {
1848 gdouble zoom = desktop->current_zoom();
1849 gdouble zx = x / zoom;
1850 gdouble zy = y / zoom;
1852 selected_move (zx, zy);
1853 }
1855 /**
1856 Select the knot next to the last selected one and deselect all other selected.
1857 */
1858 GrDragger *
1859 GrDrag::select_next ()
1860 {
1861 GrDragger *d = NULL;
1862 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1863 if (draggers)
1864 d = (GrDragger *) draggers->data;
1865 } else {
1866 d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1867 }
1868 if (d)
1869 setSelected (d);
1870 return d;
1871 }
1873 /**
1874 Select the knot previous from the last selected one and deselect all other selected.
1875 */
1876 GrDragger *
1877 GrDrag::select_prev ()
1878 {
1879 GrDragger *d = NULL;
1880 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1881 if (draggers)
1882 d = (GrDragger *) g_list_last (draggers)->data;
1883 } else {
1884 d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1885 }
1886 if (d)
1887 setSelected (d);
1888 return d;
1889 }
1892 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1893 void
1894 GrDrag::deleteSelected (bool just_one)
1895 {
1896 if (!selected) return;
1898 SPDocument *document = false;
1900 struct StructStopInfo {
1901 SPStop * spstop;
1902 GrDraggable * draggable;
1903 SPGradient * gradient;
1904 SPGradient * vector;
1905 };
1907 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1908 GSList *endstoplist = NULL; // list of stops that must be deleted
1909 while (selected) {
1910 GrDragger *dragger = (GrDragger*) selected->data;
1911 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1912 GrDraggable *draggable = (GrDraggable*) drgble->data;
1913 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1914 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1916 switch (draggable->point_type) {
1917 case POINT_LG_MID:
1918 case POINT_RG_MID1:
1919 case POINT_RG_MID2:
1920 {
1921 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1922 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1923 bool present = false;
1924 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1925 if ( (SPStop*)l->data == stop ) {
1926 present = true;
1927 break; // no need to search further.
1928 }
1929 }
1930 if (!present)
1931 midstoplist = g_slist_append(midstoplist, stop);
1932 }
1933 break;
1934 case POINT_LG_BEGIN:
1935 case POINT_LG_END:
1936 case POINT_RG_CENTER:
1937 case POINT_RG_R1:
1938 case POINT_RG_R2:
1939 {
1940 SPStop *stop = NULL;
1941 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1942 stop = sp_first_stop(vector);
1943 } else {
1944 stop = sp_last_stop(vector);
1945 }
1946 if (stop) {
1947 StructStopInfo *stopinfo = new StructStopInfo;
1948 stopinfo->spstop = stop;
1949 stopinfo->draggable = draggable;
1950 stopinfo->gradient = gradient;
1951 stopinfo->vector = vector;
1952 // check if already present in list. (e.g. when both R1 and R2 were selected)
1953 bool present = false;
1954 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1955 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1956 present = true;
1957 break; // no need to search further.
1958 }
1959 }
1960 if (!present)
1961 endstoplist = g_slist_append(endstoplist, stopinfo);
1962 }
1963 }
1964 break;
1965 default:
1966 break;
1967 }
1968 }
1969 selected = g_list_remove(selected, dragger);
1970 if ( just_one ) break; // iterate once if just_one is set.
1971 }
1972 while (midstoplist) {
1973 SPStop *stop = (SPStop*) midstoplist->data;
1974 document = SP_OBJECT_DOCUMENT (stop);
1975 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1976 parent->removeChild(SP_OBJECT_REPR(stop));
1977 midstoplist = g_slist_remove(midstoplist, stop);
1978 }
1979 while (endstoplist) {
1980 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
1981 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1983 // 2 is the minimum, cannot delete more than that without deleting the whole vector
1984 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1985 // manually count the children, don't know if there already exists a function for this...
1986 int len = 0;
1987 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1988 child != NULL ;
1989 child = SP_OBJECT_NEXT(child) )
1990 {
1991 if ( SP_IS_STOP(child) ) len ++;
1992 }
1993 if (len > 2)
1994 {
1995 switch (stopinfo->draggable->point_type) {
1996 case POINT_LG_BEGIN:
1997 {
1998 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2000 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2001 Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2002 Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2003 SPStop *stop = sp_first_stop(stopinfo->vector);
2004 gdouble offset = stop->offset;
2005 Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2006 lg->x1.computed = newbegin[Geom::X];
2007 lg->y1.computed = newbegin[Geom::Y];
2009 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2010 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2011 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2012 stop->offset = 0;
2013 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2015 // iterate through midstops to set new offset values such that they won't move on canvas.
2016 SPStop *laststop = sp_last_stop(stopinfo->vector);
2017 stop = sp_next_stop(stop);
2018 while ( stop != laststop ) {
2019 stop->offset = (stop->offset - offset)/(1 - offset);
2020 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2021 stop = sp_next_stop(stop);
2022 }
2023 }
2024 break;
2025 case POINT_LG_END:
2026 {
2027 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2029 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2030 Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2031 Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2032 SPStop *laststop = sp_last_stop(stopinfo->vector);
2033 gdouble offset = laststop->offset;
2034 Geom::Point newend = begin + offset * (oldend - begin);
2035 lg->x2.computed = newend[Geom::X];
2036 lg->y2.computed = newend[Geom::Y];
2038 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2039 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2040 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2041 laststop->offset = 1;
2042 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2044 // iterate through midstops to set new offset values such that they won't move on canvas.
2045 SPStop *stop = sp_first_stop(stopinfo->vector);
2046 stop = sp_next_stop(stop);
2047 while ( stop != laststop ) {
2048 stop->offset = stop->offset / offset;
2049 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2050 stop = sp_next_stop(stop);
2051 }
2052 }
2053 break;
2054 case POINT_RG_CENTER:
2055 {
2056 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2057 if (newfirst) {
2058 newfirst->offset = 0;
2059 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2060 }
2061 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2062 }
2063 break;
2064 case POINT_RG_R1:
2065 case POINT_RG_R2:
2066 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2068 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2069 double oldradius = rg->r.computed;
2070 SPStop *laststop = sp_last_stop(stopinfo->vector);
2071 gdouble offset = laststop->offset;
2072 double newradius = offset * oldradius;
2073 rg->r.computed = newradius;
2075 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2076 sp_repr_set_svg_double(repr, "r", rg->r.computed);
2077 laststop->offset = 1;
2078 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2080 // iterate through midstops to set new offset values such that they won't move on canvas.
2081 SPStop *stop = sp_first_stop(stopinfo->vector);
2082 stop = sp_next_stop(stop);
2083 while ( stop != laststop ) {
2084 stop->offset = stop->offset / offset;
2085 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2086 stop = sp_next_stop(stop);
2087 }
2088 break;
2089 }
2090 }
2091 else
2092 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
2093 SPCSSAttr *css = sp_repr_css_attr_new ();
2095 // stopinfo->spstop is the selected stop
2096 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2097 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2098 unselectedrepr = unselectedrepr->next();
2099 }
2101 if (unselectedrepr == NULL) {
2102 if (stopinfo->draggable->fill_or_stroke) {
2103 sp_repr_css_unset_property (css, "fill");
2104 } else {
2105 sp_repr_css_unset_property (css, "stroke");
2106 }
2107 } else {
2108 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2109 if (stopinfo->draggable->fill_or_stroke) {
2110 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2111 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2112 } else {
2113 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2114 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2115 }
2116 sp_repr_css_attr_unref (stopcss);
2117 }
2119 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2120 sp_repr_css_attr_unref (css);
2121 }
2123 endstoplist = g_slist_remove(endstoplist, stopinfo);
2124 delete stopinfo;
2125 }
2127 if (document) {
2128 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2129 }
2130 }
2133 /*
2134 Local Variables:
2135 mode:c++
2136 c-file-style:"stroustrup"
2137 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2138 indent-tabs-mode:nil
2139 fill-column:99
2140 End:
2141 */
2142 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :