Code

Fork gradientvector upon deletion of stops.
[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 #define GR_KNOT_COLOR_NORMAL 0xffffff00
45 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
47 #define GR_LINE_COLOR_FILL 0x0000ff7f
48 #define GR_LINE_COLOR_STROKE 0x9999007f
50 // screen pixels between knots when they snap:
51 #define SNAP_DIST 5
53 // absolute distance between gradient points for them to become a single dragger when the drag is created:
54 #define MERGE_DIST 0.1
56 // knot shapes corresponding to GrPointType enum
57 SPKnotShapeType gr_knot_shapes [] = {
58         SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
59         SP_KNOT_SHAPE_CIRCLE,  //POINT_LG_END
60         SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
61         SP_KNOT_SHAPE_SQUARE,  // POINT_RG_CENTER
62         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R1
63         SP_KNOT_SHAPE_CIRCLE,  // POINT_RG_R2
64         SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
65         SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
66         SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
67 };
69 const gchar *gr_knot_descr [] = {
70     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
71     N_("Linear gradient <b>end</b>"),
72     N_("Linear gradient <b>midstop</b>"),
73     N_("Radial gradient <b>center</b>"),
74     N_("Radial gradient <b>radius</b>"),
75     N_("Radial gradient <b>radius</b>"),
76     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
77     N_("Linear gradient <b>midstop</b>"),
78     N_("Linear gradient <b>midstop</b>")
79 };
81 static void
82 gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
83 {
84         GrDrag *drag = (GrDrag *) data;
85         drag->updateDraggers ();
86         drag->updateLines ();
87         drag->updateLevels ();
88 }
90 static void
91 gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
92 {
93     GrDrag *drag = (GrDrag *) data;
94     if (drag->local_change) {
95         drag->local_change = false;
96     } else {
97         drag->updateDraggers ();
98     }
99     drag->updateLines ();
100     drag->updateLevels ();
103 /**
104 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
105 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
106 any.
107 */
108 int
109 gr_drag_style_query (SPStyle *style, int property, gpointer data)
111     GrDrag *drag = (GrDrag *) data;
113     if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
114         return QUERY_STYLE_NOTHING;
115     }
117     if (!drag->selected) {
118         return QUERY_STYLE_NOTHING;
119     } else {
120         int ret = QUERY_STYLE_NOTHING;
122         float cf[4];
123         cf[0] = cf[1] = cf[2] = cf[3] = 0;
125         int count = 0;
127         for (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger
128             GrDraggable *draggable = (GrDraggable *) i->data;
130             if (ret == QUERY_STYLE_NOTHING) {
131                 ret = QUERY_STYLE_SINGLE;
132             } else if (ret == QUERY_STYLE_SINGLE) {
133                 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
134             }
136             guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
137             cf[0] += SP_RGBA32_R_F (c);
138             cf[1] += SP_RGBA32_G_F (c);
139             cf[2] += SP_RGBA32_B_F (c);
140             cf[3] += SP_RGBA32_A_F (c);
142             count ++;
143         }
145         if (count) {
146             cf[0] /= count;
147             cf[1] /= count;
148             cf[2] /= count;
149             cf[3] /= count;
151             // set both fill and stroke with our stop-color and stop-opacity
152             sp_color_set_rgb_float((SPColor *) &style->fill.value.color, cf[0], cf[1], cf[2]);
153             style->fill.set = TRUE;
154             style->fill.type = SP_PAINT_TYPE_COLOR;
155             sp_color_set_rgb_float((SPColor *) &style->stroke.value.color, cf[0], cf[1], cf[2]);
156             style->stroke.set = TRUE;
157             style->stroke.type = SP_PAINT_TYPE_COLOR;
159             style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
160             style->fill_opacity.set = TRUE;
161             style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
162             style->stroke_opacity.set = TRUE;
164             style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
165             style->opacity.set = TRUE;
166         }
168         return ret;
169     }
172 bool
173 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
175     GrDrag *drag = (GrDrag *) data;
177     if (!drag->selected)
178         return false;
180     SPCSSAttr *stop = sp_repr_css_attr_new ();
182     // See if the css contains interesting properties, and if so, translate them into the format
183     // acceptable for gradient stops
185     // any of color properties, in order of increasing priority:
186     if (css->attribute("flood-color"))
187         sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
189     if (css->attribute("lighting-color"))
190         sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
192     if (css->attribute("color"))
193         sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
195     if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
196         sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
198     if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
199         sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
201     if (css->attribute("stop-color"))
202         sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
205     if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
206         sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
207     } else {  // multiply all opacity properties:
208         gdouble accumulated = 1.0;
209         accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
210         accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
211         accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
212         accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
214         Inkscape::CSSOStringStream os;
215         os << accumulated;
216         sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
218         if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
219             (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
220             sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
221     }
223     if (!stop->attributeList()) { // nothing for us here, pass it on
224         sp_repr_css_attr_unref(stop);
225         return false;
226     }
228     for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
229         GrDragger* dragger = (GrDragger*) sel->data;
230         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
231                GrDraggable *draggable = (GrDraggable *) i->data;
233                drag->local_change = true;
234                sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
235         }
236     }
238     //sp_repr_css_print(stop);
239     sp_repr_css_attr_unref(stop);
240     return true;
243 GrDrag::GrDrag(SPDesktop *desktop) {
245     this->desktop = desktop;
247     this->selection = sp_desktop_selection(desktop);
249     this->draggers = NULL;
250     this->lines = NULL;
251     this->selected = NULL;
253     this->hor_levels.clear();
254     this->vert_levels.clear();
256     this->local_change = false;
258     this->sel_changed_connection = this->selection->connectChanged(
259         sigc::bind (
260             sigc::ptr_fun(&gr_drag_sel_changed),
261             (gpointer)this )
263         );
264     this->sel_modified_connection = this->selection->connectModified(
265         sigc::bind(
266             sigc::ptr_fun(&gr_drag_sel_modified),
267             (gpointer)this )
268         );
270     this->style_set_connection = this->desktop->connectSetStyle(
271         sigc::bind(
272             sigc::ptr_fun(&gr_drag_style_set),
273             (gpointer)this )
274         );
276     this->style_query_connection = this->desktop->connectQueryStyle(
277         sigc::bind(
278             sigc::ptr_fun(&gr_drag_style_query),
279             (gpointer)this )
280         );
282     this->updateDraggers ();
283     this->updateLines ();
284     this->updateLevels ();
286     if (desktop->gr_item) {
287         this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
288     }
291 GrDrag::~GrDrag()
293     this->sel_changed_connection.disconnect();
294     this->sel_modified_connection.disconnect();
295     this->style_set_connection.disconnect();
296     this->style_query_connection.disconnect();
298     if (this->selected) {
299         GrDraggable *draggable = (GrDraggable *)   ((GrDragger*)this->selected->data)->draggables->data;
300         desktop->gr_item = draggable->item;
301         desktop->gr_point_type = draggable->point_type;
302         desktop->gr_point_i = draggable->point_i;
303         desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
304     } else {
305         desktop->gr_item = NULL;
306         desktop->gr_point_type = 0;
307         desktop->gr_point_i = 0;
308         desktop->gr_fill_or_stroke = true;
309     }
311     deselect_all();
312     for (GList *l = this->draggers; l != NULL; l = l->next) {
313         delete ((GrDragger *) l->data);
314     }
315     g_list_free (this->draggers);
316     this->draggers = NULL;
317     this->selected = NULL;
319     for (GSList *l = this->lines; l != NULL; l = l->next) {
320         gtk_object_destroy( GTK_OBJECT (l->data));
321     }
322     g_slist_free (this->lines);
323     this->lines = NULL;
326 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
328     this->item = item;
329     this->point_type = point_type;
330     this->point_i = point_i;
331     this->fill_or_stroke = fill_or_stroke;
333     g_object_ref (G_OBJECT (this->item));
336 GrDraggable::~GrDraggable ()
338     g_object_unref (G_OBJECT (this->item));
341 // FIXME: make global function in libnr or somewhere.
342 static NR::Point *
343 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
345     double r = NR::L2 (p - o);
346     if (r < 1e-3)
347         return NULL;
348     double angle = NR::atan2 (p - o);
349     // snap angle to snaps increments, starting from initial:
350     double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
351     // calculate the new position and subtract p to get the vector:
352     return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
355 // FIXME: make global function in libnr or somewhere.
356 static NR::Point
357 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
359     double length = NR::L2(end - begin);
360     NR::Point be = (end - begin) / length;
361     double r = NR::dot(p - begin, be);
363     if (r < 0.0) return begin;
364     if (r > length) return end;
366     double snapdist = length * snap;
367     double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
369     return (begin + r_snapped * be);
372 static void
373 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
375     GrDragger *dragger = (GrDragger *) data;
376     GrDrag *drag = dragger->parent;
378     NR::Point p = *ppointer;
380     // FIXME: take from prefs
381     double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
383     if (state & GDK_SHIFT_MASK) {
384         // with Shift; unsnap if we carry more than one draggable
385         if (dragger->draggables && dragger->draggables->next) {
386             // create a new dragger
387             GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
388             dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
389             // relink to it all but the first draggable in the list
390             for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
391                 GrDraggable *draggable = (GrDraggable *) i->data;
392                 dr_new->addDraggable (draggable);
393             }
394             dr_new->updateKnotShape();
395             g_slist_free (dragger->draggables->next);
396             dragger->draggables->next = NULL;
397             dragger->updateKnotShape();
398             dragger->updateTip();
399         }
400     } else if (!(state & GDK_CONTROL_MASK)) {
401         // without Shift or Ctrl; see if we need to snap to another dragger
402         for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
403             GrDragger *d_new = (GrDragger *) di->data;
404             if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
406                 // Merge draggers:
407                 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
408                     GrDraggable *draggable = (GrDraggable *) i->data;
409                     // copy draggable to d_new:
410                     GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
411                     d_new->addDraggable (da_new);
412                 }
414                 // unlink and delete this dragger
415                 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
416                 delete dragger;
418                 // update the new merged dragger
419                 d_new->fireDraggables(true, false, true);
420                 d_new->parent->updateLines();
421                 d_new->parent->setSelected (d_new);
422                 d_new->updateKnotShape ();
423                 d_new->updateTip ();
424                 d_new->updateDependencies(true);
425                 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
426                                   _("Merge gradient handles"));
427                 return;
428             }
429         }
430     }
433     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
434         // See if we need to snap to any of the levels
435         for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
436             if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
437                 p[NR::Y] = dragger->parent->hor_levels[i];
438                 sp_knot_moveto (knot, &p);
439             }
440         }
441         for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
442             if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
443                 p[NR::X] = dragger->parent->vert_levels[i];
444                 sp_knot_moveto (knot, &p);
445             }
446         }
447     }
449     if (state & GDK_CONTROL_MASK) {
450         unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
451         /* 0 means no snapping. */
453         // This list will store snap vectors from all draggables of dragger
454         GSList *snap_vectors = NULL;
456         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
457             GrDraggable *draggable = (GrDraggable *) i->data;
459             NR::Point *dr_snap = NULL;
461             if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
462                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
463                     GrDragger *d_new = (GrDragger *) di->data;
464                     if (d_new == dragger)
465                         continue;
466                     if (d_new->isA (draggable->item,
467                                     draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
468                                     draggable->point_i,
469                                     draggable->fill_or_stroke)) {
470                         // found the other end of the linear gradient;
471                         if (state & GDK_SHIFT_MASK) {
472                             // moving linear around center
473                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
474                             dr_snap = &center;
475                         } else {
476                             // moving linear around the other end
477                             dr_snap = &d_new->point;
478                         }
479                     }
480                 }
481             } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
482                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
483                     GrDragger *d_new = (GrDragger *) di->data;
484                     if (d_new == dragger)
485                         continue;
486                     if (d_new->isA (draggable->item,
487                                     POINT_RG_CENTER,
488                                     draggable->point_i,
489                                     draggable->fill_or_stroke)) {
490                         // found the center of the radial gradient;
491                         dr_snap = &(d_new->point);
492                     }
493                 }
494             } else if (draggable->point_type == POINT_RG_CENTER) {
495                 // radial center snaps to hor/vert relative to its original position
496                 dr_snap = &(dragger->point_original);
497             }
499             NR::Point *snap_vector = NULL;
500             if (dr_snap) {
501                 if (state & GDK_MOD1_MASK) {
502                     // with Alt, snap to the original angle and its perpendiculars
503                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
504                 } else {
505                     // with Ctrl, snap to M_PI/snaps
506                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
507                 }
508             }
509             if (snap_vector) {
510                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
511             }
512         }
514         // Move by the smallest of snap vectors:
515         NR::Point move(9999, 9999);
516         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
517             NR::Point *snap_vector = (NR::Point *) i->data;
518             if (NR::L2(*snap_vector) < NR::L2(move))
519                 move = *snap_vector;
520         }
521         if (move[NR::X] < 9999) {
522             p += move;
523             sp_knot_moveto (knot, &p);
524         }
526         g_slist_free(snap_vectors);
527     }
529     dragger->point = p;
531     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
532         dragger->fireDraggables (false, true);
533     } else {
534         dragger->fireDraggables (false);
535     }
537     dragger->updateDependencies(false);
539     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
543 /**
544 Called when a midpoint knot is dragged.
545 */
546 static void
547 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
549     GrDragger *dragger = (GrDragger *) data;
550     GrDrag *drag = dragger->parent;
551     // a midpoint dragger can (logically) only contain one GrDraggable
552     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
554     // FIXME: take from prefs
555     double snap_fraction = 0.1;
557     NR::Point p = *ppointer;
558     NR::Point begin(0,0), end(0,0);
560     SPObject *server;
561     if (draggable->fill_or_stroke)
562         server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
563     else
564         server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
567     // get begin and end points between which dragging is allowed:
568     // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
569     GSList *moving = NULL;
570     moving = g_slist_append(moving, dragger);
572     guint lowest_i = draggable->point_i;
573     guint highest_i = draggable->point_i;
574     GrDragger *lowest_dragger = dragger;
575     GrDragger *highest_dragger = dragger;
576     bool is_selected = g_list_find(drag->selected, dragger);
577     if (is_selected) {
578         GrDragger* d_add;
579         while ( true )
580         {
581             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
582             if ( d_add && g_list_find(drag->selected, d_add) ) {
583                 lowest_i = lowest_i - 1;
584                 moving = g_slist_prepend(moving, d_add);
585                 lowest_dragger = d_add;
586             } else {
587                 break;
588             }
589         }
591         while ( true )
592         {
593             d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
594             if ( d_add && g_list_find(drag->selected, d_add) ) {
595                 highest_i = highest_i + 1;
596                 moving = g_slist_append(moving, d_add);
597                 highest_dragger = d_add;
598             } else {
599                 break;
600             }
601         }
602     }
604     if ( SP_IS_LINEARGRADIENT(server) ) {
605         GrDragger *d_temp;
606         if (lowest_i == 1) {
607             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
608         } else {
609             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
610         }
611         if (d_temp) begin = d_temp->point;
613         d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
614         if (d_temp == NULL) {
615             d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke);
616         }
617         if (d_temp) end = d_temp->point;
618     } else if ( SP_IS_RADIALGRADIENT(server) ) {
619         GrDragger *d_temp;
620         if (lowest_i == 1) {
621             d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
622         } else {
623             d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
624         }
625         if (d_temp) begin = d_temp->point;
627         d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
628         if (d_temp == NULL) {
629             d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, 0, draggable->fill_or_stroke);
630         }
631         if (d_temp) end = d_temp->point;
632     }
634     NR::Point low_lim  = dragger->point - (lowest_dragger->point - begin);
635     NR::Point high_lim = dragger->point - (highest_dragger->point - end);
637     if (state & GDK_CONTROL_MASK) {
638         p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
639     } else {
640         p = snap_vector_midpoint (p, low_lim, high_lim, 0);
641     }
642     NR::Point displacement = p - dragger->point;
644     for (GSList const* i = moving; i != NULL; i = i->next) {
645         GrDragger *drg = (GrDragger*) i->data;
646         SPKnot *drgknot = drg->knot;
647         drg->point += displacement;
648         sp_knot_moveto (drgknot, & drg->point);
649         drg->fireDraggables (false);
650         drg->updateDependencies(false);
651     }
653     drag->keep_selection = is_selected;
658 static void
659 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
661     GrDragger *dragger = (GrDragger *) data;
663     sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
666 /**
667 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
668 */
669 static void
670 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
672     GrDragger *dragger = (GrDragger *) data;
674     sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
676     dragger->point_original = dragger->point = knot->pos;
678     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
679         dragger->fireDraggables (true, true);
680     } else {
681         dragger->fireDraggables (true);
682     }
684     // make this dragger selected
685     if (!dragger->parent->keep_selection) {
686         dragger->parent->setSelected (dragger);
687     }
688     dragger->parent->keep_selection = false;
690     dragger->updateDependencies(true);
692     // we did an undoable action
693     sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
694                       _("Move gradient handle"));
697 /**
698 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
699 state of the keyboard keys
700 */
701 static void
702 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
704     GrDragger *dragger = (GrDragger *) data;
705     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
706     if (!draggable) return;
708     if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
709     // delete this knot from vector
710         SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
711         gradient = sp_gradient_get_vector (gradient, false);
712         if (gradient->vector.stops.size() > 2) { // 2 is the minimum
713                 SPStop *stop = NULL;
714                 switch (draggable->point_type) {  // if we delete first or last stop, move the next/previous to the edge
715                 case POINT_LG_BEGIN:
716                 case POINT_RG_CENTER:
717                     stop = sp_first_stop(gradient);
718                         {
719                             SPStop *next = sp_next_stop (stop);
720                                 if (next) {
721                                         next->offset = 0;
722                                         sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
723                                 }
724                         }
725                     break;
726                 case POINT_LG_END:
727                 case POINT_RG_R1:
728                 case POINT_RG_R2:
729                     stop = sp_last_stop(gradient);
730                     {
731                             SPStop *prev = sp_prev_stop (stop, gradient);
732                             if (prev) {
733                                     prev->offset = 1;
734                                     sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
735                             }
736                         }
737                     break;
738                 case POINT_LG_MID:
739                 case POINT_RG_MID1:
740                 case POINT_RG_MID2:
741                     stop = sp_get_stop_i(gradient, draggable->point_i);
742                     break;
743                 }
745                 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
746                 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
747                                   _("Delete gradient stop"));
748         }
749     } else {
750     // select the dragger
751         dragger->point_original = dragger->point;
753         if ( state & GDK_SHIFT_MASK ) {
754             dragger->parent->setSelected (dragger, true, false);
755         } else {
756             dragger->parent->setSelected (dragger);
757         }
758     }
761 /**
762 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
763 */
764 static void
765 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
767     GrDragger *dragger = (GrDragger *) data;
769     dragger->point_original = dragger->point;
771     if (dragger->draggables == NULL)
772         return;
774     GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
775     sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
778 /**
779 Act upon all draggables of the dragger, setting them to the dragger's point
780 */
781 void
782 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
784     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
785         GrDraggable *draggable = (GrDraggable *) i->data;
787         // set local_change flag so that selection_changed callback does not regenerate draggers
788         this->parent->local_change = true;
790         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
791         // to the center, unless it's the first update upon merge when we must snap it to the point
792         if (merging_focus ||
793             !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
794             sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
795     }
798 /**
799 Checks if the dragger has a draggable with this point_type
800  */
801 bool
802 GrDragger::isA (guint point_type)
804     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
805         GrDraggable *draggable = (GrDraggable *) i->data;
806         if (draggable->point_type == point_type) {
807             return true;
808         }
809     }
810     return false;
813 /**
814 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
815  */
816 bool
817 GrDragger::isA (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
819     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
820         GrDraggable *draggable = (GrDraggable *) i->data;
821         if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
822             return true;
823         }
824     }
825     return false;
828 bool
829 GrDraggable::mayMerge (GrDraggable *da2)
831     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
832         // we must not merge the points of the same gradient!
833         if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
834               (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
835             // except that we can snap center and focus together
836             return false;
837         }
838     }
839     // disable merging of midpoints.
840     if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
841          || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
842          || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
843         return false;
845     return true;
848 bool
849 GrDragger::mayMerge (GrDragger *other)
851     if (this == other)
852         return false;
854     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
855         GrDraggable *da1 = (GrDraggable *) i->data;
856         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
857             GrDraggable *da2 = (GrDraggable *) j->data;
858             if (!da1->mayMerge(da2))
859                 return false;
860         }
861     }
862     return true;
865 bool
866 GrDragger::mayMerge (GrDraggable *da2)
868     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
869         GrDraggable *da1 = (GrDraggable *) i->data;
870         if (!da1->mayMerge(da2))
871             return false;
872     }
873     return true;
876 /**
877 Updates the statusbar tip of the dragger knot, based on its draggables
878  */
879 void
880 GrDragger::updateTip ()
882         if (this->knot && this->knot->tip) {
883                 g_free (this->knot->tip);
884                 this->knot->tip = NULL;
885         }
887     if (g_slist_length (this->draggables) == 1) {
888         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
889         char *item_desc = sp_item_description(draggable->item);
890         switch (draggable->point_type) {
891             case POINT_LG_MID:
892             case POINT_RG_MID1:
893             case POINT_RG_MID2:
894                 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"),
895                                                    _(gr_knot_descr[draggable->point_type]),
896                                                    draggable->point_i,
897                                                    item_desc,
898                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
899                 break;
901             default:
902                 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"),
903                                                    _(gr_knot_descr[draggable->point_type]),
904                                                    item_desc,
905                                                    draggable->fill_or_stroke == false ? _(" (stroke)") : "");
906                 break;
907         }
908         g_free(item_desc);
909     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
910         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
911     } else {
912         int length = g_slist_length (this->draggables);
913         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
914                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
915                                                     length),
916                                            length);
917     }
920 /**
921 Adds a draggable to the dragger
922  */
923 void
924 GrDragger::updateKnotShape ()
926     if (!draggables)
927         return;
928     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
929     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
932 /**
933 Adds a draggable to the dragger
934  */
935 void
936 GrDragger::addDraggable (GrDraggable *draggable)
938     this->draggables = g_slist_prepend (this->draggables, draggable);
940     this->updateTip();
944 /**
945 Moves this dragger to the point of the given draggable, acting upon all other draggables
946  */
947 void
948 GrDragger::moveThisToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
950     this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
951     this->point_original = this->point;
953     sp_knot_moveto (this->knot, &(this->point));
955     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
956         GrDraggable *da = (GrDraggable *) i->data;
957         if ( (da->item == item) && (da->point_type == point_type) && (da->point_i == point_i) && (da->fill_or_stroke == fill_or_stroke) ) {
958             continue;
959         }
960         sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
961     }
962     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
966 /**
967 Moves all midstop draggables that depend on this one
968  */
969 void
970 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
971     SPObject *server;
972     if (draggable->fill_or_stroke)
973         server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
974     else
975         server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
976     guint num = SP_GRADIENT(server)->vector.stops.size();
977     if (num <= 2) return;
979     if ( SP_IS_LINEARGRADIENT(server) ) {
980         for ( guint i = 1; i < num - 1; i++ ) {
981             this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
982         }
983     } else  if ( SP_IS_RADIALGRADIENT(server) ) {
984         for ( guint i = 1; i < num - 1; i++ ) {
985             this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
986             this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
987         }
988     }
992 /**
993 Moves all draggables that depend on this one
994  */
995 void
996 GrDragger::updateDependencies (bool write_repr)
998     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
999         GrDraggable *draggable = (GrDraggable *) i->data;
1000         switch (draggable->point_type) {
1001             case POINT_LG_BEGIN:
1002                 {
1003                     // the end point is dependent only when dragging with ctrl+shift
1004                     this->moveOtherToDraggable (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke, write_repr);
1006                     this->updateMidstopDependencies (draggable, write_repr);
1007                 }
1008                 break;
1009             case POINT_LG_END:
1010                 {
1011                     // the begin point is dependent only when dragging with ctrl+shift
1012                     this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1014                     this->updateMidstopDependencies (draggable, write_repr);
1015                 }
1016                 break;
1017             case POINT_LG_MID:
1018                 // no other nodes depend on mid points.
1019                 break;
1020             case POINT_RG_R2:
1021                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1022                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1023                 this->updateMidstopDependencies (draggable, write_repr);
1024                 break;
1025             case POINT_RG_R1:
1026                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
1027                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1028                 this->updateMidstopDependencies (draggable, write_repr);
1029                 break;
1030             case POINT_RG_CENTER:
1031                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1032                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 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_FOCUS:
1037                 // nothing can depend on that
1038                 break;
1039             case POINT_RG_MID1:
1040                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1041                 break;
1042             case POINT_RG_MID2:
1043                 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1044                 break;
1045             default:
1046                 break;
1047         }
1048     }
1053 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1055     this->draggables = NULL;
1057     this->parent = parent;
1059     this->point = p;
1060     this->point_original = p;
1062     // create the knot
1063     this->knot = sp_knot_new (parent->desktop, NULL);
1064     this->knot->setMode(SP_KNOT_MODE_XOR);
1065     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1066     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1067     sp_knot_update_ctrl(this->knot);
1069     // move knot to the given point
1070     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1071     sp_knot_show (this->knot);
1073     // connect knot's signals
1074     if ( (draggable)  // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1075                        // luckily, midstops never snap to other nodes so are never unsnapped...
1076          && ( (draggable->point_type == POINT_LG_MID)
1077               || (draggable->point_type == POINT_RG_MID1)
1078               || (draggable->point_type == POINT_RG_MID2) ) )
1079     {
1080         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1081     } else {
1082         this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1083     }
1084     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1085     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1086     g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1087     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1089     // add the initial draggable
1090     if (draggable)
1091         this->addDraggable (draggable);
1092     updateKnotShape();
1095 GrDragger::~GrDragger ()
1097     // unselect if it was selected
1098     this->parent->setDeselected(this);
1100     // disconnect signals
1101     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1102     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1103     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1104     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1105     g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1107     /* unref should call destroy */
1108     g_object_unref (G_OBJECT (this->knot));
1110     // delete all draggables
1111     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1112         delete ((GrDraggable *) i->data);
1113     }
1114     g_slist_free (this->draggables);
1115     this->draggables = NULL;
1118 /**
1119 Select the dragger which has the given draggable.
1120 */
1121 GrDragger *
1122 GrDrag::getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
1124     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1125         GrDragger *dragger = (GrDragger *) i->data;
1126         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1127             GrDraggable *da2 = (GrDraggable *) j->data;
1128             if ( (da2->item == item) && (da2->point_type == point_type) && (da2->point_i == point_i) && (da2->fill_or_stroke == fill_or_stroke)) {
1129                 return (dragger);
1130             }
1131         }
1132     }
1133     return NULL;
1137 void
1138 GrDragger::moveOtherToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
1140     GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1141     if (d && d !=  this) {
1142         d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1143     }
1147 /**
1148   Draw this dragger as selected
1149 */
1150 void
1151 GrDragger::select()
1153     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1154     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1157 /**
1158   Draw this dragger as normal (deselected)
1159 */
1160 void
1161 GrDragger::deselect()
1163     this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1164     g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1169 /**
1170 \brief Deselect all stops/draggers (private)
1171 */
1172 void
1173 GrDrag::deselect_all()
1175     while (selected) {
1176         ( (GrDragger*) selected->data)->deselect();
1177         selected = g_list_remove(selected, selected->data);
1178     }
1181 /**
1182 \brief Deselect all stops/draggers (public; emits signal)
1183 */
1184 void
1185 GrDrag::deselectAll()
1187     deselect_all();
1188     this->desktop->emitToolSubselectionChanged(NULL);
1192 /**
1193 \brief Select a dragger
1194 \param dragger       The dragger to select
1195 \param add_to_selection   If true, add to selection, otherwise deselect others
1196 \param override      If true, always select this node, otherwise toggle selected status
1197 */
1198 void
1199 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1201     GrDragger *seldragger = NULL;
1203     if (add_to_selection) {
1204         if (!dragger) return;
1205         if (override) {
1206             if (!g_list_find(selected, dragger)) {
1207                 selected = g_list_prepend(selected, dragger);
1208             }
1209             dragger->select();
1210             seldragger = dragger;
1211         } else { // toggle
1212             if (g_list_find(selected, dragger)) {
1213                 selected = g_list_remove(selected, dragger);
1214                 dragger->deselect();
1215                 if (selected) {
1216                     seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1217                 }
1218             } else {
1219                 selected = g_list_prepend(selected, dragger);
1220                 dragger->select();
1221                 seldragger = dragger;
1222             }
1223         }
1224     } else {
1225         deselect_all();
1226         if (dragger) {
1227             selected = g_list_prepend(selected, dragger);
1228             dragger->select();
1229             seldragger = dragger;
1230         }
1231     }
1232     if (seldragger) {
1233         this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1234     }
1237 /**
1238 \brief Deselect a dragger
1239 \param dragger       The dragger to deselect
1240 */
1241 void
1242 GrDrag::setDeselected (GrDragger *dragger)
1244     if (g_list_find(selected, dragger)) {
1245         selected = g_list_remove(selected, dragger);
1246         dragger->deselect();
1247     }
1248     this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1253 /**
1254 Create a line from p1 to p2 and add it to the lines list
1255  */
1256 void
1257 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1259     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1260                                                             SP_TYPE_CTRLLINE, NULL);
1261     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1262     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1263         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1264     sp_canvas_item_show (line);
1265     this->lines = g_slist_append (this->lines, line);
1268 /**
1269 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1270 new dragger and add it to draggers list
1271  */
1272 void
1273 GrDrag::addDragger (GrDraggable *draggable)
1275     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1277     for (GList *i = this->draggers; i != NULL; i = i->next) {
1278         GrDragger *dragger = (GrDragger *) i->data;
1279         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1280             // distance is small, merge this draggable into dragger, no need to create new dragger
1281             dragger->addDraggable (draggable);
1282             dragger->updateKnotShape();
1283             return;
1284         }
1285     }
1287     GrDragger *new_dragger = new GrDragger(this, p, draggable);
1288     // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1289     this->draggers = g_list_append (this->draggers, new_dragger);
1292 /**
1293 Add draggers for the radial gradient rg on item
1294 */
1295 void
1296 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1298     addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1299     guint num = rg->vector.stops.size();
1300     if (num > 2) {
1301         for ( guint i = 1; i < num - 1; i++ ) {
1302             addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1303         }
1304     }
1305     addDragger (new GrDraggable (item, POINT_RG_R1, 0, fill_or_stroke));
1306     if (num > 2) {
1307         for ( guint i = 1; i < num - 1; i++ ) {
1308             addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1309         }
1310     }
1311     addDragger (new GrDraggable (item, POINT_RG_R2, 0, fill_or_stroke));
1312     addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1315 /**
1316 Add draggers for the linear gradient lg on item
1317 */
1318 void
1319 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1321     addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1322     guint num = lg->vector.stops.size();
1323     if (num > 2) {
1324         for ( guint i = 1; i < num - 1; i++ ) {
1325             addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1326         }
1327     }
1328     addDragger (new GrDraggable (item, POINT_LG_END, 0, fill_or_stroke));
1331 /**
1332 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1333 */
1334 void
1335 GrDrag::grabKnot (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1337     GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1338     if (dragger) {
1339         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1340     }
1343 /**
1344 Regenerates the draggers list from the current selection; is called when selection is changed or
1345 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1346 */
1347 void
1348 GrDrag::updateDraggers ()
1350     while (selected) {
1351         selected = g_list_remove(selected, selected->data);
1352     }
1353     // delete old draggers
1354     for (GList const* i = this->draggers; i != NULL; i = i->next) {
1355         delete ((GrDragger *) i->data);
1356     }
1357     g_list_free (this->draggers);
1358     this->draggers = NULL;
1360     g_return_if_fail (this->selection != NULL);
1362     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1364         SPItem *item = SP_ITEM(i->data);
1365         SPStyle *style = SP_OBJECT_STYLE (item);
1367         if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1368             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1369             if (SP_IS_LINEARGRADIENT (server)) {
1370                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1371             } else if (SP_IS_RADIALGRADIENT (server)) {
1372                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1373             }
1374         }
1376         if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1377             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1378             if (SP_IS_LINEARGRADIENT (server)) {
1379                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1380             } else if (SP_IS_RADIALGRADIENT (server)) {
1381                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1382             }
1383         }
1384     }
1387 /**
1388 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1389 lines are always in sync with the actual gradient
1390 */
1391 void
1392 GrDrag::updateLines ()
1394     // delete old lines
1395     for (GSList const *i = this->lines; i != NULL; i = i->next) {
1396         gtk_object_destroy( GTK_OBJECT (i->data));
1397     }
1398     g_slist_free (this->lines);
1399     this->lines = NULL;
1401     g_return_if_fail (this->selection != NULL);
1403     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1405         SPItem *item = SP_ITEM(i->data);
1407         SPStyle *style = SP_OBJECT_STYLE (item);
1409         if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1410             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1411             if (SP_IS_LINEARGRADIENT (server)) {
1412                 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);
1413             } else if (SP_IS_RADIALGRADIENT (server)) {
1414                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1415                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1416                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1417             }
1418         }
1420         if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1421             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1422             if (SP_IS_LINEARGRADIENT (server)) {
1423                 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);
1424             } else if (SP_IS_RADIALGRADIENT (server)) {
1425                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1426                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1427                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1428             }
1429         }
1430     }
1433 /**
1434 Regenerates the levels list from the current selection
1435 */
1436 void
1437 GrDrag::updateLevels ()
1439     hor_levels.clear();
1440     vert_levels.clear();
1442     g_return_if_fail (this->selection != NULL);
1444     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1445         SPItem *item = SP_ITEM(i->data);
1446         NR::Rect rect = sp_item_bbox_desktop (item);
1447         // Remember the edges of the bbox and the center axis
1448         hor_levels.push_back(rect.min()[NR::Y]);
1449         hor_levels.push_back(rect.max()[NR::Y]);
1450         hor_levels.push_back(0.5 * (rect.min()[NR::Y] + rect.max()[NR::Y]));
1451         vert_levels.push_back(rect.min()[NR::X]);
1452         vert_levels.push_back(rect.max()[NR::X]);
1453         vert_levels.push_back(0.5 * (rect.min()[NR::X] + rect.max()[NR::X]));
1454     }
1457 void
1458 GrDrag::selected_reverse_vector ()
1460     if (selected == NULL)
1461         return;
1463     for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1464         GrDraggable *draggable = (GrDraggable *) i->data;
1466         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1467     }
1470 void
1471 GrDrag::selected_move (double x, double y)
1473 /*
1474     if (selected == NULL)
1475         return;
1477     if ( (g_list_length(selected) == 1) )
1478     selected->point += NR::Point (x, y);
1479     selected->point_original = selected->point;
1480     sp_knot_moveto (selected->knot, &(selected->point));
1482     selected->fireDraggables (true);
1484     selected->updateDependencies(true);
1486     // we did an undoable action
1487     sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
1488                       _("Move gradient handle"));
1489 */
1492 void
1493 GrDrag::selected_move_screen (double x, double y)
1495     gdouble zoom = desktop->current_zoom();
1496     gdouble zx = x / zoom;
1497     gdouble zy = y / zoom;
1499     selected_move (zx, zy);
1502 /**
1503 Select the knot next to the last selected one and deselect all other selected.
1504 */
1505 void
1506 GrDrag::select_next ()
1508     if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1509         if (draggers)
1510             setSelected ((GrDragger *) draggers->data);
1511     } else {
1512         setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
1513     }
1516 /**
1517 Select the knot previous from the last selected one and deselect all other selected.
1518 */
1519 void
1520 GrDrag::select_prev ()
1522     if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1523         if (draggers)
1524             setSelected ((GrDragger *) g_list_last (draggers)->data);
1525     } else {
1526         setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
1527     }
1531 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1532 void
1533 GrDrag::deleteSelected (bool just_one)
1535     if (!selected) return;
1537     SPDocument *document = false;
1539     struct StructStopInfo {
1540         SPStop * spstop;
1541         GrDraggable * draggable;
1542         SPGradient * gradient;
1543         SPGradient * vector;
1544     };
1546     GSList *midstoplist = NULL;  // list of stops that must be deleted (will be deleted first)
1547     GSList *endstoplist = NULL;  // list of stops that must be deleted
1548     while (selected) {
1549         GrDragger *dragger = (GrDragger*) selected->data;
1550         for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1551             GrDraggable *draggable = (GrDraggable*) drgble->data;
1552             SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1553             SPGradient *vector   = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1555             switch (draggable->point_type) {
1556                 case POINT_LG_MID:
1557                 case POINT_RG_MID1:
1558                 case POINT_RG_MID2:
1559                     {
1560                         SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1561                         // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1562                         bool present = false;
1563                         for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1564                             if ( (SPStop*)l->data == stop ) {
1565                                 present = true;
1566                                 break; // no need to search further.
1567                             }
1568                         }
1569                         if (!present)
1570                             midstoplist = g_slist_append(midstoplist, stop);
1571                     }
1572                     break;
1573                 case POINT_LG_BEGIN:
1574                 case POINT_LG_END:
1575                 case POINT_RG_CENTER:
1576                 case POINT_RG_R1:
1577                 case POINT_RG_R2:
1578                     {
1579                         SPStop *stop = NULL;
1580                         if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1581                             stop = sp_first_stop(vector);
1582                         } else {
1583                             stop = sp_last_stop(vector);
1584                         }
1585                         if (stop) {
1586                             StructStopInfo *stopinfo = new StructStopInfo;
1587                             stopinfo->spstop = stop;
1588                             stopinfo->draggable = draggable;
1589                             stopinfo->gradient = gradient;
1590                             stopinfo->vector = vector;
1591                             // check if already present in list. (e.g. when both R1 and R2 were selected)
1592                             bool present = false;
1593                             for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1594                                 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1595                                     present = true;
1596                                     break; // no need to search further.
1597                                 }
1598                             }
1599                             if (!present)
1600                                 endstoplist = g_slist_append(endstoplist, stopinfo);
1601                         }
1602                     }
1603                     break;
1604                 default:
1605                     break;
1606             }
1607         }
1608         selected = g_list_remove(selected, dragger);
1609         if ( just_one ) break; // iterate once if just_one is set.
1610     }
1611     while (midstoplist) {
1612         SPStop *stop = (SPStop*) midstoplist->data;
1613         document = SP_OBJECT_DOCUMENT (stop);
1614         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1615         parent->removeChild(SP_OBJECT_REPR(stop));
1616         midstoplist = g_slist_remove(midstoplist, stop);
1617     }
1618     while (endstoplist) {
1619         StructStopInfo *stopinfo  = (StructStopInfo*) endstoplist->data;
1620         document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1622         // 2 is the minimum, cannot delete more than that without deleting the whole vector
1623         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1624         // manually count the children, don't know if there already exists a function for this...
1625         int len = 0;
1626         for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1627               child != NULL ;
1628               child = SP_OBJECT_NEXT(child) )
1629         {
1630             if ( SP_IS_STOP(child) )  len ++;
1631         }
1632         if (len > 2)
1633         {
1634             switch (stopinfo->draggable->point_type) {
1635                 case POINT_LG_BEGIN:
1636                     {
1637                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1639                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1640                         NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1641                         NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1642                         SPStop *stop = sp_first_stop(stopinfo->vector);
1643                         gdouble offset = stop->offset;
1644                         NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1645                         lg->x1.computed = newbegin[NR::X];
1646                         lg->y1.computed = newbegin[NR::Y];
1648                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1649                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1650                         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1651                         stop->offset = 0;
1652                         sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1654                         // iterate through midstops to set new offset values such that they won't move on canvas.
1655                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1656                         stop = sp_next_stop(stop);
1657                         while ( stop != laststop ) {
1658                             stop->offset = (stop->offset - offset)/(1 - offset);
1659                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1660                             stop = sp_next_stop(stop);
1661                         }
1662                     }
1663                     break;
1664                 case POINT_LG_END:
1665                     {
1666                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1668                         SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1669                         NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1670                         NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1671                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1672                         gdouble offset = laststop->offset;
1673                         NR::Point newend = begin + offset * (oldend - begin);
1674                         lg->x2.computed = newend[NR::X];
1675                         lg->y2.computed = newend[NR::Y];
1677                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1678                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1679                         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1680                         laststop->offset = 1;
1681                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1683                         // iterate through midstops to set new offset values such that they won't move on canvas.
1684                         SPStop *stop = sp_first_stop(stopinfo->vector);
1685                         stop = sp_next_stop(stop);
1686                         while ( stop != laststop ) {
1687                             stop->offset = stop->offset / offset;
1688                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1689                             stop = sp_next_stop(stop);
1690                         }
1691                     }
1692                     break;
1693                 case POINT_RG_CENTER:
1694                     {
1695                         SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1696                         if (newfirst) {
1697                             newfirst->offset = 0;
1698                             sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1699                         }
1700                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1701                     }
1702                     break;
1703                 case POINT_RG_R1:
1704                 case POINT_RG_R2:
1705                         SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1707                         SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1708                         double oldradius = rg->r.computed;
1709                         SPStop *laststop = sp_last_stop(stopinfo->vector);
1710                         gdouble offset = laststop->offset;
1711                         double newradius = offset * oldradius;
1712                         rg->r.computed = newradius;
1714                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1715                         sp_repr_set_svg_double(repr, "r", rg->r.computed);
1716                         laststop->offset = 1;
1717                         sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1719                         // iterate through midstops to set new offset values such that they won't move on canvas.
1720                         SPStop *stop = sp_first_stop(stopinfo->vector);
1721                         stop = sp_next_stop(stop);
1722                         while ( stop != laststop ) {
1723                             stop->offset = stop->offset / offset;
1724                             sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1725                             stop = sp_next_stop(stop);
1726                         }
1727                         break;
1728             }
1729         }
1730         else
1731         { // delete the gradient from the object. set fill to unset  FIXME: set to fill of unselected node?
1732             SPCSSAttr *css = sp_repr_css_attr_new ();
1733             if (stopinfo->draggable->fill_or_stroke) {
1734                 sp_repr_css_unset_property (css, "fill");
1735             } else {
1736                 sp_repr_css_unset_property (css, "stroke");
1737             }
1738             sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1739             sp_repr_css_attr_unref (css);
1740         }
1742         endstoplist = g_slist_remove(endstoplist, stopinfo);
1743         delete stopinfo;
1744     }
1746     if (document) {
1747         sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1748     }
1752 /*
1753   Local Variables:
1754   mode:c++
1755   c-file-style:"stroustrup"
1756   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1757   indent-tabs-mode:nil
1758   fill-column:99
1759   End:
1760 */
1761 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :