2e1a8db8393ba5dc1b2845c27a799b7424ebb418
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 }
1297 /**
1298 \brief Select all stops/draggers that fall within the rect
1299 */
1300 void
1301 GrDrag::selectRect(NR::Rect const &r)
1302 {
1303 for (GList *l = this->draggers; l != NULL; l = l->next) {
1304 GrDragger *d = ((GrDragger *) l->data);
1305 if (r.contains(d->point)) {
1306 setSelected (d, true, true);
1307 }
1308 }
1309 }
1311 /**
1312 \brief Select a dragger
1313 \param dragger The dragger to select
1314 \param add_to_selection If true, add to selection, otherwise deselect others
1315 \param override If true, always select this node, otherwise toggle selected status
1316 */
1317 void
1318 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1319 {
1320 GrDragger *seldragger = NULL;
1322 if (add_to_selection) {
1323 if (!dragger) return;
1324 if (override) {
1325 if (!g_list_find(selected, dragger)) {
1326 selected = g_list_prepend(selected, dragger);
1327 }
1328 dragger->select();
1329 seldragger = dragger;
1330 } else { // toggle
1331 if (g_list_find(selected, dragger)) {
1332 selected = g_list_remove(selected, dragger);
1333 dragger->deselect();
1334 if (selected) {
1335 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1336 }
1337 } else {
1338 selected = g_list_prepend(selected, dragger);
1339 dragger->select();
1340 seldragger = dragger;
1341 }
1342 }
1343 } else {
1344 deselect_all();
1345 if (dragger) {
1346 selected = g_list_prepend(selected, dragger);
1347 dragger->select();
1348 seldragger = dragger;
1349 }
1350 }
1351 if (seldragger) {
1352 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1353 }
1354 }
1356 /**
1357 \brief Deselect a dragger
1358 \param dragger The dragger to deselect
1359 */
1360 void
1361 GrDrag::setDeselected (GrDragger *dragger)
1362 {
1363 if (g_list_find(selected, dragger)) {
1364 selected = g_list_remove(selected, dragger);
1365 dragger->deselect();
1366 }
1367 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1368 }
1372 /**
1373 Create a line from p1 to p2 and add it to the lines list
1374 */
1375 void
1376 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1377 {
1378 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1379 SP_TYPE_CTRLLINE, NULL);
1380 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1381 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1382 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1383 sp_canvas_item_show (line);
1384 this->lines = g_slist_append (this->lines, line);
1385 }
1387 /**
1388 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1389 new dragger and add it to draggers list
1390 */
1391 void
1392 GrDrag::addDragger (GrDraggable *draggable)
1393 {
1394 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1396 for (GList *i = this->draggers; i != NULL; i = i->next) {
1397 GrDragger *dragger = (GrDragger *) i->data;
1398 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1399 // distance is small, merge this draggable into dragger, no need to create new dragger
1400 dragger->addDraggable (draggable);
1401 dragger->updateKnotShape();
1402 return;
1403 }
1404 }
1406 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1407 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1408 this->draggers = g_list_append (this->draggers, new_dragger);
1409 }
1411 /**
1412 Add draggers for the radial gradient rg on item
1413 */
1414 void
1415 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1416 {
1417 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1418 guint num = rg->vector.stops.size();
1419 if (num > 2) {
1420 for ( guint i = 1; i < num - 1; i++ ) {
1421 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1422 }
1423 }
1424 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1425 if (num > 2) {
1426 for ( guint i = 1; i < num - 1; i++ ) {
1427 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1428 }
1429 }
1430 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1431 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1432 }
1434 /**
1435 Add draggers for the linear gradient lg on item
1436 */
1437 void
1438 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1439 {
1440 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1441 guint num = lg->vector.stops.size();
1442 if (num > 2) {
1443 for ( guint i = 1; i < num - 1; i++ ) {
1444 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1445 }
1446 }
1447 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1448 }
1450 /**
1451 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1452 */
1453 void
1454 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1455 {
1456 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1457 if (dragger) {
1458 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1459 }
1460 }
1462 /**
1463 Regenerates the draggers list from the current selection; is called when selection is changed or
1464 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1465 */
1466 void
1467 GrDrag::updateDraggers ()
1468 {
1469 while (selected) {
1470 selected = g_list_remove(selected, selected->data);
1471 }
1472 // delete old draggers
1473 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1474 delete ((GrDragger *) i->data);
1475 }
1476 g_list_free (this->draggers);
1477 this->draggers = NULL;
1479 g_return_if_fail (this->selection != NULL);
1481 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1483 SPItem *item = SP_ITEM(i->data);
1484 SPStyle *style = SP_OBJECT_STYLE (item);
1486 if (style && (style->fill.isPaintserver())) {
1487 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1488 if (SP_IS_LINEARGRADIENT (server)) {
1489 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1490 } else if (SP_IS_RADIALGRADIENT (server)) {
1491 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1492 }
1493 }
1495 if (style && (style->stroke.isPaintserver())) {
1496 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1497 if (SP_IS_LINEARGRADIENT (server)) {
1498 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1499 } else if (SP_IS_RADIALGRADIENT (server)) {
1500 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1501 }
1502 }
1503 }
1504 }
1506 /**
1507 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1508 lines are always in sync with the actual gradient
1509 */
1510 void
1511 GrDrag::updateLines ()
1512 {
1513 // delete old lines
1514 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1515 gtk_object_destroy( GTK_OBJECT (i->data));
1516 }
1517 g_slist_free (this->lines);
1518 this->lines = NULL;
1520 g_return_if_fail (this->selection != NULL);
1522 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1524 SPItem *item = SP_ITEM(i->data);
1526 SPStyle *style = SP_OBJECT_STYLE (item);
1528 if (style && (style->fill.isPaintserver())) {
1529 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1530 if (SP_IS_LINEARGRADIENT (server)) {
1531 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);
1532 } else if (SP_IS_RADIALGRADIENT (server)) {
1533 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1534 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1535 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1536 }
1537 }
1539 if (style && (style->stroke.isPaintserver())) {
1540 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1541 if (SP_IS_LINEARGRADIENT (server)) {
1542 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);
1543 } else if (SP_IS_RADIALGRADIENT (server)) {
1544 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1545 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1546 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1547 }
1548 }
1549 }
1550 }
1552 /**
1553 Regenerates the levels list from the current selection
1554 */
1555 void
1556 GrDrag::updateLevels ()
1557 {
1558 hor_levels.clear();
1559 vert_levels.clear();
1561 g_return_if_fail (this->selection != NULL);
1563 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1564 SPItem *item = SP_ITEM(i->data);
1565 NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1566 if (rect) {
1567 // Remember the edges of the bbox and the center axis
1568 hor_levels.push_back(rect->min()[NR::Y]);
1569 hor_levels.push_back(rect->max()[NR::Y]);
1570 hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1571 vert_levels.push_back(rect->min()[NR::X]);
1572 vert_levels.push_back(rect->max()[NR::X]);
1573 vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1574 }
1575 }
1576 }
1578 void
1579 GrDrag::selected_reverse_vector ()
1580 {
1581 if (selected == NULL)
1582 return;
1584 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1585 GrDraggable *draggable = (GrDraggable *) i->data;
1587 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1588 }
1589 }
1591 void
1592 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1593 {
1594 selected_move (x, y, false, scale_radial);
1595 }
1597 void
1598 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1599 {
1600 if (selected == NULL)
1601 return;
1603 bool did = false;
1605 for (GList *i = selected; i != NULL; i = i->next) {
1606 GrDragger *d = (GrDragger *) i->data;
1608 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1609 // if this is a an endpoint,
1611 // Moving an rg center moves its focus and radii as well.
1612 // therefore, if this is a focus or radius and if selection
1613 // contains the center as well, do not move this one
1614 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1615 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1616 bool skip_radius_with_center = false;
1617 for (GList *di = selected; di != NULL; di = di->next) {
1618 GrDragger *d_new = (GrDragger *) di->data;
1619 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1620 POINT_RG_CENTER,
1621 0,
1622 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1623 // FIXME: here we take into account only the first draggable!
1624 skip_radius_with_center = true;
1625 }
1626 }
1627 if (skip_radius_with_center)
1628 continue;
1629 }
1631 did = true;
1632 d->point += NR::Point (x, y);
1633 d->point_original = d->point;
1634 sp_knot_moveto (d->knot, &(d->point));
1636 d->fireDraggables (write_repr, scale_radial);
1638 d->updateDependencies(write_repr);
1639 }
1641 if (write_repr && did) {
1642 // we did an undoable action
1643 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1644 _("Move gradient handle(s)"));
1645 return;
1646 }
1648 }
1650 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1652 GrDragger *dragger = (GrDragger *) selected->data;
1653 // a midpoint dragger can (logically) only contain one GrDraggable
1654 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1656 NR::Point begin(0,0), end(0,0);
1657 NR::Point low_lim(0,0), high_lim(0,0);
1659 SPObject *server = draggable->getServer();
1660 GSList *moving = NULL;
1661 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1663 NR::Point p(x, y);
1664 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1665 NR::Point displacement = p - dragger->point;
1667 for (GSList const* i = moving; i != NULL; i = i->next) {
1668 GrDragger *drg = (GrDragger*) i->data;
1669 SPKnot *drgknot = drg->knot;
1670 drg->point += displacement;
1671 sp_knot_moveto (drgknot, & drg->point);
1672 drg->fireDraggables (true);
1673 drg->updateDependencies(true);
1674 did = true;
1675 }
1677 g_slist_free(moving);
1679 if (write_repr && did) {
1680 // we did an undoable action
1681 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1682 _("Move gradient mid stop(s)"));
1683 }
1684 }
1685 }
1687 void
1688 GrDrag::selected_move_screen (double x, double y)
1689 {
1690 gdouble zoom = desktop->current_zoom();
1691 gdouble zx = x / zoom;
1692 gdouble zy = y / zoom;
1694 selected_move (zx, zy);
1695 }
1697 /**
1698 Select the knot next to the last selected one and deselect all other selected.
1699 */
1700 GrDragger *
1701 GrDrag::select_next ()
1702 {
1703 GrDragger *d = NULL;
1704 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1705 if (draggers)
1706 d = (GrDragger *) draggers->data;
1707 } else {
1708 d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1709 }
1710 if (d)
1711 setSelected (d);
1712 return d;
1713 }
1715 /**
1716 Select the knot previous from the last selected one and deselect all other selected.
1717 */
1718 GrDragger *
1719 GrDrag::select_prev ()
1720 {
1721 GrDragger *d = NULL;
1722 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1723 if (draggers)
1724 d = (GrDragger *) g_list_last (draggers)->data;
1725 } else {
1726 d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1727 }
1728 if (d)
1729 setSelected (d);
1730 return d;
1731 }
1734 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1735 void
1736 GrDrag::deleteSelected (bool just_one)
1737 {
1738 if (!selected) return;
1740 SPDocument *document = false;
1742 struct StructStopInfo {
1743 SPStop * spstop;
1744 GrDraggable * draggable;
1745 SPGradient * gradient;
1746 SPGradient * vector;
1747 };
1749 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1750 GSList *endstoplist = NULL; // list of stops that must be deleted
1751 while (selected) {
1752 GrDragger *dragger = (GrDragger*) selected->data;
1753 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1754 GrDraggable *draggable = (GrDraggable*) drgble->data;
1755 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1756 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1758 switch (draggable->point_type) {
1759 case POINT_LG_MID:
1760 case POINT_RG_MID1:
1761 case POINT_RG_MID2:
1762 {
1763 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1764 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1765 bool present = false;
1766 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1767 if ( (SPStop*)l->data == stop ) {
1768 present = true;
1769 break; // no need to search further.
1770 }
1771 }
1772 if (!present)
1773 midstoplist = g_slist_append(midstoplist, stop);
1774 }
1775 break;
1776 case POINT_LG_BEGIN:
1777 case POINT_LG_END:
1778 case POINT_RG_CENTER:
1779 case POINT_RG_R1:
1780 case POINT_RG_R2:
1781 {
1782 SPStop *stop = NULL;
1783 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1784 stop = sp_first_stop(vector);
1785 } else {
1786 stop = sp_last_stop(vector);
1787 }
1788 if (stop) {
1789 StructStopInfo *stopinfo = new StructStopInfo;
1790 stopinfo->spstop = stop;
1791 stopinfo->draggable = draggable;
1792 stopinfo->gradient = gradient;
1793 stopinfo->vector = vector;
1794 // check if already present in list. (e.g. when both R1 and R2 were selected)
1795 bool present = false;
1796 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1797 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1798 present = true;
1799 break; // no need to search further.
1800 }
1801 }
1802 if (!present)
1803 endstoplist = g_slist_append(endstoplist, stopinfo);
1804 }
1805 }
1806 break;
1807 default:
1808 break;
1809 }
1810 }
1811 selected = g_list_remove(selected, dragger);
1812 if ( just_one ) break; // iterate once if just_one is set.
1813 }
1814 while (midstoplist) {
1815 SPStop *stop = (SPStop*) midstoplist->data;
1816 document = SP_OBJECT_DOCUMENT (stop);
1817 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1818 parent->removeChild(SP_OBJECT_REPR(stop));
1819 midstoplist = g_slist_remove(midstoplist, stop);
1820 }
1821 while (endstoplist) {
1822 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
1823 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1825 // 2 is the minimum, cannot delete more than that without deleting the whole vector
1826 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1827 // manually count the children, don't know if there already exists a function for this...
1828 int len = 0;
1829 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1830 child != NULL ;
1831 child = SP_OBJECT_NEXT(child) )
1832 {
1833 if ( SP_IS_STOP(child) ) len ++;
1834 }
1835 if (len > 2)
1836 {
1837 switch (stopinfo->draggable->point_type) {
1838 case POINT_LG_BEGIN:
1839 {
1840 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1842 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1843 NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1844 NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1845 SPStop *stop = sp_first_stop(stopinfo->vector);
1846 gdouble offset = stop->offset;
1847 NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1848 lg->x1.computed = newbegin[NR::X];
1849 lg->y1.computed = newbegin[NR::Y];
1851 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1852 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1853 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1854 stop->offset = 0;
1855 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1857 // iterate through midstops to set new offset values such that they won't move on canvas.
1858 SPStop *laststop = sp_last_stop(stopinfo->vector);
1859 stop = sp_next_stop(stop);
1860 while ( stop != laststop ) {
1861 stop->offset = (stop->offset - offset)/(1 - offset);
1862 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1863 stop = sp_next_stop(stop);
1864 }
1865 }
1866 break;
1867 case POINT_LG_END:
1868 {
1869 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1871 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1872 NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1873 NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1874 SPStop *laststop = sp_last_stop(stopinfo->vector);
1875 gdouble offset = laststop->offset;
1876 NR::Point newend = begin + offset * (oldend - begin);
1877 lg->x2.computed = newend[NR::X];
1878 lg->y2.computed = newend[NR::Y];
1880 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1881 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1882 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1883 laststop->offset = 1;
1884 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1886 // iterate through midstops to set new offset values such that they won't move on canvas.
1887 SPStop *stop = sp_first_stop(stopinfo->vector);
1888 stop = sp_next_stop(stop);
1889 while ( stop != laststop ) {
1890 stop->offset = stop->offset / offset;
1891 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1892 stop = sp_next_stop(stop);
1893 }
1894 }
1895 break;
1896 case POINT_RG_CENTER:
1897 {
1898 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1899 if (newfirst) {
1900 newfirst->offset = 0;
1901 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1902 }
1903 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1904 }
1905 break;
1906 case POINT_RG_R1:
1907 case POINT_RG_R2:
1908 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1910 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1911 double oldradius = rg->r.computed;
1912 SPStop *laststop = sp_last_stop(stopinfo->vector);
1913 gdouble offset = laststop->offset;
1914 double newradius = offset * oldradius;
1915 rg->r.computed = newradius;
1917 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1918 sp_repr_set_svg_double(repr, "r", rg->r.computed);
1919 laststop->offset = 1;
1920 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1922 // iterate through midstops to set new offset values such that they won't move on canvas.
1923 SPStop *stop = sp_first_stop(stopinfo->vector);
1924 stop = sp_next_stop(stop);
1925 while ( stop != laststop ) {
1926 stop->offset = stop->offset / offset;
1927 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1928 stop = sp_next_stop(stop);
1929 }
1930 break;
1931 }
1932 }
1933 else
1934 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
1935 SPCSSAttr *css = sp_repr_css_attr_new ();
1937 // stopinfo->spstop is the selected stop
1938 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
1939 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
1940 unselectedrepr = unselectedrepr->next();
1941 }
1943 if (unselectedrepr == NULL) {
1944 if (stopinfo->draggable->fill_or_stroke) {
1945 sp_repr_css_unset_property (css, "fill");
1946 } else {
1947 sp_repr_css_unset_property (css, "stroke");
1948 }
1949 } else {
1950 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
1951 if (stopinfo->draggable->fill_or_stroke) {
1952 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1953 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1954 } else {
1955 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1956 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1957 }
1958 sp_repr_css_attr_unref (stopcss);
1959 }
1961 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1962 sp_repr_css_attr_unref (css);
1963 }
1965 endstoplist = g_slist_remove(endstoplist, stopinfo);
1966 delete stopinfo;
1967 }
1969 if (document) {
1970 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1971 }
1972 }
1975 /*
1976 Local Variables:
1977 mode:c++
1978 c-file-style:"stroustrup"
1979 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1980 indent-tabs-mode:nil
1981 fill-column:99
1982 End:
1983 */
1984 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :