Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / gradient-drag.cpp
1 /*
2  * On-canvas gradient dragging
3  *
4  * Authors:
5  *   bulia byak <buliabyak@users.sf.net>
6  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
7  *   Jon A. Cruz <jon@joncruz.org>
8  *   Abhishek Sharma
9  *
10  * Copyright (C) 2007 Johan Engelen
11  * Copyright (C) 2005,2010 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 "display/sp-canvas-util.h"
31 #include "xml/repr.h"
32 #include "svg/css-ostringstream.h"
33 #include "svg/svg.h"
34 #include "libnr/nr-point-fns.h"
35 #include "preferences.h"
36 #include "sp-item.h"
37 #include "style.h"
38 #include "knot.h"
39 #include "sp-linear-gradient.h"
40 #include "sp-radial-gradient.h"
41 #include "gradient-chemistry.h"
42 #include "gradient-drag.h"
43 #include "sp-stop.h"
44 #include "snap.h"
45 #include "sp-namedview.h"
46 #include "selection-chemistry.h"
48 using Inkscape::DocumentUndo;
50 #define GR_KNOT_COLOR_NORMAL 0xffffff00
51 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
52 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
54 #define GR_LINE_COLOR_FILL 0x0000ff7f
55 #define GR_LINE_COLOR_STROKE 0x9999007f
57 // screen pixels between knots when they snap:
58 #define SNAP_DIST 5
60 // absolute distance between gradient points for them to become a single dragger when the drag is created:
61 #define MERGE_DIST 0.1
63 // knot shapes corresponding to GrPointType enum
64 SPKnotShapeType gr_knot_shapes [] = {
65         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
66         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
67         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
68         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
69         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
70         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
71         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
72         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
73         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
74 };
76 const gchar *gr_knot_descr [] = {
77     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
78     N_("Linear gradient <b>end</b>"),
79     N_("Linear gradient <b>mid stop</b>"),
80     N_("Radial gradient <b>center</b>"),
81     N_("Radial gradient <b>radius</b>"),
82     N_("Radial gradient <b>radius</b>"),
83     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
84     N_("Radial gradient <b>mid stop</b>"),
85     N_("Radial gradient <b>mid stop</b>")
86 };
88 static void
89 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
90 {
91     GrDrag *drag = (GrDrag *) data;
92     drag->updateDraggers ();
93     drag->updateLines ();
94     drag->updateLevels ();
95 }
97 static void
98 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
99 {
100     GrDrag *drag = (GrDrag *) data;
101     if (drag->local_change) {
102         drag->local_change = false;
103     } else {
104         drag->updateDraggers ();
105     }
106     drag->updateLines ();
107     drag->updateLevels ();
110 /**
111 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
112 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
113 any.
114 */
115 int
116 gr_drag_style_query (SPStyle *style, int property, gpointer data)
118     GrDrag *drag = (GrDrag *) data;
120     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
121         return QUERY_STYLE_NOTHING;
122     }
124     if (!drag->selected) {
125         return QUERY_STYLE_NOTHING;
126     } else {
127         int ret = QUERY_STYLE_NOTHING;
129         float cf[4];
130         cf[0] = cf[1] = cf[2] = cf[3] = 0;
132         int count = 0;
134         for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
135             GrDragger *d = (GrDragger *) i->data;
136             for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
137                 GrDraggable *draggable = (GrDraggable *) j->data;
139                 if (ret == QUERY_STYLE_NOTHING) {
140                     ret = QUERY_STYLE_SINGLE;
141                 } else if (ret == QUERY_STYLE_SINGLE) {
142                     ret = QUERY_STYLE_MULTIPLE_AVERAGED;
143                 }
145                 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
146                 cf[0] += SP_RGBA32_R_F (c);
147                 cf[1] += SP_RGBA32_G_F (c);
148                 cf[2] += SP_RGBA32_B_F (c);
149                 cf[3] += SP_RGBA32_A_F (c);
151                 count ++;
152             }
153         }
155         if (count) {
156             cf[0] /= count;
157             cf[1] /= count;
158             cf[2] /= count;
159             cf[3] /= count;
161             // set both fill and stroke with our stop-color and stop-opacity
162             style->fill.clear();
163             style->fill.setColor( cf[0], cf[1], cf[2] );
164             style->fill.set = TRUE;
165             style->stroke.clear();
166             style->stroke.setColor( cf[0], cf[1], cf[2] );
167             style->stroke.set = TRUE;
169             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
170             style->fill_opacity.set = TRUE;
171             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
172             style->stroke_opacity.set = TRUE;
174             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
175             style->opacity.set = TRUE;
176         }
178         return ret;
179     }
182 Glib::ustring GrDrag::makeStopSafeColor( gchar const *str, bool &isNull )
184     Glib::ustring colorStr;
185     if ( str ) {
186         isNull = false;
187         colorStr = str;
188         Glib::ustring::size_type pos = colorStr.find("url(#");
189         if ( pos != Glib::ustring::npos ) {
190             Glib::ustring targetName = colorStr.substr(pos + 5, colorStr.length() - 6);
191             const GSList *gradients = desktop->doc()->getResourceList("gradient");
192             for (const GSList *item = gradients; item; item = item->next) {
193                 SPGradient* grad = SP_GRADIENT(item->data);
194                 if ( targetName == grad->getId() ) {
195                     SPGradient *vect = grad->getVector();
196                     SPStop *firstStop = (vect) ? vect->getFirstStop() : grad->getFirstStop();
197                     if (firstStop) {
198                         Glib::ustring stopColorStr;
199                         if (firstStop->currentColor) {
200                             stopColorStr = firstStop->getStyleProperty("color", NULL);
201                         } else {
202                             stopColorStr = firstStop->specified_color.toString();
203                         }
204                         if ( !stopColorStr.empty() ) {
205                             colorStr = stopColorStr;
206                         }
207                     }
208                     break;
209                 }
210             }
211         }
212     } else {
213         isNull = true;
214     }
216     return colorStr;
219 bool GrDrag::styleSet( const SPCSSAttr *css )
221     if (!selected) {
222         return false;
223     }
225     SPCSSAttr *stop = sp_repr_css_attr_new();
227     // See if the css contains interesting properties, and if so, translate them into the format
228     // acceptable for gradient stops
230     // any of color properties, in order of increasing priority:
231     if (css->attribute("flood-color")) {
232         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
233     }
235     if (css->attribute("lighting-color")) {
236         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
237     }
239     if (css->attribute("color")) {
240         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
241     }
243     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none")) {
244         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
245     }
247     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none")) {
248         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
249     }
251     if (css->attribute("stop-color")) {
252         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
253     }
255     // Make sure the style is allowed for gradient stops.
256     if ( !sp_repr_css_property_is_unset( stop, "stop-color") ) {
257         bool stopIsNull = false;
258         Glib::ustring tmp = makeStopSafeColor( sp_repr_css_property( stop, "stop-color", "" ), stopIsNull );
259         if ( !stopIsNull && !tmp.empty() ) {
260             sp_repr_css_set_property( stop, "stop-color", tmp.c_str() );
261         }
262     }
265     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
266         sp_repr_css_set_property(stop, "stop-opacity", css->attribute("stop-opacity"));
267     } else {  // multiply all opacity properties:
268         gdouble accumulated = 1.0;
269         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
270         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
271         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
272         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
274         Inkscape::CSSOStringStream os;
275         os << accumulated;
276         sp_repr_css_set_property(stop, "stop-opacity", os.str().c_str());
278         if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
279             (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none"))) {
280             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
281         }
282     }
284     if (!stop->attributeList()) { // nothing for us here, pass it on
285         sp_repr_css_attr_unref(stop);
286         return false;
287     }
289     for (GList const* sel = selected; sel != NULL; sel = sel->next) { // for all selected draggers
290         GrDragger* dragger = reinterpret_cast<GrDragger*>(sel->data);
291         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
292             GrDraggable *draggable = reinterpret_cast<GrDraggable *>(i->data);
294             local_change = true;
295             sp_item_gradient_stop_set_style(draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
296         }
297     }
299     //sp_repr_css_print(stop);
300     sp_repr_css_attr_unref(stop);
301     return true;
304 guint32 GrDrag::getColor()
306     if (!selected) return 0;
308     float cf[4];
309     cf[0] = cf[1] = cf[2] = cf[3] = 0;
311     int count = 0;
313     for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
314         GrDragger *d = (GrDragger *) i->data;
315         for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
316             GrDraggable *draggable = (GrDraggable *) j->data;
318             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
319             cf[0] += SP_RGBA32_R_F (c);
320             cf[1] += SP_RGBA32_G_F (c);
321             cf[2] += SP_RGBA32_B_F (c);
322             cf[3] += SP_RGBA32_A_F (c);
324             count ++;
325         }
326     }
328     if (count) {
329         cf[0] /= count;
330         cf[1] /= count;
331         cf[2] /= count;
332         cf[3] /= count;
333     }
335     return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
338 SPStop *
339 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
341     gfloat offset; // type of SPStop.offset = gfloat
342     SPGradient *gradient;
343     bool fill_or_stroke = true;
344     bool r1_knot = false;
346     bool addknot = false;
347     do {
348         gradient = sp_item_gradient (item, fill_or_stroke);
349         if (SP_IS_LINEARGRADIENT(gradient)) {
350             Geom::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
351             Geom::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
353             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
354             double dist_screen = Geom::L2 (mouse_p - nearest);
355             if ( dist_screen < tolerance ) {
356                 // add the knot
357                 offset = get_offset_between_points(nearest, begin, end);
358                 addknot = true;
359                 break; // break out of the while loop: add only one knot
360             }
361         } else if (SP_IS_RADIALGRADIENT(gradient)) {
362             Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
363             Geom::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
364             Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
365             double dist_screen = Geom::L2 (mouse_p - nearest);
366             if ( dist_screen < tolerance ) {
367                 offset = get_offset_between_points(nearest, begin, end);
368                 addknot = true;
369                 r1_knot = true;
370                 break; // break out of the while loop: add only one knot
371             }
373             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
374             nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
375             dist_screen = Geom::L2 (mouse_p - nearest);
376             if ( dist_screen < tolerance ) {
377                 offset = get_offset_between_points(nearest, begin, end);
378                 addknot = true;
379                 r1_knot = false;
380                 break; // break out of the while loop: add only one knot
381             }
382         }
383         fill_or_stroke = !fill_or_stroke;
384     } while (!fill_or_stroke && !addknot) ;
386     if (addknot) {
387         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
388         SPStop* prev_stop = vector->getFirstStop();
389         SPStop* next_stop = prev_stop->getNextStop();
390         guint i = 1;
391         while ( (next_stop) && (next_stop->offset < offset) ) {
392             prev_stop = next_stop;
393             next_stop = next_stop->getNextStop();
394             i++;
395         }
396         if (!next_stop) {
397             // logical error: the endstop should have offset 1 and should always be more than this offset here
398             return NULL;
399         }
402         SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
403         gradient->ensureVector();
404         updateDraggers();
406         return newstop;
407     }
409     return NULL;
413 bool
414 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
416     // Note: not sure if a null pointer can come in for the style, but handle that just in case
417     bool stopIsNull = false;
418     Glib::ustring toUse = makeStopSafeColor( c, stopIsNull );
420     // first, see if we can drop onto one of the existing draggers
421     for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
422         GrDragger *d = (GrDragger *) i->data;
424         if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
425            SPCSSAttr *stop = sp_repr_css_attr_new ();
426            sp_repr_css_set_property( stop, "stop-color", stopIsNull ? 0 : toUse.c_str() );
427            sp_repr_css_set_property( stop, "stop-opacity", "1" );
428            for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
429                GrDraggable *draggable = (GrDraggable *) j->data;
430                local_change = true;
431                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
432            }
433            sp_repr_css_attr_unref(stop);
434            return true;
435         }
436     }
438     // now see if we're over line and create a new stop
439     bool over_line = false;
440     SPCtrlLine *line = NULL;
441     if (lines) {
442         for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
443             line = (SPCtrlLine*) l->data;
444             Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
445             double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
446             if (line->item && dist_screen < 5) {
447                 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
448                 if (stop) {
449                     SPCSSAttr *css = sp_repr_css_attr_new ();
450                     sp_repr_css_set_property( css, "stop-color", stopIsNull ? 0 : toUse.c_str() );
451                     sp_repr_css_set_property( css, "stop-opacity", "1" );
452                     sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
453                     return true;
454                 }
455             }
456         }
457     }
459     return false;
463 GrDrag::GrDrag(SPDesktop *desktop) :
464     selected(0),
465     keep_selection(false),
466     local_change(false),
467     desktop(desktop),
468     hor_levels(),
469     vert_levels(),
470     draggers(0),
471     lines(0),
472     selection(sp_desktop_selection(desktop)),
473     sel_changed_connection(),
474     sel_modified_connection(),
475     style_set_connection(),
476     style_query_connection()
478     sel_changed_connection = selection->connectChanged(
479         sigc::bind(
480             sigc::ptr_fun(&gr_drag_sel_changed),
481             (gpointer)this )
483         );
484     sel_modified_connection = selection->connectModified(
485         sigc::bind(
486             sigc::ptr_fun(&gr_drag_sel_modified),
487             (gpointer)this )
488         );
490     style_set_connection = desktop->connectSetStyle( sigc::mem_fun(*this, &GrDrag::styleSet) );
492     style_query_connection = desktop->connectQueryStyle(
493         sigc::bind(
494             sigc::ptr_fun(&gr_drag_style_query),
495             (gpointer)this )
496         );
498     updateDraggers();
499     updateLines();
500     updateLevels();
502     if (desktop->gr_item) {
503         setSelected(getDraggerFor(desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
504     }
507 GrDrag::~GrDrag()
509     this->sel_changed_connection.disconnect();
510     this->sel_modified_connection.disconnect();
511     this->style_set_connection.disconnect();
512     this->style_query_connection.disconnect();
514     if (this->selected) {
515         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
516         desktop->gr_item = draggable->item;
517         desktop->gr_point_type = draggable->point_type;
518         desktop->gr_point_i = draggable->point_i;
519         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
520     } else {
521         desktop->gr_item = NULL;
522         desktop->gr_point_type = 0;
523         desktop->gr_point_i = 0;
524         desktop->gr_fill_or_stroke = true;
525     }
527     deselect_all();
528     for (GList *l = this->draggers; l != NULL; l = l->next) {
529         delete ((GrDragger *) l->data);
530     }
531     g_list_free (this->draggers);
532     this->draggers = NULL;
533     this->selected = NULL;
535     for (GSList *l = this->lines; l != NULL; l = l->next) {
536         gtk_object_destroy( GTK_OBJECT (l->data));
537     }
538     g_slist_free (this->lines);
539     this->lines = NULL;
542 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
544     this->item = item;
545     this->point_type = point_type;
546     this->point_i = point_i;
547     this->fill_or_stroke = fill_or_stroke;
549     g_object_ref (G_OBJECT (this->item));
552 GrDraggable::~GrDraggable ()
554     g_object_unref (G_OBJECT (this->item));
558 SPObject *GrDraggable::getServer()
560     if (!item) {
561         return NULL;
562     }
564     SPObject *server = NULL;
565     if (fill_or_stroke) {
566         server = item->style->getFillPaintServer();
567     }else {
568         server = item->style->getStrokePaintServer();
569     }
571     return server;
574 static void
575 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
577     GrDragger *dragger = (GrDragger *) data;
578     GrDrag *drag = dragger->parent;
580     Geom::Point p = ppointer;
582     SPDesktop *desktop = dragger->parent->desktop;
583     SnapManager &m = desktop->namedview->snap_manager;
584     double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom();
586     if (state & GDK_SHIFT_MASK) {
587         // with Shift; unsnap if we carry more than one draggable
588         if (dragger->draggables && dragger->draggables->next) {
589             // create a new dragger
590             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
591             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
592             // relink to it all but the first draggable in the list
593             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
594                 GrDraggable *draggable = (GrDraggable *) i->data;
595                 dr_new->addDraggable (draggable);
596             }
597             dr_new->updateKnotShape();
598             g_slist_free (dragger->draggables->next);
599             dragger->draggables->next = NULL;
600             dragger->updateKnotShape();
601             dragger->updateTip();
602         }
603     } else if (!(state & GDK_CONTROL_MASK)) {
604         // without Shift or Ctrl; see if we need to snap to another dragger
605         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
606             GrDragger *d_new = (GrDragger *) di->data;
607             if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
609                 // Merge draggers:
610                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
611                     GrDraggable *draggable = (GrDraggable *) i->data;
612                     // copy draggable to d_new:
613                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
614                     d_new->addDraggable (da_new);
615                 }
617                 // unlink and delete this dragger
618                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
619                 delete dragger;
621                 // update the new merged dragger
622                 d_new->fireDraggables(true, false, true);
623                 d_new->parent->updateLines();
624                 d_new->parent->setSelected (d_new);
625                 d_new->updateKnotShape ();
626                 d_new->updateTip ();
627                 d_new->updateDependencies(true);
628                 DocumentUndo::done(sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
629                                    _("Merge gradient handles"));
630                 return;
631             }
632         }
633     }
635     if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
636         m.setup(desktop);
637         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
638         m.unSetup();
639         if (s.getSnapped()) {
640             p = s.getPoint();
641             sp_knot_moveto (knot, p);
642         }
643     } else if (state & GDK_CONTROL_MASK) {
644         SnappedConstraints sc;
645         Inkscape::SnapCandidatePoint scp = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
646         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
647         unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
648         /* 0 means no snapping. */
650         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
651             GrDraggable *draggable = (GrDraggable *) i->data;
653             Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
655             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
656                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
657                     GrDragger *d_new = (GrDragger *) di->data;
658                     if (d_new == dragger)
659                         continue;
660                     if (d_new->isA (draggable->item,
661                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
662                                     draggable->fill_or_stroke)) {
663                         // found the other end of the linear gradient;
664                         if (state & GDK_SHIFT_MASK) {
665                             // moving linear around center
666                             Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
667                             dr_snap = center;
668                         } else {
669                             // moving linear around the other end
670                             dr_snap = d_new->point;
671                         }
672                     }
673                 }
674             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
675                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
676                     GrDragger *d_new = (GrDragger *) di->data;
677                     if (d_new == dragger)
678                         continue;
679                     if (d_new->isA (draggable->item,
680                                     POINT_RG_CENTER,
681                                     draggable->fill_or_stroke)) {
682                         // found the center of the radial gradient;
683                         dr_snap = d_new->point;
684                     }
685                 }
686             } else if (draggable->point_type == POINT_RG_CENTER) {
687                 // radial center snaps to hor/vert relative to its original position
688                 dr_snap = dragger->point_original;
689             }
691             // dr_snap contains the origin of the gradient, whereas p will be the new endpoint which we will try to snap now
692             Inkscape::SnappedPoint sp;
693             if (dr_snap.isFinite()) {
694                 m.setup(desktop);
695                 if (state & GDK_MOD1_MASK) {
696                     // with Alt, snap to the original angle and its perpendiculars
697                     sp = m.constrainedAngularSnap(scp, dragger->point_original, dr_snap, 2);
698                 } else {
699                     // with Ctrl, snap to M_PI/snaps
700                     sp = m.constrainedAngularSnap(scp, boost::optional<Geom::Point>(), dr_snap, snaps);
701                 }
702                 m.unSetup();
703                 sc.points.push_back(sp);
704             }
705         }
707         m.setup(desktop, false); // turn of the snap indicator temporarily
708         Inkscape::SnappedPoint bsp = m.findBestSnap(scp, sc, true);
709         m.unSetup();
710         if (!bsp.getSnapped()) {
711             // If we didn't truly snap to an object or to a grid, then we will still have to look for the
712             // closest projection onto one of the constraints. findBestSnap() will not do this for us
713             for (std::list<Inkscape::SnappedPoint>::const_iterator i = sc.points.begin(); i != sc.points.end(); i++) {
714                 if (i == sc.points.begin() || (Geom::L2((*i).getPoint() - p) < Geom::L2(bsp.getPoint() - p))) {
715                     bsp.setPoint((*i).getPoint());
716                     bsp.setTarget(Inkscape::SNAPTARGET_CONSTRAINED_ANGLE);
717                 }
718             }
719         }
720         //p = sc.points.front().getPoint();
721         p = bsp.getPoint();
722         sp_knot_moveto (knot, p);
723     }
725     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
726     bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
728     if (drag->keep_selection) {
729         Geom::Point diff = p - dragger->point;
730         drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
731     } else {
732         dragger->point = p;
733         dragger->fireDraggables (false, scale_radial);
734         dragger->updateDependencies(false);
735     }
740 static void
741 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
744     GrDrag *drag = dragger->parent;
745     // a midpoint dragger can (logically) only contain one GrDraggable
746     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
748     // get begin and end points between which dragging is allowed:
749     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
750     *moving = g_slist_append(*moving, dragger);
752     guint lowest_i = draggable->point_i;
753     guint highest_i = draggable->point_i;
754     GrDragger *lowest_dragger = dragger;
755     GrDragger *highest_dragger = dragger;
756     if (dragger->isSelected()) {
757         GrDragger* d_add;
758         while ( true )
759         {
760             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
761             if ( d_add && g_list_find(drag->selected, d_add) ) {
762                 lowest_i = lowest_i - 1;
763                 *moving = g_slist_prepend(*moving, d_add);
764                 lowest_dragger = d_add;
765             } else {
766                 break;
767             }
768         }
770         while ( true )
771         {
772             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
773             if ( d_add && g_list_find(drag->selected, d_add) ) {
774                 highest_i = highest_i + 1;
775                 *moving = g_slist_append(*moving, d_add);
776                 highest_dragger = d_add;
777             } else {
778                 break;
779             }
780         }
781     }
783     if ( SP_IS_LINEARGRADIENT(server) ) {
784         guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
785         GrDragger *d_temp;
786         if (lowest_i == 1) {
787             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
788         } else {
789             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
790         }
791         if (d_temp)
792             *begin = d_temp->point;
794         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
795         if (d_temp == NULL) {
796             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
797         }
798         if (d_temp)
799             *end = d_temp->point;
800     } else if ( SP_IS_RADIALGRADIENT(server) ) {
801         guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
802         GrDragger *d_temp;
803         if (lowest_i == 1) {
804             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
805         } else {
806             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
807         }
808         if (d_temp)
809             *begin = d_temp->point;
811         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
812         if (d_temp == NULL) {
813             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
814         }
815         if (d_temp)
816             *end = d_temp->point;
817     }
819     *low_lim  = dragger->point - (lowest_dragger->point - *begin);
820     *high_lim = dragger->point - (highest_dragger->point - *end);
825 /**
826 Called when a midpoint knot is dragged.
827 */
828 static void
829 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
831     GrDragger *dragger = (GrDragger *) data;
832     GrDrag *drag = dragger->parent;
833     // a midpoint dragger can (logically) only contain one GrDraggable
834     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
836     // FIXME: take from prefs
837     double snap_fraction = 0.1;
839     Geom::Point p = ppointer;
840     Geom::Point begin(0,0), end(0,0);
841     Geom::Point low_lim(0,0), high_lim(0,0);
843     SPObject *server = draggable->getServer();
845     GSList *moving = NULL;
846     gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
848     if (state & GDK_CONTROL_MASK) {
849         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
850     } else {
851         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
852         if (!(state & GDK_SHIFT_MASK)) {
853             Inkscape::Snapper::SnapConstraint cl(low_lim, high_lim - low_lim);
854             SPDesktop *desktop = dragger->parent->desktop;
855             SnapManager &m = desktop->namedview->snap_manager;
856             m.setup(desktop);
857             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
858             m.unSetup();
859         }
860     }
861     Geom::Point displacement = p - dragger->point;
863     for (GSList const* i = moving; i != NULL; i = i->next) {
864         GrDragger *drg = (GrDragger*) i->data;
865         SPKnot *drgknot = drg->knot;
866         Geom::Point this_move = displacement;
867         if (state & GDK_MOD1_MASK) {
868             // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
869             double alpha = 1.0;
870             if (Geom::L2(drg->point - dragger->point) + Geom::L2(drg->point - begin) - 1e-3 > Geom::L2(dragger->point - begin)) { // drg is on the end side from dragger
871                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
872                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
873             } else { // drg is on the begin side from dragger
874                 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
875                 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
876             }
877         }
878         drg->point += this_move;
879         sp_knot_moveto (drgknot, drg->point);
880         drg->fireDraggables (false);
881         drg->updateDependencies(false);
882     }
884     g_slist_free(moving);
886     drag->keep_selection = dragger->isSelected();
891 static void
892 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
894     GrDragger *dragger = (GrDragger *) data;
896     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
899 /**
900 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
901 */
902 static void
903 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
905     GrDragger *dragger = (GrDragger *) data;
907     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
909     dragger->point_original = dragger->point = knot->pos;
911     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
912         dragger->fireDraggables (true, true);
913     } else {
914         dragger->fireDraggables (true);
915     }
917     for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
918         GrDragger *d = (GrDragger *) i->data;
919         if (d == dragger)
920             continue;
921         d->fireDraggables (true);
922     }
924     // make this dragger selected
925     if (!dragger->parent->keep_selection) {
926         dragger->parent->setSelected (dragger);
927     }
928     dragger->parent->keep_selection = false;
930     dragger->updateDependencies(true);
932     // we did an undoable action
933     DocumentUndo::done(sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
934                        _("Move gradient handle"));
937 /**
938 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
939 state of the keyboard keys
940 */
941 static void
942 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
944     GrDragger *dragger = (GrDragger *) data;
945     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
946     if (!draggable) return;
948     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
949     // delete this knot from vector
950         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
951         gradient = gradient->getVector();
952         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
953             SPStop *stop = NULL;
954             switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
955             case POINT_LG_BEGIN:
956             case POINT_RG_CENTER:
957                 stop = gradient->getFirstStop();
958                 {
959                     SPStop *next = stop->getNextStop();
960                     if (next) {
961                         next->offset = 0;
962                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
963                     }
964                 }
965                 break;
966             case POINT_LG_END:
967             case POINT_RG_R1:
968             case POINT_RG_R2:
969                 stop = sp_last_stop(gradient);
970                 {
971                     SPStop *prev = stop->getPrevStop();
972                     if (prev) {
973                         prev->offset = 1;
974                         sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
975                     }
976                 }
977                 break;
978             case POINT_LG_MID:
979             case POINT_RG_MID1:
980             case POINT_RG_MID2:
981                 stop = sp_get_stop_i(gradient, draggable->point_i);
982                 break;
983             }
985             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
986             DocumentUndo::done(SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
987                                _("Delete gradient stop"));
988         }
989     } else {
990     // select the dragger
991         dragger->point_original = dragger->point;
993         if ( state & GDK_SHIFT_MASK ) {
994             dragger->parent->setSelected (dragger, true, false);
995         } else {
996             dragger->parent->setSelected (dragger);
997         }
998     }
1001 /**
1002 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
1003 */
1004 static void
1005 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
1007     GrDragger *dragger = (GrDragger *) data;
1009     dragger->point_original = dragger->point;
1011     if (dragger->draggables == NULL)
1012         return;
1014     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1015     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1018 /**
1019 Act upon all draggables of the dragger, setting them to the dragger's point
1020 */
1021 void
1022 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1024     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1025         GrDraggable *draggable = (GrDraggable *) i->data;
1027         // set local_change flag so that selection_changed callback does not regenerate draggers
1028         this->parent->local_change = true;
1030         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1031         // to the center, unless it's the first update upon merge when we must snap it to the point
1032         if (merging_focus ||
1033             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1034         {
1035             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1036         }
1037     }
1040 /**
1041 Checks if the dragger has a draggable with this point_type
1042  */
1043 bool
1044 GrDragger::isA (gint point_type)
1046     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1047         GrDraggable *draggable = (GrDraggable *) i->data;
1048         if (draggable->point_type == point_type) {
1049             return true;
1050         }
1051     }
1052     return false;
1055 /**
1056 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1057  */
1058 bool
1059 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1061     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1062         GrDraggable *draggable = (GrDraggable *) i->data;
1063         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1064             return true;
1065         }
1066     }
1067     return false;
1070 /**
1071 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1072  */
1073 bool
1074 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1076     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1077         GrDraggable *draggable = (GrDraggable *) i->data;
1078         if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1079             return true;
1080         }
1081     }
1082     return false;
1085 bool
1086 GrDraggable::mayMerge (GrDraggable *da2)
1088     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1089         // we must not merge the points of the same gradient!
1090         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1091               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1092             // except that we can snap center and focus together
1093             return false;
1094         }
1095     }
1096     // disable merging of midpoints.
1097     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1098          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1099          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1100         return false;
1102     return true;
1105 bool
1106 GrDragger::mayMerge (GrDragger *other)
1108     if (this == other)
1109         return false;
1111     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1112         GrDraggable *da1 = (GrDraggable *) i->data;
1113         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1114             GrDraggable *da2 = (GrDraggable *) j->data;
1115             if (!da1->mayMerge(da2))
1116                 return false;
1117         }
1118     }
1119     return true;
1122 bool
1123 GrDragger::mayMerge (GrDraggable *da2)
1125     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1126         GrDraggable *da1 = (GrDraggable *) i->data;
1127         if (!da1->mayMerge(da2))
1128             return false;
1129     }
1130     return true;
1133 /**
1134 Updates the statusbar tip of the dragger knot, based on its draggables
1135  */
1136 void
1137 GrDragger::updateTip ()
1139     if (this->knot && this->knot->tip) {
1140         g_free (this->knot->tip);
1141         this->knot->tip = NULL;
1142     }
1144     if (g_slist_length (this->draggables) == 1) {
1145         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1146         char *item_desc = draggable->item->description();
1147         switch (draggable->point_type) {
1148             case POINT_LG_MID:
1149             case POINT_RG_MID1:
1150             case POINT_RG_MID2:
1151                 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"),
1152                                                    _(gr_knot_descr[draggable->point_type]),
1153                                                    draggable->point_i,
1154                                                    item_desc,
1155                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1156                 break;
1158             default:
1159                 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"),
1160                                                    _(gr_knot_descr[draggable->point_type]),
1161                                                    item_desc,
1162                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1163                 break;
1164         }
1165         g_free(item_desc);
1166     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1167         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1168     } else {
1169         int length = g_slist_length (this->draggables);
1170         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1171                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1172                                                     length),
1173                                            length);
1174     }
1177 /**
1178 Adds a draggable to the dragger
1179  */
1180 void
1181 GrDragger::updateKnotShape ()
1183     if (!draggables)
1184         return;
1185     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1186     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1189 /**
1190 Adds a draggable to the dragger
1191  */
1192 void
1193 GrDragger::addDraggable (GrDraggable *draggable)
1195     this->draggables = g_slist_prepend (this->draggables, draggable);
1197     this->updateTip();
1201 /**
1202 Moves this dragger to the point of the given draggable, acting upon all other draggables
1203  */
1204 void
1205 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1207     GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1208     if (!dr_first) return;
1210     this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1211     this->point_original = this->point;
1213     sp_knot_moveto (this->knot, this->point);
1215     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1216         GrDraggable *da = (GrDraggable *) i->data;
1217         if ( (da->item == item) &&
1218              (point_type == -1 || da->point_type == point_type) &&
1219              (point_i == -1 || da->point_i == point_i) &&
1220              (da->fill_or_stroke == fill_or_stroke) ) {
1221             continue;
1222         }
1223         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1224     }
1225     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1229 /**
1230 Moves all midstop draggables that depend on this one
1231  */
1232 void
1233 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1234     SPObject *server = draggable->getServer();
1235     if (!server)
1236         return;
1237     guint num = SP_GRADIENT(server)->vector.stops.size();
1238     if (num <= 2) return;
1240     if ( SP_IS_LINEARGRADIENT(server) ) {
1241         for ( guint i = 1; i < num - 1; i++ ) {
1242             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1243         }
1244     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
1245         for ( guint i = 1; i < num - 1; i++ ) {
1246             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1247             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1248         }
1249     }
1253 /**
1254 Moves all draggables that depend on this one
1255  */
1256 void
1257 GrDragger::updateDependencies (bool write_repr)
1259     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1260         GrDraggable *draggable = (GrDraggable *) i->data;
1261         switch (draggable->point_type) {
1262             case POINT_LG_BEGIN:
1263                 {
1264                     // the end point is dependent only when dragging with ctrl+shift
1265                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1267                     this->updateMidstopDependencies (draggable, write_repr);
1268                 }
1269                 break;
1270             case POINT_LG_END:
1271                 {
1272                     // the begin point is dependent only when dragging with ctrl+shift
1273                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1275                     this->updateMidstopDependencies (draggable, write_repr);
1276                 }
1277                 break;
1278             case POINT_LG_MID:
1279                 // no other nodes depend on mid points.
1280                 break;
1281             case POINT_RG_R2:
1282                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1283                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1284                 this->updateMidstopDependencies (draggable, write_repr);
1285                 break;
1286             case POINT_RG_R1:
1287                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1288                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1289                 this->updateMidstopDependencies (draggable, write_repr);
1290                 break;
1291             case POINT_RG_CENTER:
1292                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1293                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1294                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1295                 this->updateMidstopDependencies (draggable, write_repr);
1296                 break;
1297             case POINT_RG_FOCUS:
1298                 // nothing can depend on that
1299                 break;
1300             case POINT_RG_MID1:
1301                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1302                 break;
1303             case POINT_RG_MID2:
1304                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1305                 break;
1306             default:
1307                 break;
1308         }
1309     }
1314 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1315   : point(p),
1316     point_original(p)
1318     this->draggables = NULL;
1320     this->parent = parent;
1322     // create the knot
1323     this->knot = sp_knot_new (parent->desktop, NULL);
1324     this->knot->setMode(SP_KNOT_MODE_XOR);
1325     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1326     this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1327     sp_knot_update_ctrl(this->knot);
1329     // move knot to the given point
1330     sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1331     sp_knot_show (this->knot);
1333     // connect knot's signals
1334     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1335                        // luckily, midstops never snap to other nodes so are never unsnapped...
1336          && ( (draggable->point_type == POINT_LG_MID)
1337               || (draggable->point_type == POINT_RG_MID1)
1338               || (draggable->point_type == POINT_RG_MID2) ) )
1339     {
1340         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1341     } else {
1342         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1343     }
1344     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1345     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1346     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1347     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1349     // add the initial draggable
1350     if (draggable)
1351         this->addDraggable (draggable);
1352     updateKnotShape();
1355 GrDragger::~GrDragger ()
1357     // unselect if it was selected
1358     this->parent->setDeselected(this);
1360     // disconnect signals
1361     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1362     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1363     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1364     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1365     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1367     /* unref should call destroy */
1368     g_object_unref (G_OBJECT (this->knot));
1370     // delete all draggables
1371     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1372         delete ((GrDraggable *) i->data);
1373     }
1374     g_slist_free (this->draggables);
1375     this->draggables = NULL;
1378 /**
1379 Select the dragger which has the given draggable.
1380 */
1381 GrDragger *
1382 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1384     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1385         GrDragger *dragger = (GrDragger *) i->data;
1386         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1387             GrDraggable *da2 = (GrDraggable *) j->data;
1388             if ( (da2->item == item) &&
1389                  (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1390                  (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1391                  (da2->fill_or_stroke == fill_or_stroke)) {
1392                 return (dragger);
1393             }
1394         }
1395     }
1396     return NULL;
1400 void
1401 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1403     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1404     if (d && d !=  this) {
1405         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1406     }
1410 /**
1411   Draw this dragger as selected
1412 */
1413 void
1414 GrDragger::select()
1416     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1417     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1420 /**
1421   Draw this dragger as normal (deselected)
1422 */
1423 void
1424 GrDragger::deselect()
1426     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1427     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1430 bool
1431 GrDragger::isSelected()
1433     return g_list_find (parent->selected, this);
1436 /**
1437 \brief Deselect all stops/draggers (private)
1438 */
1439 void
1440 GrDrag::deselect_all()
1442     while (selected) {
1443         ( (GrDragger*) selected->data)->deselect();
1444         selected = g_list_remove(selected, selected->data);
1445     }
1448 /**
1449 \brief Deselect all stops/draggers (public; emits signal)
1450 */
1451 void
1452 GrDrag::deselectAll()
1454     deselect_all();
1455     this->desktop->emitToolSubselectionChanged(NULL);
1458 /**
1459 \brief Select all stops/draggers
1460 */
1461 void
1462 GrDrag::selectAll()
1464     for (GList *l = this->draggers; l != NULL; l = l->next) {
1465         GrDragger *d = ((GrDragger *) l->data);
1466         setSelected (d, true, true);
1467     }
1470 /**
1471 \brief Select all stops/draggers that match the coords
1472 */
1473 void
1474 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1476     for (GList *l = this->draggers; l != NULL; l = l->next) {
1477         GrDragger *d = ((GrDragger *) l->data);
1478         for (guint k = 0; k < coords.size(); k++) {
1479             if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1480                 setSelected (d, true, true);
1481             }
1482         }
1483     }
1487 /**
1488 \brief Select all stops/draggers that fall within the rect
1489 */
1490 void
1491 GrDrag::selectRect(Geom::Rect const &r)
1493     for (GList *l = this->draggers; l != NULL; l = l->next) {
1494         GrDragger *d = ((GrDragger *) l->data);
1495         if (r.contains(d->point)) {
1496            setSelected (d, true, true);
1497         }
1498     }
1501 /**
1502 \brief Select a dragger
1503 \param dragger       The dragger to select
1504 \param add_to_selection   If true, add to selection, otherwise deselect others
1505 \param override      If true, always select this node, otherwise toggle selected status
1506 */
1507 void
1508 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1510     GrDragger *seldragger = NULL;
1512     if (add_to_selection) {
1513         if (!dragger) return;
1514         if (override) {
1515             if (!g_list_find(selected, dragger)) {
1516                 selected = g_list_prepend(selected, dragger);
1517             }
1518             dragger->select();
1519             seldragger = dragger;
1520         } else { // toggle
1521             if (g_list_find(selected, dragger)) {
1522                 selected = g_list_remove(selected, dragger);
1523                 dragger->deselect();
1524                 if (selected) {
1525                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1526                 }
1527             } else {
1528                 selected = g_list_prepend(selected, dragger);
1529                 dragger->select();
1530                 seldragger = dragger;
1531             }
1532         }
1533     } else {
1534         deselect_all();
1535         if (dragger) {
1536             selected = g_list_prepend(selected, dragger);
1537             dragger->select();
1538             seldragger = dragger;
1539         }
1540     }
1541     if (seldragger) {
1542         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1543     }
1546 /**
1547 \brief Deselect a dragger
1548 \param dragger       The dragger to deselect
1549 */
1550 void
1551 GrDrag::setDeselected (GrDragger *dragger)
1553     if (g_list_find(selected, dragger)) {
1554         selected = g_list_remove(selected, dragger);
1555         dragger->deselect();
1556     }
1557     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1562 /**
1563 Create a line from p1 to p2 and add it to the lines list
1564  */
1565 void
1566 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1568     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1569                                                             SP_TYPE_CTRLLINE, NULL);
1570     sp_canvas_item_move_to_z(line, 0);
1571     SP_CTRLLINE(line)->item = item;
1572     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1573     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1574         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1575     sp_canvas_item_show (line);
1576     this->lines = g_slist_append (this->lines, line);
1579 /**
1580 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1581 new dragger and add it to draggers list
1582  */
1583 void
1584 GrDrag::addDragger (GrDraggable *draggable)
1586     Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1588     for (GList *i = this->draggers; i != NULL; i = i->next) {
1589         GrDragger *dragger = (GrDragger *) i->data;
1590         if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1591             // distance is small, merge this draggable into dragger, no need to create new dragger
1592             dragger->addDraggable (draggable);
1593             dragger->updateKnotShape();
1594             return;
1595         }
1596     }
1598     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1599     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1600     this->draggers = g_list_append (this->draggers, new_dragger);
1603 /**
1604 Add draggers for the radial gradient rg on item
1605 */
1606 void
1607 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1609     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1610     guint num = rg->vector.stops.size();
1611     if (num > 2) {
1612         for ( guint i = 1; i < num - 1; i++ ) {
1613             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1614         }
1615     }
1616     addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1617     if (num > 2) {
1618         for ( guint i = 1; i < num - 1; i++ ) {
1619             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1620         }
1621     }
1622     addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1623     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1626 /**
1627 Add draggers for the linear gradient lg on item
1628 */
1629 void
1630 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1632     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1633     guint num = lg->vector.stops.size();
1634     if (num > 2) {
1635         for ( guint i = 1; i < num - 1; i++ ) {
1636             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1637         }
1638     }
1639     addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1642 /**
1643 Artificially grab the knot of this dragger; used by the gradient context
1644 */
1645 void
1646 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1648     if (dragger) {
1649         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1650     }
1653 /**
1654 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1655 */
1656 void
1657 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1659     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1660     if (dragger) {
1661         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1662     }
1665 /**
1666 Regenerates the draggers list from the current selection; is called when selection is changed or
1667 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1668 */
1669 void GrDrag::updateDraggers ()
1671     while (selected) {
1672         selected = g_list_remove(selected, selected->data);
1673     }
1674     // delete old draggers
1675     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1676         delete static_cast<GrDragger *>(i->data);
1677     }
1678     g_list_free(this->draggers);
1679     this->draggers = NULL;
1681     g_return_if_fail(this->selection != NULL);
1683     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1684         SPItem *item = SP_ITEM(i->data);
1685         SPStyle *style = item->style;
1687         if (style && (style->fill.isPaintserver())) {
1688             SPPaintServer *server = style->getFillPaintServer();
1689             if ( server && server->isSolid() ) {
1690                 // Suppress "gradientness" of solid paint
1691             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1692                 addDraggersLinear( SP_LINEARGRADIENT(server), item, true );
1693             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1694                 addDraggersRadial( SP_RADIALGRADIENT(server), item, true );
1695             }
1696         }
1698         if (style && (style->stroke.isPaintserver())) {
1699             SPPaintServer *server = style->getStrokePaintServer();
1700             if ( server && server->isSolid() ) {
1701                 // Suppress "gradientness" of solid paint
1702             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1703                 addDraggersLinear( SP_LINEARGRADIENT(server), item, false );
1704             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1705                 addDraggersRadial( SP_RADIALGRADIENT(server), item, false );
1706             }
1707         }
1708     }
1712 /**
1713  * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1714  */
1716 bool
1717 GrDrag::mouseOver()
1719     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1720         GrDragger *d = (GrDragger *) i->data;
1721         if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1722             return true;
1723         }
1724     }
1725     return false;
1727 /**
1728 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1729 lines are always in sync with the actual gradient
1730 */
1731 void
1732 GrDrag::updateLines ()
1734     // delete old lines
1735     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1736         gtk_object_destroy( GTK_OBJECT (i->data));
1737     }
1738     g_slist_free (this->lines);
1739     this->lines = NULL;
1741     g_return_if_fail (this->selection != NULL);
1743     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1745         SPItem *item = SP_ITEM(i->data);
1747         SPStyle *style = SP_OBJECT_STYLE (item);
1749         if (style && (style->fill.isPaintserver())) {
1750             SPPaintServer *server = item->style->getFillPaintServer();
1751             if ( server && server->isSolid() ) {
1752                 // Suppress "gradientness" of solid paint
1753             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1754                 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);
1755             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1756                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1757                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1758                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1759             }
1760         }
1762         if (style && (style->stroke.isPaintserver())) {
1763             SPPaintServer *server = item->style->getStrokePaintServer();
1764             if ( server && server->isSolid() ) {
1765                 // Suppress "gradientness" of solid paint
1766             } else if ( SP_IS_LINEARGRADIENT(server) ) {
1767                 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);
1768             } else if ( SP_IS_RADIALGRADIENT(server) ) {
1769                 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1770                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1771                 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1772             }
1773         }
1774     }
1777 /**
1778 Regenerates the levels list from the current selection
1779 */
1780 void
1781 GrDrag::updateLevels ()
1783     hor_levels.clear();
1784     vert_levels.clear();
1786     g_return_if_fail (this->selection != NULL);
1788     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1789         SPItem *item = SP_ITEM(i->data);
1790         Geom::OptRect rect = item->getBboxDesktop ();
1791         if (rect) {
1792             // Remember the edges of the bbox and the center axis
1793             hor_levels.push_back(rect->min()[Geom::Y]);
1794             hor_levels.push_back(rect->max()[Geom::Y]);
1795             hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1796             vert_levels.push_back(rect->min()[Geom::X]);
1797             vert_levels.push_back(rect->max()[Geom::X]);
1798             vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1799         }
1800     }
1803 void
1804 GrDrag::selected_reverse_vector ()
1806     if (selected == NULL)
1807         return;
1809     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1810         GrDraggable *draggable = (GrDraggable *) i->data;
1812         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1813     }
1816 void
1817 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1819     selected_move (x, y, false, scale_radial);
1822 void
1823 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1825     if (selected == NULL)
1826         return;
1828     bool did = false;
1830     for (GList *i = selected; i != NULL; i = i->next) {
1831         GrDragger *d = (GrDragger *) i->data;
1833         if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1834             // if this is an endpoint,
1836             // Moving an rg center moves its focus and radii as well.
1837             // therefore, if this is a focus or radius and if selection
1838             // contains the center as well, do not move this one
1839             if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1840                 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1841                 bool skip_radius_with_center = false;
1842                 for (GList *di = selected; di != NULL; di = di->next) {
1843                     GrDragger *d_new = (GrDragger *) di->data;
1844                     if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1845                                     POINT_RG_CENTER,
1846                                     0,
1847                                     ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1848                         // FIXME: here we take into account only the first draggable!
1849                         skip_radius_with_center = true;
1850                     }
1851                 }
1852                 if (skip_radius_with_center)
1853                     continue;
1854             }
1856             did = true;
1857             d->point += Geom::Point (x, y);
1858             d->point_original = d->point;
1859             sp_knot_moveto (d->knot, d->point);
1861             d->fireDraggables (write_repr, scale_radial);
1863             d->updateDependencies(write_repr);
1864         }
1865     }
1867     if (write_repr && did) {
1868         // we did an undoable action
1869         DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1870                                 _("Move gradient handle(s)"));
1871         return;
1872     }
1874     if (!did) { // none of the end draggers are selected, so let's try to move the mids
1876         GrDragger *dragger = (GrDragger *) selected->data;
1877         // a midpoint dragger can (logically) only contain one GrDraggable
1878         GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1880         Geom::Point begin(0,0), end(0,0);
1881         Geom::Point low_lim(0,0), high_lim(0,0);
1883         SPObject *server = draggable->getServer();
1884         GSList *moving = NULL;
1885         gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1887         Geom::Point p(x, y);
1888         p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1889         Geom::Point displacement = p - dragger->point;
1891         for (GSList const* i = moving; i != NULL; i = i->next) {
1892             GrDragger *drg = (GrDragger*) i->data;
1893             SPKnot *drgknot = drg->knot;
1894             drg->point += displacement;
1895             sp_knot_moveto (drgknot, drg->point);
1896             drg->fireDraggables (true);
1897             drg->updateDependencies(true);
1898             did = true;
1899         }
1901         g_slist_free(moving);
1903         if (write_repr && did) {
1904             // we did an undoable action
1905             DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1906                                     _("Move gradient mid stop(s)"));
1907         }
1908     }
1911 void
1912 GrDrag::selected_move_screen (double x, double y)
1914     gdouble zoom = desktop->current_zoom();
1915     gdouble zx = x / zoom;
1916     gdouble zy = y / zoom;
1918     selected_move (zx, zy);
1921 /**
1922 Select the knot next to the last selected one and deselect all other selected.
1923 */
1924 GrDragger *
1925 GrDrag::select_next ()
1927     GrDragger *d = NULL;
1928     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1929         if (draggers)
1930             d = (GrDragger *) draggers->data;
1931     } else {
1932         d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1933     }
1934     if (d)
1935         setSelected (d);
1936     return d;
1939 /**
1940 Select the knot previous from the last selected one and deselect all other selected.
1941 */
1942 GrDragger *
1943 GrDrag::select_prev ()
1945     GrDragger *d = NULL;
1946     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1947         if (draggers)
1948             d = (GrDragger *) g_list_last (draggers)->data;
1949     } else {
1950         d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1951     }
1952     if (d)
1953         setSelected (d);
1954     return d;
1958 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1959 void
1960 GrDrag::deleteSelected (bool just_one)
1962     if (!selected) return;
1964     SPDocument *document = false;
1966     struct StructStopInfo {
1967         SPStop * spstop;
1968         GrDraggable * draggable;
1969         SPGradient * gradient;
1970         SPGradient * vector;
1971     };
1973     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1974     GSList *endstoplist = NULL;  // list of stops that must be deleted
1975     while (selected) {
1976         GrDragger *dragger = (GrDragger*) selected->data;
1977         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1978             GrDraggable *draggable = (GrDraggable*) drgble->data;
1979             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1980             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1982             switch (draggable->point_type) {
1983                 case POINT_LG_MID:
1984                 case POINT_RG_MID1:
1985                 case POINT_RG_MID2:
1986                     {
1987                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1988                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1989                         bool present = false;
1990                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1991                             if ( (SPStop*)l->data == stop ) {
1992                                 present = true;
1993                                 break; // no need to search further.
1994                             }
1995                         }
1996                         if (!present)
1997                             midstoplist = g_slist_append(midstoplist, stop);
1998                     }
1999                     break;
2000                 case POINT_LG_BEGIN:
2001                 case POINT_LG_END:
2002                 case POINT_RG_CENTER:
2003                 case POINT_RG_R1:
2004                 case POINT_RG_R2:
2005                     {
2006                         SPStop *stop = NULL;
2007                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
2008                             stop = vector->getFirstStop();
2009                         } else {
2010                             stop = sp_last_stop(vector);
2011                         }
2012                         if (stop) {
2013                             StructStopInfo *stopinfo = new StructStopInfo;
2014                             stopinfo->spstop = stop;
2015                             stopinfo->draggable = draggable;
2016                             stopinfo->gradient = gradient;
2017                             stopinfo->vector = vector;
2018                             // check if already present in list. (e.g. when both R1 and R2 were selected)
2019                             bool present = false;
2020                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
2021                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
2022                                     present = true;
2023                                     break; // no need to search further.
2024                                 }
2025                             }
2026                             if (!present)
2027                                 endstoplist = g_slist_append(endstoplist, stopinfo);
2028                         }
2029                     }
2030                     break;
2031                 default:
2032                     break;
2033             }
2034         }
2035         selected = g_list_remove(selected, dragger);
2036         if ( just_one ) break; // iterate once if just_one is set.
2037     }
2038     while (midstoplist) {
2039         SPStop *stop = (SPStop*) midstoplist->data;
2040         document = SP_OBJECT_DOCUMENT (stop);
2041         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
2042         parent->removeChild(SP_OBJECT_REPR(stop));
2043         midstoplist = g_slist_remove(midstoplist, stop);
2044     }
2045     while (endstoplist) {
2046         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
2047         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2049         // 2 is the minimum, cannot delete more than that without deleting the whole vector
2050         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2051         // manually count the children, don't know if there already exists a function for this...
2052         int len = 0;
2053         for ( SPObject *child = (stopinfo->vector)->firstChild() ; child ; child = child->getNext() )
2054         {
2055             if ( SP_IS_STOP(child) ) {
2056                 len ++;
2057             }
2058         }
2059         if (len > 2)
2060         {
2061             switch (stopinfo->draggable->point_type) {
2062                 case POINT_LG_BEGIN:
2063                     {
2064                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2066                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2067                         Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2068                         Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2069                         SPStop *stop = stopinfo->vector->getFirstStop();
2070                         gdouble offset = stop->offset;
2071                         Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2072                         lg->x1.computed = newbegin[Geom::X];
2073                         lg->y1.computed = newbegin[Geom::Y];
2075                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2076                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2077                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2078                         stop->offset = 0;
2079                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2081                         // iterate through midstops to set new offset values such that they won't move on canvas.
2082                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2083                         stop = stop->getNextStop();
2084                         while ( stop != laststop ) {
2085                             stop->offset = (stop->offset - offset)/(1 - offset);
2086                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2087                             stop = stop->getNextStop();
2088                         }
2089                     }
2090                     break;
2091                 case POINT_LG_END:
2092                     {
2093                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2095                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2096                         Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2097                         Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2098                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2099                         gdouble offset = laststop->offset;
2100                         Geom::Point newend = begin + offset * (oldend - begin);
2101                         lg->x2.computed = newend[Geom::X];
2102                         lg->y2.computed = newend[Geom::Y];
2104                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2105                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2106                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2107                         laststop->offset = 1;
2108                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2110                         // iterate through midstops to set new offset values such that they won't move on canvas.
2111                         SPStop *stop = stopinfo->vector->getFirstStop();
2112                         stop = stop->getNextStop();
2113                         while ( stop != laststop ) {
2114                             stop->offset = stop->offset / offset;
2115                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2116                             stop = stop->getNextStop();
2117                         }
2118                     }
2119                     break;
2120                 case POINT_RG_CENTER:
2121                     {
2122                         SPStop *newfirst = stopinfo->spstop->getNextStop();
2123                         if (newfirst) {
2124                             newfirst->offset = 0;
2125                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2126                         }
2127                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2128                     }
2129                     break;
2130                 case POINT_RG_R1:
2131                 case POINT_RG_R2:
2132                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2134                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2135                         double oldradius = rg->r.computed;
2136                         SPStop *laststop = sp_last_stop(stopinfo->vector);
2137                         gdouble offset = laststop->offset;
2138                         double newradius = offset * oldradius;
2139                         rg->r.computed = newradius;
2141                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2142                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
2143                         laststop->offset = 1;
2144                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2146                         // iterate through midstops to set new offset values such that they won't move on canvas.
2147                         SPStop *stop = stopinfo->vector->getFirstStop();
2148                         stop = stop->getNextStop();
2149                         while ( stop != laststop ) {
2150                             stop->offset = stop->offset / offset;
2151                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2152                             stop = stop->getNextStop();
2153                         }
2154                         break;
2155             }
2156         }
2157         else
2158         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
2159             SPCSSAttr *css = sp_repr_css_attr_new ();
2161             // stopinfo->spstop is the selected stop
2162             Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2163             if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2164                 unselectedrepr = unselectedrepr->next();
2165             }
2167             if (unselectedrepr == NULL) {
2168                 if (stopinfo->draggable->fill_or_stroke) {
2169                     sp_repr_css_unset_property (css, "fill");
2170                 } else {
2171                     sp_repr_css_unset_property (css, "stroke");
2172                 }
2173             } else {
2174                 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2175                 if (stopinfo->draggable->fill_or_stroke) {
2176                     sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2177                     sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2178                 } else {
2179                     sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2180                     sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2181                 }
2182                 sp_repr_css_attr_unref (stopcss);
2183             }
2185             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2186             sp_repr_css_attr_unref (css);
2187         }
2189         endstoplist = g_slist_remove(endstoplist, stopinfo);
2190         delete stopinfo;
2191     }
2193     if (document) {
2194         DocumentUndo::done( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2195     }
2199 /*
2200   Local Variables:
2201   mode:c++
2202   c-file-style:"stroustrup"
2203   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2204   indent-tabs-mode:nil
2205   fill-column:99
2206   End:
2207 */
2208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :