Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[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  *   Abhishek Sharma
9  *
10  * Copyright (C) 2007 Johan Engelen
11  * Copyright (C) 2005,2010 Authors
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include <glibmm/i18n.h>
21 #include <cstring>
22 #include <string>
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-style.h"
28 #include "desktop-handles.h"
29 #include "document.h"
30 #include "display/sp-ctrlline.h"
31 #include "display/sp-canvas-util.h"
32 #include "xml/repr.h"
33 #include "svg/css-ostringstream.h"
34 #include "svg/svg.h"
35 #include "libnr/nr-point-fns.h"
36 #include "preferences.h"
37 #include "sp-item.h"
38 #include "style.h"
39 #include "knot.h"
40 #include "sp-linear-gradient.h"
41 #include "sp-radial-gradient.h"
42 #include "gradient-chemistry.h"
43 #include "gradient-drag.h"
44 #include "sp-stop.h"
45 #include "snap.h"
46 #include "sp-namedview.h"
47 #include "selection-chemistry.h"
49 using Inkscape::DocumentUndo;
51 #define GR_KNOT_COLOR_NORMAL 0xffffff00
52 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
53 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
55 #define GR_LINE_COLOR_FILL 0x0000ff7f
56 #define GR_LINE_COLOR_STROKE 0x9999007f
58 // screen pixels between knots when they snap:
59 #define SNAP_DIST 5
61 // absolute distance between gradient points for them to become a single dragger when the drag is created:
62 #define MERGE_DIST 0.1
64 // knot shapes corresponding to GrPointType enum
65 SPKnotShapeType gr_knot_shapes [] = {
66         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
67         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
68         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
69         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
70         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
71         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
72         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
73         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
74         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
75 };
77 const gchar *gr_knot_descr [] = {
78     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
79     N_("Linear gradient <b>end</b>"),
80     N_("Linear gradient <b>mid stop</b>"),
81     N_("Radial gradient <b>center</b>"),
82     N_("Radial gradient <b>radius</b>"),
83     N_("Radial gradient <b>radius</b>"),
84     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
85     N_("Radial gradient <b>mid stop</b>"),
86     N_("Radial gradient <b>mid stop</b>")
87 };
89 static void
90 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
91 {
92     GrDrag *drag = (GrDrag *) data;
93     drag->updateDraggers ();
94     drag->updateLines ();
95     drag->updateLevels ();
96 }
98 static void
99 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
101     GrDrag *drag = (GrDrag *) data;
102     if (drag->local_change) {
103         drag->local_change = false;
104     } else {
105         drag->updateDraggers ();
106     }
107     drag->updateLines ();
108     drag->updateLevels ();
111 /**
112 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
113 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
114 any.
115 */
116 int
117 gr_drag_style_query (SPStyle *style, int property, gpointer data)
119     GrDrag *drag = (GrDrag *) data;
121     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
122         return QUERY_STYLE_NOTHING;
123     }
125     if (!drag->selected) {
126         return QUERY_STYLE_NOTHING;
127     } else {
128         int ret = QUERY_STYLE_NOTHING;
130         float cf[4];
131         cf[0] = cf[1] = cf[2] = cf[3] = 0;
133         int count = 0;
135         for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
136             GrDragger *d = (GrDragger *) i->data;
137             for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
138                 GrDraggable *draggable = (GrDraggable *) j->data;
140                 if (ret == QUERY_STYLE_NOTHING) {
141                     ret = QUERY_STYLE_SINGLE;
142                 } else if (ret == QUERY_STYLE_SINGLE) {
143                     ret = QUERY_STYLE_MULTIPLE_AVERAGED;
144                 }
146                 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
147                 cf[0] += SP_RGBA32_R_F (c);
148                 cf[1] += SP_RGBA32_G_F (c);
149                 cf[2] += SP_RGBA32_B_F (c);
150                 cf[3] += SP_RGBA32_A_F (c);
152                 count ++;
153             }
154         }
156         if (count) {
157             cf[0] /= count;
158             cf[1] /= count;
159             cf[2] /= count;
160             cf[3] /= count;
162             // set both fill and stroke with our stop-color and stop-opacity
163             style->fill.clear();
164             style->fill.setColor( cf[0], cf[1], cf[2] );
165             style->fill.set = TRUE;
166             style->stroke.clear();
167             style->stroke.setColor( cf[0], cf[1], cf[2] );
168             style->stroke.set = TRUE;
170             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
171             style->fill_opacity.set = TRUE;
172             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
173             style->stroke_opacity.set = TRUE;
175             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
176             style->opacity.set = TRUE;
177         }
179         return ret;
180     }
183 Glib::ustring GrDrag::makeStopSafeColor( gchar const *str, bool &isNull )
185     Glib::ustring colorStr;
186     if ( str ) {
187         isNull = false;
188         colorStr = str;
189         Glib::ustring::size_type pos = colorStr.find("url(#");
190         if ( pos != Glib::ustring::npos ) {
191             Glib::ustring targetName = colorStr.substr(pos + 5, colorStr.length() - 6);
192             const GSList *gradients = desktop->doc()->getResourceList("gradient");
193             for (const GSList *item = gradients; item; item = item->next) {
194                 SPGradient* grad = SP_GRADIENT(item->data);
195                 if ( targetName == grad->getId() ) {
196                     SPGradient *vect = grad->getVector();
197                     SPStop *firstStop = (vect) ? vect->getFirstStop() : grad->getFirstStop();
198                     if (firstStop) {
199                         Glib::ustring stopColorStr;
200                         if (firstStop->currentColor) {
201                             stopColorStr = firstStop->getStyleProperty("color", NULL);
202                         } else {
203                             stopColorStr = firstStop->specified_color.toString();
204                         }
205                         if ( !stopColorStr.empty() ) {
206                             colorStr = stopColorStr;
207                         }
208                     }
209                     break;
210                 }
211             }
212         }
213     } else {
214         isNull = true;
215     }
217     return colorStr;
220 bool GrDrag::styleSet( const SPCSSAttr *css )
222     if (!selected) {
223         return false;
224     }
226     SPCSSAttr *stop = sp_repr_css_attr_new();
228     // See if the css contains interesting properties, and if so, translate them into the format
229     // acceptable for gradient stops
231     // any of color properties, in order of increasing priority:
232     if (css->attribute("flood-color")) {
233         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
234     }
236     if (css->attribute("lighting-color")) {
237         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
238     }
240     if (css->attribute("color")) {
241         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
242     }
244     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none")) {
245         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
246     }
248     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none")) {
249         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
250     }
252     if (css->attribute("stop-color")) {
253         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
254     }
256     // Make sure the style is allowed for gradient stops.
257     if ( !sp_repr_css_property_is_unset( stop, "stop-color") ) {
258         bool stopIsNull = false;
259         Glib::ustring tmp = makeStopSafeColor( sp_repr_css_property( stop, "stop-color", "" ), stopIsNull );
260         if ( !stopIsNull && !tmp.empty() ) {
261             sp_repr_css_set_property( stop, "stop-color", tmp.c_str() );
262         }
263     }
266     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
267         sp_repr_css_set_property(stop, "stop-opacity", css->attribute("stop-opacity"));
268     } else {  // multiply all opacity properties:
269         gdouble accumulated = 1.0;
270         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
271         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
272         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
273         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
275         Inkscape::CSSOStringStream os;
276         os << accumulated;
277         sp_repr_css_set_property(stop, "stop-opacity", os.str().c_str());
279         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
280             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none"))) {
281             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
282         }
283     }
285     if (!stop->attributeList()) { // nothing for us here, pass it on
286         sp_repr_css_attr_unref(stop);
287         return false;
288     }
290     for (GList const* sel = selected; sel != NULL; sel = sel->next) { // for all selected draggers
291         GrDragger* dragger = reinterpret_cast<GrDragger*>(sel->data);
292         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
293             GrDraggable *draggable = reinterpret_cast<GrDraggable *>(i->data);
295             local_change = true;
296             sp_item_gradient_stop_set_style(draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
297         }
298     }
300     //sp_repr_css_print(stop);
301     sp_repr_css_attr_unref(stop);
302     return true;
305 guint32 GrDrag::getColor()
307     if (!selected) return 0;
309     float cf[4];
310     cf[0] = cf[1] = cf[2] = cf[3] = 0;
312     int count = 0;
314     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
315         GrDragger *d = (GrDragger *) i->data;
316         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
317             GrDraggable *draggable = (GrDraggable *) j->data;
319             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
320             cf[0] += SP_RGBA32_R_F (c);
321             cf[1] += SP_RGBA32_G_F (c);
322             cf[2] += SP_RGBA32_B_F (c);
323             cf[3] += SP_RGBA32_A_F (c);
325             count ++;
326         }
327     }
329     if (count) {
330         cf[0] /= count;
331         cf[1] /= count;
332         cf[2] /= count;
333         cf[3] /= count;
334     }
336     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
339 SPStop *
340 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
342     gfloat offset; // type of SPStop.offset = gfloat
343     SPGradient *gradient;
344     bool fill_or_stroke = true;
345     bool r1_knot = false;
347     bool addknot = false;
348     do {
349         gradient = sp_item_gradient (item, fill_or_stroke);
350         if (SP_IS_LINEARGRADIENT(gradient)) {
351             Geom::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
352             Geom::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
354             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
355             double dist_screen = Geom::L2 (mouse_p - nearest);
356             if ( dist_screen < tolerance ) {
357                 // add the knot
358                 offset = get_offset_between_points(nearest, begin, end);
359                 addknot = true;
360                 break; // break out of the while loop: add only one knot
361             }
362         } else if (SP_IS_RADIALGRADIENT(gradient)) {
363             Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
364             Geom::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
365             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
366             double dist_screen = Geom::L2 (mouse_p - nearest);
367             if ( dist_screen < tolerance ) {
368                 offset = get_offset_between_points(nearest, begin, end);
369                 addknot = true;
370                 r1_knot = true;
371                 break; // break out of the while loop: add only one knot
372             }
374             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
375             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
376             dist_screen = Geom::L2 (mouse_p - nearest);
377             if ( dist_screen < tolerance ) {
378                 offset = get_offset_between_points(nearest, begin, end);
379                 addknot = true;
380                 r1_knot = false;
381                 break; // break out of the while loop: add only one knot
382             }
383         }
384         fill_or_stroke = !fill_or_stroke;
385     } while (!fill_or_stroke && !addknot) ;
387     if (addknot) {
388         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
389         SPStop* prev_stop = vector->getFirstStop();
390         SPStop* next_stop = prev_stop->getNextStop();
391         guint i = 1;
392         while ( (next_stop) && (next_stop->offset < offset) ) {
393             prev_stop = next_stop;
394             next_stop = next_stop->getNextStop();
395             i++;
396         }
397         if (!next_stop) {
398             // logical error: the endstop should have offset 1 and should always be more than this offset here
399             return NULL;
400         }
403         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
404         gradient->ensureVector();
405         updateDraggers();
407         return newstop;
408     }
410     return NULL;
414 bool
415 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
417     // Note: not sure if a null pointer can come in for the style, but handle that just in case
418     bool stopIsNull = false;
419     Glib::ustring toUse = makeStopSafeColor( c, stopIsNull );
421     // first, see if we can drop onto one of the existing draggers
422     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
423         GrDragger *d = (GrDragger *) i->data;
425         if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
426            SPCSSAttr *stop = sp_repr_css_attr_new ();
427            sp_repr_css_set_property( stop, "stop-color", stopIsNull ? 0 : toUse.c_str() );
428            sp_repr_css_set_property( stop, "stop-opacity", "1" );
429            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
430                GrDraggable *draggable = (GrDraggable *) j->data;
431                local_change = true;
432                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
433            }
434            sp_repr_css_attr_unref(stop);
435            return true;
436         }
437     }
439     // now see if we're over line and create a new stop
440     bool over_line = false;
441     SPCtrlLine *line = NULL;
442     if (lines) {
443         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
444             line = (SPCtrlLine*) l->data;
445             Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
446             double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
447             if (line->item && dist_screen < 5) {
448                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
449                 if (stop) {
450                     SPCSSAttr *css = sp_repr_css_attr_new ();
451                     sp_repr_css_set_property( css, "stop-color", stopIsNull ? 0 : toUse.c_str() );
452                     sp_repr_css_set_property( css, "stop-opacity", "1" );
453                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
454                     return true;
455                 }
456             }
457         }
458     }
460     return false;
464 GrDrag::GrDrag(SPDesktop *desktop) :
465     selected(0),
466     keep_selection(false),
467     local_change(false),
468     desktop(desktop),
469     hor_levels(),
470     vert_levels(),
471     draggers(0),
472     lines(0),
473     selection(sp_desktop_selection(desktop)),
474     sel_changed_connection(),
475     sel_modified_connection(),
476     style_set_connection(),
477     style_query_connection()
479     sel_changed_connection = selection->connectChanged(
480         sigc::bind(
481             sigc::ptr_fun(&gr_drag_sel_changed),
482             (gpointer)this )
484         );
485     sel_modified_connection = selection->connectModified(
486         sigc::bind(
487             sigc::ptr_fun(&gr_drag_sel_modified),
488             (gpointer)this )
489         );
491     style_set_connection = desktop->connectSetStyle( sigc::mem_fun(*this, &GrDrag::styleSet) );
493     style_query_connection = desktop->connectQueryStyle(
494         sigc::bind(
495             sigc::ptr_fun(&gr_drag_style_query),
496             (gpointer)this )
497         );
499     updateDraggers();
500     updateLines();
501     updateLevels();
503     if (desktop->gr_item) {
504         setSelected(getDraggerFor(desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
505     }
508 GrDrag::~GrDrag()
510     this->sel_changed_connection.disconnect();
511     this->sel_modified_connection.disconnect();
512     this->style_set_connection.disconnect();
513     this->style_query_connection.disconnect();
515     if (this->selected) {
516         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
517         desktop->gr_item = draggable->item;
518         desktop->gr_point_type = draggable->point_type;
519         desktop->gr_point_i = draggable->point_i;
520         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
521     } else {
522         desktop->gr_item = NULL;
523         desktop->gr_point_type = 0;
524         desktop->gr_point_i = 0;
525         desktop->gr_fill_or_stroke = true;
526     }
528     deselect_all();
529     for (GList *l = this->draggers; l != NULL; l = l->next) {
530         delete ((GrDragger *) l->data);
531     }
532     g_list_free (this->draggers);
533     this->draggers = NULL;
534     this->selected = NULL;
536     for (GSList *l = this->lines; l != NULL; l = l->next) {
537         gtk_object_destroy( GTK_OBJECT (l->data));
538     }
539     g_slist_free (this->lines);
540     this->lines = NULL;
543 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
545     this->item = item;
546     this->point_type = point_type;
547     this->point_i = point_i;
548     this->fill_or_stroke = fill_or_stroke;
550     g_object_ref (G_OBJECT (this->item));
553 GrDraggable::~GrDraggable ()
555     g_object_unref (G_OBJECT (this->item));
559 SPObject *GrDraggable::getServer()
561     if (!item) {
562         return NULL;
563     }
565     SPObject *server = NULL;
566     if (fill_or_stroke) {
567         server = item->style->getFillPaintServer();
568     }else {
569         server = item->style->getStrokePaintServer();
570     }
572     return server;
575 static void
576 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
578     GrDragger *dragger = (GrDragger *) data;
579     GrDrag *drag = dragger->parent;
581     Geom::Point p = ppointer;
583     SPDesktop *desktop = dragger->parent->desktop;
584     SnapManager &m = desktop->namedview->snap_manager;
585     double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom();
587     if (state & GDK_SHIFT_MASK) {
588         // with Shift; unsnap if we carry more than one draggable
589         if (dragger->draggables && dragger->draggables->next) {
590             // create a new dragger
591             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
592             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
593             // relink to it all but the first draggable in the list
594             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
595                 GrDraggable *draggable = (GrDraggable *) i->data;
596                 dr_new->addDraggable (draggable);
597             }
598             dr_new->updateKnotShape();
599             g_slist_free (dragger->draggables->next);
600             dragger->draggables->next = NULL;
601             dragger->updateKnotShape();
602             dragger->updateTip();
603         }
604     } else if (!(state & GDK_CONTROL_MASK)) {
605         // without Shift or Ctrl; see if we need to snap to another dragger
606         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
607             GrDragger *d_new = (GrDragger *) di->data;
608             if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
610                 // Merge draggers:
611                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
612                     GrDraggable *draggable = (GrDraggable *) i->data;
613                     // copy draggable to d_new:
614                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
615                     d_new->addDraggable (da_new);
616                 }
618                 // unlink and delete this dragger
619                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
620                 delete dragger;
622                 // update the new merged dragger
623                 d_new->fireDraggables(true, false, true);
624                 d_new->parent->updateLines();
625                 d_new->parent->setSelected (d_new);
626                 d_new->updateKnotShape ();
627                 d_new->updateTip ();
628                 d_new->updateDependencies(true);
629                 DocumentUndo::done(sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
630                                    _("Merge gradient handles"));
631                 return;
632             }
633         }
634     }
636     if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
637         m.setup(desktop);
638         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
639         m.unSetup();
640         if (s.getSnapped()) {
641             p = s.getPoint();
642             sp_knot_moveto (knot, p);
643         }
644     } else if (state & GDK_CONTROL_MASK) {
645         SnappedConstraints sc;
646         Inkscape::SnapCandidatePoint scp = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
647         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
648         unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
649         /* 0 means no snapping. */
651         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
652             GrDraggable *draggable = (GrDraggable *) i->data;
654             Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
656             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
657                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
658                     GrDragger *d_new = (GrDragger *) di->data;
659                     if (d_new == dragger)
660                         continue;
661                     if (d_new->isA (draggable->item,
662                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
663                                     draggable->fill_or_stroke)) {
664                         // found the other end of the linear gradient;
665                         if (state & GDK_SHIFT_MASK) {
666                             // moving linear around center
667                             Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
668                             dr_snap = center;
669                         } else {
670                             // moving linear around the other end
671                             dr_snap = d_new->point;
672                         }
673                     }
674                 }
675             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
676                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
677                     GrDragger *d_new = (GrDragger *) di->data;
678                     if (d_new == dragger)
679                         continue;
680                     if (d_new->isA (draggable->item,
681                                     POINT_RG_CENTER,
682                                     draggable->fill_or_stroke)) {
683                         // found the center of the radial gradient;
684                         dr_snap = d_new->point;
685                     }
686                 }
687             } else if (draggable->point_type == POINT_RG_CENTER) {
688                 // radial center snaps to hor/vert relative to its original position
689                 dr_snap = dragger->point_original;
690             }
692             // dr_snap contains the origin of the gradient, whereas p will be the new endpoint which we will try to snap now
693             Inkscape::SnappedPoint sp;
694             if (dr_snap.isFinite()) {
695                 m.setup(desktop);
696                 if (state & GDK_MOD1_MASK) {
697                     // with Alt, snap to the original angle and its perpendiculars
698                     sp = m.constrainedAngularSnap(scp, dragger->point_original, dr_snap, 2);
699                 } else {
700                     // with Ctrl, snap to M_PI/snaps
701                     sp = m.constrainedAngularSnap(scp, boost::optional<Geom::Point>(), dr_snap, snaps);
702                 }
703                 m.unSetup();
704                 sc.points.push_back(sp);
705             }
706         }
708         m.setup(desktop, false); // turn of the snap indicator temporarily
709         Inkscape::SnappedPoint bsp = m.findBestSnap(scp, sc, true);
710         m.unSetup();
711         if (!bsp.getSnapped()) {
712             // If we didn't truly snap to an object or to a grid, then we will still have to look for the
713             // closest projection onto one of the constraints. findBestSnap() will not do this for us
714             for (std::list<Inkscape::SnappedPoint>::const_iterator i = sc.points.begin(); i != sc.points.end(); i++) {
715                 if (i == sc.points.begin() || (Geom::L2((*i).getPoint() - p) < Geom::L2(bsp.getPoint() - p))) {
716                     bsp.setPoint((*i).getPoint());
717                     bsp.setTarget(Inkscape::SNAPTARGET_CONSTRAINED_ANGLE);
718                 }
719             }
720         }
721         //p = sc.points.front().getPoint();
722         p = bsp.getPoint();
723         sp_knot_moveto (knot, p);
724     }
726     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
727     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
729     if (drag->keep_selection) {
730         Geom::Point diff = p - dragger->point;
731         drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
732     } else {
733         dragger->point = p;
734         dragger->fireDraggables (false, scale_radial);
735         dragger->updateDependencies(false);
736     }
741 static void
742 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
745     GrDrag *drag = dragger->parent;
746     // a midpoint dragger can (logically) only contain one GrDraggable
747     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
749     // get begin and end points between which dragging is allowed:
750     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
751     *moving = g_slist_append(*moving, dragger);
753     guint lowest_i = draggable->point_i;
754     guint highest_i = draggable->point_i;
755     GrDragger *lowest_dragger = dragger;
756     GrDragger *highest_dragger = dragger;
757     if (dragger->isSelected()) {
758         GrDragger* d_add;
759         while ( true )
760         {
761             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
762             if ( d_add && g_list_find(drag->selected, d_add) ) {
763                 lowest_i = lowest_i - 1;
764                 *moving = g_slist_prepend(*moving, d_add);
765                 lowest_dragger = d_add;
766             } else {
767                 break;
768             }
769         }
771         while ( true )
772         {
773             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
774             if ( d_add && g_list_find(drag->selected, d_add) ) {
775                 highest_i = highest_i + 1;
776                 *moving = g_slist_append(*moving, d_add);
777                 highest_dragger = d_add;
778             } else {
779                 break;
780             }
781         }
782     }
784     if ( SP_IS_LINEARGRADIENT(server) ) {
785         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
786         GrDragger *d_temp;
787         if (lowest_i == 1) {
788             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
789         } else {
790             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
791         }
792         if (d_temp)
793             *begin = d_temp->point;
795         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
796         if (d_temp == NULL) {
797             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
798         }
799         if (d_temp)
800             *end = d_temp->point;
801     } else if ( SP_IS_RADIALGRADIENT(server) ) {
802         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
803         GrDragger *d_temp;
804         if (lowest_i == 1) {
805             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
806         } else {
807             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
808         }
809         if (d_temp)
810             *begin = d_temp->point;
812         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
813         if (d_temp == NULL) {
814             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
815         }
816         if (d_temp)
817             *end = d_temp->point;
818     }
820     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
821     *high_lim = dragger->point - (highest_dragger->point - *end);
826 /**
827 Called when a midpoint knot is dragged.
828 */
829 static void
830 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
832     GrDragger *dragger = (GrDragger *) data;
833     GrDrag *drag = dragger->parent;
834     // a midpoint dragger can (logically) only contain one GrDraggable
835     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
837     // FIXME: take from prefs
838     double snap_fraction = 0.1;
840     Geom::Point p = ppointer;
841     Geom::Point begin(0,0), end(0,0);
842     Geom::Point low_lim(0,0), high_lim(0,0);
844     SPObject *server = draggable->getServer();
846     GSList *moving = NULL;
847     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
849     if (state & GDK_CONTROL_MASK) {
850         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
851     } else {
852         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
853         if (!(state & GDK_SHIFT_MASK)) {
854             Inkscape::Snapper::SnapConstraint cl(low_lim, high_lim - low_lim);
855             SPDesktop *desktop = dragger->parent->desktop;
856             SnapManager &m = desktop->namedview->snap_manager;
857             m.setup(desktop);
858             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
859             m.unSetup();
860         }
861     }
862     Geom::Point displacement = p - dragger->point;
864     for (GSList const* i = moving; i != NULL; i = i->next) {
865         GrDragger *drg = (GrDragger*) i->data;
866         SPKnot *drgknot = drg->knot;
867         Geom::Point this_move = displacement;
868         if (state & GDK_MOD1_MASK) {
869             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
870             double alpha = 1.0;
871             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
872                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
873                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
874             } else { // drg is on the begin side from dragger
875                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
876                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
877             }
878         }
879         drg->point += this_move;
880         sp_knot_moveto (drgknot, drg->point);
881         drg->fireDraggables (false);
882         drg->updateDependencies(false);
883     }
885     g_slist_free(moving);
887     drag->keep_selection = dragger->isSelected();
892 static void
893 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
895     GrDragger *dragger = (GrDragger *) data;
897     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
900 /**
901 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
902 */
903 static void
904 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
906     GrDragger *dragger = (GrDragger *) data;
908     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
910     dragger->point_original = dragger->point = knot->pos;
912     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
913         dragger->fireDraggables (true, true);
914     } else {
915         dragger->fireDraggables (true);
916     }
918     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
919         GrDragger *d = (GrDragger *) i->data;
920         if (d == dragger)
921             continue;
922         d->fireDraggables (true);
923     }
925     // make this dragger selected
926     if (!dragger->parent->keep_selection) {
927         dragger->parent->setSelected (dragger);
928     }
929     dragger->parent->keep_selection = false;
931     dragger->updateDependencies(true);
933     // we did an undoable action
934     DocumentUndo::done(sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
935                        _("Move gradient handle"));
938 /**
939 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
940 state of the keyboard keys
941 */
942 static void
943 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
945     GrDragger *dragger = (GrDragger *) data;
946     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
947     if (!draggable) return;
949     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
950     // delete this knot from vector
951         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
952         gradient = gradient->getVector();
953         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
954             SPStop *stop = NULL;
955             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
956             case POINT_LG_BEGIN:
957             case POINT_RG_CENTER:
958                 stop = gradient->getFirstStop();
959                 {
960                     SPStop *next = stop->getNextStop();
961                     if (next) {
962                         next->offset = 0;
963                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
964                     }
965                 }
966                 break;
967             case POINT_LG_END:
968             case POINT_RG_R1:
969             case POINT_RG_R2:
970                 stop = sp_last_stop(gradient);
971                 {
972                     SPStop *prev = stop->getPrevStop();
973                     if (prev) {
974                         prev->offset = 1;
975                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
976                     }
977                 }
978                 break;
979             case POINT_LG_MID:
980             case POINT_RG_MID1:
981             case POINT_RG_MID2:
982                 stop = sp_get_stop_i(gradient, draggable->point_i);
983                 break;
984             }
986             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
987             DocumentUndo::done(SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
988                                _("Delete gradient stop"));
989         }
990     } else {
991     // select the dragger
992         dragger->point_original = dragger->point;
994         if ( state & GDK_SHIFT_MASK ) {
995             dragger->parent->setSelected (dragger, true, false);
996         } else {
997             dragger->parent->setSelected (dragger);
998         }
999     }
1002 /**
1003 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
1004 */
1005 static void
1006 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
1008     GrDragger *dragger = (GrDragger *) data;
1010     dragger->point_original = dragger->point;
1012     if (dragger->draggables == NULL)
1013         return;
1015     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1016     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1019 /**
1020 Act upon all draggables of the dragger, setting them to the dragger's point
1021 */
1022 void
1023 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1025     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1026         GrDraggable *draggable = (GrDraggable *) i->data;
1028         // set local_change flag so that selection_changed callback does not regenerate draggers
1029         this->parent->local_change = true;
1031         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1032         // to the center, unless it's the first update upon merge when we must snap it to the point
1033         if (merging_focus ||
1034             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1035         {
1036             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1037         }
1038     }
1041 /**
1042 Checks if the dragger has a draggable with this point_type
1043  */
1044 bool
1045 GrDragger::isA (gint point_type)
1047     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1048         GrDraggable *draggable = (GrDraggable *) i->data;
1049         if (draggable->point_type == point_type) {
1050             return true;
1051         }
1052     }
1053     return false;
1056 /**
1057 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1058  */
1059 bool
1060 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1062     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1063         GrDraggable *draggable = (GrDraggable *) i->data;
1064         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1065             return true;
1066         }
1067     }
1068     return false;
1071 /**
1072 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1073  */
1074 bool
1075 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1077     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1078         GrDraggable *draggable = (GrDraggable *) i->data;
1079         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1080             return true;
1081         }
1082     }
1083     return false;
1086 bool
1087 GrDraggable::mayMerge (GrDraggable *da2)
1089     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1090         // we must not merge the points of the same gradient!
1091         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1092               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1093             // except that we can snap center and focus together
1094             return false;
1095         }
1096     }
1097     // disable merging of midpoints.
1098     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1099          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1100          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1101         return false;
1103     return true;
1106 bool
1107 GrDragger::mayMerge (GrDragger *other)
1109     if (this == other)
1110         return false;
1112     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1113         GrDraggable *da1 = (GrDraggable *) i->data;
1114         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1115             GrDraggable *da2 = (GrDraggable *) j->data;
1116             if (!da1->mayMerge(da2))
1117                 return false;
1118         }
1119     }
1120     return true;
1123 bool
1124 GrDragger::mayMerge (GrDraggable *da2)
1126     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1127         GrDraggable *da1 = (GrDraggable *) i->data;
1128         if (!da1->mayMerge(da2))
1129             return false;
1130     }
1131     return true;
1134 /**
1135 Updates the statusbar tip of the dragger knot, based on its draggables
1136  */
1137 void
1138 GrDragger::updateTip ()
1140     if (this->knot && this->knot->tip) {
1141         g_free (this->knot->tip);
1142         this->knot->tip = NULL;
1143     }
1145     if (g_slist_length (this->draggables) == 1) {
1146         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1147         char *item_desc = draggable->item->description();
1148         switch (draggable->point_type) {
1149             case POINT_LG_MID:
1150             case POINT_RG_MID1:
1151             case POINT_RG_MID2:
1152                 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"),
1153                                                    _(gr_knot_descr[draggable->point_type]),
1154                                                    draggable->point_i,
1155                                                    item_desc,
1156                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1157                 break;
1159             default:
1160                 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"),
1161                                                    _(gr_knot_descr[draggable->point_type]),
1162                                                    item_desc,
1163                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1164                 break;
1165         }
1166         g_free(item_desc);
1167     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1168         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1169     } else {
1170         int length = g_slist_length (this->draggables);
1171         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1172                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1173                                                     length),
1174                                            length);
1175     }
1178 /**
1179 Adds a draggable to the dragger
1180  */
1181 void
1182 GrDragger::updateKnotShape ()
1184     if (!draggables)
1185         return;
1186     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1187     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1190 /**
1191 Adds a draggable to the dragger
1192  */
1193 void
1194 GrDragger::addDraggable (GrDraggable *draggable)
1196     this->draggables = g_slist_prepend (this->draggables, draggable);
1198     this->updateTip();
1202 /**
1203 Moves this dragger to the point of the given draggable, acting upon all other draggables
1204  */
1205 void
1206 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1208     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1209     if (!dr_first) return;
1211     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1212     this->point_original = this->point;
1214     sp_knot_moveto (this->knot, this->point);
1216     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1217         GrDraggable *da = (GrDraggable *) i->data;
1218         if ( (da->item == item) &&
1219              (point_type == -1 || da->point_type == point_type) &&
1220              (point_i == -1 || da->point_i == point_i) &&
1221              (da->fill_or_stroke == fill_or_stroke) ) {
1222             continue;
1223         }
1224         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1225     }
1226     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1230 /**
1231 Moves all midstop draggables that depend on this one
1232  */
1233 void
1234 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1235     SPObject *server = draggable->getServer();
1236     if (!server)
1237         return;
1238     guint num = SP_GRADIENT(server)->vector.stops.size();
1239     if (num <= 2) return;
1241     if ( SP_IS_LINEARGRADIENT(server) ) {
1242         for ( guint i = 1; i < num - 1; i++ ) {
1243             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1244         }
1245     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1246         for ( guint i = 1; i < num - 1; i++ ) {
1247             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1248             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1249         }
1250     }
1254 /**
1255 Moves all draggables that depend on this one
1256  */
1257 void
1258 GrDragger::updateDependencies (bool write_repr)
1260     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1261         GrDraggable *draggable = (GrDraggable *) i->data;
1262         switch (draggable->point_type) {
1263             case POINT_LG_BEGIN:
1264                 {
1265                     // the end point is dependent only when dragging with ctrl+shift
1266                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1268                     this->updateMidstopDependencies (draggable, write_repr);
1269                 }
1270                 break;
1271             case POINT_LG_END:
1272                 {
1273                     // the begin point is dependent only when dragging with ctrl+shift
1274                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1276                     this->updateMidstopDependencies (draggable, write_repr);
1277                 }
1278                 break;
1279             case POINT_LG_MID:
1280                 // no other nodes depend on mid points.
1281                 break;
1282             case POINT_RG_R2:
1283                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1284                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1285                 this->updateMidstopDependencies (draggable, write_repr);
1286                 break;
1287             case POINT_RG_R1:
1288                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1289                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1290                 this->updateMidstopDependencies (draggable, write_repr);
1291                 break;
1292             case POINT_RG_CENTER:
1293                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1294                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1295                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1296                 this->updateMidstopDependencies (draggable, write_repr);
1297                 break;
1298             case POINT_RG_FOCUS:
1299                 // nothing can depend on that
1300                 break;
1301             case POINT_RG_MID1:
1302                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1303                 break;
1304             case POINT_RG_MID2:
1305                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1306                 break;
1307             default:
1308                 break;
1309         }
1310     }
1315 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1316   : point(p),
1317     point_original(p)
1319     this->draggables = NULL;
1321     this->parent = parent;
1323     // create the knot
1324     this->knot = sp_knot_new (parent->desktop, NULL);
1325     this->knot->setMode(SP_KNOT_MODE_XOR);
1326     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1327     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1328     sp_knot_update_ctrl(this->knot);
1330     // move knot to the given point
1331     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1332     sp_knot_show (this->knot);
1334     // connect knot's signals
1335     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1336                        // luckily, midstops never snap to other nodes so are never unsnapped...
1337          && ( (draggable->point_type == POINT_LG_MID)
1338               || (draggable->point_type == POINT_RG_MID1)
1339               || (draggable->point_type == POINT_RG_MID2) ) )
1340     {
1341         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1342     } else {
1343         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1344     }
1345     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1346     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1347     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1348     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1350     // add the initial draggable
1351     if (draggable)
1352         this->addDraggable (draggable);
1353     updateKnotShape();
1356 GrDragger::~GrDragger ()
1358     // unselect if it was selected
1359     this->parent->setDeselected(this);
1361     // disconnect signals
1362     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1363     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1364     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1365     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1366     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1368     /* unref should call destroy */
1369     g_object_unref (G_OBJECT (this->knot));
1371     // delete all draggables
1372     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1373         delete ((GrDraggable *) i->data);
1374     }
1375     g_slist_free (this->draggables);
1376     this->draggables = NULL;
1379 /**
1380 Select the dragger which has the given draggable.
1381 */
1382 GrDragger *
1383 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1385     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1386         GrDragger *dragger = (GrDragger *) i->data;
1387         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1388             GrDraggable *da2 = (GrDraggable *) j->data;
1389             if ( (da2->item == item) &&
1390                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1391                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1392                  (da2->fill_or_stroke == fill_or_stroke)) {
1393                 return (dragger);
1394             }
1395         }
1396     }
1397     return NULL;
1401 void
1402 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1404     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1405     if (d && d !=  this) {
1406         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1407     }
1411 /**
1412   Draw this dragger as selected
1413 */
1414 void
1415 GrDragger::select()
1417     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1418     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1421 /**
1422   Draw this dragger as normal (deselected)
1423 */
1424 void
1425 GrDragger::deselect()
1427     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1428     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1431 bool
1432 GrDragger::isSelected()
1434     return g_list_find (parent->selected, this);
1437 /**
1438 \brief Deselect all stops/draggers (private)
1439 */
1440 void
1441 GrDrag::deselect_all()
1443     while (selected) {
1444         ( (GrDragger*) selected->data)->deselect();
1445         selected = g_list_remove(selected, selected->data);
1446     }
1449 /**
1450 \brief Deselect all stops/draggers (public; emits signal)
1451 */
1452 void
1453 GrDrag::deselectAll()
1455     deselect_all();
1456     this->desktop->emitToolSubselectionChanged(NULL);
1459 /**
1460 \brief Select all stops/draggers
1461 */
1462 void
1463 GrDrag::selectAll()
1465     for (GList *l = this->draggers; l != NULL; l = l->next) {
1466         GrDragger *d = ((GrDragger *) l->data);
1467         setSelected (d, true, true);
1468     }
1471 /**
1472 \brief Select all stops/draggers that match the coords
1473 */
1474 void
1475 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1477     for (GList *l = this->draggers; l != NULL; l = l->next) {
1478         GrDragger *d = ((GrDragger *) l->data);
1479         for (guint k = 0; k < coords.size(); k++) {
1480             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1481                 setSelected (d, true, true);
1482             }
1483         }
1484     }
1488 /**
1489 \brief Select all stops/draggers that fall within the rect
1490 */
1491 void
1492 GrDrag::selectRect(Geom::Rect const &r)
1494     for (GList *l = this->draggers; l != NULL; l = l->next) {
1495         GrDragger *d = ((GrDragger *) l->data);
1496         if (r.contains(d->point)) {
1497            setSelected (d, true, true);
1498         }
1499     }
1502 /**
1503 \brief Select a dragger
1504 \param dragger       The dragger to select
1505 \param add_to_selection   If true, add to selection, otherwise deselect others
1506 \param override      If true, always select this node, otherwise toggle selected status
1507 */
1508 void
1509 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1511     GrDragger *seldragger = NULL;
1513     if (add_to_selection) {
1514         if (!dragger) return;
1515         if (override) {
1516             if (!g_list_find(selected, dragger)) {
1517                 selected = g_list_prepend(selected, dragger);
1518             }
1519             dragger->select();
1520             seldragger = dragger;
1521         } else { // toggle
1522             if (g_list_find(selected, dragger)) {
1523                 selected = g_list_remove(selected, dragger);
1524                 dragger->deselect();
1525                 if (selected) {
1526                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1527                 }
1528             } else {
1529                 selected = g_list_prepend(selected, dragger);
1530                 dragger->select();
1531                 seldragger = dragger;
1532             }
1533         }
1534     } else {
1535         deselect_all();
1536         if (dragger) {
1537             selected = g_list_prepend(selected, dragger);
1538             dragger->select();
1539             seldragger = dragger;
1540         }
1541     }
1542     if (seldragger) {
1543         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1544     }
1547 /**
1548 \brief Deselect a dragger
1549 \param dragger       The dragger to deselect
1550 */
1551 void
1552 GrDrag::setDeselected (GrDragger *dragger)
1554     if (g_list_find(selected, dragger)) {
1555         selected = g_list_remove(selected, dragger);
1556         dragger->deselect();
1557     }
1558     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1563 /**
1564 Create a line from p1 to p2 and add it to the lines list
1565  */
1566 void
1567 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1569     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1570                                                             SP_TYPE_CTRLLINE, NULL);
1571     sp_canvas_item_move_to_z(line, 0);
1572     SP_CTRLLINE(line)->item = item;
1573     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1574     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1575         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1576     sp_canvas_item_show (line);
1577     this->lines = g_slist_append (this->lines, line);
1580 /**
1581 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1582 new dragger and add it to draggers list
1583  */
1584 void
1585 GrDrag::addDragger (GrDraggable *draggable)
1587     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1589     for (GList *i = this->draggers; i != NULL; i = i->next) {
1590         GrDragger *dragger = (GrDragger *) i->data;
1591         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1592             // distance is small, merge this draggable into dragger, no need to create new dragger
1593             dragger->addDraggable (draggable);
1594             dragger->updateKnotShape();
1595             return;
1596         }
1597     }
1599     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1600     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1601     this->draggers = g_list_append (this->draggers, new_dragger);
1604 /**
1605 Add draggers for the radial gradient rg on item
1606 */
1607 void
1608 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1610     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1611     guint num = rg->vector.stops.size();
1612     if (num > 2) {
1613         for ( guint i = 1; i < num - 1; i++ ) {
1614             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1615         }
1616     }
1617     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1618     if (num > 2) {
1619         for ( guint i = 1; i < num - 1; i++ ) {
1620             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1621         }
1622     }
1623     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1624     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1627 /**
1628 Add draggers for the linear gradient lg on item
1629 */
1630 void
1631 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1633     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1634     guint num = lg->vector.stops.size();
1635     if (num > 2) {
1636         for ( guint i = 1; i < num - 1; i++ ) {
1637             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1638         }
1639     }
1640     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1643 /**
1644 Artificially grab the knot of this dragger; used by the gradient context
1645 */
1646 void
1647 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1649     if (dragger) {
1650         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1651     }
1654 /**
1655 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1656 */
1657 void
1658 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1660     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1661     if (dragger) {
1662         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1663     }
1666 /**
1667 Regenerates the draggers list from the current selection; is called when selection is changed or
1668 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1669 */
1670 void GrDrag::updateDraggers ()
1672     while (selected) {
1673         selected = g_list_remove(selected, selected->data);
1674     }
1675     // delete old draggers
1676     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1677         delete static_cast<GrDragger *>(i->data);
1678     }
1679     g_list_free(this->draggers);
1680     this->draggers = NULL;
1682     g_return_if_fail(this->selection != NULL);
1684     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1685         SPItem *item = SP_ITEM(i->data);
1686         SPStyle *style = item->style;
1688         if (style && (style->fill.isPaintserver())) {
1689             SPPaintServer *server = style->getFillPaintServer();
1690             if ( server && server->isSolid() ) {
1691                 // Suppress "gradientness" of solid paint
1692             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1693                 addDraggersLinear( SP_LINEARGRADIENT(server), item, true );
1694             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1695                 addDraggersRadial( SP_RADIALGRADIENT(server), item, true );
1696             }
1697         }
1699         if (style && (style->stroke.isPaintserver())) {
1700             SPPaintServer *server = style->getStrokePaintServer();
1701             if ( server && server->isSolid() ) {
1702                 // Suppress "gradientness" of solid paint
1703             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1704                 addDraggersLinear( SP_LINEARGRADIENT(server), item, false );
1705             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1706                 addDraggersRadial( SP_RADIALGRADIENT(server), item, false );
1707             }
1708         }
1709     }
1713 /**
1714  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1715  */
1717 bool
1718 GrDrag::mouseOver()
1720     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1721         GrDragger *d = (GrDragger *) i->data;
1722         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1723             return true;
1724         }
1725     }
1726     return false;
1728 /**
1729 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1730 lines are always in sync with the actual gradient
1731 */
1732 void
1733 GrDrag::updateLines ()
1735     // delete old lines
1736     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1737         gtk_object_destroy( GTK_OBJECT (i->data));
1738     }
1739     g_slist_free (this->lines);
1740     this->lines = NULL;
1742     g_return_if_fail (this->selection != NULL);
1744     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1746         SPItem *item = SP_ITEM(i->data);
1748         SPStyle *style = SP_OBJECT_STYLE (item);
1750         if (style && (style->fill.isPaintserver())) {
1751             SPPaintServer *server = item->style->getFillPaintServer();
1752             if ( server && server->isSolid() ) {
1753                 // Suppress "gradientness" of solid paint
1754             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1755                 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);
1756             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1757                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1758                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1759                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1760             }
1761         }
1763         if (style && (style->stroke.isPaintserver())) {
1764             SPPaintServer *server = item->style->getStrokePaintServer();
1765             if ( server && server->isSolid() ) {
1766                 // Suppress "gradientness" of solid paint
1767             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1768                 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);
1769             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1770                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1771                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1772                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1773             }
1774         }
1775     }
1778 /**
1779 Regenerates the levels list from the current selection
1780 */
1781 void
1782 GrDrag::updateLevels ()
1784     hor_levels.clear();
1785     vert_levels.clear();
1787     g_return_if_fail (this->selection != NULL);
1789     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1790         SPItem *item = SP_ITEM(i->data);
1791         Geom::OptRect rect = item->getBboxDesktop ();
1792         if (rect) {
1793             // Remember the edges of the bbox and the center axis
1794             hor_levels.push_back(rect->min()[Geom::Y]);
1795             hor_levels.push_back(rect->max()[Geom::Y]);
1796             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1797             vert_levels.push_back(rect->min()[Geom::X]);
1798             vert_levels.push_back(rect->max()[Geom::X]);
1799             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1800         }
1801     }
1804 void
1805 GrDrag::selected_reverse_vector ()
1807     if (selected == NULL)
1808         return;
1810     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1811         GrDraggable *draggable = (GrDraggable *) i->data;
1813         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1814     }
1817 void
1818 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1820     selected_move (x, y, false, scale_radial);
1823 void
1824 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1826     if (selected == NULL)
1827         return;
1829     bool did = false;
1831     for (GList *i = selected; i != NULL; i = i->next) {
1832         GrDragger *d = (GrDragger *) i->data;
1834         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1835             // if this is an endpoint,
1837             // Moving an rg center moves its focus and radii as well.
1838             // therefore, if this is a focus or radius and if selection
1839             // contains the center as well, do not move this one
1840             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1841                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1842                 bool skip_radius_with_center = false;
1843                 for (GList *di = selected; di != NULL; di = di->next) {
1844                     GrDragger *d_new = (GrDragger *) di->data;
1845                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1846                                     POINT_RG_CENTER,
1847                                     0,
1848                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1849                         // FIXME: here we take into account only the first draggable!
1850                         skip_radius_with_center = true;
1851                     }
1852                 }
1853                 if (skip_radius_with_center)
1854                     continue;
1855             }
1857             did = true;
1858             d->point += Geom::Point (x, y);
1859             d->point_original = d->point;
1860             sp_knot_moveto (d->knot, d->point);
1862             d->fireDraggables (write_repr, scale_radial);
1864             d->updateDependencies(write_repr);
1865         }
1866     }
1868     if (write_repr && did) {
1869         // we did an undoable action
1870         DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1871                                 _("Move gradient handle(s)"));
1872         return;
1873     }
1875     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1877         GrDragger *dragger = (GrDragger *) selected->data;
1878         // a midpoint dragger can (logically) only contain one GrDraggable
1879         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1881         Geom::Point begin(0,0), end(0,0);
1882         Geom::Point low_lim(0,0), high_lim(0,0);
1884         SPObject *server = draggable->getServer();
1885         GSList *moving = NULL;
1886         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1888         Geom::Point p(x, y);
1889         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1890         Geom::Point displacement = p - dragger->point;
1892         for (GSList const* i = moving; i != NULL; i = i->next) {
1893             GrDragger *drg = (GrDragger*) i->data;
1894             SPKnot *drgknot = drg->knot;
1895             drg->point += displacement;
1896             sp_knot_moveto (drgknot, drg->point);
1897             drg->fireDraggables (true);
1898             drg->updateDependencies(true);
1899             did = true;
1900         }
1902         g_slist_free(moving);
1904         if (write_repr && did) {
1905             // we did an undoable action
1906             DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1907                                     _("Move gradient mid stop(s)"));
1908         }
1909     }
1912 void
1913 GrDrag::selected_move_screen (double x, double y)
1915     gdouble zoom = desktop->current_zoom();
1916     gdouble zx = x / zoom;
1917     gdouble zy = y / zoom;
1919     selected_move (zx, zy);
1922 /**
1923 Select the knot next to the last selected one and deselect all other selected.
1924 */
1925 GrDragger *
1926 GrDrag::select_next ()
1928     GrDragger *d = NULL;
1929     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1930         if (draggers)
1931             d = (GrDragger *) draggers->data;
1932     } else {
1933         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1934     }
1935     if (d)
1936         setSelected (d);
1937     return d;
1940 /**
1941 Select the knot previous from the last selected one and deselect all other selected.
1942 */
1943 GrDragger *
1944 GrDrag::select_prev ()
1946     GrDragger *d = NULL;
1947     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1948         if (draggers)
1949             d = (GrDragger *) g_list_last (draggers)->data;
1950     } else {
1951         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1952     }
1953     if (d)
1954         setSelected (d);
1955     return d;
1959 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1960 void
1961 GrDrag::deleteSelected (bool just_one)
1963     if (!selected) return;
1965     SPDocument *document = false;
1967     struct StructStopInfo {
1968         SPStop * spstop;
1969         GrDraggable * draggable;
1970         SPGradient * gradient;
1971         SPGradient * vector;
1972     };
1974     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1975     GSList *endstoplist = NULL;  // list of stops that must be deleted
1976     while (selected) {
1977         GrDragger *dragger = (GrDragger*) selected->data;
1978         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1979             GrDraggable *draggable = (GrDraggable*) drgble->data;
1980             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1981             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1983             switch (draggable->point_type) {
1984                 case POINT_LG_MID:
1985                 case POINT_RG_MID1:
1986                 case POINT_RG_MID2:
1987                     {
1988                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1989                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1990                         bool present = false;
1991                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1992                             if ( (SPStop*)l->data == stop ) {
1993                                 present = true;
1994                                 break; // no need to search further.
1995                             }
1996                         }
1997                         if (!present)
1998                             midstoplist = g_slist_append(midstoplist, stop);
1999                     }
2000                     break;
2001                 case POINT_LG_BEGIN:
2002                 case POINT_LG_END:
2003                 case POINT_RG_CENTER:
2004                 case POINT_RG_R1:
2005                 case POINT_RG_R2:
2006                     {
2007                         SPStop *stop = NULL;
2008                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
2009                             stop = vector->getFirstStop();
2010                         } else {
2011                             stop = sp_last_stop(vector);
2012                         }
2013                         if (stop) {
2014                             StructStopInfo *stopinfo = new StructStopInfo;
2015                             stopinfo->spstop = stop;
2016                             stopinfo->draggable = draggable;
2017                             stopinfo->gradient = gradient;
2018                             stopinfo->vector = vector;
2019                             // check if already present in list. (e.g. when both R1 and R2 were selected)
2020                             bool present = false;
2021                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
2022                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
2023                                     present = true;
2024                                     break; // no need to search further.
2025                                 }
2026                             }
2027                             if (!present)
2028                                 endstoplist = g_slist_append(endstoplist, stopinfo);
2029                         }
2030                     }
2031                     break;
2032                 default:
2033                     break;
2034             }
2035         }
2036         selected = g_list_remove(selected, dragger);
2037         if ( just_one ) break; // iterate once if just_one is set.
2038     }
2039     while (midstoplist) {
2040         SPStop *stop = (SPStop*) midstoplist->data;
2041         document = SP_OBJECT_DOCUMENT (stop);
2042         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
2043         parent->removeChild(SP_OBJECT_REPR(stop));
2044         midstoplist = g_slist_remove(midstoplist, stop);
2045     }
2046     while (endstoplist) {
2047         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
2048         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2050         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2051         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2052         // manually count the children, don't know if there already exists a function for this...
2053         int len = 0;
2054         for ( SPObject *child = (stopinfo->vector)->firstChild() ; child ; child = child->getNext() )
2055         {
2056             if ( SP_IS_STOP(child) ) {
2057                 len ++;
2058             }
2059         }
2060         if (len > 2)
2061         {
2062             switch (stopinfo->draggable->point_type) {
2063                 case POINT_LG_BEGIN:
2064                     {
2065                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2067                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2068                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2069                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2070                         SPStop *stop = stopinfo->vector->getFirstStop();
2071                         gdouble offset = stop->offset;
2072                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2073                         lg->x1.computed = newbegin[Geom::X];
2074                         lg->y1.computed = newbegin[Geom::Y];
2076                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2077                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2078                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2079                         stop->offset = 0;
2080                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2082                         // iterate through midstops to set new offset values such that they won't move on canvas.
2083                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2084                         stop = stop->getNextStop();
2085                         while ( stop != laststop ) {
2086                             stop->offset = (stop->offset - offset)/(1 - offset);
2087                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2088                             stop = stop->getNextStop();
2089                         }
2090                     }
2091                     break;
2092                 case POINT_LG_END:
2093                     {
2094                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2096                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2097                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2098                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2099                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2100                         gdouble offset = laststop->offset;
2101                         Geom::Point newend = begin + offset * (oldend - begin);
2102                         lg->x2.computed = newend[Geom::X];
2103                         lg->y2.computed = newend[Geom::Y];
2105                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2106                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2107                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2108                         laststop->offset = 1;
2109                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2111                         // iterate through midstops to set new offset values such that they won't move on canvas.
2112                         SPStop *stop = stopinfo->vector->getFirstStop();
2113                         stop = stop->getNextStop();
2114                         while ( stop != laststop ) {
2115                             stop->offset = stop->offset / offset;
2116                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2117                             stop = stop->getNextStop();
2118                         }
2119                     }
2120                     break;
2121                 case POINT_RG_CENTER:
2122                     {
2123                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2124                         if (newfirst) {
2125                             newfirst->offset = 0;
2126                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2127                         }
2128                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2129                     }
2130                     break;
2131                 case POINT_RG_R1:
2132                 case POINT_RG_R2:
2133                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2135                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2136                         double oldradius = rg->r.computed;
2137                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2138                         gdouble offset = laststop->offset;
2139                         double newradius = offset * oldradius;
2140                         rg->r.computed = newradius;
2142                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2143                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2144                         laststop->offset = 1;
2145                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2147                         // iterate through midstops to set new offset values such that they won't move on canvas.
2148                         SPStop *stop = stopinfo->vector->getFirstStop();
2149                         stop = stop->getNextStop();
2150                         while ( stop != laststop ) {
2151                             stop->offset = stop->offset / offset;
2152                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2153                             stop = stop->getNextStop();
2154                         }
2155                         break;
2156             }
2157         }
2158         else
2159         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2160             SPCSSAttr *css = sp_repr_css_attr_new ();
2162             // stopinfo->spstop is the selected stop
2163             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2164             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2165                 unselectedrepr = unselectedrepr->next();
2166             }
2168             if (unselectedrepr == NULL) {
2169                 if (stopinfo->draggable->fill_or_stroke) {
2170                     sp_repr_css_unset_property (css, "fill");
2171                 } else {
2172                     sp_repr_css_unset_property (css, "stroke");
2173                 }
2174             } else {
2175                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2176                 if (stopinfo->draggable->fill_or_stroke) {
2177                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2178                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2179                 } else {
2180                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2181                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2182                 }
2183                 sp_repr_css_attr_unref (stopcss);
2184             }
2186             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2187             sp_repr_css_attr_unref (css);
2188         }
2190         endstoplist = g_slist_remove(endstoplist, stopinfo);
2191         delete stopinfo;
2192     }
2194     if (document) {
2195         DocumentUndo::done( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2196     }
2200 /*
2201   Local Variables:
2202   mode:c++
2203   c-file-style:"stroustrup"
2204   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2205   indent-tabs-mode:nil
2206   fill-column:99
2207   End:
2208 */
2209 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :