Code

Stop setting of stops from getting url() reference colors.
[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 GrDrag::styleSet( const SPCSSAttr *css )
181     if (!selected) {
182         return false;
183     }
185     SPCSSAttr *stop = sp_repr_css_attr_new();
187     // See if the css contains interesting properties, and if so, translate them into the format
188     // acceptable for gradient stops
190     // any of color properties, in order of increasing priority:
191     if (css->attribute("flood-color")) {
192         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
193     }
195     if (css->attribute("lighting-color")) {
196         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
197     }
199     if (css->attribute("color")) {
200         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
201     }
203     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none")) {
204         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
205     }
207     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none")) {
208         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
209     }
211     if (css->attribute("stop-color")) {
212         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
213     }
215     // Make sure the style is allowed for gradient stops.
216     if ( !sp_repr_css_property_is_unset( stop, "stop-color") ) {
217         Glib::ustring tmp = sp_repr_css_property( stop, "stop-color", "" );
218         Glib::ustring::size_type pos = tmp.find("url(#");
219         if ( pos != Glib::ustring::npos ) {
220             Glib::ustring targetName = tmp.substr(pos + 5, tmp.length() - 6);
221             const GSList *gradients = sp_document_get_resource_list(desktop->doc(), "gradient");
222             for (const GSList *item = gradients; item; item = item->next) {
223                 SPGradient* grad = SP_GRADIENT(item->data);
224                 if ( targetName == grad->getId() ) {
225                     SPGradient *vect = grad->getVector();
226                     SPStop *firstStop = (vect) ? vect->getFirstStop() : grad->getFirstStop();
227                     if (firstStop) {
228                         Glib::ustring stopColorStr;
229                         if (firstStop->currentColor) {
230                             stopColorStr = sp_object_get_style_property(firstStop, "color", NULL);
231                         } else {
232                             stopColorStr = firstStop->specified_color.toString();
233                         }
234                         if ( !stopColorStr.empty() ) {
235                             sp_repr_css_set_property( stop, "stop-color", stopColorStr.c_str() );
236                         }
237                     }
238                     break;
239                 }
240             }
241         }
242     }
245     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
246         sp_repr_css_set_property(stop, "stop-opacity", css->attribute("stop-opacity"));
247     } else {  // multiply all opacity properties:
248         gdouble accumulated = 1.0;
249         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
250         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
251         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
252         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
254         Inkscape::CSSOStringStream os;
255         os << accumulated;
256         sp_repr_css_set_property(stop, "stop-opacity", os.str().c_str());
258         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
259             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none"))) {
260             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
261         }
262     }
264     if (!stop->attributeList()) { // nothing for us here, pass it on
265         sp_repr_css_attr_unref(stop);
266         return false;
267     }
269     for (GList const* sel = selected; sel != NULL; sel = sel->next) { // for all selected draggers
270         GrDragger* dragger = reinterpret_cast<GrDragger*>(sel->data);
271         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
272             GrDraggable *draggable = reinterpret_cast<GrDraggable *>(i->data);
274             local_change = true;
275             sp_item_gradient_stop_set_style(draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
276         }
277     }
279     //sp_repr_css_print(stop);
280     sp_repr_css_attr_unref(stop);
281     return true;
284 guint32 GrDrag::getColor()
286     if (!selected) return 0;
288     float cf[4];
289     cf[0] = cf[1] = cf[2] = cf[3] = 0;
291     int count = 0;
293     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
294         GrDragger *d = (GrDragger *) i->data;
295         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
296             GrDraggable *draggable = (GrDraggable *) j->data;
298             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
299             cf[0] += SP_RGBA32_R_F (c);
300             cf[1] += SP_RGBA32_G_F (c);
301             cf[2] += SP_RGBA32_B_F (c);
302             cf[3] += SP_RGBA32_A_F (c);
304             count ++;
305         }
306     }
308     if (count) {
309         cf[0] /= count;
310         cf[1] /= count;
311         cf[2] /= count;
312         cf[3] /= count;
313     }
315     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
318 SPStop *
319 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
321     gfloat offset; // type of SPStop.offset = gfloat
322     SPGradient *gradient;
323     bool fill_or_stroke = true;
324     bool r1_knot = false;
326     bool addknot = false;
327     do {
328         gradient = sp_item_gradient (item, fill_or_stroke);
329         if (SP_IS_LINEARGRADIENT(gradient)) {
330             Geom::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
331             Geom::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
333             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
334             double dist_screen = Geom::L2 (mouse_p - nearest);
335             if ( dist_screen < tolerance ) {
336                 // add the knot
337                 offset = get_offset_between_points(nearest, begin, end);
338                 addknot = true;
339                 break; // break out of the while loop: add only one knot
340             }
341         } else if (SP_IS_RADIALGRADIENT(gradient)) {
342             Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
343             Geom::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
344             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
345             double dist_screen = Geom::L2 (mouse_p - nearest);
346             if ( dist_screen < tolerance ) {
347                 offset = get_offset_between_points(nearest, begin, end);
348                 addknot = true;
349                 r1_knot = true;
350                 break; // break out of the while loop: add only one knot
351             }
353             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
354             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
355             dist_screen = Geom::L2 (mouse_p - nearest);
356             if ( dist_screen < tolerance ) {
357                 offset = get_offset_between_points(nearest, begin, end);
358                 addknot = true;
359                 r1_knot = false;
360                 break; // break out of the while loop: add only one knot
361             }
362         }
363         fill_or_stroke = !fill_or_stroke;
364     } while (!fill_or_stroke && !addknot) ;
366     if (addknot) {
367         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
368         SPStop* prev_stop = vector->getFirstStop();
369         SPStop* next_stop = prev_stop->getNextStop();
370         guint i = 1;
371         while ( (next_stop) && (next_stop->offset < offset) ) {
372             prev_stop = next_stop;
373             next_stop = next_stop->getNextStop();
374             i++;
375         }
376         if (!next_stop) {
377             // logical error: the endstop should have offset 1 and should always be more than this offset here
378             return NULL;
379         }
382         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
383         gradient->ensureVector();
384         updateDraggers();
386         return newstop;
387     }
389     return NULL;
393 bool
394 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
396     // first, see if we can drop onto one of the existing draggers
397     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
398         GrDragger *d = (GrDragger *) i->data;
400         if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
401            SPCSSAttr *stop = sp_repr_css_attr_new ();
402            sp_repr_css_set_property (stop, "stop-color", c);
403            sp_repr_css_set_property (stop, "stop-opacity", "1");
404            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
405                GrDraggable *draggable = (GrDraggable *) j->data;
406                local_change = true;
407                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
408            }
409            sp_repr_css_attr_unref(stop);
410            return true;
411         }
412     }
414     // now see if we're over line and create a new stop
415     bool over_line = false;
416     SPCtrlLine *line = NULL;
417     if (lines) {
418         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
419             line = (SPCtrlLine*) l->data;
420             Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
421             double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
422             if (line->item && dist_screen < 5) {
423                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
424                 if (stop) {
425                     SPCSSAttr *css = sp_repr_css_attr_new ();
426                     sp_repr_css_set_property (css, "stop-color", c);
427                     sp_repr_css_set_property (css, "stop-opacity", "1");
428                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
429                     return true;
430                 }
431             }
432         }
433     }
435     return false;
439 GrDrag::GrDrag(SPDesktop *desktop) :
440     selected(0),
441     keep_selection(false),
442     local_change(false),
443     desktop(desktop),
444     hor_levels(),
445     vert_levels(),
446     draggers(0),
447     lines(0),
448     selection(sp_desktop_selection(desktop)),
449     sel_changed_connection(),
450     sel_modified_connection(),
451     style_set_connection(),
452     style_query_connection()
454     sel_changed_connection = selection->connectChanged(
455         sigc::bind(
456             sigc::ptr_fun(&gr_drag_sel_changed),
457             (gpointer)this )
459         );
460     sel_modified_connection = selection->connectModified(
461         sigc::bind(
462             sigc::ptr_fun(&gr_drag_sel_modified),
463             (gpointer)this )
464         );
466     style_set_connection = desktop->connectSetStyle( sigc::mem_fun(*this, &GrDrag::styleSet) );
468     style_query_connection = desktop->connectQueryStyle(
469         sigc::bind(
470             sigc::ptr_fun(&gr_drag_style_query),
471             (gpointer)this )
472         );
474     updateDraggers();
475     updateLines();
476     updateLevels();
478     if (desktop->gr_item) {
479         setSelected(getDraggerFor(desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
480     }
483 GrDrag::~GrDrag()
485     this->sel_changed_connection.disconnect();
486     this->sel_modified_connection.disconnect();
487     this->style_set_connection.disconnect();
488     this->style_query_connection.disconnect();
490     if (this->selected) {
491         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
492         desktop->gr_item = draggable->item;
493         desktop->gr_point_type = draggable->point_type;
494         desktop->gr_point_i = draggable->point_i;
495         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
496     } else {
497         desktop->gr_item = NULL;
498         desktop->gr_point_type = 0;
499         desktop->gr_point_i = 0;
500         desktop->gr_fill_or_stroke = true;
501     }
503     deselect_all();
504     for (GList *l = this->draggers; l != NULL; l = l->next) {
505         delete ((GrDragger *) l->data);
506     }
507     g_list_free (this->draggers);
508     this->draggers = NULL;
509     this->selected = NULL;
511     for (GSList *l = this->lines; l != NULL; l = l->next) {
512         gtk_object_destroy( GTK_OBJECT (l->data));
513     }
514     g_slist_free (this->lines);
515     this->lines = NULL;
518 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
520     this->item = item;
521     this->point_type = point_type;
522     this->point_i = point_i;
523     this->fill_or_stroke = fill_or_stroke;
525     g_object_ref (G_OBJECT (this->item));
528 GrDraggable::~GrDraggable ()
530     g_object_unref (G_OBJECT (this->item));
534 SPObject *GrDraggable::getServer()
536     if (!item) {
537         return NULL;
538     }
540     SPObject *server = NULL;
541     if (fill_or_stroke) {
542         server = item->style->getFillPaintServer();
543     }else {
544         server = item->style->getStrokePaintServer();
545     }
547     return server;
550 static
551 boost::optional<Geom::Point>
552 get_snap_vector (Geom::Point p, Geom::Point o, double snap, double initial)
554     double r = L2 (p - o);
555     if (r < 1e-3) {
556         return boost::optional<Geom::Point>();
557     }
559     double angle = atan2 (p - o);
560     // snap angle to snaps increments, starting from initial:
561     double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
562     // calculate the new position and subtract p to get the vector:
563     return (o + r * Geom::Point(cos(a_snapped), sin(a_snapped)) - p);
566 static void
567 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
569     GrDragger *dragger = (GrDragger *) data;
570     GrDrag *drag = dragger->parent;
572     Geom::Point p = ppointer;
574     SPDesktop *desktop = dragger->parent->desktop;
575     SnapManager &m = desktop->namedview->snap_manager;
576     double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom();
578     if (state & GDK_SHIFT_MASK) {
579         // with Shift; unsnap if we carry more than one draggable
580         if (dragger->draggables && dragger->draggables->next) {
581             // create a new dragger
582             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
583             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
584             // relink to it all but the first draggable in the list
585             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
586                 GrDraggable *draggable = (GrDraggable *) i->data;
587                 dr_new->addDraggable (draggable);
588             }
589             dr_new->updateKnotShape();
590             g_slist_free (dragger->draggables->next);
591             dragger->draggables->next = NULL;
592             dragger->updateKnotShape();
593             dragger->updateTip();
594         }
595     } else if (!(state & GDK_CONTROL_MASK)) {
596         // without Shift or Ctrl; see if we need to snap to another dragger
597         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
598             GrDragger *d_new = (GrDragger *) di->data;
599             if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
601                 // Merge draggers:
602                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
603                     GrDraggable *draggable = (GrDraggable *) i->data;
604                     // copy draggable to d_new:
605                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
606                     d_new->addDraggable (da_new);
607                 }
609                 // unlink and delete this dragger
610                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
611                 delete dragger;
613                 // update the new merged dragger
614                 d_new->fireDraggables(true, false, true);
615                 d_new->parent->updateLines();
616                 d_new->parent->setSelected (d_new);
617                 d_new->updateKnotShape ();
618                 d_new->updateTip ();
619                 d_new->updateDependencies(true);
620                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
621                                   _("Merge gradient handles"));
622                 return;
623             }
624         }
625     }
627     m.setup(desktop);
628     if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
629         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
630         if (s.getSnapped()) {
631             p = s.getPoint();
632             sp_knot_moveto (knot, p);
633         }
634     } else if (state & GDK_CONTROL_MASK) {
635         SnappedConstraints sc;
636         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
637         unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
638         /* 0 means no snapping. */
640         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
641             GrDraggable *draggable = (GrDraggable *) i->data;
643             Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
645             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
646                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
647                     GrDragger *d_new = (GrDragger *) di->data;
648                     if (d_new == dragger)
649                         continue;
650                     if (d_new->isA (draggable->item,
651                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
652                                     draggable->fill_or_stroke)) {
653                         // found the other end of the linear gradient;
654                         if (state & GDK_SHIFT_MASK) {
655                             // moving linear around center
656                             Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
657                             dr_snap = center;
658                         } else {
659                             // moving linear around the other end
660                             dr_snap = d_new->point;
661                         }
662                     }
663                 }
664             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
665                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
666                     GrDragger *d_new = (GrDragger *) di->data;
667                     if (d_new == dragger)
668                         continue;
669                     if (d_new->isA (draggable->item,
670                                     POINT_RG_CENTER,
671                                     draggable->fill_or_stroke)) {
672                         // found the center of the radial gradient;
673                         dr_snap = d_new->point;
674                     }
675                 }
676             } else if (draggable->point_type == POINT_RG_CENTER) {
677                 // radial center snaps to hor/vert relative to its original position
678                 dr_snap = dragger->point_original;
679             }
681             boost::optional<Geom::Point> snap_vector;
682             if (dr_snap.isFinite()) {
683                 if (state & GDK_MOD1_MASK) {
684                     // with Alt, snap to the original angle and its perpendiculars
685                     snap_vector = get_snap_vector (p, dr_snap, M_PI/2, Geom::atan2 (dragger->point_original - dr_snap));
686                 } else {
687                     // with Ctrl, snap to M_PI/snaps
688                     snap_vector = get_snap_vector (p, dr_snap, M_PI/snaps, 0);
689                 }
690                 if (snap_vector) {
691                     Inkscape::Snapper::ConstraintLine cl(dr_snap, p + *snap_vector - dr_snap);
692                     Inkscape::SnappedPoint s = m.constrainedSnap(Inkscape::SnapCandidatePoint(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE), cl);
693                     if (s.getSnapped()) {
694                         s.setTransformation(s.getPoint() - p);
695                         sc.points.push_back(s);
696                     } else {
697                         Inkscape::SnappedPoint dummy(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE, 0, Inkscape::SNAPTARGET_CONSTRAINED_ANGLE, Geom::L2(*snap_vector), 10000, true, true, false);
698                         dummy.setTransformation(*snap_vector);
699                         sc.points.push_back(dummy);
700                     }
701                 }
702             }
703         }
705         Inkscape::SnappedPoint bsp = m.findBestSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE), sc, true); // snap indicator will be displayed if needed
707         if (bsp.getSnapped()) {
708             p += bsp.getTransformation();
709             sp_knot_moveto (knot, p);
710         }
711     }
713     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
714     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
716     if (drag->keep_selection) {
717         Geom::Point diff = p - dragger->point;
718         drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
719     } else {
720         dragger->point = p;
721         dragger->fireDraggables (false, scale_radial);
722         dragger->updateDependencies(false);
723     }
728 static void
729 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
732     GrDrag *drag = dragger->parent;
733     // a midpoint dragger can (logically) only contain one GrDraggable
734     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
736     // get begin and end points between which dragging is allowed:
737     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
738     *moving = g_slist_append(*moving, dragger);
740     guint lowest_i = draggable->point_i;
741     guint highest_i = draggable->point_i;
742     GrDragger *lowest_dragger = dragger;
743     GrDragger *highest_dragger = dragger;
744     if (dragger->isSelected()) {
745         GrDragger* d_add;
746         while ( true )
747         {
748             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
749             if ( d_add && g_list_find(drag->selected, d_add) ) {
750                 lowest_i = lowest_i - 1;
751                 *moving = g_slist_prepend(*moving, d_add);
752                 lowest_dragger = d_add;
753             } else {
754                 break;
755             }
756         }
758         while ( true )
759         {
760             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
761             if ( d_add && g_list_find(drag->selected, d_add) ) {
762                 highest_i = highest_i + 1;
763                 *moving = g_slist_append(*moving, d_add);
764                 highest_dragger = d_add;
765             } else {
766                 break;
767             }
768         }
769     }
771     if ( SP_IS_LINEARGRADIENT(server) ) {
772         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
773         GrDragger *d_temp;
774         if (lowest_i == 1) {
775             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
776         } else {
777             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
778         }
779         if (d_temp)
780             *begin = d_temp->point;
782         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
783         if (d_temp == NULL) {
784             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
785         }
786         if (d_temp)
787             *end = d_temp->point;
788     } else if ( SP_IS_RADIALGRADIENT(server) ) {
789         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
790         GrDragger *d_temp;
791         if (lowest_i == 1) {
792             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
793         } else {
794             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
795         }
796         if (d_temp)
797             *begin = d_temp->point;
799         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
800         if (d_temp == NULL) {
801             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
802         }
803         if (d_temp)
804             *end = d_temp->point;
805     }
807     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
808     *high_lim = dragger->point - (highest_dragger->point - *end);
813 /**
814 Called when a midpoint knot is dragged.
815 */
816 static void
817 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
819     GrDragger *dragger = (GrDragger *) data;
820     GrDrag *drag = dragger->parent;
821     // a midpoint dragger can (logically) only contain one GrDraggable
822     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
824     // FIXME: take from prefs
825     double snap_fraction = 0.1;
827     Geom::Point p = ppointer;
828     Geom::Point begin(0,0), end(0,0);
829     Geom::Point low_lim(0,0), high_lim(0,0);
831     SPObject *server = draggable->getServer();
833     GSList *moving = NULL;
834     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
836     if (state & GDK_CONTROL_MASK) {
837         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
838     } else {
839         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
840         if (!(state & GDK_SHIFT_MASK)) {
841             Inkscape::Snapper::ConstraintLine cl(low_lim, high_lim - low_lim);
842             SPDesktop *desktop = dragger->parent->desktop;
843             SnapManager &m = desktop->namedview->snap_manager;
844             m.setup(desktop);
845             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
846         }
847     }
848     Geom::Point displacement = p - dragger->point;
850     for (GSList const* i = moving; i != NULL; i = i->next) {
851         GrDragger *drg = (GrDragger*) i->data;
852         SPKnot *drgknot = drg->knot;
853         Geom::Point this_move = displacement;
854         if (state & GDK_MOD1_MASK) {
855             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
856             double alpha = 1.0;
857             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
858                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
859                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
860             } else { // drg is on the begin side from dragger
861                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
862                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
863             }
864         }
865         drg->point += this_move;
866         sp_knot_moveto (drgknot, drg->point);
867         drg->fireDraggables (false);
868         drg->updateDependencies(false);
869     }
871     g_slist_free(moving);
873     drag->keep_selection = dragger->isSelected();
878 static void
879 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
881     GrDragger *dragger = (GrDragger *) data;
883     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
886 /**
887 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
888 */
889 static void
890 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
892     GrDragger *dragger = (GrDragger *) data;
894     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
896     dragger->point_original = dragger->point = knot->pos;
898     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
899         dragger->fireDraggables (true, true);
900     } else {
901         dragger->fireDraggables (true);
902     }
904     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
905         GrDragger *d = (GrDragger *) i->data;
906         if (d == dragger)
907             continue;
908         d->fireDraggables (true);
909     }
911     // make this dragger selected
912     if (!dragger->parent->keep_selection) {
913         dragger->parent->setSelected (dragger);
914     }
915     dragger->parent->keep_selection = false;
917     dragger->updateDependencies(true);
919     // we did an undoable action
920     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
921                       _("Move gradient handle"));
924 /**
925 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
926 state of the keyboard keys
927 */
928 static void
929 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
931     GrDragger *dragger = (GrDragger *) data;
932     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
933     if (!draggable) return;
935     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
936     // delete this knot from vector
937         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
938         gradient = gradient->getVector();
939         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
940             SPStop *stop = NULL;
941             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
942             case POINT_LG_BEGIN:
943             case POINT_RG_CENTER:
944                 stop = gradient->getFirstStop();
945                 {
946                     SPStop *next = stop->getNextStop();
947                     if (next) {
948                         next->offset = 0;
949                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
950                     }
951                 }
952                 break;
953             case POINT_LG_END:
954             case POINT_RG_R1:
955             case POINT_RG_R2:
956                 stop = sp_last_stop(gradient);
957                 {
958                     SPStop *prev = stop->getPrevStop();
959                     if (prev) {
960                         prev->offset = 1;
961                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
962                     }
963                 }
964                 break;
965             case POINT_LG_MID:
966             case POINT_RG_MID1:
967             case POINT_RG_MID2:
968                 stop = sp_get_stop_i(gradient, draggable->point_i);
969                 break;
970             }
972             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
973             sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
974                       _("Delete gradient stop"));
975         }
976     } else {
977     // select the dragger
978         dragger->point_original = dragger->point;
980         if ( state & GDK_SHIFT_MASK ) {
981             dragger->parent->setSelected (dragger, true, false);
982         } else {
983             dragger->parent->setSelected (dragger);
984         }
985     }
988 /**
989 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
990 */
991 static void
992 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
994     GrDragger *dragger = (GrDragger *) data;
996     dragger->point_original = dragger->point;
998     if (dragger->draggables == NULL)
999         return;
1001     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1002     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1005 /**
1006 Act upon all draggables of the dragger, setting them to the dragger's point
1007 */
1008 void
1009 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1011     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1012         GrDraggable *draggable = (GrDraggable *) i->data;
1014         // set local_change flag so that selection_changed callback does not regenerate draggers
1015         this->parent->local_change = true;
1017         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1018         // to the center, unless it's the first update upon merge when we must snap it to the point
1019         if (merging_focus ||
1020             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1021         {
1022             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1023         }
1024     }
1027 /**
1028 Checks if the dragger has a draggable with this point_type
1029  */
1030 bool
1031 GrDragger::isA (gint point_type)
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) {
1036             return true;
1037         }
1038     }
1039     return false;
1042 /**
1043 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1044  */
1045 bool
1046 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1048     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1049         GrDraggable *draggable = (GrDraggable *) i->data;
1050         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1051             return true;
1052         }
1053     }
1054     return false;
1057 /**
1058 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1059  */
1060 bool
1061 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1063     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1064         GrDraggable *draggable = (GrDraggable *) i->data;
1065         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1066             return true;
1067         }
1068     }
1069     return false;
1072 bool
1073 GrDraggable::mayMerge (GrDraggable *da2)
1075     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1076         // we must not merge the points of the same gradient!
1077         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1078               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1079             // except that we can snap center and focus together
1080             return false;
1081         }
1082     }
1083     // disable merging of midpoints.
1084     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1085          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1086          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1087         return false;
1089     return true;
1092 bool
1093 GrDragger::mayMerge (GrDragger *other)
1095     if (this == other)
1096         return false;
1098     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1099         GrDraggable *da1 = (GrDraggable *) i->data;
1100         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1101             GrDraggable *da2 = (GrDraggable *) j->data;
1102             if (!da1->mayMerge(da2))
1103                 return false;
1104         }
1105     }
1106     return true;
1109 bool
1110 GrDragger::mayMerge (GrDraggable *da2)
1112     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1113         GrDraggable *da1 = (GrDraggable *) i->data;
1114         if (!da1->mayMerge(da2))
1115             return false;
1116     }
1117     return true;
1120 /**
1121 Updates the statusbar tip of the dragger knot, based on its draggables
1122  */
1123 void
1124 GrDragger::updateTip ()
1126     if (this->knot && this->knot->tip) {
1127         g_free (this->knot->tip);
1128         this->knot->tip = NULL;
1129     }
1131     if (g_slist_length (this->draggables) == 1) {
1132         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1133         char *item_desc = sp_item_description(draggable->item);
1134         switch (draggable->point_type) {
1135             case POINT_LG_MID:
1136             case POINT_RG_MID1:
1137             case POINT_RG_MID2:
1138                 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"),
1139                                                    _(gr_knot_descr[draggable->point_type]),
1140                                                    draggable->point_i,
1141                                                    item_desc,
1142                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1143                 break;
1145             default:
1146                 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"),
1147                                                    _(gr_knot_descr[draggable->point_type]),
1148                                                    item_desc,
1149                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1150                 break;
1151         }
1152         g_free(item_desc);
1153     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1154         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1155     } else {
1156         int length = g_slist_length (this->draggables);
1157         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1158                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1159                                                     length),
1160                                            length);
1161     }
1164 /**
1165 Adds a draggable to the dragger
1166  */
1167 void
1168 GrDragger::updateKnotShape ()
1170     if (!draggables)
1171         return;
1172     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1173     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1176 /**
1177 Adds a draggable to the dragger
1178  */
1179 void
1180 GrDragger::addDraggable (GrDraggable *draggable)
1182     this->draggables = g_slist_prepend (this->draggables, draggable);
1184     this->updateTip();
1188 /**
1189 Moves this dragger to the point of the given draggable, acting upon all other draggables
1190  */
1191 void
1192 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1194     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1195     if (!dr_first) return;
1197     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1198     this->point_original = this->point;
1200     sp_knot_moveto (this->knot, this->point);
1202     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1203         GrDraggable *da = (GrDraggable *) i->data;
1204         if ( (da->item == item) &&
1205              (point_type == -1 || da->point_type == point_type) &&
1206              (point_i == -1 || da->point_i == point_i) &&
1207              (da->fill_or_stroke == fill_or_stroke) ) {
1208             continue;
1209         }
1210         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1211     }
1212     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1216 /**
1217 Moves all midstop draggables that depend on this one
1218  */
1219 void
1220 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1221     SPObject *server = draggable->getServer();
1222     if (!server)
1223         return;
1224     guint num = SP_GRADIENT(server)->vector.stops.size();
1225     if (num <= 2) return;
1227     if ( SP_IS_LINEARGRADIENT(server) ) {
1228         for ( guint i = 1; i < num - 1; i++ ) {
1229             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1230         }
1231     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1232         for ( guint i = 1; i < num - 1; i++ ) {
1233             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1234             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1235         }
1236     }
1240 /**
1241 Moves all draggables that depend on this one
1242  */
1243 void
1244 GrDragger::updateDependencies (bool write_repr)
1246     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1247         GrDraggable *draggable = (GrDraggable *) i->data;
1248         switch (draggable->point_type) {
1249             case POINT_LG_BEGIN:
1250                 {
1251                     // the end point is dependent only when dragging with ctrl+shift
1252                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1254                     this->updateMidstopDependencies (draggable, write_repr);
1255                 }
1256                 break;
1257             case POINT_LG_END:
1258                 {
1259                     // the begin point is dependent only when dragging with ctrl+shift
1260                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1262                     this->updateMidstopDependencies (draggable, write_repr);
1263                 }
1264                 break;
1265             case POINT_LG_MID:
1266                 // no other nodes depend on mid points.
1267                 break;
1268             case POINT_RG_R2:
1269                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1270                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1271                 this->updateMidstopDependencies (draggable, write_repr);
1272                 break;
1273             case POINT_RG_R1:
1274                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1275                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1276                 this->updateMidstopDependencies (draggable, write_repr);
1277                 break;
1278             case POINT_RG_CENTER:
1279                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1280                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1281                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1282                 this->updateMidstopDependencies (draggable, write_repr);
1283                 break;
1284             case POINT_RG_FOCUS:
1285                 // nothing can depend on that
1286                 break;
1287             case POINT_RG_MID1:
1288                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1289                 break;
1290             case POINT_RG_MID2:
1291                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1292                 break;
1293             default:
1294                 break;
1295         }
1296     }
1301 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1302   : point(p),
1303     point_original(p)
1305     this->draggables = NULL;
1307     this->parent = parent;
1309     // create the knot
1310     this->knot = sp_knot_new (parent->desktop, NULL);
1311     this->knot->setMode(SP_KNOT_MODE_XOR);
1312     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1313     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1314     sp_knot_update_ctrl(this->knot);
1316     // move knot to the given point
1317     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1318     sp_knot_show (this->knot);
1320     // connect knot's signals
1321     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1322                        // luckily, midstops never snap to other nodes so are never unsnapped...
1323          && ( (draggable->point_type == POINT_LG_MID)
1324               || (draggable->point_type == POINT_RG_MID1)
1325               || (draggable->point_type == POINT_RG_MID2) ) )
1326     {
1327         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1328     } else {
1329         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1330     }
1331     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1332     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1333     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1334     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1336     // add the initial draggable
1337     if (draggable)
1338         this->addDraggable (draggable);
1339     updateKnotShape();
1342 GrDragger::~GrDragger ()
1344     // unselect if it was selected
1345     this->parent->setDeselected(this);
1347     // disconnect signals
1348     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1349     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1350     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1351     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1352     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1354     /* unref should call destroy */
1355     g_object_unref (G_OBJECT (this->knot));
1357     // delete all draggables
1358     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1359         delete ((GrDraggable *) i->data);
1360     }
1361     g_slist_free (this->draggables);
1362     this->draggables = NULL;
1365 /**
1366 Select the dragger which has the given draggable.
1367 */
1368 GrDragger *
1369 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1371     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1372         GrDragger *dragger = (GrDragger *) i->data;
1373         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1374             GrDraggable *da2 = (GrDraggable *) j->data;
1375             if ( (da2->item == item) &&
1376                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1377                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1378                  (da2->fill_or_stroke == fill_or_stroke)) {
1379                 return (dragger);
1380             }
1381         }
1382     }
1383     return NULL;
1387 void
1388 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1390     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1391     if (d && d !=  this) {
1392         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1393     }
1397 /**
1398   Draw this dragger as selected
1399 */
1400 void
1401 GrDragger::select()
1403     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1404     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1407 /**
1408   Draw this dragger as normal (deselected)
1409 */
1410 void
1411 GrDragger::deselect()
1413     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1414     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1417 bool
1418 GrDragger::isSelected()
1420     return g_list_find (parent->selected, this);
1423 /**
1424 \brief Deselect all stops/draggers (private)
1425 */
1426 void
1427 GrDrag::deselect_all()
1429     while (selected) {
1430         ( (GrDragger*) selected->data)->deselect();
1431         selected = g_list_remove(selected, selected->data);
1432     }
1435 /**
1436 \brief Deselect all stops/draggers (public; emits signal)
1437 */
1438 void
1439 GrDrag::deselectAll()
1441     deselect_all();
1442     this->desktop->emitToolSubselectionChanged(NULL);
1445 /**
1446 \brief Select all stops/draggers
1447 */
1448 void
1449 GrDrag::selectAll()
1451     for (GList *l = this->draggers; l != NULL; l = l->next) {
1452         GrDragger *d = ((GrDragger *) l->data);
1453         setSelected (d, true, true);
1454     }
1457 /**
1458 \brief Select all stops/draggers that match the coords
1459 */
1460 void
1461 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1463     for (GList *l = this->draggers; l != NULL; l = l->next) {
1464         GrDragger *d = ((GrDragger *) l->data);
1465         for (guint k = 0; k < coords.size(); k++) {
1466             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1467                 setSelected (d, true, true);
1468             }
1469         }
1470     }
1474 /**
1475 \brief Select all stops/draggers that fall within the rect
1476 */
1477 void
1478 GrDrag::selectRect(Geom::Rect const &r)
1480     for (GList *l = this->draggers; l != NULL; l = l->next) {
1481         GrDragger *d = ((GrDragger *) l->data);
1482         if (r.contains(d->point)) {
1483            setSelected (d, true, true);
1484         }
1485     }
1488 /**
1489 \brief Select a dragger
1490 \param dragger       The dragger to select
1491 \param add_to_selection   If true, add to selection, otherwise deselect others
1492 \param override      If true, always select this node, otherwise toggle selected status
1493 */
1494 void
1495 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1497     GrDragger *seldragger = NULL;
1499     if (add_to_selection) {
1500         if (!dragger) return;
1501         if (override) {
1502             if (!g_list_find(selected, dragger)) {
1503                 selected = g_list_prepend(selected, dragger);
1504             }
1505             dragger->select();
1506             seldragger = dragger;
1507         } else { // toggle
1508             if (g_list_find(selected, dragger)) {
1509                 selected = g_list_remove(selected, dragger);
1510                 dragger->deselect();
1511                 if (selected) {
1512                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1513                 }
1514             } else {
1515                 selected = g_list_prepend(selected, dragger);
1516                 dragger->select();
1517                 seldragger = dragger;
1518             }
1519         }
1520     } else {
1521         deselect_all();
1522         if (dragger) {
1523             selected = g_list_prepend(selected, dragger);
1524             dragger->select();
1525             seldragger = dragger;
1526         }
1527     }
1528     if (seldragger) {
1529         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1530     }
1533 /**
1534 \brief Deselect a dragger
1535 \param dragger       The dragger to deselect
1536 */
1537 void
1538 GrDrag::setDeselected (GrDragger *dragger)
1540     if (g_list_find(selected, dragger)) {
1541         selected = g_list_remove(selected, dragger);
1542         dragger->deselect();
1543     }
1544     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1549 /**
1550 Create a line from p1 to p2 and add it to the lines list
1551  */
1552 void
1553 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1555     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1556                                                             SP_TYPE_CTRLLINE, NULL);
1557     sp_canvas_item_move_to_z(line, 0);
1558     SP_CTRLLINE(line)->item = item;
1559     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1560     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1561         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1562     sp_canvas_item_show (line);
1563     this->lines = g_slist_append (this->lines, line);
1566 /**
1567 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1568 new dragger and add it to draggers list
1569  */
1570 void
1571 GrDrag::addDragger (GrDraggable *draggable)
1573     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1575     for (GList *i = this->draggers; i != NULL; i = i->next) {
1576         GrDragger *dragger = (GrDragger *) i->data;
1577         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1578             // distance is small, merge this draggable into dragger, no need to create new dragger
1579             dragger->addDraggable (draggable);
1580             dragger->updateKnotShape();
1581             return;
1582         }
1583     }
1585     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1586     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1587     this->draggers = g_list_append (this->draggers, new_dragger);
1590 /**
1591 Add draggers for the radial gradient rg on item
1592 */
1593 void
1594 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1596     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1597     guint num = rg->vector.stops.size();
1598     if (num > 2) {
1599         for ( guint i = 1; i < num - 1; i++ ) {
1600             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1601         }
1602     }
1603     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1604     if (num > 2) {
1605         for ( guint i = 1; i < num - 1; i++ ) {
1606             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1607         }
1608     }
1609     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1610     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1613 /**
1614 Add draggers for the linear gradient lg on item
1615 */
1616 void
1617 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1619     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1620     guint num = lg->vector.stops.size();
1621     if (num > 2) {
1622         for ( guint i = 1; i < num - 1; i++ ) {
1623             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1624         }
1625     }
1626     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1629 /**
1630 Artificially grab the knot of this dragger; used by the gradient context
1631 */
1632 void
1633 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1635     if (dragger) {
1636         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1637     }
1640 /**
1641 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1642 */
1643 void
1644 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1646     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1647     if (dragger) {
1648         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1649     }
1652 /**
1653 Regenerates the draggers list from the current selection; is called when selection is changed or
1654 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1655 */
1656 void GrDrag::updateDraggers ()
1658     while (selected) {
1659         selected = g_list_remove(selected, selected->data);
1660     }
1661     // delete old draggers
1662     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1663         delete static_cast<GrDragger *>(i->data);
1664     }
1665     g_list_free(this->draggers);
1666     this->draggers = NULL;
1668     g_return_if_fail(this->selection != NULL);
1670     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1671         SPItem *item = SP_ITEM(i->data);
1672         SPStyle *style = item->style;
1674         if (style && (style->fill.isPaintserver())) {
1675             SPPaintServer *server = style->getFillPaintServer();
1676             if ( server && server->isSolid() ) {
1677                 // Suppress "gradientness" of solid paint
1678             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1679                 addDraggersLinear( SP_LINEARGRADIENT(server), item, true );
1680             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1681                 addDraggersRadial( SP_RADIALGRADIENT(server), item, true );
1682             }
1683         }
1685         if (style && (style->stroke.isPaintserver())) {
1686             SPPaintServer *server = style->getStrokePaintServer();
1687             if ( server && server->isSolid() ) {
1688                 // Suppress "gradientness" of solid paint
1689             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1690                 addDraggersLinear( SP_LINEARGRADIENT(server), item, false );
1691             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1692                 addDraggersRadial( SP_RADIALGRADIENT(server), item, false );
1693             }
1694         }
1695     }
1699 /**
1700  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1701  */
1703 bool
1704 GrDrag::mouseOver()
1706     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1707         GrDragger *d = (GrDragger *) i->data;
1708         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1709             return true;
1710         }
1711     }
1712     return false;
1714 /**
1715 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1716 lines are always in sync with the actual gradient
1717 */
1718 void
1719 GrDrag::updateLines ()
1721     // delete old lines
1722     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1723         gtk_object_destroy( GTK_OBJECT (i->data));
1724     }
1725     g_slist_free (this->lines);
1726     this->lines = NULL;
1728     g_return_if_fail (this->selection != NULL);
1730     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1732         SPItem *item = SP_ITEM(i->data);
1734         SPStyle *style = SP_OBJECT_STYLE (item);
1736         if (style && (style->fill.isPaintserver())) {
1737             SPPaintServer *server = item->style->getFillPaintServer();
1738             if ( server && server->isSolid() ) {
1739                 // Suppress "gradientness" of solid paint
1740             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1741                 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);
1742             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1743                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1744                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1745                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1746             }
1747         }
1749         if (style && (style->stroke.isPaintserver())) {
1750             SPPaintServer *server = item->style->getStrokePaintServer();
1751             if ( server && server->isSolid() ) {
1752                 // Suppress "gradientness" of solid paint
1753             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1754                 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);
1755             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1756                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1757                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1758                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1759             }
1760         }
1761     }
1764 /**
1765 Regenerates the levels list from the current selection
1766 */
1767 void
1768 GrDrag::updateLevels ()
1770     hor_levels.clear();
1771     vert_levels.clear();
1773     g_return_if_fail (this->selection != NULL);
1775     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1776         SPItem *item = SP_ITEM(i->data);
1777         Geom::OptRect rect = sp_item_bbox_desktop (item);
1778         if (rect) {
1779             // Remember the edges of the bbox and the center axis
1780             hor_levels.push_back(rect->min()[Geom::Y]);
1781             hor_levels.push_back(rect->max()[Geom::Y]);
1782             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1783             vert_levels.push_back(rect->min()[Geom::X]);
1784             vert_levels.push_back(rect->max()[Geom::X]);
1785             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1786         }
1787     }
1790 void
1791 GrDrag::selected_reverse_vector ()
1793     if (selected == NULL)
1794         return;
1796     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1797         GrDraggable *draggable = (GrDraggable *) i->data;
1799         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1800     }
1803 void
1804 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1806     selected_move (x, y, false, scale_radial);
1809 void
1810 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1812     if (selected == NULL)
1813         return;
1815     bool did = false;
1817     for (GList *i = selected; i != NULL; i = i->next) {
1818         GrDragger *d = (GrDragger *) i->data;
1820         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1821             // if this is an endpoint,
1823             // Moving an rg center moves its focus and radii as well.
1824             // therefore, if this is a focus or radius and if selection
1825             // contains the center as well, do not move this one
1826             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1827                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1828                 bool skip_radius_with_center = false;
1829                 for (GList *di = selected; di != NULL; di = di->next) {
1830                     GrDragger *d_new = (GrDragger *) di->data;
1831                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1832                                     POINT_RG_CENTER,
1833                                     0,
1834                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1835                         // FIXME: here we take into account only the first draggable!
1836                         skip_radius_with_center = true;
1837                     }
1838                 }
1839                 if (skip_radius_with_center)
1840                     continue;
1841             }
1843             did = true;
1844             d->point += Geom::Point (x, y);
1845             d->point_original = d->point;
1846             sp_knot_moveto (d->knot, d->point);
1848             d->fireDraggables (write_repr, scale_radial);
1850             d->updateDependencies(write_repr);
1851         }
1852     }
1854     if (write_repr && did) {
1855         // we did an undoable action
1856         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1857                                 _("Move gradient handle(s)"));
1858         return;
1859     }
1861     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1863         GrDragger *dragger = (GrDragger *) selected->data;
1864         // a midpoint dragger can (logically) only contain one GrDraggable
1865         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1867         Geom::Point begin(0,0), end(0,0);
1868         Geom::Point low_lim(0,0), high_lim(0,0);
1870         SPObject *server = draggable->getServer();
1871         GSList *moving = NULL;
1872         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1874         Geom::Point p(x, y);
1875         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1876         Geom::Point displacement = p - dragger->point;
1878         for (GSList const* i = moving; i != NULL; i = i->next) {
1879             GrDragger *drg = (GrDragger*) i->data;
1880             SPKnot *drgknot = drg->knot;
1881             drg->point += displacement;
1882             sp_knot_moveto (drgknot, drg->point);
1883             drg->fireDraggables (true);
1884             drg->updateDependencies(true);
1885             did = true;
1886         }
1888         g_slist_free(moving);
1890         if (write_repr && did) {
1891             // we did an undoable action
1892             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1893                                     _("Move gradient mid stop(s)"));
1894         }
1895     }
1898 void
1899 GrDrag::selected_move_screen (double x, double y)
1901     gdouble zoom = desktop->current_zoom();
1902     gdouble zx = x / zoom;
1903     gdouble zy = y / zoom;
1905     selected_move (zx, zy);
1908 /**
1909 Select the knot next to the last selected one and deselect all other selected.
1910 */
1911 GrDragger *
1912 GrDrag::select_next ()
1914     GrDragger *d = NULL;
1915     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1916         if (draggers)
1917             d = (GrDragger *) draggers->data;
1918     } else {
1919         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1920     }
1921     if (d)
1922         setSelected (d);
1923     return d;
1926 /**
1927 Select the knot previous from the last selected one and deselect all other selected.
1928 */
1929 GrDragger *
1930 GrDrag::select_prev ()
1932     GrDragger *d = NULL;
1933     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1934         if (draggers)
1935             d = (GrDragger *) g_list_last (draggers)->data;
1936     } else {
1937         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1938     }
1939     if (d)
1940         setSelected (d);
1941     return d;
1945 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1946 void
1947 GrDrag::deleteSelected (bool just_one)
1949     if (!selected) return;
1951     SPDocument *document = false;
1953     struct StructStopInfo {
1954         SPStop * spstop;
1955         GrDraggable * draggable;
1956         SPGradient * gradient;
1957         SPGradient * vector;
1958     };
1960     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1961     GSList *endstoplist = NULL;  // list of stops that must be deleted
1962     while (selected) {
1963         GrDragger *dragger = (GrDragger*) selected->data;
1964         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1965             GrDraggable *draggable = (GrDraggable*) drgble->data;
1966             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1967             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1969             switch (draggable->point_type) {
1970                 case POINT_LG_MID:
1971                 case POINT_RG_MID1:
1972                 case POINT_RG_MID2:
1973                     {
1974                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1975                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1976                         bool present = false;
1977                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1978                             if ( (SPStop*)l->data == stop ) {
1979                                 present = true;
1980                                 break; // no need to search further.
1981                             }
1982                         }
1983                         if (!present)
1984                             midstoplist = g_slist_append(midstoplist, stop);
1985                     }
1986                     break;
1987                 case POINT_LG_BEGIN:
1988                 case POINT_LG_END:
1989                 case POINT_RG_CENTER:
1990                 case POINT_RG_R1:
1991                 case POINT_RG_R2:
1992                     {
1993                         SPStop *stop = NULL;
1994                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1995                             stop = vector->getFirstStop();
1996                         } else {
1997                             stop = sp_last_stop(vector);
1998                         }
1999                         if (stop) {
2000                             StructStopInfo *stopinfo = new StructStopInfo;
2001                             stopinfo->spstop = stop;
2002                             stopinfo->draggable = draggable;
2003                             stopinfo->gradient = gradient;
2004                             stopinfo->vector = vector;
2005                             // check if already present in list. (e.g. when both R1 and R2 were selected)
2006                             bool present = false;
2007                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
2008                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
2009                                     present = true;
2010                                     break; // no need to search further.
2011                                 }
2012                             }
2013                             if (!present)
2014                                 endstoplist = g_slist_append(endstoplist, stopinfo);
2015                         }
2016                     }
2017                     break;
2018                 default:
2019                     break;
2020             }
2021         }
2022         selected = g_list_remove(selected, dragger);
2023         if ( just_one ) break; // iterate once if just_one is set.
2024     }
2025     while (midstoplist) {
2026         SPStop *stop = (SPStop*) midstoplist->data;
2027         document = SP_OBJECT_DOCUMENT (stop);
2028         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
2029         parent->removeChild(SP_OBJECT_REPR(stop));
2030         midstoplist = g_slist_remove(midstoplist, stop);
2031     }
2032     while (endstoplist) {
2033         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
2034         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2036         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2037         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2038         // manually count the children, don't know if there already exists a function for this...
2039         int len = 0;
2040         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2041               child != NULL ;
2042               child = SP_OBJECT_NEXT(child) )
2043         {
2044             if ( SP_IS_STOP(child) )  len ++;
2045         }
2046         if (len > 2)
2047         {
2048             switch (stopinfo->draggable->point_type) {
2049                 case POINT_LG_BEGIN:
2050                     {
2051                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2053                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2054                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2055                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2056                         SPStop *stop = stopinfo->vector->getFirstStop();
2057                         gdouble offset = stop->offset;
2058                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2059                         lg->x1.computed = newbegin[Geom::X];
2060                         lg->y1.computed = newbegin[Geom::Y];
2062                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2063                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2064                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2065                         stop->offset = 0;
2066                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2068                         // iterate through midstops to set new offset values such that they won't move on canvas.
2069                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2070                         stop = stop->getNextStop();
2071                         while ( stop != laststop ) {
2072                             stop->offset = (stop->offset - offset)/(1 - offset);
2073                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2074                             stop = stop->getNextStop();
2075                         }
2076                     }
2077                     break;
2078                 case POINT_LG_END:
2079                     {
2080                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2082                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2083                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2084                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2085                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2086                         gdouble offset = laststop->offset;
2087                         Geom::Point newend = begin + offset * (oldend - begin);
2088                         lg->x2.computed = newend[Geom::X];
2089                         lg->y2.computed = newend[Geom::Y];
2091                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2092                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2093                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2094                         laststop->offset = 1;
2095                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2097                         // iterate through midstops to set new offset values such that they won't move on canvas.
2098                         SPStop *stop = stopinfo->vector->getFirstStop();
2099                         stop = stop->getNextStop();
2100                         while ( stop != laststop ) {
2101                             stop->offset = stop->offset / offset;
2102                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2103                             stop = stop->getNextStop();
2104                         }
2105                     }
2106                     break;
2107                 case POINT_RG_CENTER:
2108                     {
2109                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2110                         if (newfirst) {
2111                             newfirst->offset = 0;
2112                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2113                         }
2114                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2115                     }
2116                     break;
2117                 case POINT_RG_R1:
2118                 case POINT_RG_R2:
2119                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2121                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2122                         double oldradius = rg->r.computed;
2123                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2124                         gdouble offset = laststop->offset;
2125                         double newradius = offset * oldradius;
2126                         rg->r.computed = newradius;
2128                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2129                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2130                         laststop->offset = 1;
2131                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2133                         // iterate through midstops to set new offset values such that they won't move on canvas.
2134                         SPStop *stop = stopinfo->vector->getFirstStop();
2135                         stop = stop->getNextStop();
2136                         while ( stop != laststop ) {
2137                             stop->offset = stop->offset / offset;
2138                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2139                             stop = stop->getNextStop();
2140                         }
2141                         break;
2142             }
2143         }
2144         else
2145         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2146             SPCSSAttr *css = sp_repr_css_attr_new ();
2148             // stopinfo->spstop is the selected stop
2149             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2150             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2151                 unselectedrepr = unselectedrepr->next();
2152             }
2154             if (unselectedrepr == NULL) {
2155                 if (stopinfo->draggable->fill_or_stroke) {
2156                     sp_repr_css_unset_property (css, "fill");
2157                 } else {
2158                     sp_repr_css_unset_property (css, "stroke");
2159                 }
2160             } else {
2161                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2162                 if (stopinfo->draggable->fill_or_stroke) {
2163                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2164                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2165                 } else {
2166                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2167                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2168                 }
2169                 sp_repr_css_attr_unref (stopcss);
2170             }
2172             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2173             sp_repr_css_attr_unref (css);
2174         }
2176         endstoplist = g_slist_remove(endstoplist, stopinfo);
2177         delete stopinfo;
2178     }
2180     if (document) {
2181         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2182     }
2186 /*
2187   Local Variables:
2188   mode:c++
2189   c-file-style:"stroustrup"
2190   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2191   indent-tabs-mode:nil
2192   fill-column:99
2193   End:
2194 */
2195 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :