Code

32aa7c084ee63c53eb1447ebe3036a4824c1b65f
[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::SnapConstraint 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     m.unSetup();
736     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
737     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
739     if (drag->keep_selection) {
740         Geom::Point diff = p - dragger->point;
741         drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
742     } else {
743         dragger->point = p;
744         dragger->fireDraggables (false, scale_radial);
745         dragger->updateDependencies(false);
746     }
751 static void
752 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
755     GrDrag *drag = dragger->parent;
756     // a midpoint dragger can (logically) only contain one GrDraggable
757     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
759     // get begin and end points between which dragging is allowed:
760     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
761     *moving = g_slist_append(*moving, dragger);
763     guint lowest_i = draggable->point_i;
764     guint highest_i = draggable->point_i;
765     GrDragger *lowest_dragger = dragger;
766     GrDragger *highest_dragger = dragger;
767     if (dragger->isSelected()) {
768         GrDragger* d_add;
769         while ( true )
770         {
771             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
772             if ( d_add && g_list_find(drag->selected, d_add) ) {
773                 lowest_i = lowest_i - 1;
774                 *moving = g_slist_prepend(*moving, d_add);
775                 lowest_dragger = d_add;
776             } else {
777                 break;
778             }
779         }
781         while ( true )
782         {
783             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
784             if ( d_add && g_list_find(drag->selected, d_add) ) {
785                 highest_i = highest_i + 1;
786                 *moving = g_slist_append(*moving, d_add);
787                 highest_dragger = d_add;
788             } else {
789                 break;
790             }
791         }
792     }
794     if ( SP_IS_LINEARGRADIENT(server) ) {
795         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
796         GrDragger *d_temp;
797         if (lowest_i == 1) {
798             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
799         } else {
800             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
801         }
802         if (d_temp)
803             *begin = d_temp->point;
805         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
806         if (d_temp == NULL) {
807             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
808         }
809         if (d_temp)
810             *end = d_temp->point;
811     } else if ( SP_IS_RADIALGRADIENT(server) ) {
812         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
813         GrDragger *d_temp;
814         if (lowest_i == 1) {
815             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
816         } else {
817             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
818         }
819         if (d_temp)
820             *begin = d_temp->point;
822         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
823         if (d_temp == NULL) {
824             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
825         }
826         if (d_temp)
827             *end = d_temp->point;
828     }
830     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
831     *high_lim = dragger->point - (highest_dragger->point - *end);
836 /**
837 Called when a midpoint knot is dragged.
838 */
839 static void
840 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
842     GrDragger *dragger = (GrDragger *) data;
843     GrDrag *drag = dragger->parent;
844     // a midpoint dragger can (logically) only contain one GrDraggable
845     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
847     // FIXME: take from prefs
848     double snap_fraction = 0.1;
850     Geom::Point p = ppointer;
851     Geom::Point begin(0,0), end(0,0);
852     Geom::Point low_lim(0,0), high_lim(0,0);
854     SPObject *server = draggable->getServer();
856     GSList *moving = NULL;
857     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
859     if (state & GDK_CONTROL_MASK) {
860         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
861     } else {
862         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
863         if (!(state & GDK_SHIFT_MASK)) {
864             Inkscape::Snapper::SnapConstraint cl(low_lim, high_lim - low_lim);
865             SPDesktop *desktop = dragger->parent->desktop;
866             SnapManager &m = desktop->namedview->snap_manager;
867             m.setup(desktop);
868             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
869             m.unSetup();
870         }
871     }
872     Geom::Point displacement = p - dragger->point;
874     for (GSList const* i = moving; i != NULL; i = i->next) {
875         GrDragger *drg = (GrDragger*) i->data;
876         SPKnot *drgknot = drg->knot;
877         Geom::Point this_move = displacement;
878         if (state & GDK_MOD1_MASK) {
879             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
880             double alpha = 1.0;
881             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
882                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
883                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
884             } else { // drg is on the begin side from dragger
885                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
886                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
887             }
888         }
889         drg->point += this_move;
890         sp_knot_moveto (drgknot, drg->point);
891         drg->fireDraggables (false);
892         drg->updateDependencies(false);
893     }
895     g_slist_free(moving);
897     drag->keep_selection = dragger->isSelected();
902 static void
903 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
905     GrDragger *dragger = (GrDragger *) data;
907     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
910 /**
911 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
912 */
913 static void
914 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
916     GrDragger *dragger = (GrDragger *) data;
918     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
920     dragger->point_original = dragger->point = knot->pos;
922     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
923         dragger->fireDraggables (true, true);
924     } else {
925         dragger->fireDraggables (true);
926     }
928     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
929         GrDragger *d = (GrDragger *) i->data;
930         if (d == dragger)
931             continue;
932         d->fireDraggables (true);
933     }
935     // make this dragger selected
936     if (!dragger->parent->keep_selection) {
937         dragger->parent->setSelected (dragger);
938     }
939     dragger->parent->keep_selection = false;
941     dragger->updateDependencies(true);
943     // we did an undoable action
944     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
945                       _("Move gradient handle"));
948 /**
949 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
950 state of the keyboard keys
951 */
952 static void
953 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
955     GrDragger *dragger = (GrDragger *) data;
956     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
957     if (!draggable) return;
959     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
960     // delete this knot from vector
961         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
962         gradient = gradient->getVector();
963         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
964             SPStop *stop = NULL;
965             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
966             case POINT_LG_BEGIN:
967             case POINT_RG_CENTER:
968                 stop = gradient->getFirstStop();
969                 {
970                     SPStop *next = stop->getNextStop();
971                     if (next) {
972                         next->offset = 0;
973                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
974                     }
975                 }
976                 break;
977             case POINT_LG_END:
978             case POINT_RG_R1:
979             case POINT_RG_R2:
980                 stop = sp_last_stop(gradient);
981                 {
982                     SPStop *prev = stop->getPrevStop();
983                     if (prev) {
984                         prev->offset = 1;
985                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
986                     }
987                 }
988                 break;
989             case POINT_LG_MID:
990             case POINT_RG_MID1:
991             case POINT_RG_MID2:
992                 stop = sp_get_stop_i(gradient, draggable->point_i);
993                 break;
994             }
996             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
997             sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
998                       _("Delete gradient stop"));
999         }
1000     } else {
1001     // select the dragger
1002         dragger->point_original = dragger->point;
1004         if ( state & GDK_SHIFT_MASK ) {
1005             dragger->parent->setSelected (dragger, true, false);
1006         } else {
1007             dragger->parent->setSelected (dragger);
1008         }
1009     }
1012 /**
1013 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
1014 */
1015 static void
1016 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
1018     GrDragger *dragger = (GrDragger *) data;
1020     dragger->point_original = dragger->point;
1022     if (dragger->draggables == NULL)
1023         return;
1025     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1026     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1029 /**
1030 Act upon all draggables of the dragger, setting them to the dragger's point
1031 */
1032 void
1033 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1035     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1036         GrDraggable *draggable = (GrDraggable *) i->data;
1038         // set local_change flag so that selection_changed callback does not regenerate draggers
1039         this->parent->local_change = true;
1041         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1042         // to the center, unless it's the first update upon merge when we must snap it to the point
1043         if (merging_focus ||
1044             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1045         {
1046             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1047         }
1048     }
1051 /**
1052 Checks if the dragger has a draggable with this point_type
1053  */
1054 bool
1055 GrDragger::isA (gint point_type)
1057     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1058         GrDraggable *draggable = (GrDraggable *) i->data;
1059         if (draggable->point_type == point_type) {
1060             return true;
1061         }
1062     }
1063     return false;
1066 /**
1067 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1068  */
1069 bool
1070 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1072     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1073         GrDraggable *draggable = (GrDraggable *) i->data;
1074         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1075             return true;
1076         }
1077     }
1078     return false;
1081 /**
1082 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1083  */
1084 bool
1085 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1087     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1088         GrDraggable *draggable = (GrDraggable *) i->data;
1089         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1090             return true;
1091         }
1092     }
1093     return false;
1096 bool
1097 GrDraggable::mayMerge (GrDraggable *da2)
1099     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1100         // we must not merge the points of the same gradient!
1101         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1102               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1103             // except that we can snap center and focus together
1104             return false;
1105         }
1106     }
1107     // disable merging of midpoints.
1108     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1109          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1110          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1111         return false;
1113     return true;
1116 bool
1117 GrDragger::mayMerge (GrDragger *other)
1119     if (this == other)
1120         return false;
1122     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1123         GrDraggable *da1 = (GrDraggable *) i->data;
1124         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1125             GrDraggable *da2 = (GrDraggable *) j->data;
1126             if (!da1->mayMerge(da2))
1127                 return false;
1128         }
1129     }
1130     return true;
1133 bool
1134 GrDragger::mayMerge (GrDraggable *da2)
1136     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1137         GrDraggable *da1 = (GrDraggable *) i->data;
1138         if (!da1->mayMerge(da2))
1139             return false;
1140     }
1141     return true;
1144 /**
1145 Updates the statusbar tip of the dragger knot, based on its draggables
1146  */
1147 void
1148 GrDragger::updateTip ()
1150     if (this->knot && this->knot->tip) {
1151         g_free (this->knot->tip);
1152         this->knot->tip = NULL;
1153     }
1155     if (g_slist_length (this->draggables) == 1) {
1156         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1157         char *item_desc = sp_item_description(draggable->item);
1158         switch (draggable->point_type) {
1159             case POINT_LG_MID:
1160             case POINT_RG_MID1:
1161             case POINT_RG_MID2:
1162                 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"),
1163                                                    _(gr_knot_descr[draggable->point_type]),
1164                                                    draggable->point_i,
1165                                                    item_desc,
1166                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1167                 break;
1169             default:
1170                 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"),
1171                                                    _(gr_knot_descr[draggable->point_type]),
1172                                                    item_desc,
1173                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1174                 break;
1175         }
1176         g_free(item_desc);
1177     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1178         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1179     } else {
1180         int length = g_slist_length (this->draggables);
1181         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1182                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1183                                                     length),
1184                                            length);
1185     }
1188 /**
1189 Adds a draggable to the dragger
1190  */
1191 void
1192 GrDragger::updateKnotShape ()
1194     if (!draggables)
1195         return;
1196     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1197     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1200 /**
1201 Adds a draggable to the dragger
1202  */
1203 void
1204 GrDragger::addDraggable (GrDraggable *draggable)
1206     this->draggables = g_slist_prepend (this->draggables, draggable);
1208     this->updateTip();
1212 /**
1213 Moves this dragger to the point of the given draggable, acting upon all other draggables
1214  */
1215 void
1216 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1218     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1219     if (!dr_first) return;
1221     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1222     this->point_original = this->point;
1224     sp_knot_moveto (this->knot, this->point);
1226     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1227         GrDraggable *da = (GrDraggable *) i->data;
1228         if ( (da->item == item) &&
1229              (point_type == -1 || da->point_type == point_type) &&
1230              (point_i == -1 || da->point_i == point_i) &&
1231              (da->fill_or_stroke == fill_or_stroke) ) {
1232             continue;
1233         }
1234         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1235     }
1236     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1240 /**
1241 Moves all midstop draggables that depend on this one
1242  */
1243 void
1244 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1245     SPObject *server = draggable->getServer();
1246     if (!server)
1247         return;
1248     guint num = SP_GRADIENT(server)->vector.stops.size();
1249     if (num <= 2) return;
1251     if ( SP_IS_LINEARGRADIENT(server) ) {
1252         for ( guint i = 1; i < num - 1; i++ ) {
1253             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1254         }
1255     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1256         for ( guint i = 1; i < num - 1; i++ ) {
1257             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1258             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1259         }
1260     }
1264 /**
1265 Moves all draggables that depend on this one
1266  */
1267 void
1268 GrDragger::updateDependencies (bool write_repr)
1270     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1271         GrDraggable *draggable = (GrDraggable *) i->data;
1272         switch (draggable->point_type) {
1273             case POINT_LG_BEGIN:
1274                 {
1275                     // the end point is dependent only when dragging with ctrl+shift
1276                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1278                     this->updateMidstopDependencies (draggable, write_repr);
1279                 }
1280                 break;
1281             case POINT_LG_END:
1282                 {
1283                     // the begin point is dependent only when dragging with ctrl+shift
1284                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1286                     this->updateMidstopDependencies (draggable, write_repr);
1287                 }
1288                 break;
1289             case POINT_LG_MID:
1290                 // no other nodes depend on mid points.
1291                 break;
1292             case POINT_RG_R2:
1293                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1294                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1295                 this->updateMidstopDependencies (draggable, write_repr);
1296                 break;
1297             case POINT_RG_R1:
1298                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1299                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1300                 this->updateMidstopDependencies (draggable, write_repr);
1301                 break;
1302             case POINT_RG_CENTER:
1303                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1304                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1305                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1306                 this->updateMidstopDependencies (draggable, write_repr);
1307                 break;
1308             case POINT_RG_FOCUS:
1309                 // nothing can depend on that
1310                 break;
1311             case POINT_RG_MID1:
1312                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1313                 break;
1314             case POINT_RG_MID2:
1315                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1316                 break;
1317             default:
1318                 break;
1319         }
1320     }
1325 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1326   : point(p),
1327     point_original(p)
1329     this->draggables = NULL;
1331     this->parent = parent;
1333     // create the knot
1334     this->knot = sp_knot_new (parent->desktop, NULL);
1335     this->knot->setMode(SP_KNOT_MODE_XOR);
1336     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1337     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1338     sp_knot_update_ctrl(this->knot);
1340     // move knot to the given point
1341     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1342     sp_knot_show (this->knot);
1344     // connect knot's signals
1345     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1346                        // luckily, midstops never snap to other nodes so are never unsnapped...
1347          && ( (draggable->point_type == POINT_LG_MID)
1348               || (draggable->point_type == POINT_RG_MID1)
1349               || (draggable->point_type == POINT_RG_MID2) ) )
1350     {
1351         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1352     } else {
1353         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1354     }
1355     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1356     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1357     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1358     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1360     // add the initial draggable
1361     if (draggable)
1362         this->addDraggable (draggable);
1363     updateKnotShape();
1366 GrDragger::~GrDragger ()
1368     // unselect if it was selected
1369     this->parent->setDeselected(this);
1371     // disconnect signals
1372     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1373     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1374     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1375     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1376     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1378     /* unref should call destroy */
1379     g_object_unref (G_OBJECT (this->knot));
1381     // delete all draggables
1382     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1383         delete ((GrDraggable *) i->data);
1384     }
1385     g_slist_free (this->draggables);
1386     this->draggables = NULL;
1389 /**
1390 Select the dragger which has the given draggable.
1391 */
1392 GrDragger *
1393 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1395     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1396         GrDragger *dragger = (GrDragger *) i->data;
1397         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1398             GrDraggable *da2 = (GrDraggable *) j->data;
1399             if ( (da2->item == item) &&
1400                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1401                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1402                  (da2->fill_or_stroke == fill_or_stroke)) {
1403                 return (dragger);
1404             }
1405         }
1406     }
1407     return NULL;
1411 void
1412 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1414     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1415     if (d && d !=  this) {
1416         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1417     }
1421 /**
1422   Draw this dragger as selected
1423 */
1424 void
1425 GrDragger::select()
1427     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1428     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1431 /**
1432   Draw this dragger as normal (deselected)
1433 */
1434 void
1435 GrDragger::deselect()
1437     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1438     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1441 bool
1442 GrDragger::isSelected()
1444     return g_list_find (parent->selected, this);
1447 /**
1448 \brief Deselect all stops/draggers (private)
1449 */
1450 void
1451 GrDrag::deselect_all()
1453     while (selected) {
1454         ( (GrDragger*) selected->data)->deselect();
1455         selected = g_list_remove(selected, selected->data);
1456     }
1459 /**
1460 \brief Deselect all stops/draggers (public; emits signal)
1461 */
1462 void
1463 GrDrag::deselectAll()
1465     deselect_all();
1466     this->desktop->emitToolSubselectionChanged(NULL);
1469 /**
1470 \brief Select all stops/draggers
1471 */
1472 void
1473 GrDrag::selectAll()
1475     for (GList *l = this->draggers; l != NULL; l = l->next) {
1476         GrDragger *d = ((GrDragger *) l->data);
1477         setSelected (d, true, true);
1478     }
1481 /**
1482 \brief Select all stops/draggers that match the coords
1483 */
1484 void
1485 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1487     for (GList *l = this->draggers; l != NULL; l = l->next) {
1488         GrDragger *d = ((GrDragger *) l->data);
1489         for (guint k = 0; k < coords.size(); k++) {
1490             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1491                 setSelected (d, true, true);
1492             }
1493         }
1494     }
1498 /**
1499 \brief Select all stops/draggers that fall within the rect
1500 */
1501 void
1502 GrDrag::selectRect(Geom::Rect const &r)
1504     for (GList *l = this->draggers; l != NULL; l = l->next) {
1505         GrDragger *d = ((GrDragger *) l->data);
1506         if (r.contains(d->point)) {
1507            setSelected (d, true, true);
1508         }
1509     }
1512 /**
1513 \brief Select a dragger
1514 \param dragger       The dragger to select
1515 \param add_to_selection   If true, add to selection, otherwise deselect others
1516 \param override      If true, always select this node, otherwise toggle selected status
1517 */
1518 void
1519 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1521     GrDragger *seldragger = NULL;
1523     if (add_to_selection) {
1524         if (!dragger) return;
1525         if (override) {
1526             if (!g_list_find(selected, dragger)) {
1527                 selected = g_list_prepend(selected, dragger);
1528             }
1529             dragger->select();
1530             seldragger = dragger;
1531         } else { // toggle
1532             if (g_list_find(selected, dragger)) {
1533                 selected = g_list_remove(selected, dragger);
1534                 dragger->deselect();
1535                 if (selected) {
1536                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1537                 }
1538             } else {
1539                 selected = g_list_prepend(selected, dragger);
1540                 dragger->select();
1541                 seldragger = dragger;
1542             }
1543         }
1544     } else {
1545         deselect_all();
1546         if (dragger) {
1547             selected = g_list_prepend(selected, dragger);
1548             dragger->select();
1549             seldragger = dragger;
1550         }
1551     }
1552     if (seldragger) {
1553         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1554     }
1557 /**
1558 \brief Deselect a dragger
1559 \param dragger       The dragger to deselect
1560 */
1561 void
1562 GrDrag::setDeselected (GrDragger *dragger)
1564     if (g_list_find(selected, dragger)) {
1565         selected = g_list_remove(selected, dragger);
1566         dragger->deselect();
1567     }
1568     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1573 /**
1574 Create a line from p1 to p2 and add it to the lines list
1575  */
1576 void
1577 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1579     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1580                                                             SP_TYPE_CTRLLINE, NULL);
1581     sp_canvas_item_move_to_z(line, 0);
1582     SP_CTRLLINE(line)->item = item;
1583     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1584     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1585         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1586     sp_canvas_item_show (line);
1587     this->lines = g_slist_append (this->lines, line);
1590 /**
1591 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1592 new dragger and add it to draggers list
1593  */
1594 void
1595 GrDrag::addDragger (GrDraggable *draggable)
1597     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1599     for (GList *i = this->draggers; i != NULL; i = i->next) {
1600         GrDragger *dragger = (GrDragger *) i->data;
1601         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1602             // distance is small, merge this draggable into dragger, no need to create new dragger
1603             dragger->addDraggable (draggable);
1604             dragger->updateKnotShape();
1605             return;
1606         }
1607     }
1609     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1610     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1611     this->draggers = g_list_append (this->draggers, new_dragger);
1614 /**
1615 Add draggers for the radial gradient rg on item
1616 */
1617 void
1618 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1620     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1621     guint num = rg->vector.stops.size();
1622     if (num > 2) {
1623         for ( guint i = 1; i < num - 1; i++ ) {
1624             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1625         }
1626     }
1627     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1628     if (num > 2) {
1629         for ( guint i = 1; i < num - 1; i++ ) {
1630             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1631         }
1632     }
1633     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1634     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1637 /**
1638 Add draggers for the linear gradient lg on item
1639 */
1640 void
1641 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1643     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1644     guint num = lg->vector.stops.size();
1645     if (num > 2) {
1646         for ( guint i = 1; i < num - 1; i++ ) {
1647             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1648         }
1649     }
1650     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1653 /**
1654 Artificially grab the knot of this dragger; used by the gradient context
1655 */
1656 void
1657 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1659     if (dragger) {
1660         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1661     }
1664 /**
1665 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1666 */
1667 void
1668 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1670     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1671     if (dragger) {
1672         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1673     }
1676 /**
1677 Regenerates the draggers list from the current selection; is called when selection is changed or
1678 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1679 */
1680 void GrDrag::updateDraggers ()
1682     while (selected) {
1683         selected = g_list_remove(selected, selected->data);
1684     }
1685     // delete old draggers
1686     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1687         delete static_cast<GrDragger *>(i->data);
1688     }
1689     g_list_free(this->draggers);
1690     this->draggers = NULL;
1692     g_return_if_fail(this->selection != NULL);
1694     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1695         SPItem *item = SP_ITEM(i->data);
1696         SPStyle *style = item->style;
1698         if (style && (style->fill.isPaintserver())) {
1699             SPPaintServer *server = style->getFillPaintServer();
1700             if ( server && server->isSolid() ) {
1701                 // Suppress "gradientness" of solid paint
1702             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1703                 addDraggersLinear( SP_LINEARGRADIENT(server), item, true );
1704             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1705                 addDraggersRadial( SP_RADIALGRADIENT(server), item, true );
1706             }
1707         }
1709         if (style && (style->stroke.isPaintserver())) {
1710             SPPaintServer *server = style->getStrokePaintServer();
1711             if ( server && server->isSolid() ) {
1712                 // Suppress "gradientness" of solid paint
1713             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1714                 addDraggersLinear( SP_LINEARGRADIENT(server), item, false );
1715             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1716                 addDraggersRadial( SP_RADIALGRADIENT(server), item, false );
1717             }
1718         }
1719     }
1723 /**
1724  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1725  */
1727 bool
1728 GrDrag::mouseOver()
1730     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1731         GrDragger *d = (GrDragger *) i->data;
1732         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1733             return true;
1734         }
1735     }
1736     return false;
1738 /**
1739 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1740 lines are always in sync with the actual gradient
1741 */
1742 void
1743 GrDrag::updateLines ()
1745     // delete old lines
1746     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1747         gtk_object_destroy( GTK_OBJECT (i->data));
1748     }
1749     g_slist_free (this->lines);
1750     this->lines = NULL;
1752     g_return_if_fail (this->selection != NULL);
1754     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1756         SPItem *item = SP_ITEM(i->data);
1758         SPStyle *style = SP_OBJECT_STYLE (item);
1760         if (style && (style->fill.isPaintserver())) {
1761             SPPaintServer *server = item->style->getFillPaintServer();
1762             if ( server && server->isSolid() ) {
1763                 // Suppress "gradientness" of solid paint
1764             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1765                 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);
1766             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1767                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1768                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1769                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1770             }
1771         }
1773         if (style && (style->stroke.isPaintserver())) {
1774             SPPaintServer *server = item->style->getStrokePaintServer();
1775             if ( server && server->isSolid() ) {
1776                 // Suppress "gradientness" of solid paint
1777             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1778                 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);
1779             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1780                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1781                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1782                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1783             }
1784         }
1785     }
1788 /**
1789 Regenerates the levels list from the current selection
1790 */
1791 void
1792 GrDrag::updateLevels ()
1794     hor_levels.clear();
1795     vert_levels.clear();
1797     g_return_if_fail (this->selection != NULL);
1799     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1800         SPItem *item = SP_ITEM(i->data);
1801         Geom::OptRect rect = sp_item_bbox_desktop (item);
1802         if (rect) {
1803             // Remember the edges of the bbox and the center axis
1804             hor_levels.push_back(rect->min()[Geom::Y]);
1805             hor_levels.push_back(rect->max()[Geom::Y]);
1806             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1807             vert_levels.push_back(rect->min()[Geom::X]);
1808             vert_levels.push_back(rect->max()[Geom::X]);
1809             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1810         }
1811     }
1814 void
1815 GrDrag::selected_reverse_vector ()
1817     if (selected == NULL)
1818         return;
1820     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1821         GrDraggable *draggable = (GrDraggable *) i->data;
1823         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1824     }
1827 void
1828 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1830     selected_move (x, y, false, scale_radial);
1833 void
1834 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1836     if (selected == NULL)
1837         return;
1839     bool did = false;
1841     for (GList *i = selected; i != NULL; i = i->next) {
1842         GrDragger *d = (GrDragger *) i->data;
1844         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1845             // if this is an endpoint,
1847             // Moving an rg center moves its focus and radii as well.
1848             // therefore, if this is a focus or radius and if selection
1849             // contains the center as well, do not move this one
1850             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1851                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1852                 bool skip_radius_with_center = false;
1853                 for (GList *di = selected; di != NULL; di = di->next) {
1854                     GrDragger *d_new = (GrDragger *) di->data;
1855                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1856                                     POINT_RG_CENTER,
1857                                     0,
1858                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1859                         // FIXME: here we take into account only the first draggable!
1860                         skip_radius_with_center = true;
1861                     }
1862                 }
1863                 if (skip_radius_with_center)
1864                     continue;
1865             }
1867             did = true;
1868             d->point += Geom::Point (x, y);
1869             d->point_original = d->point;
1870             sp_knot_moveto (d->knot, d->point);
1872             d->fireDraggables (write_repr, scale_radial);
1874             d->updateDependencies(write_repr);
1875         }
1876     }
1878     if (write_repr && did) {
1879         // we did an undoable action
1880         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1881                                 _("Move gradient handle(s)"));
1882         return;
1883     }
1885     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1887         GrDragger *dragger = (GrDragger *) selected->data;
1888         // a midpoint dragger can (logically) only contain one GrDraggable
1889         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1891         Geom::Point begin(0,0), end(0,0);
1892         Geom::Point low_lim(0,0), high_lim(0,0);
1894         SPObject *server = draggable->getServer();
1895         GSList *moving = NULL;
1896         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1898         Geom::Point p(x, y);
1899         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1900         Geom::Point displacement = p - dragger->point;
1902         for (GSList const* i = moving; i != NULL; i = i->next) {
1903             GrDragger *drg = (GrDragger*) i->data;
1904             SPKnot *drgknot = drg->knot;
1905             drg->point += displacement;
1906             sp_knot_moveto (drgknot, drg->point);
1907             drg->fireDraggables (true);
1908             drg->updateDependencies(true);
1909             did = true;
1910         }
1912         g_slist_free(moving);
1914         if (write_repr && did) {
1915             // we did an undoable action
1916             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1917                                     _("Move gradient mid stop(s)"));
1918         }
1919     }
1922 void
1923 GrDrag::selected_move_screen (double x, double y)
1925     gdouble zoom = desktop->current_zoom();
1926     gdouble zx = x / zoom;
1927     gdouble zy = y / zoom;
1929     selected_move (zx, zy);
1932 /**
1933 Select the knot next to the last selected one and deselect all other selected.
1934 */
1935 GrDragger *
1936 GrDrag::select_next ()
1938     GrDragger *d = NULL;
1939     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1940         if (draggers)
1941             d = (GrDragger *) draggers->data;
1942     } else {
1943         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1944     }
1945     if (d)
1946         setSelected (d);
1947     return d;
1950 /**
1951 Select the knot previous from the last selected one and deselect all other selected.
1952 */
1953 GrDragger *
1954 GrDrag::select_prev ()
1956     GrDragger *d = NULL;
1957     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1958         if (draggers)
1959             d = (GrDragger *) g_list_last (draggers)->data;
1960     } else {
1961         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1962     }
1963     if (d)
1964         setSelected (d);
1965     return d;
1969 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1970 void
1971 GrDrag::deleteSelected (bool just_one)
1973     if (!selected) return;
1975     SPDocument *document = false;
1977     struct StructStopInfo {
1978         SPStop * spstop;
1979         GrDraggable * draggable;
1980         SPGradient * gradient;
1981         SPGradient * vector;
1982     };
1984     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1985     GSList *endstoplist = NULL;  // list of stops that must be deleted
1986     while (selected) {
1987         GrDragger *dragger = (GrDragger*) selected->data;
1988         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1989             GrDraggable *draggable = (GrDraggable*) drgble->data;
1990             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1991             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1993             switch (draggable->point_type) {
1994                 case POINT_LG_MID:
1995                 case POINT_RG_MID1:
1996                 case POINT_RG_MID2:
1997                     {
1998                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1999                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
2000                         bool present = false;
2001                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
2002                             if ( (SPStop*)l->data == stop ) {
2003                                 present = true;
2004                                 break; // no need to search further.
2005                             }
2006                         }
2007                         if (!present)
2008                             midstoplist = g_slist_append(midstoplist, stop);
2009                     }
2010                     break;
2011                 case POINT_LG_BEGIN:
2012                 case POINT_LG_END:
2013                 case POINT_RG_CENTER:
2014                 case POINT_RG_R1:
2015                 case POINT_RG_R2:
2016                     {
2017                         SPStop *stop = NULL;
2018                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
2019                             stop = vector->getFirstStop();
2020                         } else {
2021                             stop = sp_last_stop(vector);
2022                         }
2023                         if (stop) {
2024                             StructStopInfo *stopinfo = new StructStopInfo;
2025                             stopinfo->spstop = stop;
2026                             stopinfo->draggable = draggable;
2027                             stopinfo->gradient = gradient;
2028                             stopinfo->vector = vector;
2029                             // check if already present in list. (e.g. when both R1 and R2 were selected)
2030                             bool present = false;
2031                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
2032                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
2033                                     present = true;
2034                                     break; // no need to search further.
2035                                 }
2036                             }
2037                             if (!present)
2038                                 endstoplist = g_slist_append(endstoplist, stopinfo);
2039                         }
2040                     }
2041                     break;
2042                 default:
2043                     break;
2044             }
2045         }
2046         selected = g_list_remove(selected, dragger);
2047         if ( just_one ) break; // iterate once if just_one is set.
2048     }
2049     while (midstoplist) {
2050         SPStop *stop = (SPStop*) midstoplist->data;
2051         document = SP_OBJECT_DOCUMENT (stop);
2052         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
2053         parent->removeChild(SP_OBJECT_REPR(stop));
2054         midstoplist = g_slist_remove(midstoplist, stop);
2055     }
2056     while (endstoplist) {
2057         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
2058         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2060         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2061         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2062         // manually count the children, don't know if there already exists a function for this...
2063         int len = 0;
2064         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2065               child != NULL ;
2066               child = SP_OBJECT_NEXT(child) )
2067         {
2068             if ( SP_IS_STOP(child) )  len ++;
2069         }
2070         if (len > 2)
2071         {
2072             switch (stopinfo->draggable->point_type) {
2073                 case POINT_LG_BEGIN:
2074                     {
2075                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2077                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2078                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2079                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2080                         SPStop *stop = stopinfo->vector->getFirstStop();
2081                         gdouble offset = stop->offset;
2082                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2083                         lg->x1.computed = newbegin[Geom::X];
2084                         lg->y1.computed = newbegin[Geom::Y];
2086                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2087                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2088                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2089                         stop->offset = 0;
2090                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2092                         // iterate through midstops to set new offset values such that they won't move on canvas.
2093                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2094                         stop = stop->getNextStop();
2095                         while ( stop != laststop ) {
2096                             stop->offset = (stop->offset - offset)/(1 - offset);
2097                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2098                             stop = stop->getNextStop();
2099                         }
2100                     }
2101                     break;
2102                 case POINT_LG_END:
2103                     {
2104                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2106                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2107                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2108                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2109                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2110                         gdouble offset = laststop->offset;
2111                         Geom::Point newend = begin + offset * (oldend - begin);
2112                         lg->x2.computed = newend[Geom::X];
2113                         lg->y2.computed = newend[Geom::Y];
2115                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2116                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2117                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2118                         laststop->offset = 1;
2119                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2121                         // iterate through midstops to set new offset values such that they won't move on canvas.
2122                         SPStop *stop = stopinfo->vector->getFirstStop();
2123                         stop = stop->getNextStop();
2124                         while ( stop != laststop ) {
2125                             stop->offset = stop->offset / offset;
2126                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2127                             stop = stop->getNextStop();
2128                         }
2129                     }
2130                     break;
2131                 case POINT_RG_CENTER:
2132                     {
2133                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2134                         if (newfirst) {
2135                             newfirst->offset = 0;
2136                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2137                         }
2138                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2139                     }
2140                     break;
2141                 case POINT_RG_R1:
2142                 case POINT_RG_R2:
2143                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2145                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2146                         double oldradius = rg->r.computed;
2147                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2148                         gdouble offset = laststop->offset;
2149                         double newradius = offset * oldradius;
2150                         rg->r.computed = newradius;
2152                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2153                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2154                         laststop->offset = 1;
2155                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2157                         // iterate through midstops to set new offset values such that they won't move on canvas.
2158                         SPStop *stop = stopinfo->vector->getFirstStop();
2159                         stop = stop->getNextStop();
2160                         while ( stop != laststop ) {
2161                             stop->offset = stop->offset / offset;
2162                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2163                             stop = stop->getNextStop();
2164                         }
2165                         break;
2166             }
2167         }
2168         else
2169         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2170             SPCSSAttr *css = sp_repr_css_attr_new ();
2172             // stopinfo->spstop is the selected stop
2173             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2174             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2175                 unselectedrepr = unselectedrepr->next();
2176             }
2178             if (unselectedrepr == NULL) {
2179                 if (stopinfo->draggable->fill_or_stroke) {
2180                     sp_repr_css_unset_property (css, "fill");
2181                 } else {
2182                     sp_repr_css_unset_property (css, "stroke");
2183                 }
2184             } else {
2185                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2186                 if (stopinfo->draggable->fill_or_stroke) {
2187                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2188                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2189                 } else {
2190                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2191                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2192                 }
2193                 sp_repr_css_attr_unref (stopcss);
2194             }
2196             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2197             sp_repr_css_attr_unref (css);
2198         }
2200         endstoplist = g_slist_remove(endstoplist, stopinfo);
2201         delete stopinfo;
2202     }
2204     if (document) {
2205         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2206     }
2210 /*
2211   Local Variables:
2212   mode:c++
2213   c-file-style:"stroustrup"
2214   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2215   indent-tabs-mode:nil
2216   fill-column:99
2217   End:
2218 */
2219 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :