Code

fix pasting style where style or fill is none
[inkscape.git] / src / gradient-drag.cpp
index 59081475ee22a0f6550b243f844e1889f71445a9..67fa0933372e19c2a62191fbc50512cccae731e5 100644 (file)
@@ -30,6 +30,7 @@
 #include "svg/css-ostringstream.h"
 
 #include "svg/svg.h"
+#include "libnr/nr-point-fns.h"
 
 #include "prefs-utils.h"
 #include "sp-item.h"
@@ -46,6 +47,7 @@
 
 
 #define GR_KNOT_COLOR_NORMAL 0xffffff00
+#define GR_KNOT_COLOR_MOUSEOVER 0xff000000
 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
 
 #define GR_LINE_COLOR_FILL 0x0000ff7f
@@ -73,13 +75,13 @@ SPKnotShapeType gr_knot_shapes [] = {
 const gchar *gr_knot_descr [] = {
     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
     N_("Linear gradient <b>end</b>"),
-    N_("Linear gradient <b>midstop</b>"),
+    N_("Linear gradient <b>mid stop</b>"),
     N_("Radial gradient <b>center</b>"),
     N_("Radial gradient <b>radius</b>"),
     N_("Radial gradient <b>radius</b>"),
     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
-    N_("Linear gradient <b>midstop</b>"),
-    N_("Linear gradient <b>midstop</b>")
+    N_("Radial gradient <b>mid stop</b>"),
+    N_("Radial gradient <b>mid stop</b>")
 };
 
 static void
@@ -128,22 +130,25 @@ gr_drag_style_query (SPStyle *style, int property, gpointer data)
 
         int count = 0;
 
-        for (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger
-            GrDraggable *draggable = (GrDraggable *) i->data;
+        for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
+            GrDragger *d = (GrDragger *) i->data;
+            for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
+                GrDraggable *draggable = (GrDraggable *) j->data;
 
-            if (ret == QUERY_STYLE_NOTHING) {
-                ret = QUERY_STYLE_SINGLE;
-            } else if (ret == QUERY_STYLE_SINGLE) {
-                ret = QUERY_STYLE_MULTIPLE_AVERAGED;
-            }
+                if (ret == QUERY_STYLE_NOTHING) {
+                    ret = QUERY_STYLE_SINGLE;
+                } else if (ret == QUERY_STYLE_SINGLE) {
+                    ret = QUERY_STYLE_MULTIPLE_AVERAGED;
+                }
 
-            guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
-            cf[0] += SP_RGBA32_R_F (c);
-            cf[1] += SP_RGBA32_G_F (c);
-            cf[2] += SP_RGBA32_B_F (c);
-            cf[3] += SP_RGBA32_A_F (c);
+                guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
+                cf[0] += SP_RGBA32_R_F (c);
+                cf[1] += SP_RGBA32_G_F (c);
+                cf[2] += SP_RGBA32_B_F (c);
+                cf[3] += SP_RGBA32_A_F (c);
 
-            count ++;
+                count ++;
+            }
         }
 
         if (count) {
@@ -219,9 +224,9 @@ gr_drag_style_set (const SPCSSAttr *css, gpointer data)
         os << accumulated;
         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
 
-        if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
-            (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
-            sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
+        if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
+            (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
+            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
     }
 
     if (!stop->attributeList()) { // nothing for us here, pass it on
@@ -244,6 +249,127 @@ gr_drag_style_set (const SPCSSAttr *css, gpointer data)
     return true;
 }
 
+SPStop *
+GrDrag::addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance)
+{
+    gfloat offset; // type of SPStop.offset = gfloat
+    SPGradient *gradient;
+    bool fill_or_stroke = true;
+    bool r1_knot = false;
+
+    bool addknot = false;
+    do {
+        gradient = sp_item_gradient (item, fill_or_stroke);
+        if (SP_IS_LINEARGRADIENT(gradient)) {
+            NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
+            NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
+
+            NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
+            double dist_screen = NR::L2 (mouse_p - nearest);
+            if ( dist_screen < tolerance ) {
+                // add the knot
+                offset = get_offset_between_points(nearest, begin, end);
+                addknot = true;
+                break; // break out of the while loop: add only one knot
+            }
+        } else if (SP_IS_RADIALGRADIENT(gradient)) {
+            NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
+            NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
+            NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
+            double dist_screen = NR::L2 (mouse_p - nearest);
+            if ( dist_screen < tolerance ) {
+                offset = get_offset_between_points(nearest, begin, end);
+                addknot = true;
+                r1_knot = true;
+                break; // break out of the while loop: add only one knot
+            }
+
+            end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
+            nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
+            dist_screen = NR::L2 (mouse_p - nearest);
+            if ( dist_screen < tolerance ) {
+                offset = get_offset_between_points(nearest, begin, end);
+                addknot = true;
+                r1_knot = false;
+                break; // break out of the while loop: add only one knot
+            }
+        }
+        fill_or_stroke = !fill_or_stroke;
+    } while (!fill_or_stroke && !addknot) ;
+
+    if (addknot) {
+        SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
+        SPStop* prev_stop = sp_first_stop(vector);
+        SPStop* next_stop = sp_next_stop(prev_stop);
+        guint i = 1;
+        while ( (next_stop) && (next_stop->offset < offset) ) {
+            prev_stop = next_stop;
+            next_stop = sp_next_stop(next_stop);
+            i++;
+        }
+        if (!next_stop) {
+            // logical error: the endstop should have offset 1 and should always be more than this offset here
+            return NULL;
+        }
+
+
+        SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
+        sp_gradient_ensure_vector (gradient);
+        updateDraggers();
+
+        return newstop;
+    } 
+
+    return NULL;
+}
+
+
+bool 
+GrDrag::dropColor(SPItem *item, gchar *c, NR::Point p) 
+{
+    // first, see if we can drop onto one of the existing draggers
+    for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
+        GrDragger *d = (GrDragger *) i->data;
+
+        if (NR::L2(p - d->point)*desktop->current_zoom() < 5) {
+           SPCSSAttr *stop = sp_repr_css_attr_new ();
+           sp_repr_css_set_property (stop, "stop-color", c);
+           sp_repr_css_set_property (stop, "stop-opacity", "1");
+           for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
+               GrDraggable *draggable = (GrDraggable *) j->data;
+               local_change = true;
+               sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
+           }
+           sp_repr_css_attr_unref(stop);
+           return true;
+        }
+    }
+
+    // now see if we're over line and create a new stop
+    bool over_line = false;
+    SPCtrlLine *line = NULL;
+    if (lines) {
+        for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
+            line = (SPCtrlLine*) l->data;
+            NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
+            double dist_screen = NR::L2 (p - nearest) * desktop->current_zoom();
+            if (line->item && dist_screen < 5) {
+                SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
+                if (stop) {
+                    SPCSSAttr *css = sp_repr_css_attr_new ();
+                    sp_repr_css_set_property (css, "stop-color", c);
+                    sp_repr_css_set_property (css, "stop-opacity", "1");
+                    sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
+                    return true;
+                }
+            }
+        }
+    }
+
+    return false;
+}
+
+
 GrDrag::GrDrag(SPDesktop *desktop) {
 
     this->desktop = desktop;
@@ -342,35 +468,20 @@ GrDraggable::~GrDraggable ()
     g_object_unref (G_OBJECT (this->item));
 }
 
-// FIXME: make global function in libnr or somewhere.
-static NR::Point *
-get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
-{
-    double r = NR::L2 (p - o);
-    if (r < 1e-3)
-        return NULL;
-    double angle = NR::atan2 (p - o);
-    // snap angle to snaps increments, starting from initial:
-    double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
-    // calculate the new position and subtract p to get the vector:
-    return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
-}
 
-// FIXME: make global function in libnr or somewhere.
-static NR::Point
-snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
+SPObject *
+GrDraggable::getServer ()
 {
-    double length = NR::L2(end - begin);
-    NR::Point be = (end - begin) / length;
-    double r = NR::dot(p - begin, be);
-
-    if (r < 0.0) return begin;
-    if (r > length) return end;
+    if (!item)
+        return NULL;
 
-    double snapdist = length * snap;
-    double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
+    SPObject *server = NULL;
+    if (fill_or_stroke)
+        server = SP_OBJECT_STYLE_FILL_SERVER (item);
+    else
+        server = SP_OBJECT_STYLE_STROKE_SERVER (item);
 
-    return (begin + r_snapped * be);
+    return server;
 }
 
 static void
@@ -537,62 +648,45 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi
         g_slist_free(snap_vectors);
     }
 
-    dragger->point = p;
+    drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
+    bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
 
-    if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
-        dragger->fireDraggables (false, true);
+    if (drag->keep_selection) {
+        NR::Point diff = p - dragger->point;
+        drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
     } else {
-        dragger->fireDraggables (false);
+        dragger->point = p;
+        dragger->fireDraggables (false, scale_radial);
+        dragger->updateDependencies(false);
     }
 
-    dragger->updateDependencies(false);
-
-    drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
 }
 
 
-/**
-Called when a midpoint knot is dragged.
-*/
 static void
-gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
+gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
 {
-    GrDragger *dragger = (GrDragger *) data;
+
     GrDrag *drag = dragger->parent;
     // a midpoint dragger can (logically) only contain one GrDraggable
     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
 
-    // FIXME: take from prefs
-    double snap_fraction = 0.1;
-
-    NR::Point p = *ppointer;
-    NR::Point begin(0,0), end(0,0);
-
-    SPObject *server;
-    if (draggable->fill_or_stroke)
-        server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
-    else
-        server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
-
-
     // get begin and end points between which dragging is allowed:
     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
-    GSList *moving = NULL;
-    moving = g_slist_append(moving, dragger);
+    *moving = g_slist_append(*moving, dragger);
 
     guint lowest_i = draggable->point_i;
     guint highest_i = draggable->point_i;
     GrDragger *lowest_dragger = dragger;
     GrDragger *highest_dragger = dragger;
-    bool is_selected = g_list_find(drag->selected, dragger);
-    if (is_selected) {
+    if (dragger->isSelected()) {
         GrDragger* d_add;
         while ( true )
         {
             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
             if ( d_add && g_list_find(drag->selected, d_add) ) {
                 lowest_i = lowest_i - 1;
-                moving = g_slist_prepend(moving, d_add);
+                *moving = g_slist_prepend(*moving, d_add);
                 lowest_dragger = d_add;
             } else {
                 break;
@@ -604,7 +698,7 @@ gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint st
             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
             if ( d_add && g_list_find(drag->selected, d_add) ) {
                 highest_i = highest_i + 1;
-                moving = g_slist_append(moving, d_add);
+                *moving = g_slist_append(*moving, d_add);
                 highest_dragger = d_add;
             } else {
                 break;
@@ -613,37 +707,69 @@ gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint st
     }
 
     if ( SP_IS_LINEARGRADIENT(server) ) {
+        guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
         GrDragger *d_temp;
         if (lowest_i == 1) {
             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
         } else {
             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
         }
-        if (d_temp) begin = d_temp->point;
+        if (d_temp) 
+            *begin = d_temp->point;
 
         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
         if (d_temp == NULL) {
-            d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke);
+            d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
         }
-        if (d_temp) end = d_temp->point;
+        if (d_temp) 
+            *end = d_temp->point;
     } else if ( SP_IS_RADIALGRADIENT(server) ) {
+        guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
         GrDragger *d_temp;
         if (lowest_i == 1) {
             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
         } else {
             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
         }
-        if (d_temp) begin = d_temp->point;
+        if (d_temp) 
+            *begin = d_temp->point;
 
         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
         if (d_temp == NULL) {
-            d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, 0, draggable->fill_or_stroke);
+            d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
         }
-        if (d_temp) end = d_temp->point;
+        if (d_temp) 
+            *end = d_temp->point;
     }
 
-    NR::Point low_lim  = dragger->point - (lowest_dragger->point - begin);
-    NR::Point high_lim = dragger->point - (highest_dragger->point - end);
+    *low_lim  = dragger->point - (lowest_dragger->point - *begin);
+    *high_lim = dragger->point - (highest_dragger->point - *end);
+}
+
+
+
+/**
+Called when a midpoint knot is dragged.
+*/
+static void
+gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
+{
+    GrDragger *dragger = (GrDragger *) data;
+    GrDrag *drag = dragger->parent;
+    // a midpoint dragger can (logically) only contain one GrDraggable
+    GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
+
+    // FIXME: take from prefs
+    double snap_fraction = 0.1;
+
+    NR::Point p = *ppointer;
+    NR::Point begin(0,0), end(0,0);
+    NR::Point low_lim(0,0), high_lim(0,0);
+
+    SPObject *server = draggable->getServer();
+
+    GSList *moving = NULL;
+    gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
 
     if (state & GDK_CONTROL_MASK) {
         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
@@ -655,13 +781,27 @@ gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint st
     for (GSList const* i = moving; i != NULL; i = i->next) {
         GrDragger *drg = (GrDragger*) i->data;
         SPKnot *drgknot = drg->knot;
-        drg->point += displacement;
+        NR::Point this_move = displacement;
+        if (state & GDK_MOD1_MASK) {
+            // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
+            double alpha = 1.5;
+            if (NR::L2(drg->point - dragger->point) + NR::L2(drg->point - begin) - 1e-3 > NR::L2(dragger->point - begin)) { // drg is on the end side from dragger
+                double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
+                this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
+            } else { // drg is on the begin side from dragger
+                double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
+                this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
+            }
+        }
+        drg->point += this_move;
         sp_knot_moveto (drgknot, & drg->point);
         drg->fireDraggables (false);
         drg->updateDependencies(false);
     }
 
-    drag->keep_selection = is_selected;
+    g_slist_free(moving);
+
+    drag->keep_selection = dragger->isSelected();
 }
 
 
@@ -692,6 +832,13 @@ gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
         dragger->fireDraggables (true);
     }
 
+    for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
+        GrDragger *d = (GrDragger *) i->data;
+        if (d == dragger)
+            continue;
+        d->fireDraggables (true);
+    }
+
     // make this dragger selected
     if (!dragger->parent->keep_selection) {
         dragger->parent->setSelected (dragger);
@@ -812,7 +959,7 @@ GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focu
 Checks if the dragger has a draggable with this point_type
  */
 bool
-GrDragger::isA (guint point_type)
+GrDragger::isA (gint point_type)
 {
     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
         GrDraggable *draggable = (GrDraggable *) i->data;
@@ -827,7 +974,7 @@ GrDragger::isA (guint point_type)
 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
  */
 bool
-GrDragger::isA (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
+GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
 {
     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
         GrDraggable *draggable = (GrDraggable *) i->data;
@@ -958,7 +1105,7 @@ GrDragger::addDraggable (GrDraggable *draggable)
 Moves this dragger to the point of the given draggable, acting upon all other draggables
  */
 void
-GrDragger::moveThisToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
+GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
 {
     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
     this->point_original = this->point;
@@ -967,7 +1114,10 @@ GrDragger::moveThisToDraggable (SPItem *item, guint point_type, guint point_i, b
 
     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
         GrDraggable *da = (GrDraggable *) i->data;
-        if ( (da->item == item) && (da->point_type == point_type) && (da->point_i == point_i) && (da->fill_or_stroke == fill_or_stroke) ) {
+        if ( (da->item == item) && 
+             (point_type == -1 || da->point_type == point_type) &&
+             (point_i == -1 || da->point_i == point_i) &&
+             (da->fill_or_stroke == fill_or_stroke) ) {
             continue;
         }
         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
@@ -981,11 +1131,9 @@ Moves all midstop draggables that depend on this one
  */
 void
 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
-    SPObject *server;
-    if (draggable->fill_or_stroke)
-        server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
-    else
-        server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
+    SPObject *server = draggable->getServer();
+    if (!server) 
+        return;
     guint num = SP_GRADIENT(server)->vector.stops.size();
     if (num <= 2) return;
 
@@ -1014,7 +1162,7 @@ GrDragger::updateDependencies (bool write_repr)
             case POINT_LG_BEGIN:
                 {
                     // the end point is dependent only when dragging with ctrl+shift
-                    this->moveOtherToDraggable (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke, write_repr);
+                    this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
 
                     this->updateMidstopDependencies (draggable, write_repr);
                 }
@@ -1031,19 +1179,19 @@ GrDragger::updateDependencies (bool write_repr)
                 // no other nodes depend on mid points.
                 break;
             case POINT_RG_R2:
-                this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
-                this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
+                this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
+                this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
                 this->updateMidstopDependencies (draggable, write_repr);
                 break;
             case POINT_RG_R1:
-                this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
-                this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
+                this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
+                this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
                 this->updateMidstopDependencies (draggable, write_repr);
                 break;
             case POINT_RG_CENTER:
-                this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
-                this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
-                this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
+                this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
+                this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
+                this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
                 this->updateMidstopDependencies (draggable, write_repr);
                 break;
             case POINT_RG_FOCUS:
@@ -1075,7 +1223,7 @@ GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
     // create the knot
     this->knot = sp_knot_new (parent->desktop, NULL);
     this->knot->setMode(SP_KNOT_MODE_XOR);
-    this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
+    this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
     sp_knot_update_ctrl(this->knot);
 
@@ -1132,13 +1280,16 @@ GrDragger::~GrDragger ()
 Select the dragger which has the given draggable.
 */
 GrDragger *
-GrDrag::getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
+GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
 {
     for (GList const* i = this->draggers; i != NULL; i = i->next) {
         GrDragger *dragger = (GrDragger *) i->data;
         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
             GrDraggable *da2 = (GrDraggable *) j->data;
-            if ( (da2->item == item) && (da2->point_type == point_type) && (da2->point_i == point_i) && (da2->fill_or_stroke == fill_or_stroke)) {
+            if ( (da2->item == item) && 
+                 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
+                 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
+                 (da2->fill_or_stroke == fill_or_stroke)) {
                 return (dragger);
             }
         }
@@ -1148,7 +1299,7 @@ GrDrag::getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_
 
 
 void
-GrDragger::moveOtherToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
+GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
 {
     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
     if (d && d !=  this) {
@@ -1177,7 +1328,11 @@ GrDragger::deselect()
     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
 }
 
-
+bool
+GrDragger::isSelected()
+{
+    return g_list_find (parent->selected, this);
+}
 
 /**
 \brief Deselect all stops/draggers (private)
@@ -1201,6 +1356,48 @@ GrDrag::deselectAll()
     this->desktop->emitToolSubselectionChanged(NULL);
 }
 
+/**
+\brief Select all stops/draggers
+*/
+void
+GrDrag::selectAll()
+{
+    for (GList *l = this->draggers; l != NULL; l = l->next) {
+        GrDragger *d = ((GrDragger *) l->data);
+        setSelected (d, true, true);
+    }
+}
+
+/**
+\brief Select all stops/draggers that match the coords
+*/
+void 
+GrDrag::selectByCoords(std::vector<NR::Point> coords)
+{
+    for (GList *l = this->draggers; l != NULL; l = l->next) {
+        GrDragger *d = ((GrDragger *) l->data);
+        for (guint k = 0; k < coords.size(); k++) {
+            if (NR::L2 (d->point - coords[k]) < 1e-4) {
+                setSelected (d, true, true);
+            }
+        }
+    }
+}
+
+
+/**
+\brief Select all stops/draggers that fall within the rect
+*/
+void 
+GrDrag::selectRect(NR::Rect const &r)
+{
+    for (GList *l = this->draggers; l != NULL; l = l->next) {
+        GrDragger *d = ((GrDragger *) l->data);
+        if (r.contains(d->point)) {
+           setSelected (d, true, true);
+        }
+    }
+}
 
 /**
 \brief Select a dragger
@@ -1267,10 +1464,11 @@ GrDrag::setDeselected (GrDragger *dragger)
 Create a line from p1 to p2 and add it to the lines list
  */
 void
-GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
+GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
 {
     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
                                                             SP_TYPE_CTRLLINE, NULL);
+    SP_CTRLLINE(line)->item = item;
     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
@@ -1315,13 +1513,13 @@ GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stro
             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
         }
     }
-    addDragger (new GrDraggable (item, POINT_RG_R1, 0, fill_or_stroke));
+    addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
     if (num > 2) {
         for ( guint i = 1; i < num - 1; i++ ) {
             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
         }
     }
-    addDragger (new GrDraggable (item, POINT_RG_R2, 0, fill_or_stroke));
+    addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
 }
 
@@ -1338,14 +1536,25 @@ GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stro
             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
         }
     }
-    addDragger (new GrDraggable (item, POINT_LG_END, 0, fill_or_stroke));
+    addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
+}
+
+/**
+Artificially grab the knot of this dragger; used by the gradient context
+*/
+void
+GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
+{
+    if (dragger) {
+        sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
+    }
 }
 
 /**
 Artificially grab the knot of the dragger with this draggable; used by the gradient context
 */
 void
-GrDrag::grabKnot (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
+GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
 {
     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
     if (dragger) {
@@ -1422,22 +1631,22 @@ GrDrag::updateLines ()
         if (style && (style->fill.isPaintserver())) {
             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
             if (SP_IS_LINEARGRADIENT (server)) {
-                this->addLine (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);
+                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);
             } else if (SP_IS_RADIALGRADIENT (server)) {
                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
-                this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
-                this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
+                this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
+                this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
             }
         }
 
         if (style && (style->stroke.isPaintserver())) {
             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
             if (SP_IS_LINEARGRADIENT (server)) {
-                this->addLine (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);
+                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);
             } else if (SP_IS_RADIALGRADIENT (server)) {
                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
-                this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
-                this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
+                this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
+                this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
             }
         }
     }
@@ -1483,25 +1692,98 @@ GrDrag::selected_reverse_vector ()
 }
 
 void
-GrDrag::selected_move (double x, double y)
+GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
+{
+    selected_move (x, y, false, scale_radial);
+}
+
+void
+GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
 {
-/*
     if (selected == NULL)
         return;
 
-    if ( (g_list_length(selected) == 1) )
-    selected->point += NR::Point (x, y);
-    selected->point_original = selected->point;
-    sp_knot_moveto (selected->knot, &(selected->point));
+    bool did = false; 
 
-    selected->fireDraggables (true);
+    for (GList *i = selected; i != NULL; i = i->next) {
+        GrDragger *d = (GrDragger *) i->data;
 
-    selected->updateDependencies(true);
+        if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
+            // if this is an endpoint,
 
-    // we did an undoable action
-    sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
-                      _("Move gradient handle"));
-*/
+            // Moving an rg center moves its focus and radii as well.
+            // therefore, if this is a focus or radius and if selection
+            // contains the center as well, do not move this one
+            if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) || 
+                (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
+                bool skip_radius_with_center = false;
+                for (GList *di = selected; di != NULL; di = di->next) {
+                    GrDragger *d_new = (GrDragger *) di->data;
+                    if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
+                                    POINT_RG_CENTER,
+                                    0,
+                                    ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
+                        // FIXME: here we take into account only the first draggable!
+                        skip_radius_with_center = true;
+                    }
+                }
+                if (skip_radius_with_center)
+                    continue;
+            }
+
+            did = true;
+            d->point += NR::Point (x, y);
+            d->point_original = d->point;
+            sp_knot_moveto (d->knot, &(d->point));
+
+            d->fireDraggables (write_repr, scale_radial);
+
+            d->updateDependencies(write_repr);
+        }
+    }
+
+    if (write_repr && did) {
+        // we did an undoable action
+        sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
+                                _("Move gradient handle(s)"));
+        return;
+    }
+
+    if (!did) { // none of the end draggers are selected, so let's try to move the mids
+
+        GrDragger *dragger = (GrDragger *) selected->data;
+        // a midpoint dragger can (logically) only contain one GrDraggable
+        GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
+
+        NR::Point begin(0,0), end(0,0);
+        NR::Point low_lim(0,0), high_lim(0,0);
+
+        SPObject *server = draggable->getServer();
+        GSList *moving = NULL;
+        gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
+
+        NR::Point p(x, y);
+        p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
+        NR::Point displacement = p - dragger->point;
+
+        for (GSList const* i = moving; i != NULL; i = i->next) {
+            GrDragger *drg = (GrDragger*) i->data;
+            SPKnot *drgknot = drg->knot;
+            drg->point += displacement;
+            sp_knot_moveto (drgknot, & drg->point);
+            drg->fireDraggables (true);
+            drg->updateDependencies(true);
+            did = true;
+        }
+
+        g_slist_free(moving);
+
+        if (write_repr && did) {
+            // we did an undoable action
+            sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
+                                    _("Move gradient mid stop(s)"));
+        }
+    }
 }
 
 void
@@ -1517,29 +1799,37 @@ GrDrag::selected_move_screen (double x, double y)
 /**
 Select the knot next to the last selected one and deselect all other selected.
 */
-void
+GrDragger *
 GrDrag::select_next ()
 {
+    GrDragger *d = NULL;
     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
-        if (draggers)
-            setSelected ((GrDragger *) draggers->data);
+        if (draggers) 
+            d = (GrDragger *) draggers->data;
     } else {
-        setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
+        d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
     }
+    if (d)
+        setSelected (d);
+    return d;
 }
 
 /**
 Select the knot previous from the last selected one and deselect all other selected.
 */
-void
+GrDragger *
 GrDrag::select_prev ()
 {
+    GrDragger *d = NULL;
     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
         if (draggers)
-            setSelected ((GrDragger *) g_list_last (draggers)->data);
+            d = (GrDragger *) g_list_last (draggers)->data;
     } else {
-        setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
+        d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
     }
+    if (d)
+        setSelected (d);
+    return d;
 }