Code

fix Launchpad bug 593023: crash in constrained snap due to not calling setup() before...
[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  *   Jon A. Cruz <jon@joncruz.org>
10  *
11  * Copyright (C) 2007 Johan Engelen
12  * Copyright (C) 2005,2010 Authors
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
21 #include <glibmm/i18n.h>
22 #include <cstring>
23 #include <string>
25 #include "desktop-handles.h"
26 #include "selection.h"
27 #include "desktop.h"
28 #include "desktop-style.h"
29 #include "document.h"
30 #include "display/sp-ctrlline.h"
31 #include "display/sp-canvas-util.h"
32 #include "xml/repr.h"
33 #include "svg/css-ostringstream.h"
34 #include "svg/svg.h"
35 #include "libnr/nr-point-fns.h"
36 #include "preferences.h"
37 #include "sp-item.h"
38 #include "style.h"
39 #include "knot.h"
40 #include "sp-linear-gradient.h"
41 #include "sp-radial-gradient.h"
42 #include "gradient-chemistry.h"
43 #include "gradient-drag.h"
44 #include "sp-stop.h"
45 #include "snap.h"
46 #include "sp-namedview.h"
47 #include "selection-chemistry.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") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
228             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
229             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
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 guint32 GrDrag::getColor()
254     if (!selected) return 0;
256     float cf[4];
257     cf[0] = cf[1] = cf[2] = cf[3] = 0;
259     int count = 0;
261     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
262         GrDragger *d = (GrDragger *) i->data;
263         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
264             GrDraggable *draggable = (GrDraggable *) j->data;
266             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
267             cf[0] += SP_RGBA32_R_F (c);
268             cf[1] += SP_RGBA32_G_F (c);
269             cf[2] += SP_RGBA32_B_F (c);
270             cf[3] += SP_RGBA32_A_F (c);
272             count ++;
273         }
274     }
276     if (count) {
277         cf[0] /= count;
278         cf[1] /= count;
279         cf[2] /= count;
280         cf[3] /= count;
281     }
283     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
286 SPStop *
287 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
289     gfloat offset; // type of SPStop.offset = gfloat
290     SPGradient *gradient;
291     bool fill_or_stroke = true;
292     bool r1_knot = false;
294     bool addknot = false;
295     do {
296         gradient = sp_item_gradient (item, fill_or_stroke);
297         if (SP_IS_LINEARGRADIENT(gradient)) {
298             Geom::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
299             Geom::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
301             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
302             double dist_screen = Geom::L2 (mouse_p - nearest);
303             if ( dist_screen < tolerance ) {
304                 // add the knot
305                 offset = get_offset_between_points(nearest, begin, end);
306                 addknot = true;
307                 break; // break out of the while loop: add only one knot
308             }
309         } else if (SP_IS_RADIALGRADIENT(gradient)) {
310             Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
311             Geom::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
312             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
313             double dist_screen = Geom::L2 (mouse_p - nearest);
314             if ( dist_screen < tolerance ) {
315                 offset = get_offset_between_points(nearest, begin, end);
316                 addknot = true;
317                 r1_knot = true;
318                 break; // break out of the while loop: add only one knot
319             }
321             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
322             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
323             dist_screen = Geom::L2 (mouse_p - nearest);
324             if ( dist_screen < tolerance ) {
325                 offset = get_offset_between_points(nearest, begin, end);
326                 addknot = true;
327                 r1_knot = false;
328                 break; // break out of the while loop: add only one knot
329             }
330         }
331         fill_or_stroke = !fill_or_stroke;
332     } while (!fill_or_stroke && !addknot) ;
334     if (addknot) {
335         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
336         SPStop* prev_stop = vector->getFirstStop();
337         SPStop* next_stop = prev_stop->getNextStop();
338         guint i = 1;
339         while ( (next_stop) && (next_stop->offset < offset) ) {
340             prev_stop = next_stop;
341             next_stop = next_stop->getNextStop();
342             i++;
343         }
344         if (!next_stop) {
345             // logical error: the endstop should have offset 1 and should always be more than this offset here
346             return NULL;
347         }
350         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
351         gradient->ensureVector();
352         updateDraggers();
354         return newstop;
355     }
357     return NULL;
361 bool
362 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
364     // first, see if we can drop onto one of the existing draggers
365     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
366         GrDragger *d = (GrDragger *) i->data;
368         if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
369            SPCSSAttr *stop = sp_repr_css_attr_new ();
370            sp_repr_css_set_property (stop, "stop-color", c);
371            sp_repr_css_set_property (stop, "stop-opacity", "1");
372            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
373                GrDraggable *draggable = (GrDraggable *) j->data;
374                local_change = true;
375                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
376            }
377            sp_repr_css_attr_unref(stop);
378            return true;
379         }
380     }
382     // now see if we're over line and create a new stop
383     bool over_line = false;
384     SPCtrlLine *line = NULL;
385     if (lines) {
386         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
387             line = (SPCtrlLine*) l->data;
388             Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
389             double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
390             if (line->item && dist_screen < 5) {
391                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
392                 if (stop) {
393                     SPCSSAttr *css = sp_repr_css_attr_new ();
394                     sp_repr_css_set_property (css, "stop-color", c);
395                     sp_repr_css_set_property (css, "stop-opacity", "1");
396                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
397                     return true;
398                 }
399             }
400         }
401     }
403     return false;
407 GrDrag::GrDrag(SPDesktop *desktop) {
409     this->desktop = desktop;
411     this->selection = sp_desktop_selection(desktop);
413     this->draggers = NULL;
414     this->lines = NULL;
415     this->selected = NULL;
417     this->hor_levels.clear();
418     this->vert_levels.clear();
420     this->local_change = false;
422     this->sel_changed_connection = this->selection->connectChanged(
423         sigc::bind (
424             sigc::ptr_fun(&gr_drag_sel_changed),
425             (gpointer)this )
427         );
428     this->sel_modified_connection = this->selection->connectModified(
429         sigc::bind(
430             sigc::ptr_fun(&gr_drag_sel_modified),
431             (gpointer)this )
432         );
434     this->style_set_connection = this->desktop->connectSetStyle(
435         sigc::bind(
436             sigc::ptr_fun(&gr_drag_style_set),
437             (gpointer)this )
438         );
440     this->style_query_connection = this->desktop->connectQueryStyle(
441         sigc::bind(
442             sigc::ptr_fun(&gr_drag_style_query),
443             (gpointer)this )
444         );
446     this->updateDraggers ();
447     this->updateLines ();
448     this->updateLevels ();
450     if (desktop->gr_item) {
451         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
452     }
455 GrDrag::~GrDrag()
457     this->sel_changed_connection.disconnect();
458     this->sel_modified_connection.disconnect();
459     this->style_set_connection.disconnect();
460     this->style_query_connection.disconnect();
462     if (this->selected) {
463         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
464         desktop->gr_item = draggable->item;
465         desktop->gr_point_type = draggable->point_type;
466         desktop->gr_point_i = draggable->point_i;
467         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
468     } else {
469         desktop->gr_item = NULL;
470         desktop->gr_point_type = 0;
471         desktop->gr_point_i = 0;
472         desktop->gr_fill_or_stroke = true;
473     }
475     deselect_all();
476     for (GList *l = this->draggers; l != NULL; l = l->next) {
477         delete ((GrDragger *) l->data);
478     }
479     g_list_free (this->draggers);
480     this->draggers = NULL;
481     this->selected = NULL;
483     for (GSList *l = this->lines; l != NULL; l = l->next) {
484         gtk_object_destroy( GTK_OBJECT (l->data));
485     }
486     g_slist_free (this->lines);
487     this->lines = NULL;
490 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
492     this->item = item;
493     this->point_type = point_type;
494     this->point_i = point_i;
495     this->fill_or_stroke = fill_or_stroke;
497     g_object_ref (G_OBJECT (this->item));
500 GrDraggable::~GrDraggable ()
502     g_object_unref (G_OBJECT (this->item));
506 SPObject *
507 GrDraggable::getServer ()
509     if (!item)
510         return NULL;
512     SPObject *server = NULL;
513     if (fill_or_stroke)
514         server = SP_OBJECT_STYLE_FILL_SERVER (item);
515     else
516         server = SP_OBJECT_STYLE_STROKE_SERVER (item);
518     return server;
521 static
522 boost::optional<Geom::Point>
523 get_snap_vector (Geom::Point p, Geom::Point o, double snap, double initial)
525     double r = L2 (p - o);
526     if (r < 1e-3) {
527         return boost::optional<Geom::Point>();
528     }
530     double angle = atan2 (p - o);
531     // snap angle to snaps increments, starting from initial:
532     double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
533     // calculate the new position and subtract p to get the vector:
534     return (o + r * Geom::Point(cos(a_snapped), sin(a_snapped)) - p);
537 static void
538 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
540     GrDragger *dragger = (GrDragger *) data;
541     GrDrag *drag = dragger->parent;
543     Geom::Point p = ppointer;
545     SPDesktop *desktop = dragger->parent->desktop;
546     SnapManager &m = desktop->namedview->snap_manager;
547     double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom();
549     if (state & GDK_SHIFT_MASK) {
550         // with Shift; unsnap if we carry more than one draggable
551         if (dragger->draggables && dragger->draggables->next) {
552             // create a new dragger
553             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
554             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
555             // relink to it all but the first draggable in the list
556             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
557                 GrDraggable *draggable = (GrDraggable *) i->data;
558                 dr_new->addDraggable (draggable);
559             }
560             dr_new->updateKnotShape();
561             g_slist_free (dragger->draggables->next);
562             dragger->draggables->next = NULL;
563             dragger->updateKnotShape();
564             dragger->updateTip();
565         }
566     } else if (!(state & GDK_CONTROL_MASK)) {
567         // without Shift or Ctrl; see if we need to snap to another dragger
568         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
569             GrDragger *d_new = (GrDragger *) di->data;
570             if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
572                 // Merge draggers:
573                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
574                     GrDraggable *draggable = (GrDraggable *) i->data;
575                     // copy draggable to d_new:
576                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
577                     d_new->addDraggable (da_new);
578                 }
580                 // unlink and delete this dragger
581                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
582                 delete dragger;
584                 // update the new merged dragger
585                 d_new->fireDraggables(true, false, true);
586                 d_new->parent->updateLines();
587                 d_new->parent->setSelected (d_new);
588                 d_new->updateKnotShape ();
589                 d_new->updateTip ();
590                 d_new->updateDependencies(true);
591                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
592                                   _("Merge gradient handles"));
593                 return;
594             }
595         }
596     }
598     m.setup(desktop);
599     if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
600         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
601         if (s.getSnapped()) {
602             p = s.getPoint();
603             sp_knot_moveto (knot, p);
604         }
605     } else if (state & GDK_CONTROL_MASK) {
606         SnappedConstraints sc;
607         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
608         unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
609         /* 0 means no snapping. */
611         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
612             GrDraggable *draggable = (GrDraggable *) i->data;
614             Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
616             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
617                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
618                     GrDragger *d_new = (GrDragger *) di->data;
619                     if (d_new == dragger)
620                         continue;
621                     if (d_new->isA (draggable->item,
622                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
623                                     draggable->fill_or_stroke)) {
624                         // found the other end of the linear gradient;
625                         if (state & GDK_SHIFT_MASK) {
626                             // moving linear around center
627                             Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
628                             dr_snap = center;
629                         } else {
630                             // moving linear around the other end
631                             dr_snap = d_new->point;
632                         }
633                     }
634                 }
635             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
636                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
637                     GrDragger *d_new = (GrDragger *) di->data;
638                     if (d_new == dragger)
639                         continue;
640                     if (d_new->isA (draggable->item,
641                                     POINT_RG_CENTER,
642                                     draggable->fill_or_stroke)) {
643                         // found the center of the radial gradient;
644                         dr_snap = d_new->point;
645                     }
646                 }
647             } else if (draggable->point_type == POINT_RG_CENTER) {
648                 // radial center snaps to hor/vert relative to its original position
649                 dr_snap = dragger->point_original;
650             }
652             boost::optional<Geom::Point> snap_vector;
653             if (dr_snap.isFinite()) {
654                 if (state & GDK_MOD1_MASK) {
655                     // with Alt, snap to the original angle and its perpendiculars
656                     snap_vector = get_snap_vector (p, dr_snap, M_PI/2, Geom::atan2 (dragger->point_original - dr_snap));
657                 } else {
658                     // with Ctrl, snap to M_PI/snaps
659                     snap_vector = get_snap_vector (p, dr_snap, M_PI/snaps, 0);
660                 }
661                 if (snap_vector) {
662                     Inkscape::Snapper::ConstraintLine cl(dr_snap, p + *snap_vector - dr_snap);
663                     Inkscape::SnappedPoint s = m.constrainedSnap(Inkscape::SnapCandidatePoint(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE), cl);
664                     if (s.getSnapped()) {
665                         s.setTransformation(s.getPoint() - p);
666                         sc.points.push_back(s);
667                     } else {
668                         Inkscape::SnappedPoint dummy(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE, 0, Inkscape::SNAPTARGET_CONSTRAINED_ANGLE, Geom::L2(*snap_vector), 10000, true, true, false);
669                         dummy.setTransformation(*snap_vector);
670                         sc.points.push_back(dummy);
671                     }
672                 }
673             }
674         }
676         Inkscape::SnappedPoint bsp = m.findBestSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE), sc, true); // snap indicator will be displayed if needed
678         if (bsp.getSnapped()) {
679             p += bsp.getTransformation();
680             sp_knot_moveto (knot, p);
681         }
682     }
684     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
685     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
687     if (drag->keep_selection) {
688         Geom::Point diff = p - dragger->point;
689         drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
690     } else {
691         dragger->point = p;
692         dragger->fireDraggables (false, scale_radial);
693         dragger->updateDependencies(false);
694     }
699 static void
700 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
703     GrDrag *drag = dragger->parent;
704     // a midpoint dragger can (logically) only contain one GrDraggable
705     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
707     // get begin and end points between which dragging is allowed:
708     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
709     *moving = g_slist_append(*moving, dragger);
711     guint lowest_i = draggable->point_i;
712     guint highest_i = draggable->point_i;
713     GrDragger *lowest_dragger = dragger;
714     GrDragger *highest_dragger = dragger;
715     if (dragger->isSelected()) {
716         GrDragger* d_add;
717         while ( true )
718         {
719             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
720             if ( d_add && g_list_find(drag->selected, d_add) ) {
721                 lowest_i = lowest_i - 1;
722                 *moving = g_slist_prepend(*moving, d_add);
723                 lowest_dragger = d_add;
724             } else {
725                 break;
726             }
727         }
729         while ( true )
730         {
731             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
732             if ( d_add && g_list_find(drag->selected, d_add) ) {
733                 highest_i = highest_i + 1;
734                 *moving = g_slist_append(*moving, d_add);
735                 highest_dragger = d_add;
736             } else {
737                 break;
738             }
739         }
740     }
742     if ( SP_IS_LINEARGRADIENT(server) ) {
743         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
744         GrDragger *d_temp;
745         if (lowest_i == 1) {
746             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
747         } else {
748             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
749         }
750         if (d_temp)
751             *begin = d_temp->point;
753         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
754         if (d_temp == NULL) {
755             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
756         }
757         if (d_temp)
758             *end = d_temp->point;
759     } else if ( SP_IS_RADIALGRADIENT(server) ) {
760         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
761         GrDragger *d_temp;
762         if (lowest_i == 1) {
763             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
764         } else {
765             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
766         }
767         if (d_temp)
768             *begin = d_temp->point;
770         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
771         if (d_temp == NULL) {
772             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
773         }
774         if (d_temp)
775             *end = d_temp->point;
776     }
778     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
779     *high_lim = dragger->point - (highest_dragger->point - *end);
784 /**
785 Called when a midpoint knot is dragged.
786 */
787 static void
788 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
790     GrDragger *dragger = (GrDragger *) data;
791     GrDrag *drag = dragger->parent;
792     // a midpoint dragger can (logically) only contain one GrDraggable
793     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
795     // FIXME: take from prefs
796     double snap_fraction = 0.1;
798     Geom::Point p = ppointer;
799     Geom::Point begin(0,0), end(0,0);
800     Geom::Point low_lim(0,0), high_lim(0,0);
802     SPObject *server = draggable->getServer();
804     GSList *moving = NULL;
805     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
807     if (state & GDK_CONTROL_MASK) {
808         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
809     } else {
810         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
811         if (!(state & GDK_SHIFT_MASK)) {
812             Inkscape::Snapper::ConstraintLine cl(low_lim, high_lim - low_lim);
813             SPDesktop *desktop = dragger->parent->desktop;
814             SnapManager &m = desktop->namedview->snap_manager;
815             m.setup(desktop);
816             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
817         }
818     }
819     Geom::Point displacement = p - dragger->point;
821     for (GSList const* i = moving; i != NULL; i = i->next) {
822         GrDragger *drg = (GrDragger*) i->data;
823         SPKnot *drgknot = drg->knot;
824         Geom::Point this_move = displacement;
825         if (state & GDK_MOD1_MASK) {
826             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
827             double alpha = 1.0;
828             if (Geom::L2(drg->point - dragger->point) + Geom::L2(drg->point - begin) - 1e-3 > Geom::L2(dragger->point - begin)) { // drg is on the end side from dragger
829                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
830                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
831             } else { // drg is on the begin side from dragger
832                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
833                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
834             }
835         }
836         drg->point += this_move;
837         sp_knot_moveto (drgknot, drg->point);
838         drg->fireDraggables (false);
839         drg->updateDependencies(false);
840     }
842     g_slist_free(moving);
844     drag->keep_selection = dragger->isSelected();
849 static void
850 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
852     GrDragger *dragger = (GrDragger *) data;
854     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
857 /**
858 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
859 */
860 static void
861 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
863     GrDragger *dragger = (GrDragger *) data;
865     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
867     dragger->point_original = dragger->point = knot->pos;
869     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
870         dragger->fireDraggables (true, true);
871     } else {
872         dragger->fireDraggables (true);
873     }
875     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
876         GrDragger *d = (GrDragger *) i->data;
877         if (d == dragger)
878             continue;
879         d->fireDraggables (true);
880     }
882     // make this dragger selected
883     if (!dragger->parent->keep_selection) {
884         dragger->parent->setSelected (dragger);
885     }
886     dragger->parent->keep_selection = false;
888     dragger->updateDependencies(true);
890     // we did an undoable action
891     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
892                       _("Move gradient handle"));
895 /**
896 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
897 state of the keyboard keys
898 */
899 static void
900 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
902     GrDragger *dragger = (GrDragger *) data;
903     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
904     if (!draggable) return;
906     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
907     // delete this knot from vector
908         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
909         gradient = gradient->getVector();
910         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
911             SPStop *stop = NULL;
912             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
913             case POINT_LG_BEGIN:
914             case POINT_RG_CENTER:
915                 stop = gradient->getFirstStop();
916                 {
917                     SPStop *next = stop->getNextStop();
918                     if (next) {
919                         next->offset = 0;
920                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
921                     }
922                 }
923                 break;
924             case POINT_LG_END:
925             case POINT_RG_R1:
926             case POINT_RG_R2:
927                 stop = sp_last_stop(gradient);
928                 {
929                     SPStop *prev = stop->getPrevStop();
930                     if (prev) {
931                         prev->offset = 1;
932                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
933                     }
934                 }
935                 break;
936             case POINT_LG_MID:
937             case POINT_RG_MID1:
938             case POINT_RG_MID2:
939                 stop = sp_get_stop_i(gradient, draggable->point_i);
940                 break;
941             }
943             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
944             sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
945                       _("Delete gradient stop"));
946         }
947     } else {
948     // select the dragger
949         dragger->point_original = dragger->point;
951         if ( state & GDK_SHIFT_MASK ) {
952             dragger->parent->setSelected (dragger, true, false);
953         } else {
954             dragger->parent->setSelected (dragger);
955         }
956     }
959 /**
960 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
961 */
962 static void
963 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
965     GrDragger *dragger = (GrDragger *) data;
967     dragger->point_original = dragger->point;
969     if (dragger->draggables == NULL)
970         return;
972     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
973     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
976 /**
977 Act upon all draggables of the dragger, setting them to the dragger's point
978 */
979 void
980 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
982     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
983         GrDraggable *draggable = (GrDraggable *) i->data;
985         // set local_change flag so that selection_changed callback does not regenerate draggers
986         this->parent->local_change = true;
988         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
989         // to the center, unless it's the first update upon merge when we must snap it to the point
990         if (merging_focus ||
991             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
992         {
993             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
994         }
995     }
998 /**
999 Checks if the dragger has a draggable with this point_type
1000  */
1001 bool
1002 GrDragger::isA (gint point_type)
1004     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1005         GrDraggable *draggable = (GrDraggable *) i->data;
1006         if (draggable->point_type == point_type) {
1007             return true;
1008         }
1009     }
1010     return false;
1013 /**
1014 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1015  */
1016 bool
1017 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1019     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1020         GrDraggable *draggable = (GrDraggable *) i->data;
1021         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1022             return true;
1023         }
1024     }
1025     return false;
1028 /**
1029 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1030  */
1031 bool
1032 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1034     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1035         GrDraggable *draggable = (GrDraggable *) i->data;
1036         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1037             return true;
1038         }
1039     }
1040     return false;
1043 bool
1044 GrDraggable::mayMerge (GrDraggable *da2)
1046     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1047         // we must not merge the points of the same gradient!
1048         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1049               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1050             // except that we can snap center and focus together
1051             return false;
1052         }
1053     }
1054     // disable merging of midpoints.
1055     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1056          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1057          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1058         return false;
1060     return true;
1063 bool
1064 GrDragger::mayMerge (GrDragger *other)
1066     if (this == other)
1067         return false;
1069     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1070         GrDraggable *da1 = (GrDraggable *) i->data;
1071         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1072             GrDraggable *da2 = (GrDraggable *) j->data;
1073             if (!da1->mayMerge(da2))
1074                 return false;
1075         }
1076     }
1077     return true;
1080 bool
1081 GrDragger::mayMerge (GrDraggable *da2)
1083     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1084         GrDraggable *da1 = (GrDraggable *) i->data;
1085         if (!da1->mayMerge(da2))
1086             return false;
1087     }
1088     return true;
1091 /**
1092 Updates the statusbar tip of the dragger knot, based on its draggables
1093  */
1094 void
1095 GrDragger::updateTip ()
1097     if (this->knot && this->knot->tip) {
1098         g_free (this->knot->tip);
1099         this->knot->tip = NULL;
1100     }
1102     if (g_slist_length (this->draggables) == 1) {
1103         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1104         char *item_desc = sp_item_description(draggable->item);
1105         switch (draggable->point_type) {
1106             case POINT_LG_MID:
1107             case POINT_RG_MID1:
1108             case POINT_RG_MID2:
1109                 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"),
1110                                                    _(gr_knot_descr[draggable->point_type]),
1111                                                    draggable->point_i,
1112                                                    item_desc,
1113                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1114                 break;
1116             default:
1117                 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"),
1118                                                    _(gr_knot_descr[draggable->point_type]),
1119                                                    item_desc,
1120                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1121                 break;
1122         }
1123         g_free(item_desc);
1124     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1125         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1126     } else {
1127         int length = g_slist_length (this->draggables);
1128         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1129                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1130                                                     length),
1131                                            length);
1132     }
1135 /**
1136 Adds a draggable to the dragger
1137  */
1138 void
1139 GrDragger::updateKnotShape ()
1141     if (!draggables)
1142         return;
1143     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1144     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1147 /**
1148 Adds a draggable to the dragger
1149  */
1150 void
1151 GrDragger::addDraggable (GrDraggable *draggable)
1153     this->draggables = g_slist_prepend (this->draggables, draggable);
1155     this->updateTip();
1159 /**
1160 Moves this dragger to the point of the given draggable, acting upon all other draggables
1161  */
1162 void
1163 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1165     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1166     if (!dr_first) return;
1168     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1169     this->point_original = this->point;
1171     sp_knot_moveto (this->knot, this->point);
1173     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1174         GrDraggable *da = (GrDraggable *) i->data;
1175         if ( (da->item == item) &&
1176              (point_type == -1 || da->point_type == point_type) &&
1177              (point_i == -1 || da->point_i == point_i) &&
1178              (da->fill_or_stroke == fill_or_stroke) ) {
1179             continue;
1180         }
1181         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1182     }
1183     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1187 /**
1188 Moves all midstop draggables that depend on this one
1189  */
1190 void
1191 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1192     SPObject *server = draggable->getServer();
1193     if (!server)
1194         return;
1195     guint num = SP_GRADIENT(server)->vector.stops.size();
1196     if (num <= 2) return;
1198     if ( SP_IS_LINEARGRADIENT(server) ) {
1199         for ( guint i = 1; i < num - 1; i++ ) {
1200             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1201         }
1202     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1203         for ( guint i = 1; i < num - 1; i++ ) {
1204             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1205             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1206         }
1207     }
1211 /**
1212 Moves all draggables that depend on this one
1213  */
1214 void
1215 GrDragger::updateDependencies (bool write_repr)
1217     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1218         GrDraggable *draggable = (GrDraggable *) i->data;
1219         switch (draggable->point_type) {
1220             case POINT_LG_BEGIN:
1221                 {
1222                     // the end point is dependent only when dragging with ctrl+shift
1223                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1225                     this->updateMidstopDependencies (draggable, write_repr);
1226                 }
1227                 break;
1228             case POINT_LG_END:
1229                 {
1230                     // the begin point is dependent only when dragging with ctrl+shift
1231                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1233                     this->updateMidstopDependencies (draggable, write_repr);
1234                 }
1235                 break;
1236             case POINT_LG_MID:
1237                 // no other nodes depend on mid points.
1238                 break;
1239             case POINT_RG_R2:
1240                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1241                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1242                 this->updateMidstopDependencies (draggable, write_repr);
1243                 break;
1244             case POINT_RG_R1:
1245                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1246                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1247                 this->updateMidstopDependencies (draggable, write_repr);
1248                 break;
1249             case POINT_RG_CENTER:
1250                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1251                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1252                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1253                 this->updateMidstopDependencies (draggable, write_repr);
1254                 break;
1255             case POINT_RG_FOCUS:
1256                 // nothing can depend on that
1257                 break;
1258             case POINT_RG_MID1:
1259                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1260                 break;
1261             case POINT_RG_MID2:
1262                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1263                 break;
1264             default:
1265                 break;
1266         }
1267     }
1272 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1273   : point(p),
1274     point_original(p)
1276     this->draggables = NULL;
1278     this->parent = parent;
1280     // create the knot
1281     this->knot = sp_knot_new (parent->desktop, NULL);
1282     this->knot->setMode(SP_KNOT_MODE_XOR);
1283     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1284     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1285     sp_knot_update_ctrl(this->knot);
1287     // move knot to the given point
1288     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1289     sp_knot_show (this->knot);
1291     // connect knot's signals
1292     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1293                        // luckily, midstops never snap to other nodes so are never unsnapped...
1294          && ( (draggable->point_type == POINT_LG_MID)
1295               || (draggable->point_type == POINT_RG_MID1)
1296               || (draggable->point_type == POINT_RG_MID2) ) )
1297     {
1298         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1299     } else {
1300         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1301     }
1302     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1303     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1304     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1305     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1307     // add the initial draggable
1308     if (draggable)
1309         this->addDraggable (draggable);
1310     updateKnotShape();
1313 GrDragger::~GrDragger ()
1315     // unselect if it was selected
1316     this->parent->setDeselected(this);
1318     // disconnect signals
1319     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1320     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1321     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1322     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1323     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1325     /* unref should call destroy */
1326     g_object_unref (G_OBJECT (this->knot));
1328     // delete all draggables
1329     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1330         delete ((GrDraggable *) i->data);
1331     }
1332     g_slist_free (this->draggables);
1333     this->draggables = NULL;
1336 /**
1337 Select the dragger which has the given draggable.
1338 */
1339 GrDragger *
1340 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1342     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1343         GrDragger *dragger = (GrDragger *) i->data;
1344         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1345             GrDraggable *da2 = (GrDraggable *) j->data;
1346             if ( (da2->item == item) &&
1347                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1348                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1349                  (da2->fill_or_stroke == fill_or_stroke)) {
1350                 return (dragger);
1351             }
1352         }
1353     }
1354     return NULL;
1358 void
1359 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1361     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1362     if (d && d !=  this) {
1363         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1364     }
1368 /**
1369   Draw this dragger as selected
1370 */
1371 void
1372 GrDragger::select()
1374     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1375     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1378 /**
1379   Draw this dragger as normal (deselected)
1380 */
1381 void
1382 GrDragger::deselect()
1384     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1385     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1388 bool
1389 GrDragger::isSelected()
1391     return g_list_find (parent->selected, this);
1394 /**
1395 \brief Deselect all stops/draggers (private)
1396 */
1397 void
1398 GrDrag::deselect_all()
1400     while (selected) {
1401         ( (GrDragger*) selected->data)->deselect();
1402         selected = g_list_remove(selected, selected->data);
1403     }
1406 /**
1407 \brief Deselect all stops/draggers (public; emits signal)
1408 */
1409 void
1410 GrDrag::deselectAll()
1412     deselect_all();
1413     this->desktop->emitToolSubselectionChanged(NULL);
1416 /**
1417 \brief Select all stops/draggers
1418 */
1419 void
1420 GrDrag::selectAll()
1422     for (GList *l = this->draggers; l != NULL; l = l->next) {
1423         GrDragger *d = ((GrDragger *) l->data);
1424         setSelected (d, true, true);
1425     }
1428 /**
1429 \brief Select all stops/draggers that match the coords
1430 */
1431 void
1432 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1434     for (GList *l = this->draggers; l != NULL; l = l->next) {
1435         GrDragger *d = ((GrDragger *) l->data);
1436         for (guint k = 0; k < coords.size(); k++) {
1437             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1438                 setSelected (d, true, true);
1439             }
1440         }
1441     }
1445 /**
1446 \brief Select all stops/draggers that fall within the rect
1447 */
1448 void
1449 GrDrag::selectRect(Geom::Rect const &r)
1451     for (GList *l = this->draggers; l != NULL; l = l->next) {
1452         GrDragger *d = ((GrDragger *) l->data);
1453         if (r.contains(d->point)) {
1454            setSelected (d, true, true);
1455         }
1456     }
1459 /**
1460 \brief Select a dragger
1461 \param dragger       The dragger to select
1462 \param add_to_selection   If true, add to selection, otherwise deselect others
1463 \param override      If true, always select this node, otherwise toggle selected status
1464 */
1465 void
1466 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1468     GrDragger *seldragger = NULL;
1470     if (add_to_selection) {
1471         if (!dragger) return;
1472         if (override) {
1473             if (!g_list_find(selected, dragger)) {
1474                 selected = g_list_prepend(selected, dragger);
1475             }
1476             dragger->select();
1477             seldragger = dragger;
1478         } else { // toggle
1479             if (g_list_find(selected, dragger)) {
1480                 selected = g_list_remove(selected, dragger);
1481                 dragger->deselect();
1482                 if (selected) {
1483                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1484                 }
1485             } else {
1486                 selected = g_list_prepend(selected, dragger);
1487                 dragger->select();
1488                 seldragger = dragger;
1489             }
1490         }
1491     } else {
1492         deselect_all();
1493         if (dragger) {
1494             selected = g_list_prepend(selected, dragger);
1495             dragger->select();
1496             seldragger = dragger;
1497         }
1498     }
1499     if (seldragger) {
1500         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1501     }
1504 /**
1505 \brief Deselect a dragger
1506 \param dragger       The dragger to deselect
1507 */
1508 void
1509 GrDrag::setDeselected (GrDragger *dragger)
1511     if (g_list_find(selected, dragger)) {
1512         selected = g_list_remove(selected, dragger);
1513         dragger->deselect();
1514     }
1515     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1520 /**
1521 Create a line from p1 to p2 and add it to the lines list
1522  */
1523 void
1524 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1526     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1527                                                             SP_TYPE_CTRLLINE, NULL);
1528     sp_canvas_item_move_to_z(line, 0);
1529     SP_CTRLLINE(line)->item = item;
1530     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1531     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1532         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1533     sp_canvas_item_show (line);
1534     this->lines = g_slist_append (this->lines, line);
1537 /**
1538 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1539 new dragger and add it to draggers list
1540  */
1541 void
1542 GrDrag::addDragger (GrDraggable *draggable)
1544     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1546     for (GList *i = this->draggers; i != NULL; i = i->next) {
1547         GrDragger *dragger = (GrDragger *) i->data;
1548         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1549             // distance is small, merge this draggable into dragger, no need to create new dragger
1550             dragger->addDraggable (draggable);
1551             dragger->updateKnotShape();
1552             return;
1553         }
1554     }
1556     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1557     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1558     this->draggers = g_list_append (this->draggers, new_dragger);
1561 /**
1562 Add draggers for the radial gradient rg on item
1563 */
1564 void
1565 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1567     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1568     guint num = rg->vector.stops.size();
1569     if (num > 2) {
1570         for ( guint i = 1; i < num - 1; i++ ) {
1571             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1572         }
1573     }
1574     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1575     if (num > 2) {
1576         for ( guint i = 1; i < num - 1; i++ ) {
1577             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1578         }
1579     }
1580     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1581     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1584 /**
1585 Add draggers for the linear gradient lg on item
1586 */
1587 void
1588 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1590     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1591     guint num = lg->vector.stops.size();
1592     if (num > 2) {
1593         for ( guint i = 1; i < num - 1; i++ ) {
1594             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1595         }
1596     }
1597     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1600 /**
1601 Artificially grab the knot of this dragger; used by the gradient context
1602 */
1603 void
1604 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1606     if (dragger) {
1607         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1608     }
1611 /**
1612 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1613 */
1614 void
1615 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1617     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1618     if (dragger) {
1619         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1620     }
1623 /**
1624 Regenerates the draggers list from the current selection; is called when selection is changed or
1625 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1626 */
1627 void
1628 GrDrag::updateDraggers ()
1630     while (selected) {
1631         selected = g_list_remove(selected, selected->data);
1632     }
1633     // delete old draggers
1634     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1635         delete ((GrDragger *) i->data);
1636     }
1637     g_list_free (this->draggers);
1638     this->draggers = NULL;
1640     g_return_if_fail (this->selection != NULL);
1642     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1644         SPItem *item = SP_ITEM(i->data);
1645         SPStyle *style = SP_OBJECT_STYLE (item);
1647         if (style && (style->fill.isPaintserver())) {
1648             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1649             if (SP_IS_LINEARGRADIENT (server)) {
1650                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1651             } else if (SP_IS_RADIALGRADIENT (server)) {
1652                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1653             }
1654         }
1656         if (style && (style->stroke.isPaintserver())) {
1657             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1658             if (SP_IS_LINEARGRADIENT (server)) {
1659                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1660             } else if (SP_IS_RADIALGRADIENT (server)) {
1661                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1662             }
1663         }
1664     }
1668 /**
1669  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1670  */
1672 bool
1673 GrDrag::mouseOver()
1675     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1676         GrDragger *d = (GrDragger *) i->data;
1677         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1678             return true;
1679         }
1680     }
1681     return false;
1683 /**
1684 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1685 lines are always in sync with the actual gradient
1686 */
1687 void
1688 GrDrag::updateLines ()
1690     // delete old lines
1691     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1692         gtk_object_destroy( GTK_OBJECT (i->data));
1693     }
1694     g_slist_free (this->lines);
1695     this->lines = NULL;
1697     g_return_if_fail (this->selection != NULL);
1699     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1701         SPItem *item = SP_ITEM(i->data);
1703         SPStyle *style = SP_OBJECT_STYLE (item);
1705         if (style && (style->fill.isPaintserver())) {
1706             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1707             if (SP_IS_LINEARGRADIENT (server)) {
1708                 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);
1709             } else if (SP_IS_RADIALGRADIENT (server)) {
1710                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1711                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1712                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1713             }
1714         }
1716         if (style && (style->stroke.isPaintserver())) {
1717             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1718             if (SP_IS_LINEARGRADIENT (server)) {
1719                 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);
1720             } else if (SP_IS_RADIALGRADIENT (server)) {
1721                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1722                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1723                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1724             }
1725         }
1726     }
1729 /**
1730 Regenerates the levels list from the current selection
1731 */
1732 void
1733 GrDrag::updateLevels ()
1735     hor_levels.clear();
1736     vert_levels.clear();
1738     g_return_if_fail (this->selection != NULL);
1740     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1741         SPItem *item = SP_ITEM(i->data);
1742         Geom::OptRect rect = sp_item_bbox_desktop (item);
1743         if (rect) {
1744             // Remember the edges of the bbox and the center axis
1745             hor_levels.push_back(rect->min()[Geom::Y]);
1746             hor_levels.push_back(rect->max()[Geom::Y]);
1747             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1748             vert_levels.push_back(rect->min()[Geom::X]);
1749             vert_levels.push_back(rect->max()[Geom::X]);
1750             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1751         }
1752     }
1755 void
1756 GrDrag::selected_reverse_vector ()
1758     if (selected == NULL)
1759         return;
1761     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1762         GrDraggable *draggable = (GrDraggable *) i->data;
1764         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1765     }
1768 void
1769 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1771     selected_move (x, y, false, scale_radial);
1774 void
1775 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1777     if (selected == NULL)
1778         return;
1780     bool did = false;
1782     for (GList *i = selected; i != NULL; i = i->next) {
1783         GrDragger *d = (GrDragger *) i->data;
1785         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1786             // if this is an endpoint,
1788             // Moving an rg center moves its focus and radii as well.
1789             // therefore, if this is a focus or radius and if selection
1790             // contains the center as well, do not move this one
1791             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1792                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1793                 bool skip_radius_with_center = false;
1794                 for (GList *di = selected; di != NULL; di = di->next) {
1795                     GrDragger *d_new = (GrDragger *) di->data;
1796                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1797                                     POINT_RG_CENTER,
1798                                     0,
1799                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1800                         // FIXME: here we take into account only the first draggable!
1801                         skip_radius_with_center = true;
1802                     }
1803                 }
1804                 if (skip_radius_with_center)
1805                     continue;
1806             }
1808             did = true;
1809             d->point += Geom::Point (x, y);
1810             d->point_original = d->point;
1811             sp_knot_moveto (d->knot, d->point);
1813             d->fireDraggables (write_repr, scale_radial);
1815             d->updateDependencies(write_repr);
1816         }
1817     }
1819     if (write_repr && did) {
1820         // we did an undoable action
1821         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1822                                 _("Move gradient handle(s)"));
1823         return;
1824     }
1826     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1828         GrDragger *dragger = (GrDragger *) selected->data;
1829         // a midpoint dragger can (logically) only contain one GrDraggable
1830         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1832         Geom::Point begin(0,0), end(0,0);
1833         Geom::Point low_lim(0,0), high_lim(0,0);
1835         SPObject *server = draggable->getServer();
1836         GSList *moving = NULL;
1837         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1839         Geom::Point p(x, y);
1840         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1841         Geom::Point displacement = p - dragger->point;
1843         for (GSList const* i = moving; i != NULL; i = i->next) {
1844             GrDragger *drg = (GrDragger*) i->data;
1845             SPKnot *drgknot = drg->knot;
1846             drg->point += displacement;
1847             sp_knot_moveto (drgknot, drg->point);
1848             drg->fireDraggables (true);
1849             drg->updateDependencies(true);
1850             did = true;
1851         }
1853         g_slist_free(moving);
1855         if (write_repr && did) {
1856             // we did an undoable action
1857             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1858                                     _("Move gradient mid stop(s)"));
1859         }
1860     }
1863 void
1864 GrDrag::selected_move_screen (double x, double y)
1866     gdouble zoom = desktop->current_zoom();
1867     gdouble zx = x / zoom;
1868     gdouble zy = y / zoom;
1870     selected_move (zx, zy);
1873 /**
1874 Select the knot next to the last selected one and deselect all other selected.
1875 */
1876 GrDragger *
1877 GrDrag::select_next ()
1879     GrDragger *d = NULL;
1880     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1881         if (draggers)
1882             d = (GrDragger *) draggers->data;
1883     } else {
1884         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1885     }
1886     if (d)
1887         setSelected (d);
1888     return d;
1891 /**
1892 Select the knot previous from the last selected one and deselect all other selected.
1893 */
1894 GrDragger *
1895 GrDrag::select_prev ()
1897     GrDragger *d = NULL;
1898     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1899         if (draggers)
1900             d = (GrDragger *) g_list_last (draggers)->data;
1901     } else {
1902         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1903     }
1904     if (d)
1905         setSelected (d);
1906     return d;
1910 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1911 void
1912 GrDrag::deleteSelected (bool just_one)
1914     if (!selected) return;
1916     SPDocument *document = false;
1918     struct StructStopInfo {
1919         SPStop * spstop;
1920         GrDraggable * draggable;
1921         SPGradient * gradient;
1922         SPGradient * vector;
1923     };
1925     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1926     GSList *endstoplist = NULL;  // list of stops that must be deleted
1927     while (selected) {
1928         GrDragger *dragger = (GrDragger*) selected->data;
1929         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1930             GrDraggable *draggable = (GrDraggable*) drgble->data;
1931             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1932             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1934             switch (draggable->point_type) {
1935                 case POINT_LG_MID:
1936                 case POINT_RG_MID1:
1937                 case POINT_RG_MID2:
1938                     {
1939                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1940                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1941                         bool present = false;
1942                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1943                             if ( (SPStop*)l->data == stop ) {
1944                                 present = true;
1945                                 break; // no need to search further.
1946                             }
1947                         }
1948                         if (!present)
1949                             midstoplist = g_slist_append(midstoplist, stop);
1950                     }
1951                     break;
1952                 case POINT_LG_BEGIN:
1953                 case POINT_LG_END:
1954                 case POINT_RG_CENTER:
1955                 case POINT_RG_R1:
1956                 case POINT_RG_R2:
1957                     {
1958                         SPStop *stop = NULL;
1959                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1960                             stop = vector->getFirstStop();
1961                         } else {
1962                             stop = sp_last_stop(vector);
1963                         }
1964                         if (stop) {
1965                             StructStopInfo *stopinfo = new StructStopInfo;
1966                             stopinfo->spstop = stop;
1967                             stopinfo->draggable = draggable;
1968                             stopinfo->gradient = gradient;
1969                             stopinfo->vector = vector;
1970                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1971                             bool present = false;
1972                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1973                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1974                                     present = true;
1975                                     break; // no need to search further.
1976                                 }
1977                             }
1978                             if (!present)
1979                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1980                         }
1981                     }
1982                     break;
1983                 default:
1984                     break;
1985             }
1986         }
1987         selected = g_list_remove(selected, dragger);
1988         if ( just_one ) break; // iterate once if just_one is set.
1989     }
1990     while (midstoplist) {
1991         SPStop *stop = (SPStop*) midstoplist->data;
1992         document = SP_OBJECT_DOCUMENT (stop);
1993         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1994         parent->removeChild(SP_OBJECT_REPR(stop));
1995         midstoplist = g_slist_remove(midstoplist, stop);
1996     }
1997     while (endstoplist) {
1998         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1999         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2001         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2002         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2003         // manually count the children, don't know if there already exists a function for this...
2004         int len = 0;
2005         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2006               child != NULL ;
2007               child = SP_OBJECT_NEXT(child) )
2008         {
2009             if ( SP_IS_STOP(child) )  len ++;
2010         }
2011         if (len > 2)
2012         {
2013             switch (stopinfo->draggable->point_type) {
2014                 case POINT_LG_BEGIN:
2015                     {
2016                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2018                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2019                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2020                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2021                         SPStop *stop = stopinfo->vector->getFirstStop();
2022                         gdouble offset = stop->offset;
2023                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2024                         lg->x1.computed = newbegin[Geom::X];
2025                         lg->y1.computed = newbegin[Geom::Y];
2027                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2028                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2029                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2030                         stop->offset = 0;
2031                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2033                         // iterate through midstops to set new offset values such that they won't move on canvas.
2034                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2035                         stop = stop->getNextStop();
2036                         while ( stop != laststop ) {
2037                             stop->offset = (stop->offset - offset)/(1 - offset);
2038                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2039                             stop = stop->getNextStop();
2040                         }
2041                     }
2042                     break;
2043                 case POINT_LG_END:
2044                     {
2045                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2047                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2048                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2049                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2050                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2051                         gdouble offset = laststop->offset;
2052                         Geom::Point newend = begin + offset * (oldend - begin);
2053                         lg->x2.computed = newend[Geom::X];
2054                         lg->y2.computed = newend[Geom::Y];
2056                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2057                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2058                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2059                         laststop->offset = 1;
2060                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2062                         // iterate through midstops to set new offset values such that they won't move on canvas.
2063                         SPStop *stop = stopinfo->vector->getFirstStop();
2064                         stop = stop->getNextStop();
2065                         while ( stop != laststop ) {
2066                             stop->offset = stop->offset / offset;
2067                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2068                             stop = stop->getNextStop();
2069                         }
2070                     }
2071                     break;
2072                 case POINT_RG_CENTER:
2073                     {
2074                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2075                         if (newfirst) {
2076                             newfirst->offset = 0;
2077                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2078                         }
2079                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2080                     }
2081                     break;
2082                 case POINT_RG_R1:
2083                 case POINT_RG_R2:
2084                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2086                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2087                         double oldradius = rg->r.computed;
2088                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2089                         gdouble offset = laststop->offset;
2090                         double newradius = offset * oldradius;
2091                         rg->r.computed = newradius;
2093                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2094                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2095                         laststop->offset = 1;
2096                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2098                         // iterate through midstops to set new offset values such that they won't move on canvas.
2099                         SPStop *stop = stopinfo->vector->getFirstStop();
2100                         stop = stop->getNextStop();
2101                         while ( stop != laststop ) {
2102                             stop->offset = stop->offset / offset;
2103                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2104                             stop = stop->getNextStop();
2105                         }
2106                         break;
2107             }
2108         }
2109         else
2110         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2111             SPCSSAttr *css = sp_repr_css_attr_new ();
2113             // stopinfo->spstop is the selected stop
2114             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2115             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2116                 unselectedrepr = unselectedrepr->next();
2117             }
2119             if (unselectedrepr == NULL) {
2120                 if (stopinfo->draggable->fill_or_stroke) {
2121                     sp_repr_css_unset_property (css, "fill");
2122                 } else {
2123                     sp_repr_css_unset_property (css, "stroke");
2124                 }
2125             } else {
2126                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2127                 if (stopinfo->draggable->fill_or_stroke) {
2128                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2129                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2130                 } else {
2131                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2132                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2133                 }
2134                 sp_repr_css_attr_unref (stopcss);
2135             }
2137             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2138             sp_repr_css_attr_unref (css);
2139         }
2141         endstoplist = g_slist_remove(endstoplist, stopinfo);
2142         delete stopinfo;
2143     }
2145     if (document) {
2146         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2147     }
2151 /*
2152   Local Variables:
2153   mode:c++
2154   c-file-style:"stroustrup"
2155   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2156   indent-tabs-mode:nil
2157   fill-column:99
2158   End:
2159 */
2160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :