Code

From trunk
[inkscape.git] / src / gradient-drag.cpp
1 #define __GRADIENT_DRAG_C__
3 /*
4  * On-canvas gradient dragging
5  *
6  * Authors:
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2007 Johan Engelen
11  * Copyright (C) 2005 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 "document.h"
29 #include "display/sp-ctrlline.h"
30 #include "display/sp-canvas-util.h"
31 #include "xml/repr.h"
32 #include "svg/css-ostringstream.h"
33 #include "svg/svg.h"
34 #include "libnr/nr-point-fns.h"
35 #include "preferences.h"
36 #include "sp-item.h"
37 #include "style.h"
38 #include "knot.h"
39 #include "sp-linear-gradient.h"
40 #include "sp-radial-gradient.h"
41 #include "gradient-chemistry.h"
42 #include "gradient-drag.h"
43 #include "sp-stop.h"
44 #include "snap.h"
45 #include "sp-namedview.h"
46 #include "selection-chemistry.h"
48 #define GR_KNOT_COLOR_NORMAL 0xffffff00
49 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
50 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
52 #define GR_LINE_COLOR_FILL 0x0000ff7f
53 #define GR_LINE_COLOR_STROKE 0x9999007f
55 // screen pixels between knots when they snap:
56 #define SNAP_DIST 5
58 // absolute distance between gradient points for them to become a single dragger when the drag is created:
59 #define MERGE_DIST 0.1
61 // knot shapes corresponding to GrPointType enum
62 SPKnotShapeType gr_knot_shapes [] = {
63         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
64         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
65         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
66         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
67         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
68         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
69         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
70         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
71         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
72 };
74 const gchar *gr_knot_descr [] = {
75     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
76     N_("Linear gradient <b>end</b>"),
77     N_("Linear gradient <b>mid stop</b>"),
78     N_("Radial gradient <b>center</b>"),
79     N_("Radial gradient <b>radius</b>"),
80     N_("Radial gradient <b>radius</b>"),
81     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
82     N_("Radial gradient <b>mid stop</b>"),
83     N_("Radial gradient <b>mid stop</b>")
84 };
86 static void
87 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
88 {
89     GrDrag *drag = (GrDrag *) data;
90     drag->updateDraggers ();
91     drag->updateLines ();
92     drag->updateLevels ();
93 }
95 static void
96 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
97 {
98     GrDrag *drag = (GrDrag *) data;
99     if (drag->local_change) {
100         drag->local_change = false;
101     } else {
102         drag->updateDraggers ();
103     }
104     drag->updateLines ();
105     drag->updateLevels ();
108 /**
109 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
110 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
111 any.
112 */
113 int
114 gr_drag_style_query (SPStyle *style, int property, gpointer data)
116     GrDrag *drag = (GrDrag *) data;
118     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
119         return QUERY_STYLE_NOTHING;
120     }
122     if (!drag->selected) {
123         return QUERY_STYLE_NOTHING;
124     } else {
125         int ret = QUERY_STYLE_NOTHING;
127         float cf[4];
128         cf[0] = cf[1] = cf[2] = cf[3] = 0;
130         int count = 0;
132         for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
133             GrDragger *d = (GrDragger *) i->data;
134             for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
135                 GrDraggable *draggable = (GrDraggable *) j->data;
137                 if (ret == QUERY_STYLE_NOTHING) {
138                     ret = QUERY_STYLE_SINGLE;
139                 } else if (ret == QUERY_STYLE_SINGLE) {
140                     ret = QUERY_STYLE_MULTIPLE_AVERAGED;
141                 }
143                 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
144                 cf[0] += SP_RGBA32_R_F (c);
145                 cf[1] += SP_RGBA32_G_F (c);
146                 cf[2] += SP_RGBA32_B_F (c);
147                 cf[3] += SP_RGBA32_A_F (c);
149                 count ++;
150             }
151         }
153         if (count) {
154             cf[0] /= count;
155             cf[1] /= count;
156             cf[2] /= count;
157             cf[3] /= count;
159             // set both fill and stroke with our stop-color and stop-opacity
160             style->fill.clear();
161             style->fill.setColor( cf[0], cf[1], cf[2] );
162             style->fill.set = TRUE;
163             style->stroke.clear();
164             style->stroke.setColor( cf[0], cf[1], cf[2] );
165             style->stroke.set = TRUE;
167             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
168             style->fill_opacity.set = TRUE;
169             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
170             style->stroke_opacity.set = TRUE;
172             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
173             style->opacity.set = TRUE;
174         }
176         return ret;
177     }
180 bool
181 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
183     GrDrag *drag = (GrDrag *) data;
185     if (!drag->selected)
186         return false;
188     SPCSSAttr *stop = sp_repr_css_attr_new ();
190     // See if the css contains interesting properties, and if so, translate them into the format
191     // acceptable for gradient stops
193     // any of color properties, in order of increasing priority:
194     if (css->attribute("flood-color"))
195         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
197     if (css->attribute("lighting-color"))
198         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
200     if (css->attribute("color"))
201         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
203     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
204         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
206     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
207         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
209     if (css->attribute("stop-color"))
210         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
213     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
214         sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
215     } else {  // multiply all opacity properties:
216         gdouble accumulated = 1.0;
217         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
218         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
219         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
220         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
222         Inkscape::CSSOStringStream os;
223         os << accumulated;
224         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
226         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
227             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
228             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
229     }
231     if (!stop->attributeList()) { // nothing for us here, pass it on
232         sp_repr_css_attr_unref(stop);
233         return false;
234     }
236     for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
237         GrDragger* dragger = (GrDragger*) sel->data;
238         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
239                GrDraggable *draggable = (GrDraggable *) i->data;
241                drag->local_change = true;
242                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
243         }
244     }
246     //sp_repr_css_print(stop);
247     sp_repr_css_attr_unref(stop);
248     return true;
251 guint32 GrDrag::getColor()
253     if (!selected) return 0;
255     float cf[4];
256     cf[0] = cf[1] = cf[2] = cf[3] = 0;
258     int count = 0;
260     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
261         GrDragger *d = (GrDragger *) i->data;
262         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
263             GrDraggable *draggable = (GrDraggable *) j->data;
265             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
266             cf[0] += SP_RGBA32_R_F (c);
267             cf[1] += SP_RGBA32_G_F (c);
268             cf[2] += SP_RGBA32_B_F (c);
269             cf[3] += SP_RGBA32_A_F (c);
271             count ++;
272         }
273     }
275     if (count) {
276         cf[0] /= count;
277         cf[1] /= count;
278         cf[2] /= count;
279         cf[3] /= count;
280     }
282     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
285 SPStop *
286 GrDrag::addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance)
288     gfloat offset; // type of SPStop.offset = gfloat
289     SPGradient *gradient;
290     bool fill_or_stroke = true;
291     bool r1_knot = false;
293     bool addknot = false;
294     do {
295         gradient = sp_item_gradient (item, fill_or_stroke);
296         if (SP_IS_LINEARGRADIENT(gradient)) {
297             NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
298             NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
300             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
301             double dist_screen = NR::L2 (mouse_p - nearest);
302             if ( dist_screen < tolerance ) {
303                 // add the knot
304                 offset = get_offset_between_points(nearest, begin, end);
305                 addknot = true;
306                 break; // break out of the while loop: add only one knot
307             }
308         } else if (SP_IS_RADIALGRADIENT(gradient)) {
309             NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
310             NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
311             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
312             double dist_screen = NR::L2 (mouse_p - nearest);
313             if ( dist_screen < tolerance ) {
314                 offset = get_offset_between_points(nearest, begin, end);
315                 addknot = true;
316                 r1_knot = true;
317                 break; // break out of the while loop: add only one knot
318             }
320             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
321             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
322             dist_screen = NR::L2 (mouse_p - nearest);
323             if ( dist_screen < tolerance ) {
324                 offset = get_offset_between_points(nearest, begin, end);
325                 addknot = true;
326                 r1_knot = false;
327                 break; // break out of the while loop: add only one knot
328             }
329         }
330         fill_or_stroke = !fill_or_stroke;
331     } while (!fill_or_stroke && !addknot) ;
333     if (addknot) {
334         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
335         SPStop* prev_stop = sp_first_stop(vector);
336         SPStop* next_stop = sp_next_stop(prev_stop);
337         guint i = 1;
338         while ( (next_stop) && (next_stop->offset < offset) ) {
339             prev_stop = next_stop;
340             next_stop = sp_next_stop(next_stop);
341             i++;
342         }
343         if (!next_stop) {
344             // logical error: the endstop should have offset 1 and should always be more than this offset here
345             return NULL;
346         }
349         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
350         sp_gradient_ensure_vector (gradient);
351         updateDraggers();
353         return newstop;
354     }
356     return NULL;
360 bool
361 GrDrag::dropColor(SPItem */*item*/, gchar *c, NR::Point p)
363     // first, see if we can drop onto one of the existing draggers
364     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
365         GrDragger *d = (GrDragger *) i->data;
367         if (NR::L2(p - d->point)*desktop->current_zoom() < 5) {
368            SPCSSAttr *stop = sp_repr_css_attr_new ();
369            sp_repr_css_set_property (stop, "stop-color", c);
370            sp_repr_css_set_property (stop, "stop-opacity", "1");
371            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
372                GrDraggable *draggable = (GrDraggable *) j->data;
373                local_change = true;
374                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
375            }
376            sp_repr_css_attr_unref(stop);
377            return true;
378         }
379     }
381     // now see if we're over line and create a new stop
382     bool over_line = false;
383     SPCtrlLine *line = NULL;
384     if (lines) {
385         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
386             line = (SPCtrlLine*) l->data;
387             NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
388             double dist_screen = NR::L2 (p - nearest) * desktop->current_zoom();
389             if (line->item && dist_screen < 5) {
390                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
391                 if (stop) {
392                     SPCSSAttr *css = sp_repr_css_attr_new ();
393                     sp_repr_css_set_property (css, "stop-color", c);
394                     sp_repr_css_set_property (css, "stop-opacity", "1");
395                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
396                     return true;
397                 }
398             }
399         }
400     }
402     return false;
406 GrDrag::GrDrag(SPDesktop *desktop) {
408     this->desktop = desktop;
410     this->selection = sp_desktop_selection(desktop);
412     this->draggers = NULL;
413     this->lines = NULL;
414     this->selected = NULL;
416     this->hor_levels.clear();
417     this->vert_levels.clear();
419     this->local_change = false;
421     this->sel_changed_connection = this->selection->connectChanged(
422         sigc::bind (
423             sigc::ptr_fun(&gr_drag_sel_changed),
424             (gpointer)this )
426         );
427     this->sel_modified_connection = this->selection->connectModified(
428         sigc::bind(
429             sigc::ptr_fun(&gr_drag_sel_modified),
430             (gpointer)this )
431         );
433     this->style_set_connection = this->desktop->connectSetStyle(
434         sigc::bind(
435             sigc::ptr_fun(&gr_drag_style_set),
436             (gpointer)this )
437         );
439     this->style_query_connection = this->desktop->connectQueryStyle(
440         sigc::bind(
441             sigc::ptr_fun(&gr_drag_style_query),
442             (gpointer)this )
443         );
445     this->updateDraggers ();
446     this->updateLines ();
447     this->updateLevels ();
449     if (desktop->gr_item) {
450         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
451     }
454 GrDrag::~GrDrag()
456     this->sel_changed_connection.disconnect();
457     this->sel_modified_connection.disconnect();
458     this->style_set_connection.disconnect();
459     this->style_query_connection.disconnect();
461     if (this->selected) {
462         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
463         desktop->gr_item = draggable->item;
464         desktop->gr_point_type = draggable->point_type;
465         desktop->gr_point_i = draggable->point_i;
466         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
467     } else {
468         desktop->gr_item = NULL;
469         desktop->gr_point_type = 0;
470         desktop->gr_point_i = 0;
471         desktop->gr_fill_or_stroke = true;
472     }
474     deselect_all();
475     for (GList *l = this->draggers; l != NULL; l = l->next) {
476         delete ((GrDragger *) l->data);
477     }
478     g_list_free (this->draggers);
479     this->draggers = NULL;
480     this->selected = NULL;
482     for (GSList *l = this->lines; l != NULL; l = l->next) {
483         gtk_object_destroy( GTK_OBJECT (l->data));
484     }
485     g_slist_free (this->lines);
486     this->lines = NULL;
489 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
491     this->item = item;
492     this->point_type = point_type;
493     this->point_i = point_i;
494     this->fill_or_stroke = fill_or_stroke;
496     g_object_ref (G_OBJECT (this->item));
499 GrDraggable::~GrDraggable ()
501     g_object_unref (G_OBJECT (this->item));
505 SPObject *
506 GrDraggable::getServer ()
508     if (!item)
509         return NULL;
511     SPObject *server = NULL;
512     if (fill_or_stroke)
513         server = SP_OBJECT_STYLE_FILL_SERVER (item);
514     else
515         server = SP_OBJECT_STYLE_STROKE_SERVER (item);
517     return server;
520 static void
521 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
523     GrDragger *dragger = (GrDragger *) data;
524     GrDrag *drag = dragger->parent;
526     NR::Point p = *ppointer;
528     // FIXME: take from prefs
529     double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
531     if (state & GDK_SHIFT_MASK) {
532         // with Shift; unsnap if we carry more than one draggable
533         if (dragger->draggables && dragger->draggables->next) {
534             // create a new dragger
535             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
536             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
537             // relink to it all but the first draggable in the list
538             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
539                 GrDraggable *draggable = (GrDraggable *) i->data;
540                 dr_new->addDraggable (draggable);
541             }
542             dr_new->updateKnotShape();
543             g_slist_free (dragger->draggables->next);
544             dragger->draggables->next = NULL;
545             dragger->updateKnotShape();
546             dragger->updateTip();
547         }
548     } else if (!(state & GDK_CONTROL_MASK)) {
549         // without Shift or Ctrl; see if we need to snap to another dragger
550         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
551             GrDragger *d_new = (GrDragger *) di->data;
552             if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
554                 // Merge draggers:
555                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
556                     GrDraggable *draggable = (GrDraggable *) i->data;
557                     // copy draggable to d_new:
558                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
559                     d_new->addDraggable (da_new);
560                 }
562                 // unlink and delete this dragger
563                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
564                 delete dragger;
566                 // update the new merged dragger
567                 d_new->fireDraggables(true, false, true);
568                 d_new->parent->updateLines();
569                 d_new->parent->setSelected (d_new);
570                 d_new->updateKnotShape ();
571                 d_new->updateTip ();
572                 d_new->updateDependencies(true);
573                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
574                                   _("Merge gradient handles"));
575                 return;
576             }
577         }
578     }
580     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
581         // Try snapping to the grid or guides
582         SPDesktop *desktop = dragger->parent->desktop;
583         SnapManager &m = desktop->namedview->snap_manager;
584         m.setup(desktop);
585         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapPreferences::SNAPPOINT_NODE, to_2geom(p));
586         if (s.getSnapped()) {
587             p = s.getPoint();
588             sp_knot_moveto (knot, p);
589         } else {
590             bool was_snapped = false;
591             double dist = NR_HUGE;
592             // No snapping so far, let's see if we need to snap to any of the levels
593             for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
594                 dist = fabs(p[NR::Y] - dragger->parent->hor_levels[i]);
595                 if (dist < snap_dist) {
596                     p[NR::Y] = dragger->parent->hor_levels[i];
597                     s = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_GRADIENT, dist, snap_dist, false, false);
598                     was_snapped = true;
599                     sp_knot_moveto (knot, p);
600                 }
601             }
602             for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
603                 dist = fabs(p[NR::X] - dragger->parent->vert_levels[i]);
604                 if (dist < snap_dist) {
605                     p[NR::X] = dragger->parent->vert_levels[i];
606                     s = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_GRADIENT, dist, snap_dist, false, false);
607                     was_snapped = true;
608                     sp_knot_moveto (knot, p);
609                 }
610             }
611             if (was_snapped) {
612                 desktop->snapindicator->set_new_snappoint(s);
613             }
614         }
615     }
617     if (state & GDK_CONTROL_MASK) {
618         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
619         unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
620         /* 0 means no snapping. */
622         // This list will store snap vectors from all draggables of dragger
623         GSList *snap_vectors = NULL;
625         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
626             GrDraggable *draggable = (GrDraggable *) i->data;
628             NR::Point *dr_snap = NULL;
630             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
631                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
632                     GrDragger *d_new = (GrDragger *) di->data;
633                     if (d_new == dragger)
634                         continue;
635                     if (d_new->isA (draggable->item,
636                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
637                                     draggable->fill_or_stroke)) {
638                         // found the other end of the linear gradient;
639                         if (state & GDK_SHIFT_MASK) {
640                             // moving linear around center
641                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
642                             dr_snap = &center;
643                         } else {
644                             // moving linear around the other end
645                             dr_snap = &d_new->point;
646                         }
647                     }
648                 }
649             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
650                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
651                     GrDragger *d_new = (GrDragger *) di->data;
652                     if (d_new == dragger)
653                         continue;
654                     if (d_new->isA (draggable->item,
655                                     POINT_RG_CENTER,
656                                     draggable->fill_or_stroke)) {
657                         // found the center of the radial gradient;
658                         dr_snap = &(d_new->point);
659                     }
660                 }
661             } else if (draggable->point_type == POINT_RG_CENTER) {
662                 // radial center snaps to hor/vert relative to its original position
663                 dr_snap = &(dragger->point_original);
664             }
666             NR::Point *snap_vector = NULL;
667             if (dr_snap) {
668                 if (state & GDK_MOD1_MASK) {
669                     // with Alt, snap to the original angle and its perpendiculars
670                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
671                 } else {
672                     // with Ctrl, snap to M_PI/snaps
673                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
674                 }
675             }
676             if (snap_vector) {
677                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
678             }
679         }
681         // Move by the smallest of snap vectors:
682         NR::Point move(9999, 9999);
683         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
684             NR::Point *snap_vector = (NR::Point *) i->data;
685             if (NR::L2(*snap_vector) < NR::L2(move))
686                 move = *snap_vector;
687         }
688         if (move[NR::X] < 9999) {
689             p += move;
690             sp_knot_moveto (knot, p);
691         }
693         g_slist_free(snap_vectors);
694     }
696     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
697     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
699     if (drag->keep_selection) {
700         NR::Point diff = p - dragger->point;
701         drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
702     } else {
703         dragger->point = p;
704         dragger->fireDraggables (false, scale_radial);
705         dragger->updateDependencies(false);
706     }
711 static void
712 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
715     GrDrag *drag = dragger->parent;
716     // a midpoint dragger can (logically) only contain one GrDraggable
717     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
719     // get begin and end points between which dragging is allowed:
720     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
721     *moving = g_slist_append(*moving, dragger);
723     guint lowest_i = draggable->point_i;
724     guint highest_i = draggable->point_i;
725     GrDragger *lowest_dragger = dragger;
726     GrDragger *highest_dragger = dragger;
727     if (dragger->isSelected()) {
728         GrDragger* d_add;
729         while ( true )
730         {
731             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
732             if ( d_add && g_list_find(drag->selected, d_add) ) {
733                 lowest_i = lowest_i - 1;
734                 *moving = g_slist_prepend(*moving, d_add);
735                 lowest_dragger = d_add;
736             } else {
737                 break;
738             }
739         }
741         while ( true )
742         {
743             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
744             if ( d_add && g_list_find(drag->selected, d_add) ) {
745                 highest_i = highest_i + 1;
746                 *moving = g_slist_append(*moving, d_add);
747                 highest_dragger = d_add;
748             } else {
749                 break;
750             }
751         }
752     }
754     if ( SP_IS_LINEARGRADIENT(server) ) {
755         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
756         GrDragger *d_temp;
757         if (lowest_i == 1) {
758             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
759         } else {
760             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
761         }
762         if (d_temp)
763             *begin = d_temp->point;
765         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
766         if (d_temp == NULL) {
767             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
768         }
769         if (d_temp)
770             *end = d_temp->point;
771     } else if ( SP_IS_RADIALGRADIENT(server) ) {
772         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
773         GrDragger *d_temp;
774         if (lowest_i == 1) {
775             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
776         } else {
777             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
778         }
779         if (d_temp)
780             *begin = d_temp->point;
782         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
783         if (d_temp == NULL) {
784             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
785         }
786         if (d_temp)
787             *end = d_temp->point;
788     }
790     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
791     *high_lim = dragger->point - (highest_dragger->point - *end);
796 /**
797 Called when a midpoint knot is dragged.
798 */
799 static void
800 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, NR::Point const *ppointer, guint state, gpointer data)
802     GrDragger *dragger = (GrDragger *) data;
803     GrDrag *drag = dragger->parent;
804     // a midpoint dragger can (logically) only contain one GrDraggable
805     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
807     // FIXME: take from prefs
808     double snap_fraction = 0.1;
810     NR::Point p = *ppointer;
811     NR::Point begin(0,0), end(0,0);
812     NR::Point low_lim(0,0), high_lim(0,0);
814     SPObject *server = draggable->getServer();
816     GSList *moving = NULL;
817     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
819     if (state & GDK_CONTROL_MASK) {
820         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
821     } else {
822         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
823     }
824     NR::Point displacement = p - dragger->point;
826     for (GSList const* i = moving; i != NULL; i = i->next) {
827         GrDragger *drg = (GrDragger*) i->data;
828         SPKnot *drgknot = drg->knot;
829         NR::Point this_move = displacement;
830         if (state & GDK_MOD1_MASK) {
831             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
832             double alpha = 1.0;
833             if (NR::L2(drg->point - dragger->point) + NR::L2(drg->point - begin) - 1e-3 > NR::L2(dragger->point - begin)) { // drg is on the end side from dragger
834                 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
835                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
836             } else { // drg is on the begin side from dragger
837                 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
838                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
839             }
840         }
841         drg->point += this_move;
842         sp_knot_moveto (drgknot, drg->point);
843         drg->fireDraggables (false);
844         drg->updateDependencies(false);
845     }
847     g_slist_free(moving);
849     drag->keep_selection = dragger->isSelected();
854 static void
855 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
857     GrDragger *dragger = (GrDragger *) data;
859     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
862 /**
863 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
864 */
865 static void
866 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
868     GrDragger *dragger = (GrDragger *) data;
870     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
872     dragger->point_original = dragger->point = knot->pos;
874     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
875         dragger->fireDraggables (true, true);
876     } else {
877         dragger->fireDraggables (true);
878     }
880     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
881         GrDragger *d = (GrDragger *) i->data;
882         if (d == dragger)
883             continue;
884         d->fireDraggables (true);
885     }
887     // make this dragger selected
888     if (!dragger->parent->keep_selection) {
889         dragger->parent->setSelected (dragger);
890     }
891     dragger->parent->keep_selection = false;
893     dragger->updateDependencies(true);
895     // we did an undoable action
896     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
897                       _("Move gradient handle"));
900 /**
901 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
902 state of the keyboard keys
903 */
904 static void
905 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
907     GrDragger *dragger = (GrDragger *) data;
908     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
909     if (!draggable) return;
911     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
912     // delete this knot from vector
913         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
914         gradient = sp_gradient_get_vector (gradient, false);
915         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
916                 SPStop *stop = NULL;
917                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
918                 case POINT_LG_BEGIN:
919                 case POINT_RG_CENTER:
920                     stop = sp_first_stop(gradient);
921                         {
922                             SPStop *next = sp_next_stop (stop);
923                                 if (next) {
924                                         next->offset = 0;
925                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
926                                 }
927                         }
928                     break;
929                 case POINT_LG_END:
930                 case POINT_RG_R1:
931                 case POINT_RG_R2:
932                     stop = sp_last_stop(gradient);
933                     {
934                             SPStop *prev = sp_prev_stop (stop, gradient);
935                             if (prev) {
936                                     prev->offset = 1;
937                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
938                             }
939                         }
940                     break;
941                 case POINT_LG_MID:
942                 case POINT_RG_MID1:
943                 case POINT_RG_MID2:
944                     stop = sp_get_stop_i(gradient, draggable->point_i);
945                     break;
946                 }
948                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
949                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
950                                   _("Delete gradient stop"));
951         }
952     } else {
953     // select the dragger
954         dragger->point_original = dragger->point;
956         if ( state & GDK_SHIFT_MASK ) {
957             dragger->parent->setSelected (dragger, true, false);
958         } else {
959             dragger->parent->setSelected (dragger);
960         }
961     }
964 /**
965 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
966 */
967 static void
968 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
970     GrDragger *dragger = (GrDragger *) data;
972     dragger->point_original = dragger->point;
974     if (dragger->draggables == NULL)
975         return;
977     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
978     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
981 /**
982 Act upon all draggables of the dragger, setting them to the dragger's point
983 */
984 void
985 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
987     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
988         GrDraggable *draggable = (GrDraggable *) i->data;
990         // set local_change flag so that selection_changed callback does not regenerate draggers
991         this->parent->local_change = true;
993         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
994         // to the center, unless it's the first update upon merge when we must snap it to the point
995         if (merging_focus ||
996             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
997         {
998             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
999         }
1000     }
1003 /**
1004 Checks if the dragger has a draggable with this point_type
1005  */
1006 bool
1007 GrDragger::isA (gint point_type)
1009     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1010         GrDraggable *draggable = (GrDraggable *) i->data;
1011         if (draggable->point_type == point_type) {
1012             return true;
1013         }
1014     }
1015     return false;
1018 /**
1019 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1020  */
1021 bool
1022 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1024     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1025         GrDraggable *draggable = (GrDraggable *) i->data;
1026         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1027             return true;
1028         }
1029     }
1030     return false;
1033 /**
1034 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1035  */
1036 bool
1037 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1039     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1040         GrDraggable *draggable = (GrDraggable *) i->data;
1041         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1042             return true;
1043         }
1044     }
1045     return false;
1048 bool
1049 GrDraggable::mayMerge (GrDraggable *da2)
1051     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1052         // we must not merge the points of the same gradient!
1053         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1054               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1055             // except that we can snap center and focus together
1056             return false;
1057         }
1058     }
1059     // disable merging of midpoints.
1060     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1061          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1062          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1063         return false;
1065     return true;
1068 bool
1069 GrDragger::mayMerge (GrDragger *other)
1071     if (this == other)
1072         return false;
1074     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1075         GrDraggable *da1 = (GrDraggable *) i->data;
1076         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1077             GrDraggable *da2 = (GrDraggable *) j->data;
1078             if (!da1->mayMerge(da2))
1079                 return false;
1080         }
1081     }
1082     return true;
1085 bool
1086 GrDragger::mayMerge (GrDraggable *da2)
1088     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1089         GrDraggable *da1 = (GrDraggable *) i->data;
1090         if (!da1->mayMerge(da2))
1091             return false;
1092     }
1093     return true;
1096 /**
1097 Updates the statusbar tip of the dragger knot, based on its draggables
1098  */
1099 void
1100 GrDragger::updateTip ()
1102         if (this->knot && this->knot->tip) {
1103                 g_free (this->knot->tip);
1104                 this->knot->tip = NULL;
1105         }
1107     if (g_slist_length (this->draggables) == 1) {
1108         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1109         char *item_desc = sp_item_description(draggable->item);
1110         switch (draggable->point_type) {
1111             case POINT_LG_MID:
1112             case POINT_RG_MID1:
1113             case POINT_RG_MID2:
1114                 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"),
1115                                                    _(gr_knot_descr[draggable->point_type]),
1116                                                    draggable->point_i,
1117                                                    item_desc,
1118                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1119                 break;
1121             default:
1122                 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"),
1123                                                    _(gr_knot_descr[draggable->point_type]),
1124                                                    item_desc,
1125                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1126                 break;
1127         }
1128         g_free(item_desc);
1129     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1130         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1131     } else {
1132         int length = g_slist_length (this->draggables);
1133         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1134                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1135                                                     length),
1136                                            length);
1137     }
1140 /**
1141 Adds a draggable to the dragger
1142  */
1143 void
1144 GrDragger::updateKnotShape ()
1146     if (!draggables)
1147         return;
1148     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1149     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1152 /**
1153 Adds a draggable to the dragger
1154  */
1155 void
1156 GrDragger::addDraggable (GrDraggable *draggable)
1158     this->draggables = g_slist_prepend (this->draggables, draggable);
1160     this->updateTip();
1164 /**
1165 Moves this dragger to the point of the given draggable, acting upon all other draggables
1166  */
1167 void
1168 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1170     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1171     this->point_original = this->point;
1173     sp_knot_moveto (this->knot, this->point);
1175     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1176         GrDraggable *da = (GrDraggable *) i->data;
1177         if ( (da->item == item) &&
1178              (point_type == -1 || da->point_type == point_type) &&
1179              (point_i == -1 || da->point_i == point_i) &&
1180              (da->fill_or_stroke == fill_or_stroke) ) {
1181             continue;
1182         }
1183         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1184     }
1185     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1189 /**
1190 Moves all midstop draggables that depend on this one
1191  */
1192 void
1193 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1194     SPObject *server = draggable->getServer();
1195     if (!server)
1196         return;
1197     guint num = SP_GRADIENT(server)->vector.stops.size();
1198     if (num <= 2) return;
1200     if ( SP_IS_LINEARGRADIENT(server) ) {
1201         for ( guint i = 1; i < num - 1; i++ ) {
1202             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1203         }
1204     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1205         for ( guint i = 1; i < num - 1; i++ ) {
1206             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1207             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1208         }
1209     }
1213 /**
1214 Moves all draggables that depend on this one
1215  */
1216 void
1217 GrDragger::updateDependencies (bool write_repr)
1219     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1220         GrDraggable *draggable = (GrDraggable *) i->data;
1221         switch (draggable->point_type) {
1222             case POINT_LG_BEGIN:
1223                 {
1224                     // the end point is dependent only when dragging with ctrl+shift
1225                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1227                     this->updateMidstopDependencies (draggable, write_repr);
1228                 }
1229                 break;
1230             case POINT_LG_END:
1231                 {
1232                     // the begin point is dependent only when dragging with ctrl+shift
1233                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1235                     this->updateMidstopDependencies (draggable, write_repr);
1236                 }
1237                 break;
1238             case POINT_LG_MID:
1239                 // no other nodes depend on mid points.
1240                 break;
1241             case POINT_RG_R2:
1242                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1243                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1244                 this->updateMidstopDependencies (draggable, write_repr);
1245                 break;
1246             case POINT_RG_R1:
1247                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1248                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1249                 this->updateMidstopDependencies (draggable, write_repr);
1250                 break;
1251             case POINT_RG_CENTER:
1252                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1253                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1254                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1255                 this->updateMidstopDependencies (draggable, write_repr);
1256                 break;
1257             case POINT_RG_FOCUS:
1258                 // nothing can depend on that
1259                 break;
1260             case POINT_RG_MID1:
1261                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1262                 break;
1263             case POINT_RG_MID2:
1264                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1265                 break;
1266             default:
1267                 break;
1268         }
1269     }
1274 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1276     this->draggables = NULL;
1278     this->parent = parent;
1280     this->point = p;
1281     this->point_original = p;
1283     // create the knot
1284     this->knot = sp_knot_new (parent->desktop, NULL);
1285     this->knot->setMode(SP_KNOT_MODE_XOR);
1286     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1287     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1288     sp_knot_update_ctrl(this->knot);
1290     // move knot to the given point
1291     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1292     sp_knot_show (this->knot);
1294     // connect knot's signals
1295     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1296                        // luckily, midstops never snap to other nodes so are never unsnapped...
1297          && ( (draggable->point_type == POINT_LG_MID)
1298               || (draggable->point_type == POINT_RG_MID1)
1299               || (draggable->point_type == POINT_RG_MID2) ) )
1300     {
1301         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1302     } else {
1303         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1304     }
1305     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1306     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1307     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1308     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1310     // add the initial draggable
1311     if (draggable)
1312         this->addDraggable (draggable);
1313     updateKnotShape();
1316 GrDragger::~GrDragger ()
1318     // unselect if it was selected
1319     this->parent->setDeselected(this);
1321     // disconnect signals
1322     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1323     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1324     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1325     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1326     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1328     /* unref should call destroy */
1329     g_object_unref (G_OBJECT (this->knot));
1331     // delete all draggables
1332     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1333         delete ((GrDraggable *) i->data);
1334     }
1335     g_slist_free (this->draggables);
1336     this->draggables = NULL;
1339 /**
1340 Select the dragger which has the given draggable.
1341 */
1342 GrDragger *
1343 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1345     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1346         GrDragger *dragger = (GrDragger *) i->data;
1347         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1348             GrDraggable *da2 = (GrDraggable *) j->data;
1349             if ( (da2->item == item) &&
1350                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1351                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1352                  (da2->fill_or_stroke == fill_or_stroke)) {
1353                 return (dragger);
1354             }
1355         }
1356     }
1357     return NULL;
1361 void
1362 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1364     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1365     if (d && d !=  this) {
1366         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1367     }
1371 /**
1372   Draw this dragger as selected
1373 */
1374 void
1375 GrDragger::select()
1377     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1378     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1381 /**
1382   Draw this dragger as normal (deselected)
1383 */
1384 void
1385 GrDragger::deselect()
1387     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1388     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1391 bool
1392 GrDragger::isSelected()
1394     return g_list_find (parent->selected, this);
1397 /**
1398 \brief Deselect all stops/draggers (private)
1399 */
1400 void
1401 GrDrag::deselect_all()
1403     while (selected) {
1404         ( (GrDragger*) selected->data)->deselect();
1405         selected = g_list_remove(selected, selected->data);
1406     }
1409 /**
1410 \brief Deselect all stops/draggers (public; emits signal)
1411 */
1412 void
1413 GrDrag::deselectAll()
1415     deselect_all();
1416     this->desktop->emitToolSubselectionChanged(NULL);
1419 /**
1420 \brief Select all stops/draggers
1421 */
1422 void
1423 GrDrag::selectAll()
1425     for (GList *l = this->draggers; l != NULL; l = l->next) {
1426         GrDragger *d = ((GrDragger *) l->data);
1427         setSelected (d, true, true);
1428     }
1431 /**
1432 \brief Select all stops/draggers that match the coords
1433 */
1434 void
1435 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1437     for (GList *l = this->draggers; l != NULL; l = l->next) {
1438         GrDragger *d = ((GrDragger *) l->data);
1439         for (guint k = 0; k < coords.size(); k++) {
1440             if (NR::L2 (d->point - coords[k]) < 1e-4) {
1441                 setSelected (d, true, true);
1442             }
1443         }
1444     }
1448 /**
1449 \brief Select all stops/draggers that fall within the rect
1450 */
1451 void
1452 GrDrag::selectRect(Geom::Rect const &r)
1454     for (GList *l = this->draggers; l != NULL; l = l->next) {
1455         GrDragger *d = ((GrDragger *) l->data);
1456         if (r.contains(d->point)) {
1457            setSelected (d, true, true);
1458         }
1459     }
1462 /**
1463 \brief Select a dragger
1464 \param dragger       The dragger to select
1465 \param add_to_selection   If true, add to selection, otherwise deselect others
1466 \param override      If true, always select this node, otherwise toggle selected status
1467 */
1468 void
1469 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1471     GrDragger *seldragger = NULL;
1473     if (add_to_selection) {
1474         if (!dragger) return;
1475         if (override) {
1476             if (!g_list_find(selected, dragger)) {
1477                 selected = g_list_prepend(selected, dragger);
1478             }
1479             dragger->select();
1480             seldragger = dragger;
1481         } else { // toggle
1482             if (g_list_find(selected, dragger)) {
1483                 selected = g_list_remove(selected, dragger);
1484                 dragger->deselect();
1485                 if (selected) {
1486                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1487                 }
1488             } else {
1489                 selected = g_list_prepend(selected, dragger);
1490                 dragger->select();
1491                 seldragger = dragger;
1492             }
1493         }
1494     } else {
1495         deselect_all();
1496         if (dragger) {
1497             selected = g_list_prepend(selected, dragger);
1498             dragger->select();
1499             seldragger = dragger;
1500         }
1501     }
1502     if (seldragger) {
1503         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1504     }
1507 /**
1508 \brief Deselect a dragger
1509 \param dragger       The dragger to deselect
1510 */
1511 void
1512 GrDrag::setDeselected (GrDragger *dragger)
1514     if (g_list_find(selected, dragger)) {
1515         selected = g_list_remove(selected, dragger);
1516         dragger->deselect();
1517     }
1518     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1523 /**
1524 Create a line from p1 to p2 and add it to the lines list
1525  */
1526 void
1527 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1529     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1530                                                             SP_TYPE_CTRLLINE, NULL);
1531     sp_canvas_item_move_to_z(line, 0);
1532     SP_CTRLLINE(line)->item = item;
1533     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1534     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1535         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1536     sp_canvas_item_show (line);
1537     this->lines = g_slist_append (this->lines, line);
1540 /**
1541 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1542 new dragger and add it to draggers list
1543  */
1544 void
1545 GrDrag::addDragger (GrDraggable *draggable)
1547     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1549     for (GList *i = this->draggers; i != NULL; i = i->next) {
1550         GrDragger *dragger = (GrDragger *) i->data;
1551         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1552             // distance is small, merge this draggable into dragger, no need to create new dragger
1553             dragger->addDraggable (draggable);
1554             dragger->updateKnotShape();
1555             return;
1556         }
1557     }
1559     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1560     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1561     this->draggers = g_list_append (this->draggers, new_dragger);
1564 /**
1565 Add draggers for the radial gradient rg on item
1566 */
1567 void
1568 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1570     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1571     guint num = rg->vector.stops.size();
1572     if (num > 2) {
1573         for ( guint i = 1; i < num - 1; i++ ) {
1574             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1575         }
1576     }
1577     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1578     if (num > 2) {
1579         for ( guint i = 1; i < num - 1; i++ ) {
1580             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1581         }
1582     }
1583     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1584     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1587 /**
1588 Add draggers for the linear gradient lg on item
1589 */
1590 void
1591 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1593     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1594     guint num = lg->vector.stops.size();
1595     if (num > 2) {
1596         for ( guint i = 1; i < num - 1; i++ ) {
1597             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1598         }
1599     }
1600     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1603 /**
1604 Artificially grab the knot of this dragger; used by the gradient context
1605 */
1606 void
1607 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1609     if (dragger) {
1610         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1611     }
1614 /**
1615 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1616 */
1617 void
1618 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1620     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1621     if (dragger) {
1622         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1623     }
1626 /**
1627 Regenerates the draggers list from the current selection; is called when selection is changed or
1628 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1629 */
1630 void
1631 GrDrag::updateDraggers ()
1633     while (selected) {
1634         selected = g_list_remove(selected, selected->data);
1635     }
1636     // delete old draggers
1637     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1638         delete ((GrDragger *) i->data);
1639     }
1640     g_list_free (this->draggers);
1641     this->draggers = NULL;
1643     g_return_if_fail (this->selection != NULL);
1645     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1647         SPItem *item = SP_ITEM(i->data);
1648         SPStyle *style = SP_OBJECT_STYLE (item);
1650         if (style && (style->fill.isPaintserver())) {
1651             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1652             if (SP_IS_LINEARGRADIENT (server)) {
1653                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1654             } else if (SP_IS_RADIALGRADIENT (server)) {
1655                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1656             }
1657         }
1659         if (style && (style->stroke.isPaintserver())) {
1660             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1661             if (SP_IS_LINEARGRADIENT (server)) {
1662                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1663             } else if (SP_IS_RADIALGRADIENT (server)) {
1664                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1665             }
1666         }
1667     }
1670 /**
1671 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1672 lines are always in sync with the actual gradient
1673 */
1674 void
1675 GrDrag::updateLines ()
1677     // delete old lines
1678     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1679         gtk_object_destroy( GTK_OBJECT (i->data));
1680     }
1681     g_slist_free (this->lines);
1682     this->lines = NULL;
1684     g_return_if_fail (this->selection != NULL);
1686     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1688         SPItem *item = SP_ITEM(i->data);
1690         SPStyle *style = SP_OBJECT_STYLE (item);
1692         if (style && (style->fill.isPaintserver())) {
1693             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1694             if (SP_IS_LINEARGRADIENT (server)) {
1695                 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);
1696             } else if (SP_IS_RADIALGRADIENT (server)) {
1697                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1698                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1699                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1700             }
1701         }
1703         if (style && (style->stroke.isPaintserver())) {
1704             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1705             if (SP_IS_LINEARGRADIENT (server)) {
1706                 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);
1707             } else if (SP_IS_RADIALGRADIENT (server)) {
1708                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1709                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1710                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1711             }
1712         }
1713     }
1716 /**
1717 Regenerates the levels list from the current selection
1718 */
1719 void
1720 GrDrag::updateLevels ()
1722     hor_levels.clear();
1723     vert_levels.clear();
1725     g_return_if_fail (this->selection != NULL);
1727     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1728         SPItem *item = SP_ITEM(i->data);
1729         boost::optional<Geom::Rect> rect = sp_item_bbox_desktop (item);
1730         if (rect) {
1731             // Remember the edges of the bbox and the center axis
1732             hor_levels.push_back(rect->min()[Geom::Y]);
1733             hor_levels.push_back(rect->max()[Geom::Y]);
1734             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1735             vert_levels.push_back(rect->min()[Geom::X]);
1736             vert_levels.push_back(rect->max()[Geom::X]);
1737             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1738         }
1739     }
1742 void
1743 GrDrag::selected_reverse_vector ()
1745     if (selected == NULL)
1746         return;
1748     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1749         GrDraggable *draggable = (GrDraggable *) i->data;
1751         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1752     }
1755 void
1756 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1758     selected_move (x, y, false, scale_radial);
1761 void
1762 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1764     if (selected == NULL)
1765         return;
1767     bool did = false;
1769     for (GList *i = selected; i != NULL; i = i->next) {
1770         GrDragger *d = (GrDragger *) i->data;
1772         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1773             // if this is an endpoint,
1775             // Moving an rg center moves its focus and radii as well.
1776             // therefore, if this is a focus or radius and if selection
1777             // contains the center as well, do not move this one
1778             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1779                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1780                 bool skip_radius_with_center = false;
1781                 for (GList *di = selected; di != NULL; di = di->next) {
1782                     GrDragger *d_new = (GrDragger *) di->data;
1783                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1784                                     POINT_RG_CENTER,
1785                                     0,
1786                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1787                         // FIXME: here we take into account only the first draggable!
1788                         skip_radius_with_center = true;
1789                     }
1790                 }
1791                 if (skip_radius_with_center)
1792                     continue;
1793             }
1795             did = true;
1796             d->point += NR::Point (x, y);
1797             d->point_original = d->point;
1798             sp_knot_moveto (d->knot, d->point);
1800             d->fireDraggables (write_repr, scale_radial);
1802             d->updateDependencies(write_repr);
1803         }
1804     }
1806     if (write_repr && did) {
1807         // we did an undoable action
1808         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1809                                 _("Move gradient handle(s)"));
1810         return;
1811     }
1813     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1815         GrDragger *dragger = (GrDragger *) selected->data;
1816         // a midpoint dragger can (logically) only contain one GrDraggable
1817         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1819         NR::Point begin(0,0), end(0,0);
1820         NR::Point low_lim(0,0), high_lim(0,0);
1822         SPObject *server = draggable->getServer();
1823         GSList *moving = NULL;
1824         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1826         NR::Point p(x, y);
1827         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1828         NR::Point displacement = p - dragger->point;
1830         for (GSList const* i = moving; i != NULL; i = i->next) {
1831             GrDragger *drg = (GrDragger*) i->data;
1832             SPKnot *drgknot = drg->knot;
1833             drg->point += displacement;
1834             sp_knot_moveto (drgknot, drg->point);
1835             drg->fireDraggables (true);
1836             drg->updateDependencies(true);
1837             did = true;
1838         }
1840         g_slist_free(moving);
1842         if (write_repr && did) {
1843             // we did an undoable action
1844             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1845                                     _("Move gradient mid stop(s)"));
1846         }
1847     }
1850 void
1851 GrDrag::selected_move_screen (double x, double y)
1853     gdouble zoom = desktop->current_zoom();
1854     gdouble zx = x / zoom;
1855     gdouble zy = y / zoom;
1857     selected_move (zx, zy);
1860 /**
1861 Select the knot next to the last selected one and deselect all other selected.
1862 */
1863 GrDragger *
1864 GrDrag::select_next ()
1866     GrDragger *d = NULL;
1867     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1868         if (draggers)
1869             d = (GrDragger *) draggers->data;
1870     } else {
1871         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1872     }
1873     if (d)
1874         setSelected (d);
1875     return d;
1878 /**
1879 Select the knot previous from the last selected one and deselect all other selected.
1880 */
1881 GrDragger *
1882 GrDrag::select_prev ()
1884     GrDragger *d = NULL;
1885     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1886         if (draggers)
1887             d = (GrDragger *) g_list_last (draggers)->data;
1888     } else {
1889         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1890     }
1891     if (d)
1892         setSelected (d);
1893     return d;
1897 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1898 void
1899 GrDrag::deleteSelected (bool just_one)
1901     if (!selected) return;
1903     SPDocument *document = false;
1905     struct StructStopInfo {
1906         SPStop * spstop;
1907         GrDraggable * draggable;
1908         SPGradient * gradient;
1909         SPGradient * vector;
1910     };
1912     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1913     GSList *endstoplist = NULL;  // list of stops that must be deleted
1914     while (selected) {
1915         GrDragger *dragger = (GrDragger*) selected->data;
1916         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1917             GrDraggable *draggable = (GrDraggable*) drgble->data;
1918             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1919             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1921             switch (draggable->point_type) {
1922                 case POINT_LG_MID:
1923                 case POINT_RG_MID1:
1924                 case POINT_RG_MID2:
1925                     {
1926                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1927                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1928                         bool present = false;
1929                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1930                             if ( (SPStop*)l->data == stop ) {
1931                                 present = true;
1932                                 break; // no need to search further.
1933                             }
1934                         }
1935                         if (!present)
1936                             midstoplist = g_slist_append(midstoplist, stop);
1937                     }
1938                     break;
1939                 case POINT_LG_BEGIN:
1940                 case POINT_LG_END:
1941                 case POINT_RG_CENTER:
1942                 case POINT_RG_R1:
1943                 case POINT_RG_R2:
1944                     {
1945                         SPStop *stop = NULL;
1946                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1947                             stop = sp_first_stop(vector);
1948                         } else {
1949                             stop = sp_last_stop(vector);
1950                         }
1951                         if (stop) {
1952                             StructStopInfo *stopinfo = new StructStopInfo;
1953                             stopinfo->spstop = stop;
1954                             stopinfo->draggable = draggable;
1955                             stopinfo->gradient = gradient;
1956                             stopinfo->vector = vector;
1957                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1958                             bool present = false;
1959                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1960                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1961                                     present = true;
1962                                     break; // no need to search further.
1963                                 }
1964                             }
1965                             if (!present)
1966                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1967                         }
1968                     }
1969                     break;
1970                 default:
1971                     break;
1972             }
1973         }
1974         selected = g_list_remove(selected, dragger);
1975         if ( just_one ) break; // iterate once if just_one is set.
1976     }
1977     while (midstoplist) {
1978         SPStop *stop = (SPStop*) midstoplist->data;
1979         document = SP_OBJECT_DOCUMENT (stop);
1980         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1981         parent->removeChild(SP_OBJECT_REPR(stop));
1982         midstoplist = g_slist_remove(midstoplist, stop);
1983     }
1984     while (endstoplist) {
1985         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1986         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1988         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1989         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1990         // manually count the children, don't know if there already exists a function for this...
1991         int len = 0;
1992         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1993               child != NULL ;
1994               child = SP_OBJECT_NEXT(child) )
1995         {
1996             if ( SP_IS_STOP(child) )  len ++;
1997         }
1998         if (len > 2)
1999         {
2000             switch (stopinfo->draggable->point_type) {
2001                 case POINT_LG_BEGIN:
2002                     {
2003                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2005                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2006                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
2007                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
2008                         SPStop *stop = sp_first_stop(stopinfo->vector);
2009                         gdouble offset = stop->offset;
2010                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
2011                         lg->x1.computed = newbegin[NR::X];
2012                         lg->y1.computed = newbegin[NR::Y];
2014                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2015                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2016                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2017                         stop->offset = 0;
2018                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2020                         // iterate through midstops to set new offset values such that they won't move on canvas.
2021                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2022                         stop = sp_next_stop(stop);
2023                         while ( stop != laststop ) {
2024                             stop->offset = (stop->offset - offset)/(1 - offset);
2025                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2026                             stop = sp_next_stop(stop);
2027                         }
2028                     }
2029                     break;
2030                 case POINT_LG_END:
2031                     {
2032                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2034                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2035                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
2036                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
2037                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2038                         gdouble offset = laststop->offset;
2039                         NR::Point newend = begin + offset * (oldend - begin);
2040                         lg->x2.computed = newend[NR::X];
2041                         lg->y2.computed = newend[NR::Y];
2043                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2044                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2045                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2046                         laststop->offset = 1;
2047                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2049                         // iterate through midstops to set new offset values such that they won't move on canvas.
2050                         SPStop *stop = sp_first_stop(stopinfo->vector);
2051                         stop = sp_next_stop(stop);
2052                         while ( stop != laststop ) {
2053                             stop->offset = stop->offset / offset;
2054                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2055                             stop = sp_next_stop(stop);
2056                         }
2057                     }
2058                     break;
2059                 case POINT_RG_CENTER:
2060                     {
2061                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2062                         if (newfirst) {
2063                             newfirst->offset = 0;
2064                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2065                         }
2066                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2067                     }
2068                     break;
2069                 case POINT_RG_R1:
2070                 case POINT_RG_R2:
2071                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2073                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2074                         double oldradius = rg->r.computed;
2075                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2076                         gdouble offset = laststop->offset;
2077                         double newradius = offset * oldradius;
2078                         rg->r.computed = newradius;
2080                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2081                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2082                         laststop->offset = 1;
2083                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2085                         // iterate through midstops to set new offset values such that they won't move on canvas.
2086                         SPStop *stop = sp_first_stop(stopinfo->vector);
2087                         stop = sp_next_stop(stop);
2088                         while ( stop != laststop ) {
2089                             stop->offset = stop->offset / offset;
2090                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2091                             stop = sp_next_stop(stop);
2092                         }
2093                         break;
2094             }
2095         }
2096         else
2097         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2098             SPCSSAttr *css = sp_repr_css_attr_new ();
2100             // stopinfo->spstop is the selected stop
2101             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2102             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2103                 unselectedrepr = unselectedrepr->next();
2104             }
2106             if (unselectedrepr == NULL) {
2107                 if (stopinfo->draggable->fill_or_stroke) {
2108                     sp_repr_css_unset_property (css, "fill");
2109                 } else {
2110                     sp_repr_css_unset_property (css, "stroke");
2111                 }
2112             } else {
2113                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2114                 if (stopinfo->draggable->fill_or_stroke) {
2115                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2116                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2117                 } else {
2118                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2119                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2120                 }
2121                 sp_repr_css_attr_unref (stopcss);
2122             }
2124             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2125             sp_repr_css_attr_unref (css);
2126         }
2128         endstoplist = g_slist_remove(endstoplist, stopinfo);
2129         delete stopinfo;
2130     }
2132     if (document) {
2133         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2134     }
2138 /*
2139   Local Variables:
2140   mode:c++
2141   c-file-style:"stroustrup"
2142   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2143   indent-tabs-mode:nil
2144   fill-column:99
2145   End:
2146 */
2147 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :