Code

Implementing feature request #1673807: snapping of gradient handles
[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>midstop</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_("Linear gradient <b>midstop</b>"),
82     N_("Linear gradient <b>midstop</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             sp_color_set_rgb_float((SPColor *) &style->fill.value.color, cf[0], cf[1], cf[2]);
157             style->fill.set = TRUE;
158             style->fill.type = SP_PAINT_TYPE_COLOR;
159             sp_color_set_rgb_float((SPColor *) &style->stroke.value.color, cf[0], cf[1], cf[2]);
160             style->stroke.set = TRUE;
161             style->stroke.type = SP_PAINT_TYPE_COLOR;
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::SNAP_POINT | Inkscape::Snapper::BBOX_POINT, 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     dragger->point = p;
542     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
543         dragger->fireDraggables (false, true);
544     } else {
545         dragger->fireDraggables (false);
546     }
548     dragger->updateDependencies(false);
550     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
554 /**
555 Called when a midpoint knot is dragged.
556 */
557 static void
558 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
560     GrDragger *dragger = (GrDragger *) data;
561     GrDrag *drag = dragger->parent;
562     // a midpoint dragger can (logically) only contain one GrDraggable
563     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
565     // FIXME: take from prefs
566     double snap_fraction = 0.1;
568     NR::Point p = *ppointer;
569     NR::Point begin(0,0), end(0,0);
571     SPObject *server;
572     if (draggable->fill_or_stroke)
573         server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
574     else
575         server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
578     // get begin and end points between which dragging is allowed:
579     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
580     GSList *moving = NULL;
581     moving = g_slist_append(moving, dragger);
583     guint lowest_i = draggable->point_i;
584     guint highest_i = draggable->point_i;
585     GrDragger *lowest_dragger = dragger;
586     GrDragger *highest_dragger = dragger;
587     bool is_selected = g_list_find(drag->selected, dragger);
588     if (is_selected) {
589         GrDragger* d_add;
590         while ( true )
591         {
592             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
593             if ( d_add && g_list_find(drag->selected, d_add) ) {
594                 lowest_i = lowest_i - 1;
595                 moving = g_slist_prepend(moving, d_add);
596                 lowest_dragger = d_add;
597             } else {
598                 break;
599             }
600         }
602         while ( true )
603         {
604             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
605             if ( d_add && g_list_find(drag->selected, d_add) ) {
606                 highest_i = highest_i + 1;
607                 moving = g_slist_append(moving, d_add);
608                 highest_dragger = d_add;
609             } else {
610                 break;
611             }
612         }
613     }
615     if ( SP_IS_LINEARGRADIENT(server) ) {
616         GrDragger *d_temp;
617         if (lowest_i == 1) {
618             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
619         } else {
620             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
621         }
622         if (d_temp) begin = d_temp->point;
624         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
625         if (d_temp == NULL) {
626             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke);
627         }
628         if (d_temp) end = d_temp->point;
629     } else if ( SP_IS_RADIALGRADIENT(server) ) {
630         GrDragger *d_temp;
631         if (lowest_i == 1) {
632             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
633         } else {
634             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
635         }
636         if (d_temp) begin = d_temp->point;
638         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
639         if (d_temp == NULL) {
640             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, 0, draggable->fill_or_stroke);
641         }
642         if (d_temp) end = d_temp->point;
643     }
645     NR::Point low_lim  = dragger->point - (lowest_dragger->point - begin);
646     NR::Point high_lim = dragger->point - (highest_dragger->point - end);
648     if (state & GDK_CONTROL_MASK) {
649         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
650     } else {
651         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
652     }
653     NR::Point displacement = p - dragger->point;
655     for (GSList const* i = moving; i != NULL; i = i->next) {
656         GrDragger *drg = (GrDragger*) i->data;
657         SPKnot *drgknot = drg->knot;
658         drg->point += displacement;
659         sp_knot_moveto (drgknot, & drg->point);
660         drg->fireDraggables (false);
661         drg->updateDependencies(false);
662     }
664     drag->keep_selection = is_selected;
669 static void
670 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
672     GrDragger *dragger = (GrDragger *) data;
674     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
677 /**
678 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
679 */
680 static void
681 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
683     GrDragger *dragger = (GrDragger *) data;
685     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
687     dragger->point_original = dragger->point = knot->pos;
689     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
690         dragger->fireDraggables (true, true);
691     } else {
692         dragger->fireDraggables (true);
693     }
695     // make this dragger selected
696     if (!dragger->parent->keep_selection) {
697         dragger->parent->setSelected (dragger);
698     }
699     dragger->parent->keep_selection = false;
701     dragger->updateDependencies(true);
703     // we did an undoable action
704     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
705                       _("Move gradient handle"));
708 /**
709 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
710 state of the keyboard keys
711 */
712 static void
713 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
715     GrDragger *dragger = (GrDragger *) data;
716     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
717     if (!draggable) return;
719     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
720     // delete this knot from vector
721         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
722         gradient = sp_gradient_get_vector (gradient, false);
723         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
724                 SPStop *stop = NULL;
725                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
726                 case POINT_LG_BEGIN:
727                 case POINT_RG_CENTER:
728                     stop = sp_first_stop(gradient);
729                         {
730                             SPStop *next = sp_next_stop (stop);
731                                 if (next) {
732                                         next->offset = 0;
733                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
734                                 }
735                         }
736                     break;
737                 case POINT_LG_END:
738                 case POINT_RG_R1:
739                 case POINT_RG_R2:
740                     stop = sp_last_stop(gradient);
741                     {
742                             SPStop *prev = sp_prev_stop (stop, gradient);
743                             if (prev) {
744                                     prev->offset = 1;
745                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
746                             }
747                         }
748                     break;
749                 case POINT_LG_MID:
750                 case POINT_RG_MID1:
751                 case POINT_RG_MID2:
752                     stop = sp_get_stop_i(gradient, draggable->point_i);
753                     break;
754                 }
756                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
757                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
758                                   _("Delete gradient stop"));
759         }
760     } else {
761     // select the dragger
762         dragger->point_original = dragger->point;
764         if ( state & GDK_SHIFT_MASK ) {
765             dragger->parent->setSelected (dragger, true, false);
766         } else {
767             dragger->parent->setSelected (dragger);
768         }
769     }
772 /**
773 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
774 */
775 static void
776 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
778     GrDragger *dragger = (GrDragger *) data;
780     dragger->point_original = dragger->point;
782     if (dragger->draggables == NULL)
783         return;
785     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
786     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
789 /**
790 Act upon all draggables of the dragger, setting them to the dragger's point
791 */
792 void
793 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
795     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
796         GrDraggable *draggable = (GrDraggable *) i->data;
798         // set local_change flag so that selection_changed callback does not regenerate draggers
799         this->parent->local_change = true;
801         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
802         // to the center, unless it's the first update upon merge when we must snap it to the point
803         if (merging_focus ||
804             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
805             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
806     }
809 /**
810 Checks if the dragger has a draggable with this point_type
811  */
812 bool
813 GrDragger::isA (guint point_type)
815     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
816         GrDraggable *draggable = (GrDraggable *) i->data;
817         if (draggable->point_type == point_type) {
818             return true;
819         }
820     }
821     return false;
824 /**
825 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
826  */
827 bool
828 GrDragger::isA (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
830     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
831         GrDraggable *draggable = (GrDraggable *) i->data;
832         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
833             return true;
834         }
835     }
836     return false;
839 bool
840 GrDraggable::mayMerge (GrDraggable *da2)
842     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
843         // we must not merge the points of the same gradient!
844         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
845               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
846             // except that we can snap center and focus together
847             return false;
848         }
849     }
850     // disable merging of midpoints.
851     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
852          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
853          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
854         return false;
856     return true;
859 bool
860 GrDragger::mayMerge (GrDragger *other)
862     if (this == other)
863         return false;
865     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
866         GrDraggable *da1 = (GrDraggable *) i->data;
867         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
868             GrDraggable *da2 = (GrDraggable *) j->data;
869             if (!da1->mayMerge(da2))
870                 return false;
871         }
872     }
873     return true;
876 bool
877 GrDragger::mayMerge (GrDraggable *da2)
879     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
880         GrDraggable *da1 = (GrDraggable *) i->data;
881         if (!da1->mayMerge(da2))
882             return false;
883     }
884     return true;
887 /**
888 Updates the statusbar tip of the dragger knot, based on its draggables
889  */
890 void
891 GrDragger::updateTip ()
893         if (this->knot && this->knot->tip) {
894                 g_free (this->knot->tip);
895                 this->knot->tip = NULL;
896         }
898     if (g_slist_length (this->draggables) == 1) {
899         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
900         char *item_desc = sp_item_description(draggable->item);
901         switch (draggable->point_type) {
902             case POINT_LG_MID:
903             case POINT_RG_MID1:
904             case POINT_RG_MID2:
905                 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"),
906                                                    _(gr_knot_descr[draggable->point_type]),
907                                                    draggable->point_i,
908                                                    item_desc,
909                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
910                 break;
912             default:
913                 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"),
914                                                    _(gr_knot_descr[draggable->point_type]),
915                                                    item_desc,
916                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
917                 break;
918         }
919         g_free(item_desc);
920     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
921         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
922     } else {
923         int length = g_slist_length (this->draggables);
924         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
925                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
926                                                     length),
927                                            length);
928     }
931 /**
932 Adds a draggable to the dragger
933  */
934 void
935 GrDragger::updateKnotShape ()
937     if (!draggables)
938         return;
939     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
940     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
943 /**
944 Adds a draggable to the dragger
945  */
946 void
947 GrDragger::addDraggable (GrDraggable *draggable)
949     this->draggables = g_slist_prepend (this->draggables, draggable);
951     this->updateTip();
955 /**
956 Moves this dragger to the point of the given draggable, acting upon all other draggables
957  */
958 void
959 GrDragger::moveThisToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
961     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
962     this->point_original = this->point;
964     sp_knot_moveto (this->knot, &(this->point));
966     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
967         GrDraggable *da = (GrDraggable *) i->data;
968         if ( (da->item == item) && (da->point_type == point_type) && (da->point_i == point_i) && (da->fill_or_stroke == fill_or_stroke) ) {
969             continue;
970         }
971         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
972     }
973     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
977 /**
978 Moves all midstop draggables that depend on this one
979  */
980 void
981 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
982     SPObject *server;
983     if (draggable->fill_or_stroke)
984         server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
985     else
986         server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
987     guint num = SP_GRADIENT(server)->vector.stops.size();
988     if (num <= 2) return;
990     if ( SP_IS_LINEARGRADIENT(server) ) {
991         for ( guint i = 1; i < num - 1; i++ ) {
992             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
993         }
994     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
995         for ( guint i = 1; i < num - 1; i++ ) {
996             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
997             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
998         }
999     }
1003 /**
1004 Moves all draggables that depend on this one
1005  */
1006 void
1007 GrDragger::updateDependencies (bool write_repr)
1009     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1010         GrDraggable *draggable = (GrDraggable *) i->data;
1011         switch (draggable->point_type) {
1012             case POINT_LG_BEGIN:
1013                 {
1014                     // the end point is dependent only when dragging with ctrl+shift
1015                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke, write_repr);
1017                     this->updateMidstopDependencies (draggable, write_repr);
1018                 }
1019                 break;
1020             case POINT_LG_END:
1021                 {
1022                     // the begin point is dependent only when dragging with ctrl+shift
1023                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1025                     this->updateMidstopDependencies (draggable, write_repr);
1026                 }
1027                 break;
1028             case POINT_LG_MID:
1029                 // no other nodes depend on mid points.
1030                 break;
1031             case POINT_RG_R2:
1032                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1033                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1034                 this->updateMidstopDependencies (draggable, write_repr);
1035                 break;
1036             case POINT_RG_R1:
1037                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
1038                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1039                 this->updateMidstopDependencies (draggable, write_repr);
1040                 break;
1041             case POINT_RG_CENTER:
1042                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1043                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
1044                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1045                 this->updateMidstopDependencies (draggable, write_repr);
1046                 break;
1047             case POINT_RG_FOCUS:
1048                 // nothing can depend on that
1049                 break;
1050             case POINT_RG_MID1:
1051                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1052                 break;
1053             case POINT_RG_MID2:
1054                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1055                 break;
1056             default:
1057                 break;
1058         }
1059     }
1064 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1066     this->draggables = NULL;
1068     this->parent = parent;
1070     this->point = p;
1071     this->point_original = p;
1073     // create the knot
1074     this->knot = sp_knot_new (parent->desktop, NULL);
1075     this->knot->setMode(SP_KNOT_MODE_XOR);
1076     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1077     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1078     sp_knot_update_ctrl(this->knot);
1080     // move knot to the given point
1081     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1082     sp_knot_show (this->knot);
1084     // connect knot's signals
1085     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1086                        // luckily, midstops never snap to other nodes so are never unsnapped...
1087          && ( (draggable->point_type == POINT_LG_MID)
1088               || (draggable->point_type == POINT_RG_MID1)
1089               || (draggable->point_type == POINT_RG_MID2) ) )
1090     {
1091         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1092     } else {
1093         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1094     }
1095     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1096     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1097     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1098     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1100     // add the initial draggable
1101     if (draggable)
1102         this->addDraggable (draggable);
1103     updateKnotShape();
1106 GrDragger::~GrDragger ()
1108     // unselect if it was selected
1109     this->parent->setDeselected(this);
1111     // disconnect signals
1112     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1113     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1114     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1115     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1116     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1118     /* unref should call destroy */
1119     g_object_unref (G_OBJECT (this->knot));
1121     // delete all draggables
1122     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1123         delete ((GrDraggable *) i->data);
1124     }
1125     g_slist_free (this->draggables);
1126     this->draggables = NULL;
1129 /**
1130 Select the dragger which has the given draggable.
1131 */
1132 GrDragger *
1133 GrDrag::getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
1135     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1136         GrDragger *dragger = (GrDragger *) i->data;
1137         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1138             GrDraggable *da2 = (GrDraggable *) j->data;
1139             if ( (da2->item == item) && (da2->point_type == point_type) && (da2->point_i == point_i) && (da2->fill_or_stroke == fill_or_stroke)) {
1140                 return (dragger);
1141             }
1142         }
1143     }
1144     return NULL;
1148 void
1149 GrDragger::moveOtherToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
1151     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1152     if (d && d !=  this) {
1153         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1154     }
1158 /**
1159   Draw this dragger as selected
1160 */
1161 void
1162 GrDragger::select()
1164     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1165     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1168 /**
1169   Draw this dragger as normal (deselected)
1170 */
1171 void
1172 GrDragger::deselect()
1174     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1175     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1180 /**
1181 \brief Deselect all stops/draggers (private)
1182 */
1183 void
1184 GrDrag::deselect_all()
1186     while (selected) {
1187         ( (GrDragger*) selected->data)->deselect();
1188         selected = g_list_remove(selected, selected->data);
1189     }
1192 /**
1193 \brief Deselect all stops/draggers (public; emits signal)
1194 */
1195 void
1196 GrDrag::deselectAll()
1198     deselect_all();
1199     this->desktop->emitToolSubselectionChanged(NULL);
1203 /**
1204 \brief Select a dragger
1205 \param dragger       The dragger to select
1206 \param add_to_selection   If true, add to selection, otherwise deselect others
1207 \param override      If true, always select this node, otherwise toggle selected status
1208 */
1209 void
1210 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1212     GrDragger *seldragger = NULL;
1214     if (add_to_selection) {
1215         if (!dragger) return;
1216         if (override) {
1217             if (!g_list_find(selected, dragger)) {
1218                 selected = g_list_prepend(selected, dragger);
1219             }
1220             dragger->select();
1221             seldragger = dragger;
1222         } else { // toggle
1223             if (g_list_find(selected, dragger)) {
1224                 selected = g_list_remove(selected, dragger);
1225                 dragger->deselect();
1226                 if (selected) {
1227                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1228                 }
1229             } else {
1230                 selected = g_list_prepend(selected, dragger);
1231                 dragger->select();
1232                 seldragger = dragger;
1233             }
1234         }
1235     } else {
1236         deselect_all();
1237         if (dragger) {
1238             selected = g_list_prepend(selected, dragger);
1239             dragger->select();
1240             seldragger = dragger;
1241         }
1242     }
1243     if (seldragger) {
1244         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1245     }
1248 /**
1249 \brief Deselect a dragger
1250 \param dragger       The dragger to deselect
1251 */
1252 void
1253 GrDrag::setDeselected (GrDragger *dragger)
1255     if (g_list_find(selected, dragger)) {
1256         selected = g_list_remove(selected, dragger);
1257         dragger->deselect();
1258     }
1259     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1264 /**
1265 Create a line from p1 to p2 and add it to the lines list
1266  */
1267 void
1268 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1270     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1271                                                             SP_TYPE_CTRLLINE, NULL);
1272     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1273     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1274         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1275     sp_canvas_item_show (line);
1276     this->lines = g_slist_append (this->lines, line);
1279 /**
1280 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1281 new dragger and add it to draggers list
1282  */
1283 void
1284 GrDrag::addDragger (GrDraggable *draggable)
1286     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1288     for (GList *i = this->draggers; i != NULL; i = i->next) {
1289         GrDragger *dragger = (GrDragger *) i->data;
1290         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1291             // distance is small, merge this draggable into dragger, no need to create new dragger
1292             dragger->addDraggable (draggable);
1293             dragger->updateKnotShape();
1294             return;
1295         }
1296     }
1298     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1299     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1300     this->draggers = g_list_append (this->draggers, new_dragger);
1303 /**
1304 Add draggers for the radial gradient rg on item
1305 */
1306 void
1307 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1309     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1310     guint num = rg->vector.stops.size();
1311     if (num > 2) {
1312         for ( guint i = 1; i < num - 1; i++ ) {
1313             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1314         }
1315     }
1316     addDragger (new GrDraggable (item, POINT_RG_R1, 0, fill_or_stroke));
1317     if (num > 2) {
1318         for ( guint i = 1; i < num - 1; i++ ) {
1319             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1320         }
1321     }
1322     addDragger (new GrDraggable (item, POINT_RG_R2, 0, fill_or_stroke));
1323     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1326 /**
1327 Add draggers for the linear gradient lg on item
1328 */
1329 void
1330 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1332     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1333     guint num = lg->vector.stops.size();
1334     if (num > 2) {
1335         for ( guint i = 1; i < num - 1; i++ ) {
1336             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1337         }
1338     }
1339     addDragger (new GrDraggable (item, POINT_LG_END, 0, fill_or_stroke));
1342 /**
1343 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1344 */
1345 void
1346 GrDrag::grabKnot (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1348     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1349     if (dragger) {
1350         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1351     }
1354 /**
1355 Regenerates the draggers list from the current selection; is called when selection is changed or
1356 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1357 */
1358 void
1359 GrDrag::updateDraggers ()
1361     while (selected) {
1362         selected = g_list_remove(selected, selected->data);
1363     }
1364     // delete old draggers
1365     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1366         delete ((GrDragger *) i->data);
1367     }
1368     g_list_free (this->draggers);
1369     this->draggers = NULL;
1371     g_return_if_fail (this->selection != NULL);
1373     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1375         SPItem *item = SP_ITEM(i->data);
1376         SPStyle *style = SP_OBJECT_STYLE (item);
1378         if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1379             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1380             if (SP_IS_LINEARGRADIENT (server)) {
1381                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1382             } else if (SP_IS_RADIALGRADIENT (server)) {
1383                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1384             }
1385         }
1387         if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1388             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1389             if (SP_IS_LINEARGRADIENT (server)) {
1390                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1391             } else if (SP_IS_RADIALGRADIENT (server)) {
1392                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1393             }
1394         }
1395     }
1398 /**
1399 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1400 lines are always in sync with the actual gradient
1401 */
1402 void
1403 GrDrag::updateLines ()
1405     // delete old lines
1406     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1407         gtk_object_destroy( GTK_OBJECT (i->data));
1408     }
1409     g_slist_free (this->lines);
1410     this->lines = NULL;
1412     g_return_if_fail (this->selection != NULL);
1414     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1416         SPItem *item = SP_ITEM(i->data);
1418         SPStyle *style = SP_OBJECT_STYLE (item);
1420         if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1421             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1422             if (SP_IS_LINEARGRADIENT (server)) {
1423                 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);
1424             } else if (SP_IS_RADIALGRADIENT (server)) {
1425                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1426                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1427                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1428             }
1429         }
1431         if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1432             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1433             if (SP_IS_LINEARGRADIENT (server)) {
1434                 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);
1435             } else if (SP_IS_RADIALGRADIENT (server)) {
1436                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1437                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1438                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1439             }
1440         }
1441     }
1444 /**
1445 Regenerates the levels list from the current selection
1446 */
1447 void
1448 GrDrag::updateLevels ()
1450     hor_levels.clear();
1451     vert_levels.clear();
1453     g_return_if_fail (this->selection != NULL);
1455     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1456         SPItem *item = SP_ITEM(i->data);
1457         NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1458         if (rect) {
1459             // Remember the edges of the bbox and the center axis
1460             hor_levels.push_back(rect->min()[NR::Y]);
1461             hor_levels.push_back(rect->max()[NR::Y]);
1462             hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1463             vert_levels.push_back(rect->min()[NR::X]);
1464             vert_levels.push_back(rect->max()[NR::X]);
1465             vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1466         }
1467     }
1470 void
1471 GrDrag::selected_reverse_vector ()
1473     if (selected == NULL)
1474         return;
1476     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1477         GrDraggable *draggable = (GrDraggable *) i->data;
1479         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1480     }
1483 void
1484 GrDrag::selected_move (double x, double y)
1486 /*
1487     if (selected == NULL)
1488         return;
1490     if ( (g_list_length(selected) == 1) )
1491     selected->point += NR::Point (x, y);
1492     selected->point_original = selected->point;
1493     sp_knot_moveto (selected->knot, &(selected->point));
1495     selected->fireDraggables (true);
1497     selected->updateDependencies(true);
1499     // we did an undoable action
1500     sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
1501                       _("Move gradient handle"));
1502 */
1505 void
1506 GrDrag::selected_move_screen (double x, double y)
1508     gdouble zoom = desktop->current_zoom();
1509     gdouble zx = x / zoom;
1510     gdouble zy = y / zoom;
1512     selected_move (zx, zy);
1515 /**
1516 Select the knot next to the last selected one and deselect all other selected.
1517 */
1518 void
1519 GrDrag::select_next ()
1521     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1522         if (draggers)
1523             setSelected ((GrDragger *) draggers->data);
1524     } else {
1525         setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
1526     }
1529 /**
1530 Select the knot previous from the last selected one and deselect all other selected.
1531 */
1532 void
1533 GrDrag::select_prev ()
1535     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1536         if (draggers)
1537             setSelected ((GrDragger *) g_list_last (draggers)->data);
1538     } else {
1539         setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
1540     }
1544 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1545 void
1546 GrDrag::deleteSelected (bool just_one)
1548     if (!selected) return;
1550     SPDocument *document = false;
1552     struct StructStopInfo {
1553         SPStop * spstop;
1554         GrDraggable * draggable;
1555         SPGradient * gradient;
1556         SPGradient * vector;
1557     };
1559     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1560     GSList *endstoplist = NULL;  // list of stops that must be deleted
1561     while (selected) {
1562         GrDragger *dragger = (GrDragger*) selected->data;
1563         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1564             GrDraggable *draggable = (GrDraggable*) drgble->data;
1565             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1566             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1568             switch (draggable->point_type) {
1569                 case POINT_LG_MID:
1570                 case POINT_RG_MID1:
1571                 case POINT_RG_MID2:
1572                     {
1573                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1574                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1575                         bool present = false;
1576                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1577                             if ( (SPStop*)l->data == stop ) {
1578                                 present = true;
1579                                 break; // no need to search further.
1580                             }
1581                         }
1582                         if (!present)
1583                             midstoplist = g_slist_append(midstoplist, stop);
1584                     }
1585                     break;
1586                 case POINT_LG_BEGIN:
1587                 case POINT_LG_END:
1588                 case POINT_RG_CENTER:
1589                 case POINT_RG_R1:
1590                 case POINT_RG_R2:
1591                     {
1592                         SPStop *stop = NULL;
1593                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1594                             stop = sp_first_stop(vector);
1595                         } else {
1596                             stop = sp_last_stop(vector);
1597                         }
1598                         if (stop) {
1599                             StructStopInfo *stopinfo = new StructStopInfo;
1600                             stopinfo->spstop = stop;
1601                             stopinfo->draggable = draggable;
1602                             stopinfo->gradient = gradient;
1603                             stopinfo->vector = vector;
1604                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1605                             bool present = false;
1606                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1607                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1608                                     present = true;
1609                                     break; // no need to search further.
1610                                 }
1611                             }
1612                             if (!present)
1613                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1614                         }
1615                     }
1616                     break;
1617                 default:
1618                     break;
1619             }
1620         }
1621         selected = g_list_remove(selected, dragger);
1622         if ( just_one ) break; // iterate once if just_one is set.
1623     }
1624     while (midstoplist) {
1625         SPStop *stop = (SPStop*) midstoplist->data;
1626         document = SP_OBJECT_DOCUMENT (stop);
1627         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1628         parent->removeChild(SP_OBJECT_REPR(stop));
1629         midstoplist = g_slist_remove(midstoplist, stop);
1630     }
1631     while (endstoplist) {
1632         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1633         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1635         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1636         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1637         // manually count the children, don't know if there already exists a function for this...
1638         int len = 0;
1639         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1640               child != NULL ;
1641               child = SP_OBJECT_NEXT(child) )
1642         {
1643             if ( SP_IS_STOP(child) )  len ++;
1644         }
1645         if (len > 2)
1646         {
1647             switch (stopinfo->draggable->point_type) {
1648                 case POINT_LG_BEGIN:
1649                     {
1650                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1652                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1653                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1654                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1655                         SPStop *stop = sp_first_stop(stopinfo->vector);
1656                         gdouble offset = stop->offset;
1657                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1658                         lg->x1.computed = newbegin[NR::X];
1659                         lg->y1.computed = newbegin[NR::Y];
1661                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1662                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1663                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1664                         stop->offset = 0;
1665                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1667                         // iterate through midstops to set new offset values such that they won't move on canvas.
1668                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1669                         stop = sp_next_stop(stop);
1670                         while ( stop != laststop ) {
1671                             stop->offset = (stop->offset - offset)/(1 - offset);
1672                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1673                             stop = sp_next_stop(stop);
1674                         }
1675                     }
1676                     break;
1677                 case POINT_LG_END:
1678                     {
1679                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1681                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1682                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1683                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1684                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1685                         gdouble offset = laststop->offset;
1686                         NR::Point newend = begin + offset * (oldend - begin);
1687                         lg->x2.computed = newend[NR::X];
1688                         lg->y2.computed = newend[NR::Y];
1690                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1691                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1692                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1693                         laststop->offset = 1;
1694                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1696                         // iterate through midstops to set new offset values such that they won't move on canvas.
1697                         SPStop *stop = sp_first_stop(stopinfo->vector);
1698                         stop = sp_next_stop(stop);
1699                         while ( stop != laststop ) {
1700                             stop->offset = stop->offset / offset;
1701                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1702                             stop = sp_next_stop(stop);
1703                         }
1704                     }
1705                     break;
1706                 case POINT_RG_CENTER:
1707                     {
1708                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1709                         if (newfirst) {
1710                             newfirst->offset = 0;
1711                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1712                         }
1713                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1714                     }
1715                     break;
1716                 case POINT_RG_R1:
1717                 case POINT_RG_R2:
1718                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1720                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1721                         double oldradius = rg->r.computed;
1722                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1723                         gdouble offset = laststop->offset;
1724                         double newradius = offset * oldradius;
1725                         rg->r.computed = newradius;
1727                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1728                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
1729                         laststop->offset = 1;
1730                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1732                         // iterate through midstops to set new offset values such that they won't move on canvas.
1733                         SPStop *stop = sp_first_stop(stopinfo->vector);
1734                         stop = sp_next_stop(stop);
1735                         while ( stop != laststop ) {
1736                             stop->offset = stop->offset / offset;
1737                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1738                             stop = sp_next_stop(stop);
1739                         }
1740                         break;
1741             }
1742         }
1743         else
1744         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
1745             SPCSSAttr *css = sp_repr_css_attr_new ();
1746             if (stopinfo->draggable->fill_or_stroke) {
1747                 sp_repr_css_unset_property (css, "fill");
1748             } else {
1749                 sp_repr_css_unset_property (css, "stroke");
1750             }
1751             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1752             sp_repr_css_attr_unref (css);
1753         }
1755         endstoplist = g_slist_remove(endstoplist, stopinfo);
1756         delete stopinfo;
1757     }
1759     if (document) {
1760         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1761     }
1765 /*
1766   Local Variables:
1767   mode:c++
1768   c-file-style:"stroustrup"
1769   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1770   indent-tabs-mode:nil
1771   fill-column:99
1772   End:
1773 */
1774 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :