Code

copyedit
[inkscape.git] / src / gradient-drag.cpp
1 #define __GRADIENT_DRAG_C__
3 /*
4  * On-canvas gradient dragging
5  *
6  * Authors:
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2007 Johan Engelen
11  * Copyright (C) 2005 Authors
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include <glibmm/i18n.h>
22 #include "desktop-handles.h"
23 #include "selection.h"
24 #include "desktop.h"
25 #include "desktop-style.h"
26 #include "document.h"
27 #include "display/sp-ctrlline.h"
29 #include "xml/repr.h"
30 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
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"
44 #include "snap.h"
45 #include "sp-namedview.h"
48 #define GR_KNOT_COLOR_NORMAL 0xffffff00
49 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
51 #define GR_LINE_COLOR_FILL 0x0000ff7f
52 #define GR_LINE_COLOR_STROKE 0x9999007f
54 // screen pixels between knots when they snap:
55 #define SNAP_DIST 5
57 // absolute distance between gradient points for them to become a single dragger when the drag is created:
58 #define MERGE_DIST 0.1
60 // knot shapes corresponding to GrPointType enum
61 SPKnotShapeType gr_knot_shapes [] = {
62         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
63         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
64         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
65         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
66         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
67         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
68         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
69         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
70         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
71 };
73 const gchar *gr_knot_descr [] = {
74     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
75     N_("Linear gradient <b>end</b>"),
76     N_("Linear gradient <b>mid stop</b>"),
77     N_("Radial gradient <b>center</b>"),
78     N_("Radial gradient <b>radius</b>"),
79     N_("Radial gradient <b>radius</b>"),
80     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
81     N_("Radial gradient <b>mid stop</b>"),
82     N_("Radial gradient <b>mid stop</b>")
83 };
85 static void
86 gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
87 {
88         GrDrag *drag = (GrDrag *) data;
89         drag->updateDraggers ();
90         drag->updateLines ();
91         drag->updateLevels ();
92 }
94 static void
95 gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
96 {
97     GrDrag *drag = (GrDrag *) data;
98     if (drag->local_change) {
99         drag->local_change = false;
100     } else {
101         drag->updateDraggers ();
102     }
103     drag->updateLines ();
104     drag->updateLevels ();
107 /**
108 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
109 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
110 any.
111 */
112 int
113 gr_drag_style_query (SPStyle *style, int property, gpointer data)
115     GrDrag *drag = (GrDrag *) data;
117     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
118         return QUERY_STYLE_NOTHING;
119     }
121     if (!drag->selected) {
122         return QUERY_STYLE_NOTHING;
123     } else {
124         int ret = QUERY_STYLE_NOTHING;
126         float cf[4];
127         cf[0] = cf[1] = cf[2] = cf[3] = 0;
129         int count = 0;
131         for (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger
132             GrDraggable *draggable = (GrDraggable *) i->data;
134             if (ret == QUERY_STYLE_NOTHING) {
135                 ret = QUERY_STYLE_SINGLE;
136             } else if (ret == QUERY_STYLE_SINGLE) {
137                 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
138             }
140             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
141             cf[0] += SP_RGBA32_R_F (c);
142             cf[1] += SP_RGBA32_G_F (c);
143             cf[2] += SP_RGBA32_B_F (c);
144             cf[3] += SP_RGBA32_A_F (c);
146             count ++;
147         }
149         if (count) {
150             cf[0] /= count;
151             cf[1] /= count;
152             cf[2] /= count;
153             cf[3] /= count;
155             // set both fill and stroke with our stop-color and stop-opacity
156             style->fill.clear();
157             style->fill.setColor( cf[0], cf[1], cf[2] );
158             style->fill.set = TRUE;
159             style->stroke.clear();
160             style->stroke.setColor( cf[0], cf[1], cf[2] );
161             style->stroke.set = TRUE;
163             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
164             style->fill_opacity.set = TRUE;
165             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
166             style->stroke_opacity.set = TRUE;
168             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
169             style->opacity.set = TRUE;
170         }
172         return ret;
173     }
176 bool
177 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
179     GrDrag *drag = (GrDrag *) data;
181     if (!drag->selected)
182         return false;
184     SPCSSAttr *stop = sp_repr_css_attr_new ();
186     // See if the css contains interesting properties, and if so, translate them into the format
187     // acceptable for gradient stops
189     // any of color properties, in order of increasing priority:
190     if (css->attribute("flood-color"))
191         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
193     if (css->attribute("lighting-color"))
194         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
196     if (css->attribute("color"))
197         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
199     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
200         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
202     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
203         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
205     if (css->attribute("stop-color"))
206         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
209     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
210         sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
211     } else {  // multiply all opacity properties:
212         gdouble accumulated = 1.0;
213         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
214         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
215         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
216         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
218         Inkscape::CSSOStringStream os;
219         os << accumulated;
220         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
222         if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
223             (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
224             sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
225     }
227     if (!stop->attributeList()) { // nothing for us here, pass it on
228         sp_repr_css_attr_unref(stop);
229         return false;
230     }
232     for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
233         GrDragger* dragger = (GrDragger*) sel->data;
234         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
235                GrDraggable *draggable = (GrDraggable *) i->data;
237                drag->local_change = true;
238                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
239         }
240     }
242     //sp_repr_css_print(stop);
243     sp_repr_css_attr_unref(stop);
244     return true;
247 GrDrag::GrDrag(SPDesktop *desktop) {
249     this->desktop = desktop;
251     this->selection = sp_desktop_selection(desktop);
253     this->draggers = NULL;
254     this->lines = NULL;
255     this->selected = NULL;
257     this->hor_levels.clear();
258     this->vert_levels.clear();
260     this->local_change = false;
262     this->sel_changed_connection = this->selection->connectChanged(
263         sigc::bind (
264             sigc::ptr_fun(&gr_drag_sel_changed),
265             (gpointer)this )
267         );
268     this->sel_modified_connection = this->selection->connectModified(
269         sigc::bind(
270             sigc::ptr_fun(&gr_drag_sel_modified),
271             (gpointer)this )
272         );
274     this->style_set_connection = this->desktop->connectSetStyle(
275         sigc::bind(
276             sigc::ptr_fun(&gr_drag_style_set),
277             (gpointer)this )
278         );
280     this->style_query_connection = this->desktop->connectQueryStyle(
281         sigc::bind(
282             sigc::ptr_fun(&gr_drag_style_query),
283             (gpointer)this )
284         );
286     this->updateDraggers ();
287     this->updateLines ();
288     this->updateLevels ();
290     if (desktop->gr_item) {
291         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
292     }
295 GrDrag::~GrDrag()
297     this->sel_changed_connection.disconnect();
298     this->sel_modified_connection.disconnect();
299     this->style_set_connection.disconnect();
300     this->style_query_connection.disconnect();
302     if (this->selected) {
303         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
304         desktop->gr_item = draggable->item;
305         desktop->gr_point_type = draggable->point_type;
306         desktop->gr_point_i = draggable->point_i;
307         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
308     } else {
309         desktop->gr_item = NULL;
310         desktop->gr_point_type = 0;
311         desktop->gr_point_i = 0;
312         desktop->gr_fill_or_stroke = true;
313     }
315     deselect_all();
316     for (GList *l = this->draggers; l != NULL; l = l->next) {
317         delete ((GrDragger *) l->data);
318     }
319     g_list_free (this->draggers);
320     this->draggers = NULL;
321     this->selected = NULL;
323     for (GSList *l = this->lines; l != NULL; l = l->next) {
324         gtk_object_destroy( GTK_OBJECT (l->data));
325     }
326     g_slist_free (this->lines);
327     this->lines = NULL;
330 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
332     this->item = item;
333     this->point_type = point_type;
334     this->point_i = point_i;
335     this->fill_or_stroke = fill_or_stroke;
337     g_object_ref (G_OBJECT (this->item));
340 GrDraggable::~GrDraggable ()
342     g_object_unref (G_OBJECT (this->item));
345 // FIXME: make global function in libnr or somewhere.
346 static NR::Point *
347 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
349     double r = NR::L2 (p - o);
350     if (r < 1e-3)
351         return NULL;
352     double angle = NR::atan2 (p - o);
353     // snap angle to snaps increments, starting from initial:
354     double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
355     // calculate the new position and subtract p to get the vector:
356     return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
359 // FIXME: make global function in libnr or somewhere.
360 static NR::Point
361 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
363     double length = NR::L2(end - begin);
364     NR::Point be = (end - begin) / length;
365     double r = NR::dot(p - begin, be);
367     if (r < 0.0) return begin;
368     if (r > length) return end;
370     double snapdist = length * snap;
371     double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
373     return (begin + r_snapped * be);
376 static void
377 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
379     GrDragger *dragger = (GrDragger *) data;
380     GrDrag *drag = dragger->parent;
382     NR::Point p = *ppointer;
384     // FIXME: take from prefs
385     double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
387     if (state & GDK_SHIFT_MASK) {
388         // with Shift; unsnap if we carry more than one draggable
389         if (dragger->draggables && dragger->draggables->next) {
390             // create a new dragger
391             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
392             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
393             // relink to it all but the first draggable in the list
394             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
395                 GrDraggable *draggable = (GrDraggable *) i->data;
396                 dr_new->addDraggable (draggable);
397             }
398             dr_new->updateKnotShape();
399             g_slist_free (dragger->draggables->next);
400             dragger->draggables->next = NULL;
401             dragger->updateKnotShape();
402             dragger->updateTip();
403         }
404     } else if (!(state & GDK_CONTROL_MASK)) {
405         // without Shift or Ctrl; see if we need to snap to another dragger
406         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
407             GrDragger *d_new = (GrDragger *) di->data;
408             if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
410                 // Merge draggers:
411                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
412                     GrDraggable *draggable = (GrDraggable *) i->data;
413                     // copy draggable to d_new:
414                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
415                     d_new->addDraggable (da_new);
416                 }
418                 // unlink and delete this dragger
419                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
420                 delete dragger;
422                 // update the new merged dragger
423                 d_new->fireDraggables(true, false, true);
424                 d_new->parent->updateLines();
425                 d_new->parent->setSelected (d_new);
426                 d_new->updateKnotShape ();
427                 d_new->updateTip ();
428                 d_new->updateDependencies(true);
429                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
430                                   _("Merge gradient handles"));
431                 return;
432             }
433         }
434     }
436     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
437         // Try snapping to the grid or guides
438         SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
439         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
440         if (s.getDistance() < 1e6) {
441             p = s.getPoint();
442             sp_knot_moveto (knot, &p);
443         } else {
444             // No snapping so far, let's see if we need to snap to any of the levels
445             for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
446                 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
447                     p[NR::Y] = dragger->parent->hor_levels[i];
448                     sp_knot_moveto (knot, &p);
449                 }
450             }
451             for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
452                 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
453                     p[NR::X] = dragger->parent->vert_levels[i];
454                     sp_knot_moveto (knot, &p);
455                 }
456             }
457         }
458     }
460     if (state & GDK_CONTROL_MASK) {
461         unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
462         /* 0 means no snapping. */
464         // This list will store snap vectors from all draggables of dragger
465         GSList *snap_vectors = NULL;
467         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
468             GrDraggable *draggable = (GrDraggable *) i->data;
470             NR::Point *dr_snap = NULL;
472             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
473                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
474                     GrDragger *d_new = (GrDragger *) di->data;
475                     if (d_new == dragger)
476                         continue;
477                     if (d_new->isA (draggable->item,
478                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
479                                     draggable->point_i,
480                                     draggable->fill_or_stroke)) {
481                         // found the other end of the linear gradient;
482                         if (state & GDK_SHIFT_MASK) {
483                             // moving linear around center
484                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
485                             dr_snap = &center;
486                         } else {
487                             // moving linear around the other end
488                             dr_snap = &d_new->point;
489                         }
490                     }
491                 }
492             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
493                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
494                     GrDragger *d_new = (GrDragger *) di->data;
495                     if (d_new == dragger)
496                         continue;
497                     if (d_new->isA (draggable->item,
498                                     POINT_RG_CENTER,
499                                     draggable->point_i,
500                                     draggable->fill_or_stroke)) {
501                         // found the center of the radial gradient;
502                         dr_snap = &(d_new->point);
503                     }
504                 }
505             } else if (draggable->point_type == POINT_RG_CENTER) {
506                 // radial center snaps to hor/vert relative to its original position
507                 dr_snap = &(dragger->point_original);
508             }
510             NR::Point *snap_vector = NULL;
511             if (dr_snap) {
512                 if (state & GDK_MOD1_MASK) {
513                     // with Alt, snap to the original angle and its perpendiculars
514                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
515                 } else {
516                     // with Ctrl, snap to M_PI/snaps
517                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
518                 }
519             }
520             if (snap_vector) {
521                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
522             }
523         }
525         // Move by the smallest of snap vectors:
526         NR::Point move(9999, 9999);
527         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
528             NR::Point *snap_vector = (NR::Point *) i->data;
529             if (NR::L2(*snap_vector) < NR::L2(move))
530                 move = *snap_vector;
531         }
532         if (move[NR::X] < 9999) {
533             p += move;
534             sp_knot_moveto (knot, &p);
535         }
537         g_slist_free(snap_vectors);
538     }
540     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
541     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
543     if (drag->keep_selection) {
544         NR::Point diff = p - dragger->point;
545         drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
546     } else {
547         dragger->point = p;
548         dragger->fireDraggables (false, scale_radial);
549         dragger->updateDependencies(false);
550     }
555 /**
556 Called when a midpoint knot is dragged.
557 */
558 static void
559 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
561     GrDragger *dragger = (GrDragger *) data;
562     GrDrag *drag = dragger->parent;
563     // a midpoint dragger can (logically) only contain one GrDraggable
564     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
566     // FIXME: take from prefs
567     double snap_fraction = 0.1;
569     NR::Point p = *ppointer;
570     NR::Point begin(0,0), end(0,0);
572     SPObject *server;
573     if (draggable->fill_or_stroke)
574         server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
575     else
576         server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
579     // get begin and end points between which dragging is allowed:
580     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
581     GSList *moving = NULL;
582     moving = g_slist_append(moving, dragger);
584     guint lowest_i = draggable->point_i;
585     guint highest_i = draggable->point_i;
586     GrDragger *lowest_dragger = dragger;
587     GrDragger *highest_dragger = dragger;
588     bool is_selected = dragger->isSelected();
589     if (is_selected) {
590         GrDragger* d_add;
591         while ( true )
592         {
593             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
594             if ( d_add && g_list_find(drag->selected, d_add) ) {
595                 lowest_i = lowest_i - 1;
596                 moving = g_slist_prepend(moving, d_add);
597                 lowest_dragger = d_add;
598             } else {
599                 break;
600             }
601         }
603         while ( true )
604         {
605             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
606             if ( d_add && g_list_find(drag->selected, d_add) ) {
607                 highest_i = highest_i + 1;
608                 moving = g_slist_append(moving, d_add);
609                 highest_dragger = d_add;
610             } else {
611                 break;
612             }
613         }
614     }
616     if ( SP_IS_LINEARGRADIENT(server) ) {
617         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
618         GrDragger *d_temp;
619         if (lowest_i == 1) {
620             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
621         } else {
622             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
623         }
624         if (d_temp) begin = d_temp->point;
626         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
627         if (d_temp == NULL) {
628             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
629         }
630         if (d_temp) end = d_temp->point;
631     } else if ( SP_IS_RADIALGRADIENT(server) ) {
632         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
633         GrDragger *d_temp;
634         if (lowest_i == 1) {
635             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
636         } else {
637             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
638         }
639         if (d_temp) begin = d_temp->point;
641         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
642         if (d_temp == NULL) {
643             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
644         }
645         if (d_temp) end = d_temp->point;
646     }
648     NR::Point low_lim  = dragger->point - (lowest_dragger->point - begin);
649     NR::Point high_lim = dragger->point - (highest_dragger->point - end);
651     if (state & GDK_CONTROL_MASK) {
652         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
653     } else {
654         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
655     }
656     NR::Point displacement = p - dragger->point;
658     for (GSList const* i = moving; i != NULL; i = i->next) {
659         GrDragger *drg = (GrDragger*) i->data;
660         SPKnot *drgknot = drg->knot;
661         NR::Point this_move = displacement;
662         if (state & GDK_MOD1_MASK) {
663             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
664             double alpha = 1.5;
665             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
666                 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
667                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
668             } else { // drg is on the begin side from dragger
669                 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
670                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
671             }
672         }
673         drg->point += this_move;
674         sp_knot_moveto (drgknot, & drg->point);
675         drg->fireDraggables (false);
676         drg->updateDependencies(false);
677     }
679     g_slist_free(moving);
681     drag->keep_selection = is_selected;
686 static void
687 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
689     GrDragger *dragger = (GrDragger *) data;
691     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
694 /**
695 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
696 */
697 static void
698 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
700     GrDragger *dragger = (GrDragger *) data;
702     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
704     dragger->point_original = dragger->point = knot->pos;
706     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
707         dragger->fireDraggables (true, true);
708     } else {
709         dragger->fireDraggables (true);
710     }
712     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
713         GrDragger *d = (GrDragger *) i->data;
714         if (d == dragger)
715             continue;
716         d->fireDraggables (true);
717     }
719     // make this dragger selected
720     if (!dragger->parent->keep_selection) {
721         dragger->parent->setSelected (dragger);
722     }
723     dragger->parent->keep_selection = false;
725     dragger->updateDependencies(true);
727     // we did an undoable action
728     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
729                       _("Move gradient handle"));
732 /**
733 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
734 state of the keyboard keys
735 */
736 static void
737 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
739     GrDragger *dragger = (GrDragger *) data;
740     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
741     if (!draggable) return;
743     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
744     // delete this knot from vector
745         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
746         gradient = sp_gradient_get_vector (gradient, false);
747         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
748                 SPStop *stop = NULL;
749                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
750                 case POINT_LG_BEGIN:
751                 case POINT_RG_CENTER:
752                     stop = sp_first_stop(gradient);
753                         {
754                             SPStop *next = sp_next_stop (stop);
755                                 if (next) {
756                                         next->offset = 0;
757                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
758                                 }
759                         }
760                     break;
761                 case POINT_LG_END:
762                 case POINT_RG_R1:
763                 case POINT_RG_R2:
764                     stop = sp_last_stop(gradient);
765                     {
766                             SPStop *prev = sp_prev_stop (stop, gradient);
767                             if (prev) {
768                                     prev->offset = 1;
769                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
770                             }
771                         }
772                     break;
773                 case POINT_LG_MID:
774                 case POINT_RG_MID1:
775                 case POINT_RG_MID2:
776                     stop = sp_get_stop_i(gradient, draggable->point_i);
777                     break;
778                 }
780                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
781                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
782                                   _("Delete gradient stop"));
783         }
784     } else {
785     // select the dragger
786         dragger->point_original = dragger->point;
788         if ( state & GDK_SHIFT_MASK ) {
789             dragger->parent->setSelected (dragger, true, false);
790         } else {
791             dragger->parent->setSelected (dragger);
792         }
793     }
796 /**
797 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
798 */
799 static void
800 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
802     GrDragger *dragger = (GrDragger *) data;
804     dragger->point_original = dragger->point;
806     if (dragger->draggables == NULL)
807         return;
809     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
810     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
813 /**
814 Act upon all draggables of the dragger, setting them to the dragger's point
815 */
816 void
817 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
819     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
820         GrDraggable *draggable = (GrDraggable *) i->data;
822         // set local_change flag so that selection_changed callback does not regenerate draggers
823         this->parent->local_change = true;
825         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
826         // to the center, unless it's the first update upon merge when we must snap it to the point
827         if (merging_focus ||
828             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
829         {
830             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
831         }
832     }
835 /**
836 Checks if the dragger has a draggable with this point_type
837  */
838 bool
839 GrDragger::isA (gint point_type)
841     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
842         GrDraggable *draggable = (GrDraggable *) i->data;
843         if (draggable->point_type == point_type) {
844             return true;
845         }
846     }
847     return false;
850 /**
851 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
852  */
853 bool
854 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
856     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
857         GrDraggable *draggable = (GrDraggable *) i->data;
858         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
859             return true;
860         }
861     }
862     return false;
865 bool
866 GrDraggable::mayMerge (GrDraggable *da2)
868     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
869         // we must not merge the points of the same gradient!
870         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
871               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
872             // except that we can snap center and focus together
873             return false;
874         }
875     }
876     // disable merging of midpoints.
877     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
878          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
879          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
880         return false;
882     return true;
885 bool
886 GrDragger::mayMerge (GrDragger *other)
888     if (this == other)
889         return false;
891     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
892         GrDraggable *da1 = (GrDraggable *) i->data;
893         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
894             GrDraggable *da2 = (GrDraggable *) j->data;
895             if (!da1->mayMerge(da2))
896                 return false;
897         }
898     }
899     return true;
902 bool
903 GrDragger::mayMerge (GrDraggable *da2)
905     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
906         GrDraggable *da1 = (GrDraggable *) i->data;
907         if (!da1->mayMerge(da2))
908             return false;
909     }
910     return true;
913 /**
914 Updates the statusbar tip of the dragger knot, based on its draggables
915  */
916 void
917 GrDragger::updateTip ()
919         if (this->knot && this->knot->tip) {
920                 g_free (this->knot->tip);
921                 this->knot->tip = NULL;
922         }
924     if (g_slist_length (this->draggables) == 1) {
925         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
926         char *item_desc = sp_item_description(draggable->item);
927         switch (draggable->point_type) {
928             case POINT_LG_MID:
929             case POINT_RG_MID1:
930             case POINT_RG_MID2:
931                 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"),
932                                                    _(gr_knot_descr[draggable->point_type]),
933                                                    draggable->point_i,
934                                                    item_desc,
935                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
936                 break;
938             default:
939                 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"),
940                                                    _(gr_knot_descr[draggable->point_type]),
941                                                    item_desc,
942                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
943                 break;
944         }
945         g_free(item_desc);
946     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
947         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
948     } else {
949         int length = g_slist_length (this->draggables);
950         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
951                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
952                                                     length),
953                                            length);
954     }
957 /**
958 Adds a draggable to the dragger
959  */
960 void
961 GrDragger::updateKnotShape ()
963     if (!draggables)
964         return;
965     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
966     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
969 /**
970 Adds a draggable to the dragger
971  */
972 void
973 GrDragger::addDraggable (GrDraggable *draggable)
975     this->draggables = g_slist_prepend (this->draggables, draggable);
977     this->updateTip();
981 /**
982 Moves this dragger to the point of the given draggable, acting upon all other draggables
983  */
984 void
985 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
987     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
988     this->point_original = this->point;
990     sp_knot_moveto (this->knot, &(this->point));
992     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
993         GrDraggable *da = (GrDraggable *) i->data;
994         if ( (da->item == item) && 
995              (point_type == -1 || da->point_type == point_type) &&
996              (point_i == -1 || da->point_i == point_i) &&
997              (da->fill_or_stroke == fill_or_stroke) ) {
998             continue;
999         }
1000         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1001     }
1002     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1006 /**
1007 Moves all midstop draggables that depend on this one
1008  */
1009 void
1010 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1011     SPObject *server;
1012     if (draggable->fill_or_stroke)
1013         server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
1014     else
1015         server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
1016     guint num = SP_GRADIENT(server)->vector.stops.size();
1017     if (num <= 2) return;
1019     if ( SP_IS_LINEARGRADIENT(server) ) {
1020         for ( guint i = 1; i < num - 1; i++ ) {
1021             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1022         }
1023     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1024         for ( guint i = 1; i < num - 1; i++ ) {
1025             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1026             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1027         }
1028     }
1032 /**
1033 Moves all draggables that depend on this one
1034  */
1035 void
1036 GrDragger::updateDependencies (bool write_repr)
1038     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1039         GrDraggable *draggable = (GrDraggable *) i->data;
1040         switch (draggable->point_type) {
1041             case POINT_LG_BEGIN:
1042                 {
1043                     // the end point is dependent only when dragging with ctrl+shift
1044                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1046                     this->updateMidstopDependencies (draggable, write_repr);
1047                 }
1048                 break;
1049             case POINT_LG_END:
1050                 {
1051                     // the begin point is dependent only when dragging with ctrl+shift
1052                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1054                     this->updateMidstopDependencies (draggable, write_repr);
1055                 }
1056                 break;
1057             case POINT_LG_MID:
1058                 // no other nodes depend on mid points.
1059                 break;
1060             case POINT_RG_R2:
1061                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1062                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1063                 this->updateMidstopDependencies (draggable, write_repr);
1064                 break;
1065             case POINT_RG_R1:
1066                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1067                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1068                 this->updateMidstopDependencies (draggable, write_repr);
1069                 break;
1070             case POINT_RG_CENTER:
1071                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1072                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1073                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1074                 this->updateMidstopDependencies (draggable, write_repr);
1075                 break;
1076             case POINT_RG_FOCUS:
1077                 // nothing can depend on that
1078                 break;
1079             case POINT_RG_MID1:
1080                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1081                 break;
1082             case POINT_RG_MID2:
1083                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1084                 break;
1085             default:
1086                 break;
1087         }
1088     }
1093 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1095     this->draggables = NULL;
1097     this->parent = parent;
1099     this->point = p;
1100     this->point_original = p;
1102     // create the knot
1103     this->knot = sp_knot_new (parent->desktop, NULL);
1104     this->knot->setMode(SP_KNOT_MODE_XOR);
1105     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1106     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1107     sp_knot_update_ctrl(this->knot);
1109     // move knot to the given point
1110     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1111     sp_knot_show (this->knot);
1113     // connect knot's signals
1114     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1115                        // luckily, midstops never snap to other nodes so are never unsnapped...
1116          && ( (draggable->point_type == POINT_LG_MID)
1117               || (draggable->point_type == POINT_RG_MID1)
1118               || (draggable->point_type == POINT_RG_MID2) ) )
1119     {
1120         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1121     } else {
1122         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1123     }
1124     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1125     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1126     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1127     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1129     // add the initial draggable
1130     if (draggable)
1131         this->addDraggable (draggable);
1132     updateKnotShape();
1135 GrDragger::~GrDragger ()
1137     // unselect if it was selected
1138     this->parent->setDeselected(this);
1140     // disconnect signals
1141     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1142     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1143     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1144     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1145     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1147     /* unref should call destroy */
1148     g_object_unref (G_OBJECT (this->knot));
1150     // delete all draggables
1151     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1152         delete ((GrDraggable *) i->data);
1153     }
1154     g_slist_free (this->draggables);
1155     this->draggables = NULL;
1158 /**
1159 Select the dragger which has the given draggable.
1160 */
1161 GrDragger *
1162 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1164     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1165         GrDragger *dragger = (GrDragger *) i->data;
1166         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1167             GrDraggable *da2 = (GrDraggable *) j->data;
1168             if ( (da2->item == item) && 
1169                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1170                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1171                  (da2->fill_or_stroke == fill_or_stroke)) {
1172                 return (dragger);
1173             }
1174         }
1175     }
1176     return NULL;
1180 void
1181 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1183     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1184     if (d && d !=  this) {
1185         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1186     }
1190 /**
1191   Draw this dragger as selected
1192 */
1193 void
1194 GrDragger::select()
1196     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1197     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1200 /**
1201   Draw this dragger as normal (deselected)
1202 */
1203 void
1204 GrDragger::deselect()
1206     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1207     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1210 bool
1211 GrDragger::isSelected()
1213     return g_list_find (parent->selected, this);
1216 /**
1217 \brief Deselect all stops/draggers (private)
1218 */
1219 void
1220 GrDrag::deselect_all()
1222     while (selected) {
1223         ( (GrDragger*) selected->data)->deselect();
1224         selected = g_list_remove(selected, selected->data);
1225     }
1228 /**
1229 \brief Deselect all stops/draggers (public; emits signal)
1230 */
1231 void
1232 GrDrag::deselectAll()
1234     deselect_all();
1235     this->desktop->emitToolSubselectionChanged(NULL);
1238 /**
1239 \brief Select all stops/draggers
1240 */
1241 void
1242 GrDrag::selectAll()
1244     for (GList *l = this->draggers; l != NULL; l = l->next) {
1245         GrDragger *d = ((GrDragger *) l->data);
1246         setSelected (d, true, true);
1247     }
1250 /**
1251 \brief Select all stops/draggers that match the coords
1252 */
1253 void 
1254 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1256     for (GList *l = this->draggers; l != NULL; l = l->next) {
1257         GrDragger *d = ((GrDragger *) l->data);
1258         for (guint k = 0; k < coords.size(); k++) {
1259             if (NR::L2 (d->point - coords[k]) < 1e-4) {
1260                 setSelected (d, true, true);
1261             }
1262         }
1263     }
1266 /**
1267 \brief Select a dragger
1268 \param dragger       The dragger to select
1269 \param add_to_selection   If true, add to selection, otherwise deselect others
1270 \param override      If true, always select this node, otherwise toggle selected status
1271 */
1272 void
1273 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1275     GrDragger *seldragger = NULL;
1277     if (add_to_selection) {
1278         if (!dragger) return;
1279         if (override) {
1280             if (!g_list_find(selected, dragger)) {
1281                 selected = g_list_prepend(selected, dragger);
1282             }
1283             dragger->select();
1284             seldragger = dragger;
1285         } else { // toggle
1286             if (g_list_find(selected, dragger)) {
1287                 selected = g_list_remove(selected, dragger);
1288                 dragger->deselect();
1289                 if (selected) {
1290                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1291                 }
1292             } else {
1293                 selected = g_list_prepend(selected, dragger);
1294                 dragger->select();
1295                 seldragger = dragger;
1296             }
1297         }
1298     } else {
1299         deselect_all();
1300         if (dragger) {
1301             selected = g_list_prepend(selected, dragger);
1302             dragger->select();
1303             seldragger = dragger;
1304         }
1305     }
1306     if (seldragger) {
1307         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1308     }
1311 /**
1312 \brief Deselect a dragger
1313 \param dragger       The dragger to deselect
1314 */
1315 void
1316 GrDrag::setDeselected (GrDragger *dragger)
1318     if (g_list_find(selected, dragger)) {
1319         selected = g_list_remove(selected, dragger);
1320         dragger->deselect();
1321     }
1322     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1327 /**
1328 Create a line from p1 to p2 and add it to the lines list
1329  */
1330 void
1331 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1333     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1334                                                             SP_TYPE_CTRLLINE, NULL);
1335     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1336     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1337         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1338     sp_canvas_item_show (line);
1339     this->lines = g_slist_append (this->lines, line);
1342 /**
1343 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1344 new dragger and add it to draggers list
1345  */
1346 void
1347 GrDrag::addDragger (GrDraggable *draggable)
1349     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1351     for (GList *i = this->draggers; i != NULL; i = i->next) {
1352         GrDragger *dragger = (GrDragger *) i->data;
1353         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1354             // distance is small, merge this draggable into dragger, no need to create new dragger
1355             dragger->addDraggable (draggable);
1356             dragger->updateKnotShape();
1357             return;
1358         }
1359     }
1361     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1362     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1363     this->draggers = g_list_append (this->draggers, new_dragger);
1366 /**
1367 Add draggers for the radial gradient rg on item
1368 */
1369 void
1370 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1372     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1373     guint num = rg->vector.stops.size();
1374     if (num > 2) {
1375         for ( guint i = 1; i < num - 1; i++ ) {
1376             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1377         }
1378     }
1379     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1380     if (num > 2) {
1381         for ( guint i = 1; i < num - 1; i++ ) {
1382             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1383         }
1384     }
1385     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1386     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1389 /**
1390 Add draggers for the linear gradient lg on item
1391 */
1392 void
1393 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1395     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1396     guint num = lg->vector.stops.size();
1397     if (num > 2) {
1398         for ( guint i = 1; i < num - 1; i++ ) {
1399             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1400         }
1401     }
1402     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1405 /**
1406 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1407 */
1408 void
1409 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1411     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1412     if (dragger) {
1413         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1414     }
1417 /**
1418 Regenerates the draggers list from the current selection; is called when selection is changed or
1419 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1420 */
1421 void
1422 GrDrag::updateDraggers ()
1424     while (selected) {
1425         selected = g_list_remove(selected, selected->data);
1426     }
1427     // delete old draggers
1428     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1429         delete ((GrDragger *) i->data);
1430     }
1431     g_list_free (this->draggers);
1432     this->draggers = NULL;
1434     g_return_if_fail (this->selection != NULL);
1436     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1438         SPItem *item = SP_ITEM(i->data);
1439         SPStyle *style = SP_OBJECT_STYLE (item);
1441         if (style && (style->fill.isPaintserver())) {
1442             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1443             if (SP_IS_LINEARGRADIENT (server)) {
1444                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1445             } else if (SP_IS_RADIALGRADIENT (server)) {
1446                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1447             }
1448         }
1450         if (style && (style->stroke.isPaintserver())) {
1451             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1452             if (SP_IS_LINEARGRADIENT (server)) {
1453                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1454             } else if (SP_IS_RADIALGRADIENT (server)) {
1455                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1456             }
1457         }
1458     }
1461 /**
1462 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1463 lines are always in sync with the actual gradient
1464 */
1465 void
1466 GrDrag::updateLines ()
1468     // delete old lines
1469     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1470         gtk_object_destroy( GTK_OBJECT (i->data));
1471     }
1472     g_slist_free (this->lines);
1473     this->lines = NULL;
1475     g_return_if_fail (this->selection != NULL);
1477     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1479         SPItem *item = SP_ITEM(i->data);
1481         SPStyle *style = SP_OBJECT_STYLE (item);
1483         if (style && (style->fill.isPaintserver())) {
1484             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1485             if (SP_IS_LINEARGRADIENT (server)) {
1486                 this->addLine (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);
1487             } else if (SP_IS_RADIALGRADIENT (server)) {
1488                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1489                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1490                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1491             }
1492         }
1494         if (style && (style->stroke.isPaintserver())) {
1495             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1496             if (SP_IS_LINEARGRADIENT (server)) {
1497                 this->addLine (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);
1498             } else if (SP_IS_RADIALGRADIENT (server)) {
1499                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1500                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1501                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1502             }
1503         }
1504     }
1507 /**
1508 Regenerates the levels list from the current selection
1509 */
1510 void
1511 GrDrag::updateLevels ()
1513     hor_levels.clear();
1514     vert_levels.clear();
1516     g_return_if_fail (this->selection != NULL);
1518     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1519         SPItem *item = SP_ITEM(i->data);
1520         NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1521         if (rect) {
1522             // Remember the edges of the bbox and the center axis
1523             hor_levels.push_back(rect->min()[NR::Y]);
1524             hor_levels.push_back(rect->max()[NR::Y]);
1525             hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1526             vert_levels.push_back(rect->min()[NR::X]);
1527             vert_levels.push_back(rect->max()[NR::X]);
1528             vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1529         }
1530     }
1533 void
1534 GrDrag::selected_reverse_vector ()
1536     if (selected == NULL)
1537         return;
1539     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1540         GrDraggable *draggable = (GrDraggable *) i->data;
1542         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1543     }
1546 void
1547 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1549     selected_move (x, y, false, scale_radial);
1552 void
1553 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1555     if (selected == NULL)
1556         return;
1558     bool did = false; 
1560     for (GList *i = selected; i != NULL; i = i->next) {
1561         GrDragger *d = (GrDragger *) i->data;
1563         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1564             // if this is a an endpoint,
1566             // Moving an rg center moves its focus and radii as well.
1567             // therefore, if this is a focus or radius and if selection
1568             // contains the center as well, do not move this one
1569             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) || 
1570                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1571                 bool skip_radius_with_center = false;
1572                 for (GList *di = selected; di != NULL; di = di->next) {
1573                     GrDragger *d_new = (GrDragger *) di->data;
1574                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1575                                     POINT_RG_CENTER,
1576                                     0,
1577                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1578                         // FIXME: here we take into account only the first draggable!
1579                         skip_radius_with_center = true;
1580                     }
1581                 }
1582                 if (skip_radius_with_center)
1583                     continue;
1584             }
1586             did = true;
1587             d->point += NR::Point (x, y);
1588             d->point_original = d->point;
1589             sp_knot_moveto (d->knot, &(d->point));
1591             d->fireDraggables (write_repr, scale_radial);
1593             d->updateDependencies(write_repr);
1594         }
1595     }
1597     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1598         for (GList *i = selected; i != NULL; i = i->next) {
1599             GrDragger *d = (GrDragger *) i->data;
1601             if (d->isA(POINT_LG_MID) || !d->isA(POINT_RG_MID1) || !d->isA(POINT_RG_MID2)) {
1602             }
1603         }
1604     }
1606     if (write_repr && did) {
1607         // we did an undoable action
1608         sp_document_maybe_done (sp_desktop_document (desktop), "grmove", SP_VERB_CONTEXT_GRADIENT,
1609                           _("Move gradient handle"));
1610     }
1613 void
1614 GrDrag::selected_move_screen (double x, double y)
1616     gdouble zoom = desktop->current_zoom();
1617     gdouble zx = x / zoom;
1618     gdouble zy = y / zoom;
1620     selected_move (zx, zy);
1623 /**
1624 Select the knot next to the last selected one and deselect all other selected.
1625 */
1626 void
1627 GrDrag::select_next ()
1629     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1630         if (draggers)
1631             setSelected ((GrDragger *) draggers->data);
1632     } else {
1633         setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
1634     }
1637 /**
1638 Select the knot previous from the last selected one and deselect all other selected.
1639 */
1640 void
1641 GrDrag::select_prev ()
1643     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1644         if (draggers)
1645             setSelected ((GrDragger *) g_list_last (draggers)->data);
1646     } else {
1647         setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
1648     }
1652 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1653 void
1654 GrDrag::deleteSelected (bool just_one)
1656     if (!selected) return;
1658     SPDocument *document = false;
1660     struct StructStopInfo {
1661         SPStop * spstop;
1662         GrDraggable * draggable;
1663         SPGradient * gradient;
1664         SPGradient * vector;
1665     };
1667     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1668     GSList *endstoplist = NULL;  // list of stops that must be deleted
1669     while (selected) {
1670         GrDragger *dragger = (GrDragger*) selected->data;
1671         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1672             GrDraggable *draggable = (GrDraggable*) drgble->data;
1673             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1674             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1676             switch (draggable->point_type) {
1677                 case POINT_LG_MID:
1678                 case POINT_RG_MID1:
1679                 case POINT_RG_MID2:
1680                     {
1681                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1682                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1683                         bool present = false;
1684                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1685                             if ( (SPStop*)l->data == stop ) {
1686                                 present = true;
1687                                 break; // no need to search further.
1688                             }
1689                         }
1690                         if (!present)
1691                             midstoplist = g_slist_append(midstoplist, stop);
1692                     }
1693                     break;
1694                 case POINT_LG_BEGIN:
1695                 case POINT_LG_END:
1696                 case POINT_RG_CENTER:
1697                 case POINT_RG_R1:
1698                 case POINT_RG_R2:
1699                     {
1700                         SPStop *stop = NULL;
1701                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1702                             stop = sp_first_stop(vector);
1703                         } else {
1704                             stop = sp_last_stop(vector);
1705                         }
1706                         if (stop) {
1707                             StructStopInfo *stopinfo = new StructStopInfo;
1708                             stopinfo->spstop = stop;
1709                             stopinfo->draggable = draggable;
1710                             stopinfo->gradient = gradient;
1711                             stopinfo->vector = vector;
1712                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1713                             bool present = false;
1714                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1715                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1716                                     present = true;
1717                                     break; // no need to search further.
1718                                 }
1719                             }
1720                             if (!present)
1721                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1722                         }
1723                     }
1724                     break;
1725                 default:
1726                     break;
1727             }
1728         }
1729         selected = g_list_remove(selected, dragger);
1730         if ( just_one ) break; // iterate once if just_one is set.
1731     }
1732     while (midstoplist) {
1733         SPStop *stop = (SPStop*) midstoplist->data;
1734         document = SP_OBJECT_DOCUMENT (stop);
1735         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1736         parent->removeChild(SP_OBJECT_REPR(stop));
1737         midstoplist = g_slist_remove(midstoplist, stop);
1738     }
1739     while (endstoplist) {
1740         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1741         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1743         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1744         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1745         // manually count the children, don't know if there already exists a function for this...
1746         int len = 0;
1747         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1748               child != NULL ;
1749               child = SP_OBJECT_NEXT(child) )
1750         {
1751             if ( SP_IS_STOP(child) )  len ++;
1752         }
1753         if (len > 2)
1754         {
1755             switch (stopinfo->draggable->point_type) {
1756                 case POINT_LG_BEGIN:
1757                     {
1758                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1760                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1761                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1762                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1763                         SPStop *stop = sp_first_stop(stopinfo->vector);
1764                         gdouble offset = stop->offset;
1765                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1766                         lg->x1.computed = newbegin[NR::X];
1767                         lg->y1.computed = newbegin[NR::Y];
1769                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1770                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1771                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1772                         stop->offset = 0;
1773                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1775                         // iterate through midstops to set new offset values such that they won't move on canvas.
1776                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1777                         stop = sp_next_stop(stop);
1778                         while ( stop != laststop ) {
1779                             stop->offset = (stop->offset - offset)/(1 - offset);
1780                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1781                             stop = sp_next_stop(stop);
1782                         }
1783                     }
1784                     break;
1785                 case POINT_LG_END:
1786                     {
1787                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1789                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1790                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1791                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1792                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1793                         gdouble offset = laststop->offset;
1794                         NR::Point newend = begin + offset * (oldend - begin);
1795                         lg->x2.computed = newend[NR::X];
1796                         lg->y2.computed = newend[NR::Y];
1798                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1799                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1800                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1801                         laststop->offset = 1;
1802                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1804                         // iterate through midstops to set new offset values such that they won't move on canvas.
1805                         SPStop *stop = sp_first_stop(stopinfo->vector);
1806                         stop = sp_next_stop(stop);
1807                         while ( stop != laststop ) {
1808                             stop->offset = stop->offset / offset;
1809                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1810                             stop = sp_next_stop(stop);
1811                         }
1812                     }
1813                     break;
1814                 case POINT_RG_CENTER:
1815                     {
1816                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1817                         if (newfirst) {
1818                             newfirst->offset = 0;
1819                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1820                         }
1821                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1822                     }
1823                     break;
1824                 case POINT_RG_R1:
1825                 case POINT_RG_R2:
1826                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1828                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1829                         double oldradius = rg->r.computed;
1830                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1831                         gdouble offset = laststop->offset;
1832                         double newradius = offset * oldradius;
1833                         rg->r.computed = newradius;
1835                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1836                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
1837                         laststop->offset = 1;
1838                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1840                         // iterate through midstops to set new offset values such that they won't move on canvas.
1841                         SPStop *stop = sp_first_stop(stopinfo->vector);
1842                         stop = sp_next_stop(stop);
1843                         while ( stop != laststop ) {
1844                             stop->offset = stop->offset / offset;
1845                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1846                             stop = sp_next_stop(stop);
1847                         }
1848                         break;
1849             }
1850         }
1851         else
1852         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
1853             SPCSSAttr *css = sp_repr_css_attr_new ();
1855             // stopinfo->spstop is the selected stop
1856             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
1857             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
1858                 unselectedrepr = unselectedrepr->next();
1859             }
1861             if (unselectedrepr == NULL) {
1862                 if (stopinfo->draggable->fill_or_stroke) {
1863                     sp_repr_css_unset_property (css, "fill");
1864                 } else {
1865                     sp_repr_css_unset_property (css, "stroke");
1866                 }
1867             } else {
1868                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
1869                 if (stopinfo->draggable->fill_or_stroke) {
1870                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1871                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1872                 } else {
1873                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1874                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1875                 }
1876                 sp_repr_css_attr_unref (stopcss);
1877             }
1878             
1879             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1880             sp_repr_css_attr_unref (css);
1881         }
1883         endstoplist = g_slist_remove(endstoplist, stopinfo);
1884         delete stopinfo;
1885     }
1887     if (document) {
1888         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1889     }
1893 /*
1894   Local Variables:
1895   mode:c++
1896   c-file-style:"stroustrup"
1897   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1898   indent-tabs-mode:nil
1899   fill-column:99
1900   End:
1901 */
1902 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :