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>
21 #include <cstring>
22 #include <string>
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-style.h"
28 #include "document.h"
29 #include "display/sp-ctrlline.h"
30 #include "xml/repr.h"
31 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
33 #include "libnr/nr-point-fns.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"
43 #include "snap.h"
44 #include "sp-namedview.h"
45 #include "selection-chemistry.h"
48 #define GR_KNOT_COLOR_NORMAL 0xffffff00
49 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
50 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
52 #define GR_LINE_COLOR_FILL 0x0000ff7f
53 #define GR_LINE_COLOR_STROKE 0x9999007f
55 // screen pixels between knots when they snap:
56 #define SNAP_DIST 5
58 // absolute distance between gradient points for them to become a single dragger when the drag is created:
59 #define MERGE_DIST 0.1
61 // knot shapes corresponding to GrPointType enum
62 SPKnotShapeType gr_knot_shapes [] = {
63 SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
64 SP_KNOT_SHAPE_CIRCLE, //POINT_LG_END
65 SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
66 SP_KNOT_SHAPE_SQUARE, // POINT_RG_CENTER
67 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R1
68 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R2
69 SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
70 SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
71 SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
72 };
74 const gchar *gr_knot_descr [] = {
75 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
76 N_("Linear gradient <b>end</b>"),
77 N_("Linear gradient <b>mid stop</b>"),
78 N_("Radial gradient <b>center</b>"),
79 N_("Radial gradient <b>radius</b>"),
80 N_("Radial gradient <b>radius</b>"),
81 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
82 N_("Radial gradient <b>mid stop</b>"),
83 N_("Radial gradient <b>mid stop</b>")
84 };
86 static void
87 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
88 {
89 GrDrag *drag = (GrDrag *) data;
90 drag->updateDraggers ();
91 drag->updateLines ();
92 drag->updateLevels ();
93 }
95 static void
96 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
97 {
98 GrDrag *drag = (GrDrag *) data;
99 if (drag->local_change) {
100 drag->local_change = false;
101 } else {
102 drag->updateDraggers ();
103 }
104 drag->updateLines ();
105 drag->updateLevels ();
106 }
108 /**
109 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
110 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
111 any.
112 */
113 int
114 gr_drag_style_query (SPStyle *style, int property, gpointer data)
115 {
116 GrDrag *drag = (GrDrag *) data;
118 if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
119 return QUERY_STYLE_NOTHING;
120 }
122 if (!drag->selected) {
123 return QUERY_STYLE_NOTHING;
124 } else {
125 int ret = QUERY_STYLE_NOTHING;
127 float cf[4];
128 cf[0] = cf[1] = cf[2] = cf[3] = 0;
130 int count = 0;
132 for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
133 GrDragger *d = (GrDragger *) i->data;
134 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
135 GrDraggable *draggable = (GrDraggable *) j->data;
137 if (ret == QUERY_STYLE_NOTHING) {
138 ret = QUERY_STYLE_SINGLE;
139 } else if (ret == QUERY_STYLE_SINGLE) {
140 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
141 }
143 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
144 cf[0] += SP_RGBA32_R_F (c);
145 cf[1] += SP_RGBA32_G_F (c);
146 cf[2] += SP_RGBA32_B_F (c);
147 cf[3] += SP_RGBA32_A_F (c);
149 count ++;
150 }
151 }
153 if (count) {
154 cf[0] /= count;
155 cf[1] /= count;
156 cf[2] /= count;
157 cf[3] /= count;
159 // set both fill and stroke with our stop-color and stop-opacity
160 style->fill.clear();
161 style->fill.setColor( cf[0], cf[1], cf[2] );
162 style->fill.set = TRUE;
163 style->stroke.clear();
164 style->stroke.setColor( cf[0], cf[1], cf[2] );
165 style->stroke.set = TRUE;
167 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
168 style->fill_opacity.set = TRUE;
169 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
170 style->stroke_opacity.set = TRUE;
172 style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
173 style->opacity.set = TRUE;
174 }
176 return ret;
177 }
178 }
180 bool
181 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
182 {
183 GrDrag *drag = (GrDrag *) data;
185 if (!drag->selected)
186 return false;
188 SPCSSAttr *stop = sp_repr_css_attr_new ();
190 // See if the css contains interesting properties, and if so, translate them into the format
191 // acceptable for gradient stops
193 // any of color properties, in order of increasing priority:
194 if (css->attribute("flood-color"))
195 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
197 if (css->attribute("lighting-color"))
198 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
200 if (css->attribute("color"))
201 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
203 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
204 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
206 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
207 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
209 if (css->attribute("stop-color"))
210 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
213 if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
214 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
215 } else { // multiply all opacity properties:
216 gdouble accumulated = 1.0;
217 accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
218 accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
219 accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
220 accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
222 Inkscape::CSSOStringStream os;
223 os << accumulated;
224 sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
226 if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
227 (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
228 sp_repr_css_set_property (stop, "stop-opacity", "0"); // if a single fill/stroke property is set to none, don't change color, set opacity to 0
229 }
231 if (!stop->attributeList()) { // nothing for us here, pass it on
232 sp_repr_css_attr_unref(stop);
233 return false;
234 }
236 for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
237 GrDragger* dragger = (GrDragger*) sel->data;
238 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
239 GrDraggable *draggable = (GrDraggable *) i->data;
241 drag->local_change = true;
242 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
243 }
244 }
246 //sp_repr_css_print(stop);
247 sp_repr_css_attr_unref(stop);
248 return true;
249 }
251 bool
252 GrDrag::copy()
253 {
254 if (!selected)
255 return false;
257 float cf[4];
258 cf[0] = cf[1] = cf[2] = cf[3] = 0;
260 int count = 0;
262 for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
263 GrDragger *d = (GrDragger *) i->data;
264 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
265 GrDraggable *draggable = (GrDraggable *) j->data;
267 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
268 cf[0] += SP_RGBA32_R_F (c);
269 cf[1] += SP_RGBA32_G_F (c);
270 cf[2] += SP_RGBA32_B_F (c);
271 cf[3] += SP_RGBA32_A_F (c);
273 count ++;
274 }
275 }
277 if (count) {
278 cf[0] /= count;
279 cf[1] /= count;
280 cf[2] /= count;
281 cf[3] /= count;
282 }
284 guint32 const c32 = SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
285 gchar c[64];
287 SPCSSAttr *css = sp_repr_css_attr_new ();
288 g_snprintf(c, 64, "#%06x", c32 >> 8);
289 sp_repr_css_set_property (css, "fill", c);
290 Inkscape::CSSOStringStream os;
291 os << cf[3];
292 sp_repr_css_set_property (css, "opacity", os.str().c_str());
293 sp_set_style_clipboard (css);
295 g_snprintf(c, 64, "%06x%02x", c32 >> 8, c32 & 0x000000ff);
296 Glib::ustring text;
297 text += c;
298 if (!text.empty())
299 {
300 Glib::RefPtr<Gtk::Clipboard> refClipboard =
301 Gtk::Clipboard::get();
302 refClipboard->set_text(text);
303 }
305 return true;
306 }
308 SPStop *
309 GrDrag::addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance)
310 {
311 gfloat offset; // type of SPStop.offset = gfloat
312 SPGradient *gradient;
313 bool fill_or_stroke = true;
314 bool r1_knot = false;
316 bool addknot = false;
317 do {
318 gradient = sp_item_gradient (item, fill_or_stroke);
319 if (SP_IS_LINEARGRADIENT(gradient)) {
320 NR::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
321 NR::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
323 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
324 double dist_screen = NR::L2 (mouse_p - nearest);
325 if ( dist_screen < tolerance ) {
326 // add the knot
327 offset = get_offset_between_points(nearest, begin, end);
328 addknot = true;
329 break; // break out of the while loop: add only one knot
330 }
331 } else if (SP_IS_RADIALGRADIENT(gradient)) {
332 NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
333 NR::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
334 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
335 double dist_screen = NR::L2 (mouse_p - nearest);
336 if ( dist_screen < tolerance ) {
337 offset = get_offset_between_points(nearest, begin, end);
338 addknot = true;
339 r1_knot = true;
340 break; // break out of the while loop: add only one knot
341 }
343 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
344 nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
345 dist_screen = NR::L2 (mouse_p - nearest);
346 if ( dist_screen < tolerance ) {
347 offset = get_offset_between_points(nearest, begin, end);
348 addknot = true;
349 r1_knot = false;
350 break; // break out of the while loop: add only one knot
351 }
352 }
353 fill_or_stroke = !fill_or_stroke;
354 } while (!fill_or_stroke && !addknot) ;
356 if (addknot) {
357 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
358 SPStop* prev_stop = sp_first_stop(vector);
359 SPStop* next_stop = sp_next_stop(prev_stop);
360 guint i = 1;
361 while ( (next_stop) && (next_stop->offset < offset) ) {
362 prev_stop = next_stop;
363 next_stop = sp_next_stop(next_stop);
364 i++;
365 }
366 if (!next_stop) {
367 // logical error: the endstop should have offset 1 and should always be more than this offset here
368 return NULL;
369 }
372 SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
373 sp_gradient_ensure_vector (gradient);
374 updateDraggers();
376 return newstop;
377 }
379 return NULL;
380 }
383 bool
384 GrDrag::dropColor(SPItem */*item*/, gchar *c, NR::Point p)
385 {
386 // first, see if we can drop onto one of the existing draggers
387 for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
388 GrDragger *d = (GrDragger *) i->data;
390 if (NR::L2(p - d->point)*desktop->current_zoom() < 5) {
391 SPCSSAttr *stop = sp_repr_css_attr_new ();
392 sp_repr_css_set_property (stop, "stop-color", c);
393 sp_repr_css_set_property (stop, "stop-opacity", "1");
394 for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
395 GrDraggable *draggable = (GrDraggable *) j->data;
396 local_change = true;
397 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
398 }
399 sp_repr_css_attr_unref(stop);
400 return true;
401 }
402 }
404 // now see if we're over line and create a new stop
405 bool over_line = false;
406 SPCtrlLine *line = NULL;
407 if (lines) {
408 for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
409 line = (SPCtrlLine*) l->data;
410 NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
411 double dist_screen = NR::L2 (p - nearest) * desktop->current_zoom();
412 if (line->item && dist_screen < 5) {
413 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
414 if (stop) {
415 SPCSSAttr *css = sp_repr_css_attr_new ();
416 sp_repr_css_set_property (css, "stop-color", c);
417 sp_repr_css_set_property (css, "stop-opacity", "1");
418 sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
419 return true;
420 }
421 }
422 }
423 }
425 return false;
426 }
429 GrDrag::GrDrag(SPDesktop *desktop) {
431 this->desktop = desktop;
433 this->selection = sp_desktop_selection(desktop);
435 this->draggers = NULL;
436 this->lines = NULL;
437 this->selected = NULL;
439 this->hor_levels.clear();
440 this->vert_levels.clear();
442 this->local_change = false;
444 this->sel_changed_connection = this->selection->connectChanged(
445 sigc::bind (
446 sigc::ptr_fun(&gr_drag_sel_changed),
447 (gpointer)this )
449 );
450 this->sel_modified_connection = this->selection->connectModified(
451 sigc::bind(
452 sigc::ptr_fun(&gr_drag_sel_modified),
453 (gpointer)this )
454 );
456 this->style_set_connection = this->desktop->connectSetStyle(
457 sigc::bind(
458 sigc::ptr_fun(&gr_drag_style_set),
459 (gpointer)this )
460 );
462 this->style_query_connection = this->desktop->connectQueryStyle(
463 sigc::bind(
464 sigc::ptr_fun(&gr_drag_style_query),
465 (gpointer)this )
466 );
468 this->updateDraggers ();
469 this->updateLines ();
470 this->updateLevels ();
472 if (desktop->gr_item) {
473 this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
474 }
475 }
477 GrDrag::~GrDrag()
478 {
479 this->sel_changed_connection.disconnect();
480 this->sel_modified_connection.disconnect();
481 this->style_set_connection.disconnect();
482 this->style_query_connection.disconnect();
484 if (this->selected) {
485 GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data;
486 desktop->gr_item = draggable->item;
487 desktop->gr_point_type = draggable->point_type;
488 desktop->gr_point_i = draggable->point_i;
489 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
490 } else {
491 desktop->gr_item = NULL;
492 desktop->gr_point_type = 0;
493 desktop->gr_point_i = 0;
494 desktop->gr_fill_or_stroke = true;
495 }
497 deselect_all();
498 for (GList *l = this->draggers; l != NULL; l = l->next) {
499 delete ((GrDragger *) l->data);
500 }
501 g_list_free (this->draggers);
502 this->draggers = NULL;
503 this->selected = NULL;
505 for (GSList *l = this->lines; l != NULL; l = l->next) {
506 gtk_object_destroy( GTK_OBJECT (l->data));
507 }
508 g_slist_free (this->lines);
509 this->lines = NULL;
510 }
512 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
513 {
514 this->item = item;
515 this->point_type = point_type;
516 this->point_i = point_i;
517 this->fill_or_stroke = fill_or_stroke;
519 g_object_ref (G_OBJECT (this->item));
520 }
522 GrDraggable::~GrDraggable ()
523 {
524 g_object_unref (G_OBJECT (this->item));
525 }
528 SPObject *
529 GrDraggable::getServer ()
530 {
531 if (!item)
532 return NULL;
534 SPObject *server = NULL;
535 if (fill_or_stroke)
536 server = SP_OBJECT_STYLE_FILL_SERVER (item);
537 else
538 server = SP_OBJECT_STYLE_STROKE_SERVER (item);
540 return server;
541 }
543 static void
544 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
545 {
546 GrDragger *dragger = (GrDragger *) data;
547 GrDrag *drag = dragger->parent;
549 NR::Point p = *ppointer;
551 // FIXME: take from prefs
552 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
554 if (state & GDK_SHIFT_MASK) {
555 // with Shift; unsnap if we carry more than one draggable
556 if (dragger->draggables && dragger->draggables->next) {
557 // create a new dragger
558 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
559 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
560 // relink to it all but the first draggable in the list
561 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
562 GrDraggable *draggable = (GrDraggable *) i->data;
563 dr_new->addDraggable (draggable);
564 }
565 dr_new->updateKnotShape();
566 g_slist_free (dragger->draggables->next);
567 dragger->draggables->next = NULL;
568 dragger->updateKnotShape();
569 dragger->updateTip();
570 }
571 } else if (!(state & GDK_CONTROL_MASK)) {
572 // without Shift or Ctrl; see if we need to snap to another dragger
573 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
574 GrDragger *d_new = (GrDragger *) di->data;
575 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
577 // Merge draggers:
578 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
579 GrDraggable *draggable = (GrDraggable *) i->data;
580 // copy draggable to d_new:
581 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
582 d_new->addDraggable (da_new);
583 }
585 // unlink and delete this dragger
586 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
587 delete dragger;
589 // update the new merged dragger
590 d_new->fireDraggables(true, false, true);
591 d_new->parent->updateLines();
592 d_new->parent->setSelected (d_new);
593 d_new->updateKnotShape ();
594 d_new->updateTip ();
595 d_new->updateDependencies(true);
596 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
597 _("Merge gradient handles"));
598 return;
599 }
600 }
601 }
603 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
604 // Try snapping to the grid or guides
605 SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
606 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
607 if (s.getDistance() < 1e6) {
608 p = s.getPoint();
609 sp_knot_moveto (knot, &p);
610 } else {
611 // No snapping so far, let's see if we need to snap to any of the levels
612 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
613 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
614 p[NR::Y] = dragger->parent->hor_levels[i];
615 sp_knot_moveto (knot, &p);
616 }
617 }
618 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
619 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
620 p[NR::X] = dragger->parent->vert_levels[i];
621 sp_knot_moveto (knot, &p);
622 }
623 }
624 }
625 }
627 if (state & GDK_CONTROL_MASK) {
628 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
629 /* 0 means no snapping. */
631 // This list will store snap vectors from all draggables of dragger
632 GSList *snap_vectors = NULL;
634 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
635 GrDraggable *draggable = (GrDraggable *) i->data;
637 NR::Point *dr_snap = NULL;
639 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
640 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
641 GrDragger *d_new = (GrDragger *) di->data;
642 if (d_new == dragger)
643 continue;
644 if (d_new->isA (draggable->item,
645 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
646 draggable->fill_or_stroke)) {
647 // found the other end of the linear gradient;
648 if (state & GDK_SHIFT_MASK) {
649 // moving linear around center
650 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
651 dr_snap = ¢er;
652 } else {
653 // moving linear around the other end
654 dr_snap = &d_new->point;
655 }
656 }
657 }
658 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
659 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
660 GrDragger *d_new = (GrDragger *) di->data;
661 if (d_new == dragger)
662 continue;
663 if (d_new->isA (draggable->item,
664 POINT_RG_CENTER,
665 draggable->fill_or_stroke)) {
666 // found the center of the radial gradient;
667 dr_snap = &(d_new->point);
668 }
669 }
670 } else if (draggable->point_type == POINT_RG_CENTER) {
671 // radial center snaps to hor/vert relative to its original position
672 dr_snap = &(dragger->point_original);
673 }
675 NR::Point *snap_vector = NULL;
676 if (dr_snap) {
677 if (state & GDK_MOD1_MASK) {
678 // with Alt, snap to the original angle and its perpendiculars
679 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
680 } else {
681 // with Ctrl, snap to M_PI/snaps
682 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
683 }
684 }
685 if (snap_vector) {
686 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
687 }
688 }
690 // Move by the smallest of snap vectors:
691 NR::Point move(9999, 9999);
692 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
693 NR::Point *snap_vector = (NR::Point *) i->data;
694 if (NR::L2(*snap_vector) < NR::L2(move))
695 move = *snap_vector;
696 }
697 if (move[NR::X] < 9999) {
698 p += move;
699 sp_knot_moveto (knot, &p);
700 }
702 g_slist_free(snap_vectors);
703 }
705 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
706 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
708 if (drag->keep_selection) {
709 NR::Point diff = p - dragger->point;
710 drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
711 } else {
712 dragger->point = p;
713 dragger->fireDraggables (false, scale_radial);
714 dragger->updateDependencies(false);
715 }
717 }
720 static void
721 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
722 {
724 GrDrag *drag = dragger->parent;
725 // a midpoint dragger can (logically) only contain one GrDraggable
726 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
728 // get begin and end points between which dragging is allowed:
729 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
730 *moving = g_slist_append(*moving, dragger);
732 guint lowest_i = draggable->point_i;
733 guint highest_i = draggable->point_i;
734 GrDragger *lowest_dragger = dragger;
735 GrDragger *highest_dragger = dragger;
736 if (dragger->isSelected()) {
737 GrDragger* d_add;
738 while ( true )
739 {
740 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
741 if ( d_add && g_list_find(drag->selected, d_add) ) {
742 lowest_i = lowest_i - 1;
743 *moving = g_slist_prepend(*moving, d_add);
744 lowest_dragger = d_add;
745 } else {
746 break;
747 }
748 }
750 while ( true )
751 {
752 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
753 if ( d_add && g_list_find(drag->selected, d_add) ) {
754 highest_i = highest_i + 1;
755 *moving = g_slist_append(*moving, d_add);
756 highest_dragger = d_add;
757 } else {
758 break;
759 }
760 }
761 }
763 if ( SP_IS_LINEARGRADIENT(server) ) {
764 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
765 GrDragger *d_temp;
766 if (lowest_i == 1) {
767 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
768 } else {
769 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
770 }
771 if (d_temp)
772 *begin = d_temp->point;
774 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
775 if (d_temp == NULL) {
776 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
777 }
778 if (d_temp)
779 *end = d_temp->point;
780 } else if ( SP_IS_RADIALGRADIENT(server) ) {
781 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
782 GrDragger *d_temp;
783 if (lowest_i == 1) {
784 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
785 } else {
786 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
787 }
788 if (d_temp)
789 *begin = d_temp->point;
791 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
792 if (d_temp == NULL) {
793 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
794 }
795 if (d_temp)
796 *end = d_temp->point;
797 }
799 *low_lim = dragger->point - (lowest_dragger->point - *begin);
800 *high_lim = dragger->point - (highest_dragger->point - *end);
801 }
805 /**
806 Called when a midpoint knot is dragged.
807 */
808 static void
809 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, NR::Point const *ppointer, guint state, gpointer data)
810 {
811 GrDragger *dragger = (GrDragger *) data;
812 GrDrag *drag = dragger->parent;
813 // a midpoint dragger can (logically) only contain one GrDraggable
814 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
816 // FIXME: take from prefs
817 double snap_fraction = 0.1;
819 NR::Point p = *ppointer;
820 NR::Point begin(0,0), end(0,0);
821 NR::Point low_lim(0,0), high_lim(0,0);
823 SPObject *server = draggable->getServer();
825 GSList *moving = NULL;
826 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
828 if (state & GDK_CONTROL_MASK) {
829 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
830 } else {
831 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
832 }
833 NR::Point displacement = p - dragger->point;
835 for (GSList const* i = moving; i != NULL; i = i->next) {
836 GrDragger *drg = (GrDragger*) i->data;
837 SPKnot *drgknot = drg->knot;
838 NR::Point this_move = displacement;
839 if (state & GDK_MOD1_MASK) {
840 // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
841 double alpha = 1.0;
842 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
843 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
844 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
845 } else { // drg is on the begin side from dragger
846 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
847 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
848 }
849 }
850 drg->point += this_move;
851 sp_knot_moveto (drgknot, & drg->point);
852 drg->fireDraggables (false);
853 drg->updateDependencies(false);
854 }
856 g_slist_free(moving);
858 drag->keep_selection = dragger->isSelected();
859 }
863 static void
864 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
865 {
866 GrDragger *dragger = (GrDragger *) data;
868 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
869 }
871 /**
872 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
873 */
874 static void
875 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
876 {
877 GrDragger *dragger = (GrDragger *) data;
879 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
881 dragger->point_original = dragger->point = knot->pos;
883 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
884 dragger->fireDraggables (true, true);
885 } else {
886 dragger->fireDraggables (true);
887 }
889 for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
890 GrDragger *d = (GrDragger *) i->data;
891 if (d == dragger)
892 continue;
893 d->fireDraggables (true);
894 }
896 // make this dragger selected
897 if (!dragger->parent->keep_selection) {
898 dragger->parent->setSelected (dragger);
899 }
900 dragger->parent->keep_selection = false;
902 dragger->updateDependencies(true);
904 // we did an undoable action
905 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
906 _("Move gradient handle"));
907 }
909 /**
910 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
911 state of the keyboard keys
912 */
913 static void
914 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
915 {
916 GrDragger *dragger = (GrDragger *) data;
917 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
918 if (!draggable) return;
920 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
921 // delete this knot from vector
922 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
923 gradient = sp_gradient_get_vector (gradient, false);
924 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
925 SPStop *stop = NULL;
926 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
927 case POINT_LG_BEGIN:
928 case POINT_RG_CENTER:
929 stop = sp_first_stop(gradient);
930 {
931 SPStop *next = sp_next_stop (stop);
932 if (next) {
933 next->offset = 0;
934 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
935 }
936 }
937 break;
938 case POINT_LG_END:
939 case POINT_RG_R1:
940 case POINT_RG_R2:
941 stop = sp_last_stop(gradient);
942 {
943 SPStop *prev = sp_prev_stop (stop, gradient);
944 if (prev) {
945 prev->offset = 1;
946 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
947 }
948 }
949 break;
950 case POINT_LG_MID:
951 case POINT_RG_MID1:
952 case POINT_RG_MID2:
953 stop = sp_get_stop_i(gradient, draggable->point_i);
954 break;
955 }
957 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
958 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
959 _("Delete gradient stop"));
960 }
961 } else {
962 // select the dragger
963 dragger->point_original = dragger->point;
965 if ( state & GDK_SHIFT_MASK ) {
966 dragger->parent->setSelected (dragger, true, false);
967 } else {
968 dragger->parent->setSelected (dragger);
969 }
970 }
971 }
973 /**
974 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
975 */
976 static void
977 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
978 {
979 GrDragger *dragger = (GrDragger *) data;
981 dragger->point_original = dragger->point;
983 if (dragger->draggables == NULL)
984 return;
986 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
987 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
988 }
990 /**
991 Act upon all draggables of the dragger, setting them to the dragger's point
992 */
993 void
994 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
995 {
996 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
997 GrDraggable *draggable = (GrDraggable *) i->data;
999 // set local_change flag so that selection_changed callback does not regenerate draggers
1000 this->parent->local_change = true;
1002 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1003 // to the center, unless it's the first update upon merge when we must snap it to the point
1004 if (merging_focus ||
1005 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1006 {
1007 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1008 }
1009 }
1010 }
1012 /**
1013 Checks if the dragger has a draggable with this point_type
1014 */
1015 bool
1016 GrDragger::isA (gint point_type)
1017 {
1018 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1019 GrDraggable *draggable = (GrDraggable *) i->data;
1020 if (draggable->point_type == point_type) {
1021 return true;
1022 }
1023 }
1024 return false;
1025 }
1027 /**
1028 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1029 */
1030 bool
1031 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1032 {
1033 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1034 GrDraggable *draggable = (GrDraggable *) i->data;
1035 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1036 return true;
1037 }
1038 }
1039 return false;
1040 }
1042 /**
1043 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1044 */
1045 bool
1046 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1047 {
1048 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1049 GrDraggable *draggable = (GrDraggable *) i->data;
1050 if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1051 return true;
1052 }
1053 }
1054 return false;
1055 }
1057 bool
1058 GrDraggable::mayMerge (GrDraggable *da2)
1059 {
1060 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1061 // we must not merge the points of the same gradient!
1062 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1063 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1064 // except that we can snap center and focus together
1065 return false;
1066 }
1067 }
1068 // disable merging of midpoints.
1069 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1070 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1071 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1072 return false;
1074 return true;
1075 }
1077 bool
1078 GrDragger::mayMerge (GrDragger *other)
1079 {
1080 if (this == other)
1081 return false;
1083 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1084 GrDraggable *da1 = (GrDraggable *) i->data;
1085 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1086 GrDraggable *da2 = (GrDraggable *) j->data;
1087 if (!da1->mayMerge(da2))
1088 return false;
1089 }
1090 }
1091 return true;
1092 }
1094 bool
1095 GrDragger::mayMerge (GrDraggable *da2)
1096 {
1097 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1098 GrDraggable *da1 = (GrDraggable *) i->data;
1099 if (!da1->mayMerge(da2))
1100 return false;
1101 }
1102 return true;
1103 }
1105 /**
1106 Updates the statusbar tip of the dragger knot, based on its draggables
1107 */
1108 void
1109 GrDragger::updateTip ()
1110 {
1111 if (this->knot && this->knot->tip) {
1112 g_free (this->knot->tip);
1113 this->knot->tip = NULL;
1114 }
1116 if (g_slist_length (this->draggables) == 1) {
1117 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1118 char *item_desc = sp_item_description(draggable->item);
1119 switch (draggable->point_type) {
1120 case POINT_LG_MID:
1121 case POINT_RG_MID1:
1122 case POINT_RG_MID2:
1123 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"),
1124 _(gr_knot_descr[draggable->point_type]),
1125 draggable->point_i,
1126 item_desc,
1127 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1128 break;
1130 default:
1131 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"),
1132 _(gr_knot_descr[draggable->point_type]),
1133 item_desc,
1134 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1135 break;
1136 }
1137 g_free(item_desc);
1138 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1139 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1140 } else {
1141 int length = g_slist_length (this->draggables);
1142 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1143 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1144 length),
1145 length);
1146 }
1147 }
1149 /**
1150 Adds a draggable to the dragger
1151 */
1152 void
1153 GrDragger::updateKnotShape ()
1154 {
1155 if (!draggables)
1156 return;
1157 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1158 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1159 }
1161 /**
1162 Adds a draggable to the dragger
1163 */
1164 void
1165 GrDragger::addDraggable (GrDraggable *draggable)
1166 {
1167 this->draggables = g_slist_prepend (this->draggables, draggable);
1169 this->updateTip();
1170 }
1173 /**
1174 Moves this dragger to the point of the given draggable, acting upon all other draggables
1175 */
1176 void
1177 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1178 {
1179 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1180 this->point_original = this->point;
1182 sp_knot_moveto (this->knot, &(this->point));
1184 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1185 GrDraggable *da = (GrDraggable *) i->data;
1186 if ( (da->item == item) &&
1187 (point_type == -1 || da->point_type == point_type) &&
1188 (point_i == -1 || da->point_i == point_i) &&
1189 (da->fill_or_stroke == fill_or_stroke) ) {
1190 continue;
1191 }
1192 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1193 }
1194 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1195 }
1198 /**
1199 Moves all midstop draggables that depend on this one
1200 */
1201 void
1202 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1203 SPObject *server = draggable->getServer();
1204 if (!server)
1205 return;
1206 guint num = SP_GRADIENT(server)->vector.stops.size();
1207 if (num <= 2) return;
1209 if ( SP_IS_LINEARGRADIENT(server) ) {
1210 for ( guint i = 1; i < num - 1; i++ ) {
1211 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1212 }
1213 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1214 for ( guint i = 1; i < num - 1; i++ ) {
1215 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1216 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1217 }
1218 }
1219 }
1222 /**
1223 Moves all draggables that depend on this one
1224 */
1225 void
1226 GrDragger::updateDependencies (bool write_repr)
1227 {
1228 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1229 GrDraggable *draggable = (GrDraggable *) i->data;
1230 switch (draggable->point_type) {
1231 case POINT_LG_BEGIN:
1232 {
1233 // the end point is dependent only when dragging with ctrl+shift
1234 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1236 this->updateMidstopDependencies (draggable, write_repr);
1237 }
1238 break;
1239 case POINT_LG_END:
1240 {
1241 // the begin point is dependent only when dragging with ctrl+shift
1242 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1244 this->updateMidstopDependencies (draggable, write_repr);
1245 }
1246 break;
1247 case POINT_LG_MID:
1248 // no other nodes depend on mid points.
1249 break;
1250 case POINT_RG_R2:
1251 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1252 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1253 this->updateMidstopDependencies (draggable, write_repr);
1254 break;
1255 case POINT_RG_R1:
1256 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1257 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1258 this->updateMidstopDependencies (draggable, write_repr);
1259 break;
1260 case POINT_RG_CENTER:
1261 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1262 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1263 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1264 this->updateMidstopDependencies (draggable, write_repr);
1265 break;
1266 case POINT_RG_FOCUS:
1267 // nothing can depend on that
1268 break;
1269 case POINT_RG_MID1:
1270 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1271 break;
1272 case POINT_RG_MID2:
1273 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1274 break;
1275 default:
1276 break;
1277 }
1278 }
1279 }
1283 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1284 {
1285 this->draggables = NULL;
1287 this->parent = parent;
1289 this->point = p;
1290 this->point_original = p;
1292 // create the knot
1293 this->knot = sp_knot_new (parent->desktop, NULL);
1294 this->knot->setMode(SP_KNOT_MODE_XOR);
1295 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1296 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1297 sp_knot_update_ctrl(this->knot);
1299 // move knot to the given point
1300 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1301 sp_knot_show (this->knot);
1303 // connect knot's signals
1304 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1305 // luckily, midstops never snap to other nodes so are never unsnapped...
1306 && ( (draggable->point_type == POINT_LG_MID)
1307 || (draggable->point_type == POINT_RG_MID1)
1308 || (draggable->point_type == POINT_RG_MID2) ) )
1309 {
1310 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1311 } else {
1312 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1313 }
1314 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1315 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1316 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1317 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1319 // add the initial draggable
1320 if (draggable)
1321 this->addDraggable (draggable);
1322 updateKnotShape();
1323 }
1325 GrDragger::~GrDragger ()
1326 {
1327 // unselect if it was selected
1328 this->parent->setDeselected(this);
1330 // disconnect signals
1331 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1332 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1333 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1334 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1335 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1337 /* unref should call destroy */
1338 g_object_unref (G_OBJECT (this->knot));
1340 // delete all draggables
1341 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1342 delete ((GrDraggable *) i->data);
1343 }
1344 g_slist_free (this->draggables);
1345 this->draggables = NULL;
1346 }
1348 /**
1349 Select the dragger which has the given draggable.
1350 */
1351 GrDragger *
1352 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1353 {
1354 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1355 GrDragger *dragger = (GrDragger *) i->data;
1356 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1357 GrDraggable *da2 = (GrDraggable *) j->data;
1358 if ( (da2->item == item) &&
1359 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1360 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1361 (da2->fill_or_stroke == fill_or_stroke)) {
1362 return (dragger);
1363 }
1364 }
1365 }
1366 return NULL;
1367 }
1370 void
1371 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1372 {
1373 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1374 if (d && d != this) {
1375 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1376 }
1377 }
1380 /**
1381 Draw this dragger as selected
1382 */
1383 void
1384 GrDragger::select()
1385 {
1386 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1387 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1388 }
1390 /**
1391 Draw this dragger as normal (deselected)
1392 */
1393 void
1394 GrDragger::deselect()
1395 {
1396 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1397 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1398 }
1400 bool
1401 GrDragger::isSelected()
1402 {
1403 return g_list_find (parent->selected, this);
1404 }
1406 /**
1407 \brief Deselect all stops/draggers (private)
1408 */
1409 void
1410 GrDrag::deselect_all()
1411 {
1412 while (selected) {
1413 ( (GrDragger*) selected->data)->deselect();
1414 selected = g_list_remove(selected, selected->data);
1415 }
1416 }
1418 /**
1419 \brief Deselect all stops/draggers (public; emits signal)
1420 */
1421 void
1422 GrDrag::deselectAll()
1423 {
1424 deselect_all();
1425 this->desktop->emitToolSubselectionChanged(NULL);
1426 }
1428 /**
1429 \brief Select all stops/draggers
1430 */
1431 void
1432 GrDrag::selectAll()
1433 {
1434 for (GList *l = this->draggers; l != NULL; l = l->next) {
1435 GrDragger *d = ((GrDragger *) l->data);
1436 setSelected (d, true, true);
1437 }
1438 }
1440 /**
1441 \brief Select all stops/draggers that match the coords
1442 */
1443 void
1444 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1445 {
1446 for (GList *l = this->draggers; l != NULL; l = l->next) {
1447 GrDragger *d = ((GrDragger *) l->data);
1448 for (guint k = 0; k < coords.size(); k++) {
1449 if (NR::L2 (d->point - coords[k]) < 1e-4) {
1450 setSelected (d, true, true);
1451 }
1452 }
1453 }
1454 }
1457 /**
1458 \brief Select all stops/draggers that fall within the rect
1459 */
1460 void
1461 GrDrag::selectRect(NR::Rect const &r)
1462 {
1463 for (GList *l = this->draggers; l != NULL; l = l->next) {
1464 GrDragger *d = ((GrDragger *) l->data);
1465 if (r.contains(d->point)) {
1466 setSelected (d, true, true);
1467 }
1468 }
1469 }
1471 /**
1472 \brief Select a dragger
1473 \param dragger The dragger to select
1474 \param add_to_selection If true, add to selection, otherwise deselect others
1475 \param override If true, always select this node, otherwise toggle selected status
1476 */
1477 void
1478 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1479 {
1480 GrDragger *seldragger = NULL;
1482 if (add_to_selection) {
1483 if (!dragger) return;
1484 if (override) {
1485 if (!g_list_find(selected, dragger)) {
1486 selected = g_list_prepend(selected, dragger);
1487 }
1488 dragger->select();
1489 seldragger = dragger;
1490 } else { // toggle
1491 if (g_list_find(selected, dragger)) {
1492 selected = g_list_remove(selected, dragger);
1493 dragger->deselect();
1494 if (selected) {
1495 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1496 }
1497 } else {
1498 selected = g_list_prepend(selected, dragger);
1499 dragger->select();
1500 seldragger = dragger;
1501 }
1502 }
1503 } else {
1504 deselect_all();
1505 if (dragger) {
1506 selected = g_list_prepend(selected, dragger);
1507 dragger->select();
1508 seldragger = dragger;
1509 }
1510 }
1511 if (seldragger) {
1512 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1513 }
1514 }
1516 /**
1517 \brief Deselect a dragger
1518 \param dragger The dragger to deselect
1519 */
1520 void
1521 GrDrag::setDeselected (GrDragger *dragger)
1522 {
1523 if (g_list_find(selected, dragger)) {
1524 selected = g_list_remove(selected, dragger);
1525 dragger->deselect();
1526 }
1527 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1528 }
1532 /**
1533 Create a line from p1 to p2 and add it to the lines list
1534 */
1535 void
1536 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1537 {
1538 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1539 SP_TYPE_CTRLLINE, NULL);
1540 SP_CTRLLINE(line)->item = item;
1541 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1542 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1543 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1544 sp_canvas_item_show (line);
1545 this->lines = g_slist_append (this->lines, line);
1546 }
1548 /**
1549 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1550 new dragger and add it to draggers list
1551 */
1552 void
1553 GrDrag::addDragger (GrDraggable *draggable)
1554 {
1555 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1557 for (GList *i = this->draggers; i != NULL; i = i->next) {
1558 GrDragger *dragger = (GrDragger *) i->data;
1559 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1560 // distance is small, merge this draggable into dragger, no need to create new dragger
1561 dragger->addDraggable (draggable);
1562 dragger->updateKnotShape();
1563 return;
1564 }
1565 }
1567 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1568 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1569 this->draggers = g_list_append (this->draggers, new_dragger);
1570 }
1572 /**
1573 Add draggers for the radial gradient rg on item
1574 */
1575 void
1576 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1577 {
1578 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1579 guint num = rg->vector.stops.size();
1580 if (num > 2) {
1581 for ( guint i = 1; i < num - 1; i++ ) {
1582 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1583 }
1584 }
1585 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1586 if (num > 2) {
1587 for ( guint i = 1; i < num - 1; i++ ) {
1588 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1589 }
1590 }
1591 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1592 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1593 }
1595 /**
1596 Add draggers for the linear gradient lg on item
1597 */
1598 void
1599 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1600 {
1601 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1602 guint num = lg->vector.stops.size();
1603 if (num > 2) {
1604 for ( guint i = 1; i < num - 1; i++ ) {
1605 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1606 }
1607 }
1608 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1609 }
1611 /**
1612 Artificially grab the knot of this dragger; used by the gradient context
1613 */
1614 void
1615 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1616 {
1617 if (dragger) {
1618 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1619 }
1620 }
1622 /**
1623 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1624 */
1625 void
1626 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1627 {
1628 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1629 if (dragger) {
1630 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1631 }
1632 }
1634 /**
1635 Regenerates the draggers list from the current selection; is called when selection is changed or
1636 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1637 */
1638 void
1639 GrDrag::updateDraggers ()
1640 {
1641 while (selected) {
1642 selected = g_list_remove(selected, selected->data);
1643 }
1644 // delete old draggers
1645 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1646 delete ((GrDragger *) i->data);
1647 }
1648 g_list_free (this->draggers);
1649 this->draggers = NULL;
1651 g_return_if_fail (this->selection != NULL);
1653 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1655 SPItem *item = SP_ITEM(i->data);
1656 SPStyle *style = SP_OBJECT_STYLE (item);
1658 if (style && (style->fill.isPaintserver())) {
1659 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1660 if (SP_IS_LINEARGRADIENT (server)) {
1661 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1662 } else if (SP_IS_RADIALGRADIENT (server)) {
1663 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1664 }
1665 }
1667 if (style && (style->stroke.isPaintserver())) {
1668 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1669 if (SP_IS_LINEARGRADIENT (server)) {
1670 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1671 } else if (SP_IS_RADIALGRADIENT (server)) {
1672 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1673 }
1674 }
1675 }
1676 }
1678 /**
1679 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1680 lines are always in sync with the actual gradient
1681 */
1682 void
1683 GrDrag::updateLines ()
1684 {
1685 // delete old lines
1686 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1687 gtk_object_destroy( GTK_OBJECT (i->data));
1688 }
1689 g_slist_free (this->lines);
1690 this->lines = NULL;
1692 g_return_if_fail (this->selection != NULL);
1694 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1696 SPItem *item = SP_ITEM(i->data);
1698 SPStyle *style = SP_OBJECT_STYLE (item);
1700 if (style && (style->fill.isPaintserver())) {
1701 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1702 if (SP_IS_LINEARGRADIENT (server)) {
1703 this->addLine (item, 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);
1704 } else if (SP_IS_RADIALGRADIENT (server)) {
1705 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1706 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1707 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1708 }
1709 }
1711 if (style && (style->stroke.isPaintserver())) {
1712 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1713 if (SP_IS_LINEARGRADIENT (server)) {
1714 this->addLine (item, 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);
1715 } else if (SP_IS_RADIALGRADIENT (server)) {
1716 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1717 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1718 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1719 }
1720 }
1721 }
1722 }
1724 /**
1725 Regenerates the levels list from the current selection
1726 */
1727 void
1728 GrDrag::updateLevels ()
1729 {
1730 hor_levels.clear();
1731 vert_levels.clear();
1733 g_return_if_fail (this->selection != NULL);
1735 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1736 SPItem *item = SP_ITEM(i->data);
1737 NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1738 if (rect) {
1739 // Remember the edges of the bbox and the center axis
1740 hor_levels.push_back(rect->min()[NR::Y]);
1741 hor_levels.push_back(rect->max()[NR::Y]);
1742 hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1743 vert_levels.push_back(rect->min()[NR::X]);
1744 vert_levels.push_back(rect->max()[NR::X]);
1745 vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1746 }
1747 }
1748 }
1750 void
1751 GrDrag::selected_reverse_vector ()
1752 {
1753 if (selected == NULL)
1754 return;
1756 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1757 GrDraggable *draggable = (GrDraggable *) i->data;
1759 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1760 }
1761 }
1763 void
1764 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1765 {
1766 selected_move (x, y, false, scale_radial);
1767 }
1769 void
1770 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1771 {
1772 if (selected == NULL)
1773 return;
1775 bool did = false;
1777 for (GList *i = selected; i != NULL; i = i->next) {
1778 GrDragger *d = (GrDragger *) i->data;
1780 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1781 // if this is an endpoint,
1783 // Moving an rg center moves its focus and radii as well.
1784 // therefore, if this is a focus or radius and if selection
1785 // contains the center as well, do not move this one
1786 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1787 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1788 bool skip_radius_with_center = false;
1789 for (GList *di = selected; di != NULL; di = di->next) {
1790 GrDragger *d_new = (GrDragger *) di->data;
1791 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1792 POINT_RG_CENTER,
1793 0,
1794 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1795 // FIXME: here we take into account only the first draggable!
1796 skip_radius_with_center = true;
1797 }
1798 }
1799 if (skip_radius_with_center)
1800 continue;
1801 }
1803 did = true;
1804 d->point += NR::Point (x, y);
1805 d->point_original = d->point;
1806 sp_knot_moveto (d->knot, &(d->point));
1808 d->fireDraggables (write_repr, scale_radial);
1810 d->updateDependencies(write_repr);
1811 }
1812 }
1814 if (write_repr && did) {
1815 // we did an undoable action
1816 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1817 _("Move gradient handle(s)"));
1818 return;
1819 }
1821 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1823 GrDragger *dragger = (GrDragger *) selected->data;
1824 // a midpoint dragger can (logically) only contain one GrDraggable
1825 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1827 NR::Point begin(0,0), end(0,0);
1828 NR::Point low_lim(0,0), high_lim(0,0);
1830 SPObject *server = draggable->getServer();
1831 GSList *moving = NULL;
1832 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1834 NR::Point p(x, y);
1835 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1836 NR::Point displacement = p - dragger->point;
1838 for (GSList const* i = moving; i != NULL; i = i->next) {
1839 GrDragger *drg = (GrDragger*) i->data;
1840 SPKnot *drgknot = drg->knot;
1841 drg->point += displacement;
1842 sp_knot_moveto (drgknot, & drg->point);
1843 drg->fireDraggables (true);
1844 drg->updateDependencies(true);
1845 did = true;
1846 }
1848 g_slist_free(moving);
1850 if (write_repr && did) {
1851 // we did an undoable action
1852 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1853 _("Move gradient mid stop(s)"));
1854 }
1855 }
1856 }
1858 void
1859 GrDrag::selected_move_screen (double x, double y)
1860 {
1861 gdouble zoom = desktop->current_zoom();
1862 gdouble zx = x / zoom;
1863 gdouble zy = y / zoom;
1865 selected_move (zx, zy);
1866 }
1868 /**
1869 Select the knot next to the last selected one and deselect all other selected.
1870 */
1871 GrDragger *
1872 GrDrag::select_next ()
1873 {
1874 GrDragger *d = NULL;
1875 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1876 if (draggers)
1877 d = (GrDragger *) draggers->data;
1878 } else {
1879 d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1880 }
1881 if (d)
1882 setSelected (d);
1883 return d;
1884 }
1886 /**
1887 Select the knot previous from the last selected one and deselect all other selected.
1888 */
1889 GrDragger *
1890 GrDrag::select_prev ()
1891 {
1892 GrDragger *d = NULL;
1893 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1894 if (draggers)
1895 d = (GrDragger *) g_list_last (draggers)->data;
1896 } else {
1897 d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1898 }
1899 if (d)
1900 setSelected (d);
1901 return d;
1902 }
1905 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1906 void
1907 GrDrag::deleteSelected (bool just_one)
1908 {
1909 if (!selected) return;
1911 SPDocument *document = false;
1913 struct StructStopInfo {
1914 SPStop * spstop;
1915 GrDraggable * draggable;
1916 SPGradient * gradient;
1917 SPGradient * vector;
1918 };
1920 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1921 GSList *endstoplist = NULL; // list of stops that must be deleted
1922 while (selected) {
1923 GrDragger *dragger = (GrDragger*) selected->data;
1924 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1925 GrDraggable *draggable = (GrDraggable*) drgble->data;
1926 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1927 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1929 switch (draggable->point_type) {
1930 case POINT_LG_MID:
1931 case POINT_RG_MID1:
1932 case POINT_RG_MID2:
1933 {
1934 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1935 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1936 bool present = false;
1937 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1938 if ( (SPStop*)l->data == stop ) {
1939 present = true;
1940 break; // no need to search further.
1941 }
1942 }
1943 if (!present)
1944 midstoplist = g_slist_append(midstoplist, stop);
1945 }
1946 break;
1947 case POINT_LG_BEGIN:
1948 case POINT_LG_END:
1949 case POINT_RG_CENTER:
1950 case POINT_RG_R1:
1951 case POINT_RG_R2:
1952 {
1953 SPStop *stop = NULL;
1954 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1955 stop = sp_first_stop(vector);
1956 } else {
1957 stop = sp_last_stop(vector);
1958 }
1959 if (stop) {
1960 StructStopInfo *stopinfo = new StructStopInfo;
1961 stopinfo->spstop = stop;
1962 stopinfo->draggable = draggable;
1963 stopinfo->gradient = gradient;
1964 stopinfo->vector = vector;
1965 // check if already present in list. (e.g. when both R1 and R2 were selected)
1966 bool present = false;
1967 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1968 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1969 present = true;
1970 break; // no need to search further.
1971 }
1972 }
1973 if (!present)
1974 endstoplist = g_slist_append(endstoplist, stopinfo);
1975 }
1976 }
1977 break;
1978 default:
1979 break;
1980 }
1981 }
1982 selected = g_list_remove(selected, dragger);
1983 if ( just_one ) break; // iterate once if just_one is set.
1984 }
1985 while (midstoplist) {
1986 SPStop *stop = (SPStop*) midstoplist->data;
1987 document = SP_OBJECT_DOCUMENT (stop);
1988 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1989 parent->removeChild(SP_OBJECT_REPR(stop));
1990 midstoplist = g_slist_remove(midstoplist, stop);
1991 }
1992 while (endstoplist) {
1993 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
1994 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1996 // 2 is the minimum, cannot delete more than that without deleting the whole vector
1997 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1998 // manually count the children, don't know if there already exists a function for this...
1999 int len = 0;
2000 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2001 child != NULL ;
2002 child = SP_OBJECT_NEXT(child) )
2003 {
2004 if ( SP_IS_STOP(child) ) len ++;
2005 }
2006 if (len > 2)
2007 {
2008 switch (stopinfo->draggable->point_type) {
2009 case POINT_LG_BEGIN:
2010 {
2011 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2013 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2014 NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
2015 NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
2016 SPStop *stop = sp_first_stop(stopinfo->vector);
2017 gdouble offset = stop->offset;
2018 NR::Point newbegin = oldbegin + offset * (end - oldbegin);
2019 lg->x1.computed = newbegin[NR::X];
2020 lg->y1.computed = newbegin[NR::Y];
2022 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2023 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2024 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2025 stop->offset = 0;
2026 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2028 // iterate through midstops to set new offset values such that they won't move on canvas.
2029 SPStop *laststop = sp_last_stop(stopinfo->vector);
2030 stop = sp_next_stop(stop);
2031 while ( stop != laststop ) {
2032 stop->offset = (stop->offset - offset)/(1 - offset);
2033 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2034 stop = sp_next_stop(stop);
2035 }
2036 }
2037 break;
2038 case POINT_LG_END:
2039 {
2040 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2042 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2043 NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
2044 NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
2045 SPStop *laststop = sp_last_stop(stopinfo->vector);
2046 gdouble offset = laststop->offset;
2047 NR::Point newend = begin + offset * (oldend - begin);
2048 lg->x2.computed = newend[NR::X];
2049 lg->y2.computed = newend[NR::Y];
2051 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2052 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2053 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2054 laststop->offset = 1;
2055 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2057 // iterate through midstops to set new offset values such that they won't move on canvas.
2058 SPStop *stop = sp_first_stop(stopinfo->vector);
2059 stop = sp_next_stop(stop);
2060 while ( stop != laststop ) {
2061 stop->offset = stop->offset / offset;
2062 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2063 stop = sp_next_stop(stop);
2064 }
2065 }
2066 break;
2067 case POINT_RG_CENTER:
2068 {
2069 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2070 if (newfirst) {
2071 newfirst->offset = 0;
2072 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2073 }
2074 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2075 }
2076 break;
2077 case POINT_RG_R1:
2078 case POINT_RG_R2:
2079 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2081 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2082 double oldradius = rg->r.computed;
2083 SPStop *laststop = sp_last_stop(stopinfo->vector);
2084 gdouble offset = laststop->offset;
2085 double newradius = offset * oldradius;
2086 rg->r.computed = newradius;
2088 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2089 sp_repr_set_svg_double(repr, "r", rg->r.computed);
2090 laststop->offset = 1;
2091 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2093 // iterate through midstops to set new offset values such that they won't move on canvas.
2094 SPStop *stop = sp_first_stop(stopinfo->vector);
2095 stop = sp_next_stop(stop);
2096 while ( stop != laststop ) {
2097 stop->offset = stop->offset / offset;
2098 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2099 stop = sp_next_stop(stop);
2100 }
2101 break;
2102 }
2103 }
2104 else
2105 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
2106 SPCSSAttr *css = sp_repr_css_attr_new ();
2108 // stopinfo->spstop is the selected stop
2109 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2110 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2111 unselectedrepr = unselectedrepr->next();
2112 }
2114 if (unselectedrepr == NULL) {
2115 if (stopinfo->draggable->fill_or_stroke) {
2116 sp_repr_css_unset_property (css, "fill");
2117 } else {
2118 sp_repr_css_unset_property (css, "stroke");
2119 }
2120 } else {
2121 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2122 if (stopinfo->draggable->fill_or_stroke) {
2123 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2124 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2125 } else {
2126 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2127 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2128 }
2129 sp_repr_css_attr_unref (stopcss);
2130 }
2132 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2133 sp_repr_css_attr_unref (css);
2134 }
2136 endstoplist = g_slist_remove(endstoplist, stopinfo);
2137 delete stopinfo;
2138 }
2140 if (document) {
2141 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2142 }
2143 }
2146 /*
2147 Local Variables:
2148 mode:c++
2149 c-file-style:"stroustrup"
2150 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2151 indent-tabs-mode:nil
2152 fill-column:99
2153 End:
2154 */
2155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :