Code

system clipboard support (bug #170185) from Chris KosiƄski
[inkscape.git] / src / gradient-drag.cpp
1 #define __GRADIENT_DRAG_C__
3 /*
4  * On-canvas gradient dragging
5  *
6  * Authors:
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2007 Johan Engelen
11  * Copyright (C) 2005 Authors
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include <glibmm/i18n.h>
21 #include <cstring>
22 #include <string>
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-style.h"
28 #include "document.h"
29 #include "display/sp-ctrlline.h"
30 #include "xml/repr.h"
31 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
33 #include "libnr/nr-point-fns.h"
34 #include "prefs-utils.h"
35 #include "sp-item.h"
36 #include "style.h"
37 #include "knot.h"
38 #include "sp-linear-gradient.h"
39 #include "sp-radial-gradient.h"
40 #include "gradient-chemistry.h"
41 #include "gradient-drag.h"
42 #include "sp-stop.h"
43 #include "snap.h"
44 #include "sp-namedview.h"
45 #include "selection-chemistry.h"
46 #include "display/snap-indicator.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, NR::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             NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
299             NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
301             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
302             double dist_screen = NR::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             NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
311             NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
312             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
313             double dist_screen = NR::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 = NR::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 = sp_first_stop(vector);
337         SPStop* next_stop = sp_next_stop(prev_stop);
338         guint i = 1;
339         while ( (next_stop) && (next_stop->offset < offset) ) {
340             prev_stop = next_stop;
341             next_stop = sp_next_stop(next_stop);
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         sp_gradient_ensure_vector (gradient);
352         updateDraggers();
354         return newstop;
355     }
357     return NULL;
361 bool
362 GrDrag::dropColor(SPItem */*item*/, gchar *c, NR::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 (NR::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             NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
389             double dist_screen = NR::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 void
522 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
524     GrDragger *dragger = (GrDragger *) data;
525     GrDrag *drag = dragger->parent;
527     NR::Point p = *ppointer;
529     // FIXME: take from prefs
530     double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
532     dragger->parent->desktop->snapindicator->remove_snappoint();
534     if (state & GDK_SHIFT_MASK) {
535         // with Shift; unsnap if we carry more than one draggable
536         if (dragger->draggables && dragger->draggables->next) {
537             // create a new dragger
538             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
539             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
540             // relink to it all but the first draggable in the list
541             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
542                 GrDraggable *draggable = (GrDraggable *) i->data;
543                 dr_new->addDraggable (draggable);
544             }
545             dr_new->updateKnotShape();
546             g_slist_free (dragger->draggables->next);
547             dragger->draggables->next = NULL;
548             dragger->updateKnotShape();
549             dragger->updateTip();
550         }
551     } else if (!(state & GDK_CONTROL_MASK)) {
552         // without Shift or Ctrl; see if we need to snap to another dragger
553         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
554             GrDragger *d_new = (GrDragger *) di->data;
555             if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
557                 // Merge draggers:
558                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
559                     GrDraggable *draggable = (GrDraggable *) i->data;
560                     // copy draggable to d_new:
561                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
562                     d_new->addDraggable (da_new);
563                 }
565                 // unlink and delete this dragger
566                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
567                 delete dragger;
569                 // update the new merged dragger
570                 d_new->fireDraggables(true, false, true);
571                 d_new->parent->updateLines();
572                 d_new->parent->setSelected (d_new);
573                 d_new->updateKnotShape ();
574                 d_new->updateTip ();
575                 d_new->updateDependencies(true);
576                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
577                                   _("Merge gradient handles"));
578                 return;
579             }
580         }
581     }
583     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
584         // Try snapping to the grid or guides
585         SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
586         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
587         if (s.getDistance() < 1e6) {
588             p = s.getPoint();
589             sp_knot_moveto (knot, &p);
590             dragger->parent->desktop->snapindicator->set_new_snappoint(p.to_2geom());
591         } else {
592             bool was_snapped = false;
593             Geom::Point snapped_to;
594             // No snapping so far, let's see if we need to snap to any of the levels
595             for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
596                 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
597                     p[NR::Y] = dragger->parent->hor_levels[i];
598                     snapped_to = p.to_2geom();
599                     was_snapped = true;
600                     sp_knot_moveto (knot, &p);
601                 }
602             }
603             for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
604                 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
605                     p[NR::X] = dragger->parent->vert_levels[i];
606                     snapped_to = p.to_2geom();
607                     was_snapped = true;
608                     sp_knot_moveto (knot, &p);
609                 }
610             }
611             if (was_snapped) {
612                 dragger->parent->desktop->snapindicator->set_new_snappoint(snapped_to);
613             }
614         }
615     }
617     if (state & GDK_CONTROL_MASK) {
618         unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
619         /* 0 means no snapping. */
621         // This list will store snap vectors from all draggables of dragger
622         GSList *snap_vectors = NULL;
624         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
625             GrDraggable *draggable = (GrDraggable *) i->data;
627             NR::Point *dr_snap = NULL;
629             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
630                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
631                     GrDragger *d_new = (GrDragger *) di->data;
632                     if (d_new == dragger)
633                         continue;
634                     if (d_new->isA (draggable->item,
635                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
636                                     draggable->fill_or_stroke)) {
637                         // found the other end of the linear gradient;
638                         if (state & GDK_SHIFT_MASK) {
639                             // moving linear around center
640                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
641                             dr_snap = &center;
642                         } else {
643                             // moving linear around the other end
644                             dr_snap = &d_new->point;
645                         }
646                     }
647                 }
648             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
649                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
650                     GrDragger *d_new = (GrDragger *) di->data;
651                     if (d_new == dragger)
652                         continue;
653                     if (d_new->isA (draggable->item,
654                                     POINT_RG_CENTER,
655                                     draggable->fill_or_stroke)) {
656                         // found the center of the radial gradient;
657                         dr_snap = &(d_new->point);
658                     }
659                 }
660             } else if (draggable->point_type == POINT_RG_CENTER) {
661                 // radial center snaps to hor/vert relative to its original position
662                 dr_snap = &(dragger->point_original);
663             }
665             NR::Point *snap_vector = NULL;
666             if (dr_snap) {
667                 if (state & GDK_MOD1_MASK) {
668                     // with Alt, snap to the original angle and its perpendiculars
669                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
670                 } else {
671                     // with Ctrl, snap to M_PI/snaps
672                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
673                 }
674             }
675             if (snap_vector) {
676                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
677             }
678         }
680         // Move by the smallest of snap vectors:
681         NR::Point move(9999, 9999);
682         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
683             NR::Point *snap_vector = (NR::Point *) i->data;
684             if (NR::L2(*snap_vector) < NR::L2(move))
685                 move = *snap_vector;
686         }
687         if (move[NR::X] < 9999) {
688             p += move;
689             sp_knot_moveto (knot, &p);
690         }
692         g_slist_free(snap_vectors);
693     }
695     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
696     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
698     if (drag->keep_selection) {
699         NR::Point diff = p - dragger->point;
700         drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
701     } else {
702         dragger->point = p;
703         dragger->fireDraggables (false, scale_radial);
704         dragger->updateDependencies(false);
705     }
710 static void
711 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
714     GrDrag *drag = dragger->parent;
715     // a midpoint dragger can (logically) only contain one GrDraggable
716     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
718     // get begin and end points between which dragging is allowed:
719     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
720     *moving = g_slist_append(*moving, dragger);
722     guint lowest_i = draggable->point_i;
723     guint highest_i = draggable->point_i;
724     GrDragger *lowest_dragger = dragger;
725     GrDragger *highest_dragger = dragger;
726     if (dragger->isSelected()) {
727         GrDragger* d_add;
728         while ( true )
729         {
730             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
731             if ( d_add && g_list_find(drag->selected, d_add) ) {
732                 lowest_i = lowest_i - 1;
733                 *moving = g_slist_prepend(*moving, d_add);
734                 lowest_dragger = d_add;
735             } else {
736                 break;
737             }
738         }
740         while ( true )
741         {
742             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
743             if ( d_add && g_list_find(drag->selected, d_add) ) {
744                 highest_i = highest_i + 1;
745                 *moving = g_slist_append(*moving, d_add);
746                 highest_dragger = d_add;
747             } else {
748                 break;
749             }
750         }
751     }
753     if ( SP_IS_LINEARGRADIENT(server) ) {
754         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
755         GrDragger *d_temp;
756         if (lowest_i == 1) {
757             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
758         } else {
759             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
760         }
761         if (d_temp)
762             *begin = d_temp->point;
764         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
765         if (d_temp == NULL) {
766             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
767         }
768         if (d_temp)
769             *end = d_temp->point;
770     } else if ( SP_IS_RADIALGRADIENT(server) ) {
771         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
772         GrDragger *d_temp;
773         if (lowest_i == 1) {
774             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
775         } else {
776             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
777         }
778         if (d_temp)
779             *begin = d_temp->point;
781         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
782         if (d_temp == NULL) {
783             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
784         }
785         if (d_temp)
786             *end = d_temp->point;
787     }
789     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
790     *high_lim = dragger->point - (highest_dragger->point - *end);
795 /**
796 Called when a midpoint knot is dragged.
797 */
798 static void
799 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, NR::Point const *ppointer, guint state, gpointer data)
801     GrDragger *dragger = (GrDragger *) data;
802     GrDrag *drag = dragger->parent;
803     // a midpoint dragger can (logically) only contain one GrDraggable
804     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
806     // FIXME: take from prefs
807     double snap_fraction = 0.1;
809     NR::Point p = *ppointer;
810     NR::Point begin(0,0), end(0,0);
811     NR::Point low_lim(0,0), high_lim(0,0);
813     SPObject *server = draggable->getServer();
815     GSList *moving = NULL;
816     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
818     if (state & GDK_CONTROL_MASK) {
819         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
820     } else {
821         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
822     }
823     NR::Point displacement = p - dragger->point;
825     for (GSList const* i = moving; i != NULL; i = i->next) {
826         GrDragger *drg = (GrDragger*) i->data;
827         SPKnot *drgknot = drg->knot;
828         NR::Point this_move = displacement;
829         if (state & GDK_MOD1_MASK) {
830             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
831             double alpha = 1.0;
832             if (NR::L2(drg->point - dragger->point) + NR::L2(drg->point - begin) - 1e-3 > NR::L2(dragger->point - begin)) { // drg is on the end side from dragger
833                 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
834                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
835             } else { // drg is on the begin side from dragger
836                 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
837                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
838             }
839         }
840         drg->point += this_move;
841         sp_knot_moveto (drgknot, & drg->point);
842         drg->fireDraggables (false);
843         drg->updateDependencies(false);
844     }
846     g_slist_free(moving);
848     drag->keep_selection = dragger->isSelected();
853 static void
854 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
856     GrDragger *dragger = (GrDragger *) data;
858     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
861 /**
862 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
863 */
864 static void
865 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
867     GrDragger *dragger = (GrDragger *) data;
869     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
871     dragger->point_original = dragger->point = knot->pos;
873     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
874         dragger->fireDraggables (true, true);
875     } else {
876         dragger->fireDraggables (true);
877     }
879     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
880         GrDragger *d = (GrDragger *) i->data;
881         if (d == dragger)
882             continue;
883         d->fireDraggables (true);
884     }
886     // make this dragger selected
887     if (!dragger->parent->keep_selection) {
888         dragger->parent->setSelected (dragger);
889     }
890     dragger->parent->keep_selection = false;
892     dragger->updateDependencies(true);
894     // we did an undoable action
895     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
896                       _("Move gradient handle"));
899 /**
900 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
901 state of the keyboard keys
902 */
903 static void
904 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
906     GrDragger *dragger = (GrDragger *) data;
907     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
908     if (!draggable) return;
910     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
911     // delete this knot from vector
912         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
913         gradient = sp_gradient_get_vector (gradient, false);
914         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
915                 SPStop *stop = NULL;
916                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
917                 case POINT_LG_BEGIN:
918                 case POINT_RG_CENTER:
919                     stop = sp_first_stop(gradient);
920                         {
921                             SPStop *next = sp_next_stop (stop);
922                                 if (next) {
923                                         next->offset = 0;
924                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
925                                 }
926                         }
927                     break;
928                 case POINT_LG_END:
929                 case POINT_RG_R1:
930                 case POINT_RG_R2:
931                     stop = sp_last_stop(gradient);
932                     {
933                             SPStop *prev = sp_prev_stop (stop, gradient);
934                             if (prev) {
935                                     prev->offset = 1;
936                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
937                             }
938                         }
939                     break;
940                 case POINT_LG_MID:
941                 case POINT_RG_MID1:
942                 case POINT_RG_MID2:
943                     stop = sp_get_stop_i(gradient, draggable->point_i);
944                     break;
945                 }
947                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
948                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
949                                   _("Delete gradient stop"));
950         }
951     } else {
952     // select the dragger
953         dragger->point_original = dragger->point;
955         if ( state & GDK_SHIFT_MASK ) {
956             dragger->parent->setSelected (dragger, true, false);
957         } else {
958             dragger->parent->setSelected (dragger);
959         }
960     }
963 /**
964 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
965 */
966 static void
967 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
969     GrDragger *dragger = (GrDragger *) data;
971     dragger->point_original = dragger->point;
973     if (dragger->draggables == NULL)
974         return;
976     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
977     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
980 /**
981 Act upon all draggables of the dragger, setting them to the dragger's point
982 */
983 void
984 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
986     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
987         GrDraggable *draggable = (GrDraggable *) i->data;
989         // set local_change flag so that selection_changed callback does not regenerate draggers
990         this->parent->local_change = true;
992         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
993         // to the center, unless it's the first update upon merge when we must snap it to the point
994         if (merging_focus ||
995             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
996         {
997             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
998         }
999     }
1002 /**
1003 Checks if the dragger has a draggable with this point_type
1004  */
1005 bool
1006 GrDragger::isA (gint point_type)
1008     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1009         GrDraggable *draggable = (GrDraggable *) i->data;
1010         if (draggable->point_type == point_type) {
1011             return true;
1012         }
1013     }
1014     return false;
1017 /**
1018 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1019  */
1020 bool
1021 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1023     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1024         GrDraggable *draggable = (GrDraggable *) i->data;
1025         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1026             return true;
1027         }
1028     }
1029     return false;
1032 /**
1033 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1034  */
1035 bool
1036 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1038     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1039         GrDraggable *draggable = (GrDraggable *) i->data;
1040         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1041             return true;
1042         }
1043     }
1044     return false;
1047 bool
1048 GrDraggable::mayMerge (GrDraggable *da2)
1050     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1051         // we must not merge the points of the same gradient!
1052         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1053               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1054             // except that we can snap center and focus together
1055             return false;
1056         }
1057     }
1058     // disable merging of midpoints.
1059     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1060          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1061          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1062         return false;
1064     return true;
1067 bool
1068 GrDragger::mayMerge (GrDragger *other)
1070     if (this == other)
1071         return false;
1073     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1074         GrDraggable *da1 = (GrDraggable *) i->data;
1075         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1076             GrDraggable *da2 = (GrDraggable *) j->data;
1077             if (!da1->mayMerge(da2))
1078                 return false;
1079         }
1080     }
1081     return true;
1084 bool
1085 GrDragger::mayMerge (GrDraggable *da2)
1087     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1088         GrDraggable *da1 = (GrDraggable *) i->data;
1089         if (!da1->mayMerge(da2))
1090             return false;
1091     }
1092     return true;
1095 /**
1096 Updates the statusbar tip of the dragger knot, based on its draggables
1097  */
1098 void
1099 GrDragger::updateTip ()
1101         if (this->knot && this->knot->tip) {
1102                 g_free (this->knot->tip);
1103                 this->knot->tip = NULL;
1104         }
1106     if (g_slist_length (this->draggables) == 1) {
1107         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1108         char *item_desc = sp_item_description(draggable->item);
1109         switch (draggable->point_type) {
1110             case POINT_LG_MID:
1111             case POINT_RG_MID1:
1112             case POINT_RG_MID2:
1113                 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"),
1114                                                    _(gr_knot_descr[draggable->point_type]),
1115                                                    draggable->point_i,
1116                                                    item_desc,
1117                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1118                 break;
1120             default:
1121                 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"),
1122                                                    _(gr_knot_descr[draggable->point_type]),
1123                                                    item_desc,
1124                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1125                 break;
1126         }
1127         g_free(item_desc);
1128     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1129         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1130     } else {
1131         int length = g_slist_length (this->draggables);
1132         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1133                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1134                                                     length),
1135                                            length);
1136     }
1139 /**
1140 Adds a draggable to the dragger
1141  */
1142 void
1143 GrDragger::updateKnotShape ()
1145     if (!draggables)
1146         return;
1147     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1148     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1151 /**
1152 Adds a draggable to the dragger
1153  */
1154 void
1155 GrDragger::addDraggable (GrDraggable *draggable)
1157     this->draggables = g_slist_prepend (this->draggables, draggable);
1159     this->updateTip();
1163 /**
1164 Moves this dragger to the point of the given draggable, acting upon all other draggables
1165  */
1166 void
1167 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1169     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1170     this->point_original = this->point;
1172     sp_knot_moveto (this->knot, &(this->point));
1174     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1175         GrDraggable *da = (GrDraggable *) i->data;
1176         if ( (da->item == item) &&
1177              (point_type == -1 || da->point_type == point_type) &&
1178              (point_i == -1 || da->point_i == point_i) &&
1179              (da->fill_or_stroke == fill_or_stroke) ) {
1180             continue;
1181         }
1182         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1183     }
1184     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1188 /**
1189 Moves all midstop draggables that depend on this one
1190  */
1191 void
1192 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1193     SPObject *server = draggable->getServer();
1194     if (!server)
1195         return;
1196     guint num = SP_GRADIENT(server)->vector.stops.size();
1197     if (num <= 2) return;
1199     if ( SP_IS_LINEARGRADIENT(server) ) {
1200         for ( guint i = 1; i < num - 1; i++ ) {
1201             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1202         }
1203     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1204         for ( guint i = 1; i < num - 1; i++ ) {
1205             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1206             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1207         }
1208     }
1212 /**
1213 Moves all draggables that depend on this one
1214  */
1215 void
1216 GrDragger::updateDependencies (bool write_repr)
1218     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1219         GrDraggable *draggable = (GrDraggable *) i->data;
1220         switch (draggable->point_type) {
1221             case POINT_LG_BEGIN:
1222                 {
1223                     // the end point is dependent only when dragging with ctrl+shift
1224                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1226                     this->updateMidstopDependencies (draggable, write_repr);
1227                 }
1228                 break;
1229             case POINT_LG_END:
1230                 {
1231                     // the begin point is dependent only when dragging with ctrl+shift
1232                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1234                     this->updateMidstopDependencies (draggable, write_repr);
1235                 }
1236                 break;
1237             case POINT_LG_MID:
1238                 // no other nodes depend on mid points.
1239                 break;
1240             case POINT_RG_R2:
1241                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1242                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1243                 this->updateMidstopDependencies (draggable, write_repr);
1244                 break;
1245             case POINT_RG_R1:
1246                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1247                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1248                 this->updateMidstopDependencies (draggable, write_repr);
1249                 break;
1250             case POINT_RG_CENTER:
1251                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1252                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1253                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1254                 this->updateMidstopDependencies (draggable, write_repr);
1255                 break;
1256             case POINT_RG_FOCUS:
1257                 // nothing can depend on that
1258                 break;
1259             case POINT_RG_MID1:
1260                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1261                 break;
1262             case POINT_RG_MID2:
1263                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1264                 break;
1265             default:
1266                 break;
1267         }
1268     }
1273 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1275     this->draggables = NULL;
1277     this->parent = parent;
1279     this->point = p;
1280     this->point_original = p;
1282     // create the knot
1283     this->knot = sp_knot_new (parent->desktop, NULL);
1284     this->knot->setMode(SP_KNOT_MODE_XOR);
1285     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1286     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1287     sp_knot_update_ctrl(this->knot);
1289     // move knot to the given point
1290     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1291     sp_knot_show (this->knot);
1293     // connect knot's signals
1294     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1295                        // luckily, midstops never snap to other nodes so are never unsnapped...
1296          && ( (draggable->point_type == POINT_LG_MID)
1297               || (draggable->point_type == POINT_RG_MID1)
1298               || (draggable->point_type == POINT_RG_MID2) ) )
1299     {
1300         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1301     } else {
1302         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1303     }
1304     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1305     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1306     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1307     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1309     // add the initial draggable
1310     if (draggable)
1311         this->addDraggable (draggable);
1312     updateKnotShape();
1315 GrDragger::~GrDragger ()
1317     // unselect if it was selected
1318     this->parent->setDeselected(this);
1320     // disconnect signals
1321     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1322     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1323     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1324     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1325     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1327     /* unref should call destroy */
1328     g_object_unref (G_OBJECT (this->knot));
1330     // delete all draggables
1331     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1332         delete ((GrDraggable *) i->data);
1333     }
1334     g_slist_free (this->draggables);
1335     this->draggables = NULL;
1338 /**
1339 Select the dragger which has the given draggable.
1340 */
1341 GrDragger *
1342 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1344     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1345         GrDragger *dragger = (GrDragger *) i->data;
1346         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1347             GrDraggable *da2 = (GrDraggable *) j->data;
1348             if ( (da2->item == item) &&
1349                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1350                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1351                  (da2->fill_or_stroke == fill_or_stroke)) {
1352                 return (dragger);
1353             }
1354         }
1355     }
1356     return NULL;
1360 void
1361 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1363     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1364     if (d && d !=  this) {
1365         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1366     }
1370 /**
1371   Draw this dragger as selected
1372 */
1373 void
1374 GrDragger::select()
1376     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1377     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1380 /**
1381   Draw this dragger as normal (deselected)
1382 */
1383 void
1384 GrDragger::deselect()
1386     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1387     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1390 bool
1391 GrDragger::isSelected()
1393     return g_list_find (parent->selected, this);
1396 /**
1397 \brief Deselect all stops/draggers (private)
1398 */
1399 void
1400 GrDrag::deselect_all()
1402     while (selected) {
1403         ( (GrDragger*) selected->data)->deselect();
1404         selected = g_list_remove(selected, selected->data);
1405     }
1408 /**
1409 \brief Deselect all stops/draggers (public; emits signal)
1410 */
1411 void
1412 GrDrag::deselectAll()
1414     deselect_all();
1415     this->desktop->emitToolSubselectionChanged(NULL);
1418 /**
1419 \brief Select all stops/draggers
1420 */
1421 void
1422 GrDrag::selectAll()
1424     for (GList *l = this->draggers; l != NULL; l = l->next) {
1425         GrDragger *d = ((GrDragger *) l->data);
1426         setSelected (d, true, true);
1427     }
1430 /**
1431 \brief Select all stops/draggers that match the coords
1432 */
1433 void
1434 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1436     for (GList *l = this->draggers; l != NULL; l = l->next) {
1437         GrDragger *d = ((GrDragger *) l->data);
1438         for (guint k = 0; k < coords.size(); k++) {
1439             if (NR::L2 (d->point - coords[k]) < 1e-4) {
1440                 setSelected (d, true, true);
1441             }
1442         }
1443     }
1447 /**
1448 \brief Select all stops/draggers that fall within the rect
1449 */
1450 void
1451 GrDrag::selectRect(NR::Rect const &r)
1453     for (GList *l = this->draggers; l != NULL; l = l->next) {
1454         GrDragger *d = ((GrDragger *) l->data);
1455         if (r.contains(d->point)) {
1456            setSelected (d, true, true);
1457         }
1458     }
1461 /**
1462 \brief Select a dragger
1463 \param dragger       The dragger to select
1464 \param add_to_selection   If true, add to selection, otherwise deselect others
1465 \param override      If true, always select this node, otherwise toggle selected status
1466 */
1467 void
1468 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1470     GrDragger *seldragger = NULL;
1472     if (add_to_selection) {
1473         if (!dragger) return;
1474         if (override) {
1475             if (!g_list_find(selected, dragger)) {
1476                 selected = g_list_prepend(selected, dragger);
1477             }
1478             dragger->select();
1479             seldragger = dragger;
1480         } else { // toggle
1481             if (g_list_find(selected, dragger)) {
1482                 selected = g_list_remove(selected, dragger);
1483                 dragger->deselect();
1484                 if (selected) {
1485                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1486                 }
1487             } else {
1488                 selected = g_list_prepend(selected, dragger);
1489                 dragger->select();
1490                 seldragger = dragger;
1491             }
1492         }
1493     } else {
1494         deselect_all();
1495         if (dragger) {
1496             selected = g_list_prepend(selected, dragger);
1497             dragger->select();
1498             seldragger = dragger;
1499         }
1500     }
1501     if (seldragger) {
1502         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1503     }
1506 /**
1507 \brief Deselect a dragger
1508 \param dragger       The dragger to deselect
1509 */
1510 void
1511 GrDrag::setDeselected (GrDragger *dragger)
1513     if (g_list_find(selected, dragger)) {
1514         selected = g_list_remove(selected, dragger);
1515         dragger->deselect();
1516     }
1517     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1522 /**
1523 Create a line from p1 to p2 and add it to the lines list
1524  */
1525 void
1526 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1528     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1529                                                             SP_TYPE_CTRLLINE, NULL);
1530     SP_CTRLLINE(line)->item = item;
1531     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1532     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1533         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1534     sp_canvas_item_show (line);
1535     this->lines = g_slist_append (this->lines, line);
1538 /**
1539 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1540 new dragger and add it to draggers list
1541  */
1542 void
1543 GrDrag::addDragger (GrDraggable *draggable)
1545     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1547     for (GList *i = this->draggers; i != NULL; i = i->next) {
1548         GrDragger *dragger = (GrDragger *) i->data;
1549         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1550             // distance is small, merge this draggable into dragger, no need to create new dragger
1551             dragger->addDraggable (draggable);
1552             dragger->updateKnotShape();
1553             return;
1554         }
1555     }
1557     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1558     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1559     this->draggers = g_list_append (this->draggers, new_dragger);
1562 /**
1563 Add draggers for the radial gradient rg on item
1564 */
1565 void
1566 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1568     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1569     guint num = rg->vector.stops.size();
1570     if (num > 2) {
1571         for ( guint i = 1; i < num - 1; i++ ) {
1572             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1573         }
1574     }
1575     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1576     if (num > 2) {
1577         for ( guint i = 1; i < num - 1; i++ ) {
1578             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1579         }
1580     }
1581     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1582     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1585 /**
1586 Add draggers for the linear gradient lg on item
1587 */
1588 void
1589 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1591     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1592     guint num = lg->vector.stops.size();
1593     if (num > 2) {
1594         for ( guint i = 1; i < num - 1; i++ ) {
1595             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1596         }
1597     }
1598     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1601 /**
1602 Artificially grab the knot of this dragger; used by the gradient context
1603 */
1604 void
1605 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1607     if (dragger) {
1608         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1609     }
1612 /**
1613 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1614 */
1615 void
1616 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1618     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1619     if (dragger) {
1620         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1621     }
1624 /**
1625 Regenerates the draggers list from the current selection; is called when selection is changed or
1626 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1627 */
1628 void
1629 GrDrag::updateDraggers ()
1631     while (selected) {
1632         selected = g_list_remove(selected, selected->data);
1633     }
1634     // delete old draggers
1635     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1636         delete ((GrDragger *) i->data);
1637     }
1638     g_list_free (this->draggers);
1639     this->draggers = NULL;
1641     g_return_if_fail (this->selection != NULL);
1643     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1645         SPItem *item = SP_ITEM(i->data);
1646         SPStyle *style = SP_OBJECT_STYLE (item);
1648         if (style && (style->fill.isPaintserver())) {
1649             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1650             if (SP_IS_LINEARGRADIENT (server)) {
1651                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1652             } else if (SP_IS_RADIALGRADIENT (server)) {
1653                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1654             }
1655         }
1657         if (style && (style->stroke.isPaintserver())) {
1658             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1659             if (SP_IS_LINEARGRADIENT (server)) {
1660                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1661             } else if (SP_IS_RADIALGRADIENT (server)) {
1662                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1663             }
1664         }
1665     }
1668 /**
1669 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1670 lines are always in sync with the actual gradient
1671 */
1672 void
1673 GrDrag::updateLines ()
1675     // delete old lines
1676     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1677         gtk_object_destroy( GTK_OBJECT (i->data));
1678     }
1679     g_slist_free (this->lines);
1680     this->lines = NULL;
1682     g_return_if_fail (this->selection != NULL);
1684     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1686         SPItem *item = SP_ITEM(i->data);
1688         SPStyle *style = SP_OBJECT_STYLE (item);
1690         if (style && (style->fill.isPaintserver())) {
1691             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1692             if (SP_IS_LINEARGRADIENT (server)) {
1693                 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);
1694             } else if (SP_IS_RADIALGRADIENT (server)) {
1695                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1696                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1697                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1698             }
1699         }
1701         if (style && (style->stroke.isPaintserver())) {
1702             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1703             if (SP_IS_LINEARGRADIENT (server)) {
1704                 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);
1705             } else if (SP_IS_RADIALGRADIENT (server)) {
1706                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1707                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1708                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1709             }
1710         }
1711     }
1714 /**
1715 Regenerates the levels list from the current selection
1716 */
1717 void
1718 GrDrag::updateLevels ()
1720     hor_levels.clear();
1721     vert_levels.clear();
1723     g_return_if_fail (this->selection != NULL);
1725     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1726         SPItem *item = SP_ITEM(i->data);
1727         NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1728         if (rect) {
1729             // Remember the edges of the bbox and the center axis
1730             hor_levels.push_back(rect->min()[NR::Y]);
1731             hor_levels.push_back(rect->max()[NR::Y]);
1732             hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1733             vert_levels.push_back(rect->min()[NR::X]);
1734             vert_levels.push_back(rect->max()[NR::X]);
1735             vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1736         }
1737     }
1740 void
1741 GrDrag::selected_reverse_vector ()
1743     if (selected == NULL)
1744         return;
1746     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1747         GrDraggable *draggable = (GrDraggable *) i->data;
1749         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1750     }
1753 void
1754 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1756     selected_move (x, y, false, scale_radial);
1759 void
1760 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1762     if (selected == NULL)
1763         return;
1765     bool did = false;
1767     for (GList *i = selected; i != NULL; i = i->next) {
1768         GrDragger *d = (GrDragger *) i->data;
1770         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1771             // if this is an endpoint,
1773             // Moving an rg center moves its focus and radii as well.
1774             // therefore, if this is a focus or radius and if selection
1775             // contains the center as well, do not move this one
1776             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1777                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1778                 bool skip_radius_with_center = false;
1779                 for (GList *di = selected; di != NULL; di = di->next) {
1780                     GrDragger *d_new = (GrDragger *) di->data;
1781                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1782                                     POINT_RG_CENTER,
1783                                     0,
1784                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1785                         // FIXME: here we take into account only the first draggable!
1786                         skip_radius_with_center = true;
1787                     }
1788                 }
1789                 if (skip_radius_with_center)
1790                     continue;
1791             }
1793             did = true;
1794             d->point += NR::Point (x, y);
1795             d->point_original = d->point;
1796             sp_knot_moveto (d->knot, &(d->point));
1798             d->fireDraggables (write_repr, scale_radial);
1800             d->updateDependencies(write_repr);
1801         }
1802     }
1804     if (write_repr && did) {
1805         // we did an undoable action
1806         sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1807                                 _("Move gradient handle(s)"));
1808         return;
1809     }
1811     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1813         GrDragger *dragger = (GrDragger *) selected->data;
1814         // a midpoint dragger can (logically) only contain one GrDraggable
1815         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1817         NR::Point begin(0,0), end(0,0);
1818         NR::Point low_lim(0,0), high_lim(0,0);
1820         SPObject *server = draggable->getServer();
1821         GSList *moving = NULL;
1822         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1824         NR::Point p(x, y);
1825         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1826         NR::Point displacement = p - dragger->point;
1828         for (GSList const* i = moving; i != NULL; i = i->next) {
1829             GrDragger *drg = (GrDragger*) i->data;
1830             SPKnot *drgknot = drg->knot;
1831             drg->point += displacement;
1832             sp_knot_moveto (drgknot, & drg->point);
1833             drg->fireDraggables (true);
1834             drg->updateDependencies(true);
1835             did = true;
1836         }
1838         g_slist_free(moving);
1840         if (write_repr && did) {
1841             // we did an undoable action
1842             sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1843                                     _("Move gradient mid stop(s)"));
1844         }
1845     }
1848 void
1849 GrDrag::selected_move_screen (double x, double y)
1851     gdouble zoom = desktop->current_zoom();
1852     gdouble zx = x / zoom;
1853     gdouble zy = y / zoom;
1855     selected_move (zx, zy);
1858 /**
1859 Select the knot next to the last selected one and deselect all other selected.
1860 */
1861 GrDragger *
1862 GrDrag::select_next ()
1864     GrDragger *d = NULL;
1865     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1866         if (draggers)
1867             d = (GrDragger *) draggers->data;
1868     } else {
1869         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1870     }
1871     if (d)
1872         setSelected (d);
1873     return d;
1876 /**
1877 Select the knot previous from the last selected one and deselect all other selected.
1878 */
1879 GrDragger *
1880 GrDrag::select_prev ()
1882     GrDragger *d = NULL;
1883     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1884         if (draggers)
1885             d = (GrDragger *) g_list_last (draggers)->data;
1886     } else {
1887         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1888     }
1889     if (d)
1890         setSelected (d);
1891     return d;
1895 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1896 void
1897 GrDrag::deleteSelected (bool just_one)
1899     if (!selected) return;
1901     SPDocument *document = false;
1903     struct StructStopInfo {
1904         SPStop * spstop;
1905         GrDraggable * draggable;
1906         SPGradient * gradient;
1907         SPGradient * vector;
1908     };
1910     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1911     GSList *endstoplist = NULL;  // list of stops that must be deleted
1912     while (selected) {
1913         GrDragger *dragger = (GrDragger*) selected->data;
1914         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1915             GrDraggable *draggable = (GrDraggable*) drgble->data;
1916             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1917             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1919             switch (draggable->point_type) {
1920                 case POINT_LG_MID:
1921                 case POINT_RG_MID1:
1922                 case POINT_RG_MID2:
1923                     {
1924                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1925                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1926                         bool present = false;
1927                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1928                             if ( (SPStop*)l->data == stop ) {
1929                                 present = true;
1930                                 break; // no need to search further.
1931                             }
1932                         }
1933                         if (!present)
1934                             midstoplist = g_slist_append(midstoplist, stop);
1935                     }
1936                     break;
1937                 case POINT_LG_BEGIN:
1938                 case POINT_LG_END:
1939                 case POINT_RG_CENTER:
1940                 case POINT_RG_R1:
1941                 case POINT_RG_R2:
1942                     {
1943                         SPStop *stop = NULL;
1944                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1945                             stop = sp_first_stop(vector);
1946                         } else {
1947                             stop = sp_last_stop(vector);
1948                         }
1949                         if (stop) {
1950                             StructStopInfo *stopinfo = new StructStopInfo;
1951                             stopinfo->spstop = stop;
1952                             stopinfo->draggable = draggable;
1953                             stopinfo->gradient = gradient;
1954                             stopinfo->vector = vector;
1955                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1956                             bool present = false;
1957                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1958                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1959                                     present = true;
1960                                     break; // no need to search further.
1961                                 }
1962                             }
1963                             if (!present)
1964                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1965                         }
1966                     }
1967                     break;
1968                 default:
1969                     break;
1970             }
1971         }
1972         selected = g_list_remove(selected, dragger);
1973         if ( just_one ) break; // iterate once if just_one is set.
1974     }
1975     while (midstoplist) {
1976         SPStop *stop = (SPStop*) midstoplist->data;
1977         document = SP_OBJECT_DOCUMENT (stop);
1978         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1979         parent->removeChild(SP_OBJECT_REPR(stop));
1980         midstoplist = g_slist_remove(midstoplist, stop);
1981     }
1982     while (endstoplist) {
1983         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1984         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1986         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1987         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1988         // manually count the children, don't know if there already exists a function for this...
1989         int len = 0;
1990         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1991               child != NULL ;
1992               child = SP_OBJECT_NEXT(child) )
1993         {
1994             if ( SP_IS_STOP(child) )  len ++;
1995         }
1996         if (len > 2)
1997         {
1998             switch (stopinfo->draggable->point_type) {
1999                 case POINT_LG_BEGIN:
2000                     {
2001                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2003                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2004                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
2005                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
2006                         SPStop *stop = sp_first_stop(stopinfo->vector);
2007                         gdouble offset = stop->offset;
2008                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
2009                         lg->x1.computed = newbegin[NR::X];
2010                         lg->y1.computed = newbegin[NR::Y];
2012                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2013                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2014                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2015                         stop->offset = 0;
2016                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2018                         // iterate through midstops to set new offset values such that they won't move on canvas.
2019                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2020                         stop = sp_next_stop(stop);
2021                         while ( stop != laststop ) {
2022                             stop->offset = (stop->offset - offset)/(1 - offset);
2023                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2024                             stop = sp_next_stop(stop);
2025                         }
2026                     }
2027                     break;
2028                 case POINT_LG_END:
2029                     {
2030                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2032                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2033                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
2034                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
2035                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2036                         gdouble offset = laststop->offset;
2037                         NR::Point newend = begin + offset * (oldend - begin);
2038                         lg->x2.computed = newend[NR::X];
2039                         lg->y2.computed = newend[NR::Y];
2041                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2042                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2043                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2044                         laststop->offset = 1;
2045                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2047                         // iterate through midstops to set new offset values such that they won't move on canvas.
2048                         SPStop *stop = sp_first_stop(stopinfo->vector);
2049                         stop = sp_next_stop(stop);
2050                         while ( stop != laststop ) {
2051                             stop->offset = stop->offset / offset;
2052                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2053                             stop = sp_next_stop(stop);
2054                         }
2055                     }
2056                     break;
2057                 case POINT_RG_CENTER:
2058                     {
2059                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2060                         if (newfirst) {
2061                             newfirst->offset = 0;
2062                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2063                         }
2064                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2065                     }
2066                     break;
2067                 case POINT_RG_R1:
2068                 case POINT_RG_R2:
2069                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2071                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2072                         double oldradius = rg->r.computed;
2073                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2074                         gdouble offset = laststop->offset;
2075                         double newradius = offset * oldradius;
2076                         rg->r.computed = newradius;
2078                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2079                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2080                         laststop->offset = 1;
2081                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2083                         // iterate through midstops to set new offset values such that they won't move on canvas.
2084                         SPStop *stop = sp_first_stop(stopinfo->vector);
2085                         stop = sp_next_stop(stop);
2086                         while ( stop != laststop ) {
2087                             stop->offset = stop->offset / offset;
2088                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2089                             stop = sp_next_stop(stop);
2090                         }
2091                         break;
2092             }
2093         }
2094         else
2095         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2096             SPCSSAttr *css = sp_repr_css_attr_new ();
2098             // stopinfo->spstop is the selected stop
2099             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2100             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2101                 unselectedrepr = unselectedrepr->next();
2102             }
2104             if (unselectedrepr == NULL) {
2105                 if (stopinfo->draggable->fill_or_stroke) {
2106                     sp_repr_css_unset_property (css, "fill");
2107                 } else {
2108                     sp_repr_css_unset_property (css, "stroke");
2109                 }
2110             } else {
2111                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2112                 if (stopinfo->draggable->fill_or_stroke) {
2113                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2114                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2115                 } else {
2116                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2117                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2118                 }
2119                 sp_repr_css_attr_unref (stopcss);
2120             }
2122             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2123             sp_repr_css_attr_unref (css);
2124         }
2126         endstoplist = g_slist_remove(endstoplist, stopinfo);
2127         delete stopinfo;
2128     }
2130     if (document) {
2131         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2132     }
2136 /*
2137   Local Variables:
2138   mode:c++
2139   c-file-style:"stroustrup"
2140   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2141   indent-tabs-mode:nil
2142   fill-column:99
2143   End:
2144 */
2145 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :