Code

add dropColor; mouseovered knots red; store item for each line, for adding 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"
29 #include "xml/repr.h"
30 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
33 #include "libnr/nr-point-fns.h"
35 #include "prefs-utils.h"
36 #include "sp-item.h"
37 #include "style.h"
38 #include "knot.h"
39 #include "sp-linear-gradient.h"
40 #include "sp-radial-gradient.h"
41 #include "gradient-chemistry.h"
42 #include "gradient-drag.h"
43 #include "sp-stop.h"
45 #include "snap.h"
46 #include "sp-namedview.h"
49 #define GR_KNOT_COLOR_NORMAL 0xffffff00
50 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
51 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
53 #define GR_LINE_COLOR_FILL 0x0000ff7f
54 #define GR_LINE_COLOR_STROKE 0x9999007f
56 // screen pixels between knots when they snap:
57 #define SNAP_DIST 5
59 // absolute distance between gradient points for them to become a single dragger when the drag is created:
60 #define MERGE_DIST 0.1
62 // knot shapes corresponding to GrPointType enum
63 SPKnotShapeType gr_knot_shapes [] = {
64         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
65         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
66         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
67         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
68         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
69         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
70         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
71         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
72         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
73 };
75 const gchar *gr_knot_descr [] = {
76     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
77     N_("Linear gradient <b>end</b>"),
78     N_("Linear gradient <b>mid stop</b>"),
79     N_("Radial gradient <b>center</b>"),
80     N_("Radial gradient <b>radius</b>"),
81     N_("Radial gradient <b>radius</b>"),
82     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
83     N_("Radial gradient <b>mid stop</b>"),
84     N_("Radial gradient <b>mid stop</b>")
85 };
87 static void
88 gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
89 {
90         GrDrag *drag = (GrDrag *) data;
91         drag->updateDraggers ();
92         drag->updateLines ();
93         drag->updateLevels ();
94 }
96 static void
97 gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
98 {
99     GrDrag *drag = (GrDrag *) data;
100     if (drag->local_change) {
101         drag->local_change = false;
102     } else {
103         drag->updateDraggers ();
104     }
105     drag->updateLines ();
106     drag->updateLevels ();
109 /**
110 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
111 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
112 any.
113 */
114 int
115 gr_drag_style_query (SPStyle *style, int property, gpointer data)
117     GrDrag *drag = (GrDrag *) data;
119     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
120         return QUERY_STYLE_NOTHING;
121     }
123     if (!drag->selected) {
124         return QUERY_STYLE_NOTHING;
125     } else {
126         int ret = QUERY_STYLE_NOTHING;
128         float cf[4];
129         cf[0] = cf[1] = cf[2] = cf[3] = 0;
131         int count = 0;
133         for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
134             GrDragger *d = (GrDragger *) i->data;
135             for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
136                 GrDraggable *draggable = (GrDraggable *) j->data;
138                 if (ret == QUERY_STYLE_NOTHING) {
139                     ret = QUERY_STYLE_SINGLE;
140                 } else if (ret == QUERY_STYLE_SINGLE) {
141                     ret = QUERY_STYLE_MULTIPLE_AVERAGED;
142                 }
144                 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
145                 cf[0] += SP_RGBA32_R_F (c);
146                 cf[1] += SP_RGBA32_G_F (c);
147                 cf[2] += SP_RGBA32_B_F (c);
148                 cf[3] += SP_RGBA32_A_F (c);
150                 count ++;
151             }
152         }
154         if (count) {
155             cf[0] /= count;
156             cf[1] /= count;
157             cf[2] /= count;
158             cf[3] /= count;
160             // set both fill and stroke with our stop-color and stop-opacity
161             style->fill.clear();
162             style->fill.setColor( cf[0], cf[1], cf[2] );
163             style->fill.set = TRUE;
164             style->stroke.clear();
165             style->stroke.setColor( cf[0], cf[1], cf[2] );
166             style->stroke.set = TRUE;
168             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
169             style->fill_opacity.set = TRUE;
170             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
171             style->stroke_opacity.set = TRUE;
173             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
174             style->opacity.set = TRUE;
175         }
177         return ret;
178     }
181 bool
182 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
184     GrDrag *drag = (GrDrag *) data;
186     if (!drag->selected)
187         return false;
189     SPCSSAttr *stop = sp_repr_css_attr_new ();
191     // See if the css contains interesting properties, and if so, translate them into the format
192     // acceptable for gradient stops
194     // any of color properties, in order of increasing priority:
195     if (css->attribute("flood-color"))
196         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
198     if (css->attribute("lighting-color"))
199         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
201     if (css->attribute("color"))
202         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
204     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
205         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
207     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
208         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
210     if (css->attribute("stop-color"))
211         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
214     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
215         sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
216     } else {  // multiply all opacity properties:
217         gdouble accumulated = 1.0;
218         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
219         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
220         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
221         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
223         Inkscape::CSSOStringStream os;
224         os << accumulated;
225         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
227         if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
228             (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
229             sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
230     }
232     if (!stop->attributeList()) { // nothing for us here, pass it on
233         sp_repr_css_attr_unref(stop);
234         return false;
235     }
237     for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
238         GrDragger* dragger = (GrDragger*) sel->data;
239         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
240                GrDraggable *draggable = (GrDraggable *) i->data;
242                drag->local_change = true;
243                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
244         }
245     }
247     //sp_repr_css_print(stop);
248     sp_repr_css_attr_unref(stop);
249     return true;
252 SPStop *
253 GrDrag::addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance)
255     gfloat offset; // type of SPStop.offset = gfloat
256     SPGradient *gradient;
257     bool fill_or_stroke = true;
258     bool r1_knot = false;
260     bool addknot = false;
261     do {
262         gradient = sp_item_gradient (item, fill_or_stroke);
263         if (SP_IS_LINEARGRADIENT(gradient)) {
264             NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
265             NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
267             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
268             double dist_screen = NR::L2 (mouse_p - nearest);
269             if ( dist_screen < tolerance ) {
270                 // add the knot
271                 offset = get_offset_between_points(nearest, begin, end);
272                 addknot = true;
273                 break; // break out of the while loop: add only one knot
274             }
275         } else if (SP_IS_RADIALGRADIENT(gradient)) {
276             NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
277             NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
278             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
279             double dist_screen = NR::L2 (mouse_p - nearest);
280             if ( dist_screen < tolerance ) {
281                 offset = get_offset_between_points(nearest, begin, end);
282                 addknot = true;
283                 r1_knot = true;
284                 break; // break out of the while loop: add only one knot
285             }
287             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
288             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
289             dist_screen = NR::L2 (mouse_p - nearest);
290             if ( dist_screen < tolerance ) {
291                 offset = get_offset_between_points(nearest, begin, end);
292                 addknot = true;
293                 r1_knot = false;
294                 break; // break out of the while loop: add only one knot
295             }
296         }
297         fill_or_stroke = !fill_or_stroke;
298     } while (!fill_or_stroke && !addknot) ;
300     if (addknot) {
301         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
302         SPStop* prev_stop = sp_first_stop(vector);
303         SPStop* next_stop = sp_next_stop(prev_stop);
304         guint i = 1;
305         while ( (next_stop) && (next_stop->offset < offset) ) {
306             prev_stop = next_stop;
307             next_stop = sp_next_stop(next_stop);
308             i++;
309         }
310         if (!next_stop) {
311             // logical error: the endstop should have offset 1 and should always be more than this offset here
312             return NULL;
313         }
316         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
317         sp_gradient_ensure_vector (gradient);
318         updateDraggers();
320         return newstop;
321     } 
323     return NULL;
327 bool 
328 GrDrag::dropColor(SPItem *item, gchar *c, NR::Point p) 
330     // first, see if we can drop onto one of the existing draggers
331     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
332         GrDragger *d = (GrDragger *) i->data;
334         if (NR::L2(p - d->point)*desktop->current_zoom() < 5) {
335            SPCSSAttr *stop = sp_repr_css_attr_new ();
336            sp_repr_css_set_property (stop, "stop-color", c);
337            sp_repr_css_set_property (stop, "stop-opacity", "1");
338            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
339                GrDraggable *draggable = (GrDraggable *) j->data;
340                local_change = true;
341                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
342            }
343            sp_repr_css_attr_unref(stop);
344            return true;
345         }
346     }
348     // now see if we're over line and create a new stop
349     bool over_line = false;
350     SPCtrlLine *line = NULL;
351     if (lines) {
352         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
353             line = (SPCtrlLine*) l->data;
354             NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
355             double dist_screen = NR::L2 (p - nearest) * desktop->current_zoom();
356             if (line->item && dist_screen < 5) {
357                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
358                 if (stop) {
359                     SPCSSAttr *css = sp_repr_css_attr_new ();
360                     sp_repr_css_set_property (css, "stop-color", c);
361                     sp_repr_css_set_property (css, "stop-opacity", "1");
362                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
363                     return true;
364                 }
365             }
366         }
367     }
369     return false;
373 GrDrag::GrDrag(SPDesktop *desktop) {
375     this->desktop = desktop;
377     this->selection = sp_desktop_selection(desktop);
379     this->draggers = NULL;
380     this->lines = NULL;
381     this->selected = NULL;
383     this->hor_levels.clear();
384     this->vert_levels.clear();
386     this->local_change = false;
388     this->sel_changed_connection = this->selection->connectChanged(
389         sigc::bind (
390             sigc::ptr_fun(&gr_drag_sel_changed),
391             (gpointer)this )
393         );
394     this->sel_modified_connection = this->selection->connectModified(
395         sigc::bind(
396             sigc::ptr_fun(&gr_drag_sel_modified),
397             (gpointer)this )
398         );
400     this->style_set_connection = this->desktop->connectSetStyle(
401         sigc::bind(
402             sigc::ptr_fun(&gr_drag_style_set),
403             (gpointer)this )
404         );
406     this->style_query_connection = this->desktop->connectQueryStyle(
407         sigc::bind(
408             sigc::ptr_fun(&gr_drag_style_query),
409             (gpointer)this )
410         );
412     this->updateDraggers ();
413     this->updateLines ();
414     this->updateLevels ();
416     if (desktop->gr_item) {
417         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
418     }
421 GrDrag::~GrDrag()
423     this->sel_changed_connection.disconnect();
424     this->sel_modified_connection.disconnect();
425     this->style_set_connection.disconnect();
426     this->style_query_connection.disconnect();
428     if (this->selected) {
429         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
430         desktop->gr_item = draggable->item;
431         desktop->gr_point_type = draggable->point_type;
432         desktop->gr_point_i = draggable->point_i;
433         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
434     } else {
435         desktop->gr_item = NULL;
436         desktop->gr_point_type = 0;
437         desktop->gr_point_i = 0;
438         desktop->gr_fill_or_stroke = true;
439     }
441     deselect_all();
442     for (GList *l = this->draggers; l != NULL; l = l->next) {
443         delete ((GrDragger *) l->data);
444     }
445     g_list_free (this->draggers);
446     this->draggers = NULL;
447     this->selected = NULL;
449     for (GSList *l = this->lines; l != NULL; l = l->next) {
450         gtk_object_destroy( GTK_OBJECT (l->data));
451     }
452     g_slist_free (this->lines);
453     this->lines = NULL;
456 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
458     this->item = item;
459     this->point_type = point_type;
460     this->point_i = point_i;
461     this->fill_or_stroke = fill_or_stroke;
463     g_object_ref (G_OBJECT (this->item));
466 GrDraggable::~GrDraggable ()
468     g_object_unref (G_OBJECT (this->item));
472 SPObject *
473 GrDraggable::getServer ()
475     if (!item)
476         return NULL;
478     SPObject *server = NULL;
479     if (fill_or_stroke)
480         server = SP_OBJECT_STYLE_FILL_SERVER (item);
481     else
482         server = SP_OBJECT_STYLE_STROKE_SERVER (item);
484     return server;
487 static void
488 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
490     GrDragger *dragger = (GrDragger *) data;
491     GrDrag *drag = dragger->parent;
493     NR::Point p = *ppointer;
495     // FIXME: take from prefs
496     double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
498     if (state & GDK_SHIFT_MASK) {
499         // with Shift; unsnap if we carry more than one draggable
500         if (dragger->draggables && dragger->draggables->next) {
501             // create a new dragger
502             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
503             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
504             // relink to it all but the first draggable in the list
505             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
506                 GrDraggable *draggable = (GrDraggable *) i->data;
507                 dr_new->addDraggable (draggable);
508             }
509             dr_new->updateKnotShape();
510             g_slist_free (dragger->draggables->next);
511             dragger->draggables->next = NULL;
512             dragger->updateKnotShape();
513             dragger->updateTip();
514         }
515     } else if (!(state & GDK_CONTROL_MASK)) {
516         // without Shift or Ctrl; see if we need to snap to another dragger
517         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
518             GrDragger *d_new = (GrDragger *) di->data;
519             if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
521                 // Merge draggers:
522                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
523                     GrDraggable *draggable = (GrDraggable *) i->data;
524                     // copy draggable to d_new:
525                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
526                     d_new->addDraggable (da_new);
527                 }
529                 // unlink and delete this dragger
530                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
531                 delete dragger;
533                 // update the new merged dragger
534                 d_new->fireDraggables(true, false, true);
535                 d_new->parent->updateLines();
536                 d_new->parent->setSelected (d_new);
537                 d_new->updateKnotShape ();
538                 d_new->updateTip ();
539                 d_new->updateDependencies(true);
540                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
541                                   _("Merge gradient handles"));
542                 return;
543             }
544         }
545     }
547     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
548         // Try snapping to the grid or guides
549         SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
550         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
551         if (s.getDistance() < 1e6) {
552             p = s.getPoint();
553             sp_knot_moveto (knot, &p);
554         } else {
555             // No snapping so far, let's see if we need to snap to any of the levels
556             for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
557                 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
558                     p[NR::Y] = dragger->parent->hor_levels[i];
559                     sp_knot_moveto (knot, &p);
560                 }
561             }
562             for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
563                 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
564                     p[NR::X] = dragger->parent->vert_levels[i];
565                     sp_knot_moveto (knot, &p);
566                 }
567             }
568         }
569     }
571     if (state & GDK_CONTROL_MASK) {
572         unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
573         /* 0 means no snapping. */
575         // This list will store snap vectors from all draggables of dragger
576         GSList *snap_vectors = NULL;
578         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
579             GrDraggable *draggable = (GrDraggable *) i->data;
581             NR::Point *dr_snap = NULL;
583             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
584                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
585                     GrDragger *d_new = (GrDragger *) di->data;
586                     if (d_new == dragger)
587                         continue;
588                     if (d_new->isA (draggable->item,
589                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
590                                     draggable->point_i,
591                                     draggable->fill_or_stroke)) {
592                         // found the other end of the linear gradient;
593                         if (state & GDK_SHIFT_MASK) {
594                             // moving linear around center
595                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
596                             dr_snap = &center;
597                         } else {
598                             // moving linear around the other end
599                             dr_snap = &d_new->point;
600                         }
601                     }
602                 }
603             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
604                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
605                     GrDragger *d_new = (GrDragger *) di->data;
606                     if (d_new == dragger)
607                         continue;
608                     if (d_new->isA (draggable->item,
609                                     POINT_RG_CENTER,
610                                     draggable->point_i,
611                                     draggable->fill_or_stroke)) {
612                         // found the center of the radial gradient;
613                         dr_snap = &(d_new->point);
614                     }
615                 }
616             } else if (draggable->point_type == POINT_RG_CENTER) {
617                 // radial center snaps to hor/vert relative to its original position
618                 dr_snap = &(dragger->point_original);
619             }
621             NR::Point *snap_vector = NULL;
622             if (dr_snap) {
623                 if (state & GDK_MOD1_MASK) {
624                     // with Alt, snap to the original angle and its perpendiculars
625                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
626                 } else {
627                     // with Ctrl, snap to M_PI/snaps
628                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
629                 }
630             }
631             if (snap_vector) {
632                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
633             }
634         }
636         // Move by the smallest of snap vectors:
637         NR::Point move(9999, 9999);
638         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
639             NR::Point *snap_vector = (NR::Point *) i->data;
640             if (NR::L2(*snap_vector) < NR::L2(move))
641                 move = *snap_vector;
642         }
643         if (move[NR::X] < 9999) {
644             p += move;
645             sp_knot_moveto (knot, &p);
646         }
648         g_slist_free(snap_vectors);
649     }
651     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
652     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
654     if (drag->keep_selection) {
655         NR::Point diff = p - dragger->point;
656         drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
657     } else {
658         dragger->point = p;
659         dragger->fireDraggables (false, scale_radial);
660         dragger->updateDependencies(false);
661     }
666 static void
667 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
670     GrDrag *drag = dragger->parent;
671     // a midpoint dragger can (logically) only contain one GrDraggable
672     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
674     // get begin and end points between which dragging is allowed:
675     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
676     *moving = g_slist_append(*moving, dragger);
678     guint lowest_i = draggable->point_i;
679     guint highest_i = draggable->point_i;
680     GrDragger *lowest_dragger = dragger;
681     GrDragger *highest_dragger = dragger;
682     if (dragger->isSelected()) {
683         GrDragger* d_add;
684         while ( true )
685         {
686             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
687             if ( d_add && g_list_find(drag->selected, d_add) ) {
688                 lowest_i = lowest_i - 1;
689                 *moving = g_slist_prepend(*moving, d_add);
690                 lowest_dragger = d_add;
691             } else {
692                 break;
693             }
694         }
696         while ( true )
697         {
698             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
699             if ( d_add && g_list_find(drag->selected, d_add) ) {
700                 highest_i = highest_i + 1;
701                 *moving = g_slist_append(*moving, d_add);
702                 highest_dragger = d_add;
703             } else {
704                 break;
705             }
706         }
707     }
709     if ( SP_IS_LINEARGRADIENT(server) ) {
710         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
711         GrDragger *d_temp;
712         if (lowest_i == 1) {
713             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
714         } else {
715             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
716         }
717         if (d_temp) 
718             *begin = d_temp->point;
720         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
721         if (d_temp == NULL) {
722             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
723         }
724         if (d_temp) 
725             *end = d_temp->point;
726     } else if ( SP_IS_RADIALGRADIENT(server) ) {
727         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
728         GrDragger *d_temp;
729         if (lowest_i == 1) {
730             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
731         } else {
732             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
733         }
734         if (d_temp) 
735             *begin = d_temp->point;
737         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
738         if (d_temp == NULL) {
739             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
740         }
741         if (d_temp) 
742             *end = d_temp->point;
743     }
745     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
746     *high_lim = dragger->point - (highest_dragger->point - *end);
751 /**
752 Called when a midpoint knot is dragged.
753 */
754 static void
755 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
757     GrDragger *dragger = (GrDragger *) data;
758     GrDrag *drag = dragger->parent;
759     // a midpoint dragger can (logically) only contain one GrDraggable
760     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
762     // FIXME: take from prefs
763     double snap_fraction = 0.1;
765     NR::Point p = *ppointer;
766     NR::Point begin(0,0), end(0,0);
767     NR::Point low_lim(0,0), high_lim(0,0);
769     SPObject *server = draggable->getServer();
771     GSList *moving = NULL;
772     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
774     if (state & GDK_CONTROL_MASK) {
775         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
776     } else {
777         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
778     }
779     NR::Point displacement = p - dragger->point;
781     for (GSList const* i = moving; i != NULL; i = i->next) {
782         GrDragger *drg = (GrDragger*) i->data;
783         SPKnot *drgknot = drg->knot;
784         NR::Point this_move = displacement;
785         if (state & GDK_MOD1_MASK) {
786             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
787             double alpha = 1.5;
788             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
789                 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
790                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
791             } else { // drg is on the begin side from dragger
792                 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
793                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
794             }
795         }
796         drg->point += this_move;
797         sp_knot_moveto (drgknot, & drg->point);
798         drg->fireDraggables (false);
799         drg->updateDependencies(false);
800     }
802     g_slist_free(moving);
804     drag->keep_selection = dragger->isSelected();
809 static void
810 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
812     GrDragger *dragger = (GrDragger *) data;
814     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
817 /**
818 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
819 */
820 static void
821 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
823     GrDragger *dragger = (GrDragger *) data;
825     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
827     dragger->point_original = dragger->point = knot->pos;
829     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
830         dragger->fireDraggables (true, true);
831     } else {
832         dragger->fireDraggables (true);
833     }
835     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
836         GrDragger *d = (GrDragger *) i->data;
837         if (d == dragger)
838             continue;
839         d->fireDraggables (true);
840     }
842     // make this dragger selected
843     if (!dragger->parent->keep_selection) {
844         dragger->parent->setSelected (dragger);
845     }
846     dragger->parent->keep_selection = false;
848     dragger->updateDependencies(true);
850     // we did an undoable action
851     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
852                       _("Move gradient handle"));
855 /**
856 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
857 state of the keyboard keys
858 */
859 static void
860 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
862     GrDragger *dragger = (GrDragger *) data;
863     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
864     if (!draggable) return;
866     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
867     // delete this knot from vector
868         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
869         gradient = sp_gradient_get_vector (gradient, false);
870         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
871                 SPStop *stop = NULL;
872                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
873                 case POINT_LG_BEGIN:
874                 case POINT_RG_CENTER:
875                     stop = sp_first_stop(gradient);
876                         {
877                             SPStop *next = sp_next_stop (stop);
878                                 if (next) {
879                                         next->offset = 0;
880                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
881                                 }
882                         }
883                     break;
884                 case POINT_LG_END:
885                 case POINT_RG_R1:
886                 case POINT_RG_R2:
887                     stop = sp_last_stop(gradient);
888                     {
889                             SPStop *prev = sp_prev_stop (stop, gradient);
890                             if (prev) {
891                                     prev->offset = 1;
892                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
893                             }
894                         }
895                     break;
896                 case POINT_LG_MID:
897                 case POINT_RG_MID1:
898                 case POINT_RG_MID2:
899                     stop = sp_get_stop_i(gradient, draggable->point_i);
900                     break;
901                 }
903                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
904                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
905                                   _("Delete gradient stop"));
906         }
907     } else {
908     // select the dragger
909         dragger->point_original = dragger->point;
911         if ( state & GDK_SHIFT_MASK ) {
912             dragger->parent->setSelected (dragger, true, false);
913         } else {
914             dragger->parent->setSelected (dragger);
915         }
916     }
919 /**
920 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
921 */
922 static void
923 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
925     GrDragger *dragger = (GrDragger *) data;
927     dragger->point_original = dragger->point;
929     if (dragger->draggables == NULL)
930         return;
932     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
933     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
936 /**
937 Act upon all draggables of the dragger, setting them to the dragger's point
938 */
939 void
940 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
942     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
943         GrDraggable *draggable = (GrDraggable *) i->data;
945         // set local_change flag so that selection_changed callback does not regenerate draggers
946         this->parent->local_change = true;
948         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
949         // to the center, unless it's the first update upon merge when we must snap it to the point
950         if (merging_focus ||
951             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
952         {
953             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
954         }
955     }
958 /**
959 Checks if the dragger has a draggable with this point_type
960  */
961 bool
962 GrDragger::isA (gint point_type)
964     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
965         GrDraggable *draggable = (GrDraggable *) i->data;
966         if (draggable->point_type == point_type) {
967             return true;
968         }
969     }
970     return false;
973 /**
974 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
975  */
976 bool
977 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
979     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
980         GrDraggable *draggable = (GrDraggable *) i->data;
981         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
982             return true;
983         }
984     }
985     return false;
988 bool
989 GrDraggable::mayMerge (GrDraggable *da2)
991     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
992         // we must not merge the points of the same gradient!
993         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
994               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
995             // except that we can snap center and focus together
996             return false;
997         }
998     }
999     // disable merging of midpoints.
1000     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1001          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1002          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1003         return false;
1005     return true;
1008 bool
1009 GrDragger::mayMerge (GrDragger *other)
1011     if (this == other)
1012         return false;
1014     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1015         GrDraggable *da1 = (GrDraggable *) i->data;
1016         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1017             GrDraggable *da2 = (GrDraggable *) j->data;
1018             if (!da1->mayMerge(da2))
1019                 return false;
1020         }
1021     }
1022     return true;
1025 bool
1026 GrDragger::mayMerge (GrDraggable *da2)
1028     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1029         GrDraggable *da1 = (GrDraggable *) i->data;
1030         if (!da1->mayMerge(da2))
1031             return false;
1032     }
1033     return true;
1036 /**
1037 Updates the statusbar tip of the dragger knot, based on its draggables
1038  */
1039 void
1040 GrDragger::updateTip ()
1042         if (this->knot && this->knot->tip) {
1043                 g_free (this->knot->tip);
1044                 this->knot->tip = NULL;
1045         }
1047     if (g_slist_length (this->draggables) == 1) {
1048         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1049         char *item_desc = sp_item_description(draggable->item);
1050         switch (draggable->point_type) {
1051             case POINT_LG_MID:
1052             case POINT_RG_MID1:
1053             case POINT_RG_MID2:
1054                 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"),
1055                                                    _(gr_knot_descr[draggable->point_type]),
1056                                                    draggable->point_i,
1057                                                    item_desc,
1058                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1059                 break;
1061             default:
1062                 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"),
1063                                                    _(gr_knot_descr[draggable->point_type]),
1064                                                    item_desc,
1065                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1066                 break;
1067         }
1068         g_free(item_desc);
1069     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1070         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1071     } else {
1072         int length = g_slist_length (this->draggables);
1073         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1074                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1075                                                     length),
1076                                            length);
1077     }
1080 /**
1081 Adds a draggable to the dragger
1082  */
1083 void
1084 GrDragger::updateKnotShape ()
1086     if (!draggables)
1087         return;
1088     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1089     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1092 /**
1093 Adds a draggable to the dragger
1094  */
1095 void
1096 GrDragger::addDraggable (GrDraggable *draggable)
1098     this->draggables = g_slist_prepend (this->draggables, draggable);
1100     this->updateTip();
1104 /**
1105 Moves this dragger to the point of the given draggable, acting upon all other draggables
1106  */
1107 void
1108 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1110     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1111     this->point_original = this->point;
1113     sp_knot_moveto (this->knot, &(this->point));
1115     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1116         GrDraggable *da = (GrDraggable *) i->data;
1117         if ( (da->item == item) && 
1118              (point_type == -1 || da->point_type == point_type) &&
1119              (point_i == -1 || da->point_i == point_i) &&
1120              (da->fill_or_stroke == fill_or_stroke) ) {
1121             continue;
1122         }
1123         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1124     }
1125     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1129 /**
1130 Moves all midstop draggables that depend on this one
1131  */
1132 void
1133 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1134     SPObject *server = draggable->getServer();
1135     if (!server) 
1136         return;
1137     guint num = SP_GRADIENT(server)->vector.stops.size();
1138     if (num <= 2) return;
1140     if ( SP_IS_LINEARGRADIENT(server) ) {
1141         for ( guint i = 1; i < num - 1; i++ ) {
1142             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1143         }
1144     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1145         for ( guint i = 1; i < num - 1; i++ ) {
1146             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1147             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1148         }
1149     }
1153 /**
1154 Moves all draggables that depend on this one
1155  */
1156 void
1157 GrDragger::updateDependencies (bool write_repr)
1159     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1160         GrDraggable *draggable = (GrDraggable *) i->data;
1161         switch (draggable->point_type) {
1162             case POINT_LG_BEGIN:
1163                 {
1164                     // the end point is dependent only when dragging with ctrl+shift
1165                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1167                     this->updateMidstopDependencies (draggable, write_repr);
1168                 }
1169                 break;
1170             case POINT_LG_END:
1171                 {
1172                     // the begin point is dependent only when dragging with ctrl+shift
1173                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1175                     this->updateMidstopDependencies (draggable, write_repr);
1176                 }
1177                 break;
1178             case POINT_LG_MID:
1179                 // no other nodes depend on mid points.
1180                 break;
1181             case POINT_RG_R2:
1182                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1183                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1184                 this->updateMidstopDependencies (draggable, write_repr);
1185                 break;
1186             case POINT_RG_R1:
1187                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1188                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1189                 this->updateMidstopDependencies (draggable, write_repr);
1190                 break;
1191             case POINT_RG_CENTER:
1192                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1193                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1194                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1195                 this->updateMidstopDependencies (draggable, write_repr);
1196                 break;
1197             case POINT_RG_FOCUS:
1198                 // nothing can depend on that
1199                 break;
1200             case POINT_RG_MID1:
1201                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1202                 break;
1203             case POINT_RG_MID2:
1204                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1205                 break;
1206             default:
1207                 break;
1208         }
1209     }
1214 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1216     this->draggables = NULL;
1218     this->parent = parent;
1220     this->point = p;
1221     this->point_original = p;
1223     // create the knot
1224     this->knot = sp_knot_new (parent->desktop, NULL);
1225     this->knot->setMode(SP_KNOT_MODE_XOR);
1226     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1227     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1228     sp_knot_update_ctrl(this->knot);
1230     // move knot to the given point
1231     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1232     sp_knot_show (this->knot);
1234     // connect knot's signals
1235     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1236                        // luckily, midstops never snap to other nodes so are never unsnapped...
1237          && ( (draggable->point_type == POINT_LG_MID)
1238               || (draggable->point_type == POINT_RG_MID1)
1239               || (draggable->point_type == POINT_RG_MID2) ) )
1240     {
1241         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1242     } else {
1243         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1244     }
1245     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1246     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1247     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1248     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1250     // add the initial draggable
1251     if (draggable)
1252         this->addDraggable (draggable);
1253     updateKnotShape();
1256 GrDragger::~GrDragger ()
1258     // unselect if it was selected
1259     this->parent->setDeselected(this);
1261     // disconnect signals
1262     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1263     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1264     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1265     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1266     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1268     /* unref should call destroy */
1269     g_object_unref (G_OBJECT (this->knot));
1271     // delete all draggables
1272     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1273         delete ((GrDraggable *) i->data);
1274     }
1275     g_slist_free (this->draggables);
1276     this->draggables = NULL;
1279 /**
1280 Select the dragger which has the given draggable.
1281 */
1282 GrDragger *
1283 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1285     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1286         GrDragger *dragger = (GrDragger *) i->data;
1287         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1288             GrDraggable *da2 = (GrDraggable *) j->data;
1289             if ( (da2->item == item) && 
1290                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1291                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1292                  (da2->fill_or_stroke == fill_or_stroke)) {
1293                 return (dragger);
1294             }
1295         }
1296     }
1297     return NULL;
1301 void
1302 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1304     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1305     if (d && d !=  this) {
1306         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1307     }
1311 /**
1312   Draw this dragger as selected
1313 */
1314 void
1315 GrDragger::select()
1317     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1318     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1321 /**
1322   Draw this dragger as normal (deselected)
1323 */
1324 void
1325 GrDragger::deselect()
1327     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1328     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1331 bool
1332 GrDragger::isSelected()
1334     return g_list_find (parent->selected, this);
1337 /**
1338 \brief Deselect all stops/draggers (private)
1339 */
1340 void
1341 GrDrag::deselect_all()
1343     while (selected) {
1344         ( (GrDragger*) selected->data)->deselect();
1345         selected = g_list_remove(selected, selected->data);
1346     }
1349 /**
1350 \brief Deselect all stops/draggers (public; emits signal)
1351 */
1352 void
1353 GrDrag::deselectAll()
1355     deselect_all();
1356     this->desktop->emitToolSubselectionChanged(NULL);
1359 /**
1360 \brief Select all stops/draggers
1361 */
1362 void
1363 GrDrag::selectAll()
1365     for (GList *l = this->draggers; l != NULL; l = l->next) {
1366         GrDragger *d = ((GrDragger *) l->data);
1367         setSelected (d, true, true);
1368     }
1371 /**
1372 \brief Select all stops/draggers that match the coords
1373 */
1374 void 
1375 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1377     for (GList *l = this->draggers; l != NULL; l = l->next) {
1378         GrDragger *d = ((GrDragger *) l->data);
1379         for (guint k = 0; k < coords.size(); k++) {
1380             if (NR::L2 (d->point - coords[k]) < 1e-4) {
1381                 setSelected (d, true, true);
1382             }
1383         }
1384     }
1388 /**
1389 \brief Select all stops/draggers that fall within the rect
1390 */
1391 void 
1392 GrDrag::selectRect(NR::Rect const &r)
1394     for (GList *l = this->draggers; l != NULL; l = l->next) {
1395         GrDragger *d = ((GrDragger *) l->data);
1396         if (r.contains(d->point)) {
1397            setSelected (d, true, true);
1398         }
1399     }
1402 /**
1403 \brief Select a dragger
1404 \param dragger       The dragger to select
1405 \param add_to_selection   If true, add to selection, otherwise deselect others
1406 \param override      If true, always select this node, otherwise toggle selected status
1407 */
1408 void
1409 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1411     GrDragger *seldragger = NULL;
1413     if (add_to_selection) {
1414         if (!dragger) return;
1415         if (override) {
1416             if (!g_list_find(selected, dragger)) {
1417                 selected = g_list_prepend(selected, dragger);
1418             }
1419             dragger->select();
1420             seldragger = dragger;
1421         } else { // toggle
1422             if (g_list_find(selected, dragger)) {
1423                 selected = g_list_remove(selected, dragger);
1424                 dragger->deselect();
1425                 if (selected) {
1426                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1427                 }
1428             } else {
1429                 selected = g_list_prepend(selected, dragger);
1430                 dragger->select();
1431                 seldragger = dragger;
1432             }
1433         }
1434     } else {
1435         deselect_all();
1436         if (dragger) {
1437             selected = g_list_prepend(selected, dragger);
1438             dragger->select();
1439             seldragger = dragger;
1440         }
1441     }
1442     if (seldragger) {
1443         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1444     }
1447 /**
1448 \brief Deselect a dragger
1449 \param dragger       The dragger to deselect
1450 */
1451 void
1452 GrDrag::setDeselected (GrDragger *dragger)
1454     if (g_list_find(selected, dragger)) {
1455         selected = g_list_remove(selected, dragger);
1456         dragger->deselect();
1457     }
1458     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1463 /**
1464 Create a line from p1 to p2 and add it to the lines list
1465  */
1466 void
1467 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1469     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1470                                                             SP_TYPE_CTRLLINE, NULL);
1471     SP_CTRLLINE(line)->item = item;
1472     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1473     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1474         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1475     sp_canvas_item_show (line);
1476     this->lines = g_slist_append (this->lines, line);
1479 /**
1480 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1481 new dragger and add it to draggers list
1482  */
1483 void
1484 GrDrag::addDragger (GrDraggable *draggable)
1486     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1488     for (GList *i = this->draggers; i != NULL; i = i->next) {
1489         GrDragger *dragger = (GrDragger *) i->data;
1490         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1491             // distance is small, merge this draggable into dragger, no need to create new dragger
1492             dragger->addDraggable (draggable);
1493             dragger->updateKnotShape();
1494             return;
1495         }
1496     }
1498     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1499     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1500     this->draggers = g_list_append (this->draggers, new_dragger);
1503 /**
1504 Add draggers for the radial gradient rg on item
1505 */
1506 void
1507 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1509     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1510     guint num = rg->vector.stops.size();
1511     if (num > 2) {
1512         for ( guint i = 1; i < num - 1; i++ ) {
1513             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1514         }
1515     }
1516     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1517     if (num > 2) {
1518         for ( guint i = 1; i < num - 1; i++ ) {
1519             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1520         }
1521     }
1522     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1523     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1526 /**
1527 Add draggers for the linear gradient lg on item
1528 */
1529 void
1530 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1532     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1533     guint num = lg->vector.stops.size();
1534     if (num > 2) {
1535         for ( guint i = 1; i < num - 1; i++ ) {
1536             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1537         }
1538     }
1539     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1542 /**
1543 Artificially grab the knot of this dragger; used by the gradient context
1544 */
1545 void
1546 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1548     if (dragger) {
1549         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1550     }
1553 /**
1554 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1555 */
1556 void
1557 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1559     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1560     if (dragger) {
1561         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1562     }
1565 /**
1566 Regenerates the draggers list from the current selection; is called when selection is changed or
1567 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1568 */
1569 void
1570 GrDrag::updateDraggers ()
1572     while (selected) {
1573         selected = g_list_remove(selected, selected->data);
1574     }
1575     // delete old draggers
1576     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1577         delete ((GrDragger *) i->data);
1578     }
1579     g_list_free (this->draggers);
1580     this->draggers = NULL;
1582     g_return_if_fail (this->selection != NULL);
1584     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1586         SPItem *item = SP_ITEM(i->data);
1587         SPStyle *style = SP_OBJECT_STYLE (item);
1589         if (style && (style->fill.isPaintserver())) {
1590             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1591             if (SP_IS_LINEARGRADIENT (server)) {
1592                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1593             } else if (SP_IS_RADIALGRADIENT (server)) {
1594                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1595             }
1596         }
1598         if (style && (style->stroke.isPaintserver())) {
1599             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1600             if (SP_IS_LINEARGRADIENT (server)) {
1601                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1602             } else if (SP_IS_RADIALGRADIENT (server)) {
1603                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1604             }
1605         }
1606     }
1609 /**
1610 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1611 lines are always in sync with the actual gradient
1612 */
1613 void
1614 GrDrag::updateLines ()
1616     // delete old lines
1617     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1618         gtk_object_destroy( GTK_OBJECT (i->data));
1619     }
1620     g_slist_free (this->lines);
1621     this->lines = NULL;
1623     g_return_if_fail (this->selection != NULL);
1625     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1627         SPItem *item = SP_ITEM(i->data);
1629         SPStyle *style = SP_OBJECT_STYLE (item);
1631         if (style && (style->fill.isPaintserver())) {
1632             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1633             if (SP_IS_LINEARGRADIENT (server)) {
1634                 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);
1635             } else if (SP_IS_RADIALGRADIENT (server)) {
1636                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1637                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1638                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1639             }
1640         }
1642         if (style && (style->stroke.isPaintserver())) {
1643             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1644             if (SP_IS_LINEARGRADIENT (server)) {
1645                 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);
1646             } else if (SP_IS_RADIALGRADIENT (server)) {
1647                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1648                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1649                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1650             }
1651         }
1652     }
1655 /**
1656 Regenerates the levels list from the current selection
1657 */
1658 void
1659 GrDrag::updateLevels ()
1661     hor_levels.clear();
1662     vert_levels.clear();
1664     g_return_if_fail (this->selection != NULL);
1666     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1667         SPItem *item = SP_ITEM(i->data);
1668         NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1669         if (rect) {
1670             // Remember the edges of the bbox and the center axis
1671             hor_levels.push_back(rect->min()[NR::Y]);
1672             hor_levels.push_back(rect->max()[NR::Y]);
1673             hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1674             vert_levels.push_back(rect->min()[NR::X]);
1675             vert_levels.push_back(rect->max()[NR::X]);
1676             vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1677         }
1678     }
1681 void
1682 GrDrag::selected_reverse_vector ()
1684     if (selected == NULL)
1685         return;
1687     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1688         GrDraggable *draggable = (GrDraggable *) i->data;
1690         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1691     }
1694 void
1695 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1697     selected_move (x, y, false, scale_radial);
1700 void
1701 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1703     if (selected == NULL)
1704         return;
1706     bool did = false; 
1708     for (GList *i = selected; i != NULL; i = i->next) {
1709         GrDragger *d = (GrDragger *) i->data;
1711         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1712             // if this is an endpoint,
1714             // Moving an rg center moves its focus and radii as well.
1715             // therefore, if this is a focus or radius and if selection
1716             // contains the center as well, do not move this one
1717             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) || 
1718                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1719                 bool skip_radius_with_center = false;
1720                 for (GList *di = selected; di != NULL; di = di->next) {
1721                     GrDragger *d_new = (GrDragger *) di->data;
1722                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1723                                     POINT_RG_CENTER,
1724                                     0,
1725                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1726                         // FIXME: here we take into account only the first draggable!
1727                         skip_radius_with_center = true;
1728                     }
1729                 }
1730                 if (skip_radius_with_center)
1731                     continue;
1732             }
1734             did = true;
1735             d->point += NR::Point (x, y);
1736             d->point_original = d->point;
1737             sp_knot_moveto (d->knot, &(d->point));
1739             d->fireDraggables (write_repr, scale_radial);
1741             d->updateDependencies(write_repr);
1742         }
1743     }
1745     if (write_repr && did) {
1746         // we did an undoable action
1747         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1748                                 _("Move gradient handle(s)"));
1749         return;
1750     }
1752     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1754         GrDragger *dragger = (GrDragger *) selected->data;
1755         // a midpoint dragger can (logically) only contain one GrDraggable
1756         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1758         NR::Point begin(0,0), end(0,0);
1759         NR::Point low_lim(0,0), high_lim(0,0);
1761         SPObject *server = draggable->getServer();
1762         GSList *moving = NULL;
1763         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1765         NR::Point p(x, y);
1766         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1767         NR::Point displacement = p - dragger->point;
1769         for (GSList const* i = moving; i != NULL; i = i->next) {
1770             GrDragger *drg = (GrDragger*) i->data;
1771             SPKnot *drgknot = drg->knot;
1772             drg->point += displacement;
1773             sp_knot_moveto (drgknot, & drg->point);
1774             drg->fireDraggables (true);
1775             drg->updateDependencies(true);
1776             did = true;
1777         }
1779         g_slist_free(moving);
1781         if (write_repr && did) {
1782             // we did an undoable action
1783             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1784                                     _("Move gradient mid stop(s)"));
1785         }
1786     }
1789 void
1790 GrDrag::selected_move_screen (double x, double y)
1792     gdouble zoom = desktop->current_zoom();
1793     gdouble zx = x / zoom;
1794     gdouble zy = y / zoom;
1796     selected_move (zx, zy);
1799 /**
1800 Select the knot next to the last selected one and deselect all other selected.
1801 */
1802 GrDragger *
1803 GrDrag::select_next ()
1805     GrDragger *d = NULL;
1806     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1807         if (draggers) 
1808             d = (GrDragger *) draggers->data;
1809     } else {
1810         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1811     }
1812     if (d)
1813         setSelected (d);
1814     return d;
1817 /**
1818 Select the knot previous from the last selected one and deselect all other selected.
1819 */
1820 GrDragger *
1821 GrDrag::select_prev ()
1823     GrDragger *d = NULL;
1824     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1825         if (draggers)
1826             d = (GrDragger *) g_list_last (draggers)->data;
1827     } else {
1828         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1829     }
1830     if (d)
1831         setSelected (d);
1832     return d;
1836 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1837 void
1838 GrDrag::deleteSelected (bool just_one)
1840     if (!selected) return;
1842     SPDocument *document = false;
1844     struct StructStopInfo {
1845         SPStop * spstop;
1846         GrDraggable * draggable;
1847         SPGradient * gradient;
1848         SPGradient * vector;
1849     };
1851     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1852     GSList *endstoplist = NULL;  // list of stops that must be deleted
1853     while (selected) {
1854         GrDragger *dragger = (GrDragger*) selected->data;
1855         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1856             GrDraggable *draggable = (GrDraggable*) drgble->data;
1857             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1858             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1860             switch (draggable->point_type) {
1861                 case POINT_LG_MID:
1862                 case POINT_RG_MID1:
1863                 case POINT_RG_MID2:
1864                     {
1865                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1866                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1867                         bool present = false;
1868                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1869                             if ( (SPStop*)l->data == stop ) {
1870                                 present = true;
1871                                 break; // no need to search further.
1872                             }
1873                         }
1874                         if (!present)
1875                             midstoplist = g_slist_append(midstoplist, stop);
1876                     }
1877                     break;
1878                 case POINT_LG_BEGIN:
1879                 case POINT_LG_END:
1880                 case POINT_RG_CENTER:
1881                 case POINT_RG_R1:
1882                 case POINT_RG_R2:
1883                     {
1884                         SPStop *stop = NULL;
1885                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1886                             stop = sp_first_stop(vector);
1887                         } else {
1888                             stop = sp_last_stop(vector);
1889                         }
1890                         if (stop) {
1891                             StructStopInfo *stopinfo = new StructStopInfo;
1892                             stopinfo->spstop = stop;
1893                             stopinfo->draggable = draggable;
1894                             stopinfo->gradient = gradient;
1895                             stopinfo->vector = vector;
1896                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1897                             bool present = false;
1898                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1899                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1900                                     present = true;
1901                                     break; // no need to search further.
1902                                 }
1903                             }
1904                             if (!present)
1905                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1906                         }
1907                     }
1908                     break;
1909                 default:
1910                     break;
1911             }
1912         }
1913         selected = g_list_remove(selected, dragger);
1914         if ( just_one ) break; // iterate once if just_one is set.
1915     }
1916     while (midstoplist) {
1917         SPStop *stop = (SPStop*) midstoplist->data;
1918         document = SP_OBJECT_DOCUMENT (stop);
1919         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1920         parent->removeChild(SP_OBJECT_REPR(stop));
1921         midstoplist = g_slist_remove(midstoplist, stop);
1922     }
1923     while (endstoplist) {
1924         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1925         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1927         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1928         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1929         // manually count the children, don't know if there already exists a function for this...
1930         int len = 0;
1931         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1932               child != NULL ;
1933               child = SP_OBJECT_NEXT(child) )
1934         {
1935             if ( SP_IS_STOP(child) )  len ++;
1936         }
1937         if (len > 2)
1938         {
1939             switch (stopinfo->draggable->point_type) {
1940                 case POINT_LG_BEGIN:
1941                     {
1942                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1944                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1945                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1946                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1947                         SPStop *stop = sp_first_stop(stopinfo->vector);
1948                         gdouble offset = stop->offset;
1949                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1950                         lg->x1.computed = newbegin[NR::X];
1951                         lg->y1.computed = newbegin[NR::Y];
1953                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1954                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1955                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1956                         stop->offset = 0;
1957                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1959                         // iterate through midstops to set new offset values such that they won't move on canvas.
1960                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1961                         stop = sp_next_stop(stop);
1962                         while ( stop != laststop ) {
1963                             stop->offset = (stop->offset - offset)/(1 - offset);
1964                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1965                             stop = sp_next_stop(stop);
1966                         }
1967                     }
1968                     break;
1969                 case POINT_LG_END:
1970                     {
1971                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1973                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1974                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1975                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1976                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1977                         gdouble offset = laststop->offset;
1978                         NR::Point newend = begin + offset * (oldend - begin);
1979                         lg->x2.computed = newend[NR::X];
1980                         lg->y2.computed = newend[NR::Y];
1982                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1983                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1984                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1985                         laststop->offset = 1;
1986                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1988                         // iterate through midstops to set new offset values such that they won't move on canvas.
1989                         SPStop *stop = sp_first_stop(stopinfo->vector);
1990                         stop = sp_next_stop(stop);
1991                         while ( stop != laststop ) {
1992                             stop->offset = stop->offset / offset;
1993                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1994                             stop = sp_next_stop(stop);
1995                         }
1996                     }
1997                     break;
1998                 case POINT_RG_CENTER:
1999                     {
2000                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2001                         if (newfirst) {
2002                             newfirst->offset = 0;
2003                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2004                         }
2005                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2006                     }
2007                     break;
2008                 case POINT_RG_R1:
2009                 case POINT_RG_R2:
2010                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2012                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2013                         double oldradius = rg->r.computed;
2014                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2015                         gdouble offset = laststop->offset;
2016                         double newradius = offset * oldradius;
2017                         rg->r.computed = newradius;
2019                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2020                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2021                         laststop->offset = 1;
2022                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2024                         // iterate through midstops to set new offset values such that they won't move on canvas.
2025                         SPStop *stop = sp_first_stop(stopinfo->vector);
2026                         stop = sp_next_stop(stop);
2027                         while ( stop != laststop ) {
2028                             stop->offset = stop->offset / offset;
2029                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2030                             stop = sp_next_stop(stop);
2031                         }
2032                         break;
2033             }
2034         }
2035         else
2036         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2037             SPCSSAttr *css = sp_repr_css_attr_new ();
2039             // stopinfo->spstop is the selected stop
2040             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2041             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2042                 unselectedrepr = unselectedrepr->next();
2043             }
2045             if (unselectedrepr == NULL) {
2046                 if (stopinfo->draggable->fill_or_stroke) {
2047                     sp_repr_css_unset_property (css, "fill");
2048                 } else {
2049                     sp_repr_css_unset_property (css, "stroke");
2050                 }
2051             } else {
2052                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2053                 if (stopinfo->draggable->fill_or_stroke) {
2054                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2055                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2056                 } else {
2057                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2058                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2059                 }
2060                 sp_repr_css_attr_unref (stopcss);
2061             }
2062             
2063             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2064             sp_repr_css_attr_unref (css);
2065         }
2067         endstoplist = g_slist_remove(endstoplist, stopinfo);
2068         delete stopinfo;
2069     }
2071     if (document) {
2072         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2073     }
2077 /*
2078   Local Variables:
2079   mode:c++
2080   c-file-style:"stroustrup"
2081   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2082   indent-tabs-mode:nil
2083   fill-column:99
2084   End:
2085 */
2086 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :