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 *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 // FIXME: take from prefs
545 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
547 if (state & GDK_SHIFT_MASK) {
548 // with Shift; unsnap if we carry more than one draggable
549 if (dragger->draggables && dragger->draggables->next) {
550 // create a new dragger
551 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
552 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
553 // relink to it all but the first draggable in the list
554 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
555 GrDraggable *draggable = (GrDraggable *) i->data;
556 dr_new->addDraggable (draggable);
557 }
558 dr_new->updateKnotShape();
559 g_slist_free (dragger->draggables->next);
560 dragger->draggables->next = NULL;
561 dragger->updateKnotShape();
562 dragger->updateTip();
563 }
564 } else if (!(state & GDK_CONTROL_MASK)) {
565 // without Shift or Ctrl; see if we need to snap to another dragger
566 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
567 GrDragger *d_new = (GrDragger *) di->data;
568 if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
570 // Merge draggers:
571 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
572 GrDraggable *draggable = (GrDraggable *) i->data;
573 // copy draggable to d_new:
574 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
575 d_new->addDraggable (da_new);
576 }
578 // unlink and delete this dragger
579 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
580 delete dragger;
582 // update the new merged dragger
583 d_new->fireDraggables(true, false, true);
584 d_new->parent->updateLines();
585 d_new->parent->setSelected (d_new);
586 d_new->updateKnotShape ();
587 d_new->updateTip ();
588 d_new->updateDependencies(true);
589 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
590 _("Merge gradient handles"));
591 return;
592 }
593 }
594 }
596 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
597 // Try snapping to the grid or guides
598 SPDesktop *desktop = dragger->parent->desktop;
599 SnapManager &m = desktop->namedview->snap_manager;
600 m.setup(desktop);
601 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapPreferences::SNAPPOINT_NODE, to_2geom(p));
602 if (s.getSnapped()) {
603 p = s.getPoint();
604 sp_knot_moveto (knot, p);
605 } else {
606 bool was_snapped = false;
607 double dist = NR_HUGE;
608 // No snapping so far, let's see if we need to snap to any of the levels
609 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
610 dist = fabs(p[Geom::Y] - dragger->parent->hor_levels[i]);
611 if (dist < snap_dist) {
612 p[Geom::Y] = dragger->parent->hor_levels[i];
613 s = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_GRADIENT, dist, snap_dist, false, false);
614 was_snapped = true;
615 sp_knot_moveto (knot, p);
616 }
617 }
618 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
619 dist = fabs(p[Geom::X] - dragger->parent->vert_levels[i]);
620 if (dist < snap_dist) {
621 p[Geom::X] = dragger->parent->vert_levels[i];
622 s = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_GRADIENT, dist, snap_dist, false, false);
623 was_snapped = true;
624 sp_knot_moveto (knot, p);
625 }
626 }
627 if (was_snapped) {
628 desktop->snapindicator->set_new_snaptarget(s);
629 }
630 }
631 }
633 if (state & GDK_CONTROL_MASK) {
634 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
635 unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
636 /* 0 means no snapping. */
638 // This list will store snap vectors from all draggables of dragger
639 GSList *snap_vectors = NULL;
641 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
642 GrDraggable *draggable = (GrDraggable *) i->data;
644 Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
646 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
647 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
648 GrDragger *d_new = (GrDragger *) di->data;
649 if (d_new == dragger)
650 continue;
651 if (d_new->isA (draggable->item,
652 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
653 draggable->fill_or_stroke)) {
654 // found the other end of the linear gradient;
655 if (state & GDK_SHIFT_MASK) {
656 // moving linear around center
657 Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
658 dr_snap = center;
659 } else {
660 // moving linear around the other end
661 dr_snap = d_new->point;
662 }
663 }
664 }
665 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
666 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
667 GrDragger *d_new = (GrDragger *) di->data;
668 if (d_new == dragger)
669 continue;
670 if (d_new->isA (draggable->item,
671 POINT_RG_CENTER,
672 draggable->fill_or_stroke)) {
673 // found the center of the radial gradient;
674 dr_snap = d_new->point;
675 }
676 }
677 } else if (draggable->point_type == POINT_RG_CENTER) {
678 // radial center snaps to hor/vert relative to its original position
679 dr_snap = dragger->point_original;
680 }
682 boost::optional<Geom::Point> snap_vector;
683 if (dr_snap.isFinite()) {
684 if (state & GDK_MOD1_MASK) {
685 // with Alt, snap to the original angle and its perpendiculars
686 snap_vector = get_snap_vector (p, dr_snap, M_PI/2, Geom::atan2 (dragger->point_original - dr_snap));
687 } else {
688 // with Ctrl, snap to M_PI/snaps
689 snap_vector = get_snap_vector (p, dr_snap, M_PI/snaps, 0);
690 }
691 }
692 if (snap_vector) {
693 snap_vectors = g_slist_prepend (snap_vectors, &(*snap_vector));
694 }
695 }
697 // Move by the smallest of snap vectors:
698 Geom::Point move(9999, 9999);
699 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
700 Geom::Point *snap_vector = (Geom::Point *) i->data;
701 if (Geom::L2(*snap_vector) < Geom::L2(move))
702 move = *snap_vector;
703 }
704 if (move[Geom::X] < 9999) {
705 p += move;
706 sp_knot_moveto (knot, p);
707 }
709 g_slist_free(snap_vectors);
710 }
712 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
713 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
715 if (drag->keep_selection) {
716 Geom::Point diff = p - dragger->point;
717 drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
718 } else {
719 dragger->point = p;
720 dragger->fireDraggables (false, scale_radial);
721 dragger->updateDependencies(false);
722 }
724 }
727 static void
728 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
729 {
731 GrDrag *drag = dragger->parent;
732 // a midpoint dragger can (logically) only contain one GrDraggable
733 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
735 // get begin and end points between which dragging is allowed:
736 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
737 *moving = g_slist_append(*moving, dragger);
739 guint lowest_i = draggable->point_i;
740 guint highest_i = draggable->point_i;
741 GrDragger *lowest_dragger = dragger;
742 GrDragger *highest_dragger = dragger;
743 if (dragger->isSelected()) {
744 GrDragger* d_add;
745 while ( true )
746 {
747 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
748 if ( d_add && g_list_find(drag->selected, d_add) ) {
749 lowest_i = lowest_i - 1;
750 *moving = g_slist_prepend(*moving, d_add);
751 lowest_dragger = d_add;
752 } else {
753 break;
754 }
755 }
757 while ( true )
758 {
759 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
760 if ( d_add && g_list_find(drag->selected, d_add) ) {
761 highest_i = highest_i + 1;
762 *moving = g_slist_append(*moving, d_add);
763 highest_dragger = d_add;
764 } else {
765 break;
766 }
767 }
768 }
770 if ( SP_IS_LINEARGRADIENT(server) ) {
771 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
772 GrDragger *d_temp;
773 if (lowest_i == 1) {
774 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
775 } else {
776 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
777 }
778 if (d_temp)
779 *begin = d_temp->point;
781 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
782 if (d_temp == NULL) {
783 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
784 }
785 if (d_temp)
786 *end = d_temp->point;
787 } else if ( SP_IS_RADIALGRADIENT(server) ) {
788 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
789 GrDragger *d_temp;
790 if (lowest_i == 1) {
791 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
792 } else {
793 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
794 }
795 if (d_temp)
796 *begin = d_temp->point;
798 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
799 if (d_temp == NULL) {
800 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
801 }
802 if (d_temp)
803 *end = d_temp->point;
804 }
806 *low_lim = dragger->point - (lowest_dragger->point - *begin);
807 *high_lim = dragger->point - (highest_dragger->point - *end);
808 }
812 /**
813 Called when a midpoint knot is dragged.
814 */
815 static void
816 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
817 {
818 GrDragger *dragger = (GrDragger *) data;
819 GrDrag *drag = dragger->parent;
820 // a midpoint dragger can (logically) only contain one GrDraggable
821 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
823 // FIXME: take from prefs
824 double snap_fraction = 0.1;
826 Geom::Point p = ppointer;
827 Geom::Point begin(0,0), end(0,0);
828 Geom::Point low_lim(0,0), high_lim(0,0);
830 SPObject *server = draggable->getServer();
832 GSList *moving = NULL;
833 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
835 if (state & GDK_CONTROL_MASK) {
836 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
837 } else {
838 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
839 }
840 Geom::Point displacement = p - dragger->point;
842 for (GSList const* i = moving; i != NULL; i = i->next) {
843 GrDragger *drg = (GrDragger*) i->data;
844 SPKnot *drgknot = drg->knot;
845 Geom::Point this_move = displacement;
846 if (state & GDK_MOD1_MASK) {
847 // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
848 double alpha = 1.0;
849 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
850 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
851 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
852 } else { // drg is on the begin side from dragger
853 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
854 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
855 }
856 }
857 drg->point += this_move;
858 sp_knot_moveto (drgknot, drg->point);
859 drg->fireDraggables (false);
860 drg->updateDependencies(false);
861 }
863 g_slist_free(moving);
865 drag->keep_selection = dragger->isSelected();
866 }
870 static void
871 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
872 {
873 GrDragger *dragger = (GrDragger *) data;
875 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
876 }
878 /**
879 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
880 */
881 static void
882 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
883 {
884 GrDragger *dragger = (GrDragger *) data;
886 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
888 dragger->point_original = dragger->point = knot->pos;
890 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
891 dragger->fireDraggables (true, true);
892 } else {
893 dragger->fireDraggables (true);
894 }
896 for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
897 GrDragger *d = (GrDragger *) i->data;
898 if (d == dragger)
899 continue;
900 d->fireDraggables (true);
901 }
903 // make this dragger selected
904 if (!dragger->parent->keep_selection) {
905 dragger->parent->setSelected (dragger);
906 }
907 dragger->parent->keep_selection = false;
909 dragger->updateDependencies(true);
911 // we did an undoable action
912 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
913 _("Move gradient handle"));
914 }
916 /**
917 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
918 state of the keyboard keys
919 */
920 static void
921 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
922 {
923 GrDragger *dragger = (GrDragger *) data;
924 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
925 if (!draggable) return;
927 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
928 // delete this knot from vector
929 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
930 gradient = sp_gradient_get_vector (gradient, false);
931 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
932 SPStop *stop = NULL;
933 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
934 case POINT_LG_BEGIN:
935 case POINT_RG_CENTER:
936 stop = sp_first_stop(gradient);
937 {
938 SPStop *next = sp_next_stop (stop);
939 if (next) {
940 next->offset = 0;
941 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
942 }
943 }
944 break;
945 case POINT_LG_END:
946 case POINT_RG_R1:
947 case POINT_RG_R2:
948 stop = sp_last_stop(gradient);
949 {
950 SPStop *prev = sp_prev_stop (stop, gradient);
951 if (prev) {
952 prev->offset = 1;
953 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
954 }
955 }
956 break;
957 case POINT_LG_MID:
958 case POINT_RG_MID1:
959 case POINT_RG_MID2:
960 stop = sp_get_stop_i(gradient, draggable->point_i);
961 break;
962 }
964 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
965 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
966 _("Delete gradient stop"));
967 }
968 } else {
969 // select the dragger
970 dragger->point_original = dragger->point;
972 if ( state & GDK_SHIFT_MASK ) {
973 dragger->parent->setSelected (dragger, true, false);
974 } else {
975 dragger->parent->setSelected (dragger);
976 }
977 }
978 }
980 /**
981 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
982 */
983 static void
984 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
985 {
986 GrDragger *dragger = (GrDragger *) data;
988 dragger->point_original = dragger->point;
990 if (dragger->draggables == NULL)
991 return;
993 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
994 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
995 }
997 /**
998 Act upon all draggables of the dragger, setting them to the dragger's point
999 */
1000 void
1001 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1002 {
1003 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1004 GrDraggable *draggable = (GrDraggable *) i->data;
1006 // set local_change flag so that selection_changed callback does not regenerate draggers
1007 this->parent->local_change = true;
1009 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1010 // to the center, unless it's the first update upon merge when we must snap it to the point
1011 if (merging_focus ||
1012 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1013 {
1014 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1015 }
1016 }
1017 }
1019 /**
1020 Checks if the dragger has a draggable with this point_type
1021 */
1022 bool
1023 GrDragger::isA (gint point_type)
1024 {
1025 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1026 GrDraggable *draggable = (GrDraggable *) i->data;
1027 if (draggable->point_type == point_type) {
1028 return true;
1029 }
1030 }
1031 return false;
1032 }
1034 /**
1035 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1036 */
1037 bool
1038 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1039 {
1040 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1041 GrDraggable *draggable = (GrDraggable *) i->data;
1042 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1043 return true;
1044 }
1045 }
1046 return false;
1047 }
1049 /**
1050 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1051 */
1052 bool
1053 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1054 {
1055 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1056 GrDraggable *draggable = (GrDraggable *) i->data;
1057 if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1058 return true;
1059 }
1060 }
1061 return false;
1062 }
1064 bool
1065 GrDraggable::mayMerge (GrDraggable *da2)
1066 {
1067 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1068 // we must not merge the points of the same gradient!
1069 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1070 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1071 // except that we can snap center and focus together
1072 return false;
1073 }
1074 }
1075 // disable merging of midpoints.
1076 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1077 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1078 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1079 return false;
1081 return true;
1082 }
1084 bool
1085 GrDragger::mayMerge (GrDragger *other)
1086 {
1087 if (this == other)
1088 return false;
1090 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1091 GrDraggable *da1 = (GrDraggable *) i->data;
1092 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1093 GrDraggable *da2 = (GrDraggable *) j->data;
1094 if (!da1->mayMerge(da2))
1095 return false;
1096 }
1097 }
1098 return true;
1099 }
1101 bool
1102 GrDragger::mayMerge (GrDraggable *da2)
1103 {
1104 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1105 GrDraggable *da1 = (GrDraggable *) i->data;
1106 if (!da1->mayMerge(da2))
1107 return false;
1108 }
1109 return true;
1110 }
1112 /**
1113 Updates the statusbar tip of the dragger knot, based on its draggables
1114 */
1115 void
1116 GrDragger::updateTip ()
1117 {
1118 if (this->knot && this->knot->tip) {
1119 g_free (this->knot->tip);
1120 this->knot->tip = NULL;
1121 }
1123 if (g_slist_length (this->draggables) == 1) {
1124 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1125 char *item_desc = sp_item_description(draggable->item);
1126 switch (draggable->point_type) {
1127 case POINT_LG_MID:
1128 case POINT_RG_MID1:
1129 case POINT_RG_MID2:
1130 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"),
1131 _(gr_knot_descr[draggable->point_type]),
1132 draggable->point_i,
1133 item_desc,
1134 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1135 break;
1137 default:
1138 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"),
1139 _(gr_knot_descr[draggable->point_type]),
1140 item_desc,
1141 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1142 break;
1143 }
1144 g_free(item_desc);
1145 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1146 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1147 } else {
1148 int length = g_slist_length (this->draggables);
1149 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1150 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1151 length),
1152 length);
1153 }
1154 }
1156 /**
1157 Adds a draggable to the dragger
1158 */
1159 void
1160 GrDragger::updateKnotShape ()
1161 {
1162 if (!draggables)
1163 return;
1164 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1165 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1166 }
1168 /**
1169 Adds a draggable to the dragger
1170 */
1171 void
1172 GrDragger::addDraggable (GrDraggable *draggable)
1173 {
1174 this->draggables = g_slist_prepend (this->draggables, draggable);
1176 this->updateTip();
1177 }
1180 /**
1181 Moves this dragger to the point of the given draggable, acting upon all other draggables
1182 */
1183 void
1184 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1185 {
1186 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1187 this->point_original = this->point;
1189 sp_knot_moveto (this->knot, this->point);
1191 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1192 GrDraggable *da = (GrDraggable *) i->data;
1193 if ( (da->item == item) &&
1194 (point_type == -1 || da->point_type == point_type) &&
1195 (point_i == -1 || da->point_i == point_i) &&
1196 (da->fill_or_stroke == fill_or_stroke) ) {
1197 continue;
1198 }
1199 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1200 }
1201 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1202 }
1205 /**
1206 Moves all midstop draggables that depend on this one
1207 */
1208 void
1209 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1210 SPObject *server = draggable->getServer();
1211 if (!server)
1212 return;
1213 guint num = SP_GRADIENT(server)->vector.stops.size();
1214 if (num <= 2) return;
1216 if ( SP_IS_LINEARGRADIENT(server) ) {
1217 for ( guint i = 1; i < num - 1; i++ ) {
1218 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1219 }
1220 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1221 for ( guint i = 1; i < num - 1; i++ ) {
1222 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1223 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1224 }
1225 }
1226 }
1229 /**
1230 Moves all draggables that depend on this one
1231 */
1232 void
1233 GrDragger::updateDependencies (bool write_repr)
1234 {
1235 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1236 GrDraggable *draggable = (GrDraggable *) i->data;
1237 switch (draggable->point_type) {
1238 case POINT_LG_BEGIN:
1239 {
1240 // the end point is dependent only when dragging with ctrl+shift
1241 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1243 this->updateMidstopDependencies (draggable, write_repr);
1244 }
1245 break;
1246 case POINT_LG_END:
1247 {
1248 // the begin point is dependent only when dragging with ctrl+shift
1249 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1251 this->updateMidstopDependencies (draggable, write_repr);
1252 }
1253 break;
1254 case POINT_LG_MID:
1255 // no other nodes depend on mid points.
1256 break;
1257 case POINT_RG_R2:
1258 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1259 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1260 this->updateMidstopDependencies (draggable, write_repr);
1261 break;
1262 case POINT_RG_R1:
1263 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1264 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1265 this->updateMidstopDependencies (draggable, write_repr);
1266 break;
1267 case POINT_RG_CENTER:
1268 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1269 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1270 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1271 this->updateMidstopDependencies (draggable, write_repr);
1272 break;
1273 case POINT_RG_FOCUS:
1274 // nothing can depend on that
1275 break;
1276 case POINT_RG_MID1:
1277 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1278 break;
1279 case POINT_RG_MID2:
1280 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1281 break;
1282 default:
1283 break;
1284 }
1285 }
1286 }
1290 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1291 : point(p),
1292 point_original(p)
1293 {
1294 this->draggables = NULL;
1296 this->parent = parent;
1298 // create the knot
1299 this->knot = sp_knot_new (parent->desktop, NULL);
1300 this->knot->setMode(SP_KNOT_MODE_XOR);
1301 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1302 this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1303 sp_knot_update_ctrl(this->knot);
1305 // move knot to the given point
1306 sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1307 sp_knot_show (this->knot);
1309 // connect knot's signals
1310 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1311 // luckily, midstops never snap to other nodes so are never unsnapped...
1312 && ( (draggable->point_type == POINT_LG_MID)
1313 || (draggable->point_type == POINT_RG_MID1)
1314 || (draggable->point_type == POINT_RG_MID2) ) )
1315 {
1316 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1317 } else {
1318 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1319 }
1320 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1321 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1322 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1323 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1325 // add the initial draggable
1326 if (draggable)
1327 this->addDraggable (draggable);
1328 updateKnotShape();
1329 }
1331 GrDragger::~GrDragger ()
1332 {
1333 // unselect if it was selected
1334 this->parent->setDeselected(this);
1336 // disconnect signals
1337 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1338 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1339 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1340 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1341 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1343 /* unref should call destroy */
1344 g_object_unref (G_OBJECT (this->knot));
1346 // delete all draggables
1347 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1348 delete ((GrDraggable *) i->data);
1349 }
1350 g_slist_free (this->draggables);
1351 this->draggables = NULL;
1352 }
1354 /**
1355 Select the dragger which has the given draggable.
1356 */
1357 GrDragger *
1358 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1359 {
1360 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1361 GrDragger *dragger = (GrDragger *) i->data;
1362 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1363 GrDraggable *da2 = (GrDraggable *) j->data;
1364 if ( (da2->item == item) &&
1365 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1366 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1367 (da2->fill_or_stroke == fill_or_stroke)) {
1368 return (dragger);
1369 }
1370 }
1371 }
1372 return NULL;
1373 }
1376 void
1377 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1378 {
1379 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1380 if (d && d != this) {
1381 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1382 }
1383 }
1386 /**
1387 Draw this dragger as selected
1388 */
1389 void
1390 GrDragger::select()
1391 {
1392 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1393 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1394 }
1396 /**
1397 Draw this dragger as normal (deselected)
1398 */
1399 void
1400 GrDragger::deselect()
1401 {
1402 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1403 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1404 }
1406 bool
1407 GrDragger::isSelected()
1408 {
1409 return g_list_find (parent->selected, this);
1410 }
1412 /**
1413 \brief Deselect all stops/draggers (private)
1414 */
1415 void
1416 GrDrag::deselect_all()
1417 {
1418 while (selected) {
1419 ( (GrDragger*) selected->data)->deselect();
1420 selected = g_list_remove(selected, selected->data);
1421 }
1422 }
1424 /**
1425 \brief Deselect all stops/draggers (public; emits signal)
1426 */
1427 void
1428 GrDrag::deselectAll()
1429 {
1430 deselect_all();
1431 this->desktop->emitToolSubselectionChanged(NULL);
1432 }
1434 /**
1435 \brief Select all stops/draggers
1436 */
1437 void
1438 GrDrag::selectAll()
1439 {
1440 for (GList *l = this->draggers; l != NULL; l = l->next) {
1441 GrDragger *d = ((GrDragger *) l->data);
1442 setSelected (d, true, true);
1443 }
1444 }
1446 /**
1447 \brief Select all stops/draggers that match the coords
1448 */
1449 void
1450 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1451 {
1452 for (GList *l = this->draggers; l != NULL; l = l->next) {
1453 GrDragger *d = ((GrDragger *) l->data);
1454 for (guint k = 0; k < coords.size(); k++) {
1455 if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1456 setSelected (d, true, true);
1457 }
1458 }
1459 }
1460 }
1463 /**
1464 \brief Select all stops/draggers that fall within the rect
1465 */
1466 void
1467 GrDrag::selectRect(Geom::Rect const &r)
1468 {
1469 for (GList *l = this->draggers; l != NULL; l = l->next) {
1470 GrDragger *d = ((GrDragger *) l->data);
1471 if (r.contains(d->point)) {
1472 setSelected (d, true, true);
1473 }
1474 }
1475 }
1477 /**
1478 \brief Select a dragger
1479 \param dragger The dragger to select
1480 \param add_to_selection If true, add to selection, otherwise deselect others
1481 \param override If true, always select this node, otherwise toggle selected status
1482 */
1483 void
1484 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1485 {
1486 GrDragger *seldragger = NULL;
1488 if (add_to_selection) {
1489 if (!dragger) return;
1490 if (override) {
1491 if (!g_list_find(selected, dragger)) {
1492 selected = g_list_prepend(selected, dragger);
1493 }
1494 dragger->select();
1495 seldragger = dragger;
1496 } else { // toggle
1497 if (g_list_find(selected, dragger)) {
1498 selected = g_list_remove(selected, dragger);
1499 dragger->deselect();
1500 if (selected) {
1501 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1502 }
1503 } else {
1504 selected = g_list_prepend(selected, dragger);
1505 dragger->select();
1506 seldragger = dragger;
1507 }
1508 }
1509 } else {
1510 deselect_all();
1511 if (dragger) {
1512 selected = g_list_prepend(selected, dragger);
1513 dragger->select();
1514 seldragger = dragger;
1515 }
1516 }
1517 if (seldragger) {
1518 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1519 }
1520 }
1522 /**
1523 \brief Deselect a dragger
1524 \param dragger The dragger to deselect
1525 */
1526 void
1527 GrDrag::setDeselected (GrDragger *dragger)
1528 {
1529 if (g_list_find(selected, dragger)) {
1530 selected = g_list_remove(selected, dragger);
1531 dragger->deselect();
1532 }
1533 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1534 }
1538 /**
1539 Create a line from p1 to p2 and add it to the lines list
1540 */
1541 void
1542 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1543 {
1544 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1545 SP_TYPE_CTRLLINE, NULL);
1546 sp_canvas_item_move_to_z(line, 0);
1547 SP_CTRLLINE(line)->item = item;
1548 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1549 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1550 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1551 sp_canvas_item_show (line);
1552 this->lines = g_slist_append (this->lines, line);
1553 }
1555 /**
1556 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1557 new dragger and add it to draggers list
1558 */
1559 void
1560 GrDrag::addDragger (GrDraggable *draggable)
1561 {
1562 Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1564 for (GList *i = this->draggers; i != NULL; i = i->next) {
1565 GrDragger *dragger = (GrDragger *) i->data;
1566 if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1567 // distance is small, merge this draggable into dragger, no need to create new dragger
1568 dragger->addDraggable (draggable);
1569 dragger->updateKnotShape();
1570 return;
1571 }
1572 }
1574 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1575 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1576 this->draggers = g_list_append (this->draggers, new_dragger);
1577 }
1579 /**
1580 Add draggers for the radial gradient rg on item
1581 */
1582 void
1583 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1584 {
1585 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1586 guint num = rg->vector.stops.size();
1587 if (num > 2) {
1588 for ( guint i = 1; i < num - 1; i++ ) {
1589 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1590 }
1591 }
1592 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1593 if (num > 2) {
1594 for ( guint i = 1; i < num - 1; i++ ) {
1595 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1596 }
1597 }
1598 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1599 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1600 }
1602 /**
1603 Add draggers for the linear gradient lg on item
1604 */
1605 void
1606 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1607 {
1608 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1609 guint num = lg->vector.stops.size();
1610 if (num > 2) {
1611 for ( guint i = 1; i < num - 1; i++ ) {
1612 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1613 }
1614 }
1615 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1616 }
1618 /**
1619 Artificially grab the knot of this dragger; used by the gradient context
1620 */
1621 void
1622 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1623 {
1624 if (dragger) {
1625 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1626 }
1627 }
1629 /**
1630 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1631 */
1632 void
1633 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1634 {
1635 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1636 if (dragger) {
1637 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1638 }
1639 }
1641 /**
1642 Regenerates the draggers list from the current selection; is called when selection is changed or
1643 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1644 */
1645 void
1646 GrDrag::updateDraggers ()
1647 {
1648 while (selected) {
1649 selected = g_list_remove(selected, selected->data);
1650 }
1651 // delete old draggers
1652 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1653 delete ((GrDragger *) i->data);
1654 }
1655 g_list_free (this->draggers);
1656 this->draggers = NULL;
1658 g_return_if_fail (this->selection != NULL);
1660 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1662 SPItem *item = SP_ITEM(i->data);
1663 SPStyle *style = SP_OBJECT_STYLE (item);
1665 if (style && (style->fill.isPaintserver())) {
1666 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1667 if (SP_IS_LINEARGRADIENT (server)) {
1668 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1669 } else if (SP_IS_RADIALGRADIENT (server)) {
1670 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1671 }
1672 }
1674 if (style && (style->stroke.isPaintserver())) {
1675 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1676 if (SP_IS_LINEARGRADIENT (server)) {
1677 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1678 } else if (SP_IS_RADIALGRADIENT (server)) {
1679 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1680 }
1681 }
1682 }
1683 }
1685 /**
1686 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1687 lines are always in sync with the actual gradient
1688 */
1689 void
1690 GrDrag::updateLines ()
1691 {
1692 // delete old lines
1693 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1694 gtk_object_destroy( GTK_OBJECT (i->data));
1695 }
1696 g_slist_free (this->lines);
1697 this->lines = NULL;
1699 g_return_if_fail (this->selection != NULL);
1701 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1703 SPItem *item = SP_ITEM(i->data);
1705 SPStyle *style = SP_OBJECT_STYLE (item);
1707 if (style && (style->fill.isPaintserver())) {
1708 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1709 if (SP_IS_LINEARGRADIENT (server)) {
1710 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);
1711 } else if (SP_IS_RADIALGRADIENT (server)) {
1712 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1713 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1714 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1715 }
1716 }
1718 if (style && (style->stroke.isPaintserver())) {
1719 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1720 if (SP_IS_LINEARGRADIENT (server)) {
1721 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);
1722 } else if (SP_IS_RADIALGRADIENT (server)) {
1723 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1724 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1725 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1726 }
1727 }
1728 }
1729 }
1731 /**
1732 Regenerates the levels list from the current selection
1733 */
1734 void
1735 GrDrag::updateLevels ()
1736 {
1737 hor_levels.clear();
1738 vert_levels.clear();
1740 g_return_if_fail (this->selection != NULL);
1742 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1743 SPItem *item = SP_ITEM(i->data);
1744 Geom::OptRect rect = sp_item_bbox_desktop (item);
1745 if (rect) {
1746 // Remember the edges of the bbox and the center axis
1747 hor_levels.push_back(rect->min()[Geom::Y]);
1748 hor_levels.push_back(rect->max()[Geom::Y]);
1749 hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1750 vert_levels.push_back(rect->min()[Geom::X]);
1751 vert_levels.push_back(rect->max()[Geom::X]);
1752 vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1753 }
1754 }
1755 }
1757 void
1758 GrDrag::selected_reverse_vector ()
1759 {
1760 if (selected == NULL)
1761 return;
1763 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1764 GrDraggable *draggable = (GrDraggable *) i->data;
1766 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1767 }
1768 }
1770 void
1771 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1772 {
1773 selected_move (x, y, false, scale_radial);
1774 }
1776 void
1777 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1778 {
1779 if (selected == NULL)
1780 return;
1782 bool did = false;
1784 for (GList *i = selected; i != NULL; i = i->next) {
1785 GrDragger *d = (GrDragger *) i->data;
1787 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1788 // if this is an endpoint,
1790 // Moving an rg center moves its focus and radii as well.
1791 // therefore, if this is a focus or radius and if selection
1792 // contains the center as well, do not move this one
1793 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1794 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1795 bool skip_radius_with_center = false;
1796 for (GList *di = selected; di != NULL; di = di->next) {
1797 GrDragger *d_new = (GrDragger *) di->data;
1798 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1799 POINT_RG_CENTER,
1800 0,
1801 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1802 // FIXME: here we take into account only the first draggable!
1803 skip_radius_with_center = true;
1804 }
1805 }
1806 if (skip_radius_with_center)
1807 continue;
1808 }
1810 did = true;
1811 d->point += Geom::Point (x, y);
1812 d->point_original = d->point;
1813 sp_knot_moveto (d->knot, d->point);
1815 d->fireDraggables (write_repr, scale_radial);
1817 d->updateDependencies(write_repr);
1818 }
1819 }
1821 if (write_repr && did) {
1822 // we did an undoable action
1823 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1824 _("Move gradient handle(s)"));
1825 return;
1826 }
1828 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1830 GrDragger *dragger = (GrDragger *) selected->data;
1831 // a midpoint dragger can (logically) only contain one GrDraggable
1832 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1834 Geom::Point begin(0,0), end(0,0);
1835 Geom::Point low_lim(0,0), high_lim(0,0);
1837 SPObject *server = draggable->getServer();
1838 GSList *moving = NULL;
1839 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1841 Geom::Point p(x, y);
1842 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1843 Geom::Point displacement = p - dragger->point;
1845 for (GSList const* i = moving; i != NULL; i = i->next) {
1846 GrDragger *drg = (GrDragger*) i->data;
1847 SPKnot *drgknot = drg->knot;
1848 drg->point += displacement;
1849 sp_knot_moveto (drgknot, drg->point);
1850 drg->fireDraggables (true);
1851 drg->updateDependencies(true);
1852 did = true;
1853 }
1855 g_slist_free(moving);
1857 if (write_repr && did) {
1858 // we did an undoable action
1859 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1860 _("Move gradient mid stop(s)"));
1861 }
1862 }
1863 }
1865 void
1866 GrDrag::selected_move_screen (double x, double y)
1867 {
1868 gdouble zoom = desktop->current_zoom();
1869 gdouble zx = x / zoom;
1870 gdouble zy = y / zoom;
1872 selected_move (zx, zy);
1873 }
1875 /**
1876 Select the knot next to the last selected one and deselect all other selected.
1877 */
1878 GrDragger *
1879 GrDrag::select_next ()
1880 {
1881 GrDragger *d = NULL;
1882 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1883 if (draggers)
1884 d = (GrDragger *) draggers->data;
1885 } else {
1886 d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1887 }
1888 if (d)
1889 setSelected (d);
1890 return d;
1891 }
1893 /**
1894 Select the knot previous from the last selected one and deselect all other selected.
1895 */
1896 GrDragger *
1897 GrDrag::select_prev ()
1898 {
1899 GrDragger *d = NULL;
1900 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1901 if (draggers)
1902 d = (GrDragger *) g_list_last (draggers)->data;
1903 } else {
1904 d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1905 }
1906 if (d)
1907 setSelected (d);
1908 return d;
1909 }
1912 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1913 void
1914 GrDrag::deleteSelected (bool just_one)
1915 {
1916 if (!selected) return;
1918 SPDocument *document = false;
1920 struct StructStopInfo {
1921 SPStop * spstop;
1922 GrDraggable * draggable;
1923 SPGradient * gradient;
1924 SPGradient * vector;
1925 };
1927 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1928 GSList *endstoplist = NULL; // list of stops that must be deleted
1929 while (selected) {
1930 GrDragger *dragger = (GrDragger*) selected->data;
1931 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1932 GrDraggable *draggable = (GrDraggable*) drgble->data;
1933 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1934 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1936 switch (draggable->point_type) {
1937 case POINT_LG_MID:
1938 case POINT_RG_MID1:
1939 case POINT_RG_MID2:
1940 {
1941 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1942 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1943 bool present = false;
1944 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1945 if ( (SPStop*)l->data == stop ) {
1946 present = true;
1947 break; // no need to search further.
1948 }
1949 }
1950 if (!present)
1951 midstoplist = g_slist_append(midstoplist, stop);
1952 }
1953 break;
1954 case POINT_LG_BEGIN:
1955 case POINT_LG_END:
1956 case POINT_RG_CENTER:
1957 case POINT_RG_R1:
1958 case POINT_RG_R2:
1959 {
1960 SPStop *stop = NULL;
1961 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1962 stop = sp_first_stop(vector);
1963 } else {
1964 stop = sp_last_stop(vector);
1965 }
1966 if (stop) {
1967 StructStopInfo *stopinfo = new StructStopInfo;
1968 stopinfo->spstop = stop;
1969 stopinfo->draggable = draggable;
1970 stopinfo->gradient = gradient;
1971 stopinfo->vector = vector;
1972 // check if already present in list. (e.g. when both R1 and R2 were selected)
1973 bool present = false;
1974 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1975 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1976 present = true;
1977 break; // no need to search further.
1978 }
1979 }
1980 if (!present)
1981 endstoplist = g_slist_append(endstoplist, stopinfo);
1982 }
1983 }
1984 break;
1985 default:
1986 break;
1987 }
1988 }
1989 selected = g_list_remove(selected, dragger);
1990 if ( just_one ) break; // iterate once if just_one is set.
1991 }
1992 while (midstoplist) {
1993 SPStop *stop = (SPStop*) midstoplist->data;
1994 document = SP_OBJECT_DOCUMENT (stop);
1995 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1996 parent->removeChild(SP_OBJECT_REPR(stop));
1997 midstoplist = g_slist_remove(midstoplist, stop);
1998 }
1999 while (endstoplist) {
2000 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
2001 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2003 // 2 is the minimum, cannot delete more than that without deleting the whole vector
2004 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2005 // manually count the children, don't know if there already exists a function for this...
2006 int len = 0;
2007 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2008 child != NULL ;
2009 child = SP_OBJECT_NEXT(child) )
2010 {
2011 if ( SP_IS_STOP(child) ) len ++;
2012 }
2013 if (len > 2)
2014 {
2015 switch (stopinfo->draggable->point_type) {
2016 case POINT_LG_BEGIN:
2017 {
2018 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2020 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2021 Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2022 Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2023 SPStop *stop = sp_first_stop(stopinfo->vector);
2024 gdouble offset = stop->offset;
2025 Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2026 lg->x1.computed = newbegin[Geom::X];
2027 lg->y1.computed = newbegin[Geom::Y];
2029 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2030 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2031 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2032 stop->offset = 0;
2033 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2035 // iterate through midstops to set new offset values such that they won't move on canvas.
2036 SPStop *laststop = sp_last_stop(stopinfo->vector);
2037 stop = sp_next_stop(stop);
2038 while ( stop != laststop ) {
2039 stop->offset = (stop->offset - offset)/(1 - offset);
2040 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2041 stop = sp_next_stop(stop);
2042 }
2043 }
2044 break;
2045 case POINT_LG_END:
2046 {
2047 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2049 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2050 Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2051 Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2052 SPStop *laststop = sp_last_stop(stopinfo->vector);
2053 gdouble offset = laststop->offset;
2054 Geom::Point newend = begin + offset * (oldend - begin);
2055 lg->x2.computed = newend[Geom::X];
2056 lg->y2.computed = newend[Geom::Y];
2058 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2059 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2060 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2061 laststop->offset = 1;
2062 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2064 // iterate through midstops to set new offset values such that they won't move on canvas.
2065 SPStop *stop = sp_first_stop(stopinfo->vector);
2066 stop = sp_next_stop(stop);
2067 while ( stop != laststop ) {
2068 stop->offset = stop->offset / offset;
2069 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2070 stop = sp_next_stop(stop);
2071 }
2072 }
2073 break;
2074 case POINT_RG_CENTER:
2075 {
2076 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2077 if (newfirst) {
2078 newfirst->offset = 0;
2079 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2080 }
2081 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2082 }
2083 break;
2084 case POINT_RG_R1:
2085 case POINT_RG_R2:
2086 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2088 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2089 double oldradius = rg->r.computed;
2090 SPStop *laststop = sp_last_stop(stopinfo->vector);
2091 gdouble offset = laststop->offset;
2092 double newradius = offset * oldradius;
2093 rg->r.computed = newradius;
2095 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2096 sp_repr_set_svg_double(repr, "r", rg->r.computed);
2097 laststop->offset = 1;
2098 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2100 // iterate through midstops to set new offset values such that they won't move on canvas.
2101 SPStop *stop = sp_first_stop(stopinfo->vector);
2102 stop = sp_next_stop(stop);
2103 while ( stop != laststop ) {
2104 stop->offset = stop->offset / offset;
2105 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2106 stop = sp_next_stop(stop);
2107 }
2108 break;
2109 }
2110 }
2111 else
2112 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
2113 SPCSSAttr *css = sp_repr_css_attr_new ();
2115 // stopinfo->spstop is the selected stop
2116 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2117 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2118 unselectedrepr = unselectedrepr->next();
2119 }
2121 if (unselectedrepr == NULL) {
2122 if (stopinfo->draggable->fill_or_stroke) {
2123 sp_repr_css_unset_property (css, "fill");
2124 } else {
2125 sp_repr_css_unset_property (css, "stroke");
2126 }
2127 } else {
2128 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2129 if (stopinfo->draggable->fill_or_stroke) {
2130 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2131 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2132 } else {
2133 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2134 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2135 }
2136 sp_repr_css_attr_unref (stopcss);
2137 }
2139 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2140 sp_repr_css_attr_unref (css);
2141 }
2143 endstoplist = g_slist_remove(endstoplist, stopinfo);
2144 delete stopinfo;
2145 }
2147 if (document) {
2148 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2149 }
2150 }
2153 /*
2154 Local Variables:
2155 mode:c++
2156 c-file-style:"stroustrup"
2157 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2158 indent-tabs-mode:nil
2159 fill-column:99
2160 End:
2161 */
2162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :