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 (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
132 GrDragger *d = (GrDragger *) i->data;
133 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
134 GrDraggable *draggable = (GrDraggable *) j->data;
136 if (ret == QUERY_STYLE_NOTHING) {
137 ret = QUERY_STYLE_SINGLE;
138 } else if (ret == QUERY_STYLE_SINGLE) {
139 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
140 }
142 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
143 cf[0] += SP_RGBA32_R_F (c);
144 cf[1] += SP_RGBA32_G_F (c);
145 cf[2] += SP_RGBA32_B_F (c);
146 cf[3] += SP_RGBA32_A_F (c);
148 count ++;
149 }
150 }
152 if (count) {
153 cf[0] /= count;
154 cf[1] /= count;
155 cf[2] /= count;
156 cf[3] /= count;
158 // set both fill and stroke with our stop-color and stop-opacity
159 style->fill.clear();
160 style->fill.setColor( cf[0], cf[1], cf[2] );
161 style->fill.set = TRUE;
162 style->stroke.clear();
163 style->stroke.setColor( cf[0], cf[1], cf[2] );
164 style->stroke.set = TRUE;
166 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
167 style->fill_opacity.set = TRUE;
168 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
169 style->stroke_opacity.set = TRUE;
171 style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
172 style->opacity.set = TRUE;
173 }
175 return ret;
176 }
177 }
179 bool
180 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
181 {
182 GrDrag *drag = (GrDrag *) data;
184 if (!drag->selected)
185 return false;
187 SPCSSAttr *stop = sp_repr_css_attr_new ();
189 // See if the css contains interesting properties, and if so, translate them into the format
190 // acceptable for gradient stops
192 // any of color properties, in order of increasing priority:
193 if (css->attribute("flood-color"))
194 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
196 if (css->attribute("lighting-color"))
197 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
199 if (css->attribute("color"))
200 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
202 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
203 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
205 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
206 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
208 if (css->attribute("stop-color"))
209 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
212 if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
213 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
214 } else { // multiply all opacity properties:
215 gdouble accumulated = 1.0;
216 accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
217 accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
218 accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
219 accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
221 Inkscape::CSSOStringStream os;
222 os << accumulated;
223 sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
225 if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
226 (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
227 sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
228 }
230 if (!stop->attributeList()) { // nothing for us here, pass it on
231 sp_repr_css_attr_unref(stop);
232 return false;
233 }
235 for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
236 GrDragger* dragger = (GrDragger*) sel->data;
237 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
238 GrDraggable *draggable = (GrDraggable *) i->data;
240 drag->local_change = true;
241 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
242 }
243 }
245 //sp_repr_css_print(stop);
246 sp_repr_css_attr_unref(stop);
247 return true;
248 }
250 GrDrag::GrDrag(SPDesktop *desktop) {
252 this->desktop = desktop;
254 this->selection = sp_desktop_selection(desktop);
256 this->draggers = NULL;
257 this->lines = NULL;
258 this->selected = NULL;
260 this->hor_levels.clear();
261 this->vert_levels.clear();
263 this->local_change = false;
265 this->sel_changed_connection = this->selection->connectChanged(
266 sigc::bind (
267 sigc::ptr_fun(&gr_drag_sel_changed),
268 (gpointer)this )
270 );
271 this->sel_modified_connection = this->selection->connectModified(
272 sigc::bind(
273 sigc::ptr_fun(&gr_drag_sel_modified),
274 (gpointer)this )
275 );
277 this->style_set_connection = this->desktop->connectSetStyle(
278 sigc::bind(
279 sigc::ptr_fun(&gr_drag_style_set),
280 (gpointer)this )
281 );
283 this->style_query_connection = this->desktop->connectQueryStyle(
284 sigc::bind(
285 sigc::ptr_fun(&gr_drag_style_query),
286 (gpointer)this )
287 );
289 this->updateDraggers ();
290 this->updateLines ();
291 this->updateLevels ();
293 if (desktop->gr_item) {
294 this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
295 }
296 }
298 GrDrag::~GrDrag()
299 {
300 this->sel_changed_connection.disconnect();
301 this->sel_modified_connection.disconnect();
302 this->style_set_connection.disconnect();
303 this->style_query_connection.disconnect();
305 if (this->selected) {
306 GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data;
307 desktop->gr_item = draggable->item;
308 desktop->gr_point_type = draggable->point_type;
309 desktop->gr_point_i = draggable->point_i;
310 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
311 } else {
312 desktop->gr_item = NULL;
313 desktop->gr_point_type = 0;
314 desktop->gr_point_i = 0;
315 desktop->gr_fill_or_stroke = true;
316 }
318 deselect_all();
319 for (GList *l = this->draggers; l != NULL; l = l->next) {
320 delete ((GrDragger *) l->data);
321 }
322 g_list_free (this->draggers);
323 this->draggers = NULL;
324 this->selected = NULL;
326 for (GSList *l = this->lines; l != NULL; l = l->next) {
327 gtk_object_destroy( GTK_OBJECT (l->data));
328 }
329 g_slist_free (this->lines);
330 this->lines = NULL;
331 }
333 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
334 {
335 this->item = item;
336 this->point_type = point_type;
337 this->point_i = point_i;
338 this->fill_or_stroke = fill_or_stroke;
340 g_object_ref (G_OBJECT (this->item));
341 }
343 GrDraggable::~GrDraggable ()
344 {
345 g_object_unref (G_OBJECT (this->item));
346 }
349 SPObject *
350 GrDraggable::getServer ()
351 {
352 if (!item)
353 return NULL;
355 SPObject *server = NULL;
356 if (fill_or_stroke)
357 server = SP_OBJECT_STYLE_FILL_SERVER (item);
358 else
359 server = SP_OBJECT_STYLE_STROKE_SERVER (item);
361 return server;
362 }
365 // FIXME: make global function in libnr or somewhere.
366 static NR::Point *
367 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
368 {
369 double r = NR::L2 (p - o);
370 if (r < 1e-3)
371 return NULL;
372 double angle = NR::atan2 (p - o);
373 // snap angle to snaps increments, starting from initial:
374 double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
375 // calculate the new position and subtract p to get the vector:
376 return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
377 }
379 // FIXME: make global function in libnr or somewhere.
380 static NR::Point
381 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
382 {
383 double length = NR::L2(end - begin);
384 NR::Point be = (end - begin) / length;
385 double r = NR::dot(p - begin, be);
387 if (r < 0.0) return begin;
388 if (r > length) return end;
390 double snapdist = length * snap;
391 double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
393 return (begin + r_snapped * be);
394 }
396 static void
397 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
398 {
399 GrDragger *dragger = (GrDragger *) data;
400 GrDrag *drag = dragger->parent;
402 NR::Point p = *ppointer;
404 // FIXME: take from prefs
405 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
407 if (state & GDK_SHIFT_MASK) {
408 // with Shift; unsnap if we carry more than one draggable
409 if (dragger->draggables && dragger->draggables->next) {
410 // create a new dragger
411 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
412 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
413 // relink to it all but the first draggable in the list
414 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
415 GrDraggable *draggable = (GrDraggable *) i->data;
416 dr_new->addDraggable (draggable);
417 }
418 dr_new->updateKnotShape();
419 g_slist_free (dragger->draggables->next);
420 dragger->draggables->next = NULL;
421 dragger->updateKnotShape();
422 dragger->updateTip();
423 }
424 } else if (!(state & GDK_CONTROL_MASK)) {
425 // without Shift or Ctrl; see if we need to snap to another dragger
426 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
427 GrDragger *d_new = (GrDragger *) di->data;
428 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
430 // Merge draggers:
431 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
432 GrDraggable *draggable = (GrDraggable *) i->data;
433 // copy draggable to d_new:
434 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
435 d_new->addDraggable (da_new);
436 }
438 // unlink and delete this dragger
439 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
440 delete dragger;
442 // update the new merged dragger
443 d_new->fireDraggables(true, false, true);
444 d_new->parent->updateLines();
445 d_new->parent->setSelected (d_new);
446 d_new->updateKnotShape ();
447 d_new->updateTip ();
448 d_new->updateDependencies(true);
449 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
450 _("Merge gradient handles"));
451 return;
452 }
453 }
454 }
456 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
457 // Try snapping to the grid or guides
458 SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
459 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
460 if (s.getDistance() < 1e6) {
461 p = s.getPoint();
462 sp_knot_moveto (knot, &p);
463 } else {
464 // No snapping so far, let's see if we need to snap to any of the levels
465 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
466 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
467 p[NR::Y] = dragger->parent->hor_levels[i];
468 sp_knot_moveto (knot, &p);
469 }
470 }
471 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
472 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
473 p[NR::X] = dragger->parent->vert_levels[i];
474 sp_knot_moveto (knot, &p);
475 }
476 }
477 }
478 }
480 if (state & GDK_CONTROL_MASK) {
481 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
482 /* 0 means no snapping. */
484 // This list will store snap vectors from all draggables of dragger
485 GSList *snap_vectors = NULL;
487 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
488 GrDraggable *draggable = (GrDraggable *) i->data;
490 NR::Point *dr_snap = NULL;
492 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
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 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
499 draggable->point_i,
500 draggable->fill_or_stroke)) {
501 // found the other end of the linear gradient;
502 if (state & GDK_SHIFT_MASK) {
503 // moving linear around center
504 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
505 dr_snap = ¢er;
506 } else {
507 // moving linear around the other end
508 dr_snap = &d_new->point;
509 }
510 }
511 }
512 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
513 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
514 GrDragger *d_new = (GrDragger *) di->data;
515 if (d_new == dragger)
516 continue;
517 if (d_new->isA (draggable->item,
518 POINT_RG_CENTER,
519 draggable->point_i,
520 draggable->fill_or_stroke)) {
521 // found the center of the radial gradient;
522 dr_snap = &(d_new->point);
523 }
524 }
525 } else if (draggable->point_type == POINT_RG_CENTER) {
526 // radial center snaps to hor/vert relative to its original position
527 dr_snap = &(dragger->point_original);
528 }
530 NR::Point *snap_vector = NULL;
531 if (dr_snap) {
532 if (state & GDK_MOD1_MASK) {
533 // with Alt, snap to the original angle and its perpendiculars
534 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
535 } else {
536 // with Ctrl, snap to M_PI/snaps
537 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
538 }
539 }
540 if (snap_vector) {
541 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
542 }
543 }
545 // Move by the smallest of snap vectors:
546 NR::Point move(9999, 9999);
547 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
548 NR::Point *snap_vector = (NR::Point *) i->data;
549 if (NR::L2(*snap_vector) < NR::L2(move))
550 move = *snap_vector;
551 }
552 if (move[NR::X] < 9999) {
553 p += move;
554 sp_knot_moveto (knot, &p);
555 }
557 g_slist_free(snap_vectors);
558 }
560 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
561 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
563 if (drag->keep_selection) {
564 NR::Point diff = p - dragger->point;
565 drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
566 } else {
567 dragger->point = p;
568 dragger->fireDraggables (false, scale_radial);
569 dragger->updateDependencies(false);
570 }
572 }
575 static void
576 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
577 {
579 GrDrag *drag = dragger->parent;
580 // a midpoint dragger can (logically) only contain one GrDraggable
581 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
583 // get begin and end points between which dragging is allowed:
584 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
585 *moving = g_slist_append(*moving, dragger);
587 guint lowest_i = draggable->point_i;
588 guint highest_i = draggable->point_i;
589 GrDragger *lowest_dragger = dragger;
590 GrDragger *highest_dragger = dragger;
591 if (dragger->isSelected()) {
592 GrDragger* d_add;
593 while ( true )
594 {
595 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
596 if ( d_add && g_list_find(drag->selected, d_add) ) {
597 lowest_i = lowest_i - 1;
598 *moving = g_slist_prepend(*moving, d_add);
599 lowest_dragger = d_add;
600 } else {
601 break;
602 }
603 }
605 while ( true )
606 {
607 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
608 if ( d_add && g_list_find(drag->selected, d_add) ) {
609 highest_i = highest_i + 1;
610 *moving = g_slist_append(*moving, d_add);
611 highest_dragger = d_add;
612 } else {
613 break;
614 }
615 }
616 }
618 if ( SP_IS_LINEARGRADIENT(server) ) {
619 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
620 GrDragger *d_temp;
621 if (lowest_i == 1) {
622 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
623 } else {
624 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
625 }
626 if (d_temp)
627 *begin = d_temp->point;
629 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
630 if (d_temp == NULL) {
631 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
632 }
633 if (d_temp)
634 *end = d_temp->point;
635 } else if ( SP_IS_RADIALGRADIENT(server) ) {
636 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
637 GrDragger *d_temp;
638 if (lowest_i == 1) {
639 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
640 } else {
641 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
642 }
643 if (d_temp)
644 *begin = d_temp->point;
646 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
647 if (d_temp == NULL) {
648 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
649 }
650 if (d_temp)
651 *end = d_temp->point;
652 }
654 *low_lim = dragger->point - (lowest_dragger->point - *begin);
655 *high_lim = dragger->point - (highest_dragger->point - *end);
656 }
660 /**
661 Called when a midpoint knot is dragged.
662 */
663 static void
664 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
665 {
666 GrDragger *dragger = (GrDragger *) data;
667 GrDrag *drag = dragger->parent;
668 // a midpoint dragger can (logically) only contain one GrDraggable
669 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
671 // FIXME: take from prefs
672 double snap_fraction = 0.1;
674 NR::Point p = *ppointer;
675 NR::Point begin(0,0), end(0,0);
676 NR::Point low_lim(0,0), high_lim(0,0);
678 SPObject *server = draggable->getServer();
680 GSList *moving = NULL;
681 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
683 if (state & GDK_CONTROL_MASK) {
684 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
685 } else {
686 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
687 }
688 NR::Point displacement = p - dragger->point;
690 for (GSList const* i = moving; i != NULL; i = i->next) {
691 GrDragger *drg = (GrDragger*) i->data;
692 SPKnot *drgknot = drg->knot;
693 NR::Point this_move = displacement;
694 if (state & GDK_MOD1_MASK) {
695 // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
696 double alpha = 1.5;
697 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
698 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
699 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
700 } else { // drg is on the begin side from dragger
701 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
702 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
703 }
704 }
705 drg->point += this_move;
706 sp_knot_moveto (drgknot, & drg->point);
707 drg->fireDraggables (false);
708 drg->updateDependencies(false);
709 }
711 g_slist_free(moving);
713 drag->keep_selection = dragger->isSelected();
714 }
718 static void
719 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
720 {
721 GrDragger *dragger = (GrDragger *) data;
723 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
724 }
726 /**
727 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
728 */
729 static void
730 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
731 {
732 GrDragger *dragger = (GrDragger *) data;
734 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
736 dragger->point_original = dragger->point = knot->pos;
738 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
739 dragger->fireDraggables (true, true);
740 } else {
741 dragger->fireDraggables (true);
742 }
744 for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
745 GrDragger *d = (GrDragger *) i->data;
746 if (d == dragger)
747 continue;
748 d->fireDraggables (true);
749 }
751 // make this dragger selected
752 if (!dragger->parent->keep_selection) {
753 dragger->parent->setSelected (dragger);
754 }
755 dragger->parent->keep_selection = false;
757 dragger->updateDependencies(true);
759 // we did an undoable action
760 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
761 _("Move gradient handle"));
762 }
764 /**
765 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
766 state of the keyboard keys
767 */
768 static void
769 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
770 {
771 GrDragger *dragger = (GrDragger *) data;
772 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
773 if (!draggable) return;
775 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
776 // delete this knot from vector
777 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
778 gradient = sp_gradient_get_vector (gradient, false);
779 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
780 SPStop *stop = NULL;
781 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
782 case POINT_LG_BEGIN:
783 case POINT_RG_CENTER:
784 stop = sp_first_stop(gradient);
785 {
786 SPStop *next = sp_next_stop (stop);
787 if (next) {
788 next->offset = 0;
789 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
790 }
791 }
792 break;
793 case POINT_LG_END:
794 case POINT_RG_R1:
795 case POINT_RG_R2:
796 stop = sp_last_stop(gradient);
797 {
798 SPStop *prev = sp_prev_stop (stop, gradient);
799 if (prev) {
800 prev->offset = 1;
801 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
802 }
803 }
804 break;
805 case POINT_LG_MID:
806 case POINT_RG_MID1:
807 case POINT_RG_MID2:
808 stop = sp_get_stop_i(gradient, draggable->point_i);
809 break;
810 }
812 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
813 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
814 _("Delete gradient stop"));
815 }
816 } else {
817 // select the dragger
818 dragger->point_original = dragger->point;
820 if ( state & GDK_SHIFT_MASK ) {
821 dragger->parent->setSelected (dragger, true, false);
822 } else {
823 dragger->parent->setSelected (dragger);
824 }
825 }
826 }
828 /**
829 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
830 */
831 static void
832 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
833 {
834 GrDragger *dragger = (GrDragger *) data;
836 dragger->point_original = dragger->point;
838 if (dragger->draggables == NULL)
839 return;
841 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
842 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
843 }
845 /**
846 Act upon all draggables of the dragger, setting them to the dragger's point
847 */
848 void
849 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
850 {
851 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
852 GrDraggable *draggable = (GrDraggable *) i->data;
854 // set local_change flag so that selection_changed callback does not regenerate draggers
855 this->parent->local_change = true;
857 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
858 // to the center, unless it's the first update upon merge when we must snap it to the point
859 if (merging_focus ||
860 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
861 {
862 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
863 }
864 }
865 }
867 /**
868 Checks if the dragger has a draggable with this point_type
869 */
870 bool
871 GrDragger::isA (gint point_type)
872 {
873 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
874 GrDraggable *draggable = (GrDraggable *) i->data;
875 if (draggable->point_type == point_type) {
876 return true;
877 }
878 }
879 return false;
880 }
882 /**
883 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
884 */
885 bool
886 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
887 {
888 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
889 GrDraggable *draggable = (GrDraggable *) i->data;
890 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
891 return true;
892 }
893 }
894 return false;
895 }
897 bool
898 GrDraggable::mayMerge (GrDraggable *da2)
899 {
900 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
901 // we must not merge the points of the same gradient!
902 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
903 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
904 // except that we can snap center and focus together
905 return false;
906 }
907 }
908 // disable merging of midpoints.
909 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
910 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
911 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
912 return false;
914 return true;
915 }
917 bool
918 GrDragger::mayMerge (GrDragger *other)
919 {
920 if (this == other)
921 return false;
923 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
924 GrDraggable *da1 = (GrDraggable *) i->data;
925 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
926 GrDraggable *da2 = (GrDraggable *) j->data;
927 if (!da1->mayMerge(da2))
928 return false;
929 }
930 }
931 return true;
932 }
934 bool
935 GrDragger::mayMerge (GrDraggable *da2)
936 {
937 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
938 GrDraggable *da1 = (GrDraggable *) i->data;
939 if (!da1->mayMerge(da2))
940 return false;
941 }
942 return true;
943 }
945 /**
946 Updates the statusbar tip of the dragger knot, based on its draggables
947 */
948 void
949 GrDragger::updateTip ()
950 {
951 if (this->knot && this->knot->tip) {
952 g_free (this->knot->tip);
953 this->knot->tip = NULL;
954 }
956 if (g_slist_length (this->draggables) == 1) {
957 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
958 char *item_desc = sp_item_description(draggable->item);
959 switch (draggable->point_type) {
960 case POINT_LG_MID:
961 case POINT_RG_MID1:
962 case POINT_RG_MID2:
963 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"),
964 _(gr_knot_descr[draggable->point_type]),
965 draggable->point_i,
966 item_desc,
967 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
968 break;
970 default:
971 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"),
972 _(gr_knot_descr[draggable->point_type]),
973 item_desc,
974 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
975 break;
976 }
977 g_free(item_desc);
978 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
979 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
980 } else {
981 int length = g_slist_length (this->draggables);
982 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
983 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
984 length),
985 length);
986 }
987 }
989 /**
990 Adds a draggable to the dragger
991 */
992 void
993 GrDragger::updateKnotShape ()
994 {
995 if (!draggables)
996 return;
997 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
998 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
999 }
1001 /**
1002 Adds a draggable to the dragger
1003 */
1004 void
1005 GrDragger::addDraggable (GrDraggable *draggable)
1006 {
1007 this->draggables = g_slist_prepend (this->draggables, draggable);
1009 this->updateTip();
1010 }
1013 /**
1014 Moves this dragger to the point of the given draggable, acting upon all other draggables
1015 */
1016 void
1017 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1018 {
1019 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1020 this->point_original = this->point;
1022 sp_knot_moveto (this->knot, &(this->point));
1024 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1025 GrDraggable *da = (GrDraggable *) i->data;
1026 if ( (da->item == item) &&
1027 (point_type == -1 || da->point_type == point_type) &&
1028 (point_i == -1 || da->point_i == point_i) &&
1029 (da->fill_or_stroke == fill_or_stroke) ) {
1030 continue;
1031 }
1032 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1033 }
1034 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1035 }
1038 /**
1039 Moves all midstop draggables that depend on this one
1040 */
1041 void
1042 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1043 SPObject *server = draggable->getServer();
1044 if (!server)
1045 return;
1046 guint num = SP_GRADIENT(server)->vector.stops.size();
1047 if (num <= 2) return;
1049 if ( SP_IS_LINEARGRADIENT(server) ) {
1050 for ( guint i = 1; i < num - 1; i++ ) {
1051 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1052 }
1053 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1054 for ( guint i = 1; i < num - 1; i++ ) {
1055 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1056 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1057 }
1058 }
1059 }
1062 /**
1063 Moves all draggables that depend on this one
1064 */
1065 void
1066 GrDragger::updateDependencies (bool write_repr)
1067 {
1068 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1069 GrDraggable *draggable = (GrDraggable *) i->data;
1070 switch (draggable->point_type) {
1071 case POINT_LG_BEGIN:
1072 {
1073 // the end point is dependent only when dragging with ctrl+shift
1074 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1076 this->updateMidstopDependencies (draggable, write_repr);
1077 }
1078 break;
1079 case POINT_LG_END:
1080 {
1081 // the begin point is dependent only when dragging with ctrl+shift
1082 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1084 this->updateMidstopDependencies (draggable, write_repr);
1085 }
1086 break;
1087 case POINT_LG_MID:
1088 // no other nodes depend on mid points.
1089 break;
1090 case POINT_RG_R2:
1091 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1092 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1093 this->updateMidstopDependencies (draggable, write_repr);
1094 break;
1095 case POINT_RG_R1:
1096 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1097 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1098 this->updateMidstopDependencies (draggable, write_repr);
1099 break;
1100 case POINT_RG_CENTER:
1101 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1102 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1103 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1104 this->updateMidstopDependencies (draggable, write_repr);
1105 break;
1106 case POINT_RG_FOCUS:
1107 // nothing can depend on that
1108 break;
1109 case POINT_RG_MID1:
1110 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1111 break;
1112 case POINT_RG_MID2:
1113 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1114 break;
1115 default:
1116 break;
1117 }
1118 }
1119 }
1123 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1124 {
1125 this->draggables = NULL;
1127 this->parent = parent;
1129 this->point = p;
1130 this->point_original = p;
1132 // create the knot
1133 this->knot = sp_knot_new (parent->desktop, NULL);
1134 this->knot->setMode(SP_KNOT_MODE_XOR);
1135 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1136 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1137 sp_knot_update_ctrl(this->knot);
1139 // move knot to the given point
1140 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1141 sp_knot_show (this->knot);
1143 // connect knot's signals
1144 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1145 // luckily, midstops never snap to other nodes so are never unsnapped...
1146 && ( (draggable->point_type == POINT_LG_MID)
1147 || (draggable->point_type == POINT_RG_MID1)
1148 || (draggable->point_type == POINT_RG_MID2) ) )
1149 {
1150 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1151 } else {
1152 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1153 }
1154 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1155 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1156 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1157 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1159 // add the initial draggable
1160 if (draggable)
1161 this->addDraggable (draggable);
1162 updateKnotShape();
1163 }
1165 GrDragger::~GrDragger ()
1166 {
1167 // unselect if it was selected
1168 this->parent->setDeselected(this);
1170 // disconnect signals
1171 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1172 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1173 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1174 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1175 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1177 /* unref should call destroy */
1178 g_object_unref (G_OBJECT (this->knot));
1180 // delete all draggables
1181 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1182 delete ((GrDraggable *) i->data);
1183 }
1184 g_slist_free (this->draggables);
1185 this->draggables = NULL;
1186 }
1188 /**
1189 Select the dragger which has the given draggable.
1190 */
1191 GrDragger *
1192 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1193 {
1194 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1195 GrDragger *dragger = (GrDragger *) i->data;
1196 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1197 GrDraggable *da2 = (GrDraggable *) j->data;
1198 if ( (da2->item == item) &&
1199 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1200 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1201 (da2->fill_or_stroke == fill_or_stroke)) {
1202 return (dragger);
1203 }
1204 }
1205 }
1206 return NULL;
1207 }
1210 void
1211 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1212 {
1213 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1214 if (d && d != this) {
1215 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1216 }
1217 }
1220 /**
1221 Draw this dragger as selected
1222 */
1223 void
1224 GrDragger::select()
1225 {
1226 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1227 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1228 }
1230 /**
1231 Draw this dragger as normal (deselected)
1232 */
1233 void
1234 GrDragger::deselect()
1235 {
1236 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1237 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1238 }
1240 bool
1241 GrDragger::isSelected()
1242 {
1243 return g_list_find (parent->selected, this);
1244 }
1246 /**
1247 \brief Deselect all stops/draggers (private)
1248 */
1249 void
1250 GrDrag::deselect_all()
1251 {
1252 while (selected) {
1253 ( (GrDragger*) selected->data)->deselect();
1254 selected = g_list_remove(selected, selected->data);
1255 }
1256 }
1258 /**
1259 \brief Deselect all stops/draggers (public; emits signal)
1260 */
1261 void
1262 GrDrag::deselectAll()
1263 {
1264 deselect_all();
1265 this->desktop->emitToolSubselectionChanged(NULL);
1266 }
1268 /**
1269 \brief Select all stops/draggers
1270 */
1271 void
1272 GrDrag::selectAll()
1273 {
1274 for (GList *l = this->draggers; l != NULL; l = l->next) {
1275 GrDragger *d = ((GrDragger *) l->data);
1276 setSelected (d, true, true);
1277 }
1278 }
1280 /**
1281 \brief Select all stops/draggers that match the coords
1282 */
1283 void
1284 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1285 {
1286 for (GList *l = this->draggers; l != NULL; l = l->next) {
1287 GrDragger *d = ((GrDragger *) l->data);
1288 for (guint k = 0; k < coords.size(); k++) {
1289 if (NR::L2 (d->point - coords[k]) < 1e-4) {
1290 setSelected (d, true, true);
1291 }
1292 }
1293 }
1294 }
1296 /**
1297 \brief Select a dragger
1298 \param dragger The dragger to select
1299 \param add_to_selection If true, add to selection, otherwise deselect others
1300 \param override If true, always select this node, otherwise toggle selected status
1301 */
1302 void
1303 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1304 {
1305 GrDragger *seldragger = NULL;
1307 if (add_to_selection) {
1308 if (!dragger) return;
1309 if (override) {
1310 if (!g_list_find(selected, dragger)) {
1311 selected = g_list_prepend(selected, dragger);
1312 }
1313 dragger->select();
1314 seldragger = dragger;
1315 } else { // toggle
1316 if (g_list_find(selected, dragger)) {
1317 selected = g_list_remove(selected, dragger);
1318 dragger->deselect();
1319 if (selected) {
1320 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1321 }
1322 } else {
1323 selected = g_list_prepend(selected, dragger);
1324 dragger->select();
1325 seldragger = dragger;
1326 }
1327 }
1328 } else {
1329 deselect_all();
1330 if (dragger) {
1331 selected = g_list_prepend(selected, dragger);
1332 dragger->select();
1333 seldragger = dragger;
1334 }
1335 }
1336 if (seldragger) {
1337 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1338 }
1339 }
1341 /**
1342 \brief Deselect a dragger
1343 \param dragger The dragger to deselect
1344 */
1345 void
1346 GrDrag::setDeselected (GrDragger *dragger)
1347 {
1348 if (g_list_find(selected, dragger)) {
1349 selected = g_list_remove(selected, dragger);
1350 dragger->deselect();
1351 }
1352 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1353 }
1357 /**
1358 Create a line from p1 to p2 and add it to the lines list
1359 */
1360 void
1361 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1362 {
1363 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1364 SP_TYPE_CTRLLINE, NULL);
1365 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1366 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1367 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1368 sp_canvas_item_show (line);
1369 this->lines = g_slist_append (this->lines, line);
1370 }
1372 /**
1373 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1374 new dragger and add it to draggers list
1375 */
1376 void
1377 GrDrag::addDragger (GrDraggable *draggable)
1378 {
1379 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1381 for (GList *i = this->draggers; i != NULL; i = i->next) {
1382 GrDragger *dragger = (GrDragger *) i->data;
1383 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1384 // distance is small, merge this draggable into dragger, no need to create new dragger
1385 dragger->addDraggable (draggable);
1386 dragger->updateKnotShape();
1387 return;
1388 }
1389 }
1391 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1392 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1393 this->draggers = g_list_append (this->draggers, new_dragger);
1394 }
1396 /**
1397 Add draggers for the radial gradient rg on item
1398 */
1399 void
1400 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1401 {
1402 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1403 guint num = rg->vector.stops.size();
1404 if (num > 2) {
1405 for ( guint i = 1; i < num - 1; i++ ) {
1406 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1407 }
1408 }
1409 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1410 if (num > 2) {
1411 for ( guint i = 1; i < num - 1; i++ ) {
1412 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1413 }
1414 }
1415 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1416 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1417 }
1419 /**
1420 Add draggers for the linear gradient lg on item
1421 */
1422 void
1423 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1424 {
1425 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1426 guint num = lg->vector.stops.size();
1427 if (num > 2) {
1428 for ( guint i = 1; i < num - 1; i++ ) {
1429 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1430 }
1431 }
1432 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1433 }
1435 /**
1436 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1437 */
1438 void
1439 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1440 {
1441 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1442 if (dragger) {
1443 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1444 }
1445 }
1447 /**
1448 Regenerates the draggers list from the current selection; is called when selection is changed or
1449 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1450 */
1451 void
1452 GrDrag::updateDraggers ()
1453 {
1454 while (selected) {
1455 selected = g_list_remove(selected, selected->data);
1456 }
1457 // delete old draggers
1458 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1459 delete ((GrDragger *) i->data);
1460 }
1461 g_list_free (this->draggers);
1462 this->draggers = NULL;
1464 g_return_if_fail (this->selection != NULL);
1466 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1468 SPItem *item = SP_ITEM(i->data);
1469 SPStyle *style = SP_OBJECT_STYLE (item);
1471 if (style && (style->fill.isPaintserver())) {
1472 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1473 if (SP_IS_LINEARGRADIENT (server)) {
1474 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1475 } else if (SP_IS_RADIALGRADIENT (server)) {
1476 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1477 }
1478 }
1480 if (style && (style->stroke.isPaintserver())) {
1481 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1482 if (SP_IS_LINEARGRADIENT (server)) {
1483 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1484 } else if (SP_IS_RADIALGRADIENT (server)) {
1485 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1486 }
1487 }
1488 }
1489 }
1491 /**
1492 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1493 lines are always in sync with the actual gradient
1494 */
1495 void
1496 GrDrag::updateLines ()
1497 {
1498 // delete old lines
1499 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1500 gtk_object_destroy( GTK_OBJECT (i->data));
1501 }
1502 g_slist_free (this->lines);
1503 this->lines = NULL;
1505 g_return_if_fail (this->selection != NULL);
1507 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1509 SPItem *item = SP_ITEM(i->data);
1511 SPStyle *style = SP_OBJECT_STYLE (item);
1513 if (style && (style->fill.isPaintserver())) {
1514 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1515 if (SP_IS_LINEARGRADIENT (server)) {
1516 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);
1517 } else if (SP_IS_RADIALGRADIENT (server)) {
1518 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1519 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1520 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1521 }
1522 }
1524 if (style && (style->stroke.isPaintserver())) {
1525 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1526 if (SP_IS_LINEARGRADIENT (server)) {
1527 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);
1528 } else if (SP_IS_RADIALGRADIENT (server)) {
1529 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1530 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1531 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1532 }
1533 }
1534 }
1535 }
1537 /**
1538 Regenerates the levels list from the current selection
1539 */
1540 void
1541 GrDrag::updateLevels ()
1542 {
1543 hor_levels.clear();
1544 vert_levels.clear();
1546 g_return_if_fail (this->selection != NULL);
1548 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1549 SPItem *item = SP_ITEM(i->data);
1550 NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1551 if (rect) {
1552 // Remember the edges of the bbox and the center axis
1553 hor_levels.push_back(rect->min()[NR::Y]);
1554 hor_levels.push_back(rect->max()[NR::Y]);
1555 hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1556 vert_levels.push_back(rect->min()[NR::X]);
1557 vert_levels.push_back(rect->max()[NR::X]);
1558 vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1559 }
1560 }
1561 }
1563 void
1564 GrDrag::selected_reverse_vector ()
1565 {
1566 if (selected == NULL)
1567 return;
1569 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1570 GrDraggable *draggable = (GrDraggable *) i->data;
1572 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1573 }
1574 }
1576 void
1577 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1578 {
1579 selected_move (x, y, false, scale_radial);
1580 }
1582 void
1583 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1584 {
1585 if (selected == NULL)
1586 return;
1588 bool did = false;
1590 for (GList *i = selected; i != NULL; i = i->next) {
1591 GrDragger *d = (GrDragger *) i->data;
1593 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1594 // if this is a an endpoint,
1596 // Moving an rg center moves its focus and radii as well.
1597 // therefore, if this is a focus or radius and if selection
1598 // contains the center as well, do not move this one
1599 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1600 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1601 bool skip_radius_with_center = false;
1602 for (GList *di = selected; di != NULL; di = di->next) {
1603 GrDragger *d_new = (GrDragger *) di->data;
1604 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1605 POINT_RG_CENTER,
1606 0,
1607 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1608 // FIXME: here we take into account only the first draggable!
1609 skip_radius_with_center = true;
1610 }
1611 }
1612 if (skip_radius_with_center)
1613 continue;
1614 }
1616 did = true;
1617 d->point += NR::Point (x, y);
1618 d->point_original = d->point;
1619 sp_knot_moveto (d->knot, &(d->point));
1621 d->fireDraggables (write_repr, scale_radial);
1623 d->updateDependencies(write_repr);
1624 }
1626 if (write_repr && did) {
1627 // we did an undoable action
1628 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1629 _("Move gradient handle(s)"));
1630 return;
1631 }
1633 }
1635 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1637 GrDragger *dragger = (GrDragger *) selected->data;
1638 // a midpoint dragger can (logically) only contain one GrDraggable
1639 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1641 NR::Point begin(0,0), end(0,0);
1642 NR::Point low_lim(0,0), high_lim(0,0);
1644 SPObject *server = draggable->getServer();
1645 GSList *moving = NULL;
1646 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1648 NR::Point p(x, y);
1649 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1650 NR::Point displacement = p - dragger->point;
1652 for (GSList const* i = moving; i != NULL; i = i->next) {
1653 GrDragger *drg = (GrDragger*) i->data;
1654 SPKnot *drgknot = drg->knot;
1655 drg->point += displacement;
1656 sp_knot_moveto (drgknot, & drg->point);
1657 drg->fireDraggables (true);
1658 drg->updateDependencies(true);
1659 did = true;
1660 }
1662 g_slist_free(moving);
1664 if (write_repr && did) {
1665 // we did an undoable action
1666 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1667 _("Move gradient mid stop(s)"));
1668 }
1669 }
1670 }
1672 void
1673 GrDrag::selected_move_screen (double x, double y)
1674 {
1675 gdouble zoom = desktop->current_zoom();
1676 gdouble zx = x / zoom;
1677 gdouble zy = y / zoom;
1679 selected_move (zx, zy);
1680 }
1682 /**
1683 Select the knot next to the last selected one and deselect all other selected.
1684 */
1685 GrDragger *
1686 GrDrag::select_next ()
1687 {
1688 GrDragger *d = NULL;
1689 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1690 if (draggers)
1691 d = (GrDragger *) draggers->data;
1692 } else {
1693 d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1694 }
1695 if (d)
1696 setSelected (d);
1697 return d;
1698 }
1700 /**
1701 Select the knot previous from the last selected one and deselect all other selected.
1702 */
1703 GrDragger *
1704 GrDrag::select_prev ()
1705 {
1706 GrDragger *d = NULL;
1707 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1708 if (draggers)
1709 d = (GrDragger *) g_list_last (draggers)->data;
1710 } else {
1711 d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1712 }
1713 if (d)
1714 setSelected (d);
1715 return d;
1716 }
1719 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1720 void
1721 GrDrag::deleteSelected (bool just_one)
1722 {
1723 if (!selected) return;
1725 SPDocument *document = false;
1727 struct StructStopInfo {
1728 SPStop * spstop;
1729 GrDraggable * draggable;
1730 SPGradient * gradient;
1731 SPGradient * vector;
1732 };
1734 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1735 GSList *endstoplist = NULL; // list of stops that must be deleted
1736 while (selected) {
1737 GrDragger *dragger = (GrDragger*) selected->data;
1738 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1739 GrDraggable *draggable = (GrDraggable*) drgble->data;
1740 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1741 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1743 switch (draggable->point_type) {
1744 case POINT_LG_MID:
1745 case POINT_RG_MID1:
1746 case POINT_RG_MID2:
1747 {
1748 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1749 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1750 bool present = false;
1751 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1752 if ( (SPStop*)l->data == stop ) {
1753 present = true;
1754 break; // no need to search further.
1755 }
1756 }
1757 if (!present)
1758 midstoplist = g_slist_append(midstoplist, stop);
1759 }
1760 break;
1761 case POINT_LG_BEGIN:
1762 case POINT_LG_END:
1763 case POINT_RG_CENTER:
1764 case POINT_RG_R1:
1765 case POINT_RG_R2:
1766 {
1767 SPStop *stop = NULL;
1768 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1769 stop = sp_first_stop(vector);
1770 } else {
1771 stop = sp_last_stop(vector);
1772 }
1773 if (stop) {
1774 StructStopInfo *stopinfo = new StructStopInfo;
1775 stopinfo->spstop = stop;
1776 stopinfo->draggable = draggable;
1777 stopinfo->gradient = gradient;
1778 stopinfo->vector = vector;
1779 // check if already present in list. (e.g. when both R1 and R2 were selected)
1780 bool present = false;
1781 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1782 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1783 present = true;
1784 break; // no need to search further.
1785 }
1786 }
1787 if (!present)
1788 endstoplist = g_slist_append(endstoplist, stopinfo);
1789 }
1790 }
1791 break;
1792 default:
1793 break;
1794 }
1795 }
1796 selected = g_list_remove(selected, dragger);
1797 if ( just_one ) break; // iterate once if just_one is set.
1798 }
1799 while (midstoplist) {
1800 SPStop *stop = (SPStop*) midstoplist->data;
1801 document = SP_OBJECT_DOCUMENT (stop);
1802 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1803 parent->removeChild(SP_OBJECT_REPR(stop));
1804 midstoplist = g_slist_remove(midstoplist, stop);
1805 }
1806 while (endstoplist) {
1807 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
1808 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1810 // 2 is the minimum, cannot delete more than that without deleting the whole vector
1811 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1812 // manually count the children, don't know if there already exists a function for this...
1813 int len = 0;
1814 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1815 child != NULL ;
1816 child = SP_OBJECT_NEXT(child) )
1817 {
1818 if ( SP_IS_STOP(child) ) len ++;
1819 }
1820 if (len > 2)
1821 {
1822 switch (stopinfo->draggable->point_type) {
1823 case POINT_LG_BEGIN:
1824 {
1825 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1827 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1828 NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1829 NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1830 SPStop *stop = sp_first_stop(stopinfo->vector);
1831 gdouble offset = stop->offset;
1832 NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1833 lg->x1.computed = newbegin[NR::X];
1834 lg->y1.computed = newbegin[NR::Y];
1836 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1837 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1838 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1839 stop->offset = 0;
1840 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1842 // iterate through midstops to set new offset values such that they won't move on canvas.
1843 SPStop *laststop = sp_last_stop(stopinfo->vector);
1844 stop = sp_next_stop(stop);
1845 while ( stop != laststop ) {
1846 stop->offset = (stop->offset - offset)/(1 - offset);
1847 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1848 stop = sp_next_stop(stop);
1849 }
1850 }
1851 break;
1852 case POINT_LG_END:
1853 {
1854 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1856 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1857 NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1858 NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1859 SPStop *laststop = sp_last_stop(stopinfo->vector);
1860 gdouble offset = laststop->offset;
1861 NR::Point newend = begin + offset * (oldend - begin);
1862 lg->x2.computed = newend[NR::X];
1863 lg->y2.computed = newend[NR::Y];
1865 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1866 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1867 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1868 laststop->offset = 1;
1869 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1871 // iterate through midstops to set new offset values such that they won't move on canvas.
1872 SPStop *stop = sp_first_stop(stopinfo->vector);
1873 stop = sp_next_stop(stop);
1874 while ( stop != laststop ) {
1875 stop->offset = stop->offset / offset;
1876 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1877 stop = sp_next_stop(stop);
1878 }
1879 }
1880 break;
1881 case POINT_RG_CENTER:
1882 {
1883 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1884 if (newfirst) {
1885 newfirst->offset = 0;
1886 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1887 }
1888 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1889 }
1890 break;
1891 case POINT_RG_R1:
1892 case POINT_RG_R2:
1893 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1895 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1896 double oldradius = rg->r.computed;
1897 SPStop *laststop = sp_last_stop(stopinfo->vector);
1898 gdouble offset = laststop->offset;
1899 double newradius = offset * oldradius;
1900 rg->r.computed = newradius;
1902 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1903 sp_repr_set_svg_double(repr, "r", rg->r.computed);
1904 laststop->offset = 1;
1905 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1907 // iterate through midstops to set new offset values such that they won't move on canvas.
1908 SPStop *stop = sp_first_stop(stopinfo->vector);
1909 stop = sp_next_stop(stop);
1910 while ( stop != laststop ) {
1911 stop->offset = stop->offset / offset;
1912 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1913 stop = sp_next_stop(stop);
1914 }
1915 break;
1916 }
1917 }
1918 else
1919 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
1920 SPCSSAttr *css = sp_repr_css_attr_new ();
1922 // stopinfo->spstop is the selected stop
1923 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
1924 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
1925 unselectedrepr = unselectedrepr->next();
1926 }
1928 if (unselectedrepr == NULL) {
1929 if (stopinfo->draggable->fill_or_stroke) {
1930 sp_repr_css_unset_property (css, "fill");
1931 } else {
1932 sp_repr_css_unset_property (css, "stroke");
1933 }
1934 } else {
1935 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
1936 if (stopinfo->draggable->fill_or_stroke) {
1937 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1938 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1939 } else {
1940 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1941 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1942 }
1943 sp_repr_css_attr_unref (stopcss);
1944 }
1946 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1947 sp_repr_css_attr_unref (css);
1948 }
1950 endstoplist = g_slist_remove(endstoplist, stopinfo);
1951 delete stopinfo;
1952 }
1954 if (document) {
1955 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1956 }
1957 }
1960 /*
1961 Local Variables:
1962 mode:c++
1963 c-file-style:"stroustrup"
1964 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1965 indent-tabs-mode:nil
1966 fill-column:99
1967 End:
1968 */
1969 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :