Code

Next roud of NR ==> Geom conversion
[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 "xml/repr.h"
31 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
33 #include "libnr/nr-point-fns.h"
34 #include "prefs-utils.h"
35 #include "sp-item.h"
36 #include "style.h"
37 #include "knot.h"
38 #include "sp-linear-gradient.h"
39 #include "sp-radial-gradient.h"
40 #include "gradient-chemistry.h"
41 #include "gradient-drag.h"
42 #include "sp-stop.h"
43 #include "snap.h"
44 #include "sp-namedview.h"
45 #include "selection-chemistry.h"
47 #define GR_KNOT_COLOR_NORMAL 0xffffff00
48 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
49 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
51 #define GR_LINE_COLOR_FILL 0x0000ff7f
52 #define GR_LINE_COLOR_STROKE 0x9999007f
54 // screen pixels between knots when they snap:
55 #define SNAP_DIST 5
57 // absolute distance between gradient points for them to become a single dragger when the drag is created:
58 #define MERGE_DIST 0.1
60 // knot shapes corresponding to GrPointType enum
61 SPKnotShapeType gr_knot_shapes [] = {
62         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
63         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
64         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
65         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
66         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
67         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
68         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
69         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
70         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
71 };
73 const gchar *gr_knot_descr [] = {
74     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
75     N_("Linear gradient <b>end</b>"),
76     N_("Linear gradient <b>mid stop</b>"),
77     N_("Radial gradient <b>center</b>"),
78     N_("Radial gradient <b>radius</b>"),
79     N_("Radial gradient <b>radius</b>"),
80     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
81     N_("Radial gradient <b>mid stop</b>"),
82     N_("Radial gradient <b>mid stop</b>")
83 };
85 static void
86 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
87 {
88     GrDrag *drag = (GrDrag *) data;
89     drag->updateDraggers ();
90     drag->updateLines ();
91     drag->updateLevels ();
92 }
94 static void
95 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
96 {
97     GrDrag *drag = (GrDrag *) data;
98     if (drag->local_change) {
99         drag->local_change = false;
100     } else {
101         drag->updateDraggers ();
102     }
103     drag->updateLines ();
104     drag->updateLevels ();
107 /**
108 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
109 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
110 any.
111 */
112 int
113 gr_drag_style_query (SPStyle *style, int property, gpointer data)
115     GrDrag *drag = (GrDrag *) data;
117     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
118         return QUERY_STYLE_NOTHING;
119     }
121     if (!drag->selected) {
122         return QUERY_STYLE_NOTHING;
123     } else {
124         int ret = QUERY_STYLE_NOTHING;
126         float cf[4];
127         cf[0] = cf[1] = cf[2] = cf[3] = 0;
129         int count = 0;
131         for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
132             GrDragger *d = (GrDragger *) i->data;
133             for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
134                 GrDraggable *draggable = (GrDraggable *) j->data;
136                 if (ret == QUERY_STYLE_NOTHING) {
137                     ret = QUERY_STYLE_SINGLE;
138                 } else if (ret == QUERY_STYLE_SINGLE) {
139                     ret = QUERY_STYLE_MULTIPLE_AVERAGED;
140                 }
142                 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
143                 cf[0] += SP_RGBA32_R_F (c);
144                 cf[1] += SP_RGBA32_G_F (c);
145                 cf[2] += SP_RGBA32_B_F (c);
146                 cf[3] += SP_RGBA32_A_F (c);
148                 count ++;
149             }
150         }
152         if (count) {
153             cf[0] /= count;
154             cf[1] /= count;
155             cf[2] /= count;
156             cf[3] /= count;
158             // set both fill and stroke with our stop-color and stop-opacity
159             style->fill.clear();
160             style->fill.setColor( cf[0], cf[1], cf[2] );
161             style->fill.set = TRUE;
162             style->stroke.clear();
163             style->stroke.setColor( cf[0], cf[1], cf[2] );
164             style->stroke.set = TRUE;
166             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
167             style->fill_opacity.set = TRUE;
168             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
169             style->stroke_opacity.set = TRUE;
171             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
172             style->opacity.set = TRUE;
173         }
175         return ret;
176     }
179 bool
180 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
182     GrDrag *drag = (GrDrag *) data;
184     if (!drag->selected)
185         return false;
187     SPCSSAttr *stop = sp_repr_css_attr_new ();
189     // See if the css contains interesting properties, and if so, translate them into the format
190     // acceptable for gradient stops
192     // any of color properties, in order of increasing priority:
193     if (css->attribute("flood-color"))
194         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
196     if (css->attribute("lighting-color"))
197         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
199     if (css->attribute("color"))
200         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
202     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
203         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
205     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
206         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
208     if (css->attribute("stop-color"))
209         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
212     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
213         sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
214     } else {  // multiply all opacity properties:
215         gdouble accumulated = 1.0;
216         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
217         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
218         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
219         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
221         Inkscape::CSSOStringStream os;
222         os << accumulated;
223         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
225         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
226             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
227             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
228     }
230     if (!stop->attributeList()) { // nothing for us here, pass it on
231         sp_repr_css_attr_unref(stop);
232         return false;
233     }
235     for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
236         GrDragger* dragger = (GrDragger*) sel->data;
237         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
238                GrDraggable *draggable = (GrDraggable *) i->data;
240                drag->local_change = true;
241                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
242         }
243     }
245     //sp_repr_css_print(stop);
246     sp_repr_css_attr_unref(stop);
247     return true;
250 guint32 GrDrag::getColor()
252     if (!selected) return 0;
254     float cf[4];
255     cf[0] = cf[1] = cf[2] = cf[3] = 0;
257     int count = 0;
259     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
260         GrDragger *d = (GrDragger *) i->data;
261         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
262             GrDraggable *draggable = (GrDraggable *) j->data;
264             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
265             cf[0] += SP_RGBA32_R_F (c);
266             cf[1] += SP_RGBA32_G_F (c);
267             cf[2] += SP_RGBA32_B_F (c);
268             cf[3] += SP_RGBA32_A_F (c);
270             count ++;
271         }
272     }
274     if (count) {
275         cf[0] /= count;
276         cf[1] /= count;
277         cf[2] /= count;
278         cf[3] /= count;
279     }
281     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
284 SPStop *
285 GrDrag::addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance)
287     gfloat offset; // type of SPStop.offset = gfloat
288     SPGradient *gradient;
289     bool fill_or_stroke = true;
290     bool r1_knot = false;
292     bool addknot = false;
293     do {
294         gradient = sp_item_gradient (item, fill_or_stroke);
295         if (SP_IS_LINEARGRADIENT(gradient)) {
296             NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
297             NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
299             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
300             double dist_screen = NR::L2 (mouse_p - nearest);
301             if ( dist_screen < tolerance ) {
302                 // add the knot
303                 offset = get_offset_between_points(nearest, begin, end);
304                 addknot = true;
305                 break; // break out of the while loop: add only one knot
306             }
307         } else if (SP_IS_RADIALGRADIENT(gradient)) {
308             NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
309             NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
310             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
311             double dist_screen = NR::L2 (mouse_p - nearest);
312             if ( dist_screen < tolerance ) {
313                 offset = get_offset_between_points(nearest, begin, end);
314                 addknot = true;
315                 r1_knot = true;
316                 break; // break out of the while loop: add only one knot
317             }
319             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
320             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
321             dist_screen = NR::L2 (mouse_p - nearest);
322             if ( dist_screen < tolerance ) {
323                 offset = get_offset_between_points(nearest, begin, end);
324                 addknot = true;
325                 r1_knot = false;
326                 break; // break out of the while loop: add only one knot
327             }
328         }
329         fill_or_stroke = !fill_or_stroke;
330     } while (!fill_or_stroke && !addknot) ;
332     if (addknot) {
333         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
334         SPStop* prev_stop = sp_first_stop(vector);
335         SPStop* next_stop = sp_next_stop(prev_stop);
336         guint i = 1;
337         while ( (next_stop) && (next_stop->offset < offset) ) {
338             prev_stop = next_stop;
339             next_stop = sp_next_stop(next_stop);
340             i++;
341         }
342         if (!next_stop) {
343             // logical error: the endstop should have offset 1 and should always be more than this offset here
344             return NULL;
345         }
348         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
349         sp_gradient_ensure_vector (gradient);
350         updateDraggers();
352         return newstop;
353     }
355     return NULL;
359 bool
360 GrDrag::dropColor(SPItem */*item*/, gchar *c, NR::Point p)
362     // first, see if we can drop onto one of the existing draggers
363     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
364         GrDragger *d = (GrDragger *) i->data;
366         if (NR::L2(p - d->point)*desktop->current_zoom() < 5) {
367            SPCSSAttr *stop = sp_repr_css_attr_new ();
368            sp_repr_css_set_property (stop, "stop-color", c);
369            sp_repr_css_set_property (stop, "stop-opacity", "1");
370            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
371                GrDraggable *draggable = (GrDraggable *) j->data;
372                local_change = true;
373                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
374            }
375            sp_repr_css_attr_unref(stop);
376            return true;
377         }
378     }
380     // now see if we're over line and create a new stop
381     bool over_line = false;
382     SPCtrlLine *line = NULL;
383     if (lines) {
384         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
385             line = (SPCtrlLine*) l->data;
386             NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
387             double dist_screen = NR::L2 (p - nearest) * desktop->current_zoom();
388             if (line->item && dist_screen < 5) {
389                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
390                 if (stop) {
391                     SPCSSAttr *css = sp_repr_css_attr_new ();
392                     sp_repr_css_set_property (css, "stop-color", c);
393                     sp_repr_css_set_property (css, "stop-opacity", "1");
394                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
395                     return true;
396                 }
397             }
398         }
399     }
401     return false;
405 GrDrag::GrDrag(SPDesktop *desktop) {
407     this->desktop = desktop;
409     this->selection = sp_desktop_selection(desktop);
411     this->draggers = NULL;
412     this->lines = NULL;
413     this->selected = NULL;
415     this->hor_levels.clear();
416     this->vert_levels.clear();
418     this->local_change = false;
420     this->sel_changed_connection = this->selection->connectChanged(
421         sigc::bind (
422             sigc::ptr_fun(&gr_drag_sel_changed),
423             (gpointer)this )
425         );
426     this->sel_modified_connection = this->selection->connectModified(
427         sigc::bind(
428             sigc::ptr_fun(&gr_drag_sel_modified),
429             (gpointer)this )
430         );
432     this->style_set_connection = this->desktop->connectSetStyle(
433         sigc::bind(
434             sigc::ptr_fun(&gr_drag_style_set),
435             (gpointer)this )
436         );
438     this->style_query_connection = this->desktop->connectQueryStyle(
439         sigc::bind(
440             sigc::ptr_fun(&gr_drag_style_query),
441             (gpointer)this )
442         );
444     this->updateDraggers ();
445     this->updateLines ();
446     this->updateLevels ();
448     if (desktop->gr_item) {
449         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
450     }
453 GrDrag::~GrDrag()
455     this->sel_changed_connection.disconnect();
456     this->sel_modified_connection.disconnect();
457     this->style_set_connection.disconnect();
458     this->style_query_connection.disconnect();
460     if (this->selected) {
461         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
462         desktop->gr_item = draggable->item;
463         desktop->gr_point_type = draggable->point_type;
464         desktop->gr_point_i = draggable->point_i;
465         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
466     } else {
467         desktop->gr_item = NULL;
468         desktop->gr_point_type = 0;
469         desktop->gr_point_i = 0;
470         desktop->gr_fill_or_stroke = true;
471     }
473     deselect_all();
474     for (GList *l = this->draggers; l != NULL; l = l->next) {
475         delete ((GrDragger *) l->data);
476     }
477     g_list_free (this->draggers);
478     this->draggers = NULL;
479     this->selected = NULL;
481     for (GSList *l = this->lines; l != NULL; l = l->next) {
482         gtk_object_destroy( GTK_OBJECT (l->data));
483     }
484     g_slist_free (this->lines);
485     this->lines = NULL;
488 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
490     this->item = item;
491     this->point_type = point_type;
492     this->point_i = point_i;
493     this->fill_or_stroke = fill_or_stroke;
495     g_object_ref (G_OBJECT (this->item));
498 GrDraggable::~GrDraggable ()
500     g_object_unref (G_OBJECT (this->item));
504 SPObject *
505 GrDraggable::getServer ()
507     if (!item)
508         return NULL;
510     SPObject *server = NULL;
511     if (fill_or_stroke)
512         server = SP_OBJECT_STYLE_FILL_SERVER (item);
513     else
514         server = SP_OBJECT_STYLE_STROKE_SERVER (item);
516     return server;
519 static void
520 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
522     GrDragger *dragger = (GrDragger *) data;
523     GrDrag *drag = dragger->parent;
525     NR::Point p = *ppointer;
527     // FIXME: take from prefs
528     double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
530     if (state & GDK_SHIFT_MASK) {
531         // with Shift; unsnap if we carry more than one draggable
532         if (dragger->draggables && dragger->draggables->next) {
533             // create a new dragger
534             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
535             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
536             // relink to it all but the first draggable in the list
537             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
538                 GrDraggable *draggable = (GrDraggable *) i->data;
539                 dr_new->addDraggable (draggable);
540             }
541             dr_new->updateKnotShape();
542             g_slist_free (dragger->draggables->next);
543             dragger->draggables->next = NULL;
544             dragger->updateKnotShape();
545             dragger->updateTip();
546         }
547     } else if (!(state & GDK_CONTROL_MASK)) {
548         // without Shift or Ctrl; see if we need to snap to another dragger
549         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
550             GrDragger *d_new = (GrDragger *) di->data;
551             if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
553                 // Merge draggers:
554                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
555                     GrDraggable *draggable = (GrDraggable *) i->data;
556                     // copy draggable to d_new:
557                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
558                     d_new->addDraggable (da_new);
559                 }
561                 // unlink and delete this dragger
562                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
563                 delete dragger;
565                 // update the new merged dragger
566                 d_new->fireDraggables(true, false, true);
567                 d_new->parent->updateLines();
568                 d_new->parent->setSelected (d_new);
569                 d_new->updateKnotShape ();
570                 d_new->updateTip ();
571                 d_new->updateDependencies(true);
572                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
573                                   _("Merge gradient handles"));
574                 return;
575             }
576         }
577     }
579     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
580         // Try snapping to the grid or guides
581         SPDesktop *desktop = dragger->parent->desktop;
582         SnapManager &m = desktop->namedview->snap_manager;
583         m.setup(desktop);
584         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, to_2geom(p));
585         if (s.getSnapped()) {
586             p = s.getPoint();
587             sp_knot_moveto (knot, p);
588         } else {
589             bool was_snapped = false;
590             double dist = NR_HUGE;
591             // No snapping so far, let's see if we need to snap to any of the levels
592             for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
593                 dist = fabs(p[NR::Y] - dragger->parent->hor_levels[i]);
594                 if (dist < snap_dist) {
595                     p[NR::Y] = dragger->parent->hor_levels[i];
596                     s = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_GRADIENT, dist, snap_dist, false);
597                     was_snapped = true;
598                     sp_knot_moveto (knot, p);
599                 }
600             }
601             for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
602                 dist = fabs(p[NR::X] - dragger->parent->vert_levels[i]);
603                 if (dist < snap_dist) {
604                     p[NR::X] = dragger->parent->vert_levels[i];
605                     s = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_GRADIENT, dist, snap_dist, false);
606                     was_snapped = true;
607                     sp_knot_moveto (knot, p);
608                 }
609             }
610             if (was_snapped) {
611                 desktop->snapindicator->set_new_snappoint(s);
612             }
613         }
614     }
616     if (state & GDK_CONTROL_MASK) {
617         unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
618         /* 0 means no snapping. */
620         // This list will store snap vectors from all draggables of dragger
621         GSList *snap_vectors = NULL;
623         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
624             GrDraggable *draggable = (GrDraggable *) i->data;
626             NR::Point *dr_snap = NULL;
628             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
629                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
630                     GrDragger *d_new = (GrDragger *) di->data;
631                     if (d_new == dragger)
632                         continue;
633                     if (d_new->isA (draggable->item,
634                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
635                                     draggable->fill_or_stroke)) {
636                         // found the other end of the linear gradient;
637                         if (state & GDK_SHIFT_MASK) {
638                             // moving linear around center
639                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
640                             dr_snap = &center;
641                         } else {
642                             // moving linear around the other end
643                             dr_snap = &d_new->point;
644                         }
645                     }
646                 }
647             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
648                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
649                     GrDragger *d_new = (GrDragger *) di->data;
650                     if (d_new == dragger)
651                         continue;
652                     if (d_new->isA (draggable->item,
653                                     POINT_RG_CENTER,
654                                     draggable->fill_or_stroke)) {
655                         // found the center of the radial gradient;
656                         dr_snap = &(d_new->point);
657                     }
658                 }
659             } else if (draggable->point_type == POINT_RG_CENTER) {
660                 // radial center snaps to hor/vert relative to its original position
661                 dr_snap = &(dragger->point_original);
662             }
664             NR::Point *snap_vector = NULL;
665             if (dr_snap) {
666                 if (state & GDK_MOD1_MASK) {
667                     // with Alt, snap to the original angle and its perpendiculars
668                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
669                 } else {
670                     // with Ctrl, snap to M_PI/snaps
671                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
672                 }
673             }
674             if (snap_vector) {
675                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
676             }
677         }
679         // Move by the smallest of snap vectors:
680         NR::Point move(9999, 9999);
681         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
682             NR::Point *snap_vector = (NR::Point *) i->data;
683             if (NR::L2(*snap_vector) < NR::L2(move))
684                 move = *snap_vector;
685         }
686         if (move[NR::X] < 9999) {
687             p += move;
688             sp_knot_moveto (knot, p);
689         }
691         g_slist_free(snap_vectors);
692     }
694     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
695     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
697     if (drag->keep_selection) {
698         NR::Point diff = p - dragger->point;
699         drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
700     } else {
701         dragger->point = p;
702         dragger->fireDraggables (false, scale_radial);
703         dragger->updateDependencies(false);
704     }
709 static void
710 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
713     GrDrag *drag = dragger->parent;
714     // a midpoint dragger can (logically) only contain one GrDraggable
715     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
717     // get begin and end points between which dragging is allowed:
718     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
719     *moving = g_slist_append(*moving, dragger);
721     guint lowest_i = draggable->point_i;
722     guint highest_i = draggable->point_i;
723     GrDragger *lowest_dragger = dragger;
724     GrDragger *highest_dragger = dragger;
725     if (dragger->isSelected()) {
726         GrDragger* d_add;
727         while ( true )
728         {
729             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
730             if ( d_add && g_list_find(drag->selected, d_add) ) {
731                 lowest_i = lowest_i - 1;
732                 *moving = g_slist_prepend(*moving, d_add);
733                 lowest_dragger = d_add;
734             } else {
735                 break;
736             }
737         }
739         while ( true )
740         {
741             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
742             if ( d_add && g_list_find(drag->selected, d_add) ) {
743                 highest_i = highest_i + 1;
744                 *moving = g_slist_append(*moving, d_add);
745                 highest_dragger = d_add;
746             } else {
747                 break;
748             }
749         }
750     }
752     if ( SP_IS_LINEARGRADIENT(server) ) {
753         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
754         GrDragger *d_temp;
755         if (lowest_i == 1) {
756             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
757         } else {
758             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
759         }
760         if (d_temp)
761             *begin = d_temp->point;
763         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
764         if (d_temp == NULL) {
765             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
766         }
767         if (d_temp)
768             *end = d_temp->point;
769     } else if ( SP_IS_RADIALGRADIENT(server) ) {
770         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
771         GrDragger *d_temp;
772         if (lowest_i == 1) {
773             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
774         } else {
775             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
776         }
777         if (d_temp)
778             *begin = d_temp->point;
780         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
781         if (d_temp == NULL) {
782             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
783         }
784         if (d_temp)
785             *end = d_temp->point;
786     }
788     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
789     *high_lim = dragger->point - (highest_dragger->point - *end);
794 /**
795 Called when a midpoint knot is dragged.
796 */
797 static void
798 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, NR::Point const *ppointer, guint state, gpointer data)
800     GrDragger *dragger = (GrDragger *) data;
801     GrDrag *drag = dragger->parent;
802     // a midpoint dragger can (logically) only contain one GrDraggable
803     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
805     // FIXME: take from prefs
806     double snap_fraction = 0.1;
808     NR::Point p = *ppointer;
809     NR::Point begin(0,0), end(0,0);
810     NR::Point low_lim(0,0), high_lim(0,0);
812     SPObject *server = draggable->getServer();
814     GSList *moving = NULL;
815     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
817     if (state & GDK_CONTROL_MASK) {
818         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
819     } else {
820         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
821     }
822     NR::Point displacement = p - dragger->point;
824     for (GSList const* i = moving; i != NULL; i = i->next) {
825         GrDragger *drg = (GrDragger*) i->data;
826         SPKnot *drgknot = drg->knot;
827         NR::Point this_move = displacement;
828         if (state & GDK_MOD1_MASK) {
829             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
830             double alpha = 1.0;
831             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
832                 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
833                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
834             } else { // drg is on the begin side from dragger
835                 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
836                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
837             }
838         }
839         drg->point += this_move;
840         sp_knot_moveto (drgknot, drg->point);
841         drg->fireDraggables (false);
842         drg->updateDependencies(false);
843     }
845     g_slist_free(moving);
847     drag->keep_selection = dragger->isSelected();
852 static void
853 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
855     GrDragger *dragger = (GrDragger *) data;
857     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
860 /**
861 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
862 */
863 static void
864 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
866     GrDragger *dragger = (GrDragger *) data;
868     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
870     dragger->point_original = dragger->point = knot->pos;
872     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
873         dragger->fireDraggables (true, true);
874     } else {
875         dragger->fireDraggables (true);
876     }
878     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
879         GrDragger *d = (GrDragger *) i->data;
880         if (d == dragger)
881             continue;
882         d->fireDraggables (true);
883     }
885     // make this dragger selected
886     if (!dragger->parent->keep_selection) {
887         dragger->parent->setSelected (dragger);
888     }
889     dragger->parent->keep_selection = false;
891     dragger->updateDependencies(true);
893     // we did an undoable action
894     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
895                       _("Move gradient handle"));
898 /**
899 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
900 state of the keyboard keys
901 */
902 static void
903 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
905     GrDragger *dragger = (GrDragger *) data;
906     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
907     if (!draggable) return;
909     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
910     // delete this knot from vector
911         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
912         gradient = sp_gradient_get_vector (gradient, false);
913         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
914                 SPStop *stop = NULL;
915                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
916                 case POINT_LG_BEGIN:
917                 case POINT_RG_CENTER:
918                     stop = sp_first_stop(gradient);
919                         {
920                             SPStop *next = sp_next_stop (stop);
921                                 if (next) {
922                                         next->offset = 0;
923                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
924                                 }
925                         }
926                     break;
927                 case POINT_LG_END:
928                 case POINT_RG_R1:
929                 case POINT_RG_R2:
930                     stop = sp_last_stop(gradient);
931                     {
932                             SPStop *prev = sp_prev_stop (stop, gradient);
933                             if (prev) {
934                                     prev->offset = 1;
935                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
936                             }
937                         }
938                     break;
939                 case POINT_LG_MID:
940                 case POINT_RG_MID1:
941                 case POINT_RG_MID2:
942                     stop = sp_get_stop_i(gradient, draggable->point_i);
943                     break;
944                 }
946                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
947                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
948                                   _("Delete gradient stop"));
949         }
950     } else {
951     // select the dragger
952         dragger->point_original = dragger->point;
954         if ( state & GDK_SHIFT_MASK ) {
955             dragger->parent->setSelected (dragger, true, false);
956         } else {
957             dragger->parent->setSelected (dragger);
958         }
959     }
962 /**
963 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
964 */
965 static void
966 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
968     GrDragger *dragger = (GrDragger *) data;
970     dragger->point_original = dragger->point;
972     if (dragger->draggables == NULL)
973         return;
975     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
976     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
979 /**
980 Act upon all draggables of the dragger, setting them to the dragger's point
981 */
982 void
983 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
985     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
986         GrDraggable *draggable = (GrDraggable *) i->data;
988         // set local_change flag so that selection_changed callback does not regenerate draggers
989         this->parent->local_change = true;
991         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
992         // to the center, unless it's the first update upon merge when we must snap it to the point
993         if (merging_focus ||
994             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
995         {
996             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
997         }
998     }
1001 /**
1002 Checks if the dragger has a draggable with this point_type
1003  */
1004 bool
1005 GrDragger::isA (gint point_type)
1007     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1008         GrDraggable *draggable = (GrDraggable *) i->data;
1009         if (draggable->point_type == point_type) {
1010             return true;
1011         }
1012     }
1013     return false;
1016 /**
1017 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1018  */
1019 bool
1020 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1022     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1023         GrDraggable *draggable = (GrDraggable *) i->data;
1024         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1025             return true;
1026         }
1027     }
1028     return false;
1031 /**
1032 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1033  */
1034 bool
1035 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1037     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1038         GrDraggable *draggable = (GrDraggable *) i->data;
1039         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1040             return true;
1041         }
1042     }
1043     return false;
1046 bool
1047 GrDraggable::mayMerge (GrDraggable *da2)
1049     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1050         // we must not merge the points of the same gradient!
1051         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1052               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1053             // except that we can snap center and focus together
1054             return false;
1055         }
1056     }
1057     // disable merging of midpoints.
1058     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1059          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1060          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1061         return false;
1063     return true;
1066 bool
1067 GrDragger::mayMerge (GrDragger *other)
1069     if (this == other)
1070         return false;
1072     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1073         GrDraggable *da1 = (GrDraggable *) i->data;
1074         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1075             GrDraggable *da2 = (GrDraggable *) j->data;
1076             if (!da1->mayMerge(da2))
1077                 return false;
1078         }
1079     }
1080     return true;
1083 bool
1084 GrDragger::mayMerge (GrDraggable *da2)
1086     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1087         GrDraggable *da1 = (GrDraggable *) i->data;
1088         if (!da1->mayMerge(da2))
1089             return false;
1090     }
1091     return true;
1094 /**
1095 Updates the statusbar tip of the dragger knot, based on its draggables
1096  */
1097 void
1098 GrDragger::updateTip ()
1100         if (this->knot && this->knot->tip) {
1101                 g_free (this->knot->tip);
1102                 this->knot->tip = NULL;
1103         }
1105     if (g_slist_length (this->draggables) == 1) {
1106         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1107         char *item_desc = sp_item_description(draggable->item);
1108         switch (draggable->point_type) {
1109             case POINT_LG_MID:
1110             case POINT_RG_MID1:
1111             case POINT_RG_MID2:
1112                 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"),
1113                                                    _(gr_knot_descr[draggable->point_type]),
1114                                                    draggable->point_i,
1115                                                    item_desc,
1116                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1117                 break;
1119             default:
1120                 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"),
1121                                                    _(gr_knot_descr[draggable->point_type]),
1122                                                    item_desc,
1123                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1124                 break;
1125         }
1126         g_free(item_desc);
1127     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1128         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1129     } else {
1130         int length = g_slist_length (this->draggables);
1131         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1132                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1133                                                     length),
1134                                            length);
1135     }
1138 /**
1139 Adds a draggable to the dragger
1140  */
1141 void
1142 GrDragger::updateKnotShape ()
1144     if (!draggables)
1145         return;
1146     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1147     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1150 /**
1151 Adds a draggable to the dragger
1152  */
1153 void
1154 GrDragger::addDraggable (GrDraggable *draggable)
1156     this->draggables = g_slist_prepend (this->draggables, draggable);
1158     this->updateTip();
1162 /**
1163 Moves this dragger to the point of the given draggable, acting upon all other draggables
1164  */
1165 void
1166 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1168     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1169     this->point_original = this->point;
1171     sp_knot_moveto (this->knot, this->point);
1173     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1174         GrDraggable *da = (GrDraggable *) i->data;
1175         if ( (da->item == item) &&
1176              (point_type == -1 || da->point_type == point_type) &&
1177              (point_i == -1 || da->point_i == point_i) &&
1178              (da->fill_or_stroke == fill_or_stroke) ) {
1179             continue;
1180         }
1181         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1182     }
1183     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1187 /**
1188 Moves all midstop draggables that depend on this one
1189  */
1190 void
1191 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1192     SPObject *server = draggable->getServer();
1193     if (!server)
1194         return;
1195     guint num = SP_GRADIENT(server)->vector.stops.size();
1196     if (num <= 2) return;
1198     if ( SP_IS_LINEARGRADIENT(server) ) {
1199         for ( guint i = 1; i < num - 1; i++ ) {
1200             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1201         }
1202     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1203         for ( guint i = 1; i < num - 1; i++ ) {
1204             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1205             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1206         }
1207     }
1211 /**
1212 Moves all draggables that depend on this one
1213  */
1214 void
1215 GrDragger::updateDependencies (bool write_repr)
1217     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1218         GrDraggable *draggable = (GrDraggable *) i->data;
1219         switch (draggable->point_type) {
1220             case POINT_LG_BEGIN:
1221                 {
1222                     // the end point is dependent only when dragging with ctrl+shift
1223                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1225                     this->updateMidstopDependencies (draggable, write_repr);
1226                 }
1227                 break;
1228             case POINT_LG_END:
1229                 {
1230                     // the begin point is dependent only when dragging with ctrl+shift
1231                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1233                     this->updateMidstopDependencies (draggable, write_repr);
1234                 }
1235                 break;
1236             case POINT_LG_MID:
1237                 // no other nodes depend on mid points.
1238                 break;
1239             case POINT_RG_R2:
1240                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1241                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1242                 this->updateMidstopDependencies (draggable, write_repr);
1243                 break;
1244             case POINT_RG_R1:
1245                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1246                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1247                 this->updateMidstopDependencies (draggable, write_repr);
1248                 break;
1249             case POINT_RG_CENTER:
1250                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1251                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1252                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1253                 this->updateMidstopDependencies (draggable, write_repr);
1254                 break;
1255             case POINT_RG_FOCUS:
1256                 // nothing can depend on that
1257                 break;
1258             case POINT_RG_MID1:
1259                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1260                 break;
1261             case POINT_RG_MID2:
1262                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1263                 break;
1264             default:
1265                 break;
1266         }
1267     }
1272 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1274     this->draggables = NULL;
1276     this->parent = parent;
1278     this->point = p;
1279     this->point_original = p;
1281     // create the knot
1282     this->knot = sp_knot_new (parent->desktop, NULL);
1283     this->knot->setMode(SP_KNOT_MODE_XOR);
1284     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1285     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1286     sp_knot_update_ctrl(this->knot);
1288     // move knot to the given point
1289     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1290     sp_knot_show (this->knot);
1292     // connect knot's signals
1293     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1294                        // luckily, midstops never snap to other nodes so are never unsnapped...
1295          && ( (draggable->point_type == POINT_LG_MID)
1296               || (draggable->point_type == POINT_RG_MID1)
1297               || (draggable->point_type == POINT_RG_MID2) ) )
1298     {
1299         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1300     } else {
1301         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1302     }
1303     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1304     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1305     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1306     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1308     // add the initial draggable
1309     if (draggable)
1310         this->addDraggable (draggable);
1311     updateKnotShape();
1314 GrDragger::~GrDragger ()
1316     // unselect if it was selected
1317     this->parent->setDeselected(this);
1319     // disconnect signals
1320     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1321     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1322     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1323     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1324     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1326     /* unref should call destroy */
1327     g_object_unref (G_OBJECT (this->knot));
1329     // delete all draggables
1330     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1331         delete ((GrDraggable *) i->data);
1332     }
1333     g_slist_free (this->draggables);
1334     this->draggables = NULL;
1337 /**
1338 Select the dragger which has the given draggable.
1339 */
1340 GrDragger *
1341 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1343     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1344         GrDragger *dragger = (GrDragger *) i->data;
1345         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1346             GrDraggable *da2 = (GrDraggable *) j->data;
1347             if ( (da2->item == item) &&
1348                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1349                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1350                  (da2->fill_or_stroke == fill_or_stroke)) {
1351                 return (dragger);
1352             }
1353         }
1354     }
1355     return NULL;
1359 void
1360 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1362     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1363     if (d && d !=  this) {
1364         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1365     }
1369 /**
1370   Draw this dragger as selected
1371 */
1372 void
1373 GrDragger::select()
1375     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1376     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1379 /**
1380   Draw this dragger as normal (deselected)
1381 */
1382 void
1383 GrDragger::deselect()
1385     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1386     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1389 bool
1390 GrDragger::isSelected()
1392     return g_list_find (parent->selected, this);
1395 /**
1396 \brief Deselect all stops/draggers (private)
1397 */
1398 void
1399 GrDrag::deselect_all()
1401     while (selected) {
1402         ( (GrDragger*) selected->data)->deselect();
1403         selected = g_list_remove(selected, selected->data);
1404     }
1407 /**
1408 \brief Deselect all stops/draggers (public; emits signal)
1409 */
1410 void
1411 GrDrag::deselectAll()
1413     deselect_all();
1414     this->desktop->emitToolSubselectionChanged(NULL);
1417 /**
1418 \brief Select all stops/draggers
1419 */
1420 void
1421 GrDrag::selectAll()
1423     for (GList *l = this->draggers; l != NULL; l = l->next) {
1424         GrDragger *d = ((GrDragger *) l->data);
1425         setSelected (d, true, true);
1426     }
1429 /**
1430 \brief Select all stops/draggers that match the coords
1431 */
1432 void
1433 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1435     for (GList *l = this->draggers; l != NULL; l = l->next) {
1436         GrDragger *d = ((GrDragger *) l->data);
1437         for (guint k = 0; k < coords.size(); k++) {
1438             if (NR::L2 (d->point - coords[k]) < 1e-4) {
1439                 setSelected (d, true, true);
1440             }
1441         }
1442     }
1446 /**
1447 \brief Select all stops/draggers that fall within the rect
1448 */
1449 void
1450 GrDrag::selectRect(Geom::Rect const &r)
1452     for (GList *l = this->draggers; l != NULL; l = l->next) {
1453         GrDragger *d = ((GrDragger *) l->data);
1454         if (r.contains(d->point)) {
1455            setSelected (d, true, true);
1456         }
1457     }
1460 /**
1461 \brief Select a dragger
1462 \param dragger       The dragger to select
1463 \param add_to_selection   If true, add to selection, otherwise deselect others
1464 \param override      If true, always select this node, otherwise toggle selected status
1465 */
1466 void
1467 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1469     GrDragger *seldragger = NULL;
1471     if (add_to_selection) {
1472         if (!dragger) return;
1473         if (override) {
1474             if (!g_list_find(selected, dragger)) {
1475                 selected = g_list_prepend(selected, dragger);
1476             }
1477             dragger->select();
1478             seldragger = dragger;
1479         } else { // toggle
1480             if (g_list_find(selected, dragger)) {
1481                 selected = g_list_remove(selected, dragger);
1482                 dragger->deselect();
1483                 if (selected) {
1484                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1485                 }
1486             } else {
1487                 selected = g_list_prepend(selected, dragger);
1488                 dragger->select();
1489                 seldragger = dragger;
1490             }
1491         }
1492     } else {
1493         deselect_all();
1494         if (dragger) {
1495             selected = g_list_prepend(selected, dragger);
1496             dragger->select();
1497             seldragger = dragger;
1498         }
1499     }
1500     if (seldragger) {
1501         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1502     }
1505 /**
1506 \brief Deselect a dragger
1507 \param dragger       The dragger to deselect
1508 */
1509 void
1510 GrDrag::setDeselected (GrDragger *dragger)
1512     if (g_list_find(selected, dragger)) {
1513         selected = g_list_remove(selected, dragger);
1514         dragger->deselect();
1515     }
1516     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1521 /**
1522 Create a line from p1 to p2 and add it to the lines list
1523  */
1524 void
1525 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1527     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1528                                                             SP_TYPE_CTRLLINE, NULL);
1529     SP_CTRLLINE(line)->item = item;
1530     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1531     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1532         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1533     sp_canvas_item_show (line);
1534     this->lines = g_slist_append (this->lines, line);
1537 /**
1538 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1539 new dragger and add it to draggers list
1540  */
1541 void
1542 GrDrag::addDragger (GrDraggable *draggable)
1544     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1546     for (GList *i = this->draggers; i != NULL; i = i->next) {
1547         GrDragger *dragger = (GrDragger *) i->data;
1548         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1549             // distance is small, merge this draggable into dragger, no need to create new dragger
1550             dragger->addDraggable (draggable);
1551             dragger->updateKnotShape();
1552             return;
1553         }
1554     }
1556     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1557     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1558     this->draggers = g_list_append (this->draggers, new_dragger);
1561 /**
1562 Add draggers for the radial gradient rg on item
1563 */
1564 void
1565 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1567     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1568     guint num = rg->vector.stops.size();
1569     if (num > 2) {
1570         for ( guint i = 1; i < num - 1; i++ ) {
1571             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1572         }
1573     }
1574     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1575     if (num > 2) {
1576         for ( guint i = 1; i < num - 1; i++ ) {
1577             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1578         }
1579     }
1580     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1581     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1584 /**
1585 Add draggers for the linear gradient lg on item
1586 */
1587 void
1588 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1590     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1591     guint num = lg->vector.stops.size();
1592     if (num > 2) {
1593         for ( guint i = 1; i < num - 1; i++ ) {
1594             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1595         }
1596     }
1597     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1600 /**
1601 Artificially grab the knot of this dragger; used by the gradient context
1602 */
1603 void
1604 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1606     if (dragger) {
1607         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1608     }
1611 /**
1612 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1613 */
1614 void
1615 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1617     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1618     if (dragger) {
1619         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1620     }
1623 /**
1624 Regenerates the draggers list from the current selection; is called when selection is changed or
1625 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1626 */
1627 void
1628 GrDrag::updateDraggers ()
1630     while (selected) {
1631         selected = g_list_remove(selected, selected->data);
1632     }
1633     // delete old draggers
1634     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1635         delete ((GrDragger *) i->data);
1636     }
1637     g_list_free (this->draggers);
1638     this->draggers = NULL;
1640     g_return_if_fail (this->selection != NULL);
1642     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1644         SPItem *item = SP_ITEM(i->data);
1645         SPStyle *style = SP_OBJECT_STYLE (item);
1647         if (style && (style->fill.isPaintserver())) {
1648             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1649             if (SP_IS_LINEARGRADIENT (server)) {
1650                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1651             } else if (SP_IS_RADIALGRADIENT (server)) {
1652                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1653             }
1654         }
1656         if (style && (style->stroke.isPaintserver())) {
1657             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1658             if (SP_IS_LINEARGRADIENT (server)) {
1659                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1660             } else if (SP_IS_RADIALGRADIENT (server)) {
1661                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1662             }
1663         }
1664     }
1667 /**
1668 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1669 lines are always in sync with the actual gradient
1670 */
1671 void
1672 GrDrag::updateLines ()
1674     // delete old lines
1675     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1676         gtk_object_destroy( GTK_OBJECT (i->data));
1677     }
1678     g_slist_free (this->lines);
1679     this->lines = NULL;
1681     g_return_if_fail (this->selection != NULL);
1683     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1685         SPItem *item = SP_ITEM(i->data);
1687         SPStyle *style = SP_OBJECT_STYLE (item);
1689         if (style && (style->fill.isPaintserver())) {
1690             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1691             if (SP_IS_LINEARGRADIENT (server)) {
1692                 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);
1693             } else if (SP_IS_RADIALGRADIENT (server)) {
1694                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1695                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1696                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1697             }
1698         }
1700         if (style && (style->stroke.isPaintserver())) {
1701             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1702             if (SP_IS_LINEARGRADIENT (server)) {
1703                 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);
1704             } else if (SP_IS_RADIALGRADIENT (server)) {
1705                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1706                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1707                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1708             }
1709         }
1710     }
1713 /**
1714 Regenerates the levels list from the current selection
1715 */
1716 void
1717 GrDrag::updateLevels ()
1719     hor_levels.clear();
1720     vert_levels.clear();
1722     g_return_if_fail (this->selection != NULL);
1724     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1725         SPItem *item = SP_ITEM(i->data);
1726         boost::optional<NR::Rect> rect = sp_item_bbox_desktop (item);
1727         if (rect) {
1728             // Remember the edges of the bbox and the center axis
1729             hor_levels.push_back(rect->min()[NR::Y]);
1730             hor_levels.push_back(rect->max()[NR::Y]);
1731             hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1732             vert_levels.push_back(rect->min()[NR::X]);
1733             vert_levels.push_back(rect->max()[NR::X]);
1734             vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1735         }
1736     }
1739 void
1740 GrDrag::selected_reverse_vector ()
1742     if (selected == NULL)
1743         return;
1745     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1746         GrDraggable *draggable = (GrDraggable *) i->data;
1748         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1749     }
1752 void
1753 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1755     selected_move (x, y, false, scale_radial);
1758 void
1759 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1761     if (selected == NULL)
1762         return;
1764     bool did = false;
1766     for (GList *i = selected; i != NULL; i = i->next) {
1767         GrDragger *d = (GrDragger *) i->data;
1769         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1770             // if this is an endpoint,
1772             // Moving an rg center moves its focus and radii as well.
1773             // therefore, if this is a focus or radius and if selection
1774             // contains the center as well, do not move this one
1775             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1776                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1777                 bool skip_radius_with_center = false;
1778                 for (GList *di = selected; di != NULL; di = di->next) {
1779                     GrDragger *d_new = (GrDragger *) di->data;
1780                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1781                                     POINT_RG_CENTER,
1782                                     0,
1783                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1784                         // FIXME: here we take into account only the first draggable!
1785                         skip_radius_with_center = true;
1786                     }
1787                 }
1788                 if (skip_radius_with_center)
1789                     continue;
1790             }
1792             did = true;
1793             d->point += NR::Point (x, y);
1794             d->point_original = d->point;
1795             sp_knot_moveto (d->knot, d->point);
1797             d->fireDraggables (write_repr, scale_radial);
1799             d->updateDependencies(write_repr);
1800         }
1801     }
1803     if (write_repr && did) {
1804         // we did an undoable action
1805         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1806                                 _("Move gradient handle(s)"));
1807         return;
1808     }
1810     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1812         GrDragger *dragger = (GrDragger *) selected->data;
1813         // a midpoint dragger can (logically) only contain one GrDraggable
1814         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1816         NR::Point begin(0,0), end(0,0);
1817         NR::Point low_lim(0,0), high_lim(0,0);
1819         SPObject *server = draggable->getServer();
1820         GSList *moving = NULL;
1821         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1823         NR::Point p(x, y);
1824         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1825         NR::Point displacement = p - dragger->point;
1827         for (GSList const* i = moving; i != NULL; i = i->next) {
1828             GrDragger *drg = (GrDragger*) i->data;
1829             SPKnot *drgknot = drg->knot;
1830             drg->point += displacement;
1831             sp_knot_moveto (drgknot, drg->point);
1832             drg->fireDraggables (true);
1833             drg->updateDependencies(true);
1834             did = true;
1835         }
1837         g_slist_free(moving);
1839         if (write_repr && did) {
1840             // we did an undoable action
1841             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1842                                     _("Move gradient mid stop(s)"));
1843         }
1844     }
1847 void
1848 GrDrag::selected_move_screen (double x, double y)
1850     gdouble zoom = desktop->current_zoom();
1851     gdouble zx = x / zoom;
1852     gdouble zy = y / zoom;
1854     selected_move (zx, zy);
1857 /**
1858 Select the knot next to the last selected one and deselect all other selected.
1859 */
1860 GrDragger *
1861 GrDrag::select_next ()
1863     GrDragger *d = NULL;
1864     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1865         if (draggers)
1866             d = (GrDragger *) draggers->data;
1867     } else {
1868         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1869     }
1870     if (d)
1871         setSelected (d);
1872     return d;
1875 /**
1876 Select the knot previous from the last selected one and deselect all other selected.
1877 */
1878 GrDragger *
1879 GrDrag::select_prev ()
1881     GrDragger *d = NULL;
1882     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1883         if (draggers)
1884             d = (GrDragger *) g_list_last (draggers)->data;
1885     } else {
1886         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1887     }
1888     if (d)
1889         setSelected (d);
1890     return d;
1894 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1895 void
1896 GrDrag::deleteSelected (bool just_one)
1898     if (!selected) return;
1900     SPDocument *document = false;
1902     struct StructStopInfo {
1903         SPStop * spstop;
1904         GrDraggable * draggable;
1905         SPGradient * gradient;
1906         SPGradient * vector;
1907     };
1909     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1910     GSList *endstoplist = NULL;  // list of stops that must be deleted
1911     while (selected) {
1912         GrDragger *dragger = (GrDragger*) selected->data;
1913         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1914             GrDraggable *draggable = (GrDraggable*) drgble->data;
1915             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1916             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1918             switch (draggable->point_type) {
1919                 case POINT_LG_MID:
1920                 case POINT_RG_MID1:
1921                 case POINT_RG_MID2:
1922                     {
1923                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1924                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1925                         bool present = false;
1926                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1927                             if ( (SPStop*)l->data == stop ) {
1928                                 present = true;
1929                                 break; // no need to search further.
1930                             }
1931                         }
1932                         if (!present)
1933                             midstoplist = g_slist_append(midstoplist, stop);
1934                     }
1935                     break;
1936                 case POINT_LG_BEGIN:
1937                 case POINT_LG_END:
1938                 case POINT_RG_CENTER:
1939                 case POINT_RG_R1:
1940                 case POINT_RG_R2:
1941                     {
1942                         SPStop *stop = NULL;
1943                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1944                             stop = sp_first_stop(vector);
1945                         } else {
1946                             stop = sp_last_stop(vector);
1947                         }
1948                         if (stop) {
1949                             StructStopInfo *stopinfo = new StructStopInfo;
1950                             stopinfo->spstop = stop;
1951                             stopinfo->draggable = draggable;
1952                             stopinfo->gradient = gradient;
1953                             stopinfo->vector = vector;
1954                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1955                             bool present = false;
1956                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1957                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1958                                     present = true;
1959                                     break; // no need to search further.
1960                                 }
1961                             }
1962                             if (!present)
1963                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1964                         }
1965                     }
1966                     break;
1967                 default:
1968                     break;
1969             }
1970         }
1971         selected = g_list_remove(selected, dragger);
1972         if ( just_one ) break; // iterate once if just_one is set.
1973     }
1974     while (midstoplist) {
1975         SPStop *stop = (SPStop*) midstoplist->data;
1976         document = SP_OBJECT_DOCUMENT (stop);
1977         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1978         parent->removeChild(SP_OBJECT_REPR(stop));
1979         midstoplist = g_slist_remove(midstoplist, stop);
1980     }
1981     while (endstoplist) {
1982         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1983         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1985         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1986         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1987         // manually count the children, don't know if there already exists a function for this...
1988         int len = 0;
1989         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1990               child != NULL ;
1991               child = SP_OBJECT_NEXT(child) )
1992         {
1993             if ( SP_IS_STOP(child) )  len ++;
1994         }
1995         if (len > 2)
1996         {
1997             switch (stopinfo->draggable->point_type) {
1998                 case POINT_LG_BEGIN:
1999                     {
2000                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2002                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2003                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
2004                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
2005                         SPStop *stop = sp_first_stop(stopinfo->vector);
2006                         gdouble offset = stop->offset;
2007                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
2008                         lg->x1.computed = newbegin[NR::X];
2009                         lg->y1.computed = newbegin[NR::Y];
2011                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2012                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2013                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2014                         stop->offset = 0;
2015                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2017                         // iterate through midstops to set new offset values such that they won't move on canvas.
2018                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2019                         stop = sp_next_stop(stop);
2020                         while ( stop != laststop ) {
2021                             stop->offset = (stop->offset - offset)/(1 - offset);
2022                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2023                             stop = sp_next_stop(stop);
2024                         }
2025                     }
2026                     break;
2027                 case POINT_LG_END:
2028                     {
2029                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2031                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2032                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
2033                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
2034                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2035                         gdouble offset = laststop->offset;
2036                         NR::Point newend = begin + offset * (oldend - begin);
2037                         lg->x2.computed = newend[NR::X];
2038                         lg->y2.computed = newend[NR::Y];
2040                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2041                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2042                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2043                         laststop->offset = 1;
2044                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2046                         // iterate through midstops to set new offset values such that they won't move on canvas.
2047                         SPStop *stop = sp_first_stop(stopinfo->vector);
2048                         stop = sp_next_stop(stop);
2049                         while ( stop != laststop ) {
2050                             stop->offset = stop->offset / offset;
2051                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2052                             stop = sp_next_stop(stop);
2053                         }
2054                     }
2055                     break;
2056                 case POINT_RG_CENTER:
2057                     {
2058                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2059                         if (newfirst) {
2060                             newfirst->offset = 0;
2061                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2062                         }
2063                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2064                     }
2065                     break;
2066                 case POINT_RG_R1:
2067                 case POINT_RG_R2:
2068                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2070                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2071                         double oldradius = rg->r.computed;
2072                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2073                         gdouble offset = laststop->offset;
2074                         double newradius = offset * oldradius;
2075                         rg->r.computed = newradius;
2077                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2078                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2079                         laststop->offset = 1;
2080                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2082                         // iterate through midstops to set new offset values such that they won't move on canvas.
2083                         SPStop *stop = sp_first_stop(stopinfo->vector);
2084                         stop = sp_next_stop(stop);
2085                         while ( stop != laststop ) {
2086                             stop->offset = stop->offset / offset;
2087                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2088                             stop = sp_next_stop(stop);
2089                         }
2090                         break;
2091             }
2092         }
2093         else
2094         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2095             SPCSSAttr *css = sp_repr_css_attr_new ();
2097             // stopinfo->spstop is the selected stop
2098             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2099             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2100                 unselectedrepr = unselectedrepr->next();
2101             }
2103             if (unselectedrepr == NULL) {
2104                 if (stopinfo->draggable->fill_or_stroke) {
2105                     sp_repr_css_unset_property (css, "fill");
2106                 } else {
2107                     sp_repr_css_unset_property (css, "stroke");
2108                 }
2109             } else {
2110                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2111                 if (stopinfo->draggable->fill_or_stroke) {
2112                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2113                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2114                 } else {
2115                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2116                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2117                 }
2118                 sp_repr_css_attr_unref (stopcss);
2119             }
2121             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2122             sp_repr_css_attr_unref (css);
2123         }
2125         endstoplist = g_slist_remove(endstoplist, stopinfo);
2126         delete stopinfo;
2127     }
2129     if (document) {
2130         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2131     }
2135 /*
2136   Local Variables:
2137   mode:c++
2138   c-file-style:"stroustrup"
2139   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2140   indent-tabs-mode:nil
2141   fill-column:99
2142   End:
2143 */
2144 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :