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 ();
105 }
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)
114 {
115 GrDrag *drag = (GrDrag *) data;
117 if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
118 return QUERY_STYLE_NOTHING;
119 }
121 if (!drag->selected) {
122 return QUERY_STYLE_NOTHING;
123 } else {
124 int ret = QUERY_STYLE_NOTHING;
126 float cf[4];
127 cf[0] = cf[1] = cf[2] = cf[3] = 0;
129 int count = 0;
131 for (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger
132 GrDraggable *draggable = (GrDraggable *) i->data;
134 if (ret == QUERY_STYLE_NOTHING) {
135 ret = QUERY_STYLE_SINGLE;
136 } else if (ret == QUERY_STYLE_SINGLE) {
137 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
138 }
140 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
141 cf[0] += SP_RGBA32_R_F (c);
142 cf[1] += SP_RGBA32_G_F (c);
143 cf[2] += SP_RGBA32_B_F (c);
144 cf[3] += SP_RGBA32_A_F (c);
146 count ++;
147 }
149 if (count) {
150 cf[0] /= count;
151 cf[1] /= count;
152 cf[2] /= count;
153 cf[3] /= count;
155 // set both fill and stroke with our stop-color and stop-opacity
156 style->fill.clear();
157 style->fill.setColor( cf[0], cf[1], cf[2] );
158 style->fill.set = TRUE;
159 style->stroke.clear();
160 style->stroke.setColor( cf[0], cf[1], cf[2] );
161 style->stroke.set = TRUE;
163 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
164 style->fill_opacity.set = TRUE;
165 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
166 style->stroke_opacity.set = TRUE;
168 style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
169 style->opacity.set = TRUE;
170 }
172 return ret;
173 }
174 }
176 bool
177 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
178 {
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;
245 }
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 }
293 }
295 GrDrag::~GrDrag()
296 {
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;
328 }
330 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
331 {
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));
338 }
340 GrDraggable::~GrDraggable ()
341 {
342 g_object_unref (G_OBJECT (this->item));
343 }
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)
348 {
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);
357 }
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)
362 {
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);
374 }
376 static void
377 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
378 {
379 GrDragger *dragger = (GrDragger *) data;
380 GrDrag *drag = dragger->parent;
382 NR::Point p = *ppointer;
384 // FIXME: take from prefs
385 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
387 if (state & GDK_SHIFT_MASK) {
388 // with Shift; unsnap if we carry more than one draggable
389 if (dragger->draggables && dragger->draggables->next) {
390 // create a new dragger
391 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
392 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
393 // relink to it all but the first draggable in the list
394 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
395 GrDraggable *draggable = (GrDraggable *) i->data;
396 dr_new->addDraggable (draggable);
397 }
398 dr_new->updateKnotShape();
399 g_slist_free (dragger->draggables->next);
400 dragger->draggables->next = NULL;
401 dragger->updateKnotShape();
402 dragger->updateTip();
403 }
404 } else if (!(state & GDK_CONTROL_MASK)) {
405 // without Shift or Ctrl; see if we need to snap to another dragger
406 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
407 GrDragger *d_new = (GrDragger *) di->data;
408 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
410 // Merge draggers:
411 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
412 GrDraggable *draggable = (GrDraggable *) i->data;
413 // copy draggable to d_new:
414 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
415 d_new->addDraggable (da_new);
416 }
418 // unlink and delete this dragger
419 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
420 delete dragger;
422 // update the new merged dragger
423 d_new->fireDraggables(true, false, true);
424 d_new->parent->updateLines();
425 d_new->parent->setSelected (d_new);
426 d_new->updateKnotShape ();
427 d_new->updateTip ();
428 d_new->updateDependencies(true);
429 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
430 _("Merge gradient handles"));
431 return;
432 }
433 }
434 }
436 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
437 // Try snapping to the grid or guides
438 SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
439 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
440 if (s.getDistance() < 1e6) {
441 p = s.getPoint();
442 sp_knot_moveto (knot, &p);
443 } else {
444 // No snapping so far, let's see if we need to snap to any of the levels
445 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
446 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
447 p[NR::Y] = dragger->parent->hor_levels[i];
448 sp_knot_moveto (knot, &p);
449 }
450 }
451 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
452 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
453 p[NR::X] = dragger->parent->vert_levels[i];
454 sp_knot_moveto (knot, &p);
455 }
456 }
457 }
458 }
460 if (state & GDK_CONTROL_MASK) {
461 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
462 /* 0 means no snapping. */
464 // This list will store snap vectors from all draggables of dragger
465 GSList *snap_vectors = NULL;
467 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
468 GrDraggable *draggable = (GrDraggable *) i->data;
470 NR::Point *dr_snap = NULL;
472 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
473 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
474 GrDragger *d_new = (GrDragger *) di->data;
475 if (d_new == dragger)
476 continue;
477 if (d_new->isA (draggable->item,
478 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
479 draggable->point_i,
480 draggable->fill_or_stroke)) {
481 // found the other end of the linear gradient;
482 if (state & GDK_SHIFT_MASK) {
483 // moving linear around center
484 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
485 dr_snap = ¢er;
486 } else {
487 // moving linear around the other end
488 dr_snap = &d_new->point;
489 }
490 }
491 }
492 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
493 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
494 GrDragger *d_new = (GrDragger *) di->data;
495 if (d_new == dragger)
496 continue;
497 if (d_new->isA (draggable->item,
498 POINT_RG_CENTER,
499 draggable->point_i,
500 draggable->fill_or_stroke)) {
501 // found the center of the radial gradient;
502 dr_snap = &(d_new->point);
503 }
504 }
505 } else if (draggable->point_type == POINT_RG_CENTER) {
506 // radial center snaps to hor/vert relative to its original position
507 dr_snap = &(dragger->point_original);
508 }
510 NR::Point *snap_vector = NULL;
511 if (dr_snap) {
512 if (state & GDK_MOD1_MASK) {
513 // with Alt, snap to the original angle and its perpendiculars
514 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
515 } else {
516 // with Ctrl, snap to M_PI/snaps
517 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
518 }
519 }
520 if (snap_vector) {
521 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
522 }
523 }
525 // Move by the smallest of snap vectors:
526 NR::Point move(9999, 9999);
527 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
528 NR::Point *snap_vector = (NR::Point *) i->data;
529 if (NR::L2(*snap_vector) < NR::L2(move))
530 move = *snap_vector;
531 }
532 if (move[NR::X] < 9999) {
533 p += move;
534 sp_knot_moveto (knot, &p);
535 }
537 g_slist_free(snap_vectors);
538 }
540 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
541 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
543 if (drag->keep_selection) {
544 NR::Point diff = p - dragger->point;
545 drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
546 } else {
547 dragger->point = p;
548 dragger->fireDraggables (false, scale_radial);
549 dragger->updateDependencies(false);
550 }
552 }
555 /**
556 Called when a midpoint knot is dragged.
557 */
558 static void
559 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
560 {
561 GrDragger *dragger = (GrDragger *) data;
562 GrDrag *drag = dragger->parent;
563 // a midpoint dragger can (logically) only contain one GrDraggable
564 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
566 // FIXME: take from prefs
567 double snap_fraction = 0.1;
569 NR::Point p = *ppointer;
570 NR::Point begin(0,0), end(0,0);
572 SPObject *server;
573 if (draggable->fill_or_stroke)
574 server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
575 else
576 server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
579 // get begin and end points between which dragging is allowed:
580 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
581 GSList *moving = NULL;
582 moving = g_slist_append(moving, dragger);
584 guint lowest_i = draggable->point_i;
585 guint highest_i = draggable->point_i;
586 GrDragger *lowest_dragger = dragger;
587 GrDragger *highest_dragger = dragger;
588 bool is_selected = g_list_find(drag->selected, dragger);
589 if (is_selected) {
590 GrDragger* d_add;
591 while ( true )
592 {
593 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
594 if ( d_add && g_list_find(drag->selected, d_add) ) {
595 lowest_i = lowest_i - 1;
596 moving = g_slist_prepend(moving, d_add);
597 lowest_dragger = d_add;
598 } else {
599 break;
600 }
601 }
603 while ( true )
604 {
605 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
606 if ( d_add && g_list_find(drag->selected, d_add) ) {
607 highest_i = highest_i + 1;
608 moving = g_slist_append(moving, d_add);
609 highest_dragger = d_add;
610 } else {
611 break;
612 }
613 }
614 }
616 if ( SP_IS_LINEARGRADIENT(server) ) {
617 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
618 GrDragger *d_temp;
619 if (lowest_i == 1) {
620 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
621 } else {
622 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
623 }
624 if (d_temp) begin = d_temp->point;
626 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
627 if (d_temp == NULL) {
628 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
629 }
630 if (d_temp) end = d_temp->point;
631 } else if ( SP_IS_RADIALGRADIENT(server) ) {
632 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
633 GrDragger *d_temp;
634 if (lowest_i == 1) {
635 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
636 } else {
637 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
638 }
639 if (d_temp) begin = d_temp->point;
641 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
642 if (d_temp == NULL) {
643 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
644 }
645 if (d_temp) end = d_temp->point;
646 }
648 NR::Point low_lim = dragger->point - (lowest_dragger->point - begin);
649 NR::Point high_lim = dragger->point - (highest_dragger->point - end);
651 if (state & GDK_CONTROL_MASK) {
652 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
653 } else {
654 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
655 }
656 NR::Point displacement = p - dragger->point;
658 for (GSList const* i = moving; i != NULL; i = i->next) {
659 GrDragger *drg = (GrDragger*) i->data;
660 SPKnot *drgknot = drg->knot;
661 drg->point += displacement;
662 sp_knot_moveto (drgknot, & drg->point);
663 drg->fireDraggables (false);
664 drg->updateDependencies(false);
665 }
667 drag->keep_selection = is_selected;
668 }
672 static void
673 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
674 {
675 GrDragger *dragger = (GrDragger *) data;
677 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
678 }
680 /**
681 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
682 */
683 static void
684 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
685 {
686 GrDragger *dragger = (GrDragger *) data;
688 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
690 dragger->point_original = dragger->point = knot->pos;
692 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
693 dragger->fireDraggables (true, true);
694 } else {
695 dragger->fireDraggables (true);
696 }
698 // make this dragger selected
699 if (!dragger->parent->keep_selection) {
700 dragger->parent->setSelected (dragger);
701 }
702 dragger->parent->keep_selection = false;
704 dragger->updateDependencies(true);
706 // we did an undoable action
707 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
708 _("Move gradient handle"));
709 }
711 /**
712 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
713 state of the keyboard keys
714 */
715 static void
716 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
717 {
718 GrDragger *dragger = (GrDragger *) data;
719 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
720 if (!draggable) return;
722 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
723 // delete this knot from vector
724 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
725 gradient = sp_gradient_get_vector (gradient, false);
726 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
727 SPStop *stop = NULL;
728 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
729 case POINT_LG_BEGIN:
730 case POINT_RG_CENTER:
731 stop = sp_first_stop(gradient);
732 {
733 SPStop *next = sp_next_stop (stop);
734 if (next) {
735 next->offset = 0;
736 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
737 }
738 }
739 break;
740 case POINT_LG_END:
741 case POINT_RG_R1:
742 case POINT_RG_R2:
743 stop = sp_last_stop(gradient);
744 {
745 SPStop *prev = sp_prev_stop (stop, gradient);
746 if (prev) {
747 prev->offset = 1;
748 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
749 }
750 }
751 break;
752 case POINT_LG_MID:
753 case POINT_RG_MID1:
754 case POINT_RG_MID2:
755 stop = sp_get_stop_i(gradient, draggable->point_i);
756 break;
757 }
759 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
760 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
761 _("Delete gradient stop"));
762 }
763 } else {
764 // select the dragger
765 dragger->point_original = dragger->point;
767 if ( state & GDK_SHIFT_MASK ) {
768 dragger->parent->setSelected (dragger, true, false);
769 } else {
770 dragger->parent->setSelected (dragger);
771 }
772 }
773 }
775 /**
776 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
777 */
778 static void
779 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
780 {
781 GrDragger *dragger = (GrDragger *) data;
783 dragger->point_original = dragger->point;
785 if (dragger->draggables == NULL)
786 return;
788 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
789 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
790 }
792 /**
793 Act upon all draggables of the dragger, setting them to the dragger's point
794 */
795 void
796 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
797 {
798 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
799 GrDraggable *draggable = (GrDraggable *) i->data;
801 // set local_change flag so that selection_changed callback does not regenerate draggers
802 this->parent->local_change = true;
804 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
805 // to the center, unless it's the first update upon merge when we must snap it to the point
806 if (merging_focus ||
807 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
808 {
809 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
810 }
811 }
812 }
814 /**
815 Checks if the dragger has a draggable with this point_type
816 */
817 bool
818 GrDragger::isA (gint point_type)
819 {
820 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
821 GrDraggable *draggable = (GrDraggable *) i->data;
822 if (draggable->point_type == point_type) {
823 return true;
824 }
825 }
826 return false;
827 }
829 /**
830 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
831 */
832 bool
833 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
834 {
835 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
836 GrDraggable *draggable = (GrDraggable *) i->data;
837 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
838 return true;
839 }
840 }
841 return false;
842 }
844 bool
845 GrDraggable::mayMerge (GrDraggable *da2)
846 {
847 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
848 // we must not merge the points of the same gradient!
849 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
850 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
851 // except that we can snap center and focus together
852 return false;
853 }
854 }
855 // disable merging of midpoints.
856 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
857 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
858 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
859 return false;
861 return true;
862 }
864 bool
865 GrDragger::mayMerge (GrDragger *other)
866 {
867 if (this == other)
868 return false;
870 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
871 GrDraggable *da1 = (GrDraggable *) i->data;
872 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
873 GrDraggable *da2 = (GrDraggable *) j->data;
874 if (!da1->mayMerge(da2))
875 return false;
876 }
877 }
878 return true;
879 }
881 bool
882 GrDragger::mayMerge (GrDraggable *da2)
883 {
884 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
885 GrDraggable *da1 = (GrDraggable *) i->data;
886 if (!da1->mayMerge(da2))
887 return false;
888 }
889 return true;
890 }
892 /**
893 Updates the statusbar tip of the dragger knot, based on its draggables
894 */
895 void
896 GrDragger::updateTip ()
897 {
898 if (this->knot && this->knot->tip) {
899 g_free (this->knot->tip);
900 this->knot->tip = NULL;
901 }
903 if (g_slist_length (this->draggables) == 1) {
904 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
905 char *item_desc = sp_item_description(draggable->item);
906 switch (draggable->point_type) {
907 case POINT_LG_MID:
908 case POINT_RG_MID1:
909 case POINT_RG_MID2:
910 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"),
911 _(gr_knot_descr[draggable->point_type]),
912 draggable->point_i,
913 item_desc,
914 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
915 break;
917 default:
918 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"),
919 _(gr_knot_descr[draggable->point_type]),
920 item_desc,
921 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
922 break;
923 }
924 g_free(item_desc);
925 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
926 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
927 } else {
928 int length = g_slist_length (this->draggables);
929 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
930 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
931 length),
932 length);
933 }
934 }
936 /**
937 Adds a draggable to the dragger
938 */
939 void
940 GrDragger::updateKnotShape ()
941 {
942 if (!draggables)
943 return;
944 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
945 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
946 }
948 /**
949 Adds a draggable to the dragger
950 */
951 void
952 GrDragger::addDraggable (GrDraggable *draggable)
953 {
954 this->draggables = g_slist_prepend (this->draggables, draggable);
956 this->updateTip();
957 }
960 /**
961 Moves this dragger to the point of the given draggable, acting upon all other draggables
962 */
963 void
964 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
965 {
966 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
967 this->point_original = this->point;
969 sp_knot_moveto (this->knot, &(this->point));
971 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
972 GrDraggable *da = (GrDraggable *) i->data;
973 if ( (da->item == item) &&
974 (point_type == -1 || da->point_type == point_type) &&
975 (point_i == -1 || da->point_i == point_i) &&
976 (da->fill_or_stroke == fill_or_stroke) ) {
977 continue;
978 }
979 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
980 }
981 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
982 }
985 /**
986 Moves all midstop draggables that depend on this one
987 */
988 void
989 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
990 SPObject *server;
991 if (draggable->fill_or_stroke)
992 server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
993 else
994 server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
995 guint num = SP_GRADIENT(server)->vector.stops.size();
996 if (num <= 2) return;
998 if ( SP_IS_LINEARGRADIENT(server) ) {
999 for ( guint i = 1; i < num - 1; i++ ) {
1000 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1001 }
1002 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1003 for ( guint i = 1; i < num - 1; i++ ) {
1004 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1005 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1006 }
1007 }
1008 }
1011 /**
1012 Moves all draggables that depend on this one
1013 */
1014 void
1015 GrDragger::updateDependencies (bool write_repr)
1016 {
1017 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1018 GrDraggable *draggable = (GrDraggable *) i->data;
1019 switch (draggable->point_type) {
1020 case POINT_LG_BEGIN:
1021 {
1022 // the end point is dependent only when dragging with ctrl+shift
1023 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1025 this->updateMidstopDependencies (draggable, write_repr);
1026 }
1027 break;
1028 case POINT_LG_END:
1029 {
1030 // the begin point is dependent only when dragging with ctrl+shift
1031 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1033 this->updateMidstopDependencies (draggable, write_repr);
1034 }
1035 break;
1036 case POINT_LG_MID:
1037 // no other nodes depend on mid points.
1038 break;
1039 case POINT_RG_R2:
1040 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1041 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1042 this->updateMidstopDependencies (draggable, write_repr);
1043 break;
1044 case POINT_RG_R1:
1045 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1046 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1047 this->updateMidstopDependencies (draggable, write_repr);
1048 break;
1049 case POINT_RG_CENTER:
1050 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1051 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1052 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1053 this->updateMidstopDependencies (draggable, write_repr);
1054 break;
1055 case POINT_RG_FOCUS:
1056 // nothing can depend on that
1057 break;
1058 case POINT_RG_MID1:
1059 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1060 break;
1061 case POINT_RG_MID2:
1062 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1063 break;
1064 default:
1065 break;
1066 }
1067 }
1068 }
1072 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1073 {
1074 this->draggables = NULL;
1076 this->parent = parent;
1078 this->point = p;
1079 this->point_original = p;
1081 // create the knot
1082 this->knot = sp_knot_new (parent->desktop, NULL);
1083 this->knot->setMode(SP_KNOT_MODE_XOR);
1084 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1085 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1086 sp_knot_update_ctrl(this->knot);
1088 // move knot to the given point
1089 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1090 sp_knot_show (this->knot);
1092 // connect knot's signals
1093 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1094 // luckily, midstops never snap to other nodes so are never unsnapped...
1095 && ( (draggable->point_type == POINT_LG_MID)
1096 || (draggable->point_type == POINT_RG_MID1)
1097 || (draggable->point_type == POINT_RG_MID2) ) )
1098 {
1099 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1100 } else {
1101 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1102 }
1103 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1104 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1105 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1106 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1108 // add the initial draggable
1109 if (draggable)
1110 this->addDraggable (draggable);
1111 updateKnotShape();
1112 }
1114 GrDragger::~GrDragger ()
1115 {
1116 // unselect if it was selected
1117 this->parent->setDeselected(this);
1119 // disconnect signals
1120 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1121 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1122 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1123 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1124 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1126 /* unref should call destroy */
1127 g_object_unref (G_OBJECT (this->knot));
1129 // delete all draggables
1130 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1131 delete ((GrDraggable *) i->data);
1132 }
1133 g_slist_free (this->draggables);
1134 this->draggables = NULL;
1135 }
1137 /**
1138 Select the dragger which has the given draggable.
1139 */
1140 GrDragger *
1141 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1142 {
1143 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1144 GrDragger *dragger = (GrDragger *) i->data;
1145 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1146 GrDraggable *da2 = (GrDraggable *) j->data;
1147 if ( (da2->item == item) &&
1148 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1149 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1150 (da2->fill_or_stroke == fill_or_stroke)) {
1151 return (dragger);
1152 }
1153 }
1154 }
1155 return NULL;
1156 }
1159 void
1160 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1161 {
1162 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1163 if (d && d != this) {
1164 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1165 }
1166 }
1169 /**
1170 Draw this dragger as selected
1171 */
1172 void
1173 GrDragger::select()
1174 {
1175 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1176 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1177 }
1179 /**
1180 Draw this dragger as normal (deselected)
1181 */
1182 void
1183 GrDragger::deselect()
1184 {
1185 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1186 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1187 }
1189 bool
1190 GrDragger::isSelected()
1191 {
1192 return g_list_find (parent->selected, this);
1193 }
1195 /**
1196 \brief Deselect all stops/draggers (private)
1197 */
1198 void
1199 GrDrag::deselect_all()
1200 {
1201 while (selected) {
1202 ( (GrDragger*) selected->data)->deselect();
1203 selected = g_list_remove(selected, selected->data);
1204 }
1205 }
1207 /**
1208 \brief Deselect all stops/draggers (public; emits signal)
1209 */
1210 void
1211 GrDrag::deselectAll()
1212 {
1213 deselect_all();
1214 this->desktop->emitToolSubselectionChanged(NULL);
1215 }
1217 /**
1218 \brief Select all stops/draggers
1219 */
1220 void
1221 GrDrag::selectAll()
1222 {
1223 for (GList *l = this->draggers; l != NULL; l = l->next) {
1224 GrDragger *d = ((GrDragger *) l->data);
1225 setSelected (d, true, true);
1226 }
1227 }
1229 /**
1230 \brief Select all stops/draggers that match the coords
1231 */
1232 void
1233 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1234 {
1235 for (GList *l = this->draggers; l != NULL; l = l->next) {
1236 GrDragger *d = ((GrDragger *) l->data);
1237 for (guint k = 0; k < coords.size(); k++) {
1238 if (NR::L2 (d->point - coords[k]) < 1e-4) {
1239 setSelected (d, true, true);
1240 }
1241 }
1242 }
1243 }
1245 /**
1246 \brief Select a dragger
1247 \param dragger The dragger to select
1248 \param add_to_selection If true, add to selection, otherwise deselect others
1249 \param override If true, always select this node, otherwise toggle selected status
1250 */
1251 void
1252 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1253 {
1254 GrDragger *seldragger = NULL;
1256 if (add_to_selection) {
1257 if (!dragger) return;
1258 if (override) {
1259 if (!g_list_find(selected, dragger)) {
1260 selected = g_list_prepend(selected, dragger);
1261 }
1262 dragger->select();
1263 seldragger = dragger;
1264 } else { // toggle
1265 if (g_list_find(selected, dragger)) {
1266 selected = g_list_remove(selected, dragger);
1267 dragger->deselect();
1268 if (selected) {
1269 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1270 }
1271 } else {
1272 selected = g_list_prepend(selected, dragger);
1273 dragger->select();
1274 seldragger = dragger;
1275 }
1276 }
1277 } else {
1278 deselect_all();
1279 if (dragger) {
1280 selected = g_list_prepend(selected, dragger);
1281 dragger->select();
1282 seldragger = dragger;
1283 }
1284 }
1285 if (seldragger) {
1286 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1287 }
1288 }
1290 /**
1291 \brief Deselect a dragger
1292 \param dragger The dragger to deselect
1293 */
1294 void
1295 GrDrag::setDeselected (GrDragger *dragger)
1296 {
1297 if (g_list_find(selected, dragger)) {
1298 selected = g_list_remove(selected, dragger);
1299 dragger->deselect();
1300 }
1301 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1302 }
1306 /**
1307 Create a line from p1 to p2 and add it to the lines list
1308 */
1309 void
1310 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1311 {
1312 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1313 SP_TYPE_CTRLLINE, NULL);
1314 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1315 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1316 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1317 sp_canvas_item_show (line);
1318 this->lines = g_slist_append (this->lines, line);
1319 }
1321 /**
1322 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1323 new dragger and add it to draggers list
1324 */
1325 void
1326 GrDrag::addDragger (GrDraggable *draggable)
1327 {
1328 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1330 for (GList *i = this->draggers; i != NULL; i = i->next) {
1331 GrDragger *dragger = (GrDragger *) i->data;
1332 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1333 // distance is small, merge this draggable into dragger, no need to create new dragger
1334 dragger->addDraggable (draggable);
1335 dragger->updateKnotShape();
1336 return;
1337 }
1338 }
1340 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1341 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1342 this->draggers = g_list_append (this->draggers, new_dragger);
1343 }
1345 /**
1346 Add draggers for the radial gradient rg on item
1347 */
1348 void
1349 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1350 {
1351 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1352 guint num = rg->vector.stops.size();
1353 if (num > 2) {
1354 for ( guint i = 1; i < num - 1; i++ ) {
1355 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1356 }
1357 }
1358 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1359 if (num > 2) {
1360 for ( guint i = 1; i < num - 1; i++ ) {
1361 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1362 }
1363 }
1364 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1365 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1366 }
1368 /**
1369 Add draggers for the linear gradient lg on item
1370 */
1371 void
1372 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1373 {
1374 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1375 guint num = lg->vector.stops.size();
1376 if (num > 2) {
1377 for ( guint i = 1; i < num - 1; i++ ) {
1378 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1379 }
1380 }
1381 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1382 }
1384 /**
1385 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1386 */
1387 void
1388 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1389 {
1390 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1391 if (dragger) {
1392 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1393 }
1394 }
1396 /**
1397 Regenerates the draggers list from the current selection; is called when selection is changed or
1398 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1399 */
1400 void
1401 GrDrag::updateDraggers ()
1402 {
1403 while (selected) {
1404 selected = g_list_remove(selected, selected->data);
1405 }
1406 // delete old draggers
1407 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1408 delete ((GrDragger *) i->data);
1409 }
1410 g_list_free (this->draggers);
1411 this->draggers = NULL;
1413 g_return_if_fail (this->selection != NULL);
1415 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1417 SPItem *item = SP_ITEM(i->data);
1418 SPStyle *style = SP_OBJECT_STYLE (item);
1420 if (style && (style->fill.isPaintserver())) {
1421 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1422 if (SP_IS_LINEARGRADIENT (server)) {
1423 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1424 } else if (SP_IS_RADIALGRADIENT (server)) {
1425 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1426 }
1427 }
1429 if (style && (style->stroke.isPaintserver())) {
1430 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1431 if (SP_IS_LINEARGRADIENT (server)) {
1432 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1433 } else if (SP_IS_RADIALGRADIENT (server)) {
1434 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1435 }
1436 }
1437 }
1438 }
1440 /**
1441 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1442 lines are always in sync with the actual gradient
1443 */
1444 void
1445 GrDrag::updateLines ()
1446 {
1447 // delete old lines
1448 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1449 gtk_object_destroy( GTK_OBJECT (i->data));
1450 }
1451 g_slist_free (this->lines);
1452 this->lines = NULL;
1454 g_return_if_fail (this->selection != NULL);
1456 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1458 SPItem *item = SP_ITEM(i->data);
1460 SPStyle *style = SP_OBJECT_STYLE (item);
1462 if (style && (style->fill.isPaintserver())) {
1463 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1464 if (SP_IS_LINEARGRADIENT (server)) {
1465 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);
1466 } else if (SP_IS_RADIALGRADIENT (server)) {
1467 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1468 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1469 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1470 }
1471 }
1473 if (style && (style->stroke.isPaintserver())) {
1474 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1475 if (SP_IS_LINEARGRADIENT (server)) {
1476 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);
1477 } else if (SP_IS_RADIALGRADIENT (server)) {
1478 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1479 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1480 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1481 }
1482 }
1483 }
1484 }
1486 /**
1487 Regenerates the levels list from the current selection
1488 */
1489 void
1490 GrDrag::updateLevels ()
1491 {
1492 hor_levels.clear();
1493 vert_levels.clear();
1495 g_return_if_fail (this->selection != NULL);
1497 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1498 SPItem *item = SP_ITEM(i->data);
1499 NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1500 if (rect) {
1501 // Remember the edges of the bbox and the center axis
1502 hor_levels.push_back(rect->min()[NR::Y]);
1503 hor_levels.push_back(rect->max()[NR::Y]);
1504 hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1505 vert_levels.push_back(rect->min()[NR::X]);
1506 vert_levels.push_back(rect->max()[NR::X]);
1507 vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1508 }
1509 }
1510 }
1512 void
1513 GrDrag::selected_reverse_vector ()
1514 {
1515 if (selected == NULL)
1516 return;
1518 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1519 GrDraggable *draggable = (GrDraggable *) i->data;
1521 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1522 }
1523 }
1525 void
1526 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1527 {
1528 selected_move (x, y, false, scale_radial);
1529 }
1531 void
1532 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1533 {
1534 if (selected == NULL)
1535 return;
1537 bool did = false;
1539 for (GList *i = selected; i != NULL; i = i->next) {
1540 GrDragger *d = (GrDragger *) i->data;
1542 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1543 // if this is a an endpoint,
1545 // Moving an rg center moves its focus and radii as well.
1546 // therefore, if this is a focus or radius and if selection
1547 // contains the center as well, do not move this one
1548 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1549 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1550 bool skip_radius_with_center = false;
1551 for (GList *di = selected; di != NULL; di = di->next) {
1552 GrDragger *d_new = (GrDragger *) di->data;
1553 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1554 POINT_RG_CENTER,
1555 0,
1556 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1557 // FIXME: here we take into account only the first draggable!
1558 skip_radius_with_center = true;
1559 }
1560 }
1561 if (skip_radius_with_center)
1562 continue;
1563 }
1565 did = true;
1566 d->point += NR::Point (x, y);
1567 d->point_original = d->point;
1568 sp_knot_moveto (d->knot, &(d->point));
1570 d->fireDraggables (write_repr, scale_radial);
1572 d->updateDependencies(write_repr);
1573 }
1574 }
1576 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1577 for (GList *i = selected; i != NULL; i = i->next) {
1578 GrDragger *d = (GrDragger *) i->data;
1580 if (d->isA(POINT_LG_MID) || !d->isA(POINT_RG_MID1) || !d->isA(POINT_RG_MID2)) {
1581 }
1582 }
1583 }
1585 if (write_repr && did) {
1586 // we did an undoable action
1587 sp_document_maybe_done (sp_desktop_document (desktop), "grmove", SP_VERB_CONTEXT_GRADIENT,
1588 _("Move gradient handle"));
1589 }
1590 }
1592 void
1593 GrDrag::selected_move_screen (double x, double y)
1594 {
1595 gdouble zoom = desktop->current_zoom();
1596 gdouble zx = x / zoom;
1597 gdouble zy = y / zoom;
1599 selected_move (zx, zy);
1600 }
1602 /**
1603 Select the knot next to the last selected one and deselect all other selected.
1604 */
1605 void
1606 GrDrag::select_next ()
1607 {
1608 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1609 if (draggers)
1610 setSelected ((GrDragger *) draggers->data);
1611 } else {
1612 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
1613 }
1614 }
1616 /**
1617 Select the knot previous from the last selected one and deselect all other selected.
1618 */
1619 void
1620 GrDrag::select_prev ()
1621 {
1622 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1623 if (draggers)
1624 setSelected ((GrDragger *) g_list_last (draggers)->data);
1625 } else {
1626 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
1627 }
1628 }
1631 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1632 void
1633 GrDrag::deleteSelected (bool just_one)
1634 {
1635 if (!selected) return;
1637 SPDocument *document = false;
1639 struct StructStopInfo {
1640 SPStop * spstop;
1641 GrDraggable * draggable;
1642 SPGradient * gradient;
1643 SPGradient * vector;
1644 };
1646 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1647 GSList *endstoplist = NULL; // list of stops that must be deleted
1648 while (selected) {
1649 GrDragger *dragger = (GrDragger*) selected->data;
1650 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1651 GrDraggable *draggable = (GrDraggable*) drgble->data;
1652 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1653 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1655 switch (draggable->point_type) {
1656 case POINT_LG_MID:
1657 case POINT_RG_MID1:
1658 case POINT_RG_MID2:
1659 {
1660 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1661 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1662 bool present = false;
1663 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1664 if ( (SPStop*)l->data == stop ) {
1665 present = true;
1666 break; // no need to search further.
1667 }
1668 }
1669 if (!present)
1670 midstoplist = g_slist_append(midstoplist, stop);
1671 }
1672 break;
1673 case POINT_LG_BEGIN:
1674 case POINT_LG_END:
1675 case POINT_RG_CENTER:
1676 case POINT_RG_R1:
1677 case POINT_RG_R2:
1678 {
1679 SPStop *stop = NULL;
1680 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1681 stop = sp_first_stop(vector);
1682 } else {
1683 stop = sp_last_stop(vector);
1684 }
1685 if (stop) {
1686 StructStopInfo *stopinfo = new StructStopInfo;
1687 stopinfo->spstop = stop;
1688 stopinfo->draggable = draggable;
1689 stopinfo->gradient = gradient;
1690 stopinfo->vector = vector;
1691 // check if already present in list. (e.g. when both R1 and R2 were selected)
1692 bool present = false;
1693 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1694 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1695 present = true;
1696 break; // no need to search further.
1697 }
1698 }
1699 if (!present)
1700 endstoplist = g_slist_append(endstoplist, stopinfo);
1701 }
1702 }
1703 break;
1704 default:
1705 break;
1706 }
1707 }
1708 selected = g_list_remove(selected, dragger);
1709 if ( just_one ) break; // iterate once if just_one is set.
1710 }
1711 while (midstoplist) {
1712 SPStop *stop = (SPStop*) midstoplist->data;
1713 document = SP_OBJECT_DOCUMENT (stop);
1714 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1715 parent->removeChild(SP_OBJECT_REPR(stop));
1716 midstoplist = g_slist_remove(midstoplist, stop);
1717 }
1718 while (endstoplist) {
1719 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
1720 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1722 // 2 is the minimum, cannot delete more than that without deleting the whole vector
1723 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1724 // manually count the children, don't know if there already exists a function for this...
1725 int len = 0;
1726 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1727 child != NULL ;
1728 child = SP_OBJECT_NEXT(child) )
1729 {
1730 if ( SP_IS_STOP(child) ) len ++;
1731 }
1732 if (len > 2)
1733 {
1734 switch (stopinfo->draggable->point_type) {
1735 case POINT_LG_BEGIN:
1736 {
1737 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1739 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1740 NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1741 NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1742 SPStop *stop = sp_first_stop(stopinfo->vector);
1743 gdouble offset = stop->offset;
1744 NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1745 lg->x1.computed = newbegin[NR::X];
1746 lg->y1.computed = newbegin[NR::Y];
1748 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1749 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1750 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1751 stop->offset = 0;
1752 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1754 // iterate through midstops to set new offset values such that they won't move on canvas.
1755 SPStop *laststop = sp_last_stop(stopinfo->vector);
1756 stop = sp_next_stop(stop);
1757 while ( stop != laststop ) {
1758 stop->offset = (stop->offset - offset)/(1 - offset);
1759 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1760 stop = sp_next_stop(stop);
1761 }
1762 }
1763 break;
1764 case POINT_LG_END:
1765 {
1766 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1768 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1769 NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1770 NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1771 SPStop *laststop = sp_last_stop(stopinfo->vector);
1772 gdouble offset = laststop->offset;
1773 NR::Point newend = begin + offset * (oldend - begin);
1774 lg->x2.computed = newend[NR::X];
1775 lg->y2.computed = newend[NR::Y];
1777 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1778 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1779 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1780 laststop->offset = 1;
1781 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1783 // iterate through midstops to set new offset values such that they won't move on canvas.
1784 SPStop *stop = sp_first_stop(stopinfo->vector);
1785 stop = sp_next_stop(stop);
1786 while ( stop != laststop ) {
1787 stop->offset = stop->offset / offset;
1788 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1789 stop = sp_next_stop(stop);
1790 }
1791 }
1792 break;
1793 case POINT_RG_CENTER:
1794 {
1795 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1796 if (newfirst) {
1797 newfirst->offset = 0;
1798 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1799 }
1800 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1801 }
1802 break;
1803 case POINT_RG_R1:
1804 case POINT_RG_R2:
1805 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1807 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1808 double oldradius = rg->r.computed;
1809 SPStop *laststop = sp_last_stop(stopinfo->vector);
1810 gdouble offset = laststop->offset;
1811 double newradius = offset * oldradius;
1812 rg->r.computed = newradius;
1814 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1815 sp_repr_set_svg_double(repr, "r", rg->r.computed);
1816 laststop->offset = 1;
1817 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1819 // iterate through midstops to set new offset values such that they won't move on canvas.
1820 SPStop *stop = sp_first_stop(stopinfo->vector);
1821 stop = sp_next_stop(stop);
1822 while ( stop != laststop ) {
1823 stop->offset = stop->offset / offset;
1824 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1825 stop = sp_next_stop(stop);
1826 }
1827 break;
1828 }
1829 }
1830 else
1831 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
1832 SPCSSAttr *css = sp_repr_css_attr_new ();
1834 // stopinfo->spstop is the selected stop
1835 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
1836 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
1837 unselectedrepr = unselectedrepr->next();
1838 }
1840 if (unselectedrepr == NULL) {
1841 if (stopinfo->draggable->fill_or_stroke) {
1842 sp_repr_css_unset_property (css, "fill");
1843 } else {
1844 sp_repr_css_unset_property (css, "stroke");
1845 }
1846 } else {
1847 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
1848 if (stopinfo->draggable->fill_or_stroke) {
1849 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1850 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1851 } else {
1852 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1853 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1854 }
1855 sp_repr_css_attr_unref (stopcss);
1856 }
1858 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1859 sp_repr_css_attr_unref (css);
1860 }
1862 endstoplist = g_slist_remove(endstoplist, stopinfo);
1863 delete stopinfo;
1864 }
1866 if (document) {
1867 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1868 }
1869 }
1872 /*
1873 Local Variables:
1874 mode:c++
1875 c-file-style:"stroustrup"
1876 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1877 indent-tabs-mode:nil
1878 fill-column:99
1879 End:
1880 */
1881 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :