Code

Follow-up to complete fix for gradient stops and icc.
[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 Glib::ustring GrDrag::makeStopSafeColor( gchar const *str, bool &isNull )
181     Glib::ustring colorStr;
182     if ( str ) {
183         isNull = false;
184         colorStr = str;
185         Glib::ustring::size_type pos = colorStr.find("url(#");
186         if ( pos != Glib::ustring::npos ) {
187             Glib::ustring targetName = colorStr.substr(pos + 5, colorStr.length() - 6);
188             const GSList *gradients = sp_document_get_resource_list(desktop->doc(), "gradient");
189             for (const GSList *item = gradients; item; item = item->next) {
190                 SPGradient* grad = SP_GRADIENT(item->data);
191                 if ( targetName == grad->getId() ) {
192                     SPGradient *vect = grad->getVector();
193                     SPStop *firstStop = (vect) ? vect->getFirstStop() : grad->getFirstStop();
194                     if (firstStop) {
195                         Glib::ustring stopColorStr;
196                         if (firstStop->currentColor) {
197                             stopColorStr = sp_object_get_style_property(firstStop, "color", NULL);
198                         } else {
199                             stopColorStr = firstStop->specified_color.toString();
200                         }
201                         if ( !stopColorStr.empty() ) {
202                             colorStr = stopColorStr;
203                         }
204                     }
205                     break;
206                 }
207             }
208         }
209     } else {
210         isNull = true;
211     }
213     return colorStr;
216 bool GrDrag::styleSet( const SPCSSAttr *css )
218     if (!selected) {
219         return false;
220     }
222     SPCSSAttr *stop = sp_repr_css_attr_new();
224     // See if the css contains interesting properties, and if so, translate them into the format
225     // acceptable for gradient stops
227     // any of color properties, in order of increasing priority:
228     if (css->attribute("flood-color")) {
229         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
230     }
232     if (css->attribute("lighting-color")) {
233         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
234     }
236     if (css->attribute("color")) {
237         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
238     }
240     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none")) {
241         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
242     }
244     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none")) {
245         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
246     }
248     if (css->attribute("stop-color")) {
249         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
250     }
252     // Make sure the style is allowed for gradient stops.
253     if ( !sp_repr_css_property_is_unset( stop, "stop-color") ) {
254         bool stopIsNull = false;
255         Glib::ustring tmp = makeStopSafeColor( sp_repr_css_property( stop, "stop-color", "" ), stopIsNull );
256         if ( !stopIsNull && !tmp.empty() ) {
257             sp_repr_css_set_property( stop, "stop-color", tmp.c_str() );
258         }
259     }
262     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
263         sp_repr_css_set_property(stop, "stop-opacity", css->attribute("stop-opacity"));
264     } else {  // multiply all opacity properties:
265         gdouble accumulated = 1.0;
266         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
267         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
268         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
269         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
271         Inkscape::CSSOStringStream os;
272         os << accumulated;
273         sp_repr_css_set_property(stop, "stop-opacity", os.str().c_str());
275         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
276             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none"))) {
277             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
278         }
279     }
281     if (!stop->attributeList()) { // nothing for us here, pass it on
282         sp_repr_css_attr_unref(stop);
283         return false;
284     }
286     for (GList const* sel = selected; sel != NULL; sel = sel->next) { // for all selected draggers
287         GrDragger* dragger = reinterpret_cast<GrDragger*>(sel->data);
288         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
289             GrDraggable *draggable = reinterpret_cast<GrDraggable *>(i->data);
291             local_change = true;
292             sp_item_gradient_stop_set_style(draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
293         }
294     }
296     //sp_repr_css_print(stop);
297     sp_repr_css_attr_unref(stop);
298     return true;
301 guint32 GrDrag::getColor()
303     if (!selected) return 0;
305     float cf[4];
306     cf[0] = cf[1] = cf[2] = cf[3] = 0;
308     int count = 0;
310     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
311         GrDragger *d = (GrDragger *) i->data;
312         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
313             GrDraggable *draggable = (GrDraggable *) j->data;
315             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
316             cf[0] += SP_RGBA32_R_F (c);
317             cf[1] += SP_RGBA32_G_F (c);
318             cf[2] += SP_RGBA32_B_F (c);
319             cf[3] += SP_RGBA32_A_F (c);
321             count ++;
322         }
323     }
325     if (count) {
326         cf[0] /= count;
327         cf[1] /= count;
328         cf[2] /= count;
329         cf[3] /= count;
330     }
332     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
335 SPStop *
336 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
338     gfloat offset; // type of SPStop.offset = gfloat
339     SPGradient *gradient;
340     bool fill_or_stroke = true;
341     bool r1_knot = false;
343     bool addknot = false;
344     do {
345         gradient = sp_item_gradient (item, fill_or_stroke);
346         if (SP_IS_LINEARGRADIENT(gradient)) {
347             Geom::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
348             Geom::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
350             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
351             double dist_screen = Geom::L2 (mouse_p - nearest);
352             if ( dist_screen < tolerance ) {
353                 // add the knot
354                 offset = get_offset_between_points(nearest, begin, end);
355                 addknot = true;
356                 break; // break out of the while loop: add only one knot
357             }
358         } else if (SP_IS_RADIALGRADIENT(gradient)) {
359             Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
360             Geom::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
361             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
362             double dist_screen = Geom::L2 (mouse_p - nearest);
363             if ( dist_screen < tolerance ) {
364                 offset = get_offset_between_points(nearest, begin, end);
365                 addknot = true;
366                 r1_knot = true;
367                 break; // break out of the while loop: add only one knot
368             }
370             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
371             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
372             dist_screen = Geom::L2 (mouse_p - nearest);
373             if ( dist_screen < tolerance ) {
374                 offset = get_offset_between_points(nearest, begin, end);
375                 addknot = true;
376                 r1_knot = false;
377                 break; // break out of the while loop: add only one knot
378             }
379         }
380         fill_or_stroke = !fill_or_stroke;
381     } while (!fill_or_stroke && !addknot) ;
383     if (addknot) {
384         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
385         SPStop* prev_stop = vector->getFirstStop();
386         SPStop* next_stop = prev_stop->getNextStop();
387         guint i = 1;
388         while ( (next_stop) && (next_stop->offset < offset) ) {
389             prev_stop = next_stop;
390             next_stop = next_stop->getNextStop();
391             i++;
392         }
393         if (!next_stop) {
394             // logical error: the endstop should have offset 1 and should always be more than this offset here
395             return NULL;
396         }
399         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
400         gradient->ensureVector();
401         updateDraggers();
403         return newstop;
404     }
406     return NULL;
410 bool
411 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
413     // Note: not sure if a null pointer can come in for the style, but handle that just in case
414     bool stopIsNull = false;
415     Glib::ustring toUse = makeStopSafeColor( c, stopIsNull );
417     // first, see if we can drop onto one of the existing draggers
418     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
419         GrDragger *d = (GrDragger *) i->data;
421         if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
422            SPCSSAttr *stop = sp_repr_css_attr_new ();
423            sp_repr_css_set_property( stop, "stop-color", stopIsNull ? 0 : toUse.c_str() );
424            sp_repr_css_set_property( stop, "stop-opacity", "1" );
425            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
426                GrDraggable *draggable = (GrDraggable *) j->data;
427                local_change = true;
428                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
429            }
430            sp_repr_css_attr_unref(stop);
431            return true;
432         }
433     }
435     // now see if we're over line and create a new stop
436     bool over_line = false;
437     SPCtrlLine *line = NULL;
438     if (lines) {
439         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
440             line = (SPCtrlLine*) l->data;
441             Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
442             double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
443             if (line->item && dist_screen < 5) {
444                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
445                 if (stop) {
446                     SPCSSAttr *css = sp_repr_css_attr_new ();
447                     sp_repr_css_set_property( css, "stop-color", stopIsNull ? 0 : toUse.c_str() );
448                     sp_repr_css_set_property( css, "stop-opacity", "1" );
449                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
450                     return true;
451                 }
452             }
453         }
454     }
456     return false;
460 GrDrag::GrDrag(SPDesktop *desktop) :
461     selected(0),
462     keep_selection(false),
463     local_change(false),
464     desktop(desktop),
465     hor_levels(),
466     vert_levels(),
467     draggers(0),
468     lines(0),
469     selection(sp_desktop_selection(desktop)),
470     sel_changed_connection(),
471     sel_modified_connection(),
472     style_set_connection(),
473     style_query_connection()
475     sel_changed_connection = selection->connectChanged(
476         sigc::bind(
477             sigc::ptr_fun(&gr_drag_sel_changed),
478             (gpointer)this )
480         );
481     sel_modified_connection = selection->connectModified(
482         sigc::bind(
483             sigc::ptr_fun(&gr_drag_sel_modified),
484             (gpointer)this )
485         );
487     style_set_connection = desktop->connectSetStyle( sigc::mem_fun(*this, &GrDrag::styleSet) );
489     style_query_connection = desktop->connectQueryStyle(
490         sigc::bind(
491             sigc::ptr_fun(&gr_drag_style_query),
492             (gpointer)this )
493         );
495     updateDraggers();
496     updateLines();
497     updateLevels();
499     if (desktop->gr_item) {
500         setSelected(getDraggerFor(desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
501     }
504 GrDrag::~GrDrag()
506     this->sel_changed_connection.disconnect();
507     this->sel_modified_connection.disconnect();
508     this->style_set_connection.disconnect();
509     this->style_query_connection.disconnect();
511     if (this->selected) {
512         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
513         desktop->gr_item = draggable->item;
514         desktop->gr_point_type = draggable->point_type;
515         desktop->gr_point_i = draggable->point_i;
516         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
517     } else {
518         desktop->gr_item = NULL;
519         desktop->gr_point_type = 0;
520         desktop->gr_point_i = 0;
521         desktop->gr_fill_or_stroke = true;
522     }
524     deselect_all();
525     for (GList *l = this->draggers; l != NULL; l = l->next) {
526         delete ((GrDragger *) l->data);
527     }
528     g_list_free (this->draggers);
529     this->draggers = NULL;
530     this->selected = NULL;
532     for (GSList *l = this->lines; l != NULL; l = l->next) {
533         gtk_object_destroy( GTK_OBJECT (l->data));
534     }
535     g_slist_free (this->lines);
536     this->lines = NULL;
539 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
541     this->item = item;
542     this->point_type = point_type;
543     this->point_i = point_i;
544     this->fill_or_stroke = fill_or_stroke;
546     g_object_ref (G_OBJECT (this->item));
549 GrDraggable::~GrDraggable ()
551     g_object_unref (G_OBJECT (this->item));
555 SPObject *GrDraggable::getServer()
557     if (!item) {
558         return NULL;
559     }
561     SPObject *server = NULL;
562     if (fill_or_stroke) {
563         server = item->style->getFillPaintServer();
564     }else {
565         server = item->style->getStrokePaintServer();
566     }
568     return server;
571 static
572 boost::optional<Geom::Point>
573 get_snap_vector (Geom::Point p, Geom::Point o, double snap, double initial)
575     double r = L2 (p - o);
576     if (r < 1e-3) {
577         return boost::optional<Geom::Point>();
578     }
580     double angle = atan2 (p - o);
581     // snap angle to snaps increments, starting from initial:
582     double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
583     // calculate the new position and subtract p to get the vector:
584     return (o + r * Geom::Point(cos(a_snapped), sin(a_snapped)) - p);
587 static void
588 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
590     GrDragger *dragger = (GrDragger *) data;
591     GrDrag *drag = dragger->parent;
593     Geom::Point p = ppointer;
595     SPDesktop *desktop = dragger->parent->desktop;
596     SnapManager &m = desktop->namedview->snap_manager;
597     double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom();
599     if (state & GDK_SHIFT_MASK) {
600         // with Shift; unsnap if we carry more than one draggable
601         if (dragger->draggables && dragger->draggables->next) {
602             // create a new dragger
603             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
604             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
605             // relink to it all but the first draggable in the list
606             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
607                 GrDraggable *draggable = (GrDraggable *) i->data;
608                 dr_new->addDraggable (draggable);
609             }
610             dr_new->updateKnotShape();
611             g_slist_free (dragger->draggables->next);
612             dragger->draggables->next = NULL;
613             dragger->updateKnotShape();
614             dragger->updateTip();
615         }
616     } else if (!(state & GDK_CONTROL_MASK)) {
617         // without Shift or Ctrl; see if we need to snap to another dragger
618         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
619             GrDragger *d_new = (GrDragger *) di->data;
620             if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
622                 // Merge draggers:
623                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
624                     GrDraggable *draggable = (GrDraggable *) i->data;
625                     // copy draggable to d_new:
626                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
627                     d_new->addDraggable (da_new);
628                 }
630                 // unlink and delete this dragger
631                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
632                 delete dragger;
634                 // update the new merged dragger
635                 d_new->fireDraggables(true, false, true);
636                 d_new->parent->updateLines();
637                 d_new->parent->setSelected (d_new);
638                 d_new->updateKnotShape ();
639                 d_new->updateTip ();
640                 d_new->updateDependencies(true);
641                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
642                                   _("Merge gradient handles"));
643                 return;
644             }
645         }
646     }
648     m.setup(desktop);
649     if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
650         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
651         if (s.getSnapped()) {
652             p = s.getPoint();
653             sp_knot_moveto (knot, p);
654         }
655     } else if (state & GDK_CONTROL_MASK) {
656         SnappedConstraints sc;
657         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
658         unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
659         /* 0 means no snapping. */
661         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
662             GrDraggable *draggable = (GrDraggable *) i->data;
664             Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
666             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
667                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
668                     GrDragger *d_new = (GrDragger *) di->data;
669                     if (d_new == dragger)
670                         continue;
671                     if (d_new->isA (draggable->item,
672                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
673                                     draggable->fill_or_stroke)) {
674                         // found the other end of the linear gradient;
675                         if (state & GDK_SHIFT_MASK) {
676                             // moving linear around center
677                             Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
678                             dr_snap = center;
679                         } else {
680                             // moving linear around the other end
681                             dr_snap = d_new->point;
682                         }
683                     }
684                 }
685             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
686                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
687                     GrDragger *d_new = (GrDragger *) di->data;
688                     if (d_new == dragger)
689                         continue;
690                     if (d_new->isA (draggable->item,
691                                     POINT_RG_CENTER,
692                                     draggable->fill_or_stroke)) {
693                         // found the center of the radial gradient;
694                         dr_snap = d_new->point;
695                     }
696                 }
697             } else if (draggable->point_type == POINT_RG_CENTER) {
698                 // radial center snaps to hor/vert relative to its original position
699                 dr_snap = dragger->point_original;
700             }
702             boost::optional<Geom::Point> snap_vector;
703             if (dr_snap.isFinite()) {
704                 if (state & GDK_MOD1_MASK) {
705                     // with Alt, snap to the original angle and its perpendiculars
706                     snap_vector = get_snap_vector (p, dr_snap, M_PI/2, Geom::atan2 (dragger->point_original - dr_snap));
707                 } else {
708                     // with Ctrl, snap to M_PI/snaps
709                     snap_vector = get_snap_vector (p, dr_snap, M_PI/snaps, 0);
710                 }
711                 if (snap_vector) {
712                     Inkscape::Snapper::ConstraintLine cl(dr_snap, p + *snap_vector - dr_snap);
713                     Inkscape::SnappedPoint s = m.constrainedSnap(Inkscape::SnapCandidatePoint(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE), cl);
714                     if (s.getSnapped()) {
715                         s.setTransformation(s.getPoint() - p);
716                         sc.points.push_back(s);
717                     } else {
718                         Inkscape::SnappedPoint dummy(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE, 0, Inkscape::SNAPTARGET_CONSTRAINED_ANGLE, Geom::L2(*snap_vector), 10000, true, true, false);
719                         dummy.setTransformation(*snap_vector);
720                         sc.points.push_back(dummy);
721                     }
722                 }
723             }
724         }
726         Inkscape::SnappedPoint bsp = m.findBestSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE), sc, true); // snap indicator will be displayed if needed
728         if (bsp.getSnapped()) {
729             p += bsp.getTransformation();
730             sp_knot_moveto (knot, p);
731         }
732     }
734     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
735     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
737     if (drag->keep_selection) {
738         Geom::Point diff = p - dragger->point;
739         drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
740     } else {
741         dragger->point = p;
742         dragger->fireDraggables (false, scale_radial);
743         dragger->updateDependencies(false);
744     }
749 static void
750 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
753     GrDrag *drag = dragger->parent;
754     // a midpoint dragger can (logically) only contain one GrDraggable
755     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
757     // get begin and end points between which dragging is allowed:
758     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
759     *moving = g_slist_append(*moving, dragger);
761     guint lowest_i = draggable->point_i;
762     guint highest_i = draggable->point_i;
763     GrDragger *lowest_dragger = dragger;
764     GrDragger *highest_dragger = dragger;
765     if (dragger->isSelected()) {
766         GrDragger* d_add;
767         while ( true )
768         {
769             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
770             if ( d_add && g_list_find(drag->selected, d_add) ) {
771                 lowest_i = lowest_i - 1;
772                 *moving = g_slist_prepend(*moving, d_add);
773                 lowest_dragger = d_add;
774             } else {
775                 break;
776             }
777         }
779         while ( true )
780         {
781             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
782             if ( d_add && g_list_find(drag->selected, d_add) ) {
783                 highest_i = highest_i + 1;
784                 *moving = g_slist_append(*moving, d_add);
785                 highest_dragger = d_add;
786             } else {
787                 break;
788             }
789         }
790     }
792     if ( SP_IS_LINEARGRADIENT(server) ) {
793         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
794         GrDragger *d_temp;
795         if (lowest_i == 1) {
796             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
797         } else {
798             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
799         }
800         if (d_temp)
801             *begin = d_temp->point;
803         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
804         if (d_temp == NULL) {
805             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
806         }
807         if (d_temp)
808             *end = d_temp->point;
809     } else if ( SP_IS_RADIALGRADIENT(server) ) {
810         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
811         GrDragger *d_temp;
812         if (lowest_i == 1) {
813             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
814         } else {
815             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
816         }
817         if (d_temp)
818             *begin = d_temp->point;
820         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
821         if (d_temp == NULL) {
822             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
823         }
824         if (d_temp)
825             *end = d_temp->point;
826     }
828     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
829     *high_lim = dragger->point - (highest_dragger->point - *end);
834 /**
835 Called when a midpoint knot is dragged.
836 */
837 static void
838 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
840     GrDragger *dragger = (GrDragger *) data;
841     GrDrag *drag = dragger->parent;
842     // a midpoint dragger can (logically) only contain one GrDraggable
843     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
845     // FIXME: take from prefs
846     double snap_fraction = 0.1;
848     Geom::Point p = ppointer;
849     Geom::Point begin(0,0), end(0,0);
850     Geom::Point low_lim(0,0), high_lim(0,0);
852     SPObject *server = draggable->getServer();
854     GSList *moving = NULL;
855     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
857     if (state & GDK_CONTROL_MASK) {
858         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
859     } else {
860         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
861         if (!(state & GDK_SHIFT_MASK)) {
862             Inkscape::Snapper::ConstraintLine cl(low_lim, high_lim - low_lim);
863             SPDesktop *desktop = dragger->parent->desktop;
864             SnapManager &m = desktop->namedview->snap_manager;
865             m.setup(desktop);
866             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
867         }
868     }
869     Geom::Point displacement = p - dragger->point;
871     for (GSList const* i = moving; i != NULL; i = i->next) {
872         GrDragger *drg = (GrDragger*) i->data;
873         SPKnot *drgknot = drg->knot;
874         Geom::Point this_move = displacement;
875         if (state & GDK_MOD1_MASK) {
876             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
877             double alpha = 1.0;
878             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
879                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
880                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
881             } else { // drg is on the begin side from dragger
882                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
883                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
884             }
885         }
886         drg->point += this_move;
887         sp_knot_moveto (drgknot, drg->point);
888         drg->fireDraggables (false);
889         drg->updateDependencies(false);
890     }
892     g_slist_free(moving);
894     drag->keep_selection = dragger->isSelected();
899 static void
900 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
902     GrDragger *dragger = (GrDragger *) data;
904     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
907 /**
908 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
909 */
910 static void
911 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
913     GrDragger *dragger = (GrDragger *) data;
915     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
917     dragger->point_original = dragger->point = knot->pos;
919     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
920         dragger->fireDraggables (true, true);
921     } else {
922         dragger->fireDraggables (true);
923     }
925     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
926         GrDragger *d = (GrDragger *) i->data;
927         if (d == dragger)
928             continue;
929         d->fireDraggables (true);
930     }
932     // make this dragger selected
933     if (!dragger->parent->keep_selection) {
934         dragger->parent->setSelected (dragger);
935     }
936     dragger->parent->keep_selection = false;
938     dragger->updateDependencies(true);
940     // we did an undoable action
941     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
942                       _("Move gradient handle"));
945 /**
946 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
947 state of the keyboard keys
948 */
949 static void
950 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
952     GrDragger *dragger = (GrDragger *) data;
953     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
954     if (!draggable) return;
956     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
957     // delete this knot from vector
958         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
959         gradient = gradient->getVector();
960         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
961             SPStop *stop = NULL;
962             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
963             case POINT_LG_BEGIN:
964             case POINT_RG_CENTER:
965                 stop = gradient->getFirstStop();
966                 {
967                     SPStop *next = stop->getNextStop();
968                     if (next) {
969                         next->offset = 0;
970                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
971                     }
972                 }
973                 break;
974             case POINT_LG_END:
975             case POINT_RG_R1:
976             case POINT_RG_R2:
977                 stop = sp_last_stop(gradient);
978                 {
979                     SPStop *prev = stop->getPrevStop();
980                     if (prev) {
981                         prev->offset = 1;
982                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
983                     }
984                 }
985                 break;
986             case POINT_LG_MID:
987             case POINT_RG_MID1:
988             case POINT_RG_MID2:
989                 stop = sp_get_stop_i(gradient, draggable->point_i);
990                 break;
991             }
993             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
994             sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
995                       _("Delete gradient stop"));
996         }
997     } else {
998     // select the dragger
999         dragger->point_original = dragger->point;
1001         if ( state & GDK_SHIFT_MASK ) {
1002             dragger->parent->setSelected (dragger, true, false);
1003         } else {
1004             dragger->parent->setSelected (dragger);
1005         }
1006     }
1009 /**
1010 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
1011 */
1012 static void
1013 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
1015     GrDragger *dragger = (GrDragger *) data;
1017     dragger->point_original = dragger->point;
1019     if (dragger->draggables == NULL)
1020         return;
1022     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1023     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1026 /**
1027 Act upon all draggables of the dragger, setting them to the dragger's point
1028 */
1029 void
1030 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1032     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1033         GrDraggable *draggable = (GrDraggable *) i->data;
1035         // set local_change flag so that selection_changed callback does not regenerate draggers
1036         this->parent->local_change = true;
1038         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1039         // to the center, unless it's the first update upon merge when we must snap it to the point
1040         if (merging_focus ||
1041             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1042         {
1043             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1044         }
1045     }
1048 /**
1049 Checks if the dragger has a draggable with this point_type
1050  */
1051 bool
1052 GrDragger::isA (gint point_type)
1054     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1055         GrDraggable *draggable = (GrDraggable *) i->data;
1056         if (draggable->point_type == point_type) {
1057             return true;
1058         }
1059     }
1060     return false;
1063 /**
1064 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1065  */
1066 bool
1067 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1069     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1070         GrDraggable *draggable = (GrDraggable *) i->data;
1071         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1072             return true;
1073         }
1074     }
1075     return false;
1078 /**
1079 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1080  */
1081 bool
1082 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1084     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1085         GrDraggable *draggable = (GrDraggable *) i->data;
1086         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1087             return true;
1088         }
1089     }
1090     return false;
1093 bool
1094 GrDraggable::mayMerge (GrDraggable *da2)
1096     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1097         // we must not merge the points of the same gradient!
1098         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1099               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1100             // except that we can snap center and focus together
1101             return false;
1102         }
1103     }
1104     // disable merging of midpoints.
1105     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1106          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1107          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1108         return false;
1110     return true;
1113 bool
1114 GrDragger::mayMerge (GrDragger *other)
1116     if (this == other)
1117         return false;
1119     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1120         GrDraggable *da1 = (GrDraggable *) i->data;
1121         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1122             GrDraggable *da2 = (GrDraggable *) j->data;
1123             if (!da1->mayMerge(da2))
1124                 return false;
1125         }
1126     }
1127     return true;
1130 bool
1131 GrDragger::mayMerge (GrDraggable *da2)
1133     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1134         GrDraggable *da1 = (GrDraggable *) i->data;
1135         if (!da1->mayMerge(da2))
1136             return false;
1137     }
1138     return true;
1141 /**
1142 Updates the statusbar tip of the dragger knot, based on its draggables
1143  */
1144 void
1145 GrDragger::updateTip ()
1147     if (this->knot && this->knot->tip) {
1148         g_free (this->knot->tip);
1149         this->knot->tip = NULL;
1150     }
1152     if (g_slist_length (this->draggables) == 1) {
1153         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1154         char *item_desc = sp_item_description(draggable->item);
1155         switch (draggable->point_type) {
1156             case POINT_LG_MID:
1157             case POINT_RG_MID1:
1158             case POINT_RG_MID2:
1159                 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"),
1160                                                    _(gr_knot_descr[draggable->point_type]),
1161                                                    draggable->point_i,
1162                                                    item_desc,
1163                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1164                 break;
1166             default:
1167                 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"),
1168                                                    _(gr_knot_descr[draggable->point_type]),
1169                                                    item_desc,
1170                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1171                 break;
1172         }
1173         g_free(item_desc);
1174     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1175         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1176     } else {
1177         int length = g_slist_length (this->draggables);
1178         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1179                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1180                                                     length),
1181                                            length);
1182     }
1185 /**
1186 Adds a draggable to the dragger
1187  */
1188 void
1189 GrDragger::updateKnotShape ()
1191     if (!draggables)
1192         return;
1193     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1194     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1197 /**
1198 Adds a draggable to the dragger
1199  */
1200 void
1201 GrDragger::addDraggable (GrDraggable *draggable)
1203     this->draggables = g_slist_prepend (this->draggables, draggable);
1205     this->updateTip();
1209 /**
1210 Moves this dragger to the point of the given draggable, acting upon all other draggables
1211  */
1212 void
1213 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1215     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1216     if (!dr_first) return;
1218     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1219     this->point_original = this->point;
1221     sp_knot_moveto (this->knot, this->point);
1223     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1224         GrDraggable *da = (GrDraggable *) i->data;
1225         if ( (da->item == item) &&
1226              (point_type == -1 || da->point_type == point_type) &&
1227              (point_i == -1 || da->point_i == point_i) &&
1228              (da->fill_or_stroke == fill_or_stroke) ) {
1229             continue;
1230         }
1231         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1232     }
1233     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1237 /**
1238 Moves all midstop draggables that depend on this one
1239  */
1240 void
1241 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1242     SPObject *server = draggable->getServer();
1243     if (!server)
1244         return;
1245     guint num = SP_GRADIENT(server)->vector.stops.size();
1246     if (num <= 2) return;
1248     if ( SP_IS_LINEARGRADIENT(server) ) {
1249         for ( guint i = 1; i < num - 1; i++ ) {
1250             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1251         }
1252     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1253         for ( guint i = 1; i < num - 1; i++ ) {
1254             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1255             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1256         }
1257     }
1261 /**
1262 Moves all draggables that depend on this one
1263  */
1264 void
1265 GrDragger::updateDependencies (bool write_repr)
1267     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1268         GrDraggable *draggable = (GrDraggable *) i->data;
1269         switch (draggable->point_type) {
1270             case POINT_LG_BEGIN:
1271                 {
1272                     // the end point is dependent only when dragging with ctrl+shift
1273                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1275                     this->updateMidstopDependencies (draggable, write_repr);
1276                 }
1277                 break;
1278             case POINT_LG_END:
1279                 {
1280                     // the begin point is dependent only when dragging with ctrl+shift
1281                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1283                     this->updateMidstopDependencies (draggable, write_repr);
1284                 }
1285                 break;
1286             case POINT_LG_MID:
1287                 // no other nodes depend on mid points.
1288                 break;
1289             case POINT_RG_R2:
1290                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1291                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1292                 this->updateMidstopDependencies (draggable, write_repr);
1293                 break;
1294             case POINT_RG_R1:
1295                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1296                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1297                 this->updateMidstopDependencies (draggable, write_repr);
1298                 break;
1299             case POINT_RG_CENTER:
1300                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1301                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1302                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1303                 this->updateMidstopDependencies (draggable, write_repr);
1304                 break;
1305             case POINT_RG_FOCUS:
1306                 // nothing can depend on that
1307                 break;
1308             case POINT_RG_MID1:
1309                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1310                 break;
1311             case POINT_RG_MID2:
1312                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1313                 break;
1314             default:
1315                 break;
1316         }
1317     }
1322 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1323   : point(p),
1324     point_original(p)
1326     this->draggables = NULL;
1328     this->parent = parent;
1330     // create the knot
1331     this->knot = sp_knot_new (parent->desktop, NULL);
1332     this->knot->setMode(SP_KNOT_MODE_XOR);
1333     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1334     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1335     sp_knot_update_ctrl(this->knot);
1337     // move knot to the given point
1338     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1339     sp_knot_show (this->knot);
1341     // connect knot's signals
1342     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1343                        // luckily, midstops never snap to other nodes so are never unsnapped...
1344          && ( (draggable->point_type == POINT_LG_MID)
1345               || (draggable->point_type == POINT_RG_MID1)
1346               || (draggable->point_type == POINT_RG_MID2) ) )
1347     {
1348         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1349     } else {
1350         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1351     }
1352     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1353     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1354     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1355     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1357     // add the initial draggable
1358     if (draggable)
1359         this->addDraggable (draggable);
1360     updateKnotShape();
1363 GrDragger::~GrDragger ()
1365     // unselect if it was selected
1366     this->parent->setDeselected(this);
1368     // disconnect signals
1369     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1370     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1371     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1372     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1373     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1375     /* unref should call destroy */
1376     g_object_unref (G_OBJECT (this->knot));
1378     // delete all draggables
1379     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1380         delete ((GrDraggable *) i->data);
1381     }
1382     g_slist_free (this->draggables);
1383     this->draggables = NULL;
1386 /**
1387 Select the dragger which has the given draggable.
1388 */
1389 GrDragger *
1390 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1392     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1393         GrDragger *dragger = (GrDragger *) i->data;
1394         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1395             GrDraggable *da2 = (GrDraggable *) j->data;
1396             if ( (da2->item == item) &&
1397                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1398                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1399                  (da2->fill_or_stroke == fill_or_stroke)) {
1400                 return (dragger);
1401             }
1402         }
1403     }
1404     return NULL;
1408 void
1409 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1411     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1412     if (d && d !=  this) {
1413         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1414     }
1418 /**
1419   Draw this dragger as selected
1420 */
1421 void
1422 GrDragger::select()
1424     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1425     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1428 /**
1429   Draw this dragger as normal (deselected)
1430 */
1431 void
1432 GrDragger::deselect()
1434     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1435     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1438 bool
1439 GrDragger::isSelected()
1441     return g_list_find (parent->selected, this);
1444 /**
1445 \brief Deselect all stops/draggers (private)
1446 */
1447 void
1448 GrDrag::deselect_all()
1450     while (selected) {
1451         ( (GrDragger*) selected->data)->deselect();
1452         selected = g_list_remove(selected, selected->data);
1453     }
1456 /**
1457 \brief Deselect all stops/draggers (public; emits signal)
1458 */
1459 void
1460 GrDrag::deselectAll()
1462     deselect_all();
1463     this->desktop->emitToolSubselectionChanged(NULL);
1466 /**
1467 \brief Select all stops/draggers
1468 */
1469 void
1470 GrDrag::selectAll()
1472     for (GList *l = this->draggers; l != NULL; l = l->next) {
1473         GrDragger *d = ((GrDragger *) l->data);
1474         setSelected (d, true, true);
1475     }
1478 /**
1479 \brief Select all stops/draggers that match the coords
1480 */
1481 void
1482 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1484     for (GList *l = this->draggers; l != NULL; l = l->next) {
1485         GrDragger *d = ((GrDragger *) l->data);
1486         for (guint k = 0; k < coords.size(); k++) {
1487             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1488                 setSelected (d, true, true);
1489             }
1490         }
1491     }
1495 /**
1496 \brief Select all stops/draggers that fall within the rect
1497 */
1498 void
1499 GrDrag::selectRect(Geom::Rect const &r)
1501     for (GList *l = this->draggers; l != NULL; l = l->next) {
1502         GrDragger *d = ((GrDragger *) l->data);
1503         if (r.contains(d->point)) {
1504            setSelected (d, true, true);
1505         }
1506     }
1509 /**
1510 \brief Select a dragger
1511 \param dragger       The dragger to select
1512 \param add_to_selection   If true, add to selection, otherwise deselect others
1513 \param override      If true, always select this node, otherwise toggle selected status
1514 */
1515 void
1516 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1518     GrDragger *seldragger = NULL;
1520     if (add_to_selection) {
1521         if (!dragger) return;
1522         if (override) {
1523             if (!g_list_find(selected, dragger)) {
1524                 selected = g_list_prepend(selected, dragger);
1525             }
1526             dragger->select();
1527             seldragger = dragger;
1528         } else { // toggle
1529             if (g_list_find(selected, dragger)) {
1530                 selected = g_list_remove(selected, dragger);
1531                 dragger->deselect();
1532                 if (selected) {
1533                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1534                 }
1535             } else {
1536                 selected = g_list_prepend(selected, dragger);
1537                 dragger->select();
1538                 seldragger = dragger;
1539             }
1540         }
1541     } else {
1542         deselect_all();
1543         if (dragger) {
1544             selected = g_list_prepend(selected, dragger);
1545             dragger->select();
1546             seldragger = dragger;
1547         }
1548     }
1549     if (seldragger) {
1550         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1551     }
1554 /**
1555 \brief Deselect a dragger
1556 \param dragger       The dragger to deselect
1557 */
1558 void
1559 GrDrag::setDeselected (GrDragger *dragger)
1561     if (g_list_find(selected, dragger)) {
1562         selected = g_list_remove(selected, dragger);
1563         dragger->deselect();
1564     }
1565     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1570 /**
1571 Create a line from p1 to p2 and add it to the lines list
1572  */
1573 void
1574 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1576     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1577                                                             SP_TYPE_CTRLLINE, NULL);
1578     sp_canvas_item_move_to_z(line, 0);
1579     SP_CTRLLINE(line)->item = item;
1580     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1581     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1582         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1583     sp_canvas_item_show (line);
1584     this->lines = g_slist_append (this->lines, line);
1587 /**
1588 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1589 new dragger and add it to draggers list
1590  */
1591 void
1592 GrDrag::addDragger (GrDraggable *draggable)
1594     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1596     for (GList *i = this->draggers; i != NULL; i = i->next) {
1597         GrDragger *dragger = (GrDragger *) i->data;
1598         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1599             // distance is small, merge this draggable into dragger, no need to create new dragger
1600             dragger->addDraggable (draggable);
1601             dragger->updateKnotShape();
1602             return;
1603         }
1604     }
1606     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1607     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1608     this->draggers = g_list_append (this->draggers, new_dragger);
1611 /**
1612 Add draggers for the radial gradient rg on item
1613 */
1614 void
1615 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1617     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1618     guint num = rg->vector.stops.size();
1619     if (num > 2) {
1620         for ( guint i = 1; i < num - 1; i++ ) {
1621             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1622         }
1623     }
1624     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1625     if (num > 2) {
1626         for ( guint i = 1; i < num - 1; i++ ) {
1627             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1628         }
1629     }
1630     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1631     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1634 /**
1635 Add draggers for the linear gradient lg on item
1636 */
1637 void
1638 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1640     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1641     guint num = lg->vector.stops.size();
1642     if (num > 2) {
1643         for ( guint i = 1; i < num - 1; i++ ) {
1644             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1645         }
1646     }
1647     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1650 /**
1651 Artificially grab the knot of this dragger; used by the gradient context
1652 */
1653 void
1654 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1656     if (dragger) {
1657         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1658     }
1661 /**
1662 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1663 */
1664 void
1665 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1667     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1668     if (dragger) {
1669         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1670     }
1673 /**
1674 Regenerates the draggers list from the current selection; is called when selection is changed or
1675 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1676 */
1677 void GrDrag::updateDraggers ()
1679     while (selected) {
1680         selected = g_list_remove(selected, selected->data);
1681     }
1682     // delete old draggers
1683     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1684         delete static_cast<GrDragger *>(i->data);
1685     }
1686     g_list_free(this->draggers);
1687     this->draggers = NULL;
1689     g_return_if_fail(this->selection != NULL);
1691     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1692         SPItem *item = SP_ITEM(i->data);
1693         SPStyle *style = item->style;
1695         if (style && (style->fill.isPaintserver())) {
1696             SPPaintServer *server = style->getFillPaintServer();
1697             if ( server && server->isSolid() ) {
1698                 // Suppress "gradientness" of solid paint
1699             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1700                 addDraggersLinear( SP_LINEARGRADIENT(server), item, true );
1701             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1702                 addDraggersRadial( SP_RADIALGRADIENT(server), item, true );
1703             }
1704         }
1706         if (style && (style->stroke.isPaintserver())) {
1707             SPPaintServer *server = style->getStrokePaintServer();
1708             if ( server && server->isSolid() ) {
1709                 // Suppress "gradientness" of solid paint
1710             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1711                 addDraggersLinear( SP_LINEARGRADIENT(server), item, false );
1712             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1713                 addDraggersRadial( SP_RADIALGRADIENT(server), item, false );
1714             }
1715         }
1716     }
1720 /**
1721  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1722  */
1724 bool
1725 GrDrag::mouseOver()
1727     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1728         GrDragger *d = (GrDragger *) i->data;
1729         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1730             return true;
1731         }
1732     }
1733     return false;
1735 /**
1736 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1737 lines are always in sync with the actual gradient
1738 */
1739 void
1740 GrDrag::updateLines ()
1742     // delete old lines
1743     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1744         gtk_object_destroy( GTK_OBJECT (i->data));
1745     }
1746     g_slist_free (this->lines);
1747     this->lines = NULL;
1749     g_return_if_fail (this->selection != NULL);
1751     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1753         SPItem *item = SP_ITEM(i->data);
1755         SPStyle *style = SP_OBJECT_STYLE (item);
1757         if (style && (style->fill.isPaintserver())) {
1758             SPPaintServer *server = item->style->getFillPaintServer();
1759             if ( server && server->isSolid() ) {
1760                 // Suppress "gradientness" of solid paint
1761             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1762                 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);
1763             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1764                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1765                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1766                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1767             }
1768         }
1770         if (style && (style->stroke.isPaintserver())) {
1771             SPPaintServer *server = item->style->getStrokePaintServer();
1772             if ( server && server->isSolid() ) {
1773                 // Suppress "gradientness" of solid paint
1774             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1775                 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);
1776             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1777                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1778                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1779                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1780             }
1781         }
1782     }
1785 /**
1786 Regenerates the levels list from the current selection
1787 */
1788 void
1789 GrDrag::updateLevels ()
1791     hor_levels.clear();
1792     vert_levels.clear();
1794     g_return_if_fail (this->selection != NULL);
1796     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1797         SPItem *item = SP_ITEM(i->data);
1798         Geom::OptRect rect = sp_item_bbox_desktop (item);
1799         if (rect) {
1800             // Remember the edges of the bbox and the center axis
1801             hor_levels.push_back(rect->min()[Geom::Y]);
1802             hor_levels.push_back(rect->max()[Geom::Y]);
1803             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1804             vert_levels.push_back(rect->min()[Geom::X]);
1805             vert_levels.push_back(rect->max()[Geom::X]);
1806             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1807         }
1808     }
1811 void
1812 GrDrag::selected_reverse_vector ()
1814     if (selected == NULL)
1815         return;
1817     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1818         GrDraggable *draggable = (GrDraggable *) i->data;
1820         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1821     }
1824 void
1825 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1827     selected_move (x, y, false, scale_radial);
1830 void
1831 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1833     if (selected == NULL)
1834         return;
1836     bool did = false;
1838     for (GList *i = selected; i != NULL; i = i->next) {
1839         GrDragger *d = (GrDragger *) i->data;
1841         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1842             // if this is an endpoint,
1844             // Moving an rg center moves its focus and radii as well.
1845             // therefore, if this is a focus or radius and if selection
1846             // contains the center as well, do not move this one
1847             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1848                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1849                 bool skip_radius_with_center = false;
1850                 for (GList *di = selected; di != NULL; di = di->next) {
1851                     GrDragger *d_new = (GrDragger *) di->data;
1852                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1853                                     POINT_RG_CENTER,
1854                                     0,
1855                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1856                         // FIXME: here we take into account only the first draggable!
1857                         skip_radius_with_center = true;
1858                     }
1859                 }
1860                 if (skip_radius_with_center)
1861                     continue;
1862             }
1864             did = true;
1865             d->point += Geom::Point (x, y);
1866             d->point_original = d->point;
1867             sp_knot_moveto (d->knot, d->point);
1869             d->fireDraggables (write_repr, scale_radial);
1871             d->updateDependencies(write_repr);
1872         }
1873     }
1875     if (write_repr && did) {
1876         // we did an undoable action
1877         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1878                                 _("Move gradient handle(s)"));
1879         return;
1880     }
1882     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1884         GrDragger *dragger = (GrDragger *) selected->data;
1885         // a midpoint dragger can (logically) only contain one GrDraggable
1886         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1888         Geom::Point begin(0,0), end(0,0);
1889         Geom::Point low_lim(0,0), high_lim(0,0);
1891         SPObject *server = draggable->getServer();
1892         GSList *moving = NULL;
1893         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1895         Geom::Point p(x, y);
1896         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1897         Geom::Point displacement = p - dragger->point;
1899         for (GSList const* i = moving; i != NULL; i = i->next) {
1900             GrDragger *drg = (GrDragger*) i->data;
1901             SPKnot *drgknot = drg->knot;
1902             drg->point += displacement;
1903             sp_knot_moveto (drgknot, drg->point);
1904             drg->fireDraggables (true);
1905             drg->updateDependencies(true);
1906             did = true;
1907         }
1909         g_slist_free(moving);
1911         if (write_repr && did) {
1912             // we did an undoable action
1913             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1914                                     _("Move gradient mid stop(s)"));
1915         }
1916     }
1919 void
1920 GrDrag::selected_move_screen (double x, double y)
1922     gdouble zoom = desktop->current_zoom();
1923     gdouble zx = x / zoom;
1924     gdouble zy = y / zoom;
1926     selected_move (zx, zy);
1929 /**
1930 Select the knot next to the last selected one and deselect all other selected.
1931 */
1932 GrDragger *
1933 GrDrag::select_next ()
1935     GrDragger *d = NULL;
1936     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1937         if (draggers)
1938             d = (GrDragger *) draggers->data;
1939     } else {
1940         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1941     }
1942     if (d)
1943         setSelected (d);
1944     return d;
1947 /**
1948 Select the knot previous from the last selected one and deselect all other selected.
1949 */
1950 GrDragger *
1951 GrDrag::select_prev ()
1953     GrDragger *d = NULL;
1954     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1955         if (draggers)
1956             d = (GrDragger *) g_list_last (draggers)->data;
1957     } else {
1958         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1959     }
1960     if (d)
1961         setSelected (d);
1962     return d;
1966 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1967 void
1968 GrDrag::deleteSelected (bool just_one)
1970     if (!selected) return;
1972     SPDocument *document = false;
1974     struct StructStopInfo {
1975         SPStop * spstop;
1976         GrDraggable * draggable;
1977         SPGradient * gradient;
1978         SPGradient * vector;
1979     };
1981     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1982     GSList *endstoplist = NULL;  // list of stops that must be deleted
1983     while (selected) {
1984         GrDragger *dragger = (GrDragger*) selected->data;
1985         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1986             GrDraggable *draggable = (GrDraggable*) drgble->data;
1987             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1988             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1990             switch (draggable->point_type) {
1991                 case POINT_LG_MID:
1992                 case POINT_RG_MID1:
1993                 case POINT_RG_MID2:
1994                     {
1995                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1996                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1997                         bool present = false;
1998                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1999                             if ( (SPStop*)l->data == stop ) {
2000                                 present = true;
2001                                 break; // no need to search further.
2002                             }
2003                         }
2004                         if (!present)
2005                             midstoplist = g_slist_append(midstoplist, stop);
2006                     }
2007                     break;
2008                 case POINT_LG_BEGIN:
2009                 case POINT_LG_END:
2010                 case POINT_RG_CENTER:
2011                 case POINT_RG_R1:
2012                 case POINT_RG_R2:
2013                     {
2014                         SPStop *stop = NULL;
2015                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
2016                             stop = vector->getFirstStop();
2017                         } else {
2018                             stop = sp_last_stop(vector);
2019                         }
2020                         if (stop) {
2021                             StructStopInfo *stopinfo = new StructStopInfo;
2022                             stopinfo->spstop = stop;
2023                             stopinfo->draggable = draggable;
2024                             stopinfo->gradient = gradient;
2025                             stopinfo->vector = vector;
2026                             // check if already present in list. (e.g. when both R1 and R2 were selected)
2027                             bool present = false;
2028                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
2029                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
2030                                     present = true;
2031                                     break; // no need to search further.
2032                                 }
2033                             }
2034                             if (!present)
2035                                 endstoplist = g_slist_append(endstoplist, stopinfo);
2036                         }
2037                     }
2038                     break;
2039                 default:
2040                     break;
2041             }
2042         }
2043         selected = g_list_remove(selected, dragger);
2044         if ( just_one ) break; // iterate once if just_one is set.
2045     }
2046     while (midstoplist) {
2047         SPStop *stop = (SPStop*) midstoplist->data;
2048         document = SP_OBJECT_DOCUMENT (stop);
2049         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
2050         parent->removeChild(SP_OBJECT_REPR(stop));
2051         midstoplist = g_slist_remove(midstoplist, stop);
2052     }
2053     while (endstoplist) {
2054         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
2055         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2057         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2058         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2059         // manually count the children, don't know if there already exists a function for this...
2060         int len = 0;
2061         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2062               child != NULL ;
2063               child = SP_OBJECT_NEXT(child) )
2064         {
2065             if ( SP_IS_STOP(child) )  len ++;
2066         }
2067         if (len > 2)
2068         {
2069             switch (stopinfo->draggable->point_type) {
2070                 case POINT_LG_BEGIN:
2071                     {
2072                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2074                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2075                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2076                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2077                         SPStop *stop = stopinfo->vector->getFirstStop();
2078                         gdouble offset = stop->offset;
2079                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2080                         lg->x1.computed = newbegin[Geom::X];
2081                         lg->y1.computed = newbegin[Geom::Y];
2083                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2084                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2085                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2086                         stop->offset = 0;
2087                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2089                         // iterate through midstops to set new offset values such that they won't move on canvas.
2090                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2091                         stop = stop->getNextStop();
2092                         while ( stop != laststop ) {
2093                             stop->offset = (stop->offset - offset)/(1 - offset);
2094                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2095                             stop = stop->getNextStop();
2096                         }
2097                     }
2098                     break;
2099                 case POINT_LG_END:
2100                     {
2101                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2103                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2104                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2105                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2106                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2107                         gdouble offset = laststop->offset;
2108                         Geom::Point newend = begin + offset * (oldend - begin);
2109                         lg->x2.computed = newend[Geom::X];
2110                         lg->y2.computed = newend[Geom::Y];
2112                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2113                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2114                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2115                         laststop->offset = 1;
2116                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2118                         // iterate through midstops to set new offset values such that they won't move on canvas.
2119                         SPStop *stop = stopinfo->vector->getFirstStop();
2120                         stop = stop->getNextStop();
2121                         while ( stop != laststop ) {
2122                             stop->offset = stop->offset / offset;
2123                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2124                             stop = stop->getNextStop();
2125                         }
2126                     }
2127                     break;
2128                 case POINT_RG_CENTER:
2129                     {
2130                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2131                         if (newfirst) {
2132                             newfirst->offset = 0;
2133                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2134                         }
2135                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2136                     }
2137                     break;
2138                 case POINT_RG_R1:
2139                 case POINT_RG_R2:
2140                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2142                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2143                         double oldradius = rg->r.computed;
2144                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2145                         gdouble offset = laststop->offset;
2146                         double newradius = offset * oldradius;
2147                         rg->r.computed = newradius;
2149                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2150                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2151                         laststop->offset = 1;
2152                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2154                         // iterate through midstops to set new offset values such that they won't move on canvas.
2155                         SPStop *stop = stopinfo->vector->getFirstStop();
2156                         stop = stop->getNextStop();
2157                         while ( stop != laststop ) {
2158                             stop->offset = stop->offset / offset;
2159                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2160                             stop = stop->getNextStop();
2161                         }
2162                         break;
2163             }
2164         }
2165         else
2166         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2167             SPCSSAttr *css = sp_repr_css_attr_new ();
2169             // stopinfo->spstop is the selected stop
2170             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2171             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2172                 unselectedrepr = unselectedrepr->next();
2173             }
2175             if (unselectedrepr == NULL) {
2176                 if (stopinfo->draggable->fill_or_stroke) {
2177                     sp_repr_css_unset_property (css, "fill");
2178                 } else {
2179                     sp_repr_css_unset_property (css, "stroke");
2180                 }
2181             } else {
2182                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2183                 if (stopinfo->draggable->fill_or_stroke) {
2184                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2185                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2186                 } else {
2187                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2188                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2189                 }
2190                 sp_repr_css_attr_unref (stopcss);
2191             }
2193             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2194             sp_repr_css_attr_unref (css);
2195         }
2197         endstoplist = g_slist_remove(endstoplist, stopinfo);
2198         delete stopinfo;
2199     }
2201     if (document) {
2202         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2203     }
2207 /*
2208   Local Variables:
2209   mode:c++
2210   c-file-style:"stroustrup"
2211   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2212   indent-tabs-mode:nil
2213   fill-column:99
2214   End:
2215 */
2216 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :