Code

Suppress gradient direction line for 'solid' gradients.
[inkscape.git] / src / gradient-drag.cpp
1 /*
2  * On-canvas gradient dragging
3  *
4  * Authors:
5  *   bulia byak <buliabyak@users.sf.net>
6  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
7  *   Jon A. Cruz <jon@joncruz.org>
8  *
9  * Copyright (C) 2007 Johan Engelen
10  * Copyright (C) 2005,2010 Authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
19 #include <glibmm/i18n.h>
20 #include <cstring>
21 #include <string>
23 #include "desktop-handles.h"
24 #include "selection.h"
25 #include "desktop.h"
26 #include "desktop-style.h"
27 #include "document.h"
28 #include "display/sp-ctrlline.h"
29 #include "display/sp-canvas-util.h"
30 #include "xml/repr.h"
31 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
33 #include "libnr/nr-point-fns.h"
34 #include "preferences.h"
35 #include "sp-item.h"
36 #include "style.h"
37 #include "knot.h"
38 #include "sp-linear-gradient.h"
39 #include "sp-radial-gradient.h"
40 #include "gradient-chemistry.h"
41 #include "gradient-drag.h"
42 #include "sp-stop.h"
43 #include "snap.h"
44 #include "sp-namedview.h"
45 #include "selection-chemistry.h"
47 #define GR_KNOT_COLOR_NORMAL 0xffffff00
48 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
49 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
51 #define GR_LINE_COLOR_FILL 0x0000ff7f
52 #define GR_LINE_COLOR_STROKE 0x9999007f
54 // screen pixels between knots when they snap:
55 #define SNAP_DIST 5
57 // absolute distance between gradient points for them to become a single dragger when the drag is created:
58 #define MERGE_DIST 0.1
60 // knot shapes corresponding to GrPointType enum
61 SPKnotShapeType gr_knot_shapes [] = {
62         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
63         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
64         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
65         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
66         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
67         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
68         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
69         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
70         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
71 };
73 const gchar *gr_knot_descr [] = {
74     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
75     N_("Linear gradient <b>end</b>"),
76     N_("Linear gradient <b>mid stop</b>"),
77     N_("Radial gradient <b>center</b>"),
78     N_("Radial gradient <b>radius</b>"),
79     N_("Radial gradient <b>radius</b>"),
80     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
81     N_("Radial gradient <b>mid stop</b>"),
82     N_("Radial gradient <b>mid stop</b>")
83 };
85 static void
86 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
87 {
88     GrDrag *drag = (GrDrag *) data;
89     drag->updateDraggers ();
90     drag->updateLines ();
91     drag->updateLevels ();
92 }
94 static void
95 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
96 {
97     GrDrag *drag = (GrDrag *) data;
98     if (drag->local_change) {
99         drag->local_change = false;
100     } else {
101         drag->updateDraggers ();
102     }
103     drag->updateLines ();
104     drag->updateLevels ();
107 /**
108 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
109 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
110 any.
111 */
112 int
113 gr_drag_style_query (SPStyle *style, int property, gpointer data)
115     GrDrag *drag = (GrDrag *) data;
117     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
118         return QUERY_STYLE_NOTHING;
119     }
121     if (!drag->selected) {
122         return QUERY_STYLE_NOTHING;
123     } else {
124         int ret = QUERY_STYLE_NOTHING;
126         float cf[4];
127         cf[0] = cf[1] = cf[2] = cf[3] = 0;
129         int count = 0;
131         for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
132             GrDragger *d = (GrDragger *) i->data;
133             for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
134                 GrDraggable *draggable = (GrDraggable *) j->data;
136                 if (ret == QUERY_STYLE_NOTHING) {
137                     ret = QUERY_STYLE_SINGLE;
138                 } else if (ret == QUERY_STYLE_SINGLE) {
139                     ret = QUERY_STYLE_MULTIPLE_AVERAGED;
140                 }
142                 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
143                 cf[0] += SP_RGBA32_R_F (c);
144                 cf[1] += SP_RGBA32_G_F (c);
145                 cf[2] += SP_RGBA32_B_F (c);
146                 cf[3] += SP_RGBA32_A_F (c);
148                 count ++;
149             }
150         }
152         if (count) {
153             cf[0] /= count;
154             cf[1] /= count;
155             cf[2] /= count;
156             cf[3] /= count;
158             // set both fill and stroke with our stop-color and stop-opacity
159             style->fill.clear();
160             style->fill.setColor( cf[0], cf[1], cf[2] );
161             style->fill.set = TRUE;
162             style->stroke.clear();
163             style->stroke.setColor( cf[0], cf[1], cf[2] );
164             style->stroke.set = TRUE;
166             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
167             style->fill_opacity.set = TRUE;
168             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
169             style->stroke_opacity.set = TRUE;
171             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
172             style->opacity.set = TRUE;
173         }
175         return ret;
176     }
179 bool
180 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
182     GrDrag *drag = (GrDrag *) data;
184     if (!drag->selected)
185         return false;
187     SPCSSAttr *stop = sp_repr_css_attr_new ();
189     // See if the css contains interesting properties, and if so, translate them into the format
190     // acceptable for gradient stops
192     // any of color properties, in order of increasing priority:
193     if (css->attribute("flood-color"))
194         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
196     if (css->attribute("lighting-color"))
197         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
199     if (css->attribute("color"))
200         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
202     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
203         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
205     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
206         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
208     if (css->attribute("stop-color"))
209         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
212     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
213         sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
214     } else {  // multiply all opacity properties:
215         gdouble accumulated = 1.0;
216         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
217         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
218         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
219         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
221         Inkscape::CSSOStringStream os;
222         os << accumulated;
223         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
225         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
226             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
227             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
228     }
230     if (!stop->attributeList()) { // nothing for us here, pass it on
231         sp_repr_css_attr_unref(stop);
232         return false;
233     }
235     for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
236         GrDragger* dragger = (GrDragger*) sel->data;
237         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
238                GrDraggable *draggable = (GrDraggable *) i->data;
240                drag->local_change = true;
241                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
242         }
243     }
245     //sp_repr_css_print(stop);
246     sp_repr_css_attr_unref(stop);
247     return true;
250 guint32 GrDrag::getColor()
252     if (!selected) return 0;
254     float cf[4];
255     cf[0] = cf[1] = cf[2] = cf[3] = 0;
257     int count = 0;
259     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
260         GrDragger *d = (GrDragger *) i->data;
261         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
262             GrDraggable *draggable = (GrDraggable *) j->data;
264             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
265             cf[0] += SP_RGBA32_R_F (c);
266             cf[1] += SP_RGBA32_G_F (c);
267             cf[2] += SP_RGBA32_B_F (c);
268             cf[3] += SP_RGBA32_A_F (c);
270             count ++;
271         }
272     }
274     if (count) {
275         cf[0] /= count;
276         cf[1] /= count;
277         cf[2] /= count;
278         cf[3] /= count;
279     }
281     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
284 SPStop *
285 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
287     gfloat offset; // type of SPStop.offset = gfloat
288     SPGradient *gradient;
289     bool fill_or_stroke = true;
290     bool r1_knot = false;
292     bool addknot = false;
293     do {
294         gradient = sp_item_gradient (item, fill_or_stroke);
295         if (SP_IS_LINEARGRADIENT(gradient)) {
296             Geom::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
297             Geom::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
299             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
300             double dist_screen = Geom::L2 (mouse_p - nearest);
301             if ( dist_screen < tolerance ) {
302                 // add the knot
303                 offset = get_offset_between_points(nearest, begin, end);
304                 addknot = true;
305                 break; // break out of the while loop: add only one knot
306             }
307         } else if (SP_IS_RADIALGRADIENT(gradient)) {
308             Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
309             Geom::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
310             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
311             double dist_screen = Geom::L2 (mouse_p - nearest);
312             if ( dist_screen < tolerance ) {
313                 offset = get_offset_between_points(nearest, begin, end);
314                 addknot = true;
315                 r1_knot = true;
316                 break; // break out of the while loop: add only one knot
317             }
319             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
320             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
321             dist_screen = Geom::L2 (mouse_p - nearest);
322             if ( dist_screen < tolerance ) {
323                 offset = get_offset_between_points(nearest, begin, end);
324                 addknot = true;
325                 r1_knot = false;
326                 break; // break out of the while loop: add only one knot
327             }
328         }
329         fill_or_stroke = !fill_or_stroke;
330     } while (!fill_or_stroke && !addknot) ;
332     if (addknot) {
333         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
334         SPStop* prev_stop = vector->getFirstStop();
335         SPStop* next_stop = prev_stop->getNextStop();
336         guint i = 1;
337         while ( (next_stop) && (next_stop->offset < offset) ) {
338             prev_stop = next_stop;
339             next_stop = next_stop->getNextStop();
340             i++;
341         }
342         if (!next_stop) {
343             // logical error: the endstop should have offset 1 and should always be more than this offset here
344             return NULL;
345         }
348         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
349         gradient->ensureVector();
350         updateDraggers();
352         return newstop;
353     }
355     return NULL;
359 bool
360 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
362     // first, see if we can drop onto one of the existing draggers
363     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
364         GrDragger *d = (GrDragger *) i->data;
366         if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
367            SPCSSAttr *stop = sp_repr_css_attr_new ();
368            sp_repr_css_set_property (stop, "stop-color", c);
369            sp_repr_css_set_property (stop, "stop-opacity", "1");
370            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
371                GrDraggable *draggable = (GrDraggable *) j->data;
372                local_change = true;
373                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
374            }
375            sp_repr_css_attr_unref(stop);
376            return true;
377         }
378     }
380     // now see if we're over line and create a new stop
381     bool over_line = false;
382     SPCtrlLine *line = NULL;
383     if (lines) {
384         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
385             line = (SPCtrlLine*) l->data;
386             Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
387             double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
388             if (line->item && dist_screen < 5) {
389                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
390                 if (stop) {
391                     SPCSSAttr *css = sp_repr_css_attr_new ();
392                     sp_repr_css_set_property (css, "stop-color", c);
393                     sp_repr_css_set_property (css, "stop-opacity", "1");
394                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
395                     return true;
396                 }
397             }
398         }
399     }
401     return false;
405 GrDrag::GrDrag(SPDesktop *desktop) {
407     this->desktop = desktop;
409     this->selection = sp_desktop_selection(desktop);
411     this->draggers = NULL;
412     this->lines = NULL;
413     this->selected = NULL;
415     this->hor_levels.clear();
416     this->vert_levels.clear();
418     this->local_change = false;
420     this->sel_changed_connection = this->selection->connectChanged(
421         sigc::bind (
422             sigc::ptr_fun(&gr_drag_sel_changed),
423             (gpointer)this )
425         );
426     this->sel_modified_connection = this->selection->connectModified(
427         sigc::bind(
428             sigc::ptr_fun(&gr_drag_sel_modified),
429             (gpointer)this )
430         );
432     this->style_set_connection = this->desktop->connectSetStyle(
433         sigc::bind(
434             sigc::ptr_fun(&gr_drag_style_set),
435             (gpointer)this )
436         );
438     this->style_query_connection = this->desktop->connectQueryStyle(
439         sigc::bind(
440             sigc::ptr_fun(&gr_drag_style_query),
441             (gpointer)this )
442         );
444     this->updateDraggers ();
445     this->updateLines ();
446     this->updateLevels ();
448     if (desktop->gr_item) {
449         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
450     }
453 GrDrag::~GrDrag()
455     this->sel_changed_connection.disconnect();
456     this->sel_modified_connection.disconnect();
457     this->style_set_connection.disconnect();
458     this->style_query_connection.disconnect();
460     if (this->selected) {
461         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
462         desktop->gr_item = draggable->item;
463         desktop->gr_point_type = draggable->point_type;
464         desktop->gr_point_i = draggable->point_i;
465         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
466     } else {
467         desktop->gr_item = NULL;
468         desktop->gr_point_type = 0;
469         desktop->gr_point_i = 0;
470         desktop->gr_fill_or_stroke = true;
471     }
473     deselect_all();
474     for (GList *l = this->draggers; l != NULL; l = l->next) {
475         delete ((GrDragger *) l->data);
476     }
477     g_list_free (this->draggers);
478     this->draggers = NULL;
479     this->selected = NULL;
481     for (GSList *l = this->lines; l != NULL; l = l->next) {
482         gtk_object_destroy( GTK_OBJECT (l->data));
483     }
484     g_slist_free (this->lines);
485     this->lines = NULL;
488 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
490     this->item = item;
491     this->point_type = point_type;
492     this->point_i = point_i;
493     this->fill_or_stroke = fill_or_stroke;
495     g_object_ref (G_OBJECT (this->item));
498 GrDraggable::~GrDraggable ()
500     g_object_unref (G_OBJECT (this->item));
504 SPObject *GrDraggable::getServer()
506     if (!item) {
507         return NULL;
508     }
510     SPObject *server = NULL;
511     if (fill_or_stroke) {
512         server = item->style->getFillPaintServer();
513     }else {
514         server = item->style->getStrokePaintServer();
515     }
517     return server;
520 static
521 boost::optional<Geom::Point>
522 get_snap_vector (Geom::Point p, Geom::Point o, double snap, double initial)
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);
536 static void
537 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
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, 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     }
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)
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);
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)
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             Inkscape::Snapper::ConstraintLine cl(low_lim, high_lim - low_lim);
812             SPDesktop *desktop = dragger->parent->desktop;
813             SnapManager &m = desktop->namedview->snap_manager;
814             m.setup(desktop);
815             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
816         }
817     }
818     Geom::Point displacement = p - dragger->point;
820     for (GSList const* i = moving; i != NULL; i = i->next) {
821         GrDragger *drg = (GrDragger*) i->data;
822         SPKnot *drgknot = drg->knot;
823         Geom::Point this_move = displacement;
824         if (state & GDK_MOD1_MASK) {
825             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
826             double alpha = 1.0;
827             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
828                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
829                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
830             } else { // drg is on the begin side from dragger
831                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
832                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
833             }
834         }
835         drg->point += this_move;
836         sp_knot_moveto (drgknot, drg->point);
837         drg->fireDraggables (false);
838         drg->updateDependencies(false);
839     }
841     g_slist_free(moving);
843     drag->keep_selection = dragger->isSelected();
848 static void
849 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
851     GrDragger *dragger = (GrDragger *) data;
853     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
856 /**
857 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
858 */
859 static void
860 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
862     GrDragger *dragger = (GrDragger *) data;
864     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
866     dragger->point_original = dragger->point = knot->pos;
868     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
869         dragger->fireDraggables (true, true);
870     } else {
871         dragger->fireDraggables (true);
872     }
874     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
875         GrDragger *d = (GrDragger *) i->data;
876         if (d == dragger)
877             continue;
878         d->fireDraggables (true);
879     }
881     // make this dragger selected
882     if (!dragger->parent->keep_selection) {
883         dragger->parent->setSelected (dragger);
884     }
885     dragger->parent->keep_selection = false;
887     dragger->updateDependencies(true);
889     // we did an undoable action
890     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
891                       _("Move gradient handle"));
894 /**
895 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
896 state of the keyboard keys
897 */
898 static void
899 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
901     GrDragger *dragger = (GrDragger *) data;
902     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
903     if (!draggable) return;
905     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
906     // delete this knot from vector
907         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
908         gradient = gradient->getVector();
909         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
910             SPStop *stop = NULL;
911             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
912             case POINT_LG_BEGIN:
913             case POINT_RG_CENTER:
914                 stop = gradient->getFirstStop();
915                 {
916                     SPStop *next = stop->getNextStop();
917                     if (next) {
918                         next->offset = 0;
919                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
920                     }
921                 }
922                 break;
923             case POINT_LG_END:
924             case POINT_RG_R1:
925             case POINT_RG_R2:
926                 stop = sp_last_stop(gradient);
927                 {
928                     SPStop *prev = stop->getPrevStop();
929                     if (prev) {
930                         prev->offset = 1;
931                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
932                     }
933                 }
934                 break;
935             case POINT_LG_MID:
936             case POINT_RG_MID1:
937             case POINT_RG_MID2:
938                 stop = sp_get_stop_i(gradient, draggable->point_i);
939                 break;
940             }
942             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
943             sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
944                       _("Delete gradient stop"));
945         }
946     } else {
947     // select the dragger
948         dragger->point_original = dragger->point;
950         if ( state & GDK_SHIFT_MASK ) {
951             dragger->parent->setSelected (dragger, true, false);
952         } else {
953             dragger->parent->setSelected (dragger);
954         }
955     }
958 /**
959 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
960 */
961 static void
962 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
964     GrDragger *dragger = (GrDragger *) data;
966     dragger->point_original = dragger->point;
968     if (dragger->draggables == NULL)
969         return;
971     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
972     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
975 /**
976 Act upon all draggables of the dragger, setting them to the dragger's point
977 */
978 void
979 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
981     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
982         GrDraggable *draggable = (GrDraggable *) i->data;
984         // set local_change flag so that selection_changed callback does not regenerate draggers
985         this->parent->local_change = true;
987         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
988         // to the center, unless it's the first update upon merge when we must snap it to the point
989         if (merging_focus ||
990             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
991         {
992             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
993         }
994     }
997 /**
998 Checks if the dragger has a draggable with this point_type
999  */
1000 bool
1001 GrDragger::isA (gint point_type)
1003     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1004         GrDraggable *draggable = (GrDraggable *) i->data;
1005         if (draggable->point_type == point_type) {
1006             return true;
1007         }
1008     }
1009     return false;
1012 /**
1013 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1014  */
1015 bool
1016 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1018     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1019         GrDraggable *draggable = (GrDraggable *) i->data;
1020         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1021             return true;
1022         }
1023     }
1024     return false;
1027 /**
1028 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1029  */
1030 bool
1031 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1033     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1034         GrDraggable *draggable = (GrDraggable *) i->data;
1035         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1036             return true;
1037         }
1038     }
1039     return false;
1042 bool
1043 GrDraggable::mayMerge (GrDraggable *da2)
1045     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1046         // we must not merge the points of the same gradient!
1047         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1048               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1049             // except that we can snap center and focus together
1050             return false;
1051         }
1052     }
1053     // disable merging of midpoints.
1054     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1055          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1056          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1057         return false;
1059     return true;
1062 bool
1063 GrDragger::mayMerge (GrDragger *other)
1065     if (this == other)
1066         return false;
1068     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1069         GrDraggable *da1 = (GrDraggable *) i->data;
1070         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1071             GrDraggable *da2 = (GrDraggable *) j->data;
1072             if (!da1->mayMerge(da2))
1073                 return false;
1074         }
1075     }
1076     return true;
1079 bool
1080 GrDragger::mayMerge (GrDraggable *da2)
1082     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1083         GrDraggable *da1 = (GrDraggable *) i->data;
1084         if (!da1->mayMerge(da2))
1085             return false;
1086     }
1087     return true;
1090 /**
1091 Updates the statusbar tip of the dragger knot, based on its draggables
1092  */
1093 void
1094 GrDragger::updateTip ()
1096     if (this->knot && this->knot->tip) {
1097         g_free (this->knot->tip);
1098         this->knot->tip = NULL;
1099     }
1101     if (g_slist_length (this->draggables) == 1) {
1102         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1103         char *item_desc = sp_item_description(draggable->item);
1104         switch (draggable->point_type) {
1105             case POINT_LG_MID:
1106             case POINT_RG_MID1:
1107             case POINT_RG_MID2:
1108                 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"),
1109                                                    _(gr_knot_descr[draggable->point_type]),
1110                                                    draggable->point_i,
1111                                                    item_desc,
1112                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1113                 break;
1115             default:
1116                 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"),
1117                                                    _(gr_knot_descr[draggable->point_type]),
1118                                                    item_desc,
1119                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1120                 break;
1121         }
1122         g_free(item_desc);
1123     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1124         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1125     } else {
1126         int length = g_slist_length (this->draggables);
1127         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1128                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1129                                                     length),
1130                                            length);
1131     }
1134 /**
1135 Adds a draggable to the dragger
1136  */
1137 void
1138 GrDragger::updateKnotShape ()
1140     if (!draggables)
1141         return;
1142     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1143     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1146 /**
1147 Adds a draggable to the dragger
1148  */
1149 void
1150 GrDragger::addDraggable (GrDraggable *draggable)
1152     this->draggables = g_slist_prepend (this->draggables, draggable);
1154     this->updateTip();
1158 /**
1159 Moves this dragger to the point of the given draggable, acting upon all other draggables
1160  */
1161 void
1162 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1164     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1165     if (!dr_first) return;
1167     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1168     this->point_original = this->point;
1170     sp_knot_moveto (this->knot, this->point);
1172     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1173         GrDraggable *da = (GrDraggable *) i->data;
1174         if ( (da->item == item) &&
1175              (point_type == -1 || da->point_type == point_type) &&
1176              (point_i == -1 || da->point_i == point_i) &&
1177              (da->fill_or_stroke == fill_or_stroke) ) {
1178             continue;
1179         }
1180         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1181     }
1182     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1186 /**
1187 Moves all midstop draggables that depend on this one
1188  */
1189 void
1190 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1191     SPObject *server = draggable->getServer();
1192     if (!server)
1193         return;
1194     guint num = SP_GRADIENT(server)->vector.stops.size();
1195     if (num <= 2) return;
1197     if ( SP_IS_LINEARGRADIENT(server) ) {
1198         for ( guint i = 1; i < num - 1; i++ ) {
1199             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1200         }
1201     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1202         for ( guint i = 1; i < num - 1; i++ ) {
1203             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1204             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1205         }
1206     }
1210 /**
1211 Moves all draggables that depend on this one
1212  */
1213 void
1214 GrDragger::updateDependencies (bool write_repr)
1216     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1217         GrDraggable *draggable = (GrDraggable *) i->data;
1218         switch (draggable->point_type) {
1219             case POINT_LG_BEGIN:
1220                 {
1221                     // the end point is dependent only when dragging with ctrl+shift
1222                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1224                     this->updateMidstopDependencies (draggable, write_repr);
1225                 }
1226                 break;
1227             case POINT_LG_END:
1228                 {
1229                     // the begin point is dependent only when dragging with ctrl+shift
1230                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1232                     this->updateMidstopDependencies (draggable, write_repr);
1233                 }
1234                 break;
1235             case POINT_LG_MID:
1236                 // no other nodes depend on mid points.
1237                 break;
1238             case POINT_RG_R2:
1239                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1240                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1241                 this->updateMidstopDependencies (draggable, write_repr);
1242                 break;
1243             case POINT_RG_R1:
1244                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1245                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1246                 this->updateMidstopDependencies (draggable, write_repr);
1247                 break;
1248             case POINT_RG_CENTER:
1249                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1250                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1251                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1252                 this->updateMidstopDependencies (draggable, write_repr);
1253                 break;
1254             case POINT_RG_FOCUS:
1255                 // nothing can depend on that
1256                 break;
1257             case POINT_RG_MID1:
1258                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1259                 break;
1260             case POINT_RG_MID2:
1261                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1262                 break;
1263             default:
1264                 break;
1265         }
1266     }
1271 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1272   : point(p),
1273     point_original(p)
1275     this->draggables = NULL;
1277     this->parent = parent;
1279     // create the knot
1280     this->knot = sp_knot_new (parent->desktop, NULL);
1281     this->knot->setMode(SP_KNOT_MODE_XOR);
1282     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1283     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1284     sp_knot_update_ctrl(this->knot);
1286     // move knot to the given point
1287     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1288     sp_knot_show (this->knot);
1290     // connect knot's signals
1291     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1292                        // luckily, midstops never snap to other nodes so are never unsnapped...
1293          && ( (draggable->point_type == POINT_LG_MID)
1294               || (draggable->point_type == POINT_RG_MID1)
1295               || (draggable->point_type == POINT_RG_MID2) ) )
1296     {
1297         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1298     } else {
1299         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1300     }
1301     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1302     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1303     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1304     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1306     // add the initial draggable
1307     if (draggable)
1308         this->addDraggable (draggable);
1309     updateKnotShape();
1312 GrDragger::~GrDragger ()
1314     // unselect if it was selected
1315     this->parent->setDeselected(this);
1317     // disconnect signals
1318     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1319     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1320     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1321     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1322     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1324     /* unref should call destroy */
1325     g_object_unref (G_OBJECT (this->knot));
1327     // delete all draggables
1328     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1329         delete ((GrDraggable *) i->data);
1330     }
1331     g_slist_free (this->draggables);
1332     this->draggables = NULL;
1335 /**
1336 Select the dragger which has the given draggable.
1337 */
1338 GrDragger *
1339 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1341     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1342         GrDragger *dragger = (GrDragger *) i->data;
1343         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1344             GrDraggable *da2 = (GrDraggable *) j->data;
1345             if ( (da2->item == item) &&
1346                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1347                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1348                  (da2->fill_or_stroke == fill_or_stroke)) {
1349                 return (dragger);
1350             }
1351         }
1352     }
1353     return NULL;
1357 void
1358 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1360     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1361     if (d && d !=  this) {
1362         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1363     }
1367 /**
1368   Draw this dragger as selected
1369 */
1370 void
1371 GrDragger::select()
1373     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1374     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1377 /**
1378   Draw this dragger as normal (deselected)
1379 */
1380 void
1381 GrDragger::deselect()
1383     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1384     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1387 bool
1388 GrDragger::isSelected()
1390     return g_list_find (parent->selected, this);
1393 /**
1394 \brief Deselect all stops/draggers (private)
1395 */
1396 void
1397 GrDrag::deselect_all()
1399     while (selected) {
1400         ( (GrDragger*) selected->data)->deselect();
1401         selected = g_list_remove(selected, selected->data);
1402     }
1405 /**
1406 \brief Deselect all stops/draggers (public; emits signal)
1407 */
1408 void
1409 GrDrag::deselectAll()
1411     deselect_all();
1412     this->desktop->emitToolSubselectionChanged(NULL);
1415 /**
1416 \brief Select all stops/draggers
1417 */
1418 void
1419 GrDrag::selectAll()
1421     for (GList *l = this->draggers; l != NULL; l = l->next) {
1422         GrDragger *d = ((GrDragger *) l->data);
1423         setSelected (d, true, true);
1424     }
1427 /**
1428 \brief Select all stops/draggers that match the coords
1429 */
1430 void
1431 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1433     for (GList *l = this->draggers; l != NULL; l = l->next) {
1434         GrDragger *d = ((GrDragger *) l->data);
1435         for (guint k = 0; k < coords.size(); k++) {
1436             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1437                 setSelected (d, true, true);
1438             }
1439         }
1440     }
1444 /**
1445 \brief Select all stops/draggers that fall within the rect
1446 */
1447 void
1448 GrDrag::selectRect(Geom::Rect const &r)
1450     for (GList *l = this->draggers; l != NULL; l = l->next) {
1451         GrDragger *d = ((GrDragger *) l->data);
1452         if (r.contains(d->point)) {
1453            setSelected (d, true, true);
1454         }
1455     }
1458 /**
1459 \brief Select a dragger
1460 \param dragger       The dragger to select
1461 \param add_to_selection   If true, add to selection, otherwise deselect others
1462 \param override      If true, always select this node, otherwise toggle selected status
1463 */
1464 void
1465 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1467     GrDragger *seldragger = NULL;
1469     if (add_to_selection) {
1470         if (!dragger) return;
1471         if (override) {
1472             if (!g_list_find(selected, dragger)) {
1473                 selected = g_list_prepend(selected, dragger);
1474             }
1475             dragger->select();
1476             seldragger = dragger;
1477         } else { // toggle
1478             if (g_list_find(selected, dragger)) {
1479                 selected = g_list_remove(selected, dragger);
1480                 dragger->deselect();
1481                 if (selected) {
1482                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1483                 }
1484             } else {
1485                 selected = g_list_prepend(selected, dragger);
1486                 dragger->select();
1487                 seldragger = dragger;
1488             }
1489         }
1490     } else {
1491         deselect_all();
1492         if (dragger) {
1493             selected = g_list_prepend(selected, dragger);
1494             dragger->select();
1495             seldragger = dragger;
1496         }
1497     }
1498     if (seldragger) {
1499         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1500     }
1503 /**
1504 \brief Deselect a dragger
1505 \param dragger       The dragger to deselect
1506 */
1507 void
1508 GrDrag::setDeselected (GrDragger *dragger)
1510     if (g_list_find(selected, dragger)) {
1511         selected = g_list_remove(selected, dragger);
1512         dragger->deselect();
1513     }
1514     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1519 /**
1520 Create a line from p1 to p2 and add it to the lines list
1521  */
1522 void
1523 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1525     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1526                                                             SP_TYPE_CTRLLINE, NULL);
1527     sp_canvas_item_move_to_z(line, 0);
1528     SP_CTRLLINE(line)->item = item;
1529     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1530     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1531         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1532     sp_canvas_item_show (line);
1533     this->lines = g_slist_append (this->lines, line);
1536 /**
1537 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1538 new dragger and add it to draggers list
1539  */
1540 void
1541 GrDrag::addDragger (GrDraggable *draggable)
1543     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1545     for (GList *i = this->draggers; i != NULL; i = i->next) {
1546         GrDragger *dragger = (GrDragger *) i->data;
1547         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1548             // distance is small, merge this draggable into dragger, no need to create new dragger
1549             dragger->addDraggable (draggable);
1550             dragger->updateKnotShape();
1551             return;
1552         }
1553     }
1555     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1556     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1557     this->draggers = g_list_append (this->draggers, new_dragger);
1560 /**
1561 Add draggers for the radial gradient rg on item
1562 */
1563 void
1564 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1566     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1567     guint num = rg->vector.stops.size();
1568     if (num > 2) {
1569         for ( guint i = 1; i < num - 1; i++ ) {
1570             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1571         }
1572     }
1573     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1574     if (num > 2) {
1575         for ( guint i = 1; i < num - 1; i++ ) {
1576             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1577         }
1578     }
1579     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1580     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1583 /**
1584 Add draggers for the linear gradient lg on item
1585 */
1586 void
1587 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1589     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1590     guint num = lg->vector.stops.size();
1591     if (num > 2) {
1592         for ( guint i = 1; i < num - 1; i++ ) {
1593             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1594         }
1595     }
1596     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1599 /**
1600 Artificially grab the knot of this dragger; used by the gradient context
1601 */
1602 void
1603 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1605     if (dragger) {
1606         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1607     }
1610 /**
1611 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1612 */
1613 void
1614 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1616     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1617     if (dragger) {
1618         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1619     }
1622 /**
1623 Regenerates the draggers list from the current selection; is called when selection is changed or
1624 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1625 */
1626 void GrDrag::updateDraggers ()
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 static_cast<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) {
1641         SPItem *item = SP_ITEM(i->data);
1642         SPStyle *style = item->style;
1644         if (style && (style->fill.isPaintserver())) {
1645             SPPaintServer *server = style->getFillPaintServer();
1646             if ( server && server->isSolid() ) {
1647                 // Suppress "gradientness" of solid paint
1648             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1649                 addDraggersLinear( SP_LINEARGRADIENT(server), item, true );
1650             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1651                 addDraggersRadial( SP_RADIALGRADIENT(server), item, true );
1652             }
1653         }
1655         if (style && (style->stroke.isPaintserver())) {
1656             SPPaintServer *server = style->getStrokePaintServer();
1657             if ( server && server->isSolid() ) {
1658                 // Suppress "gradientness" of solid paint
1659             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1660                 addDraggersLinear( SP_LINEARGRADIENT(server), item, false );
1661             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1662                 addDraggersRadial( SP_RADIALGRADIENT(server), item, false );
1663             }
1664         }
1665     }
1669 /**
1670  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1671  */
1673 bool
1674 GrDrag::mouseOver()
1676     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1677         GrDragger *d = (GrDragger *) i->data;
1678         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1679             return true;
1680         }
1681     }
1682     return false;
1684 /**
1685 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1686 lines are always in sync with the actual gradient
1687 */
1688 void
1689 GrDrag::updateLines ()
1691     // delete old lines
1692     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1693         gtk_object_destroy( GTK_OBJECT (i->data));
1694     }
1695     g_slist_free (this->lines);
1696     this->lines = NULL;
1698     g_return_if_fail (this->selection != NULL);
1700     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1702         SPItem *item = SP_ITEM(i->data);
1704         SPStyle *style = SP_OBJECT_STYLE (item);
1706         if (style && (style->fill.isPaintserver())) {
1707             SPPaintServer *server = item->style->getFillPaintServer();
1708             if ( server && server->isSolid() ) {
1709                 // Suppress "gradientness" of solid paint
1710             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1711                 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);
1712             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1713                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1714                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1715                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1716             }
1717         }
1719         if (style && (style->stroke.isPaintserver())) {
1720             SPPaintServer *server = item->style->getStrokePaintServer();
1721             if ( server && server->isSolid() ) {
1722                 // Suppress "gradientness" of solid paint
1723             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1724                 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);
1725             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1726                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1727                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1728                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1729             }
1730         }
1731     }
1734 /**
1735 Regenerates the levels list from the current selection
1736 */
1737 void
1738 GrDrag::updateLevels ()
1740     hor_levels.clear();
1741     vert_levels.clear();
1743     g_return_if_fail (this->selection != NULL);
1745     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1746         SPItem *item = SP_ITEM(i->data);
1747         Geom::OptRect rect = sp_item_bbox_desktop (item);
1748         if (rect) {
1749             // Remember the edges of the bbox and the center axis
1750             hor_levels.push_back(rect->min()[Geom::Y]);
1751             hor_levels.push_back(rect->max()[Geom::Y]);
1752             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1753             vert_levels.push_back(rect->min()[Geom::X]);
1754             vert_levels.push_back(rect->max()[Geom::X]);
1755             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1756         }
1757     }
1760 void
1761 GrDrag::selected_reverse_vector ()
1763     if (selected == NULL)
1764         return;
1766     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1767         GrDraggable *draggable = (GrDraggable *) i->data;
1769         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1770     }
1773 void
1774 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1776     selected_move (x, y, false, scale_radial);
1779 void
1780 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1782     if (selected == NULL)
1783         return;
1785     bool did = false;
1787     for (GList *i = selected; i != NULL; i = i->next) {
1788         GrDragger *d = (GrDragger *) i->data;
1790         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1791             // if this is an endpoint,
1793             // Moving an rg center moves its focus and radii as well.
1794             // therefore, if this is a focus or radius and if selection
1795             // contains the center as well, do not move this one
1796             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1797                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1798                 bool skip_radius_with_center = false;
1799                 for (GList *di = selected; di != NULL; di = di->next) {
1800                     GrDragger *d_new = (GrDragger *) di->data;
1801                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1802                                     POINT_RG_CENTER,
1803                                     0,
1804                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1805                         // FIXME: here we take into account only the first draggable!
1806                         skip_radius_with_center = true;
1807                     }
1808                 }
1809                 if (skip_radius_with_center)
1810                     continue;
1811             }
1813             did = true;
1814             d->point += Geom::Point (x, y);
1815             d->point_original = d->point;
1816             sp_knot_moveto (d->knot, d->point);
1818             d->fireDraggables (write_repr, scale_radial);
1820             d->updateDependencies(write_repr);
1821         }
1822     }
1824     if (write_repr && did) {
1825         // we did an undoable action
1826         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1827                                 _("Move gradient handle(s)"));
1828         return;
1829     }
1831     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1833         GrDragger *dragger = (GrDragger *) selected->data;
1834         // a midpoint dragger can (logically) only contain one GrDraggable
1835         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1837         Geom::Point begin(0,0), end(0,0);
1838         Geom::Point low_lim(0,0), high_lim(0,0);
1840         SPObject *server = draggable->getServer();
1841         GSList *moving = NULL;
1842         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1844         Geom::Point p(x, y);
1845         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1846         Geom::Point displacement = p - dragger->point;
1848         for (GSList const* i = moving; i != NULL; i = i->next) {
1849             GrDragger *drg = (GrDragger*) i->data;
1850             SPKnot *drgknot = drg->knot;
1851             drg->point += displacement;
1852             sp_knot_moveto (drgknot, drg->point);
1853             drg->fireDraggables (true);
1854             drg->updateDependencies(true);
1855             did = true;
1856         }
1858         g_slist_free(moving);
1860         if (write_repr && did) {
1861             // we did an undoable action
1862             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1863                                     _("Move gradient mid stop(s)"));
1864         }
1865     }
1868 void
1869 GrDrag::selected_move_screen (double x, double y)
1871     gdouble zoom = desktop->current_zoom();
1872     gdouble zx = x / zoom;
1873     gdouble zy = y / zoom;
1875     selected_move (zx, zy);
1878 /**
1879 Select the knot next to the last selected one and deselect all other selected.
1880 */
1881 GrDragger *
1882 GrDrag::select_next ()
1884     GrDragger *d = NULL;
1885     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1886         if (draggers)
1887             d = (GrDragger *) draggers->data;
1888     } else {
1889         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1890     }
1891     if (d)
1892         setSelected (d);
1893     return d;
1896 /**
1897 Select the knot previous from the last selected one and deselect all other selected.
1898 */
1899 GrDragger *
1900 GrDrag::select_prev ()
1902     GrDragger *d = NULL;
1903     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1904         if (draggers)
1905             d = (GrDragger *) g_list_last (draggers)->data;
1906     } else {
1907         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1908     }
1909     if (d)
1910         setSelected (d);
1911     return d;
1915 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1916 void
1917 GrDrag::deleteSelected (bool just_one)
1919     if (!selected) return;
1921     SPDocument *document = false;
1923     struct StructStopInfo {
1924         SPStop * spstop;
1925         GrDraggable * draggable;
1926         SPGradient * gradient;
1927         SPGradient * vector;
1928     };
1930     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1931     GSList *endstoplist = NULL;  // list of stops that must be deleted
1932     while (selected) {
1933         GrDragger *dragger = (GrDragger*) selected->data;
1934         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1935             GrDraggable *draggable = (GrDraggable*) drgble->data;
1936             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1937             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1939             switch (draggable->point_type) {
1940                 case POINT_LG_MID:
1941                 case POINT_RG_MID1:
1942                 case POINT_RG_MID2:
1943                     {
1944                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1945                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1946                         bool present = false;
1947                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1948                             if ( (SPStop*)l->data == stop ) {
1949                                 present = true;
1950                                 break; // no need to search further.
1951                             }
1952                         }
1953                         if (!present)
1954                             midstoplist = g_slist_append(midstoplist, stop);
1955                     }
1956                     break;
1957                 case POINT_LG_BEGIN:
1958                 case POINT_LG_END:
1959                 case POINT_RG_CENTER:
1960                 case POINT_RG_R1:
1961                 case POINT_RG_R2:
1962                     {
1963                         SPStop *stop = NULL;
1964                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1965                             stop = vector->getFirstStop();
1966                         } else {
1967                             stop = sp_last_stop(vector);
1968                         }
1969                         if (stop) {
1970                             StructStopInfo *stopinfo = new StructStopInfo;
1971                             stopinfo->spstop = stop;
1972                             stopinfo->draggable = draggable;
1973                             stopinfo->gradient = gradient;
1974                             stopinfo->vector = vector;
1975                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1976                             bool present = false;
1977                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1978                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1979                                     present = true;
1980                                     break; // no need to search further.
1981                                 }
1982                             }
1983                             if (!present)
1984                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1985                         }
1986                     }
1987                     break;
1988                 default:
1989                     break;
1990             }
1991         }
1992         selected = g_list_remove(selected, dragger);
1993         if ( just_one ) break; // iterate once if just_one is set.
1994     }
1995     while (midstoplist) {
1996         SPStop *stop = (SPStop*) midstoplist->data;
1997         document = SP_OBJECT_DOCUMENT (stop);
1998         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1999         parent->removeChild(SP_OBJECT_REPR(stop));
2000         midstoplist = g_slist_remove(midstoplist, stop);
2001     }
2002     while (endstoplist) {
2003         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
2004         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2006         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2007         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2008         // manually count the children, don't know if there already exists a function for this...
2009         int len = 0;
2010         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2011               child != NULL ;
2012               child = SP_OBJECT_NEXT(child) )
2013         {
2014             if ( SP_IS_STOP(child) )  len ++;
2015         }
2016         if (len > 2)
2017         {
2018             switch (stopinfo->draggable->point_type) {
2019                 case POINT_LG_BEGIN:
2020                     {
2021                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2023                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2024                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2025                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2026                         SPStop *stop = stopinfo->vector->getFirstStop();
2027                         gdouble offset = stop->offset;
2028                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2029                         lg->x1.computed = newbegin[Geom::X];
2030                         lg->y1.computed = newbegin[Geom::Y];
2032                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2033                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2034                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2035                         stop->offset = 0;
2036                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2038                         // iterate through midstops to set new offset values such that they won't move on canvas.
2039                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2040                         stop = stop->getNextStop();
2041                         while ( stop != laststop ) {
2042                             stop->offset = (stop->offset - offset)/(1 - offset);
2043                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2044                             stop = stop->getNextStop();
2045                         }
2046                     }
2047                     break;
2048                 case POINT_LG_END:
2049                     {
2050                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2052                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2053                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2054                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2055                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2056                         gdouble offset = laststop->offset;
2057                         Geom::Point newend = begin + offset * (oldend - begin);
2058                         lg->x2.computed = newend[Geom::X];
2059                         lg->y2.computed = newend[Geom::Y];
2061                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2062                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2063                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2064                         laststop->offset = 1;
2065                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2067                         // iterate through midstops to set new offset values such that they won't move on canvas.
2068                         SPStop *stop = stopinfo->vector->getFirstStop();
2069                         stop = stop->getNextStop();
2070                         while ( stop != laststop ) {
2071                             stop->offset = stop->offset / offset;
2072                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2073                             stop = stop->getNextStop();
2074                         }
2075                     }
2076                     break;
2077                 case POINT_RG_CENTER:
2078                     {
2079                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2080                         if (newfirst) {
2081                             newfirst->offset = 0;
2082                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2083                         }
2084                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2085                     }
2086                     break;
2087                 case POINT_RG_R1:
2088                 case POINT_RG_R2:
2089                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2091                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2092                         double oldradius = rg->r.computed;
2093                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2094                         gdouble offset = laststop->offset;
2095                         double newradius = offset * oldradius;
2096                         rg->r.computed = newradius;
2098                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2099                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2100                         laststop->offset = 1;
2101                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2103                         // iterate through midstops to set new offset values such that they won't move on canvas.
2104                         SPStop *stop = stopinfo->vector->getFirstStop();
2105                         stop = stop->getNextStop();
2106                         while ( stop != laststop ) {
2107                             stop->offset = stop->offset / offset;
2108                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2109                             stop = stop->getNextStop();
2110                         }
2111                         break;
2112             }
2113         }
2114         else
2115         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2116             SPCSSAttr *css = sp_repr_css_attr_new ();
2118             // stopinfo->spstop is the selected stop
2119             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2120             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2121                 unselectedrepr = unselectedrepr->next();
2122             }
2124             if (unselectedrepr == NULL) {
2125                 if (stopinfo->draggable->fill_or_stroke) {
2126                     sp_repr_css_unset_property (css, "fill");
2127                 } else {
2128                     sp_repr_css_unset_property (css, "stroke");
2129                 }
2130             } else {
2131                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2132                 if (stopinfo->draggable->fill_or_stroke) {
2133                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2134                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2135                 } else {
2136                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2137                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2138                 }
2139                 sp_repr_css_attr_unref (stopcss);
2140             }
2142             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2143             sp_repr_css_attr_unref (css);
2144         }
2146         endstoplist = g_slist_remove(endstoplist, stopinfo);
2147         delete stopinfo;
2148     }
2150     if (document) {
2151         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2152     }
2156 /*
2157   Local Variables:
2158   mode:c++
2159   c-file-style:"stroustrup"
2160   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2161   indent-tabs-mode:nil
2162   fill-column:99
2163   End:
2164 */
2165 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :