Code

add copy-to-clipboard for selected stops
[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>
22 #include "desktop-handles.h"
23 #include "selection.h"
24 #include "desktop.h"
25 #include "desktop-style.h"
26 #include "document.h"
27 #include "display/sp-ctrlline.h"
28 #include "xml/repr.h"
29 #include "svg/css-ostringstream.h"
30 #include "svg/svg.h"
31 #include "libnr/nr-point-fns.h"
32 #include "prefs-utils.h"
33 #include "sp-item.h"
34 #include "style.h"
35 #include "knot.h"
36 #include "sp-linear-gradient.h"
37 #include "sp-radial-gradient.h"
38 #include "gradient-chemistry.h"
39 #include "gradient-drag.h"
40 #include "sp-stop.h"
41 #include "snap.h"
42 #include "sp-namedview.h"
43 #include "selection-chemistry.h"
46 #define GR_KNOT_COLOR_NORMAL 0xffffff00
47 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
48 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
50 #define GR_LINE_COLOR_FILL 0x0000ff7f
51 #define GR_LINE_COLOR_STROKE 0x9999007f
53 // screen pixels between knots when they snap:
54 #define SNAP_DIST 5
56 // absolute distance between gradient points for them to become a single dragger when the drag is created:
57 #define MERGE_DIST 0.1
59 // knot shapes corresponding to GrPointType enum
60 SPKnotShapeType gr_knot_shapes [] = {
61         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
62         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
63         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
64         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
65         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
66         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
67         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
68         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
69         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
70 };
72 const gchar *gr_knot_descr [] = {
73     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
74     N_("Linear gradient <b>end</b>"),
75     N_("Linear gradient <b>mid stop</b>"),
76     N_("Radial gradient <b>center</b>"),
77     N_("Radial gradient <b>radius</b>"),
78     N_("Radial gradient <b>radius</b>"),
79     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
80     N_("Radial gradient <b>mid stop</b>"),
81     N_("Radial gradient <b>mid stop</b>")
82 };
84 static void
85 gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
86 {
87         GrDrag *drag = (GrDrag *) data;
88         drag->updateDraggers ();
89         drag->updateLines ();
90         drag->updateLevels ();
91 }
93 static void
94 gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
95 {
96     GrDrag *drag = (GrDrag *) data;
97     if (drag->local_change) {
98         drag->local_change = false;
99     } else {
100         drag->updateDraggers ();
101     }
102     drag->updateLines ();
103     drag->updateLevels ();
106 /**
107 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
108 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
109 any.
110 */
111 int
112 gr_drag_style_query (SPStyle *style, int property, gpointer data)
114     GrDrag *drag = (GrDrag *) data;
116     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
117         return QUERY_STYLE_NOTHING;
118     }
120     if (!drag->selected) {
121         return QUERY_STYLE_NOTHING;
122     } else {
123         int ret = QUERY_STYLE_NOTHING;
125         float cf[4];
126         cf[0] = cf[1] = cf[2] = cf[3] = 0;
128         int count = 0;
130         for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
131             GrDragger *d = (GrDragger *) i->data;
132             for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
133                 GrDraggable *draggable = (GrDraggable *) j->data;
135                 if (ret == QUERY_STYLE_NOTHING) {
136                     ret = QUERY_STYLE_SINGLE;
137                 } else if (ret == QUERY_STYLE_SINGLE) {
138                     ret = QUERY_STYLE_MULTIPLE_AVERAGED;
139                 }
141                 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
142                 cf[0] += SP_RGBA32_R_F (c);
143                 cf[1] += SP_RGBA32_G_F (c);
144                 cf[2] += SP_RGBA32_B_F (c);
145                 cf[3] += SP_RGBA32_A_F (c);
147                 count ++;
148             }
149         }
151         if (count) {
152             cf[0] /= count;
153             cf[1] /= count;
154             cf[2] /= count;
155             cf[3] /= count;
157             // set both fill and stroke with our stop-color and stop-opacity
158             style->fill.clear();
159             style->fill.setColor( cf[0], cf[1], cf[2] );
160             style->fill.set = TRUE;
161             style->stroke.clear();
162             style->stroke.setColor( cf[0], cf[1], cf[2] );
163             style->stroke.set = TRUE;
165             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
166             style->fill_opacity.set = TRUE;
167             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
168             style->stroke_opacity.set = TRUE;
170             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
171             style->opacity.set = TRUE;
172         }
174         return ret;
175     }
178 bool
179 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
181     GrDrag *drag = (GrDrag *) data;
183     if (!drag->selected)
184         return false;
186     SPCSSAttr *stop = sp_repr_css_attr_new ();
188     // See if the css contains interesting properties, and if so, translate them into the format
189     // acceptable for gradient stops
191     // any of color properties, in order of increasing priority:
192     if (css->attribute("flood-color"))
193         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
195     if (css->attribute("lighting-color"))
196         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
198     if (css->attribute("color"))
199         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
201     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
202         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
204     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
205         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
207     if (css->attribute("stop-color"))
208         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
211     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
212         sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
213     } else {  // multiply all opacity properties:
214         gdouble accumulated = 1.0;
215         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
216         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
217         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
218         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
220         Inkscape::CSSOStringStream os;
221         os << accumulated;
222         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
224         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
225             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
226             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
227     }
229     if (!stop->attributeList()) { // nothing for us here, pass it on
230         sp_repr_css_attr_unref(stop);
231         return false;
232     }
234     for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
235         GrDragger* dragger = (GrDragger*) sel->data;
236         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
237                GrDraggable *draggable = (GrDraggable *) i->data;
239                drag->local_change = true;
240                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
241         }
242     }
244     //sp_repr_css_print(stop);
245     sp_repr_css_attr_unref(stop);
246     return true;
249 bool
250 GrDrag::copy()
252     if (!selected)
253         return false;
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     guint32 const c32 = SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
283     gchar c[64];
285     SPCSSAttr *css = sp_repr_css_attr_new ();
286     g_snprintf(c, 64, "#%06x", c32 >> 8);
287     sp_repr_css_set_property (css, "fill", c);
288     Inkscape::CSSOStringStream os;
289     os << cf[3];
290     sp_repr_css_set_property (css, "opacity", os.str().c_str());
291     sp_set_style_clipboard (css);
293     g_snprintf(c, 64, "%06x%02x", c32 >> 8, c32 & 0x000000ff);
294     Glib::ustring text;
295     text += c;
296     if (!text.empty())
297     {
298         Glib::RefPtr<Gtk::Clipboard> refClipboard =
299             Gtk::Clipboard::get();
300         refClipboard->set_text(text);
301     }
303     return true;
306 SPStop *
307 GrDrag::addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance)
309     gfloat offset; // type of SPStop.offset = gfloat
310     SPGradient *gradient;
311     bool fill_or_stroke = true;
312     bool r1_knot = false;
314     bool addknot = false;
315     do {
316         gradient = sp_item_gradient (item, fill_or_stroke);
317         if (SP_IS_LINEARGRADIENT(gradient)) {
318             NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
319             NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
321             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
322             double dist_screen = NR::L2 (mouse_p - nearest);
323             if ( dist_screen < tolerance ) {
324                 // add the knot
325                 offset = get_offset_between_points(nearest, begin, end);
326                 addknot = true;
327                 break; // break out of the while loop: add only one knot
328             }
329         } else if (SP_IS_RADIALGRADIENT(gradient)) {
330             NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
331             NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
332             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
333             double dist_screen = NR::L2 (mouse_p - nearest);
334             if ( dist_screen < tolerance ) {
335                 offset = get_offset_between_points(nearest, begin, end);
336                 addknot = true;
337                 r1_knot = true;
338                 break; // break out of the while loop: add only one knot
339             }
341             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
342             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
343             dist_screen = NR::L2 (mouse_p - nearest);
344             if ( dist_screen < tolerance ) {
345                 offset = get_offset_between_points(nearest, begin, end);
346                 addknot = true;
347                 r1_knot = false;
348                 break; // break out of the while loop: add only one knot
349             }
350         }
351         fill_or_stroke = !fill_or_stroke;
352     } while (!fill_or_stroke && !addknot) ;
354     if (addknot) {
355         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
356         SPStop* prev_stop = sp_first_stop(vector);
357         SPStop* next_stop = sp_next_stop(prev_stop);
358         guint i = 1;
359         while ( (next_stop) && (next_stop->offset < offset) ) {
360             prev_stop = next_stop;
361             next_stop = sp_next_stop(next_stop);
362             i++;
363         }
364         if (!next_stop) {
365             // logical error: the endstop should have offset 1 and should always be more than this offset here
366             return NULL;
367         }
370         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
371         sp_gradient_ensure_vector (gradient);
372         updateDraggers();
374         return newstop;
375     } 
377     return NULL;
381 bool 
382 GrDrag::dropColor(SPItem *item, gchar *c, NR::Point p) 
384     // first, see if we can drop onto one of the existing draggers
385     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
386         GrDragger *d = (GrDragger *) i->data;
388         if (NR::L2(p - d->point)*desktop->current_zoom() < 5) {
389            SPCSSAttr *stop = sp_repr_css_attr_new ();
390            sp_repr_css_set_property (stop, "stop-color", c);
391            sp_repr_css_set_property (stop, "stop-opacity", "1");
392            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
393                GrDraggable *draggable = (GrDraggable *) j->data;
394                local_change = true;
395                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
396            }
397            sp_repr_css_attr_unref(stop);
398            return true;
399         }
400     }
402     // now see if we're over line and create a new stop
403     bool over_line = false;
404     SPCtrlLine *line = NULL;
405     if (lines) {
406         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
407             line = (SPCtrlLine*) l->data;
408             NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
409             double dist_screen = NR::L2 (p - nearest) * desktop->current_zoom();
410             if (line->item && dist_screen < 5) {
411                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
412                 if (stop) {
413                     SPCSSAttr *css = sp_repr_css_attr_new ();
414                     sp_repr_css_set_property (css, "stop-color", c);
415                     sp_repr_css_set_property (css, "stop-opacity", "1");
416                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
417                     return true;
418                 }
419             }
420         }
421     }
423     return false;
427 GrDrag::GrDrag(SPDesktop *desktop) {
429     this->desktop = desktop;
431     this->selection = sp_desktop_selection(desktop);
433     this->draggers = NULL;
434     this->lines = NULL;
435     this->selected = NULL;
437     this->hor_levels.clear();
438     this->vert_levels.clear();
440     this->local_change = false;
442     this->sel_changed_connection = this->selection->connectChanged(
443         sigc::bind (
444             sigc::ptr_fun(&gr_drag_sel_changed),
445             (gpointer)this )
447         );
448     this->sel_modified_connection = this->selection->connectModified(
449         sigc::bind(
450             sigc::ptr_fun(&gr_drag_sel_modified),
451             (gpointer)this )
452         );
454     this->style_set_connection = this->desktop->connectSetStyle(
455         sigc::bind(
456             sigc::ptr_fun(&gr_drag_style_set),
457             (gpointer)this )
458         );
460     this->style_query_connection = this->desktop->connectQueryStyle(
461         sigc::bind(
462             sigc::ptr_fun(&gr_drag_style_query),
463             (gpointer)this )
464         );
466     this->updateDraggers ();
467     this->updateLines ();
468     this->updateLevels ();
470     if (desktop->gr_item) {
471         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
472     }
475 GrDrag::~GrDrag()
477     this->sel_changed_connection.disconnect();
478     this->sel_modified_connection.disconnect();
479     this->style_set_connection.disconnect();
480     this->style_query_connection.disconnect();
482     if (this->selected) {
483         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
484         desktop->gr_item = draggable->item;
485         desktop->gr_point_type = draggable->point_type;
486         desktop->gr_point_i = draggable->point_i;
487         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
488     } else {
489         desktop->gr_item = NULL;
490         desktop->gr_point_type = 0;
491         desktop->gr_point_i = 0;
492         desktop->gr_fill_or_stroke = true;
493     }
495     deselect_all();
496     for (GList *l = this->draggers; l != NULL; l = l->next) {
497         delete ((GrDragger *) l->data);
498     }
499     g_list_free (this->draggers);
500     this->draggers = NULL;
501     this->selected = NULL;
503     for (GSList *l = this->lines; l != NULL; l = l->next) {
504         gtk_object_destroy( GTK_OBJECT (l->data));
505     }
506     g_slist_free (this->lines);
507     this->lines = NULL;
510 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
512     this->item = item;
513     this->point_type = point_type;
514     this->point_i = point_i;
515     this->fill_or_stroke = fill_or_stroke;
517     g_object_ref (G_OBJECT (this->item));
520 GrDraggable::~GrDraggable ()
522     g_object_unref (G_OBJECT (this->item));
526 SPObject *
527 GrDraggable::getServer ()
529     if (!item)
530         return NULL;
532     SPObject *server = NULL;
533     if (fill_or_stroke)
534         server = SP_OBJECT_STYLE_FILL_SERVER (item);
535     else
536         server = SP_OBJECT_STYLE_STROKE_SERVER (item);
538     return server;
541 static void
542 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
544     GrDragger *dragger = (GrDragger *) data;
545     GrDrag *drag = dragger->parent;
547     NR::Point p = *ppointer;
549     // FIXME: take from prefs
550     double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
552     if (state & GDK_SHIFT_MASK) {
553         // with Shift; unsnap if we carry more than one draggable
554         if (dragger->draggables && dragger->draggables->next) {
555             // create a new dragger
556             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
557             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
558             // relink to it all but the first draggable in the list
559             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
560                 GrDraggable *draggable = (GrDraggable *) i->data;
561                 dr_new->addDraggable (draggable);
562             }
563             dr_new->updateKnotShape();
564             g_slist_free (dragger->draggables->next);
565             dragger->draggables->next = NULL;
566             dragger->updateKnotShape();
567             dragger->updateTip();
568         }
569     } else if (!(state & GDK_CONTROL_MASK)) {
570         // without Shift or Ctrl; see if we need to snap to another dragger
571         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
572             GrDragger *d_new = (GrDragger *) di->data;
573             if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
575                 // Merge draggers:
576                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
577                     GrDraggable *draggable = (GrDraggable *) i->data;
578                     // copy draggable to d_new:
579                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
580                     d_new->addDraggable (da_new);
581                 }
583                 // unlink and delete this dragger
584                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
585                 delete dragger;
587                 // update the new merged dragger
588                 d_new->fireDraggables(true, false, true);
589                 d_new->parent->updateLines();
590                 d_new->parent->setSelected (d_new);
591                 d_new->updateKnotShape ();
592                 d_new->updateTip ();
593                 d_new->updateDependencies(true);
594                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
595                                   _("Merge gradient handles"));
596                 return;
597             }
598         }
599     }
601     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
602         // Try snapping to the grid or guides
603         SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
604         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
605         if (s.getDistance() < 1e6) {
606             p = s.getPoint();
607             sp_knot_moveto (knot, &p);
608         } else {
609             // No snapping so far, let's see if we need to snap to any of the levels
610             for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
611                 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
612                     p[NR::Y] = dragger->parent->hor_levels[i];
613                     sp_knot_moveto (knot, &p);
614                 }
615             }
616             for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
617                 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
618                     p[NR::X] = dragger->parent->vert_levels[i];
619                     sp_knot_moveto (knot, &p);
620                 }
621             }
622         }
623     }
625     if (state & GDK_CONTROL_MASK) {
626         unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
627         /* 0 means no snapping. */
629         // This list will store snap vectors from all draggables of dragger
630         GSList *snap_vectors = NULL;
632         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
633             GrDraggable *draggable = (GrDraggable *) i->data;
635             NR::Point *dr_snap = NULL;
637             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
638                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
639                     GrDragger *d_new = (GrDragger *) di->data;
640                     if (d_new == dragger)
641                         continue;
642                     if (d_new->isA (draggable->item,
643                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
644                                     draggable->point_i,
645                                     draggable->fill_or_stroke)) {
646                         // found the other end of the linear gradient;
647                         if (state & GDK_SHIFT_MASK) {
648                             // moving linear around center
649                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
650                             dr_snap = &center;
651                         } else {
652                             // moving linear around the other end
653                             dr_snap = &d_new->point;
654                         }
655                     }
656                 }
657             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
658                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
659                     GrDragger *d_new = (GrDragger *) di->data;
660                     if (d_new == dragger)
661                         continue;
662                     if (d_new->isA (draggable->item,
663                                     POINT_RG_CENTER,
664                                     draggable->point_i,
665                                     draggable->fill_or_stroke)) {
666                         // found the center of the radial gradient;
667                         dr_snap = &(d_new->point);
668                     }
669                 }
670             } else if (draggable->point_type == POINT_RG_CENTER) {
671                 // radial center snaps to hor/vert relative to its original position
672                 dr_snap = &(dragger->point_original);
673             }
675             NR::Point *snap_vector = NULL;
676             if (dr_snap) {
677                 if (state & GDK_MOD1_MASK) {
678                     // with Alt, snap to the original angle and its perpendiculars
679                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
680                 } else {
681                     // with Ctrl, snap to M_PI/snaps
682                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
683                 }
684             }
685             if (snap_vector) {
686                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
687             }
688         }
690         // Move by the smallest of snap vectors:
691         NR::Point move(9999, 9999);
692         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
693             NR::Point *snap_vector = (NR::Point *) i->data;
694             if (NR::L2(*snap_vector) < NR::L2(move))
695                 move = *snap_vector;
696         }
697         if (move[NR::X] < 9999) {
698             p += move;
699             sp_knot_moveto (knot, &p);
700         }
702         g_slist_free(snap_vectors);
703     }
705     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
706     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
708     if (drag->keep_selection) {
709         NR::Point diff = p - dragger->point;
710         drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
711     } else {
712         dragger->point = p;
713         dragger->fireDraggables (false, scale_radial);
714         dragger->updateDependencies(false);
715     }
720 static void
721 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
724     GrDrag *drag = dragger->parent;
725     // a midpoint dragger can (logically) only contain one GrDraggable
726     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
728     // get begin and end points between which dragging is allowed:
729     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
730     *moving = g_slist_append(*moving, dragger);
732     guint lowest_i = draggable->point_i;
733     guint highest_i = draggable->point_i;
734     GrDragger *lowest_dragger = dragger;
735     GrDragger *highest_dragger = dragger;
736     if (dragger->isSelected()) {
737         GrDragger* d_add;
738         while ( true )
739         {
740             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
741             if ( d_add && g_list_find(drag->selected, d_add) ) {
742                 lowest_i = lowest_i - 1;
743                 *moving = g_slist_prepend(*moving, d_add);
744                 lowest_dragger = d_add;
745             } else {
746                 break;
747             }
748         }
750         while ( true )
751         {
752             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
753             if ( d_add && g_list_find(drag->selected, d_add) ) {
754                 highest_i = highest_i + 1;
755                 *moving = g_slist_append(*moving, d_add);
756                 highest_dragger = d_add;
757             } else {
758                 break;
759             }
760         }
761     }
763     if ( SP_IS_LINEARGRADIENT(server) ) {
764         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
765         GrDragger *d_temp;
766         if (lowest_i == 1) {
767             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
768         } else {
769             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
770         }
771         if (d_temp) 
772             *begin = d_temp->point;
774         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
775         if (d_temp == NULL) {
776             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
777         }
778         if (d_temp) 
779             *end = d_temp->point;
780     } else if ( SP_IS_RADIALGRADIENT(server) ) {
781         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
782         GrDragger *d_temp;
783         if (lowest_i == 1) {
784             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
785         } else {
786             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
787         }
788         if (d_temp) 
789             *begin = d_temp->point;
791         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
792         if (d_temp == NULL) {
793             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
794         }
795         if (d_temp) 
796             *end = d_temp->point;
797     }
799     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
800     *high_lim = dragger->point - (highest_dragger->point - *end);
805 /**
806 Called when a midpoint knot is dragged.
807 */
808 static void
809 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
811     GrDragger *dragger = (GrDragger *) data;
812     GrDrag *drag = dragger->parent;
813     // a midpoint dragger can (logically) only contain one GrDraggable
814     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
816     // FIXME: take from prefs
817     double snap_fraction = 0.1;
819     NR::Point p = *ppointer;
820     NR::Point begin(0,0), end(0,0);
821     NR::Point low_lim(0,0), high_lim(0,0);
823     SPObject *server = draggable->getServer();
825     GSList *moving = NULL;
826     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
828     if (state & GDK_CONTROL_MASK) {
829         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
830     } else {
831         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
832     }
833     NR::Point displacement = p - dragger->point;
835     for (GSList const* i = moving; i != NULL; i = i->next) {
836         GrDragger *drg = (GrDragger*) i->data;
837         SPKnot *drgknot = drg->knot;
838         NR::Point this_move = displacement;
839         if (state & GDK_MOD1_MASK) {
840             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
841             double alpha = 1.5;
842             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
843                 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
844                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
845             } else { // drg is on the begin side from dragger
846                 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
847                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
848             }
849         }
850         drg->point += this_move;
851         sp_knot_moveto (drgknot, & drg->point);
852         drg->fireDraggables (false);
853         drg->updateDependencies(false);
854     }
856     g_slist_free(moving);
858     drag->keep_selection = dragger->isSelected();
863 static void
864 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
866     GrDragger *dragger = (GrDragger *) data;
868     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
871 /**
872 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
873 */
874 static void
875 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
877     GrDragger *dragger = (GrDragger *) data;
879     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
881     dragger->point_original = dragger->point = knot->pos;
883     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
884         dragger->fireDraggables (true, true);
885     } else {
886         dragger->fireDraggables (true);
887     }
889     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
890         GrDragger *d = (GrDragger *) i->data;
891         if (d == dragger)
892             continue;
893         d->fireDraggables (true);
894     }
896     // make this dragger selected
897     if (!dragger->parent->keep_selection) {
898         dragger->parent->setSelected (dragger);
899     }
900     dragger->parent->keep_selection = false;
902     dragger->updateDependencies(true);
904     // we did an undoable action
905     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
906                       _("Move gradient handle"));
909 /**
910 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
911 state of the keyboard keys
912 */
913 static void
914 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
916     GrDragger *dragger = (GrDragger *) data;
917     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
918     if (!draggable) return;
920     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
921     // delete this knot from vector
922         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
923         gradient = sp_gradient_get_vector (gradient, false);
924         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
925                 SPStop *stop = NULL;
926                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
927                 case POINT_LG_BEGIN:
928                 case POINT_RG_CENTER:
929                     stop = sp_first_stop(gradient);
930                         {
931                             SPStop *next = sp_next_stop (stop);
932                                 if (next) {
933                                         next->offset = 0;
934                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
935                                 }
936                         }
937                     break;
938                 case POINT_LG_END:
939                 case POINT_RG_R1:
940                 case POINT_RG_R2:
941                     stop = sp_last_stop(gradient);
942                     {
943                             SPStop *prev = sp_prev_stop (stop, gradient);
944                             if (prev) {
945                                     prev->offset = 1;
946                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
947                             }
948                         }
949                     break;
950                 case POINT_LG_MID:
951                 case POINT_RG_MID1:
952                 case POINT_RG_MID2:
953                     stop = sp_get_stop_i(gradient, draggable->point_i);
954                     break;
955                 }
957                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
958                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
959                                   _("Delete gradient stop"));
960         }
961     } else {
962     // select the dragger
963         dragger->point_original = dragger->point;
965         if ( state & GDK_SHIFT_MASK ) {
966             dragger->parent->setSelected (dragger, true, false);
967         } else {
968             dragger->parent->setSelected (dragger);
969         }
970     }
973 /**
974 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
975 */
976 static void
977 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
979     GrDragger *dragger = (GrDragger *) data;
981     dragger->point_original = dragger->point;
983     if (dragger->draggables == NULL)
984         return;
986     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
987     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
990 /**
991 Act upon all draggables of the dragger, setting them to the dragger's point
992 */
993 void
994 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
996     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
997         GrDraggable *draggable = (GrDraggable *) i->data;
999         // set local_change flag so that selection_changed callback does not regenerate draggers
1000         this->parent->local_change = true;
1002         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1003         // to the center, unless it's the first update upon merge when we must snap it to the point
1004         if (merging_focus ||
1005             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1006         {
1007             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1008         }
1009     }
1012 /**
1013 Checks if the dragger has a draggable with this point_type
1014  */
1015 bool
1016 GrDragger::isA (gint point_type)
1018     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1019         GrDraggable *draggable = (GrDraggable *) i->data;
1020         if (draggable->point_type == point_type) {
1021             return true;
1022         }
1023     }
1024     return false;
1027 /**
1028 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1029  */
1030 bool
1031 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1033     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1034         GrDraggable *draggable = (GrDraggable *) i->data;
1035         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1036             return true;
1037         }
1038     }
1039     return false;
1042 bool
1043 GrDraggable::mayMerge (GrDraggable *da2)
1045     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1046         // we must not merge the points of the same gradient!
1047         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1048               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1049             // except that we can snap center and focus together
1050             return false;
1051         }
1052     }
1053     // disable merging of midpoints.
1054     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1055          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1056          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1057         return false;
1059     return true;
1062 bool
1063 GrDragger::mayMerge (GrDragger *other)
1065     if (this == other)
1066         return false;
1068     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1069         GrDraggable *da1 = (GrDraggable *) i->data;
1070         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1071             GrDraggable *da2 = (GrDraggable *) j->data;
1072             if (!da1->mayMerge(da2))
1073                 return false;
1074         }
1075     }
1076     return true;
1079 bool
1080 GrDragger::mayMerge (GrDraggable *da2)
1082     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1083         GrDraggable *da1 = (GrDraggable *) i->data;
1084         if (!da1->mayMerge(da2))
1085             return false;
1086     }
1087     return true;
1090 /**
1091 Updates the statusbar tip of the dragger knot, based on its draggables
1092  */
1093 void
1094 GrDragger::updateTip ()
1096         if (this->knot && this->knot->tip) {
1097                 g_free (this->knot->tip);
1098                 this->knot->tip = NULL;
1099         }
1101     if (g_slist_length (this->draggables) == 1) {
1102         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1103         char *item_desc = sp_item_description(draggable->item);
1104         switch (draggable->point_type) {
1105             case POINT_LG_MID:
1106             case POINT_RG_MID1:
1107             case POINT_RG_MID2:
1108                 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"),
1109                                                    _(gr_knot_descr[draggable->point_type]),
1110                                                    draggable->point_i,
1111                                                    item_desc,
1112                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1113                 break;
1115             default:
1116                 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"),
1117                                                    _(gr_knot_descr[draggable->point_type]),
1118                                                    item_desc,
1119                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1120                 break;
1121         }
1122         g_free(item_desc);
1123     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1124         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1125     } else {
1126         int length = g_slist_length (this->draggables);
1127         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1128                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1129                                                     length),
1130                                            length);
1131     }
1134 /**
1135 Adds a draggable to the dragger
1136  */
1137 void
1138 GrDragger::updateKnotShape ()
1140     if (!draggables)
1141         return;
1142     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1143     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1146 /**
1147 Adds a draggable to the dragger
1148  */
1149 void
1150 GrDragger::addDraggable (GrDraggable *draggable)
1152     this->draggables = g_slist_prepend (this->draggables, draggable);
1154     this->updateTip();
1158 /**
1159 Moves this dragger to the point of the given draggable, acting upon all other draggables
1160  */
1161 void
1162 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1164     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1165     this->point_original = this->point;
1167     sp_knot_moveto (this->knot, &(this->point));
1169     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1170         GrDraggable *da = (GrDraggable *) i->data;
1171         if ( (da->item == item) && 
1172              (point_type == -1 || da->point_type == point_type) &&
1173              (point_i == -1 || da->point_i == point_i) &&
1174              (da->fill_or_stroke == fill_or_stroke) ) {
1175             continue;
1176         }
1177         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1178     }
1179     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1183 /**
1184 Moves all midstop draggables that depend on this one
1185  */
1186 void
1187 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1188     SPObject *server = draggable->getServer();
1189     if (!server) 
1190         return;
1191     guint num = SP_GRADIENT(server)->vector.stops.size();
1192     if (num <= 2) return;
1194     if ( SP_IS_LINEARGRADIENT(server) ) {
1195         for ( guint i = 1; i < num - 1; i++ ) {
1196             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1197         }
1198     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1199         for ( guint i = 1; i < num - 1; i++ ) {
1200             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1201             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1202         }
1203     }
1207 /**
1208 Moves all draggables that depend on this one
1209  */
1210 void
1211 GrDragger::updateDependencies (bool write_repr)
1213     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1214         GrDraggable *draggable = (GrDraggable *) i->data;
1215         switch (draggable->point_type) {
1216             case POINT_LG_BEGIN:
1217                 {
1218                     // the end point is dependent only when dragging with ctrl+shift
1219                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1221                     this->updateMidstopDependencies (draggable, write_repr);
1222                 }
1223                 break;
1224             case POINT_LG_END:
1225                 {
1226                     // the begin point is dependent only when dragging with ctrl+shift
1227                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1229                     this->updateMidstopDependencies (draggable, write_repr);
1230                 }
1231                 break;
1232             case POINT_LG_MID:
1233                 // no other nodes depend on mid points.
1234                 break;
1235             case POINT_RG_R2:
1236                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1237                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1238                 this->updateMidstopDependencies (draggable, write_repr);
1239                 break;
1240             case POINT_RG_R1:
1241                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1242                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1243                 this->updateMidstopDependencies (draggable, write_repr);
1244                 break;
1245             case POINT_RG_CENTER:
1246                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
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_FOCUS:
1252                 // nothing can depend on that
1253                 break;
1254             case POINT_RG_MID1:
1255                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1256                 break;
1257             case POINT_RG_MID2:
1258                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1259                 break;
1260             default:
1261                 break;
1262         }
1263     }
1268 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1270     this->draggables = NULL;
1272     this->parent = parent;
1274     this->point = p;
1275     this->point_original = p;
1277     // create the knot
1278     this->knot = sp_knot_new (parent->desktop, NULL);
1279     this->knot->setMode(SP_KNOT_MODE_XOR);
1280     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1281     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1282     sp_knot_update_ctrl(this->knot);
1284     // move knot to the given point
1285     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1286     sp_knot_show (this->knot);
1288     // connect knot's signals
1289     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1290                        // luckily, midstops never snap to other nodes so are never unsnapped...
1291          && ( (draggable->point_type == POINT_LG_MID)
1292               || (draggable->point_type == POINT_RG_MID1)
1293               || (draggable->point_type == POINT_RG_MID2) ) )
1294     {
1295         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1296     } else {
1297         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1298     }
1299     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1300     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1301     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1302     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1304     // add the initial draggable
1305     if (draggable)
1306         this->addDraggable (draggable);
1307     updateKnotShape();
1310 GrDragger::~GrDragger ()
1312     // unselect if it was selected
1313     this->parent->setDeselected(this);
1315     // disconnect signals
1316     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1317     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1318     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1319     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1320     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1322     /* unref should call destroy */
1323     g_object_unref (G_OBJECT (this->knot));
1325     // delete all draggables
1326     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1327         delete ((GrDraggable *) i->data);
1328     }
1329     g_slist_free (this->draggables);
1330     this->draggables = NULL;
1333 /**
1334 Select the dragger which has the given draggable.
1335 */
1336 GrDragger *
1337 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1339     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1340         GrDragger *dragger = (GrDragger *) i->data;
1341         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1342             GrDraggable *da2 = (GrDraggable *) j->data;
1343             if ( (da2->item == item) && 
1344                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1345                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1346                  (da2->fill_or_stroke == fill_or_stroke)) {
1347                 return (dragger);
1348             }
1349         }
1350     }
1351     return NULL;
1355 void
1356 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1358     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1359     if (d && d !=  this) {
1360         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1361     }
1365 /**
1366   Draw this dragger as selected
1367 */
1368 void
1369 GrDragger::select()
1371     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1372     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1375 /**
1376   Draw this dragger as normal (deselected)
1377 */
1378 void
1379 GrDragger::deselect()
1381     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1382     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1385 bool
1386 GrDragger::isSelected()
1388     return g_list_find (parent->selected, this);
1391 /**
1392 \brief Deselect all stops/draggers (private)
1393 */
1394 void
1395 GrDrag::deselect_all()
1397     while (selected) {
1398         ( (GrDragger*) selected->data)->deselect();
1399         selected = g_list_remove(selected, selected->data);
1400     }
1403 /**
1404 \brief Deselect all stops/draggers (public; emits signal)
1405 */
1406 void
1407 GrDrag::deselectAll()
1409     deselect_all();
1410     this->desktop->emitToolSubselectionChanged(NULL);
1413 /**
1414 \brief Select all stops/draggers
1415 */
1416 void
1417 GrDrag::selectAll()
1419     for (GList *l = this->draggers; l != NULL; l = l->next) {
1420         GrDragger *d = ((GrDragger *) l->data);
1421         setSelected (d, true, true);
1422     }
1425 /**
1426 \brief Select all stops/draggers that match the coords
1427 */
1428 void 
1429 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1431     for (GList *l = this->draggers; l != NULL; l = l->next) {
1432         GrDragger *d = ((GrDragger *) l->data);
1433         for (guint k = 0; k < coords.size(); k++) {
1434             if (NR::L2 (d->point - coords[k]) < 1e-4) {
1435                 setSelected (d, true, true);
1436             }
1437         }
1438     }
1442 /**
1443 \brief Select all stops/draggers that fall within the rect
1444 */
1445 void 
1446 GrDrag::selectRect(NR::Rect const &r)
1448     for (GList *l = this->draggers; l != NULL; l = l->next) {
1449         GrDragger *d = ((GrDragger *) l->data);
1450         if (r.contains(d->point)) {
1451            setSelected (d, true, true);
1452         }
1453     }
1456 /**
1457 \brief Select a dragger
1458 \param dragger       The dragger to select
1459 \param add_to_selection   If true, add to selection, otherwise deselect others
1460 \param override      If true, always select this node, otherwise toggle selected status
1461 */
1462 void
1463 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1465     GrDragger *seldragger = NULL;
1467     if (add_to_selection) {
1468         if (!dragger) return;
1469         if (override) {
1470             if (!g_list_find(selected, dragger)) {
1471                 selected = g_list_prepend(selected, dragger);
1472             }
1473             dragger->select();
1474             seldragger = dragger;
1475         } else { // toggle
1476             if (g_list_find(selected, dragger)) {
1477                 selected = g_list_remove(selected, dragger);
1478                 dragger->deselect();
1479                 if (selected) {
1480                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1481                 }
1482             } else {
1483                 selected = g_list_prepend(selected, dragger);
1484                 dragger->select();
1485                 seldragger = dragger;
1486             }
1487         }
1488     } else {
1489         deselect_all();
1490         if (dragger) {
1491             selected = g_list_prepend(selected, dragger);
1492             dragger->select();
1493             seldragger = dragger;
1494         }
1495     }
1496     if (seldragger) {
1497         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1498     }
1501 /**
1502 \brief Deselect a dragger
1503 \param dragger       The dragger to deselect
1504 */
1505 void
1506 GrDrag::setDeselected (GrDragger *dragger)
1508     if (g_list_find(selected, dragger)) {
1509         selected = g_list_remove(selected, dragger);
1510         dragger->deselect();
1511     }
1512     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1517 /**
1518 Create a line from p1 to p2 and add it to the lines list
1519  */
1520 void
1521 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1523     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1524                                                             SP_TYPE_CTRLLINE, NULL);
1525     SP_CTRLLINE(line)->item = item;
1526     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1527     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1528         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1529     sp_canvas_item_show (line);
1530     this->lines = g_slist_append (this->lines, line);
1533 /**
1534 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1535 new dragger and add it to draggers list
1536  */
1537 void
1538 GrDrag::addDragger (GrDraggable *draggable)
1540     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1542     for (GList *i = this->draggers; i != NULL; i = i->next) {
1543         GrDragger *dragger = (GrDragger *) i->data;
1544         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1545             // distance is small, merge this draggable into dragger, no need to create new dragger
1546             dragger->addDraggable (draggable);
1547             dragger->updateKnotShape();
1548             return;
1549         }
1550     }
1552     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1553     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1554     this->draggers = g_list_append (this->draggers, new_dragger);
1557 /**
1558 Add draggers for the radial gradient rg on item
1559 */
1560 void
1561 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1563     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1564     guint num = rg->vector.stops.size();
1565     if (num > 2) {
1566         for ( guint i = 1; i < num - 1; i++ ) {
1567             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1568         }
1569     }
1570     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1571     if (num > 2) {
1572         for ( guint i = 1; i < num - 1; i++ ) {
1573             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1574         }
1575     }
1576     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1577     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1580 /**
1581 Add draggers for the linear gradient lg on item
1582 */
1583 void
1584 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1586     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1587     guint num = lg->vector.stops.size();
1588     if (num > 2) {
1589         for ( guint i = 1; i < num - 1; i++ ) {
1590             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1591         }
1592     }
1593     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1596 /**
1597 Artificially grab the knot of this dragger; used by the gradient context
1598 */
1599 void
1600 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1602     if (dragger) {
1603         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1604     }
1607 /**
1608 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1609 */
1610 void
1611 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1613     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1614     if (dragger) {
1615         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1616     }
1619 /**
1620 Regenerates the draggers list from the current selection; is called when selection is changed or
1621 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1622 */
1623 void
1624 GrDrag::updateDraggers ()
1626     while (selected) {
1627         selected = g_list_remove(selected, selected->data);
1628     }
1629     // delete old draggers
1630     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1631         delete ((GrDragger *) i->data);
1632     }
1633     g_list_free (this->draggers);
1634     this->draggers = NULL;
1636     g_return_if_fail (this->selection != NULL);
1638     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1640         SPItem *item = SP_ITEM(i->data);
1641         SPStyle *style = SP_OBJECT_STYLE (item);
1643         if (style && (style->fill.isPaintserver())) {
1644             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1645             if (SP_IS_LINEARGRADIENT (server)) {
1646                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1647             } else if (SP_IS_RADIALGRADIENT (server)) {
1648                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1649             }
1650         }
1652         if (style && (style->stroke.isPaintserver())) {
1653             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1654             if (SP_IS_LINEARGRADIENT (server)) {
1655                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1656             } else if (SP_IS_RADIALGRADIENT (server)) {
1657                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1658             }
1659         }
1660     }
1663 /**
1664 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1665 lines are always in sync with the actual gradient
1666 */
1667 void
1668 GrDrag::updateLines ()
1670     // delete old lines
1671     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1672         gtk_object_destroy( GTK_OBJECT (i->data));
1673     }
1674     g_slist_free (this->lines);
1675     this->lines = NULL;
1677     g_return_if_fail (this->selection != NULL);
1679     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1681         SPItem *item = SP_ITEM(i->data);
1683         SPStyle *style = SP_OBJECT_STYLE (item);
1685         if (style && (style->fill.isPaintserver())) {
1686             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1687             if (SP_IS_LINEARGRADIENT (server)) {
1688                 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);
1689             } else if (SP_IS_RADIALGRADIENT (server)) {
1690                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1691                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1692                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1693             }
1694         }
1696         if (style && (style->stroke.isPaintserver())) {
1697             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1698             if (SP_IS_LINEARGRADIENT (server)) {
1699                 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);
1700             } else if (SP_IS_RADIALGRADIENT (server)) {
1701                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1702                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1703                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1704             }
1705         }
1706     }
1709 /**
1710 Regenerates the levels list from the current selection
1711 */
1712 void
1713 GrDrag::updateLevels ()
1715     hor_levels.clear();
1716     vert_levels.clear();
1718     g_return_if_fail (this->selection != NULL);
1720     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1721         SPItem *item = SP_ITEM(i->data);
1722         NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1723         if (rect) {
1724             // Remember the edges of the bbox and the center axis
1725             hor_levels.push_back(rect->min()[NR::Y]);
1726             hor_levels.push_back(rect->max()[NR::Y]);
1727             hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1728             vert_levels.push_back(rect->min()[NR::X]);
1729             vert_levels.push_back(rect->max()[NR::X]);
1730             vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1731         }
1732     }
1735 void
1736 GrDrag::selected_reverse_vector ()
1738     if (selected == NULL)
1739         return;
1741     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1742         GrDraggable *draggable = (GrDraggable *) i->data;
1744         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1745     }
1748 void
1749 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1751     selected_move (x, y, false, scale_radial);
1754 void
1755 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1757     if (selected == NULL)
1758         return;
1760     bool did = false; 
1762     for (GList *i = selected; i != NULL; i = i->next) {
1763         GrDragger *d = (GrDragger *) i->data;
1765         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1766             // if this is an endpoint,
1768             // Moving an rg center moves its focus and radii as well.
1769             // therefore, if this is a focus or radius and if selection
1770             // contains the center as well, do not move this one
1771             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) || 
1772                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1773                 bool skip_radius_with_center = false;
1774                 for (GList *di = selected; di != NULL; di = di->next) {
1775                     GrDragger *d_new = (GrDragger *) di->data;
1776                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1777                                     POINT_RG_CENTER,
1778                                     0,
1779                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1780                         // FIXME: here we take into account only the first draggable!
1781                         skip_radius_with_center = true;
1782                     }
1783                 }
1784                 if (skip_radius_with_center)
1785                     continue;
1786             }
1788             did = true;
1789             d->point += NR::Point (x, y);
1790             d->point_original = d->point;
1791             sp_knot_moveto (d->knot, &(d->point));
1793             d->fireDraggables (write_repr, scale_radial);
1795             d->updateDependencies(write_repr);
1796         }
1797     }
1799     if (write_repr && did) {
1800         // we did an undoable action
1801         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1802                                 _("Move gradient handle(s)"));
1803         return;
1804     }
1806     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1808         GrDragger *dragger = (GrDragger *) selected->data;
1809         // a midpoint dragger can (logically) only contain one GrDraggable
1810         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1812         NR::Point begin(0,0), end(0,0);
1813         NR::Point low_lim(0,0), high_lim(0,0);
1815         SPObject *server = draggable->getServer();
1816         GSList *moving = NULL;
1817         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1819         NR::Point p(x, y);
1820         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1821         NR::Point displacement = p - dragger->point;
1823         for (GSList const* i = moving; i != NULL; i = i->next) {
1824             GrDragger *drg = (GrDragger*) i->data;
1825             SPKnot *drgknot = drg->knot;
1826             drg->point += displacement;
1827             sp_knot_moveto (drgknot, & drg->point);
1828             drg->fireDraggables (true);
1829             drg->updateDependencies(true);
1830             did = true;
1831         }
1833         g_slist_free(moving);
1835         if (write_repr && did) {
1836             // we did an undoable action
1837             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1838                                     _("Move gradient mid stop(s)"));
1839         }
1840     }
1843 void
1844 GrDrag::selected_move_screen (double x, double y)
1846     gdouble zoom = desktop->current_zoom();
1847     gdouble zx = x / zoom;
1848     gdouble zy = y / zoom;
1850     selected_move (zx, zy);
1853 /**
1854 Select the knot next to the last selected one and deselect all other selected.
1855 */
1856 GrDragger *
1857 GrDrag::select_next ()
1859     GrDragger *d = NULL;
1860     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1861         if (draggers) 
1862             d = (GrDragger *) draggers->data;
1863     } else {
1864         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1865     }
1866     if (d)
1867         setSelected (d);
1868     return d;
1871 /**
1872 Select the knot previous from the last selected one and deselect all other selected.
1873 */
1874 GrDragger *
1875 GrDrag::select_prev ()
1877     GrDragger *d = NULL;
1878     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1879         if (draggers)
1880             d = (GrDragger *) g_list_last (draggers)->data;
1881     } else {
1882         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1883     }
1884     if (d)
1885         setSelected (d);
1886     return d;
1890 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1891 void
1892 GrDrag::deleteSelected (bool just_one)
1894     if (!selected) return;
1896     SPDocument *document = false;
1898     struct StructStopInfo {
1899         SPStop * spstop;
1900         GrDraggable * draggable;
1901         SPGradient * gradient;
1902         SPGradient * vector;
1903     };
1905     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1906     GSList *endstoplist = NULL;  // list of stops that must be deleted
1907     while (selected) {
1908         GrDragger *dragger = (GrDragger*) selected->data;
1909         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1910             GrDraggable *draggable = (GrDraggable*) drgble->data;
1911             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1912             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1914             switch (draggable->point_type) {
1915                 case POINT_LG_MID:
1916                 case POINT_RG_MID1:
1917                 case POINT_RG_MID2:
1918                     {
1919                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1920                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1921                         bool present = false;
1922                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1923                             if ( (SPStop*)l->data == stop ) {
1924                                 present = true;
1925                                 break; // no need to search further.
1926                             }
1927                         }
1928                         if (!present)
1929                             midstoplist = g_slist_append(midstoplist, stop);
1930                     }
1931                     break;
1932                 case POINT_LG_BEGIN:
1933                 case POINT_LG_END:
1934                 case POINT_RG_CENTER:
1935                 case POINT_RG_R1:
1936                 case POINT_RG_R2:
1937                     {
1938                         SPStop *stop = NULL;
1939                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1940                             stop = sp_first_stop(vector);
1941                         } else {
1942                             stop = sp_last_stop(vector);
1943                         }
1944                         if (stop) {
1945                             StructStopInfo *stopinfo = new StructStopInfo;
1946                             stopinfo->spstop = stop;
1947                             stopinfo->draggable = draggable;
1948                             stopinfo->gradient = gradient;
1949                             stopinfo->vector = vector;
1950                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1951                             bool present = false;
1952                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1953                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1954                                     present = true;
1955                                     break; // no need to search further.
1956                                 }
1957                             }
1958                             if (!present)
1959                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1960                         }
1961                     }
1962                     break;
1963                 default:
1964                     break;
1965             }
1966         }
1967         selected = g_list_remove(selected, dragger);
1968         if ( just_one ) break; // iterate once if just_one is set.
1969     }
1970     while (midstoplist) {
1971         SPStop *stop = (SPStop*) midstoplist->data;
1972         document = SP_OBJECT_DOCUMENT (stop);
1973         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1974         parent->removeChild(SP_OBJECT_REPR(stop));
1975         midstoplist = g_slist_remove(midstoplist, stop);
1976     }
1977     while (endstoplist) {
1978         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1979         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1981         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1982         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1983         // manually count the children, don't know if there already exists a function for this...
1984         int len = 0;
1985         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1986               child != NULL ;
1987               child = SP_OBJECT_NEXT(child) )
1988         {
1989             if ( SP_IS_STOP(child) )  len ++;
1990         }
1991         if (len > 2)
1992         {
1993             switch (stopinfo->draggable->point_type) {
1994                 case POINT_LG_BEGIN:
1995                     {
1996                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1998                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1999                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
2000                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
2001                         SPStop *stop = sp_first_stop(stopinfo->vector);
2002                         gdouble offset = stop->offset;
2003                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
2004                         lg->x1.computed = newbegin[NR::X];
2005                         lg->y1.computed = newbegin[NR::Y];
2007                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2008                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2009                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2010                         stop->offset = 0;
2011                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2013                         // iterate through midstops to set new offset values such that they won't move on canvas.
2014                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2015                         stop = sp_next_stop(stop);
2016                         while ( stop != laststop ) {
2017                             stop->offset = (stop->offset - offset)/(1 - offset);
2018                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2019                             stop = sp_next_stop(stop);
2020                         }
2021                     }
2022                     break;
2023                 case POINT_LG_END:
2024                     {
2025                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2027                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2028                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
2029                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
2030                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2031                         gdouble offset = laststop->offset;
2032                         NR::Point newend = begin + offset * (oldend - begin);
2033                         lg->x2.computed = newend[NR::X];
2034                         lg->y2.computed = newend[NR::Y];
2036                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2037                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2038                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2039                         laststop->offset = 1;
2040                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2042                         // iterate through midstops to set new offset values such that they won't move on canvas.
2043                         SPStop *stop = sp_first_stop(stopinfo->vector);
2044                         stop = sp_next_stop(stop);
2045                         while ( stop != laststop ) {
2046                             stop->offset = stop->offset / offset;
2047                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2048                             stop = sp_next_stop(stop);
2049                         }
2050                     }
2051                     break;
2052                 case POINT_RG_CENTER:
2053                     {
2054                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2055                         if (newfirst) {
2056                             newfirst->offset = 0;
2057                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2058                         }
2059                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2060                     }
2061                     break;
2062                 case POINT_RG_R1:
2063                 case POINT_RG_R2:
2064                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2066                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2067                         double oldradius = rg->r.computed;
2068                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2069                         gdouble offset = laststop->offset;
2070                         double newradius = offset * oldradius;
2071                         rg->r.computed = newradius;
2073                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2074                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2075                         laststop->offset = 1;
2076                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2078                         // iterate through midstops to set new offset values such that they won't move on canvas.
2079                         SPStop *stop = sp_first_stop(stopinfo->vector);
2080                         stop = sp_next_stop(stop);
2081                         while ( stop != laststop ) {
2082                             stop->offset = stop->offset / offset;
2083                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2084                             stop = sp_next_stop(stop);
2085                         }
2086                         break;
2087             }
2088         }
2089         else
2090         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2091             SPCSSAttr *css = sp_repr_css_attr_new ();
2093             // stopinfo->spstop is the selected stop
2094             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2095             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2096                 unselectedrepr = unselectedrepr->next();
2097             }
2099             if (unselectedrepr == NULL) {
2100                 if (stopinfo->draggable->fill_or_stroke) {
2101                     sp_repr_css_unset_property (css, "fill");
2102                 } else {
2103                     sp_repr_css_unset_property (css, "stroke");
2104                 }
2105             } else {
2106                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2107                 if (stopinfo->draggable->fill_or_stroke) {
2108                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2109                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2110                 } else {
2111                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2112                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2113                 }
2114                 sp_repr_css_attr_unref (stopcss);
2115             }
2116             
2117             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2118             sp_repr_css_attr_unref (css);
2119         }
2121         endstoplist = g_slist_remove(endstoplist, stopinfo);
2122         delete stopinfo;
2123     }
2125     if (document) {
2126         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2127     }
2131 /*
2132   Local Variables:
2133   mode:c++
2134   c-file-style:"stroustrup"
2135   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2136   indent-tabs-mode:nil
2137   fill-column:99
2138   End:
2139 */
2140 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :