1 #define __GRADIENT_DRAG_C__
3 /*
4 * On-canvas gradient dragging
5 *
6 * Authors:
7 * bulia byak <buliabyak@users.sf.net>
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 *
10 * Copyright (C) 2007 Johan Engelen
11 * Copyright (C) 2005 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include <glibmm/i18n.h>
22 #include "desktop-handles.h"
23 #include "selection.h"
24 #include "desktop.h"
25 #include "desktop-style.h"
26 #include "document.h"
27 #include "display/sp-ctrlline.h"
29 #include "xml/repr.h"
30 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
34 #include "prefs-utils.h"
35 #include "sp-item.h"
36 #include "style.h"
37 #include "knot.h"
38 #include "sp-linear-gradient.h"
39 #include "sp-radial-gradient.h"
40 #include "gradient-chemistry.h"
41 #include "gradient-drag.h"
42 #include "sp-stop.h"
44 #include "snap.h"
45 #include "sp-namedview.h"
48 #define GR_KNOT_COLOR_NORMAL 0xffffff00
49 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
51 #define GR_LINE_COLOR_FILL 0x0000ff7f
52 #define GR_LINE_COLOR_STROKE 0x9999007f
54 // screen pixels between knots when they snap:
55 #define SNAP_DIST 5
57 // absolute distance between gradient points for them to become a single dragger when the drag is created:
58 #define MERGE_DIST 0.1
60 // knot shapes corresponding to GrPointType enum
61 SPKnotShapeType gr_knot_shapes [] = {
62 SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
63 SP_KNOT_SHAPE_CIRCLE, //POINT_LG_END
64 SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
65 SP_KNOT_SHAPE_SQUARE, // POINT_RG_CENTER
66 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R1
67 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R2
68 SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
69 SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
70 SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
71 };
73 const gchar *gr_knot_descr [] = {
74 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
75 N_("Linear gradient <b>end</b>"),
76 N_("Linear gradient <b>mid stop</b>"),
77 N_("Radial gradient <b>center</b>"),
78 N_("Radial gradient <b>radius</b>"),
79 N_("Radial gradient <b>radius</b>"),
80 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
81 N_("Radial gradient <b>mid stop</b>"),
82 N_("Radial gradient <b>mid stop</b>")
83 };
85 static void
86 gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
87 {
88 GrDrag *drag = (GrDrag *) data;
89 drag->updateDraggers ();
90 drag->updateLines ();
91 drag->updateLevels ();
92 }
94 static void
95 gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
96 {
97 GrDrag *drag = (GrDrag *) data;
98 if (drag->local_change) {
99 drag->local_change = false;
100 } else {
101 drag->updateDraggers ();
102 }
103 drag->updateLines ();
104 drag->updateLevels ();
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 }
346 SPObject *
347 GrDraggable::getServer ()
348 {
349 if (!item)
350 return NULL;
352 SPObject *server = NULL;
353 if (fill_or_stroke)
354 server = SP_OBJECT_STYLE_FILL_SERVER (item);
355 else
356 server = SP_OBJECT_STYLE_STROKE_SERVER (item);
358 return server;
359 }
362 // FIXME: make global function in libnr or somewhere.
363 static NR::Point *
364 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
365 {
366 double r = NR::L2 (p - o);
367 if (r < 1e-3)
368 return NULL;
369 double angle = NR::atan2 (p - o);
370 // snap angle to snaps increments, starting from initial:
371 double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
372 // calculate the new position and subtract p to get the vector:
373 return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
374 }
376 // FIXME: make global function in libnr or somewhere.
377 static NR::Point
378 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
379 {
380 double length = NR::L2(end - begin);
381 NR::Point be = (end - begin) / length;
382 double r = NR::dot(p - begin, be);
384 if (r < 0.0) return begin;
385 if (r > length) return end;
387 double snapdist = length * snap;
388 double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
390 return (begin + r_snapped * be);
391 }
393 static void
394 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
395 {
396 GrDragger *dragger = (GrDragger *) data;
397 GrDrag *drag = dragger->parent;
399 NR::Point p = *ppointer;
401 // FIXME: take from prefs
402 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
404 if (state & GDK_SHIFT_MASK) {
405 // with Shift; unsnap if we carry more than one draggable
406 if (dragger->draggables && dragger->draggables->next) {
407 // create a new dragger
408 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
409 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
410 // relink to it all but the first draggable in the list
411 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
412 GrDraggable *draggable = (GrDraggable *) i->data;
413 dr_new->addDraggable (draggable);
414 }
415 dr_new->updateKnotShape();
416 g_slist_free (dragger->draggables->next);
417 dragger->draggables->next = NULL;
418 dragger->updateKnotShape();
419 dragger->updateTip();
420 }
421 } else if (!(state & GDK_CONTROL_MASK)) {
422 // without Shift or Ctrl; see if we need to snap to another dragger
423 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
424 GrDragger *d_new = (GrDragger *) di->data;
425 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
427 // Merge draggers:
428 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
429 GrDraggable *draggable = (GrDraggable *) i->data;
430 // copy draggable to d_new:
431 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
432 d_new->addDraggable (da_new);
433 }
435 // unlink and delete this dragger
436 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
437 delete dragger;
439 // update the new merged dragger
440 d_new->fireDraggables(true, false, true);
441 d_new->parent->updateLines();
442 d_new->parent->setSelected (d_new);
443 d_new->updateKnotShape ();
444 d_new->updateTip ();
445 d_new->updateDependencies(true);
446 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
447 _("Merge gradient handles"));
448 return;
449 }
450 }
451 }
453 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
454 // Try snapping to the grid or guides
455 SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
456 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
457 if (s.getDistance() < 1e6) {
458 p = s.getPoint();
459 sp_knot_moveto (knot, &p);
460 } else {
461 // No snapping so far, let's see if we need to snap to any of the levels
462 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
463 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
464 p[NR::Y] = dragger->parent->hor_levels[i];
465 sp_knot_moveto (knot, &p);
466 }
467 }
468 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
469 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
470 p[NR::X] = dragger->parent->vert_levels[i];
471 sp_knot_moveto (knot, &p);
472 }
473 }
474 }
475 }
477 if (state & GDK_CONTROL_MASK) {
478 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
479 /* 0 means no snapping. */
481 // This list will store snap vectors from all draggables of dragger
482 GSList *snap_vectors = NULL;
484 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
485 GrDraggable *draggable = (GrDraggable *) i->data;
487 NR::Point *dr_snap = NULL;
489 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
490 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
491 GrDragger *d_new = (GrDragger *) di->data;
492 if (d_new == dragger)
493 continue;
494 if (d_new->isA (draggable->item,
495 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
496 draggable->point_i,
497 draggable->fill_or_stroke)) {
498 // found the other end of the linear gradient;
499 if (state & GDK_SHIFT_MASK) {
500 // moving linear around center
501 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
502 dr_snap = ¢er;
503 } else {
504 // moving linear around the other end
505 dr_snap = &d_new->point;
506 }
507 }
508 }
509 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
510 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
511 GrDragger *d_new = (GrDragger *) di->data;
512 if (d_new == dragger)
513 continue;
514 if (d_new->isA (draggable->item,
515 POINT_RG_CENTER,
516 draggable->point_i,
517 draggable->fill_or_stroke)) {
518 // found the center of the radial gradient;
519 dr_snap = &(d_new->point);
520 }
521 }
522 } else if (draggable->point_type == POINT_RG_CENTER) {
523 // radial center snaps to hor/vert relative to its original position
524 dr_snap = &(dragger->point_original);
525 }
527 NR::Point *snap_vector = NULL;
528 if (dr_snap) {
529 if (state & GDK_MOD1_MASK) {
530 // with Alt, snap to the original angle and its perpendiculars
531 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
532 } else {
533 // with Ctrl, snap to M_PI/snaps
534 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
535 }
536 }
537 if (snap_vector) {
538 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
539 }
540 }
542 // Move by the smallest of snap vectors:
543 NR::Point move(9999, 9999);
544 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
545 NR::Point *snap_vector = (NR::Point *) i->data;
546 if (NR::L2(*snap_vector) < NR::L2(move))
547 move = *snap_vector;
548 }
549 if (move[NR::X] < 9999) {
550 p += move;
551 sp_knot_moveto (knot, &p);
552 }
554 g_slist_free(snap_vectors);
555 }
557 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
558 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
560 if (drag->keep_selection) {
561 NR::Point diff = p - dragger->point;
562 drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
563 } else {
564 dragger->point = p;
565 dragger->fireDraggables (false, scale_radial);
566 dragger->updateDependencies(false);
567 }
569 }
572 static void
573 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
574 {
576 GrDrag *drag = dragger->parent;
577 // a midpoint dragger can (logically) only contain one GrDraggable
578 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
580 // get begin and end points between which dragging is allowed:
581 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
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 if (dragger->isSelected()) {
589 GrDragger* d_add;
590 while ( true )
591 {
592 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
593 if ( d_add && g_list_find(drag->selected, d_add) ) {
594 lowest_i = lowest_i - 1;
595 *moving = g_slist_prepend(*moving, d_add);
596 lowest_dragger = d_add;
597 } else {
598 break;
599 }
600 }
602 while ( true )
603 {
604 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
605 if ( d_add && g_list_find(drag->selected, d_add) ) {
606 highest_i = highest_i + 1;
607 *moving = g_slist_append(*moving, d_add);
608 highest_dragger = d_add;
609 } else {
610 break;
611 }
612 }
613 }
615 if ( SP_IS_LINEARGRADIENT(server) ) {
616 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
617 GrDragger *d_temp;
618 if (lowest_i == 1) {
619 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
620 } else {
621 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
622 }
623 if (d_temp)
624 *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)
631 *end = d_temp->point;
632 } else if ( SP_IS_RADIALGRADIENT(server) ) {
633 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
634 GrDragger *d_temp;
635 if (lowest_i == 1) {
636 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
637 } else {
638 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
639 }
640 if (d_temp)
641 *begin = d_temp->point;
643 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
644 if (d_temp == NULL) {
645 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
646 }
647 if (d_temp)
648 *end = d_temp->point;
649 }
651 *low_lim = dragger->point - (lowest_dragger->point - *begin);
652 *high_lim = dragger->point - (highest_dragger->point - *end);
653 }
657 /**
658 Called when a midpoint knot is dragged.
659 */
660 static void
661 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
662 {
663 GrDragger *dragger = (GrDragger *) data;
664 GrDrag *drag = dragger->parent;
665 // a midpoint dragger can (logically) only contain one GrDraggable
666 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
668 // FIXME: take from prefs
669 double snap_fraction = 0.1;
671 NR::Point p = *ppointer;
672 NR::Point begin(0,0), end(0,0);
673 NR::Point low_lim(0,0), high_lim(0,0);
675 SPObject *server = draggable->getServer();
677 GSList *moving = NULL;
678 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
680 if (state & GDK_CONTROL_MASK) {
681 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
682 } else {
683 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
684 }
685 NR::Point displacement = p - dragger->point;
687 for (GSList const* i = moving; i != NULL; i = i->next) {
688 GrDragger *drg = (GrDragger*) i->data;
689 SPKnot *drgknot = drg->knot;
690 NR::Point this_move = displacement;
691 if (state & GDK_MOD1_MASK) {
692 // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
693 double alpha = 1.5;
694 if (NR::L2(drg->point - dragger->point) + NR::L2(drg->point - begin) - 1e-3 > NR::L2(dragger->point - begin)) { // drg is on the end side from dragger
695 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
696 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
697 } else { // drg is on the begin side from dragger
698 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
699 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
700 }
701 }
702 drg->point += this_move;
703 sp_knot_moveto (drgknot, & drg->point);
704 drg->fireDraggables (false);
705 drg->updateDependencies(false);
706 }
708 g_slist_free(moving);
710 drag->keep_selection = dragger->isSelected();
711 }
715 static void
716 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
717 {
718 GrDragger *dragger = (GrDragger *) data;
720 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
721 }
723 /**
724 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
725 */
726 static void
727 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
728 {
729 GrDragger *dragger = (GrDragger *) data;
731 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
733 dragger->point_original = dragger->point = knot->pos;
735 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
736 dragger->fireDraggables (true, true);
737 } else {
738 dragger->fireDraggables (true);
739 }
741 for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
742 GrDragger *d = (GrDragger *) i->data;
743 if (d == dragger)
744 continue;
745 d->fireDraggables (true);
746 }
748 // make this dragger selected
749 if (!dragger->parent->keep_selection) {
750 dragger->parent->setSelected (dragger);
751 }
752 dragger->parent->keep_selection = false;
754 dragger->updateDependencies(true);
756 // we did an undoable action
757 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
758 _("Move gradient handle"));
759 }
761 /**
762 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
763 state of the keyboard keys
764 */
765 static void
766 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
767 {
768 GrDragger *dragger = (GrDragger *) data;
769 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
770 if (!draggable) return;
772 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
773 // delete this knot from vector
774 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
775 gradient = sp_gradient_get_vector (gradient, false);
776 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
777 SPStop *stop = NULL;
778 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
779 case POINT_LG_BEGIN:
780 case POINT_RG_CENTER:
781 stop = sp_first_stop(gradient);
782 {
783 SPStop *next = sp_next_stop (stop);
784 if (next) {
785 next->offset = 0;
786 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
787 }
788 }
789 break;
790 case POINT_LG_END:
791 case POINT_RG_R1:
792 case POINT_RG_R2:
793 stop = sp_last_stop(gradient);
794 {
795 SPStop *prev = sp_prev_stop (stop, gradient);
796 if (prev) {
797 prev->offset = 1;
798 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
799 }
800 }
801 break;
802 case POINT_LG_MID:
803 case POINT_RG_MID1:
804 case POINT_RG_MID2:
805 stop = sp_get_stop_i(gradient, draggable->point_i);
806 break;
807 }
809 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
810 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
811 _("Delete gradient stop"));
812 }
813 } else {
814 // select the dragger
815 dragger->point_original = dragger->point;
817 if ( state & GDK_SHIFT_MASK ) {
818 dragger->parent->setSelected (dragger, true, false);
819 } else {
820 dragger->parent->setSelected (dragger);
821 }
822 }
823 }
825 /**
826 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
827 */
828 static void
829 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
830 {
831 GrDragger *dragger = (GrDragger *) data;
833 dragger->point_original = dragger->point;
835 if (dragger->draggables == NULL)
836 return;
838 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
839 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
840 }
842 /**
843 Act upon all draggables of the dragger, setting them to the dragger's point
844 */
845 void
846 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
847 {
848 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
849 GrDraggable *draggable = (GrDraggable *) i->data;
851 // set local_change flag so that selection_changed callback does not regenerate draggers
852 this->parent->local_change = true;
854 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
855 // to the center, unless it's the first update upon merge when we must snap it to the point
856 if (merging_focus ||
857 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
858 {
859 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
860 }
861 }
862 }
864 /**
865 Checks if the dragger has a draggable with this point_type
866 */
867 bool
868 GrDragger::isA (gint point_type)
869 {
870 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
871 GrDraggable *draggable = (GrDraggable *) i->data;
872 if (draggable->point_type == point_type) {
873 return true;
874 }
875 }
876 return false;
877 }
879 /**
880 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
881 */
882 bool
883 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
884 {
885 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
886 GrDraggable *draggable = (GrDraggable *) i->data;
887 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
888 return true;
889 }
890 }
891 return false;
892 }
894 bool
895 GrDraggable::mayMerge (GrDraggable *da2)
896 {
897 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
898 // we must not merge the points of the same gradient!
899 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
900 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
901 // except that we can snap center and focus together
902 return false;
903 }
904 }
905 // disable merging of midpoints.
906 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
907 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
908 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
909 return false;
911 return true;
912 }
914 bool
915 GrDragger::mayMerge (GrDragger *other)
916 {
917 if (this == other)
918 return false;
920 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
921 GrDraggable *da1 = (GrDraggable *) i->data;
922 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
923 GrDraggable *da2 = (GrDraggable *) j->data;
924 if (!da1->mayMerge(da2))
925 return false;
926 }
927 }
928 return true;
929 }
931 bool
932 GrDragger::mayMerge (GrDraggable *da2)
933 {
934 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
935 GrDraggable *da1 = (GrDraggable *) i->data;
936 if (!da1->mayMerge(da2))
937 return false;
938 }
939 return true;
940 }
942 /**
943 Updates the statusbar tip of the dragger knot, based on its draggables
944 */
945 void
946 GrDragger::updateTip ()
947 {
948 if (this->knot && this->knot->tip) {
949 g_free (this->knot->tip);
950 this->knot->tip = NULL;
951 }
953 if (g_slist_length (this->draggables) == 1) {
954 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
955 char *item_desc = sp_item_description(draggable->item);
956 switch (draggable->point_type) {
957 case POINT_LG_MID:
958 case POINT_RG_MID1:
959 case POINT_RG_MID2:
960 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"),
961 _(gr_knot_descr[draggable->point_type]),
962 draggable->point_i,
963 item_desc,
964 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
965 break;
967 default:
968 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"),
969 _(gr_knot_descr[draggable->point_type]),
970 item_desc,
971 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
972 break;
973 }
974 g_free(item_desc);
975 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
976 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
977 } else {
978 int length = g_slist_length (this->draggables);
979 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
980 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
981 length),
982 length);
983 }
984 }
986 /**
987 Adds a draggable to the dragger
988 */
989 void
990 GrDragger::updateKnotShape ()
991 {
992 if (!draggables)
993 return;
994 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
995 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
996 }
998 /**
999 Adds a draggable to the dragger
1000 */
1001 void
1002 GrDragger::addDraggable (GrDraggable *draggable)
1003 {
1004 this->draggables = g_slist_prepend (this->draggables, draggable);
1006 this->updateTip();
1007 }
1010 /**
1011 Moves this dragger to the point of the given draggable, acting upon all other draggables
1012 */
1013 void
1014 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1015 {
1016 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1017 this->point_original = this->point;
1019 sp_knot_moveto (this->knot, &(this->point));
1021 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1022 GrDraggable *da = (GrDraggable *) i->data;
1023 if ( (da->item == item) &&
1024 (point_type == -1 || da->point_type == point_type) &&
1025 (point_i == -1 || da->point_i == point_i) &&
1026 (da->fill_or_stroke == fill_or_stroke) ) {
1027 continue;
1028 }
1029 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1030 }
1031 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1032 }
1035 /**
1036 Moves all midstop draggables that depend on this one
1037 */
1038 void
1039 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1040 SPObject *server = draggable->getServer();
1041 if (!server)
1042 return;
1043 guint num = SP_GRADIENT(server)->vector.stops.size();
1044 if (num <= 2) return;
1046 if ( SP_IS_LINEARGRADIENT(server) ) {
1047 for ( guint i = 1; i < num - 1; i++ ) {
1048 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1049 }
1050 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1051 for ( guint i = 1; i < num - 1; i++ ) {
1052 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1053 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1054 }
1055 }
1056 }
1059 /**
1060 Moves all draggables that depend on this one
1061 */
1062 void
1063 GrDragger::updateDependencies (bool write_repr)
1064 {
1065 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1066 GrDraggable *draggable = (GrDraggable *) i->data;
1067 switch (draggable->point_type) {
1068 case POINT_LG_BEGIN:
1069 {
1070 // the end point is dependent only when dragging with ctrl+shift
1071 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1073 this->updateMidstopDependencies (draggable, write_repr);
1074 }
1075 break;
1076 case POINT_LG_END:
1077 {
1078 // the begin point is dependent only when dragging with ctrl+shift
1079 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1081 this->updateMidstopDependencies (draggable, write_repr);
1082 }
1083 break;
1084 case POINT_LG_MID:
1085 // no other nodes depend on mid points.
1086 break;
1087 case POINT_RG_R2:
1088 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1089 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1090 this->updateMidstopDependencies (draggable, write_repr);
1091 break;
1092 case POINT_RG_R1:
1093 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1094 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1095 this->updateMidstopDependencies (draggable, write_repr);
1096 break;
1097 case POINT_RG_CENTER:
1098 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1099 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1100 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1101 this->updateMidstopDependencies (draggable, write_repr);
1102 break;
1103 case POINT_RG_FOCUS:
1104 // nothing can depend on that
1105 break;
1106 case POINT_RG_MID1:
1107 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1108 break;
1109 case POINT_RG_MID2:
1110 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1111 break;
1112 default:
1113 break;
1114 }
1115 }
1116 }
1120 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1121 {
1122 this->draggables = NULL;
1124 this->parent = parent;
1126 this->point = p;
1127 this->point_original = p;
1129 // create the knot
1130 this->knot = sp_knot_new (parent->desktop, NULL);
1131 this->knot->setMode(SP_KNOT_MODE_XOR);
1132 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1133 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1134 sp_knot_update_ctrl(this->knot);
1136 // move knot to the given point
1137 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1138 sp_knot_show (this->knot);
1140 // connect knot's signals
1141 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1142 // luckily, midstops never snap to other nodes so are never unsnapped...
1143 && ( (draggable->point_type == POINT_LG_MID)
1144 || (draggable->point_type == POINT_RG_MID1)
1145 || (draggable->point_type == POINT_RG_MID2) ) )
1146 {
1147 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1148 } else {
1149 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1150 }
1151 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1152 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1153 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1154 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1156 // add the initial draggable
1157 if (draggable)
1158 this->addDraggable (draggable);
1159 updateKnotShape();
1160 }
1162 GrDragger::~GrDragger ()
1163 {
1164 // unselect if it was selected
1165 this->parent->setDeselected(this);
1167 // disconnect signals
1168 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1169 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1170 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1171 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1172 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1174 /* unref should call destroy */
1175 g_object_unref (G_OBJECT (this->knot));
1177 // delete all draggables
1178 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1179 delete ((GrDraggable *) i->data);
1180 }
1181 g_slist_free (this->draggables);
1182 this->draggables = NULL;
1183 }
1185 /**
1186 Select the dragger which has the given draggable.
1187 */
1188 GrDragger *
1189 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1190 {
1191 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1192 GrDragger *dragger = (GrDragger *) i->data;
1193 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1194 GrDraggable *da2 = (GrDraggable *) j->data;
1195 if ( (da2->item == item) &&
1196 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1197 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1198 (da2->fill_or_stroke == fill_or_stroke)) {
1199 return (dragger);
1200 }
1201 }
1202 }
1203 return NULL;
1204 }
1207 void
1208 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1209 {
1210 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1211 if (d && d != this) {
1212 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1213 }
1214 }
1217 /**
1218 Draw this dragger as selected
1219 */
1220 void
1221 GrDragger::select()
1222 {
1223 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1224 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1225 }
1227 /**
1228 Draw this dragger as normal (deselected)
1229 */
1230 void
1231 GrDragger::deselect()
1232 {
1233 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1234 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1235 }
1237 bool
1238 GrDragger::isSelected()
1239 {
1240 return g_list_find (parent->selected, this);
1241 }
1243 /**
1244 \brief Deselect all stops/draggers (private)
1245 */
1246 void
1247 GrDrag::deselect_all()
1248 {
1249 while (selected) {
1250 ( (GrDragger*) selected->data)->deselect();
1251 selected = g_list_remove(selected, selected->data);
1252 }
1253 }
1255 /**
1256 \brief Deselect all stops/draggers (public; emits signal)
1257 */
1258 void
1259 GrDrag::deselectAll()
1260 {
1261 deselect_all();
1262 this->desktop->emitToolSubselectionChanged(NULL);
1263 }
1265 /**
1266 \brief Select all stops/draggers
1267 */
1268 void
1269 GrDrag::selectAll()
1270 {
1271 for (GList *l = this->draggers; l != NULL; l = l->next) {
1272 GrDragger *d = ((GrDragger *) l->data);
1273 setSelected (d, true, true);
1274 }
1275 }
1277 /**
1278 \brief Select all stops/draggers that match the coords
1279 */
1280 void
1281 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1282 {
1283 for (GList *l = this->draggers; l != NULL; l = l->next) {
1284 GrDragger *d = ((GrDragger *) l->data);
1285 for (guint k = 0; k < coords.size(); k++) {
1286 if (NR::L2 (d->point - coords[k]) < 1e-4) {
1287 setSelected (d, true, true);
1288 }
1289 }
1290 }
1291 }
1293 /**
1294 \brief Select a dragger
1295 \param dragger The dragger to select
1296 \param add_to_selection If true, add to selection, otherwise deselect others
1297 \param override If true, always select this node, otherwise toggle selected status
1298 */
1299 void
1300 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1301 {
1302 GrDragger *seldragger = NULL;
1304 if (add_to_selection) {
1305 if (!dragger) return;
1306 if (override) {
1307 if (!g_list_find(selected, dragger)) {
1308 selected = g_list_prepend(selected, dragger);
1309 }
1310 dragger->select();
1311 seldragger = dragger;
1312 } else { // toggle
1313 if (g_list_find(selected, dragger)) {
1314 selected = g_list_remove(selected, dragger);
1315 dragger->deselect();
1316 if (selected) {
1317 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1318 }
1319 } else {
1320 selected = g_list_prepend(selected, dragger);
1321 dragger->select();
1322 seldragger = dragger;
1323 }
1324 }
1325 } else {
1326 deselect_all();
1327 if (dragger) {
1328 selected = g_list_prepend(selected, dragger);
1329 dragger->select();
1330 seldragger = dragger;
1331 }
1332 }
1333 if (seldragger) {
1334 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1335 }
1336 }
1338 /**
1339 \brief Deselect a dragger
1340 \param dragger The dragger to deselect
1341 */
1342 void
1343 GrDrag::setDeselected (GrDragger *dragger)
1344 {
1345 if (g_list_find(selected, dragger)) {
1346 selected = g_list_remove(selected, dragger);
1347 dragger->deselect();
1348 }
1349 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1350 }
1354 /**
1355 Create a line from p1 to p2 and add it to the lines list
1356 */
1357 void
1358 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1359 {
1360 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1361 SP_TYPE_CTRLLINE, NULL);
1362 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1363 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1364 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1365 sp_canvas_item_show (line);
1366 this->lines = g_slist_append (this->lines, line);
1367 }
1369 /**
1370 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1371 new dragger and add it to draggers list
1372 */
1373 void
1374 GrDrag::addDragger (GrDraggable *draggable)
1375 {
1376 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1378 for (GList *i = this->draggers; i != NULL; i = i->next) {
1379 GrDragger *dragger = (GrDragger *) i->data;
1380 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1381 // distance is small, merge this draggable into dragger, no need to create new dragger
1382 dragger->addDraggable (draggable);
1383 dragger->updateKnotShape();
1384 return;
1385 }
1386 }
1388 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1389 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1390 this->draggers = g_list_append (this->draggers, new_dragger);
1391 }
1393 /**
1394 Add draggers for the radial gradient rg on item
1395 */
1396 void
1397 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1398 {
1399 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1400 guint num = rg->vector.stops.size();
1401 if (num > 2) {
1402 for ( guint i = 1; i < num - 1; i++ ) {
1403 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1404 }
1405 }
1406 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1407 if (num > 2) {
1408 for ( guint i = 1; i < num - 1; i++ ) {
1409 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1410 }
1411 }
1412 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1413 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1414 }
1416 /**
1417 Add draggers for the linear gradient lg on item
1418 */
1419 void
1420 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1421 {
1422 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1423 guint num = lg->vector.stops.size();
1424 if (num > 2) {
1425 for ( guint i = 1; i < num - 1; i++ ) {
1426 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1427 }
1428 }
1429 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1430 }
1432 /**
1433 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1434 */
1435 void
1436 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1437 {
1438 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1439 if (dragger) {
1440 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1441 }
1442 }
1444 /**
1445 Regenerates the draggers list from the current selection; is called when selection is changed or
1446 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1447 */
1448 void
1449 GrDrag::updateDraggers ()
1450 {
1451 while (selected) {
1452 selected = g_list_remove(selected, selected->data);
1453 }
1454 // delete old draggers
1455 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1456 delete ((GrDragger *) i->data);
1457 }
1458 g_list_free (this->draggers);
1459 this->draggers = NULL;
1461 g_return_if_fail (this->selection != NULL);
1463 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1465 SPItem *item = SP_ITEM(i->data);
1466 SPStyle *style = SP_OBJECT_STYLE (item);
1468 if (style && (style->fill.isPaintserver())) {
1469 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1470 if (SP_IS_LINEARGRADIENT (server)) {
1471 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1472 } else if (SP_IS_RADIALGRADIENT (server)) {
1473 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1474 }
1475 }
1477 if (style && (style->stroke.isPaintserver())) {
1478 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1479 if (SP_IS_LINEARGRADIENT (server)) {
1480 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1481 } else if (SP_IS_RADIALGRADIENT (server)) {
1482 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1483 }
1484 }
1485 }
1486 }
1488 /**
1489 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1490 lines are always in sync with the actual gradient
1491 */
1492 void
1493 GrDrag::updateLines ()
1494 {
1495 // delete old lines
1496 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1497 gtk_object_destroy( GTK_OBJECT (i->data));
1498 }
1499 g_slist_free (this->lines);
1500 this->lines = NULL;
1502 g_return_if_fail (this->selection != NULL);
1504 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1506 SPItem *item = SP_ITEM(i->data);
1508 SPStyle *style = SP_OBJECT_STYLE (item);
1510 if (style && (style->fill.isPaintserver())) {
1511 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1512 if (SP_IS_LINEARGRADIENT (server)) {
1513 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);
1514 } else if (SP_IS_RADIALGRADIENT (server)) {
1515 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1516 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1517 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1518 }
1519 }
1521 if (style && (style->stroke.isPaintserver())) {
1522 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1523 if (SP_IS_LINEARGRADIENT (server)) {
1524 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);
1525 } else if (SP_IS_RADIALGRADIENT (server)) {
1526 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1527 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1528 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1529 }
1530 }
1531 }
1532 }
1534 /**
1535 Regenerates the levels list from the current selection
1536 */
1537 void
1538 GrDrag::updateLevels ()
1539 {
1540 hor_levels.clear();
1541 vert_levels.clear();
1543 g_return_if_fail (this->selection != NULL);
1545 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1546 SPItem *item = SP_ITEM(i->data);
1547 NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1548 if (rect) {
1549 // Remember the edges of the bbox and the center axis
1550 hor_levels.push_back(rect->min()[NR::Y]);
1551 hor_levels.push_back(rect->max()[NR::Y]);
1552 hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1553 vert_levels.push_back(rect->min()[NR::X]);
1554 vert_levels.push_back(rect->max()[NR::X]);
1555 vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1556 }
1557 }
1558 }
1560 void
1561 GrDrag::selected_reverse_vector ()
1562 {
1563 if (selected == NULL)
1564 return;
1566 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1567 GrDraggable *draggable = (GrDraggable *) i->data;
1569 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1570 }
1571 }
1573 void
1574 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1575 {
1576 selected_move (x, y, false, scale_radial);
1577 }
1579 void
1580 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1581 {
1582 if (selected == NULL)
1583 return;
1585 bool did = false;
1587 for (GList *i = selected; i != NULL; i = i->next) {
1588 GrDragger *d = (GrDragger *) i->data;
1590 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1591 // if this is a an endpoint,
1593 // Moving an rg center moves its focus and radii as well.
1594 // therefore, if this is a focus or radius and if selection
1595 // contains the center as well, do not move this one
1596 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1597 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1598 bool skip_radius_with_center = false;
1599 for (GList *di = selected; di != NULL; di = di->next) {
1600 GrDragger *d_new = (GrDragger *) di->data;
1601 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1602 POINT_RG_CENTER,
1603 0,
1604 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1605 // FIXME: here we take into account only the first draggable!
1606 skip_radius_with_center = true;
1607 }
1608 }
1609 if (skip_radius_with_center)
1610 continue;
1611 }
1613 did = true;
1614 d->point += NR::Point (x, y);
1615 d->point_original = d->point;
1616 sp_knot_moveto (d->knot, &(d->point));
1618 d->fireDraggables (write_repr, scale_radial);
1620 d->updateDependencies(write_repr);
1621 }
1623 if (write_repr && did) {
1624 // we did an undoable action
1625 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1626 _("Move gradient handle(s)"));
1627 return;
1628 }
1630 }
1632 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1634 GrDragger *dragger = (GrDragger *) selected->data;
1635 // a midpoint dragger can (logically) only contain one GrDraggable
1636 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1638 NR::Point begin(0,0), end(0,0);
1639 NR::Point low_lim(0,0), high_lim(0,0);
1641 SPObject *server = draggable->getServer();
1642 GSList *moving = NULL;
1643 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1645 NR::Point p(x, y);
1646 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1647 NR::Point displacement = p - dragger->point;
1649 for (GSList const* i = moving; i != NULL; i = i->next) {
1650 GrDragger *drg = (GrDragger*) i->data;
1651 SPKnot *drgknot = drg->knot;
1652 drg->point += displacement;
1653 sp_knot_moveto (drgknot, & drg->point);
1654 drg->fireDraggables (true);
1655 drg->updateDependencies(true);
1656 did = true;
1657 }
1659 g_slist_free(moving);
1661 if (write_repr && did) {
1662 // we did an undoable action
1663 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1664 _("Move gradient mid stop(s)"));
1665 }
1666 }
1667 }
1669 void
1670 GrDrag::selected_move_screen (double x, double y)
1671 {
1672 gdouble zoom = desktop->current_zoom();
1673 gdouble zx = x / zoom;
1674 gdouble zy = y / zoom;
1676 selected_move (zx, zy);
1677 }
1679 /**
1680 Select the knot next to the last selected one and deselect all other selected.
1681 */
1682 void
1683 GrDrag::select_next ()
1684 {
1685 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1686 if (draggers)
1687 setSelected ((GrDragger *) draggers->data);
1688 } else {
1689 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
1690 }
1691 }
1693 /**
1694 Select the knot previous from the last selected one and deselect all other selected.
1695 */
1696 void
1697 GrDrag::select_prev ()
1698 {
1699 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1700 if (draggers)
1701 setSelected ((GrDragger *) g_list_last (draggers)->data);
1702 } else {
1703 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
1704 }
1705 }
1708 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1709 void
1710 GrDrag::deleteSelected (bool just_one)
1711 {
1712 if (!selected) return;
1714 SPDocument *document = false;
1716 struct StructStopInfo {
1717 SPStop * spstop;
1718 GrDraggable * draggable;
1719 SPGradient * gradient;
1720 SPGradient * vector;
1721 };
1723 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1724 GSList *endstoplist = NULL; // list of stops that must be deleted
1725 while (selected) {
1726 GrDragger *dragger = (GrDragger*) selected->data;
1727 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1728 GrDraggable *draggable = (GrDraggable*) drgble->data;
1729 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1730 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1732 switch (draggable->point_type) {
1733 case POINT_LG_MID:
1734 case POINT_RG_MID1:
1735 case POINT_RG_MID2:
1736 {
1737 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1738 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1739 bool present = false;
1740 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1741 if ( (SPStop*)l->data == stop ) {
1742 present = true;
1743 break; // no need to search further.
1744 }
1745 }
1746 if (!present)
1747 midstoplist = g_slist_append(midstoplist, stop);
1748 }
1749 break;
1750 case POINT_LG_BEGIN:
1751 case POINT_LG_END:
1752 case POINT_RG_CENTER:
1753 case POINT_RG_R1:
1754 case POINT_RG_R2:
1755 {
1756 SPStop *stop = NULL;
1757 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1758 stop = sp_first_stop(vector);
1759 } else {
1760 stop = sp_last_stop(vector);
1761 }
1762 if (stop) {
1763 StructStopInfo *stopinfo = new StructStopInfo;
1764 stopinfo->spstop = stop;
1765 stopinfo->draggable = draggable;
1766 stopinfo->gradient = gradient;
1767 stopinfo->vector = vector;
1768 // check if already present in list. (e.g. when both R1 and R2 were selected)
1769 bool present = false;
1770 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1771 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1772 present = true;
1773 break; // no need to search further.
1774 }
1775 }
1776 if (!present)
1777 endstoplist = g_slist_append(endstoplist, stopinfo);
1778 }
1779 }
1780 break;
1781 default:
1782 break;
1783 }
1784 }
1785 selected = g_list_remove(selected, dragger);
1786 if ( just_one ) break; // iterate once if just_one is set.
1787 }
1788 while (midstoplist) {
1789 SPStop *stop = (SPStop*) midstoplist->data;
1790 document = SP_OBJECT_DOCUMENT (stop);
1791 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1792 parent->removeChild(SP_OBJECT_REPR(stop));
1793 midstoplist = g_slist_remove(midstoplist, stop);
1794 }
1795 while (endstoplist) {
1796 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
1797 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1799 // 2 is the minimum, cannot delete more than that without deleting the whole vector
1800 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1801 // manually count the children, don't know if there already exists a function for this...
1802 int len = 0;
1803 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1804 child != NULL ;
1805 child = SP_OBJECT_NEXT(child) )
1806 {
1807 if ( SP_IS_STOP(child) ) len ++;
1808 }
1809 if (len > 2)
1810 {
1811 switch (stopinfo->draggable->point_type) {
1812 case POINT_LG_BEGIN:
1813 {
1814 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1816 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1817 NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1818 NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1819 SPStop *stop = sp_first_stop(stopinfo->vector);
1820 gdouble offset = stop->offset;
1821 NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1822 lg->x1.computed = newbegin[NR::X];
1823 lg->y1.computed = newbegin[NR::Y];
1825 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1826 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1827 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1828 stop->offset = 0;
1829 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1831 // iterate through midstops to set new offset values such that they won't move on canvas.
1832 SPStop *laststop = sp_last_stop(stopinfo->vector);
1833 stop = sp_next_stop(stop);
1834 while ( stop != laststop ) {
1835 stop->offset = (stop->offset - offset)/(1 - offset);
1836 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1837 stop = sp_next_stop(stop);
1838 }
1839 }
1840 break;
1841 case POINT_LG_END:
1842 {
1843 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1845 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1846 NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1847 NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1848 SPStop *laststop = sp_last_stop(stopinfo->vector);
1849 gdouble offset = laststop->offset;
1850 NR::Point newend = begin + offset * (oldend - begin);
1851 lg->x2.computed = newend[NR::X];
1852 lg->y2.computed = newend[NR::Y];
1854 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1855 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1856 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1857 laststop->offset = 1;
1858 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1860 // iterate through midstops to set new offset values such that they won't move on canvas.
1861 SPStop *stop = sp_first_stop(stopinfo->vector);
1862 stop = sp_next_stop(stop);
1863 while ( stop != laststop ) {
1864 stop->offset = stop->offset / offset;
1865 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1866 stop = sp_next_stop(stop);
1867 }
1868 }
1869 break;
1870 case POINT_RG_CENTER:
1871 {
1872 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1873 if (newfirst) {
1874 newfirst->offset = 0;
1875 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1876 }
1877 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1878 }
1879 break;
1880 case POINT_RG_R1:
1881 case POINT_RG_R2:
1882 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1884 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1885 double oldradius = rg->r.computed;
1886 SPStop *laststop = sp_last_stop(stopinfo->vector);
1887 gdouble offset = laststop->offset;
1888 double newradius = offset * oldradius;
1889 rg->r.computed = newradius;
1891 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1892 sp_repr_set_svg_double(repr, "r", rg->r.computed);
1893 laststop->offset = 1;
1894 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1896 // iterate through midstops to set new offset values such that they won't move on canvas.
1897 SPStop *stop = sp_first_stop(stopinfo->vector);
1898 stop = sp_next_stop(stop);
1899 while ( stop != laststop ) {
1900 stop->offset = stop->offset / offset;
1901 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1902 stop = sp_next_stop(stop);
1903 }
1904 break;
1905 }
1906 }
1907 else
1908 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
1909 SPCSSAttr *css = sp_repr_css_attr_new ();
1911 // stopinfo->spstop is the selected stop
1912 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
1913 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
1914 unselectedrepr = unselectedrepr->next();
1915 }
1917 if (unselectedrepr == NULL) {
1918 if (stopinfo->draggable->fill_or_stroke) {
1919 sp_repr_css_unset_property (css, "fill");
1920 } else {
1921 sp_repr_css_unset_property (css, "stroke");
1922 }
1923 } else {
1924 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
1925 if (stopinfo->draggable->fill_or_stroke) {
1926 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1927 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1928 } else {
1929 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1930 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1931 }
1932 sp_repr_css_attr_unref (stopcss);
1933 }
1935 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1936 sp_repr_css_attr_unref (css);
1937 }
1939 endstoplist = g_slist_remove(endstoplist, stopinfo);
1940 delete stopinfo;
1941 }
1943 if (document) {
1944 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1945 }
1946 }
1949 /*
1950 Local Variables:
1951 mode:c++
1952 c-file-style:"stroustrup"
1953 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1954 indent-tabs-mode:nil
1955 fill-column:99
1956 End:
1957 */
1958 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :