Code

A simple layout document as to what, why and how is cppification.
[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                 SPDocumentUndo::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             SPDesktop *desktop = dragger->parent->desktop;
813             SnapManager &m = desktop->namedview->snap_manager;
814             Inkscape::Snapper::ConstraintLine cl(low_lim, high_lim - low_lim);
815             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
816         }
817     }
818     Geom::Point displacement = p - dragger->point;
820     for (GSList const* i = moving; i != NULL; i = i->next) {
821         GrDragger *drg = (GrDragger*) i->data;
822         SPKnot *drgknot = drg->knot;
823         Geom::Point this_move = displacement;
824         if (state & GDK_MOD1_MASK) {
825             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
826             double alpha = 1.0;
827             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
828                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
829                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
830             } else { // drg is on the begin side from dragger
831                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
832                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
833             }
834         }
835         drg->point += this_move;
836         sp_knot_moveto (drgknot, drg->point);
837         drg->fireDraggables (false);
838         drg->updateDependencies(false);
839     }
841     g_slist_free(moving);
843     drag->keep_selection = dragger->isSelected();
848 static void
849 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
851     GrDragger *dragger = (GrDragger *) data;
853     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
856 /**
857 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
858 */
859 static void
860 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
862     GrDragger *dragger = (GrDragger *) data;
864     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
866     dragger->point_original = dragger->point = knot->pos;
868     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
869         dragger->fireDraggables (true, true);
870     } else {
871         dragger->fireDraggables (true);
872     }
874     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
875         GrDragger *d = (GrDragger *) i->data;
876         if (d == dragger)
877             continue;
878         d->fireDraggables (true);
879     }
881     // make this dragger selected
882     if (!dragger->parent->keep_selection) {
883         dragger->parent->setSelected (dragger);
884     }
885     dragger->parent->keep_selection = false;
887     dragger->updateDependencies(true);
889     // we did an undoable action
890     SPDocumentUndo::done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
891                       _("Move gradient handle"));
894 /**
895 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
896 state of the keyboard keys
897 */
898 static void
899 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
901     GrDragger *dragger = (GrDragger *) data;
902     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
903     if (!draggable) return;
905     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
906     // delete this knot from vector
907         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
908         gradient = gradient->getVector();
909         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
910             SPStop *stop = NULL;
911             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
912             case POINT_LG_BEGIN:
913             case POINT_RG_CENTER:
914                 stop = gradient->getFirstStop();
915                 {
916                     SPStop *next = stop->getNextStop();
917                     if (next) {
918                         next->offset = 0;
919                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
920                     }
921                 }
922                 break;
923             case POINT_LG_END:
924             case POINT_RG_R1:
925             case POINT_RG_R2:
926                 stop = sp_last_stop(gradient);
927                 {
928                     SPStop *prev = stop->getPrevStop();
929                     if (prev) {
930                         prev->offset = 1;
931                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
932                     }
933                 }
934                 break;
935             case POINT_LG_MID:
936             case POINT_RG_MID1:
937             case POINT_RG_MID2:
938                 stop = sp_get_stop_i(gradient, draggable->point_i);
939                 break;
940             }
942             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
943             SPDocumentUndo::done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
944                       _("Delete gradient stop"));
945         }
946     } else {
947     // select the dragger
948         dragger->point_original = dragger->point;
950         if ( state & GDK_SHIFT_MASK ) {
951             dragger->parent->setSelected (dragger, true, false);
952         } else {
953             dragger->parent->setSelected (dragger);
954         }
955     }
958 /**
959 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
960 */
961 static void
962 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
964     GrDragger *dragger = (GrDragger *) data;
966     dragger->point_original = dragger->point;
968     if (dragger->draggables == NULL)
969         return;
971     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
972     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
975 /**
976 Act upon all draggables of the dragger, setting them to the dragger's point
977 */
978 void
979 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
981     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
982         GrDraggable *draggable = (GrDraggable *) i->data;
984         // set local_change flag so that selection_changed callback does not regenerate draggers
985         this->parent->local_change = true;
987         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
988         // to the center, unless it's the first update upon merge when we must snap it to the point
989         if (merging_focus ||
990             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
991         {
992             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
993         }
994     }
997 /**
998 Checks if the dragger has a draggable with this point_type
999  */
1000 bool
1001 GrDragger::isA (gint point_type)
1003     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1004         GrDraggable *draggable = (GrDraggable *) i->data;
1005         if (draggable->point_type == point_type) {
1006             return true;
1007         }
1008     }
1009     return false;
1012 /**
1013 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1014  */
1015 bool
1016 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1018     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1019         GrDraggable *draggable = (GrDraggable *) i->data;
1020         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1021             return true;
1022         }
1023     }
1024     return false;
1027 /**
1028 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1029  */
1030 bool
1031 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1033     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1034         GrDraggable *draggable = (GrDraggable *) i->data;
1035         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1036             return true;
1037         }
1038     }
1039     return false;
1042 bool
1043 GrDraggable::mayMerge (GrDraggable *da2)
1045     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1046         // we must not merge the points of the same gradient!
1047         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1048               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1049             // except that we can snap center and focus together
1050             return false;
1051         }
1052     }
1053     // disable merging of midpoints.
1054     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1055          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1056          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1057         return false;
1059     return true;
1062 bool
1063 GrDragger::mayMerge (GrDragger *other)
1065     if (this == other)
1066         return false;
1068     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1069         GrDraggable *da1 = (GrDraggable *) i->data;
1070         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1071             GrDraggable *da2 = (GrDraggable *) j->data;
1072             if (!da1->mayMerge(da2))
1073                 return false;
1074         }
1075     }
1076     return true;
1079 bool
1080 GrDragger::mayMerge (GrDraggable *da2)
1082     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1083         GrDraggable *da1 = (GrDraggable *) i->data;
1084         if (!da1->mayMerge(da2))
1085             return false;
1086     }
1087     return true;
1090 /**
1091 Updates the statusbar tip of the dragger knot, based on its draggables
1092  */
1093 void
1094 GrDragger::updateTip ()
1096     if (this->knot && this->knot->tip) {
1097         g_free (this->knot->tip);
1098         this->knot->tip = NULL;
1099     }
1101     if (g_slist_length (this->draggables) == 1) {
1102         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1103         char *item_desc = draggable->item->description();
1104         switch (draggable->point_type) {
1105             case POINT_LG_MID:
1106             case POINT_RG_MID1:
1107             case POINT_RG_MID2:
1108                 this->knot->tip = g_strdup_printf (_("%s %d for: %s%s; drag with <b>Ctrl</b> to snap offset; click with <b>Ctrl+Alt</b> to delete stop"),
1109                                                    _(gr_knot_descr[draggable->point_type]),
1110                                                    draggable->point_i,
1111                                                    item_desc,
1112                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1113                 break;
1115             default:
1116                 this->knot->tip = g_strdup_printf (_("%s for: %s%s; drag with <b>Ctrl</b> to snap angle, with <b>Ctrl+Alt</b> to preserve angle, with <b>Ctrl+Shift</b> to scale around center"),
1117                                                    _(gr_knot_descr[draggable->point_type]),
1118                                                    item_desc,
1119                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1120                 break;
1121         }
1122         g_free(item_desc);
1123     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1124         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1125     } else {
1126         int length = g_slist_length (this->draggables);
1127         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1128                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1129                                                     length),
1130                                            length);
1131     }
1134 /**
1135 Adds a draggable to the dragger
1136  */
1137 void
1138 GrDragger::updateKnotShape ()
1140     if (!draggables)
1141         return;
1142     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1143     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1146 /**
1147 Adds a draggable to the dragger
1148  */
1149 void
1150 GrDragger::addDraggable (GrDraggable *draggable)
1152     this->draggables = g_slist_prepend (this->draggables, draggable);
1154     this->updateTip();
1158 /**
1159 Moves this dragger to the point of the given draggable, acting upon all other draggables
1160  */
1161 void
1162 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1164     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1165     if (!dr_first) return;
1167     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1168     this->point_original = this->point;
1170     sp_knot_moveto (this->knot, this->point);
1172     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1173         GrDraggable *da = (GrDraggable *) i->data;
1174         if ( (da->item == item) &&
1175              (point_type == -1 || da->point_type == point_type) &&
1176              (point_i == -1 || da->point_i == point_i) &&
1177              (da->fill_or_stroke == fill_or_stroke) ) {
1178             continue;
1179         }
1180         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1181     }
1182     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1186 /**
1187 Moves all midstop draggables that depend on this one
1188  */
1189 void
1190 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1191     SPObject *server = draggable->getServer();
1192     if (!server)
1193         return;
1194     guint num = SP_GRADIENT(server)->vector.stops.size();
1195     if (num <= 2) return;
1197     if ( SP_IS_LINEARGRADIENT(server) ) {
1198         for ( guint i = 1; i < num - 1; i++ ) {
1199             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1200         }
1201     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1202         for ( guint i = 1; i < num - 1; i++ ) {
1203             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1204             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1205         }
1206     }
1210 /**
1211 Moves all draggables that depend on this one
1212  */
1213 void
1214 GrDragger::updateDependencies (bool write_repr)
1216     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1217         GrDraggable *draggable = (GrDraggable *) i->data;
1218         switch (draggable->point_type) {
1219             case POINT_LG_BEGIN:
1220                 {
1221                     // the end point is dependent only when dragging with ctrl+shift
1222                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1224                     this->updateMidstopDependencies (draggable, write_repr);
1225                 }
1226                 break;
1227             case POINT_LG_END:
1228                 {
1229                     // the begin point is dependent only when dragging with ctrl+shift
1230                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1232                     this->updateMidstopDependencies (draggable, write_repr);
1233                 }
1234                 break;
1235             case POINT_LG_MID:
1236                 // no other nodes depend on mid points.
1237                 break;
1238             case POINT_RG_R2:
1239                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1240                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1241                 this->updateMidstopDependencies (draggable, write_repr);
1242                 break;
1243             case POINT_RG_R1:
1244                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1245                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1246                 this->updateMidstopDependencies (draggable, write_repr);
1247                 break;
1248             case POINT_RG_CENTER:
1249                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1250                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1251                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1252                 this->updateMidstopDependencies (draggable, write_repr);
1253                 break;
1254             case POINT_RG_FOCUS:
1255                 // nothing can depend on that
1256                 break;
1257             case POINT_RG_MID1:
1258                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1259                 break;
1260             case POINT_RG_MID2:
1261                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1262                 break;
1263             default:
1264                 break;
1265         }
1266     }
1271 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1272   : point(p),
1273     point_original(p)
1275     this->draggables = NULL;
1277     this->parent = parent;
1279     // create the knot
1280     this->knot = sp_knot_new (parent->desktop, NULL);
1281     this->knot->setMode(SP_KNOT_MODE_XOR);
1282     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1283     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1284     sp_knot_update_ctrl(this->knot);
1286     // move knot to the given point
1287     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1288     sp_knot_show (this->knot);
1290     // connect knot's signals
1291     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1292                        // luckily, midstops never snap to other nodes so are never unsnapped...
1293          && ( (draggable->point_type == POINT_LG_MID)
1294               || (draggable->point_type == POINT_RG_MID1)
1295               || (draggable->point_type == POINT_RG_MID2) ) )
1296     {
1297         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1298     } else {
1299         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1300     }
1301     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1302     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1303     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1304     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1306     // add the initial draggable
1307     if (draggable)
1308         this->addDraggable (draggable);
1309     updateKnotShape();
1312 GrDragger::~GrDragger ()
1314     // unselect if it was selected
1315     this->parent->setDeselected(this);
1317     // disconnect signals
1318     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1319     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1320     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1321     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1322     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1324     /* unref should call destroy */
1325     g_object_unref (G_OBJECT (this->knot));
1327     // delete all draggables
1328     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1329         delete ((GrDraggable *) i->data);
1330     }
1331     g_slist_free (this->draggables);
1332     this->draggables = NULL;
1335 /**
1336 Select the dragger which has the given draggable.
1337 */
1338 GrDragger *
1339 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1341     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1342         GrDragger *dragger = (GrDragger *) i->data;
1343         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1344             GrDraggable *da2 = (GrDraggable *) j->data;
1345             if ( (da2->item == item) &&
1346                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1347                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1348                  (da2->fill_or_stroke == fill_or_stroke)) {
1349                 return (dragger);
1350             }
1351         }
1352     }
1353     return NULL;
1357 void
1358 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1360     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1361     if (d && d !=  this) {
1362         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1363     }
1367 /**
1368   Draw this dragger as selected
1369 */
1370 void
1371 GrDragger::select()
1373     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1374     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1377 /**
1378   Draw this dragger as normal (deselected)
1379 */
1380 void
1381 GrDragger::deselect()
1383     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1384     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1387 bool
1388 GrDragger::isSelected()
1390     return g_list_find (parent->selected, this);
1393 /**
1394 \brief Deselect all stops/draggers (private)
1395 */
1396 void
1397 GrDrag::deselect_all()
1399     while (selected) {
1400         ( (GrDragger*) selected->data)->deselect();
1401         selected = g_list_remove(selected, selected->data);
1402     }
1405 /**
1406 \brief Deselect all stops/draggers (public; emits signal)
1407 */
1408 void
1409 GrDrag::deselectAll()
1411     deselect_all();
1412     this->desktop->emitToolSubselectionChanged(NULL);
1415 /**
1416 \brief Select all stops/draggers
1417 */
1418 void
1419 GrDrag::selectAll()
1421     for (GList *l = this->draggers; l != NULL; l = l->next) {
1422         GrDragger *d = ((GrDragger *) l->data);
1423         setSelected (d, true, true);
1424     }
1427 /**
1428 \brief Select all stops/draggers that match the coords
1429 */
1430 void
1431 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1433     for (GList *l = this->draggers; l != NULL; l = l->next) {
1434         GrDragger *d = ((GrDragger *) l->data);
1435         for (guint k = 0; k < coords.size(); k++) {
1436             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1437                 setSelected (d, true, true);
1438             }
1439         }
1440     }
1444 /**
1445 \brief Select all stops/draggers that fall within the rect
1446 */
1447 void
1448 GrDrag::selectRect(Geom::Rect const &r)
1450     for (GList *l = this->draggers; l != NULL; l = l->next) {
1451         GrDragger *d = ((GrDragger *) l->data);
1452         if (r.contains(d->point)) {
1453            setSelected (d, true, true);
1454         }
1455     }
1458 /**
1459 \brief Select a dragger
1460 \param dragger       The dragger to select
1461 \param add_to_selection   If true, add to selection, otherwise deselect others
1462 \param override      If true, always select this node, otherwise toggle selected status
1463 */
1464 void
1465 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1467     GrDragger *seldragger = NULL;
1469     if (add_to_selection) {
1470         if (!dragger) return;
1471         if (override) {
1472             if (!g_list_find(selected, dragger)) {
1473                 selected = g_list_prepend(selected, dragger);
1474             }
1475             dragger->select();
1476             seldragger = dragger;
1477         } else { // toggle
1478             if (g_list_find(selected, dragger)) {
1479                 selected = g_list_remove(selected, dragger);
1480                 dragger->deselect();
1481                 if (selected) {
1482                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1483                 }
1484             } else {
1485                 selected = g_list_prepend(selected, dragger);
1486                 dragger->select();
1487                 seldragger = dragger;
1488             }
1489         }
1490     } else {
1491         deselect_all();
1492         if (dragger) {
1493             selected = g_list_prepend(selected, dragger);
1494             dragger->select();
1495             seldragger = dragger;
1496         }
1497     }
1498     if (seldragger) {
1499         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1500     }
1503 /**
1504 \brief Deselect a dragger
1505 \param dragger       The dragger to deselect
1506 */
1507 void
1508 GrDrag::setDeselected (GrDragger *dragger)
1510     if (g_list_find(selected, dragger)) {
1511         selected = g_list_remove(selected, dragger);
1512         dragger->deselect();
1513     }
1514     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1519 /**
1520 Create a line from p1 to p2 and add it to the lines list
1521  */
1522 void
1523 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1525     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1526                                                             SP_TYPE_CTRLLINE, NULL);
1527     sp_canvas_item_move_to_z(line, 0);
1528     SP_CTRLLINE(line)->item = item;
1529     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1530     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1531         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1532     sp_canvas_item_show (line);
1533     this->lines = g_slist_append (this->lines, line);
1536 /**
1537 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1538 new dragger and add it to draggers list
1539  */
1540 void
1541 GrDrag::addDragger (GrDraggable *draggable)
1543     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1545     for (GList *i = this->draggers; i != NULL; i = i->next) {
1546         GrDragger *dragger = (GrDragger *) i->data;
1547         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1548             // distance is small, merge this draggable into dragger, no need to create new dragger
1549             dragger->addDraggable (draggable);
1550             dragger->updateKnotShape();
1551             return;
1552         }
1553     }
1555     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1556     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1557     this->draggers = g_list_append (this->draggers, new_dragger);
1560 /**
1561 Add draggers for the radial gradient rg on item
1562 */
1563 void
1564 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1566     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1567     guint num = rg->vector.stops.size();
1568     if (num > 2) {
1569         for ( guint i = 1; i < num - 1; i++ ) {
1570             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1571         }
1572     }
1573     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1574     if (num > 2) {
1575         for ( guint i = 1; i < num - 1; i++ ) {
1576             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1577         }
1578     }
1579     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1580     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1583 /**
1584 Add draggers for the linear gradient lg on item
1585 */
1586 void
1587 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1589     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1590     guint num = lg->vector.stops.size();
1591     if (num > 2) {
1592         for ( guint i = 1; i < num - 1; i++ ) {
1593             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1594         }
1595     }
1596     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1599 /**
1600 Artificially grab the knot of this dragger; used by the gradient context
1601 */
1602 void
1603 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1605     if (dragger) {
1606         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1607     }
1610 /**
1611 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1612 */
1613 void
1614 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1616     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1617     if (dragger) {
1618         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1619     }
1622 /**
1623 Regenerates the draggers list from the current selection; is called when selection is changed or
1624 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1625 */
1626 void
1627 GrDrag::updateDraggers ()
1629     while (selected) {
1630         selected = g_list_remove(selected, selected->data);
1631     }
1632     // delete old draggers
1633     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1634         delete ((GrDragger *) i->data);
1635     }
1636     g_list_free (this->draggers);
1637     this->draggers = NULL;
1639     g_return_if_fail (this->selection != NULL);
1641     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1643         SPItem *item = SP_ITEM(i->data);
1644         SPStyle *style = SP_OBJECT_STYLE (item);
1646         if (style && (style->fill.isPaintserver())) {
1647             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1648             if (SP_IS_LINEARGRADIENT (server)) {
1649                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1650             } else if (SP_IS_RADIALGRADIENT (server)) {
1651                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1652             }
1653         }
1655         if (style && (style->stroke.isPaintserver())) {
1656             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1657             if (SP_IS_LINEARGRADIENT (server)) {
1658                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1659             } else if (SP_IS_RADIALGRADIENT (server)) {
1660                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1661             }
1662         }
1663     }
1667 /**
1668  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1669  */
1671 bool
1672 GrDrag::mouseOver()
1674     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1675         GrDragger *d = (GrDragger *) i->data;
1676         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1677             return true;
1678         }
1679     }
1680     return false;
1682 /**
1683 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1684 lines are always in sync with the actual gradient
1685 */
1686 void
1687 GrDrag::updateLines ()
1689     // delete old lines
1690     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1691         gtk_object_destroy( GTK_OBJECT (i->data));
1692     }
1693     g_slist_free (this->lines);
1694     this->lines = NULL;
1696     g_return_if_fail (this->selection != NULL);
1698     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1700         SPItem *item = SP_ITEM(i->data);
1702         SPStyle *style = SP_OBJECT_STYLE (item);
1704         if (style && (style->fill.isPaintserver())) {
1705             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1706             if (SP_IS_LINEARGRADIENT (server)) {
1707                 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);
1708             } else if (SP_IS_RADIALGRADIENT (server)) {
1709                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1710                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1711                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1712             }
1713         }
1715         if (style && (style->stroke.isPaintserver())) {
1716             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1717             if (SP_IS_LINEARGRADIENT (server)) {
1718                 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);
1719             } else if (SP_IS_RADIALGRADIENT (server)) {
1720                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1721                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1722                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1723             }
1724         }
1725     }
1728 /**
1729 Regenerates the levels list from the current selection
1730 */
1731 void
1732 GrDrag::updateLevels ()
1734     hor_levels.clear();
1735     vert_levels.clear();
1737     g_return_if_fail (this->selection != NULL);
1739     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1740         SPItem *item = SP_ITEM(i->data);
1741         Geom::OptRect rect = item->getBboxDesktop ();
1742         if (rect) {
1743             // Remember the edges of the bbox and the center axis
1744             hor_levels.push_back(rect->min()[Geom::Y]);
1745             hor_levels.push_back(rect->max()[Geom::Y]);
1746             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1747             vert_levels.push_back(rect->min()[Geom::X]);
1748             vert_levels.push_back(rect->max()[Geom::X]);
1749             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1750         }
1751     }
1754 void
1755 GrDrag::selected_reverse_vector ()
1757     if (selected == NULL)
1758         return;
1760     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1761         GrDraggable *draggable = (GrDraggable *) i->data;
1763         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1764     }
1767 void
1768 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1770     selected_move (x, y, false, scale_radial);
1773 void
1774 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1776     if (selected == NULL)
1777         return;
1779     bool did = false;
1781     for (GList *i = selected; i != NULL; i = i->next) {
1782         GrDragger *d = (GrDragger *) i->data;
1784         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1785             // if this is an endpoint,
1787             // Moving an rg center moves its focus and radii as well.
1788             // therefore, if this is a focus or radius and if selection
1789             // contains the center as well, do not move this one
1790             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1791                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1792                 bool skip_radius_with_center = false;
1793                 for (GList *di = selected; di != NULL; di = di->next) {
1794                     GrDragger *d_new = (GrDragger *) di->data;
1795                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1796                                     POINT_RG_CENTER,
1797                                     0,
1798                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1799                         // FIXME: here we take into account only the first draggable!
1800                         skip_radius_with_center = true;
1801                     }
1802                 }
1803                 if (skip_radius_with_center)
1804                     continue;
1805             }
1807             did = true;
1808             d->point += Geom::Point (x, y);
1809             d->point_original = d->point;
1810             sp_knot_moveto (d->knot, d->point);
1812             d->fireDraggables (write_repr, scale_radial);
1814             d->updateDependencies(write_repr);
1815         }
1816     }
1818     if (write_repr && did) {
1819         // we did an undoable action
1820         SPDocumentUndo::maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1821                                 _("Move gradient handle(s)"));
1822         return;
1823     }
1825     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1827         GrDragger *dragger = (GrDragger *) selected->data;
1828         // a midpoint dragger can (logically) only contain one GrDraggable
1829         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1831         Geom::Point begin(0,0), end(0,0);
1832         Geom::Point low_lim(0,0), high_lim(0,0);
1834         SPObject *server = draggable->getServer();
1835         GSList *moving = NULL;
1836         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1838         Geom::Point p(x, y);
1839         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1840         Geom::Point displacement = p - dragger->point;
1842         for (GSList const* i = moving; i != NULL; i = i->next) {
1843             GrDragger *drg = (GrDragger*) i->data;
1844             SPKnot *drgknot = drg->knot;
1845             drg->point += displacement;
1846             sp_knot_moveto (drgknot, drg->point);
1847             drg->fireDraggables (true);
1848             drg->updateDependencies(true);
1849             did = true;
1850         }
1852         g_slist_free(moving);
1854         if (write_repr && did) {
1855             // we did an undoable action
1856             SPDocumentUndo::maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1857                                     _("Move gradient mid stop(s)"));
1858         }
1859     }
1862 void
1863 GrDrag::selected_move_screen (double x, double y)
1865     gdouble zoom = desktop->current_zoom();
1866     gdouble zx = x / zoom;
1867     gdouble zy = y / zoom;
1869     selected_move (zx, zy);
1872 /**
1873 Select the knot next to the last selected one and deselect all other selected.
1874 */
1875 GrDragger *
1876 GrDrag::select_next ()
1878     GrDragger *d = NULL;
1879     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1880         if (draggers)
1881             d = (GrDragger *) draggers->data;
1882     } else {
1883         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1884     }
1885     if (d)
1886         setSelected (d);
1887     return d;
1890 /**
1891 Select the knot previous from the last selected one and deselect all other selected.
1892 */
1893 GrDragger *
1894 GrDrag::select_prev ()
1896     GrDragger *d = NULL;
1897     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1898         if (draggers)
1899             d = (GrDragger *) g_list_last (draggers)->data;
1900     } else {
1901         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1902     }
1903     if (d)
1904         setSelected (d);
1905     return d;
1909 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1910 void
1911 GrDrag::deleteSelected (bool just_one)
1913     if (!selected) return;
1915     SPDocument *document = false;
1917     struct StructStopInfo {
1918         SPStop * spstop;
1919         GrDraggable * draggable;
1920         SPGradient * gradient;
1921         SPGradient * vector;
1922     };
1924     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1925     GSList *endstoplist = NULL;  // list of stops that must be deleted
1926     while (selected) {
1927         GrDragger *dragger = (GrDragger*) selected->data;
1928         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1929             GrDraggable *draggable = (GrDraggable*) drgble->data;
1930             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1931             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1933             switch (draggable->point_type) {
1934                 case POINT_LG_MID:
1935                 case POINT_RG_MID1:
1936                 case POINT_RG_MID2:
1937                     {
1938                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1939                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1940                         bool present = false;
1941                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1942                             if ( (SPStop*)l->data == stop ) {
1943                                 present = true;
1944                                 break; // no need to search further.
1945                             }
1946                         }
1947                         if (!present)
1948                             midstoplist = g_slist_append(midstoplist, stop);
1949                     }
1950                     break;
1951                 case POINT_LG_BEGIN:
1952                 case POINT_LG_END:
1953                 case POINT_RG_CENTER:
1954                 case POINT_RG_R1:
1955                 case POINT_RG_R2:
1956                     {
1957                         SPStop *stop = NULL;
1958                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1959                             stop = vector->getFirstStop();
1960                         } else {
1961                             stop = sp_last_stop(vector);
1962                         }
1963                         if (stop) {
1964                             StructStopInfo *stopinfo = new StructStopInfo;
1965                             stopinfo->spstop = stop;
1966                             stopinfo->draggable = draggable;
1967                             stopinfo->gradient = gradient;
1968                             stopinfo->vector = vector;
1969                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1970                             bool present = false;
1971                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1972                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1973                                     present = true;
1974                                     break; // no need to search further.
1975                                 }
1976                             }
1977                             if (!present)
1978                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1979                         }
1980                     }
1981                     break;
1982                 default:
1983                     break;
1984             }
1985         }
1986         selected = g_list_remove(selected, dragger);
1987         if ( just_one ) break; // iterate once if just_one is set.
1988     }
1989     while (midstoplist) {
1990         SPStop *stop = (SPStop*) midstoplist->data;
1991         document = SP_OBJECT_DOCUMENT (stop);
1992         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1993         parent->removeChild(SP_OBJECT_REPR(stop));
1994         midstoplist = g_slist_remove(midstoplist, stop);
1995     }
1996     while (endstoplist) {
1997         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1998         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2000         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2001         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2002         // manually count the children, don't know if there already exists a function for this...
2003         int len = 0;
2004         for ( SPObject *child = (stopinfo->vector)->first_child() ;
2005               child != NULL ;
2006               child = SP_OBJECT_NEXT(child) )
2007         {
2008             if ( SP_IS_STOP(child) )  len ++;
2009         }
2010         if (len > 2)
2011         {
2012             switch (stopinfo->draggable->point_type) {
2013                 case POINT_LG_BEGIN:
2014                     {
2015                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2017                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2018                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2019                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2020                         SPStop *stop = stopinfo->vector->getFirstStop();
2021                         gdouble offset = stop->offset;
2022                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2023                         lg->x1.computed = newbegin[Geom::X];
2024                         lg->y1.computed = newbegin[Geom::Y];
2026                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2027                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2028                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2029                         stop->offset = 0;
2030                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2032                         // iterate through midstops to set new offset values such that they won't move on canvas.
2033                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2034                         stop = stop->getNextStop();
2035                         while ( stop != laststop ) {
2036                             stop->offset = (stop->offset - offset)/(1 - offset);
2037                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2038                             stop = stop->getNextStop();
2039                         }
2040                     }
2041                     break;
2042                 case POINT_LG_END:
2043                     {
2044                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2046                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2047                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2048                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2049                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2050                         gdouble offset = laststop->offset;
2051                         Geom::Point newend = begin + offset * (oldend - begin);
2052                         lg->x2.computed = newend[Geom::X];
2053                         lg->y2.computed = newend[Geom::Y];
2055                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2056                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2057                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2058                         laststop->offset = 1;
2059                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2061                         // iterate through midstops to set new offset values such that they won't move on canvas.
2062                         SPStop *stop = stopinfo->vector->getFirstStop();
2063                         stop = stop->getNextStop();
2064                         while ( stop != laststop ) {
2065                             stop->offset = stop->offset / offset;
2066                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2067                             stop = stop->getNextStop();
2068                         }
2069                     }
2070                     break;
2071                 case POINT_RG_CENTER:
2072                     {
2073                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2074                         if (newfirst) {
2075                             newfirst->offset = 0;
2076                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2077                         }
2078                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2079                     }
2080                     break;
2081                 case POINT_RG_R1:
2082                 case POINT_RG_R2:
2083                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2085                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2086                         double oldradius = rg->r.computed;
2087                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2088                         gdouble offset = laststop->offset;
2089                         double newradius = offset * oldradius;
2090                         rg->r.computed = newradius;
2092                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2093                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2094                         laststop->offset = 1;
2095                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2097                         // iterate through midstops to set new offset values such that they won't move on canvas.
2098                         SPStop *stop = stopinfo->vector->getFirstStop();
2099                         stop = stop->getNextStop();
2100                         while ( stop != laststop ) {
2101                             stop->offset = stop->offset / offset;
2102                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2103                             stop = stop->getNextStop();
2104                         }
2105                         break;
2106             }
2107         }
2108         else
2109         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2110             SPCSSAttr *css = sp_repr_css_attr_new ();
2112             // stopinfo->spstop is the selected stop
2113             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2114             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2115                 unselectedrepr = unselectedrepr->next();
2116             }
2118             if (unselectedrepr == NULL) {
2119                 if (stopinfo->draggable->fill_or_stroke) {
2120                     sp_repr_css_unset_property (css, "fill");
2121                 } else {
2122                     sp_repr_css_unset_property (css, "stroke");
2123                 }
2124             } else {
2125                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2126                 if (stopinfo->draggable->fill_or_stroke) {
2127                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2128                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2129                 } else {
2130                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2131                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2132                 }
2133                 sp_repr_css_attr_unref (stopcss);
2134             }
2136             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2137             sp_repr_css_attr_unref (css);
2138         }
2140         endstoplist = g_slist_remove(endstoplist, stopinfo);
2141         delete stopinfo;
2142     }
2144     if (document) {
2145         SPDocumentUndo::done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2146     }
2150 /*
2151   Local Variables:
2152   mode:c++
2153   c-file-style:"stroustrup"
2154   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2155   indent-tabs-mode:nil
2156   fill-column:99
2157   End:
2158 */
2159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :