Code

fix bug [ 1824359 ] Gradient isn't snapping with ctrl pressed
[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->fill_or_stroke)) {
645                         // found the other end of the linear gradient;
646                         if (state & GDK_SHIFT_MASK) {
647                             // moving linear around center
648                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
649                             dr_snap = &center;
650                         } else {
651                             // moving linear around the other end
652                             dr_snap = &d_new->point;
653                         }
654                     }
655                 }
656             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
657                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
658                     GrDragger *d_new = (GrDragger *) di->data;
659                     if (d_new == dragger)
660                         continue;
661                     if (d_new->isA (draggable->item,
662                                     POINT_RG_CENTER,
663                                     draggable->fill_or_stroke)) {
664                         // found the center of the radial gradient;
665                         dr_snap = &(d_new->point);
666                     }
667                 }
668             } else if (draggable->point_type == POINT_RG_CENTER) {
669                 // radial center snaps to hor/vert relative to its original position
670                 dr_snap = &(dragger->point_original);
671             }
673             NR::Point *snap_vector = NULL;
674             if (dr_snap) {
675                 if (state & GDK_MOD1_MASK) {
676                     // with Alt, snap to the original angle and its perpendiculars
677                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
678                 } else {
679                     // with Ctrl, snap to M_PI/snaps
680                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
681                 }
682             }
683             if (snap_vector) {
684                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
685             }
686         }
688         // Move by the smallest of snap vectors:
689         NR::Point move(9999, 9999);
690         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
691             NR::Point *snap_vector = (NR::Point *) i->data;
692             if (NR::L2(*snap_vector) < NR::L2(move))
693                 move = *snap_vector;
694         }
695         if (move[NR::X] < 9999) {
696             p += move;
697             sp_knot_moveto (knot, &p);
698         }
700         g_slist_free(snap_vectors);
701     }
703     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
704     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
706     if (drag->keep_selection) {
707         NR::Point diff = p - dragger->point;
708         drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
709     } else {
710         dragger->point = p;
711         dragger->fireDraggables (false, scale_radial);
712         dragger->updateDependencies(false);
713     }
718 static void
719 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
722     GrDrag *drag = dragger->parent;
723     // a midpoint dragger can (logically) only contain one GrDraggable
724     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
726     // get begin and end points between which dragging is allowed:
727     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
728     *moving = g_slist_append(*moving, dragger);
730     guint lowest_i = draggable->point_i;
731     guint highest_i = draggable->point_i;
732     GrDragger *lowest_dragger = dragger;
733     GrDragger *highest_dragger = dragger;
734     if (dragger->isSelected()) {
735         GrDragger* d_add;
736         while ( true )
737         {
738             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
739             if ( d_add && g_list_find(drag->selected, d_add) ) {
740                 lowest_i = lowest_i - 1;
741                 *moving = g_slist_prepend(*moving, d_add);
742                 lowest_dragger = d_add;
743             } else {
744                 break;
745             }
746         }
748         while ( true )
749         {
750             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
751             if ( d_add && g_list_find(drag->selected, d_add) ) {
752                 highest_i = highest_i + 1;
753                 *moving = g_slist_append(*moving, d_add);
754                 highest_dragger = d_add;
755             } else {
756                 break;
757             }
758         }
759     }
761     if ( SP_IS_LINEARGRADIENT(server) ) {
762         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
763         GrDragger *d_temp;
764         if (lowest_i == 1) {
765             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
766         } else {
767             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
768         }
769         if (d_temp) 
770             *begin = d_temp->point;
772         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
773         if (d_temp == NULL) {
774             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
775         }
776         if (d_temp) 
777             *end = d_temp->point;
778     } else if ( SP_IS_RADIALGRADIENT(server) ) {
779         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
780         GrDragger *d_temp;
781         if (lowest_i == 1) {
782             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
783         } else {
784             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
785         }
786         if (d_temp) 
787             *begin = d_temp->point;
789         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
790         if (d_temp == NULL) {
791             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
792         }
793         if (d_temp) 
794             *end = d_temp->point;
795     }
797     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
798     *high_lim = dragger->point - (highest_dragger->point - *end);
803 /**
804 Called when a midpoint knot is dragged.
805 */
806 static void
807 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
809     GrDragger *dragger = (GrDragger *) data;
810     GrDrag *drag = dragger->parent;
811     // a midpoint dragger can (logically) only contain one GrDraggable
812     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
814     // FIXME: take from prefs
815     double snap_fraction = 0.1;
817     NR::Point p = *ppointer;
818     NR::Point begin(0,0), end(0,0);
819     NR::Point low_lim(0,0), high_lim(0,0);
821     SPObject *server = draggable->getServer();
823     GSList *moving = NULL;
824     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
826     if (state & GDK_CONTROL_MASK) {
827         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
828     } else {
829         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
830     }
831     NR::Point displacement = p - dragger->point;
833     for (GSList const* i = moving; i != NULL; i = i->next) {
834         GrDragger *drg = (GrDragger*) i->data;
835         SPKnot *drgknot = drg->knot;
836         NR::Point this_move = displacement;
837         if (state & GDK_MOD1_MASK) {
838             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
839             double alpha = 1.5;
840             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
841                 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
842                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
843             } else { // drg is on the begin side from dragger
844                 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
845                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
846             }
847         }
848         drg->point += this_move;
849         sp_knot_moveto (drgknot, & drg->point);
850         drg->fireDraggables (false);
851         drg->updateDependencies(false);
852     }
854     g_slist_free(moving);
856     drag->keep_selection = dragger->isSelected();
861 static void
862 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
864     GrDragger *dragger = (GrDragger *) data;
866     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
869 /**
870 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
871 */
872 static void
873 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
875     GrDragger *dragger = (GrDragger *) data;
877     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
879     dragger->point_original = dragger->point = knot->pos;
881     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
882         dragger->fireDraggables (true, true);
883     } else {
884         dragger->fireDraggables (true);
885     }
887     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
888         GrDragger *d = (GrDragger *) i->data;
889         if (d == dragger)
890             continue;
891         d->fireDraggables (true);
892     }
894     // make this dragger selected
895     if (!dragger->parent->keep_selection) {
896         dragger->parent->setSelected (dragger);
897     }
898     dragger->parent->keep_selection = false;
900     dragger->updateDependencies(true);
902     // we did an undoable action
903     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
904                       _("Move gradient handle"));
907 /**
908 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
909 state of the keyboard keys
910 */
911 static void
912 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
914     GrDragger *dragger = (GrDragger *) data;
915     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
916     if (!draggable) return;
918     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
919     // delete this knot from vector
920         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
921         gradient = sp_gradient_get_vector (gradient, false);
922         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
923                 SPStop *stop = NULL;
924                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
925                 case POINT_LG_BEGIN:
926                 case POINT_RG_CENTER:
927                     stop = sp_first_stop(gradient);
928                         {
929                             SPStop *next = sp_next_stop (stop);
930                                 if (next) {
931                                         next->offset = 0;
932                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
933                                 }
934                         }
935                     break;
936                 case POINT_LG_END:
937                 case POINT_RG_R1:
938                 case POINT_RG_R2:
939                     stop = sp_last_stop(gradient);
940                     {
941                             SPStop *prev = sp_prev_stop (stop, gradient);
942                             if (prev) {
943                                     prev->offset = 1;
944                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
945                             }
946                         }
947                     break;
948                 case POINT_LG_MID:
949                 case POINT_RG_MID1:
950                 case POINT_RG_MID2:
951                     stop = sp_get_stop_i(gradient, draggable->point_i);
952                     break;
953                 }
955                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
956                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
957                                   _("Delete gradient stop"));
958         }
959     } else {
960     // select the dragger
961         dragger->point_original = dragger->point;
963         if ( state & GDK_SHIFT_MASK ) {
964             dragger->parent->setSelected (dragger, true, false);
965         } else {
966             dragger->parent->setSelected (dragger);
967         }
968     }
971 /**
972 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
973 */
974 static void
975 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
977     GrDragger *dragger = (GrDragger *) data;
979     dragger->point_original = dragger->point;
981     if (dragger->draggables == NULL)
982         return;
984     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
985     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
988 /**
989 Act upon all draggables of the dragger, setting them to the dragger's point
990 */
991 void
992 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
994     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
995         GrDraggable *draggable = (GrDraggable *) i->data;
997         // set local_change flag so that selection_changed callback does not regenerate draggers
998         this->parent->local_change = true;
1000         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1001         // to the center, unless it's the first update upon merge when we must snap it to the point
1002         if (merging_focus ||
1003             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1004         {
1005             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1006         }
1007     }
1010 /**
1011 Checks if the dragger has a draggable with this point_type
1012  */
1013 bool
1014 GrDragger::isA (gint point_type)
1016     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1017         GrDraggable *draggable = (GrDraggable *) i->data;
1018         if (draggable->point_type == point_type) {
1019             return true;
1020         }
1021     }
1022     return false;
1025 /**
1026 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1027  */
1028 bool
1029 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1031     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1032         GrDraggable *draggable = (GrDraggable *) i->data;
1033         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1034             return true;
1035         }
1036     }
1037     return false;
1040 /**
1041 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1042  */
1043 bool
1044 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1046     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1047         GrDraggable *draggable = (GrDraggable *) i->data;
1048         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1049             return true;
1050         }
1051     }
1052     return false;
1055 bool
1056 GrDraggable::mayMerge (GrDraggable *da2)
1058     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1059         // we must not merge the points of the same gradient!
1060         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1061               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1062             // except that we can snap center and focus together
1063             return false;
1064         }
1065     }
1066     // disable merging of midpoints.
1067     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1068          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1069          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1070         return false;
1072     return true;
1075 bool
1076 GrDragger::mayMerge (GrDragger *other)
1078     if (this == other)
1079         return false;
1081     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1082         GrDraggable *da1 = (GrDraggable *) i->data;
1083         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1084             GrDraggable *da2 = (GrDraggable *) j->data;
1085             if (!da1->mayMerge(da2))
1086                 return false;
1087         }
1088     }
1089     return true;
1092 bool
1093 GrDragger::mayMerge (GrDraggable *da2)
1095     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1096         GrDraggable *da1 = (GrDraggable *) i->data;
1097         if (!da1->mayMerge(da2))
1098             return false;
1099     }
1100     return true;
1103 /**
1104 Updates the statusbar tip of the dragger knot, based on its draggables
1105  */
1106 void
1107 GrDragger::updateTip ()
1109         if (this->knot && this->knot->tip) {
1110                 g_free (this->knot->tip);
1111                 this->knot->tip = NULL;
1112         }
1114     if (g_slist_length (this->draggables) == 1) {
1115         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1116         char *item_desc = sp_item_description(draggable->item);
1117         switch (draggable->point_type) {
1118             case POINT_LG_MID:
1119             case POINT_RG_MID1:
1120             case POINT_RG_MID2:
1121                 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"),
1122                                                    _(gr_knot_descr[draggable->point_type]),
1123                                                    draggable->point_i,
1124                                                    item_desc,
1125                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1126                 break;
1128             default:
1129                 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"),
1130                                                    _(gr_knot_descr[draggable->point_type]),
1131                                                    item_desc,
1132                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1133                 break;
1134         }
1135         g_free(item_desc);
1136     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1137         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1138     } else {
1139         int length = g_slist_length (this->draggables);
1140         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1141                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1142                                                     length),
1143                                            length);
1144     }
1147 /**
1148 Adds a draggable to the dragger
1149  */
1150 void
1151 GrDragger::updateKnotShape ()
1153     if (!draggables)
1154         return;
1155     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1156     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1159 /**
1160 Adds a draggable to the dragger
1161  */
1162 void
1163 GrDragger::addDraggable (GrDraggable *draggable)
1165     this->draggables = g_slist_prepend (this->draggables, draggable);
1167     this->updateTip();
1171 /**
1172 Moves this dragger to the point of the given draggable, acting upon all other draggables
1173  */
1174 void
1175 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1177     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1178     this->point_original = this->point;
1180     sp_knot_moveto (this->knot, &(this->point));
1182     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1183         GrDraggable *da = (GrDraggable *) i->data;
1184         if ( (da->item == item) && 
1185              (point_type == -1 || da->point_type == point_type) &&
1186              (point_i == -1 || da->point_i == point_i) &&
1187              (da->fill_or_stroke == fill_or_stroke) ) {
1188             continue;
1189         }
1190         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1191     }
1192     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1196 /**
1197 Moves all midstop draggables that depend on this one
1198  */
1199 void
1200 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1201     SPObject *server = draggable->getServer();
1202     if (!server) 
1203         return;
1204     guint num = SP_GRADIENT(server)->vector.stops.size();
1205     if (num <= 2) return;
1207     if ( SP_IS_LINEARGRADIENT(server) ) {
1208         for ( guint i = 1; i < num - 1; i++ ) {
1209             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1210         }
1211     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1212         for ( guint i = 1; i < num - 1; i++ ) {
1213             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1214             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1215         }
1216     }
1220 /**
1221 Moves all draggables that depend on this one
1222  */
1223 void
1224 GrDragger::updateDependencies (bool write_repr)
1226     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1227         GrDraggable *draggable = (GrDraggable *) i->data;
1228         switch (draggable->point_type) {
1229             case POINT_LG_BEGIN:
1230                 {
1231                     // the end point is dependent only when dragging with ctrl+shift
1232                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1234                     this->updateMidstopDependencies (draggable, write_repr);
1235                 }
1236                 break;
1237             case POINT_LG_END:
1238                 {
1239                     // the begin point is dependent only when dragging with ctrl+shift
1240                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1242                     this->updateMidstopDependencies (draggable, write_repr);
1243                 }
1244                 break;
1245             case POINT_LG_MID:
1246                 // no other nodes depend on mid points.
1247                 break;
1248             case POINT_RG_R2:
1249                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1250                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1251                 this->updateMidstopDependencies (draggable, write_repr);
1252                 break;
1253             case POINT_RG_R1:
1254                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1255                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1256                 this->updateMidstopDependencies (draggable, write_repr);
1257                 break;
1258             case POINT_RG_CENTER:
1259                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1260                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1261                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1262                 this->updateMidstopDependencies (draggable, write_repr);
1263                 break;
1264             case POINT_RG_FOCUS:
1265                 // nothing can depend on that
1266                 break;
1267             case POINT_RG_MID1:
1268                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1269                 break;
1270             case POINT_RG_MID2:
1271                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1272                 break;
1273             default:
1274                 break;
1275         }
1276     }
1281 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1283     this->draggables = NULL;
1285     this->parent = parent;
1287     this->point = p;
1288     this->point_original = p;
1290     // create the knot
1291     this->knot = sp_knot_new (parent->desktop, NULL);
1292     this->knot->setMode(SP_KNOT_MODE_XOR);
1293     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1294     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1295     sp_knot_update_ctrl(this->knot);
1297     // move knot to the given point
1298     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1299     sp_knot_show (this->knot);
1301     // connect knot's signals
1302     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1303                        // luckily, midstops never snap to other nodes so are never unsnapped...
1304          && ( (draggable->point_type == POINT_LG_MID)
1305               || (draggable->point_type == POINT_RG_MID1)
1306               || (draggable->point_type == POINT_RG_MID2) ) )
1307     {
1308         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1309     } else {
1310         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1311     }
1312     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1313     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1314     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1315     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1317     // add the initial draggable
1318     if (draggable)
1319         this->addDraggable (draggable);
1320     updateKnotShape();
1323 GrDragger::~GrDragger ()
1325     // unselect if it was selected
1326     this->parent->setDeselected(this);
1328     // disconnect signals
1329     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1330     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1331     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1332     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1333     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1335     /* unref should call destroy */
1336     g_object_unref (G_OBJECT (this->knot));
1338     // delete all draggables
1339     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1340         delete ((GrDraggable *) i->data);
1341     }
1342     g_slist_free (this->draggables);
1343     this->draggables = NULL;
1346 /**
1347 Select the dragger which has the given draggable.
1348 */
1349 GrDragger *
1350 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1352     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1353         GrDragger *dragger = (GrDragger *) i->data;
1354         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1355             GrDraggable *da2 = (GrDraggable *) j->data;
1356             if ( (da2->item == item) && 
1357                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1358                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1359                  (da2->fill_or_stroke == fill_or_stroke)) {
1360                 return (dragger);
1361             }
1362         }
1363     }
1364     return NULL;
1368 void
1369 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1371     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1372     if (d && d !=  this) {
1373         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1374     }
1378 /**
1379   Draw this dragger as selected
1380 */
1381 void
1382 GrDragger::select()
1384     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1385     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1388 /**
1389   Draw this dragger as normal (deselected)
1390 */
1391 void
1392 GrDragger::deselect()
1394     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1395     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1398 bool
1399 GrDragger::isSelected()
1401     return g_list_find (parent->selected, this);
1404 /**
1405 \brief Deselect all stops/draggers (private)
1406 */
1407 void
1408 GrDrag::deselect_all()
1410     while (selected) {
1411         ( (GrDragger*) selected->data)->deselect();
1412         selected = g_list_remove(selected, selected->data);
1413     }
1416 /**
1417 \brief Deselect all stops/draggers (public; emits signal)
1418 */
1419 void
1420 GrDrag::deselectAll()
1422     deselect_all();
1423     this->desktop->emitToolSubselectionChanged(NULL);
1426 /**
1427 \brief Select all stops/draggers
1428 */
1429 void
1430 GrDrag::selectAll()
1432     for (GList *l = this->draggers; l != NULL; l = l->next) {
1433         GrDragger *d = ((GrDragger *) l->data);
1434         setSelected (d, true, true);
1435     }
1438 /**
1439 \brief Select all stops/draggers that match the coords
1440 */
1441 void 
1442 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1444     for (GList *l = this->draggers; l != NULL; l = l->next) {
1445         GrDragger *d = ((GrDragger *) l->data);
1446         for (guint k = 0; k < coords.size(); k++) {
1447             if (NR::L2 (d->point - coords[k]) < 1e-4) {
1448                 setSelected (d, true, true);
1449             }
1450         }
1451     }
1455 /**
1456 \brief Select all stops/draggers that fall within the rect
1457 */
1458 void 
1459 GrDrag::selectRect(NR::Rect const &r)
1461     for (GList *l = this->draggers; l != NULL; l = l->next) {
1462         GrDragger *d = ((GrDragger *) l->data);
1463         if (r.contains(d->point)) {
1464            setSelected (d, true, true);
1465         }
1466     }
1469 /**
1470 \brief Select a dragger
1471 \param dragger       The dragger to select
1472 \param add_to_selection   If true, add to selection, otherwise deselect others
1473 \param override      If true, always select this node, otherwise toggle selected status
1474 */
1475 void
1476 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1478     GrDragger *seldragger = NULL;
1480     if (add_to_selection) {
1481         if (!dragger) return;
1482         if (override) {
1483             if (!g_list_find(selected, dragger)) {
1484                 selected = g_list_prepend(selected, dragger);
1485             }
1486             dragger->select();
1487             seldragger = dragger;
1488         } else { // toggle
1489             if (g_list_find(selected, dragger)) {
1490                 selected = g_list_remove(selected, dragger);
1491                 dragger->deselect();
1492                 if (selected) {
1493                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1494                 }
1495             } else {
1496                 selected = g_list_prepend(selected, dragger);
1497                 dragger->select();
1498                 seldragger = dragger;
1499             }
1500         }
1501     } else {
1502         deselect_all();
1503         if (dragger) {
1504             selected = g_list_prepend(selected, dragger);
1505             dragger->select();
1506             seldragger = dragger;
1507         }
1508     }
1509     if (seldragger) {
1510         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1511     }
1514 /**
1515 \brief Deselect a dragger
1516 \param dragger       The dragger to deselect
1517 */
1518 void
1519 GrDrag::setDeselected (GrDragger *dragger)
1521     if (g_list_find(selected, dragger)) {
1522         selected = g_list_remove(selected, dragger);
1523         dragger->deselect();
1524     }
1525     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1530 /**
1531 Create a line from p1 to p2 and add it to the lines list
1532  */
1533 void
1534 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1536     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1537                                                             SP_TYPE_CTRLLINE, NULL);
1538     SP_CTRLLINE(line)->item = item;
1539     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1540     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1541         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1542     sp_canvas_item_show (line);
1543     this->lines = g_slist_append (this->lines, line);
1546 /**
1547 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1548 new dragger and add it to draggers list
1549  */
1550 void
1551 GrDrag::addDragger (GrDraggable *draggable)
1553     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1555     for (GList *i = this->draggers; i != NULL; i = i->next) {
1556         GrDragger *dragger = (GrDragger *) i->data;
1557         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1558             // distance is small, merge this draggable into dragger, no need to create new dragger
1559             dragger->addDraggable (draggable);
1560             dragger->updateKnotShape();
1561             return;
1562         }
1563     }
1565     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1566     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1567     this->draggers = g_list_append (this->draggers, new_dragger);
1570 /**
1571 Add draggers for the radial gradient rg on item
1572 */
1573 void
1574 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1576     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1577     guint num = rg->vector.stops.size();
1578     if (num > 2) {
1579         for ( guint i = 1; i < num - 1; i++ ) {
1580             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1581         }
1582     }
1583     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1584     if (num > 2) {
1585         for ( guint i = 1; i < num - 1; i++ ) {
1586             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1587         }
1588     }
1589     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1590     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1593 /**
1594 Add draggers for the linear gradient lg on item
1595 */
1596 void
1597 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1599     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1600     guint num = lg->vector.stops.size();
1601     if (num > 2) {
1602         for ( guint i = 1; i < num - 1; i++ ) {
1603             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1604         }
1605     }
1606     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1609 /**
1610 Artificially grab the knot of this dragger; used by the gradient context
1611 */
1612 void
1613 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1615     if (dragger) {
1616         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1617     }
1620 /**
1621 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1622 */
1623 void
1624 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1626     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1627     if (dragger) {
1628         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1629     }
1632 /**
1633 Regenerates the draggers list from the current selection; is called when selection is changed or
1634 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1635 */
1636 void
1637 GrDrag::updateDraggers ()
1639     while (selected) {
1640         selected = g_list_remove(selected, selected->data);
1641     }
1642     // delete old draggers
1643     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1644         delete ((GrDragger *) i->data);
1645     }
1646     g_list_free (this->draggers);
1647     this->draggers = NULL;
1649     g_return_if_fail (this->selection != NULL);
1651     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1653         SPItem *item = SP_ITEM(i->data);
1654         SPStyle *style = SP_OBJECT_STYLE (item);
1656         if (style && (style->fill.isPaintserver())) {
1657             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1658             if (SP_IS_LINEARGRADIENT (server)) {
1659                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1660             } else if (SP_IS_RADIALGRADIENT (server)) {
1661                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1662             }
1663         }
1665         if (style && (style->stroke.isPaintserver())) {
1666             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1667             if (SP_IS_LINEARGRADIENT (server)) {
1668                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1669             } else if (SP_IS_RADIALGRADIENT (server)) {
1670                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1671             }
1672         }
1673     }
1676 /**
1677 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1678 lines are always in sync with the actual gradient
1679 */
1680 void
1681 GrDrag::updateLines ()
1683     // delete old lines
1684     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1685         gtk_object_destroy( GTK_OBJECT (i->data));
1686     }
1687     g_slist_free (this->lines);
1688     this->lines = NULL;
1690     g_return_if_fail (this->selection != NULL);
1692     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1694         SPItem *item = SP_ITEM(i->data);
1696         SPStyle *style = SP_OBJECT_STYLE (item);
1698         if (style && (style->fill.isPaintserver())) {
1699             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1700             if (SP_IS_LINEARGRADIENT (server)) {
1701                 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);
1702             } else if (SP_IS_RADIALGRADIENT (server)) {
1703                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1704                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1705                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1706             }
1707         }
1709         if (style && (style->stroke.isPaintserver())) {
1710             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1711             if (SP_IS_LINEARGRADIENT (server)) {
1712                 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);
1713             } else if (SP_IS_RADIALGRADIENT (server)) {
1714                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1715                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1716                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1717             }
1718         }
1719     }
1722 /**
1723 Regenerates the levels list from the current selection
1724 */
1725 void
1726 GrDrag::updateLevels ()
1728     hor_levels.clear();
1729     vert_levels.clear();
1731     g_return_if_fail (this->selection != NULL);
1733     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1734         SPItem *item = SP_ITEM(i->data);
1735         NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1736         if (rect) {
1737             // Remember the edges of the bbox and the center axis
1738             hor_levels.push_back(rect->min()[NR::Y]);
1739             hor_levels.push_back(rect->max()[NR::Y]);
1740             hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1741             vert_levels.push_back(rect->min()[NR::X]);
1742             vert_levels.push_back(rect->max()[NR::X]);
1743             vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1744         }
1745     }
1748 void
1749 GrDrag::selected_reverse_vector ()
1751     if (selected == NULL)
1752         return;
1754     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1755         GrDraggable *draggable = (GrDraggable *) i->data;
1757         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1758     }
1761 void
1762 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1764     selected_move (x, y, false, scale_radial);
1767 void
1768 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1770     if (selected == NULL)
1771         return;
1773     bool did = false; 
1775     for (GList *i = selected; i != NULL; i = i->next) {
1776         GrDragger *d = (GrDragger *) i->data;
1778         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1779             // if this is an endpoint,
1781             // Moving an rg center moves its focus and radii as well.
1782             // therefore, if this is a focus or radius and if selection
1783             // contains the center as well, do not move this one
1784             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) || 
1785                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1786                 bool skip_radius_with_center = false;
1787                 for (GList *di = selected; di != NULL; di = di->next) {
1788                     GrDragger *d_new = (GrDragger *) di->data;
1789                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1790                                     POINT_RG_CENTER,
1791                                     0,
1792                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1793                         // FIXME: here we take into account only the first draggable!
1794                         skip_radius_with_center = true;
1795                     }
1796                 }
1797                 if (skip_radius_with_center)
1798                     continue;
1799             }
1801             did = true;
1802             d->point += NR::Point (x, y);
1803             d->point_original = d->point;
1804             sp_knot_moveto (d->knot, &(d->point));
1806             d->fireDraggables (write_repr, scale_radial);
1808             d->updateDependencies(write_repr);
1809         }
1810     }
1812     if (write_repr && did) {
1813         // we did an undoable action
1814         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1815                                 _("Move gradient handle(s)"));
1816         return;
1817     }
1819     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1821         GrDragger *dragger = (GrDragger *) selected->data;
1822         // a midpoint dragger can (logically) only contain one GrDraggable
1823         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1825         NR::Point begin(0,0), end(0,0);
1826         NR::Point low_lim(0,0), high_lim(0,0);
1828         SPObject *server = draggable->getServer();
1829         GSList *moving = NULL;
1830         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1832         NR::Point p(x, y);
1833         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1834         NR::Point displacement = p - dragger->point;
1836         for (GSList const* i = moving; i != NULL; i = i->next) {
1837             GrDragger *drg = (GrDragger*) i->data;
1838             SPKnot *drgknot = drg->knot;
1839             drg->point += displacement;
1840             sp_knot_moveto (drgknot, & drg->point);
1841             drg->fireDraggables (true);
1842             drg->updateDependencies(true);
1843             did = true;
1844         }
1846         g_slist_free(moving);
1848         if (write_repr && did) {
1849             // we did an undoable action
1850             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1851                                     _("Move gradient mid stop(s)"));
1852         }
1853     }
1856 void
1857 GrDrag::selected_move_screen (double x, double y)
1859     gdouble zoom = desktop->current_zoom();
1860     gdouble zx = x / zoom;
1861     gdouble zy = y / zoom;
1863     selected_move (zx, zy);
1866 /**
1867 Select the knot next to the last selected one and deselect all other selected.
1868 */
1869 GrDragger *
1870 GrDrag::select_next ()
1872     GrDragger *d = NULL;
1873     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1874         if (draggers) 
1875             d = (GrDragger *) draggers->data;
1876     } else {
1877         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1878     }
1879     if (d)
1880         setSelected (d);
1881     return d;
1884 /**
1885 Select the knot previous from the last selected one and deselect all other selected.
1886 */
1887 GrDragger *
1888 GrDrag::select_prev ()
1890     GrDragger *d = NULL;
1891     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1892         if (draggers)
1893             d = (GrDragger *) g_list_last (draggers)->data;
1894     } else {
1895         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1896     }
1897     if (d)
1898         setSelected (d);
1899     return d;
1903 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1904 void
1905 GrDrag::deleteSelected (bool just_one)
1907     if (!selected) return;
1909     SPDocument *document = false;
1911     struct StructStopInfo {
1912         SPStop * spstop;
1913         GrDraggable * draggable;
1914         SPGradient * gradient;
1915         SPGradient * vector;
1916     };
1918     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1919     GSList *endstoplist = NULL;  // list of stops that must be deleted
1920     while (selected) {
1921         GrDragger *dragger = (GrDragger*) selected->data;
1922         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1923             GrDraggable *draggable = (GrDraggable*) drgble->data;
1924             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1925             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1927             switch (draggable->point_type) {
1928                 case POINT_LG_MID:
1929                 case POINT_RG_MID1:
1930                 case POINT_RG_MID2:
1931                     {
1932                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1933                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1934                         bool present = false;
1935                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1936                             if ( (SPStop*)l->data == stop ) {
1937                                 present = true;
1938                                 break; // no need to search further.
1939                             }
1940                         }
1941                         if (!present)
1942                             midstoplist = g_slist_append(midstoplist, stop);
1943                     }
1944                     break;
1945                 case POINT_LG_BEGIN:
1946                 case POINT_LG_END:
1947                 case POINT_RG_CENTER:
1948                 case POINT_RG_R1:
1949                 case POINT_RG_R2:
1950                     {
1951                         SPStop *stop = NULL;
1952                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1953                             stop = sp_first_stop(vector);
1954                         } else {
1955                             stop = sp_last_stop(vector);
1956                         }
1957                         if (stop) {
1958                             StructStopInfo *stopinfo = new StructStopInfo;
1959                             stopinfo->spstop = stop;
1960                             stopinfo->draggable = draggable;
1961                             stopinfo->gradient = gradient;
1962                             stopinfo->vector = vector;
1963                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1964                             bool present = false;
1965                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1966                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1967                                     present = true;
1968                                     break; // no need to search further.
1969                                 }
1970                             }
1971                             if (!present)
1972                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1973                         }
1974                     }
1975                     break;
1976                 default:
1977                     break;
1978             }
1979         }
1980         selected = g_list_remove(selected, dragger);
1981         if ( just_one ) break; // iterate once if just_one is set.
1982     }
1983     while (midstoplist) {
1984         SPStop *stop = (SPStop*) midstoplist->data;
1985         document = SP_OBJECT_DOCUMENT (stop);
1986         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1987         parent->removeChild(SP_OBJECT_REPR(stop));
1988         midstoplist = g_slist_remove(midstoplist, stop);
1989     }
1990     while (endstoplist) {
1991         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1992         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1994         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1995         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1996         // manually count the children, don't know if there already exists a function for this...
1997         int len = 0;
1998         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1999               child != NULL ;
2000               child = SP_OBJECT_NEXT(child) )
2001         {
2002             if ( SP_IS_STOP(child) )  len ++;
2003         }
2004         if (len > 2)
2005         {
2006             switch (stopinfo->draggable->point_type) {
2007                 case POINT_LG_BEGIN:
2008                     {
2009                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2011                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2012                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
2013                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
2014                         SPStop *stop = sp_first_stop(stopinfo->vector);
2015                         gdouble offset = stop->offset;
2016                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
2017                         lg->x1.computed = newbegin[NR::X];
2018                         lg->y1.computed = newbegin[NR::Y];
2020                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2021                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2022                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2023                         stop->offset = 0;
2024                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2026                         // iterate through midstops to set new offset values such that they won't move on canvas.
2027                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2028                         stop = sp_next_stop(stop);
2029                         while ( stop != laststop ) {
2030                             stop->offset = (stop->offset - offset)/(1 - offset);
2031                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2032                             stop = sp_next_stop(stop);
2033                         }
2034                     }
2035                     break;
2036                 case POINT_LG_END:
2037                     {
2038                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2040                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2041                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
2042                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
2043                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2044                         gdouble offset = laststop->offset;
2045                         NR::Point newend = begin + offset * (oldend - begin);
2046                         lg->x2.computed = newend[NR::X];
2047                         lg->y2.computed = newend[NR::Y];
2049                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2050                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2051                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2052                         laststop->offset = 1;
2053                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2055                         // iterate through midstops to set new offset values such that they won't move on canvas.
2056                         SPStop *stop = sp_first_stop(stopinfo->vector);
2057                         stop = sp_next_stop(stop);
2058                         while ( stop != laststop ) {
2059                             stop->offset = stop->offset / offset;
2060                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2061                             stop = sp_next_stop(stop);
2062                         }
2063                     }
2064                     break;
2065                 case POINT_RG_CENTER:
2066                     {
2067                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2068                         if (newfirst) {
2069                             newfirst->offset = 0;
2070                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2071                         }
2072                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2073                     }
2074                     break;
2075                 case POINT_RG_R1:
2076                 case POINT_RG_R2:
2077                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2079                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2080                         double oldradius = rg->r.computed;
2081                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2082                         gdouble offset = laststop->offset;
2083                         double newradius = offset * oldradius;
2084                         rg->r.computed = newradius;
2086                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2087                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2088                         laststop->offset = 1;
2089                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2091                         // iterate through midstops to set new offset values such that they won't move on canvas.
2092                         SPStop *stop = sp_first_stop(stopinfo->vector);
2093                         stop = sp_next_stop(stop);
2094                         while ( stop != laststop ) {
2095                             stop->offset = stop->offset / offset;
2096                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2097                             stop = sp_next_stop(stop);
2098                         }
2099                         break;
2100             }
2101         }
2102         else
2103         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2104             SPCSSAttr *css = sp_repr_css_attr_new ();
2106             // stopinfo->spstop is the selected stop
2107             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2108             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2109                 unselectedrepr = unselectedrepr->next();
2110             }
2112             if (unselectedrepr == NULL) {
2113                 if (stopinfo->draggable->fill_or_stroke) {
2114                     sp_repr_css_unset_property (css, "fill");
2115                 } else {
2116                     sp_repr_css_unset_property (css, "stroke");
2117                 }
2118             } else {
2119                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2120                 if (stopinfo->draggable->fill_or_stroke) {
2121                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2122                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2123                 } else {
2124                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2125                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2126                 }
2127                 sp_repr_css_attr_unref (stopcss);
2128             }
2129             
2130             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2131             sp_repr_css_attr_unref (css);
2132         }
2134         endstoplist = g_slist_remove(endstoplist, stopinfo);
2135         delete stopinfo;
2136     }
2138     if (document) {
2139         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2140     }
2144 /*
2145   Local Variables:
2146   mode:c++
2147   c-file-style:"stroustrup"
2148   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2149   indent-tabs-mode:nil
2150   fill-column:99
2151   End:
2152 */
2153 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :